Skip to content

Commit 31253db

Browse files
committed
Add more tests with VLAs
1 parent 1110d62 commit 31253db

File tree

7 files changed

+160
-2
lines changed

7 files changed

+160
-2
lines changed

extras/tests/Deprecated/containsKey.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,25 @@ TEST_CASE("JsonDocument::containsKey()") {
4444
REQUIRE(doc.containsKey("hello") == false);
4545
}
4646

47-
SECTION("support JsonVariant") {
47+
SECTION("supports JsonVariant") {
4848
doc["hello"] = "world";
4949
doc["key"] = "hello";
5050

5151
REQUIRE(doc.containsKey(doc["key"]) == true);
5252
REQUIRE(doc.containsKey(doc["foo"]) == false);
5353
}
54+
55+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
56+
SECTION("supports VLAs") {
57+
size_t i = 16;
58+
char vla[i];
59+
strcpy(vla, "hello");
60+
61+
doc["hello"] = "world";
62+
63+
REQUIRE(doc.containsKey(vla) == true);
64+
}
65+
#endif
5466
}
5567

5668
TEST_CASE("MemberProxy::containsKey()") {
@@ -70,6 +82,18 @@ TEST_CASE("MemberProxy::containsKey()") {
7082
REQUIRE(mp.containsKey("key"_s) == true);
7183
REQUIRE(mp.containsKey("key"_s) == true);
7284
}
85+
86+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
87+
SECTION("supports VLAs") {
88+
size_t i = 16;
89+
char vla[i];
90+
strcpy(vla, "hello");
91+
92+
mp["hello"] = "world";
93+
94+
REQUIRE(mp.containsKey(vla) == true);
95+
}
96+
#endif
7397
}
7498

7599
TEST_CASE("JsonObject::containsKey()") {
@@ -175,6 +199,18 @@ TEST_CASE("JsonVariant::containsKey()") {
175199
REQUIRE(var.containsKey(doc["key"]) == true);
176200
REQUIRE(var.containsKey(doc["foo"]) == false);
177201
}
202+
203+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
204+
SECTION("supports VLAs") {
205+
size_t i = 16;
206+
char vla[i];
207+
strcpy(vla, "hello");
208+
209+
var["hello"] = "world";
210+
211+
REQUIRE(var.containsKey(vla) == true);
212+
}
213+
#endif
178214
}
179215

180216
TEST_CASE("JsonVariantConst::containsKey()") {

extras/tests/JsonDocument/ElementProxy.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,25 @@ TEST_CASE("ElementProxy::add()") {
2626
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
2727
}
2828

29-
SECTION("set(char[])") {
29+
SECTION("add(char[])") {
3030
char s[] = "world";
3131
ep.add(s);
3232
strcpy(s, "!!!!!");
3333

3434
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
3535
}
36+
37+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
38+
SECTION("set(vla)") {
39+
size_t i = 8;
40+
char vla[i];
41+
strcpy(vla, "world");
42+
43+
ep.add(vla);
44+
45+
REQUIRE(doc.as<std::string>() == "[[\"world\"]]");
46+
}
47+
#endif
3648
}
3749

3850
TEST_CASE("ElementProxy::clear()") {
@@ -166,6 +178,18 @@ TEST_CASE("ElementProxy::set()") {
166178

167179
REQUIRE(doc.as<std::string>() == "[\"world\"]");
168180
}
181+
182+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
183+
SECTION("set(VLA)") {
184+
size_t i = 8;
185+
char vla[i];
186+
strcpy(vla, "world");
187+
188+
ep.set(vla);
189+
190+
REQUIRE(doc.as<std::string>() == "[\"world\"]");
191+
}
192+
#endif
169193
}
170194

171195
TEST_CASE("ElementProxy::size()") {
@@ -205,6 +229,18 @@ TEST_CASE("ElementProxy::operator[]") {
205229

206230
REQUIRE(doc.as<std::string>() == "[null,[null,null,42]]");
207231
}
232+
233+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
234+
SECTION("set VLA") {
235+
size_t i = 8;
236+
char vla[i];
237+
strcpy(vla, "world");
238+
239+
ep[0] = vla;
240+
241+
REQUIRE(doc.as<std::string>() == "[null,[\"world\"]]");
242+
}
243+
#endif
208244
}
209245

210246
TEST_CASE("ElementProxy cast to JsonVariantConst") {

extras/tests/JsonDocument/MemberProxy.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ TEST_CASE("MemberProxy::add()") {
3232

3333
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
3434
}
35+
36+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
37+
SECTION("add(vla)") {
38+
size_t i = 16;
39+
char vla[i];
40+
strcpy(vla, "world");
41+
42+
mp.add(vla);
43+
44+
REQUIRE(doc.as<std::string>() == "{\"hello\":[\"world\"]}");
45+
}
46+
#endif
3547
}
3648

3749
TEST_CASE("MemberProxy::clear()") {
@@ -195,6 +207,18 @@ TEST_CASE("MemberProxy::set()") {
195207

196208
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
197209
}
210+
211+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
212+
SECTION("set(vla)") {
213+
size_t i = 8;
214+
char vla[i];
215+
strcpy(vla, "world");
216+
217+
mp.set(vla);
218+
219+
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
220+
}
221+
#endif
198222
}
199223

200224
TEST_CASE("MemberProxy::size()") {

extras/tests/JsonDocument/add.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,24 @@ TEST_CASE("JsonDocument::add(T)") {
7979
Allocate(sizeofString("example")),
8080
});
8181
}
82+
83+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
84+
SECTION("VLA") {
85+
size_t i = 16;
86+
char vla[i];
87+
strcpy(vla, "example");
88+
89+
doc.add(vla);
90+
doc.add(vla);
91+
92+
CHECK(doc[0].as<const char*>() == doc[1].as<const char*>());
93+
REQUIRE("example"_s == doc[0]);
94+
REQUIRE(spy.log() == AllocatorLog{
95+
Allocate(sizeofPool()),
96+
Allocate(sizeofString("example")),
97+
});
98+
}
99+
#endif
82100
}
83101

