@@ -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 (" \0 XX" _s));
119+
120+ REQUIRE (doc.as <std::string>() == " [\0 XX]" _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 (" \0 XX" _s));
152- REQUIRE (spy.log () == AllocatorLog{
153- Allocate (sizeofPool ()),
154- Allocate (sizeofString (" XX" )),
155- });
156- }
157187}
158188
159189TEST_CASE (" JsonArray::add<T>()" ) {
0 commit comments