Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding tests for queue functions #3

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ target_sources(app PRIVATE
src/main.c
src/buffer.c
src/crc32.c
src/queue.c
)
200 changes: 200 additions & 0 deletions src/queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* Copyright (c) 2024 Space Cubics, LLC.
*
* SPDX - License - Identifier: Apache - 2.0
*/

#include <zephyr/ztest.h>
#include <csp/csp.h>

/* test enqueue, dequeue */
ZTEST(queue, test_queue_enqueue_dequeue_one)
{
char item1[] = "abc";
char item2[sizeof(item1)];

int qlength = 10;
int buf_size = qlength * sizeof(item1);
char buf[buf_size];

csp_queue_handle_t qh;
csp_static_queue_t q;

/* zero clear */
memset(buf, 0, buf_size);
memset(item2, 0, sizeof(item2));

csp_init();

/* create */
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);

/* enqueue, dequeue */
zassert_equal(csp_queue_enqueue(qh, item1, 1000), CSP_QUEUE_OK);
zassert_equal(csp_queue_dequeue(qh, item2, 1000), CSP_QUEUE_OK);

/* compare item1 & item2 */
zassert_equal(memcmp(item1, item2, sizeof(item1)), 0);
}

/* test enqueue, dequeue when full */
ZTEST(queue, test_queue_enqueue_dequeue_full)
{
char item1[] = "abc";
char item2[sizeof(item1)];

int qlength = 10;
int buf_size = qlength * sizeof(item1);
char buf[buf_size];

csp_queue_handle_t qh;
csp_static_queue_t q;

/* zero clear */
memset(buf, 0, buf_size);
memset(item2, 0, sizeof(item2));

csp_init();

/* create */
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);

/* enqueue to the limit */
for (int i = 0; i < qlength; i++) {
zassert_equal(csp_queue_enqueue(qh, item1, 1000), CSP_QUEUE_OK);
}
/* dequeue to the limit */
for (int i = 0; i < qlength; i++) {
zassert_equal(csp_queue_dequeue(qh, buf, 1000), CSP_QUEUE_OK);
}
}

/* test when full, enqueue additionally */
ZTEST(queue, test_queue_enqueue_overflow)
{
char item1[] = "abc";
char item2[sizeof(item1)];

int qlength = 10;
int buf_size = qlength * sizeof(item1);
char buf[buf_size];

csp_queue_handle_t qh;
csp_static_queue_t q;

/* zero clear */
memset(buf, 0, buf_size);
memset(item2, 0, sizeof(item2));

csp_init();

/* create */
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);

/* enqueue to the limit */
for (int i = 0; i < qlength; i++) {
zassert_equal(csp_queue_enqueue(qh, item1, 1000), CSP_QUEUE_OK);
}

/* The queue is full, so enqueue operation throws an error */
zassert_equal(csp_queue_enqueue(qh, item1, 1000), CSP_QUEUE_ERROR);
}

/* test when empty, dequeue one more */
ZTEST(queue, test_queue_dequeue_underflow)
{
char item1[] = "abc";
char item2[sizeof(item1)];

int qlength = 10;
int buf_size = qlength * sizeof(item1);
char buf[buf_size];

csp_queue_handle_t qh;
csp_static_queue_t q;

/* zero clear */
memset(buf, 0, buf_size);
memset(item2, 0, sizeof(item2));

csp_init();

/* create */
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);

/* The queue is empty, so dequeue operation throws an error */
zassert_equal(csp_queue_dequeue(qh, item2, 1000), CSP_QUEUE_ERROR);
}

/* test queue_free for queue size */
ZTEST(queue, test_queue_size)
{
char item1[] = "abc";
char item2[sizeof(item1)];

int qlength = 10;
int buf_size = qlength * sizeof(item1);
char buf[buf_size];

csp_queue_handle_t qh;
csp_static_queue_t q;
int qsize;

/* zero clear */
memset(buf, 0, buf_size);
memset(item2, 0, sizeof(item2));

csp_init();

/* create */
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);

/* Before enqueue, size is 0 */
qsize = csp_queue_size(qh);
zassert_equal(qsize, 0);

/* After enqueue, size is 1 */
csp_queue_enqueue(qh, item1, 1000);
zassert_equal(csp_queue_size(qh), qsize + 1);

/* After dequeue, size is 1 */
qsize = csp_queue_size(qh);
csp_queue_dequeue(qh, item2, 1000);
zassert_equal(csp_queue_size(qh), qsize - 1);
}

/* test queue_free for available size */
ZTEST(queue, test_queue_free)
{
char item1[] = "abc";
char item2[sizeof(item1)];

int qlength = 10;
int buf_size = qlength * sizeof(item1);
char buf[buf_size];

csp_queue_handle_t qh;
csp_static_queue_t q;

/* zero clear */
memset(buf, 0, buf_size);
memset(item2, 0, sizeof(item2));

csp_init();

/* create */
qh = csp_queue_create_static(qlength, sizeof(item1), buf, &q);

/* Before enqueue, the available size is qlength */
zassert_equal(csp_queue_free(qh), qlength);

/* After enqueue, the available size is qlength - 1. */
csp_queue_enqueue(qh, item1, 1000);
zassert_equal(csp_queue_free(qh), qlength - 1);

/* After dequeue, the available size is qlength */
csp_queue_dequeue(qh, item2, 1000);
zassert_equal(csp_queue_free(qh), qlength);
}

ZTEST_SUITE(queue, NULL, NULL, NULL, NULL, NULL);