aboutsummaryrefslogtreecommitdiffstats
path: root/test/testthd.c
diff options
context:
space:
mode:
authorgdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2009-04-13 08:15:51 +0000
committergdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4>2009-04-13 08:15:51 +0000
commitf140302b0b3e9f7c84fede1120e3bb15c2c4322a (patch)
tree86ff3985fd57c65923a911141eea0634b6affd4d /test/testthd.c
parent90c431b280ac0188980b43a42b11191ca2800fa8 (diff)
downloadChibiOS-f140302b0b3e9f7c84fede1120e3bb15c2c4322a.tar.gz
ChibiOS-f140302b0b3e9f7c84fede1120e3bb15c2c4322a.tar.bz2
ChibiOS-f140302b0b3e9f7c84fede1120e3bb15c2c4322a.zip
97.92% code coverage for threads.
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@899 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'test/testthd.c')
-rw-r--r--test/testthd.c145
1 files changed, 145 insertions, 0 deletions
diff --git a/test/testthd.c b/test/testthd.c
new file mode 100644
index 000000000..92f0bfe9f
--- /dev/null
+++ b/test/testthd.c
@@ -0,0 +1,145 @@
+/*
+ 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"
+
+static msg_t thread(void *p) {
+
+ test_emit_token(*(char *)p);
+ return 0;
+}
+
+static char *thd1_gettest(void) {
+
+ return "Threads, enqueuing test #1";
+}
+
+static void thd1_execute(void) {
+
+ threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority()-5, thread, "E");
+ threads[1] = chThdCreateStatic(wa[1], WA_SIZE, chThdGetPriority()-4, thread, "D");
+ threads[2] = chThdCreateStatic(wa[2], WA_SIZE, chThdGetPriority()-3, thread, "C");
+ threads[3] = chThdCreateStatic(wa[3], WA_SIZE, chThdGetPriority()-2, thread, "B");
+ threads[4] = chThdCreateStatic(wa[4], WA_SIZE, chThdGetPriority()-1, thread, "A");
+ test_wait_threads();
+ test_assert_sequence("ABCDE");
+}
+
+const struct testcase testthd1 = {
+ thd1_gettest,
+ NULL,
+ NULL,
+ thd1_execute
+};
+
+static char *thd2_gettest(void) {
+
+ return "Threads, enqueuing test #2";
+}
+
+static void thd2_execute(void) {
+
+ threads[1] = chThdCreateStatic(wa[1], WA_SIZE, chThdGetPriority()-4, thread, "D");
+ threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority()-5, thread, "E");
+ threads[4] = chThdCreateStatic(wa[4], WA_SIZE, chThdGetPriority()-1, thread, "A");
+ threads[3] = chThdCreateStatic(wa[3], WA_SIZE, chThdGetPriority()-2, thread, "B");
+ threads[2] = chThdCreateStatic(wa[2], WA_SIZE, chThdGetPriority()-3, thread, "C");
+ test_wait_threads();
+ test_assert_sequence("ABCDE");
+}
+
+const struct testcase testthd2 = {
+ thd2_gettest,
+ NULL,
+ NULL,
+ thd2_execute
+};
+
+static char *thd3_gettest(void) {
+
+ return "Threads, priority change";
+}
+
+static void thd3_execute(void) {
+ tprio_t prio, p1;
+
+ prio = chThdGetPriority();
+ p1 = chThdSetPriority(prio + 1);
+ test_assert(p1 == prio, "#1");
+ test_assert(chThdGetPriority() == prio + 1, "#2");
+ p1 = chThdSetPriority(p1);
+ test_assert(p1 == prio + 1, "#3");
+ test_assert(chThdGetPriority() == prio, "#4");
+}
+
+const struct testcase testthd3 = {
+ thd3_gettest,
+ NULL,
+ NULL,
+ thd3_execute
+};
+
+static char *thd4_gettest(void) {
+
+ return "Threads, delays";
+}
+
+static void thd4_execute(void) {
+ systime_t time;
+
+ /* Timeouts in microseconds.*/
+ time = chTimeNow();
+ chThdSleepMicroseconds(100000);
+ test_assert(chTimeIsWithin(time + US2ST(100000), time + US2ST(100000) + 1), "#1");
+
+ /* Timeouts in milliseconds.*/
+ time = chTimeNow();
+ chThdSleepMilliseconds(100);
+ test_assert(chTimeIsWithin(time + MS2ST(100), time + MS2ST(100) + 1), "#2");
+
+ /* Timeouts in seconds.*/
+ time = chTimeNow();
+ chThdSleepSeconds(1);
+ test_assert(chTimeIsWithin(time + S2ST(1), time + S2ST(1) + 1), "#3");
+
+ /* Absolute timelines.*/
+ time = chTimeNow() + MS2ST(100);
+ chThdSleepUntil(time);
+ test_assert(chTimeIsWithin(time, time + 1), "#4");
+}
+
+const struct testcase testthd4 = {
+ thd4_gettest,
+ NULL,
+ NULL,
+ thd4_execute
+};
+
+/*
+ * Test sequence for ready list pattern.
+ */
+const struct testcase * const patternthd[] = {
+ &testthd1,
+ &testthd2,
+ &testthd3,
+ &testthd4,
+ NULL
+};