Skip to content

Commit d01c7fa

Browse files
committed
Add test cases for ConditionVariable
Add basic tests for the ConditionVariable class.
1 parent 0c4e50a commit d01c7fa

File tree

1 file changed

+111
-0
lines changed
  • TESTS/mbedmicro-rtos-mbed/condition_variable

1 file changed

+111
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "mbed.h"
17+
#include "greentea-client/test_env.h"
18+
#include "unity.h"
19+
#include "utest.h"
20+
#include "rtos.h"
21+
22+
#if defined(MBED_RTOS_SINGLE_THREAD)
23+
#error [NOT_SUPPORTED] test not supported
24+
#endif
25+
26+
using namespace utest::v1;
27+
28+
#define TEST_STACK_SIZE 512
29+
#define TEST_DELAY 10
30+
31+
static int change_counter = 0;
32+
static Mutex mutex;
33+
static ConditionVariable cond(mutex);
34+
35+
void increment_on_signal()
36+
{
37+
mutex.lock();
38+
39+
cond.wait();
40+
change_counter++;
41+
42+
mutex.unlock();
43+
}
44+
45+
void test_notify_one()
46+
{
47+
Thread t1(osPriorityNormal, TEST_STACK_SIZE);
48+
Thread t2(osPriorityNormal, TEST_STACK_SIZE);
49+
50+
change_counter = 0;
51+
t1.start(increment_on_signal);
52+
t2.start(increment_on_signal);
53+
54+
wait_ms(TEST_DELAY);
55+
TEST_ASSERT_EQUAL(0, change_counter);
56+
57+
mutex.lock();
58+
cond.notify_one();
59+
mutex.unlock();
60+
61+
wait_ms(TEST_DELAY);
62+
TEST_ASSERT_EQUAL(1, change_counter);
63+
64+
mutex.lock();
65+
cond.notify_one();
66+
mutex.unlock();
67+
68+
t1.join();
69+
t2.join();
70+
}
71+
72+
void test_notify_all()
73+
{
74+
Thread t1(osPriorityNormal, TEST_STACK_SIZE);
75+
Thread t2(osPriorityNormal, TEST_STACK_SIZE);
76+
77+
change_counter = 0;
78+
t1.start(increment_on_signal);
79+
t2.start(increment_on_signal);
80+
81+
wait_ms(TEST_DELAY);
82+
TEST_ASSERT_EQUAL(0, change_counter);
83+
84+
mutex.lock();
85+
cond.notify_all();
86+
mutex.unlock();
87+
88+
wait_ms(TEST_DELAY);
89+
TEST_ASSERT_EQUAL(2, change_counter);
90+
91+
t1.join();
92+
t2.join();
93+
}
94+
95+
utest::v1::status_t test_setup(const size_t number_of_cases)
96+
{
97+
GREENTEA_SETUP(10, "default_auto");
98+
return verbose_test_setup_handler(number_of_cases);
99+
}
100+
101+
Case cases[] = {
102+
Case("Test notify one", test_notify_one),
103+
Case("Test notify all", test_notify_all),
104+
};
105+
106+
Specification specification(test_setup, cases);
107+
108+
int main()
109+
{
110+
return !Harness::run(specification);
111+
}

0 commit comments

Comments
 (0)