Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 1 | |
| 2 | struct rb_test_ctx { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 3 | struct ringbuffer_consumer *rbc; |
| 4 | bool ignore_poll; |
| 5 | bool force_only; |
| 6 | int count; |
| 7 | uint8_t *data; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 8 | size_t len; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 9 | }; |
| 10 | |
| 11 | void ringbuffer_test_context_init(struct rb_test_ctx *ctx) |
| 12 | { |
| 13 | ctx->count = 0; |
| 14 | ctx->data = NULL; |
| 15 | ctx->len = 0; |
| 16 | ctx->ignore_poll = false; |
| 17 | ctx->force_only = false; |
| 18 | } |
| 19 | |
| 20 | void ringbuffer_test_context_fini(struct rb_test_ctx *ctx) |
| 21 | { |
| 22 | free(ctx->data); |
| 23 | } |
| 24 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 25 | enum ringbuffer_poll_ret ringbuffer_poll_nop(void *data __attribute__((unused)), |
| 26 | size_t force_len |
| 27 | __attribute__((unused))) |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 28 | { |
| 29 | return RINGBUFFER_POLL_OK; |
| 30 | } |
| 31 | |
| 32 | enum ringbuffer_poll_ret ringbuffer_poll_append_all(void *data, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 33 | size_t force_len) |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 34 | { |
| 35 | struct rb_test_ctx *ctx = data; |
| 36 | size_t len, total_len; |
| 37 | uint8_t *buf; |
| 38 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 39 | if (ctx->ignore_poll) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 40 | return RINGBUFFER_POLL_OK; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 41 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 42 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 43 | if (ctx->force_only && !force_len) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 44 | return RINGBUFFER_POLL_OK; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 45 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 46 | |
| 47 | ctx->count++; |
| 48 | |
| 49 | total_len = 0; |
| 50 | for (;;) { |
| 51 | len = ringbuffer_dequeue_peek(ctx->rbc, total_len, &buf); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 52 | if (!len) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 53 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 54 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 55 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 56 | if (ctx->force_only && total_len + len > force_len) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 57 | len = force_len - total_len; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 58 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 59 | |
| 60 | ctx->data = realloc(ctx->data, ctx->len + len); |
| 61 | memcpy(ctx->data + ctx->len, buf, len); |
| 62 | ctx->len += len; |
| 63 | total_len += len; |
| 64 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 65 | if (ctx->force_only && total_len >= force_len) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 66 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 67 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 68 | } |
| 69 | ringbuffer_dequeue_commit(ctx->rbc, total_len); |
| 70 | |
| 71 | return RINGBUFFER_POLL_OK; |
| 72 | } |
| 73 | |
| 74 | void ringbuffer_dump(struct ringbuffer *rb) |
| 75 | { |
| 76 | struct ringbuffer_consumer *rbc; |
Andrew Jeffery | 8f548f6 | 2023-04-18 11:48:49 +0930 | [diff] [blame] | 77 | size_t i; |
| 78 | int j; |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 79 | |
| 80 | printf("---- ringbuffer (%d consumer%s)\n", rb->n_consumers, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 81 | rb->n_consumers == 1 ? "" : "s"); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 82 | |
| 83 | for (i = 0; i < rb->size; i++) { |
| 84 | bool has_consumer = false; |
| 85 | const char *prefix = ""; |
| 86 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 87 | if (rb->tail == i) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 88 | prefix = "tail=>"; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 89 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 90 | |
| 91 | printf("%6s %02x", prefix, rb->buf[i]); |
| 92 | for (j = 0; j < rb->n_consumers; j++) { |
| 93 | rbc = rb->consumers[j]; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 94 | if (rbc->pos != i) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 95 | continue; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 96 | } |
| 97 | if (!has_consumer) { |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 98 | printf(" <="); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame^] | 99 | } |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 100 | printf("c[%d],len=%zd ", j, ringbuffer_len(rbc)); |
| 101 | has_consumer = true; |
| 102 | } |
| 103 | printf("\n"); |
| 104 | } |
| 105 | } |