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 | { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame^] | 11 | uint8_t in_buf[] = { |
| 12 | 'a', 'b', 'c', 'd', 'e', 'f', |
| 13 | }; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 14 | struct rb_test_ctx _ctx, *ctx = &_ctx; |
| 15 | struct ringbuffer *rb; |
| 16 | int rc; |
| 17 | |
| 18 | ringbuffer_test_context_init(ctx); |
| 19 | |
| 20 | rb = ringbuffer_init(5); |
| 21 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame^] | 22 | ctx->rbc = ringbuffer_consumer_register(rb, ringbuffer_poll_append_all, |
| 23 | ctx); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 24 | |
| 25 | ctx->force_only = true; |
| 26 | |
| 27 | /* fill the ringbuffer */ |
| 28 | rc = ringbuffer_queue(rb, in_buf, 4); |
| 29 | assert(!rc); |
| 30 | |
| 31 | assert(ctx->count == 0); |
| 32 | |
| 33 | /* add more data */ |
| 34 | rc = ringbuffer_queue(rb, in_buf + 4, 2); |
| 35 | assert(!rc); |
| 36 | |
| 37 | /* we should have had a forced poll for the initial two bytes */ |
| 38 | assert(ctx->count == 1); |
| 39 | assert(ctx->len == 2); |
| 40 | assert(!memcmp(in_buf, ctx->data, 2)); |
| 41 | |
| 42 | ringbuffer_fini(rb); |
| 43 | ringbuffer_test_context_fini(ctx); |
| 44 | } |
| 45 | |
| 46 | int main(void) |
| 47 | { |
| 48 | test_poll_force(); |
| 49 | return EXIT_SUCCESS; |
| 50 | } |