Skip to content

Commit e7e5cd2

Browse files
equeue tests: add user allocated events tests
1 parent b336f73 commit e7e5cd2

File tree

3 files changed

+172
-2
lines changed

3 files changed

+172
-2
lines changed

TESTS/events/equeue/main.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,65 @@ 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, 1, -1, NULL, NULL }, 0 };
1004+
user_allocated_event e3 = { { 0, 0, 0, NULL, NULL, NULL, 1, -1, NULL, NULL }, 0 };
1005+
user_allocated_event e4 = { { 0, 0, 0, NULL, NULL, NULL, 1, -1, NULL, NULL }, 0 };
1006+
user_allocated_event e5 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
1007+
1008+
TEST_ASSERT_NOT_EQUAL(0, equeue_call(&q, simple_func, &touched));
1009+
TEST_ASSERT_EQUAL_INT(0, equeue_call(&q, simple_func, &touched));
1010+
TEST_ASSERT_EQUAL_INT(0, equeue_call(&q, simple_func, &touched));
1011+
1012+
equeue_post_user_allocated(&q, simple_func, &e1.e);
1013+
equeue_post_user_allocated(&q, simple_func, &e2.e);
1014+
equeue_post_user_allocated(&q, simple_func, &e3.e);
1015+
equeue_post_user_allocated(&q, simple_func, &e4.e);
1016+
equeue_post_user_allocated(&q, simple_func, &e5.e);
1017+
equeue_cancel_user_allocated(&q, &e3.e);
1018+
1019+
equeue_dispatch(&q, 1);
1020+
1021+
TEST_ASSERT_EQUAL_UINT8(1, touched);
1022+
TEST_ASSERT_EQUAL_UINT8(1, e1.touched);
1023+
TEST_ASSERT_EQUAL_UINT8(1, e2.touched);
1024+
TEST_ASSERT_EQUAL_UINT8(0, e3.touched);
1025+
TEST_ASSERT_EQUAL_UINT8(1, e4.touched);
1026+
TEST_ASSERT_EQUAL_UINT8(1, e5.touched);
1027+
1028+
equeue_dispatch(&q, 10);
1029+
1030+
TEST_ASSERT_EQUAL_UINT8(1, touched);
1031+
TEST_ASSERT_EQUAL_UINT8(1, e1.touched);
1032+
TEST_ASSERT_EQUAL_UINT8(1, e2.touched);
1033+
TEST_ASSERT_EQUAL_UINT8(0, e3.touched);
1034+
TEST_ASSERT_EQUAL_UINT8(1, e4.touched);
1035+
TEST_ASSERT_EQUAL_UINT8(1, e5.touched);
1036+
1037+
equeue_destroy(&q);
1038+
}
9801039

