wibble
0.1.28
|
00001 /* -*- C++ -*- (c) 2007 Petr Rockai <me@mornfall.net> 00002 (c) 2007 Enrico Zini <enrico@enricozini.org> */ 00003 00004 #include <wibble/sys/buffer.h> 00005 00006 #include <wibble/test.h> 00007 #include <string.h> 00008 00009 using namespace std; 00010 using namespace wibble::sys; 00011 00012 struct TestBuffer { 00013 Test emptiness() { 00014 Buffer buf; 00015 assert_eq(buf.size(), 0u); 00016 assert_eq(buf.data(), (void*)0); 00017 00018 // Empty buffers should be equal 00019 Buffer buf1; 00020 assert(buf == buf); 00021 assert(buf == buf1); 00022 assert(!(buf < buf1)); 00023 assert(!(buf1 < buf)); 00024 } 00025 00026 Test nonemptiness() { 00027 // Nonempty buffers should be properly nonempty 00028 Buffer buf(1); 00029 ((char*)buf.data())[0] = 'a'; 00030 assert_eq(buf.size(), 1u); 00031 assert(buf.data() != 0); 00032 00033 // Nonempty buffers should compare by content 00034 Buffer buf1(1); 00035 ((char*)buf1.data())[0] = 'z'; 00036 assert(buf == buf); 00037 assert(buf1 == buf1); 00038 assert(!(buf == buf1)); 00039 assert(buf != buf1); 00040 assert(buf < buf1); 00041 assert(!(buf1 < buf)); 00042 00043 ((char*)buf1.data())[0] = 'a'; 00044 assert(buf == buf1); 00045 assert(!(buf != buf1)); 00046 assert(!(buf < buf1)); 00047 assert(!(buf1 < buf)); 00048 00049 // Empty buffers should come before the nonempty ones 00050 Buffer buf2; 00051 assert(!(buf == buf2)); 00052 assert(buf != buf2); 00053 assert(!(buf < buf2)); 00054 assert(buf2 < buf); 00055 } 00056 00057 // Construct by copy should work 00058 Test copy() { 00059 const char* str = "Ciao"; 00060 Buffer buf(str, 4); 00061 00062 assert_eq(buf.size(), 4u); 00063 assert(memcmp(str, buf.data(), 4) == 0); 00064 } 00065 00066 // Resize should work and preserve the contents 00067 Test resize() { 00068 const char* str = "Ciao"; 00069 Buffer buf(str, 4); 00070 00071 assert_eq(buf.size(), 4u); 00072 assert(memcmp(str, buf.data(), 4) == 0); 00073 00074 buf.resize(8); 00075 assert_eq(buf.size(), 8u); 00076 assert(memcmp(str, buf.data(), 4) == 0); 00077 } 00078 00079 // Check creation by taking ownership of another buffer 00080 Test takeover() { 00081 char* str = new char[4]; 00082 memcpy(str, "ciao", 4); 00083 Buffer buf(str, 4, true); 00084 00085 assert_eq(buf.size(), 4u); 00086 assert_eq((void*)str, buf.data()); 00087 } 00088 }; 00089 00090 // vim:set ts=4 sw=4: