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
163
164
165
166
|
/*
* BIRD Library -- Buffer Tests
*
* (c) 2015 CZ.NIC z.s.p.o.
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#include <stdlib.h>
#include "test/birdtest.h"
#include "lib/buffer.h"
#define MAX_NUM 33
typedef BUFFER(int) buffer_int;
static int expected[MAX_NUM];
static buffer_int buf;
static struct pool *buffer_pool;
static void
show_buf(buffer_int *b)
{
uint i;
bt_debug(".used = %d, .size = %d\n", b->used, b->size);
for (i = 0; i < b->used; i++)
bt_debug(" .data[%3u] = %-16d expected %-16d %s\n", i, b->data[i], expected[i], (b->data[i] == expected[i] ? "OK" : "FAIL!"));
}
static void
fill_expected_array(void)
{
int i;
for (i = 0; i < MAX_NUM; i++)
expected[i] = bt_random();
}
static void
init_buffer(void)
{
buffer_pool = &root_pool;
BUFFER_INIT(buf, buffer_pool, MAX_NUM);
}
static int
is_buffer_as_expected(buffer_int *b)
{
show_buf(b);
int i;
for (i = 0; i < MAX_NUM; i++)
bt_assert(b->data[i] == expected[i]);
return 1;
}
static int
t_buffer_push(void)
{
int i;
init_buffer();
fill_expected_array();
for (i = 0; i < MAX_NUM; i++)
BUFFER_PUSH(buf) = expected[i];
is_buffer_as_expected(&buf);
return 1;
}
static int
t_buffer_pop(void)
{
int i;
init_buffer();
fill_expected_array();
/* POP a half of elements */
for (i = 0; i < MAX_NUM; i++)
BUFFER_PUSH(buf) = expected[i];
for (i = MAX_NUM-1; i >= MAX_NUM/2; i--)
BUFFER_POP(buf);
for (i = MAX_NUM/2; i < MAX_NUM; i++)
BUFFER_PUSH(buf) = expected[i] = bt_random();
is_buffer_as_expected(&buf);
/* POP all of elements */
for (i = MAX_NUM-1; i >= 0; i--)
BUFFER_POP(buf);
bt_assert(buf.used == 0);
for (i = 0; i < MAX_NUM; i++)
BUFFER_PUSH(buf) = expected[i];
is_buffer_as_expected(&buf);
return 1;
}
static int
t_buffer_resize(void)
{
int i;
init_buffer();
BUFFER_INIT(buf, buffer_pool, 0);
fill_expected_array();
for (i = 0; i < MAX_NUM; i++)
BUFFER_PUSH(buf) = expected[i];
is_buffer_as_expected(&buf);
bt_assert(buf.size >= MAX_NUM);
return 1;
}
static int
t_buffer_flush(void)
{
int i;
init_buffer();
fill_expected_array();
for (i = 0; i < MAX_NUM; i++)
BUFFER_PUSH(buf) = expected[i];
BUFFER_FLUSH(buf);
bt_assert(buf.used == 0);
return 1;
}
static int
t_buffer_walk(void)
{
int i;
init_buffer();
fill_expected_array();
for (i = 0; i < MAX_NUM; i++)
BUFFER_PUSH(buf) = expected[i];
i = 0;
BUFFER_WALK(buf, v)
bt_assert(v == expected[i++]);
bt_assert(i == MAX_NUM);
return 1;
}
int
main(int argc, char *argv[])
{
bt_init(argc, argv);
bt_test_suite(t_buffer_push, "Pushing new elements");
bt_test_suite(t_buffer_pop, "Fill whole buffer (PUSH), a half of elements POP and PUSH new elements");
bt_test_suite(t_buffer_resize, "Init a small buffer and try overfill");
bt_test_suite(t_buffer_flush, "Fill and flush all elements");
bt_test_suite(t_buffer_walk, "Fill and walk through buffer");
return bt_exit_value();
}
|