Skip to content

Commit

Permalink
tests/thread_float: improve output and add test script
Browse files Browse the repository at this point in the history
- 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`
  • Loading branch information
maribu committed Sep 28, 2021
1 parent 7313a1c commit 2b50205
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
36 changes: 22 additions & 14 deletions tests/thread_float/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#include <stdio.h>
#include <string.h>

#include "thread.h"
#include "msg.h"
Expand All @@ -44,39 +45,43 @@ 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) {
for (unsigned long i = 0; i < 10000ul; i++) {
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;
}
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) {
Expand All @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions tests/thread_float/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -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 <marian.buschsieweke@ovgu.de>

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))

0 comments on commit 2b50205

Please # to comment.