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_poll_force(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(5); |
| 19 | |
| 20 | ctx->rbc = ringbuffer_consumer_register(rb, |
| 21 | ringbuffer_poll_append_all, ctx); |
| 22 | |
| 23 | ctx->force_only = true; |
| 24 | |
| 25 | /* fill the ringbuffer */ |
| 26 | rc = ringbuffer_queue(rb, in_buf, 4); |
| 27 | assert(!rc); |
| 28 | |
| 29 | assert(ctx->count == 0); |
| 30 | |
| 31 | /* add more data */ |
| 32 | rc = ringbuffer_queue(rb, in_buf + 4, 2); |
| 33 | assert(!rc); |
| 34 | |
| 35 | /* we should have had a forced poll for the initial two bytes */ |
| 36 | assert(ctx->count == 1); |
| 37 | assert(ctx->len == 2); |
| 38 | assert(!memcmp(in_buf, ctx->data, 2)); |
| 39 | |
| 40 | ringbuffer_fini(rb); |
| 41 | ringbuffer_test_context_fini(ctx); |
| 42 | } |
| 43 | |
| 44 | int main(void) |
| 45 | { |
| 46 | test_poll_force(); |
| 47 | return EXIT_SUCCESS; |
| 48 | } |