forked from bblanchon/ArduinoJson
Added ARDUINOJSON_ENABLE_INFINITY
to enable Infinity in JSON
This commit is contained in:
@ -8,12 +8,14 @@ set(CMAKE_CXX_STANDARD 11)
|
||||
add_executable(MixedConfigurationTests
|
||||
decode_unicode_0.cpp
|
||||
decode_unicode_1.cpp
|
||||
enable_nan_0.cpp
|
||||
enable_nan_1.cpp
|
||||
enable_infinity_0.cpp
|
||||
enable_infinity_1.cpp
|
||||
use_double_0.cpp
|
||||
use_double_1.cpp
|
||||
use_long_long_0.cpp
|
||||
use_long_long_1.cpp
|
||||
enable_nan_0.cpp
|
||||
enable_nan_1.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(MixedConfigurationTests catch)
|
||||
|
35
test/MixedConfiguration/enable_infinity_0.cpp
Normal file
35
test/MixedConfiguration/enable_infinity_0.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#define ARDUINOJSON_ENABLE_INFINITY 0
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
#include <limits>
|
||||
|
||||
static void assertParseFails(const char* json) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
auto err = deserializeJson(doc, json);
|
||||
|
||||
REQUIRE(err == DeserializationError::InvalidInput);
|
||||
}
|
||||
|
||||
static void assertJsonEquals(const JsonDocument& doc,
|
||||
std::string expectedJson) {
|
||||
std::string actualJson;
|
||||
serializeJson(doc, actualJson);
|
||||
REQUIRE(actualJson == expectedJson);
|
||||
}
|
||||
|
||||
TEST_CASE("ARDUINOJSON_ENABLE_INFINITY == 0") {
|
||||
SECTION("serializeJson()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
doc.add(std::numeric_limits<double>::infinity());
|
||||
doc.add(-std::numeric_limits<double>::infinity());
|
||||
|
||||
assertJsonEquals(doc, "[null,null]");
|
||||
}
|
||||
|
||||
SECTION("deserializeJson()") {
|
||||
assertParseFails("{\"X\":Infinity}");
|
||||
assertParseFails("{\"X\":-Infinity}");
|
||||
assertParseFails("{\"X\":+Infinity}");
|
||||
}
|
||||
}
|
38
test/MixedConfiguration/enable_infinity_1.cpp
Normal file
38
test/MixedConfiguration/enable_infinity_1.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#define ARDUINOJSON_ENABLE_INFINITY 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
#include <limits>
|
||||
|
||||
namespace my {
|
||||
using ARDUINOJSON_NAMESPACE::isinf;
|
||||
} // namespace my
|
||||
|
||||
TEST_CASE("ARDUINOJSON_ENABLE_INFINITY == 1") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("serializeJson()") {
|
||||
doc.add(std::numeric_limits<double>::infinity());
|
||||
doc.add(-std::numeric_limits<double>::infinity());
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "[Infinity,-Infinity]");
|
||||
}
|
||||
|
||||
SECTION("deserializeJson()") {
|
||||
auto err = deserializeJson(doc, "[Infinity,-Infinity,+Infinity]");
|
||||
float a = doc[0];
|
||||
float b = doc[1];
|
||||
float c = doc[2];
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(my::isinf(a));
|
||||
REQUIRE(a > 0);
|
||||
REQUIRE(my::isinf(b));
|
||||
REQUIRE(b < 0);
|
||||
REQUIRE(my::isinf(c));
|
||||
REQUIRE(c > 0);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user