number.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #define ARDUINOJSON_USE_LONG_LONG 0
  5. #define ARDUINOJSON_ENABLE_NAN 1
  6. #define ARDUINOJSON_ENABLE_INFINITY 1
  7. #include <ArduinoJson.h>
  8. #include <limits.h>
  9. #include <catch.hpp>
  10. namespace my {
  11. using ARDUINOJSON_NAMESPACE::isinf;
  12. using ARDUINOJSON_NAMESPACE::isnan;
  13. } // namespace my
  14. TEST_CASE("deserialize an integer") {
  15. DynamicJsonDocument doc(4096);
  16. SECTION("Integer") {
  17. SECTION("0") {
  18. DeserializationError err = deserializeJson(doc, "0");
  19. REQUIRE(err == DeserializationError::Ok);
  20. REQUIRE(doc.is<int>() == true);
  21. REQUIRE(doc.as<int>() == 0);
  22. REQUIRE(doc.as<std::string>() == "0"); // issue #808
  23. }
  24. SECTION("Negative") {
  25. DeserializationError err = deserializeJson(doc, "-42");
  26. REQUIRE(err == DeserializationError::Ok);
  27. REQUIRE(doc.is<int>());
  28. REQUIRE_FALSE(doc.is<bool>());
  29. REQUIRE(doc.as<int>() == -42);
  30. }
  31. #if LONG_MAX == 2147483647
  32. SECTION("LONG_MAX") {
  33. DeserializationError err = deserializeJson(doc, "2147483647");
  34. REQUIRE(err == DeserializationError::Ok);
  35. REQUIRE(doc.is<long>() == true);
  36. REQUIRE(doc.as<long>() == LONG_MAX);
  37. }
  38. SECTION("LONG_MAX + 1") {
  39. DeserializationError err = deserializeJson(doc, "2147483648");
  40. CAPTURE(LONG_MIN);
  41. REQUIRE(err == DeserializationError::Ok);
  42. REQUIRE(doc.is<long>() == false);
  43. REQUIRE(doc.is<float>() == true);
  44. }
  45. #endif
  46. #if LONG_MIN == -2147483648
  47. SECTION("LONG_MIN") {
  48. DeserializationError err = deserializeJson(doc, "-2147483648");
  49. REQUIRE(err == DeserializationError::Ok);
  50. REQUIRE(doc.is<long>() == true);
  51. REQUIRE(doc.as<long>() == LONG_MIN);
  52. }
  53. SECTION("LONG_MIN - 1") {
  54. DeserializationError err = deserializeJson(doc, "-2147483649");
  55. REQUIRE(err == DeserializationError::Ok);
  56. REQUIRE(doc.is<long>() == false);
  57. REQUIRE(doc.is<float>() == true);
  58. }
  59. #endif
  60. #if ULONG_MAX == 4294967295
  61. SECTION("ULONG_MAX") {
  62. DeserializationError err = deserializeJson(doc, "4294967295");
  63. REQUIRE(err == DeserializationError::Ok);
  64. REQUIRE(doc.is<unsigned long>() == true);
  65. REQUIRE(doc.as<unsigned long>() == ULONG_MAX);
  66. REQUIRE(doc.is<long>() == false);
  67. }
  68. SECTION("ULONG_MAX + 1") {
  69. DeserializationError err = deserializeJson(doc, "4294967296");
  70. REQUIRE(err == DeserializationError::Ok);
  71. REQUIRE(doc.is<unsigned long>() == false);
  72. REQUIRE(doc.is<float>() == true);
  73. }
  74. #endif
  75. }
  76. SECTION("Floats") {
  77. SECTION("Double") {
  78. DeserializationError err = deserializeJson(doc, "-1.23e+4");
  79. REQUIRE(err == DeserializationError::Ok);
  80. REQUIRE_FALSE(doc.is<int>());
  81. REQUIRE(doc.is<double>());
  82. REQUIRE(doc.as<double>() == Approx(-1.23e+4));
  83. }
  84. SECTION("NaN") {
  85. DeserializationError err = deserializeJson(doc, "NaN");
  86. REQUIRE(err == DeserializationError::Ok);
  87. REQUIRE(doc.is<float>() == true);
  88. REQUIRE(my::isnan(doc.as<float>()));
  89. }
  90. SECTION("Infinity") {
  91. DeserializationError err = deserializeJson(doc, "Infinity");
  92. REQUIRE(err == DeserializationError::Ok);
  93. REQUIRE(doc.is<float>() == true);
  94. REQUIRE(my::isinf(doc.as<float>()));
  95. }
  96. SECTION("+Infinity") {
  97. DeserializationError err = deserializeJson(doc, "+Infinity");
  98. REQUIRE(err == DeserializationError::Ok);
  99. REQUIRE(doc.is<float>() == true);
  100. REQUIRE(my::isinf(doc.as<float>()));
  101. }
  102. SECTION("-Infinity") {
  103. DeserializationError err = deserializeJson(doc, "-Infinity");
  104. REQUIRE(err == DeserializationError::Ok);
  105. REQUIRE(doc.is<float>() == true);
  106. REQUIRE(my::isinf(doc.as<float>()));
  107. }
  108. }
  109. }