@@ -132,3 +132,127 @@ TEST(UtilTest, UncheckedCalloc) {
132
132
TEST_AND_FREE (UncheckedCalloc (0 ));
133
133
TEST_AND_FREE (UncheckedCalloc (1 ));
134
134
}
135
+
136
+ template <typename T>
137
+ static void MaybeStackBufferBasic () {
138
+ using node::MaybeStackBuffer;
139
+
140
+ MaybeStackBuffer<T> buf;
141
+ size_t old_length;
142
+ size_t old_capacity;
143
+
144
+ /* Default constructor */
145
+ EXPECT_EQ (0U , buf.length ());
146
+ EXPECT_FALSE (buf.IsAllocated ());
147
+ EXPECT_GT (buf.capacity (), buf.length ());
148
+
149
+ /* SetLength() expansion */
150
+ buf.SetLength (buf.capacity ());
151
+ EXPECT_EQ (buf.capacity (), buf.length ());
152
+ EXPECT_FALSE (buf.IsAllocated ());
153
+
154
+ /* Means of accessing raw buffer */
155
+ EXPECT_EQ (buf.out (), *buf);
156
+ EXPECT_EQ (&buf[0 ], *buf);
157
+
158
+ /* Basic I/O */
159
+ for (size_t i = 0 ; i < buf.length (); i++)
160
+ buf[i] = static_cast <T>(i);
161
+ for (size_t i = 0 ; i < buf.length (); i++)
162
+ EXPECT_EQ (static_cast <T>(i), buf[i]);
163
+
164
+ /* SetLengthAndZeroTerminate() */
165
+ buf.SetLengthAndZeroTerminate (buf.capacity () - 1 );
166
+ EXPECT_EQ (buf.capacity () - 1 , buf.length ());
167
+ for (size_t i = 0 ; i < buf.length (); i++)
168
+ EXPECT_EQ (static_cast <T>(i), buf[i]);
169
+ buf.SetLength (buf.capacity ());
170
+ EXPECT_EQ (0 , buf[buf.length () - 1 ]);
171
+
172
+ /* Initial Realloc */
173
+ old_length = buf.length () - 1 ;
174
+ old_capacity = buf.capacity ();
175
+ buf.AllocateSufficientStorage (buf.capacity () * 2 );
176
+ EXPECT_EQ (buf.capacity (), buf.length ());
177
+ EXPECT_TRUE (buf.IsAllocated ());
178
+ for (size_t i = 0 ; i < old_length; i++)
179
+ EXPECT_EQ (static_cast <T>(i), buf[i]);
180
+ EXPECT_EQ (0 , buf[old_length]);
181
+
182
+ /* SetLength() reduction and expansion */
183
+ for (size_t i = 0 ; i < buf.length (); i++)
184
+ buf[i] = static_cast <T>(i);
185
+ buf.SetLength (10 );
186
+ for (size_t i = 0 ; i < buf.length (); i++)
187
+ EXPECT_EQ (static_cast <T>(i), buf[i]);
188
+ buf.SetLength (buf.capacity ());
189
+ for (size_t i = 0 ; i < buf.length (); i++)
190
+ EXPECT_EQ (static_cast <T>(i), buf[i]);
191
+
192
+ /* Subsequent Realloc */
193
+ old_length = buf.length ();
194
+ old_capacity = buf.capacity ();
195
+ buf.AllocateSufficientStorage (old_capacity * 1.5 );
196
+ EXPECT_EQ (buf.capacity (), buf.length ());
197
+ EXPECT_EQ (static_cast <size_t >(old_capacity * 1.5 ), buf.length ());
198
+ EXPECT_TRUE (buf.IsAllocated ());
199
+ for (size_t i = 0 ; i < old_length; i++)
200
+ EXPECT_EQ (static_cast <T>(i), buf[i]);
201
+
202
+ /* Basic I/O on Realloc'd buffer */
203
+ for (size_t i = 0 ; i < buf.length (); i++)
204
+ buf[i] = static_cast <T>(i);
205
+ for (size_t i = 0 ; i < buf.length (); i++)
206
+ EXPECT_EQ (static_cast <T>(i), buf[i]);
207
+
208
+ /* Release() */
209
+ T* rawbuf = buf.out ();
210
+ buf.Release ();
211
+ EXPECT_EQ (0U , buf.length ());
212
+ EXPECT_FALSE (buf.IsAllocated ());
213
+ EXPECT_GT (buf.capacity (), buf.length ());
214
+ free (rawbuf);
215
+ }
216
+
217
+ TEST (UtilTest, MaybeStackBuffer) {
218
+ using node::MaybeStackBuffer;
219
+
220
+ MaybeStackBufferBasic<uint8_t >();
221
+ MaybeStackBufferBasic<uint16_t >();
222
+
223
+ // Constructor with size parameter
224
+ {
225
+ MaybeStackBuffer<unsigned char > buf (100 );
226
+ EXPECT_EQ (100U , buf.length ());
227
+ EXPECT_FALSE (buf.IsAllocated ());
228
+ EXPECT_GT (buf.capacity (), buf.length ());
229
+ buf.SetLength (buf.capacity ());
230
+ EXPECT_EQ (buf.capacity (), buf.length ());
231
+ EXPECT_FALSE (buf.IsAllocated ());
232
+ for (size_t i = 0 ; i < buf.length (); i++)
233
+ buf[i] = static_cast <unsigned char >(i);
234
+ for (size_t i = 0 ; i < buf.length (); i++)
235
+ EXPECT_EQ (static_cast <unsigned char >(i), buf[i]);
236
+
237
+ MaybeStackBuffer<unsigned char > bigbuf (10000 );
238
+ EXPECT_EQ (10000U , bigbuf.length ());
239
+ EXPECT_TRUE (bigbuf.IsAllocated ());
240
+ EXPECT_EQ (bigbuf.length (), bigbuf.capacity ());
241
+ for (size_t i = 0 ; i < bigbuf.length (); i++)
242
+ bigbuf[i] = static_cast <unsigned char >(i);
243
+ for (size_t i = 0 ; i < bigbuf.length (); i++)
244
+ EXPECT_EQ (static_cast <unsigned char >(i), bigbuf[i]);
245
+ }
246
+
247
+ // Invalidated buffer
248
+ {
249
+ MaybeStackBuffer<char > buf;
250
+ buf.Invalidate ();
251
+ EXPECT_TRUE (buf.IsInvalidated ());
252
+ EXPECT_FALSE (buf.IsAllocated ());
253
+ EXPECT_EQ (0U , buf.length ());
254
+ EXPECT_EQ (0U , buf.capacity ());
255
+ buf.Invalidate ();
256
+ EXPECT_TRUE (buf.IsInvalidated ());
257
+ }
258
+ }
0 commit comments