Skip to content

Commit 9ffc5bc

Browse files
authored
Merge pull request #12779 from rajkan01/eventflags_greentea_test
Baremetal: Enable EventFlags greentea test
2 parents 58de040 + 45de008 commit 9ffc5bc

File tree

1 file changed

+94
-19
lines changed
  • TESTS/mbedmicro-rtos-mbed/event_flags

1 file changed

+94
-19
lines changed

TESTS/mbedmicro-rtos-mbed/event_flags/main.cpp

Lines changed: 94 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
#if defined(MBED_RTOS_SINGLE_THREAD) || !defined(MBED_CONF_RTOS_PRESENT)
18-
#error [NOT_SUPPORTED] Event flags test cases require RTOS with multithread to run
19-
#else
2017

2118
#include "mbed.h"
2219
#include "greentea-client/test_env.h"
@@ -29,11 +26,13 @@ using utest::v1::Case;
2926
#error [NOT_SUPPORTED] UsTicker need to be enabled for this test.
3027
#else
3128

29+
#if defined(MBED_CONF_RTOS_PRESENT)
3230
#if defined(__CORTEX_M23) || defined(__CORTEX_M33)
3331
#define THREAD_STACK_SIZE 512
3432
#else
3533
#define THREAD_STACK_SIZE 320 /* 512B stack on GCC_ARM compiler cause out of memory on some 16kB RAM boards e.g. NUCLEO_F070RB */
3634
#endif
35+
#endif
3736

3837
#define MAX_FLAG_POS 30
3938
#define PROHIBITED_FLAG_POS 31
@@ -45,20 +44,22 @@ using utest::v1::Case;
4544
#define PROHIBITED_FLAG 0x80000000 /* 10000000000000000000000000000000 */
4645
#define NO_FLAGS 0x0
4746

48-
Semaphore sync_sem(0, 1);
49-
50-
template<uint32_t flags, uint32_t wait_ms>
51-
void send_thread(EventFlags *ef)
47+
void send_thread(EventFlags *ef, uint32_t flags, uint32_t wait_ms)
5248
{
5349
for (uint32_t i = 0; i <= MAX_FLAG_POS; i++) {
5450
const uint32_t flag = flags & (1 << i);
5551
if (flag) {
5652
ef->set(flag);
57-
ThisThread::sleep_for(wait_ms);
53+
if (wait_ms != 0) {
54+
ThisThread::sleep_for(wait_ms);
55+
}
5856
}
5957
}
6058
}
6159

60+
#if defined(MBED_CONF_RTOS_PRESENT)
61+
Semaphore sync_sem(0, 1);
62+
6263
template<uint32_t flags, uint32_t wait_ms>
6364
void send_thread_sync(EventFlags *ef)
6465
{
@@ -81,6 +82,7 @@ void wait_thread_all(EventFlags *ef)
8182
TEST_ASSERT(flags | ret);
8283
TEST_ASSERT(flags | ~flags_after_clear);
8384
}
85+
#endif
8486

8587

