mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-16 12:02:14 +02:00
Disabled lazy number deserialization (fixes #772)
This commit is contained in:
13
test/Numbers/CMakeLists.txt
Normal file
13
test/Numbers/CMakeLists.txt
Normal file
@ -0,0 +1,13 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2018
|
||||
# MIT License
|
||||
|
||||
add_executable(NumbersTests
|
||||
isFloat.cpp
|
||||
isInteger.cpp
|
||||
parseFloat.cpp
|
||||
parseInteger.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(NumbersTests catch)
|
||||
add_test(Numbers NumbersTests)
|
80
test/Numbers/isFloat.cpp
Normal file
80
test/Numbers/isFloat.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Numbers/isFloat.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
TEST_CASE("isFloat()") {
|
||||
SECTION("Input is NULL") {
|
||||
REQUIRE(isFloat(NULL) == false);
|
||||
}
|
||||
|
||||
SECTION("Empty string") {
|
||||
REQUIRE(isFloat("") == false);
|
||||
}
|
||||
|
||||
SECTION("NoExponent") {
|
||||
REQUIRE(isFloat("3.14") == true);
|
||||
REQUIRE(isFloat("-3.14") == true);
|
||||
REQUIRE(isFloat("+3.14") == true);
|
||||
}
|
||||
|
||||
SECTION("IntegralPartMissing") {
|
||||
REQUIRE(isFloat(".14") == true);
|
||||
REQUIRE(isFloat("-.14") == true);
|
||||
REQUIRE(isFloat("+.14") == true);
|
||||
}
|
||||
|
||||
SECTION("FractionalPartMissing") {
|
||||
REQUIRE(isFloat("3.") == true);
|
||||
REQUIRE(isFloat("-3.e14") == true);
|
||||
REQUIRE(isFloat("+3.e-14") == true);
|
||||
}
|
||||
|
||||
SECTION("NoDot") {
|
||||
REQUIRE(isFloat("3e14") == true);
|
||||
REQUIRE(isFloat("3e-14") == true);
|
||||
REQUIRE(isFloat("3e+14") == true);
|
||||
}
|
||||
|
||||
SECTION("Integer") {
|
||||
REQUIRE(isFloat("14") == true);
|
||||
REQUIRE(isFloat("-14") == true);
|
||||
REQUIRE(isFloat("+14") == true);
|
||||
}
|
||||
|
||||
SECTION("ExponentMissing") {
|
||||
REQUIRE(isFloat("3.14e") == false);
|
||||
REQUIRE(isFloat("3.14e-") == false);
|
||||
REQUIRE(isFloat("3.14e+") == false);
|
||||
}
|
||||
|
||||
SECTION("JustASign") {
|
||||
REQUIRE(isFloat("-") == false);
|
||||
REQUIRE(isFloat("+") == false);
|
||||
}
|
||||
|
||||
SECTION("Empty") {
|
||||
REQUIRE(isFloat("") == false);
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
REQUIRE(isFloat("NaN") == true);
|
||||
REQUIRE(isFloat("n") == false);
|
||||
REQUIRE(isFloat("N") == false);
|
||||
REQUIRE(isFloat("nan") == false);
|
||||
REQUIRE(isFloat("-NaN") == false);
|
||||
REQUIRE(isFloat("+NaN") == false);
|
||||
}
|
||||
|
||||
SECTION("Infinity") {
|
||||
REQUIRE(isFloat("Infinity") == true);
|
||||
REQUIRE(isFloat("+Infinity") == true);
|
||||
REQUIRE(isFloat("-Infinity") == true);
|
||||
REQUIRE(isFloat("infinity") == false);
|
||||
REQUIRE(isFloat("Inf") == false);
|
||||
}
|
||||
}
|
40
test/Numbers/isInteger.cpp
Normal file
40
test/Numbers/isInteger.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Numbers/isInteger.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
TEST_CASE("isInteger()") {
|
||||
SECTION("Null") {
|
||||
REQUIRE(isInteger(NULL) == false);
|
||||
}
|
||||
|
||||
SECTION("Empty string") {
|
||||
REQUIRE(isInteger("") == false);
|
||||
}
|
||||
|
||||
SECTION("FloatNotInteger") {
|
||||
REQUIRE(isInteger("3.14") == false);
|
||||
REQUIRE(isInteger("-3.14") == false);
|
||||
REQUIRE(isInteger("+3.14") == false);
|
||||
}
|
||||
|
||||
SECTION("Spaces") {
|
||||
REQUIRE(isInteger("42 ") == false);
|
||||
REQUIRE(isInteger(" 42") == false);
|
||||
}
|
||||
|
||||
SECTION("Valid") {
|
||||
REQUIRE(isInteger("42") == true);
|
||||
REQUIRE(isInteger("-42") == true);
|
||||
REQUIRE(isInteger("+42") == true);
|
||||
}
|
||||
|
||||
SECTION("ExtraSign") {
|
||||
REQUIRE(isInteger("--42") == false);
|
||||
REQUIRE(isInteger("++42") == false);
|
||||
}
|
||||
}
|
177
test/Numbers/parseFloat.cpp
Normal file
177
test/Numbers/parseFloat.cpp
Normal file
@ -0,0 +1,177 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Numbers/parseFloat.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
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("Null") {
|
||||
check<float>(NULL, 0);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
SECTION("Boolean") {
|
||||
check<float>("false", 0.0f);
|
||||
check<float>("true", 1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("parseFloat<double>()") {
|
||||
SECTION("Null") {
|
||||
check<double>(NULL, 0);
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
SECTION("Boolean") {
|
||||
check<double>("false", 0.0);
|
||||
check<double>("true", 1.0);
|
||||
}
|
||||
}
|
79
test/Numbers/parseInteger.cpp
Normal file
79
test/Numbers/parseInteger.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <stdint.h>
|
||||
#include <ArduinoJson/Numbers/parseInteger.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
template <typename T>
|
||||
void check(const char* input, T expected) {
|
||||
CAPTURE(input);
|
||||
T actual = parseInteger<T>(input);
|
||||
REQUIRE(expected == actual);
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<int8_t>()") {
|
||||
check<int8_t>("-128", -128);
|
||||
check<int8_t>("127", 127);
|
||||
check<int8_t>("+127", 127);
|
||||
check<int8_t>("3.14", 3);
|
||||
check<int8_t>("x42", 0);
|
||||
check<int8_t>("128", -128);
|
||||
check<int8_t>("-129", 127);
|
||||
check<int8_t>(NULL, 0);
|
||||
check<int8_t>("true", 1);
|
||||
check<int8_t>("false", 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<int16_t>()") {
|
||||
check<int16_t>("-32768", -32768);
|
||||
check<int16_t>("32767", 32767);
|
||||
check<int16_t>("+32767", 32767);
|
||||
check<int16_t>("3.14", 3);
|
||||
check<int16_t>("x42", 0);
|
||||
check<int16_t>("-32769", 32767);
|
||||
check<int16_t>("32768", -32768);
|
||||
check<int16_t>(NULL, 0);
|
||||
check<int16_t>("true", 1);
|
||||
check<int16_t>("false", 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<int32_t>()") {
|
||||
check<int32_t>("-2147483648", (-2147483647 - 1));
|
||||
check<int32_t>("2147483647", 2147483647);
|
||||
check<int32_t>("+2147483647", 2147483647);
|
||||
check<int32_t>("3.14", 3);
|
||||
check<int32_t>("x42", 0);
|
||||
check<int32_t>("-2147483649", 2147483647);
|
||||
check<int32_t>("2147483648", (-2147483647 - 1));
|
||||
check<int32_t>("true", 1);
|
||||
check<int32_t>("false", 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<uint8_t>()") {
|
||||
check<uint8_t>("0", 0);
|
||||
check<uint8_t>("255", 255);
|
||||
check<uint8_t>("+255", 255);
|
||||
check<uint8_t>("3.14", 3);
|
||||
check<uint8_t>("x42", 0);
|
||||
check<uint8_t>("-1", 255);
|
||||
check<uint8_t>("256", 0);
|
||||
check<uint8_t>("true", 1);
|
||||
check<uint8_t>("false", 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<uint16_t>()") {
|
||||
check<uint16_t>("0", 0);
|
||||
check<uint16_t>("65535", 65535);
|
||||
check<uint16_t>("+65535", 65535);
|
||||
check<uint16_t>("3.14", 3);
|
||||
// check<uint16_t>(" 42", 0);
|
||||
check<uint16_t>("x42", 0);
|
||||
check<uint16_t>("-1", 65535);
|
||||
check<uint16_t>("65536", 0);
|
||||
check<uint16_t>("true", 1);
|
||||
check<uint16_t>("false", 0);
|
||||
}
|
Reference in New Issue
Block a user