Add variadic event flag
[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 #include <stdbool.h>
11
12 #include <side/trace.h>
13 #include "tracer.h"
14
15 /* User code example */
16
17 static side_define_event(my_provider_event, "myprovider", "myevent", SIDE_LOGLEVEL_DEBUG,
18 side_field_list(
19 side_field("abc", SIDE_TYPE_U32),
20 side_field("def", SIDE_TYPE_S64),
21 side_field("dynamic", SIDE_TYPE_DYNAMIC),
22 )
23 );
24
25 static
26 void test_fields(void)
27 {
28 uint32_t uw = 42;
29 int64_t sdw = -500;
30
31 my_provider_event.enabled = 1;
32 side_event(&my_provider_event, side_arg_list(side_arg_u32(uw), side_arg_s64(sdw),
33 side_arg_dynamic(side_arg_dynamic_string("zzz"))));
34 }
35
36 static side_define_event(my_provider_event2, "myprovider", "myevent2", SIDE_LOGLEVEL_DEBUG,
37 side_field_list(
38 side_field_struct("structfield",
39 side_field_list(
40 side_field("x", SIDE_TYPE_U32),
41 side_field("y", SIDE_TYPE_S64),
42 )
43 ),
44 side_field("z", SIDE_TYPE_U8),
45 )
46 );
47
48 static
49 void test_struct(void)
50 {
51 my_provider_event2.enabled = 1;
52 side_event_cond(&my_provider_event2) {
53 side_arg_define_vec(mystruct, side_arg_list(side_arg_u32(21), side_arg_s64(22)));
54 side_event_call(&my_provider_event2, side_arg_list(side_arg_struct(&mystruct), side_arg_u8(55)));
55 }
56 }
57
58 static side_define_event(my_provider_event_array, "myprovider", "myarray", SIDE_LOGLEVEL_DEBUG,
59 side_field_list(
60 side_field_array("arr", side_elem_type(SIDE_TYPE_U32), 3),
61 side_field("v", SIDE_TYPE_S64),
62 )
63 );
64
65 static
66 void test_array(void)
67 {
68 my_provider_event_array.enabled = 1;
69 side_event_cond(&my_provider_event_array) {
70 side_arg_define_vec(myarray, side_arg_list(side_arg_u32(1), side_arg_u32(2), side_arg_u32(3)));
71 side_event_call(&my_provider_event_array, side_arg_list(side_arg_array(&myarray), side_arg_s64(42)));
72 }
73 }
74
75 static side_define_event(my_provider_event_vla, "myprovider", "myvla", SIDE_LOGLEVEL_DEBUG,
76 side_field_list(
77 side_field_vla("vla", side_elem_type(SIDE_TYPE_U32)),
78 side_field("v", SIDE_TYPE_S64),
79 )
80 );
81
82 static
83 void test_vla(void)
84 {
85 my_provider_event_vla.enabled = 1;
86 side_event_cond(&my_provider_event_vla) {
87 side_arg_define_vec(myvla, side_arg_list(side_arg_u32(1), side_arg_u32(2), side_arg_u32(3)));
88 side_event_call(&my_provider_event_vla, side_arg_list(side_arg_vla(&myvla), side_arg_s64(42)));
89 }
90 }
91
92 /* 1D array visitor */
93
94 struct app_visitor_ctx {
95 const uint32_t *ptr;
96 uint32_t length;
97 };
98
99 static
100 enum side_visitor_status test_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
101 {
102 struct app_visitor_ctx *ctx = (struct app_visitor_ctx *) _ctx;
103 uint32_t length = ctx->length, i;
104
105 for (i = 0; i < length; i++) {
106 const struct side_arg_vec elem = {
107 .type = SIDE_TYPE_U32,
108 .u = {
109 .side_u32 = ctx->ptr[i],
110 },
111 };
112 if (tracer_ctx->write_elem(tracer_ctx, &elem) != SIDE_VISITOR_STATUS_OK)
113 return SIDE_VISITOR_STATUS_ERROR;
114 }
115 return SIDE_VISITOR_STATUS_OK;
116 }
117
118 static uint32_t testarray[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
119
120 static side_define_event(my_provider_event_vla_visitor, "myprovider", "myvlavisit", SIDE_LOGLEVEL_DEBUG,
121 side_field_list(
122 side_field_vla_visitor("vlavisit", side_elem_type(SIDE_TYPE_U32), test_visitor),
123 side_field("v", SIDE_TYPE_S64),
124 )
125 );
126
127 static
128 void test_vla_visitor(void)
129 {
130 my_provider_event_vla_visitor.enabled = 1;
131 side_event_cond(&my_provider_event_vla_visitor) {
132 struct app_visitor_ctx ctx = {
133 .ptr = testarray,
134 .length = SIDE_ARRAY_SIZE(testarray),
135 };
136 side_event_call(&my_provider_event_vla_visitor, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42)));
137 }
138 }
139
140 /* 2D array visitor */
141
142 struct app_visitor_2d_inner_ctx {
143 const uint32_t *ptr;
144 uint32_t length;
145 };
146
147 static
148 enum side_visitor_status test_inner_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
149 {
150 struct app_visitor_2d_inner_ctx *ctx = (struct app_visitor_2d_inner_ctx *) _ctx;
151 uint32_t length = ctx->length, i;
152
153 for (i = 0; i < length; i++) {
154 const struct side_arg_vec elem = {
155 .type = SIDE_TYPE_U32,
156 .u = {
157 .side_u32 = ctx->ptr[i],
158 },
159 };
160 if (tracer_ctx->write_elem(tracer_ctx, &elem) != SIDE_VISITOR_STATUS_OK)
161 return SIDE_VISITOR_STATUS_ERROR;
162 }
163 return SIDE_VISITOR_STATUS_OK;
164 }
165
166 struct app_visitor_2d_outer_ctx {
167 const uint32_t (*ptr)[2];
168 uint32_t length;
169 };
170
171 static
172 enum side_visitor_status test_outer_visitor(const struct side_tracer_visitor_ctx *tracer_ctx, void *_ctx)
173 {
174 struct app_visitor_2d_outer_ctx *ctx = (struct app_visitor_2d_outer_ctx *) _ctx;
175 uint32_t length = ctx->length, i;
176
177 for (i = 0; i < length; i++) {
178 struct app_visitor_2d_inner_ctx inner_ctx = {
179 .ptr = ctx->ptr[i],
180 .length = 2,
181 };
182 const struct side_arg_vec elem = side_arg_vla_visitor(&inner_ctx);
183 if (tracer_ctx->write_elem(tracer_ctx, &elem) != SIDE_VISITOR_STATUS_OK)
184 return SIDE_VISITOR_STATUS_ERROR;
185 }
186 return SIDE_VISITOR_STATUS_OK;
187 }
188
189 static uint32_t testarray2d[][2] = {
190 { 1, 2 },
191 { 33, 44 },
192 { 55, 66 },
193 };
194
195 static side_define_event(my_provider_event_vla_visitor2d, "myprovider", "myvlavisit2d", SIDE_LOGLEVEL_DEBUG,
196 side_field_list(
197 side_field_vla_visitor("vlavisit2d",
198 side_elem(side_type_vla_visitor_decl(side_elem_type(SIDE_TYPE_U32), test_inner_visitor)), test_outer_visitor),
199 side_field("v", SIDE_TYPE_S64),
200 )
201 );
202
203 static
204 void test_vla_visitor_2d(void)
205 {
206 my_provider_event_vla_visitor2d.enabled = 1;
207 side_event_cond(&my_provider_event_vla_visitor2d) {
208 struct app_visitor_2d_outer_ctx ctx = {
209 .ptr = testarray2d,
210 .length = SIDE_ARRAY_SIZE(testarray2d),
211 };
212 side_event_call(&my_provider_event_vla_visitor2d, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42)));
213 }
214 }
215
216 static int64_t array_fixint[] = { -444, 555, 123, 2897432587 };
217
218 static side_define_event(my_provider_event_array_fixint, "myprovider", "myarrayfixint", SIDE_LOGLEVEL_DEBUG,
219 side_field_list(
220 side_field_array("arrfixint", side_elem_type(SIDE_TYPE_S64), SIDE_ARRAY_SIZE(array_fixint)),
221 side_field("v", SIDE_TYPE_S64),
222 )
223 );
224
225 static
226 void test_array_fixint(void)
227 {
228 my_provider_event_array_fixint.enabled = 1;
229 side_event(&my_provider_event_array_fixint,
230 side_arg_list(side_arg_array_s64(array_fixint), side_arg_s64(42)));
231 }
232
233 static int64_t vla_fixint[] = { -444, 555, 123, 2897432587 };
234
235 static side_define_event(my_provider_event_vla_fixint, "myprovider", "myvlafixint", SIDE_LOGLEVEL_DEBUG,
236 side_field_list(
237 side_field_vla("vlafixint", side_elem_type(SIDE_TYPE_S64)),
238 side_field("v", SIDE_TYPE_S64),
239 )
240 );
241
242 static
243 void test_vla_fixint(void)
244 {
245 my_provider_event_vla_fixint.enabled = 1;
246 side_event(&my_provider_event_vla_fixint,
247 side_arg_list(side_arg_vla_s64(vla_fixint, SIDE_ARRAY_SIZE(vla_fixint)), side_arg_s64(42)));
248 }
249
250 static side_define_event(my_provider_event_dynamic_basic,
251 "myprovider", "mydynamicbasic", SIDE_LOGLEVEL_DEBUG,
252 side_field_list(
253 side_field("dynamic", SIDE_TYPE_DYNAMIC),
254 )
255 );
256
257 static
258 void test_dynamic_basic_type(void)
259 {
260 my_provider_event_dynamic_basic.enabled = 1;
261 side_event(&my_provider_event_dynamic_basic,
262 side_arg_list(side_arg_dynamic(side_arg_dynamic_s16(-33))));
263 }
264
265 static side_define_event(my_provider_event_dynamic_vla,
266 "myprovider", "mydynamicvla", SIDE_LOGLEVEL_DEBUG,
267 side_field_list(
268 side_field("dynamic", SIDE_TYPE_DYNAMIC),
269 )
270 );
271
272 static
273 void test_dynamic_vla(void)
274 {
275 side_arg_dynamic_define_vec(myvla,
276 side_arg_list(
277 side_arg_dynamic_u32(1), side_arg_dynamic_u32(2), side_arg_dynamic_u32(3),
278 )
279 );
280 my_provider_event_dynamic_vla.enabled = 1;
281 side_event(&my_provider_event_dynamic_vla,
282 side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla))));
283 }
284
285 static side_define_event(my_provider_event_dynamic_null,
286 "myprovider", "mydynamicnull", SIDE_LOGLEVEL_DEBUG,
287 side_field_list(
288 side_field("dynamic", SIDE_TYPE_DYNAMIC),
289 )
290 );
291
292 static
293 void test_dynamic_null(void)
294 {
295 my_provider_event_dynamic_null.enabled = 1;
296 side_event(&my_provider_event_dynamic_null,
297 side_arg_list(side_arg_dynamic(side_arg_dynamic_null())));
298 }
299
300 static side_define_event(my_provider_event_dynamic_struct,
301 "myprovider", "mydynamicstruct", SIDE_LOGLEVEL_DEBUG,
302 side_field_list(
303 side_field("dynamic", SIDE_TYPE_DYNAMIC),
304 )
305 );
306
307 static
308 void test_dynamic_struct(void)
309 {
310 side_arg_dynamic_define_struct(mystruct,
311 side_arg_list(
312 side_arg_dynamic_field("a", side_arg_dynamic_u32(43)),
313 side_arg_dynamic_field("b", side_arg_dynamic_string("zzz")),
314 side_arg_dynamic_field("c", side_arg_dynamic_null()),
315 )
316 );
317
318 my_provider_event_dynamic_struct.enabled = 1;
319 side_event(&my_provider_event_dynamic_struct,
320 side_arg_list(side_arg_dynamic(side_arg_dynamic_struct(&mystruct))));
321 }
322
323 static side_define_event(my_provider_event_dynamic_nested_struct,
324 "myprovider", "mydynamicnestedstruct", SIDE_LOGLEVEL_DEBUG,
325 side_field_list(
326 side_field("dynamic", SIDE_TYPE_DYNAMIC),
327 )
328 );
329
330 static
331 void test_dynamic_nested_struct(void)
332 {
333 side_arg_dynamic_define_struct(nested,
334 side_arg_list(
335 side_arg_dynamic_field("a", side_arg_dynamic_u32(43)),
336 side_arg_dynamic_field("b", side_arg_dynamic_u8(55)),
337 )
338 );
339 side_arg_dynamic_define_struct(nested2,
340 side_arg_list(
341 side_arg_dynamic_field("aa", side_arg_dynamic_u64(128)),
342 side_arg_dynamic_field("bb", side_arg_dynamic_u16(1)),
343 )
344 );
345 side_arg_dynamic_define_struct(mystruct,
346 side_arg_list(
347 side_arg_dynamic_field("nested", side_arg_dynamic_struct(&nested)),
348 side_arg_dynamic_field("nested2", side_arg_dynamic_struct(&nested2)),
349 )
350 );
351 my_provider_event_dynamic_nested_struct.enabled = 1;
352 side_event(&my_provider_event_dynamic_nested_struct,
353 side_arg_list(side_arg_dynamic(side_arg_dynamic_struct(&mystruct))));
354 }
355
356 static side_define_event(my_provider_event_dynamic_vla_struct,
357 "myprovider", "mydynamicvlastruct", SIDE_LOGLEVEL_DEBUG,
358 side_field_list(
359 side_field("dynamic", SIDE_TYPE_DYNAMIC),
360 )
361 );
362
363 static
364 void test_dynamic_vla_struct(void)
365 {
366 side_arg_dynamic_define_struct(nested,
367 side_arg_list(
368 side_arg_dynamic_field("a", side_arg_dynamic_u32(43)),
369 side_arg_dynamic_field("b", side_arg_dynamic_u8(55)),
370 )
371 );
372 side_arg_dynamic_define_vec(myvla,
373 side_arg_list(
374 side_arg_dynamic_struct(&nested),
375 side_arg_dynamic_struct(&nested),
376 side_arg_dynamic_struct(&nested),
377 side_arg_dynamic_struct(&nested),
378 )
379 );
380 my_provider_event_dynamic_vla_struct.enabled = 1;
381 side_event(&my_provider_event_dynamic_vla_struct,
382 side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla))));
383 }
384
385 static side_define_event(my_provider_event_dynamic_struct_vla,
386 "myprovider", "mydynamicstructvla", SIDE_LOGLEVEL_DEBUG,
387 side_field_list(
388 side_field("dynamic", SIDE_TYPE_DYNAMIC),
389 )
390 );
391
392 static
393 void test_dynamic_struct_vla(void)
394 {
395 side_arg_dynamic_define_vec(myvla,
396 side_arg_list(
397 side_arg_dynamic_u32(1), side_arg_dynamic_u32(2), side_arg_dynamic_u32(3),
398 )
399 );
400 side_arg_dynamic_define_vec(myvla2,
401 side_arg_list(
402 side_arg_dynamic_u32(4), side_arg_dynamic_u64(5), side_arg_dynamic_u32(6),
403 )
404 );
405 side_arg_dynamic_define_struct(mystruct,
406 side_arg_list(
407 side_arg_dynamic_field("a", side_arg_dynamic_vla(&myvla)),
408 side_arg_dynamic_field("b", side_arg_dynamic_vla(&myvla2)),
409 )
410 );
411 my_provider_event_dynamic_struct_vla.enabled = 1;
412 side_event(&my_provider_event_dynamic_struct_vla,
413 side_arg_list(side_arg_dynamic(side_arg_dynamic_struct(&mystruct))));
414 }
415
416 static side_define_event(my_provider_event_dynamic_nested_vla,
417 "myprovider", "mydynamicnestedvla", SIDE_LOGLEVEL_DEBUG,
418 side_field_list(
419 side_field("dynamic", SIDE_TYPE_DYNAMIC),
420 )
421 );
422
423 static
424 void test_dynamic_nested_vla(void)
425 {
426 side_arg_dynamic_define_vec(nestedvla,
427 side_arg_list(
428 side_arg_dynamic_u32(1), side_arg_dynamic_u16(2), side_arg_dynamic_u32(3),
429 )
430 );
431 side_arg_dynamic_define_vec(nestedvla2,
432 side_arg_list(
433 side_arg_dynamic_u8(4), side_arg_dynamic_u32(5), side_arg_dynamic_u32(6),
434 )
435 );
436 side_arg_dynamic_define_vec(myvla,
437 side_arg_list(
438 side_arg_dynamic_vla(&nestedvla),
439 side_arg_dynamic_vla(&nestedvla2),
440 )
441 );
442 my_provider_event_dynamic_nested_vla.enabled = 1;
443 side_event(&my_provider_event_dynamic_nested_vla,
444 side_arg_list(side_arg_dynamic(side_arg_dynamic_vla(&myvla))));
445 }
446
447 static side_define_event_variadic(my_provider_event_variadic,
448 "myprovider", "myvariadicevent", SIDE_LOGLEVEL_DEBUG,
449 side_field_list()
450 );
451
452 static
453 void test_variadic(void)
454 {
455 my_provider_event_variadic.enabled = 1;
456 side_event_variadic(&my_provider_event_variadic,
457 side_arg_list(),
458 side_arg_list(
459 side_arg_dynamic_field("a", side_arg_dynamic_u32(55)),
460 side_arg_dynamic_field("b", side_arg_dynamic_s8(-4)),
461 )
462 );
463 }
464
465 static side_define_event_variadic(my_provider_event_static_variadic,
466 "myprovider", "mystaticvariadicevent", SIDE_LOGLEVEL_DEBUG,
467 side_field_list(
468 side_field("abc", SIDE_TYPE_U32),
469 side_field("def", SIDE_TYPE_U16),
470 )
471 );
472
473 static
474 void test_static_variadic(void)
475 {
476 my_provider_event_static_variadic.enabled = 1;
477 side_event_variadic(&my_provider_event_static_variadic,
478 side_arg_list(
479 side_arg_u32(1),
480 side_arg_u16(2),
481 ),
482 side_arg_list(
483 side_arg_dynamic_field("a", side_arg_dynamic_u32(55)),
484 side_arg_dynamic_field("b", side_arg_dynamic_s8(-4)),
485 )
486 );
487 }
488
489 static side_define_event(my_provider_event_bool, "myprovider", "myeventbool", SIDE_LOGLEVEL_DEBUG,
490 side_field_list(
491 side_field("a_false", SIDE_TYPE_BOOL),
492 side_field("b_true", SIDE_TYPE_BOOL),
493 side_field("c_true", SIDE_TYPE_BOOL),
494 side_field("d_true", SIDE_TYPE_BOOL),
495 side_field("e_true", SIDE_TYPE_BOOL),
496 side_field("f_false", SIDE_TYPE_BOOL),
497 side_field("g_true", SIDE_TYPE_BOOL),
498 )
499 );
500
501 static
502 void test_bool(void)
503 {
504 uint32_t a = 0;
505 uint32_t b = 1;
506 uint64_t c = 0x12345678;
507 int16_t d = -32768;
508 bool e = true;
509 bool f = false;
510 uint32_t g = 256;
511
512 my_provider_event_bool.enabled = 1;
513 side_event(&my_provider_event_bool,
514 side_arg_list(
515 side_arg_bool(a),
516 side_arg_bool(b),
517 side_arg_bool(c),
518 side_arg_bool(d),
519 side_arg_bool(e),
520 side_arg_bool(f),
521 side_arg_bool(g),
522 )
523 );
524 }
525
526 static side_define_event_variadic(my_provider_event_dynamic_bool,
527 "myprovider", "mydynamicbool", SIDE_LOGLEVEL_DEBUG,
528 side_field_list()
529 );
530
531 static
532 void test_dynamic_bool(void)
533 {
534 my_provider_event_dynamic_bool.enabled = 1;
535 side_event_variadic(&my_provider_event_dynamic_bool,
536 side_arg_list(),
537 side_arg_list(
538 side_arg_dynamic_field("a_true", side_arg_dynamic_bool(55)),
539 side_arg_dynamic_field("b_true", side_arg_dynamic_bool(-4)),
540 side_arg_dynamic_field("c_false", side_arg_dynamic_bool(0)),
541 side_arg_dynamic_field("d_true", side_arg_dynamic_bool(256)),
542 )
543 );
544 }
545
546 int main()
547 {
548 test_fields();
549 test_struct();
550 test_array();
551 test_vla();
552 test_vla_visitor();
553 test_vla_visitor_2d();
554 test_array_fixint();
555 test_vla_fixint();
556 test_dynamic_basic_type();
557 test_dynamic_vla();
558 test_dynamic_null();
559 test_dynamic_struct();
560 test_dynamic_nested_struct();
561 test_dynamic_vla_struct();
562 test_dynamic_struct_vla();
563 test_dynamic_nested_vla();
564 test_variadic();
565 test_static_variadic();
566 test_bool();
567 test_dynamic_bool();
568 return 0;
569 }
This page took 0.075184 seconds and 4 git commands to generate.