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_poll(void) |
| 10 | { |
| 11 | uint8_t in_buf[] = { 'a', 'b', 'c', 'd', 'e', 'f' }; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 12 | struct rb_test_ctx _ctx; |
| 13 | struct rb_test_ctx *ctx = &_ctx; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 14 | struct ringbuffer *rb; |
| 15 | int rc; |
| 16 | |
| 17 | ringbuffer_test_context_init(ctx); |
| 18 | |
| 19 | rb = ringbuffer_init(10); |
| 20 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 21 | ctx->rbc = ringbuffer_consumer_register(rb, ringbuffer_poll_append_all, |
| 22 | ctx); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 23 | |
| 24 | /* don't consume initial data in the poll callback */ |
| 25 | ctx->ignore_poll = true; |
| 26 | |
| 27 | /* queue and dequeue, so our tail is non-zero */ |
| 28 | ringbuffer_queue(rb, in_buf, sizeof(in_buf)); |
| 29 | ringbuffer_dequeue_commit(ctx->rbc, sizeof(in_buf)); |
| 30 | |
| 31 | /* start queueing data */ |
| 32 | ctx->ignore_poll = false; |
| 33 | |
| 34 | /* ensure we're getting the second batch of data back */ |
| 35 | in_buf[0] = 'A'; |
| 36 | |
| 37 | rc = ringbuffer_queue(rb, in_buf, sizeof(in_buf)); |
| 38 | assert(!rc); |
| 39 | |
| 40 | assert(ctx->count == 1); |
| 41 | assert(ctx->len == sizeof(in_buf)); |
| 42 | assert(!memcmp(in_buf, ctx->data, ctx->len)); |
| 43 | |
| 44 | ringbuffer_fini(rb); |
| 45 | ringbuffer_test_context_fini(ctx); |
| 46 | } |
| 47 | |
| 48 | int main(void) |
| 49 | { |
| 50 | test_boundary_poll(); |
| 51 | return EXIT_SUCCESS; |
| 52 | } |