diff --git a/test/test.c b/test/test.c index 9a1af1725..b603a20b8 100644 --- a/test/test.c +++ b/test/test.c @@ -23,6 +23,7 @@ #include "testrdy.h" #include "testsem.h" #include "testmtx.h" +#include "testcond.h" #include "testmsg.h" #include "testevt.h" #include "testheap.h" @@ -44,6 +45,10 @@ static const struct testcase *tests[] = { &testmtx1, &testmtx2, &testmtx3, +#ifdef CH_USE_CONDVARS + &testcond1, + &testcond2, +#endif #endif #ifdef CH_USE_MESSAGES &testmsg1, diff --git a/test/test.mk b/test/test.mk index e65db3345..9677d21dc 100644 --- a/test/test.mk +++ b/test/test.mk @@ -1,5 +1,5 @@ # List of all the ChibiOS/RT test files. -TESTSRC = ../../test/test.c ../../test/testrdy.c ../../test/testsem.c \ - ../../test/testmtx.c ../../test/testmsg.c ../../test/testevt.c \ - ../../test/testheap.c ../../test/testpools.c ../../test/testdyn.c \ - ../../test/testbmk.c +TESTSRC = ../../test/test.c ../../test/testrdy.c ../../test/testsem.c \ + ../../test/testmtx.c ../../test/testcond.c ../../test/testmsg.c \ + ../../test/testevt.c ../../test/testheap.c ../../test/testpools.c \ + ../../test/testdyn.c ../../test/testbmk.c diff --git a/test/testcond.c b/test/testcond.c new file mode 100644 index 000000000..07c2a441e --- /dev/null +++ b/test/testcond.c @@ -0,0 +1,105 @@ +/* + 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 . +*/ + +#include + +#include "test.h" + +#if defined(CH_USE_CONDVARS) && defined(CH_USE_MUTEXES) + +static Mutex m1; +static CondVar c1; + +static char *cond1_gettest(void) { + + return "CondVar, signal test"; +} + +static void cond1_setup(void) { + + chCondInit(&c1); + chMtxInit(&m1); +} + +static void cond1_teardown(void) { +} + +static msg_t thread1(void *p) { + + chMtxLock(&m1); + chCondWait(&c1); + test_emit_token(*(char *)p); + chMtxUnlock(); + return 0; +} + +static void cond1_execute(void) { + + // Bacause priority inheritance. + tprio_t prio = chThdGetPriority(); + threads[0] = chThdCreate(prio+1, 0, wa[0], STKSIZE, thread1, "E"); + threads[1] = chThdCreate(prio+2, 0, wa[1], STKSIZE, thread1, "D"); + threads[2] = chThdCreate(prio+3, 0, wa[2], STKSIZE, thread1, "C"); + threads[3] = chThdCreate(prio+4, 0, wa[3], STKSIZE, thread1, "B"); + threads[4] = chThdCreate(prio+5, 0, wa[4], STKSIZE, thread1, "A"); + test_assert(prio == chThdGetPriority(), "priority return failure"); + chCondSignal(&c1); + chCondSignal(&c1); + chCondSignal(&c1); + chCondSignal(&c1); + chCondSignal(&c1); + test_wait_threads(); + test_assert_sequence("ABCDE"); +} + +const struct testcase testcond1 = { + cond1_gettest, + cond1_setup, + cond1_teardown, + cond1_execute +}; + +static char *cond2_gettest(void) { + + return "CondVar, broadcast test"; +} + +static void cond2_execute(void) { + + // Bacause priority inheritance. + tprio_t prio = chThdGetPriority(); + threads[0] = chThdCreate(prio+1, 0, wa[0], STKSIZE, thread1, "E"); + threads[1] = chThdCreate(prio+2, 0, wa[1], STKSIZE, thread1, "D"); + threads[2] = chThdCreate(prio+3, 0, wa[2], STKSIZE, thread1, "C"); + threads[3] = chThdCreate(prio+4, 0, wa[3], STKSIZE, thread1, "B"); + threads[4] = chThdCreate(prio+5, 0, wa[4], STKSIZE, thread1, "A"); + test_assert(prio == chThdGetPriority(), "priority return failure"); + chCondBroadcast(&c1); + test_wait_threads(); + test_assert_sequence("ABCDE"); +} + +const struct testcase testcond2 = { + cond2_gettest, + cond1_setup, + cond1_teardown, + cond2_execute +}; + +#endif /* defined(CH_USE_CONDVARS) && defined(CH_USE_MUTEXES) */ diff --git a/test/testcond.h b/test/testcond.h new file mode 100644 index 000000000..8e6f286b1 --- /dev/null +++ b/test/testcond.h @@ -0,0 +1,26 @@ +/* + 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 . +*/ + +#ifndef _TESTCOND_H_ +#define _TESTCOND_H_ + +extern const struct testcase testcond1; +extern const struct testcase testcond2; + +#endif /* _TESTCOND_H_ */