forked from bblanchon/ArduinoJson
Moved ancillary files to extras/
(fixes #1011)
This commit is contained in:
24
extras/tests/MixedConfiguration/CMakeLists.txt
Normal file
24
extras/tests/MixedConfiguration/CMakeLists.txt
Normal file
@ -0,0 +1,24 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2019
|
||||
# MIT License
|
||||
|
||||
# we need C++11 for 'long long'
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
|
||||
add_executable(MixedConfigurationTests
|
||||
cpp11.cpp
|
||||
decode_unicode_0.cpp
|
||||
decode_unicode_1.cpp
|
||||
enable_infinity_0.cpp
|
||||
enable_infinity_1.cpp
|
||||
enable_nan_0.cpp
|
||||
enable_nan_1.cpp
|
||||
use_double_0.cpp
|
||||
use_double_1.cpp
|
||||
use_long_long_0.cpp
|
||||
use_long_long_1.cpp
|
||||
enable_progmem_1.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(MixedConfigurationTests catch)
|
||||
add_test(MixedConfiguration MixedConfigurationTests)
|
31
extras/tests/MixedConfiguration/cpp11.cpp
Normal file
31
extras/tests/MixedConfiguration/cpp11.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
|
||||
TEST_CASE("nullptr") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonVariant variant = doc.to<JsonVariant>();
|
||||
|
||||
SECTION("JsonVariant == nullptr") {
|
||||
REQUIRE((variant == nullptr));
|
||||
REQUIRE_FALSE((variant != nullptr));
|
||||
}
|
||||
|
||||
SECTION("JsonVariant != nullptr") {
|
||||
variant.set(42);
|
||||
|
||||
REQUIRE_FALSE((variant == nullptr));
|
||||
REQUIRE((variant != nullptr));
|
||||
}
|
||||
|
||||
SECTION("JsonVariant.set(nullptr)") {
|
||||
variant.set(42);
|
||||
variant.set(nullptr);
|
||||
|
||||
REQUIRE(variant.isNull());
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
11
extras/tests/MixedConfiguration/decode_unicode_0.cpp
Normal file
11
extras/tests/MixedConfiguration/decode_unicode_0.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#define ARDUINOJSON_DECODE_UNICODE 0
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("ARDUINOJSON_DECODE_UNICODE == 0") {
|
||||
DynamicJsonDocument doc(2048);
|
||||
DeserializationError err = deserializeJson(doc, "\"\\uD834\\uDD1E\"");
|
||||
|
||||
REQUIRE(err == DeserializationError::NotSupported);
|
||||
}
|
11
extras/tests/MixedConfiguration/decode_unicode_1.cpp
Normal file
11
extras/tests/MixedConfiguration/decode_unicode_1.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#define ARDUINOJSON_DECODE_UNICODE 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("ARDUINOJSON_DECODE_UNICODE == 1") {
|
||||
DynamicJsonDocument doc(2048);
|
||||
DeserializationError err = deserializeJson(doc, "\"\\uD834\\uDD1E\"");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
35
extras/tests/MixedConfiguration/enable_infinity_0.cpp
Normal file
35
extras/tests/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
extras/tests/MixedConfiguration/enable_infinity_1.cpp
Normal file
38
extras/tests/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);
|
||||
}
|
||||
}
|
25
extras/tests/MixedConfiguration/enable_nan_0.cpp
Normal file
25
extras/tests/MixedConfiguration/enable_nan_0.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#define ARDUINOJSON_ENABLE_NAN 0
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
#include <limits>
|
||||
|
||||
TEST_CASE("ARDUINOJSON_ENABLE_NAN == 0") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
SECTION("serializeJson()") {
|
||||
root["X"] = std::numeric_limits<double>::signaling_NaN();
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "{\"X\":null}");
|
||||
}
|
||||
|
||||
SECTION("deserializeJson()") {
|
||||
auto err = deserializeJson(doc, "{\"X\":NaN}");
|
||||
|
||||
REQUIRE(err == DeserializationError::InvalidInput);
|
||||
}
|
||||
}
|
31
extras/tests/MixedConfiguration/enable_nan_1.cpp
Normal file
31
extras/tests/MixedConfiguration/enable_nan_1.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#define ARDUINOJSON_ENABLE_NAN 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
#include <limits>
|
||||
|
||||
namespace my {
|
||||
using ARDUINOJSON_NAMESPACE::isnan;
|
||||
} // namespace my
|
||||
|
||||
TEST_CASE("ARDUINOJSON_ENABLE_NAN == 1") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
SECTION("serializeJson()") {
|
||||
root["X"] = std::numeric_limits<double>::signaling_NaN();
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "{\"X\":NaN}");
|
||||
}
|
||||
|
||||
SECTION("deserializeJson()") {
|
||||
auto err = deserializeJson(doc, "{\"X\":NaN}");
|
||||
float x = doc["X"];
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(my::isnan(x));
|
||||
}
|
||||
}
|
87
extras/tests/MixedConfiguration/enable_progmem_1.cpp
Normal file
87
extras/tests/MixedConfiguration/enable_progmem_1.cpp
Normal file
@ -0,0 +1,87 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include "progmem_emulation.hpp"
|
||||
|
||||
#define ARDUINOJSON_ENABLE_PROGMEM 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("Flash strings") {
|
||||
DynamicJsonDocument doc(2048);
|
||||
|
||||
SECTION("deserializeJson()") {
|
||||
DeserializationError err = deserializeJson(doc, F("{'hello':'world'}"));
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc["hello"] == "world");
|
||||
}
|
||||
|
||||
SECTION("JsonDocument::operator[]") {
|
||||
doc[F("hello")] = F("world");
|
||||
|
||||
REQUIRE(doc["hello"] == "world");
|
||||
}
|
||||
|
||||
SECTION("JsonDocument::add()") {
|
||||
doc.add(F("world"));
|
||||
|
||||
REQUIRE(doc[0] == "world");
|
||||
}
|
||||
|
||||
SECTION("JsonVariant::set()") {
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
|
||||
var.set(F("world"));
|
||||
|
||||
REQUIRE(var == "world");
|
||||
}
|
||||
|
||||
SECTION("MemberProxy::operator==") {
|
||||
doc["hello"] = "world";
|
||||
|
||||
REQUIRE(doc["hello"] == F("world"));
|
||||
}
|
||||
|
||||
SECTION("ElementProxy::operator==") {
|
||||
doc.add("world");
|
||||
|
||||
REQUIRE(doc[0] == F("world"));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("strlen_P") {
|
||||
CHECK(strlen_P(FC("")) == 0);
|
||||
CHECK(strlen_P(FC("a")) == 1);
|
||||
CHECK(strlen_P(FC("ac")) == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("strncmp_P") {
|
||||
CHECK(strncmp_P("a", FC("b"), 0) == 0);
|
||||
CHECK(strncmp_P("a", FC("b"), 1) == -1);
|
||||
CHECK(strncmp_P("b", FC("a"), 1) == 1);
|
||||
CHECK(strncmp_P("a", FC("a"), 0) == 0);
|
||||
CHECK(strncmp_P("a", FC("b"), 2) == -1);
|
||||
CHECK(strncmp_P("b", FC("a"), 2) == 1);
|
||||
CHECK(strncmp_P("a", FC("a"), 2) == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("strcmp_P") {
|
||||
CHECK(strcmp_P("a", FC("b")) == -1);
|
||||
CHECK(strcmp_P("b", FC("a")) == 1);
|
||||
CHECK(strcmp_P("a", FC("a")) == 0);
|
||||
CHECK(strcmp_P("aa", FC("ab")) == -1);
|
||||
CHECK(strcmp_P("ab", FC("aa")) == 1);
|
||||
CHECK(strcmp_P("aa", FC("aa")) == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("memcpy_P") {
|
||||
char dst[4];
|
||||
CHECK(memcpy_P(dst, FC("ABC"), 4) == dst);
|
||||
CHECK(dst[0] == 'A');
|
||||
CHECK(dst[1] == 'B');
|
||||
CHECK(dst[2] == 'C');
|
||||
CHECK(dst[3] == 0);
|
||||
}
|
23
extras/tests/MixedConfiguration/progmem_emulation.hpp
Normal file
23
extras/tests/MixedConfiguration/progmem_emulation.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <stdint.h> // uint8_t
|
||||
#include <string.h> // strcmp, strlen...
|
||||
|
||||
class __FlashStringHelper;
|
||||
|
||||
inline const void* convertPtrToFlash(const void* s) {
|
||||
return reinterpret_cast<const char*>(s) + 42;
|
||||
}
|
||||
|
||||
inline const void* convertFlashToPtr(const void* s) {
|
||||
return reinterpret_cast<const char*>(s) - 42;
|
||||
}
|
||||
|
||||
#define F(X) reinterpret_cast<const __FlashStringHelper*>(convertPtrToFlash(X))
|
||||
#define FC(X) reinterpret_cast<const char*>(convertPtrToFlash(X))
|
||||
|
||||
inline uint8_t pgm_read_byte(const void* p) {
|
||||
return *reinterpret_cast<const uint8_t*>(convertFlashToPtr(p));
|
||||
}
|
17
extras/tests/MixedConfiguration/use_double_0.cpp
Normal file
17
extras/tests/MixedConfiguration/use_double_0.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#define ARDUINOJSON_USE_DOUBLE 0
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("ARDUINOJSON_USE_DOUBLE == 0") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
root["pi"] = 3.14;
|
||||
root["e"] = 2.72;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "{\"pi\":3.14,\"e\":2.72}");
|
||||
}
|
17
extras/tests/MixedConfiguration/use_double_1.cpp
Normal file
17
extras/tests/MixedConfiguration/use_double_1.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#define ARDUINOJSON_USE_DOUBLE 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("ARDUINOJSON_USE_DOUBLE == 1") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
root["pi"] = 3.14;
|
||||
root["e"] = 2.72;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "{\"pi\":3.14,\"e\":2.72}");
|
||||
}
|
30
extras/tests/MixedConfiguration/use_long_long_0.cpp
Normal file
30
extras/tests/MixedConfiguration/use_long_long_0.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#define ARDUINOJSON_USE_LONG_LONG 0
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
template <size_t size_of_long>
|
||||
std::string get_expected_json();
|
||||
|
||||
template <>
|
||||
std::string get_expected_json<4>() {
|
||||
return "{\"A\":2899336981,\"B\":2129924785}";
|
||||
}
|
||||
|
||||
template <>
|
||||
std::string get_expected_json<8>() {
|
||||
return "{\"A\":123456789123456789,\"B\":987654321987654321}";
|
||||
}
|
||||
|
||||
TEST_CASE("ARDUINOJSON_USE_LONG_LONG == 0") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
root["A"] = 123456789123456789;
|
||||
root["B"] = 987654321987654321;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == get_expected_json<sizeof(long)>());
|
||||
}
|
17
extras/tests/MixedConfiguration/use_long_long_1.cpp
Normal file
17
extras/tests/MixedConfiguration/use_long_long_1.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#define ARDUINOJSON_USE_LONG_LONG 1
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("ARDUINOJSON_USE_LONG_LONG == 1") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
root["A"] = 123456789123456789;
|
||||
root["B"] = 987654321987654321;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "{\"A\":123456789123456789,\"B\":987654321987654321}");
|
||||
}
|
Reference in New Issue
Block a user