mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-25 14:33:52 +01:00
tests/event: initial commit of sys/event test application
This commit is contained in:
parent
0e09213e53
commit
27cdd5497e
11
tests/events/Makefile
Normal file
11
tests/events/Makefile
Normal file
@ -0,0 +1,11 @@
|
||||
APPLICATION = event
|
||||
include ../Makefile.tests_common
|
||||
|
||||
FORCE_ASSERTS = 1
|
||||
USEMODULE += event_callback
|
||||
USEMODULE += event_timeout
|
||||
|
||||
test:
|
||||
tests/01-run.py
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
||||
102
tests/events/main.c
Normal file
102
tests/events/main.c
Normal file
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*
|
||||
* 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 event test application
|
||||
*
|
||||
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "thread.h"
|
||||
#include "event.h"
|
||||
#include "event/timeout.h"
|
||||
#include "event/callback.h"
|
||||
|
||||
static unsigned order;
|
||||
static uint32_t before;
|
||||
|
||||
static void callback(event_t *arg);
|
||||
static void custom_callback(event_t *event);
|
||||
static void timed_callback(void *arg);
|
||||
|
||||
|
||||
static event_t event = { .handler = callback };
|
||||
static event_t event2 = { .handler = callback };
|
||||
|
||||
static void callback(event_t *arg)
|
||||
{
|
||||
order++;
|
||||
assert(order == 1);
|
||||
assert(arg == &event);
|
||||
printf("triggered 0x%08x\n", (unsigned)arg);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
event_t super;
|
||||
const char *text;
|
||||
} custom_event_t;
|
||||
|
||||
static custom_event_t custom_event = { .super.handler = custom_callback, .text = "CUSTOM CALLBACK" };
|
||||
static event_callback_t event_callback = EVENT_CALLBACK_INIT(timed_callback, 0x12345678);
|
||||
|
||||
static void custom_callback(event_t *event)
|
||||
{
|
||||
order++;
|
||||
assert(order == 2);
|
||||
assert(event == (event_t *)&custom_event);
|
||||
custom_event_t *custom_event = (custom_event_t *)event;
|
||||
printf("triggered custom event with text: \"%s\"\n", custom_event->text);
|
||||
}
|
||||
|
||||
static void timed_callback(void *arg)
|
||||
{
|
||||
order++;
|
||||
assert(order == 3);
|
||||
assert(arg == event_callback.arg);
|
||||
uint32_t now = xtimer_now_usec();
|
||||
assert((now - before >= 100000LU));
|
||||
printf("triggered timed callback with arg 0x%08x after %" PRIu32 "us\n", (unsigned)arg, now - before);
|
||||
printf("[SUCCESS]\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
puts("[START] event test application.\n");
|
||||
|
||||
event_queue_t queue = { .waiter = (thread_t *)sched_active_thread };
|
||||
printf("posting 0x%08x\n", (unsigned)&event);
|
||||
event_post(&queue, &event);
|
||||
|
||||
printf("posting 0x%08x\n", (unsigned)&event2);
|
||||
event_post(&queue, &event2);
|
||||
printf("canceling 0x%08x\n", (unsigned)&event2);
|
||||
event_cancel(&queue, &event2);
|
||||
|
||||
printf("posting custom event\n");
|
||||
event_post(&queue, (event_t *)&custom_event);
|
||||
|
||||
event_timeout_t event_timeout;
|
||||
|
||||
printf("posting timed callback with timeout 1sec\n");
|
||||
event_timeout_init(&event_timeout, &queue, (event_t *)&event_callback);
|
||||
before = xtimer_now_usec();
|
||||
event_timeout_set(&event_timeout, 100000LU);
|
||||
|
||||
printf("launching event queue\n");
|
||||
event_loop(&queue);
|
||||
|
||||
return 0;
|
||||
}
|
||||
20
tests/events/tests/01-run.py
Executable file
20
tests/events/tests/01-run.py
Executable file
@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
|
||||
# 2017 Sebastian Meiling <s@mlng.net>
|
||||
#
|
||||
# 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 os
|
||||
import sys
|
||||
|
||||
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
|
||||
import testrunner
|
||||
|
||||
def testfunc(child):
|
||||
child.expect_exact(u"[SUCCESS]")
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(testrunner.run(testfunc))
|
||||
Loading…
x
Reference in New Issue
Block a user