buffer.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/buffer.h>
#include <wibble/test.h>
#include <string.h>
using namespace std;
using namespace wibble::sys;
struct TestBuffer {
Test emptiness() {
Buffer buf;
assert_eq(buf.size(), 0u);
assert_eq(buf.data(), static_cast<void*>(0));
// Empty buffers should be equal
Buffer buf1;
assert(buf == buf);
assert(buf == buf1);
assert(!(buf < buf1));
assert(!(buf1 < buf));
}
Test nonemptiness() {
// Nonempty buffers should be properly nonempty
Buffer buf(1);
(static_cast<char*>(buf.data()))[0] = 'a';
assert_eq(buf.size(), 1u);
assert(buf.data() != 0);
// Nonempty buffers should compare by content
Buffer buf1(1);
(static_cast<char*>(buf1.data()))[0] = 'z';
assert(buf == buf);
assert(buf1 == buf1);
assert(!(buf == buf1));
assert(buf != buf1);
assert(buf < buf1);
assert(!(buf1 < buf));
(static_cast<char*>(buf1.data()))[0] = 'a';
assert(buf == buf1);
assert(!(buf != buf1));
assert(!(buf < buf1));
assert(!(buf1 < buf));
// Empty buffers should come before the nonempty ones
Buffer buf2;
assert(!(buf == buf2));
assert(buf != buf2);
assert(!(buf < buf2));
assert(buf2 < buf);
}
// Construct by copy should work
Test copy() {
const char* str = "Ciao";
Buffer buf(str, 4);
assert_eq(buf.size(), 4u);
assert(memcmp(str, buf.data(), 4) == 0);
}
// Resize should work and preserve the contents
Test resize() {
const char* str = "Ciao";
Buffer buf(str, 4);
assert_eq(buf.size(), 4u);
assert(memcmp(str, buf.data(), 4) == 0);
buf.resize(8);
assert_eq(buf.size(), 8u);
assert(memcmp(str, buf.data(), 4) == 0);
}
// Check creation by taking ownership of another buffer
Test takeover() {
char* str = (char*)malloc(4);
memcpy(str, "ciao", 4);
Buffer buf(str, 4, true);
assert_eq(buf.size(), 4u);
assert_eq(static_cast<void*>(str), buf.data());
}
};
// vim:set ts=4 sw=4:
buffer.h
wibble::sys::Buffer
Variable-size, reference-counted memory buffer.
Definition: buffer.h:34
wibble::sys::Buffer::resize
void resize(size_t newSize)
Resize the buffer to hold exactly the specified amount of bytes.
Definition: buffer.h:152
wibble::sys::Buffer::size
size_t size() const
Return the buffer size.
Definition: buffer.h:149
wibble::sys::Buffer::data
void * data()
Return a pointer to the buffer.
Definition: buffer.h:143
wibble::sys
Definition: buffer.cpp:28
string.h
TestBuffer
Definition: buffer.test.h:12
TestBuffer::copy
Test copy()
Definition: buffer.test.h:58
TestBuffer::nonemptiness
Test nonemptiness()
Definition: buffer.test.h:26
TestBuffer::resize
Test resize()
Definition: buffer.test.h:67
TestBuffer::takeover
Test takeover()
Definition: buffer.test.h:80
TestBuffer::emptiness
Test emptiness()
Definition: buffer.test.h:13
test.h
Test
void Test
Definition: test.h:178
assert_eq
#define assert_eq(x, y)
Definition: test.h:33
assert
#define assert(x)
Definition: test.h:30