From cc99dee30d2ab4856e05cb2e427388e0bc168d78 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 6 Oct 2016 17:56:55 -0400 Subject: [PATCH] added first non-verified unit-tests --- tests/hist.c | 34 +++++++++++++++++++ tests/list.c | 8 +++++ tests/queue.c | 25 ++++++++++++++ tests/timing.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 tests/hist.c create mode 100644 tests/queue.c create mode 100644 tests/timing.c diff --git a/tests/hist.c b/tests/hist.c new file mode 100644 index 000000000..b5092b29d --- /dev/null +++ b/tests/hist.c @@ -0,0 +1,34 @@ +/** Unit tests for histogram + * + * @author Steffen Vogel + * @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + *********************************************************************************/ + +#include + +#include "hist.h" + +const double test_data[] = {}; + +/* Histogram of test_data with 200 buckets between -100 and 100 */ +const int hist_result[] = {}; + +Test(hist) { + struct hist h; + + hist_create(&h, -100, 100, 1); + + for (int i = 0; i < ARRAY_LEN(test_data); i++) + hist_put(&h, test_data[i]) + + cr_assert_float_eq(hist_mean(&h), 1, 1e-6); + cr_assert_float_eq(hist_var(s&h), 1, 1e-6); + cr_assert_float_eq(hist_stddev(&h), 1, 1e-6); + + for (int i = 0; i < ARRAY_LEN(hist_result); i++) + cr_assert_eq() + + hist_destroy(&h); +} \ No newline at end of file diff --git a/tests/list.c b/tests/list.c index 3b08d7546..11f7975c1 100644 --- a/tests/list.c +++ b/tests/list.c @@ -1,3 +1,11 @@ +/** Unit tests for array-based list + * + * @author Steffen Vogel + * @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + *********************************************************************************/ + #include #include diff --git a/tests/queue.c b/tests/queue.c new file mode 100644 index 000000000..1c02306ba --- /dev/null +++ b/tests/queue.c @@ -0,0 +1,25 @@ +/** Unit tests for MPMC queue + * + * @author Steffen Vogel + * @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + *********************************************************************************/ + +#include + +#include "queue.h" + +Test(queue, singleThreaded) +{ +/* struct queue q; + + queue_init(&q, 100, &memtype_heap); + + srand(1337); + + for (int i = 0; i < 100; i++) + queue_push(&q, &) + + queue_destroy(&q);*/ +} \ No newline at end of file diff --git a/tests/timing.c b/tests/timing.c new file mode 100644 index 000000000..28412a2ac --- /dev/null +++ b/tests/timing.c @@ -0,0 +1,90 @@ +/** Unit tests for time related utlities + * + * @author Steffen Vogel + * @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. + *********************************************************************************/ + +#include + +#include "timing.h" + +Test(time_now) { + struct timespec now1 = time_now(); + struct timespec now2 = time_now(); + + double delta = time_delta(&now1, &now2); + + cr_assert_float_eq(delta, 0, 1e-5, "time_now() shows large variance!"); + cr_assert_gt(delta, 0, "time_now() was reordered!"); +} + +Test(time_diff) { + struct timespec ts1 = { .tv_sec 1, .tv_nsec = 0}; /* Value doesnt matter */ + struct timespec ts2 = { .tv_sec 0, .tv_nsec = 1}; /* Overflow in nano seconds! */ + + struct timespec ts3 = time_diff(&ts1, &ts2); + + /* ts4 == ts2? */ + cr_assert_eq(ts3.tv_sec, 0); + cr_assert_eq(ts3.tv_nsec, 999999999); +} + +Test(time_add) { + struct timespec ts1 = { .tv_sec 1, .tv_nsec = 999999999}; /* Value doesnt matter */ + struct timespec ts2 = { .tv_sec 1, .tv_nsec = 1}; /* Overflow in nano seconds! */ + + struct timespec ts3 = time_add(&ts1, &ts2); + + /* ts4 == ts2? */ + cr_assert_eq(ts3.tv_sec, 2); + cr_assert_eq(ts3.tv_nsec, 0); +} + +Test(time_delta) { + struct timespec ts1 = { .tv_sec 1, .tv_nsec = 123}; /* Value doesnt matter */ + struct timespec ts2 = { .tv_sec 5, .tv_nsec = 246}; /* Overflow in nano seconds! */ + + double = time_delta(&ts1, &ts2); + + cr_assert_float_eq(ts3.tv_sec, 4 + 123e-9, 1e-9); +} + +Test(time_from_double) { + double ref = 1234.56789; + + struct timespec ts = time_from_double(ref); + + cr_assert_eq(ts.tv_sec, 1234); + cr_assert_eq(ts.tv_nsec, 567890000); +} + +Test(time_to_from_double) { + double ref = 1234.56789; + + struct timespec ts = time_from_double(ref); + double dbl = time_to_double(&ts); + + cr_assert_float_eq(dbl, ref, 1e-9); +} + +Test(timer_wait_until) { + int tfd = timer_fd_create(CLOCK_MONOTONIC, 0); + + cr_assert(tfd > 0); + + double waitfor = 1.123456789; + + struct timespec start = time_now(); + struct timespec diff = time_from_double(waitfor); + struct timespec future = time_add(&start, &diff) + + timer_wait_until(tfd, &future); + + struct timespec end = time_now(); + + double waited = time_delta(&end, &start); + + cr_assert_float_eq(waited, waitfor, 1e-3, "We did not wait for %f secs"); +} \ No newline at end of file