parseNumber.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2023, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.hpp>
  5. #include <catch.hpp>
  6. using namespace ArduinoJson;
  7. using namespace ArduinoJson::detail;
  8. TEST_CASE("Test unsigned integer overflow") {
  9. VariantData first, second;
  10. // Avoids MSVC warning C4127 (conditional expression is constant)
  11. size_t integerSize = sizeof(JsonInteger);
  12. if (integerSize == 8) {
  13. parseNumber("18446744073709551615", first);
  14. parseNumber("18446744073709551616", second);
  15. } else {
  16. parseNumber("4294967295", first);
  17. parseNumber("4294967296", second);
  18. }
  19. REQUIRE(first.type() == uint8_t(VALUE_IS_UNSIGNED_INTEGER));
  20. REQUIRE(second.type() == uint8_t(VALUE_IS_FLOAT));
  21. }
  22. TEST_CASE("Test signed integer overflow") {
  23. VariantData first, second;
  24. // Avoids MSVC warning C4127 (conditional expression is constant)
  25. size_t integerSize = sizeof(JsonInteger);
  26. if (integerSize == 8) {
  27. parseNumber("-9223372036854775808", first);
  28. parseNumber("-9223372036854775809", second);
  29. } else {
  30. parseNumber("-2147483648", first);
  31. parseNumber("-2147483649", second);
  32. }
  33. REQUIRE(first.type() == uint8_t(VALUE_IS_SIGNED_INTEGER));
  34. REQUIRE(second.type() == uint8_t(VALUE_IS_FLOAT));
  35. }
  36. TEST_CASE("Invalid value") {
  37. VariantData result;
  38. parseNumber("6a3", result);
  39. REQUIRE(result.type() == uint8_t(VALUE_IS_NULL));
  40. }