Skip to content

Commit 023f630

Browse files
equeue tests: add user allocated events tests
1 parent 59baf07 commit 023f630

File tree

3 files changed

+187
-2
lines changed

3 files changed

+187
-2
lines changed

TESTS/events/equeue/main.cpp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,70 @@ 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+
int id1 = equeue_post(&q, simple_func, &(e1.e) + 1);
1013+
TEST_ASSERT_NOT_EQUAL(0, id1);
1014+
int id2 = equeue_post(&q, simple_func, &(e2.e) + 1);
1015+
TEST_ASSERT_NOT_EQUAL(0, id2);
1016+
int id3 = equeue_post(&q, simple_func, &(e3.e) + 1);
1017+
TEST_ASSERT_NOT_EQUAL(0, id3);
1018+
int id4 = equeue_post(&q, simple_func, &(e4.e) + 1);
1019+
TEST_ASSERT_NOT_EQUAL(0, id4);
1020+
int id5 = equeue_post(&q, simple_func, &(e5.e) + 1);
1021+
TEST_ASSERT_NOT_EQUAL(0, id5);
1022+
equeue_cancel(&q, id3);
1023+
1024+
equeue_dispatch(&q, 1);
1025+
1026+
TEST_ASSERT_EQUAL_UINT8(1, touched);
1027+
TEST_ASSERT_EQUAL_UINT8(1, e1.touched);
1028+
TEST_ASSERT_EQUAL_UINT8(1, e2.touched);
1029+
TEST_ASSERT_EQUAL_UINT8(0, e3.touched);
1030+
TEST_ASSERT_EQUAL_UINT8(1, e4.touched);
1031+
TEST_ASSERT_EQUAL_UINT8(1, e5.touched);
1032+
1033+
equeue_dispatch(&q, 10);
1034+
1035+
TEST_ASSERT_EQUAL_UINT8(1, touched);
1036+
TEST_ASSERT_EQUAL_UINT8(1, e1.touched);
1037+
TEST_ASSERT_EQUAL_UINT8(1, e2.touched);
1038+
TEST_ASSERT_EQUAL_UINT8(0, e3.touched);
1039+
TEST_ASSERT_EQUAL_UINT8(1, e4.touched);
1040+
TEST_ASSERT_EQUAL_UINT8(1, e5.touched);
1041+
1042+
equeue_destroy(&q);
1043+
}
9801044

9811045
Case cases[] = {
9821046
Case("simple call test", test_equeue_simple_call),
@@ -1006,7 +1070,8 @@ Case cases[] = {
10061070
Case("fragmenting barrage test", test_equeue_fragmenting_barrage<10>),
10071071
Case("multithreaded barrage test", test_equeue_multithreaded_barrage<10>),
10081072
Case("break request cleared on timeout test", test_equeue_break_request_cleared_on_timeout),
1009-
Case("sibling test", test_equeue_sibling)
1073+
Case("sibling test", test_equeue_sibling),
1074+
Case("user allocated event test", test_equeue_user_allocated_event_post)
10101075

10111076
};
10121077

UNITTESTS/events/equeue/test_equeue.cpp

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,4 +994,68 @@ 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+
int id1 = equeue_post(&q, simple_func, &(e1.e) + 1);
1031+
EXPECT_NE(0, id1);
1032+
int id2 = equeue_post(&q, simple_func, &(e2.e) + 1);
1033+
EXPECT_NE(0, id2);
1034+
int id3 = equeue_post(&q, simple_func, &(e3.e) + 1);
1035+
EXPECT_NE(0, id3);
1036+
int id4 = equeue_post(&q, simple_func, &(e4.e) + 1);
1037+
EXPECT_NE(0, id4);
1038+
int id5 = equeue_post(&q, simple_func, &(e5.e) + 1);
1039+
EXPECT_NE(0, id5);
1040+
equeue_cancel(&q, id3);
1041+
1042+
equeue_dispatch(&q, 1);
1043+
1044+
EXPECT_EQ(1, touched);
1045+
EXPECT_EQ(1, e1.touched);
1046+
EXPECT_EQ(1, e2.touched);
1047+
EXPECT_EQ(0, e3.touched);
1048+
EXPECT_EQ(1, e4.touched);
1049+
EXPECT_EQ(1, e5.touched);
1050+
1051+
equeue_dispatch(&q, 10);
1052+
1053+
EXPECT_EQ(1, touched);
1054+
EXPECT_EQ(1, e1.touched);
1055+
EXPECT_EQ(1, e2.touched);
1056+
EXPECT_EQ(0, e3.touched);
1057+
EXPECT_EQ(1, e4.touched);
1058+
EXPECT_EQ(1, e5.touched);
1059+
1060+
equeue_destroy(&q);
1061+
}

events/source/tests/tests.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,61 @@ 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+
int id1 = equeue_post(&q, simple_func, &(e1.e) + 1);
828+
test_assert(0 != id1);
829+
int id2 = equeue_post(&q, simple_func, &(e2.e) + 1);
830+
test_assert(0 != id2);
831+
int id3 = equeue_post(&q, simple_func, &(e3.e) + 1);
832+
test_assert(0 != id3);
833+
int id4 = equeue_post(&q, simple_func, &(e4.e) + 1);
834+
test_assert(0 != id4);
835+
int id5 = equeue_post(&q, simple_func, &(e5.e) + 1);
836+
test_assert(0 != id5);
837+
equeue_cancel(&q, id3);
838+
839+
equeue_dispatch(&q, 1);
840+
841+
test_assert(true == touched);
842+
test_assert(true == e1.touched);
843+
test_assert(true == e2.touched);
844+
test_assert(false == e3.touched);
845+
test_assert(true == e4.touched);
846+
test_assert(true == e5.touched);
847+
848+
equeue_dispatch(&q, 10);
849+
850+
test_assert(true == touched);
851+
test_assert(true == e1.touched);
852+
test_assert(true == e2.touched);
853+
test_assert(false == e3.touched);
854+
test_assert(true == e4.touched);
855+
test_assert(true == e5.touched);
856+
857+
equeue_destroy(&q);
858+
}
859+
805860
int main()
806861
{
807862
printf("beginning tests...\n");
@@ -830,6 +885,7 @@ int main()
830885
test_run(multithreaded_barrage_test, 20);
831886
test_run(break_request_cleared_on_timeout);
832887
test_run(sibling_test);
888+
test_run(user_allocated_event_test);
833889
printf("done!\n");
834890
return test_failure;
835891
}

0 commit comments

Comments
 (0)