4 * CTF IR visitor interface test
6 * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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.
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.
23 #include <babeltrace/ctf-ir/event-class.h>
24 #include <babeltrace/ctf-ir/field-types.h>
25 #include <babeltrace/ctf-ir/stream-class.h>
26 #include <babeltrace/ctf-ir/trace.h>
27 #include <babeltrace/ctf-ir/visitor.h>
35 struct visitor_state
{
39 struct expected_result
{
40 const char *object_name
;
41 enum bt_ctf_object_type object_type
;
44 struct expected_result expected_results
[] = {
45 { NULL
, BT_CTF_OBJECT_TYPE_TRACE
},
46 { "sc1", BT_CTF_OBJECT_TYPE_STREAM_CLASS
},
47 { "ec1", BT_CTF_OBJECT_TYPE_EVENT_CLASS
},
48 { "sc2", BT_CTF_OBJECT_TYPE_STREAM_CLASS
},
49 { "ec2", BT_CTF_OBJECT_TYPE_EVENT_CLASS
},
50 { "ec3", BT_CTF_OBJECT_TYPE_EVENT_CLASS
},
53 const char *object_type_str(enum bt_ctf_object_type type
)
56 case BT_CTF_OBJECT_TYPE_TRACE
:
58 case BT_CTF_OBJECT_TYPE_STREAM_CLASS
:
59 return "stream class";
60 case BT_CTF_OBJECT_TYPE_STREAM
:
62 case BT_CTF_OBJECT_TYPE_EVENT_CLASS
:
64 case BT_CTF_OBJECT_TYPE_EVENT
:
71 struct bt_ctf_event_class
*init_event_class(const char *name
)
74 struct bt_ctf_event_class
*ec
= bt_ctf_event_class_create(name
);
75 struct bt_ctf_field_type
*int_field
=
76 bt_ctf_field_type_integer_create(8);
78 if (!ec
|| !int_field
) {
82 ret
= bt_ctf_event_class_add_field(ec
, int_field
, "an_int_field");
95 static void set_stream_class_field_types(
96 struct bt_ctf_stream_class
*stream_class
)
98 struct bt_ctf_field_type
*packet_context_type
;
99 struct bt_ctf_field_type
*event_header_type
;
100 struct bt_ctf_field_type
*ft
;
103 packet_context_type
= bt_ctf_field_type_structure_create();
104 assert(packet_context_type
);
105 ft
= bt_ctf_field_type_integer_create(32);
107 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
111 ft
= bt_ctf_field_type_integer_create(32);
113 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
118 event_header_type
= bt_ctf_field_type_structure_create();
119 assert(event_header_type
);
120 ft
= bt_ctf_field_type_integer_create(32);
122 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
127 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
,
128 packet_context_type
);
130 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
,
134 bt_put(packet_context_type
);
135 bt_put(event_header_type
);
138 static void set_trace_packet_header(struct bt_ctf_trace
*trace
)
140 struct bt_ctf_field_type
*packet_header_type
;
141 struct bt_ctf_field_type
*ft
;
144 packet_header_type
= bt_ctf_field_type_structure_create();
145 assert(packet_header_type
);
146 ft
= bt_ctf_field_type_integer_create(32);
148 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
153 ret
= bt_ctf_trace_set_packet_header_type(trace
,
157 bt_put(packet_header_type
);
160 struct bt_ctf_trace
*init_trace(void)
163 struct bt_ctf_trace
*trace
= bt_ctf_trace_create();
164 struct bt_ctf_stream_class
*sc1
= bt_ctf_stream_class_create("sc1");
165 struct bt_ctf_stream_class
*sc2
= bt_ctf_stream_class_create("sc2");
166 struct bt_ctf_event_class
*ec1
= init_event_class("ec1");
167 struct bt_ctf_event_class
*ec2
= init_event_class("ec2");
168 struct bt_ctf_event_class
*ec3
= init_event_class("ec3");
170 if (!trace
|| !sc1
|| !sc2
|| !ec1
|| !ec2
|| !ec3
) {
174 set_trace_packet_header(trace
);
175 set_stream_class_field_types(sc1
);
176 set_stream_class_field_types(sc2
);
177 ret
= bt_ctf_trace_set_native_byte_order(trace
,
178 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
180 ret
= bt_ctf_stream_class_add_event_class(sc1
, ec1
);
185 ret
= bt_ctf_stream_class_add_event_class(sc2
, ec2
);
190 ret
= bt_ctf_stream_class_add_event_class(sc2
, ec3
);
195 ret
= bt_ctf_trace_add_stream_class(trace
, sc1
);
200 ret
= bt_ctf_trace_add_stream_class(trace
, sc2
);
216 int visitor(struct bt_ctf_object
*object
, void *data
)
220 const char *object_name
;
221 struct visitor_state
*state
= data
;
222 struct expected_result
*expected
= &expected_results
[state
->i
++];
224 switch (bt_ctf_object_get_type(object
)) {
225 case BT_CTF_OBJECT_TYPE_TRACE
:
227 names_match
= expected
->object_name
== NULL
;
229 case BT_CTF_OBJECT_TYPE_STREAM_CLASS
:
230 object_name
= bt_ctf_stream_class_get_name(
231 bt_ctf_object_get_object(object
));
237 names_match
= !strcmp(object_name
, expected
->object_name
);
239 case BT_CTF_OBJECT_TYPE_EVENT_CLASS
:
240 object_name
= bt_ctf_event_class_get_name(
241 bt_ctf_object_get_object(object
));
247 names_match
= !strcmp(object_name
, expected
->object_name
);
250 diag("Encountered an unexpected type while visiting trace");
255 ok(expected
->object_type
== bt_ctf_object_get_type(object
),
256 "Encoutered object type %s, expected %s",
257 object_type_str(expected
->object_type
),
258 object_type_str(bt_ctf_object_get_type(object
)));
259 ok(names_match
, "Element name is %s, expected %s",
260 object_name
? : "NULL",
261 expected
->object_name
? : "NULL");
266 int main(int argc
, char **argv
)
269 struct bt_ctf_trace
*trace
;
270 struct visitor_state state
= { 0 };
272 plan_tests(NR_TESTS
);
275 * Initialize a reference trace which we'll walk using the
276 * bt_ctf_*_visit() interface.
278 trace
= init_trace();
280 diag("Failed to initialize reference trace, aborting.");
284 ret
= bt_ctf_trace_visit(trace
, visitor
, &state
);
285 ok(!ret
, "bt_ctf_trace_visit returned success");
288 return exit_status();