123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- // ArduinoJson - arduinojson.org
- // Copyright Benoit Blanchon 2014-2019
- // 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() == addPadding(doc1.memoryUsage()));
- }
- SECTION("Construct from StaticJsonDocument") {
- StaticJsonDocument<200> doc1;
- deserializeJson(doc1, "{\"hello\":\"world\"}");
- DynamicJsonDocument doc2 = doc1;
- REQUIRE_JSON(doc2, "{\"hello\":\"world\"}");
- REQUIRE(doc2.capacity() == addPadding(doc1.memoryUsage()));
- }
- 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);
- }
- }
|