]>
git.proxmox.com Git - mirror_frr.git/blob - tests/test-nexthop-iter.c
2 * Recursive Nexthop Iterator test.
3 * This tests the ALL_NEXTHOPS_RO macro.
5 * Copyright (C) 2012 by Open Source Routing.
6 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
8 * This file is part of Quagga
10 * Quagga is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
15 * Quagga is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Quagga; see the file COPYING. If not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "zebra/rib.h"
30 struct thread_master
*master
;
34 str_append(char **buf
, const char *repr
)
38 *buf
= realloc(*buf
, strlen(*buf
) + strlen(repr
) + 1);
40 strncpy((*buf
) + strlen(*buf
), repr
, strlen(repr
) + 1);
50 str_appendf(char **buf
, const char *format
, ...)
57 rv
= vasprintf(&pbuf
, format
, ap
);
61 str_append(buf
, pbuf
);
65 /* This structure contains a nexthop chain
66 * and its expected representation */
69 /* Head of the chain */
71 /* Last nexthop in top chain */
72 struct nexthop
*current_top
;
73 /* Last nexthop in current recursive chain */
74 struct nexthop
*current_recursive
;
75 /* Expected string representation. */
79 static struct nexthop_chain
*
80 nexthop_chain_new(void)
82 struct nexthop_chain
*rv
;
84 rv
= calloc(sizeof(*rv
), 1);
90 nexthop_chain_add_top(struct nexthop_chain
*nc
)
94 nh
= calloc(sizeof(*nh
), 1);
99 nc
->current_top
->next
= nh
;
100 nh
->prev
= nc
->current_top
;
101 nc
->current_top
= nh
;
105 nc
->head
= nc
->current_top
= nh
;
107 nc
->current_recursive
= NULL
;
108 str_appendf(&nc
->repr
, "%p\n", nh
);
112 nexthop_chain_add_recursive(struct nexthop_chain
*nc
)
116 nh
= calloc(sizeof(*nh
), 1);
119 assert(nc
->current_top
);
120 if (nc
->current_recursive
)
122 nc
->current_recursive
->next
= nh
;
123 nh
->prev
= nc
->current_recursive
;
124 nc
->current_recursive
= nh
;
128 SET_FLAG(nc
->current_top
->flags
, NEXTHOP_FLAG_RECURSIVE
);
129 nc
->current_top
->resolved
= nh
;
130 nc
->current_recursive
= nh
;
132 str_appendf(&nc
->repr
, " %p\n", nh
);
136 nexthop_chain_clear(struct nexthop_chain
*nc
)
138 struct nexthop
*tcur
, *tnext
;
140 for (tcur
= nc
->head
; tcur
; tcur
= tnext
)
143 if (CHECK_FLAG(tcur
->flags
, NEXTHOP_FLAG_RECURSIVE
))
145 struct nexthop
*rcur
, *rnext
;
146 for (rcur
= tcur
->resolved
; rcur
; rcur
= rnext
)
154 nc
->head
= nc
->current_top
= nc
->current_recursive
= NULL
;
160 nexthop_chain_free(struct nexthop_chain
*nc
)
164 nexthop_chain_clear(nc
);
168 /* This function builds a string representation of
169 * the nexthop chain using the ALL_NEXTHOPS_RO macro.
170 * It verifies that the ALL_NEXTHOPS_RO macro iterated
171 * correctly over the nexthop chain by comparing the
172 * generated representation with the expected representation.
175 nexthop_chain_verify_iter(struct nexthop_chain
*nc
)
177 struct nexthop
*nh
, *tnh
;
181 for (ALL_NEXTHOPS_RO(nc
->head
, nh
, tnh
, recursing
))
184 str_appendf(&repr
, " %p\n", nh
);
186 str_appendf(&repr
, "%p\n", nh
);
190 printf("===\n%s", repr
);
191 assert((!repr
&& !nc
->repr
) || (repr
&& nc
->repr
&& !strcmp(repr
, nc
->repr
)));
195 /* This test run builds a simple nexthop chain
196 * with some recursive nexthops and verifies that
197 * the iterator works correctly in each stage along
203 struct nexthop_chain
*nc
;
205 nc
= nexthop_chain_new();
206 nexthop_chain_verify_iter(nc
);
208 nexthop_chain_add_top(nc
);
209 nexthop_chain_verify_iter(nc
);
211 nexthop_chain_add_top(nc
);
212 nexthop_chain_verify_iter(nc
);
214 nexthop_chain_add_recursive(nc
);
215 nexthop_chain_verify_iter(nc
);
217 nexthop_chain_add_recursive(nc
);
218 nexthop_chain_verify_iter(nc
);
220 nexthop_chain_add_top(nc
);
221 nexthop_chain_verify_iter(nc
);
223 nexthop_chain_add_top(nc
);
224 nexthop_chain_verify_iter(nc
);
226 nexthop_chain_add_top(nc
);
227 nexthop_chain_verify_iter(nc
);
229 nexthop_chain_add_recursive(nc
);
230 nexthop_chain_verify_iter(nc
);
232 nexthop_chain_add_recursive(nc
);
233 nexthop_chain_verify_iter(nc
);
235 nexthop_chain_add_recursive(nc
);
236 nexthop_chain_verify_iter(nc
);
238 nexthop_chain_free(nc
);
241 /* This test run builds numerous random
242 * nexthop chain configurations and verifies
243 * that the iterator correctly progresses
248 struct nexthop_chain
*nc
;
252 nc
= nexthop_chain_new();
255 for (i
= 0; i
< 1000000; i
++)
257 switch (prng_rand(prng
) % 10)
260 nexthop_chain_clear(nc
);
267 nexthop_chain_add_top(nc
);
274 nexthop_chain_add_recursive(nc
);
277 nexthop_chain_verify_iter(nc
);
279 nexthop_chain_free(nc
);
283 int main(int argc
, char **argv
)
285 if (argc
>= 2 && !strcmp("-v", argv
[1]))
288 printf("Simple test passed.\n");
290 printf("PRNG test passed.\n");