parseFloat.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  3. // MIT License
  4. #define ARDUINOJSON_USE_DOUBLE 0
  5. #define ARDUINOJSON_ENABLE_NAN 1
  6. #define ARDUINOJSON_ENABLE_INFINITY 1
  7. #include <ArduinoJson/Numbers/parseFloat.hpp>
  8. #include <catch.hpp>
  9. using namespace ARDUINOJSON_NAMESPACE;
  10. template <typename T>
  11. void check(const char* input, T expected) {
  12. CAPTURE(input);
  13. REQUIRE(parseFloat<T>(input) == Approx(expected));
  14. }
  15. template <typename T>
  16. void checkNaN(const char* input) {
  17. CAPTURE(input);
  18. T result = parseFloat<T>(input);
  19. REQUIRE(result != result);
  20. }
  21. template <typename T>
  22. void checkInf(const char* input, bool negative) {
  23. CAPTURE(input);
  24. T x = parseFloat<T>(input);
  25. if (negative)
  26. REQUIRE(x < 0);
  27. else
  28. REQUIRE(x > 0);
  29. REQUIRE(x == x); // not a NaN
  30. REQUIRE(x * 2 == x); // a property of infinity
  31. }
  32. TEST_CASE("parseFloat<float>()") {
  33. SECTION("Float_Short_NoExponent") {
  34. check<float>("3.14", 3.14f);
  35. check<float>("-3.14", -3.14f);
  36. check<float>("+3.14", +3.14f);
  37. }
  38. SECTION("Short_NoDot") {
  39. check<float>("1E+38", 1E+38f);
  40. check<float>("-1E+38", -1E+38f);
  41. check<float>("+1E-38", +1E-38f);
  42. check<float>("+1e+38", +1e+38f);
  43. check<float>("-1e-38", -1e-38f);
  44. }
  45. SECTION("Max") {
  46. check<float>("340.2823e+36", 3.402823e+38f);
  47. check<float>("34.02823e+37", 3.402823e+38f);
  48. check<float>("3.402823e+38", 3.402823e+38f);
  49. check<float>("0.3402823e+39", 3.402823e+38f);
  50. check<float>("0.03402823e+40", 3.402823e+38f);
  51. check<float>("0.003402823e+41", 3.402823e+38f);
  52. }
  53. SECTION("VeryLong") {
  54. check<float>("0.00000000000000000000000000000001", 1e-32f);
  55. check<float>("100000000000000000000000000000000.0", 1e+32f);
  56. check<float>(
  57. "100000000000000000000000000000000.00000000000000000000000000000",
  58. 1e+32f);
  59. }
  60. SECTION("MantissaTooLongToFit") {
  61. check<float>("0.340282346638528861111111111111", 0.34028234663852886f);
  62. check<float>("34028234663852886.11111111111111", 34028234663852886.0f);
  63. check<float>("34028234.66385288611111111111111", 34028234.663852886f);
  64. check<float>("-0.340282346638528861111111111111", -0.34028234663852886f);
  65. check<float>("-34028234663852886.11111111111111", -34028234663852886.0f);
  66. check<float>("-34028234.66385288611111111111111", -34028234.663852886f);
  67. }
  68. SECTION("ExponentTooBig") {
  69. checkInf<float>("1e39", false);
  70. checkInf<float>("-1e39", true);
  71. checkInf<float>("1e255", false);
  72. check<float>("1e-255", 0.0f);
  73. }
  74. SECTION("NaN") {
  75. checkNaN<float>("NaN");
  76. checkNaN<float>("nan");
  77. }
  78. SECTION("Infinity") {
  79. checkInf<float>("Infinity", false);
  80. checkInf<float>("+Infinity", false);
  81. checkInf<float>("-Infinity", true);
  82. checkInf<float>("inf", false);
  83. checkInf<float>("+inf", false);
  84. checkInf<float>("-inf", true);
  85. checkInf<float>("1e300", false);
  86. checkInf<float>("-1e300", true);
  87. }
  88. }
  89. TEST_CASE("parseFloat<double>()") {
  90. SECTION("Short_NoExponent") {
  91. check<double>("3.14", 3.14);
  92. check<double>("-3.14", -3.14);
  93. check<double>("+3.14", +3.14);
  94. }
  95. SECTION("Short_NoDot") {
  96. check<double>("1E+308", 1E+308);
  97. check<double>("-1E+308", -1E+308);
  98. check<double>("+1E-308", +1E-308);
  99. check<double>("+1e+308", +1e+308);
  100. check<double>("-1e-308", -1e-308);
  101. }
  102. SECTION("Max") {
  103. check<double>(".017976931348623147e+310", 1.7976931348623147e+308);
  104. check<double>(".17976931348623147e+309", 1.7976931348623147e+308);
  105. check<double>("1.7976931348623147e+308", 1.7976931348623147e+308);
  106. check<double>("17.976931348623147e+307", 1.7976931348623147e+308);
  107. check<double>("179.76931348623147e+306", 1.7976931348623147e+308);
  108. }
  109. SECTION("Min") {
  110. check<double>(".022250738585072014e-306", 2.2250738585072014e-308);
  111. check<double>(".22250738585072014e-307", 2.2250738585072014e-308);
  112. check<double>("2.2250738585072014e-308", 2.2250738585072014e-308);
  113. check<double>("22.250738585072014e-309", 2.2250738585072014e-308);
  114. check<double>("222.50738585072014e-310", 2.2250738585072014e-308);
  115. }
  116. SECTION("VeryLong") {
  117. check<double>("0.00000000000000000000000000000001", 1e-32);
  118. check<double>("100000000000000000000000000000000.0", 1e+32);
  119. check<double>(
  120. "100000000000000000000000000000000.00000000000000000000000000000",
  121. 1e+32);
  122. }
  123. SECTION("MantissaTooLongToFit") {
  124. check<double>("0.179769313486231571111111111111", 0.17976931348623157);
  125. check<double>("17976931348623157.11111111111111", 17976931348623157.0);
  126. check<double>("1797693.134862315711111111111111", 1797693.1348623157);
  127. check<double>("-0.179769313486231571111111111111", -0.17976931348623157);
  128. check<double>("-17976931348623157.11111111111111", -17976931348623157.0);
  129. check<double>("-1797693.134862315711111111111111", -1797693.1348623157);
  130. }
  131. SECTION("ExponentTooBig") {
  132. checkInf<double>("1e309", false);
  133. checkInf<double>("-1e309", true);
  134. checkInf<double>("1e65535", false);
  135. check<double>("1e-65535", 0.0);
  136. }
  137. SECTION("NaN") {
  138. checkNaN<double>("NaN");
  139. checkNaN<double>("nan");
  140. }
  141. }