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