From 9c8f4e43fa31fc64ca159ecf74b8f48fe42d7ebb Mon Sep 17 00:00:00 2001 From: kkrentz Date: Wed, 28 Sep 2022 17:47:59 +0200 Subject: [PATCH] etimer: Regression tests --- os/sys/etimer.c | 14 +++ os/sys/etimer.h | 6 ++ tests/08-native-runs/16-etimer.sh | 3 + tests/08-native-runs/16-etimer/Makefile | 9 ++ tests/08-native-runs/16-etimer/test-etimer.c | 99 ++++++++++++++++++++ tests/08-native-runs/Makefile | 3 +- 6 files changed, 133 insertions(+), 1 deletion(-) create mode 100755 tests/08-native-runs/16-etimer.sh create mode 100644 tests/08-native-runs/16-etimer/Makefile create mode 100644 tests/08-native-runs/16-etimer/test-etimer.c diff --git a/os/sys/etimer.c b/os/sys/etimer.c index b25aa967e77..95b4d40f8b4 100644 --- a/os/sys/etimer.c +++ b/os/sys/etimer.c @@ -219,4 +219,18 @@ etimer_stop(struct etimer *et) et->p = PROCESS_NONE; } /*---------------------------------------------------------------------------*/ +bool +etimer_check_ordering(void) +{ + struct etimer *current = next_etimer; + while(current && current->next) { + if(CLOCK_LT(etimer_expiration_time(current->next), + etimer_expiration_time(current))) { + return false; + } + current = current->next; + } + return true; +} +/*---------------------------------------------------------------------------*/ /** @} */ diff --git a/os/sys/etimer.h b/os/sys/etimer.h index ebdea76c693..29c2df58c6f 100644 --- a/os/sys/etimer.h +++ b/os/sys/etimer.h @@ -64,6 +64,7 @@ #define ETIMER_H_ #include "contiki.h" +#include #include #include @@ -253,6 +254,11 @@ int etimer_pending(void); */ clock_time_t etimer_next_expiration_time(void); +/** + * \brief Check if all etimers are correctly ordered by expiration time. + * \return True if the ordering is correct and false otherwise. + */ +bool etimer_check_ordering(void); /** @} */ diff --git a/tests/08-native-runs/16-etimer.sh b/tests/08-native-runs/16-etimer.sh new file mode 100755 index 00000000000..c1165988cd4 --- /dev/null +++ b/tests/08-native-runs/16-etimer.sh @@ -0,0 +1,3 @@ +#!/bin/sh -e + +./run-one.sh 16-etimer diff --git a/tests/08-native-runs/16-etimer/Makefile b/tests/08-native-runs/16-etimer/Makefile new file mode 100644 index 00000000000..ef710a41f6f --- /dev/null +++ b/tests/08-native-runs/16-etimer/Makefile @@ -0,0 +1,9 @@ +CONTIKI_PROJECT = test-etimer +all: $(CONTIKI_PROJECT) + +TARGET = native + +MODULES += os/services/unit-test + +CONTIKI = ../../.. +include $(CONTIKI)/Makefile.include diff --git a/tests/08-native-runs/16-etimer/test-etimer.c b/tests/08-native-runs/16-etimer/test-etimer.c new file mode 100644 index 00000000000..5a8b1da5181 --- /dev/null +++ b/tests/08-native-runs/16-etimer/test-etimer.c @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2021, Uppsala universitet. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "contiki.h" +#include "unit-test.h" +#include + +PROCESS(test_process, "test"); +AUTOSTART_PROCESSES(&test_process); + +/*---------------------------------------------------------------------------*/ +UNIT_TEST_REGISTER(expiration, "expiration"); +UNIT_TEST(expiration) +{ + struct etimer et; + + UNIT_TEST_BEGIN(); + + etimer_set(&et, CLOCK_SECOND); + UNIT_TEST_ASSERT(etimer_pending()); + UNIT_TEST_ASSERT( + etimer_start_time(&et) + CLOCK_SECOND == etimer_expiration_time(&et)); + etimer_stop(&et); + UNIT_TEST_ASSERT(etimer_expired(&et)); + + UNIT_TEST_END(); +} +/*---------------------------------------------------------------------------*/ +UNIT_TEST_REGISTER(ordering, "ordering"); +UNIT_TEST(ordering) +{ + struct etimer et[3]; + + UNIT_TEST_BEGIN(); + + UNIT_TEST_ASSERT(etimer_check_ordering()); + etimer_set(&et[0], CLOCK_SECOND); + etimer_set(&et[1], 3 * CLOCK_SECOND); + etimer_set(&et[2], 2 * CLOCK_SECOND); + UNIT_TEST_ASSERT(etimer_check_ordering()); + etimer_stop(&et[0]); + etimer_stop(&et[2]); + UNIT_TEST_ASSERT(etimer_check_ordering()); + etimer_reset(&et[0]); + etimer_restart(&et[2]); + UNIT_TEST_ASSERT(etimer_check_ordering()); + + UNIT_TEST_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(test_process, ev, data) +{ + PROCESS_BEGIN(); + + printf("Run unit-test\n"); + printf("---\n"); + + UNIT_TEST_RUN(expiration); + UNIT_TEST_RUN(ordering); + + if(!UNIT_TEST_PASSED(expiration) + || !UNIT_TEST_PASSED(ordering)) { + printf("=check-me= FAILED\n"); + printf("---\n"); + } + + printf("=check-me= DONE\n"); + printf("---\n"); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ diff --git a/tests/08-native-runs/Makefile b/tests/08-native-runs/Makefile index 2c1b03b2346..bec1a671c0d 100644 --- a/tests/08-native-runs/Makefile +++ b/tests/08-native-runs/Makefile @@ -18,7 +18,8 @@ tests/08-native-runs/11-aes-ccm/native:./11-aes-ccm.sh \ tests/08-native-runs/12-heapmem/native:./12-heapmem.sh \ tests/08-native-runs/13-coffee/native:./13-coffee.sh \ tests/08-native-runs/14-sha-256/native:./14-sha-256.sh \ -tests/08-native-runs/15-ieee802154-security/native:./15-ieee802154-security.sh +tests/08-native-runs/15-ieee802154-security/native:./15-ieee802154-security.sh \ +tests/08-native-runs/16-etimer/native:./16-etimer.sh include ../Makefile.compile-test