add arduinoJson
This commit is contained in:
36
lib/ArduinoJson/extras/tests/Cpp11/CMakeLists.txt
Normal file
36
lib/ArduinoJson/extras/tests/Cpp11/CMakeLists.txt
Normal file
@@ -0,0 +1,36 @@
|
||||
# ArduinoJson - https://arduinojson.org
|
||||
# Copyright © 2014-2022, Benoit BLANCHON
|
||||
# MIT License
|
||||
|
||||
if("cxx_nullptr" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
|
||||
list(APPEND SOURCES nullptr.cpp)
|
||||
add_definitions(-DARDUINOJSON_HAS_NULLPTR=1)
|
||||
endif()
|
||||
|
||||
if("cxx_auto_type" IN_LIST CMAKE_CXX_COMPILE_FEATURES AND "cxx_constexpr" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
|
||||
list(APPEND SOURCES issue1120.cpp)
|
||||
endif()
|
||||
|
||||
if("cxx_long_long_type" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
|
||||
list(APPEND SOURCES use_long_long_0.cpp use_long_long_1.cpp)
|
||||
endif()
|
||||
|
||||
if("cxx_range_for" IN_LIST CMAKE_CXX_COMPILE_FEATURES AND "cxx_generalized_initializers" IN_LIST CMAKE_CXX_COMPILE_FEATURES)
|
||||
list(APPEND SOURCES stl_containers.cpp)
|
||||
endif()
|
||||
|
||||
if(NOT SOURCES)
|
||||
return()
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
add_executable(Cpp11Tests ${SOURCES})
|
||||
|
||||
add_test(Cpp11 Cpp11Tests)
|
||||
|
||||
set_tests_properties(Cpp11
|
||||
PROPERTIES
|
||||
LABELS "Catch"
|
||||
)
|
||||
58
lib/ArduinoJson/extras/tests/Cpp11/issue1120.cpp
Normal file
58
lib/ArduinoJson/extras/tests/Cpp11/issue1120.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("Issue #1120") {
|
||||
StaticJsonDocument<500> doc;
|
||||
constexpr char str[] =
|
||||
"{\"contents\":[{\"module\":\"Packet\"},{\"module\":\"Analog\"}]}";
|
||||
deserializeJson(doc, str);
|
||||
|
||||
SECTION("MemberProxy<std::string>::isNull()") {
|
||||
SECTION("returns false") {
|
||||
auto value = doc[std::string("contents")];
|
||||
CHECK(value.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true") {
|
||||
auto value = doc[std::string("zontents")];
|
||||
CHECK(value.isNull() == true);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("ElementProxy<MemberProxy<const char*> >::isNull()") {
|
||||
SECTION("returns false") { // Issue #1120
|
||||
auto value = doc["contents"][1];
|
||||
CHECK(value.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true") {
|
||||
auto value = doc["contents"][2];
|
||||
CHECK(value.isNull() == true);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("MemberProxy<ElementProxy<MemberProxy>, const char*>::isNull()") {
|
||||
SECTION("returns false") {
|
||||
auto value = doc["contents"][1]["module"];
|
||||
CHECK(value.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true") {
|
||||
auto value = doc["contents"][1]["zodule"];
|
||||
CHECK(value.isNull() == true);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("MemberProxy<ElementProxy<MemberProxy>, std::string>::isNull()") {
|
||||
SECTION("returns false") {
|
||||
auto value = doc["contents"][1][std::string("module")];
|
||||
CHECK(value.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true") {
|
||||
auto value = doc["contents"][1][std::string("zodule")];
|
||||
CHECK(value.isNull() == true);
|
||||
}
|
||||
}
|
||||
}
|
||||
47
lib/ArduinoJson/extras/tests/Cpp11/nullptr.cpp
Normal file
47
lib/ArduinoJson/extras/tests/Cpp11/nullptr.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
#if !ARDUINOJSON_HAS_NULLPTR
|
||||
# error ARDUINOJSON_HAS_NULLPTR must be set to 1
|
||||
#endif
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
SECTION("JsonVariant.set(nullptr) with unbound reference") {
|
||||
JsonVariant unboundReference;
|
||||
|
||||
unboundReference.set(nullptr);
|
||||
|
||||
REQUIRE(variant.isNull());
|
||||
}
|
||||
|
||||
SECTION("JsonVariant.is<nullptr_t>()") {
|
||||
variant.set(42);
|
||||
REQUIRE(variant.is<std::nullptr_t>() == false);
|
||||
|
||||
variant.clear();
|
||||
REQUIRE(variant.is<std::nullptr_t>() == true);
|
||||
}
|
||||
}
|
||||
138
lib/ArduinoJson/extras/tests/Cpp11/stl_containers.cpp
Normal file
138
lib/ArduinoJson/extras/tests/Cpp11/stl_containers.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
// ArduinoJson - https://arduinojson.org
|
||||
// Copyright © 2014-2022, Benoit BLANCHON
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <stdint.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
template <typename T>
|
||||
struct Converter<std::vector<T> > {
|
||||
static void toJson(const std::vector<T>& src, JsonVariant dst) {
|
||||
JsonArray array = dst.to<JsonArray>();
|
||||
for (T item : src)
|
||||
array.add(item);
|
||||
}
|
||||
|
||||
static std::vector<T> fromJson(JsonVariantConst src) {
|
||||
std::vector<T> dst;
|
||||
for (T item : src.as<JsonArrayConst>())
|
||||
dst.push_back(item);
|
||||
return dst;
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
JsonArrayConst array = src;
|
||||
bool result = array;
|
||||
for (JsonVariantConst item : array)
|
||||
result &= item.is<T>();
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, size_t N>
|
||||
struct Converter<std::array<T, N> > {
|
||||
static void toJson(const std::array<T, N>& src, JsonVariant dst) {
|
||||
JsonArray array = dst.to<JsonArray>();
|
||||
for (T item : src)
|
||||
array.add(item);
|
||||
}
|
||||
|
||||
static std::array<T, N> fromJson(JsonVariantConst src) {
|
||||
std::array<T, N> dst;
|
||||
dst.fill(0);
|
||||
size_t idx = 0;
|
||||
for (T item : src.as<JsonArrayConst>())
|
||||
dst[idx++] = item;
|
||||
return dst;
|
||||
}
|
||||
|
||||
static bool checkJson(JsonVariantConst src) {
|
||||
JsonArrayConst array = src;
|
||||
bool result = array;
|
||||
size_t size = 0;
|
||||
for (JsonVariantConst item : array) {
|
||||
result &= item.is<T>();
|
||||
size++;
|
||||
}
|
||||
return result && size == N;
|
||||
}
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
TEST_CASE("vector<int>") {
|
||||
SECTION("toJson") {
|
||||
std::vector<int> v = {1, 2};
|
||||
|
||||
StaticJsonDocument<128> doc;
|
||||
doc.set(v);
|
||||
REQUIRE(doc.as<std::string>() == "[1,2]");
|
||||
}
|
||||
|
||||
SECTION("fromJson") {
|
||||
StaticJsonDocument<128> doc;
|
||||
doc.add(1);
|
||||
doc.add(2);
|
||||
|
||||
auto v = doc.as<std::vector<int> >();
|
||||
REQUIRE(v.size() == 2);
|
||||
CHECK(v[0] == 1);
|
||||
CHECK(v[1] == 2);
|
||||
}
|
||||
|
||||
SECTION("checkJson") {
|
||||
StaticJsonDocument<128> doc;
|
||||
CHECK(doc.is<std::vector<int> >() == false);
|
||||
|
||||
doc.add(1);
|
||||
doc.add(2);
|
||||
CHECK(doc.is<std::vector<int> >() == true);
|
||||
|
||||
doc.add("foo");
|
||||
CHECK(doc.is<std::vector<int> >() == false);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("array<int, 2>") {
|
||||
typedef std::array<int, 2> array_type;
|
||||
|
||||
SECTION("toJson") {
|
||||
array_type v;
|
||||
v[0] = 1;
|
||||
v[1] = 2;
|
||||
|
||||
StaticJsonDocument<128> doc;
|
||||
doc.set(v);
|
||||
REQUIRE(doc.as<std::string>() == "[1,2]");
|
||||
}
|
||||
|
||||
SECTION("fromJson") {
|
||||
StaticJsonDocument<128> doc;
|
||||
doc.add(1);
|
||||
doc.add(2);
|
||||
|
||||
auto v = doc.as<array_type>();
|
||||
REQUIRE(v.size() == 2);
|
||||
CHECK(v[0] == 1);
|
||||
CHECK(v[1] == 2);
|
||||
}
|
||||
|
||||
SECTION("checkJson") {
|
||||
StaticJsonDocument<128> doc;
|
||||
CHECK(doc.is<array_type>() == false);
|
||||
|
||||
doc.add(1);
|
||||
CHECK(doc.is<array_type>() == false);
|
||||
|
||||
doc.add(2);
|
||||
CHECK(doc.is<array_type>() == true);
|
||||
|
||||
doc[0] = "foo";
|
||||
CHECK(doc.is<array_type>() == false);
|
||||
}
|
||||
}
|
||||
16
lib/ArduinoJson/extras/tests/Cpp11/use_long_long_0.cpp
Normal file
16
lib/ArduinoJson/extras/tests/Cpp11/use_long_long_0.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#define ARDUINOJSON_USE_LONG_LONG 0
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("ARDUINOJSON_USE_LONG_LONG == 0") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
doc["A"] = 42;
|
||||
doc["B"] = 84;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "{\"A\":42,\"B\":84}");
|
||||
}
|
||||
17
lib/ArduinoJson/extras/tests/Cpp11/use_long_long_1.cpp
Normal file
17
lib/ArduinoJson/extras/tests/Cpp11/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