From f94912a3ea1025c689a5336a1fdc807baa8fa36f Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Tue, 8 Mar 2016 08:37:35 +0100 Subject: tests: added test for the du_buff structure tests/du_buff.c: tests the du_buff_t structure by creating different size du_buff_t's and filling it with different size sdu's at different locations in the du_buff_t tests/CMakefile.txt: added du_buff test lib/du_buff.c: fixed unsafe #define adding brackets --- src/lib/du_buff.c | 2 +- src/lib/tests/CMakeLists.txt | 1 + src/lib/tests/du_buff.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/lib/tests/du_buff.c (limited to 'src') diff --git a/src/lib/du_buff.c b/src/lib/du_buff.c index d88d7ac4..bd1a092d 100644 --- a/src/lib/du_buff.c +++ b/src/lib/du_buff.c @@ -29,7 +29,7 @@ #define OUROBOROS_PREFIX "du_buff" #ifndef DU_BUFF_BLOCKSIZE -#define DU_BUFF_BLOCKSIZE 1 << 16 +#define DU_BUFF_BLOCKSIZE (1 << 16) #endif #include "ouroboros/logs.h" diff --git a/src/lib/tests/CMakeLists.txt b/src/lib/tests/CMakeLists.txt index 2b02768b..5725acc7 100644 --- a/src/lib/tests/CMakeLists.txt +++ b/src/lib/tests/CMakeLists.txt @@ -4,6 +4,7 @@ get_filename_component(src_folder "${tmp}" NAME) create_test_sourcelist(${src_folder}_tests test_suite.c # Add new tests here linked_list.c + du_buff.c ) add_executable(${src_folder}_test EXCLUDE_FROM_ALL ${${src_folder}_tests}) diff --git a/src/lib/tests/du_buff.c b/src/lib/tests/du_buff.c new file mode 100644 index 00000000..e0ae2595 --- /dev/null +++ b/src/lib/tests/du_buff.c @@ -0,0 +1,57 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Test of the du_buff + * + * Dimitri Staessens + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "../du_buff.c" + +#define TEST_BUFF_SIZE 16 * DU_BUFF_BLOCKSIZE +#define MAX(a,b) a > b ? a : b + +int du_buff(int argc, char ** argv) +{ + int i, j, k; + int i_inc, j_inc, k_inc; + + uint8_t bits[TEST_BUFF_SIZE]; + + for (i = 0; i < TEST_BUFF_SIZE; i++) + bits[i] = 170; + + i_inc = MAX(1, DU_BUFF_BLOCKSIZE / 4); + j_inc = MAX(1, DU_BUFF_BLOCKSIZE / 8); + k_inc = MAX(1, DU_BUFF_BLOCKSIZE / 16); + + for (i = DU_BUFF_BLOCKSIZE / 4; i <= TEST_BUFF_SIZE; i += i_inc) { + for (j = 0; j < i; j += j_inc) { + for (k = 0; k < i - j; k += k_inc) { + du_buff_t * dub = du_buff_create(i); + if (dub == NULL) + return -1; + + if (du_buff_init(dub, k, bits, j) < 0) + return -1; + + du_buff_destroy (dub); + } + } + } + return 0; /* tests succeeded */ +} -- cgit v1.2.3 From 5f249bae0ca8fbdf81aa2ab78ce0f5efc7c35f38 Mon Sep 17 00:00:00 2001 From: Dimitri Staessens Date: Tue, 8 Mar 2016 11:18:47 +0100 Subject: lib: Updated du_buff test Tests should have "_test.c" appended and include the source they are testing. The build system will find it in the parent directory. Removed linked_list example test as it is now useless. --- src/lib/tests/CMakeLists.txt | 3 +-- src/lib/tests/du_buff.c | 57 -------------------------------------------- src/lib/tests/du_buff_test.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ src/lib/tests/linked_list.c | 26 -------------------- 4 files changed, 58 insertions(+), 85 deletions(-) delete mode 100644 src/lib/tests/du_buff.c create mode 100644 src/lib/tests/du_buff_test.c delete mode 100644 src/lib/tests/linked_list.c (limited to 'src') diff --git a/src/lib/tests/CMakeLists.txt b/src/lib/tests/CMakeLists.txt index 5725acc7..b1259fa8 100644 --- a/src/lib/tests/CMakeLists.txt +++ b/src/lib/tests/CMakeLists.txt @@ -3,8 +3,7 @@ get_filename_component(src_folder "${tmp}" NAME) create_test_sourcelist(${src_folder}_tests test_suite.c # Add new tests here - linked_list.c - du_buff.c + du_buff_test.c ) add_executable(${src_folder}_test EXCLUDE_FROM_ALL ${${src_folder}_tests}) diff --git a/src/lib/tests/du_buff.c b/src/lib/tests/du_buff.c deleted file mode 100644 index e0ae2595..00000000 --- a/src/lib/tests/du_buff.c +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - * - * Test of the du_buff - * - * Dimitri Staessens - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -#include "../du_buff.c" - -#define TEST_BUFF_SIZE 16 * DU_BUFF_BLOCKSIZE -#define MAX(a,b) a > b ? a : b - -int du_buff(int argc, char ** argv) -{ - int i, j, k; - int i_inc, j_inc, k_inc; - - uint8_t bits[TEST_BUFF_SIZE]; - - for (i = 0; i < TEST_BUFF_SIZE; i++) - bits[i] = 170; - - i_inc = MAX(1, DU_BUFF_BLOCKSIZE / 4); - j_inc = MAX(1, DU_BUFF_BLOCKSIZE / 8); - k_inc = MAX(1, DU_BUFF_BLOCKSIZE / 16); - - for (i = DU_BUFF_BLOCKSIZE / 4; i <= TEST_BUFF_SIZE; i += i_inc) { - for (j = 0; j < i; j += j_inc) { - for (k = 0; k < i - j; k += k_inc) { - du_buff_t * dub = du_buff_create(i); - if (dub == NULL) - return -1; - - if (du_buff_init(dub, k, bits, j) < 0) - return -1; - - du_buff_destroy (dub); - } - } - } - return 0; /* tests succeeded */ -} diff --git a/src/lib/tests/du_buff_test.c b/src/lib/tests/du_buff_test.c new file mode 100644 index 00000000..7bdf1459 --- /dev/null +++ b/src/lib/tests/du_buff_test.c @@ -0,0 +1,57 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Test of the du_buff + * + * Dimitri Staessens + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "du_buff.c" + +#define TEST_BUFF_SIZE 16 * DU_BUFF_BLOCKSIZE +#define MAX(a,b) a > b ? a : b + +int du_buff_test(int argc, char ** argv) +{ + int i, j, k; + int i_inc, j_inc, k_inc; + + uint8_t bits[TEST_BUFF_SIZE]; + + for (i = 0; i < TEST_BUFF_SIZE; i++) + bits[i] = 170; + + i_inc = MAX(1, DU_BUFF_BLOCKSIZE / 4); + j_inc = MAX(1, DU_BUFF_BLOCKSIZE / 8); + k_inc = MAX(1, DU_BUFF_BLOCKSIZE / 16); + + for (i = DU_BUFF_BLOCKSIZE / 4; i <= TEST_BUFF_SIZE; i += i_inc) { + for (j = 0; j < i; j += j_inc) { + for (k = 0; k < i - j; k += k_inc) { + du_buff_t * dub = du_buff_create(i); + if (dub == NULL) + return -1; + + if (du_buff_init(dub, k, bits, j) < 0) + return -1; + + du_buff_destroy (dub); + } + } + } + return 0; /* tests succeeded */ +} diff --git a/src/lib/tests/linked_list.c b/src/lib/tests/linked_list.c deleted file mode 100644 index 2239b065..00000000 --- a/src/lib/tests/linked_list.c +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - * - * Test of the linked list - * - * Sander Vrijders - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - */ - -int linked_list(int argc, char ** argv) -{ - return 0; -} -- cgit v1.2.3 From a480c3510f98c491879d42ff7106287f07d03e2f Mon Sep 17 00:00:00 2001 From: Sander Vrijders Date: Wed, 9 Mar 2016 16:53:46 +0100 Subject: lib: Add bitmap test This adds a test for the bitmap. During the testing I also removed some bugs that were present in the bitmap implementation. --- include/ouroboros/bitmap.h | 16 ++++----- src/lib/bitmap.c | 61 ++++++++++++++++++++--------------- src/lib/tests/CMakeLists.txt | 1 + src/lib/tests/bitmap_test.c | 77 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 34 deletions(-) create mode 100644 src/lib/tests/bitmap_test.c (limited to 'src') diff --git a/include/ouroboros/bitmap.h b/include/ouroboros/bitmap.h index 6296e2e0..c109f3e8 100644 --- a/include/ouroboros/bitmap.h +++ b/include/ouroboros/bitmap.h @@ -1,7 +1,7 @@ /* * Ouroboros - Copyright (C) 2016 * - * RINA bitmap implementation - wraps around bitmap from Linux kernel + * Bitmap implementation * * Sander Vrijders * Francesco Salvestrini @@ -28,14 +28,14 @@ #include #include -struct rbmp; +struct bmp; -struct rbmp * rbmp_create(size_t bits, ssize_t offset); -int rbmp_destroy(struct rbmp * b); +struct bmp * bmp_create(size_t bits, ssize_t offset); +int bmp_destroy(struct bmp * b); -ssize_t rbmp_allocate(struct rbmp * instance); -int rbmp_release(struct rbmp * instance, - ssize_t id); -bool rbmp_is_id_ok(struct rbmp * b, ssize_t id); +ssize_t bmp_allocate(struct bmp * instance); +int bmp_release(struct bmp * instance, + ssize_t id); +bool bmp_is_id_valid(struct bmp * b, ssize_t id); #endif diff --git a/src/lib/bitmap.c b/src/lib/bitmap.c index 3aaa422c..8aabb4f4 100644 --- a/src/lib/bitmap.c +++ b/src/lib/bitmap.c @@ -21,7 +21,10 @@ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ +#define OUROBOROS_PREFIX "bitmap" + #include +#include #include #include #include @@ -37,8 +40,6 @@ #define BITS_TO_LONGS(nr) \ DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long)) -#define BITS_IN_BITMAP ((2 << BITS_PER_BYTE) * sizeof(size_t)) - static unsigned long find_next_zero_bit(const unsigned long * addr, unsigned long nbits) { @@ -59,7 +60,7 @@ static unsigned long find_next_zero_bit(const unsigned long * addr, /* Find the free bit in the word */ mask = 1UL; - while (!(tmp ^ mask)) { + while (!(tmp & mask)) { pos++; mask = 1UL << pos; } @@ -78,31 +79,30 @@ static void bitmap_clear(unsigned long * map, unsigned int start) { unsigned long * p = map + BIT_WORD(start); - unsigned long mask = ~(1UL << (start % (BITS_PER_LONG - 1))); + unsigned long mask = ~(1UL << (start % (BITS_PER_LONG))); *p &= mask; } - static void bitmap_set(unsigned long * map, unsigned int start) { unsigned long * p = map + BIT_WORD(start); - unsigned long mask = 1UL << (start % (BITS_PER_LONG - 1)); + unsigned long mask = 1UL << (start % (BITS_PER_LONG)); *p |= mask; } -struct rbmp { +struct bmp { ssize_t offset; size_t size; - unsigned long bitmap[BITS_TO_LONGS(BITS_IN_BITMAP)]; + unsigned long * bitmap; }; -struct rbmp * rbmp_create(size_t bits, ssize_t offset) +struct bmp * bmp_create(size_t bits, ssize_t offset) { - struct rbmp * tmp; + struct bmp * tmp; if (bits == 0) return NULL; @@ -111,32 +111,41 @@ struct rbmp * rbmp_create(size_t bits, ssize_t offset) if (!tmp) return NULL; + tmp->bitmap = malloc(BITS_TO_LONGS(bits) * sizeof(*(tmp->bitmap))); + if (!tmp->bitmap) + return NULL; + tmp->size = bits; tmp->offset = offset; - bitmap_zero(tmp->bitmap, BITS_IN_BITMAP); + bitmap_zero(tmp->bitmap, bits); return tmp; } - -int rbmp_destroy(struct rbmp * b) +int bmp_destroy(struct bmp * b) { - if (!b) + if (b == NULL) + return -1; + + if (b->bitmap == NULL) { + free(b); return -1; + } + free(b->bitmap); free(b); return 0; } -static ssize_t bad_id(struct rbmp * b) +static ssize_t bad_id(struct bmp * b) { assert(b); return b->offset - 1; } -ssize_t rbmp_allocate(struct rbmp * b) +ssize_t bmp_allocate(struct bmp * b) { ssize_t id; @@ -144,9 +153,9 @@ ssize_t rbmp_allocate(struct rbmp * b) return bad_id(b); id = (ssize_t) find_next_zero_bit(b->bitmap, - BITS_IN_BITMAP); + b->size); - if (id == BITS_IN_BITMAP) + if (id >= b->size) return bad_id(b); bitmap_set(b->bitmap, id); @@ -154,8 +163,8 @@ ssize_t rbmp_allocate(struct rbmp * b) return id + b->offset; } -static bool is_id_ok(struct rbmp * b, - ssize_t id) +static bool is_id_valid(struct bmp * b, + ssize_t id) { assert(b); @@ -165,24 +174,24 @@ static bool is_id_ok(struct rbmp * b, return true; } -bool rbmp_is_id_ok(struct rbmp * b, - ssize_t id) +bool bmp_is_id_valid(struct bmp * b, + ssize_t id) { if (!b) return false; - return is_id_ok(b, id); + return is_id_valid(b, id); } -int rbmp_release(struct rbmp * b, - ssize_t id) +int bmp_release(struct bmp * b, + ssize_t id) { ssize_t rid; if (!b) return -1; - if (!is_id_ok(b, id)) + if (!is_id_valid(b, id)) return -1; rid = id - b->offset; diff --git a/src/lib/tests/CMakeLists.txt b/src/lib/tests/CMakeLists.txt index b1259fa8..99df7232 100644 --- a/src/lib/tests/CMakeLists.txt +++ b/src/lib/tests/CMakeLists.txt @@ -3,6 +3,7 @@ get_filename_component(src_folder "${tmp}" NAME) create_test_sourcelist(${src_folder}_tests test_suite.c # Add new tests here + bitmap_test.c du_buff_test.c ) diff --git a/src/lib/tests/bitmap_test.c b/src/lib/tests/bitmap_test.c new file mode 100644 index 00000000..4d2d0c73 --- /dev/null +++ b/src/lib/tests/bitmap_test.c @@ -0,0 +1,77 @@ +/* + * Ouroboros - Copyright (C) 2016 + * + * Test of the bitmap + * + * Sander Vrijders + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "bitmap.c" +#include +#include + +#define BITMAP_SIZE 200 + +int bitmap_test(int argc, char ** argv) +{ + struct bmp * bmp; + size_t bits = BITMAP_SIZE; + ssize_t id; + int i; + ssize_t r; + ssize_t offset = 100; + + srand(time(NULL)); + + bmp = bmp_create(bits, offset); + if (bmp == NULL) + return -1; + + if (bmp_destroy(bmp)) + return -1; + + bmp = bmp_create(bits, offset); + if (bmp == NULL) + return -1; + + for (i = offset; i < BITMAP_SIZE + 5 + offset; i++) { + id = bmp_allocate(bmp); + if (!bmp_is_id_valid(bmp, id)) + continue; + + if (id != i) + return -1; + } + + for (i = 0; i < BITMAP_SIZE + 5; i++) { + r = (ssize_t) (rand() % BITMAP_SIZE) + offset; + + if (bmp_release(bmp, r)) + return -1; + + id = bmp_allocate(bmp); + if (!bmp_is_id_valid(bmp, id)) + continue; + if (id != r) + return -1; + } + + if (bmp_destroy(bmp)) + return -1; + + return 0; +} -- cgit v1.2.3