Browse Source

tests: provide tests for evtimer

master
Martine Lenders 6 years ago
parent
commit
15732bede6
No known key found for this signature in database
GPG Key ID: 8E97A9FE55F25D62
  1. 13
      tests/evtimer_msg/Makefile
  2. 75
      tests/evtimer_msg/main.c
  3. 36
      tests/evtimer_msg/tests/01-run.py

13
tests/evtimer_msg/Makefile

@ -0,0 +1,13 @@
APPLICATION = evtimer_msg
include ../Makefile.tests_common
BOARD_INSUFFICIENT_MEMORY := nucleo32-f031 nucleo32-f042
USEMODULE += evtimer
include $(RIOTBASE)/Makefile.include
test:
# `testrunner` calls `make term` recursively, results in duplicated `TERMFLAGS`.
# So clears `TERMFLAGS` before run.
TERMFLAGS= tests/01-run.py

75
tests/evtimer_msg/main.c

@ -0,0 +1,75 @@
/*
* Copyright (C) 2017 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.
*/
/**
* @ingroup tests
* @{
*
* @file
* @brief evtimer_msg test application
*
* @author Martine Lenders <m.lenders@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include "evtimer_msg.h"
#include "thread.h"
#include "msg.h"
#include "xtimer.h"
static char worker_stack[THREAD_STACKSIZE_MAIN];
static evtimer_t evtimer;
static evtimer_msg_event_t events[] = {
{ .event = { .offset = 1000 }, .msg = { .content = { .ptr = "supposed to be 1000" } } },
{ .event = { .offset = 1500 }, .msg = { .content = { .ptr = "supposed to be 1500" } } },
{ .event = { .offset = 659 }, .msg = { .content = { .ptr = "supposed to be 659" } } },
{ .event = { .offset = 3954 }, .msg = { .content = { .ptr = "supposed to be 3954" } } },
};
#define NEVENTS ((unsigned)(sizeof(events) / sizeof(evtimer_msg_event_t)))
/* This thread will print the drift to stdout once per second */
void *worker_thread(void *arg)
{
int count = 0;
(void) arg;
while (1) {
char *ctx;
msg_t m;
uint32_t now;
msg_receive(&m);
now = xtimer_now_usec() / US_PER_MS;
ctx = m.content.ptr;
printf("At %6" PRIu32 " ms received msg %i: \"%s\"\n", now, count++, ctx);
}
}
int main(void)
{
uint32_t now = xtimer_now_usec() / US_PER_MS;
evtimer_init_msg(&evtimer);
/* create worker thread */
kernel_pid_t pid = thread_create(worker_stack, sizeof(worker_stack),
THREAD_PRIORITY_MAIN - 1,
THREAD_CREATE_STACKTEST,
worker_thread, NULL, "worker");
printf("Testing generic evtimer (start time = %" PRIu32 " ms)\n", now);
for (unsigned i = 0; i < NEVENTS; i++) {
evtimer_add_msg(&evtimer, &events[i], pid);
}
printf("Are the reception times of all %u msgs close to the supposed values?\n",
NEVENTS);
puts("If yes, the tests were successful");
}

36
tests/evtimer_msg/tests/01-run.py

@ -0,0 +1,36 @@
#!/usr/bin/env python3
# Copyright (C) 2016 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.
from __future__ import print_function
import os
import sys
import time
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
import testrunner
ACCEPTED_ERROR = 20
def testfunc(child):
child.expect(r"Testing generic evtimer \(start time = (\d+) ms\)")
timer_offset = int(child.match.group(1))
child.expect(r"Are the reception times of all (\d+) msgs close to the supposed values?")
numof = int(child.match.group(1))
for i in range(numof):
child.expect(r'At \s*(\d+) ms received msg %i: "supposed to be (\d+)"' % i)
stop = int(time.time() * 1000)
# check if output is correct
exp = int(child.match.group(2)) + timer_offset
assert(int(child.match.group(1)) in range(exp - ACCEPTED_ERROR, exp + ACCEPTED_ERROR + 1))
print(".", end="", flush=True)
print("")
print("All tests successful")
if __name__ == "__main__":
sys.exit(testrunner.run(testfunc, echo=False))
Loading…
Cancel
Save