Skip to content

Commit 1b28830

Browse files
committed
Update JsonArray tests
1 parent cb2f8c9 commit 1b28830

File tree

4 files changed

+133
-144
lines changed

4 files changed

+133
-144
lines changed

extras/tests/JsonArray/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ add_executable(JsonArrayTests
1313
nesting.cpp
1414
remove.cpp
1515
size.cpp
16-
std_string.cpp
1716
subscript.cpp
1817
unbound.cpp
1918
)

extras/tests/JsonArray/add.cpp

Lines changed: 90 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,111 @@ TEST_CASE("JsonArray::add(T)") {
1717

1818
SECTION("int") {
1919
array.add(123);
20+
2021
REQUIRE(123 == array[0].as<int>());
2122
REQUIRE(array[0].is<int>());
2223
REQUIRE(array[0].is<double>());
24+
REQUIRE(spy.log() == AllocatorLog{
25+
Allocate(sizeofPool()),
26+
});
2327
}
2428

2529
SECTION("double") {
2630
array.add(123.45);
31+
2732
REQUIRE(123.45 == array[0].as<double>());
2833
REQUIRE(array[0].is<double>());
2934
REQUIRE_FALSE(array[0].is<bool>());
35+
REQUIRE(spy.log() == AllocatorLog{
36+
Allocate(sizeofPool()),
37+
});
3038
}
3139

3240
SECTION("bool") {
3341
array.add(true);
34-
REQUIRE(true == array[0].as<bool>());
42+
43+
REQUIRE(array[0].as<bool>() == true);
3544
REQUIRE(array[0].is<bool>());
3645
REQUIRE_FALSE(array[0].is<int>());
46+
REQUIRE(spy.log() == AllocatorLog{
47+
Allocate(sizeofPool()),
48+
});
49+
}
50+
51+
SECTION("string literal") {
52+
array.add("hello");
53+
54+
REQUIRE(array[0].as<std::string>() == "hello");
55+
REQUIRE(array[0].is<const char*>());
56+
REQUIRE(array[0].is<int>() == false);
57+
REQUIRE(spy.log() == AllocatorLog{
58+
Allocate(sizeofPool()),
59+
});
60+
}
61+
62+
SECTION("std::string") {
63+
array.add("hello"_s);
64+
65+
REQUIRE(array[0].as<std::string>() == "hello");
66+
REQUIRE(array[0].is<const char*>() == true);
67+
REQUIRE(array[0].is<int>() == false);
68+
REQUIRE(spy.log() == AllocatorLog{
69+
Allocate(sizeofPool()),
70+
Allocate(sizeofString("hello")),
71+
});
3772
}
3873

3974
SECTION("const char*") {
4075
const char* str = "hello";
4176
array.add(str);
42-
REQUIRE(str == array[0].as<std::string>());
43-
REQUIRE(array[0].is<const char*>());
44-
REQUIRE_FALSE(array[0].is<int>());
77+
78+
REQUIRE(array[0].as<std::string>() == "hello");
79+
REQUIRE(array[0].is<const char*>() == true);
80+
REQUIRE(array[0].is<int>() == false);
81+
REQUIRE(spy.log() == AllocatorLog{
82+
Allocate(sizeofPool()),
83+
Allocate(sizeofString("hello")),
84+
});
85+
}
86+
87+
SECTION("serialized(const char*)") {
88+
array.add(serialized("{}"));
89+
90+
REQUIRE(doc.as<std::string>() == "[{}]");
91+
REQUIRE(spy.log() == AllocatorLog{
92+
Allocate(sizeofPool()),
93+
Allocate(sizeofString("{}")),
94+
});
95+
}
96+
97+
SECTION("serialized(char*)") {
98+
array.add(serialized(const_cast<char*>("{}")));
99+
100+
REQUIRE(doc.as<std::string>() == "[{}]");
101+
REQUIRE(spy.log() == AllocatorLog{
102+
Allocate(sizeofPool()),
103+
Allocate(sizeofString("{}")),
104+
});
105+
}
106+
107+
SECTION("serialized(std::string)") {
108+
array.add(serialized("{}"_s));
109+
110+
REQUIRE(doc.as<std::string>() == "[{}]");
111+
REQUIRE(spy.log() == AllocatorLog{
112+
Allocate(sizeofPool()),
113+
Allocate(sizeofString("{}")),
114+
});
115+
}
116+
117+
SECTION("serialized(std::string)") {
118+
array.add(serialized("\0XX"_s));
119+
120+
REQUIRE(doc.as<std::string>() == "[\0XX]"_s);
121+
REQUIRE(spy.log() == AllocatorLog{
122+
Allocate(sizeofPool()),
123+
Allocate(sizeofString(" XX")),
124+
});
45125
}
46126

47127
#ifdef HAS_VARIABLE_LENGTH_ARRAY
@@ -52,7 +132,12 @@ TEST_CASE("JsonArray::add(T)") {
52132

53133
array.add(vla);
54134

55-
REQUIRE("world"_s == array[0]);
135+
strcpy(vla, "hello");
136+
REQUIRE(array[0] == "world"_s);
137+
REQUIRE(spy.log() == AllocatorLog{
138+
Allocate(sizeofPool()),
139+
Allocate(sizeofString("hello")),
140+
});
56141
}
57142
#endif
58143

@@ -99,61 +184,6 @@ TEST_CASE("JsonArray::add(T)") {
99184

100185
REQUIRE(str == array[0]);
101186
}
102-
103-
SECTION("should not duplicate const char*") {
104-
array.add("world");
105-
REQUIRE(spy.log() == AllocatorLog{
106-
Allocate(sizeofPool()),
107-
});
108-
}
109-
110-
SECTION("should duplicate char*") {
111-
array.add(const_cast<char*>("world"));
112-
REQUIRE(spy.log() == AllocatorLog{
113-
Allocate(sizeofPool()),
114-
Allocate(sizeofString("world")),
115-
});
116-
}
117-
118-
SECTION("should duplicate std::string") {
119-
array.add("world"_s);
120-
REQUIRE(spy.log() == AllocatorLog{
121-
Allocate(sizeofPool()),
122-
Allocate(sizeofString("world")),
123-
});
124-
}
125-
126-
SECTION("should duplicate serialized(const char*)") {
127-
array.add(serialized("{}"));
128-
REQUIRE(spy.log() == AllocatorLog{
129-
Allocate(sizeofPool()),
130-
Allocate(sizeofString("{}")),
131-
});
132-
}
133-
134-
SECTION("should duplicate serialized(char*)") {
135-
array.add(serialized(const_cast<char*>("{}")));
136-
REQUIRE(spy.log() == AllocatorLog{
137-
Allocate(sizeofPool()),
138-
Allocate(sizeofString("{}")),
139-
});
140-
}
141-
142-
SECTION("should duplicate serialized(std::string)") {
143-
array.add(serialized("{}"_s));
144-
REQUIRE(spy.log() == AllocatorLog{
145-
Allocate(sizeofPool()),
146-
Allocate(sizeofString("{}")),
147-
});
148-
}
149-
150-
SECTION("should duplicate serialized(std::string)") {
151-
array.add(serialized("\0XX"_s));
152-
REQUIRE(spy.log() == AllocatorLog{
153-
Allocate(sizeofPool()),
154-
Allocate(sizeofString(" XX")),
155-
});
156-
}
157187
}
158188

