mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-30 18:57:32 +02:00
Move some numbers tests to use_double_0.cpp
This commit is contained in:
@ -3,7 +3,21 @@
|
|||||||
|
|
||||||
#include <catch.hpp>
|
#include <catch.hpp>
|
||||||
|
|
||||||
|
namespace my {
|
||||||
|
using ArduinoJson::detail::isinf;
|
||||||
|
} // namespace my
|
||||||
|
|
||||||
|
void checkFloat(const char* input, float expected) {
|
||||||
|
using ArduinoJson::detail::NumberType;
|
||||||
|
using ArduinoJson::detail::parseNumber;
|
||||||
|
CAPTURE(input);
|
||||||
|
auto result = parseNumber(input);
|
||||||
|
REQUIRE(result.type() == NumberType::Float);
|
||||||
|
REQUIRE(result.asFloat() == Approx(expected));
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("ARDUINOJSON_USE_DOUBLE == 0") {
|
TEST_CASE("ARDUINOJSON_USE_DOUBLE == 0") {
|
||||||
|
SECTION("serializeJson()") {
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
JsonObject root = doc.to<JsonObject>();
|
JsonObject root = doc.to<JsonObject>();
|
||||||
|
|
||||||
@ -14,4 +28,40 @@ TEST_CASE("ARDUINOJSON_USE_DOUBLE == 0") {
|
|||||||
serializeJson(doc, json);
|
serializeJson(doc, json);
|
||||||
|
|
||||||
REQUIRE(json == "{\"pi\":3.14,\"e\":2.72}");
|
REQUIRE(json == "{\"pi\":3.14,\"e\":2.72}");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("parseNumber()") {
|
||||||
|
using ArduinoJson::detail::NumberType;
|
||||||
|
using ArduinoJson::detail::parseNumber;
|
||||||
|
|
||||||
|
SECTION("Large positive number") {
|
||||||
|
auto result = parseNumber("1e300");
|
||||||
|
REQUIRE(result.type() == NumberType::Float);
|
||||||
|
REQUIRE(result.asFloat() > 0);
|
||||||
|
REQUIRE(my::isinf(result.asFloat()));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Large negative number") {
|
||||||
|
auto result = parseNumber("-1e300");
|
||||||
|
REQUIRE(result.type() == NumberType::Float);
|
||||||
|
REQUIRE(result.asFloat() < 0);
|
||||||
|
REQUIRE(my::isinf(result.asFloat()));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Too small to be represented") {
|
||||||
|
auto result = parseNumber("1e-300");
|
||||||
|
REQUIRE(result.type() == NumberType::Float);
|
||||||
|
REQUIRE(result.asFloat() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("MantissaTooLongToFit") {
|
||||||
|
checkFloat("0.340282346638528861111111111111", 0.34028234663852886f);
|
||||||
|
checkFloat("34028234663852886.11111111111111", 34028234663852886.0f);
|
||||||
|
checkFloat("34028234.66385288611111111111111", 34028234.663852886f);
|
||||||
|
|
||||||
|
checkFloat("-0.340282346638528861111111111111", -0.34028234663852886f);
|
||||||
|
checkFloat("-34028234663852886.11111111111111", -34028234663852886.0f);
|
||||||
|
checkFloat("-34028234.66385288611111111111111", -34028234.663852886f);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
// Copyright © 2014-2024, Benoit BLANCHON
|
// Copyright © 2014-2024, Benoit BLANCHON
|
||||||
// MIT License
|
// MIT License
|
||||||
|
|
||||||
#define ARDUINOJSON_USE_DOUBLE 0
|
|
||||||
#define ARDUINOJSON_ENABLE_NAN 1
|
#define ARDUINOJSON_ENABLE_NAN 1
|
||||||
#define ARDUINOJSON_ENABLE_INFINITY 1
|
#define ARDUINOJSON_ENABLE_INFINITY 1
|
||||||
|
|
||||||
@ -13,7 +12,9 @@ using namespace ArduinoJson::detail;
|
|||||||
|
|
||||||
void checkFloat(const char* input, float expected) {
|
void checkFloat(const char* input, float expected) {
|
||||||
CAPTURE(input);
|
CAPTURE(input);
|
||||||
REQUIRE(parseNumber<float>(input) == Approx(expected));
|
auto result = parseNumber(input);
|
||||||
|
REQUIRE(result.type() == NumberType::Float);
|
||||||
|
REQUIRE(result.asFloat() == Approx(expected));
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkFloatNaN(const char* input) {
|
void checkFloatNaN(const char* input) {
|
||||||
@ -65,23 +66,6 @@ TEST_CASE("parseNumber<float>()") {
|
|||||||
1e+32f);
|
1e+32f);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("MantissaTooLongToFit") {
|
|
||||||
checkFloat("0.340282346638528861111111111111", 0.34028234663852886f);
|
|
||||||
checkFloat("34028234663852886.11111111111111", 34028234663852886.0f);
|
|
||||||
checkFloat("34028234.66385288611111111111111", 34028234.663852886f);
|
|
||||||
|
|
||||||
checkFloat("-0.340282346638528861111111111111", -0.34028234663852886f);
|
|
||||||
checkFloat("-34028234663852886.11111111111111", -34028234663852886.0f);
|
|
||||||
checkFloat("-34028234.66385288611111111111111", -34028234.663852886f);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("ExponentTooBig") {
|
|
||||||
checkFloatInf("1e39", false);
|
|
||||||
checkFloatInf("-1e39", true);
|
|
||||||
checkFloatInf("1e255", false);
|
|
||||||
checkFloat("1e-255", 0.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("NaN") {
|
SECTION("NaN") {
|
||||||
checkFloatNaN("NaN");
|
checkFloatNaN("NaN");
|
||||||
checkFloatNaN("nan");
|
checkFloatNaN("nan");
|
||||||
@ -94,8 +78,5 @@ TEST_CASE("parseNumber<float>()") {
|
|||||||
checkFloatInf("inf", false);
|
checkFloatInf("inf", false);
|
||||||
checkFloatInf("+inf", false);
|
checkFloatInf("+inf", false);
|
||||||
checkFloatInf("-inf", true);
|
checkFloatInf("-inf", true);
|
||||||
|
|
||||||
checkFloatInf("1e300", false);
|
|
||||||
checkFloatInf("-1e300", true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user