blob: 2b7e4b4406a977c01ce25bfda9e0ed0fd1d7a190 [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_boundary_read(void)
10{
Andrew Jefferyb70f8712023-04-19 12:53:34 +093011 uint8_t *out_buf;
12 uint8_t in_buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080013 struct ringbuffer_consumer *rbc;
14 struct ringbuffer *rb;
Andrew Jefferyb70f8712023-04-19 12:53:34 +093015 size_t len;
16 size_t pos;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080017 int rc;
18
Andrew Jeffery4ee419d2023-04-19 12:44:55 +093019 static_assert(sizeof(in_buf) * 2 > 10, "");
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080020
21 rb = ringbuffer_init(10);
22 rbc = ringbuffer_consumer_register(rb, ringbuffer_poll_nop, NULL);
23
24 /* queue and dequeue, so our tail is non-zero */
25 ringbuffer_queue(rb, in_buf, sizeof(in_buf));
26 ringbuffer_dequeue_commit(rbc, sizeof(in_buf));
27
28 /* ensure we're getting the second batch of data back */
29 in_buf[0] = 'A';
30
31 /* the next queue should cross the end of the buffer */
32 rc = ringbuffer_queue(rb, in_buf, sizeof(in_buf));
33 assert(!rc);
34
35 /* dequeue everything we can */
36 pos = 0;
37 for (;;) {
38 len = ringbuffer_dequeue_peek(rbc, pos, &out_buf);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093039 if (len == 0) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080040 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093041 }
Andrew Jefferya72711a2023-04-18 18:19:41 +093042 assert(!memcmp(in_buf + pos, out_buf, len));
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080043 pos += len;
44 }
45 assert(pos == sizeof(in_buf));
46
47 ringbuffer_fini(rb);
48}
49
50int main(void)
51{
52 test_boundary_read();
53 return EXIT_SUCCESS;
54}