mirror of
				https://github.com/eledio-devices/thirdparty-ArduinoJson.git
				synced 2025-10-31 16:14:11 +01:00 
			
		
		
		
	Simplified the implementation of parseNumber()
This commit is contained in:
		| @@ -4,6 +4,7 @@ | ||||
|  | ||||
| add_executable(NumbersTests  | ||||
| 	parseFloat.cpp | ||||
| 	parseDouble.cpp | ||||
| 	parseInteger.cpp | ||||
| 	parseNumber.cpp | ||||
| ) | ||||
|   | ||||
							
								
								
									
										97
									
								
								extras/tests/Numbers/parseDouble.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								extras/tests/Numbers/parseDouble.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,97 @@ | ||||
| // ArduinoJson - arduinojson.org | ||||
| // Copyright Benoit Blanchon 2014-2020 | ||||
| // MIT License | ||||
|  | ||||
| #define ARDUINOJSON_USE_DOUBLE 1 | ||||
| #define ARDUINOJSON_ENABLE_NAN 1 | ||||
| #define ARDUINOJSON_ENABLE_INFINITY 1 | ||||
|  | ||||
| #include <ArduinoJson/Numbers/parseNumber.hpp> | ||||
| #include <ArduinoJson/Variant/VariantImpl.hpp> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| using namespace ARDUINOJSON_NAMESPACE; | ||||
|  | ||||
| void checkDouble(const char* input, double expected) { | ||||
|   CAPTURE(input); | ||||
|   REQUIRE(parseNumber<double>(input) == Approx(expected)); | ||||
| } | ||||
|  | ||||
| void checkDoubleNaN(const char* input) { | ||||
|   CAPTURE(input); | ||||
|   double result = parseNumber<double>(input); | ||||
|   REQUIRE(result != result); | ||||
| } | ||||
|  | ||||
| void checkDoubleInf(const char* input, bool negative) { | ||||
|   CAPTURE(input); | ||||
|   double x = parseNumber<double>(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("parseNumber<double>()") { | ||||
|   SECTION("Short_NoExponent") { | ||||
|     checkDouble("3.14", 3.14); | ||||
|     checkDouble("-3.14", -3.14); | ||||
|     checkDouble("+3.14", +3.14); | ||||
|   } | ||||
|  | ||||
|   SECTION("Short_NoDot") { | ||||
|     checkDouble("1E+308", 1E+308); | ||||
|     checkDouble("-1E+308", -1E+308); | ||||
|     checkDouble("+1E-308", +1E-308); | ||||
|     checkDouble("+1e+308", +1e+308); | ||||
|     checkDouble("-1e-308", -1e-308); | ||||
|   } | ||||
|  | ||||
|   SECTION("Max") { | ||||
|     checkDouble(".017976931348623147e+310", 1.7976931348623147e+308); | ||||
|     checkDouble(".17976931348623147e+309", 1.7976931348623147e+308); | ||||
|     checkDouble("1.7976931348623147e+308", 1.7976931348623147e+308); | ||||
|     checkDouble("17.976931348623147e+307", 1.7976931348623147e+308); | ||||
|     checkDouble("179.76931348623147e+306", 1.7976931348623147e+308); | ||||
|   } | ||||
|  | ||||
|   SECTION("Min") { | ||||
|     checkDouble(".022250738585072014e-306", 2.2250738585072014e-308); | ||||
|     checkDouble(".22250738585072014e-307", 2.2250738585072014e-308); | ||||
|     checkDouble("2.2250738585072014e-308", 2.2250738585072014e-308); | ||||
|     checkDouble("22.250738585072014e-309", 2.2250738585072014e-308); | ||||
|     checkDouble("222.50738585072014e-310", 2.2250738585072014e-308); | ||||
|   } | ||||
|  | ||||
|   SECTION("VeryLong") { | ||||
|     checkDouble("0.00000000000000000000000000000001", 1e-32); | ||||
|     checkDouble("100000000000000000000000000000000.0", 1e+32); | ||||
|     checkDouble( | ||||
|         "100000000000000000000000000000000.00000000000000000000000000000", | ||||
|         1e+32); | ||||
|   } | ||||
|  | ||||
|   SECTION("MantissaTooLongToFit") { | ||||
|     checkDouble("0.179769313486231571111111111111", 0.17976931348623157); | ||||
|     checkDouble("17976931348623157.11111111111111", 17976931348623157.0); | ||||
|     checkDouble("1797693.134862315711111111111111", 1797693.1348623157); | ||||
|  | ||||
|     checkDouble("-0.179769313486231571111111111111", -0.17976931348623157); | ||||
|     checkDouble("-17976931348623157.11111111111111", -17976931348623157.0); | ||||
|     checkDouble("-1797693.134862315711111111111111", -1797693.1348623157); | ||||
|   } | ||||
|  | ||||
|   SECTION("ExponentTooBig") { | ||||
|     checkDoubleInf("1e309", false); | ||||
|     checkDoubleInf("-1e309", true); | ||||
|     checkDoubleInf("1e65535", false); | ||||
|     checkDouble("1e-65535", 0.0); | ||||
|   } | ||||
|  | ||||
|   SECTION("NaN") { | ||||
|     checkDoubleNaN("NaN"); | ||||
|     checkDoubleNaN("nan"); | ||||
|   } | ||||
| } | ||||
| @@ -6,28 +6,26 @@ | ||||
| #define ARDUINOJSON_ENABLE_NAN 1 | ||||
| #define ARDUINOJSON_ENABLE_INFINITY 1 | ||||
|  | ||||
| #include <ArduinoJson/Numbers/parseFloat.hpp> | ||||
| #include <ArduinoJson/Numbers/parseNumber.hpp> | ||||
| #include <ArduinoJson/Variant/VariantImpl.hpp> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| using namespace ARDUINOJSON_NAMESPACE; | ||||
|  | ||||
| template <typename T> | ||||
| void checkFloat(const char* input, T expected) { | ||||
| void checkFloat(const char* input, float expected) { | ||||
|   CAPTURE(input); | ||||
|   REQUIRE(parseFloat<T>(input) == Approx(expected)); | ||||
|   REQUIRE(parseNumber<float>(input) == Approx(expected)); | ||||
| } | ||||
|  | ||||
| template <typename T> | ||||
| void checkNaN(const char* input) { | ||||
| void checkFloatNaN(const char* input) { | ||||
|   CAPTURE(input); | ||||
|   T result = parseFloat<T>(input); | ||||
|   float result = parseNumber<float>(input); | ||||
|   REQUIRE(result != result); | ||||
| } | ||||
|  | ||||
| template <typename T> | ||||
| void checkInf(const char* input, bool negative) { | ||||
| void checkFloatInf(const char* input, bool negative) { | ||||
|   CAPTURE(input); | ||||
|   T x = parseFloat<T>(input); | ||||
|   float x = parseNumber<float>(input); | ||||
|   if (negative) | ||||
|     REQUIRE(x < 0); | ||||
|   else | ||||
| @@ -36,137 +34,69 @@ void checkInf(const char* input, bool negative) { | ||||
|   REQUIRE(x * 2 == x);  // a property of infinity | ||||
| } | ||||
|  | ||||
| TEST_CASE("parseFloat<float>()") { | ||||
| TEST_CASE("parseNumber<float>()") { | ||||
|   SECTION("Float_Short_NoExponent") { | ||||
|     checkFloat<float>("3.14", 3.14f); | ||||
|     checkFloat<float>("-3.14", -3.14f); | ||||
|     checkFloat<float>("+3.14", +3.14f); | ||||
|     checkFloat("3.14", 3.14f); | ||||
|     checkFloat("-3.14", -3.14f); | ||||
|     checkFloat("+3.14", +3.14f); | ||||
|   } | ||||
|  | ||||
|   SECTION("Short_NoDot") { | ||||
|     checkFloat<float>("1E+38", 1E+38f); | ||||
|     checkFloat<float>("-1E+38", -1E+38f); | ||||
|     checkFloat<float>("+1E-38", +1E-38f); | ||||
|     checkFloat<float>("+1e+38", +1e+38f); | ||||
|     checkFloat<float>("-1e-38", -1e-38f); | ||||
|     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<float>("340.2823e+36", 3.402823e+38f); | ||||
|     checkFloat<float>("34.02823e+37", 3.402823e+38f); | ||||
|     checkFloat<float>("3.402823e+38", 3.402823e+38f); | ||||
|     checkFloat<float>("0.3402823e+39", 3.402823e+38f); | ||||
|     checkFloat<float>("0.03402823e+40", 3.402823e+38f); | ||||
|     checkFloat<float>("0.003402823e+41", 3.402823e+38f); | ||||
|     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<float>("0.00000000000000000000000000000001", 1e-32f); | ||||
|     checkFloat<float>("100000000000000000000000000000000.0", 1e+32f); | ||||
|     checkFloat<float>( | ||||
|     checkFloat("0.00000000000000000000000000000001", 1e-32f); | ||||
|     checkFloat("100000000000000000000000000000000.0", 1e+32f); | ||||
|     checkFloat( | ||||
|         "100000000000000000000000000000000.00000000000000000000000000000", | ||||
|         1e+32f); | ||||
|   } | ||||
|  | ||||
|   SECTION("MantissaTooLongToFit") { | ||||
|     checkFloat<float>("0.340282346638528861111111111111", 0.34028234663852886f); | ||||
|     checkFloat<float>("34028234663852886.11111111111111", 34028234663852886.0f); | ||||
|     checkFloat<float>("34028234.66385288611111111111111", 34028234.663852886f); | ||||
|     checkFloat("0.340282346638528861111111111111", 0.34028234663852886f); | ||||
|     checkFloat("34028234663852886.11111111111111", 34028234663852886.0f); | ||||
|     checkFloat("34028234.66385288611111111111111", 34028234.663852886f); | ||||
|  | ||||
|     checkFloat<float>("-0.340282346638528861111111111111", | ||||
|                       -0.34028234663852886f); | ||||
|     checkFloat<float>("-34028234663852886.11111111111111", | ||||
|                       -34028234663852886.0f); | ||||
|     checkFloat<float>("-34028234.66385288611111111111111", | ||||
|                       -34028234.663852886f); | ||||
|     checkFloat("-0.340282346638528861111111111111", -0.34028234663852886f); | ||||
|     checkFloat("-34028234663852886.11111111111111", -34028234663852886.0f); | ||||
|     checkFloat("-34028234.66385288611111111111111", -34028234.663852886f); | ||||
|   } | ||||
|  | ||||
|   SECTION("ExponentTooBig") { | ||||
|     checkInf<float>("1e39", false); | ||||
|     checkInf<float>("-1e39", true); | ||||
|     checkInf<float>("1e255", false); | ||||
|     checkFloat<float>("1e-255", 0.0f); | ||||
|     checkFloatInf("1e39", false); | ||||
|     checkFloatInf("-1e39", true); | ||||
|     checkFloatInf("1e255", false); | ||||
|     checkFloat("1e-255", 0.0f); | ||||
|   } | ||||
|  | ||||
|   SECTION("NaN") { | ||||
|     checkNaN<float>("NaN"); | ||||
|     checkNaN<float>("nan"); | ||||
|     checkFloatNaN("NaN"); | ||||
|     checkFloatNaN("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); | ||||
|     checkFloatInf("Infinity", false); | ||||
|     checkFloatInf("+Infinity", false); | ||||
|     checkFloatInf("-Infinity", true); | ||||
|     checkFloatInf("inf", false); | ||||
|     checkFloatInf("+inf", false); | ||||
|     checkFloatInf("-inf", true); | ||||
|  | ||||
|     checkInf<float>("1e300", false); | ||||
|     checkInf<float>("-1e300", true); | ||||
|   } | ||||
| } | ||||
|  | ||||
| TEST_CASE("parseFloat<double>()") { | ||||
|   SECTION("Short_NoExponent") { | ||||
|     checkFloat<double>("3.14", 3.14); | ||||
|     checkFloat<double>("-3.14", -3.14); | ||||
|     checkFloat<double>("+3.14", +3.14); | ||||
|   } | ||||
|  | ||||
|   SECTION("Short_NoDot") { | ||||
|     checkFloat<double>("1E+308", 1E+308); | ||||
|     checkFloat<double>("-1E+308", -1E+308); | ||||
|     checkFloat<double>("+1E-308", +1E-308); | ||||
|     checkFloat<double>("+1e+308", +1e+308); | ||||
|     checkFloat<double>("-1e-308", -1e-308); | ||||
|   } | ||||
|  | ||||
|   SECTION("Max") { | ||||
|     checkFloat<double>(".017976931348623147e+310", 1.7976931348623147e+308); | ||||
|     checkFloat<double>(".17976931348623147e+309", 1.7976931348623147e+308); | ||||
|     checkFloat<double>("1.7976931348623147e+308", 1.7976931348623147e+308); | ||||
|     checkFloat<double>("17.976931348623147e+307", 1.7976931348623147e+308); | ||||
|     checkFloat<double>("179.76931348623147e+306", 1.7976931348623147e+308); | ||||
|   } | ||||
|  | ||||
|   SECTION("Min") { | ||||
|     checkFloat<double>(".022250738585072014e-306", 2.2250738585072014e-308); | ||||
|     checkFloat<double>(".22250738585072014e-307", 2.2250738585072014e-308); | ||||
|     checkFloat<double>("2.2250738585072014e-308", 2.2250738585072014e-308); | ||||
|     checkFloat<double>("22.250738585072014e-309", 2.2250738585072014e-308); | ||||
|     checkFloat<double>("222.50738585072014e-310", 2.2250738585072014e-308); | ||||
|   } | ||||
|  | ||||
|   SECTION("VeryLong") { | ||||
|     checkFloat<double>("0.00000000000000000000000000000001", 1e-32); | ||||
|     checkFloat<double>("100000000000000000000000000000000.0", 1e+32); | ||||
|     checkFloat<double>( | ||||
|         "100000000000000000000000000000000.00000000000000000000000000000", | ||||
|         1e+32); | ||||
|   } | ||||
|  | ||||
|   SECTION("MantissaTooLongToFit") { | ||||
|     checkFloat<double>("0.179769313486231571111111111111", 0.17976931348623157); | ||||
|     checkFloat<double>("17976931348623157.11111111111111", 17976931348623157.0); | ||||
|     checkFloat<double>("1797693.134862315711111111111111", 1797693.1348623157); | ||||
|  | ||||
|     checkFloat<double>("-0.179769313486231571111111111111", | ||||
|                        -0.17976931348623157); | ||||
|     checkFloat<double>("-17976931348623157.11111111111111", | ||||
|                        -17976931348623157.0); | ||||
|     checkFloat<double>("-1797693.134862315711111111111111", | ||||
|                        -1797693.1348623157); | ||||
|   } | ||||
|  | ||||
|   SECTION("ExponentTooBig") { | ||||
|     checkInf<double>("1e309", false); | ||||
|     checkInf<double>("-1e309", true); | ||||
|     checkInf<double>("1e65535", false); | ||||
|     checkFloat<double>("1e-65535", 0.0); | ||||
|   } | ||||
|  | ||||
|   SECTION("NaN") { | ||||
|     checkNaN<double>("NaN"); | ||||
|     checkNaN<double>("nan"); | ||||
|     checkFloatInf("1e300", false); | ||||
|     checkFloatInf("-1e300", true); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -3,7 +3,8 @@ | ||||
| // MIT License | ||||
|  | ||||
| #include <stdint.h> | ||||
| #include <ArduinoJson/Numbers/parseInteger.hpp> | ||||
| #include <ArduinoJson/Numbers/parseNumber.hpp> | ||||
| #include <ArduinoJson/Variant/VariantImpl.hpp> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| using namespace ARDUINOJSON_NAMESPACE; | ||||
| @@ -11,11 +12,11 @@ using namespace ARDUINOJSON_NAMESPACE; | ||||
| template <typename T> | ||||
| void checkInteger(const char* input, T expected) { | ||||
|   CAPTURE(input); | ||||
|   T actual = parseInteger<T>(input); | ||||
|   T actual = parseNumber<T>(input); | ||||
|   REQUIRE(expected == actual); | ||||
| } | ||||
|  | ||||
| TEST_CASE("parseInteger<int8_t>()") { | ||||
| TEST_CASE("parseNumber<int8_t>()") { | ||||
|   checkInteger<int8_t>("-128", -128); | ||||
|   checkInteger<int8_t>("127", 127); | ||||
|   checkInteger<int8_t>("+127", 127); | ||||
| @@ -25,7 +26,7 @@ TEST_CASE("parseInteger<int8_t>()") { | ||||
|   checkInteger<int8_t>("-129", 0);  // overflow | ||||
| } | ||||
|  | ||||
| TEST_CASE("parseInteger<int16_t>()") { | ||||
| TEST_CASE("parseNumber<int16_t>()") { | ||||
|   checkInteger<int16_t>("-32768", -32768); | ||||
|   checkInteger<int16_t>("32767", 32767); | ||||
|   checkInteger<int16_t>("+32767", 32767); | ||||
| @@ -35,7 +36,7 @@ TEST_CASE("parseInteger<int16_t>()") { | ||||
|   checkInteger<int16_t>("32768", 0);   // overflow | ||||
| } | ||||
|  | ||||
| TEST_CASE("parseInteger<int32_t>()") { | ||||
| TEST_CASE("parseNumber<int32_t>()") { | ||||
|   checkInteger<int32_t>("-2147483648", (-2147483647 - 1)); | ||||
|   checkInteger<int32_t>("2147483647", 2147483647); | ||||
|   checkInteger<int32_t>("+2147483647", 2147483647); | ||||
| @@ -45,7 +46,7 @@ TEST_CASE("parseInteger<int32_t>()") { | ||||
|   checkInteger<int32_t>("2147483648", 0);   // overflow | ||||
| } | ||||
|  | ||||
| TEST_CASE("parseInteger<uint8_t>()") { | ||||
| TEST_CASE("parseNumber<uint8_t>()") { | ||||
|   checkInteger<uint8_t>("0", 0); | ||||
|   checkInteger<uint8_t>("255", 255); | ||||
|   checkInteger<uint8_t>("+255", 255); | ||||
| @@ -55,7 +56,7 @@ TEST_CASE("parseInteger<uint8_t>()") { | ||||
|   checkInteger<uint8_t>("256", 0); | ||||
| } | ||||
|  | ||||
| TEST_CASE("parseInteger<uint16_t>()") { | ||||
| TEST_CASE("parseNumber<uint16_t>()") { | ||||
|   checkInteger<uint16_t>("0", 0); | ||||
|   checkInteger<uint16_t>("65535", 65535); | ||||
|   checkInteger<uint16_t>("+65535", 65535); | ||||
|   | ||||
| @@ -2,22 +2,37 @@ | ||||
| // Copyright Benoit Blanchon 2014-2020 | ||||
| // MIT License | ||||
|  | ||||
| #include <ArduinoJson/Numbers/Integer.hpp> | ||||
| #include <ArduinoJson/Numbers/parseNumber.hpp> | ||||
| #include <ArduinoJson/Variant/VariantImpl.hpp> | ||||
| #include <catch.hpp> | ||||
|  | ||||
| using namespace ARDUINOJSON_NAMESPACE; | ||||
|  | ||||
| TEST_CASE("Test uint32_t overflow") { | ||||
|   ParsedNumber<float, uint32_t> first, second; | ||||
|   parseNumber("4294967295", first); | ||||
|   parseNumber("4294967296", second); | ||||
| TEST_CASE("Test unsigned integer overflow") { | ||||
|   VariantData first, second; | ||||
|   first.init(); | ||||
|   second.init(); | ||||
|  | ||||
|   // Avoids MSVC warning C4127 (conditional expression is constant) | ||||
|   size_t integerSize = sizeof(Integer); | ||||
|  | ||||
|   if (integerSize == 8) { | ||||
|     parseNumber("18446744073709551615", first); | ||||
|     parseNumber("18446744073709551616", second); | ||||
|   } else { | ||||
|     parseNumber("4294967295", first); | ||||
|     parseNumber("4294967296", second); | ||||
|   } | ||||
|  | ||||
|   REQUIRE(first.type() == uint8_t(VALUE_IS_POSITIVE_INTEGER)); | ||||
|   REQUIRE(second.type() == uint8_t(VALUE_IS_FLOAT)); | ||||
| } | ||||
|  | ||||
| TEST_CASE("Invalid value") { | ||||
|   ParsedNumber<float, uint32_t> result; | ||||
|   VariantData result; | ||||
|   result.init(); | ||||
|  | ||||
|   parseNumber("6a3", result); | ||||
|  | ||||
|   REQUIRE(result.type() == uint8_t(VALUE_IS_NULL)); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user