PageRenderTime 1321ms CodeModel.GetById 111ms app.highlight 1095ms RepoModel.GetById 36ms app.codeStats 22ms

/mordor/tests/buffer.cpp

http://github.com/mozy/mordor
C++ | 840 lines | 737 code | 95 blank | 8 comment | 52 complexity | b8f9b1e326ae510e0e680c84a6117497 MD5 | raw file
  1// Copyright (c) 2009 - Mozy, Inc.
  2
  3#include <boost/bind.hpp>
  4
  5#include "mordor/streams/buffer.h"
  6#include "mordor/test/test.h"
  7
  8using namespace Mordor;
  9using namespace Mordor::Test;
 10
 11MORDOR_UNITTEST(Buffer, copyInString)
 12{
 13    Buffer b;
 14    b.copyIn("hello");
 15    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 5u);
 16    MORDOR_TEST_ASSERT_EQUAL(b.writeAvailable(), 0u);
 17    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 1u);
 18    MORDOR_TEST_ASSERT(b == "hello");
 19}
 20
 21MORDOR_UNITTEST(Buffer, copyInStdString)
 22{
 23    Buffer b;
 24    std::string str("abc\0def", 7);
 25    b.copyIn(str);
 26    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 7u);
 27    MORDOR_TEST_ASSERT_EQUAL(b.writeAvailable(), 0u);
 28    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 1u);
 29    MORDOR_TEST_ASSERT(b == str);
 30}
 31
 32MORDOR_UNITTEST(Buffer, copyInStdString2)
 33{
 34    Buffer b;
 35    std::ostringstream os;
 36    os << "hello" << '\0' << "world" << '\0' << '\x0a';
 37    b.copyIn(os.str());
 38    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 13u);
 39    MORDOR_TEST_ASSERT_EQUAL(b.writeAvailable(), 0u);
 40    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 1u);
 41    MORDOR_TEST_ASSERT(b == os.str());
 42}
 43
 44MORDOR_UNITTEST(Buffer, copyInOtherBuffer)
 45{
 46    Buffer b1, b2("hello");
 47    b1.copyIn(b2);
 48    MORDOR_TEST_ASSERT_EQUAL(b1.readAvailable(), 5u);
 49    MORDOR_TEST_ASSERT_EQUAL(b1.writeAvailable(), 0u);
 50    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 1u);
 51    MORDOR_TEST_ASSERT(b1 == "hello");
 52}
 53
 54MORDOR_UNITTEST(Buffer, copyInPartial)
 55{
 56    Buffer b1, b2("hello");
 57    b1.copyIn(b2, 3);
 58    MORDOR_TEST_ASSERT_EQUAL(b1.readAvailable(), 3u);
 59    MORDOR_TEST_ASSERT_EQUAL(b1.writeAvailable(), 0u);
 60    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 1u);
 61    MORDOR_TEST_ASSERT(b1 == "hel");
 62}
 63
 64MORDOR_UNITTEST(Buffer, copyInOffset)
 65{
 66    Buffer b1, b2("hello world");
 67    b1.copyIn(b2, 7, 2);
 68    MORDOR_TEST_ASSERT_EQUAL(b1.readAvailable(), 7u);
 69    MORDOR_TEST_ASSERT_EQUAL(b1.writeAvailable(), 0u);
 70    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 1u);
 71    MORDOR_TEST_ASSERT(b1 == "llo wor");
 72}
 73
 74MORDOR_UNITTEST(Buffer, copyInOffsetMultiSegments)
 75{
 76    Buffer b1, b2;
 77    b2.copyIn("hello\n");
 78    b2.copyIn("foo\n");
 79    b2.copyIn("bar\n");
 80    MORDOR_TEST_ASSERT_EQUAL(b2.segments(), 3u);
 81    b1.copyIn(b2, 5, 7);
 82    MORDOR_TEST_ASSERT_EQUAL(b1.readAvailable(), 5u);
 83    MORDOR_TEST_ASSERT_EQUAL(b1.writeAvailable(), 0u);
 84    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 2u);
 85    MORDOR_TEST_ASSERT(b1 == "oo\nba");
 86}
 87
 88MORDOR_UNITTEST(Buffer, copyInStringToReserved)
 89{
 90    Buffer b;
 91    b.reserve(5);
 92    b.copyIn("hello");
 93    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 5u);
 94    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 1u);
 95    MORDOR_TEST_ASSERT(b == "hello");
 96}
 97
 98MORDOR_UNITTEST(Buffer, copyInStringAfterAnotherSegment)
 99{
100    Buffer b("hello");
101    b.copyIn("world");
102    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 10u);
103    MORDOR_TEST_ASSERT_EQUAL(b.writeAvailable(), 0u);
104    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 2u);
105    MORDOR_TEST_ASSERT(b == "helloworld");
106}
107
108MORDOR_UNITTEST(Buffer, copyInStringToReservedAfterAnotherSegment)
109{
110    Buffer b("hello");
111    b.reserve(5);
112    b.copyIn("world");
113    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 10u);
114    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 2u);
115    MORDOR_TEST_ASSERT(b == "helloworld");
116}
117
118MORDOR_UNITTEST(Buffer, copyInStringToSplitSegment)
119{
120    Buffer b;
121    b.reserve(10);
122    b.copyIn("hello");
123    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 5u);
124    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(b.writeAvailable(), 5u);
125    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 1u);
126    b.copyIn("world");
127    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 10u);
128    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 1u);
129    MORDOR_TEST_ASSERT(b == "helloworld");
130}
131
132MORDOR_UNITTEST(Buffer, copyInWithReserve)
133{
134    Buffer b1, b2("hello");
135    b1.reserve(10);
136    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(b1.writeAvailable(), 10u);
137    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 1u);
138    size_t writeAvailable = b1.writeAvailable();
139    b1.copyIn(b2);
140    MORDOR_TEST_ASSERT_EQUAL(b1.readAvailable(), 5u);
141    // Shouldn't have eaten any
142    MORDOR_TEST_ASSERT_EQUAL(b1.writeAvailable(), writeAvailable);
143    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 2u);
144    MORDOR_TEST_ASSERT(b1 == "hello");
145}
146
147MORDOR_UNITTEST(Buffer, copyInToSplitSegment)
148{
149    Buffer b1, b2("world");
150    b1.reserve(10);
151    b1.copyIn("hello");
152    MORDOR_TEST_ASSERT_EQUAL(b1.readAvailable(), 5u);
153    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(b1.writeAvailable(), 5u);
154    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 1u);
155    size_t writeAvailable = b1.writeAvailable();
156    b1.copyIn(b2, 5);
157    MORDOR_TEST_ASSERT_EQUAL(b1.readAvailable(), 10u);
158    // Shouldn't have eaten any
159    MORDOR_TEST_ASSERT_EQUAL(b1.writeAvailable(), writeAvailable);
160    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 3u);
161    MORDOR_TEST_ASSERT(b1 == "helloworld");
162}
163
164#ifndef NDEBUG
165MORDOR_UNITTEST(Buffer, copyInMoreThanThereIs)
166{
167    Buffer b1, b2;
168    MORDOR_TEST_ASSERT_ASSERTED(b1.copyIn(b2, 1));
169    b2.copyIn("hello");
170    MORDOR_TEST_ASSERT_ASSERTED(b1.copyIn(b2, 6));
171}
172#endif
173
174MORDOR_UNITTEST(Buffer, copyInMerge)
175{
176    Buffer b1, b2("hello");
177    b1.copyIn(b2, 2);
178    b2.consume(2);
179    b1.copyIn(b2, 3);
180    MORDOR_TEST_ASSERT_EQUAL(b1.readAvailable(), 5u);
181    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 1u);
182    MORDOR_TEST_ASSERT(b1 == "hello");
183}
184
185MORDOR_UNITTEST(Buffer, copyInMergePlus)
186{
187    Buffer b1, b2("hello");
188    b2.copyIn("world");
189    MORDOR_TEST_ASSERT_EQUAL(b2.segments(), 2u);
190    b1.copyIn(b2, 2);
191    b2.consume(2);
192    b1.copyIn(b2, 4);
193    MORDOR_TEST_ASSERT_EQUAL(b1.readAvailable(), 6u);
194    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 2u);
195    MORDOR_TEST_ASSERT(b1 == "hellow");
196}
197
198MORDOR_UNITTEST(Buffer, copyOutOffset)
199{
200    Buffer b("hello world");
201    std::string out;
202    out.resize(7);
203    b.copyOut(&out[0], 7, 2);
204    MORDOR_TEST_ASSERT(out == "llo wor");
205}
206
207MORDOR_UNITTEST(Buffer, noSplitOnTruncate)
208{
209    Buffer b1;
210    b1.reserve(10);
211    b1.copyIn("hello");
212    b1.truncate(5);
213    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(b1.writeAvailable(), 5u);
214    b1.copyIn("world");
215    MORDOR_TEST_ASSERT_EQUAL(b1.segments(), 1u);
216    MORDOR_TEST_ASSERT(b1 == "helloworld");
217}
218
219MORDOR_UNITTEST(Buffer, copyConstructor)
220{
221    Buffer buf1;
222    buf1.copyIn("hello");
223    Buffer buf2(buf1);
224    MORDOR_TEST_ASSERT(buf1 == "hello");
225    MORDOR_TEST_ASSERT(buf2 == "hello");
226    MORDOR_TEST_ASSERT_EQUAL(buf1.writeAvailable(), 0u);
227    MORDOR_TEST_ASSERT_EQUAL(buf2.writeAvailable(), 0u);
228}
229
230MORDOR_UNITTEST(Buffer, copyConstructorImmutability)
231{
232    Buffer buf1;
233    buf1.reserve(10);
234    Buffer buf2(buf1);
235    buf1.copyIn("hello");
236    buf2.copyIn("tommy");
237    MORDOR_TEST_ASSERT_EQUAL(buf1.readAvailable(), 5u);
238    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(buf1.writeAvailable(), 5u);
239    MORDOR_TEST_ASSERT_EQUAL(buf2.readAvailable(), 5u);
240    MORDOR_TEST_ASSERT_EQUAL(buf2.writeAvailable(), 0u);
241    MORDOR_TEST_ASSERT(buf1 == "hello");
242    MORDOR_TEST_ASSERT(buf2 == "tommy");
243}
244
245MORDOR_UNITTEST(Buffer, truncate)
246{
247    Buffer buf("hello");
248    buf.truncate(3);
249    MORDOR_TEST_ASSERT(buf == "hel");
250}
251
252MORDOR_UNITTEST(Buffer, truncateMultipleSegments1)
253{
254    Buffer buf("hello");
255    buf.copyIn("world");
256    buf.truncate(3);
257    MORDOR_TEST_ASSERT(buf == "hel");
258}
259
260MORDOR_UNITTEST(Buffer, truncateMultipleSegments2)
261{
262    Buffer buf("hello");
263    buf.copyIn("world");
264    buf.truncate(8);
265    MORDOR_TEST_ASSERT(buf == "hellowor");
266}
267
268MORDOR_UNITTEST(Buffer, truncateBeforeWriteSegments)
269{
270    Buffer buf("hello");
271    buf.reserve(5);
272    buf.truncate(3);
273    MORDOR_TEST_ASSERT(buf == "hel");
274    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(buf.writeAvailable(), 5u);
275}
276
277MORDOR_UNITTEST(Buffer, truncateAtWriteSegments)
278{
279    Buffer buf("hello");
280    buf.reserve(10);
281    buf.copyIn("world");
282    buf.truncate(8);
283    MORDOR_TEST_ASSERT(buf == "hellowor");
284    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(buf.writeAvailable(), 10u);
285}
286
287MORDOR_UNITTEST(Buffer, compareEmpty)
288{
289    Buffer buf1, buf2;
290    MORDOR_TEST_ASSERT(buf1 == buf2);
291    MORDOR_TEST_ASSERT(!(buf1 != buf2));
292}
293
294MORDOR_UNITTEST(Buffer, compareSimpleInequality)
295{
296    Buffer buf1, buf2("h");
297    MORDOR_TEST_ASSERT(buf1 != buf2);
298    MORDOR_TEST_ASSERT(!(buf1 == buf2));
299}
300
301MORDOR_UNITTEST(Buffer, compareIdentical)
302{
303    Buffer buf1("hello"), buf2("hello");
304    MORDOR_TEST_ASSERT(buf1 == buf2);
305    MORDOR_TEST_ASSERT(!(buf1 != buf2));
306}
307
308MORDOR_UNITTEST(Buffer, compareLotsOfSegmentsOnTheLeft)
309{
310    Buffer buf1, buf2("hello world!");
311    buf1.copyIn("he");
312    buf1.copyIn("l");
313    buf1.copyIn("l");
314    buf1.copyIn("o wor");
315    buf1.copyIn("ld!");
316    MORDOR_TEST_ASSERT(buf1 == buf2);
317    MORDOR_TEST_ASSERT(!(buf1 != buf2));
318}
319
320MORDOR_UNITTEST(Buffer, compareLotOfSegmentsOnTheRight)
321{
322    Buffer buf1("hello world!"), buf2;
323    buf2.copyIn("he");
324    buf2.copyIn("l");
325    buf2.copyIn("l");
326    buf2.copyIn("o wor");
327    buf2.copyIn("ld!");
328    MORDOR_TEST_ASSERT(buf1 == buf2);
329    MORDOR_TEST_ASSERT(!(buf1 != buf2));
330}
331
332MORDOR_UNITTEST(Buffer, compareLotsOfSegments)
333{
334    Buffer buf1, buf2;
335    buf1.copyIn("he");
336    buf1.copyIn("l");
337    buf1.copyIn("l");
338    buf1.copyIn("o wor");
339    buf1.copyIn("ld!");
340    buf2.copyIn("he");
341    buf2.copyIn("l");
342    buf2.copyIn("l");
343    buf2.copyIn("o wor");
344    buf2.copyIn("ld!");
345    MORDOR_TEST_ASSERT(buf1 == buf2);
346    MORDOR_TEST_ASSERT(!(buf1 != buf2));
347}
348
349MORDOR_UNITTEST(Buffer, compareLotsOfMismatchedSegments)
350{
351    Buffer buf1, buf2;
352    buf1.copyIn("hel");
353    buf1.copyIn("lo ");
354    buf1.copyIn("wo");
355    buf1.copyIn("rld!");
356    buf2.copyIn("he");
357    buf2.copyIn("l");
358    buf2.copyIn("l");
359    buf2.copyIn("o wor");
360    buf2.copyIn("ld!");
361    MORDOR_TEST_ASSERT(buf1 == buf2);
362    MORDOR_TEST_ASSERT(!(buf1 != buf2));
363}
364
365MORDOR_UNITTEST(Buffer, compareLotsOfSegmentsOnTheLeftInequality)
366{
367    Buffer buf1, buf2("hello world!");
368    buf1.copyIn("he");
369    buf1.copyIn("l");
370    buf1.copyIn("l");
371    buf1.copyIn("o wor");
372    buf1.copyIn("ld! ");
373    MORDOR_TEST_ASSERT(buf1 != buf2);
374    MORDOR_TEST_ASSERT(!(buf1 == buf2));
375}
376
377MORDOR_UNITTEST(Buffer, compareLotOfSegmentsOnTheRightInequality)
378{
379    Buffer buf1("hello world!"), buf2;
380    buf2.copyIn("he");
381    buf2.copyIn("l");
382    buf2.copyIn("l");
383    buf2.copyIn("o wor");
384    buf2.copyIn("ld! ");
385    MORDOR_TEST_ASSERT(buf1 != buf2);
386    MORDOR_TEST_ASSERT(!(buf1 == buf2));
387}
388
389MORDOR_UNITTEST(Buffer, compareLotsOfSegmentsInequality)
390{
391    Buffer buf1, buf2;
392    buf1.copyIn("he");
393    buf1.copyIn("l");
394    buf1.copyIn("l");
395    buf1.copyIn("o wor");
396    buf1.copyIn("ld!");
397    buf2.copyIn("he");
398    buf2.copyIn("l");
399    buf2.copyIn("l");
400    buf2.copyIn("o wor");
401    buf2.copyIn("ld! ");
402    MORDOR_TEST_ASSERT(buf1 != buf2);
403    MORDOR_TEST_ASSERT(!(buf1 == buf2));
404}
405
406MORDOR_UNITTEST(Buffer, compareLotsOfMismatchedSegmentsInequality)
407{
408    Buffer buf1, buf2;
409    buf1.copyIn("hel");
410    buf1.copyIn("lo ");
411    buf1.copyIn("wo");
412    buf1.copyIn("rld!");
413    buf2.copyIn("he");
414    buf2.copyIn("l");
415    buf2.copyIn("l");
416    buf2.copyIn("o wor");
417    buf2.copyIn("ld! ");
418    MORDOR_TEST_ASSERT(buf1 != buf2);
419    MORDOR_TEST_ASSERT(!(buf1 == buf2));
420}
421
422MORDOR_UNITTEST(Buffer, reserveWithReadAvailable)
423{
424    Buffer buf1("hello");
425    buf1.reserve(10);
426    MORDOR_TEST_ASSERT_EQUAL(buf1.readAvailable(), 5u);
427    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(buf1.writeAvailable(), 10u);
428}
429
430MORDOR_UNITTEST(Buffer, reserveWithWriteAvailable)
431{
432    Buffer buf1;
433    buf1.reserve(5);
434    // Internal knowledge that reserve doubles the reservation
435    MORDOR_TEST_ASSERT_EQUAL(buf1.writeAvailable(), 10u);
436    buf1.reserve(11);
437    MORDOR_TEST_ASSERT_EQUAL(buf1.writeAvailable(), 22u);
438}
439
440MORDOR_UNITTEST(Buffer, reserveWithReadAndWriteAvailable)
441{
442    Buffer buf1("hello");
443    buf1.reserve(5);
444    // Internal knowledge that reserve doubles the reservation
445    MORDOR_TEST_ASSERT_EQUAL(buf1.readAvailable(), 5u);
446    MORDOR_TEST_ASSERT_EQUAL(buf1.writeAvailable(), 10u);
447    buf1.reserve(11);
448    MORDOR_TEST_ASSERT_EQUAL(buf1.readAvailable(), 5u);
449    MORDOR_TEST_ASSERT_EQUAL(buf1.writeAvailable(), 22u);
450}
451
452static void
453visitor1(const void *b, size_t len)
454{
455    MORDOR_NOTREACHED();
456}
457
458MORDOR_UNITTEST(Buffer, visitEmpty)
459{
460    Buffer b;
461    b.visit(&visitor1);
462}
463
464MORDOR_UNITTEST(Buffer, visitNonEmpty0)
465{
466    Buffer b("hello");
467    b.visit(&visitor1, 0);
468}
469
470static void
471visitor2(const void *b, size_t len, int &sequence)
472{
473    MORDOR_TEST_ASSERT_EQUAL(++sequence, 1);
474    MORDOR_TEST_ASSERT_EQUAL(len, 5u);
475    MORDOR_TEST_ASSERT(memcmp(b, "hello", 5) == 0);
476}
477
478MORDOR_UNITTEST(Buffer, visitSingleSegment)
479{
480    Buffer b("hello");
481    int sequence = 0;
482    b.visit(boost::bind(&visitor2, _1, _2, boost::ref(sequence)));
483    MORDOR_TEST_ASSERT_EQUAL(++sequence, 2);
484}
485
486static void
487visitor3(const void *b, size_t len, int &sequence)
488{
489    switch (len) {
490        case 1:
491            MORDOR_TEST_ASSERT_EQUAL(++sequence, 1);
492            MORDOR_TEST_ASSERT(memcmp(b, "a", 1) == 0);
493            break;
494        case 2:
495            MORDOR_TEST_ASSERT_EQUAL(++sequence, 2);
496            MORDOR_TEST_ASSERT(memcmp(b, "bc", 2) == 0);
497            break;
498        default:
499            MORDOR_NOTREACHED();
500    }
501}
502
503MORDOR_UNITTEST(Buffer, visitMultipleSegments)
504{
505    Buffer b;
506    int sequence = 0;
507    b.copyIn("a");
508    b.copyIn("bc");
509    b.visit(boost::bind(&visitor3, _1, _2, boost::ref(sequence)));
510    MORDOR_TEST_ASSERT_EQUAL(++sequence, 3);
511}
512
513MORDOR_UNITTEST(Buffer, visitMultipleSegmentsPartial)
514{
515    Buffer b;
516    int sequence = 0;
517    b.copyIn("a");
518    b.copyIn("bcd");
519    b.visit(boost::bind(&visitor3, _1, _2, boost::ref(sequence)), 3);
520    MORDOR_TEST_ASSERT_EQUAL(++sequence, 3);
521}
522
523MORDOR_UNITTEST(Buffer, visitWithWriteSegment)
524{
525    Buffer b("hello");
526    b.reserve(5);
527    int sequence = 0;
528    b.visit(boost::bind(&visitor2, _1, _2, boost::ref(sequence)));
529    MORDOR_TEST_ASSERT_EQUAL(++sequence, 2);
530}
531
532MORDOR_UNITTEST(Buffer, visitWithMixedSegment)
533{
534    Buffer b;
535    b.reserve(10);
536    b.copyIn("hello");
537    int sequence = 0;
538    b.visit(boost::bind(&visitor2, _1, _2, boost::ref(sequence)));
539    MORDOR_TEST_ASSERT_EQUAL(++sequence, 2);
540}
541
542#ifndef NDEBUG
543MORDOR_UNITTEST(Buffer, visitMoreThanThereIs)
544{
545    Buffer b;
546    MORDOR_TEST_ASSERT_ASSERTED(b.visit(&visitor1, 1));
547}
548#endif
549
550MORDOR_UNITTEST(Buffer, findCharEmpty)
551{
552    Buffer b;
553    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 0u);
554    MORDOR_TEST_ASSERT_EQUAL(b.find('\n'), -1);
555    MORDOR_TEST_ASSERT_EQUAL(b.find('\n', 0), -1);
556
557#ifndef NDEBUG
558    MORDOR_TEST_ASSERT_ASSERTED(b.find('\n', 1));
559#endif
560
561    // Put a write segment on the end
562    b.reserve(10);
563    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 1u);
564    MORDOR_TEST_ASSERT_EQUAL(b.find('\n'), -1);
565
566#ifndef NDEBUG
567    MORDOR_TEST_ASSERT_ASSERTED(b.find('\n', 1));
568#endif
569}
570
571MORDOR_UNITTEST(Buffer, findCharSimple)
572{
573    Buffer b("\nhello");
574    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 1u);
575
576    MORDOR_TEST_ASSERT_EQUAL(b.find('\r'), -1);
577    MORDOR_TEST_ASSERT_EQUAL(b.find('\n'), 0);
578    MORDOR_TEST_ASSERT_EQUAL(b.find('h'), 1);
579    MORDOR_TEST_ASSERT_EQUAL(b.find('e'), 2);
580    MORDOR_TEST_ASSERT_EQUAL(b.find('l'), 3);
581    MORDOR_TEST_ASSERT_EQUAL(b.find('o'), 5);
582
583    MORDOR_TEST_ASSERT_EQUAL(b.find('\r', 2), -1);
584    MORDOR_TEST_ASSERT_EQUAL(b.find('\n', 2), 0);
585    MORDOR_TEST_ASSERT_EQUAL(b.find('h', 2), 1);
586    MORDOR_TEST_ASSERT_EQUAL(b.find('e', 2), -1);
587    MORDOR_TEST_ASSERT_EQUAL(b.find('l', 2), -1);
588    MORDOR_TEST_ASSERT_EQUAL(b.find('o', 2), -1);
589
590    MORDOR_TEST_ASSERT_EQUAL(b.find('\n', 0), -1);
591}
592
593MORDOR_UNITTEST(Buffer, findCharTwoSegments)
594{
595    Buffer b("\nhe");
596    b.copyIn("llo");
597    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 2u);
598
599    MORDOR_TEST_ASSERT_EQUAL(b.find('\r'), -1);
600    MORDOR_TEST_ASSERT_EQUAL(b.find('\n'), 0);
601    MORDOR_TEST_ASSERT_EQUAL(b.find('h'), 1);
602    MORDOR_TEST_ASSERT_EQUAL(b.find('e'), 2);
603    MORDOR_TEST_ASSERT_EQUAL(b.find('l'), 3);
604    MORDOR_TEST_ASSERT_EQUAL(b.find('o'), 5);
605
606    MORDOR_TEST_ASSERT_EQUAL(b.find('\r', 2), -1);
607    MORDOR_TEST_ASSERT_EQUAL(b.find('\n', 2), 0);
608    MORDOR_TEST_ASSERT_EQUAL(b.find('h', 2), 1);
609    MORDOR_TEST_ASSERT_EQUAL(b.find('e', 2), -1);
610    MORDOR_TEST_ASSERT_EQUAL(b.find('l', 2), -1);
611    MORDOR_TEST_ASSERT_EQUAL(b.find('o', 2), -1);
612
613    MORDOR_TEST_ASSERT_EQUAL(b.find('\r', 4), -1);
614    MORDOR_TEST_ASSERT_EQUAL(b.find('\n', 4), 0);
615    MORDOR_TEST_ASSERT_EQUAL(b.find('h', 4), 1);
616    MORDOR_TEST_ASSERT_EQUAL(b.find('e', 4), 2);
617    MORDOR_TEST_ASSERT_EQUAL(b.find('l', 4), 3);
618    MORDOR_TEST_ASSERT_EQUAL(b.find('o', 4), -1);
619
620    // Put a write segment on the end
621    b.reserve(10);
622    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 3u);
623
624    MORDOR_TEST_ASSERT_EQUAL(b.find('\r'), -1);
625    MORDOR_TEST_ASSERT_EQUAL(b.find('\n'), 0);
626    MORDOR_TEST_ASSERT_EQUAL(b.find('h'), 1);
627    MORDOR_TEST_ASSERT_EQUAL(b.find('e'), 2);
628    MORDOR_TEST_ASSERT_EQUAL(b.find('l'), 3);
629    MORDOR_TEST_ASSERT_EQUAL(b.find('o'), 5);
630
631    MORDOR_TEST_ASSERT_EQUAL(b.find('\r', 2), -1);
632    MORDOR_TEST_ASSERT_EQUAL(b.find('\n', 2), 0);
633    MORDOR_TEST_ASSERT_EQUAL(b.find('h', 2), 1);
634    MORDOR_TEST_ASSERT_EQUAL(b.find('e', 2), -1);
635    MORDOR_TEST_ASSERT_EQUAL(b.find('l', 2), -1);
636    MORDOR_TEST_ASSERT_EQUAL(b.find('o', 2), -1);
637
638    MORDOR_TEST_ASSERT_EQUAL(b.find('\r', 4), -1);
639    MORDOR_TEST_ASSERT_EQUAL(b.find('\n', 4), 0);
640    MORDOR_TEST_ASSERT_EQUAL(b.find('h', 4), 1);
641    MORDOR_TEST_ASSERT_EQUAL(b.find('e', 4), 2);
642    MORDOR_TEST_ASSERT_EQUAL(b.find('l', 4), 3);
643    MORDOR_TEST_ASSERT_EQUAL(b.find('o', 4), -1);
644}
645
646MORDOR_UNITTEST(Buffer, findCharMixedSegment)
647{
648    Buffer b("\nhe");
649    b.reserve(10);
650    b.copyIn("llo");
651    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 2u);
652
653    MORDOR_TEST_ASSERT_EQUAL(b.find('\r'), -1);
654    MORDOR_TEST_ASSERT_EQUAL(b.find('\n'), 0);
655    MORDOR_TEST_ASSERT_EQUAL(b.find('h'), 1);
656    MORDOR_TEST_ASSERT_EQUAL(b.find('e'), 2);
657    MORDOR_TEST_ASSERT_EQUAL(b.find('l'), 3);
658    MORDOR_TEST_ASSERT_EQUAL(b.find('o'), 5);
659
660    MORDOR_TEST_ASSERT_EQUAL(b.find('\r', 2), -1);
661    MORDOR_TEST_ASSERT_EQUAL(b.find('\n', 2), 0);
662    MORDOR_TEST_ASSERT_EQUAL(b.find('h', 2), 1);
663    MORDOR_TEST_ASSERT_EQUAL(b.find('e', 2), -1);
664    MORDOR_TEST_ASSERT_EQUAL(b.find('l', 2), -1);
665    MORDOR_TEST_ASSERT_EQUAL(b.find('o', 2), -1);
666
667    MORDOR_TEST_ASSERT_EQUAL(b.find('\r', 4), -1);
668    MORDOR_TEST_ASSERT_EQUAL(b.find('\n', 4), 0);
669    MORDOR_TEST_ASSERT_EQUAL(b.find('h', 4), 1);
670    MORDOR_TEST_ASSERT_EQUAL(b.find('e', 4), 2);
671    MORDOR_TEST_ASSERT_EQUAL(b.find('l', 4), 3);
672    MORDOR_TEST_ASSERT_EQUAL(b.find('o', 4), -1);
673}
674
675MORDOR_UNITTEST(Buffer, findStringEmpty)
676{
677    Buffer b;
678
679    MORDOR_TEST_ASSERT_EQUAL(b.find("h"), -1);
680    MORDOR_TEST_ASSERT_EQUAL(b.find("h", 0), -1);
681#ifndef NDEBUG
682    MORDOR_TEST_ASSERT_ASSERTED(b.find(""));
683    MORDOR_TEST_ASSERT_ASSERTED(b.find("h", 1));
684#endif
685
686    // Put a write segment on the end
687    b.reserve(10);
688    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 1u);
689    MORDOR_TEST_ASSERT_EQUAL(b.find("h"), -1);
690    MORDOR_TEST_ASSERT_EQUAL(b.find("h", 0), -1);
691
692#ifndef NDEBUG
693    MORDOR_TEST_ASSERT_ASSERTED(b.find(""));
694    MORDOR_TEST_ASSERT_ASSERTED(b.find("h", 1));
695#endif
696}
697
698MORDOR_UNITTEST(Buffer, findStringSimple)
699{
700    Buffer b("helloworld");
701    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 1u);
702
703    MORDOR_TEST_ASSERT_EQUAL(b.find("abc"), -1);
704    MORDOR_TEST_ASSERT_EQUAL(b.find("helloworld"), 0);
705    MORDOR_TEST_ASSERT_EQUAL(b.find("helloworld2"), -1);
706    MORDOR_TEST_ASSERT_EQUAL(b.find("elloworld"), 1);
707    MORDOR_TEST_ASSERT_EQUAL(b.find("helloworl"), 0);
708    MORDOR_TEST_ASSERT_EQUAL(b.find("h"), 0);
709    MORDOR_TEST_ASSERT_EQUAL(b.find("l"), 2);
710    MORDOR_TEST_ASSERT_EQUAL(b.find("o"), 4);
711    MORDOR_TEST_ASSERT_EQUAL(b.find("lo"), 3);
712    MORDOR_TEST_ASSERT_EQUAL(b.find("d"), 9);
713
714    MORDOR_TEST_ASSERT_EQUAL(b.find("abc", 5), -1);
715    MORDOR_TEST_ASSERT_EQUAL(b.find("helloworld", 5), -1);
716    MORDOR_TEST_ASSERT_EQUAL(b.find("hello", 5), 0);
717    MORDOR_TEST_ASSERT_EQUAL(b.find("ello", 5), 1);
718    MORDOR_TEST_ASSERT_EQUAL(b.find("helloworld2", 5), -1);
719    MORDOR_TEST_ASSERT_EQUAL(b.find("elloworld", 5), -1);
720    MORDOR_TEST_ASSERT_EQUAL(b.find("hell", 5), 0);
721    MORDOR_TEST_ASSERT_EQUAL(b.find("h", 5), 0);
722    MORDOR_TEST_ASSERT_EQUAL(b.find("l", 5), 2);
723    MORDOR_TEST_ASSERT_EQUAL(b.find("o", 5), 4);
724    MORDOR_TEST_ASSERT_EQUAL(b.find("lo", 5), 3);
725    MORDOR_TEST_ASSERT_EQUAL(b.find("ow", 5), -1);
726
727    MORDOR_TEST_ASSERT_EQUAL(b.find("h", 0), -1);
728}
729
730MORDOR_UNITTEST(Buffer, findStringTwoSegments)
731{
732    Buffer b("hello");
733    b.copyIn("world");
734    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 2u);
735
736    MORDOR_TEST_ASSERT_EQUAL(b.find("abc"), -1);
737    MORDOR_TEST_ASSERT_EQUAL(b.find("helloworld"), 0);
738    MORDOR_TEST_ASSERT_EQUAL(b.find("helloworld2"), -1);
739    MORDOR_TEST_ASSERT_EQUAL(b.find("elloworld"), 1);
740    MORDOR_TEST_ASSERT_EQUAL(b.find("helloworl"), 0);
741    MORDOR_TEST_ASSERT_EQUAL(b.find("h"), 0);
742    MORDOR_TEST_ASSERT_EQUAL(b.find("l"), 2);
743    MORDOR_TEST_ASSERT_EQUAL(b.find("o"), 4);
744    MORDOR_TEST_ASSERT_EQUAL(b.find("lo"), 3);
745    MORDOR_TEST_ASSERT_EQUAL(b.find("d"), 9);
746
747    MORDOR_TEST_ASSERT_EQUAL(b.find("abc", 7), -1);
748    MORDOR_TEST_ASSERT_EQUAL(b.find("helloworld", 7), -1);
749    MORDOR_TEST_ASSERT_EQUAL(b.find("hellowo", 7), 0);
750    MORDOR_TEST_ASSERT_EQUAL(b.find("ellowo", 7), 1);
751    MORDOR_TEST_ASSERT_EQUAL(b.find("helloworld2", 7), -1);
752    MORDOR_TEST_ASSERT_EQUAL(b.find("elloworld", 7), -1);
753    MORDOR_TEST_ASSERT_EQUAL(b.find("hellow", 7), 0);
754    MORDOR_TEST_ASSERT_EQUAL(b.find("h", 7), 0);
755    MORDOR_TEST_ASSERT_EQUAL(b.find("l", 7), 2);
756    MORDOR_TEST_ASSERT_EQUAL(b.find("o", 7), 4);
757    MORDOR_TEST_ASSERT_EQUAL(b.find("lo", 7), 3);
758    MORDOR_TEST_ASSERT_EQUAL(b.find("or", 7), -1);
759
760    MORDOR_TEST_ASSERT_EQUAL(b.find("h", 0), -1);
761}
762
763MORDOR_UNITTEST(Buffer, findStringAcrossMultipleSegments)
764{
765    Buffer b("hello");
766    b.copyIn("world");
767    b.copyIn("foo");
768    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 3u);
769
770    MORDOR_TEST_ASSERT_EQUAL(b.find("lloworldfo"), 2);
771}
772
773MORDOR_UNITTEST(Buffer, findStringLongFalsePositive)
774{
775    Buffer b("100000011");
776
777    MORDOR_TEST_ASSERT_EQUAL(b.find("000011"), 3);
778}
779
780MORDOR_UNITTEST(Buffer, findStringFalsePositiveAcrossMultipleSegments)
781{
782    Buffer b("10");
783    b.copyIn("00");
784    b.copyIn("00");
785    b.copyIn("00");
786    b.copyIn("11");
787    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 5u);
788
789    MORDOR_TEST_ASSERT_EQUAL(b.find("000011"), 4);
790}
791
792MORDOR_UNITTEST(Buffer, toString)
793{
794    Buffer b;
795    MORDOR_TEST_ASSERT(b.toString().empty());
796    b.copyIn("hello");
797    MORDOR_TEST_ASSERT_EQUAL(b.toString(), "hello");
798    b.copyIn("world");
799    MORDOR_TEST_ASSERT_EQUAL(b.toString(), "helloworld");
800    b.consume(3);
801    MORDOR_TEST_ASSERT_EQUAL(b.toString(), "loworld");
802}
803
804MORDOR_UNITTEST(Buffer, reserve0)
805{
806    Buffer b;
807    b.reserve(0);
808    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 0u);
809}
810
811MORDOR_UNITTEST(Buffer, writeBuffer0)
812{
813    Buffer b;
814    iovec iov = b.writeBuffer(0, true);
815    MORDOR_TEST_ASSERT_EQUAL(iov.iov_len, 0u);
816    MORDOR_TEST_ASSERT_EQUAL(b.segments(), 0u);
817}
818
819MORDOR_UNITTEST(Buffer, clearReadPortionOnly)
820{
821    Buffer b;
822    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 0u);
823    MORDOR_TEST_ASSERT_EQUAL(b.writeAvailable(), 0u);
824    b.clear(false);
825    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 0u);
826    MORDOR_TEST_ASSERT_EQUAL(b.writeAvailable(), 0u);
827    b.copyIn("hello");
828    b.clear(false);
829    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 0u);
830    MORDOR_TEST_ASSERT_EQUAL(b.writeAvailable(), 0u);
831    b.copyIn("hello");
832    b.reserve(10);
833    b.clear(false);
834    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 0u);
835    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(b.writeAvailable(), 10u);
836    b.copyIn("world");
837    b.clear(false);
838    MORDOR_TEST_ASSERT_EQUAL(b.readAvailable(), 0u);
839    MORDOR_TEST_ASSERT_GREATER_THAN_OR_EQUAL(b.writeAvailable(), 5u);
840}