Skip to content

Commit

Permalink
Queue test: cover more branches to improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
polesskiy-dev committed Oct 18, 2023
1 parent e2e190c commit a78e7fc
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/event_queue/event_queue.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "./event_queue.h"

#include "event_queue.h"

void EventQueue_Initialize(TEventQueue* queue, TEvent* events, uint32_t capacity) {
queue->events = events;
queue->capacity = capacity;
Expand Down
77 changes: 77 additions & 0 deletions test/event_queue/event_queue.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,23 @@ void test_EventQueue_Enqueue(void) {
TEST_ASSERT_FALSE(EventQueue_IsEmpty(&queue));
}

void test_EventQueue_Enqueue_FullQueue(void) {
// Fill the queue to its capacity
for (int i = 0; i < QUEUE_MAX_CAPACITY; ++i) {
TEvent event = {i, NULL, 0};
bool enqueueResult = EventQueue_Enqueue(&queue, event);
TEST_ASSERT_TRUE(enqueueResult);
}

// Try to enqueue one more event
TEvent extraEvent = {TEST_SIG_1, NULL, 0};
bool enqueueResult = EventQueue_Enqueue(&queue, extraEvent);

// Check that enqueueResult is false, indicating that the queue is full
TEST_ASSERT_FALSE(enqueueResult);
}


void test_EventQueue_Dequeue(void) {
TEvent event = {TEST_SIG_1, NULL, 0};
EventQueue_Enqueue(&queue, event);
Expand All @@ -49,6 +66,47 @@ void test_EventQueue_Dequeue(void) {
TEST_ASSERT_TRUE(EventQueue_IsEmpty(&queue));
}

void test_EventQueue_Dequeue_EmptyQueue(void) {
// Ensure the queue is empty
TEST_ASSERT_TRUE(EventQueue_IsEmpty(&queue));

// Try to dequeue an event from an empty queue
TEvent dequeuedEvent = EventQueue_Dequeue(&queue);

// Check that the dequeuedEvent is an empty event (all fields set to zero)
TEST_ASSERT_EQUAL_INT(0, dequeuedEvent.sig);
TEST_ASSERT_NULL(dequeuedEvent.payload);
TEST_ASSERT_EQUAL_INT(0, dequeuedEvent.size);
}

void test_EventQueue_Dequeue_FrontEqualsRear(void) {
// Enqueue an event
TEvent event = {TEST_SIG_1, NULL, 0};
EventQueue_Enqueue(&queue, event);

// Dequeue the event, causing front and rear to become equal
TEvent dequeuedEvent = EventQueue_Dequeue(&queue);

// Check that front and rear are set to -1
TEST_ASSERT_EQUAL_INT(-1, queue.front);
TEST_ASSERT_EQUAL_INT(-1, queue.rear);
}

void test_EventQueue_Dequeue_FrontNotEqualsRear(void) {
// Enqueue two events
TEvent event1 = {TEST_SIG_1, NULL, 0};
TEvent event2 = {TEST_SIG_2, NULL, 0};
EventQueue_Enqueue(&queue, event1);
EventQueue_Enqueue(&queue, event2);

// Dequeue the first event, front and rear should not be equal
TEvent dequeuedEvent = EventQueue_Dequeue(&queue);

// Check that front is incremented
TEST_ASSERT_EQUAL_INT(1, queue.front);
TEST_ASSERT_EQUAL_INT(-1, queue.rear);
}

void test_EventQueue_Peek(void) {
TEvent event = {TEST_SIG_1, NULL, 0};
EventQueue_Enqueue(&queue, event);
Expand All @@ -58,6 +116,20 @@ void test_EventQueue_Peek(void) {
TEST_ASSERT_EQUAL_INT(TEST_SIG_1, peekedEvent.sig);
}

void test_EventQueue_Peek_EmptyQueue(void) {
// Ensure the queue is empty
TEST_ASSERT_TRUE(EventQueue_IsEmpty(&queue));

// Try to peek into an empty queue
TEvent peekedEvent = EventQueue_Peek(&queue);

// Check that the peekedEvent is an empty event (all fields set to zero)
TEST_ASSERT_EQUAL_INT(0, peekedEvent.sig);
TEST_ASSERT_NULL(peekedEvent.payload);
TEST_ASSERT_EQUAL_INT(0, peekedEvent.size);
}


void test_EventQueue_IsEmpty(void) {
TEST_ASSERT_TRUE(EventQueue_IsEmpty(&queue));
}
Expand All @@ -76,8 +148,13 @@ int main(void) {
UNITY_BEGIN();
RUN_TEST(test_EventQueue_Initialize);
RUN_TEST(test_EventQueue_Enqueue);
RUN_TEST(test_EventQueue_Enqueue_FullQueue);
RUN_TEST(test_EventQueue_Dequeue);
RUN_TEST(test_EventQueue_Dequeue_EmptyQueue);
RUN_TEST(test_EventQueue_Dequeue_FrontEqualsRear);
RUN_TEST(test_EventQueue_Dequeue_FrontNotEqualsRear);
RUN_TEST(test_EventQueue_Peek);
RUN_TEST(test_EventQueue_Peek_EmptyQueue);
RUN_TEST(test_EventQueue_IsEmpty);
RUN_TEST(test_EventQueue_IsFull);
return UNITY_END();
Expand Down

0 comments on commit a78e7fc

Please sign in to comment.