]>
Commit | Line | Data |
---|---|---|
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 | ||
17 | typedef BUFFER(int) buffer_int; | |
18 | static int expected[MAX_NUM]; | |
19 | static buffer_int buf; | |
20 | static struct pool *buffer_pool; | |
21 | ||
22 | static void | |
23 | show_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 | ||
32 | static void | |
33 | fill_expected_array(void) | |
34 | { | |
35 | int i; | |
36 | ||
37 | for (i = 0; i < MAX_NUM; i++) | |
38 | expected[i] = bt_random(); | |
39 | } | |
40 | ||
41 | static void | |
42 | init_buffer(void) | |
43 | { | |
44 | resource_init(); | |
45 | buffer_pool = &root_pool; | |
46 | BUFFER_INIT(buf, buffer_pool, MAX_NUM); | |
47 | } | |
48 | ||
49 | static int | |
50 | is_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 | ||
60 | static int | |
61 | t_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 | ||
75 | static int | |
76 | t_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 | ||
103 | static int | |
104 | t_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 | ||
120 | static int | |
121 | t_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 |
136 | static int |
137 | t_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 |
155 | int |
156 | main(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 | } |