use_double_0.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #define ARDUINOJSON_USE_DOUBLE 0
  2. #include <ArduinoJson.h>
  3. #include <catch.hpp>
  4. namespace my {
  5. using ArduinoJson::detail::isinf;
  6. } // namespace my
  7. void checkFloat(const char* input, float expected) {
  8. using ArduinoJson::detail::NumberType;
  9. using ArduinoJson::detail::parseNumber;
  10. CAPTURE(input);
  11. auto result = parseNumber(input);
  12. REQUIRE(result.type() == NumberType::Float);
  13. REQUIRE(result.asFloat() == Approx(expected));
  14. }
  15. TEST_CASE("ARDUINOJSON_USE_DOUBLE == 0") {
  16. SECTION("serializeJson()") {
  17. JsonDocument doc;
  18. JsonObject root = doc.to<JsonObject>();
  19. root["pi"] = 3.14;
  20. root["e"] = 2.72;
  21. std::string json;
  22. serializeJson(doc, json);
  23. REQUIRE(json == "{\"pi\":3.14,\"e\":2.72}");
  24. }
  25. SECTION("parseNumber()") {
  26. using ArduinoJson::detail::NumberType;
  27. using ArduinoJson::detail::parseNumber;
  28. SECTION("Large positive number") {
  29. auto result = parseNumber("1e300");
  30. REQUIRE(result.type() == NumberType::Float);
  31. REQUIRE(result.asFloat() > 0);
  32. REQUIRE(my::isinf(result.asFloat()));
  33. }
  34. SECTION("Large negative number") {
  35. auto result = parseNumber("-1e300");
  36. REQUIRE(result.type() == NumberType::Float);
  37. REQUIRE(result.asFloat() < 0);
  38. REQUIRE(my::isinf(result.asFloat()));
  39. }
  40. SECTION("Too small to be represented") {
  41. auto result = parseNumber("1e-300");
  42. REQUIRE(result.type() == NumberType::Float);
  43. REQUIRE(result.asFloat() == 0);
  44. }
  45. SECTION("MantissaTooLongToFit") {
  46. checkFloat("0.340282346638528861111111111111", 0.34028234663852886f);
  47. checkFloat("34028234663852886.11111111111111", 34028234663852886.0f);
  48. checkFloat("34028234.66385288611111111111111", 34028234.663852886f);
  49. checkFloat("-0.340282346638528861111111111111", -0.34028234663852886f);
  50. checkFloat("-34028234663852886.11111111111111", -34028234663852886.0f);
  51. checkFloat("-34028234.66385288611111111111111", -34028234.663852886f);
  52. }
  53. }
  54. }