From 8de490064101381bc0ae12ceb14872def8c3c131 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Fri, 5 Jan 2018 07:49:39 +0100 Subject: [PATCH] Work on timer unit tests --- test/unit/core/test_timers.c | 104 ++++++++++++++++++++++++++++++----- 1 file changed, 89 insertions(+), 15 deletions(-) diff --git a/test/unit/core/test_timers.c b/test/unit/core/test_timers.c index ab28bd9e..5b0701ef 100644 --- a/test/unit/core/test_timers.c +++ b/test/unit/core/test_timers.c @@ -24,40 +24,114 @@ timers_teardown(void) lwip_sys_now = 0; } +static int fired[3]; static void dummy_handler(void* arg) { - LWIP_UNUSED_ARG(arg); + int index = LWIP_PTR_NUMERIC_CAST(int, arg); + fired[index] = 1; } static void test_timers(void) { - struct sys_timeo** list_head = lwip_sys_timers_get_next_timout(); + /* struct sys_timeo** list_head = lwip_sys_timers_get_next_timout(); */ + + /* check without u32_t wraparound */ lwip_sys_now = 100; - sys_timeout(10, dummy_handler, NULL); + sys_timeout(10, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0)); fail_unless(sys_timeouts_sleeptime() == 10); - sys_timeout(20, dummy_handler, NULL); + sys_timeout(20, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1)); fail_unless(sys_timeouts_sleeptime() == 10); - sys_timeout( 5, dummy_handler, NULL); + sys_timeout( 5, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2)); fail_unless(sys_timeouts_sleeptime() == 5); - sys_untimeout(dummy_handler, NULL); - sys_untimeout(dummy_handler, NULL); - sys_untimeout(dummy_handler, NULL); + /* linked list correctly sorted? */ + /* + fail_unless((*list_head)->time == (u32_t)(lwip_sys_now + 5)); + fail_unless((*list_head)->next->time == (u32_t)(lwip_sys_now + 10)); + fail_unless((*list_head)->next->next->time == (u32_t)(lwip_sys_now + 20)); + */ + + /* check timers expire in correct order */ + memset(&fired, 0, sizeof(fired)); - lwip_sys_now = 0xfffffff0; + lwip_sys_now += 4; + sys_check_timeouts(); + fail_unless(fired[2] == 0); - sys_timeout(10, dummy_handler, NULL); + lwip_sys_now += 1; + sys_check_timeouts(); + fail_unless(fired[2] == 1); + + lwip_sys_now += 4; + sys_check_timeouts(); + fail_unless(fired[0] == 0); + + lwip_sys_now += 1; + sys_check_timeouts(); + fail_unless(fired[0] == 1); + + lwip_sys_now += 9; + sys_check_timeouts(); + fail_unless(fired[1] == 0); + + lwip_sys_now += 1; + sys_check_timeouts(); + fail_unless(fired[1] == 1); + + sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0)); + sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1)); + sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2)); + + /* check u32_t wraparound */ + + lwip_sys_now = 0xfffffff5; + + sys_timeout(10, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0)); fail_unless(sys_timeouts_sleeptime() == 10); - sys_timeout(20, dummy_handler, NULL); + sys_timeout(20, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1)); fail_unless(sys_timeouts_sleeptime() == 10); - sys_timeout( 5, dummy_handler, NULL); + sys_timeout( 5, dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2)); fail_unless(sys_timeouts_sleeptime() == 5); - sys_untimeout(dummy_handler, NULL); - sys_untimeout(dummy_handler, NULL); - sys_untimeout(dummy_handler, NULL); + /* linked list correctly sorted? */ + /* + fail_unless((*list_head)->time == (u32_t)(lwip_sys_now + 5)); + fail_unless((*list_head)->next->time == (u32_t)(lwip_sys_now + 10)); + fail_unless((*list_head)->next->next->time == (u32_t)(lwip_sys_now + 20)); + */ + + /* check timers expire in correct order */ + memset(&fired, 0, sizeof(fired)); + + lwip_sys_now += 4; + sys_check_timeouts(); + fail_unless(fired[2] == 0); + + lwip_sys_now += 1; + sys_check_timeouts(); + fail_unless(fired[2] == 1); + + lwip_sys_now += 4; + sys_check_timeouts(); + fail_unless(fired[0] == 0); + + lwip_sys_now += 1; + sys_check_timeouts(); + fail_unless(fired[0] == 1); + + lwip_sys_now += 9; + sys_check_timeouts(); + fail_unless(fired[1] == 0); + + lwip_sys_now += 1; + sys_check_timeouts(); + fail_unless(fired[1] == 1); + + sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 0)); + sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 1)); + sys_untimeout(dummy_handler, LWIP_PTR_NUMERIC_CAST(void*, 2)); } START_TEST(test_lwip_timers)