mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
tests/unittests: add unit tests for core_mbox
This commit is contained in:
parent
54b0100c44
commit
9b3df5b4ff
1
tests/unittests/tests-core/Makefile.include
Normal file
1
tests/unittests/tests-core/Makefile.include
Normal file
@ -0,0 +1 @@
|
||||
USEMODULE += core_mbox
|
71
tests/unittests/tests-core/tests-core-mbox.c
Normal file
71
tests/unittests/tests-core/tests-core-mbox.c
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Otto-von-Guericke-Universität Magdeburg
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include "embUnit.h"
|
||||
|
||||
#include "mbox.h"
|
||||
|
||||
#include "tests-core.h"
|
||||
|
||||
#define QUEUE_SIZE 8
|
||||
|
||||
static unsigned gen_val(unsigned i) {
|
||||
return i + 1337;
|
||||
}
|
||||
|
||||
static void test_mbox_put_get(void)
|
||||
{
|
||||
mbox_t mbox;
|
||||
msg_t queue[QUEUE_SIZE];
|
||||
msg_t msg = { .type = 0 };
|
||||
mbox_init(&mbox, queue, ARRAY_SIZE(queue));
|
||||
TEST_ASSERT_EQUAL_INT(0, mbox_avail(&mbox));
|
||||
|
||||
/* Filling the queue up to capacity one item at a time. This should
|
||||
* succeed every single time. */
|
||||
for (unsigned i = 0; i < ARRAY_SIZE(queue); i++) {
|
||||
msg.type = i;
|
||||
msg.content.value = gen_val(i);
|
||||
TEST_ASSERT_EQUAL_INT(1, mbox_try_put(&mbox, &msg));
|
||||
}
|
||||
|
||||
/* Adding one item over capacity must fail. */
|
||||
msg.type = 4242;
|
||||
msg.content.value = 4242;
|
||||
TEST_ASSERT_EQUAL_INT(0, mbox_try_put(&mbox, &msg));
|
||||
|
||||
/* The queue must contain the items we filled in and in that order. */
|
||||
for (unsigned i = 0; i < ARRAY_SIZE(queue); i++) {
|
||||
TEST_ASSERT_EQUAL_INT(i, queue[i].type);
|
||||
TEST_ASSERT_EQUAL_INT(gen_val(i), queue[i].content.value);
|
||||
}
|
||||
|
||||
/* Now we drain the queue one item at a time. We expect to get the exact
|
||||
* items we filled in and in that order. */
|
||||
for (unsigned i = 0; i < ARRAY_SIZE(queue); i++) {
|
||||
TEST_ASSERT_EQUAL_INT(1, mbox_try_get(&mbox, &msg));
|
||||
TEST_ASSERT_EQUAL_INT(i, msg.type);
|
||||
TEST_ASSERT_EQUAL_INT(gen_val(i), msg.content.value);
|
||||
}
|
||||
|
||||
/* The queue is now empty. Getting one more item (non-blocking) must fail */
|
||||
TEST_ASSERT_EQUAL_INT(0, mbox_try_get(&mbox, &msg));
|
||||
}
|
||||
|
||||
Test *tests_core_mbox_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
new_TestFixture(test_mbox_put_get),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(core_mbox_tests, NULL, NULL, fixtures);
|
||||
|
||||
return (Test *)&core_mbox_tests;
|
||||
}
|
@ -15,6 +15,7 @@ void tests_core(void)
|
||||
TESTS_RUN(tests_core_cib_tests());
|
||||
TESTS_RUN(tests_core_clist_tests());
|
||||
TESTS_RUN(tests_core_list_tests());
|
||||
TESTS_RUN(tests_core_mbox_tests());
|
||||
TESTS_RUN(tests_core_priority_queue_tests());
|
||||
TESTS_RUN(tests_core_byteorder_tests());
|
||||
TESTS_RUN(tests_core_ringbuffer_tests());
|
||||
|
@ -64,6 +64,13 @@ Test *tests_core_clist_tests(void);
|
||||
*/
|
||||
Test *tests_core_list_tests(void);
|
||||
|
||||
/**
|
||||
* @brief Generates tests for mbox.h
|
||||
*
|
||||
* @return embUnit tests if successful, NULL if not.
|
||||
*/
|
||||
Test *tests_core_mbox_tests(void);
|
||||
|
||||
/**
|
||||
* @brief Generates tests for priority_queue.h
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user