diff --git a/TESTS/events/queue/main.cpp b/TESTS/events/queue/main.cpp index f52cf70665ae..591d065420ad 100644 --- a/TESTS/events/queue/main.cpp +++ b/TESTS/events/queue/main.cpp @@ -322,6 +322,134 @@ void time_left_test() TEST_ASSERT_EQUAL(-1, queue.time_left(0)); } +void f5(int a1, int a2, int a3, int a4, int a5) +{ + touched = true; +} + +class UserAllocatedTest { +public: + UserAllocatedTest() : counter() {} + void f0() + { + counter++; + } + void f1(int a) + { + counter += a; + } + void f5(int a, int b, int c, int d, int e) + { + counter += a + b + c + d + e; + } + uint32_t counter; +}; + +void my_f0() +{ + touched = true; +} +void my_f5(int, int, int, int, int) +{ + touched = true; +} + +/** Test that queue executes user allocated events. + * + * Given queue is initialized and its size is set to store one Event at max in its internal memory. + * When post queue allocated event. + * Then only one event can be posted due to queue memory size. + * When post user allocated evens. + * Then number of posted events is not limited by queue memory size. + * When both Event and UserAllocatedEvent are posted and queue dispatch is called. + * Then both types of events are executed properly. + * + * Given queue is initialized and its size is set 1B - only user allocated events allowed. + * When post user allocated evens. + * Then UserAllocatedEvent are posted and dispatched without any error. + */ +void user_allocated_events_test() +{ + { + EventQueue queue(4 * EVENTS_EVENT_SIZE); + + Event e0 = queue.event(func0); + TEST_ASSERT_NOT_EQUAL(0, e0.post()); + Event e1 = queue.event(func0); + TEST_ASSERT_EQUAL(0, e1.post()); + + auto ue0 = make_user_allocated_event(func0); + UserAllocatedTest test1; + auto ue1 = make_user_allocated_event(&test1, &UserAllocatedTest::f0); + UserAllocatedTest test2; + auto ue2 = make_user_allocated_event(&test2, &UserAllocatedTest::f1, 3); + UserAllocatedTest test3; + auto ue3 = make_user_allocated_event(&test3, &UserAllocatedTest::f5, 1, 2, 3, 4, 5); + UserAllocatedTest test4; + auto ue4 = make_user_allocated_event(&test4, &UserAllocatedTest::f5, 1, 2, 3, 4, 5); + + touched = false; + + ue0.call_on(&queue); + TEST_ASSERT_EQUAL(false, ue0.try_call()); + ue1.call_on(&queue); + TEST_ASSERT_EQUAL(false, ue1.try_call()); + ue2.call_on(&queue); + TEST_ASSERT_EQUAL(false, ue2.try_call()); + ue3.call_on(&queue); + TEST_ASSERT_EQUAL(false, ue3.try_call()); + ue4.call_on(&queue); + ue4.cancel(); + TEST_ASSERT_EQUAL(true, ue4.try_call()); + ue4.cancel(); + + queue.dispatch(1); + + TEST_ASSERT_EQUAL(true, touched); + TEST_ASSERT_EQUAL(1, test1.counter); + TEST_ASSERT_EQUAL(3, test2.counter); + TEST_ASSERT_EQUAL(15, test3.counter); + TEST_ASSERT_EQUAL(0, test4.counter); + } + + { + EventQueue queue(1); + + Event e0 = queue.event(func0); + TEST_ASSERT_EQUAL(0, e0.post()); + + auto ue0 = make_user_allocated_event(func0); + UserAllocatedTest test1; + auto ue1 = make_user_allocated_event(&test1, &UserAllocatedTest::f0); + UserAllocatedTest test2; + auto ue2 = make_user_allocated_event(&test2, &UserAllocatedTest::f1, 3); + UserAllocatedTest test3; + auto ue3 = make_user_allocated_event(&test3, &UserAllocatedTest::f5, 1, 2, 3, 4, 5); + UserAllocatedTest test4; + auto ue4 = make_user_allocated_event(&test4, &UserAllocatedTest::f5, 1, 2, 3, 4, 5); + + ue0.call_on(&queue); + TEST_ASSERT_EQUAL(false, ue0.try_call()); + ue1.call_on(&queue); + TEST_ASSERT_EQUAL(false, ue1.try_call()); + ue2.call_on(&queue); + TEST_ASSERT_EQUAL(false, ue2.try_call()); + ue3.call_on(&queue); + TEST_ASSERT_EQUAL(false, ue3.try_call()); + ue4.call_on(&queue); + ue4.cancel(); + TEST_ASSERT_EQUAL(true, ue4.try_call()); + ue4.cancel(); + + queue.dispatch(1); + + TEST_ASSERT_EQUAL(1, test1.counter); + TEST_ASSERT_EQUAL(3, test2.counter); + TEST_ASSERT_EQUAL(15, test3.counter); + TEST_ASSERT_EQUAL(0, test4.counter); + } +} + // Test setup utest::v1::status_t test_setup(const size_t number_of_cases) { @@ -348,6 +476,7 @@ const Case cases[] = { Case("Testing the event inference", event_inference_test), Case("Testing time_left", time_left_test), + Case("Testing user allocated event class", user_allocated_events_test) }; Specification specification(test_setup, cases);