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

Birdtest: add some utils to framework

and updates old tests
parent 39059e21
Loading
Loading
Loading
Loading
+5 −19
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
 */

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

#define MAX_NUM 1000
@@ -79,27 +80,12 @@ t_masklen(void)
  return BT_SUCCESS;
}

static u32
pow2(u32 exp)
{
  u32 i, n, r;
  n = r = 2;

  if (exp == 0)
    return 1;

  for (i = 2; i <= exp; i++)
    r *= n;

  return r;
}

static void
check_log2(u32 n)
{
  u32 log  = u32_log2(n);
  u32 low  = pow2(log);
  u32 high = pow2(log+1);
  u32 low  = naive_pow(2, log);
  u32 high = naive_pow(2, log+1);

  bt_debug("Test u32_log2(%u) = %u, %u should be in the range <%u, %u) \n", n, log, n, low, high);
  bt_assert(n >= low && n < high);
@@ -117,7 +103,7 @@ t_log2(void)

  for (i = 0; i < 31; i++)
  {
    in_out_data[i].in  = pow2(i+1);
    in_out_data[i].in  = naive_pow(2, i+1);
    in_out_data[i].out = i+1;
  }
  bt_assert_out_fn_in(u32_log2, in_out_data, "%u", "%u");
@@ -128,7 +114,7 @@ t_log2(void)
    check_log2(i);

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

  return BT_SUCCESS;
}
+4 −4
Original line number Diff line number Diff line
@@ -14,11 +14,11 @@
#include "lib/buffer.h"

#define MAX_NUM 33
typedef BUFFER(int) buffer_int;

int expected[MAX_NUM];
buffer_int buffer;
struct pool *buffer_pool;
typedef BUFFER(int) buffer_int;
static int expected[MAX_NUM];
static buffer_int buffer;
static struct pool *buffer_pool;

static void
show_buf(buffer_int *b)
+1 −1
Original line number Diff line number Diff line
@@ -90,7 +90,7 @@ validate_filled_hash(void)
  for (i = 0; i < MAX_NUM; i++)
  {
    node = HASH_FIND(hash, TEST, nodes[i].key);
    bt_assert_msg(node == &nodes[i], "Hash should be filled, but we not find (%p) the node[%d] (%p) with .key = %u, .next %p \n", node, i, &nodes[i], nodes[i].key, nodes[i].next);
    bt_assert_msg(node->key == nodes[i].key, "Hash should be filled, but we not find (%p) the node[%d] (%p) with .key = %u, .next %p \n", node, i, &nodes[i], nodes[i].key, nodes[i].next);
  }

  print_rate_of_fulfilment();
+12 −7
Original line number Diff line number Diff line
@@ -36,11 +36,15 @@ const char *bt_test_id;
uint bt_success;
uint bt_test_suite_success;

int
long int
bt_rand_num(void)
{
  /* Seeded in bt_init() */
  return random();
  long int rand_low, rand_high;

  rand_low = random();
  rand_high = random();
  return (rand_low & 0xffff) | ((rand_high & 0xffff) << 16);
}

void
@@ -119,7 +123,7 @@ dump_stack(void)
}

static
int bt_run_test_fn(int (*test_fn)(void *), void *test_fn_argument, int timeout)
int bt_run_test_fn(int (*test_fn)(const void *), const void *test_fn_argument, int timeout)
{
  int result;
  alarm(timeout);
@@ -133,7 +137,7 @@ int bt_run_test_fn(int (*test_fn)(void *), void *test_fn_argument, int timeout)
}

void
bt_test_suite_base(int (*test_fn)(void *), const char *test_id, void *test_fn_argument, int forked, int timeout, const char *dsc, ...)
bt_test_suite_base(int (*test_fn)(const void *), const char *test_id, const void *test_fn_argument, int forked, int timeout, const char *dsc, ...)
{
  if (list_tests)
  {
@@ -213,9 +217,10 @@ bt_test_suite_base(int (*test_fn)(void *), const char *test_id, void *test_fn_ar
static uint
get_num_terminal_cols(void)
{
  struct winsize w;
  struct winsize w = {};
  ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
  return w.ws_col;
  uint cols = w.ws_col;
  return (cols > 0 ? cols : 80);
}

void
@@ -229,7 +234,7 @@ bt_result(const char *to_right_align_msg, const char *to_left_align_msg, ...)

    va_list argptr;
    va_start(argptr, to_left_align_msg);
    vsnprintf(msg_buf + strlen(msg_buf), sizeof(msg_buf), to_left_align_msg, argptr);
    vsnprintf(msg_buf + strlen((char *)msg_buf), sizeof(msg_buf), to_left_align_msg, argptr);

    char fmt_buf[BT_BUFFER_SIZE];
    uint line_len = strlen(msg_buf) + BT_PROMPT_OK_FAIL_LEN;
+3 −3
Original line number Diff line number Diff line
@@ -27,8 +27,8 @@ extern const char *bt_test_id;

void bt_init(int argc, char *argv[]);
int  bt_end(void);
void bt_test_suite_base(int (*test_fn)(void *), const char *test_id, void *test_fn_argument, int forked, int timeout, const char *dsc, ...);
int  bt_rand_num(void);
void bt_test_suite_base(int (*test_fn)(const void *), const char *test_id, const void *test_fn_argument, int forked, int timeout, const char *dsc, ...);
long int bt_rand_num(void);
void bt_result(const char *result, const char *msg, ...);

#define BT_SUCCESS 			0
@@ -54,7 +54,7 @@ void bt_result(const char *result, const char *msg, ...);
    bt_test_suite_extra(fn, BT_DEFAULT_FORKING, BT_DEFAULT_TIMEOUT, dsc, ##__VA_ARGS__)

#define bt_test_suite_extra(fn, f, t, dsc, ...) \
    bt_test_suite_base((int (*)(void *))fn, #fn, NULL, f, t, dsc, ##__VA_ARGS__)
    bt_test_suite_base((int (*)(const void *))fn, #fn, NULL, f, t, dsc, ##__VA_ARGS__)

#define bt_test_suite_arg(fn, arg, dsc, ...) \
    bt_test_suite_arg_extra(fn, arg, BT_DEFAULT_FORKING, BT_DEFAULT_TIMEOUT, dsc, ##__VA_ARGS__)
Loading