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