159189
TEST_CASE("JsonArray::add<T>()") {

extras/tests/JsonArray/std_string.cpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

extras/tests/JsonArray/subscript.cpp

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,55 @@ TEST_CASE("JsonArray::operator[]") {
5959
REQUIRE(false == array[0].is<int>());
6060
}
6161

62+
SECTION("string literal") {
63+
array[0] = "hello";
64+
65+
REQUIRE(array[0].as<std::string>() == "hello");
66+
REQUIRE(true == array[0].is<const char*>());
67+
REQUIRE(false == array[0].is<int>());
68+
REQUIRE(spy.log() == AllocatorLog{
69+
Allocate(sizeofPool()),
70+
Allocate(sizeofString("world")),
71+
});
72+
}
73+
6274
SECTION("const char*") {
6375
const char* str = "hello";
64-
6576
array[0] = str;
66-
REQUIRE(str == array[0].as<const char*>());
77+
78+
REQUIRE(array[0].as<std::string>() == "hello");
79+
REQUIRE(true == array[0].is<const char*>());
80+
REQUIRE(false == array[0].is<int>());
81+
REQUIRE(spy.log() == AllocatorLog{
82+
Allocate(sizeofPool()),
83+
Allocate(sizeofString("world")),
84+
});
85+
}
86+
87+
SECTION("std::string") {
88+
array[0] = "hello"_s;
89+
90+
REQUIRE(array[0].as<std::string>() == "hello");
6791
REQUIRE(true == array[0].is<const char*>());
6892
REQUIRE(false == array[0].is<int>());
93+
REQUIRE(spy.log() == AllocatorLog{
94+
Allocate(sizeofPool()),
95+
Allocate(sizeofString("world")),
96+
});
97+
}
98+
99+
#ifdef HAS_VARIABLE_LENGTH_ARRAY
100+
SECTION("VLA") {
101+
size_t i = 16;
102+
char vla[i];
103+
strcpy(vla, "world");
104+
105+
array.add("hello");
106+
array[0] = vla;
107+
108+
REQUIRE(array[0] == "world"_s);
69109
}
110+
#endif
70111

71112
SECTION("nested array") {
72113
JsonDocument doc2;
@@ -114,58 +155,11 @@ TEST_CASE("JsonArray::operator[]") {
114155
REQUIRE(str == array[0]);
115156
}
116157

117-
SECTION("should not duplicate const char*") {
118-
array[0] = "world";
119-
REQUIRE(spy.log() == AllocatorLog{
120-
Allocate(sizeofPool()),
121-
});
122-
}
123-
124-
SECTION("should duplicate char*") {
125-
array[0] = const_cast<char*>("world");
126-
REQUIRE(spy.log() == AllocatorLog{
127-
Allocate(sizeofPool()),
128-
Allocate(sizeofString("world")),
129-
});
130-
}
131-
132-
SECTION("should duplicate std::string") {
133-
array[0] = "world"_s;
134-
REQUIRE(spy.log() == AllocatorLog{
135-
Allocate(sizeofPool()),
136-
Allocate(sizeofString("world")),
137-
});
138-
}
139-
140158
SECTION("array[0].to<JsonObject>()") {
141159
JsonObject obj = array[0].to<JsonObject>();
142160
REQUIRE(obj.isNull() == false);
143161
}
144162

145-
#ifdef HAS_VARIABLE_LENGTH_ARRAY
146-
SECTION("set(VLA)") {
147-
size_t i = 16;
148-
char vla[i];
149-
strcpy(vla, "world");
150-
151-
array.add("hello");
152-
array[0].set(vla);
153-
154-
REQUIRE("world"_s == array[0]);
155-
}
156-
157-
SECTION("operator=(VLA)") {
158-
size_t i = 16;
159-
char vla[i];
160-
strcpy(vla, "world");
161-
162-
array.add("hello");
163-
array[0] = vla;
164-
165-
REQUIRE("world"_s == array[0]);
166-
}
167-
#endif
168-
169163
SECTION("Use a JsonVariant as index") {
170164
array[0] = 1;
171165
array[1] = 2;

0 commit comments

Comments
 (0)