|
| 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 | +} |
0 commit comments