From 7b30777429b7a2b083739e66e56cfb93e33367e7 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Fri, 18 Sep 2020 14:51:08 +0200 Subject: [PATCH] tests/ztimer_periodic: initial commit --- tests/ztimer_periodic/Makefile | 7 ++ tests/ztimer_periodic/README.md | 3 + tests/ztimer_periodic/main.c | 94 +++++++++++++++++++++++++++ tests/ztimer_periodic/tests/01-run.py | 18 +++++ 4 files changed, 122 insertions(+) create mode 100644 tests/ztimer_periodic/Makefile create mode 100644 tests/ztimer_periodic/README.md create mode 100644 tests/ztimer_periodic/main.c create mode 100755 tests/ztimer_periodic/tests/01-run.py diff --git a/tests/ztimer_periodic/Makefile b/tests/ztimer_periodic/Makefile new file mode 100644 index 0000000000..e3ee9ea098 --- /dev/null +++ b/tests/ztimer_periodic/Makefile @@ -0,0 +1,7 @@ +DEVELHELP ?= 0 +include ../Makefile.tests_common + +USEMODULE += fmt +USEMODULE += ztimer_usec ztimer_msec + +include $(RIOTBASE)/Makefile.include diff --git a/tests/ztimer_periodic/README.md b/tests/ztimer_periodic/README.md new file mode 100644 index 0000000000..7619d16ee5 --- /dev/null +++ b/tests/ztimer_periodic/README.md @@ -0,0 +1,3 @@ +# Introduction + +This test application does some basic functionality testing of ztimer_periodic. diff --git a/tests/ztimer_periodic/main.c b/tests/ztimer_periodic/main.c new file mode 100644 index 0000000000..ce2fb557c9 --- /dev/null +++ b/tests/ztimer_periodic/main.c @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2020 Kaspar Schleiser + * 2020 Freie Universität Berlin + * 2020 Inria + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup test + * @{ + * + * @file + * @brief ztimer periodic test application + * + * @author Kaspar Schleiser + * + * @} + */ + +#include +#include +#include +#include + +#include "ztimer.h" +#include "ztimer/periodic.h" +#include "fmt.h" +#include "mutex.h" + +static mutex_t _mutex = MUTEX_INIT_LOCKED; + +#define MAX_OFFSET 2 +#define N 5 +#define INTERVAL 100LU + +static uint32_t _times[N]; + +static int callback(void *arg) +{ + (void)arg; + static int count = 0; + + _times[count] = ztimer_now(ZTIMER_MSEC); + + count += 1; + + /* enable this to test underflow behavior */ +#if 0 + if (count == 2) { + ztimer_spin(ZTIMER_MSEC, INTERVAL*2); + } +#endif + + if (count == N) { + mutex_unlock(&_mutex); + } + + return (count == N); +} + +int main(void) +{ + ztimer_periodic_t t; + + ztimer_periodic_init(ZTIMER_MSEC, &t, callback, NULL, INTERVAL); + uint32_t last = ztimer_now(ZTIMER_MSEC); + + ztimer_periodic_start(&t); + + /* wait for periodic to trigger N times */ + mutex_lock(&_mutex); + + int failed = 0; + + for (unsigned i = 0; i < N; i++) { + uint32_t offset = labs((int32_t)(_times[i] - INTERVAL - last)); + printf("i: %u time: %" PRIu32 " offset: %" PRIu32 "\n", + i, _times[i], offset); + if (offset > MAX_OFFSET) { + failed = 1; + } + last = _times[i]; + } + + if (!failed) { + print_str("Test successful.\n"); + } + else { + print_str("Test failed!\n"); + } +} diff --git a/tests/ztimer_periodic/tests/01-run.py b/tests/ztimer_periodic/tests/01-run.py new file mode 100755 index 0000000000..eec0244c60 --- /dev/null +++ b/tests/ztimer_periodic/tests/01-run.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2019 Kaspar Schleiser +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +import sys +from testrunner import run + + +def testfunc(child): + child.expect_exact("Test successful.") + + +if __name__ == "__main__": + sys.exit(run(testfunc))