8688
/** Test if get on empty EventFlags object return NO_FLAGS
@@ -203,6 +205,7 @@ void test_set_get_clear_full_flag_range(void)
203205
}
204206
}
205207

208+
#if defined(MBED_CONF_RTOS_PRESENT)
206209
/** Test if multi-threaded flag set cause wait_all to return
207210
208211
Given a EventFlags object and three threads are started in parallel
@@ -215,9 +218,9 @@ void test_multi_thread_all(void)
215218
Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
216219
Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
217220
Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
218-
thread1.start(callback(send_thread<FLAG01, 1>, &ef));
219-
thread2.start(callback(send_thread<FLAG02, 2>, &ef));
220-
thread3.start(callback(send_thread<FLAG03, 3>, &ef));
221+
thread1.start([&] { send_thread(&ef, FLAG01, 1); });
222+
thread2.start([&] { send_thread(&ef, FLAG02, 2); });
223+
thread3.start([&] { send_thread(&ef, FLAG03, 3); });
221224

222225
uint32_t ret = ef.wait_all(FLAG01 | FLAG02 | FLAG03);
223226
TEST_ASSERT_EQUAL(FLAG01 | FLAG02 | FLAG03, ret);
@@ -236,9 +239,9 @@ void test_multi_thread_any(void)
236239
Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
237240
Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
238241
Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
239-
thread1.start(callback(send_thread<FLAG01, 1>, &ef));
240-
thread2.start(callback(send_thread<FLAG02, 1>, &ef));
241-
thread3.start(callback(send_thread<FLAG03, 1>, &ef));
242+
thread1.start([&] { send_thread(&ef, FLAG01, 1); });
243+
thread2.start([&] { send_thread(&ef, FLAG02, 1); });
244+
thread3.start([&] { send_thread(&ef, FLAG03, 1); });
242245

243246
for (int i = 0; i <= MAX_FLAG_POS; i++) {
244247
uint32_t flag = 1 << i;
@@ -291,9 +294,9 @@ void test_multi_thread_any_no_clear(void)
291294
Thread thread1(osPriorityNormal, THREAD_STACK_SIZE);
292295
Thread thread2(osPriorityNormal, THREAD_STACK_SIZE);
293296
Thread thread3(osPriorityNormal, THREAD_STACK_SIZE);
294-
thread1.start(callback(send_thread<FLAG01, 1>, &ef));
295-
thread2.start(callback(send_thread<FLAG02, 1>, &ef));
296-
thread3.start(callback(send_thread<FLAG03, 1>, &ef));
297+
thread1.start([&] { send_thread(&ef, FLAG01, 1); });
298+
thread2.start([&] { send_thread(&ef, FLAG02, 1); });
299+
thread3.start([&] { send_thread(&ef, FLAG03, 1); });
297300

298301
for (int i = 0; i <= MAX_FLAG_POS; i++) {
299302
uint32_t flag = 1 << i;
@@ -347,6 +350,74 @@ void test_multi_thread_all_many_wait(void)
347350
TEST_ASSERT_EQUAL(NO_FLAGS, ef.get());
348351
}
349352
}
353+
#endif
354+
355+
/** Test if multi-event flag set cause wait_all to return
356+
357+
Given a EventFlags object and three ticker instance with the callback registered
358+
When callbacks set specified flags
359+
Then main thread waits until receive all of them
360+
*/
361+
void test_multi_eventflags_all(void)
362+
{
363+
EventFlags ef;
364+
Ticker t1, t2, t3;
365+
t1.attach_us([&] { send_thread(&ef, FLAG01, 0); }, 3000);
366+
t2.attach_us([&] { send_thread(&ef, FLAG02, 0); }, 4000);
367+
t3.attach_us([&] { send_thread(&ef, FLAG03, 0); }, 5000);
368+
uint32_t ret = ef.wait_all(FLAG01 | FLAG02 | FLAG03, 20, false);
369+
TEST_ASSERT_EQUAL(FLAG01 | FLAG02 | FLAG03, ret);
370+
}
371+
372+
/** Test if multi-event flag set cause wait_any to return
373+
374+
Given a EventFlags object and three ticker instance with the callback registered
375+
When callbacks set specified flags
376+
Then main thread waits until receive all of them
377+
*/
378+
void test_multi_eventflags_any(void)
379+
{
380+
EventFlags ef;
381+
uint32_t ret;
382+
Ticker t1, t2, t3;
383+
t1.attach_us([&] { send_thread(&ef, FLAG01, 0); }, 3000);
384+
t2.attach_us([&] { send_thread(&ef, FLAG02, 0); }, 4000);
385+
t3.attach_us([&] { send_thread(&ef, FLAG03, 0); }, 5000);
386+
387+
for (int i = 0; i <= MAX_FLAG_POS; i++) {
388+
uint32_t flag = 1 << i;
389+
ret = ef.wait_any(flag);
390+
TEST_ASSERT(flag | ret);
391+
}
392+
ret = ef.get();
393+
TEST_ASSERT_EQUAL(NO_FLAGS, ret);
394+
}
395+
396+
/** Test if multi-event flag set cause wait_any(without clear) to return
397+
398+
Given a EventFlags object and three ticker instance with the callback registered
399+
When callbacks set specified flags
400+
Then main thread waits until receive all of them
401+
*/
402+
void test_multi_eventflags_any_no_clear(void)
403+
{
404+
EventFlags ef;
405+
uint32_t ret;
406+
Ticker t1, t2, t3;
407+
t1.attach_us([&] { send_thread(&ef, FLAG01, 0); }, 3000);
408+
t2.attach_us([&] { send_thread(&ef, FLAG02, 0); }, 4000);
409+
t3.attach_us([&] { send_thread(&ef, FLAG03, 0); }, 5000);
410+
411+
for (int i = 0; i <= MAX_FLAG_POS; i++) {
412+
uint32_t flag = 1 << i;
413+
ret = ef.wait_any(flag, osWaitForever, false);
414+
TEST_ASSERT(flag | ret);
415+
ret = ef.clear(flag);
416+
TEST_ASSERT(ret < osFlagsError);
417+
}
418+
ret = ef.get();
419+
TEST_ASSERT_EQUAL(NO_FLAGS, ret);
420+
}
350421

351422
utest::v1::status_t test_setup(const size_t number_of_cases)
352423
{
@@ -360,11 +431,16 @@ Case cases[] = {
360431
Case("Test empty set", test_empty_set),
361432
Case("Test clear/set with prohibited flag", test_prohibited),
362433
Case("Test set/get/clear for full flag range", test_set_get_clear_full_flag_range),
434+
#if defined(MBED_CONF_RTOS_PRESENT)
363435
Case("Test multi-threaded wait_all", test_multi_thread_all),
364436
Case("Test multi-threaded wait_any", test_multi_thread_any),
365437
Case("Test multi-threaded wait_all many wait", test_multi_thread_all_many_wait),
366438
Case("Test multi-threaded wait_any timeout", test_multi_thread_any_timeout),
367-
Case("Test multi-threaded wait_any no clear", test_multi_thread_any_no_clear)
439+
Case("Test multi-threaded wait_any no clear", test_multi_thread_any_no_clear),
440+
#endif
441+
Case("Test multi-eventflags wait_all", test_multi_eventflags_all),
442+
Case("Test multi-eventflags wait_any", test_multi_eventflags_any),
443+
Case("Test multi-eventflags wait_any no clear", test_multi_eventflags_any_no_clear)
368444
};
369445

370446
utest::v1::Specification specification(test_setup, cases);
@@ -375,4 +451,3 @@ int main()
375451
}
376452

377453
#endif // !DEVICE_USTICKER
378-
#endif // defined(MBED_RTOS_SINGLE_THREAD) || !defined(MBED_CONF_RTOS_PRESENT)

0 commit comments

Comments
 (0)