Commit | Line | Data |
---|---|---|
07926183 JG |
1 | /* |
2 | * test_ir_visit.c | |
3 | * | |
4 | * CTF IR visitor interface test | |
5 | * | |
6 | * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; under version 2 of the License. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 | */ | |
21 | ||
22 | #include "tap/tap.h" | |
9d408fca | 23 | #include <babeltrace/ref.h> |
07926183 JG |
24 | #include <babeltrace/ctf-ir/event-class.h> |
25 | #include <babeltrace/ctf-ir/field-types.h> | |
26 | #include <babeltrace/ctf-ir/stream-class.h> | |
27 | #include <babeltrace/ctf-ir/trace.h> | |
28 | #include <babeltrace/ctf-ir/visitor.h> | |
29 | #include <stdlib.h> | |
c55a9f58 | 30 | #include <stdbool.h> |
07926183 | 31 | #include <string.h> |
d490fcbe | 32 | #include <assert.h> |
07926183 JG |
33 | |
34 | #define NR_TESTS 13 | |
35 | ||
36 | struct visitor_state { | |
37 | int i; | |
38 | }; | |
39 | ||
40 | struct expected_result { | |
d9a13d86 PP |
41 | const char *object_name; |
42 | enum bt_ctf_object_type object_type; | |
07926183 JG |
43 | }; |
44 | ||
45 | struct expected_result expected_results[] = { | |
d9a13d86 PP |
46 | { NULL, BT_CTF_OBJECT_TYPE_TRACE }, |
47 | { "sc1", BT_CTF_OBJECT_TYPE_STREAM_CLASS }, | |
48 | { "ec1", BT_CTF_OBJECT_TYPE_EVENT_CLASS }, | |
49 | { "sc2", BT_CTF_OBJECT_TYPE_STREAM_CLASS }, | |
50 | { "ec2", BT_CTF_OBJECT_TYPE_EVENT_CLASS }, | |
51 | { "ec3", BT_CTF_OBJECT_TYPE_EVENT_CLASS }, | |
07926183 JG |
52 | }; |
53 | ||
d9a13d86 | 54 | const char *object_type_str(enum bt_ctf_object_type type) |
07926183 JG |
55 | { |
56 | switch (type) { | |
d9a13d86 | 57 | case BT_CTF_OBJECT_TYPE_TRACE: |
07926183 | 58 | return "trace"; |
d9a13d86 | 59 | case BT_CTF_OBJECT_TYPE_STREAM_CLASS: |
07926183 | 60 | return "stream class"; |
d9a13d86 | 61 | case BT_CTF_OBJECT_TYPE_STREAM: |
07926183 | 62 | return "stream"; |
d9a13d86 | 63 | case BT_CTF_OBJECT_TYPE_EVENT_CLASS: |
07926183 | 64 | return "event class"; |
d9a13d86 | 65 | case BT_CTF_OBJECT_TYPE_EVENT: |
07926183 JG |
66 | return "event"; |
67 | default: | |
68 | return "unknown"; | |
69 | } | |
70 | } | |
71 | ||
72 | struct bt_ctf_event_class *init_event_class(const char *name) | |
73 | { | |
74 | int ret; | |
75 | struct bt_ctf_event_class *ec = bt_ctf_event_class_create(name); | |
76 | struct bt_ctf_field_type *int_field = | |
77 | bt_ctf_field_type_integer_create(8); | |
78 | ||
79 | if (!ec || !int_field) { | |
80 | goto error; | |
81 | } | |
82 | ||
83 | ret = bt_ctf_event_class_add_field(ec, int_field, "an_int_field"); | |
84 | if (ret) { | |
85 | goto error; | |
86 | } | |
87 | ||
88 | BT_PUT(int_field); | |
89 | return ec; | |
90 | error: | |
91 | BT_PUT(ec); | |
92 | BT_PUT(int_field); | |
93 | return NULL; | |
94 | } | |
95 | ||
c2aff3ff PP |
96 | static void set_stream_class_field_types( |
97 | struct bt_ctf_stream_class *stream_class) | |
98 | { | |
99 | struct bt_ctf_field_type *packet_context_type; | |
100 | struct bt_ctf_field_type *event_header_type; | |
101 | struct bt_ctf_field_type *ft; | |
102 | int ret; | |
103 | ||
104 | packet_context_type = bt_ctf_field_type_structure_create(); | |
105 | assert(packet_context_type); | |
106 | ft = bt_ctf_field_type_integer_create(32); | |
107 | assert(ft); | |
108 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
109 | ft, "packet_size"); | |
110 | assert(ret == 0); | |
111 | bt_put(ft); | |
112 | ft = bt_ctf_field_type_integer_create(32); | |
113 | assert(ft); | |
114 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
115 | ft, "content_size"); | |
116 | assert(ret == 0); | |
117 | bt_put(ft); | |
118 | ||
119 | event_header_type = bt_ctf_field_type_structure_create(); | |
120 | assert(event_header_type); | |
121 | ft = bt_ctf_field_type_integer_create(32); | |
122 | assert(ft); | |
123 | ret = bt_ctf_field_type_structure_add_field(event_header_type, | |
124 | ft, "id"); | |
125 | assert(ret == 0); | |
126 | bt_put(ft); | |
127 | ||
128 | ret = bt_ctf_stream_class_set_packet_context_type(stream_class, | |
129 | packet_context_type); | |
130 | assert(ret == 0); | |
131 | ret = bt_ctf_stream_class_set_event_header_type(stream_class, | |
132 | event_header_type); | |
133 | assert(ret == 0); | |
134 | ||
135 | bt_put(packet_context_type); | |
136 | bt_put(event_header_type); | |
137 | } | |
138 | ||
139 | static void set_trace_packet_header(struct bt_ctf_trace *trace) | |
140 | { | |
141 | struct bt_ctf_field_type *packet_header_type; | |
142 | struct bt_ctf_field_type *ft; | |
143 | int ret; | |
144 | ||
145 | packet_header_type = bt_ctf_field_type_structure_create(); | |
146 | assert(packet_header_type); | |
147 | ft = bt_ctf_field_type_integer_create(32); | |
148 | assert(ft); | |
149 | ret = bt_ctf_field_type_structure_add_field(packet_header_type, | |
150 | ft, "stream_id"); | |
151 | assert(ret == 0); | |
152 | bt_put(ft); | |
153 | ||
154 | ret = bt_ctf_trace_set_packet_header_type(trace, | |
155 | packet_header_type); | |
156 | assert(ret == 0); | |
157 | ||
158 | bt_put(packet_header_type); | |
159 | } | |
160 | ||
07926183 JG |
161 | struct bt_ctf_trace *init_trace(void) |
162 | { | |
163 | int ret; | |
164 | struct bt_ctf_trace *trace = bt_ctf_trace_create(); | |
165 | struct bt_ctf_stream_class *sc1 = bt_ctf_stream_class_create("sc1"); | |
166 | struct bt_ctf_stream_class *sc2 = bt_ctf_stream_class_create("sc2"); | |
167 | struct bt_ctf_event_class *ec1 = init_event_class("ec1"); | |
168 | struct bt_ctf_event_class *ec2 = init_event_class("ec2"); | |
169 | struct bt_ctf_event_class *ec3 = init_event_class("ec3"); | |
170 | ||
171 | if (!trace || !sc1 || !sc2 || !ec1 || !ec2 || !ec3) { | |
172 | goto end; | |
173 | } | |
174 | ||
c2aff3ff PP |
175 | set_trace_packet_header(trace); |
176 | set_stream_class_field_types(sc1); | |
177 | set_stream_class_field_types(sc2); | |
07926183 JG |
178 | ret = bt_ctf_stream_class_add_event_class(sc1, ec1); |
179 | if (ret) { | |
180 | goto error; | |
181 | } | |
182 | ||
183 | ret = bt_ctf_stream_class_add_event_class(sc2, ec2); | |
184 | if (ret) { | |
185 | goto error; | |
186 | } | |
187 | ||
188 | ret = bt_ctf_stream_class_add_event_class(sc2, ec3); | |
189 | if (ret) { | |
190 | goto error; | |
191 | } | |
192 | ||
193 | ret = bt_ctf_trace_add_stream_class(trace, sc1); | |
194 | if (ret) { | |
195 | goto error; | |
196 | } | |
197 | ||
198 | ret = bt_ctf_trace_add_stream_class(trace, sc2); | |
199 | if (ret) { | |
200 | goto error; | |
201 | } | |
202 | end: | |
203 | BT_PUT(sc1); | |
204 | BT_PUT(sc2); | |
205 | BT_PUT(ec1); | |
206 | BT_PUT(ec2); | |
207 | BT_PUT(ec3); | |
208 | return trace; | |
209 | error: | |
210 | BT_PUT(trace); | |
211 | goto end; | |
212 | } | |
213 | ||
d9a13d86 | 214 | int visitor(struct bt_ctf_object *object, void *data) |
07926183 JG |
215 | { |
216 | int ret = 0; | |
217 | bool names_match; | |
d9a13d86 | 218 | const char *object_name; |
07926183 JG |
219 | struct visitor_state *state = data; |
220 | struct expected_result *expected = &expected_results[state->i++]; | |
221 | ||
d9a13d86 PP |
222 | switch (bt_ctf_object_get_type(object)) { |
223 | case BT_CTF_OBJECT_TYPE_TRACE: | |
224 | object_name = NULL; | |
225 | names_match = expected->object_name == NULL; | |
07926183 | 226 | break; |
d9a13d86 PP |
227 | case BT_CTF_OBJECT_TYPE_STREAM_CLASS: |
228 | object_name = bt_ctf_stream_class_get_name( | |
229 | bt_ctf_object_get_object(object)); | |
230 | if (!object_name) { | |
07926183 JG |
231 | ret = -1; |
232 | goto end; | |
233 | } | |
234 | ||
d9a13d86 | 235 | names_match = !strcmp(object_name, expected->object_name); |
07926183 | 236 | break; |
d9a13d86 PP |
237 | case BT_CTF_OBJECT_TYPE_EVENT_CLASS: |
238 | object_name = bt_ctf_event_class_get_name( | |
239 | bt_ctf_object_get_object(object)); | |
240 | if (!object_name) { | |
07926183 JG |
241 | ret = -1; |
242 | goto end; | |
243 | } | |
244 | ||
d9a13d86 | 245 | names_match = !strcmp(object_name, expected->object_name); |
07926183 JG |
246 | break; |
247 | default: | |
248 | diag("Encountered an unexpected type while visiting trace"); | |
249 | ret = -1; | |
250 | goto end; | |
251 | } | |
252 | ||
d9a13d86 PP |
253 | ok(expected->object_type == bt_ctf_object_get_type(object), |
254 | "Encoutered object type %s, expected %s", | |
255 | object_type_str(expected->object_type), | |
256 | object_type_str(bt_ctf_object_get_type(object))); | |
07926183 | 257 | ok(names_match, "Element name is %s, expected %s", |
d9a13d86 PP |
258 | object_name ? : "NULL", |
259 | expected->object_name ? : "NULL"); | |
07926183 JG |
260 | end: |
261 | return ret; | |
262 | } | |
263 | ||
264 | int main(int argc, char **argv) | |
265 | { | |
266 | int ret; | |
267 | struct bt_ctf_trace *trace; | |
268 | struct visitor_state state = { 0 }; | |
269 | ||
270 | plan_tests(NR_TESTS); | |
271 | ||
272 | /* | |
273 | * Initialize a reference trace which we'll walk using the | |
274 | * bt_ctf_*_visit() interface. | |
275 | */ | |
276 | trace = init_trace(); | |
277 | if (!trace) { | |
278 | diag("Failed to initialize reference trace, aborting."); | |
279 | exit(-1); | |
280 | } | |
281 | ||
282 | ret = bt_ctf_trace_visit(trace, visitor, &state); | |
283 | ok(!ret, "bt_ctf_trace_visit returned success"); | |
284 | ||
285 | BT_PUT(trace); | |
286 | return exit_status(); | |
287 | } | |
288 |