blob: 56e37eed8f8cc52ae4153e823bbe5956f9ce8d67 [file] [log] [blame]
Jeremy Kerrc9775ce2017-02-07 16:25:34 +08001
2struct rb_test_ctx {
Andrew Jefferya72711a2023-04-18 18:19:41 +09303 struct ringbuffer_consumer *rbc;
4 bool ignore_poll;
5 bool force_only;
6 int count;
7 uint8_t *data;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +09308 size_t len;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +08009};
10
11void 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
20void ringbuffer_test_context_fini(struct rb_test_ctx *ctx)
21{
22 free(ctx->data);
23}
24
Andrew Jefferya72711a2023-04-18 18:19:41 +093025enum ringbuffer_poll_ret ringbuffer_poll_nop(void *data __attribute__((unused)),
26 size_t force_len
27 __attribute__((unused)))
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080028{
29 return RINGBUFFER_POLL_OK;
30}
31
32enum ringbuffer_poll_ret ringbuffer_poll_append_all(void *data,
Andrew Jefferya72711a2023-04-18 18:19:41 +093033 size_t force_len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080034{
35 struct rb_test_ctx *ctx = data;
36 size_t len, total_len;
37 uint8_t *buf;
38
39 if (ctx->ignore_poll)
40 return RINGBUFFER_POLL_OK;
41
42 if (ctx->force_only && !force_len)
43 return RINGBUFFER_POLL_OK;
44
45 ctx->count++;
46
47 total_len = 0;
48 for (;;) {
49 len = ringbuffer_dequeue_peek(ctx->rbc, total_len, &buf);
50 if (!len)
51 break;
52
53 if (ctx->force_only && total_len + len > force_len)
54 len = force_len - total_len;
55
56 ctx->data = realloc(ctx->data, ctx->len + len);
57 memcpy(ctx->data + ctx->len, buf, len);
58 ctx->len += len;
59 total_len += len;
60
61 if (ctx->force_only && total_len >= force_len)
62 break;
63 }
64 ringbuffer_dequeue_commit(ctx->rbc, total_len);
65
66 return RINGBUFFER_POLL_OK;
67}
68
69void ringbuffer_dump(struct ringbuffer *rb)
70{
71 struct ringbuffer_consumer *rbc;
Andrew Jeffery8f548f62023-04-18 11:48:49 +093072 size_t i;
73 int j;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080074
75 printf("---- ringbuffer (%d consumer%s)\n", rb->n_consumers,
Andrew Jefferya72711a2023-04-18 18:19:41 +093076 rb->n_consumers == 1 ? "" : "s");
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080077
78 for (i = 0; i < rb->size; i++) {
79 bool has_consumer = false;
80 const char *prefix = "";
81
82 if (rb->tail == i)
83 prefix = "tail=>";
84
85 printf("%6s %02x", prefix, rb->buf[i]);
86 for (j = 0; j < rb->n_consumers; j++) {
87 rbc = rb->consumers[j];
88 if (rbc->pos != i)
89 continue;
90 if (!has_consumer)
91 printf(" <=");
92 printf("c[%d],len=%zd ", j, ringbuffer_len(rbc));
93 has_consumer = true;
94 }
95 printf("\n");
96 }
97}