6
6
#include < catch.hpp>
7
7
8
8
#include " Allocators.hpp"
9
- #include " Literals.hpp"
10
9
10
+ using namespace ArduinoJson ;
11
11
using namespace ArduinoJson ::detail;
12
12
13
13
TEST_CASE (" StringBuilder" ) {
@@ -22,13 +22,31 @@ TEST_CASE("StringBuilder") {
22
22
str.startString ();
23
23
str.save (&data);
24
24
25
- REQUIRE (resources.size () == sizeofString (" " ));
26
25
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" );
32
50
}
33
51
34
52
SECTION (" Short string fits in first allocation" ) {
@@ -98,12 +116,12 @@ TEST_CASE("StringBuilder") {
98
116
}
99
117
}
100
118
101
- static const char * saveString (StringBuilder& builder, const char * s) {
119
+ static JsonString saveString (StringBuilder& builder, const char * s) {
102
120
VariantData data;
103
121
builder.startString ();
104
122
builder.append (s);
105
123
builder.save (&data);
106
- return data.asString (). c_str () ;
124
+ return data.asString ();
107
125
}
108
126
109
127
TEST_CASE (" StringBuilder::save() deduplicates strings" ) {
@@ -116,9 +134,9 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
116
134
auto s2 = saveString (builder, " world" );
117
135
auto s3 = saveString (builder, " hello" );
118
136
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
122
140
123
141
REQUIRE (spy.log () ==
124
142
AllocatorLog{
@@ -134,9 +152,9 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
134
152
auto s1 = saveString (builder, " hello world" );
135
153
auto s2 = saveString (builder, " hello" );
136
154
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
140
158
141
159
REQUIRE (spy.log () ==
142
160
AllocatorLog{
@@ -149,18 +167,18 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
149
167
150
168
SECTION (" Don't overrun" ) {
151
169
auto s1 = saveString (builder, " hello world" );
152
- auto s2 = saveString (builder, " wor " );
170
+ auto s2 = saveString (builder, " worl " );
153
171
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
157
175
158
176
REQUIRE (spy.log () ==
159
177
AllocatorLog{
160
178
Allocate (sizeofStringBuffer ()),
161
179
Reallocate (sizeofStringBuffer (), sizeofString (" hello world" )),
162
180
Allocate (sizeofStringBuffer ()),
163
- Reallocate (sizeofStringBuffer (), sizeofString (" wor " )),
181
+ Reallocate (sizeofStringBuffer (), sizeofString (" worl " )),
164
182
});
165
183
}
166
184
}
0 commit comments