Skip to content

Commit

Permalink
equeue tests: add user allocated events tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejbocianski committed Aug 27, 2019
1 parent 4fa864a commit 3af1024
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 2 deletions.
57 changes: 56 additions & 1 deletion TESTS/events/equeue/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,60 @@ static void test_equeue_sibling()
equeue_destroy(&q);
}

struct user_allocated_event {
struct equeue_event e;
uint8_t touched;
};

/** Test that equeue executes user allocated events passed by equeue_post.
*
* Given queue is initialized and its size is set to store one event at max in its internal memory.
* When post events allocated in queues internal memory (what is done by calling equeue_call).
* Then only one event can be posted due to queue memory size.
* When post user allocated events.
* Then number of posted events is not limited by queue memory size.
* When both queue allocaded and user allocated events are posted and equeue_dispatch is called.
* Then both types of events are executed properly.
*/
static void test_equeue_user_allocated_event_post()
{
equeue_t q;
int err = equeue_create(&q, EQUEUE_EVENT_SIZE);
TEST_ASSERT_EQUAL_INT(0, err);

uint8_t touched = 0;
user_allocated_event e1 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
user_allocated_event e2 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
user_allocated_event e3 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };

TEST_ASSERT_NOT_EQUAL(0, equeue_call(&q, simple_func, &touched));
TEST_ASSERT_EQUAL_INT(0, equeue_call(&q, simple_func, &touched));
TEST_ASSERT_EQUAL_INT(0, equeue_call(&q, simple_func, &touched));

int id1 = equeue_post(&q, simple_func, &(e1.e) + 1);
TEST_ASSERT_NOT_EQUAL(0, id1);
int id2 = equeue_post(&q, simple_func, &(e2.e) + 1);
TEST_ASSERT_NOT_EQUAL(0, id2);
int id3 = equeue_post(&q, simple_func, &(e3.e) + 1);
TEST_ASSERT_NOT_EQUAL(0, id3);
equeue_cancel(&q, id3);

equeue_dispatch(&q, 1);

TEST_ASSERT_EQUAL_UINT8(1, touched);
TEST_ASSERT_EQUAL_UINT8(1, e1.touched);
TEST_ASSERT_EQUAL_UINT8(1, e2.touched);
TEST_ASSERT_EQUAL_UINT8(0, e3.touched);

equeue_dispatch(&q, 10);

TEST_ASSERT_EQUAL_UINT8(1, touched);
TEST_ASSERT_EQUAL_UINT8(1, e1.touched);
TEST_ASSERT_EQUAL_UINT8(1, e2.touched);
TEST_ASSERT_EQUAL_UINT8(0, e3.touched);

equeue_destroy(&q);
}

Case cases[] = {
Case("simple call test", test_equeue_simple_call),
Expand Down Expand Up @@ -1006,7 +1060,8 @@ Case cases[] = {
Case("fragmenting barrage test", test_equeue_fragmenting_barrage<10>),
Case("multithreaded barrage test", test_equeue_multithreaded_barrage<10>),
Case("break request cleared on timeout test", test_equeue_break_request_cleared_on_timeout),
Case("sibling test", test_equeue_sibling)
Case("sibling test", test_equeue_sibling),
Case("user allocated event test", test_equeue_user_allocated_event_post)

};

Expand Down
56 changes: 55 additions & 1 deletion UNITTESTS/events/equeue/test_equeue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,4 +994,58 @@ TEST_F(TestEqueue, test_equeue_sibling)
equeue_cancel(&q, id1);
equeue_cancel(&q, id2);
equeue_destroy(&q);
}
}

/** Test that equeue executes user allocated events passed by equeue_post.
*
* Given queue is initialized and its size is set to store one event at max in its internal memory.
* When post events allocated in queues internal memory (what is done by calling equeue_call).
* Then only one event can be posted due to queue memory size.
* When post user allocated events.
* Then number of posted events is not limited by queue memory size.
* When both queue allocaded and user allocated events are posted and equeue_dispatch is called.
* Then both types of events are executed properly.
*/
TEST_F(TestEqueue, test_equeue_user_allocated_event_post)
{
struct user_allocated_event {
struct equeue_event e;
uint8_t touched;
};
equeue_t q;
int err = equeue_create(&q, EQUEUE_EVENT_SIZE);
ASSERT_EQ(0, err);

uint8_t touched = 0;
user_allocated_event e1 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
user_allocated_event e2 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
user_allocated_event e3 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };

EXPECT_NE(0, equeue_call(&q, simple_func, &touched));
EXPECT_EQ(0, equeue_call(&q, simple_func, &touched));
EXPECT_EQ(0, equeue_call(&q, simple_func, &touched));

int id1 = equeue_post(&q, simple_func, &(e1.e) + 1);
EXPECT_NE(0, id1);
int id2 = equeue_post(&q, simple_func, &(e2.e) + 1);
EXPECT_NE(0, id2);
int id3 = equeue_post(&q, simple_func, &(e3.e) + 1);
EXPECT_NE(0, id3);
equeue_cancel(&q, id3);

equeue_dispatch(&q, 1);

EXPECT_EQ(1, touched);
EXPECT_EQ(1, e1.touched);
EXPECT_EQ(1, e2.touched);
EXPECT_EQ(0, e3.touched);

equeue_dispatch(&q, 10);

EXPECT_EQ(1, touched);
EXPECT_EQ(1, e1.touched);
EXPECT_EQ(1, e2.touched);
EXPECT_EQ(0, e3.touched);

equeue_destroy(&q);
}
46 changes: 46 additions & 0 deletions events/source/tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,51 @@ void sibling_test(void)
equeue_destroy(&q);
}

struct user_allocated_event {
struct equeue_event e;
bool touched;
};

void user_allocated_event_test()
{
equeue_t q;
int err = equeue_create(&q, EQUEUE_EVENT_SIZE);
test_assert(!err);

bool touched = false;
struct user_allocated_event e1 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
struct user_allocated_event e2 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };
struct user_allocated_event e3 = { { 0, 0, 0, NULL, NULL, NULL, 0, -1, NULL, NULL }, 0 };

test_assert(0 != equeue_call(&q, simple_func, &touched));
test_assert(0 == equeue_call(&q, simple_func, &touched));
test_assert(0 == equeue_call(&q, simple_func, &touched));

int id1 = equeue_post(&q, simple_func, &(e1.e) + 1);
test_assert(0 != id1);
int id2 = equeue_post(&q, simple_func, &(e2.e) + 1);
test_assert(0 != id2);
int id3 = equeue_post(&q, simple_func, &(e3.e) + 1);
test_assert(0 != id3);
equeue_cancel(&q, id3);

equeue_dispatch(&q, 1);

test_assert(true == touched);
test_assert(true == e1.touched);
test_assert(true == e2.touched);
test_assert(false == e3.touched);

equeue_dispatch(&q, 10);

test_assert(true == touched);
test_assert(true == e1.touched);
test_assert(true == e2.touched);
test_assert(false == e3.touched);

equeue_destroy(&q);
}

int main()
{
printf("beginning tests...\n");
Expand Down Expand Up @@ -830,6 +875,7 @@ int main()
test_run(multithreaded_barrage_test, 20);
test_run(break_request_cleared_on_timeout);
test_run(sibling_test);
test_run(user_allocated_event_test);
printf("done!\n");
return test_failure;
}

0 comments on commit 3af1024

Please sign in to comment.