Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 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 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 11 | uint8_t *out_buf; |
| 12 | uint8_t in_buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 13 | struct ringbuffer_consumer *rbc; |
| 14 | struct ringbuffer *rb; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 15 | size_t len; |
| 16 | size_t pos; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 17 | int rc; |
| 18 | |
Andrew Jeffery | 4ee419d | 2023-04-19 12:44:55 +0930 | [diff] [blame] | 19 | static_assert(sizeof(in_buf) * 2 > 10, ""); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 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); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 39 | if (len == 0) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 40 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 41 | } |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 42 | assert(!memcmp(in_buf + pos, out_buf, len)); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 43 | pos += len; |
| 44 | } |
| 45 | assert(pos == sizeof(in_buf)); |
| 46 | |
| 47 | ringbuffer_fini(rb); |
| 48 | } |
| 49 | |
| 50 | int main(void) |
| 51 | { |
| 52 | test_boundary_read(); |
| 53 | return EXIT_SUCCESS; |
| 54 | } |