// ArduinoJson - https://arduinojson.org // Copyright © 2014-2023, Benoit BLANCHON // MIT License #include #include #include #include "Allocators.hpp" using ArduinoJson::detail::sizeofArray; using ArduinoJson::detail::sizeofString; TEST_CASE("JsonArray::operator[]") { SpyingAllocator allocator; JsonDocument doc(&allocator); JsonArray array = doc.to(); SECTION("Pad with null") { array[2] = 2; array[5] = 5; REQUIRE(array.size() == 6); REQUIRE(array[0].isNull() == true); REQUIRE(array[1].isNull() == true); REQUIRE(array[2].isNull() == false); REQUIRE(array[3].isNull() == true); REQUIRE(array[4].isNull() == true); REQUIRE(array[5].isNull() == false); REQUIRE(array[2] == 2); REQUIRE(array[5] == 5); } SECTION("int") { array[0] = 123; REQUIRE(123 == array[0].as()); REQUIRE(true == array[0].is()); REQUIRE(false == array[0].is()); } #if ARDUINOJSON_USE_LONG_LONG SECTION("long long") { array[0] = 9223372036854775807; REQUIRE(9223372036854775807 == array[0].as()); REQUIRE(true == array[0].is()); REQUIRE(false == array[0].is()); REQUIRE(false == array[0].is()); } #endif SECTION("double") { array[0] = 123.45; REQUIRE(123.45 == array[0].as()); REQUIRE(true == array[0].is()); REQUIRE(false == array[0].is()); } SECTION("bool") { array[0] = true; REQUIRE(true == array[0].as()); REQUIRE(true == array[0].is()); REQUIRE(false == array[0].is()); } SECTION("const char*") { const char* str = "hello"; array[0] = str; REQUIRE(str == array[0].as()); REQUIRE(true == array[0].is()); REQUIRE(false == array[0].is()); } SECTION("nested array") { JsonDocument doc2; JsonArray arr2 = doc2.to(); array[0] = arr2; REQUIRE(arr2 == array[0].as()); REQUIRE(true == array[0].is()); REQUIRE(false == array[0].is()); } SECTION("nested object") { JsonDocument doc2; JsonObject obj = doc2.to(); array[0] = obj; REQUIRE(obj == array[0].as()); REQUIRE(true == array[0].is()); REQUIRE(false == array[0].is()); } SECTION("array subscript") { JsonDocument doc2; JsonArray arr2 = doc2.to(); const char* str = "hello"; arr2.add(str); array[0] = arr2[0]; REQUIRE(str == array[0]); } SECTION("object subscript") { const char* str = "hello"; JsonDocument doc2; JsonObject obj = doc2.to(); obj["x"] = str; array[0] = obj["x"]; REQUIRE(str == array[0]); } SECTION("should not duplicate const char*") { array[0] = "world"; REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(sizeofPool())); } SECTION("should duplicate char*") { array[0] = const_cast("world"); REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) << AllocatorLog::Allocate(sizeofString(5))); } SECTION("should duplicate std::string") { array[0] = std::string("world"); REQUIRE(allocator.log() == AllocatorLog() << AllocatorLog::Allocate(sizeofPool()) << AllocatorLog::Allocate(sizeofString(5))); } SECTION("array[0].to()") { JsonObject obj = array[0].to(); REQUIRE(obj.isNull() == false); } #ifdef HAS_VARIABLE_LENGTH_ARRAY SECTION("set(VLA)") { size_t i = 16; char vla[i]; strcpy(vla, "world"); array.add("hello"); array[0].set(vla); REQUIRE(std::string("world") == array[0]); } SECTION("operator=(VLA)") { size_t i = 16; char vla[i]; strcpy(vla, "world"); array.add("hello"); array[0] = vla; REQUIRE(std::string("world") == array[0]); } #endif } TEST_CASE("JsonArrayConst::operator[]") { JsonDocument doc; JsonArray array = doc.to(); array.add(0); SECTION("int") { array[0] = 123; JsonArrayConst carr = array; REQUIRE(123 == carr[0].as()); REQUIRE(true == carr[0].is()); REQUIRE(false == carr[0].is()); } }