writeFloat.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #include <catch.hpp>
  5. #include <limits>
  6. #include <string>
  7. #define ARDUINOJSON_ENABLE_NAN 1
  8. #define ARDUINOJSON_ENABLE_INFINITY 1
  9. #include <ArduinoJson/Json/TextFormatter.hpp>
  10. #include <ArduinoJson/Serialization/Writer.hpp>
  11. using namespace ArduinoJson::detail;
  12. template <typename TFloat>
  13. static std::string toString(TFloat input) {
  14. std::string output;
  15. Writer<std::string> sb(output);
  16. TextFormatter<Writer<std::string>> writer(sb);
  17. writer.writeFloat(input);
  18. return output;
  19. }
  20. TEST_CASE("TextFormatter::writeFloat(double)") {
  21. SECTION("Pi") {
  22. REQUIRE(toString(3.14159265359) == "3.14159265");
  23. }
  24. SECTION("Signaling NaN") {
  25. double nan = std::numeric_limits<double>::signaling_NaN();
  26. REQUIRE(toString(nan) == "NaN");
  27. }
  28. SECTION("Quiet NaN") {
  29. double nan = std::numeric_limits<double>::quiet_NaN();
  30. REQUIRE(toString(nan) == "NaN");
  31. }
  32. SECTION("Infinity") {
  33. double inf = std::numeric_limits<double>::infinity();
  34. REQUIRE(toString(inf) == "Infinity");
  35. REQUIRE(toString(-inf) == "-Infinity");
  36. }
  37. SECTION("Zero") {
  38. REQUIRE(toString(0.0) == "0");
  39. REQUIRE(toString(-0.0) == "0");
  40. }
  41. SECTION("Espilon") {
  42. REQUIRE(toString(2.2250738585072014E-308) == "2.22507386e-308");
  43. REQUIRE(toString(-2.2250738585072014E-308) == "-2.22507386e-308");
  44. }
  45. SECTION("Max double") {
  46. REQUIRE(toString(1.7976931348623157E+308) == "1.79769313e308");
  47. REQUIRE(toString(-1.7976931348623157E+308) == "-1.79769313e308");
  48. }
  49. SECTION("Big exponent") {
  50. REQUIRE(toString(1e255) == "1e255");
  51. REQUIRE(toString(1e-255) == "1e-255");
  52. }
  53. SECTION("Exponentation when <= 1e-5") {
  54. REQUIRE(toString(1e-4) == "0.0001");
  55. REQUIRE(toString(1e-5) == "1e-5");
  56. REQUIRE(toString(-1e-4) == "-0.0001");
  57. REQUIRE(toString(-1e-5) == "-1e-5");
  58. }
  59. SECTION("Exponentation when >= 1e7") {
  60. REQUIRE(toString(9999999.99) == "9999999.99");
  61. REQUIRE(toString(10000000.0) == "1e7");
  62. REQUIRE(toString(-9999999.99) == "-9999999.99");
  63. REQUIRE(toString(-10000000.0) == "-1e7");
  64. }
  65. SECTION("Rounding when too many decimals") {
  66. REQUIRE(toString(0.000099999999999) == "0.0001");
  67. REQUIRE(toString(0.0000099999999999) == "1e-5");
  68. REQUIRE(toString(0.9999999996) == "1");
  69. }
  70. SECTION("9 decimal places") {
  71. REQUIRE(toString(0.10000001) == "0.10000001");
  72. REQUIRE(toString(0.99999999) == "0.99999999");
  73. REQUIRE(toString(9.00000001) == "9.00000001");
  74. REQUIRE(toString(9.99999999) == "9.99999999");
  75. }
  76. SECTION("9 decimal places") {
  77. REQUIRE(toString(0.100000001) == "0.100000001");
  78. REQUIRE(toString(0.999999999) == "0.999999999");
  79. REQUIRE(toString(9.000000001) == "9");
  80. REQUIRE(toString(9.999999999) == "10");
  81. }
  82. SECTION("10 decimal places") {
  83. REQUIRE(toString(0.1000000001) == "0.1");
  84. REQUIRE(toString(0.9999999999) == "1");
  85. REQUIRE(toString(9.0000000001) == "9");
  86. REQUIRE(toString(9.9999999999) == "10");
  87. }
  88. }
  89. TEST_CASE("TextFormatter::writeFloat(float)") {
  90. SECTION("Pi") {
  91. REQUIRE(toString(3.14159265359f) == "3.141593");
  92. }
  93. SECTION("999.9") { // issue #543
  94. REQUIRE(toString(999.9f) == "999.9");
  95. }
  96. SECTION("24.3") { // # issue #588
  97. REQUIRE(toString(24.3f) == "24.3");
  98. }
  99. }