From d86c1418422392ca913874d56e6c07fe86b2c0b6 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Wed, 13 Jul 2016 00:53:14 +0200 Subject: [PATCH] core, tests: adapt to changed clist function names --- core/include/clist.h | 2 +- core/sched.c | 4 +- core/thread.c | 2 +- tests/unittests/tests-core/tests-core-clist.c | 205 ++++++++++++++++-- 4 files changed, 192 insertions(+), 21 deletions(-) diff --git a/core/include/clist.h b/core/include/clist.h index b02d06adcf..d2da84b31d 100644 --- a/core/include/clist.h +++ b/core/include/clist.h @@ -225,7 +225,7 @@ static inline clist_node_t *clist_rpop(clist_node_t *list) if (list->next) { list_node_t *last = list->next; while (list->next->next != last) { - clist_advance(list); + clist_lpoprpush(list); } return clist_lpop(list); } diff --git a/core/sched.c b/core/sched.c index 151649aad2..f346152480 100644 --- a/core/sched.c +++ b/core/sched.c @@ -133,7 +133,7 @@ void sched_set_status(thread_t *process, unsigned int status) if (!(process->status >= STATUS_ON_RUNQUEUE)) { DEBUG("sched_set_status: adding thread %" PRIkernel_pid " to runqueue %" PRIu16 ".\n", process->pid, process->priority); - clist_insert(&sched_runqueues[process->priority], &(process->rq_entry)); + clist_rpush(&sched_runqueues[process->priority], &(process->rq_entry)); runqueue_bitcache |= 1 << process->priority; } } @@ -141,7 +141,7 @@ void sched_set_status(thread_t *process, unsigned int status) if (process->status >= STATUS_ON_RUNQUEUE) { DEBUG("sched_set_status: removing thread %" PRIkernel_pid " to runqueue %" PRIu16 ".\n", process->pid, process->priority); - clist_remove_head(&sched_runqueues[process->priority]); + clist_lpop(&sched_runqueues[process->priority]); if (!sched_runqueues[process->priority].next) { runqueue_bitcache &= ~(1 << process->priority); diff --git a/core/thread.c b/core/thread.c index f1ce96b06e..ca50b3e765 100644 --- a/core/thread.c +++ b/core/thread.c @@ -98,7 +98,7 @@ void thread_yield(void) unsigned old_state = irq_disable(); thread_t *me = (thread_t *)sched_active_thread; if (me->status >= STATUS_ON_RUNQUEUE) { - clist_advance(&sched_runqueues[me->priority]); + clist_lpoprpush(&sched_runqueues[me->priority]); } irq_restore(old_state); diff --git a/tests/unittests/tests-core/tests-core-clist.c b/tests/unittests/tests-core/tests-core-clist.c index d9ec7e42fb..21271ef6d2 100644 --- a/tests/unittests/tests-core/tests-core-clist.c +++ b/tests/unittests/tests-core/tests-core-clist.c @@ -22,15 +22,15 @@ static list_node_t test_clist; static void set_up(void) { memset(tests_clist_buf, 0, sizeof(tests_clist_buf)); + test_clist.next = NULL; } -static void test_clist_add_one(void) +static void test_clist_rpush(void) { list_node_t *elem = &(tests_clist_buf[0]); list_node_t *list = &test_clist; - list->next = NULL; - clist_insert(list, elem); + clist_rpush(list, elem); TEST_ASSERT_NOT_NULL(list->next); TEST_ASSERT(list->next->next == list->next); @@ -39,29 +39,130 @@ static void test_clist_add_one(void) static void test_clist_add_two(void) { list_node_t *list = &test_clist; - list->next = NULL; list_node_t *elem = &(tests_clist_buf[1]); - test_clist_add_one(); + test_clist_rpush(); - clist_insert(list, elem); + clist_rpush(list, elem); TEST_ASSERT_NOT_NULL(list->next); TEST_ASSERT(list->next == elem); TEST_ASSERT(list->next->next->next == list->next); } -static void test_clist_remove_head(void) +static void test_clist_add_three(void) +{ + list_node_t *list = &test_clist; + + for (int i = 0; i < 3; i++) { + clist_rpush(list, &(tests_clist_buf[i])); + } + + TEST_ASSERT_NOT_NULL(list->next); + TEST_ASSERT(list->next == &(tests_clist_buf[2])); + TEST_ASSERT(list->next->next == &(tests_clist_buf[0])); + TEST_ASSERT(list->next->next->next == &(tests_clist_buf[1])); + TEST_ASSERT(list->next->next->next->next == &(tests_clist_buf[2])); +} + +static void test_clist_find(void) +{ + list_node_t *list = &test_clist; + + test_clist_add_three(); + + for (int i = 0; i < 3; i++) { + TEST_ASSERT(clist_find(list, &(tests_clist_buf[i])) == &(tests_clist_buf[i])); + } + + TEST_ASSERT_NULL(clist_find(list, &(tests_clist_buf[3]))); +} + +static void test_clist_find_before(void) +{ + list_node_t *list = &test_clist; + + test_clist_add_three(); + + TEST_ASSERT(clist_find_before(list, &(tests_clist_buf[0])) == &(tests_clist_buf[2])); + + for (int i = 1; i < 3; i++) { + TEST_ASSERT(clist_find_before(list, &(tests_clist_buf[i])) == &(tests_clist_buf[i-1])); + } + + TEST_ASSERT_NULL(clist_find_before(list, &(tests_clist_buf[3]))); +} + +static void test_clist_remove(void) +{ + list_node_t *list = &test_clist; + + for (int i = 0; i < 3; i++) { + set_up(); + test_clist_add_three(); + clist_remove(list, &(tests_clist_buf[i])); + + for (int j = 0; j < 3; j++) { + if (i == j) { + TEST_ASSERT_NULL(clist_find(list, &(tests_clist_buf[j]))); + } + else { + TEST_ASSERT(clist_find(list, &(tests_clist_buf[j])) == &(tests_clist_buf[j])); + } + } + } + + /* list now contains 0, 1 */ + TEST_ASSERT(list->next == &(tests_clist_buf[1])); + TEST_ASSERT(list->next->next == &(tests_clist_buf[0])); + + clist_remove(list, &(tests_clist_buf[1])); + TEST_ASSERT(list->next == &(tests_clist_buf[0])); + TEST_ASSERT(list->next->next == &(tests_clist_buf[0])); + + clist_remove(list, &(tests_clist_buf[0])); + TEST_ASSERT_NULL(list->next); +} + +static void test_clist_lpop(void) +{ + list_node_t *list = &test_clist; + + test_clist_add_three(); + + TEST_ASSERT(clist_lpop(list) == &tests_clist_buf[0]); + TEST_ASSERT_NOT_NULL(list->next); + + TEST_ASSERT(clist_lpop(list) == &tests_clist_buf[1]); + TEST_ASSERT_NOT_NULL(list->next); + + TEST_ASSERT(clist_lpop(list) == &tests_clist_buf[2]); + TEST_ASSERT_NULL(list->next); + TEST_ASSERT_NULL(clist_lpop(list)); +} + +static void test_clist_lpush(void) +{ + list_node_t *list = &test_clist; + + test_clist_add_two(); + clist_lpush(list, &tests_clist_buf[2]); + + TEST_ASSERT_NOT_NULL(list->next); + TEST_ASSERT(list->next->next == &tests_clist_buf[2]); +} + +static void test_clist_rpop(void) { list_node_t *list = &test_clist; test_clist_add_two(); - clist_remove_head(list); + clist_rpop(list); TEST_ASSERT_NOT_NULL(list->next); - TEST_ASSERT(list->next->next == &tests_clist_buf[1]); + TEST_ASSERT(list->next->next == &tests_clist_buf[0]); } static void test_clist_remove_two(void) @@ -70,36 +171,106 @@ static void test_clist_remove_two(void) test_clist_add_two(); - clist_remove_head(list); - clist_remove_head(list); + clist_lpop(list); + clist_lpop(list); TEST_ASSERT_NULL(list->next); } -static void test_clist_advance(void) +static void test_clist_lpoprpush(void) { list_node_t *list = &test_clist; list->next = NULL; test_clist_add_two(); - clist_advance(list); + clist_lpoprpush(list); TEST_ASSERT(list->next->next == &tests_clist_buf[1]); - clist_advance(list); + clist_lpoprpush(list); TEST_ASSERT(list->next->next == &tests_clist_buf[0]); } +static int _foreach_called; +static int _foreach_visited[TEST_CLIST_LEN]; +static int _foreach_abort_after = TEST_CLIST_LEN/2; + +static void _foreach_test(clist_node_t *node) +{ + TEST_ASSERT(node == &tests_clist_buf[_foreach_called]); + + for (int i = 0; i < TEST_CLIST_LEN; i++) { + if (node == &tests_clist_buf[i]) { + _foreach_visited[i]++; + break; + } + } + + for (int i = 0; i < TEST_CLIST_LEN; i++) { + if (i <= _foreach_called) { + TEST_ASSERT(_foreach_visited[i] == 1); + } + else { + TEST_ASSERT(_foreach_visited[i] == 0); + } + } + + _foreach_called++; +} + +/* embunit test macros only work within void returning functions, so this + * trampoline function is needed */ +static int _foreach_test_trampoline(clist_node_t *node) +{ + _foreach_test(node); + if (_foreach_called == _foreach_abort_after) { + return 1; + } + else { + return 0; + } +} + +static void test_clist_foreach(void) +{ + list_node_t *list = &test_clist; + + for (int i = 0; i < TEST_CLIST_LEN; i++) { + clist_rpush(list, &tests_clist_buf[i]); + } + + clist_foreach(list, _foreach_test_trampoline); + + TEST_ASSERT(_foreach_called == _foreach_abort_after); + + _foreach_called = 0; + for (int i = 0; i < TEST_CLIST_LEN; i++) { + _foreach_visited[i] = 0; + } + + _foreach_abort_after = (TEST_CLIST_LEN + 1); + clist_foreach(list, _foreach_test_trampoline); + + TEST_ASSERT(_foreach_called == TEST_CLIST_LEN); +} + Test *tests_core_clist_tests(void) { EMB_UNIT_TESTFIXTURES(fixtures) { - new_TestFixture(test_clist_add_one), + new_TestFixture(test_clist_rpush), new_TestFixture(test_clist_add_two), - new_TestFixture(test_clist_remove_head), + new_TestFixture(test_clist_lpop), + new_TestFixture(test_clist_rpop), + new_TestFixture(test_clist_lpush), new_TestFixture(test_clist_remove_two), - new_TestFixture(test_clist_advance), + new_TestFixture(test_clist_add_three), + new_TestFixture(test_clist_find), + new_TestFixture(test_clist_find_before), + new_TestFixture(test_clist_remove), + new_TestFixture(test_clist_lpoprpush), + new_TestFixture(test_clist_foreach), }; EMB_UNIT_TESTCALLER(core_clist_tests, set_up, NULL,