Statistics
| Branch: | Revision:

iof-bird / bird-2.0.1 / nest / a-set_test.c @ 6b3f1a54

History | View | Annotate | Download (7.26 KB)

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
18
static struct adata *set_sequence;                /* <0; SET_SIZE) */
19
static struct adata *set_sequence_same;                /* <0; SET_SIZE) */
20
static struct adata *set_sequence_higher;        /* <SET_SIZE; 2*SET_SIZE) */
21
static struct adata *set_random;
22

    
23
#define BUFFER_SIZE 1000
24
static byte buf[BUFFER_SIZE] = {};
25

    
26
#define SET_SIZE_FOR_FORMAT_OUTPUT 10
27

    
28
struct linpool *lp;
29

    
30
enum set_type
31
{
32
  SET_TYPE_INT,
33
  SET_TYPE_EC
34
};
35

    
36
static void
37
generate_set_sequence(enum set_type type)
38
{
39
  struct adata empty_as_path = {};
40
  set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path;
41
  lp = lp_new_default(&root_pool);
42

    
43
  int i;
44
  for (i = 0; i < SET_SIZE; i++)
45
  {
46
    if (type == SET_TYPE_INT)
47
    {
48
      set_sequence           = int_set_add(lp, set_sequence, i);
49
      set_sequence_same   = int_set_add(lp, set_sequence_same, i);
50
      set_sequence_higher = int_set_add(lp, set_sequence_higher, i + SET_SIZE);
51
      set_random             = int_set_add(lp, set_random, bt_random());
52
    }
53
    else if (type == SET_TYPE_EC)
54
    {
55
      set_sequence           = ec_set_add(lp, set_sequence, i);
56
      set_sequence_same   = ec_set_add(lp, set_sequence_same, i);
57
      set_sequence_higher = ec_set_add(lp, set_sequence_higher, i + SET_SIZE);
58
      set_random             = ec_set_add(lp, set_random, (bt_random() << 32 | bt_random()));
59
    }
60
    else
61
      bt_abort_msg("This should be unreachable");
62
  }
63
}
64

    
65
/*
66
 * SET INT TESTS
67
 */
68

    
69
static int
70
t_set_int_contains(void)
71
{
72
  int i;
73

    
74
  resource_init();
75
  generate_set_sequence(SET_TYPE_INT);
76

    
77
  bt_assert(int_set_get_size(set_sequence) == SET_SIZE);
78

    
79
  for (i = 0; i < SET_SIZE; i++)
80
    bt_assert(int_set_contains(set_sequence, i));
81
  bt_assert(int_set_contains(set_sequence, -1) == 0);
82
  bt_assert(int_set_contains(set_sequence, SET_SIZE) == 0);
83

    
84
  int *data = int_set_get_data(set_sequence);
85
  for (i = 0; i < SET_SIZE; i++)
86
    bt_assert_msg(data[i] == i, "(data[i] = %d) == i = %d)", data[i], i);
87

    
88
  rfree(lp);
89
  return 1;
90
}
91

    
92
static int
93
t_set_int_union(void)
94
{
95
  resource_init();
96
  generate_set_sequence(SET_TYPE_INT);
97

    
98
  struct adata *set_union;
99
  set_union = int_set_union(lp, set_sequence, set_sequence_same);
100
  bt_assert(int_set_get_size(set_union) == SET_SIZE);
101
  bt_assert(int_set_format(set_union, 0, 2, buf, BUFFER_SIZE) == 0);
102

    
103
  set_union = int_set_union(lp, set_sequence, set_sequence_higher);
104
  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);
105
  bt_assert(int_set_format(set_union, 0, 2, buf, BUFFER_SIZE) == 0);
106

    
107
  rfree(lp);
108
  return 1;
109
}
110

    
111
static int
112
t_set_int_format(void)
113
{
114
  resource_init();
115
  generate_set_sequence(SET_TYPE_INT);
116

    
117
  set_sequence->length = 4 * SET_SIZE_FOR_FORMAT_OUTPUT; /* dirty */
118
  bt_assert(int_set_format(set_sequence, 0, 0, buf, BUFFER_SIZE) == 0);
119
  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);
120

    
121
  bzero(buf, BUFFER_SIZE);
122
  bt_assert(int_set_format(set_sequence, 0, 2, buf, BUFFER_SIZE) == 0);
123
  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);
124

    
125
  bzero(buf, BUFFER_SIZE);
126
  bt_assert(int_set_format(set_sequence, 1, 0, buf, BUFFER_SIZE) == 0);
127
  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);
128

    
129
  rfree(lp);
130
  return 1;
