Use the GTest portability headers
[folly.git] / folly / io / test / IOBufQueueTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <folly/io/IOBufQueue.h>
18
19 #include <iostream>
20 #include <stdexcept>
21 #include <string.h>
22
23 #include <folly/Range.h>
24 #include <folly/portability/GTest.h>
25
26 using folly::IOBuf;
27 using folly::IOBufQueue;
28 using folly::StringPiece;
29 using std::pair;
30 using std::string;
31 using std::unique_ptr;
32
33 // String Comma Length macro for string literals
34 #define SCL(x) (x), sizeof(x) - 1
35
36 namespace {
37
38 IOBufQueue::Options clOptions;
39 struct Initializer {
40   Initializer() {
41     clOptions.cacheChainLength = true;
42   }
43 };
44 Initializer initializer;
45
46 unique_ptr<IOBuf>
47 stringToIOBuf(const char* s, uint32_t len) {
48   unique_ptr<IOBuf> buf = IOBuf::create(len);
49   memcpy(buf->writableTail(), s, len);
50   buf->append(len);
51   return buf;
52 }
53
54 void checkConsistency(const IOBufQueue& queue) {
55   if (queue.options().cacheChainLength) {
56     size_t len = queue.front() ? queue.front()->computeChainDataLength() : 0;
57     EXPECT_EQ(len, queue.chainLength());
58   }
59 }
60
61 }
62
63 TEST(IOBufQueue, Simple) {
64   IOBufQueue queue(clOptions);
65   EXPECT_EQ(nullptr, queue.front());
66   queue.append(SCL(""));
67   EXPECT_EQ(nullptr, queue.front());
68   queue.append(unique_ptr<IOBuf>());
69   EXPECT_EQ(nullptr, queue.front());
70   string emptyString;
71   queue.append(emptyString);
72   EXPECT_EQ(nullptr, queue.front());
73 }
74
75 TEST(IOBufQueue, Append) {
76   IOBufQueue queue(clOptions);
77   queue.append(SCL("Hello"));
78   IOBufQueue queue2(clOptions);
79   queue2.append(SCL(", "));
80   queue2.append(SCL("World"));
81   checkConsistency(queue);
82   checkConsistency(queue2);
83   queue.append(queue2.move());
84   checkConsistency(queue);
85   checkConsistency(queue2);
86   const IOBuf* chain = queue.front();
87   EXPECT_NE((IOBuf*)nullptr, chain);
88   EXPECT_EQ(12, chain->computeChainDataLength());
89   EXPECT_EQ(nullptr, queue2.front());
90 }
91
92 TEST(IOBufQueue, Append2) {
93   IOBufQueue queue(clOptions);
94   queue.append(SCL("Hello"));
95   IOBufQueue queue2(clOptions);
96   queue2.append(SCL(", "));
97   queue2.append(SCL("World"));
98   checkConsistency(queue);
99   checkConsistency(queue2);
100   queue.append(queue2);
101   checkConsistency(queue);
102   checkConsistency(queue2);
103   const IOBuf* chain = queue.front();
104   EXPECT_NE((IOBuf*)nullptr, chain);
105   EXPECT_EQ(12, chain->computeChainDataLength());
106   EXPECT_EQ(nullptr, queue2.front());
107 }
108
109 TEST(IOBufQueue, AppendStringPiece) {
110   std::string s("Hello, World");
111   IOBufQueue queue(clOptions);
112   IOBufQueue queue2(clOptions);
113   queue.append(s.data(), s.length());
114   queue2.append(s);
115   checkConsistency(queue);
116   checkConsistency(queue2);
117   const IOBuf* chain = queue.front();
118   const IOBuf* chain2 = queue2.front();
119   EXPECT_EQ(s.length(), chain->computeChainDataLength());
120   EXPECT_EQ(s.length(), chain2->computeChainDataLength());
121   EXPECT_EQ(0, memcmp(chain->data(), chain2->data(), s.length()));
122 }
123
124 TEST(IOBufQueue, Split) {
125   IOBufQueue queue(clOptions);
126   queue.append(stringToIOBuf(SCL("Hello")));
127   queue.append(stringToIOBuf(SCL(",")));
128   queue.append(stringToIOBuf(SCL(" ")));
129   queue.append(stringToIOBuf(SCL("")));
130   queue.append(stringToIOBuf(SCL("World")));
131   checkConsistency(queue);
132   EXPECT_EQ(12, queue.front()->computeChainDataLength());
133
134   unique_ptr<IOBuf> prefix(queue.split(1));
135   checkConsistency(queue);
136   EXPECT_EQ(1, prefix->computeChainDataLength());
137   EXPECT_EQ(11, queue.front()->computeChainDataLength());
138   prefix = queue.split(2);
139   checkConsistency(queue);
140   EXPECT_EQ(2, prefix->computeChainDataLength());
141   EXPECT_EQ(9, queue.front()->computeChainDataLength());
142   prefix = queue.split(3);
143   checkConsistency(queue);
144   EXPECT_EQ(3, prefix->computeChainDataLength());
145   EXPECT_EQ(6, queue.front()->computeChainDataLength());
146   prefix = queue.split(1);
147   checkConsistency(queue);
148   EXPECT_EQ(1, prefix->computeChainDataLength());
149   EXPECT_EQ(5, queue.front()->computeChainDataLength());
150   prefix = queue.split(5);
151   checkConsistency(queue);
152   EXPECT_EQ(5, prefix->computeChainDataLength());
153   EXPECT_EQ((IOBuf*)nullptr, queue.front());
154
155   queue.append(stringToIOBuf(SCL("Hello,")));
156   queue.append(stringToIOBuf(SCL(" World")));
157   checkConsistency(queue);
158   EXPECT_THROW({prefix = queue.split(13);}, std::underflow_error);
159   checkConsistency(queue);
160 }
161
162 TEST(IOBufQueue, Preallocate) {
163   IOBufQueue queue(clOptions);
164   queue.append(string("Hello"));
165   pair<void*,uint32_t> writable = queue.preallocate(2, 64, 64);
166   checkConsistency(queue);
167   EXPECT_NE((void*)nullptr, writable.first);
168   EXPECT_LE(2, writable.second);
169   EXPECT_GE(64, writable.second);
170   memcpy(writable.first, SCL(", "));
171   queue.postallocate(2);
172   checkConsistency(queue);
173   EXPECT_EQ(7, queue.front()->computeChainDataLength());
174   queue.append(SCL("World"));
175   checkConsistency(queue);
176   EXPECT_EQ(12, queue.front()->computeChainDataLength());
177   // There are not 2048 bytes available, this will alloc a new buf
178   writable = queue.preallocate(2048, 4096);
179   checkConsistency(queue);
180   EXPECT_LE(2048, writable.second);
181   // IOBuf allocates more than newAllocationSize, and we didn't cap it
182   EXPECT_GE(writable.second, 4096);
183   queue.postallocate(writable.second);
184   // queue has no empty space, make sure we allocate at least min, even if
185   // newAllocationSize < min
186   writable = queue.preallocate(1024, 1, 1024);
187   checkConsistency(queue);
188   EXPECT_EQ(1024, writable.second);
189 }
190
191 TEST(IOBufQueue, Wrap) {
192   IOBufQueue queue(clOptions);
193   const char* buf = "hello world goodbye";
194   size_t len = strlen(buf);
195   queue.wrapBuffer(buf, len, 6);
196   auto iob = queue.move();
197   EXPECT_EQ((len - 1) / 6 + 1, iob->countChainElements());
198   iob->unshare();
199   iob->coalesce();
200   EXPECT_EQ(StringPiece(buf),
201             StringPiece(reinterpret_cast<const char*>(iob->data()),
202                         iob->length()));
203 }
204
205 TEST(IOBufQueue, Trim) {
206   IOBufQueue queue(clOptions);
207   unique_ptr<IOBuf> a = IOBuf::create(4);
208   a->append(4);
209   queue.append(std::move(a));
210   checkConsistency(queue);
211   a = IOBuf::create(6);
212   a->append(6);
213   queue.append(std::move(a));
214   checkConsistency(queue);
215   a = IOBuf::create(8);
216   a->append(8);
217   queue.append(std::move(a));
218   checkConsistency(queue);
219   a = IOBuf::create(10);
220   a->append(10);
221   queue.append(std::move(a));
222   checkConsistency(queue);
223
224   EXPECT_EQ(4, queue.front()->countChainElements());
225   EXPECT_EQ(28, queue.front()->computeChainDataLength());
226   EXPECT_EQ(4, queue.front()->length());
227
228   queue.trimStart(1);
229   checkConsistency(queue);
230   EXPECT_EQ(4, queue.front()->countChainElements());
231   EXPECT_EQ(27, queue.front()->computeChainDataLength());
232   EXPECT_EQ(3, queue.front()->length());
233
234   queue.trimStart(5);
235   checkConsistency(queue);
236   EXPECT_EQ(3, queue.front()->countChainElements());
237   EXPECT_EQ(22, queue.front()->computeChainDataLength());
238   EXPECT_EQ(4, queue.front()->length());
239
240   queue.trimEnd(1);
241   checkConsistency(queue);
242   EXPECT_EQ(3, queue.front()->countChainElements());
243   EXPECT_EQ(21, queue.front()->computeChainDataLength());
244   EXPECT_EQ(9, queue.front()->prev()->length());
245
246   queue.trimEnd(20);
247   checkConsistency(queue);
248   EXPECT_EQ(1, queue.front()->countChainElements());
249   EXPECT_EQ(1, queue.front()->computeChainDataLength());
250   EXPECT_EQ(1, queue.front()->prev()->length());
251
252   queue.trimEnd(1);
253   checkConsistency(queue);
254   EXPECT_EQ(nullptr, queue.front());
255
256   EXPECT_THROW(queue.trimStart(2), std::underflow_error);
257   checkConsistency(queue);
258
259   EXPECT_THROW(queue.trimEnd(30), std::underflow_error);
260   checkConsistency(queue);
261 }
262
263 TEST(IOBufQueue, TrimPack) {
264   IOBufQueue queue(clOptions);
265   unique_ptr<IOBuf> a = IOBuf::create(64);
266   a->append(4);
267   queue.append(std::move(a), true);
268   checkConsistency(queue);
269   a = IOBuf::create(6);
270   a->append(6);
271   queue.append(std::move(a), true);
272   checkConsistency(queue);
273   a = IOBuf::create(8);
274   a->append(8);
275   queue.append(std::move(a), true);
276   checkConsistency(queue);
277   a = IOBuf::create(10);
278   a->append(10);
279   queue.append(std::move(a), true);
280   checkConsistency(queue);
281
282   EXPECT_EQ(1, queue.front()->countChainElements());
283   EXPECT_EQ(28, queue.front()->computeChainDataLength());
284   EXPECT_EQ(28, queue.front()->length());
285
286   queue.trimStart(1);
287   checkConsistency(queue);
288   EXPECT_EQ(1, queue.front()->countChainElements());
289   EXPECT_EQ(27, queue.front()->computeChainDataLength());
290   EXPECT_EQ(27, queue.front()->length());
291
292   queue.trimStart(5);
293   checkConsistency(queue);
294   EXPECT_EQ(1, queue.front()->countChainElements());
295   EXPECT_EQ(22, queue.front()->computeChainDataLength());
296   EXPECT_EQ(22, queue.front()->length());
297
298   queue.trimEnd(1);
299   checkConsistency(queue);
300   EXPECT_EQ(1, queue.front()->countChainElements());
301   EXPECT_EQ(21, queue.front()->computeChainDataLength());
302   EXPECT_EQ(21, queue.front()->prev()->length());
303
304   queue.trimEnd(20);
305   checkConsistency(queue);
306   EXPECT_EQ(1, queue.front()->countChainElements());
307   EXPECT_EQ(1, queue.front()->computeChainDataLength());
308   EXPECT_EQ(1, queue.front()->prev()->length());
309
310   queue.trimEnd(1);
311   checkConsistency(queue);
312   EXPECT_EQ(nullptr, queue.front());
313
314   EXPECT_THROW(queue.trimStart(2), std::underflow_error);
315   checkConsistency(queue);
316
317   EXPECT_THROW(queue.trimEnd(30), std::underflow_error);
318   checkConsistency(queue);
319 }
320
321 TEST(IOBufQueue, Prepend) {
322   folly::IOBufQueue queue;
323
324   auto buf = folly::IOBuf::create(10);
325   buf->advance(5);
326   queue.append(std::move(buf));
327
328   queue.append(SCL(" World"));
329   queue.prepend(SCL("Hello"));
330
331   EXPECT_THROW(queue.prepend(SCL("x")), std::overflow_error);
332
333   auto out = queue.move();
334   out->coalesce();
335   EXPECT_EQ("Hello World",
336             StringPiece(reinterpret_cast<const char*>(out->data()),
337                         out->length()));
338 }
339
340 TEST(IOBufQueue, PopFirst) {
341   IOBufQueue queue(IOBufQueue::cacheChainLength());
342   const char * strings[] = {
343     "Hello",
344     ",",
345     " ",
346     "",
347     "World"
348   };
349
350   const size_t numStrings=sizeof(strings)/sizeof(*strings);
351   size_t chainLength = 0;
352   for(size_t i = 0; i < numStrings; ++i) {
353     queue.append(stringToIOBuf(strings[i], strlen(strings[i])));
354     checkConsistency(queue);
355     chainLength += strlen(strings[i]);
356   }
357
358   unique_ptr<IOBuf> first;
359   for(size_t i = 0; i < numStrings; ++i) {
360     checkConsistency(queue);
361     EXPECT_EQ(chainLength, queue.front()->computeChainDataLength());
362     EXPECT_EQ(chainLength, queue.chainLength());
363     first = queue.pop_front();
364     chainLength-=strlen(strings[i]);
365     EXPECT_EQ(strlen(strings[i]), first->computeChainDataLength());
366   }
367   checkConsistency(queue);
368   EXPECT_EQ(chainLength, queue.chainLength());
369
370   EXPECT_EQ((IOBuf*)nullptr, queue.front());
371   first = queue.pop_front();
372   EXPECT_EQ((IOBuf*)nullptr, first.get());
373
374   checkConsistency(queue);
375   EXPECT_EQ((IOBuf*)nullptr, queue.front());
376   EXPECT_EQ(0, queue.chainLength());
377 }
378
379 TEST(IOBufQueue, AppendToString) {
380   IOBufQueue queue;
381   queue.append("hello ", 6);
382   queue.append("world", 5);
383   std::string s;
384   queue.appendToString(s);
385   EXPECT_EQ("hello world", s);
386 }
387
388 TEST(IOBufQueue, Gather) {
389   IOBufQueue queue;
390
391   queue.append(stringToIOBuf(SCL("hello ")));
392   queue.append(stringToIOBuf(SCL("world")));
393
394   EXPECT_EQ(queue.front()->length(), 6);
395   queue.gather(11);
396   EXPECT_EQ(queue.front()->length(), 11);
397
398   StringPiece s(
399       reinterpret_cast<const char*>(queue.front()->data()),
400       queue.front()->length());
401   EXPECT_EQ("hello world", s);
402 }