Add TODO
[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(
71002b5e 58 side_field_array("arr", side_elem(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(
71002b5e 75 side_field_vla("vla", side_elem(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
90struct app_visitor_ctx {
91 const uint32_t *ptr;
92 int init_pos;
93 int current_pos;
94 int end_pos;
95};
96
97enum side_visitor_status test_visitor_begin(void *_ctx)
98{
99 struct app_visitor_ctx *ctx = (struct app_visitor_ctx *) _ctx;
100 ctx->current_pos = ctx->init_pos;
101 return SIDE_VISITOR_STATUS_OK;
102}
103
104enum side_visitor_status test_visitor_end(void *_ctx)
105{
106 return SIDE_VISITOR_STATUS_OK;
107}
108
109enum side_visitor_status test_visitor_get_next(void *_ctx, struct side_arg_vec *sav_elem)
110{
111 struct app_visitor_ctx *ctx = (struct app_visitor_ctx *) _ctx;
112
113 if (ctx->current_pos >= ctx->end_pos)
114 return SIDE_VISITOR_STATUS_END;
115 sav_elem->type = SIDE_TYPE_U32;
116 sav_elem->u.side_u32 = ctx->ptr[ctx->current_pos++];
117 return SIDE_VISITOR_STATUS_OK;
118}
119
120static uint32_t testarray[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
121
122static side_define_event(my_provider_event_vla_visitor, "myprovider", "myvlavisit", SIDE_LOGLEVEL_DEBUG,
123 side_field_list(
71002b5e 124 side_field_vla_visitor("vlavisit", side_elem(SIDE_TYPE_U32),
f611d0c3
MD
125 test_visitor_begin, test_visitor_end, test_visitor_get_next),
126 side_field(SIDE_TYPE_S64, "v"),
127 )
128);
129
130static
131void test_vla_visitor(void)
132{
133 my_provider_event_vla_visitor.enabled = 1;
134 side_event_cond(&my_provider_event_vla_visitor) {
135 struct app_visitor_ctx ctx = {
136 .ptr = testarray,
137 .init_pos = 0,
138 .current_pos = 0,
139 .end_pos = SIDE_ARRAY_SIZE(testarray),
140 };
141 side_event_call(&my_provider_event_vla_visitor, side_arg_list(side_arg_vla_visitor(&ctx), side_arg_s64(42)));
142 }
143}
144
ba845af5
MD
145static int64_t array_fixint[] = { -444, 555, 123, 2897432587 };
146
147static side_define_event(my_provider_event_array_fixint, "myprovider", "myarrayfixint", SIDE_LOGLEVEL_DEBUG,
148 side_field_list(
149 side_field_array("arrfixint", side_elem(SIDE_TYPE_S64), SIDE_ARRAY_SIZE(array_fixint)),
150 side_field(SIDE_TYPE_S64, "v"),
151 )
152);
153
154static
155void test_array_fixint(void)
156{
157 my_provider_event_array_fixint.enabled = 1;
1533629f
MD
158 side_event(&my_provider_event_array_fixint,
159 side_arg_list(side_arg_array_s64(array_fixint), side_arg_s64(42)));
160}
161
162static int64_t vla_fixint[] = { -444, 555, 123, 2897432587 };
163
164static side_define_event(my_provider_event_vla_fixint, "myprovider", "myvlafixint", SIDE_LOGLEVEL_DEBUG,
165 side_field_list(
166 side_field_vla("vlafixint", side_elem(SIDE_TYPE_S64)),
167 side_field(SIDE_TYPE_S64, "v"),
168 )
169);
170
171static
172void test_vla_fixint(void)
173{
174 my_provider_event_vla_fixint.enabled = 1;
175 side_event(&my_provider_event_vla_fixint,
176 side_arg_list(side_arg_vla_s64(vla_fixint, SIDE_ARRAY_SIZE(vla_fixint)), side_arg_s64(42)));
ba845af5
MD
177}
178
f611d0c3
MD
179int main()
180{
181 test_fields();
182 test_struct();
183 test_array();
184 test_vla();
185 test_vla_visitor();
ba845af5 186 test_array_fixint();
1533629f 187 test_vla_fixint();
f611d0c3
MD
188 return 0;
189}
This page took 0.030309 seconds and 4 git commands to generate.