Skip to content

Commit 9b3df5b

Browse files
committed
tests/unittests: add unit tests for core_mbox
1 parent 54b0100 commit 9b3df5b

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
USEMODULE += core_mbox
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (C) 2022 Otto-von-Guericke-Universität Magdeburg
3+
*
4+
* This file is subject to the terms and conditions of the GNU Lesser
5+
* General Public License v2.1. See the file LICENSE in the top level
6+
* directory for more details.
7+
*/
8+
9+
#include <limits.h>
10+
11+
#include "embUnit.h"
12+
13+
#include "mbox.h"
14+
15+
#include "tests-core.h"
16+
17+
#define QUEUE_SIZE 8
18+
19+
static unsigned gen_val(unsigned i) {
20+
return i + 1337;
21+
}
22+
23+
static void test_mbox_put_get(void)
24+
{
25+
mbox_t mbox;
26+
msg_t queue[QUEUE_SIZE];
27+
msg_t msg = { .type = 0 };
28+
mbox_init(&mbox, queue, ARRAY_SIZE(queue));
29+
TEST_ASSERT_EQUAL_INT(0, mbox_avail(&mbox));
30+
31+
/* Filling the queue up to capacity one item at a time. This should
32+
* succeed every single time. */
33+
for (unsigned i = 0; i < ARRAY_SIZE(queue); i++) {
34+
msg.type = i;
35+
msg.content.value = gen_val(i);
36+
TEST_ASSERT_EQUAL_INT(1, mbox_try_put(&mbox, &msg));
37+
}
38+
39+
/* Adding one item over capacity must fail. */
40+
msg.type = 4242;
41+
msg.content.value = 4242;
42+
TEST_ASSERT_EQUAL_INT(0, mbox_try_put(&mbox, &msg));
43+
44+
/* The queue must contain the items we filled in and in that order. */
45+
for (unsigned i = 0; i < ARRAY_SIZE(queue); i++) {
46+
TEST_ASSERT_EQUAL_INT(i, queue[i].type);
47+
TEST_ASSERT_EQUAL_INT(gen_val(i), queue[i].content.value);
48+
}
49+
50+
/* Now we drain the queue one item at a time. We expect to get the exact
51+
* items we filled in and in that order. */
52+
for (unsigned i = 0; i < ARRAY_SIZE(queue); i++) {
53+
TEST_ASSERT_EQUAL_INT(1, mbox_try_get(&mbox, &msg));
54+
TEST_ASSERT_EQUAL_INT(i, msg.type);
55+
TEST_ASSERT_EQUAL_INT(gen_val(i), msg.content.value);
56+
}
57+
58+
/* The queue is now empty. Getting one more item (non-blocking) must fail */
59+
TEST_ASSERT_EQUAL_INT(0, mbox_try_get(&mbox, &msg));
60+
}
61+
62+
Test *tests_core_mbox_tests(void)
63+
{
64+
EMB_UNIT_TESTFIXTURES(fixtures) {
65+
new_TestFixture(test_mbox_put_get),
66+
};
67+
68+
EMB_UNIT_TESTCALLER(core_mbox_tests, NULL, NULL, fixtures);
69+
70+
return (Test *)&core_mbox_tests;
71+
}

tests/unittests/tests-core/tests-core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ void tests_core(void)
1515
TESTS_RUN(tests_core_cib_tests());
1616
TESTS_RUN(tests_core_clist_tests());
1717
TESTS_RUN(tests_core_list_tests());
18+
TESTS_RUN(tests_core_mbox_tests());
1819
TESTS_RUN(tests_core_priority_queue_tests());
1920
TESTS_RUN(tests_core_byteorder_tests());
2021
TESTS_RUN(tests_core_ringbuffer_tests());

tests/unittests/tests-core/tests-core.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ Test *tests_core_clist_tests(void);
6464
*/
6565
Test *tests_core_list_tests(void);
6666

67+
/**
68+
* @brief Generates tests for mbox.h
69+
*
70+
* @return embUnit tests if successful, NULL if not.
71+
*/
72+
Test *tests_core_mbox_tests(void);
73+
6774
/**
6875
* @brief Generates tests for priority_queue.h
6976
*

0 commit comments

Comments
 (0)