writeFloat.cpp 2.9 KB

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