mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
initial commit
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <stdlib.h> // malloc, free
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
using ARDUINOJSON_NAMESPACE::addPadding;
|
||||
using ARDUINOJSON_NAMESPACE::move;
|
||||
|
||||
class SpyingAllocator {
|
||||
public:
|
||||
SpyingAllocator(const SpyingAllocator& src) : _log(src._log) {}
|
||||
SpyingAllocator(std::ostream& log) : _log(log) {}
|
||||
|
||||
void* allocate(size_t n) {
|
||||
_log << "A" << n;
|
||||
return malloc(n);
|
||||
}
|
||||
void deallocate(void* p) {
|
||||
_log << "F";
|
||||
free(p);
|
||||
}
|
||||
|
||||
private:
|
||||
SpyingAllocator& operator=(const SpyingAllocator& src);
|
||||
|
||||
std::ostream& _log;
|
||||
};
|
||||
|
||||
class ControllableAllocator {
|
||||
public:
|
||||
ControllableAllocator() : _enabled(true) {}
|
||||
|
||||
void* allocate(size_t n) {
|
||||
return _enabled ? malloc(n) : 0;
|
||||
}
|
||||
|
||||
void deallocate(void* p) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
void disable() {
|
||||
_enabled = false;
|
||||
}
|
||||
|
||||
private:
|
||||
bool _enabled;
|
||||
};
|
||||
|
||||
TEST_CASE("BasicJsonDocument") {
|
||||
std::stringstream log;
|
||||
|
||||
SECTION("Construct/Destruct") {
|
||||
{ BasicJsonDocument<SpyingAllocator> doc(4096, log); }
|
||||
REQUIRE(log.str() == "A4096F");
|
||||
}
|
||||
|
||||
SECTION("Copy construct") {
|
||||
{
|
||||
BasicJsonDocument<SpyingAllocator> doc1(4096, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
|
||||
BasicJsonDocument<SpyingAllocator> doc2(doc1);
|
||||
|
||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
REQUIRE(log.str() == "A4096A4096FF");
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
|
||||
SECTION("Move construct") {
|
||||
{
|
||||
BasicJsonDocument<SpyingAllocator> doc1(4096, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
|
||||
BasicJsonDocument<SpyingAllocator> doc2(move(doc1));
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc1.as<std::string>() == "null");
|
||||
REQUIRE(doc1.capacity() == 0);
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
REQUIRE(log.str() == "A4096F");
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("Copy assign") {
|
||||
{
|
||||
BasicJsonDocument<SpyingAllocator> doc1(4096, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
BasicJsonDocument<SpyingAllocator> doc2(8, log);
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE(doc1.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
REQUIRE(log.str() == "A4096A8FA4096FF");
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_HAS_RVALUE_REFERENCES
|
||||
SECTION("Move assign") {
|
||||
{
|
||||
BasicJsonDocument<SpyingAllocator> doc1(4096, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
BasicJsonDocument<SpyingAllocator> doc2(8, log);
|
||||
|
||||
doc2 = move(doc1);
|
||||
|
||||
REQUIRE(doc2.as<std::string>() == "The size of this string is 32!!");
|
||||
REQUIRE(doc1.as<std::string>() == "null");
|
||||
REQUIRE(doc1.capacity() == 0);
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
REQUIRE(log.str() == "A4096A8FF");
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("garbageCollect()") {
|
||||
BasicJsonDocument<ControllableAllocator> doc(4096);
|
||||
|
||||
SECTION("when allocation succeeds") {
|
||||
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
||||
REQUIRE(doc.capacity() == 4096);
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
|
||||
doc.remove("blanket");
|
||||
|
||||
bool result = doc.garbageCollect();
|
||||
|
||||
REQUIRE(result == true);
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 8);
|
||||
REQUIRE(doc.capacity() == 4096);
|
||||
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
||||
}
|
||||
|
||||
SECTION("when allocation fails") {
|
||||
deserializeJson(doc, "{\"blanket\":1,\"dancing\":2}");
|
||||
REQUIRE(doc.capacity() == 4096);
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
|
||||
doc.remove("blanket");
|
||||
doc.allocator().disable();
|
||||
|
||||
bool result = doc.garbageCollect();
|
||||
|
||||
REQUIRE(result == false);
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2) + 16);
|
||||
REQUIRE(doc.capacity() == 4096);
|
||||
REQUIRE(doc.as<std::string>() == "{\"dancing\":2}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2020
|
||||
# MIT License
|
||||
|
||||
add_executable(JsonDocumentTests
|
||||
add.cpp
|
||||
BasicJsonDocument.cpp
|
||||
compare.cpp
|
||||
containsKey.cpp
|
||||
createNested.cpp
|
||||
DynamicJsonDocument.cpp
|
||||
isNull.cpp
|
||||
nesting.cpp
|
||||
remove.cpp
|
||||
shrinkToFit.cpp
|
||||
size.cpp
|
||||
StaticJsonDocument.cpp
|
||||
subscript.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(JsonDocumentTests catch)
|
||||
add_test(JsonDocument JsonDocumentTests)
|
||||
@@ -0,0 +1,209 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using ARDUINOJSON_NAMESPACE::addPadding;
|
||||
|
||||
static void REQUIRE_JSON(JsonDocument& doc, const std::string& expected) {
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
REQUIRE(json == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicJsonDocument") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("serializeJson()") {
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = "world";
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("memoryUsage()") {
|
||||
SECTION("starts at zero") {
|
||||
REQUIRE(doc.memoryUsage() == 0);
|
||||
}
|
||||
|
||||
SECTION("JSON_ARRAY_SIZE(0)") {
|
||||
doc.to<JsonArray>();
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
|
||||
}
|
||||
|
||||
SECTION("JSON_ARRAY_SIZE(1)") {
|
||||
doc.to<JsonArray>().add(42);
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1));
|
||||
}
|
||||
|
||||
SECTION("JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(0)") {
|
||||
doc.to<JsonArray>().createNestedArray();
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(0));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("capacity()") {
|
||||
SECTION("matches constructor argument") {
|
||||
DynamicJsonDocument doc2(256);
|
||||
REQUIRE(doc2.capacity() == 256);
|
||||
}
|
||||
|
||||
SECTION("rounds up constructor argument") {
|
||||
DynamicJsonDocument doc2(253);
|
||||
REQUIRE(doc2.capacity() == 256);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("memoryUsage()") {
|
||||
SECTION("Increases after adding value to array") {
|
||||
JsonArray arr = doc.to<JsonArray>();
|
||||
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(0));
|
||||
arr.add(42);
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(1));
|
||||
arr.add(43);
|
||||
REQUIRE(doc.memoryUsage() == JSON_ARRAY_SIZE(2));
|
||||
}
|
||||
|
||||
SECTION("Increases after adding value to object") {
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
|
||||
obj["a"] = 1;
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1));
|
||||
obj["b"] = 2;
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(2));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicJsonDocument constructor") {
|
||||
SECTION("Copy constructor") {
|
||||
DynamicJsonDocument doc1(1234);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
DynamicJsonDocument doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||
}
|
||||
|
||||
SECTION("Construct from StaticJsonDocument") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
DynamicJsonDocument doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonObject") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
JsonObject obj = doc1.to<JsonObject>();
|
||||
obj["hello"] = "world";
|
||||
|
||||
DynamicJsonDocument doc2 = obj;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonArray") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
JsonArray arr = doc1.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
|
||||
DynamicJsonDocument doc2 = arr;
|
||||
|
||||
REQUIRE_JSON(doc2, "[\"hello\"]");
|
||||
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonVariant") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
DynamicJsonDocument doc2 = doc1.as<JsonVariant>();
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicJsonDocument assignment") {
|
||||
SECTION("Copy assignment preserves the buffer when capacity is sufficient") {
|
||||
DynamicJsonDocument doc1(1234);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
DynamicJsonDocument doc2(doc1.capacity());
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||
}
|
||||
|
||||
SECTION("Copy assignment realloc the buffer when capacity is insufficient") {
|
||||
DynamicJsonDocument doc1(1234);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
DynamicJsonDocument doc2(8);
|
||||
|
||||
REQUIRE(doc2.capacity() < doc1.memoryUsage());
|
||||
doc2 = doc1;
|
||||
REQUIRE(doc2.capacity() >= doc1.memoryUsage());
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from StaticJsonDocument") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc2.to<JsonVariant>().set(666);
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonObject") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
JsonObject obj = doc1.to<JsonObject>();
|
||||
obj["hello"] = "world";
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc2 = obj;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonArray") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
JsonArray arr = doc1.to<JsonArray>();
|
||||
arr.add("hello");
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc2 = arr;
|
||||
|
||||
REQUIRE_JSON(doc2, "[\"hello\"]");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonVariant") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc2 = doc1.as<JsonVariant>();
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
REQUIRE(doc2.capacity() == 4096);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void REQUIRE_JSON(JsonDocument& doc, const std::string& expected) {
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
REQUIRE(json == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("StaticJsonDocument") {
|
||||
SECTION("capacity()") {
|
||||
SECTION("matches template argument") {
|
||||
StaticJsonDocument<256> doc;
|
||||
REQUIRE(doc.capacity() == 256);
|
||||
}
|
||||
|
||||
SECTION("rounds up template argument") {
|
||||
StaticJsonDocument<253> doc;
|
||||
REQUIRE(doc.capacity() == 256);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("serializeJson()") {
|
||||
StaticJsonDocument<200> doc;
|
||||
JsonObject obj = doc.to<JsonObject>();
|
||||
obj["hello"] = "world";
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Copy assignment") {
|
||||
StaticJsonDocument<200> doc1, doc2;
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc2, "{\"hello\":\"world\"}");
|
||||
|
||||
doc1 = doc2;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Contructor") {
|
||||
SECTION("Copy constructor") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1;
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Construct from StaticJsonDocument of different size") {
|
||||
StaticJsonDocument<300> doc1;
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Construct from DynamicJsonDocument") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonObject") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1.as<JsonObject>();
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonArray") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
deserializeJson(doc1, "[\"hello\",\"world\"]");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1.as<JsonArray>();
|
||||
|
||||
deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]");
|
||||
REQUIRE_JSON(doc2, "[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
SECTION("Construct from JsonVariant") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1.as<JsonVariant>();
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Assignment") {
|
||||
SECTION("Copy assignment") {
|
||||
StaticJsonDocument<200> doc1, doc2;
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from StaticJsonDocument of different capacity") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
StaticJsonDocument<300> doc2;
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from DynamicJsonDocument") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
doc2 = doc1;
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonArray") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "[\"hello\",\"world\"]");
|
||||
|
||||
doc2 = doc1.as<JsonArray>();
|
||||
|
||||
deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]");
|
||||
REQUIRE_JSON(doc2, "[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonArrayConst") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "[\"hello\",\"world\"]");
|
||||
|
||||
doc2 = doc1.as<JsonArrayConst>();
|
||||
|
||||
deserializeJson(doc1, "[\"HELLO\",\"WORLD\"]");
|
||||
REQUIRE_JSON(doc2, "[\"hello\",\"world\"]");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonObject") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
doc2 = doc1.as<JsonObject>();
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonObjectConst") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
|
||||
doc2 = doc1.as<JsonObjectConst>();
|
||||
|
||||
deserializeJson(doc1, "{\"HELLO\":\"WORLD\"}");
|
||||
REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonVariant") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
StaticJsonDocument<200> doc2;
|
||||
doc2 = doc1.as<JsonVariant>();
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
}
|
||||
|
||||
SECTION("Assign from JsonVariantConst") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc1, "42");
|
||||
|
||||
StaticJsonDocument<200> doc2;
|
||||
doc2 = doc1.as<JsonVariantConst>();
|
||||
|
||||
REQUIRE_JSON(doc2, "42");
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("garbageCollect()") {
|
||||
StaticJsonDocument<256> doc;
|
||||
doc[std::string("example")] = std::string("example");
|
||||
doc.remove("example");
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 16);
|
||||
|
||||
doc.garbageCollect();
|
||||
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(0));
|
||||
REQUIRE_JSON(doc, "{}");
|
||||
}
|
||||
}
|
||||
22
lib_standalone/ArduinoJson/extras/tests/JsonDocument/add.cpp
Normal file
22
lib_standalone/ArduinoJson/extras/tests/JsonDocument/add.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::add()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("integer") {
|
||||
doc.add(42);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[42]");
|
||||
}
|
||||
|
||||
SECTION("const char*") {
|
||||
doc.add("hello");
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[\"hello\"]");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("DynamicJsonDocument::operator==(const DynamicJsonDocument&)") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
DynamicJsonDocument doc2(4096);
|
||||
|
||||
SECTION("Empty") {
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With same object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["hello"] = "world";
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
SECTION("With different object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["world"] = "hello";
|
||||
REQUIRE_FALSE(doc1 == doc2);
|
||||
REQUIRE(doc1 != doc2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicJsonDocument::operator==(const StaticJsonDocument&)") {
|
||||
DynamicJsonDocument doc1(4096);
|
||||
StaticJsonDocument<256> doc2;
|
||||
|
||||
SECTION("Empty") {
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With same object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["hello"] = "world";
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With different object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["world"] = "hello";
|
||||
REQUIRE_FALSE(doc1 == doc2);
|
||||
REQUIRE(doc1 != doc2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("StaticJsonDocument::operator==(const DynamicJsonDocument&)") {
|
||||
StaticJsonDocument<256> doc1;
|
||||
DynamicJsonDocument doc2(4096);
|
||||
|
||||
SECTION("Empty") {
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With same object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["hello"] = "world";
|
||||
REQUIRE(doc1 == doc2);
|
||||
REQUIRE_FALSE(doc1 != doc2);
|
||||
}
|
||||
|
||||
SECTION("With different object") {
|
||||
doc1["hello"] = "world";
|
||||
doc2["world"] = "hello";
|
||||
REQUIRE_FALSE(doc1 == doc2);
|
||||
REQUIRE(doc1 != doc2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::containsKey()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("returns true on object") {
|
||||
doc["hello"] = "world";
|
||||
|
||||
REQUIRE(doc.containsKey("hello") == true);
|
||||
}
|
||||
|
||||
SECTION("returns true when value is null") {
|
||||
doc["hello"] = static_cast<const char*>(0);
|
||||
|
||||
REQUIRE(doc.containsKey("hello") == true);
|
||||
}
|
||||
|
||||
SECTION("returns true when key is a std::string") {
|
||||
doc["hello"] = "world";
|
||||
|
||||
REQUIRE(doc.containsKey(std::string("hello")) == true);
|
||||
}
|
||||
|
||||
SECTION("returns false on object") {
|
||||
doc["world"] = "hello";
|
||||
|
||||
REQUIRE(doc.containsKey("hello") == false);
|
||||
}
|
||||
|
||||
SECTION("returns false on array") {
|
||||
doc.add("hello");
|
||||
|
||||
REQUIRE(doc.containsKey("hello") == false);
|
||||
}
|
||||
|
||||
SECTION("returns false on null") {
|
||||
REQUIRE(doc.containsKey("hello") == false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::createNestedArray()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("promotes to array") {
|
||||
doc.createNestedArray();
|
||||
|
||||
REQUIRE(doc.is<JsonArray>());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument::createNestedArray(key)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("key is const char*") {
|
||||
SECTION("promotes to object") {
|
||||
doc.createNestedArray("hello");
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("key is std::string") {
|
||||
SECTION("promotes to object") {
|
||||
doc.createNestedArray(std::string("hello"));
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument::createNestedObject()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("promotes to array") {
|
||||
doc.createNestedObject();
|
||||
|
||||
REQUIRE(doc.is<JsonArray>());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument::createNestedObject(key)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("key is const char*") {
|
||||
SECTION("promotes to object") {
|
||||
doc.createNestedObject("hello");
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("key is std::string") {
|
||||
SECTION("promotes to object") {
|
||||
doc.createNestedObject(std::string("hello"));
|
||||
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::isNull()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("returns true if uninitialized") {
|
||||
REQUIRE(doc.isNull() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false after to<JsonObject>()") {
|
||||
doc.to<JsonObject>();
|
||||
REQUIRE(doc.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("returns false after to<JsonArray>()") {
|
||||
doc.to<JsonArray>();
|
||||
REQUIRE(doc.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true after to<JsonVariant>()") {
|
||||
REQUIRE(doc.isNull() == true);
|
||||
}
|
||||
|
||||
SECTION("returns false after set()") {
|
||||
doc.to<JsonVariant>().set(42);
|
||||
REQUIRE(doc.isNull() == false);
|
||||
}
|
||||
|
||||
SECTION("returns true after clear()") {
|
||||
doc.to<JsonObject>();
|
||||
doc.clear();
|
||||
REQUIRE(doc.isNull() == true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::nesting()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("return 0 if uninitialized") {
|
||||
REQUIRE(doc.nesting() == 0);
|
||||
}
|
||||
|
||||
SECTION("returns 0 for string") {
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
var.set("hello");
|
||||
REQUIRE(doc.nesting() == 0);
|
||||
}
|
||||
|
||||
SECTION("returns 1 for empty object") {
|
||||
doc.to<JsonObject>();
|
||||
REQUIRE(doc.nesting() == 1);
|
||||
}
|
||||
|
||||
SECTION("returns 1 for empty array") {
|
||||
doc.to<JsonArray>();
|
||||
REQUIRE(doc.nesting() == 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::remove()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("remove(int)") {
|
||||
doc.add(1);
|
||||
doc.add(2);
|
||||
doc.add(3);
|
||||
|
||||
doc.remove(1);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[1,3]");
|
||||
}
|
||||
|
||||
SECTION("remove(const char *)") {
|
||||
doc["a"] = 1;
|
||||
doc["b"] = 2;
|
||||
|
||||
doc.remove("a");
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"b\":2}");
|
||||
}
|
||||
|
||||
SECTION("remove(std::string)") {
|
||||
doc["a"] = 1;
|
||||
doc["b"] = 2;
|
||||
|
||||
doc.remove(std::string("b"));
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
|
||||
}
|
||||
|
||||
#ifdef HAS_VARIABLE_LENGTH_ARRAY
|
||||
SECTION("remove(vla)") {
|
||||
doc["a"] = 1;
|
||||
doc["b"] = 2;
|
||||
|
||||
int i = 4;
|
||||
char vla[i];
|
||||
strcpy(vla, "b");
|
||||
doc.remove(vla);
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "{\"a\":1}");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#include <stdlib.h> // malloc, free
|
||||
#include <string>
|
||||
|
||||
using ARDUINOJSON_NAMESPACE::addPadding;
|
||||
|
||||
class ArmoredAllocator {
|
||||
public:
|
||||
ArmoredAllocator() : _ptr(0), _size(0) {}
|
||||
|
||||
void* allocate(size_t size) {
|
||||
_ptr = malloc(size);
|
||||
_size = size;
|
||||
return _ptr;
|
||||
}
|
||||
|
||||
void deallocate(void* ptr) {
|
||||
REQUIRE(ptr == _ptr);
|
||||
free(ptr);
|
||||
_ptr = 0;
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
void* reallocate(void* ptr, size_t new_size) {
|
||||
REQUIRE(ptr == _ptr);
|
||||
// don't call realloc, instead alloc a new buffer and erase the old one
|
||||
// this way we make sure we support relocation
|
||||
void* new_ptr = malloc(new_size);
|
||||
memcpy(new_ptr, _ptr, std::min(new_size, _size));
|
||||
memset(_ptr, '#', _size); // erase
|
||||
free(_ptr);
|
||||
_ptr = new_ptr;
|
||||
return new_ptr;
|
||||
}
|
||||
|
||||
private:
|
||||
void* _ptr;
|
||||
size_t _size;
|
||||
};
|
||||
|
||||
typedef BasicJsonDocument<ArmoredAllocator> ShrinkToFitTestDocument;
|
||||
|
||||
void testShrinkToFit(ShrinkToFitTestDocument& doc, std::string expected_json,
|
||||
size_t expected_size) {
|
||||
// test twice: shrinkToFit() should be idempotent
|
||||
for (int i = 0; i < 2; i++) {
|
||||
doc.shrinkToFit();
|
||||
|
||||
REQUIRE(doc.capacity() == expected_size);
|
||||
REQUIRE(doc.memoryUsage() == expected_size);
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
REQUIRE(json == expected_json);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("BasicJsonDocument::shrinkToFit()") {
|
||||
ShrinkToFitTestDocument doc(4096);
|
||||
|
||||
SECTION("null") {
|
||||
testShrinkToFit(doc, "null", 0);
|
||||
}
|
||||
|
||||
SECTION("empty object") {
|
||||
deserializeJson(doc, "{}");
|
||||
testShrinkToFit(doc, "{}", JSON_OBJECT_SIZE(0));
|
||||
}
|
||||
|
||||
SECTION("empty array") {
|
||||
deserializeJson(doc, "[]");
|
||||
testShrinkToFit(doc, "[]", JSON_ARRAY_SIZE(0));
|
||||
}
|
||||
|
||||
SECTION("linked string") {
|
||||
doc.set("hello");
|
||||
testShrinkToFit(doc, "\"hello\"", 0);
|
||||
}
|
||||
|
||||
SECTION("owned string") {
|
||||
doc.set(std::string("abcdefg"));
|
||||
testShrinkToFit(doc, "\"abcdefg\"", 8);
|
||||
}
|
||||
|
||||
SECTION("linked raw") {
|
||||
doc.set(serialized("[{},123]"));
|
||||
testShrinkToFit(doc, "[{},123]", 0);
|
||||
}
|
||||
|
||||
SECTION("owned raw") {
|
||||
doc.set(serialized(std::string("[{},123]")));
|
||||
testShrinkToFit(doc, "[{},123]", 8);
|
||||
}
|
||||
|
||||
SECTION("linked key") {
|
||||
doc["key"] = 42;
|
||||
testShrinkToFit(doc, "{\"key\":42}", JSON_OBJECT_SIZE(1));
|
||||
}
|
||||
|
||||
SECTION("owned key") {
|
||||
doc[std::string("abcdefg")] = 42;
|
||||
testShrinkToFit(doc, "{\"abcdefg\":42}", JSON_OBJECT_SIZE(1) + 8);
|
||||
}
|
||||
|
||||
SECTION("linked string in array") {
|
||||
doc.add("hello");
|
||||
testShrinkToFit(doc, "[\"hello\"]", JSON_ARRAY_SIZE(1));
|
||||
}
|
||||
|
||||
SECTION("owned string in array") {
|
||||
doc.add(std::string("abcdefg"));
|
||||
testShrinkToFit(doc, "[\"abcdefg\"]", JSON_ARRAY_SIZE(1) + 8);
|
||||
}
|
||||
|
||||
SECTION("linked string in object") {
|
||||
doc["key"] = "hello";
|
||||
testShrinkToFit(doc, "{\"key\":\"hello\"}", JSON_OBJECT_SIZE(1));
|
||||
}
|
||||
|
||||
SECTION("owned string in object") {
|
||||
doc["key"] = std::string("abcdefg");
|
||||
testShrinkToFit(doc, "{\"key\":\"abcdefg\"}", JSON_ARRAY_SIZE(1) + 8);
|
||||
}
|
||||
|
||||
SECTION("unaligned") {
|
||||
doc.add(std::string("?")); // two bytes in the string pool
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 2);
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
// the new capacity should be padded to align the pointers
|
||||
REQUIRE(doc.capacity() == JSON_OBJECT_SIZE(1) + sizeof(void*));
|
||||
REQUIRE(doc.memoryUsage() == JSON_OBJECT_SIZE(1) + 2);
|
||||
REQUIRE(doc[0] == "?");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("DynamicJsonDocument::shrinkToFit()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
deserializeJson(doc, "{\"hello\":[\"world\"]");
|
||||
|
||||
doc.shrinkToFit();
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc, json);
|
||||
REQUIRE(json == "{\"hello\":[\"world\"]}");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::size()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("returns 0") {
|
||||
REQUIRE(doc.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("as an array, return 2") {
|
||||
doc.add(1);
|
||||
doc.add(2);
|
||||
|
||||
REQUIRE(doc.size() == 2);
|
||||
}
|
||||
|
||||
SECTION("as an object, return 2") {
|
||||
doc["a"] = 1;
|
||||
doc["b"] = 2;
|
||||
|
||||
REQUIRE(doc.size() == 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2020
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::operator[]") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
const JsonDocument& cdoc = doc;
|
||||
|
||||
SECTION("object") {
|
||||
deserializeJson(doc, "{\"hello\":\"world\"}");
|
||||
|
||||
SECTION("const char*") {
|
||||
REQUIRE(doc["hello"] == "world");
|
||||
REQUIRE(cdoc["hello"] == "world");
|
||||
}
|
||||
|
||||
SECTION("std::string") {
|
||||
REQUIRE(doc[std::string("hello")] == "world");
|
||||
REQUIRE(cdoc[std::string("hello")] == "world");
|
||||
}
|
||||
|
||||
SECTION("supports operator|") {
|
||||
REQUIRE((doc["hello"] | "nope") == std::string("world"));
|
||||
REQUIRE((doc["world"] | "nope") == std::string("nope"));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("array") {
|
||||
deserializeJson(doc, "[\"hello\",\"world\"]");
|
||||
|
||||
REQUIRE(doc[1] == "world");
|
||||
REQUIRE(cdoc[1] == "world");
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument automatically promotes to object") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
doc["one"]["two"]["three"] = 4;
|
||||
|
||||
REQUIRE(doc["one"]["two"]["three"] == 4);
|
||||
}
|
||||
|
||||
TEST_CASE("JsonDocument automatically promotes to array") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
doc[2] = 2;
|
||||
|
||||
REQUIRE(doc.as<std::string>() == "[null,null,2]");
|
||||
}
|
||||
Reference in New Issue
Block a user