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