netbuffer.test.h
Go to the documentation of this file.
/* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net>
(c) 2007 Enrico Zini <enrico@enricozini.org> */
#include <wibble/sys/netbuffer.h>
#include <wibble/test.h>
#include <cstring>
using namespace std;
using namespace wibble::sys;
struct TestNetBuffer {
// Ensure that a plain NetBuffer starts from the beginning of the string
Test startAtBeginning() {
NetBuffer buf("antani", 6);
assert_eq(buf.size(), 6u);
assert(memcmp(buf.data(), "antani", 6) == 0);
assert_eq(*buf.cast<char>(), 'a');
assert(buf.fits<short int>());
assert(!buf.fits<long long int>());
}
// Try skipping some bytes
Test skipBytes() {
NetBuffer buf("antani", 6);
NetBuffer buf1(buf);
assert_eq(buf1.size(), 6u);
assert(memcmp(buf1.data(), "antani", 6) == 0);
buf1 += 2;
assert_eq(buf1.size(), 4u);
assert(memcmp(buf1.data(), "tani", 4) == 0);
assert_eq(*buf1.cast<char>(), 't');
buf1 = buf + 2;
assert_eq(buf1.size(), 4u);
assert(memcmp(buf1.data(), "tani", 4) == 0);
assert_eq(*buf1.cast<char>(), 't');
buf1 = buf;
buf1.skip(2);
assert_eq(buf1.size(), 4u);
assert(memcmp(buf1.data(), "tani", 4) == 0);
assert_eq(*buf1.cast<char>(), 't');
buf1 = buf.after(2);
assert_eq(buf1.size(), 4u);
assert(memcmp(buf1.data(), "tani", 4) == 0);
assert_eq(*buf1.cast<char>(), 't');
buf1 = buf;
buf1.skip<short int>();
assert_eq(buf1.size(), 4u);
assert(memcmp(buf1.data(), "tani", 4) == 0);
assert_eq(*buf1.cast<char>(), 't');
buf1 = buf.after<short int>();
assert_eq(buf1.size(), 4u);
assert(memcmp(buf1.data(), "tani", 4) == 0);
assert_eq(*buf1.cast<char>(), 't');
}
};
// vim:set ts=4 sw=4:
wibble::sys::NetBuffer::after
const NetBuffer after(size_t ofs) const
Return another NetBuffer starting ofs bytes from the beginning of this one.
Definition: netbuffer.h:111
TestNetBuffer::skipBytes
Test skipBytes()
Definition: netbuffer.test.h:27
wibble::sys::NetBuffer::cast
const T * cast(size_t ofs=0) const
Access the buffer contents as a structure T at the given offset.
Definition: netbuffer.h:93
Test
void Test
Definition: test.h:178
wibble::sys::NetBuffer::skip
void skip()
Move the starting point of this buffer sizeof(T) bytes from the beginning.
Definition: netbuffer.h:144
TestNetBuffer
Definition: netbuffer.test.h:12
test.h
std
wibble::sys
Definition: buffer.cpp:28
wibble::sys::NetBuffer::fits
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
assert_eq
#define assert_eq(x, y)
Definition: test.h:33
assert
#define assert(x)
Definition: test.h:30
wibble::sys::NetBuffer::size
size_t size() const
Return the buffer size.
Definition: netbuffer.h:77
wibble::sys::NetBuffer::data
const void * data(size_t ofs=0) const
Return a pointer to the buffer.
Definition: netbuffer.h:71
TestNetBuffer::startAtBeginning
Test startAtBeginning()
Definition: netbuffer.test.h:15
netbuffer.h
wibble::sys::NetBuffer
Buffer whose starting can be moved back and forth, useful to decapsulate stacked network packets...
Definition: netbuffer.h:35