From 8017cffe2f319fb8bafd2074c842f9f3fa3a8e77 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Thu, 13 Oct 2016 22:12:06 -0400 Subject: [PATCH] increase test coverage --- tests/list.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++- tests/timing.c | 32 +++++++++++++++++-- 2 files changed, 115 insertions(+), 3 deletions(-) diff --git a/tests/list.c b/tests/list.c index 11f7975c1..79543d264 100644 --- a/tests/list.c +++ b/tests/list.c @@ -7,15 +7,99 @@ *********************************************************************************/ #include +#include #include +#include "utils.h" #include "list.h" +static char *nouns[] = { "time", "person", "year", "way", "day", "thing", "man", "world", "life", "hand", "part", "child", "eye", "woman", "place", "work", "week", "case", "point", "government", "company", "number", "group", "problem", "fact" }; + +struct data { + char *tag; + int data; +}; + +Test(list, list_lookup) +{ + struct list l; + + list_init(&l); + + for (int i = 0; i < ARRAY_LEN(nouns); i++) { + struct data *d = malloc(sizeof(struct data)); + + d->tag = nouns[i]; + d->data = i; + + list_push(&l, d); + } + + struct data *found = list_lookup(&l, "woman"); + + cr_assert_eq(found->data, 13); + + list_destroy(&l, NULL, true); +} + +Test(list, list_search) +{ + struct list l; + + list_init(&l); + + /* Fill list */ + for (int i = 0; i < ARRAY_LEN(nouns); i++) + list_push(&l, nouns[i]); + + /* Declare on stack! */ + char positive[] = "woman"; + char negative[] = "dinosaurrier"; + + char *found = (char *) list_search(&l, (cmp_cb_t) strcmp, positive); + + cr_assert_not_null(found); + cr_assert_eq(found, nouns[13]); + cr_assert_str_eq(found, positive); + + char *not_found = (char *) list_search(&l, (cmp_cb_t) strcmp, negative); + + cr_assert_null(not_found); + + list_destroy(&l, NULL, false); +} + +struct content { + int destroyed; +}; + +static void dtor(void *ptr) +{ + struct content *elm = (struct content *) ptr; + + elm->destroyed = 1; +} + +Test(list, destructor) +{ + struct list l; + struct content elm = { .destroyed = 0 }; + + list_init(&l); + list_push(&l, &elm); + + cr_assert_eq(list_length(&l), 1); + + list_destroy(&l, dtor, false); + + cr_assert_eq(elm.destroyed, 1); +} + static int compare(const void *a, const void *b) { return b - a; } -Test(test, basics) +Test(list, basics) { intptr_t i; struct list l; diff --git a/tests/timing.c b/tests/timing.c index 4ca8b18ef..b4ef56813 100644 --- a/tests/timing.c +++ b/tests/timing.c @@ -6,6 +6,7 @@ * Unauthorized copying of this file, via any medium is strictly prohibited. *********************************************************************************/ +#include #include #include "timing.h" @@ -75,7 +76,32 @@ Test(timing, time_to_from_double) cr_assert_float_eq(dbl, ref, 1e-9); } -Test(timing, timerfd_wait_until) { +Test(timing, timerfd_create_rate) +{ + struct timespec start, end; + + double rate = 5, waited; + + int tfd = timerfd_create_rate(rate); + + cr_assert(tfd > 0); + + for (int i = 0; i < 10; i++) { + start = time_now(); + + timerfd_wait(tfd); + + end = time_now(); + waited = time_delta(&start, &end); + + cr_assert_float_eq(waited, 1.0 / rate, 10e-3, "We slept for %f instead of %f secs in round %d", waited, 1.0 / rate, i); + } + + close(tfd); +} + +Test(timing, timerfd_wait_until) +{ int tfd = timerfd_create(CLOCK_REALTIME, 0); cr_assert(tfd > 0); @@ -92,5 +118,7 @@ Test(timing, timerfd_wait_until) { double waited = time_delta(&start, &end); - cr_assert_float_eq(waited, waitfor, 1e-3, "We slept for %f instead of %f secs", waited, waitfor); + cr_assert_float_eq(waited, waitfor, 5e-3, "We slept for %f instead of %f secs", waited, waitfor); + + close(tfd); } \ No newline at end of file