From 2b50205edc3c0b6c2bc0893b74834da47541edc3 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 28 Sep 2021 12:27:50 +0200 Subject: [PATCH] tests/thread_float: improve output and add test script - Give the three threads the names "t1", "t2", and "t3" and print them on console, instead of the process ID. This makes interpretation of the output easier, as the process IDs depend e.g. on whether a given platforms requires an idle thread or not. - Do not use the thread ID in the calculation, but the number at the end of the thread name. This will result in the number printed only depending on the precision of the (software) FPU and the printf() implementation, and not on which threads are created in which order (including the idle thread) - Add a script to support running `make test` --- tests/thread_float/main.c | 36 ++++++++++++++++++------------ tests/thread_float/tests/01-run.py | 34 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 14 deletions(-) create mode 100755 tests/thread_float/tests/01-run.py diff --git a/tests/thread_float/main.c b/tests/thread_float/main.c index 693e127dfafe8..a8712222c8bb5 100644 --- a/tests/thread_float/main.c +++ b/tests/thread_float/main.c @@ -19,6 +19,7 @@ */ #include +#include #include "thread.h" #include "msg.h" @@ -44,15 +45,17 @@ static void timer_cb(void *arg) xtimer_set(&timer, OFFSET); } -static void *thread1(void *arg) +static void *thread1(void *_arg) { - (void) arg; - + const char *arg = _arg; float f, init; - printf("THREAD %" PRIkernel_pid " start\n", thread_getpid()); + mutex_lock(&lock); + printf("THREAD %s start\n", arg); + mutex_unlock(&lock); - init = 1.0 * thread_getpid(); + /* Use number at end of thread name, e.g. 3 for "t3", to seed the calculation */ + init = 1.0 * (arg[strlen(arg) - 1] - '0'); f = init; while (1) { @@ -60,7 +63,7 @@ static void *thread1(void *arg) f = f + 1.0 / f; } mutex_lock(&lock); - printf("T(%" PRIkernel_pid "): %f\n", thread_getpid(), (double)f); + printf("%s: %f\n", arg, (double)f); mutex_unlock(&lock); init += 1.0; f = init; @@ -68,15 +71,17 @@ static void *thread1(void *arg) return NULL; } -static void *thread2(void *arg) +static void *thread2(void *_arg) { - (void) arg; - + const char *arg = _arg; float f, init; - printf("THREAD %" PRIkernel_pid " start\n", thread_getpid()); + mutex_lock(&lock); + printf("THREAD %s start\n", arg); + mutex_unlock(&lock); - init = 1.0 * thread_getpid(); + /* Use number at end of thread name, e.g. 3 for "t3", to seed the calculation */ + init = 1.0 * (arg[strlen(arg) - 1] - '0'); f = init; while (1) { @@ -91,15 +96,18 @@ static void *thread2(void *arg) int main(void) { + const char *t1_name = "t1"; + const char *t2_name = "t2"; + const char *t3_name = "t3"; p1 = thread_create(t1_stack, sizeof(t1_stack), THREAD_PRIORITY_MAIN + 1, THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST, - thread1, NULL, "nr1"); + thread1, (void *)t1_name, t1_name); p2 = thread_create(t2_stack, sizeof(t2_stack), THREAD_PRIORITY_MAIN + 1, THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST, - thread2, NULL, "nr2"); + thread2, (void *)t2_name, t2_name); p3 = thread_create(t3_stack, sizeof(t3_stack), THREAD_PRIORITY_MAIN + 1, THREAD_CREATE_WOUT_YIELD | THREAD_CREATE_STACKTEST, - thread1, NULL, "nr3"); + thread1, (void *)t3_name, t3_name); puts("THREADS CREATED\n"); timer.callback = timer_cb; diff --git a/tests/thread_float/tests/01-run.py b/tests/thread_float/tests/01-run.py new file mode 100755 index 0000000000000..d0983a981aac1 --- /dev/null +++ b/tests/thread_float/tests/01-run.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2021 Otto-von-Guericke-Universität Magdeburg +# +# 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. + +# @author Marian Buschsieweke + +import sys +from testrunner import run + + +def testfunc(child): + child.expect("THREADS CREATED") + child.expect("") + child.expect("THREAD t1 start") + child.expect("THREAD t2 start") + child.expect("THREAD t3 start") + for _ in range(10): + child.expect(r"t(\d): (\d\d\d).\d+") + first_thread = int(child.match.group(1)) + assert 141 == int(child.match.group(2)) + assert (first_thread == 1) or (first_thread == 3) + child.expect(r"t(\d): (\d\d\d).\d+") + second_thread = int(child.match.group(1)) + assert 141 == int(child.match.group(2)) + assert (second_thread == 1) or (second_thread == 3) + assert first_thread != second_thread + + +if __name__ == "__main__": + sys.exit(run(testfunc))