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