Added overflow handling in JsonVariant::as<T>() and JsonVariant::is<T>()

This commit is contained in:
Benoit Blanchon
2019-03-06 15:31:37 +01:00
parent 746f2882f7
commit 576543c4b4
42 changed files with 781 additions and 434 deletions

View File

@ -3,10 +3,9 @@
# MIT License
add_executable(NumbersTests
isFloat.cpp
isInteger.cpp
parseFloat.cpp
parseInteger.cpp
parseNumber.cpp
)
target_link_libraries(NumbersTests catch)

View File

@ -1,80 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson/Numbers/isFloat.hpp>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
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);
}
}

View File

@ -1,40 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson/Numbers/isInteger.hpp>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
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);
}
}

View File

@ -2,6 +2,8 @@
// Copyright Benoit Blanchon 2014-2019
// MIT License
#define ARDUINOJSON_USE_DOUBLE 0
#include <ArduinoJson/Numbers/parseFloat.hpp>
#include <catch.hpp>
@ -33,10 +35,6 @@ void checkInf(const char* input, bool negative) {
}
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);
@ -97,19 +95,13 @@ TEST_CASE("parseFloat<float>()") {
checkInf<float>("inf", false);
checkInf<float>("+inf", false);
checkInf<float>("-inf", true);
}
SECTION("Boolean") {
check<float>("false", 0.0f);
check<float>("true", 1.0f);
checkInf<float>("1e300", false);
checkInf<float>("-1e300", true);
}
}
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);
@ -169,9 +161,4 @@ TEST_CASE("parseFloat<double>()") {
checkNaN<double>("NaN");
checkNaN<double>("nan");
}
SECTION("Boolean") {
check<double>("false", 0.0);
check<double>("true", 1.0);
}
}

View File

@ -21,11 +21,8 @@ TEST_CASE("parseInteger<int8_t>()") {
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);
check<int8_t>("128", 0); // overflow
check<int8_t>("-129", 0); // overflow
}
TEST_CASE("parseInteger<int16_t>()") {
@ -34,11 +31,8 @@ TEST_CASE("parseInteger<int16_t>()") {
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);
check<int16_t>("-32769", 0); // overflow
check<int16_t>("32768", 0); // overflow
}
TEST_CASE("parseInteger<int32_t>()") {
@ -47,10 +41,8 @@ TEST_CASE("parseInteger<int32_t>()") {
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);
check<int32_t>("-2147483649", 0); // overflow
check<int32_t>("2147483648", 0); // overflow
}
TEST_CASE("parseInteger<uint8_t>()") {
@ -59,10 +51,8 @@ TEST_CASE("parseInteger<uint8_t>()") {
check<uint8_t>("+255", 255);
check<uint8_t>("3.14", 3);
check<uint8_t>("x42", 0);
check<uint8_t>("-1", 255);
check<uint8_t>("-1", 0);
check<uint8_t>("256", 0);
check<uint8_t>("true", 1);
check<uint8_t>("false", 0);
}
TEST_CASE("parseInteger<uint16_t>()") {
@ -72,8 +62,6 @@ TEST_CASE("parseInteger<uint16_t>()") {
check<uint16_t>("3.14", 3);
// check<uint16_t>(" 42", 0);
check<uint16_t>("x42", 0);
check<uint16_t>("-1", 65535);
check<uint16_t>("-1", 0);
check<uint16_t>("65536", 0);
check<uint16_t>("true", 1);
check<uint16_t>("false", 0);
}

View File

@ -0,0 +1,18 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2019
// MIT License
#include <ArduinoJson/Numbers/parseNumber.hpp>
#include <catch.hpp>
using namespace ARDUINOJSON_NAMESPACE;
TEST_CASE("Test uint32_t overflow") {
ParsedNumber<float, uint32_t> first =
parseNumber<float, uint32_t>("4294967295");
ParsedNumber<float, uint32_t> second =
parseNumber<float, uint32_t>("4294967296");
REQUIRE(first.type() == uint8_t(VALUE_IS_POSITIVE_INTEGER));
REQUIRE(second.type() == uint8_t(VALUE_IS_FLOAT));
}