number.cpp 3.6 KB

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