blob: 05484d27ab04a186383a6acf391c135203722d6d [file] [log] [blame]
Jeremy Kerrc9775ce2017-02-07 16:25:34 +08001
2#include <stdlib.h>
3#include <stdint.h>
4#include <stdio.h>
5
6#include "ringbuffer.c"
7#include "ringbuffer-test-utils.c"
8
9void test_contained_offset_read(void)
10{
Andrew Jefferyb70f8712023-04-19 12:53:34 +093011 uint8_t *out_buf;
12 uint8_t in_buf[] = { 'a', 'b', 'c' };
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080013 struct ringbuffer_consumer *rbc;
14 struct ringbuffer *rb;
15 size_t len;
Andrew Jeffery8f548f62023-04-18 11:48:49 +093016 size_t i;
17 int rc;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080018
19 rb = ringbuffer_init(10);
20 rbc = ringbuffer_consumer_register(rb, ringbuffer_poll_nop, NULL);
21
22 rc = ringbuffer_queue(rb, in_buf, sizeof(in_buf));
23 assert(!rc);
24
25 /* test all possible offsets */
26 for (i = 0; i <= sizeof(in_buf); i++) {
27 len = ringbuffer_dequeue_peek(rbc, i, &out_buf);
28 assert(len == sizeof(in_buf) - i);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093029 if (len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080030 assert(!memcmp(in_buf + i, out_buf, len));
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093031 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080032 }
33
34 ringbuffer_fini(rb);
35}
36
37int main(void)
38{
39 test_contained_offset_read();
40 return EXIT_SUCCESS;
41}