2008-12-06 09:02:28 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.
|
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ch.h>
|
|
|
|
|
|
|
|
#include "test.h"
|
|
|
|
|
2009-02-07 19:14:15 +00:00
|
|
|
#if CH_USE_EVENTS
|
2008-12-06 09:02:28 +00:00
|
|
|
|
|
|
|
#define ALLOWED_DELAY MS2ST(5)
|
|
|
|
|
|
|
|
static EventSource es1, es2;
|
|
|
|
|
|
|
|
static char *evt1_gettest(void) {
|
|
|
|
|
2009-04-17 14:53:04 +00:00
|
|
|
return "Events, registration and dispatch";
|
2008-12-06 09:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void evt1_setup(void) {
|
|
|
|
|
|
|
|
chEvtClear(ALL_EVENTS);
|
|
|
|
}
|
|
|
|
|
2009-04-17 14:53:04 +00:00
|
|
|
static void h1(eventid_t id) {test_emit_token('A');}
|
|
|
|
static void h2(eventid_t id) {test_emit_token('B');}
|
|
|
|
static void h3(eventid_t id) {test_emit_token('C');}
|
|
|
|
static const evhandler_t evhndl[] = {h1, h2, h3};
|
|
|
|
|
|
|
|
static void evt1_execute(void) {
|
|
|
|
EventListener el1, el2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Testing chEvtRegisterMask() and chEvtUnregister().
|
|
|
|
*/
|
|
|
|
chEvtInit(&es1);
|
|
|
|
chEvtRegisterMask(&es1, &el1, 1);
|
|
|
|
chEvtRegisterMask(&es1, &el2, 2);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(1, chEvtIsListening(&es1), "no listener");
|
2009-04-17 14:53:04 +00:00
|
|
|
chEvtUnregister(&es1, &el1);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(2, chEvtIsListening(&es1), "no listener");
|
2009-04-17 14:53:04 +00:00
|
|
|
chEvtUnregister(&es1, &el2);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(3, !chEvtIsListening(&es1), "stuck listener");
|
2009-04-17 14:53:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Testing chEvtDispatch().
|
|
|
|
*/
|
|
|
|
chEvtDispatch(evhndl, 7);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_sequence(4, "ABC");
|
2009-04-17 14:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct testcase testevt1 = {
|
|
|
|
evt1_gettest,
|
|
|
|
evt1_setup,
|
|
|
|
NULL,
|
|
|
|
evt1_execute
|
|
|
|
};
|
|
|
|
|
|
|
|
static char *evt2_gettest(void) {
|
|
|
|
|
|
|
|
return "Events, wait and broadcast";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void evt2_setup(void) {
|
|
|
|
|
|
|
|
chEvtClear(ALL_EVENTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static msg_t thread1(void *p) {
|
|
|
|
|
|
|
|
chThdSleepMilliseconds(50);
|
|
|
|
chEvtSignal((Thread *)p, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static msg_t thread2(void *p) {
|
2008-12-06 09:02:28 +00:00
|
|
|
|
|
|
|
chEvtBroadcast(&es1);
|
|
|
|
chThdSleepMilliseconds(50);
|
|
|
|
chEvtBroadcast(&es2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-17 14:53:04 +00:00
|
|
|
static void evt2_execute(void) {
|
2008-12-06 09:02:28 +00:00
|
|
|
eventmask_t m;
|
|
|
|
EventListener el1, el2;
|
|
|
|
systime_t target_time;
|
|
|
|
|
|
|
|
/*
|
2009-04-17 14:53:04 +00:00
|
|
|
* Test on chEvtWaitOne() without wait.
|
2008-12-06 09:02:28 +00:00
|
|
|
*/
|
|
|
|
chEvtPend(5);
|
|
|
|
m = chEvtWaitOne(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(1, m == 1, "single event error");
|
2008-12-06 09:02:28 +00:00
|
|
|
m = chEvtWaitOne(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(2, m == 4, "single event error");
|
2008-12-06 09:02:28 +00:00
|
|
|
m = chEvtClear(0);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(3, m == 0, "stuck event");
|
|
|
|
|
2009-04-17 14:53:04 +00:00
|
|
|
/*
|
|
|
|
* Test on chEvtWaitOne() with wait.
|
|
|
|
*/
|
|
|
|
test_wait_tick();
|
|
|
|
target_time = chTimeNow() + MS2ST(50);
|
|
|
|
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority() - 1,
|
|
|
|
thread1, chThdSelf());
|
|
|
|
m = chEvtWaitOne(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_time_window(4, target_time, target_time + ALLOWED_DELAY);
|
|
|
|
test_assert(5, m == 1, "single event error");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtClear(0);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(6, m == 0, "stuck event");
|
2009-04-17 14:53:04 +00:00
|
|
|
test_wait_threads();
|
2008-12-06 09:02:28 +00:00
|
|
|
|
|
|
|
/*
|
2009-04-17 14:53:04 +00:00
|
|
|
* Test on chEvtWaitAny() without wait.
|
2008-12-06 09:02:28 +00:00
|
|
|
*/
|
|
|
|
chEvtPend(5);
|
|
|
|
m = chEvtWaitAny(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(7, m == 5, "unexpected pending bit");
|
2008-12-06 09:02:28 +00:00
|
|
|
m = chEvtClear(0);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(8, m == 0, "stuck event");
|
2008-12-06 09:02:28 +00:00
|
|
|
|
|
|
|
/*
|
2009-04-17 14:53:04 +00:00
|
|
|
* Test on chEvtWaitAny() with wait.
|
|
|
|
*/
|
|
|
|
test_wait_tick();
|
|
|
|
target_time = chTimeNow() + MS2ST(50);
|
|
|
|
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority() - 1,
|
|
|
|
thread1, chThdSelf());
|
|
|
|
m = chEvtWaitAny(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_time_window(9, target_time, target_time + ALLOWED_DELAY);
|
|
|
|
test_assert(10, m == 1, "single event error");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtClear(0);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(11, m == 0, "stuck event");
|
2009-04-17 14:53:04 +00:00
|
|
|
test_wait_threads();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test on chEvtWaitAll().
|
2008-12-06 09:02:28 +00:00
|
|
|
*/
|
|
|
|
chEvtInit(&es1);
|
|
|
|
chEvtInit(&es2);
|
|
|
|
chEvtRegisterMask(&es1, &el1, 1);
|
|
|
|
chEvtRegisterMask(&es2, &el2, 4);
|
2009-04-17 14:53:04 +00:00
|
|
|
test_wait_tick();
|
2009-03-10 15:31:58 +00:00
|
|
|
target_time = chTimeNow() + MS2ST(50);
|
2009-04-17 14:53:04 +00:00
|
|
|
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority() - 1,
|
|
|
|
thread2, "A");
|
2008-12-06 09:02:28 +00:00
|
|
|
m = chEvtWaitAll(5);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_time_window(12, target_time, target_time + ALLOWED_DELAY);
|
2008-12-06 09:02:28 +00:00
|
|
|
m = chEvtClear(0);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(13, m == 0, "stuck event");
|
2008-12-06 09:02:28 +00:00
|
|
|
test_wait_threads();
|
|
|
|
chEvtUnregister(&es1, &el1);
|
|
|
|
chEvtUnregister(&es2, &el2);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(14, !chEvtIsListening(&es1), "stuck listener");
|
|
|
|
test_assert(15, !chEvtIsListening(&es2), "stuck listener");
|
2008-12-06 09:02:28 +00:00
|
|
|
}
|
|
|
|
|
2009-04-17 14:53:04 +00:00
|
|
|
const struct testcase testevt2 = {
|
|
|
|
evt2_gettest,
|
|
|
|
evt2_setup,
|
2009-02-16 18:22:49 +00:00
|
|
|
NULL,
|
2009-04-17 14:53:04 +00:00
|
|
|
evt2_execute
|
|
|
|
};
|
|
|
|
|
|
|
|
#if CH_USE_EVENTS_TIMEOUT
|
|
|
|
static char *evt3_gettest(void) {
|
|
|
|
|
|
|
|
return "Events, timeouts";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void evt3_setup(void) {
|
|
|
|
|
|
|
|
chEvtClear(ALL_EVENTS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void evt3_execute(void) {
|
|
|
|
eventmask_t m;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tests various timeout situations.
|
|
|
|
*/
|
|
|
|
m = chEvtWaitOneTimeout(ALL_EVENTS, TIME_IMMEDIATE);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(1, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtWaitAnyTimeout(ALL_EVENTS, TIME_IMMEDIATE);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(2, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtWaitAllTimeout(ALL_EVENTS, TIME_IMMEDIATE);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(3, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtWaitOneTimeout(ALL_EVENTS, 10);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(4, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtWaitAnyTimeout(ALL_EVENTS, 10);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(5, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtWaitAllTimeout(ALL_EVENTS, 10);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(6, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct testcase testevt3 = {
|
|
|
|
evt3_gettest,
|
|
|
|
evt3_setup,
|
|
|
|
NULL,
|
|
|
|
evt3_execute
|
2008-12-06 09:02:28 +00:00
|
|
|
};
|
2009-05-01 13:34:51 +00:00
|
|
|
#endif /* CH_USE_EVENTS_TIMEOUT */
|
2009-02-16 18:22:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test sequence for events pattern.
|
|
|
|
*/
|
2009-02-28 10:38:32 +00:00
|
|
|
const struct testcase * const patternevt[] = {
|
2009-02-16 18:22:49 +00:00
|
|
|
#if CH_USE_EVENTS
|
|
|
|
&testevt1,
|
2009-04-17 14:53:04 +00:00
|
|
|
&testevt2,
|
|
|
|
#if CH_USE_EVENTS_TIMEOUT
|
|
|
|
&testevt3,
|
|
|
|
#endif
|
2009-02-16 18:22:49 +00:00
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|
2009-05-01 13:34:51 +00:00
|
|
|
|
|
|
|
#endif /* CH_USE_EVENTS */
|