MOS Source Code
Loading...
Searching...
No Matches
test_ring_buffer.c
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "test_engine_impl.h"
4
6#include <stddef.h>
7
8MOS_TEST_CASE(ringbuffer_creation_and_destruction)
9{
11
12 MOS_TEST_CHECK(rb != NULL, true);
14 MOS_TEST_CHECK(rb->pos.count, 0);
15 MOS_TEST_CHECK(rb->pos.head, 0);
17
19
20 rb = ring_buffer_create(0);
22}
23
24MOS_TEST_CASE(ringbuffer_put_and_get)
25{
27 size_t written;
28
29 written = ring_buffer_push_back_byte(rb, 'a');
30 MOS_TEST_CHECK(rb->pos.count, 1);
31 MOS_TEST_CHECK(rb->pos.head, 0);
33 MOS_TEST_CHECK(written, 1);
34
35 written = ring_buffer_push_back_byte(rb, 'b');
36 MOS_TEST_CHECK(rb->pos.count, 2);
37 MOS_TEST_CHECK(rb->pos.head, 0);
39 MOS_TEST_CHECK(written, 1);
40
41 written = ring_buffer_push_back_byte(rb, 'c');
42 MOS_TEST_CHECK(rb->pos.count, 3);
43 MOS_TEST_CHECK(rb->pos.head, 0);
45 MOS_TEST_CHECK(written, 1);
46
47 written = ring_buffer_push_back_byte(rb, 'd');
48 MOS_TEST_CHECK(rb->pos.count, 4);
49 MOS_TEST_CHECK(rb->pos.head, 0);
51 MOS_TEST_CHECK(written, 1);
52
53 written = ring_buffer_push_back_byte(rb, 'e');
54 MOS_TEST_CHECK(rb->pos.count, 5);
55 MOS_TEST_CHECK(rb->pos.head, 0);
57 MOS_TEST_CHECK(written, 1);
58
59 written = ring_buffer_push_back_byte(rb, 'f');
60 MOS_TEST_CHECK(rb->pos.count, 5);
61 MOS_TEST_CHECK(rb->pos.head, 0);
63 MOS_TEST_CHECK(written, 0); // full
64
65 char c = ring_buffer_pop_back_byte(rb);
66 MOS_TEST_CHECK(rb->pos.count, 4);
67 MOS_TEST_CHECK(rb->pos.head, 0);
69 MOS_TEST_CHECK(c, 'e');
70
72 MOS_TEST_CHECK(rb->pos.count, 3);
73 MOS_TEST_CHECK(rb->pos.head, 0);
75 MOS_TEST_CHECK(c, 'd');
76
78 MOS_TEST_CHECK(rb->pos.count, 2);
79 MOS_TEST_CHECK(rb->pos.head, 0);
81 MOS_TEST_CHECK(c, 'c');
82
84 MOS_TEST_CHECK(rb->pos.count, 1);
85 MOS_TEST_CHECK(rb->pos.head, 0);
87 MOS_TEST_CHECK(c, 'b');
88
90 MOS_TEST_CHECK(rb->pos.count, 0);
91 MOS_TEST_CHECK(rb->pos.head, 0);
93 MOS_TEST_CHECK(c, 'a');
94
96 MOS_TEST_CHECK(rb->pos.count, 0);
97 MOS_TEST_CHECK(rb->pos.head, 0);
99 MOS_TEST_CHECK(c, 0); // empty
100
102}
103
104MOS_TEST_CASE(ringbuffer_push_pop_back)
105{
107 size_t written;
108
109 written = ring_buffer_push_back_byte(rb, 'a');
110 MOS_TEST_CHECK(rb->pos.count, 1);
111 MOS_TEST_CHECK(rb->pos.head, 0);
113 MOS_TEST_CHECK(written, 1);
114
115 written = ring_buffer_push_back_byte(rb, 'b');
116 MOS_TEST_CHECK(rb->pos.count, 2);
117 MOS_TEST_CHECK(rb->pos.head, 0);
119 MOS_TEST_CHECK(written, 1);
120
121 written = ring_buffer_push_back_byte(rb, 'c');
122 MOS_TEST_CHECK(rb->pos.count, 3);
123 MOS_TEST_CHECK(rb->pos.head, 0);
125 MOS_TEST_CHECK(written, 1);
126
127 written = ring_buffer_push_back_byte(rb, 'd');
128 MOS_TEST_CHECK(rb->pos.count, 4);
129 MOS_TEST_CHECK(rb->pos.head, 0);
131 MOS_TEST_CHECK(written, 1);
132
133 written = ring_buffer_push_back_byte(rb, 'e');
134 MOS_TEST_CHECK(rb->pos.count, 5);
135 MOS_TEST_CHECK(rb->pos.head, 0);
137 MOS_TEST_CHECK(written, 1);
138
139 char c = ring_buffer_pop_back_byte(rb);
140 MOS_TEST_CHECK(rb->pos.count, 4);
141 MOS_TEST_CHECK(rb->pos.head, 0);
143 MOS_TEST_CHECK(c, 'e');
144
146 MOS_TEST_CHECK(rb->pos.count, 3);
147 MOS_TEST_CHECK(rb->pos.head, 0);
149 MOS_TEST_CHECK(c, 'd');
150
152 MOS_TEST_CHECK(rb->pos.count, 2);
153 MOS_TEST_CHECK(rb->pos.head, 0);
155 MOS_TEST_CHECK(c, 'c');
156
158 MOS_TEST_CHECK(rb->pos.count, 1);
159 MOS_TEST_CHECK(rb->pos.head, 0);
161 MOS_TEST_CHECK(c, 'b');
162
164 MOS_TEST_CHECK(rb->pos.count, 0);
165 MOS_TEST_CHECK(rb->pos.head, 0);
167 MOS_TEST_CHECK(c, 'a');
168
170 MOS_TEST_CHECK(rb->pos.count, 0);
171 MOS_TEST_CHECK(rb->pos.head, 0);
173 MOS_TEST_CHECK(c, 0); // empty
174
176}
177
178MOS_TEST_CASE(ringbuffer_push_pop_front)
179{
181 size_t written;
182
183 written = ring_buffer_push_front_byte(rb, 'a');
184 MOS_TEST_CHECK(rb->pos.count, 1);
185 MOS_TEST_CHECK(rb->pos.head, 4);
187 MOS_TEST_CHECK(written, 1);
188
189 written = ring_buffer_push_front_byte(rb, 'b');
190 MOS_TEST_CHECK(rb->pos.count, 2);
191 MOS_TEST_CHECK(rb->pos.head, 3);
193 MOS_TEST_CHECK(written, 1);
194
195 written = ring_buffer_push_front_byte(rb, 'c');
196 MOS_TEST_CHECK(rb->pos.count, 3);
197 MOS_TEST_CHECK(rb->pos.head, 2);
199 MOS_TEST_CHECK(written, 1);
200
201 written = ring_buffer_push_front_byte(rb, 'd');
202 MOS_TEST_CHECK(rb->pos.count, 4);
203 MOS_TEST_CHECK(rb->pos.head, 1);
205 MOS_TEST_CHECK(written, 1);
206
207 written = ring_buffer_push_front_byte(rb, 'e');
208 MOS_TEST_CHECK(rb->pos.count, 5);
209 MOS_TEST_CHECK(rb->pos.head, 0);
211 MOS_TEST_CHECK(written, 1);
212
213 char c = ring_buffer_pop_front_byte(rb);
214 MOS_TEST_CHECK(rb->pos.count, 4);
215 MOS_TEST_CHECK(rb->pos.head, 1);
217 MOS_TEST_CHECK(c, 'e');
218
220 MOS_TEST_CHECK(rb->pos.count, 3);
221 MOS_TEST_CHECK(rb->pos.head, 2);
223 MOS_TEST_CHECK(c, 'd');
224
226 MOS_TEST_CHECK(rb->pos.count, 2);
227 MOS_TEST_CHECK(rb->pos.head, 3);
229 MOS_TEST_CHECK(c, 'c');
230
232 MOS_TEST_CHECK(rb->pos.count, 1);
233 MOS_TEST_CHECK(rb->pos.head, 4);
235 MOS_TEST_CHECK(c, 'b');
236
238 MOS_TEST_CHECK(rb->pos.count, 0);
239 MOS_TEST_CHECK(rb->pos.head, 0);
241 MOS_TEST_CHECK(c, 'a');
242
244 MOS_TEST_CHECK(rb->pos.count, 0);
245 MOS_TEST_CHECK(rb->pos.head, 0);
247 MOS_TEST_CHECK(c, 0); // empty
248
250}
251
252MOS_TEST_CASE(ringbuffer_full_and_empty)
253{
255 size_t written;
256 bool full;
257 bool empty;
258
259 full = ring_buffer_is_full(rb);
260 MOS_TEST_CHECK(full, false);
261
262 empty = ring_buffer_is_empty(rb);
263 MOS_TEST_CHECK(empty, true);
264
265 written = ring_buffer_push_back_byte(rb, 'a');
266 MOS_TEST_CHECK(rb->pos.count, 1);
267 MOS_TEST_CHECK(rb->pos.head, 0);
269 MOS_TEST_CHECK(written, 1);
270
271 full = ring_buffer_is_full(rb);
272 MOS_TEST_CHECK(full, true);
273
274 empty = ring_buffer_is_empty(rb);
275 MOS_TEST_CHECK(empty, false);
276
277 char c = ring_buffer_pop_back_byte(rb);
278 MOS_TEST_CHECK(rb->pos.count, 0);
279 MOS_TEST_CHECK(rb->pos.head, 0);
281 MOS_TEST_CHECK(c, 'a');
282
283 full = ring_buffer_is_full(rb);
284 MOS_TEST_CHECK(full, false);
285
286 empty = ring_buffer_is_empty(rb);
287 MOS_TEST_CHECK(empty, true);
288}
289
290MOS_TEST_CASE(ringbuffer_complicated_ops)
291{
293
294 bool full;
295 size_t written;
296 char c;
297
303 MOS_TEST_CHECK(rb->pos.count, 5);
304 MOS_TEST_CHECK(rb->pos.head, 0);
306
312 MOS_TEST_CHECK(rb->pos.count, 10);
313 MOS_TEST_CHECK(rb->pos.head, 0);
315
316 // |1|2|3|4|5|6|7|8|9|0|
317
318 full = ring_buffer_is_full(rb);
319 MOS_TEST_CHECK(full, true);
320
322 MOS_TEST_CHECK(c, '1');
323
325 MOS_TEST_CHECK(c, '2');
326
328 MOS_TEST_CHECK(c, '3');
329
331 MOS_TEST_CHECK(c, '0');
332
334 MOS_TEST_CHECK(c, '9');
335
337 MOS_TEST_CHECK(c, '8');
338
339 // | | | |4|5|6|7| | | |
340
341 written = ring_buffer_push_front_byte(rb, 'a'); // | | |a|4|5|6|7| | | |
342 MOS_TEST_CHECK(written, 1);
343
344 written = ring_buffer_push_front_byte(rb, 'b'); // | |b|a|4|5|6|7| | | |
345 MOS_TEST_CHECK(rb->pos.head, 1);
347 MOS_TEST_CHECK(written, 1);
348
349 written = ring_buffer_push_front_byte(rb, 'c'); // |c|b|a|4|5|6|7| | | |
350 MOS_TEST_CHECK(rb->pos.head, 0);
352 MOS_TEST_CHECK(written, 1);
353
354 written = ring_buffer_push_front_byte(rb, 'd'); // |c|b|a|4|5|6|7| | |d|
355 MOS_TEST_CHECK(rb->pos.head, 9);
357 MOS_TEST_CHECK(written, 1);
358
359 written = ring_buffer_push_front_byte(rb, 'e'); // |c|b|a|4|5|6|7| |e|d|
360 MOS_TEST_CHECK(rb->pos.head, 8);
362 MOS_TEST_CHECK(written, 1);
363
364 written = ring_buffer_push_front_byte(rb, 'f'); // |c|b|a|4|5|6|7|f|e|d|
365 MOS_TEST_CHECK(rb->pos.head, 7);
367 MOS_TEST_CHECK(written, 1);
368
369 written = ring_buffer_push_front_byte(rb, 'g'); // |c|b|a|4|5|6|7|f|e|d|
370 MOS_TEST_CHECK(rb->pos.head, 7);
372 MOS_TEST_CHECK(written, 0);
373
374 full = ring_buffer_is_full(rb);
375 MOS_TEST_CHECK(full, true);
376
377 c = ring_buffer_pop_back_byte(rb); // |c|b|a|4|5|6| |f|e|d|
378 MOS_TEST_CHECK(c, '7');
379 MOS_TEST_CHECK(rb->pos.head, 7);
381
382 written = ring_buffer_push_front_byte(rb, '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);
386
387 c = ring_buffer_pop_front_byte(rb); // |c|b|a|4|5|6| |f|e|d|
388 MOS_TEST_CHECK(c, 'h');
389 MOS_TEST_CHECK(rb->pos.head, 7);
391
392 written = ring_buffer_push_back_byte(rb, '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);
396}
397
398MOS_TEST_CASE(ringbuffer_push_pop_multiple_bytes)
399{
401 const char *data = "MY_DATA!";
402
403 size_t written;
404 written = ring_buffer_push_back(rb, (u8 *) data, 8);
405 MOS_TEST_CHECK(written, 8);
406 MOS_TEST_CHECK(rb->pos.head, 0);
408 MOS_TEST_CHECK(rb->pos.count, 8);
409
410 char buf[8];
411 size_t read;
412 read = ring_buffer_pop_front(rb, (u8 *) buf, 8);
413 MOS_TEST_CHECK(read, 8);
414 MOS_TEST_CHECK(rb->pos.head, 8);
416 MOS_TEST_CHECK(rb->pos.count, 0);
417
418 MOS_TEST_CHECK(strncmp(buf, data, 8), 0);
420 rb->pos.head = 0;
421 rb->pos.next_pos = 0;
422 rb->pos.count = 0;
423
424 written = ring_buffer_push_back(rb, (u8 *) data, 8);
425 MOS_TEST_CHECK(written, 8);
426 MOS_TEST_CHECK(rb->pos.head, 0);
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(rb, (u8 *) data, 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(rb, (u8 *) data, 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(rb, (u8 *) buf, 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(rb, (u8 *) data, 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(rb, (u8 *) buf, 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(rb, (u8 *) buf, 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(rb, (u8 *) data, 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(rb, (u8 *) data, 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(rb, (u8 *) data, 8); // can't write more
490 MOS_TEST_CHECK(written, 0);
491 MOS_TEST_CHECK(rb->pos.head, 0);
493 MOS_TEST_CHECK(rb->pos.count, 20);
494
495 read = ring_buffer_pop_back(rb, (u8 *) buf, 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(rb, (u8 *) data, 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(rb, (u8 *) buf, 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}
MOSAPI s32 strncmp(const char *str1, const char *str2, size_t n)
Definition mos_string.c:32
MOSAPI void ring_buffer_destroy(ring_buffer_t *buffer)
Definition ring_buffer.c:52
should_inline size_t ring_buffer_pop_front(ring_buffer_t *buffer, u8 *buf, size_t size)
Definition ring_buffer.h:74
should_inline size_t ring_buffer_push_back_byte(ring_buffer_t *buffer, u8 byte)
Definition ring_buffer.h:78
should_inline bool ring_buffer_is_empty(ring_buffer_t *buffer)
Definition ring_buffer.h:68
should_inline bool ring_buffer_is_full(ring_buffer_t *buffer)
Definition ring_buffer.h:67
should_inline size_t ring_buffer_push_front_byte(ring_buffer_t *buffer, u8 byte)
Definition ring_buffer.h:76
should_inline size_t ring_buffer_push_back(ring_buffer_t *buffer, const u8 *data, size_t size)
Definition ring_buffer.h:70
should_inline size_t ring_buffer_pop_back(ring_buffer_t *buffer, u8 *buf, size_t size)
Definition ring_buffer.h:71
should_inline u8 ring_buffer_pop_front_byte(ring_buffer_t *buffer)
Definition ring_buffer.h:80
should_inline u8 ring_buffer_pop_back_byte(ring_buffer_t *buffer)
Definition ring_buffer.h:79
MOSAPI ring_buffer_t * ring_buffer_create(size_t capacity)
Definition ring_buffer.c:13
should_inline size_t ring_buffer_push_front(ring_buffer_t *buffer, const u8 *data, size_t size)
Definition ring_buffer.h:73
#define NULL
Definition pb_syshdr.h:46
ring_buffer_pos_t pos
Definition ring_buffer.h:28
#define MOS_TEST_CHECK_STRING_N(actual, expected, n)
#define MOS_TEST_CHECK(actual, expected)
#define MOS_TEST_CHECK_STRING(actual, expected)
#define MOS_TEST_CASE(_TestName)
unsigned char u8
Definition types.h:19