Add more tests with VLAs

This commit is contained in:
Benoit Blanchon
2024-11-07 14:19:47 +01:00
parent 1110d62128
commit 31253dbe13
7 changed files with 160 additions and 2 deletions

View File

@ -44,13 +44,25 @@ TEST_CASE("JsonDocument::containsKey()") {
REQUIRE(doc.containsKey("hello") == false);
}
SECTION("support JsonVariant") {
SECTION("supports JsonVariant") {
doc["hello"] = "world";
doc["key"] = "hello";
REQUIRE(doc.containsKey(doc["key"]) == true);
REQUIRE(doc.containsKey(doc["foo"]) == false);
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("supports VLAs") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
doc["hello"] = "world";
REQUIRE(doc.containsKey(vla) == true);
}
#endif
}
TEST_CASE("MemberProxy::containsKey()") {
@ -70,6 +82,18 @@ TEST_CASE("MemberProxy::containsKey()") {
REQUIRE(mp.containsKey("key"_s) == true);
REQUIRE(mp.containsKey("key"_s) == true);
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("supports VLAs") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
mp["hello"] = "world";
REQUIRE(mp.containsKey(vla) == true);
}
#endif
}
TEST_CASE("JsonObject::containsKey()") {
@ -175,6 +199,18 @@ TEST_CASE("JsonVariant::containsKey()") {
REQUIRE(var.containsKey(doc["key"]) == true);
REQUIRE(var.containsKey(doc["foo"]) == false);
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("supports VLAs") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
var["hello"] = "world";
REQUIRE(var.containsKey(vla) == true);
}
#endif
}
TEST_CASE("JsonVariantConst::containsKey()") {

View File

@ -26,13 +26,25 @@ TEST_CASE("ElementProxy::add()") {
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
}
SECTION("set(char[])") {
SECTION("add(char[])") {
char s[] = "world";
ep.add(s);
strcpy(s, "!!!!!");
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("set(vla)") {
size_t i = 8;
char vla[i];
strcpy(vla, "world");
ep.add(vla);
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
}
#endif
}
TEST_CASE("ElementProxy::clear()") {
@ -166,6 +178,18 @@ TEST_CASE("ElementProxy::set()") {
REQUIRE(doc.as<std::string>() == "[\"world\"]");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("set(VLA)") {
size_t i = 8;
char vla[i];
strcpy(vla, "world");
ep.set(vla);
REQUIRE(doc.as<std::string>() == "[\"world\"]");
}
#endif
}
TEST_CASE("ElementProxy::size()") {
@ -205,6 +229,18 @@ TEST_CASE("ElementProxy::operator[]") {
REQUIRE(doc.as<std::string>() == "[null,[null,null,42]]");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("set VLA") {
size_t i = 8;
char vla[i];
strcpy(vla, "world");
ep[0] = vla;
REQUIRE(doc.as<std::string>() == "[null,[\"world\"]]");
}
#endif
}
TEST_CASE("ElementProxy cast to JsonVariantConst") {

View File

@ -32,6 +32,18 @@ TEST_CASE("MemberProxy::add()") {
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("add(vla)") {
size_t i = 16;
char vla[i];
strcpy(vla, "world");
mp.add(vla);
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
}
#endif
}
TEST_CASE("MemberProxy::clear()") {
@ -195,6 +207,18 @@ TEST_CASE("MemberProxy::set()") {
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("set(vla)") {
size_t i = 8;
char vla[i];
strcpy(vla, "world");
mp.set(vla);
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
}
#endif
}
TEST_CASE("MemberProxy::size()") {

View File

@ -79,6 +79,24 @@ TEST_CASE("JsonDocument::add(T)") {
Allocate(sizeofString("example")),
});
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("VLA") {
size_t i = 16;
char vla[i];
strcpy(vla, "example");
doc.add(vla);
doc.add(vla);
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
REQUIRE("example"_s == doc[0]);
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("example")),
});
}
#endif
}
TEST_CASE("JsonDocument::add<T>()") {

View File

@ -34,6 +34,21 @@ TEST_CASE("JsonDocument::operator[]") {
REQUIRE((doc["hello"] | "nope") == "world"_s);
REQUIRE((doc["world"] | "nope") == "nope"_s);
}
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
!defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
SECTION("supports VLAs") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
doc[vla] = "world";
REQUIRE(doc[vla] == "world");
REQUIRE(cdoc[vla] == "world");
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
}
#endif
}
SECTION("array") {

View File

@ -46,6 +46,18 @@ TEST_CASE("JsonVariant::add(T)") {
REQUIRE(var.as<std::string>() == "{\"val\":123}");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("supports VLAs") {
size_t i = 16;
char vla[i];
strcpy(vla, "hello");
var.add(vla);
REQUIRE(var.as<std::string>() == "[\"hello\"]");
}
#endif
}
TEST_CASE("JsonVariant::add<T>()") {

View File

@ -83,6 +83,23 @@ TEST_CASE("JsonVariant::remove(std::string)") {
REQUIRE(var.as<std::string>() == "{\"a\":1}");
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
TEST_CASE("JsonVariant::remove(VLA)") {
JsonDocument doc;
JsonVariant var = doc.to<JsonVariant>();
var["a"] = 1;
var["b"] = 2;
size_t i = 16;
char vla[i];
strcpy(vla, "b");
var.remove("b"_s);
REQUIRE(var.as<std::string>() == "{\"a\":1}");
}
#endif
TEST_CASE("JsonVariant::remove(JsonVariant) from object") {
JsonDocument doc;
JsonVariant var = doc.to<JsonVariant>();