parseFloat.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  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 checkFloat(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. checkFloat<float>("3.14", 3.14f);
  35. checkFloat<float>("-3.14", -3.14f);
  36. checkFloat<float>("+3.14", +3.14f);
  37. }
  38. SECTION("Short_NoDot") {
  39. checkFloat<float>("1E+38", 1E+38f);
  40. checkFloat<float>("-1E+38", -1E+38f);
  41. checkFloat<float>("+1E-38", +1E-38f);
  42. checkFloat<float>("+1e+38", +1e+38f);
  43. checkFloat<float>("-1e-38", -1e-38f);
  44. }
  45. SECTION("Max") {
  46. checkFloat<float>("340.2823e+36", 3.402823e+38f);
  47. checkFloat<float>("34.02823e+37", 3.402823e+38f);
  48. checkFloat<float>("3.402823e+38", 3.402823e+38f);
  49. checkFloat<float>("0.3402823e+39", 3.402823e+38f);
  50. checkFloat<float>("0.03402823e+40", 3.402823e+38f);
  51. checkFloat<float>("0.003402823e+41", 3.402823e+38f);
  52. }
  53. SECTION("VeryLong") {
  54. checkFloat<float>("0.00000000000000000000000000000001", 1e-32f);
  55. checkFloat<float>("100000000000000000000000000000000.0", 1e+32f);
  56. checkFloat<float>(
  57. "100000000000000000000000000000000.00000000000000000000000000000",
  58. 1e+32f);
  59. }
  60. SECTION("MantissaTooLongToFit") {
  61. checkFloat<float>("0.340282346638528861111111111111", 0.34028234663852886f);
  62. checkFloat<float>("34028234663852886.11111111111111", 34028234663852886.0f);
  63. checkFloat<float>("34028234.66385288611111111111111", 34028234.663852886f);
  64. checkFloat<float>("-0.340282346638528861111111111111",
  65. -0.34028234663852886f);
  66. checkFloat<float>("-34028234663852886.11111111111111",
  67. -34028234663852886.0f);
  68. checkFloat<float>("-34028234.66385288611111111111111",
  69. -34028234.663852886f);
  70. }
  71. SECTION("ExponentTooBig") {
  72. checkInf<float>("1e39", false);
  73. checkInf<float>("-1e39", true);
  74. checkInf<float>("1e255", false);
  75. checkFloat<float>("1e-255", 0.0f);
  76. }
  77. SECTION("NaN") {
  78. checkNaN<float>("NaN");
  79. checkNaN<float>("nan");
  80. }
  81. SECTION("Infinity") {
  82. checkInf<float>("Infinity", false);
  83. checkInf<float>("+Infinity", false);
  84. checkInf<float>("-Infinity", true);
  85. checkInf<float>("inf", false);
  86. checkInf<float>("+inf", false);
  87. checkInf<float>("-inf", true);
  88. checkInf<float>("1e300", false);
  89. checkInf<float>("-1e300", true);
  90. }
  91. }
  92. TEST_CASE("parseFloat<double>()") {
  93. SECTION("Short_NoExponent") {
  94. checkFloat<double>("3.14", 3.14);
  95. checkFloat<double>("-3.14", -3.14);
  96. checkFloat<double>("+3.14", +3.14);
  97. }
  98. SECTION("Short_NoDot") {
  99. checkFloat<double>("1E+308", 1E+308);
  100. checkFloat<double>("-1E+308", -1E+308);
  101. checkFloat<double>("+1E-308", +1E-308);
  102. checkFloat<double>("+1e+308", +1e+308);
  103. checkFloat<double>("-1e-308", -1e-308);
  104. }
  105. SECTION("Max") {
  106. checkFloat<double>(".017976931348623147e+310", 1.7976931348623147e+308);
  107. checkFloat<double>(".17976931348623147e+309", 1.7976931348623147e+308);
  108. checkFloat<double>("1.7976931348623147e+308", 1.7976931348623147e+308);
  109. checkFloat<double>("17.976931348623147e+307", 1.7976931348623147e+308);
  110. checkFloat<double>("179.76931348623147e+306", 1.7976931348623147e+308);
  111. }
  112. SECTION("Min") {
  113. checkFloat<double>(".022250738585072014e-306", 2.2250738585072014e-308);
  114. checkFloat<double>(".22250738585072014e-307", 2.2250738585072014e-308);
  115. checkFloat<double>("2.2250738585072014e-308", 2.2250738585072014e-308);
  116. checkFloat<double>("22.250738585072014e-309", 2.2250738585072014e-308);
  117. checkFloat<double>("222.50738585072014e-310", 2.2250738585072014e-308);
  118. }
  119. SECTION("VeryLong") {
  120. checkFloat<double>("0.00000000000000000000000000000001", 1e-32);
  121. checkFloat<double>("100000000000000000000000000000000.0", 1e+32);
  122. checkFloat<double>(
  123. "100000000000000000000000000000000.00000000000000000000000000000",
  124. 1e+32);
  125. }
  126. SECTION("MantissaTooLongToFit") {
  127. checkFloat<double>("0.179769313486231571111111111111", 0.17976931348623157);
  128. checkFloat<double>("17976931348623157.11111111111111", 17976931348623157.0);
  129. checkFloat<double>("1797693.134862315711111111111111", 1797693.1348623157);
  130. checkFloat<double>("-0.179769313486231571111111111111",
  131. -0.17976931348623157);
  132. checkFloat<double>("-17976931348623157.11111111111111",
  133. -17976931348623157.0);
  134. checkFloat<double>("-1797693.134862315711111111111111",
  135. -1797693.1348623157);
  136. }
  137. SECTION("ExponentTooBig") {
  138. checkInf<double>("1e309", false);
  139. checkInf<double>("-1e309", true);
  140. checkInf<double>("1e65535", false);
  141. checkFloat<double>("1e-65535", 0.0);
  142. }
  143. SECTION("NaN") {
  144. checkNaN<double>("NaN");
  145. checkNaN<double>("nan");
  146. }
  147. }