Commit 4fb50646 authored by Pavel Tvrdík's avatar Pavel Tvrdík
Browse files

Birdtest: filter/trie

Thanks to Santiago for reuse here his code.
bt_rand_num() -> bt_random()
parent 45e65e82
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -104,7 +104,7 @@ get_random_degenerated_left_tree(uint nodes_count)
  {
    uint selected_idx;
    do {
      selected_idx = bt_rand_num() % nodes_count;
      selected_idx = bt_random() % nodes_count;
    } while(avaible_indexes[selected_idx] != 0);

    avaible_indexes[selected_idx] = 1;
@@ -127,7 +127,7 @@ get_balanced_tree_with_ranged_values(uint nodes_count)
  {
    n->from.type = n->to.type = T_INT;
    n->from.val.i = idx;
    idx += (uint)bt_rand_num() / nodes_count;	/* (... / nodes_count) preventing overflow an uint idx */
    idx += (uint)bt_random() / nodes_count;	/* (... / nodes_count) preventing overflow an uint idx */
    n->to.val.i = idx++;
  }

@@ -250,7 +250,7 @@ t_find_ranges(void)
    struct f_val looking_up_value = {
	.type = T_INT
    };
    for(looking_up_value.val.i = 0; looking_up_value.val.i <= max_value; looking_up_value.val.i += ((uint)bt_rand_num()/nodes_count))
    for(looking_up_value.val.i = 0; looking_up_value.val.i <= max_value; looking_up_value.val.i += ((uint)bt_random()/nodes_count))
    {
      struct f_tree *found_tree = find_tree(tree, looking_up_value);
      bt_debug("searching: %u \n", looking_up_value.val.i);

filter/trie_test.c

0 → 100644
+187 −0
Original line number Diff line number Diff line
/*
 *	Filters: Utility Functions Tests
 *
 *	(c) 2015 CZ.NIC z.s.p.o.
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include "test/birdtest.h"
#include "test/bt-utils.h"

#include "filter/filter.h"

#define TESTS_NUM		10
#define PREFIXES_NUM 		10
#define PREFIX_TESTS_NUM 	10000

#define BIG_BUFFER_SIZE		10000

struct f_extended_prefix {
  node n; 				/* node in prefixes list */
  struct f_prefix prefix;
  int l;
  int h;
};

static u32
xrandom(u32 max)
{
  return ((u32)bt_random() % max);
}

static int
is_prefix_included(list *prefixes, struct f_prefix *needle)
{
  struct f_extended_prefix *n;
  WALK_LIST(n, *prefixes)
  {
    ip_addr cmask = ipa_mkmask(MIN(n->prefix.len, needle->len));

    if ((ipa_compare(ipa_and(n->prefix.ip, cmask), ipa_and(needle->ip, cmask)) == 0) &&
	(n->l <= needle->len) && (needle->len <= n->h))
    {
      bt_debug("FOUND\t" PRIipa "/%d %d-%d\n", ARGipa(n->prefix.ip), n->prefix.len, n->l, n->h);
      return 1; /* OK */
    }
  }
  return 0; /* FAIL */
}

static struct f_prefix
get_random_prefix(void)
{
  struct f_prefix f = {
#ifdef IPV6
      .ip = ipa_build6(bt_random(), bt_random(), bt_random(), bt_random()),
      .len = xrandom(120)+8,
#else
      .ip = ipa_build4(xrandom(256), xrandom(256), xrandom(256), xrandom(256)),
      .len = xrandom(25)+8,
#endif
  };

  return f;
}

static void
generate_random_prefixes(list *prefixes)
{
  int l, h, x, i;
  for (i = 0; i < PREFIXES_NUM; i++)
  {
    struct f_prefix f = get_random_prefix();

#ifdef IPV6
    l = xrandom(129);
    h = xrandom(129);
#else
    l = xrandom(33);
    h = xrandom(33);
#endif
    if (h < l)
    {
      x = l;
      l = h;
      h = x;
    }

    struct f_extended_prefix *px = calloc(1, sizeof(struct f_extended_prefix));
    px->prefix = f;
    px->l = l;
    px->h = h;

    bt_debug("ADD\t" PRIipa "/%d %d-%d\n", ARGipa(px->prefix.ip), px->prefix.len, px->l, px->h);
    add_tail(prefixes, &px->n);
  }
}

static int
t_match_prefix(void)
{
  bt_bird_init_with_simple_configuration();

  uint round;
  for (round = 0; round < TESTS_NUM; round++)
  {
    list prefixes; /* of structs f_extended_prefix */
    init_list(&prefixes);
    struct f_trie *trie = f_new_trie(config->mem, sizeof(struct f_trie_node));

    generate_random_prefixes(&prefixes);
    struct f_extended_prefix *n;
    WALK_LIST(n, prefixes)
    {
      trie_add_prefix(trie, n->prefix.ip, n->prefix.len, n->l, n->h);
    }

    int i;
    for (i = 0; i < PREFIX_TESTS_NUM; i++)
    {
      struct f_prefix f = get_random_prefix();
      bt_debug("TEST\t" PRIipa "/%d\n", ARGipa(f.ip), f.len);

      int should_be = is_prefix_included(&prefixes, &f);
      int is_there  = trie_match_prefix(trie, f.ip, f.len);
      bt_assert_msg(should_be == is_there, "Prefix " PRIipa "/%d %s", ARGipa(f.ip), f.len, (should_be ? "should be founded but was not founded in trie" : "is not inside trie but searching was false positive."));
    }

    struct f_extended_prefix *nxt;
    WALK_LIST_DELSAFE(n, nxt, prefixes)
    {
      free(n);
    }
  }

  return BT_SUCCESS;
}

static int
t_trie_same(void)
{
  bt_bird_init_with_simple_configuration();

  int round;
  for (round = 0; round < TESTS_NUM*4; round++)
  {
    struct f_trie * trie1 = f_new_trie(config->mem, sizeof(struct f_trie_node));
    struct f_trie * trie2 = f_new_trie(config->mem, sizeof(struct f_trie_node));

    list prefixes; /* of structs f_extended_prefix */
    init_list(&prefixes);
    int i;
    for (i = 0; i < 100; i++)
      generate_random_prefixes(&prefixes);

    struct f_extended_prefix *n;
    WALK_LIST(n, prefixes)
    {
      trie_add_prefix(trie1, n->prefix.ip, n->prefix.len, n->l, n->h);
    }
    WALK_LIST_BACKWARDS(n, prefixes)
    {
      trie_add_prefix(trie2, n->prefix.ip, n->prefix.len, n->l, n->h);
    }

    bt_assert_msg(trie_same(trie1, trie2), "Trie1 and trie2 (backward fullfill) are not same!");

    struct f_extended_prefix *nxt;
    WALK_LIST_DELSAFE(n, nxt, prefixes)
    {
      free(n);
    }
  }

  return BT_SUCCESS;
}

int
main(int argc, char *argv[])
{
  bt_init(argc, argv);

  bt_test_suite(t_match_prefix, "Testing random prefix matching");
  bt_test_suite(t_trie_same, "A trie filled forward should be same with a trie filled backward.");

  return bt_end();
}
+2 −2
Original line number Diff line number Diff line
@@ -75,7 +75,7 @@ t_masklen(void)
    check_mask(((u32) (0xffffffff << (32-i))) & 0xffffffff);

  for (i = 0; i <= MAX_NUM; i++)
    check_mask(bt_rand_num());
    check_mask(bt_random());

  return BT_SUCCESS;
}
@@ -114,7 +114,7 @@ t_log2(void)
    check_log2(i);

  for (i = 1; i < MAX_NUM; i++)
    check_log2(bt_rand_num() % 0xffff);
    check_log2(bt_random() % 0xffff);

  return BT_SUCCESS;
}
+2 −2
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ fill_expected_array(void)
  int i;

  for (i = 0; i < MAX_NUM; i++)
    expected[i] = bt_rand_num();
    expected[i] = bt_random();
}

static void
@@ -86,7 +86,7 @@ t_buffer_pop(void)
  for (i = MAX_NUM-1; i >= MAX_NUM/2; i--)
    BUFFER_POP(buffer);
  for (i = MAX_NUM/2; i < MAX_NUM; i++)
    BUFFER_PUSH(buffer) = expected[i] = bt_rand_num();
    BUFFER_PUSH(buffer) = expected[i] = bt_random();
  is_buffer_as_expected(&buffer);

  /* POP all of elements */
+2 −2
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ t_calculate(void)
  int i;

  for (i = 0; i < MAX_NUM; i++)
    a[i] = bt_rand_num();
    a[i] = bt_random();

  u16 sum_calculated   = ipsum_calculate(a, sizeof(a), NULL);
  u16 sum_calculated_2 = ipsum_calculate(&a[0], sizeof(u32)*(MAX_NUM/2), &a[MAX_NUM/2], sizeof(u32)*(MAX_NUM - MAX_NUM/2), NULL);
@@ -71,7 +71,7 @@ t_verify(void)
  int i;

  for (i = 0; i < MAX_NUM; i++)
    a[i] = bt_rand_num();
    a[i] = bt_random();

  u16 sum = ipsum_calculate_expected(a);

Loading