]> git.ipfire.org Git - thirdparty/bird.git/blame - nest/a-set_test.c
Replaced custom linpools in tests for the common tmp_linpool
[thirdparty/bird.git] / nest / a-set_test.c
CommitLineData
9b0a0ba9
OZ
1/*
2 * BIRD -- Set/Community-list Operations 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 "test/birdtest.h"
10#include "test/bt-utils.h"
11
12#include "lib/net.h"
13#include "nest/route.h"
14#include "nest/attrs.h"
15#include "lib/resource.h"
16
17#define SET_SIZE 10
4c553c5a
MM
18static const struct adata *set_sequence; /* <0; SET_SIZE) */
19static const struct adata *set_sequence_same; /* <0; SET_SIZE) */
20static const struct adata *set_sequence_higher; /* <SET_SIZE; 2*SET_SIZE) */
21static const struct adata *set_random;
9b0a0ba9
OZ
22
23#define BUFFER_SIZE 1000
24static byte buf[BUFFER_SIZE] = {};
25
26#define SET_SIZE_FOR_FORMAT_OUTPUT 10
27
9b0a0ba9
OZ
28enum set_type
29{
30 SET_TYPE_INT,
31 SET_TYPE_EC
32};
33
34static void
4c553c5a 35generate_set_sequence(enum set_type type, int len)
9b0a0ba9
OZ
36{
37 struct adata empty_as_path = {};
38 set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path;
9b0a0ba9
OZ
39
40 int i;
4c553c5a 41 for (i = 0; i < len; i++)
9b0a0ba9
OZ
42 {
43 if (type == SET_TYPE_INT)
44 {
d814a8cb
MM
45 set_sequence = int_set_add(tmp_linpool, set_sequence, i);
46 set_sequence_same = int_set_add(tmp_linpool, set_sequence_same, i);
47 set_sequence_higher = int_set_add(tmp_linpool, set_sequence_higher, i + SET_SIZE);
48 set_random = int_set_add(tmp_linpool, set_random, bt_random());
9b0a0ba9
OZ
49 }
50 else if (type == SET_TYPE_EC)
51 {
d814a8cb
MM
52 set_sequence = ec_set_add(tmp_linpool, set_sequence, i);
53 set_sequence_same = ec_set_add(tmp_linpool, set_sequence_same, i);
54 set_sequence_higher = ec_set_add(tmp_linpool, set_sequence_higher, i + SET_SIZE);
55 set_random = ec_set_add(tmp_linpool, set_random, (bt_random() << 32 | bt_random()));
9b0a0ba9
OZ
56 }
57 else
58 bt_abort_msg("This should be unreachable");
59 }
60}
61
62/*
63 * SET INT TESTS
64 */
65
66static int
67t_set_int_contains(void)
68{
69 int i;
70
71 resource_init();
4c553c5a 72 generate_set_sequence(SET_TYPE_INT, SET_SIZE);
9b0a0ba9
OZ
73
74 bt_assert(int_set_get_size(set_sequence) == SET_SIZE);
75
76 for (i = 0; i < SET_SIZE; i++)
77 bt_assert(int_set_contains(set_sequence, i));
78 bt_assert(int_set_contains(set_sequence, -1) == 0);
79 bt_assert(int_set_contains(set_sequence, SET_SIZE) == 0);
80
81 int *data = int_set_get_data(set_sequence);
82 for (i = 0; i < SET_SIZE; i++)
83 bt_assert_msg(data[i] == i, "(data[i] = %d) == i = %d)", data[i], i);
84
5e3cd0e5 85 return 1;
9b0a0ba9
OZ
86}
87
88static int
89t_set_int_union(void)
90{
91 resource_init();
4c553c5a 92 generate_set_sequence(SET_TYPE_INT, SET_SIZE);
9b0a0ba9 93
4c553c5a 94 const struct adata *set_union;
d814a8cb 95 set_union = int_set_union(tmp_linpool, set_sequence, set_sequence_same);
9b0a0ba9
OZ
96 bt_assert(int_set_get_size(set_union) == SET_SIZE);
97 bt_assert(int_set_format(set_union, 0, 2, buf, BUFFER_SIZE) == 0);
98
d814a8cb 99 set_union = int_set_union(tmp_linpool, set_sequence, set_sequence_higher);
9b0a0ba9
OZ
100 bt_assert_msg(int_set_get_size(set_union) == SET_SIZE*2, "int_set_get_size(set_union) %d, SET_SIZE*2 %d", int_set_get_size(set_union), SET_SIZE*2);
101 bt_assert(int_set_format(set_union, 0, 2, buf, BUFFER_SIZE) == 0);
102
5e3cd0e5 103 return 1;
9b0a0ba9
OZ
104}
105
106static int
107t_set_int_format(void)
108{
109 resource_init();
4c553c5a 110 generate_set_sequence(SET_TYPE_INT, SET_SIZE_FOR_FORMAT_OUTPUT);
9b0a0ba9 111
9b0a0ba9
OZ
112 bt_assert(int_set_format(set_sequence, 0, 0, buf, BUFFER_SIZE) == 0);
113 bt_assert(strcmp(buf, "0.0.0.0 0.0.0.1 0.0.0.2 0.0.0.3 0.0.0.4 0.0.0.5 0.0.0.6 0.0.0.7 0.0.0.8 0.0.0.9") == 0);
114
115 bzero(buf, BUFFER_SIZE);
116 bt_assert(int_set_format(set_sequence, 0, 2, buf, BUFFER_SIZE) == 0);
117 bt_assert(strcmp(buf, "0.0.0.2 0.0.0.3 0.0.0.4 0.0.0.5 0.0.0.6 0.0.0.7 0.0.0.8 0.0.0.9") == 0);
118
119 bzero(buf, BUFFER_SIZE);
120 bt_assert(int_set_format(set_sequence, 1, 0, buf, BUFFER_SIZE) == 0);
121 bt_assert(strcmp(buf, "(0,0) (0,1) (0,2) (0,3) (0,4) (0,5) (0,6) (0,7) (0,8) (0,9)") == 0);
122
5e3cd0e5 123 return 1;
9b0a0ba9
OZ
124}
125
126static int
127t_set_int_delete(void)
128{
129 resource_init();
4c553c5a 130 generate_set_sequence(SET_TYPE_INT, SET_SIZE);
9b0a0ba9 131
4c553c5a 132 const struct adata *deleting_sequence = set_sequence;
9b0a0ba9
OZ
133 u32 i;
134 for (i = 0; i < SET_SIZE; i++)
135 {
d814a8cb 136 deleting_sequence = int_set_del(tmp_linpool, deleting_sequence, i);
9b0a0ba9
OZ
137 bt_assert_msg(int_set_get_size(deleting_sequence) == (int) (SET_SIZE-1-i),
138 "int_set_get_size(deleting_sequence) %d == SET_SIZE-1-i %d",
139 int_set_get_size(deleting_sequence),
140 SET_SIZE-1-i);
141 }
142
143 bt_assert(int_set_get_size(set_sequence) == SET_SIZE);
144
5e3cd0e5 145 return 1;
9b0a0ba9
OZ
146}
147
148/*
149 * SET EC TESTS
150 */
151
152static int
153t_set_ec_contains(void)
154{
155 u32 i;
156
157 resource_init();
4c553c5a 158 generate_set_sequence(SET_TYPE_EC, SET_SIZE);
9b0a0ba9
OZ
159
160 bt_assert(ec_set_get_size(set_sequence) == SET_SIZE);
161
162 for (i = 0; i < SET_SIZE; i++)
163 bt_assert(ec_set_contains(set_sequence, i));
164 bt_assert(ec_set_contains(set_sequence, -1) == 0);
165 bt_assert(ec_set_contains(set_sequence, SET_SIZE) == 0);
166
167// int *data = ec_set_get_data(set_sequence);
168// for (i = 0; i < SET_SIZE; i++)
169// bt_assert_msg(data[i] == (SET_SIZE-1-i), "(data[i] = %d) == ((SET_SIZE-1-i) = %d)", data[i], SET_SIZE-1-i);
170
5e3cd0e5 171 return 1;
9b0a0ba9
OZ
172}
173
174static int
175t_set_ec_union(void)
176{
177 resource_init();
4c553c5a 178 generate_set_sequence(SET_TYPE_EC, SET_SIZE);
9b0a0ba9 179
4c553c5a 180 const struct adata *set_union;
d814a8cb 181 set_union = ec_set_union(tmp_linpool, set_sequence, set_sequence_same);
9b0a0ba9
OZ
182 bt_assert(ec_set_get_size(set_union) == SET_SIZE);
183 bt_assert(ec_set_format(set_union, 0, buf, BUFFER_SIZE) == 0);
184
d814a8cb 185 set_union = ec_set_union(tmp_linpool, set_sequence, set_sequence_higher);
9b0a0ba9
OZ
186 bt_assert_msg(ec_set_get_size(set_union) == SET_SIZE*2, "ec_set_get_size(set_union) %d, SET_SIZE*2 %d", ec_set_get_size(set_union), SET_SIZE*2);
187 bt_assert(ec_set_format(set_union, 0, buf, BUFFER_SIZE) == 0);
188
5e3cd0e5 189 return 1;
9b0a0ba9
OZ
190}
191
192static int
193t_set_ec_format(void)
194{
195 resource_init();
196
4c553c5a 197 const struct adata empty_as_path = {};
9b0a0ba9 198 set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path;
9b0a0ba9
OZ
199
200 u64 i = 0;
d814a8cb 201 set_sequence = ec_set_add(tmp_linpool, set_sequence, i);
9b0a0ba9 202 for (i = 1; i < SET_SIZE_FOR_FORMAT_OUTPUT; i++)
d814a8cb 203 set_sequence = ec_set_add(tmp_linpool, set_sequence, i + ((i%2) ? ((u64)EC_RO << 48) : ((u64)EC_RT << 48)));
9b0a0ba9
OZ
204
205 bt_assert(ec_set_format(set_sequence, 0, buf, BUFFER_SIZE) == 0);
206 bt_assert_msg(strcmp(buf, "(unknown 0x0, 0, 0) (ro, 0, 1) (rt, 0, 2) (ro, 0, 3) (rt, 0, 4) (ro, 0, 5) (rt, 0, 6) (ro, 0, 7) (rt, 0, 8) (ro, 0, 9)") == 0,
207 "ec_set_format() returns '%s'", buf);
208
5e3cd0e5 209 return 1;
9b0a0ba9
OZ
210}
211
212static int
213t_set_ec_delete(void)
214{
215 resource_init();
4c553c5a 216 generate_set_sequence(SET_TYPE_EC, SET_SIZE);
9b0a0ba9 217
4c553c5a 218 const struct adata *deleting_sequence = set_sequence;
9b0a0ba9
OZ
219 u32 i;
220 for (i = 0; i < SET_SIZE; i++)
221 {
d814a8cb 222 deleting_sequence = ec_set_del(tmp_linpool, deleting_sequence, i);
9b0a0ba9
OZ
223 bt_assert_msg(ec_set_get_size(deleting_sequence) == (int) (SET_SIZE-1-i),
224 "ec_set_get_size(deleting_sequence) %d == SET_SIZE-1-i %d",
225 ec_set_get_size(deleting_sequence), SET_SIZE-1-i);
226 }
227
228 bt_assert(ec_set_get_size(set_sequence) == SET_SIZE);
229
5e3cd0e5 230 return 1;
9b0a0ba9
OZ
231}
232
233int
234main(int argc, char *argv[])
235{
236 bt_init(argc, argv);
237
238 bt_test_suite(t_set_int_contains, "Testing sets of integers: contains, get_data");
239 bt_test_suite(t_set_int_format, "Testing sets of integers: format");
240 bt_test_suite(t_set_int_union, "Testing sets of integers: union");
241 bt_test_suite(t_set_int_delete, "Testing sets of integers: delete");
242
243 bt_test_suite(t_set_ec_contains, "Testing sets of Extended Community values: contains, get_data");
244 bt_test_suite(t_set_ec_format, "Testing sets of Extended Community values: format");
245 bt_test_suite(t_set_ec_union, "Testing sets of Extended Community values: union");
246 bt_test_suite(t_set_ec_delete, "Testing sets of Extended Community values: delete");
247
248 return bt_exit_value();
249}