wibble  1.1
buffer.test.h
Go to the documentation of this file.
1 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
2  (c) 2007 Enrico Zini <enrico@enricozini.org> */
3 
4 #include <wibble/sys/buffer.h>
5 
6 #include <wibble/test.h>
7 #include <string.h>
8 
9 using namespace std;
10 using namespace wibble::sys;
11 
12 struct TestBuffer {
14  Buffer buf;
15  assert_eq(buf.size(), 0u);
16  assert_eq(buf.data(), static_cast<void*>(0));
17 
18  // Empty buffers should be equal
19  Buffer buf1;
20  assert(buf == buf);
21  assert(buf == buf1);
22  assert(!(buf < buf1));
23  assert(!(buf1 < buf));
24  }
25 
27  // Nonempty buffers should be properly nonempty
28  Buffer buf(1);
29  (static_cast<char*>(buf.data()))[0] = 'a';
30  assert_eq(buf.size(), 1u);
31  assert(buf.data() != 0);
32 
33  // Nonempty buffers should compare by content
34  Buffer buf1(1);
35  (static_cast<char*>(buf1.data()))[0] = 'z';
36  assert(buf == buf);
37  assert(buf1 == buf1);
38  assert(!(buf == buf1));
39  assert(buf != buf1);
40  assert(buf < buf1);
41  assert(!(buf1 < buf));
42 
43  (static_cast<char*>(buf1.data()))[0] = 'a';
44  assert(buf == buf1);
45  assert(!(buf != buf1));
46  assert(!(buf < buf1));
47  assert(!(buf1 < buf));
48 
49  // Empty buffers should come before the nonempty ones
50  Buffer buf2;
51  assert(!(buf == buf2));
52  assert(buf != buf2);
53  assert(!(buf < buf2));
54  assert(buf2 < buf);
55  }
56 
57 // Construct by copy should work
58  Test copy() {
59  const char* str = "Ciao";
60  Buffer buf(str, 4);
61 
62  assert_eq(buf.size(), 4u);
63  assert(memcmp(str, buf.data(), 4) == 0);
64  }
65 
66 // Resize should work and preserve the contents
68  const char* str = "Ciao";
69  Buffer buf(str, 4);
70 
71  assert_eq(buf.size(), 4u);
72  assert(memcmp(str, buf.data(), 4) == 0);
73 
74  buf.resize(8);
75  assert_eq(buf.size(), 8u);
76  assert(memcmp(str, buf.data(), 4) == 0);
77  }
78 
79 // Check creation by taking ownership of another buffer
81  char* str = (char*)malloc(4);
82  memcpy(str, "ciao", 4);
83  Buffer buf(str, 4, true);
84 
85  assert_eq(buf.size(), 4u);
86  assert_eq(static_cast<void*>(str), buf.data());
87  }
88 };
89 
90 // vim:set ts=4 sw=4:
Variable-size, reference-counted memory buffer.
Definition: buffer.h:33
Test nonemptiness()
Definition: buffer.test.h:26
void Test
Definition: test.h:178
Definition: buffer.cpp:28
#define assert_eq(x, y)
Definition: test.h:33
#define assert(x)
Definition: test.h:30
Test copy()
Definition: buffer.test.h:58
Test emptiness()
Definition: buffer.test.h:13
size_t size() const
Return the buffer size.
Definition: buffer.h:149
Test takeover()
Definition: buffer.test.h:80
void * data()
Return a pointer to the buffer.
Definition: buffer.h:143
Definition: buffer.test.h:12
Test resize()
Definition: buffer.test.h:67
void resize(size_t newSize)
Resize the buffer to hold exactly the specified amount of bytes.
Definition: buffer.h:152