improve io::Appender functionality
[folly.git] / folly / io / test / IOBufCursorTest.cpp
1 /*
2  * Copyright 2014 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/IOBuf.h>
18
19 #include <gflags/gflags.h>
20 #include <boost/random.hpp>
21 #include <gtest/gtest.h>
22 #include <folly/Benchmark.h>
23 #include <folly/Format.h>
24 #include <folly/Range.h>
25 #include <folly/io/Cursor.h>
26 #include <folly/io/Cursor-defs.h>
27
28 DECLARE_bool(benchmark);
29
30 using folly::ByteRange;
31 using folly::format;
32 using folly::IOBuf;
33 using folly::StringPiece;
34 using std::unique_ptr;
35 using namespace folly::io;
36
37 TEST(IOBuf, RWCursor) {
38   unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
39   iobuf1->append(20);
40   unique_ptr<IOBuf> iobuf2(IOBuf::create(20));
41   iobuf2->append(20);
42
43   IOBuf* iob2ptr = iobuf2.get();
44   iobuf1->prependChain(std::move(iobuf2));
45
46   EXPECT_TRUE(iobuf1->isChained());
47
48   RWPrivateCursor wcursor(iobuf1.get());
49   Cursor rcursor(iobuf1.get());
50   wcursor.writeLE((uint64_t)1);
51   wcursor.writeLE((uint64_t)1);
52   wcursor.writeLE((uint64_t)1);
53   wcursor.write((uint8_t)1);
54
55   EXPECT_EQ(1, rcursor.readLE<uint64_t>());
56   rcursor.skip(8);
57   EXPECT_EQ(1, rcursor.readLE<uint32_t>());
58   rcursor.skip(0);
59   EXPECT_EQ(0, rcursor.read<uint8_t>());
60   EXPECT_EQ(0, rcursor.read<uint8_t>());
61   EXPECT_EQ(0, rcursor.read<uint8_t>());
62   EXPECT_EQ(0, rcursor.read<uint8_t>());
63   EXPECT_EQ(1, rcursor.read<uint8_t>());
64 }
65
66 TEST(IOBuf, skip) {
67   unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
68   iobuf1->append(20);
69   RWPrivateCursor wcursor(iobuf1.get());
70   wcursor.write((uint8_t)1);
71   wcursor.write((uint8_t)2);
72   Cursor cursor(iobuf1.get());
73   cursor.skip(1);
74   EXPECT_EQ(2, cursor.read<uint8_t>());
75 }
76
77 TEST(IOBuf, reset) {
78   unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
79   iobuf1->append(20);
80   RWPrivateCursor wcursor(iobuf1.get());
81   wcursor.write((uint8_t)1);
82   wcursor.write((uint8_t)2);
83   wcursor.reset(iobuf1.get());
84   EXPECT_EQ(1, wcursor.read<uint8_t>());
85 }
86
87 TEST(IOBuf, copy_assign_convert) {
88   unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
89   iobuf1->append(20);
90   RWPrivateCursor wcursor(iobuf1.get());
91   RWPrivateCursor cursor2(wcursor);
92   RWPrivateCursor cursor3(iobuf1.get());
93
94   wcursor.write((uint8_t)1);
95   cursor3 = wcursor;
96   wcursor.write((uint8_t)2);
97   Cursor cursor4(wcursor);
98   RWPrivateCursor cursor5(wcursor);
99   wcursor.write((uint8_t)3);
100
101   EXPECT_EQ(1, cursor2.read<uint8_t>());
102   EXPECT_EQ(2, cursor3.read<uint8_t>());
103   EXPECT_EQ(3, cursor4.read<uint8_t>());
104 }
105
106 TEST(IOBuf, arithmetic) {
107   IOBuf iobuf1(IOBuf::CREATE, 20);
108   iobuf1.append(20);
109   RWPrivateCursor wcursor(&iobuf1);
110   wcursor += 1;
111   wcursor.write((uint8_t)1);
112   Cursor cursor(&iobuf1);
113   cursor += 1;
114   EXPECT_EQ(1, cursor.read<uint8_t>());
115
116   Cursor start(&iobuf1);
117   Cursor cursor2 = start + 9;
118   EXPECT_EQ(7, cursor2 - cursor);
119   EXPECT_NE(cursor, cursor2);
120   cursor += 8;
121   cursor2 = cursor2 + 1;
122   EXPECT_EQ(cursor, cursor2);
123 }
124
125 TEST(IOBuf, endian) {
126   unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
127   iobuf1->append(20);
128   RWPrivateCursor wcursor(iobuf1.get());
129   Cursor rcursor(iobuf1.get());
130   uint16_t v = 1;
131   int16_t vu = -1;
132   wcursor.writeBE(v);
133   wcursor.writeBE(vu);
134   // Try a couple combinations to ensure they were generated correctly
135   wcursor.writeBE(vu);
136   wcursor.writeLE(vu);
137   wcursor.writeLE(vu);
138   wcursor.writeLE(v);
139   EXPECT_EQ(v, rcursor.readBE<uint16_t>());
140 }
141
142 TEST(IOBuf, Cursor) {
143   unique_ptr<IOBuf> iobuf1(IOBuf::create(1));
144   iobuf1->append(1);
145   RWPrivateCursor c(iobuf1.get());
146   c.write((uint8_t)40); // OK
147   try {
148     c.write((uint8_t)10); // Bad write, checked should except.
149     EXPECT_EQ(true, false);
150   } catch (...) {
151   }
152 }
153
154 TEST(IOBuf, UnshareCursor) {
155   uint8_t buf = 0;
156   unique_ptr<IOBuf> iobuf1(IOBuf::wrapBuffer(&buf, 1));
157   unique_ptr<IOBuf> iobuf2(IOBuf::wrapBuffer(&buf, 1));
158   RWUnshareCursor c1(iobuf1.get());
159   RWUnshareCursor c2(iobuf2.get());
160
161   c1.write((uint8_t)10); // This should duplicate the two buffers.
162   uint8_t t = c2.read<uint8_t>();
163   EXPECT_EQ(0, t);
164
165   iobuf1 = IOBuf::wrapBuffer(&buf, 1);
166   iobuf2 = IOBuf::wrapBuffer(&buf, 1);
167   RWPrivateCursor c3(iobuf1.get());
168   RWPrivateCursor c4(iobuf2.get());
169
170   c3.write((uint8_t)10); // This should _not_ duplicate the two buffers.
171   t = c4.read<uint8_t>();
172   EXPECT_EQ(10, t);
173 }
174
175 namespace {
176 void append(std::unique_ptr<IOBuf>& buf, folly::StringPiece data) {
177   EXPECT_LE(data.size(), buf->tailroom());
178   memcpy(buf->writableData(), data.data(), data.size());
179   buf->append(data.size());
180 }
181
182 void append(Appender& appender, StringPiece data) {
183   appender.push(ByteRange(data));
184 }
185
186 std::string toString(const IOBuf& buf) {
187   std::string str;
188   Cursor cursor(&buf);
189   std::pair<const uint8_t*, size_t> p;
190   while ((p = cursor.peek()).second) {
191     str.append(reinterpret_cast<const char*>(p.first), p.second);
192     cursor.skip(p.second);
193   }
194   return str;
195 }
196
197 }  // namespace
198
199 TEST(IOBuf, PullAndPeek) {
200   std::unique_ptr<IOBuf> iobuf1(IOBuf::create(10));
201   append(iobuf1, "he");
202   std::unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
203   append(iobuf2, "llo ");
204   std::unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
205   append(iobuf3, "world");
206   iobuf1->prependChain(std::move(iobuf2));
207   iobuf1->prependChain(std::move(iobuf3));
208   EXPECT_EQ(3, iobuf1->countChainElements());
209   EXPECT_EQ(11, iobuf1->computeChainDataLength());
210
211   char buf[12];
212   memset(buf, 0, sizeof(buf));
213   Cursor(iobuf1.get()).pull(buf, 11);
214   EXPECT_EQ("hello world", std::string(buf));
215
216   memset(buf, 0, sizeof(buf));
217   EXPECT_EQ(11, Cursor(iobuf1.get()).pullAtMost(buf, 20));
218   EXPECT_EQ("hello world", std::string(buf));
219
220   EXPECT_THROW({Cursor(iobuf1.get()).pull(buf, 20);},
221                std::out_of_range);
222
223   {
224     RWPrivateCursor cursor(iobuf1.get());
225     auto p = cursor.peek();
226     EXPECT_EQ("he", std::string(reinterpret_cast<const char*>(p.first),
227                                 p.second));
228     cursor.skip(p.second);
229     p = cursor.peek();
230     EXPECT_EQ("llo ", std::string(reinterpret_cast<const char*>(p.first),
231                                   p.second));
232     cursor.skip(p.second);
233     p = cursor.peek();
234     EXPECT_EQ("world", std::string(reinterpret_cast<const char*>(p.first),
235                                    p.second));
236     cursor.skip(p.second);
237     EXPECT_EQ(3, iobuf1->countChainElements());
238     EXPECT_EQ(11, iobuf1->computeChainDataLength());
239   }
240
241   {
242     RWPrivateCursor cursor(iobuf1.get());
243     cursor.gather(11);
244     auto p = cursor.peek();
245     EXPECT_EQ("hello world", std::string(reinterpret_cast<const
246                                          char*>(p.first), p.second));
247     EXPECT_EQ(1, iobuf1->countChainElements());
248     EXPECT_EQ(11, iobuf1->computeChainDataLength());
249   }
250 }
251
252 TEST(IOBuf, pushCursorData) {
253   unique_ptr<IOBuf> iobuf1(IOBuf::create(20));
254   iobuf1->append(15);
255   iobuf1->trimStart(5);
256   unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
257   unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
258   iobuf3->append(10);
259
260   iobuf1->prependChain(std::move(iobuf2));
261   iobuf1->prependChain(std::move(iobuf3));
262   EXPECT_TRUE(iobuf1->isChained());
263
264   //write 20 bytes to the buffer chain
265   RWPrivateCursor wcursor(iobuf1.get());
266   wcursor.writeBE<uint64_t>(1);
267   wcursor.writeBE<uint64_t>(10);
268   wcursor.writeBE<uint32_t>(20);
269
270   // create a read buffer for the buffer chain
271   Cursor rcursor(iobuf1.get());
272   EXPECT_EQ(1, rcursor.readBE<uint64_t>());
273   EXPECT_EQ(10, rcursor.readBE<uint64_t>());
274   EXPECT_EQ(20, rcursor.readBE<uint32_t>());
275   EXPECT_EQ(0, rcursor.totalLength());
276   rcursor.reset(iobuf1.get());
277   EXPECT_EQ(20, rcursor.totalLength());
278
279   // create another write buffer
280   unique_ptr<IOBuf> iobuf4(IOBuf::create(30));
281   iobuf4->append(30);
282   RWPrivateCursor wcursor2(iobuf4.get());
283   // write buffer chain data into it, now wcursor2 should only
284   // have 10 bytes writable space
285   wcursor2.push(rcursor, 20);
286   EXPECT_EQ(wcursor2.totalLength(), 10);
287   // write again with not enough space in rcursor
288   EXPECT_THROW(wcursor2.push(rcursor, 20), std::out_of_range);
289
290   // create a read cursor to check iobuf3 data back
291   Cursor rcursor2(iobuf4.get());
292   EXPECT_EQ(1, rcursor2.readBE<uint64_t>());
293   EXPECT_EQ(10, rcursor2.readBE<uint64_t>());
294   EXPECT_EQ(20, rcursor2.readBE<uint32_t>());
295
296 }
297
298 TEST(IOBuf, Gather) {
299   std::unique_ptr<IOBuf> iobuf1(IOBuf::create(10));
300   append(iobuf1, "he");
301   std::unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
302   append(iobuf2, "llo ");
303   std::unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
304   append(iobuf3, "world");
305   iobuf1->prependChain(std::move(iobuf2));
306   iobuf1->prependChain(std::move(iobuf3));
307   EXPECT_EQ(3, iobuf1->countChainElements());
308   EXPECT_EQ(11, iobuf1->computeChainDataLength());
309
310   // Attempting to gather() more data than available in the chain should fail.
311   // Try from the very beginning of the chain.
312   RWPrivateCursor cursor(iobuf1.get());
313   EXPECT_THROW(cursor.gather(15), std::overflow_error);
314   // Now try from the middle of the chain
315   cursor += 3;
316   EXPECT_THROW(cursor.gather(10), std::overflow_error);
317
318   // Calling gatherAtMost() should succeed, however, and just gather
319   // as much as it can
320   cursor.gatherAtMost(10);
321   EXPECT_EQ(8, cursor.length());
322   EXPECT_EQ(8, cursor.totalLength());
323   EXPECT_EQ("lo world",
324             folly::StringPiece(reinterpret_cast<const char*>(cursor.data()),
325                                cursor.length()));
326   EXPECT_EQ(2, iobuf1->countChainElements());
327   EXPECT_EQ(11, iobuf1->computeChainDataLength());
328
329   // Now try gather again on the chain head
330   cursor = RWPrivateCursor(iobuf1.get());
331   cursor.gather(5);
332   // Since gather() doesn't split buffers, everything should be collapsed into
333   // a single buffer now.
334   EXPECT_EQ(1, iobuf1->countChainElements());
335   EXPECT_EQ(11, iobuf1->computeChainDataLength());
336   EXPECT_EQ(11, cursor.length());
337   EXPECT_EQ(11, cursor.totalLength());
338 }
339
340 TEST(IOBuf, cloneAndInsert) {
341   std::unique_ptr<IOBuf> iobuf1(IOBuf::create(10));
342   append(iobuf1, "he");
343   std::unique_ptr<IOBuf> iobuf2(IOBuf::create(10));
344   append(iobuf2, "llo ");
345   std::unique_ptr<IOBuf> iobuf3(IOBuf::create(10));
346   append(iobuf3, "world");
347   iobuf1->prependChain(std::move(iobuf2));
348   iobuf1->prependChain(std::move(iobuf3));
349   EXPECT_EQ(3, iobuf1->countChainElements());
350   EXPECT_EQ(11, iobuf1->computeChainDataLength());
351
352   std::unique_ptr<IOBuf> cloned;
353
354   Cursor(iobuf1.get()).clone(cloned, 3);
355   EXPECT_EQ(2, cloned->countChainElements());
356   EXPECT_EQ(3, cloned->computeChainDataLength());
357
358
359   EXPECT_EQ(11, Cursor(iobuf1.get()).cloneAtMost(cloned, 20));
360   EXPECT_EQ(3, cloned->countChainElements());
361   EXPECT_EQ(11, cloned->computeChainDataLength());
362
363
364   EXPECT_THROW({Cursor(iobuf1.get()).clone(cloned, 20);},
365                std::out_of_range);
366
367   {
368     // Check that inserting in the middle of an iobuf splits
369     RWPrivateCursor cursor(iobuf1.get());
370     Cursor(iobuf1.get()).clone(cloned, 3);
371     EXPECT_EQ(2, cloned->countChainElements());
372     EXPECT_EQ(3, cloned->computeChainDataLength());
373
374     cursor.skip(1);
375
376     cursor.insert(std::move(cloned));
377     cursor.insert(folly::IOBuf::create(0));
378     EXPECT_EQ(7, iobuf1->countChainElements());
379     EXPECT_EQ(14, iobuf1->computeChainDataLength());
380     // Check that nextBuf got set correctly to the buffer with 1 byte left
381     EXPECT_EQ(1, cursor.peek().second);
382     cursor.read<uint8_t>();
383   }
384
385   {
386     // Check that inserting at the end doesn't create empty buf
387     RWPrivateCursor cursor(iobuf1.get());
388     Cursor(iobuf1.get()).clone(cloned, 1);
389     EXPECT_EQ(1, cloned->countChainElements());
390     EXPECT_EQ(1, cloned->computeChainDataLength());
391
392     cursor.skip(1);
393
394     cursor.insert(std::move(cloned));
395     EXPECT_EQ(8, iobuf1->countChainElements());
396     EXPECT_EQ(15, iobuf1->computeChainDataLength());
397     // Check that nextBuf got set correctly
398     cursor.read<uint8_t>();
399   }
400   {
401     // Check that inserting at the beginning doesn't create empty buf
402     RWPrivateCursor cursor(iobuf1.get());
403     Cursor(iobuf1.get()).clone(cloned, 1);
404     EXPECT_EQ(1, cloned->countChainElements());
405     EXPECT_EQ(1, cloned->computeChainDataLength());
406
407     cursor.insert(std::move(cloned));
408     EXPECT_EQ(9, iobuf1->countChainElements());
409     EXPECT_EQ(16, iobuf1->computeChainDataLength());
410     // Check that nextBuf got set correctly
411     cursor.read<uint8_t>();
412   }
413 }
414
415 TEST(IOBuf, Appender) {
416   std::unique_ptr<IOBuf> head(IOBuf::create(10));
417   append(head, "hello");
418
419   Appender app(head.get(), 10);
420   uint32_t cap = head->capacity();
421   uint32_t len1 = app.length();
422   EXPECT_EQ(cap - 5, len1);
423   app.ensure(len1);  // won't grow
424   EXPECT_EQ(len1, app.length());
425   app.ensure(len1 + 1);  // will grow
426   EXPECT_LE(len1 + 1, app.length());
427
428   append(app, " world");
429   EXPECT_EQ("hello world", toString(*head));
430 }
431
432 TEST(IOBuf, Printf) {
433   IOBuf head(IOBuf::CREATE, 24);
434   Appender app(&head, 32);
435
436   app.printf("%s", "test");
437   EXPECT_EQ(head.length(), 4);
438   EXPECT_EQ(0, memcmp(head.data(), "test\0", 5));
439
440   app.printf("%d%s %s%s %#x", 32, "this string is",
441              "longer than our original allocation size,",
442              "and will therefore require a new allocation", 0x12345678);
443   // The tailroom should start with a nul byte now.
444   EXPECT_GE(head.prev()->tailroom(), 1);
445   EXPECT_EQ(0, *head.prev()->tail());
446
447   EXPECT_EQ("test32this string is longer than our original "
448             "allocation size,and will therefore require a "
449             "new allocation 0x12345678",
450             head.moveToFbString().toStdString());
451 }
452
453 TEST(IOBuf, Format) {
454   IOBuf head(IOBuf::CREATE, 24);
455   Appender app(&head, 32);
456
457   format("{}", "test")(app);
458   EXPECT_EQ(head.length(), 4);
459   EXPECT_EQ(0, memcmp(head.data(), "test", 4));
460
461   auto fmt = format("{}{} {}{} {:#x}",
462                     32, "this string is",
463                     "longer than our original allocation size,",
464                     "and will therefore require a new allocation",
465                     0x12345678);
466   fmt(app);
467   EXPECT_EQ("test32this string is longer than our original "
468             "allocation size,and will therefore require a "
469             "new allocation 0x12345678",
470             head.moveToFbString().toStdString());
471 }
472
473 TEST(IOBuf, QueueAppender) {
474   folly::IOBufQueue queue;
475
476   // Allocate 100 bytes at once, but don't grow past 1024
477   QueueAppender app(&queue, 100);
478   size_t n = 1024 / sizeof(uint32_t);
479   for (uint32_t i = 0; i < n; ++i) {
480     app.writeBE(i);
481   }
482
483   // There must be a goodMallocSize between 100 and 1024...
484   EXPECT_LT(1, queue.front()->countChainElements());
485   const IOBuf* buf = queue.front();
486   do {
487     EXPECT_LE(100, buf->capacity());
488     buf = buf->next();
489   } while (buf != queue.front());
490
491   Cursor cursor(queue.front());
492   for (uint32_t i = 0; i < n; ++i) {
493     EXPECT_EQ(i, cursor.readBE<uint32_t>());
494   }
495
496   EXPECT_THROW({cursor.readBE<uint32_t>();}, std::out_of_range);
497 }
498
499 TEST(IOBuf, CursorOperators) {
500   // Test operators on a single-item chain
501   {
502     std::unique_ptr<IOBuf> chain1(IOBuf::create(20));
503     chain1->append(10);
504
505     Cursor curs1(chain1.get());
506     EXPECT_EQ(0, curs1 - chain1.get());
507     curs1.skip(3);
508     EXPECT_EQ(3, curs1 - chain1.get());
509     curs1.skip(7);
510     EXPECT_EQ(10, curs1 - chain1.get());
511
512     Cursor curs2(chain1.get());
513     EXPECT_EQ(0, curs2 - chain1.get());
514     EXPECT_EQ(10, curs1 - curs2);
515     EXPECT_THROW(curs2 - curs1, std::out_of_range);
516   }
517
518   // Test cross-chain operations
519   {
520     std::unique_ptr<IOBuf> chain1(IOBuf::create(20));
521     chain1->append(10);
522     std::unique_ptr<IOBuf> chain2 = chain1->clone();
523
524     Cursor curs1(chain1.get());
525     Cursor curs2(chain2.get());
526     EXPECT_THROW(curs1 - curs2, std::out_of_range);
527     EXPECT_THROW(curs1 - chain2.get(), std::out_of_range);
528   }
529
530   // Test operations on multi-item chains
531   {
532     std::unique_ptr<IOBuf> chain(IOBuf::create(20));
533     chain->append(10);
534     chain->appendChain(chain->clone());
535     EXPECT_EQ(20, chain->computeChainDataLength());
536
537     Cursor curs1(chain.get());
538     curs1.skip(5);
539     Cursor curs2(chain.get());
540     curs2.skip(3);
541     EXPECT_EQ(2, curs1 - curs2);
542     EXPECT_EQ(5, curs1 - chain.get());
543     EXPECT_THROW(curs2 - curs1, std::out_of_range);
544
545     curs1.skip(7);
546     EXPECT_EQ(9, curs1 - curs2);
547     EXPECT_EQ(12, curs1 - chain.get());
548     EXPECT_THROW(curs2 - curs1, std::out_of_range);
549
550     curs2.skip(7);
551     EXPECT_EQ(2, curs1 - curs2);
552     EXPECT_THROW(curs2 - curs1, std::out_of_range);
553   }
554 }
555
556 TEST(IOBuf, StringOperations) {
557   // Test a single buffer with two null-terminated strings and an extra uint8_t
558   // at the end
559   {
560     std::unique_ptr<IOBuf> chain(IOBuf::create(16));
561     Appender app(chain.get(), 0);
562     app.push(reinterpret_cast<const uint8_t*>("hello\0world\0\x01"), 13);
563
564     Cursor curs(chain.get());
565     EXPECT_STREQ("hello", curs.readTerminatedString().c_str());
566     EXPECT_STREQ("world", curs.readTerminatedString().c_str());
567     EXPECT_EQ(1, curs.read<uint8_t>());
568   }
569
570   // Test multiple buffers where the first is empty and the string starts in
571   // the second buffer.
572   {
573     std::unique_ptr<IOBuf> chain(IOBuf::create(8));
574     chain->prependChain(IOBuf::create(12));
575     Appender app(chain.get(), 0);
576     app.push(reinterpret_cast<const uint8_t*>("hello world\0"), 12);
577
578     Cursor curs(chain.get());
579     EXPECT_STREQ("hello world", curs.readTerminatedString().c_str());
580   }
581
582   // Test multiple buffers with a single null-terminated string spanning them
583   {
584     std::unique_ptr<IOBuf> chain(IOBuf::create(8));
585     chain->prependChain(IOBuf::create(8));
586     chain->append(8);
587     chain->next()->append(4);
588     RWPrivateCursor rwc(chain.get());
589     rwc.push(reinterpret_cast<const uint8_t*>("hello world\0"), 12);
590
591     Cursor curs(chain.get());
592     EXPECT_STREQ("hello world", curs.readTerminatedString().c_str());
593   }
594
595   // Test a reading a null-terminated string that's longer than the maximum
596   // allowable length
597   {
598     std::unique_ptr<IOBuf> chain(IOBuf::create(16));
599     Appender app(chain.get(), 0);
600     app.push(reinterpret_cast<const uint8_t*>("hello world\0"), 12);
601
602     Cursor curs(chain.get());
603     EXPECT_THROW(curs.readTerminatedString('\0', 5), std::length_error);
604   }
605
606   // Test reading a null-terminated string from a chain with an empty buffer at
607   // the front
608   {
609     std::unique_ptr<IOBuf> buf(IOBuf::create(8));
610     Appender app(buf.get(), 0);
611     app.push(reinterpret_cast<const uint8_t*>("hello\0"), 6);
612     std::unique_ptr<IOBuf> chain(IOBuf::create(8));
613     chain->prependChain(std::move(buf));
614
615     Cursor curs(chain.get());
616     EXPECT_STREQ("hello", curs.readTerminatedString().c_str());
617   }
618
619   // Test reading a two fixed-length strings from a single buffer with an extra
620   // uint8_t at the end
621   {
622     std::unique_ptr<IOBuf> chain(IOBuf::create(16));
623     Appender app(chain.get(), 0);
624     app.push(reinterpret_cast<const uint8_t*>("helloworld\x01"), 11);
625
626     Cursor curs(chain.get());
627     EXPECT_STREQ("hello", curs.readFixedString(5).c_str());
628     EXPECT_STREQ("world", curs.readFixedString(5).c_str());
629     EXPECT_EQ(1, curs.read<uint8_t>());
630   }
631
632   // Test multiple buffers where the first is empty and a fixed-length string
633   // starts in the second buffer.
634   {
635     std::unique_ptr<IOBuf> chain(IOBuf::create(8));
636     chain->prependChain(IOBuf::create(16));
637     Appender app(chain.get(), 0);
638     app.push(reinterpret_cast<const uint8_t*>("hello world"), 11);
639
640     Cursor curs(chain.get());
641     EXPECT_STREQ("hello world", curs.readFixedString(11).c_str());
642   }
643
644   // Test multiple buffers with a single fixed-length string spanning them
645   {
646     std::unique_ptr<IOBuf> chain(IOBuf::create(8));
647     chain->prependChain(IOBuf::create(8));
648     chain->append(7);
649     chain->next()->append(4);
650     RWPrivateCursor rwc(chain.get());
651     rwc.push(reinterpret_cast<const uint8_t*>("hello world"), 11);
652
653     Cursor curs(chain.get());
654     EXPECT_STREQ("hello world", curs.readFixedString(11).c_str());
655   }
656
657   // Test reading a fixed-length string from a chain with an empty buffer at
658   // the front
659   {
660     std::unique_ptr<IOBuf> buf(IOBuf::create(8));
661     Appender app(buf.get(), 0);
662     app.push(reinterpret_cast<const uint8_t*>("hello"), 5);
663     std::unique_ptr<IOBuf> chain(IOBuf::create(8));
664     chain->prependChain(std::move(buf));
665
666     Cursor curs(chain.get());
667     EXPECT_STREQ("hello", curs.readFixedString(5).c_str());
668   }
669 }
670
671 int benchmark_size = 1000;
672 unique_ptr<IOBuf> iobuf_benchmark;
673
674 unique_ptr<IOBuf> iobuf_read_benchmark;
675
676 template <class CursClass>
677 void runBenchmark() {
678   CursClass c(iobuf_benchmark.get());
679
680   for(int i = 0; i < benchmark_size; i++) {
681     c.write((uint8_t)0);
682   }
683 }
684
685 BENCHMARK(rwPrivateCursorBenchmark, iters) {
686   while (iters--) {
687     runBenchmark<RWPrivateCursor>();
688   }
689 }
690
691 BENCHMARK(rwUnshareCursorBenchmark, iters) {
692   while (iters--) {
693     runBenchmark<RWUnshareCursor>();
694   }
695 }
696
697
698 BENCHMARK(cursorBenchmark, iters) {
699   while (iters--) {
700     Cursor c(iobuf_read_benchmark.get());
701     for(int i = 0; i < benchmark_size ; i++) {
702       c.read<uint8_t>();
703     }
704   }
705 }
706
707 BENCHMARK(skipBenchmark, iters) {
708   uint8_t buf;
709   while (iters--) {
710     Cursor c(iobuf_read_benchmark.get());
711     for(int i = 0; i < benchmark_size ; i++) {
712       c.peek();
713       c.skip(1);
714     }
715   }
716 }
717
718 // fbmake opt
719 // _bin/folly/experimental/io/test/iobuf_cursor_test -benchmark
720 //
721 // Benchmark                               Iters   Total t    t/iter iter/sec
722 // ---------------------------------------------------------------------------
723 // rwPrivateCursorBenchmark               100000  142.9 ms  1.429 us  683.5 k
724 // rwUnshareCursorBenchmark               100000  309.3 ms  3.093 us  315.7 k
725 // cursorBenchmark                        100000  741.4 ms  7.414 us  131.7 k
726 // skipBenchmark                          100000  738.9 ms  7.389 us  132.2 k
727 //
728 // uname -a:
729 //
730 // Linux dev2159.snc6.facebook.com 2.6.33-7_fbk15_104e4d0 #1 SMP
731 // Tue Oct 19 22:40:30 PDT 2010 x86_64 x86_64 x86_64 GNU/Linux
732 //
733 // 72GB RAM, 2 CPUs (Intel(R) Xeon(R) CPU L5630  @ 2.13GHz)
734 // hyperthreading disabled
735
736 int main(int argc, char** argv) {
737   testing::InitGoogleTest(&argc, argv);
738   gflags::ParseCommandLineFlags(&argc, &argv, true);
739
740   auto ret = RUN_ALL_TESTS();
741
742   if (ret == 0 && FLAGS_benchmark) {
743     iobuf_benchmark = IOBuf::create(benchmark_size);
744     iobuf_benchmark->append(benchmark_size);
745
746     iobuf_read_benchmark = IOBuf::create(1);
747     for (int i = 0; i < benchmark_size; i++) {
748       unique_ptr<IOBuf> iobuf2(IOBuf::create(1));
749       iobuf2->append(1);
750       iobuf_read_benchmark->prependChain(std::move(iobuf2));
751     }
752
753     folly::runBenchmarks();
754   }
755
756   return ret;
757 }