Dynamic types
[libside.git] / src / test.c
CommitLineData
f611d0c3
MD
1// SPDX-License-Identifier: MIT
2/*
3 * Copyright 2022 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 */
5
6#include <stdint.h>
7#include <inttypes.h>
8#include <stdlib.h>
9#include <stdio.h>
10
11#include <side/trace.h>
12#include "tracer.h"
13
14/* User code example */
15
16static side_define_event(my_provider_event, "myprovider", "myevent", SIDE_LOGLEVEL_DEBUG,
17 side_field_list(
18 side_field(SIDE_TYPE_U32, "abc"),
19 side_field(SIDE_TYPE_S64, "def"),
20 side_field(SIDE_TYPE_DYNAMIC, "dynamic"),
21 )
22);
23
24static
25void test_fields(void)
26{
27 uint32_t uw = 42;
28 int64_t sdw = -500;
29
30 my_provider_event.enabled = 1;
31 side_event(&my_provider_event, side_arg_list(side_arg_u32(uw), side_arg_s64(sdw), side_arg_string("zzz")));
32}
33
34static side_define_event(my_provider_event2, "myprovider", "myevent2", SIDE_LOGLEVEL_DEBUG,
35 side_field_list(
36 side_field_struct("structfield",
37 side_field_list(
38 side_field(SIDE_TYPE_U32, "x"),
39 side_field(SIDE_TYPE_S64, "y"),
40 )
41 ),
42 side_field(SIDE_TYPE_U8, "z"),
43 )
44);
45
46static
47void test_struct(void)
48{
49 my_provider_event2.enabled = 1;
50 side_event_cond(&my_provider_event2) {
51 side_arg_define_vec(mystruct, side_arg_list(side_arg_u32(21), side_arg_s64(22)));
52 side_event_call(&my_provider_event2, side_arg_list(side_arg_struct(&mystruct), side_arg_u8(55)));
53 }
54}
55
56static side_define_event(my_provider_event_array, "myprovider", "myarray", SIDE_LOGLEVEL_DEBUG,
57 side_field_list(
cdd6e858 58 side_field_array("arr", side_elem_type(SIDE_TYPE_U32), 3),
f611d0c3
MD
59 side_field(SIDE_TYPE_S64, "v"),
60 )
61);
62
63static
64void test_array(void)
65{
66 my_provider_event_array.enabled = 1;
67 side_event_cond(&my_provider_event_array) {
68 side_arg_define_vec(myarray, side_arg_list(side_arg_u32(1), side_arg_u32(2), side_arg_u32(3)));
69 side_event_call(&my_provider_event_array, side_arg_list(side_arg_array(&myarray), side_arg_s64(42)));
70 }
71}
72
73static side_define_event(my_provider_event_vla, "myprovider", "myvla", SIDE_LOGLEVEL_DEBUG,
74 side_field_list(
cdd6e858 75 side_field_vla("vla", side_elem_type(SIDE_TYPE_U32)),
f611d0c3
MD
76 side_field(SIDE_TYPE_S64, "v"),
77 )
78);
79
80static
81void test_vla(void)
82{
83 my_provider_event_vla.enabled = 1;
84 side_event_cond(&my_provider_event_vla) {
85 side_arg_define_vec(myvla, side_arg_list(side_arg_u32(1), side_arg_u32(2), side_arg_u32(3)));
86 side_event_call(&my_provider_event_vla, side_arg_list(side_arg_vla(&myvla), side_arg_s64(42)));
87 }
88}
89
cdd6e858
MD
90/* 1D array visitor */
91
f611d0c3
MD
92struct app_visitor_ctx {
93 const uint32_t *ptr;
352a4b77 94 uint32_t length;
f611d0c3
MD
95};
96
352a4b77
MD
97static
98enum side_visitor_status test_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
f611d0c3
MD
99{
100 struct app_visitor_ctx *ctx = (struct app_visitor_ctx *) _ctx;
352a4b77
MD
101 uint32_t length = ctx->length, i;
102
103 for (i = 0; i < length; i++) {
104 const struct side_arg_vec elem = {
105 .type = SIDE_TYPE_U32,
106 .u = {
107 .side_u32 = ctx->ptr[i],
108 },
109 };
110 tracer_ctx->write_elem(tracer_ctx, &elem);
111 }
f611d0c3
MD
112 return SIDE_VISITOR_STATUS_OK;
113}
114
115static uint32_t testarray[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
116
117static side_define_event(my_provider_event_vla_visitor, "myprovider", "myvlavisit", SIDE_LOGLEVEL_DEBUG,
118 side_field_list(
cdd6e858 119 side_field_vla_visitor("vlavisit", side_elem_type(SIDE_TYPE_U32), test_visitor),
f611d0c3
MD
120 side_field(SIDE_TYPE_S64, "v"),
121 )
122);
123
124static
125void test_vla_visitor(void)
126{
127 my_provider_event_vla_visitor.enabled = 1;
128 side_event_cond(&my_provider_event_vla_visitor) {
129 struct app_visitor_ctx ctx = {
130 .ptr = testarray,
352a4b77 131 .length = SIDE_ARRAY_SIZE(testarray),
f611d0c3
MD
132 };
133 side_event_call(&my_provider_event_vla_visitor, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42)));
134 }
135}
136
cdd6e858
MD
137/* 2D array visitor */
138
139struct app_visitor_2d_inner_ctx {
140 const uint32_t *ptr;
141 uint32_t length;
142};
143
144static
145enum side_visitor_status test_inner_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
146{
147 struct app_visitor_2d_inner_ctx *ctx = (struct app_visitor_2d_inner_ctx *) _ctx;
148 uint32_t length = ctx->length, i;
149
150 for (i = 0; i < length; i++) {
151 const struct side_arg_vec elem = {
152 .type = SIDE_TYPE_U32,
153 .u = {
154 .side_u32 = ctx->ptr[i],
155 },
156 };
157 tracer_ctx->write_elem(tracer_ctx, &elem);
158 }
159 return SIDE_VISITOR_STATUS_OK;
160}
161
162struct app_visitor_2d_outer_ctx {
163 const uint32_t (*ptr)[2];
164 uint32_t length;
165};
166
167static
168enum side_visitor_status test_outer_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
169{
170 struct app_visitor_2d_outer_ctx *ctx = (struct app_visitor_2d_outer_ctx *) _ctx;
171 uint32_t length = ctx->length, i;
172
173 for (i = 0; i < length; i++) {
174 struct app_visitor_2d_inner_ctx inner_ctx = {
175 .ptr = ctx->ptr[i],
176 .length = 2,
177 };
178 const struct side_arg_vec elem = side_arg_vla_visitor(&inner_ctx);
179 tracer_ctx->write_elem(tracer_ctx, &elem);
180 }
181 return SIDE_VISITOR_STATUS_OK;
182}
183
184static uint32_t testarray2d[][2] = {
185 { 1, 2 },
186 { 33, 44 },
187 { 55, 66 },
188};
189
190static side_define_event(my_provider_event_vla_visitor2d, "myprovider", "myvlavisit2d", SIDE_LOGLEVEL_DEBUG,
191 side_field_list(
192 side_field_vla_visitor("vlavisit2d",
193 side_elem(side_type_vla_visitor_decl(side_elem_type(SIDE_TYPE_U32), test_inner_visitor)), test_outer_visitor),
194 side_field(SIDE_TYPE_S64, "v"),
195 )
196);
197
198static
199void test_vla_visitor_2d(void)
200{
201 my_provider_event_vla_visitor2d.enabled = 1;
202 side_event_cond(&my_provider_event_vla_visitor2d) {
203 struct app_visitor_2d_outer_ctx ctx = {
204 .ptr = testarray2d,
205 .length = SIDE_ARRAY_SIZE(testarray2d),
206 };
207 side_event_call(&my_provider_event_vla_visitor2d, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42)));
208 }
209}
210
ba845af5
MD
211static int64_t array_fixint[] = { -444, 555, 123, 2897432587 };
212
213static side_define_event(my_provider_event_array_fixint, "myprovider", "myarrayfixint", SIDE_LOGLEVEL_DEBUG,
214 side_field_list(
cdd6e858 215 side_field_array("arrfixint", side_elem_type(SIDE_TYPE_S64), SIDE_ARRAY_SIZE(array_fixint)),
ba845af5
MD
216 side_field(SIDE_TYPE_S64, "v"),
217 )
218);
219
220static
221void test_array_fixint(void)
222{
223 my_provider_event_array_fixint.enabled = 1;
1533629f
MD
224 side_event(&my_provider_event_array_fixint,
225 side_arg_list(side_arg_array_s64(array_fixint), side_arg_s64(42)));
226}
227
228static int64_t vla_fixint[] = { -444, 555, 123, 2897432587 };
229
230static side_define_event(my_provider_event_vla_fixint, "myprovider", "myvlafixint", SIDE_LOGLEVEL_DEBUG,
231 side_field_list(
cdd6e858 232 side_field_vla("vlafixint", side_elem_type(SIDE_TYPE_S64)),
1533629f
MD
233 side_field(SIDE_TYPE_S64, "v"),
234 )
235);
236
237static
238void test_vla_fixint(void)
239{
240 my_provider_event_vla_fixint.enabled = 1;
241 side_event(&my_provider_event_vla_fixint,
242 side_arg_list(side_arg_vla_s64(vla_fixint, SIDE_ARRAY_SIZE(vla_fixint)), side_arg_s64(42)));
ba845af5
MD
243}
244
f611d0c3
MD
245int main()
246{
247 test_fields();
248 test_struct();
249 test_array();
250 test_vla();
251 test_vla_visitor();
cdd6e858 252 test_vla_visitor_2d();
ba845af5 253 test_array_fixint();
1533629f 254 test_vla_fixint();
f611d0c3
MD
255 return 0;
256}
This page took 0.033926 seconds and 4 git commands to generate.