wibble
1.1
Main Page
Namespaces
Classes
Files
File List
File Members
wibble
sys
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
{
13
Test
emptiness
() {
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
26
Test
nonemptiness
() {
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
67
Test
resize
() {
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
80
Test
takeover
() {
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:
Generated on Wed Oct 23 2013 17:14:25 for wibble by
1.8.4