Skip to content

Commit

Permalink
Add more tests with VLAs
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Nov 8, 2024
1 parent 1110d62 commit 31253db
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 2 deletions.
38 changes: 37 additions & 1 deletion extras/tests/Deprecated/containsKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()") {
Expand All @@ -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()") {
Expand Down Expand Up @@ -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()") {
Expand Down
38 changes: 37 additions & 1 deletion extras/tests/JsonDocument/ElementProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()") {
Expand Down Expand Up @@ -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()") {
Expand Down Expand Up @@ -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") {
Expand Down
24 changes: 24 additions & 0 deletions extras/tests/JsonDocument/MemberProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()") {
Expand Down Expand Up @@ -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()") {
Expand Down
18 changes: 18 additions & 0 deletions extras/tests/JsonDocument/add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>()") {
Expand Down
15 changes: 15 additions & 0 deletions extras/tests/JsonDocument/subscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
12 changes: 12 additions & 0 deletions extras/tests/JsonVariant/add.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>()") {
Expand Down
17 changes: 17 additions & 0 deletions extras/tests/JsonVariant/remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand Down

0 comments on commit 31253db

Please sign in to comment.