diff options
author | Dimitri Staessens <[email protected]> | 2019-06-20 22:00:34 +0200 |
---|---|---|
committer | Sander Vrijders <[email protected]> | 2019-06-21 18:47:30 +0200 |
commit | dc6be97bccd86cfbd64586a5fd9faefb673b70a6 (patch) | |
tree | 3b09335a529d099e82fd661a06dfc566b8b905a6 | |
parent | 6a81bd8a4df688f397a8b218f326f4e5ce71f6cd (diff) | |
download | ouroboros-dc6be97bccd86cfbd64586a5fd9faefb673b70a6.tar.gz ouroboros-dc6be97bccd86cfbd64586a5fd9faefb673b70a6.zip |
lib: Add tests for the shm_rbuff
This adds some tests for the shm_rbuff after some reports that the
queue length would be erroneously reported as 0 when the rbuff was
full. The test passes for the reported case.
Signed-off-by: Dimitri Staessens <[email protected]>
Signed-off-by: Sander Vrijders <[email protected]>
-rw-r--r-- | src/lib/tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/lib/tests/shm_rbuff_test.c | 105 |
2 files changed, 106 insertions, 0 deletions
diff --git a/src/lib/tests/CMakeLists.txt b/src/lib/tests/CMakeLists.txt index cc51c203..c887626a 100644 --- a/src/lib/tests/CMakeLists.txt +++ b/src/lib/tests/CMakeLists.txt @@ -9,6 +9,7 @@ create_test_sourcelist(${PARENT_DIR}_tests test_suite.c hashtable_test.c md5_test.c sha3_test.c + shm_rbuff_test.c time_utils_test.c ) diff --git a/src/lib/tests/shm_rbuff_test.c b/src/lib/tests/shm_rbuff_test.c new file mode 100644 index 00000000..df29d48f --- /dev/null +++ b/src/lib/tests/shm_rbuff_test.c @@ -0,0 +1,105 @@ +/* + * Ouroboros - Copyright (C) 2016 - 2019 + * + * Test of the shm_rbuff + * + * Dimitri Staessens <[email protected]> + * Sander Vrijders <[email protected]> + * + * 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)); + + shm_rbuff_destroy(rb); + + return 0; + + error: + shm_rbuff_destroy(rb); + err: + printf("failed.\n\n"); + return -1; +} |