84102
TEST_CASE("JsonDocument::add<T>()") {

extras/tests/JsonDocument/subscript.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ TEST_CASE("JsonDocument::operator[]") {
3434
REQUIRE((doc["hello"] | "nope") == "world"_s);
3535
REQUIRE((doc["world"] | "nope") == "nope"_s);
3636
}
37+
38+
#if defined(HAS_VARIABLE_LENGTH_ARRAY) && \
39+
!defined(SUBSCRIPT_CONFLICTS_WITH_BUILTIN_OPERATOR)
40+
SECTION("supports VLAs") {
41+
size_t i = 16;
42+
char vla[i];
43+
strcpy(vla, "hello");
44+
45+
doc[vla] = "world";
46+
47+
REQUIRE(doc[vla] == "world");
48+
REQUIRE(cdoc[vla] == "world");
49+
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\"}");
50+
}
51+
#endif
3752
}
3853

3954
SECTION("array") {

extras/tests/JsonVariant/add.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ TEST_CASE("JsonVariant::add(T)") {
4646

4747
REQUIRE(var.as<std::string>() == "{\"val\":123}");
4848
}
49+
50+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
51+
SECTION("supports VLAs") {
52+
size_t i = 16;
53+
char vla[i];
54+
strcpy(vla, "hello");
55+
56+
var.add(vla);
57+
58+
REQUIRE(var.as<std::string>() == "[\"hello\"]");
59+
}
60+
#endif
4961
}
5062

5163
TEST_CASE("JsonVariant::add<T>()") {

extras/tests/JsonVariant/remove.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ TEST_CASE("JsonVariant::remove(std::string)") {
8383
REQUIRE(var.as<std::string>() == "{\"a\":1}");
8484
}
8585

86+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
87+
TEST_CASE("JsonVariant::remove(VLA)") {
88+
JsonDocument doc;
89+
JsonVariant var = doc.to<JsonVariant>();
90+
91+
var["a"] = 1;
92+
var["b"] = 2;
93+
size_t i = 16;
94+
char vla[i];
95+
strcpy(vla, "b");
96+
97+
var.remove("b"_s);
98+
99+
REQUIRE(var.as<std::string>() == "{\"a\":1}");
100+
}
101+
#endif
102+
86103
TEST_CASE("JsonVariant::remove(JsonVariant) from object") {
87104
JsonDocument doc;
88105
JsonVariant var = doc.to<JsonVariant>();

0 commit comments

Comments
 (0)