xref: /openbmc/qemu/tests/unit/test-fifo.c (revision e7217726)
1 /*
2  * Fifo8 tests
3  *
4  * Copyright 2024 Mark Cave-Ayland
5  *
6  * Authors:
7  *  Mark Cave-Ayland    <mark.cave-ayland@ilande.co.uk>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "migration/vmstate.h"
15 #include "qemu/fifo8.h"
16 
17 const VMStateInfo vmstate_info_uint32;
18 const VMStateInfo vmstate_info_buffer;
19 
20 
21 static void test_fifo8_pop_bufptr_wrap(void)
22 {
23     Fifo8 fifo;
24     uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 };
25     uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x9, 0xa };
26     const uint8_t *buf;
27     uint32_t count;
28 
29     fifo8_create(&fifo, 8);
30 
31     fifo8_push_all(&fifo, data_in1, sizeof(data_in1));
32     buf = fifo8_pop_bufptr(&fifo, 2, &count);
33     g_assert(count == 2);
34     g_assert(buf[0] == 0x1 && buf[1] == 0x2);
35 
36     fifo8_push_all(&fifo, data_in2, sizeof(data_in2));
37     buf = fifo8_pop_bufptr(&fifo, 8, &count);
38     g_assert(count == 6);
39     g_assert(buf[0] == 0x3 && buf[1] == 0x4 && buf[2] == 0x5 &&
40              buf[3] == 0x6 && buf[4] == 0x7 && buf[5] == 0x8);
41 
42     g_assert(fifo8_num_used(&fifo) == 2);
43     fifo8_destroy(&fifo);
44 }
45 
46 static void test_fifo8_pop_bufptr(void)
47 {
48     Fifo8 fifo;
49     uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 };
50     const uint8_t *buf;
51     uint32_t count;
52 
53     fifo8_create(&fifo, 8);
54 
55     fifo8_push_all(&fifo, data_in, sizeof(data_in));
56     buf = fifo8_pop_bufptr(&fifo, 2, &count);
57     g_assert(count == 2);
58     g_assert(buf[0] == 0x1 && buf[1] == 0x2);
59 
60     g_assert(fifo8_num_used(&fifo) == 2);
61     fifo8_destroy(&fifo);
62 }
63 
64 static void test_fifo8_peek_bufptr_wrap(void)
65 {
66     Fifo8 fifo;
67     uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 };
68     uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x9, 0xa };
69     const uint8_t *buf;
70     uint32_t count;
71 
72     fifo8_create(&fifo, 8);
73 
74     fifo8_push_all(&fifo, data_in1, sizeof(data_in1));
75     buf = fifo8_peek_bufptr(&fifo, 2, &count);
76     g_assert(count == 2);
77     g_assert(buf[0] == 0x1 && buf[1] == 0x2);
78 
79     buf = fifo8_pop_bufptr(&fifo, 2, &count);
80     g_assert(count == 2);
81     g_assert(buf[0] == 0x1 && buf[1] == 0x2);
82     fifo8_push_all(&fifo, data_in2, sizeof(data_in2));
83 
84     buf = fifo8_peek_bufptr(&fifo, 8, &count);
85     g_assert(count == 6);
86     g_assert(buf[0] == 0x3 && buf[1] == 0x4 && buf[2] == 0x5 &&
87              buf[3] == 0x6 && buf[4] == 0x7 && buf[5] == 0x8);
88 
89     g_assert(fifo8_num_used(&fifo) == 8);
90     fifo8_destroy(&fifo);
91 }
92 
93 static void test_fifo8_peek_bufptr(void)
94 {
95     Fifo8 fifo;
96     uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 };
97     const uint8_t *buf;
98     uint32_t count;
99 
100     fifo8_create(&fifo, 8);
101 
102     fifo8_push_all(&fifo, data_in, sizeof(data_in));
103     buf = fifo8_peek_bufptr(&fifo, 2, &count);
104     g_assert(count == 2);
105     g_assert(buf[0] == 0x1 && buf[1] == 0x2);
106 
107     g_assert(fifo8_num_used(&fifo) == 4);
108     fifo8_destroy(&fifo);
109 }
110 
111 static void test_fifo8_pop_buf_wrap(void)
112 {
113     Fifo8 fifo;
114     uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 };
115     uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc };
116     uint8_t data_out[4];
117     int count;
118 
119     fifo8_create(&fifo, 8);
120 
121     fifo8_push_all(&fifo, data_in1, sizeof(data_in1));
122     fifo8_pop_buf(&fifo, NULL, 4);
123 
124     fifo8_push_all(&fifo, data_in2, sizeof(data_in2));
125     count = fifo8_pop_buf(&fifo, NULL, 4);
126     g_assert(count == 4);
127     count = fifo8_pop_buf(&fifo, data_out, 4);
128     g_assert(count == 4);
129     g_assert(data_out[0] == 0x9 && data_out[1] == 0xa &&
130              data_out[2] == 0xb && data_out[3] == 0xc);
131 
132     g_assert(fifo8_num_used(&fifo) == 0);
133     fifo8_destroy(&fifo);
134 }
135 
136 static void test_fifo8_pop_buf(void)
137 {
138     Fifo8 fifo;
139     uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 };
140     uint8_t data_out[] = { 0xff, 0xff, 0xff, 0xff };
141     int count;
142 
143     fifo8_create(&fifo, 8);
144 
145     fifo8_push_all(&fifo, data_in, sizeof(data_in));
146     count = fifo8_pop_buf(&fifo, NULL, 4);
147     g_assert(count == 4);
148     count = fifo8_pop_buf(&fifo, data_out, 4);
149     g_assert(data_out[0] == 0x5 && data_out[1] == 0x6 &&
150              data_out[2] == 0x7 && data_out[3] == 0x8);
151 
152     g_assert(fifo8_num_used(&fifo) == 0);
153     fifo8_destroy(&fifo);
154 }
155 
156 static void test_fifo8_peek_buf_wrap(void)
157 {
158     Fifo8 fifo;
159     uint8_t data_in1[] = { 0x1, 0x2, 0x3, 0x4 };
160     uint8_t data_in2[] = { 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc };
161     uint8_t data_out[4];
162     int count;
163 
164     fifo8_create(&fifo, 8);
165 
166     fifo8_push_all(&fifo, data_in1, sizeof(data_in1));
167     fifo8_pop_buf(&fifo, NULL, 4);
168 
169     fifo8_push_all(&fifo, data_in2, sizeof(data_in2));
170     count = fifo8_peek_buf(&fifo, NULL, 4);
171     g_assert(count == 4);
172     count = fifo8_peek_buf(&fifo, data_out, 4);
173     g_assert(count == 4);
174     g_assert(data_out[0] == 0x5 && data_out[1] == 0x6 &&
175              data_out[2] == 0x7 && data_out[3] == 0x8);
176 
177     g_assert(fifo8_num_used(&fifo) == 8);
178     fifo8_destroy(&fifo);
179 }
180 
181 static void test_fifo8_peek_buf(void)
182 {
183     Fifo8 fifo;
184     uint8_t data_in[] = { 0x1, 0x2, 0x3, 0x4 };
185     uint8_t data_out[] = { 0xff, 0xff, 0xff, 0xff };
186     int count;
187 
188     fifo8_create(&fifo, 8);
189 
190     fifo8_push_all(&fifo, data_in, sizeof(data_in));
191     count = fifo8_peek_buf(&fifo, NULL, 4);
192     g_assert(count == 4);
193     g_assert(data_out[0] == 0xff && data_out[1] == 0xff &&
194              data_out[2] == 0xff && data_out[3] == 0xff);
195 
196     count = fifo8_peek_buf(&fifo, data_out, 4);
197     g_assert(count == 4);
198     g_assert(data_out[0] == 0x1 && data_out[1] == 0x2 &&
199              data_out[2] == 0x3 && data_out[3] == 0x4);
200 
201     g_assert(fifo8_num_used(&fifo) == 4);
202     fifo8_destroy(&fifo);
203 }
204 
205 static void test_fifo8_peek(void)
206 {
207     Fifo8 fifo;
208     uint8_t c;
209 
210     fifo8_create(&fifo, 8);
211     fifo8_push(&fifo, 0x1);
212     fifo8_push(&fifo, 0x2);
213 
214     c = fifo8_peek(&fifo);
215     g_assert(c == 0x1);
216     fifo8_pop(&fifo);
217     c = fifo8_peek(&fifo);
218     g_assert(c == 0x2);
219 
220     g_assert(fifo8_num_used(&fifo) == 1);
221     fifo8_destroy(&fifo);
222 }
223 
224 static void test_fifo8_pushpop(void)
225 {
226     Fifo8 fifo;
227     uint8_t c;
228 
229     fifo8_create(&fifo, 8);
230     fifo8_push(&fifo, 0x1);
231     fifo8_push(&fifo, 0x2);
232 
233     c = fifo8_pop(&fifo);
234     g_assert(c == 0x1);
235     c = fifo8_pop(&fifo);
236     g_assert(c == 0x2);
237 
238     g_assert(fifo8_num_used(&fifo) == 0);
239     fifo8_destroy(&fifo);
240 }
241 
242 int main(int argc, char *argv[])
243 {
244     g_test_init(&argc, &argv, NULL);
245     g_test_add_func("/fifo8/pushpop", test_fifo8_pushpop);
246     g_test_add_func("/fifo8/peek", test_fifo8_peek);
247     g_test_add_func("/fifo8/peek_buf", test_fifo8_peek_buf);
248     g_test_add_func("/fifo8/peek_buf_wrap", test_fifo8_peek_buf_wrap);
249     g_test_add_func("/fifo8/pop_buf", test_fifo8_pop_buf);
250     g_test_add_func("/fifo8/pop_buf_wrap", test_fifo8_pop_buf_wrap);
251     g_test_add_func("/fifo8/peek_bufptr", test_fifo8_peek_bufptr);
252     g_test_add_func("/fifo8/peek_bufptr_wrap", test_fifo8_peek_bufptr_wrap);
253     g_test_add_func("/fifo8/pop_bufptr", test_fifo8_pop_bufptr);
254     g_test_add_func("/fifo8/pop_bufptr_wrap", test_fifo8_pop_bufptr_wrap);
255     return g_test_run();
256 }
257