131
}
132

    
133
static int
134
t_set_int_delete(void)
135
{
136
  resource_init();
137
  generate_set_sequence(SET_TYPE_INT);
138

    
139
  struct adata *deleting_sequence = set_sequence;
140
  u32 i;
141
  for (i = 0; i < SET_SIZE; i++)
142
  {
143
    deleting_sequence = int_set_del(lp, deleting_sequence, i);
144
    bt_assert_msg(int_set_get_size(deleting_sequence) == (int) (SET_SIZE-1-i),
145
                  "int_set_get_size(deleting_sequence) %d == SET_SIZE-1-i %d",
146
                  int_set_get_size(deleting_sequence),
147
                  SET_SIZE-1-i);
148
  }
149

    
150
  bt_assert(int_set_get_size(set_sequence) == SET_SIZE);
151

    
152
  return 1;
153
}
154

    
155
/*
156
 * SET EC TESTS
157
 */
158

    
159
static int
160
t_set_ec_contains(void)
161
{
162
  u32 i;
163

    
164
  resource_init();
165
  generate_set_sequence(SET_TYPE_EC);
166

    
167
  bt_assert(ec_set_get_size(set_sequence) == SET_SIZE);
168

    
169
  for (i = 0; i < SET_SIZE; i++)
170
    bt_assert(ec_set_contains(set_sequence, i));
171
  bt_assert(ec_set_contains(set_sequence, -1) == 0);
172
  bt_assert(ec_set_contains(set_sequence, SET_SIZE) == 0);
173

    
174
//  int *data = ec_set_get_data(set_sequence);
175
//  for (i = 0; i < SET_SIZE; i++)
176
//    bt_assert_msg(data[i] == (SET_SIZE-1-i), "(data[i] = %d) == ((SET_SIZE-1-i) = %d)", data[i], SET_SIZE-1-i);
177

    
178
  rfree(lp);
179
  return 1;
180
}
181

    
182
static int
183
t_set_ec_union(void)
184
{
185
  resource_init();
186
  generate_set_sequence(SET_TYPE_EC);
187

    
188
  struct adata *set_union;
189
  set_union = ec_set_union(lp, set_sequence, set_sequence_same);
190
  bt_assert(ec_set_get_size(set_union) == SET_SIZE);
191
  bt_assert(ec_set_format(set_union, 0, buf, BUFFER_SIZE) == 0);
192

    
193
  set_union = ec_set_union(lp, set_sequence, set_sequence_higher);
194
  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);
195
  bt_assert(ec_set_format(set_union, 0, buf, BUFFER_SIZE) == 0);
196

    
197
  rfree(lp);
198
  return 1;
199
}
200

    
201
static int
202
t_set_ec_format(void)
203
{
204
  resource_init();
205

    
206
  struct adata empty_as_path = {};
207
  set_sequence = set_sequence_same = set_sequence_higher = set_random = &empty_as_path;
208
  lp = lp_new_default(&root_pool);
209

    
210
  u64 i = 0;
211
  set_sequence = ec_set_add(lp, set_sequence, i);
212
  for (i = 1; i < SET_SIZE_FOR_FORMAT_OUTPUT; i++)
213
    set_sequence = ec_set_add(lp, set_sequence, i + ((i%2) ? ((u64)EC_RO << 48) : ((u64)EC_RT << 48)));
214

    
215
  bt_assert(ec_set_format(set_sequence, 0, buf, BUFFER_SIZE) == 0);
216
  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,
217
                "ec_set_format() returns '%s'", buf);
218

    
219
  rfree(lp);
220
  return 1;
221
}
222

    
223
static int
224
t_set_ec_delete(void)
225
{
226
  resource_init();
227
  generate_set_sequence(SET_TYPE_EC);
228

    
229
  struct adata *deleting_sequence = set_sequence;
230
  u32 i;
231
  for (i = 0; i < SET_SIZE; i++)
232
  {
233
    deleting_sequence = ec_set_del(lp, deleting_sequence, i);
234
    bt_assert_msg(ec_set_get_size(deleting_sequence) == (int) (SET_SIZE-1-i),
235
                  "ec_set_get_size(deleting_sequence) %d  == SET_SIZE-1-i %d",
236
                  ec_set_get_size(deleting_sequence), SET_SIZE-1-i);
237
  }
238

    
239
  bt_assert(ec_set_get_size(set_sequence) == SET_SIZE);
240

    
241
  return 1;
242
}
243

    
244
int
245
main(int argc, char *argv[])
246
{
247
  bt_init(argc, argv);
248

    
249
  bt_test_suite(t_set_int_contains, "Testing sets of integers: contains, get_data");
250
  bt_test_suite(t_set_int_format,   "Testing sets of integers: format");
251
  bt_test_suite(t_set_int_union,    "Testing sets of integers: union");
252
  bt_test_suite(t_set_int_delete,   "Testing sets of integers: delete");
253

    
254
  bt_test_suite(t_set_ec_contains, "Testing sets of Extended Community values: contains, get_data");
255
  bt_test_suite(t_set_ec_format,   "Testing sets of Extended Community values: format");
256
  bt_test_suite(t_set_ec_union,    "Testing sets of Extended Community values: union");
257
  bt_test_suite(t_set_ec_delete,   "Testing sets of Extended Community values: delete");
258

    
259
  return bt_exit_value();
260
}