diff options
author | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2016-11-09 16:36:34 +0100 |
---|---|---|
committer | Ondrej Zajicek (work) <santiago@crfreenet.org> | 2016-11-09 16:36:34 +0100 |
commit | 9b0a0ba9e671d9134b93c33ab73ccccb352acafa (patch) | |
tree | 2a3b007b698c02c72c7bae25d3c7cae6293cd36f /lib/buffer_test.c | |
parent | 8860e991f6650e47cfe6c1af595fe4fe92a4edfd (diff) |
Unit Testing for BIRD
- Unit Testing Framework (BirdTest)
- Integration of BirdTest into the BIRD build system
- Tests for several BIRD modules
Based on squashed Pavel Tvrdik's int-test branch, updated for
current int-new branch.
Diffstat (limited to 'lib/buffer_test.c')
-rw-r--r-- | lib/buffer_test.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/lib/buffer_test.c b/lib/buffer_test.c new file mode 100644 index 00000000..38e540b7 --- /dev/null +++ b/lib/buffer_test.c @@ -0,0 +1,147 @@ +/* + * BIRD Library -- Buffer Tests + * + * (c) 2015 CZ.NIC z.s.p.o. + * + * Can be freely distributed and used under the terms of the GNU GPL. + */ + +#include <stdlib.h> + +#include "test/birdtest.h" + +#include "lib/buffer.h" + +#define MAX_NUM 33 + +typedef BUFFER(int) buffer_int; +static int expected[MAX_NUM]; +static buffer_int buf; +static struct pool *buffer_pool; + +static void +show_buf(buffer_int *b) +{ + uint i; + bt_debug(".used = %d, .size = %d\n", b->used, b->size); + + for (i = 0; i < b->used; i++) + bt_debug(" .data[%3u] = %-16d expected %-16d %s\n", i, b->data[i], expected[i], (b->data[i] == expected[i] ? "OK" : "FAIL!")); +} + +static void +fill_expected_array(void) +{ + int i; + + for (i = 0; i < MAX_NUM; i++) + expected[i] = bt_random(); +} + +static void +init_buffer(void) +{ + resource_init(); + buffer_pool = &root_pool; + BUFFER_INIT(buf, buffer_pool, MAX_NUM); +} + +static int +is_buffer_as_expected(buffer_int *b) +{ + show_buf(b); + + int i; + for (i = 0; i < MAX_NUM; i++) + bt_assert(b->data[i] == expected[i]); + return 1; +} + +static int +t_buffer_push(void) +{ + int i; + + init_buffer(); + fill_expected_array(); + + for (i = 0; i < MAX_NUM; i++) + BUFFER_PUSH(buf) = expected[i]; + is_buffer_as_expected(&buf); + + return BT_SUCCESS; +} + +static int +t_buffer_pop(void) +{ + int i; + + init_buffer(); + fill_expected_array(); + + /* POP a half of elements */ + for (i = 0; i < MAX_NUM; i++) + BUFFER_PUSH(buf) = expected[i]; + for (i = MAX_NUM-1; i >= MAX_NUM/2; i--) + BUFFER_POP(buf); + for (i = MAX_NUM/2; i < MAX_NUM; i++) + BUFFER_PUSH(buf) = expected[i] = bt_random(); + is_buffer_as_expected(&buf); + + /* POP all of elements */ + for (i = MAX_NUM-1; i >= 0; i--) + BUFFER_POP(buf); + bt_assert(buf.used == 0); + for (i = 0; i < MAX_NUM; i++) + BUFFER_PUSH(buf) = expected[i]; + is_buffer_as_expected(&buf); + + return BT_SUCCESS; +} + +static int +t_buffer_resize(void) +{ + int i; + + init_buffer(); + BUFFER_INIT(buf, buffer_pool, 0); + fill_expected_array(); + + for (i = 0; i < MAX_NUM; i++) + BUFFER_PUSH(buf) = expected[i]; + is_buffer_as_expected(&buf); + bt_assert(buf.size >= MAX_NUM); + + return BT_SUCCESS; +} + +static int +t_buffer_flush(void) +{ + int i; + + init_buffer(); + fill_expected_array(); + for (i = 0; i < MAX_NUM; i++) + BUFFER_PUSH(buf) = expected[i]; + + BUFFER_FLUSH(buf); + bt_assert(buf.used == 0); + + return BT_SUCCESS; +} + +int +main(int argc, char *argv[]) +{ + bt_init(argc, argv); + + bt_test_suite(t_buffer_push, "Pushing new elements"); + bt_test_suite(t_buffer_pop, "Fill whole buffer (PUSH), a half of elements POP and PUSH new elements"); + bt_test_suite(t_buffer_resize, "Init a small buffer and try overfill"); + bt_test_suite(t_buffer_flush, "Fill and flush all elements"); + + return bt_exit_value(); +} |