66#include < catch.hpp>
77
88#include " Allocators.hpp"
9- #include " Literals.hpp"
109
10+ using namespace ArduinoJson ;
1111using namespace ArduinoJson ::detail;
1212
1313TEST_CASE (" StringBuilder" ) {
@@ -22,13 +22,31 @@ TEST_CASE("StringBuilder") {
2222 str.startString ();
2323 str.save (&data);
2424
25- REQUIRE (resources.size () == sizeofString (" " ));
2625 REQUIRE (resources.overflowed () == false );
27- REQUIRE (spyingAllocator.log () ==
28- AllocatorLog{
29- Allocate (sizeofStringBuffer ()),
30- Reallocate (sizeofStringBuffer (), sizeofString (" " )),
31- });
26+ REQUIRE (spyingAllocator.log () == AllocatorLog{
27+ Allocate (sizeofStringBuffer ()),
28+ });
29+ REQUIRE (data.type () == VariantType::TinyString);
30+ }
31+
32+ SECTION (" Tiny string" ) {
33+ StringBuilder str (&resources);
34+
35+ str.startString ();
36+ str.append (" url" );
37+
38+ REQUIRE (str.isValid () == true );
39+ REQUIRE (str.str () == " url" );
40+ REQUIRE (spyingAllocator.log () == AllocatorLog{
41+ Allocate (sizeofStringBuffer ()),
42+ });
43+
44+ VariantData data;
45+ str.save (&data);
46+
47+ REQUIRE (resources.overflowed () == false );
48+ REQUIRE (data.type () == VariantType::TinyString);
49+ REQUIRE (data.asString () == " url" );
3250 }
3351
3452 SECTION (" Short string fits in first allocation" ) {
@@ -98,12 +116,12 @@ TEST_CASE("StringBuilder") {
98116 }
99117}
100118
101- static const char * saveString (StringBuilder& builder, const char * s) {
119+ static JsonString saveString (StringBuilder& builder, const char * s) {
102120 VariantData data;
103121 builder.startString ();
104122 builder.append (s);
105123 builder.save (&data);
106- return data.asString (). c_str () ;
124+ return data.asString ();
107125}
108126
109127TEST_CASE (" StringBuilder::save() deduplicates strings" ) {
@@ -116,9 +134,9 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
116134 auto s2 = saveString (builder, " world" );
117135 auto s3 = saveString (builder, " hello" );
118136
119- REQUIRE (s1 == " hello" _s );
120- REQUIRE (s2 == " world" _s );
121- REQUIRE (+s1 == +s3); // same address
137+ REQUIRE (s1 == " hello" );
138+ REQUIRE (s2 == " world" );
139+ REQUIRE (+s1. c_str () == +s3. c_str () ); // same address
122140
123141 REQUIRE (spy.log () ==
124142 AllocatorLog{
@@ -134,9 +152,9 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
134152 auto s1 = saveString (builder, " hello world" );
135153 auto s2 = saveString (builder, " hello" );
136154
137- REQUIRE (s1 == " hello world" _s );
138- REQUIRE (s2 == " hello" _s );
139- REQUIRE (+s2 != +s1); // different address
155+ REQUIRE (s1 == " hello world" );
156+ REQUIRE (s2 == " hello" );
157+ REQUIRE (+s2. c_str () != +s1. c_str () ); // different address
140158
141159 REQUIRE (spy.log () ==
142160 AllocatorLog{
@@ -149,18 +167,18 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
149167
150168 SECTION (" Don't overrun" ) {
151169 auto s1 = saveString (builder, " hello world" );
152- auto s2 = saveString (builder, " wor " );
170+ auto s2 = saveString (builder, " worl " );
153171
154- REQUIRE (s1 == " hello world" _s );
155- REQUIRE (s2 == " wor " _s );
156- REQUIRE (s2 != s1);
172+ REQUIRE (s1 == " hello world" );
173+ REQUIRE (s2 == " worl " );
174+ REQUIRE (s2. c_str () != s1. c_str ()); // different address
157175
158176 REQUIRE (spy.log () ==
159177 AllocatorLog{
160178 Allocate (sizeofStringBuffer ()),
161179 Reallocate (sizeofStringBuffer (), sizeofString (" hello world" )),
162180 Allocate (sizeofStringBuffer ()),
163- Reallocate (sizeofStringBuffer (), sizeofString (" wor " )),
181+ Reallocate (sizeofStringBuffer (), sizeofString (" worl " )),
164182 });
165183 }
166184}
0 commit comments