1 | // SPDX-License-Identifier: GPL-3.0-or-later |
2 | |
3 | #include "test_engine_impl.h" |
4 | |
5 | #include <mos/lib/structures/ring_buffer.h> |
6 | #include <stddef.h> |
7 | |
8 | MOS_TEST_CASE(ringbuffer_creation_and_destruction) |
9 | { |
10 | ring_buffer_t *rb = ring_buffer_create(capacity: 10); |
11 | |
12 | MOS_TEST_CHECK(rb != NULL, true); |
13 | MOS_TEST_CHECK(rb->pos.capacity, 10); |
14 | MOS_TEST_CHECK(rb->pos.count, 0); |
15 | MOS_TEST_CHECK(rb->pos.head, 0); |
16 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
17 | |
18 | ring_buffer_destroy(buffer: rb); |
19 | |
20 | rb = ring_buffer_create(capacity: 0); |
21 | MOS_TEST_CHECK(rb, NULL); |
22 | } |
23 | |
24 | MOS_TEST_CASE(ringbuffer_put_and_get) |
25 | { |
26 | ring_buffer_t *rb = ring_buffer_create(capacity: 5); |
27 | size_t written; |
28 | |
29 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'a'); |
30 | MOS_TEST_CHECK(rb->pos.count, 1); |
31 | MOS_TEST_CHECK(rb->pos.head, 0); |
32 | MOS_TEST_CHECK(rb->pos.next_pos, 1); |
33 | MOS_TEST_CHECK(written, 1); |
34 | |
35 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'b'); |
36 | MOS_TEST_CHECK(rb->pos.count, 2); |
37 | MOS_TEST_CHECK(rb->pos.head, 0); |
38 | MOS_TEST_CHECK(rb->pos.next_pos, 2); |
39 | MOS_TEST_CHECK(written, 1); |
40 | |
41 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'c'); |
42 | MOS_TEST_CHECK(rb->pos.count, 3); |
43 | MOS_TEST_CHECK(rb->pos.head, 0); |
44 | MOS_TEST_CHECK(rb->pos.next_pos, 3); |
45 | MOS_TEST_CHECK(written, 1); |
46 | |
47 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'd'); |
48 | MOS_TEST_CHECK(rb->pos.count, 4); |
49 | MOS_TEST_CHECK(rb->pos.head, 0); |
50 | MOS_TEST_CHECK(rb->pos.next_pos, 4); |
51 | MOS_TEST_CHECK(written, 1); |
52 | |
53 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'e'); |
54 | MOS_TEST_CHECK(rb->pos.count, 5); |
55 | MOS_TEST_CHECK(rb->pos.head, 0); |
56 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
57 | MOS_TEST_CHECK(written, 1); |
58 | |
59 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'f'); |
60 | MOS_TEST_CHECK(rb->pos.count, 5); |
61 | MOS_TEST_CHECK(rb->pos.head, 0); |
62 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
63 | MOS_TEST_CHECK(written, 0); // full |
64 | |
65 | char c = ring_buffer_pop_back_byte(buffer: rb); |
66 | MOS_TEST_CHECK(rb->pos.count, 4); |
67 | MOS_TEST_CHECK(rb->pos.head, 0); |
68 | MOS_TEST_CHECK(rb->pos.next_pos, 4); |
69 | MOS_TEST_CHECK(c, 'e'); |
70 | |
71 | c = ring_buffer_pop_back_byte(buffer: rb); |
72 | MOS_TEST_CHECK(rb->pos.count, 3); |
73 | MOS_TEST_CHECK(rb->pos.head, 0); |
74 | MOS_TEST_CHECK(rb->pos.next_pos, 3); |
75 | MOS_TEST_CHECK(c, 'd'); |
76 | |
77 | c = ring_buffer_pop_back_byte(buffer: rb); |
78 | MOS_TEST_CHECK(rb->pos.count, 2); |
79 | MOS_TEST_CHECK(rb->pos.head, 0); |
80 | MOS_TEST_CHECK(rb->pos.next_pos, 2); |
81 | MOS_TEST_CHECK(c, 'c'); |
82 | |
83 | c = ring_buffer_pop_back_byte(buffer: rb); |
84 | MOS_TEST_CHECK(rb->pos.count, 1); |
85 | MOS_TEST_CHECK(rb->pos.head, 0); |
86 | MOS_TEST_CHECK(rb->pos.next_pos, 1); |
87 | MOS_TEST_CHECK(c, 'b'); |
88 | |
89 | c = ring_buffer_pop_back_byte(buffer: rb); |
90 | MOS_TEST_CHECK(rb->pos.count, 0); |
91 | MOS_TEST_CHECK(rb->pos.head, 0); |
92 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
93 | MOS_TEST_CHECK(c, 'a'); |
94 | |
95 | c = ring_buffer_pop_back_byte(buffer: rb); |
96 | MOS_TEST_CHECK(rb->pos.count, 0); |
97 | MOS_TEST_CHECK(rb->pos.head, 0); |
98 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
99 | MOS_TEST_CHECK(c, 0); // empty |
100 | |
101 | ring_buffer_destroy(buffer: rb); |
102 | } |
103 | |
104 | MOS_TEST_CASE(ringbuffer_push_pop_back) |
105 | { |
106 | ring_buffer_t *rb = ring_buffer_create(capacity: 5); |
107 | size_t written; |
108 | |
109 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'a'); |
110 | MOS_TEST_CHECK(rb->pos.count, 1); |
111 | MOS_TEST_CHECK(rb->pos.head, 0); |
112 | MOS_TEST_CHECK(rb->pos.next_pos, 1); |
113 | MOS_TEST_CHECK(written, 1); |
114 | |
115 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'b'); |
116 | MOS_TEST_CHECK(rb->pos.count, 2); |
117 | MOS_TEST_CHECK(rb->pos.head, 0); |
118 | MOS_TEST_CHECK(rb->pos.next_pos, 2); |
119 | MOS_TEST_CHECK(written, 1); |
120 | |
121 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'c'); |
122 | MOS_TEST_CHECK(rb->pos.count, 3); |
123 | MOS_TEST_CHECK(rb->pos.head, 0); |
124 | MOS_TEST_CHECK(rb->pos.next_pos, 3); |
125 | MOS_TEST_CHECK(written, 1); |
126 | |
127 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'd'); |
128 | MOS_TEST_CHECK(rb->pos.count, 4); |
129 | MOS_TEST_CHECK(rb->pos.head, 0); |
130 | MOS_TEST_CHECK(rb->pos.next_pos, 4); |
131 | MOS_TEST_CHECK(written, 1); |
132 | |
133 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'e'); |
134 | MOS_TEST_CHECK(rb->pos.count, 5); |
135 | MOS_TEST_CHECK(rb->pos.head, 0); |
136 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
137 | MOS_TEST_CHECK(written, 1); |
138 | |
139 | char c = ring_buffer_pop_back_byte(buffer: rb); |
140 | MOS_TEST_CHECK(rb->pos.count, 4); |
141 | MOS_TEST_CHECK(rb->pos.head, 0); |
142 | MOS_TEST_CHECK(rb->pos.next_pos, 4); |
143 | MOS_TEST_CHECK(c, 'e'); |
144 | |
145 | c = ring_buffer_pop_back_byte(buffer: rb); |
146 | MOS_TEST_CHECK(rb->pos.count, 3); |
147 | MOS_TEST_CHECK(rb->pos.head, 0); |
148 | MOS_TEST_CHECK(rb->pos.next_pos, 3); |
149 | MOS_TEST_CHECK(c, 'd'); |
150 | |
151 | c = ring_buffer_pop_back_byte(buffer: rb); |
152 | MOS_TEST_CHECK(rb->pos.count, 2); |
153 | MOS_TEST_CHECK(rb->pos.head, 0); |
154 | MOS_TEST_CHECK(rb->pos.next_pos, 2); |
155 | MOS_TEST_CHECK(c, 'c'); |
156 | |
157 | c = ring_buffer_pop_back_byte(buffer: rb); |
158 | MOS_TEST_CHECK(rb->pos.count, 1); |
159 | MOS_TEST_CHECK(rb->pos.head, 0); |
160 | MOS_TEST_CHECK(rb->pos.next_pos, 1); |
161 | MOS_TEST_CHECK(c, 'b'); |
162 | |
163 | c = ring_buffer_pop_back_byte(buffer: rb); |
164 | MOS_TEST_CHECK(rb->pos.count, 0); |
165 | MOS_TEST_CHECK(rb->pos.head, 0); |
166 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
167 | MOS_TEST_CHECK(c, 'a'); |
168 | |
169 | c = ring_buffer_pop_back_byte(buffer: rb); |
170 | MOS_TEST_CHECK(rb->pos.count, 0); |
171 | MOS_TEST_CHECK(rb->pos.head, 0); |
172 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
173 | MOS_TEST_CHECK(c, 0); // empty |
174 | |
175 | ring_buffer_destroy(buffer: rb); |
176 | } |
177 | |
178 | MOS_TEST_CASE(ringbuffer_push_pop_front) |
179 | { |
180 | ring_buffer_t *rb = ring_buffer_create(capacity: 5); |
181 | size_t written; |
182 | |
183 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'a'); |
184 | MOS_TEST_CHECK(rb->pos.count, 1); |
185 | MOS_TEST_CHECK(rb->pos.head, 4); |
186 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
187 | MOS_TEST_CHECK(written, 1); |
188 | |
189 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'b'); |
190 | MOS_TEST_CHECK(rb->pos.count, 2); |
191 | MOS_TEST_CHECK(rb->pos.head, 3); |
192 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
193 | MOS_TEST_CHECK(written, 1); |
194 | |
195 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'c'); |
196 | MOS_TEST_CHECK(rb->pos.count, 3); |
197 | MOS_TEST_CHECK(rb->pos.head, 2); |
198 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
199 | MOS_TEST_CHECK(written, 1); |
200 | |
201 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'd'); |
202 | MOS_TEST_CHECK(rb->pos.count, 4); |
203 | MOS_TEST_CHECK(rb->pos.head, 1); |
204 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
205 | MOS_TEST_CHECK(written, 1); |
206 | |
207 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'e'); |
208 | MOS_TEST_CHECK(rb->pos.count, 5); |
209 | MOS_TEST_CHECK(rb->pos.head, 0); |
210 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
211 | MOS_TEST_CHECK(written, 1); |
212 | |
213 | char c = ring_buffer_pop_front_byte(buffer: rb); |
214 | MOS_TEST_CHECK(rb->pos.count, 4); |
215 | MOS_TEST_CHECK(rb->pos.head, 1); |
216 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
217 | MOS_TEST_CHECK(c, 'e'); |
218 | |
219 | c = ring_buffer_pop_front_byte(buffer: rb); |
220 | MOS_TEST_CHECK(rb->pos.count, 3); |
221 | MOS_TEST_CHECK(rb->pos.head, 2); |
222 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
223 | MOS_TEST_CHECK(c, 'd'); |
224 | |
225 | c = ring_buffer_pop_front_byte(buffer: rb); |
226 | MOS_TEST_CHECK(rb->pos.count, 2); |
227 | MOS_TEST_CHECK(rb->pos.head, 3); |
228 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
229 | MOS_TEST_CHECK(c, 'c'); |
230 | |
231 | c = ring_buffer_pop_front_byte(buffer: rb); |
232 | MOS_TEST_CHECK(rb->pos.count, 1); |
233 | MOS_TEST_CHECK(rb->pos.head, 4); |
234 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
235 | MOS_TEST_CHECK(c, 'b'); |
236 | |
237 | c = ring_buffer_pop_front_byte(buffer: rb); |
238 | MOS_TEST_CHECK(rb->pos.count, 0); |
239 | MOS_TEST_CHECK(rb->pos.head, 0); |
240 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
241 | MOS_TEST_CHECK(c, 'a'); |
242 | |
243 | c = ring_buffer_pop_front_byte(buffer: rb); |
244 | MOS_TEST_CHECK(rb->pos.count, 0); |
245 | MOS_TEST_CHECK(rb->pos.head, 0); |
246 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
247 | MOS_TEST_CHECK(c, 0); // empty |
248 | |
249 | ring_buffer_destroy(buffer: rb); |
250 | } |
251 | |
252 | MOS_TEST_CASE(ringbuffer_full_and_empty) |
253 | { |
254 | ring_buffer_t *rb = ring_buffer_create(capacity: 1); |
255 | size_t written; |
256 | bool full; |
257 | bool empty; |
258 | |
259 | full = ring_buffer_is_full(buffer: rb); |
260 | MOS_TEST_CHECK(full, false); |
261 | |
262 | empty = ring_buffer_is_empty(buffer: rb); |
263 | MOS_TEST_CHECK(empty, true); |
264 | |
265 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'a'); |
266 | MOS_TEST_CHECK(rb->pos.count, 1); |
267 | MOS_TEST_CHECK(rb->pos.head, 0); |
268 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
269 | MOS_TEST_CHECK(written, 1); |
270 | |
271 | full = ring_buffer_is_full(buffer: rb); |
272 | MOS_TEST_CHECK(full, true); |
273 | |
274 | empty = ring_buffer_is_empty(buffer: rb); |
275 | MOS_TEST_CHECK(empty, false); |
276 | |
277 | char c = ring_buffer_pop_back_byte(buffer: rb); |
278 | MOS_TEST_CHECK(rb->pos.count, 0); |
279 | MOS_TEST_CHECK(rb->pos.head, 0); |
280 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
281 | MOS_TEST_CHECK(c, 'a'); |
282 | |
283 | full = ring_buffer_is_full(buffer: rb); |
284 | MOS_TEST_CHECK(full, false); |
285 | |
286 | empty = ring_buffer_is_empty(buffer: rb); |
287 | MOS_TEST_CHECK(empty, true); |
288 | } |
289 | |
290 | MOS_TEST_CASE(ringbuffer_complicated_ops) |
291 | { |
292 | ring_buffer_t *rb = ring_buffer_create(capacity: 10); |
293 | |
294 | bool full; |
295 | size_t written; |
296 | char c; |
297 | |
298 | ring_buffer_push_back_byte(buffer: rb, byte: '1'); |
299 | ring_buffer_push_back_byte(buffer: rb, byte: '2'); |
300 | ring_buffer_push_back_byte(buffer: rb, byte: '3'); |
301 | ring_buffer_push_back_byte(buffer: rb, byte: '4'); |
302 | ring_buffer_push_back_byte(buffer: rb, byte: '5'); |
303 | MOS_TEST_CHECK(rb->pos.count, 5); |
304 | MOS_TEST_CHECK(rb->pos.head, 0); |
305 | MOS_TEST_CHECK(rb->pos.next_pos, 5); |
306 | |
307 | ring_buffer_push_back_byte(buffer: rb, byte: '6'); |
308 | ring_buffer_push_back_byte(buffer: rb, byte: '7'); |
309 | ring_buffer_push_back_byte(buffer: rb, byte: '8'); |
310 | ring_buffer_push_back_byte(buffer: rb, byte: '9'); |
311 | ring_buffer_push_back_byte(buffer: rb, byte: '0'); |
312 | MOS_TEST_CHECK(rb->pos.count, 10); |
313 | MOS_TEST_CHECK(rb->pos.head, 0); |
314 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
315 | |
316 | // |1|2|3|4|5|6|7|8|9|0| |
317 | |
318 | full = ring_buffer_is_full(buffer: rb); |
319 | MOS_TEST_CHECK(full, true); |
320 | |
321 | c = ring_buffer_pop_front_byte(buffer: rb); |
322 | MOS_TEST_CHECK(c, '1'); |
323 | |
324 | c = ring_buffer_pop_front_byte(buffer: rb); |
325 | MOS_TEST_CHECK(c, '2'); |
326 | |
327 | c = ring_buffer_pop_front_byte(buffer: rb); |
328 | MOS_TEST_CHECK(c, '3'); |
329 | |
330 | c = ring_buffer_pop_back_byte(buffer: rb); |
331 | MOS_TEST_CHECK(c, '0'); |
332 | |
333 | c = ring_buffer_pop_back_byte(buffer: rb); |
334 | MOS_TEST_CHECK(c, '9'); |
335 | |
336 | c = ring_buffer_pop_back_byte(buffer: rb); |
337 | MOS_TEST_CHECK(c, '8'); |
338 | |
339 | // | | | |4|5|6|7| | | | |
340 | |
341 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'a'); // | | |a|4|5|6|7| | | | |
342 | MOS_TEST_CHECK(written, 1); |
343 | |
344 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'b'); // | |b|a|4|5|6|7| | | | |
345 | MOS_TEST_CHECK(rb->pos.head, 1); |
346 | MOS_TEST_CHECK(rb->pos.next_pos, 7); |
347 | MOS_TEST_CHECK(written, 1); |
348 | |
349 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'c'); // |c|b|a|4|5|6|7| | | | |
350 | MOS_TEST_CHECK(rb->pos.head, 0); |
351 | MOS_TEST_CHECK(rb->pos.next_pos, 7); |
352 | MOS_TEST_CHECK(written, 1); |
353 | |
354 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'd'); // |c|b|a|4|5|6|7| | |d| |
355 | MOS_TEST_CHECK(rb->pos.head, 9); |
356 | MOS_TEST_CHECK(rb->pos.next_pos, 7); |
357 | MOS_TEST_CHECK(written, 1); |
358 | |
359 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'e'); // |c|b|a|4|5|6|7| |e|d| |
360 | MOS_TEST_CHECK(rb->pos.head, 8); |
361 | MOS_TEST_CHECK(rb->pos.next_pos, 7); |
362 | MOS_TEST_CHECK(written, 1); |
363 | |
364 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'f'); // |c|b|a|4|5|6|7|f|e|d| |
365 | MOS_TEST_CHECK(rb->pos.head, 7); |
366 | MOS_TEST_CHECK(rb->pos.next_pos, 7); |
367 | MOS_TEST_CHECK(written, 1); |
368 | |
369 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'g'); // |c|b|a|4|5|6|7|f|e|d| |
370 | MOS_TEST_CHECK(rb->pos.head, 7); |
371 | MOS_TEST_CHECK(rb->pos.next_pos, 7); |
372 | MOS_TEST_CHECK(written, 0); |
373 | |
374 | full = ring_buffer_is_full(buffer: rb); |
375 | MOS_TEST_CHECK(full, true); |
376 | |
377 | c = ring_buffer_pop_back_byte(buffer: rb); // |c|b|a|4|5|6| |f|e|d| |
378 | MOS_TEST_CHECK(c, '7'); |
379 | MOS_TEST_CHECK(rb->pos.head, 7); |
380 | MOS_TEST_CHECK(rb->pos.next_pos, 6); |
381 | |
382 | written = ring_buffer_push_front_byte(buffer: rb, byte: 'h'); // |c|b|a|4|5|6|h|f|e|d| |
383 | MOS_TEST_CHECK(written, 1); |
384 | MOS_TEST_CHECK(rb->pos.head, 6); |
385 | MOS_TEST_CHECK(rb->pos.next_pos, 6); |
386 | |
387 | c = ring_buffer_pop_front_byte(buffer: rb); // |c|b|a|4|5|6| |f|e|d| |
388 | MOS_TEST_CHECK(c, 'h'); |
389 | MOS_TEST_CHECK(rb->pos.head, 7); |
390 | MOS_TEST_CHECK(rb->pos.next_pos, 6); |
391 | |
392 | written = ring_buffer_push_back_byte(buffer: rb, byte: 'i'); // |c|b|a|4|5|6|i|f|e|d| |
393 | MOS_TEST_CHECK(written, 1); |
394 | MOS_TEST_CHECK(rb->pos.head, 7); |
395 | MOS_TEST_CHECK(rb->pos.next_pos, 7); |
396 | } |
397 | |
398 | MOS_TEST_CASE(ringbuffer_push_pop_multiple_bytes) |
399 | { |
400 | ring_buffer_t *rb = ring_buffer_create(capacity: 20); |
401 | const char *data = "MY_DATA!" ; |
402 | |
403 | size_t written; |
404 | written = ring_buffer_push_back(buffer: rb, data: (u8 *) data, size: 8); |
405 | MOS_TEST_CHECK(written, 8); |
406 | MOS_TEST_CHECK(rb->pos.head, 0); |
407 | MOS_TEST_CHECK(rb->pos.next_pos, 8); |
408 | MOS_TEST_CHECK(rb->pos.count, 8); |
409 | |
410 | char buf[8]; |
411 | size_t read; |
412 | read = ring_buffer_pop_front(buffer: rb, buf: (u8 *) buf, size: 8); |
413 | MOS_TEST_CHECK(read, 8); |
414 | MOS_TEST_CHECK(rb->pos.head, 8); |
415 | MOS_TEST_CHECK(rb->pos.next_pos, 8); |
416 | MOS_TEST_CHECK(rb->pos.count, 0); |
417 | |
418 | MOS_TEST_CHECK(strncmp(buf, data, 8), 0); |
419 | MOS_TEST_CHECK(ring_buffer_is_empty(rb), true); |
420 | rb->pos.head = 0; |
421 | rb->pos.next_pos = 0; |
422 | rb->pos.count = 0; |
423 | |
424 | written = ring_buffer_push_back(buffer: rb, data: (u8 *) data, size: 8); |
425 | MOS_TEST_CHECK(written, 8); |
426 | MOS_TEST_CHECK(rb->pos.head, 0); |
427 | MOS_TEST_CHECK(rb->pos.next_pos, 8); |
428 | MOS_TEST_CHECK(rb->pos.count, 8); |
429 | |
430 | MOS_TEST_CHECK(strncmp((char *) rb->data, "MY_DATA!" , 8), 0); |
431 | |
432 | written = ring_buffer_push_back(buffer: rb, data: (u8 *) data, size: 8); |
433 | MOS_TEST_CHECK(written, 8); |
434 | MOS_TEST_CHECK(rb->pos.head, 0); |
435 | MOS_TEST_CHECK(rb->pos.next_pos, 16); |
436 | MOS_TEST_CHECK(rb->pos.count, 16); |
437 | |
438 | MOS_TEST_CHECK(strncmp((char *) rb->data, "MY_DATA!MY_DATA!" , 16), 0); |
439 | |
440 | written = ring_buffer_push_back(buffer: rb, data: (u8 *) data, size: 8); // "MY_DATA!MY_DATA!MY_D" |
441 | MOS_TEST_CHECK(written, 4); // only part of the data is written (MY_D) |
442 | MOS_TEST_CHECK(rb->pos.head, 0); // no change |
443 | MOS_TEST_CHECK(rb->pos.next_pos, 0); // wrap around |
444 | MOS_TEST_CHECK(rb->pos.count, 20); // full, 16 existing + 4 new |
445 | |
446 | read = ring_buffer_pop_front(buffer: rb, buf: (u8 *) buf, size: 8); |
447 | MOS_TEST_CHECK(read, 8); |
448 | MOS_TEST_CHECK(rb->pos.head, 8); |
449 | MOS_TEST_CHECK(rb->pos.next_pos, 0); // no change |
450 | MOS_TEST_CHECK(rb->pos.count, 12); // 20 - 8 = 12 |
451 | MOS_TEST_CHECK(strncmp(buf, data, 8), 0); |
452 | |
453 | MOS_TEST_CHECK_STRING_N((char *) rb->data + rb->pos.head, "MY_DATA!" , 8); |
454 | |
455 | written = ring_buffer_push_back(buffer: rb, data: (u8 *) data, size: 8); // wrap around |
456 | MOS_TEST_CHECK(written, 8); |
457 | MOS_TEST_CHECK(rb->pos.head, 8); |
458 | MOS_TEST_CHECK(rb->pos.next_pos, 8); // 0 + 8 = 8 |
459 | MOS_TEST_CHECK(rb->pos.count, 20); // 12 + 8 = 20 |
460 | |
461 | MOS_TEST_CHECK_STRING((char *) rb->data, "MY_DATA!MY_DATA!MY_D" ); |
462 | |
463 | read = ring_buffer_pop_front(buffer: rb, buf: (u8 *) buf, size: 8); |
464 | MOS_TEST_CHECK(read, 8); |
465 | MOS_TEST_CHECK(rb->pos.head, 16); // 8 + 8 = 16 |
466 | MOS_TEST_CHECK(rb->pos.next_pos, 8); // unchanged |
467 | MOS_TEST_CHECK(rb->pos.count, 12); // 20 - 8 = 12 |
468 | MOS_TEST_CHECK(strncmp(buf, data, 8), 0); |
469 | |
470 | read = ring_buffer_pop_back(buffer: rb, buf: (u8 *) buf, size: 8); |
471 | MOS_TEST_CHECK(read, 8); |
472 | MOS_TEST_CHECK(rb->pos.head, 16); // unchanged |
473 | MOS_TEST_CHECK(rb->pos.next_pos, 0); // 8 - 8 = 0 |
474 | MOS_TEST_CHECK(rb->pos.count, 4); // 12 - 8 = 4 |
475 | MOS_TEST_CHECK(strncmp(buf, data, 8), 0); |
476 | |
477 | written = ring_buffer_push_front(buffer: rb, data: (u8 *) data, size: 8); // no wrap around |
478 | MOS_TEST_CHECK(written, 8); |
479 | MOS_TEST_CHECK(rb->pos.head, 8); // 16 - 8 = 8 |
480 | MOS_TEST_CHECK(rb->pos.next_pos, 0); // unchanged |
481 | MOS_TEST_CHECK(rb->pos.count, 12); // 4 + 8 = 12 |
482 | |
483 | written = ring_buffer_push_front(buffer: rb, data: (u8 *) data, size: 8); |
484 | MOS_TEST_CHECK(written, 8); |
485 | MOS_TEST_CHECK(rb->pos.head, 0); // 8 - 8 = 0 |
486 | MOS_TEST_CHECK(rb->pos.next_pos, 0); // unchanged |
487 | MOS_TEST_CHECK(rb->pos.count, 20); // 12 + 8 = 20 |
488 | |
489 | written = ring_buffer_push_front(buffer: rb, data: (u8 *) data, size: 8); // can't write more |
490 | MOS_TEST_CHECK(written, 0); |
491 | MOS_TEST_CHECK(rb->pos.head, 0); |
492 | MOS_TEST_CHECK(rb->pos.next_pos, 0); |
493 | MOS_TEST_CHECK(rb->pos.count, 20); |
494 | |
495 | read = ring_buffer_pop_back(buffer: rb, buf: (u8 *) buf, size: 8); |
496 | MOS_TEST_CHECK(read, 8); |
497 | MOS_TEST_CHECK(rb->pos.head, 0); // unchanged |
498 | MOS_TEST_CHECK(rb->pos.next_pos, 12); // 20 - 8 = 12 |
499 | MOS_TEST_CHECK(rb->pos.count, 12); // 20 - 8 = 12 |
500 | |
501 | read = ring_buffer_push_front(buffer: rb, data: (u8 *) data, size: 8); // wrap around |
502 | MOS_TEST_CHECK(read, 8); |
503 | MOS_TEST_CHECK(rb->pos.head, 12); // 0 - 8 mod 20 = 12 |
504 | MOS_TEST_CHECK(rb->pos.next_pos, 12); // unchanged |
505 | MOS_TEST_CHECK(rb->pos.count, 20); // 12 + 8 = 20 |
506 | |
507 | read = ring_buffer_pop_back(buffer: rb, buf: (u8 *) buf, size: 8); |
508 | MOS_TEST_CHECK(read, 8); |
509 | MOS_TEST_CHECK(rb->pos.head, 12); // unchanged |
510 | MOS_TEST_CHECK(rb->pos.next_pos, 4); // 12 - 8 = 4 |
511 | MOS_TEST_CHECK(rb->pos.count, 12); // 20 - 8 = 12 |
512 | } |
513 | |