Skip to content

Commit b551870

Browse files
Yuka Furutayashi
Yuka Furuta
authored andcommitted
Adding tests for queue functions
This commit adds simple test code for csp_queue_enqueue, csp_queue_dequeue, csp_queue_size, and csp_queue_free. test_queue_enqueue_dequeue_one is a basic test that verifies the dequeued content is exactly the same as what was enqueued. test_queue_enqueue_dequeue_full checks that enqueue and dequeue operations can be performed for the entire allocated queue size. test_queue_enqueue_overflow and test_queue_dequeue_underflow check if overflow during enqueue and underflow during dequeue trigger errors. test_queue_size and test_queue_free ensure that size and free operations reflect an increment and decrement after one enqueue and one dequeue operation, respectively. Signed-off-by: Yuka Furuta <[email protected]>
1 parent 6e41823 commit b551870

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ target_sources(app PRIVATE
99
src/main.c
1010
src/buffer.c
1111
src/crc32.c
12+
src/queue.c
1213
)

src/queue.c

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* Copyright (c) 2024 Space Cubics, LLC.
3+
*
4+
* SPDX - License - Identifier: Apache - 2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
#include <csp/csp.h>
9+
10+
/* test enqueue, dequeue */
11+
ZTEST(queue, test_queue_enqueue_dequeue_one)
12+
{
13+
char item1[] = "abc";
14+
char item2[sizeof(item1)];
15+
16+
int qlength = 10;
17+
int buf_size = qlength * sizeof(item1);
18+
char buf[buf_size];
19+
20+
csp_queue_handle_t qh;
21+
csp_static_queue_t q;
22+
23+
/* zero clear */
24+
memset(buf, 0, buf_size);
25+
memset(item2, 0, sizeof(item2));
26+
27+
csp_init();
28+
29+
/* create */
30+
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);
31+
32+
/* enqueue, dequeue */
33+
zassert_equal(csp_queue_enqueue(qh, item1, 1000), CSP_QUEUE_OK);
34+
zassert_equal(csp_queue_dequeue(qh, item2, 1000), CSP_QUEUE_OK);
35+
36+
/* compare item1 & item2 */
37+
zassert_equal(memcmp(item1, item2, sizeof(item1)), 0);
38+
}
39+
40+
/* test enqueue, dequeue when full */
41+
ZTEST(queue, test_queue_enqueue_dequeue_full)
42+
{
43+
char item1[] = "abc";
44+
char item2[sizeof(item1)];
45+
46+
int qlength = 10;
47+
int buf_size = qlength * sizeof(item1);
48+
char buf[buf_size];
49+
50+
csp_queue_handle_t qh;
51+
csp_static_queue_t q;
52+
53+
/* zero clear */
54+
memset(buf, 0, buf_size);
55+
memset(item2, 0, sizeof(item2));
56+
57+
csp_init();
58+
59+
/* create */
60+
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);
61+
62+
/* enqueue to the limit */
63+
for (int i = 0; i < qlength; i++) {
64+
zassert_equal(csp_queue_enqueue(qh, item1, 1000), CSP_QUEUE_OK);
65+
}
66+
/* dequeue to the limit */
67+
for (int i = 0; i < qlength; i++) {
68+
zassert_equal(csp_queue_dequeue(qh, buf, 1000), CSP_QUEUE_OK);
69+
}
70+
}
71+
72+
/* test when full, enqueue additionally */
73+
ZTEST(queue, test_queue_enqueue_overflow)
74+
{
75+
char item1[] = "abc";
76+
char item2[sizeof(item1)];
77+
78+
int qlength = 10;
79+
int buf_size = qlength * sizeof(item1);
80+
char buf[buf_size];
81+
82+
csp_queue_handle_t qh;
83+
csp_static_queue_t q;
84+
85+
/* zero clear */
86+
memset(buf, 0, buf_size);
87+
memset(item2, 0, sizeof(item2));
88+
89+
csp_init();
90+
91+
/* create */
92+
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);
93+
94+
/* enqueue to the limit */
95+
for (int i = 0; i < qlength; i++) {
96+
zassert_equal(csp_queue_enqueue(qh, item1, 1000), CSP_QUEUE_OK);
97+
}
98+
99+
/* The queue is full, so enqueue operation throws an error */
100+
zassert_equal(csp_queue_enqueue(qh, item1, 1000), CSP_QUEUE_ERROR);
101+
}
102+
103+
/* test when empty, dequeue one more */
104+
ZTEST(queue, test_queue_dequeue_underflow)
105+
{
106+
char item1[] = "abc";
107+
char item2[sizeof(item1)];
108+
109+
int qlength = 10;
110+
int buf_size = qlength * sizeof(item1);
111+
char buf[buf_size];
112+
113+
csp_queue_handle_t qh;
114+
csp_static_queue_t q;
115+
116+
/* zero clear */
117+
memset(buf, 0, buf_size);
118+
memset(item2, 0, sizeof(item2));
119+
120+
csp_init();
121+
122+
/* create */
123+
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);
124+
125+
/* The queue is empty, so dequeue operation throws an error */
126+
zassert_equal(csp_queue_dequeue(qh, item2, 1000), CSP_QUEUE_ERROR);
127+
}
128+
129+
/* test queue_free for queue size */
130+
ZTEST(queue, test_queue_size)
131+
{
132+
char item1[] = "abc";
133+
char item2[sizeof(item1)];
134+
135+
int qlength = 10;
136+
int buf_size = qlength * sizeof(item1);
137+
char buf[buf_size];
138+
139+
csp_queue_handle_t qh;
140+
csp_static_queue_t q;
141+
int qsize;
142+
143+
/* zero clear */
144+
memset(buf, 0, buf_size);
145+
memset(item2, 0, sizeof(item2));
146+
147+
csp_init();
148+
149+
/* create */
150+
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);
151+
152+
/* Before enqueue, size is 0 */
153+
qsize = csp_queue_size(qh);
154+
zassert_equal(qsize, 0);
155+
156+
/* After enqueue, size is 1 */
157+
csp_queue_enqueue(qh, item1, 1000);
158+
zassert_equal(csp_queue_size(qh), qsize + 1);
159+
160+
/* After dequeue, size is 1 */
161+
qsize = csp_queue_size(qh);
162+
csp_queue_dequeue(qh, item2, 1000);
163+
zassert_equal(csp_queue_size(qh), qsize - 1);
164+
}
165+
166+
/* test queue_free for available size */
167+
ZTEST(queue, test_queue_free)
168+
{
169+
char item1[] = "abc";
170+
char item2[sizeof(item1)];
171+
172+
int qlength = 10;
173+
int buf_size = qlength * sizeof(item1);
174+
char buf[buf_size];
175+
176+
csp_queue_handle_t qh;
177+
csp_static_queue_t q;
178+
179+
/* zero clear */
180+
memset(buf, 0, buf_size);
181+
memset(item2, 0, sizeof(item2));
182+
183+
csp_init();
184+
185+
/* create */
186+
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);
187+
188+
/* Before enqueue, the available size is qlength */
189+
zassert_equal(csp_queue_free(qh), qlength);
190+
191+
/* After enqueue, the available size is qlength - 1. */
192+
csp_queue_enqueue(qh, item1, 1000);
193+
zassert_equal(csp_queue_free(qh), qlength - 1);
194+
195+
/* After dequeue, the available size is qlength */
196+
csp_queue_dequeue(qh, item2, 1000);
197+
zassert_equal(csp_queue_free(qh), qlength);
198+
}
199+
200+
ZTEST_SUITE(queue, NULL, NULL, NULL, NULL, NULL);

0 commit comments

Comments
 (0)