Test nested dynamic vla
[libside.git] / src / test.c
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
16 static 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
24 static
25 void 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),
32 side_arg_dynamic(side_arg_dynamic_string("zzz"))));
33 }
34
35 static side_define_event(my_provider_event2, "myprovider", "myevent2", SIDE_LOGLEVEL_DEBUG,
36 side_field_list(
37 side_field_struct("structfield",
38 side_field_list(
39 side_field(SIDE_TYPE_U32, "x"),
40 side_field(SIDE_TYPE_S64, "y"),
41 )
42 ),
43 side_field(SIDE_TYPE_U8, "z"),
44 )
45 );
46
47 static
48 void test_struct(void)
49 {
50 my_provider_event2.enabled = 1;
51 side_event_cond(&my_provider_event2) {
52 side_arg_define_vec(mystruct, side_arg_list(side_arg_u32(21), side_arg_s64(22)));
53 side_event_call(&my_provider_event2, side_arg_list(side_arg_struct(&mystruct), side_arg_u8(55)));
54 }
55 }
56
57 static side_define_event(my_provider_event_array, "myprovider", "myarray", SIDE_LOGLEVEL_DEBUG,
58 side_field_list(
59 side_field_array("arr", side_elem_type(SIDE_TYPE_U32), 3),
60 side_field(SIDE_TYPE_S64, "v"),
61 )
62 );
63
64 static
65 void test_array(void)
66 {
67 my_provider_event_array.enabled = 1;
68 side_event_cond(&my_provider_event_array) {
69 side_arg_define_vec(myarray, side_arg_list(side_arg_u32(1), side_arg_u32(2), side_arg_u32(3)));
70 side_event_call(&my_provider_event_array, side_arg_list(side_arg_array(&myarray), side_arg_s64(42)));
71 }
72 }
73
74 static side_define_event(my_provider_event_vla, "myprovider", "myvla", SIDE_LOGLEVEL_DEBUG,
75 side_field_list(
76 side_field_vla("vla", side_elem_type(SIDE_TYPE_U32)),
77 side_field(SIDE_TYPE_S64, "v"),
78 )
79 );
80
81 static
82 void test_vla(void)
83 {
84 my_provider_event_vla.enabled = 1;
85 side_event_cond(&my_provider_event_vla) {
86 side_arg_define_vec(myvla, side_arg_list(side_arg_u32(1), side_arg_u32(2), side_arg_u32(3)));
87 side_event_call(&my_provider_event_vla, side_arg_list(side_arg_vla(&myvla), side_arg_s64(42)));
88 }
89 }
90
91 /* 1D array visitor */
92
93 struct app_visitor_ctx {
94 const uint32_t *ptr;
95 uint32_t length;
96 };
97
98 static
99 enum side_visitor_status test_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
100 {
101 struct app_visitor_ctx *ctx = (struct app_visitor_ctx *) _ctx;
102 uint32_t length = ctx->length, i;
103
104 for (i = 0; i < length; i++) {
105 const struct side_arg_vec elem = {
106 .type = SIDE_TYPE_U32,
107 .u = {
108 .side_u32 = ctx->ptr[i],
109 },
110 };
111 tracer_ctx->write_elem(tracer_ctx, &elem);
112 }
113 return SIDE_VISITOR_STATUS_OK;
114 }
115
116 static uint32_t testarray[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
117
118 static side_define_event(my_provider_event_vla_visitor, "myprovider", "myvlavisit", SIDE_LOGLEVEL_DEBUG,
119 side_field_list(
120 side_field_vla_visitor("vlavisit", side_elem_type(SIDE_TYPE_U32), test_visitor),
121 side_field(SIDE_TYPE_S64, "v"),
122 )
123 );
124
125 static
126 void test_vla_visitor(void)
127 {
128 my_provider_event_vla_visitor.enabled = 1;
129 side_event_cond(&my_provider_event_vla_visitor) {
130 struct app_visitor_ctx ctx = {
131 .ptr = testarray,
132 .length = SIDE_ARRAY_SIZE(testarray),
133 };
134 side_event_call(&my_provider_event_vla_visitor, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42)));
135 }
136 }
137
138 /* 2D array visitor */
139
140 struct app_visitor_2d_inner_ctx {
141 const uint32_t *ptr;
142 uint32_t length;
143 };
144
145 static
146 enum side_visitor_status test_inner_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
147 {
148 struct app_visitor_2d_inner_ctx *ctx = (struct app_visitor_2d_inner_ctx *) _ctx;
149 uint32_t length = ctx->length, i;
150
151 for (i = 0; i < length; i++) {
152 const struct side_arg_vec elem = {
153 .type = SIDE_TYPE_U32,
154 .u = {
155 .side_u32 = ctx->ptr[i],
156 },
157 };
158 tracer_ctx->write_elem(tracer_ctx, &elem);
159 }
160 return SIDE_VISITOR_STATUS_OK;
161 }
162
163 struct app_visitor_2d_outer_ctx {
164 const uint32_t (*ptr)[2];
165 uint32_t length;
166 };
167
168 static
169 enum side_visitor_status test_outer_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
170 {
171 struct app_visitor_2d_outer_ctx *ctx = (struct app_visitor_2d_outer_ctx *) _ctx;
172 uint32_t length = ctx->length, i;
173
174 for (i = 0; i < length; i++) {
175 struct app_visitor_2d_inner_ctx inner_ctx = {
176 .ptr = ctx->ptr[i],
177 .length = 2,
178 };
179 const struct side_arg_vec elem = side_arg_vla_visitor(&inner_ctx);
180 tracer_ctx->write_elem(tracer_ctx, &elem);
181 }
182 return SIDE_VISITOR_STATUS_OK;
183 }
184
185 static uint32_t testarray2d[][2] = {
186 { 1, 2 },
187 { 33, 44 },
188 { 55, 66 },
189 };
190
191 static side_define_event(my_provider_event_vla_visitor2d, "myprovider", "myvlavisit2d", SIDE_LOGLEVEL_DEBUG,
192 side_field_list(
193 side_field_vla_visitor("vlavisit2d",
194 side_elem(side_type_vla_visitor_decl(side_elem_type(SIDE_TYPE_U32), test_inner_visitor)), test_outer_visitor),
195 side_field(SIDE_TYPE_S64, "v"),
196 )
197 );
198
199 static
200 void test_vla_visitor_2d(void)
201 {
202 my_provider_event_vla_visitor2d.enabled = 1;
203 side_event_cond(&my_provider_event_vla_visitor2d) {
204 struct app_visitor_2d_outer_ctx ctx = {
205 .ptr = testarray2d,
206 .length = SIDE_ARRAY_SIZE(testarray2d),
207 };
208 side_event_call(&my_provider_event_vla_visitor2d, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42)));
209 }
210 }
211
212 static int64_t array_fixint[] = { -444, 555, 123, 2897432587 };
213
214 static side_define_event(my_provider_event_array_fixint, "myprovider", "myarrayfixint", SIDE_LOGLEVEL_DEBUG,
215 side_field_list(
216 side_field_array("arrfixint", side_elem_type(SIDE_TYPE_S64), SIDE_ARRAY_SIZE(array_fixint)),
217 side_field(SIDE_TYPE_S64, "v"),
218 )
219 );
220
221 static
222 void test_array_fixint(void)
223 {
224 my_provider_event_array_fixint.enabled = 1;
225 side_event(&my_provider_event_array_fixint,
226 side_arg_list(side_arg_array_s64(array_fixint), side_arg_s64(42)));
227 }
228
229 static int64_t vla_fixint[] = { -444, 555, 123, 2897432587 };
230
231 static side_define_event(my_provider_event_vla_fixint, "myprovider", "myvlafixint", SIDE_LOGLEVEL_DEBUG,
232 side_field_list(
233 side_field_vla("vlafixint", side_elem_type(SIDE_TYPE_S64)),
234 side_field(SIDE_TYPE_S64, "v"),
235 )
236 );
237
238 static
239 void test_vla_fixint(void)
240 {
241 my_provider_event_vla_fixint.enabled = 1;
242 side_event(&my_provider_event_vla_fixint,
243 side_arg_list(side_arg_vla_s64(vla_fixint, SIDE_ARRAY_SIZE(vla_fixint)), side_arg_s64(42)));
244 }
245
246 static side_define_event(my_provider_event_dynamic_basic,
247 "myprovider", "mydynamicbasic", SIDE_LOGLEVEL_DEBUG,
248 side_field_list(
249 side_field(SIDE_TYPE_DYNAMIC, "dynamic"),
250 )
251 );
252
253 static
254 void test_dynamic_basic_type(void)
255 {
256 my_provider_event_dynamic_basic.enabled = 1;
257 side_event(&my_provider_event_dynamic_basic,
258 side_arg_list(side_arg_dynamic(side_arg_dynamic_s16(-33))));
259 }
260
261 static side_define_event(my_provider_event_dynamic_vla,
262 "myprovider", "mydynamicvla", SIDE_LOGLEVEL_DEBUG,
263 side_field_list(
264 side_field(SIDE_TYPE_DYNAMIC, "dynamic"),
265 )
266 );
267
268 static
269 void test_dynamic_vla(void)
270 {
271 side_arg_dynamic_define_vec(myvla,
272 side_arg_list(
273 side_arg_dynamic_u32(1), side_arg_dynamic_u32(2), side_arg_dynamic_u32(3))
274 );
275 my_provider_event_dynamic_vla.enabled = 1;
276 side_event(&my_provider_event_dynamic_vla,
277 side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla))));
278 }
279
280 static side_define_event(my_provider_event_dynamic_null,
281 "myprovider", "mydynamicnull", SIDE_LOGLEVEL_DEBUG,
282 side_field_list(
283 side_field(SIDE_TYPE_DYNAMIC, "dynamic"),
284 )
285 );
286
287 static
288 void test_dynamic_null(void)
289 {
290 my_provider_event_dynamic_null.enabled = 1;
291 side_event(&my_provider_event_dynamic_null,
292 side_arg_list(side_arg_dynamic(side_arg_dynamic_null())));
293 }
294
295 static side_define_event(my_provider_event_dynamic_map,
296 "myprovider", "mydynamicmap", SIDE_LOGLEVEL_DEBUG,
297 side_field_list(
298 side_field(SIDE_TYPE_DYNAMIC, "dynamic"),
299 )
300 );
301
302 static
303 void test_dynamic_map(void)
304 {
305 side_arg_dynamic_define_map(mymap,
306 side_arg_list(
307 side_arg_dynamic_field("a", side_arg_dynamic_u32(43)),
308 side_arg_dynamic_field("b", side_arg_dynamic_string("zzz")),
309 side_arg_dynamic_field("c", side_arg_dynamic_null())
310 )
311 );
312
313 my_provider_event_dynamic_map.enabled = 1;
314 side_event(&my_provider_event_dynamic_map,
315 side_arg_list(side_arg_dynamic(side_arg_dynamic_map(&mymap))));
316 }
317
318 static side_define_event(my_provider_event_dynamic_nested_map,
319 "myprovider", "mydynamicnestedmap", SIDE_LOGLEVEL_DEBUG,
320 side_field_list(
321 side_field(SIDE_TYPE_DYNAMIC, "dynamic"),
322 )
323 );
324
325 static
326 void test_dynamic_nested_map(void)
327 {
328 side_arg_dynamic_define_map(nested,
329 side_arg_list(
330 side_arg_dynamic_field("a", side_arg_dynamic_u32(43)),
331 side_arg_dynamic_field("b", side_arg_dynamic_u8(55)),
332 )
333 );
334 side_arg_dynamic_define_map(nested2,
335 side_arg_list(
336 side_arg_dynamic_field("aa", side_arg_dynamic_u64(128)),
337 side_arg_dynamic_field("bb", side_arg_dynamic_u16(1)),
338 )
339 );
340 side_arg_dynamic_define_map(mymap,
341 side_arg_list(
342 side_arg_dynamic_field("nested", side_arg_dynamic_map(&nested)),
343 side_arg_dynamic_field("nested2", side_arg_dynamic_map(&nested2)),
344 )
345 );
346 my_provider_event_dynamic_nested_map.enabled = 1;
347 side_event(&my_provider_event_dynamic_nested_map,
348 side_arg_list(side_arg_dynamic(side_arg_dynamic_map(&mymap))));
349 }
350
351 static side_define_event(my_provider_event_dynamic_vla_map,
352 "myprovider", "mydynamicvlamap", SIDE_LOGLEVEL_DEBUG,
353 side_field_list(
354 side_field(SIDE_TYPE_DYNAMIC, "dynamic"),
355 )
356 );
357
358 static
359 void test_dynamic_vla_map(void)
360 {
361 side_arg_dynamic_define_map(nested,
362 side_arg_list(
363 side_arg_dynamic_field("a", side_arg_dynamic_u32(43)),
364 side_arg_dynamic_field("b", side_arg_dynamic_u8(55)),
365 )
366 );
367 side_arg_dynamic_define_vec(myvla,
368 side_arg_list(
369 side_arg_dynamic_map(&nested),
370 side_arg_dynamic_map(&nested),
371 side_arg_dynamic_map(&nested),
372 side_arg_dynamic_map(&nested),
373 )
374 );
375 my_provider_event_dynamic_vla_map.enabled = 1;
376 side_event(&my_provider_event_dynamic_vla_map,
377 side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla))));
378 }
379
380 static side_define_event(my_provider_event_dynamic_map_vla,
381 "myprovider", "mydynamicmapvla", SIDE_LOGLEVEL_DEBUG,
382 side_field_list(
383 side_field(SIDE_TYPE_DYNAMIC, "dynamic"),
384 )
385 );
386
387 static
388 void test_dynamic_map_vla(void)
389 {
390 side_arg_dynamic_define_vec(myvla,
391 side_arg_list(
392 side_arg_dynamic_u32(1), side_arg_dynamic_u32(2), side_arg_dynamic_u32(3))
393 );
394 side_arg_dynamic_define_vec(myvla2,
395 side_arg_list(
396 side_arg_dynamic_u32(4), side_arg_dynamic_u64(5), side_arg_dynamic_u32(6))
397 );
398 side_arg_dynamic_define_map(mymap,
399 side_arg_list(
400 side_arg_dynamic_field("a", side_arg_dynamic_vla(&myvla)),
401 side_arg_dynamic_field("b", side_arg_dynamic_vla(&myvla2)),
402 )
403 );
404 my_provider_event_dynamic_map_vla.enabled = 1;
405 side_event(&my_provider_event_dynamic_map_vla,
406 side_arg_list(side_arg_dynamic(side_arg_dynamic_map(&mymap))));
407 }
408
409 static side_define_event(my_provider_event_dynamic_nested_vla,
410 "myprovider", "mydynamicnestedvla", SIDE_LOGLEVEL_DEBUG,
411 side_field_list(
412 side_field(SIDE_TYPE_DYNAMIC, "dynamic"),
413 )
414 );
415
416 static
417 void test_dynamic_nested_vla(void)
418 {
419 side_arg_dynamic_define_vec(nestedvla,
420 side_arg_list(
421 side_arg_dynamic_u32(1), side_arg_dynamic_u16(2), side_arg_dynamic_u32(3),
422 )
423 );
424 side_arg_dynamic_define_vec(nestedvla2,
425 side_arg_list(
426 side_arg_dynamic_u8(4), side_arg_dynamic_u32(5), side_arg_dynamic_u32(6),
427 )
428 );
429 side_arg_dynamic_define_vec(myvla,
430 side_arg_list(
431 side_arg_dynamic_vla(&nestedvla),
432 side_arg_dynamic_vla(&nestedvla2),
433 )
434 );
435 my_provider_event_dynamic_nested_vla.enabled = 1;
436 side_event(&my_provider_event_dynamic_nested_vla,
437 side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla))));
438 }
439
440 int main()
441 {
442 test_fields();
443 test_struct();
444 test_array();
445 test_vla();
446 test_vla_visitor();
447 test_vla_visitor_2d();
448 test_array_fixint();
449 test_vla_fixint();
450 test_dynamic_basic_type();
451 test_dynamic_vla();
452 test_dynamic_null();
453 test_dynamic_map();
454 test_dynamic_nested_map();
455 test_dynamic_vla_map();
456 test_dynamic_map_vla();
457 test_dynamic_nested_vla();
458 return 0;
459 }
This page took 0.049559 seconds and 4 git commands to generate.