9811040
Case cases[] = {
9821041
Case("simple call test", test_equeue_simple_call),
@@ -1006,7 +1065,8 @@ Case cases[] = {
10061065
Case("fragmenting barrage test", test_equeue_fragmenting_barrage<10>),
10071066
Case("multithreaded barrage test", test_equeue_multithreaded_barrage<10>),
10081067
Case("break request cleared on timeout test", test_equeue_break_request_cleared_on_timeout),
1009-
Case("sibling test", test_equeue_sibling)
1068+
Case("sibling test", test_equeue_sibling),
1069+
Case("user allocated event test", test_equeue_user_allocated_event_post)
10101070

10111071
};
10121072

UNITTESTS/events/equeue/test_equeue.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,4 +994,63 @@ 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, 1, -1, NULL, NULL }, 0 };
1022+
user_allocated_event e3 = { { 0, 0, 0, NULL, NULL, NULL, 1, -1, NULL, NULL }, 0 };
1023+
user_allocated_event e4 = { { 0, 0, 0, NULL, NULL, NULL, 1, -1, NULL, NULL }, 0 };
1024+
user_allocated_event e5 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
1025+
1026+
EXPECT_NE(0, equeue_call(&q, simple_func, &touched));
1027+
EXPECT_EQ(0, equeue_call(&q, simple_func, &touched));
1028+
EXPECT_EQ(0, equeue_call(&q, simple_func, &touched));
1029+
1030+
equeue_post_user_allocated(&q, simple_func, &e1.e);
1031+
equeue_post_user_allocated(&q, simple_func, &e2.e);
1032+
equeue_post_user_allocated(&q, simple_func, &e3.e);
1033+
equeue_post_user_allocated(&q, simple_func, &e4.e);
1034+
equeue_post_user_allocated(&q, simple_func, &e5.e);
1035+
equeue_cancel_user_allocated(&q, &e3.e);
1036+
1037+
equeue_dispatch(&q, 1);
1038+
1039+
EXPECT_EQ(1, touched);
1040+
EXPECT_EQ(1, e1.touched);
1041+
EXPECT_EQ(1, e2.touched);
1042+
EXPECT_EQ(0, e3.touched);
1043+
EXPECT_EQ(1, e4.touched);
1044+
EXPECT_EQ(1, e5.touched);
1045+
1046+
equeue_dispatch(&q, 10);
1047+
1048+
EXPECT_EQ(1, touched);
1049+
EXPECT_EQ(1, e1.touched);
1050+
EXPECT_EQ(1, e2.touched);
1051+
EXPECT_EQ(0, e3.touched);
1052+
EXPECT_EQ(1, e4.touched);
1053+
EXPECT_EQ(1, e5.touched);
1054+
1055+
equeue_destroy(&q);
1056+
}

events/source/tests/tests.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,56 @@ 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, 1, -1, NULL, NULL }, 0 };
819+
struct user_allocated_event e3 = { { 0, 0, 0, NULL, NULL, NULL, 1, -1, NULL, NULL }, 0 };
820+
struct user_allocated_event e4 = { { 0, 0, 0, NULL, NULL, NULL, 1, -1, NULL, NULL }, 0 };
821+
struct user_allocated_event e5 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
822+
823+
test_assert(0 != equeue_call(&q, simple_func, &touched));
824+
test_assert(0 == equeue_call(&q, simple_func, &touched));
825+
test_assert(0 == equeue_call(&q, simple_func, &touched));
826+
827+
equeue_post_user_allocated(&q, simple_func, &e1.e);
828+
equeue_post_user_allocated(&q, simple_func, &e2.e);
829+
equeue_post_user_allocated(&q, simple_func, &e3.e);
830+
equeue_post_user_allocated(&q, simple_func, &e4.e);
831+
equeue_post_user_allocated(&q, simple_func, &e5.e);
832+
equeue_cancel_user_allocated(&q, &e3.e);
833+
834+
equeue_dispatch(&q, 1);
835+
836+
test_assert(true == touched);
837+
test_assert(true == e1.touched);
838+
test_assert(true == e2.touched);
839+
test_assert(false == e3.touched);
840+
test_assert(true == e4.touched);
841+
test_assert(true == e5.touched);
842+
843+
equeue_dispatch(&q, 10);
844+
845+
test_assert(true == touched);
846+
test_assert(true == e1.touched);
847+
test_assert(true == e2.touched);
848+
test_assert(false == e3.touched);
849+
test_assert(true == e4.touched);
850+
test_assert(true == e5.touched);
851+
852+
equeue_destroy(&q);
853+
}
854+
805855
int main()
806856
{
807857
printf("beginning tests...\n");
@@ -830,6 +880,7 @@ int main()
830880
test_run(multithreaded_barrage_test, 20);
831881
test_run(break_request_cleared_on_timeout);
832882
test_run(sibling_test);
883+
test_run(user_allocated_event_test);
833884
printf("done!\n");
834885
return test_failure;
835886
}

0 commit comments

Comments
 (0)