parseFloat.cpp 4.9 KB

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