-
Notifications
You must be signed in to change notification settings - Fork 13
/
tests.cpp
162 lines (128 loc) · 3.91 KB
/
tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <bev/linear_ringbuffer.hpp>
#include <bev/io_buffer.hpp>
#include <iostream>
#include <assert.h>
void print_mappings()
{
pid_t pid = getpid();
char cmd[128];
sprintf(cmd, "cat /proc/%d/maps", pid);
system(cmd);
}
int test_linear_ringbuffer() {
bev::linear_ringbuffer rb(4096);
int n = rb.capacity();
// Test 1: Check that we can read and write the full capacity
// of the buffer.
std::cout << "Test 1..." << std::flush;
char* sbuf = static_cast<char*>(malloc(rb.capacity()));
char* tbuf = static_cast<char*>(malloc(rb.capacity()));
std::fill_n(sbuf, n, 'x');
std::fill_n(tbuf, n, '\0');
assert(rb.free_size() == rb.capacity());
::memcpy(rb.write_head(), sbuf, n);
rb.commit(n);
::memcpy(tbuf, rb.read_head(), rb.size());
rb.consume(n);
assert(rb.size() == 0);
std::fill_n(tbuf, n, 'x');
std::cout << "success\n";
// Test 2: Check than we can read and write "over the edge", i.e.
// starting in one copy of the buffer and ending in the other.
std::cout << "Test 2..." << std::flush;
rb.clear();
std::fill_n(sbuf, n, 'y');
rb.commit(n/2);
rb.consume(n/2);
// Arbitrarily use some amount n/2 < m < n to ensure we write
// over the edge.
int m = n/2 + n/4;
::memcpy(rb.write_head(), sbuf, m);
rb.commit(m);
assert(rb.size() == m);
::memcpy(tbuf, rb.read_head(), m);
rb.consume(m);
assert(rb.size() == 0);
for (int i=0; i<m; ++i) {
assert(tbuf[i] == 'y');
}
std::cout << "success\n";
// Test 3: Check that for-loop iteration works
const char* test3 = "Test 3...success\n";
rb.clear();
::strcpy(reinterpret_cast<char*>(rb.write_head()), test3);
rb.commit(strlen(test3));
for (char c : rb) {
std::cout << c;
}
}
int test_io_buffer()
{
bev::io_buffer iob(4096);
int n = iob.capacity();
// Test 1: Check that we can read and write the full capacity
// of the buffer.
std::cout << "Test 1..." << std::flush;
char* sbuf = static_cast<char*>(malloc(iob.capacity()));
char* tbuf = static_cast<char*>(malloc(iob.capacity()));
std::fill_n(sbuf, n, 'x');
std::fill_n(tbuf, n, '\0');
assert(iob.free_size() == iob.capacity());
::memcpy(iob.write_head(), sbuf, n);
iob.commit(n);
::memcpy(tbuf, iob.read_head(), iob.size());
iob.consume(n);
assert(iob.size() == 0);
std::fill_n(tbuf, n, 'x');
std::cout << "success\n";
// Test 2: Check than we can read and write "over the edge", i.e.
// starting in one copy of the buffer and ending in the other.
std::cout << "Test 2..." << std::flush;
iob.clear();
std::fill_n(sbuf, n, 'y');
iob.commit(n/2);
iob.consume(n/2);
// Arbitrarily use some amount n/2 < m < n to ensure we write
// over the edge.
int m = n/2 + n/4;
auto slab = iob.prepare(m);
assert(slab.size == m);
::memcpy(slab.data, sbuf, slab.size);
iob.commit(slab.size);
assert(iob.size() == slab.size);
::memcpy(tbuf, iob.read_head(), m);
iob.consume(m);
assert(iob.size() == 0);
for (int i=0; i<m; ++i) {
assert(tbuf[i] == 'y');
}
std::cout << "success\n";
// Test 3: Use a custom memory region.
std::cout << "Test 3..." << std::flush;
static int deletes;
deletes = 0;
struct Deleter {
Deleter() = default;
// The standard says that "A deleter’s state need never be copied, only moved or
// swapped as ownership changes." I'm not sure if that means that the state is not
// allowed to be copied, or that it is not required to copy the state for a conforming
// implementation. Let's assume the former to be safe.
Deleter(const Deleter&) { throw std::runtime_error("dont copy me :("); }
void operator()(char* c) { ++deletes; delete[] c; }
} static_deleter;
std::unique_ptr<char, Deleter&> p(new char[128], static_deleter);
{
bev::io_buffer buf(std::move(p), 128);
assert(buf.free_size() == 128);
assert(buf.capacity() == 128);
}
assert(deletes == 1);
std::cout << "success\n";
}
int main()
{
std::cout << "Testing linear_ringbuffer...\n";
test_linear_ringbuffer();
std::cout << "Testing io_ringbuffer...\n";
test_io_buffer();
}