Skip to content

Commit ef291e7

Browse files
committed
Add unit tests over spawning/joining threads with thread functions
1 parent da6571c commit ef291e7

File tree

1 file changed

+110
-0
lines changed
  • core/mbed-rtos/TESTS/mbed-rtos/threads

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#include "mbed.h"
2+
#include "test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "rtos.h"
6+
7+
8+
using namespace utest::v1;
9+
10+
11+
// Tasks with different functions to test on threads
12+
void increment(const void *var) {
13+
(*(int *)var)++;
14+
}
15+
16+
void increment_with_yield(const void *var) {
17+
Thread::yield();
18+
(*(int *)var)++;
19+
}
20+
21+
void increment_with_wait(const void *var) {
22+
Thread::wait(100);
23+
(*(int *)var)++;
24+
}
25+
26+
void increment_with_child(const void *var) {
27+
Thread child(increment, (void*)var);
28+
child.join();
29+
}
30+
31+
void increment_with_murder(const void *var) {
32+
Thread child(increment_with_wait, (void*)var);
33+
// Kill child before it can increment var
34+
child.terminate();
35+
(*(int *)var)++;
36+
}
37+
38+
39+
// Tests that spawn tasks in different configurations
40+
template <void (*F)(const void *)>
41+
void test_single_thread() {
42+
int var = 0;
43+
Thread thread(F, &var);
44+
thread.join();
45+
TEST_ASSERT_EQUAL(var, 1);
46+
}
47+
48+
template <int N, void (*F)(const void *)>
49+
void test_parallel_threads() {
50+
int var = 0;
51+
Thread *threads[N];
52+
53+
for (int i = 0; i < N; i++) {
54+
threads[i] = new Thread(F, &var);
55+
}
56+
57+
for (int i = 0; i < N; i++) {
58+
threads[i]->join();
59+
delete threads[i];
60+
}
61+
62+
TEST_ASSERT_EQUAL(var, N);
63+
}
64+
65+
template <int N, void (*F)(const void *)>
66+
void test_serial_threads() {
67+
int var = 0;
68+
69+
for (int i = 0; i < N; i++) {
70+
Thread thread(F, &var);
71+
thread.join();
72+
}
73+
74+
TEST_ASSERT_EQUAL(var, N);
75+
}
76+
77+
78+
status_t test_setup(const size_t number_of_cases) {
79+
GREENTEA_SETUP(40, "default_auto");
80+
return verbose_test_setup_handler(number_of_cases);
81+
}
82+
83+
// Test cases
84+
Case cases[] = {
85+
Case("Testing single thread", test_single_thread<increment>),
86+
Case("Testing parallel threads", test_parallel_threads<3, increment>),
87+
Case("Testing serial threads", test_serial_threads<10, increment>),
88+
89+
Case("Testing single thread with yield", test_single_thread<increment_with_yield>),
90+
Case("Testing parallel threads with yield", test_parallel_threads<3, increment_with_yield>),
91+
Case("Testing serial threads with yield", test_serial_threads<10, increment_with_yield>),
92+
93+
Case("Testing single thread with wait", test_single_thread<increment_with_wait>),
94+
Case("Testing parallel threads with wait", test_parallel_threads<3, increment_with_wait>),
95+
Case("Testing serial threads with wait", test_serial_threads<10, increment_with_wait>),
96+
97+
Case("Testing single thread with child", test_single_thread<increment_with_child>),
98+
Case("Testing parallel threads with child", test_parallel_threads<3, increment_with_child>),
99+
Case("Testing serial threads with child", test_serial_threads<10, increment_with_child>),
100+
101+
Case("Testing single thread with murder", test_single_thread<increment_with_murder>),
102+
Case("Testing parallel threads with murder", test_parallel_threads<3, increment_with_murder>),
103+
Case("Testing serial threads with murder", test_serial_threads<10, increment_with_murder>),
104+
};
105+
106+
Specification specification(test_setup, cases);
107+
108+
int main() {
109+
return !Harness::run(specification);
110+
}

0 commit comments

Comments
 (0)