| 1 | #include "structures/tests/test_internal.h" |
| 2 | |
| 3 | TEST_GROUP_DECLARE(mpmc_queue); |
| 4 | |
| 5 | TEST_DECLARE_UNIT(mpmc_queue, basic_enqueue_dequeue) { |
| 6 | struct mpmc_queue q; |
| 7 | bool ok = mpmc_queue_init(q: &q, capacity: 8); |
| 8 | TEST_ASSERT(ok); |
| 9 | TEST_ASSERT_EQ(q.capacity, 8); |
| 10 | TEST_ASSERT(mpmc_queue_empty(&q)); |
| 11 | |
| 12 | /* Enqueue up to capacity */ |
| 13 | for (uintptr_t i = 1; i <= 8; i++) { |
| 14 | TEST_ASSERT(mpmc_queue_enqueue_uintptr(&q, i * 10)); |
| 15 | } |
| 16 | |
| 17 | /* 9th item should fail (queue full) */ |
| 18 | TEST_ASSERT(!mpmc_queue_enqueue_uintptr(&q, 999)); |
| 19 | |
| 20 | /* Dequeue all items in FIFO order */ |
| 21 | for (uintptr_t i = 1; i <= 8; i++) { |
| 22 | uintptr_t val = 0; |
| 23 | TEST_ASSERT(mpmc_queue_dequeue_uintptr(&q, &val)); |
| 24 | TEST_ASSERT_EQ(val, i * 10); |
| 25 | } |
| 26 | |
| 27 | /* Next dequeue should fail (queue empty) */ |
| 28 | uintptr_t = 0; |
| 29 | TEST_ASSERT(!mpmc_queue_dequeue_uintptr(&q, &extra)); |
| 30 | TEST_ASSERT(mpmc_queue_empty(&q)); |
| 31 | |
| 32 | /* Wraparound test */ |
| 33 | for (uintptr_t i = 100; i < 120; i++) { |
| 34 | TEST_ASSERT(mpmc_queue_enqueue_uintptr(&q, i)); |
| 35 | uintptr_t out = 0; |
| 36 | TEST_ASSERT(mpmc_queue_dequeue_uintptr(&q, &out)); |
| 37 | TEST_ASSERT_EQ(out, i); |
| 38 | } |
| 39 | |
| 40 | mpmc_queue_destroy(q: &q); |
| 41 | return TEST_SUCCESS; |
| 42 | } |
| 43 | |