From 027426793cc7b9fd4e721d436722284e110767bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Sat, 23 Mar 2019 14:20:18 +0100 Subject: [PATCH] test_utils_interactive_sync: add a helper for synchronizing tests Add an implementation that waits for 's' to print 'START' and return. If 'r' is given is prints 'READY' to allow querying for state. The help and answered string have to be different to not match the other. Using puts/getchar was smaller than using `stdio_read/stdio_write` on the example I tested with `esp32`. --- sys/Makefile | 3 ++ sys/Makefile.dep | 2 + sys/include/test_utils/interactive_sync.h | 42 +++++++++++++++++++ sys/test_utils/Makefile.dep | 3 ++ sys/test_utils/interactive_sync/Makefile | 3 ++ .../interactive_sync/interactive_sync.c | 20 +++++++++ 6 files changed, 73 insertions(+) create mode 100644 sys/include/test_utils/interactive_sync.h create mode 100644 sys/test_utils/Makefile.dep create mode 100644 sys/test_utils/interactive_sync/Makefile create mode 100644 sys/test_utils/interactive_sync/interactive_sync.c diff --git a/sys/Makefile b/sys/Makefile index 058697f249..1926dabe36 100644 --- a/sys/Makefile +++ b/sys/Makefile @@ -25,6 +25,9 @@ endif ifneq (,$(filter shell_commands,$(USEMODULE))) DIRS += shell/commands endif +ifneq (,$(filter test_utils_interactive_sync,$(USEMODULE))) + DIRS += test_utils/interactive_sync +endif ifneq (,$(filter net_help,$(USEMODULE))) DIRS += net/crosslayer/net_help endif diff --git a/sys/Makefile.dep b/sys/Makefile.dep index f9afc587e3..dcddeac2bc 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -9,3 +9,5 @@ endif ifneq (,$(filter prng_fortuna,$(USEMODULE))) CFLAGS += -DCRYPTO_AES endif + +include $(RIOTBASE)/sys/test_utils/Makefile.dep diff --git a/sys/include/test_utils/interactive_sync.h b/sys/include/test_utils/interactive_sync.h new file mode 100644 index 0000000000..b27a571f2d --- /dev/null +++ b/sys/include/test_utils/interactive_sync.h @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2019 Freie Universität Berlin + * + * 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. + */ + +/** + * @defgroup test_utils_interactive_sync Test interactive synchronization + * @ingroup sys + * @brief Utility function for synchronizing before a test + * + * @{ + * @file + * @brief Synchronization for normally non interactive tests + * + * @author Gaëtan Harter + */ + +#ifndef TEST_UTILS_INTERACTIVE_SYNC_H +#define TEST_UTILS_INTERACTIVE_SYNC_H + + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Wait for the tester to start test + * + * @details Wait for a 's' character to return + * + */ +void test_utils_interactive_sync(void); + + +#ifdef __cplusplus +} +#endif +#endif /* TEST_UTILS_INTERACTIVE_SYNC_H */ +/** @} */ diff --git a/sys/test_utils/Makefile.dep b/sys/test_utils/Makefile.dep new file mode 100644 index 0000000000..4bafa543b8 --- /dev/null +++ b/sys/test_utils/Makefile.dep @@ -0,0 +1,3 @@ +ifneq (,$(filter test_utils_interactive_sync,$(USEMODULE))) + USEMODULE += stdin +endif diff --git a/sys/test_utils/interactive_sync/Makefile b/sys/test_utils/interactive_sync/Makefile new file mode 100644 index 0000000000..e22cec26b0 --- /dev/null +++ b/sys/test_utils/interactive_sync/Makefile @@ -0,0 +1,3 @@ +MODULE = test_utils_interactive_sync + +include $(RIOTBASE)/Makefile.base diff --git a/sys/test_utils/interactive_sync/interactive_sync.c b/sys/test_utils/interactive_sync/interactive_sync.c new file mode 100644 index 0000000000..6dba6ed425 --- /dev/null +++ b/sys/test_utils/interactive_sync/interactive_sync.c @@ -0,0 +1,20 @@ +#include +#include "test_utils/interactive_sync.h" + +void test_utils_interactive_sync(void) +{ + char c = '\0'; /* Print help on first loop */ + do { + if (c == 'r') { + /* This one should have a different case than the help message + * otherwise we match it when using 'expect' */ + puts("READY"); + } + else if (c != '\n' && c != '\r') { + puts("Help: Press s to start test, r to print it is ready"); + } + c = getchar(); + } while (c != 's'); + + puts("START"); +}