]> git.ipfire.org Git - thirdparty/bird.git/blame - lib/buffer_test.c
Filter: Fix function comparison
[thirdparty/bird.git] / lib / buffer_test.c
CommitLineData
9b0a0ba9
OZ
1/*
2 * BIRD Library -- Buffer Tests
3 *
4 * (c) 2015 CZ.NIC z.s.p.o.
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9#include <stdlib.h>
10
11#include "test/birdtest.h"
12
13#include "lib/buffer.h"
14
15#define MAX_NUM 33
16
17typedef BUFFER(int) buffer_int;
18static int expected[MAX_NUM];
19static buffer_int buf;
20static struct pool *buffer_pool;
21
22static void
23show_buf(buffer_int *b)
24{
25 uint i;
26 bt_debug(".used = %d, .size = %d\n", b->used, b->size);
27
28 for (i = 0; i < b->used; i++)
29 bt_debug(" .data[%3u] = %-16d expected %-16d %s\n", i, b->data[i], expected[i], (b->data[i] == expected[i] ? "OK" : "FAIL!"));
30}
31
32static void
33fill_expected_array(void)
34{
35 int i;
36
37 for (i = 0; i < MAX_NUM; i++)
38 expected[i] = bt_random();
39}
40
41static void
42init_buffer(void)
43{
44 resource_init();
45 buffer_pool = &root_pool;
46 BUFFER_INIT(buf, buffer_pool, MAX_NUM);
47}
48
49static int
50is_buffer_as_expected(buffer_int *b)
51{
52 show_buf(b);
53
54 int i;
55 for (i = 0; i < MAX_NUM; i++)
56 bt_assert(b->data[i] == expected[i]);
57 return 1;
58}
59
60static int
61t_buffer_push(void)
62{
63 int i;
64
65 init_buffer();
66 fill_expected_array();
67
68 for (i = 0; i < MAX_NUM; i++)
69 BUFFER_PUSH(buf) = expected[i];
70 is_buffer_as_expected(&buf);
71
5e3cd0e5 72 return 1;
9b0a0ba9
OZ
73}
74
75static int
76t_buffer_pop(void)
77{
78 int i;
79
80 init_buffer();
81 fill_expected_array();
82
83 /* POP a half of elements */
84 for (i = 0; i < MAX_NUM; i++)
85 BUFFER_PUSH(buf) = expected[i];
86 for (i = MAX_NUM-1; i >= MAX_NUM/2; i--)
87 BUFFER_POP(buf);
88 for (i = MAX_NUM/2; i < MAX_NUM; i++)
89 BUFFER_PUSH(buf) = expected[i] = bt_random();
90 is_buffer_as_expected(&buf);
91
92 /* POP all of elements */
93 for (i = MAX_NUM-1; i >= 0; i--)
94 BUFFER_POP(buf);
95 bt_assert(buf.used == 0);
96 for (i = 0; i < MAX_NUM; i++)
97 BUFFER_PUSH(buf) = expected[i];
98 is_buffer_as_expected(&buf);
99
5e3cd0e5 100 return 1;
9b0a0ba9
OZ
101}
102
103static int
104t_buffer_resize(void)
105{
106 int i;
107
108 init_buffer();
109 BUFFER_INIT(buf, buffer_pool, 0);
110 fill_expected_array();
111
112 for (i = 0; i < MAX_NUM; i++)
113 BUFFER_PUSH(buf) = expected[i];
114 is_buffer_as_expected(&buf);
115 bt_assert(buf.size >= MAX_NUM);
116
5e3cd0e5 117 return 1;
9b0a0ba9
OZ
118}
119
120static int
121t_buffer_flush(void)
122{
123 int i;
124
125 init_buffer();
126 fill_expected_array();
127 for (i = 0; i < MAX_NUM; i++)
128 BUFFER_PUSH(buf) = expected[i];
129
130 BUFFER_FLUSH(buf);
131 bt_assert(buf.used == 0);
132
5e3cd0e5 133 return 1;
9b0a0ba9
OZ
134}
135
7126cadf
OZ
136static int
137t_buffer_walk(void)
138{
139 int i;
140
141 init_buffer();
142 fill_expected_array();
143 for (i = 0; i < MAX_NUM; i++)
144 BUFFER_PUSH(buf) = expected[i];
145
146 i = 0;
147 BUFFER_WALK(buf, v)
148 bt_assert(v == expected[i++]);
149
150 bt_assert(i == MAX_NUM);
151
152 return 1;
153}
154
9b0a0ba9
OZ
155int
156main(int argc, char *argv[])
157{
158 bt_init(argc, argv);
159
160 bt_test_suite(t_buffer_push, "Pushing new elements");
161 bt_test_suite(t_buffer_pop, "Fill whole buffer (PUSH), a half of elements POP and PUSH new elements");
162 bt_test_suite(t_buffer_resize, "Init a small buffer and try overfill");
163 bt_test_suite(t_buffer_flush, "Fill and flush all elements");
7126cadf 164 bt_test_suite(t_buffer_walk, "Fill and walk through buffer");
9b0a0ba9
OZ
165
166 return bt_exit_value();
167}