diff options
Diffstat (limited to 'src/lib/tests')
| -rw-r--r-- | src/lib/tests/CMakeLists.txt | 19 | ||||
| -rw-r--r-- | src/lib/tests/bitmap_test.c | 31 | ||||
| -rw-r--r-- | src/lib/tests/btree_test.c | 6 | ||||
| -rw-r--r-- | src/lib/tests/crc32_test.c | 6 | ||||
| -rw-r--r-- | src/lib/tests/hash_test.c | 202 | ||||
| -rw-r--r-- | src/lib/tests/hashtable_test.c | 128 | ||||
| -rw-r--r-- | src/lib/tests/md5_test.c | 6 | ||||
| -rw-r--r-- | src/lib/tests/rq_test.c | 115 | ||||
| -rw-r--r-- | src/lib/tests/sha3_test.c | 6 | ||||
| -rw-r--r-- | src/lib/tests/shm_rbuff_test.c | 113 | ||||
| -rw-r--r-- | src/lib/tests/time_test.c (renamed from src/lib/tests/time_utils_test.c) | 12 | ||||
| -rw-r--r-- | src/lib/tests/timerwheel_test.c | 104 |
12 files changed, 357 insertions, 391 deletions
diff --git a/src/lib/tests/CMakeLists.txt b/src/lib/tests/CMakeLists.txt index a9ff207e..dc90671b 100644 --- a/src/lib/tests/CMakeLists.txt +++ b/src/lib/tests/CMakeLists.txt @@ -1,23 +1,16 @@ get_filename_component(PARENT_PATH ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY) get_filename_component(PARENT_DIR ${PARENT_PATH} NAME) -if (NOT (APPLE OR GNU)) - set(TIMERWHEEL_TEST "timerwheel_test.c") -else () - set(TIMERWHEEL_TEST "") -endif () - create_test_sourcelist(${PARENT_DIR}_tests test_suite.c # Add new tests here bitmap_test.c btree_test.c crc32_test.c - hashtable_test.c + hash_test.c md5_test.c - rq_test.c sha3_test.c - time_utils_test.c - ${TIMERWHEEL_TEST} + shm_rbuff_test.c + time_test.c ) add_executable(${PARENT_DIR}_test EXCLUDE_FROM_ALL ${${PARENT_DIR}_tests}) @@ -27,7 +20,11 @@ target_link_libraries(${PARENT_DIR}_test ouroboros-common) add_dependencies(check ${PARENT_DIR}_test) set(tests_to_run ${${PARENT_DIR}_tests}) -remove(tests_to_run test_suite.c) +if(CMAKE_VERSION VERSION_LESS "3.29.0") + remove(tests_to_run test_suite.c) +else () + list(POP_FRONT tests_to_run) +endif() foreach (test ${tests_to_run}) get_filename_component(test_name ${test} NAME_WE) diff --git a/src/lib/tests/bitmap_test.c b/src/lib/tests/bitmap_test.c index 619d065c..4dbd6653 100644 --- a/src/lib/tests/bitmap_test.c +++ b/src/lib/tests/bitmap_test.c @@ -1,10 +1,10 @@ /* - * Ouroboros - Copyright (C) 2016 - 2018 + * Ouroboros - Copyright (C) 2016 - 2024 * * Test of the bitmap * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> + * Dimitri Staessens <dimitri@ouroboros.rocks> + * Sander Vrijders <sander@ouroboros.rocks> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -27,7 +27,8 @@ #define BITMAP_SIZE 200 -int bitmap_test(int argc, char ** argv) +int bitmap_test(int argc, + char ** argv) { struct bmp * bmp; ssize_t bits = BITMAP_SIZE; @@ -60,27 +61,23 @@ int bitmap_test(int argc, char ** argv) if (!bmp_is_id_valid(bmp, id)) { if (i < BITMAP_SIZE + offset) { printf("Failed valid ID %d (%zd).\n", i, id); - bmp_destroy(bmp); - return -1; + goto fail; } if (id >= offset && id < bits + offset) { printf("Valid ID %zd returned invalid.\n", id); - bmp_destroy(bmp); - return -1; + goto fail; } continue; } if (!bmp_is_id_used(bmp, id)) { printf("ID not marked in use.\n"); - bmp_destroy(bmp); - return -1; + goto fail; } if (id != i) { printf("Wrong ID returned.\n"); - bmp_destroy(bmp); - return -1; + goto fail; } } @@ -89,20 +86,24 @@ int bitmap_test(int argc, char ** argv) if (bmp_release(bmp, r)) { printf("Failed to release ID.\n"); - return -1; + goto fail; } id = bmp_allocate(bmp); if (!bmp_is_id_valid(bmp, id)) continue; + if (id != r) { printf("Wrong prev ID returned.\n"); - bmp_destroy(bmp); - return -1; + goto fail; } } bmp_destroy(bmp); return 0; + + fail: + bmp_destroy(bmp); + return -1; } diff --git a/src/lib/tests/btree_test.c b/src/lib/tests/btree_test.c index bd4390c7..8bd30370 100644 --- a/src/lib/tests/btree_test.c +++ b/src/lib/tests/btree_test.c @@ -1,10 +1,10 @@ /* - * Ouroboros - Copyright (C) 2016 - 2018 + * Ouroboros - Copyright (C) 2016 - 2024 * * Test of the B-tree implementation * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> + * Dimitri Staessens <dimitri@ouroboros.rocks> + * Sander Vrijders <sander@ouroboros.rocks> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as diff --git a/src/lib/tests/crc32_test.c b/src/lib/tests/crc32_test.c index df980d3f..a26c8220 100644 --- a/src/lib/tests/crc32_test.c +++ b/src/lib/tests/crc32_test.c @@ -1,10 +1,10 @@ /* - * Ouroboros - Copyright (C) 2016 - 2018 + * Ouroboros - Copyright (C) 2016 - 2024 * * Test of the CRC32 function * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> + * Dimitri Staessens <dimitri@ouroboros.rocks> + * Sander Vrijders <sander@ouroboros.rocks> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as diff --git a/src/lib/tests/hash_test.c b/src/lib/tests/hash_test.c new file mode 100644 index 00000000..970d9185 --- /dev/null +++ b/src/lib/tests/hash_test.c @@ -0,0 +1,202 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2024 + * + * Test of the hashing functions + * + * Dimitri Staessens <dimitri@ouroboros.rocks> + * Sander Vrijders <sander@ouroboros.rocks> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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., http://www.fsf.org/about/contact/. + */ + +#include <ouroboros/hash.h> +#include <ouroboros/test.h> + +#include <stdlib.h> +#include <stdint.h> +#include <assert.h> +#include <string.h> +#include <stdio.h> + +/* + * Test vectors calculated at + * https://www.lammertbies.nl/comm/info/crc-calculation.html + */ + +struct vec_entry { + char * in; + char * out; +}; + +static int test_crc32(void) +{ + int ret = 0; + + struct vec_entry vec [] = { + { "0", "f4dbdf21" }, + { "123456789", "cbf43926" }, + { "987654321", "015f0201" }, + { NULL, NULL } + }; + + struct vec_entry * cur = vec; + + TEST_START(); + + while (cur->in != NULL) { + uint8_t crc[4]; + char res[9]; + + str_hash(HASH_CRC32, crc, cur->in); + + sprintf(res, HASH_FMT32, HASH_VAL32(crc)); + if (strcmp(res, cur->out) != 0) { + printf("Hash failed %s != %s.\n", res, cur->out); + ret |= -1; + } + + ++cur; + } + + TEST_END(ret); + + return ret; +} + +static int test_md5(void) +{ + int ret = 0; + + struct vec_entry vec [] = {{ + "abc", + "900150983cd24fb0d6963f7d28e17f72" + }, { + "The quick brown fox jumps over the lazy dog", + "9e107d9d372bb6826bd81d3542a419d6" + }, { + "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", + "8215ef0796a20bcaaae116d3876c664a" + }, { + NULL, + NULL + }}; + + struct vec_entry * cur = vec; + + TEST_START(); + + + while (cur->in != NULL) { + uint8_t md5[16]; + char res[33]; + + str_hash(HASH_MD5, md5, cur->in); + + sprintf(res, HASH_FMT128, HASH_VAL128(md5)); + if (strcmp(res, cur->out) != 0) { + printf("Hash failed %s != %s.\n", res, cur->out); + ret |= -1; + } + + ++cur; + } + + TEST_END(ret); + + return ret; +} + +static int test_sha3(void) +{ + int ret = 0; + + uint8_t sha3[64]; + char res[129]; + + char * in = "abc"; + + char * out = + "e642824c3f8cf24ad09234ee7d3c766f" + "c9a3a5168d0c94ad73b46fdf"; + + TEST_START(); + + str_hash(HASH_SHA3_224, sha3, in); + + sprintf(res, HASH_FMT224, HASH_VAL224(sha3)); + if (strcmp(res, out) != 0) { + printf("SHA3-224 failed %s != %s", res, out); + ret |= -1; + } + + out = + "3a985da74fe225b2045c172d6bd390bd" + "855f086e3e9d525b46bfe24511431532"; + + str_hash(HASH_SHA3_256, sha3, in); + + sprintf(res, HASH_FMT256, HASH_VAL256(sha3)); + if (strcmp(res, out) != 0) { + printf("SHA3-256 failed %s != %s.\n", res, out); + ret |= -1; + } + + out = + "ec01498288516fc926459f58e2c6ad8d" + "f9b473cb0fc08c2596da7cf0e49be4b2" + "98d88cea927ac7f539f1edf228376d25"; + + str_hash(HASH_SHA3_384, sha3, in); + + sprintf(res, HASH_FMT384, HASH_VAL384(sha3)); + if (strcmp(res, out) != 0) { + printf("SHA3-384failed %s != %s.'n", res, out); + ret |= -1; + } + + out = + "b751850b1a57168a5693cd924b6b096e" + "08f621827444f70d884f5d0240d2712e" + "10e116e9192af3c91a7ec57647e39340" + "57340b4cf408d5a56592f8274eec53f0"; + + str_hash(HASH_SHA3_512, sha3, in); + + sprintf(res, HASH_FMT512, HASH_VAL512(sha3)); + if (strcmp(res, out) != 0) { + printf("SHA3-512 failed %s != %s.\n", res, out); + ret |= -1; + } + + TEST_END(ret); + + return ret; +} + +int hash_test(int argc, + char ** argv) +{ + int ret = 0; + + (void) argc; + (void) argv; + + ret |= test_crc32(); + + ret |= test_md5(); + + ret |= test_sha3(); + + return ret; +} diff --git a/src/lib/tests/hashtable_test.c b/src/lib/tests/hashtable_test.c deleted file mode 100644 index d8cc73c5..00000000 --- a/src/lib/tests/hashtable_test.c +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2018 - * - * Test of the hash table - * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * 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., http://www.fsf.org/about/contact/. - */ - -#include "hashtable.c" - -#include <stdio.h> - -#define HASHTABLE_SIZE 256 -#define INT_TEST 4 - -int hashtable_test(int argc, char ** argv) -{ - struct htable * table; - int i; - int * j; - void * el; - size_t len; - - (void) argc; - (void) argv; - - table = htable_create(HASHTABLE_SIZE, true); - if (table == NULL) { - printf("Failed to create.\n"); - return -1; - } - - htable_destroy(table); - - table = htable_create(HASHTABLE_SIZE, false); - if (table == NULL) { - printf("Failed to create.\n"); - return -1; - } - - for (i = 0; i < HASHTABLE_SIZE + INT_TEST + 2; i++) { - j = malloc(sizeof(*j)); - if (j == NULL) { - printf("Failed to malloc.\n"); - htable_destroy(table); - return -1; - } - *j = i; - - if (htable_insert(table, i, (void *) j, 1)) { - printf("Failed to insert.\n"); - htable_destroy(table); - return -1; - } - } - - if (htable_lookup(table, INT_TEST, &el, &len)) { - printf("Failed to lookup.\n"); - htable_destroy(table); - return -1; - } - - j = (int *) el; - if (*j != INT_TEST) { - printf("Lookup returned wrong value (%d != %d).\n", - INT_TEST, *j); - htable_destroy(table); - return -1; - } - - if (htable_lookup(table, HASHTABLE_SIZE + INT_TEST, &el, &len)) { - printf("Failed to lookup.\n"); - htable_destroy(table); - return -1; - } - - j = (int *) el; - if (*j != HASHTABLE_SIZE + INT_TEST) { - printf("Lookup returned wrong value (%d != %d).\n", - INT_TEST, *j); - htable_destroy(table); - return -1; - } - - if (htable_delete(table, INT_TEST)) { - printf("Failed to delete.\n"); - htable_destroy(table); - return -1; - } - - if (htable_lookup(table, INT_TEST, &el, &len) == 0) { - printf("Failed to delete properly.\n"); - htable_destroy(table); - return -1; - } - - if (htable_lookup(table, HASHTABLE_SIZE + INT_TEST, &el, &len)) { - printf("Failed to lookup after deletion.\n"); - htable_destroy(table); - return -1; - } - - j = (int *) el; - if (*j != HASHTABLE_SIZE + INT_TEST) { - printf("Lookup returned wrong value (%d != %d).\n", - INT_TEST, *j); - htable_destroy(table); - return -1; - } - - htable_destroy(table); - - return 0; -} diff --git a/src/lib/tests/md5_test.c b/src/lib/tests/md5_test.c index a7cdd8ec..28e8f42f 100644 --- a/src/lib/tests/md5_test.c +++ b/src/lib/tests/md5_test.c @@ -1,10 +1,10 @@ /* - * Ouroboros - Copyright (C) 2016 - 2018 + * Ouroboros - Copyright (C) 2016 - 2024 * * Test of the MD5 function * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> + * Dimitri Staessens <dimitri@ouroboros.rocks> + * Sander Vrijders <sander@ouroboros.rocks> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as diff --git a/src/lib/tests/rq_test.c b/src/lib/tests/rq_test.c deleted file mode 100644 index 3d358830..00000000 --- a/src/lib/tests/rq_test.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2018 - * - * Reordering queue test - * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * version 2.1 as published by the Free Software Foundation. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., http://www.fsf.org/about/contact/. - */ - -#include "rq.h" - -#include <stdio.h> - -#define Q_SIZE 5 - -int rq_test(int argc, - char ** argv) -{ - struct rq * q; - int i; - - (void) argc; - (void) argv; - - q = rq_create(Q_SIZE); - if (q == NULL) { - printf("Failed to create.\n"); - return -1; - } - - if (rq_push(q, 1, 1)) { - printf("Failed to insert.\n"); - return -1; - } - - if (!rq_has(q, 1)) { - printf("Inserted item not present.\n"); - return -1; - } - - if (rq_peek(q) != 1) { - printf("Inserted item not present.\n"); - return -1; - } - - if (rq_pop(q) != 1) { - printf("Bad pop.\n"); - return -1; - } - - if (rq_push(q, 3, 5)) { - printf("Failed to insert.\n"); - return -1; - } - - if (rq_push(q, 1, 3)) { - printf("Failed to insert.\n"); - return -1; - } - - if (rq_push(q, 2, 7)) { - printf("Failed to insert.\n"); - return -1; - } - - if (!rq_has(q, 3)) { - printf("Inserted item not present.\n"); - return -1; - } - - if (rq_has(q, 4)) { - printf("Item present that was not inserted.\n"); - return -1; - } - - if (rq_peek(q) != 1) { - printf("Inserted item not present.\n"); - return -1; - } - - if (rq_pop(q) != 3) { - printf("Bad pop.\n"); - return -1; - } - - if (rq_peek(q) != 2) { - printf("Inserted item not present.\n"); - return -1; - } - - if (rq_pop(q) != 7) { - printf("Bad pop.\n"); - return -1; - } - - for (i = 0; i < Q_SIZE + 1; i++) - rq_push(q, i, i); - - rq_destroy(q); - - return 0; -} diff --git a/src/lib/tests/sha3_test.c b/src/lib/tests/sha3_test.c index edf3b0e0..82b4ef0d 100644 --- a/src/lib/tests/sha3_test.c +++ b/src/lib/tests/sha3_test.c @@ -1,10 +1,10 @@ /* - * Ouroboros - Copyright (C) 2016 - 2018 + * Ouroboros - Copyright (C) 2016 - 2024 * * Test of the SHA3 function * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> + * Dimitri Staessens <dimitri@ouroboros.rocks> + * Sander Vrijders <sander@ouroboros.rocks> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as diff --git a/src/lib/tests/shm_rbuff_test.c b/src/lib/tests/shm_rbuff_test.c new file mode 100644 index 00000000..e36c3229 --- /dev/null +++ b/src/lib/tests/shm_rbuff_test.c @@ -0,0 +1,113 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2024 + * + * Test of the shm_rbuff + * + * Dimitri Staessens <dimitri@ouroboros.rocks> + * Sander Vrijders <sander@ouroboros.rocks> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * 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., http://www.fsf.org/about/contact/. + */ + +#define _POSIX_C_SOURCE 200112L + +#include "config.h" + +#include <ouroboros/shm_rbuff.h> + +#include <errno.h> +#include <stdio.h> +#include <unistd.h> + +int shm_rbuff_test(int argc, + char ** argv) +{ + struct shm_rbuff * rb; + size_t i; + + (void) argc; + (void) argv; + + printf("Test: create rbuff..."); + + rb = shm_rbuff_create(getpid(), 1); + if (rb == NULL) + goto err; + + printf("success.\n\n"); + printf("Test: write a value..."); + + if (shm_rbuff_write(rb, 1) < 0) + goto error; + + printf("success.\n\n"); + printf("Test: check queue length is 1..."); + + if (shm_rbuff_queued(rb) != 1) + goto error; + + printf("success.\n\n"); + printf("Test: read a value..."); + + if (shm_rbuff_read(rb) != 1) + goto error; + + printf("success.\n\n"); + printf("Test: check queue is empty..."); + + if (shm_rbuff_read(rb) != -EAGAIN) + goto error; + + printf("success.\n\n"); + printf("Test: fill the queue..."); + + for (i = 0; i < SHM_RBUFF_SIZE - 1; ++i) { + if (shm_rbuff_queued(rb) != i) + goto error; + if (shm_rbuff_write(rb, 1) < 0) + goto error; + } + + printf("success.\n\n"); + printf("Test: check queue is full..."); + + if (shm_rbuff_queued(rb) != SHM_RBUFF_SIZE - 1) + goto error; + + printf("success [%zd entries].\n\n", shm_rbuff_queued(rb)); + + printf("Test: check queue is full by writing value..."); + if (!(shm_rbuff_write(rb, 1) < 0)) + goto error; + + printf("success [%zd entries].\n\n", shm_rbuff_queued(rb)); + + /* empty the rbuff */ + while (shm_rbuff_read(rb) >= 0) + ; + + shm_rbuff_destroy(rb); + + return 0; + + error: + /* empty the rbuff */ + while (shm_rbuff_read(rb) >= 0) + ; + + shm_rbuff_destroy(rb); + err: + printf("failed.\n\n"); + return -1; +} diff --git a/src/lib/tests/time_utils_test.c b/src/lib/tests/time_test.c index aacf3ba8..65f896bb 100644 --- a/src/lib/tests/time_utils_test.c +++ b/src/lib/tests/time_test.c @@ -1,10 +1,10 @@ /* - * Ouroboros - Copyright (C) 2016 - 2018 + * Ouroboros - Copyright (C) 2016 - 2024 * * Test of the time utilities * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> + * Dimitri Staessens <dimitri@ouroboros.rocks> + * Sander Vrijders <sander@ouroboros.rocks> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as @@ -22,7 +22,7 @@ #define _POSIX_C_SOURCE 200809L -#include <ouroboros/time_utils.h> +#include <ouroboros/time.h> #include <stdio.h> @@ -66,8 +66,8 @@ static int tv_check(struct timeval * v, return v->tv_sec == sec && v->tv_usec == usec; } -int time_utils_test(int argc, - char ** argv) +int time_test(int argc, + char ** argv) { struct timespec s0; struct timespec s1; diff --git a/src/lib/tests/timerwheel_test.c b/src/lib/tests/timerwheel_test.c deleted file mode 100644 index 0ec98316..00000000 --- a/src/lib/tests/timerwheel_test.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Ouroboros - Copyright (C) 2016 - 2018 - * - * Test of the timer wheel - * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 as - * published by the Free Software Foundation. - * - * 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., http://www.fsf.org/about/contact/. - */ - -#include "timerwheel.c" - -#include <pthread.h> -#include <time.h> -#include <stdlib.h> -#include <stdio.h> - -#define MAX_ELEMENTS 100 -#define MAX_RESOLUTION 10 /* ms */ -#define MAX_ADDITIONS 1000 - -int total; - -int add(void * o) -{ - total += *((int *) o); - return 0; -} - -int timerwheel_test(int argc, char ** argv) -{ - struct timerwheel * tw; - long resolution; - long elements; - struct timespec wait; - - int additions; - - int check_total = 0; - - int i; - int var = 5; - - struct tw_f * f; - - (void) argc; - (void) argv; - - total = 0; - - srand(time(NULL)); - - resolution = rand() % (MAX_RESOLUTION - 1) + 1; - elements = rand() % (MAX_ELEMENTS - 10) + 10; - - tw = timerwheel_create(resolution, resolution * elements); - if (tw == NULL) { - printf("Failed to create timerwheel.\n"); - return -1; - } - - wait.tv_sec = (resolution * elements) / 1000; - wait.tv_nsec = ((resolution * elements) % 1000) * MILLION; - - additions = rand() % MAX_ADDITIONS + 1000; - - for (i = 0; i < additions; ++i) { - int delay = rand() % (resolution * elements); - check_total += var; - f = timerwheel_start(tw, - (void (*)(void *)) add, - (void *) &var, - delay); - if (f == NULL) { - printf("Failed to add function."); - return -1; - } - } - - nanosleep(&wait, NULL); - - timerwheel_move(tw); - - timerwheel_destroy(tw); - - if (total != check_total) { - printf("Totals do not match: %d and %d.\n", total, check_total); - return -1; - } - - return 0; -} |
