1#include "structures/tests/test_internal.h"
2
3TEST_GROUP_DEFINE(mpmc_fifo_ring);
4
5MPMC_FIFO_RING_DECLARE(test_mpmc_word, uintptr_t);
6
7/* Odd-sized payload: slot stride must keep every seq 8-byte aligned */
8struct test_mpmc_odd {
9 uint32_t a, b, c;
10};
11
12TEST_DEFINE_UNIT(mpmc_fifo_ring, fifo_capacity_and_wrap) {
13 struct test_mpmc_word q;
14 bool ok = test_mpmc_word_init(q: &q, cap: 8);
15 TEST_ASSERT(ok);
16 TEST_ASSERT_EQ(test_mpmc_word_capacity(&q), 8);
17 TEST_ASSERT(test_mpmc_word_empty(&q));
18
19 /* Enqueue up to capacity */
20 for (uintptr_t i = 1; i <= 8; i++) {
21 TEST_ASSERT(test_mpmc_word_enqueue(&q, i * 10));
22 }
23
24 /* 9th item should fail (queue full) */
25 TEST_ASSERT(!test_mpmc_word_enqueue(&q, 999));
26
27 /* Dequeue all items in FIFO order */
28 for (uintptr_t i = 1; i <= 8; i++) {
29 uintptr_t val = 0;
30 TEST_ASSERT(test_mpmc_word_dequeue(&q, &val));
31 TEST_ASSERT_EQ(val, i * 10);
32 }
33
34 /* Next dequeue should fail (queue empty) */
35 uintptr_t extra = 0;
36 TEST_ASSERT(!test_mpmc_word_dequeue(&q, &extra));
37 TEST_ASSERT(test_mpmc_word_empty(&q));
38
39 /* Wraparound test */
40 for (uintptr_t i = 100; i < 120; i++) {
41 TEST_ASSERT(test_mpmc_word_enqueue(&q, i));
42 uintptr_t out = 0;
43 TEST_ASSERT(test_mpmc_word_dequeue(&q, &out));
44 TEST_ASSERT_EQ(out, i);
45 }
46
47 test_mpmc_word_destroy(q: &q);
48 return TEST_SUCCESS;
49}
50
51TEST_DEFINE_UNIT(mpmc_fifo_ring, drain_empty_partial_full) {
52 struct test_mpmc_word q;
53 TEST_ASSERT(test_mpmc_word_init(&q, 8));
54
55 TEST_ASSERT_EQ(test_mpmc_word_drain(&q), 0);
56
57 for (uintptr_t i = 1; i <= 3; i++)
58 TEST_ASSERT(test_mpmc_word_enqueue(&q, i));
59 TEST_ASSERT_EQ(test_mpmc_word_drain(&q), 3);
60 TEST_ASSERT(test_mpmc_word_empty(&q));
61
62 for (int round = 0; round < 3; round++) {
63 for (uintptr_t i = 1; i <= 8; i++)
64 TEST_ASSERT(test_mpmc_word_enqueue(&q, i));
65 TEST_ASSERT(!test_mpmc_word_enqueue(&q, 999));
66 TEST_ASSERT_EQ(test_mpmc_word_drain(&q), 8);
67 TEST_ASSERT(test_mpmc_word_empty(&q));
68
69 uintptr_t v = 0;
70 TEST_ASSERT(!test_mpmc_word_dequeue(&q, &v));
71 TEST_ASSERT(test_mpmc_word_enqueue(&q, 42));
72 TEST_ASSERT(test_mpmc_word_dequeue(&q, &v));
73 TEST_ASSERT_EQ(v, 42);
74 }
75
76 test_mpmc_word_destroy(q: &q);
77 return TEST_SUCCESS;
78}
79