1 
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 
6 #include "ringbuffer.c"
7 #include "ringbuffer-test-utils.c"
8 
9 void 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 
17 	assert(sizeof(in_buf) * 2 > 10);
18 
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);
37 		if (len == 0)
38 			break;
39 		assert(!memcmp(in_buf + pos, out_buf, len));
40 		pos += len;
41 	}
42 	assert(pos == sizeof(in_buf));
43 
44 	ringbuffer_fini(rb);
45 }
46 
47 int main(void)
48 {
49 	test_boundary_read();
50 	return EXIT_SUCCESS;
51 }
52