@@ -17,31 +17,112 @@ 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 ].as <const char *>() != str);
80+ REQUIRE (array[0 ].is <const char *>() == true );
81+ REQUIRE (array[0 ].is <int >() == false );
82+ REQUIRE (spy.log () == AllocatorLog{
83+ Allocate (sizeofPool ()),
84+ Allocate (sizeofString (" hello" )),
85+ });
86+ }
87+
88+ SECTION (" serialized(const char*)" ) {
89+ array.add (serialized (" {}" ));
90+
91+ REQUIRE (doc.as <std::string>() == " [{}]" );
92+ REQUIRE (spy.log () == AllocatorLog{
93+ Allocate (sizeofPool ()),
94+ Allocate (sizeofString (" {}" )),
95+ });
96+ }
97+
98+ SECTION (" serialized(char*)" ) {
99+ array.add (serialized (const_cast <char *>(" {}" )));
100+
101+ REQUIRE (doc.as <std::string>() == " [{}]" );
102+ REQUIRE (spy.log () == AllocatorLog{
103+ Allocate (sizeofPool ()),
104+ Allocate (sizeofString (" {}" )),
105+ });
106+ }
107+
108+ SECTION (" serialized(std::string)" ) {
109+ array.add (serialized (" {}" _s));
110+
111+ REQUIRE (doc.as <std::string>() == " [{}]" );
112+ REQUIRE (spy.log () == AllocatorLog{
113+ Allocate (sizeofPool ()),
114+ Allocate (sizeofString (" {}" )),
115+ });
116+ }
117+
118+ SECTION (" serialized(std::string)" ) {
119+ array.add (serialized (" \0 XX" _s));
120+
121+ REQUIRE (doc.as <std::string>() == " [\0 XX]" _s);
122+ REQUIRE (spy.log () == AllocatorLog{
123+ Allocate (sizeofPool ()),
124+ Allocate (sizeofString (" XX" )),
125+ });
45126 }
46127
47128#ifdef HAS_VARIABLE_LENGTH_ARRAY
@@ -52,7 +133,12 @@ TEST_CASE("JsonArray::add(T)") {
52133
53134 array.add (vla);
54135
55- REQUIRE (" world" _s == array[0 ]);
136+ strcpy (vla, " hello" );
137+ REQUIRE (array[0 ] == " world" _s);
138+ REQUIRE (spy.log () == AllocatorLog{
139+ Allocate (sizeofPool ()),
140+ Allocate (sizeofString (" hello" )),
141+ });
56142 }
57143#endif
58144
@@ -99,61 +185,6 @@ TEST_CASE("JsonArray::add(T)") {
99185
100186 REQUIRE (str == array[0 ]);
101187 }
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 (" \0 XX" _s));
152- REQUIRE (spy.log () == AllocatorLog{
153- Allocate (sizeofPool ()),
154- Allocate (sizeofString (" XX" )),
155- });
156- }
157188}
158189
159190TEST_CASE (" JsonArray::add<T>()" ) {
0 commit comments