forked from bblanchon/ArduinoJson
Added overflow handling in JsonVariant::as<T>() and JsonVariant::is<T>()
This commit is contained in:
@ -22,7 +22,8 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
SECTION("long long") {
|
||||
array[0] = 9223372036854775807;
|
||||
REQUIRE(9223372036854775807 == array[0].as<int64_t>());
|
||||
REQUIRE(true == array[0].is<int>());
|
||||
REQUIRE(true == array[0].is<int64_t>());
|
||||
REQUIRE(false == array[0].is<int32_t>());
|
||||
REQUIRE(false == array[0].is<bool>());
|
||||
}
|
||||
#endif
|
||||
|
@ -3,17 +3,18 @@
|
||||
# MIT License
|
||||
|
||||
add_executable(JsonDeserializerTests
|
||||
array.cpp
|
||||
array_static.cpp
|
||||
DeserializationError.cpp
|
||||
deserializeJsonArray.cpp
|
||||
deserializeJsonArrayStatic.cpp
|
||||
deserializeJsonObject.cpp
|
||||
deserializeJsonObjectStatic.cpp
|
||||
deserializeJsonValue.cpp
|
||||
deserializeJsonString.cpp
|
||||
input_types.cpp
|
||||
incomplete_input.cpp
|
||||
input_types.cpp
|
||||
number.cpp
|
||||
invalid_input.cpp
|
||||
misc.cpp
|
||||
nestingLimit.cpp
|
||||
object.cpp
|
||||
object_static.cpp
|
||||
string.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(JsonDeserializerTests catch)
|
||||
|
@ -7,11 +7,6 @@
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
namespace my {
|
||||
using ARDUINOJSON_NAMESPACE::isinf;
|
||||
using ARDUINOJSON_NAMESPACE::isnan;
|
||||
} // namespace my
|
||||
|
||||
TEST_CASE("deserializeJson(DynamicJsonDocument&)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
@ -48,64 +43,6 @@ TEST_CASE("deserializeJson(DynamicJsonDocument&)") {
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Integers") {
|
||||
SECTION("0") {
|
||||
DeserializationError err = deserializeJson(doc, "0");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<int>() == true);
|
||||
REQUIRE(doc.as<int>() == 0);
|
||||
REQUIRE(doc.as<std::string>() == "0"); // issue #808
|
||||
}
|
||||
|
||||
SECTION("Negative") {
|
||||
DeserializationError err = deserializeJson(doc, "-42");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<int>());
|
||||
REQUIRE_FALSE(doc.is<bool>());
|
||||
REQUIRE(doc.as<int>() == -42);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Floats") {
|
||||
SECTION("Double") {
|
||||
DeserializationError err = deserializeJson(doc, "-1.23e+4");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE_FALSE(doc.is<int>());
|
||||
REQUIRE(doc.is<double>());
|
||||
REQUIRE(doc.as<double>() == Approx(-1.23e+4));
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
DeserializationError err = deserializeJson(doc, "NaN");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isnan(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("+Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "+Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("-Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "-Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Booleans") {
|
||||
SECTION("True") {
|
||||
DeserializationError err = deserializeJson(doc, "true");
|
131
test/JsonDeserializer/number.cpp
Normal file
131
test/JsonDeserializer/number.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#define ARDUINOJSON_USE_LONG_LONG 0
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <limits.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
namespace my {
|
||||
using ARDUINOJSON_NAMESPACE::isinf;
|
||||
using ARDUINOJSON_NAMESPACE::isnan;
|
||||
} // namespace my
|
||||
|
||||
TEST_CASE("deserialize an integer") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("Integer") {
|
||||
SECTION("0") {
|
||||
DeserializationError err = deserializeJson(doc, "0");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<int>() == true);
|
||||
REQUIRE(doc.as<int>() == 0);
|
||||
REQUIRE(doc.as<std::string>() == "0"); // issue #808
|
||||
}
|
||||
|
||||
SECTION("Negative") {
|
||||
DeserializationError err = deserializeJson(doc, "-42");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<int>());
|
||||
REQUIRE_FALSE(doc.is<bool>());
|
||||
REQUIRE(doc.as<int>() == -42);
|
||||
}
|
||||
|
||||
#if LONG_MAX == 2147483647
|
||||
SECTION("LONG_MAX") {
|
||||
DeserializationError err = deserializeJson(doc, "2147483647");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<long>() == true);
|
||||
REQUIRE(doc.as<long>() == LONG_MAX);
|
||||
}
|
||||
|
||||
SECTION("LONG_MAX + 1") {
|
||||
DeserializationError err = deserializeJson(doc, "2147483648");
|
||||
|
||||
CAPTURE(LONG_MIN);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<long>() == false);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if LONG_MIN == -2147483648
|
||||
SECTION("LONG_MIN") {
|
||||
DeserializationError err = deserializeJson(doc, "-2147483648");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<long>() == true);
|
||||
REQUIRE(doc.as<long>() == LONG_MIN);
|
||||
}
|
||||
|
||||
SECTION("LONG_MIN - 1") {
|
||||
DeserializationError err = deserializeJson(doc, "-2147483649");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<long>() == false);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ULONG_MAX == 4294967295
|
||||
SECTION("ULONG_MAX") {
|
||||
DeserializationError err = deserializeJson(doc, "4294967295");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<unsigned long>() == true);
|
||||
REQUIRE(doc.as<unsigned long>() == ULONG_MAX);
|
||||
REQUIRE(doc.is<long>() == false);
|
||||
}
|
||||
|
||||
SECTION("ULONG_MAX + 1") {
|
||||
DeserializationError err = deserializeJson(doc, "4294967296");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<unsigned long>() == false);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("Floats") {
|
||||
SECTION("Double") {
|
||||
DeserializationError err = deserializeJson(doc, "-1.23e+4");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE_FALSE(doc.is<int>());
|
||||
REQUIRE(doc.is<double>());
|
||||
REQUIRE(doc.as<double>() == Approx(-1.23e+4));
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
DeserializationError err = deserializeJson(doc, "NaN");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isnan(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("+Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "+Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("-Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "-Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ add_executable(JsonVariantTests
|
||||
misc.cpp
|
||||
nesting.cpp
|
||||
or.cpp
|
||||
overflow.cpp
|
||||
remove.cpp
|
||||
set.cpp
|
||||
subscript.cpp
|
||||
|
@ -6,6 +6,10 @@
|
||||
#include <stdint.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
namespace my {
|
||||
using ARDUINOJSON_NAMESPACE::isinf;
|
||||
} // namespace my
|
||||
|
||||
static const char* null = 0;
|
||||
|
||||
TEST_CASE("JsonVariant::as()") {
|
||||
@ -94,7 +98,6 @@ TEST_CASE("JsonVariant::as()") {
|
||||
SECTION("set(\"42\")") {
|
||||
variant.set("42");
|
||||
|
||||
REQUIRE(variant.as<bool>());
|
||||
REQUIRE(variant.as<long>() == 42L);
|
||||
}
|
||||
|
||||
@ -111,7 +114,6 @@ TEST_CASE("JsonVariant::as()") {
|
||||
SECTION("set(std::string(\"4.2\"))") {
|
||||
variant.set(std::string("4.2"));
|
||||
|
||||
REQUIRE(variant.as<bool>() == true);
|
||||
REQUIRE(variant.as<long>() == 4L);
|
||||
REQUIRE(variant.as<double>() == 4.2);
|
||||
REQUIRE(variant.as<char*>() == std::string("4.2"));
|
||||
@ -121,8 +123,31 @@ TEST_CASE("JsonVariant::as()") {
|
||||
SECTION("set(\"true\")") {
|
||||
variant.set("true");
|
||||
|
||||
REQUIRE(variant.as<bool>());
|
||||
REQUIRE(variant.as<long>() == 1L);
|
||||
REQUIRE(variant.as<bool>() == true);
|
||||
REQUIRE(variant.as<int>() == 0);
|
||||
}
|
||||
|
||||
SECTION("set(-1e300)") {
|
||||
variant.set(-1e300);
|
||||
|
||||
REQUIRE(variant.as<double>() == -1e300);
|
||||
REQUIRE(variant.as<float>() < 0);
|
||||
REQUIRE(my::isinf(variant.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("set(1e300)") {
|
||||
variant.set(1e300);
|
||||
|
||||
REQUIRE(variant.as<double>() == 1e300);
|
||||
REQUIRE(variant.as<float>() > 0);
|
||||
REQUIRE(my::isinf(variant.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("set(1e300)") {
|
||||
variant.set(1e-300);
|
||||
|
||||
REQUIRE(variant.as<double>() == 1e-300);
|
||||
REQUIRE(variant.as<float>() == 0);
|
||||
}
|
||||
|
||||
SECTION("to<JsonObject>()") {
|
||||
|
72
test/JsonVariant/overflow.cpp
Normal file
72
test/JsonVariant/overflow.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
void shouldBeOk(TIn value) {
|
||||
StaticJsonDocument<1> doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
var.set(value);
|
||||
REQUIRE(var.as<TOut>() == TOut(value));
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
void shouldOverflow(TIn value) {
|
||||
StaticJsonDocument<1> doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
var.set(value);
|
||||
REQUIRE(var.as<TOut>() == 0);
|
||||
REQUIRE(var.is<TOut>() == false);
|
||||
}
|
||||
|
||||
TEST_CASE("Handle integer overflow in stored integer") {
|
||||
SECTION("int8_t") {
|
||||
// ok
|
||||
shouldBeOk<int8_t>(-128);
|
||||
shouldBeOk<int8_t>(42.0);
|
||||
shouldBeOk<int8_t>(127);
|
||||
|
||||
// too low
|
||||
shouldOverflow<int8_t>(-128.1);
|
||||
shouldOverflow<int8_t>(-129);
|
||||
|
||||
// too high
|
||||
shouldOverflow<int8_t>(128);
|
||||
shouldOverflow<int8_t>(127.1);
|
||||
}
|
||||
|
||||
SECTION("int16_t") {
|
||||
// ok
|
||||
shouldBeOk<int16_t>(-32768);
|
||||
shouldBeOk<int16_t>(-32767.9);
|
||||
shouldBeOk<int16_t>(32766.9);
|
||||
shouldBeOk<int16_t>(32767);
|
||||
|
||||
// too low
|
||||
shouldOverflow<int16_t>(-32768.1);
|
||||
shouldOverflow<int16_t>(-32769);
|
||||
|
||||
// too high
|
||||
shouldOverflow<int16_t>(32767.1);
|
||||
shouldOverflow<int16_t>(32768);
|
||||
}
|
||||
|
||||
SECTION("uint8_t") {
|
||||
// ok
|
||||
shouldBeOk<uint8_t>(1);
|
||||
shouldBeOk<uint8_t>(42.0);
|
||||
shouldBeOk<uint8_t>(255);
|
||||
|
||||
// too low
|
||||
shouldOverflow<uint8_t>(-1);
|
||||
shouldOverflow<uint8_t>(-0.1);
|
||||
|
||||
// to high
|
||||
shouldOverflow<uint8_t>(255.1);
|
||||
shouldOverflow<uint8_t>(256);
|
||||
shouldOverflow<uint8_t>(257);
|
||||
}
|
||||
}
|
@ -16,6 +16,14 @@ static void check(const char* input, U expected) {
|
||||
REQUIRE(doc.as<T>() == expected);
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG == 0
|
||||
static void checkNotSupported(const char* input) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
REQUIRE(error == DeserializationError::NotSupported);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void checkIsNull(const char* input) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
@ -70,9 +78,9 @@ TEST_CASE("deserialize MsgPack value") {
|
||||
check<uint64_t>("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
|
||||
0x123456789ABCDEF0U);
|
||||
#else
|
||||
check<uint32_t>("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
|
||||
check<uint32_t>("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0xFFFFFFFF);
|
||||
check<uint32_t>("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0", 0x9ABCDEF0);
|
||||
checkNotSupported("\xCF\x00\x00\x00\x00\x00\x00\x00\x00");
|
||||
checkNotSupported("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
|
||||
checkNotSupported("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -95,15 +103,15 @@ TEST_CASE("deserialize MsgPack value") {
|
||||
|
||||
SECTION("int 64") {
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
check<uint64_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
|
||||
check<uint64_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
|
||||
0xFFFFFFFFFFFFFFFFU);
|
||||
check<uint64_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
|
||||
0x123456789ABCDEF0U);
|
||||
check<int64_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", int64_t(0U));
|
||||
check<int64_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
|
||||
int64_t(0xFFFFFFFFFFFFFFFFU));
|
||||
check<int64_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
|
||||
int64_t(0x123456789ABCDEF0));
|
||||
#else
|
||||
check<uint32_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
|
||||
check<uint32_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0xFFFFFFFF);
|
||||
check<uint32_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0", 0x9ABCDEF0);
|
||||
checkNotSupported("\xD3\x00\x00\x00\x00\x00\x00\x00\x00");
|
||||
checkNotSupported("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
|
||||
checkNotSupported("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -60,9 +60,11 @@ TEST_CASE("deserializeMsgPack() returns IncompleteInput") {
|
||||
checkAllSizes("\xCE\x00\x00\x00\x01", 5);
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
SECTION("uint 64") {
|
||||
checkAllSizes("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 9);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("int 8") {
|
||||
checkAllSizes("\xD0\x01", 2);
|
||||
@ -76,9 +78,11 @@ TEST_CASE("deserializeMsgPack() returns IncompleteInput") {
|
||||
checkAllSizes("\xD2\x00\x00\x00\x01", 5);
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
SECTION("int 64") {
|
||||
checkAllSizes("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 9);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("float 32") {
|
||||
checkAllSizes("\xCA\x40\x48\xF5\xC3", 5);
|
||||
|
@ -3,10 +3,9 @@
|
||||
# MIT License
|
||||
|
||||
add_executable(NumbersTests
|
||||
isFloat.cpp
|
||||
isInteger.cpp
|
||||
parseFloat.cpp
|
||||
parseInteger.cpp
|
||||
parseNumber.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(NumbersTests catch)
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
18
test/Numbers/parseNumber.cpp
Normal file
18
test/Numbers/parseNumber.cpp
Normal 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));
|
||||
}
|
Reference in New Issue
Block a user