blob: f62f66f0a824325cbe9e09b38603a7d5f2d15c2c [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{
11 uint8_t *out_buf, in_buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
12 struct ringbuffer_consumer *rbc;
13 struct ringbuffer *rb;
14 size_t len, pos;
15 int rc;
16
Andrew Jeffery4ee419d2023-04-19 12:44:55 +093017 static_assert(sizeof(in_buf) * 2 > 10, "");
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 /* queue and dequeue, so our tail is non-zero */
23 ringbuffer_queue(rb, in_buf, sizeof(in_buf));
24 ringbuffer_dequeue_commit(rbc, sizeof(in_buf));
25
26 /* ensure we're getting the second batch of data back */
27 in_buf[0] = 'A';
28
29 /* the next queue should cross the end of the buffer */
30 rc = ringbuffer_queue(rb, in_buf, sizeof(in_buf));
31 assert(!rc);
32
33 /* dequeue everything we can */
34 pos = 0;
35 for (;;) {
36 len = ringbuffer_dequeue_peek(rbc, pos, &out_buf);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093037 if (len == 0) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080038 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093039 }
Andrew Jefferya72711a2023-04-18 18:19:41 +093040 assert(!memcmp(in_buf + pos, out_buf, len));
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080041 pos += len;
42 }
43 assert(pos == sizeof(in_buf));
44
45 ringbuffer_fini(rb);
46}
47
48int main(void)
49{
50 test_boundary_read();
51 return EXIT_SUCCESS;
52}