// ArduinoJson - arduinojson.org // Copyright Benoit Blanchon 2014-2018 // MIT License #include #include using namespace Catch::Matchers; TEST_CASE("JsonArray::set()") { DynamicJsonDocument doc; JsonArray array = doc.to(); array.add(0); SECTION("int") { array.set(0, 123); REQUIRE(123 == array[0].as()); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } SECTION("double") { array.set(0, 123.45); REQUIRE(123.45 == array[0].as()); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } SECTION("bool") { array.set(0, true); REQUIRE(true == array[0].as()); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } SECTION("const char*") { array.set(0, "hello"); REQUIRE_THAT(array[0].as(), Equals("hello")); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } #ifdef HAS_VARIABLE_LENGTH_ARRAY SECTION("set()") { int i = 16; char vla[i]; strcpy(vla, "world"); array.add("hello"); array.set(0, vla); REQUIRE(std::string("world") == array[0]); } #endif SECTION("nested array") { DynamicJsonDocument doc2; JsonArray arr = doc2.to(); array.set(0, arr); REQUIRE(arr == array[0].as()); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } SECTION("nested object") { DynamicJsonDocument doc2; JsonObject obj = doc2.to(); array.set(0, obj); REQUIRE(obj == array[0].as()); REQUIRE(array[0].is()); REQUIRE_FALSE(array[0].is()); } SECTION("array subscript") { DynamicJsonDocument doc2; JsonArray arr = doc2.to(); arr.add("hello"); array.set(0, arr[0]); REQUIRE_THAT(array[0].as(), Equals("hello")); } SECTION("object subscript") { DynamicJsonDocument doc2; JsonObject obj = doc2.to(); obj["x"] = "hello"; array.set(0, obj["x"]); REQUIRE_THAT(array[0].as(), Equals("hello")); } SECTION("should not duplicate const char*") { array.set(0, "world"); const size_t expectedSize = JSON_ARRAY_SIZE(1); REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate char*") { array.set(0, const_cast("world")); const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } SECTION("should duplicate std::string") { array.set(0, std::string("world")); const size_t expectedSize = JSON_ARRAY_SIZE(1) + 6; REQUIRE(expectedSize == doc.memoryUsage()); } }