Skip to content

Commit cc24ee9

Browse files
equeue tests: add user allocated events tests
1 parent 70f4d2b commit cc24ee9

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed

TESTS/events/equeue/main.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,60 @@ static void test_equeue_sibling()
977977
equeue_destroy(&q);
978978
}
979979

980+
struct user_allocated_event {
981+
struct equeue_event e;
982+
uint8_t touched;
983+
};
984+
985+
/** Test that equeue executes user allocated events passed by equeue_post.
986+
*
987+
* Given queue is initialized and its size is set to store one event at max in its internal memory.
988+
* When post events allocated in queues internal memory (what is done by calling equeue_call).
989+
* Then only one event can be posted due to queue memory size.
990+
* When post user allocated events.
991+
* Then number of posted events is not limited by queue memory size.
992+
* When both queue allocaded and user allocated events are posted and equeue_dispatch is called.
993+
* Then both types of events are executed properly.
994+
*/
995+
static void test_equeue_user_allocated_event_post()
996+
{
997+
equeue_t q;
998+
int err = equeue_create(&q, EQUEUE_EVENT_SIZE);
999+
TEST_ASSERT_EQUAL_INT(0, err);
1000+
1001+
uint8_t touched = 0;
1002+
user_allocated_event e1 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
1003+
user_allocated_event e2 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
1004+
user_allocated_event e3 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
1005+
1006+
TEST_ASSERT_NOT_EQUAL(0, equeue_call(&q, simple_func, &touched));
1007+
TEST_ASSERT_EQUAL_INT(0, equeue_call(&q, simple_func, &touched));
1008+
TEST_ASSERT_EQUAL_INT(0, equeue_call(&q, simple_func, &touched));
1009+
1010+
int id1 = equeue_post(&q, simple_func, &(e1.e) + 1);
1011+
TEST_ASSERT_NOT_EQUAL(0, id1);
1012+
int id2 = equeue_post(&q, simple_func, &(e2.e) + 1);
1013+
TEST_ASSERT_NOT_EQUAL(0, id2);
1014+
int id3 = equeue_post(&q, simple_func, &(e3.e) + 1);
1015+
TEST_ASSERT_NOT_EQUAL(0, id3);
1016+
equeue_cancel(&q, id3);
1017+
1018+
equeue_dispatch(&q, 1);
1019+
1020+
TEST_ASSERT_EQUAL_UINT8(1, touched);
1021+
TEST_ASSERT_EQUAL_UINT8(1, e1.touched);
1022+
TEST_ASSERT_EQUAL_UINT8(1, e2.touched);
1023+
TEST_ASSERT_EQUAL_UINT8(0, e3.touched);
1024+
1025+
equeue_dispatch(&q, 10);
1026+
1027+
TEST_ASSERT_EQUAL_UINT8(1, touched);
1028+
TEST_ASSERT_EQUAL_UINT8(1, e1.touched);
1029+
TEST_ASSERT_EQUAL_UINT8(1, e2.touched);
1030+
TEST_ASSERT_EQUAL_UINT8(0, e3.touched);
1031+
1032+
equeue_destroy(&q);
1033+
}
9801034

