Skip to content

Commit fec4e20

Browse files
committed
queue: get rid of printf, return bool result on enqueue, add unit tests for corner cases
1 parent bb10ba9 commit fec4e20

File tree

4 files changed

+37
-78
lines changed

4 files changed

+37
-78
lines changed

Makefile

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/queue/queue.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
T elements[maxQueueCapacity]; \
4242
} QUEUE_##T; \
4343
void QUEUE_##T##_Ctor(QUEUE_##T *const q); \
44-
void QUEUE_##T##_Enqueue(QUEUE_##T *const q, T item); \
44+
bool QUEUE_##T##_Enqueue(QUEUE_##T *const q, T item); \
4545
T QUEUE_##T##_Dequeue(QUEUE_##T *const q); \
4646
int QUEUE_##T##_GetSize(QUEUE_##T *const q); \
4747
T QUEUE_##T##_Peek(QUEUE_##T *const q); \
48-
bool QUEUE_##T##_IsFull(QUEUE_##T *const q);
48+
bool QUEUE_##T##_IsFull(QUEUE_##T *const q);
4949

5050
/**
5151
* @struct QUEUE_T
@@ -78,20 +78,22 @@ void QUEUE_T_Ctor(QUEUE_T *const q);
7878
* @param q Pointer to the queue.
7979
* @param item The item to be enqueued.
8080
*
81+
* @return `true` if element enqueued (queue wasn't full), `false` otherwise.
82+
*
8183
* ####Example:
8284
* @code
8385
* QUEUE_int q;
8486
* QUEUE_int_Ctor(&q);
8587
* QUEUE_int_Enqueue(&q, 5);
8688
* @endcode
8789
*/
88-
void QUEUE_T_Enqueue(QUEUE_T *const q, T item);
90+
bool QUEUE_T_Enqueue(QUEUE_T *const q, T item);
8991

9092
/**
9193
* @brief Dequeues an element from the queue.
9294
*
9395
* @param q Pointer to the queue.
94-
* @return The dequeued item.
96+
* @return The dequeued item or NULL
9597
*
9698
* ####Example:
9799
* @code
@@ -123,7 +125,7 @@ int QUEUE_T_GetSize(QUEUE_T *const q);
123125
* @brief Peeks at the front element of the queue.
124126
*
125127
* @param q Pointer to the queue.
126-
* @return The element at the front of the queue.
128+
* @return The element at the front of the queue or NULL
127129
*
128130
* ####Example:
129131
* @code

src/queue/queue_impl.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@
99
q->rear = -1; \
1010
} \
1111
\
12-
void QUEUE_##T##_Enqueue(QUEUE_##T *const q, T item) { \
12+
bool QUEUE_##T##_Enqueue(QUEUE_##T *const q, T item) { \
1313
if (((q->rear + 1) % maxQueueCapacity) == q->front) { \
14-
printf("Queue is full. Cannot enqueue.\n"); \
15-
return; \
14+
/* Queue is full. Cannot enqueue.*/ \
15+
return false; \
1616
} \
1717
q->rear = (q->rear + 1) % maxQueueCapacity; \
1818
q->elements[q->rear] = item; \
1919
if (q->front == -1) { \
2020
q->front = 0; \
2121
} \
22+
return true; \
2223
} \
2324
\
2425
T QUEUE_##T##_Dequeue(QUEUE_##T *const q) { \
2526
if (q->front == -1) { \
26-
printf("Queue is empty. Cannot dequeue.\n"); \
27+
/* Queue is empty. Cannot dequeue. */ \
2728
return (T){0}; \
2829
} \
2930
T item = q->elements[q->front]; \
@@ -49,7 +50,7 @@
4950
\
5051
T QUEUE_##T##_Peek(QUEUE_##T *const q) { \
5152
if (q->front == -1) { \
52-
printf("Queue is empty. Cannot peek.\n"); \
53+
/* "Queue is empty. Cannot peek.\n"); */ \
5354
return (T){0}; \
5455
} \
5556
return q->elements[q->front]; \

test/queue/queue.test.c

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
#include "../../src/queue/queue_impl.h"
44

55
// We're using a queue of integers for simplicity
6-
#define QUEUE_SIZE 5
6+
#define QUEUE_SIZE 4
77
DECLARE_QUEUE(int, QUEUE_SIZE)
88
QUEUE_IMPLEMENTATION(int, QUEUE_SIZE)
99

1010
QUEUE_int q;
1111

1212
void setUp(void) {
1313
// This is run before EACH test
14-
QUEUE_int_Ctor(&q); // constructor
14+
QUEUE_int_Ctor(&q); // constructor, BEWARE: it's not emptying actual elements
1515
}
1616

1717
void tearDown(void) {
@@ -48,11 +48,33 @@ void test_queue_isfull(void) {
4848
TEST_ASSERT_TRUE(QUEUE_int_IsFull(&q));
4949
}
5050

51+
void test_queue_dequeue_empty(void) {
52+
TEST_ASSERT_EQUAL_INT(0, QUEUE_int_Dequeue(&q));
53+
TEST_ASSERT_EQUAL_INT(0, QUEUE_int_GetSize(&q));
54+
}
55+
56+
void test_queue_peek_empty(void) {
57+
TEST_ASSERT_EQUAL_INT(0, QUEUE_int_Peek(&q));
58+
TEST_ASSERT_EQUAL_INT(0, QUEUE_int_GetSize(&q));
59+
}
60+
61+
void test_queue_enqueue_full(void) {
62+
for (int i = 0; i < QUEUE_SIZE; i++) {
63+
TEST_ASSERT_TRUE(QUEUE_int_Enqueue(&q, i));
64+
}
65+
TEST_ASSERT_FALSE(QUEUE_int_Enqueue(&q, 99));
66+
TEST_ASSERT_EQUAL_INT(QUEUE_SIZE, QUEUE_int_GetSize(&q));
67+
}
68+
5169
int main(void) {
5270
UNITY_BEGIN();
5371
RUN_TEST(test_queue_ctor);
5472
RUN_TEST(test_queue_enqueue);
5573
RUN_TEST(test_queue_dequeue);
5674
RUN_TEST(test_queue_isfull);
75+
RUN_TEST(test_queue_dequeue_empty);
76+
RUN_TEST(test_queue_peek_empty);
77+
RUN_TEST(test_queue_enqueue_full);
78+
5779
return UNITY_END();
5880
}

0 commit comments

Comments
 (0)