parseNumber.cpp 1.4 KB

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