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

@ -15,6 +15,7 @@ add_executable(JsonVariantTests
misc.cpp
nesting.cpp
or.cpp
overflow.cpp
remove.cpp
set.cpp
subscript.cpp

View File

@ -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>()") {

View 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);
}
}