Something went wrong. Try again.
forked from cz-nic/bird
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261/* * BIRD Library -- Bitmap Tests * * (c) 2019 Ondrej Zajicek <santiago@crfreenet.org> * (c) 2019 CZ.NIC z.s.p.o. * * Can be freely distributed and used under the terms of the GNU GPL. */
#include "test/birdtest.h"#include "sysdep/config.h"#include "lib/bitmap.h"
#define MAX_NUM (1 << 20)#define MAX_SET (1 << 19)#define MAX_CLR (1 << 17)
#define STEP_NUM 1000#define STEP_SET 1000#define STEP_CLR 500
static intt_bmap_set_clear_random(void){ struct bmap b;
bmap_init(&b, &root_pool, 1024);
char expected[MAX_NUM] = {}; uint i, n;
for (i = 0; i < MAX_SET; i++) { do n = bt_random() % MAX_NUM; while (expected[n]);
bmap_set(&b, n); expected[n] = 1; }
for (i = 0; i < MAX_CLR; i++) { do n = bt_random() % MAX_NUM; while (!expected[n]);
bmap_clear(&b, n); expected[n] = 0; }
for (i = 0; i < MAX_NUM; i++) if (bmap_test(&b, i) != expected[i]) bt_abort_msg("Bitmap mismatch on %d (should be %d %d)", i, bmap_test(&b, i), expected[i]);
return 1;}
static intt_hmap_set_clear_random(void){ struct hmap b;
hmap_init(&b, &root_pool, 1024);
char expected[MAX_NUM] = {}; uint i, n;
for (i = 0; i < MAX_SET; i++) { do n = bt_random() % MAX_NUM; while (expected[n]);
hmap_set(&b, n); expected[n] = 1; }
hmap_check(&b);
for (i = 0; i < MAX_CLR; i++) { do n = bt_random() % MAX_NUM; while (!expected[n]);
hmap_clear(&b, n); expected[n] = 0; }
hmap_check(&b);
for (i = 0; i < MAX_NUM; i++) if (hmap_test(&b, i) != expected[i]) bt_abort_msg("Bitmap mismatch on %d (should be %d %d)", i, hmap_test(&b, i), expected[i]);
for (i = 0; 1; i++) { n = hmap_first_zero(&b); bt_assert(n >= i); bt_assert(n <= MAX_NUM);
for (; i < n; i++) bt_assert(expected[i]);
if (n == MAX_NUM) break;
bt_assert(!expected[i]);
hmap_set(&b, n); }
hmap_check(&b);
return 1;}
static intt_hmap_set_clear_fill(void){ struct hmap b;
hmap_init(&b, &root_pool, 1024);
char expected[MAX_NUM] = {}; uint i, j, n, max = 0;
for (i = 0; i < STEP_NUM; i++) { uint last = 0; uint step_set = bt_random() % STEP_SET; uint step_clr = bt_random() % STEP_CLR;
for (j = 0; j < step_set; j++) { n = hmap_first_zero(&b); bt_assert(n > last || !last); bt_assert(n < MAX_NUM);
if (!last) last = n;
for (; last < n; last++) bt_assert(expected[last]);
bt_assert(!expected[n]);
hmap_set(&b, n); expected[n] = 1; max = MAX(max, n); }
for (j = 0; j < step_clr; j++) { uint k = 0; do n = bt_random() % max; while (!expected[n] && (k++ < 8));
if (!expected[n]) continue;
hmap_clear(&b, n); expected[n] = 0; } }
for (i = 0; i < MAX_NUM; i++) if (hmap_test(&b, i) != expected[i]) bt_abort_msg("Bitmap mismatch on %d (should be %d %d)", i, hmap_test(&b, i), expected[i]);
hmap_check(&b);
return 1;}
static intt_lmap_set_clear_fill(void){ struct lmap b;
lmap_init(&b, &root_pool);
char expected[MAX_NUM] = {}; uint i, j, n;
for (i = 0; i < STEP_NUM; i++) { uint last = 0; uint lo = bt_random() % (1 << 19); uint hi = lo + 2 * STEP_SET; uint step_set = bt_random() % STEP_SET; uint step_clr = bt_random() % STEP_CLR;
for (j = 0; j < step_set; j++) { n = lmap_first_zero_in_range(&b, lo, hi); bt_assert(n >= lo); bt_assert(n <= hi);
for (last = lo; last < n; last++) bt_assert(expected[last]);
if (n >= hi) break;
bt_assert(!expected[n]);
lmap_set(&b, n); expected[n] = 1; }
for (j = 0; j < step_clr; j++) { n = lo + bt_random() % (step_set + 1);
if (!expected[n]) continue;
lmap_clear(&b, n); expected[n] = 0; }
{ n = lmap_last_one_in_range(&b, lo, hi); bt_assert(n >= lo); bt_assert(n <= hi);
for (last = n + 1; last < hi; last++) bt_assert(!expected[last]);
if (n < hi) bt_assert(expected[n]); } }
uint cnt = 0; for (i = 0; i < MAX_NUM; i++) { if (lmap_test(&b, i) != expected[i]) bt_abort_msg("Bitmap mismatch on %d (should be %d %d)", i, lmap_test(&b, i), expected[i]);
if (expected[i]) cnt++; } // bt_log("Total %u", cnt);
lmap_check(&b);
return 1;}
intmain(int argc, char *argv[]){ bt_init(argc, argv);
bt_test_suite(t_bmap_set_clear_random, "BMap - random sequence of sets / clears"); bt_test_suite(t_hmap_set_clear_random, "HMap - random sequence of sets / clears"); bt_test_suite(t_hmap_set_clear_fill, "HMap - linear sets and random clears"); bt_test_suite(t_lmap_set_clear_fill, "LMap - linear sets and random clears");
return bt_exit_value();}