Files
ArduinoJson/extras/tests/JsonArray/add.cpp

231 lines
6.1 KiB
C++
Raw Normal View History

// ArduinoJson - https://arduinojson.org
2024-01-03 08:47:06 +01:00
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
2023-07-24 17:21:25 +02:00
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
TEST_CASE("JsonArray::add(T)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
2018-08-22 14:37:17 +02:00
JsonArray array = doc.to<JsonArray>();
SECTION("int") {
2018-08-22 14:37:17 +02:00
array.add(123);
REQUIRE(123 == array[0].as<int>());
REQUIRE(array[0].is<int>());
REQUIRE(array[0].is<double>());
}
SECTION("double") {
2018-08-22 14:37:17 +02:00
array.add(123.45);
REQUIRE(123.45 == array[0].as<double>());
REQUIRE(array[0].is<double>());
REQUIRE_FALSE(array[0].is<bool>());
}
SECTION("bool") {
2018-08-22 14:37:17 +02:00
array.add(true);
REQUIRE(true == array[0].as<bool>());
REQUIRE(array[0].is<bool>());
REQUIRE_FALSE(array[0].is<int>());
}
SECTION("const char*") {
const char* str = "hello";
2018-08-22 14:37:17 +02:00
array.add(str);
REQUIRE(str == array[0].as<std::string>());
REQUIRE(array[0].is<const char*>());
REQUIRE_FALSE(array[0].is<int>());
}
2018-08-22 14:37:17 +02:00
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("vla") {
size_t i = 16;
2018-08-22 14:37:17 +02:00
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<JsonArray>();
2018-08-22 14:37:17 +02:00
array.add(arr);
2018-08-22 14:37:17 +02:00
REQUIRE(arr == array[0].as<JsonArray>());
REQUIRE(array[0].is<JsonArray>());
REQUIRE_FALSE(array[0].is<int>());
}
SECTION("nested object") {
JsonDocument doc2;
JsonObject obj = doc2.to<JsonObject>();
2018-08-22 14:37:17 +02:00
array.add(obj);
2018-08-22 14:37:17 +02:00
REQUIRE(obj == array[0].as<JsonObject>());
REQUIRE(array[0].is<JsonObject>());
REQUIRE_FALSE(array[0].is<int>());
}
SECTION("array subscript") {
const char* str = "hello";
JsonDocument doc2;
JsonArray arr = doc2.to<JsonArray>();
arr.add(str);
2018-08-22 14:37:17 +02:00
array.add(arr[0]);
2018-08-22 14:37:17 +02:00
REQUIRE(str == array[0]);
}
SECTION("object subscript") {
const char* str = "hello";
JsonDocument doc2;
JsonObject obj = doc2.to<JsonObject>();
obj["x"] = str;
2018-08-22 14:37:17 +02:00
array.add(obj["x"]);
2018-08-22 14:37:17 +02:00
REQUIRE(str == array[0]);
}
SECTION("should not duplicate const char*") {
2018-08-22 14:37:17 +02:00
array.add("world");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
});
}
SECTION("should duplicate char*") {
2018-08-22 14:37:17 +02:00
array.add(const_cast<char*>("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate std::string") {
2018-08-22 14:37:17 +02:00
array.add(std::string("world"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("world")),
});
}
SECTION("should duplicate serialized(const char*)") {
2018-08-22 14:37:17 +02:00
array.add(serialized("{}"));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("{}")),
});
}
SECTION("should duplicate serialized(char*)") {
2018-08-22 14:37:17 +02:00
array.add(serialized(const_cast<char*>("{}")));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("{}")),
});
}
SECTION("should duplicate serialized(std::string)") {
2018-08-22 14:37:17 +02:00
array.add(serialized(std::string("{}")));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("{}")),
});
}
SECTION("should duplicate serialized(std::string)") {
2018-08-22 14:37:17 +02:00
array.add(serialized(std::string("\0XX", 3)));
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString(" XX")),
});
}
}
TEST_CASE("JsonArray::add<T>()") {
JsonDocument doc;
JsonArray array = doc.to<JsonArray>();
SECTION("add<JsonArray>()") {
JsonArray nestedArray = array.add<JsonArray>();
nestedArray.add(1);
nestedArray.add(2);
REQUIRE(doc.as<std::string>() == "[[1,2]]");
}
SECTION("add<JsonObject>()") {
JsonObject nestedObject = array.add<JsonObject>();
nestedObject["a"] = 1;
nestedObject["b"] = 2;
REQUIRE(doc.as<std::string>() == "[{\"a\":1,\"b\":2}]");
}
SECTION("add<JsonVariant>()") {
JsonVariant nestedVariant = array.add<JsonVariant>();
nestedVariant.set(42);
REQUIRE(doc.as<std::string>() == "[42]");
}
}
TEST_CASE("JsonObject::add(JsonObject) ") {
JsonDocument doc1;
doc1[std::string("key1")] = std::string("value1");
TimebombAllocator allocator(10);
SpyingAllocator spy(&allocator);
JsonDocument doc2(&spy);
JsonArray array = doc2.to<JsonArray>();
SECTION("success") {
bool result = array.add(doc1.as<JsonObject>());
REQUIRE(result == true);
REQUIRE(doc2.as<std::string>() == "[{\"key1\":\"value1\"}]");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("key1")),
Allocate(sizeofString("value1")),
});
}
SECTION("partial failure") { // issue #2081
allocator.setCountdown(2);
bool result = array.add(doc1.as<JsonObject>());
REQUIRE(result == false);
REQUIRE(doc2.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("key1")),
AllocateFail(sizeofString("value1")),
Deallocate(sizeofString("key1")),
});
}
SECTION("complete failure") {
allocator.setCountdown(0);
bool result = array.add(doc1.as<JsonObject>());
REQUIRE(result == false);
REQUIRE(doc2.as<std::string>() == "[]");
REQUIRE(spy.log() == AllocatorLog{
AllocateFail(sizeofPool()),
});
}
}