// ArduinoJson - https://arduinojson.org // Copyright © 2014-2023, Benoit BLANCHON // MIT License #include #include #include "Allocators.hpp" using ArduinoJson::detail::sizeofArray; using ArduinoJson::detail::sizeofString; TEST_CASE("JsonArray::add()") { SpyingAllocator allocator; JsonDocument doc(&allocator); JsonArray array = doc.to(); SECTION("int") { array.add(123); REQUIRE(123 == array[0].as()); REQUIRE(array[0].is()); REQUIRE(array[0].is()); } SECTION("double") { array.add(123.45); REQUIRE(123.45 == array[0].as()); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } SECTION("bool") { array.add(true); REQUIRE(true == array[0].as()); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } SECTION("const char*") { const char* str = "hello"; array.add(str); REQUIRE(str == array[0].as()); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } #ifdef HAS_VARIABLE_LENGTH_ARRAY SECTION("vla") { size_t i = 16; char vla[i]; strcpy(vla, "world"); array.add(vla); REQUIRE(std::string("world") == array[0]); } #endif SECTION("nested array") { JsonDocument doc2; JsonArray arr = doc2.to(); array.add(arr); REQUIRE(arr == array[0].as()); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } SECTION("nested object") { JsonDocument doc2; JsonObject obj = doc2.to(); array.add(obj); REQUIRE(obj == array[0].as()); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } SECTION("array subscript") { const char* str = "hello"; JsonDocument doc2; JsonArray arr = doc2.to(); arr.add(str); array.add(arr[0]); REQUIRE(str == array[0]); } SECTION("object subscript") { const char* str = "hello"; JsonDocument doc2; JsonObject obj = doc2.to(); obj["x"] = str; array.add(obj["x"]); REQUIRE(str == array[0]); } SECTION("should not duplicate const char*") { array.add("world"); REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(sizeofPool())); } SECTION("should duplicate char*") { array.add(const_cast("world")); REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) << AllocatorLog::Allocate(sizeofString(5))); } SECTION("should duplicate std::string") { array.add(std::string("world")); REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) << AllocatorLog::Allocate(sizeofString(5))); } SECTION("should duplicate serialized(const char*)") { array.add(serialized("{}")); REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) << AllocatorLog::Allocate(sizeofString(2))); } SECTION("should duplicate serialized(char*)") { array.add(serialized(const_cast("{}"))); REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) << AllocatorLog::Allocate(sizeofString(2))); } SECTION("should duplicate serialized(std::string)") { array.add(serialized(std::string("{}"))); REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) << AllocatorLog::Allocate(sizeofString(2))); } SECTION("should duplicate serialized(std::string)") { array.add(serialized(std::string("\0XX", 3))); REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) << AllocatorLog::Allocate(sizeofString(3))); } }