wibble  1.1
netbuffer.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/netbuffer.h>
5 
6 #include <wibble/test.h>
7 #include <cstring>
8 
9 using namespace std;
10 using namespace wibble::sys;
11 
12 struct TestNetBuffer {
13 
14  // Ensure that a plain NetBuffer starts from the beginning of the string
16  NetBuffer buf("antani", 6);
17 
18  assert_eq(buf.size(), 6u);
19  assert(memcmp(buf.data(), "antani", 6) == 0);
20 
21  assert_eq(*buf.cast<char>(), 'a');
22  assert(buf.fits<short int>());
23  assert(!buf.fits<long long int>());
24  }
25 
26  // Try skipping some bytes
28  NetBuffer buf("antani", 6);
29 
30  NetBuffer buf1(buf);
31 
32  assert_eq(buf1.size(), 6u);
33  assert(memcmp(buf1.data(), "antani", 6) == 0);
34 
35  buf1 += 2;
36  assert_eq(buf1.size(), 4u);
37  assert(memcmp(buf1.data(), "tani", 4) == 0);
38  assert_eq(*buf1.cast<char>(), 't');
39 
40  buf1 = buf + 2;
41  assert_eq(buf1.size(), 4u);
42  assert(memcmp(buf1.data(), "tani", 4) == 0);
43  assert_eq(*buf1.cast<char>(), 't');
44 
45  buf1 = buf;
46  buf1.skip(2);
47  assert_eq(buf1.size(), 4u);
48  assert(memcmp(buf1.data(), "tani", 4) == 0);
49  assert_eq(*buf1.cast<char>(), 't');
50 
51  buf1 = buf.after(2);
52  assert_eq(buf1.size(), 4u);
53  assert(memcmp(buf1.data(), "tani", 4) == 0);
54  assert_eq(*buf1.cast<char>(), 't');
55 
56  buf1 = buf;
57  buf1.skip<short int>();
58  assert_eq(buf1.size(), 4u);
59  assert(memcmp(buf1.data(), "tani", 4) == 0);
60  assert_eq(*buf1.cast<char>(), 't');
61 
62  buf1 = buf.after<short int>();
63  assert_eq(buf1.size(), 4u);
64  assert(memcmp(buf1.data(), "tani", 4) == 0);
65  assert_eq(*buf1.cast<char>(), 't');
66  }
67 
68 };
69 
70 // vim:set ts=4 sw=4:
Buffer whose starting can be moved back and forth, useful to decapsulate stacked network packets.
Definition: netbuffer.h:36
void skip()
Move the starting point of this buffer sizeof(T) bytes from the beginning.
Definition: netbuffer.h:144
size_t size() const
Return the buffer size.
Definition: netbuffer.h:77
const T * cast(size_t ofs=0) const
Access the buffer contents as a structure T at the given offset.
Definition: netbuffer.h:93
const void * data(size_t ofs=0) const
Return a pointer to the buffer.
Definition: netbuffer.h:71
const NetBuffer after(size_t ofs) const
Return another NetBuffer starting ofs bytes from the beginning of this one.
Definition: netbuffer.h:111
bool fits(size_t ofs=0) const
Check if the buffer is long enough to contain a structure T at the given offset.
Definition: netbuffer.h:84
Definition: buffer.cpp:28
Definition: netbuffer.test.h:12
Test startAtBeginning()
Definition: netbuffer.test.h:15
Test skipBytes()
Definition: netbuffer.test.h:27
void Test
Definition: test.h:178
#define assert_eq(x, y)
Definition: test.h:33
#define assert(x)
Definition: test.h:30