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 
test_boundary_read(void)9 void test_boundary_read(void)
10 {
11 	uint8_t *out_buf;
12 	uint8_t in_buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
13 	struct ringbuffer_consumer *rbc;
14 	struct ringbuffer *rb;
15 	size_t len;
16 	size_t pos;
17 	int rc;
18 
19 	static_assert(sizeof(in_buf) * 2 > 10, "");
20 
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);
39 		if (len == 0) {
40 			break;
41 		}
42 		assert(!memcmp(in_buf + pos, out_buf, len));
43 		pos += len;
44 	}
45 	assert(pos == sizeof(in_buf));
46 
47 	ringbuffer_fini(rb);
48 }
49 
main(void)50 int main(void)
51 {
52 	test_boundary_read();
53 	return EXIT_SUCCESS;
54 }
55