writeFloat.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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_NAMESPACE;
  12. template <typename TFloat>
  13. void check(TFloat input, const std::string& expected) {
  14. std::string output;
  15. Writer<std::string> sb(output);
  16. TextFormatter<Writer<std::string> > writer(sb);
  17. writer.writeFloat(input);
  18. REQUIRE(writer.bytesWritten() == output.size());
  19. CHECK(expected == output);
  20. }
  21. TEST_CASE("TextFormatter::writeFloat(double)") {
  22. SECTION("Pi") {
  23. check<double>(3.14159265359, "3.141592654");
  24. }
  25. SECTION("Signaling NaN") {
  26. double nan = std::numeric_limits<double>::signaling_NaN();
  27. check<double>(nan, "NaN");
  28. }
  29. SECTION("Quiet NaN") {
  30. double nan = std::numeric_limits<double>::quiet_NaN();
  31. check<double>(nan, "NaN");
  32. }
  33. SECTION("Infinity") {
  34. double inf = std::numeric_limits<double>::infinity();
  35. check<double>(inf, "Infinity");
  36. check<double>(-inf, "-Infinity");
  37. }
  38. SECTION("Zero") {
  39. check<double>(0.0, "0");
  40. check<double>(-0.0, "0");
  41. }
  42. SECTION("Espilon") {
  43. check<double>(2.2250738585072014E-308, "2.225073859e-308");
  44. check<double>(-2.2250738585072014E-308, "-2.225073859e-308");
  45. }
  46. SECTION("Max double") {
  47. check<double>(1.7976931348623157E+308, "1.797693135e308");
  48. check<double>(-1.7976931348623157E+308, "-1.797693135e308");
  49. }
  50. SECTION("Big exponent") {
  51. // this test increases coverage of normalize()
  52. check<double>(1e255, "1e255");
  53. check<double>(1e-255, "1e-255");
  54. }
  55. SECTION("Exponentation when <= 1e-5") {
  56. check<double>(1e-4, "0.0001");
  57. check<double>(1e-5, "1e-5");
  58. check<double>(-1e-4, "-0.0001");
  59. check<double>(-1e-5, "-1e-5");
  60. }
  61. SECTION("Exponentation when >= 1e7") {
  62. check<double>(9999999.999, "9999999.999");
  63. check<double>(10000000.0, "1e7");
  64. check<double>(-9999999.999, "-9999999.999");
  65. check<double>(-10000000.0, "-1e7");
  66. }
  67. SECTION("Rounding when too many decimals") {
  68. check<double>(0.000099999999999, "0.0001");
  69. check<double>(0.0000099999999999, "1e-5");
  70. check<double>(0.9999999996, "1");
  71. }
  72. SECTION("9 decimal places") {
  73. check<double>(0.100000001, "0.100000001");
  74. check<double>(0.999999999, "0.999999999");
  75. check<double>(9.000000001, "9.000000001");
  76. check<double>(9.999999999, "9.999999999");
  77. }
  78. SECTION("10 decimal places") {
  79. check<double>(0.1000000001, "0.1");
  80. check<double>(0.9999999999, "1");
  81. check<double>(9.0000000001, "9");
  82. check<double>(9.9999999999, "10");
  83. }
  84. }
  85. TEST_CASE("TextFormatter::writeFloat(float)") {
  86. SECTION("Pi") {
  87. check<float>(3.14159265359f, "3.141593");
  88. }
  89. SECTION("999.9") { // issue #543
  90. check<float>(999.9f, "999.9");
  91. }
  92. SECTION("24.3") { // # issue #588
  93. check<float>(24.3f, "24.3");
  94. }
  95. }