// ArduinoJson - arduinojson.org // Copyright Benoit Blanchon 2014-2020 // MIT License #define ARDUINOJSON_USE_DOUBLE 0 #define ARDUINOJSON_ENABLE_NAN 1 #define ARDUINOJSON_ENABLE_INFINITY 1 #include #include using namespace ARDUINOJSON_NAMESPACE; template void checkFloat(const char* input, T expected) { CAPTURE(input); REQUIRE(parseFloat(input) == Approx(expected)); } template void checkNaN(const char* input) { CAPTURE(input); T result = parseFloat(input); REQUIRE(result != result); } template void checkInf(const char* input, bool negative) { CAPTURE(input); T x = parseFloat(input); if (negative) REQUIRE(x < 0); else REQUIRE(x > 0); REQUIRE(x == x); // not a NaN REQUIRE(x * 2 == x); // a property of infinity } TEST_CASE("parseFloat()") { SECTION("Float_Short_NoExponent") { checkFloat("3.14", 3.14f); checkFloat("-3.14", -3.14f); checkFloat("+3.14", +3.14f); } SECTION("Short_NoDot") { checkFloat("1E+38", 1E+38f); checkFloat("-1E+38", -1E+38f); checkFloat("+1E-38", +1E-38f); checkFloat("+1e+38", +1e+38f); checkFloat("-1e-38", -1e-38f); } SECTION("Max") { checkFloat("340.2823e+36", 3.402823e+38f); checkFloat("34.02823e+37", 3.402823e+38f); checkFloat("3.402823e+38", 3.402823e+38f); checkFloat("0.3402823e+39", 3.402823e+38f); checkFloat("0.03402823e+40", 3.402823e+38f); checkFloat("0.003402823e+41", 3.402823e+38f); } SECTION("VeryLong") { checkFloat("0.00000000000000000000000000000001", 1e-32f); checkFloat("100000000000000000000000000000000.0", 1e+32f); checkFloat( "100000000000000000000000000000000.00000000000000000000000000000", 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") { checkInf("1e39", false); checkInf("-1e39", true); checkInf("1e255", false); checkFloat("1e-255", 0.0f); } SECTION("NaN") { checkNaN("NaN"); checkNaN("nan"); } SECTION("Infinity") { checkInf("Infinity", false); checkInf("+Infinity", false); checkInf("-Infinity", true); checkInf("inf", false); checkInf("+inf", false); checkInf("-inf", true); checkInf("1e300", false); checkInf("-1e300", true); } } TEST_CASE("parseFloat()") { SECTION("Short_NoExponent") { checkFloat("3.14", 3.14); checkFloat("-3.14", -3.14); checkFloat("+3.14", +3.14); } SECTION("Short_NoDot") { checkFloat("1E+308", 1E+308); checkFloat("-1E+308", -1E+308); checkFloat("+1E-308", +1E-308); checkFloat("+1e+308", +1e+308); checkFloat("-1e-308", -1e-308); } SECTION("Max") { checkFloat(".017976931348623147e+310", 1.7976931348623147e+308); checkFloat(".17976931348623147e+309", 1.7976931348623147e+308); checkFloat("1.7976931348623147e+308", 1.7976931348623147e+308); checkFloat("17.976931348623147e+307", 1.7976931348623147e+308); checkFloat("179.76931348623147e+306", 1.7976931348623147e+308); } SECTION("Min") { checkFloat(".022250738585072014e-306", 2.2250738585072014e-308); checkFloat(".22250738585072014e-307", 2.2250738585072014e-308); checkFloat("2.2250738585072014e-308", 2.2250738585072014e-308); checkFloat("22.250738585072014e-309", 2.2250738585072014e-308); checkFloat("222.50738585072014e-310", 2.2250738585072014e-308); } SECTION("VeryLong") { checkFloat("0.00000000000000000000000000000001", 1e-32); checkFloat("100000000000000000000000000000000.0", 1e+32); checkFloat( "100000000000000000000000000000000.00000000000000000000000000000", 1e+32); } SECTION("MantissaTooLongToFit") { checkFloat("0.179769313486231571111111111111", 0.17976931348623157); checkFloat("17976931348623157.11111111111111", 17976931348623157.0); checkFloat("1797693.134862315711111111111111", 1797693.1348623157); checkFloat("-0.179769313486231571111111111111", -0.17976931348623157); checkFloat("-17976931348623157.11111111111111", -17976931348623157.0); checkFloat("-1797693.134862315711111111111111", -1797693.1348623157); } SECTION("ExponentTooBig") { checkInf("1e309", false); checkInf("-1e309", true); checkInf("1e65535", false); checkFloat("1e-65535", 0.0); } SECTION("NaN") { checkNaN("NaN"); checkNaN("nan"); } }