blob: fff432586c4c5257d43bf83b56666768714dd9d3 [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
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093039 if (ctx->ignore_poll) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080040 return RINGBUFFER_POLL_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093041 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080042
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093043 if (ctx->force_only && !force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080044 return RINGBUFFER_POLL_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093045 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080046
47 ctx->count++;
48
49 total_len = 0;
50 for (;;) {
51 len = ringbuffer_dequeue_peek(ctx->rbc, total_len, &buf);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093052 if (!len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080053 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093054 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080055
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093056 if (ctx->force_only && total_len + len > force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080057 len = force_len - total_len;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093058 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080059
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 Jeffery2834c5b2023-04-19 12:47:09 +093065 if (ctx->force_only && total_len >= force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080066 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093067 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080068 }
69 ringbuffer_dequeue_commit(ctx->rbc, total_len);
70
71 return RINGBUFFER_POLL_OK;
72}
73
74void ringbuffer_dump(struct ringbuffer *rb)
75{
76 struct ringbuffer_consumer *rbc;
Andrew Jeffery8f548f62023-04-18 11:48:49 +093077 size_t i;
78 int j;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080079
80 printf("---- ringbuffer (%d consumer%s)\n", rb->n_consumers,
Andrew Jefferya72711a2023-04-18 18:19:41 +093081 rb->n_consumers == 1 ? "" : "s");
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080082
83 for (i = 0; i < rb->size; i++) {
84 bool has_consumer = false;
85 const char *prefix = "";
86
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093087 if (rb->tail == i) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080088 prefix = "tail=>";
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093089 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080090
91 printf("%6s %02x", prefix, rb->buf[i]);
92 for (j = 0; j < rb->n_consumers; j++) {
93 rbc = rb->consumers[j];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093094 if (rbc->pos != i) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080095 continue;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093096 }
97 if (!has_consumer) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080098 printf(" <=");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093099 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800100 printf("c[%d],len=%zd ", j, ringbuffer_len(rbc));
101 has_consumer = true;
102 }
103 printf("\n");
104 }
105}