parseDouble.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #define ARDUINOJSON_USE_DOUBLE 1
  5. #define ARDUINOJSON_ENABLE_NAN 1
  6. #define ARDUINOJSON_ENABLE_INFINITY 1
  7. #include <ArduinoJson.hpp>
  8. #include <catch.hpp>
  9. using namespace ARDUINOJSON_NAMESPACE;
  10. void checkDouble(const char* input, double expected) {
  11. CAPTURE(input);
  12. REQUIRE(parseNumber<double>(input) == Approx(expected));
  13. }
  14. void checkDoubleNaN(const char* input) {
  15. CAPTURE(input);
  16. double result = parseNumber<double>(input);
  17. REQUIRE(result != result);
  18. }
  19. void checkDoubleInf(const char* input, bool negative) {
  20. CAPTURE(input);
  21. double x = parseNumber<double>(input);
  22. if (negative)
  23. REQUIRE(x < 0);
  24. else
  25. REQUIRE(x > 0);
  26. REQUIRE(x == x); // not a NaN
  27. REQUIRE(x * 2 == x); // a property of infinity
  28. }
  29. TEST_CASE("parseNumber<double>()") {
  30. SECTION("Short_NoExponent") {
  31. checkDouble("3.14", 3.14);
  32. checkDouble("-3.14", -3.14);
  33. checkDouble("+3.14", +3.14);
  34. }
  35. SECTION("Short_NoDot") {
  36. checkDouble("1E+308", 1E+308);
  37. checkDouble("-1E+308", -1E+308);
  38. checkDouble("+1E-308", +1E-308);
  39. checkDouble("+1e+308", +1e+308);
  40. checkDouble("-1e-308", -1e-308);
  41. }
  42. SECTION("Max") {
  43. checkDouble(".017976931348623147e+310", 1.7976931348623147e+308);
  44. checkDouble(".17976931348623147e+309", 1.7976931348623147e+308);
  45. checkDouble("1.7976931348623147e+308", 1.7976931348623147e+308);
  46. checkDouble("17.976931348623147e+307", 1.7976931348623147e+308);
  47. checkDouble("179.76931348623147e+306", 1.7976931348623147e+308);
  48. }
  49. SECTION("Min") {
  50. checkDouble(".022250738585072014e-306", 2.2250738585072014e-308);
  51. checkDouble(".22250738585072014e-307", 2.2250738585072014e-308);
  52. checkDouble("2.2250738585072014e-308", 2.2250738585072014e-308);
  53. checkDouble("22.250738585072014e-309", 2.2250738585072014e-308);
  54. checkDouble("222.50738585072014e-310", 2.2250738585072014e-308);
  55. }
  56. SECTION("VeryLong") {
  57. checkDouble("0.00000000000000000000000000000001", 1e-32);
  58. checkDouble("100000000000000000000000000000000.0", 1e+32);
  59. checkDouble(
  60. "100000000000000000000000000000000.00000000000000000000000000000",
  61. 1e+32);
  62. }
  63. SECTION("MantissaTooLongToFit") {
  64. checkDouble("0.179769313486231571111111111111", 0.17976931348623157);
  65. checkDouble("17976931348623157.11111111111111", 17976931348623157.0);
  66. checkDouble("1797693.134862315711111111111111", 1797693.1348623157);
  67. checkDouble("-0.179769313486231571111111111111", -0.17976931348623157);
  68. checkDouble("-17976931348623157.11111111111111", -17976931348623157.0);
  69. checkDouble("-1797693.134862315711111111111111", -1797693.1348623157);
  70. }
  71. SECTION("ExponentTooBig") {
  72. checkDoubleInf("1e309", false);
  73. checkDoubleInf("-1e309", true);
  74. checkDoubleInf("1e65535", false);
  75. checkDouble("1e-65535", 0.0);
  76. }
  77. SECTION("NaN") {
  78. checkDoubleNaN("NaN");
  79. checkDoubleNaN("nan");
  80. }
  81. }