9811035
Case cases[] = {
9821036
Case("simple call test", test_equeue_simple_call),
@@ -1006,7 +1060,8 @@ Case cases[] = {
10061060
Case("fragmenting barrage test", test_equeue_fragmenting_barrage<10>),
10071061
Case("multithreaded barrage test", test_equeue_multithreaded_barrage<10>),
10081062
Case("break request cleared on timeout test", test_equeue_break_request_cleared_on_timeout),
1009-
Case("sibling test", test_equeue_sibling)
1063+
Case("sibling test", test_equeue_sibling),
1064+
Case("user allocated event test", test_equeue_user_allocated_event_post)
10101065

10111066
};
10121067

UNITTESTS/events/equeue/test_equeue.cpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,4 +994,58 @@ TEST_F(TestEqueue, test_equeue_sibling)
994994
equeue_cancel(&q, id1);
995995
equeue_cancel(&q, id2);
996996
equeue_destroy(&q);
997-
}
997+
}
998+
999+
/** Test that equeue executes user allocated events passed by equeue_post.
1000+
*
1001+
* Given queue is initialized and its size is set to store one event at max in its internal memory.
1002+
* When post events allocated in queues internal memory (what is done by calling equeue_call).
1003+
* Then only one event can be posted due to queue memory size.
1004+
* When post user allocated events.
1005+
* Then number of posted events is not limited by queue memory size.
1006+
* When both queue allocaded and user allocated events are posted and equeue_dispatch is called.
1007+
* Then both types of events are executed properly.
1008+
*/
1009+
TEST_F(TestEqueue, test_equeue_user_allocated_event_post)
1010+
{
1011+
struct user_allocated_event {
1012+
struct equeue_event e;
1013+
uint8_t touched;
1014+
};
1015+
equeue_t q;
1016+
int err = equeue_create(&q, EQUEUE_EVENT_SIZE);
1017+
ASSERT_EQ(0, err);
1018+
1019+
uint8_t touched = 0;
1020+
user_allocated_event e1 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
1021+
user_allocated_event e2 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
1022+
user_allocated_event e3 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
1023+
1024+
EXPECT_NE(0, equeue_call(&q, simple_func, &touched));
1025+
EXPECT_EQ(0, equeue_call(&q, simple_func, &touched));
1026+
EXPECT_EQ(0, equeue_call(&q, simple_func, &touched));
1027+
1028+
int id1 = equeue_post(&q, simple_func, &(e1.e) + 1);
1029+
EXPECT_NE(0, id1);
1030+
int id2 = equeue_post(&q, simple_func, &(e2.e) + 1);
1031+
EXPECT_NE(0, id2);
1032+
int id3 = equeue_post(&q, simple_func, &(e3.e) + 1);
1033+
EXPECT_NE(0, id3);
1034+
equeue_cancel(&q, id3);
1035+
1036+
equeue_dispatch(&q, 1);
1037+
1038+
EXPECT_EQ(1, touched);
1039+
EXPECT_EQ(1, e1.touched);
1040+
EXPECT_EQ(1, e2.touched);
1041+
EXPECT_EQ(0, e3.touched);
1042+
1043+
equeue_dispatch(&q, 10);
1044+
1045+
EXPECT_EQ(1, touched);
1046+
EXPECT_EQ(1, e1.touched);
1047+
EXPECT_EQ(1, e2.touched);
1048+
EXPECT_EQ(0, e3.touched);
1049+
1050+
equeue_destroy(&q);
1051+
}

events/source/tests/tests.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,51 @@ void sibling_test(void)
802802
equeue_destroy(&q);
803803
}
804804

805+
struct user_allocated_event {
806+
struct equeue_event e;
807+
bool touched;
808+
};
809+
810+
void user_allocated_event_test()
811+
{
812+
equeue_t q;
813+
int err = equeue_create(&q, EQUEUE_EVENT_SIZE);
814+
test_assert(!err);
815+
816+
bool touched = false;
817+
struct user_allocated_event e1 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
818+
struct user_allocated_event e2 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
819+
struct user_allocated_event e3 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
820+
821+
test_assert(0 != equeue_call(&q, simple_func, &touched));
822+
test_assert(0 == equeue_call(&q, simple_func, &touched));
823+
test_assert(0 == equeue_call(&q, simple_func, &touched));
824+
825+
int id1 = equeue_post(&q, simple_func, &(e1.e) + 1);
826+
test_assert(0 != id1);
827+
int id2 = equeue_post(&q, simple_func, &(e2.e) + 1);
828+
test_assert(0 != id2);
829+
int id3 = equeue_post(&q, simple_func, &(e3.e) + 1);
830+
test_assert(0 != id3);
831+
equeue_cancel(&q, id3);
832+
833+
equeue_dispatch(&q, 1);
834+
835+
test_assert(true == touched);
836+
test_assert(true == e1.touched);
837+
test_assert(true == e2.touched);
838+
test_assert(false == e3.touched);
839+
840+
equeue_dispatch(&q, 10);
841+
842+
test_assert(true == touched);
843+
test_assert(true == e1.touched);
844+
test_assert(true == e2.touched);
845+
test_assert(false == e3.touched);
846+
847+
equeue_destroy(&q);
848+
}
849+
805850
int main()
806851
{
807852
printf("beginning tests...\n");
@@ -830,6 +875,7 @@ int main()
830875
test_run(multithreaded_barrage_test, 20);
831876
test_run(break_request_cleared_on_timeout);
832877
test_run(sibling_test);
878+
test_run(user_allocated_event_test);
833879
printf("done!\n");
834880
return test_failure;
835881
}

0 commit comments

Comments
 (0)