parseFloat.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #define ARDUINOJSON_USE_DOUBLE 0
  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 checkFloat(const char* input, float expected) {
  11. CAPTURE(input);
  12. REQUIRE(parseNumber<float>(input) == Approx(expected));
  13. }
  14. void checkFloatNaN(const char* input) {
  15. CAPTURE(input);
  16. float result = parseNumber<float>(input);
  17. REQUIRE(result != result);
  18. }
  19. void checkFloatInf(const char* input, bool negative) {
  20. CAPTURE(input);
  21. float x = parseNumber<float>(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<float>()") {
  30. SECTION("Float_Short_NoExponent") {
  31. checkFloat("3.14", 3.14f);
  32. checkFloat("-3.14", -3.14f);
  33. checkFloat("+3.14", +3.14f);
  34. }
  35. SECTION("Short_NoDot") {
  36. checkFloat("1E+38", 1E+38f);
  37. checkFloat("-1E+38", -1E+38f);
  38. checkFloat("+1E-38", +1E-38f);
  39. checkFloat("+1e+38", +1e+38f);
  40. checkFloat("-1e-38", -1e-38f);
  41. }
  42. SECTION("Max") {
  43. checkFloat("340.2823e+36", 3.402823e+38f);
  44. checkFloat("34.02823e+37", 3.402823e+38f);
  45. checkFloat("3.402823e+38", 3.402823e+38f);
  46. checkFloat("0.3402823e+39", 3.402823e+38f);
  47. checkFloat("0.03402823e+40", 3.402823e+38f);
  48. checkFloat("0.003402823e+41", 3.402823e+38f);
  49. }
  50. SECTION("VeryLong") {
  51. checkFloat("0.00000000000000000000000000000001", 1e-32f);
  52. checkFloat("100000000000000000000000000000000.0", 1e+32f);
  53. checkFloat(
  54. "100000000000000000000000000000000.00000000000000000000000000000",
  55. 1e+32f);
  56. }
  57. SECTION("MantissaTooLongToFit") {
  58. checkFloat("0.340282346638528861111111111111", 0.34028234663852886f);
  59. checkFloat("34028234663852886.11111111111111", 34028234663852886.0f);
  60. checkFloat("34028234.66385288611111111111111", 34028234.663852886f);
  61. checkFloat("-0.340282346638528861111111111111", -0.34028234663852886f);
  62. checkFloat("-34028234663852886.11111111111111", -34028234663852886.0f);
  63. checkFloat("-34028234.66385288611111111111111", -34028234.663852886f);
  64. }
  65. SECTION("ExponentTooBig") {
  66. checkFloatInf("1e39", false);
  67. checkFloatInf("-1e39", true);
  68. checkFloatInf("1e255", false);
  69. checkFloat("1e-255", 0.0f);
  70. }
  71. SECTION("NaN") {
  72. checkFloatNaN("NaN");
  73. checkFloatNaN("nan");
  74. }
  75. SECTION("Infinity") {
  76. checkFloatInf("Infinity", false);
  77. checkFloatInf("+Infinity", false);
  78. checkFloatInf("-Infinity", true);
  79. checkFloatInf("inf", false);
  80. checkFloatInf("+inf", false);
  81. checkFloatInf("-inf", true);
  82. checkFloatInf("1e300", false);
  83. checkFloatInf("-1e300", true);
  84. }
  85. }