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/ref.h>
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>
36 struct visitor_state
{
40 struct expected_result
{
41 const char *object_name
;
42 enum bt_ctf_object_type object_type
;
45 struct expected_result expected_results
[] = {
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
},
54 const char *object_type_str(enum bt_ctf_object_type type
)
57 case BT_CTF_OBJECT_TYPE_TRACE
:
59 case BT_CTF_OBJECT_TYPE_STREAM_CLASS
:
60 return "stream class";
61 case BT_CTF_OBJECT_TYPE_STREAM
:
63 case BT_CTF_OBJECT_TYPE_EVENT_CLASS
:
65 case BT_CTF_OBJECT_TYPE_EVENT
:
72 struct bt_ctf_event_class
*init_event_class(const char *name
)
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);
79 if (!ec
|| !int_field
) {
83 ret
= bt_ctf_event_class_add_field(ec
, int_field
, "an_int_field");
96 static void set_stream_class_field_types(
97 struct bt_ctf_stream_class
*stream_class
)
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
;
104 packet_context_type
= bt_ctf_field_type_structure_create();
105 assert(packet_context_type
);
106 ft
= bt_ctf_field_type_integer_create(32);
108 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
112 ft
= bt_ctf_field_type_integer_create(32);
114 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
119 event_header_type
= bt_ctf_field_type_structure_create();
120 assert(event_header_type
);
121 ft
= bt_ctf_field_type_integer_create(32);
123 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
128 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
,
129 packet_context_type
);
131 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
,
135 bt_put(packet_context_type
);
136 bt_put(event_header_type
);
139 static void set_trace_packet_header(struct bt_ctf_trace
*trace
)
141 struct bt_ctf_field_type
*packet_header_type
;
142 struct bt_ctf_field_type
*ft
;
145 packet_header_type
= bt_ctf_field_type_structure_create();
146 assert(packet_header_type
);
147 ft
= bt_ctf_field_type_integer_create(32);
149 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
154 ret
= bt_ctf_trace_set_packet_header_type(trace
,
158 bt_put(packet_header_type
);
161 struct bt_ctf_trace
*init_trace(void)
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");
171 if (!trace
|| !sc1
|| !sc2
|| !ec1
|| !ec2
|| !ec3
) {
175 set_trace_packet_header(trace
);
176 set_stream_class_field_types(sc1
);
177 set_stream_class_field_types(sc2
);
178 ret
= bt_ctf_stream_class_add_event_class(sc1
, ec1
);
183 ret
= bt_ctf_stream_class_add_event_class(sc2
, ec2
);
188 ret
= bt_ctf_stream_class_add_event_class(sc2
, ec3
);
193 ret
= bt_ctf_trace_add_stream_class(trace
, sc1
);
198 ret
= bt_ctf_trace_add_stream_class(trace
, sc2
);
214 int visitor(struct bt_ctf_object
*object
, void *data
)
218 const char *object_name
;
219 struct visitor_state
*state
= data
;
220 struct expected_result
*expected
= &expected_results
[state
->i
++];
222 switch (bt_ctf_object_get_type(object
)) {
223 case BT_CTF_OBJECT_TYPE_TRACE
:
225 names_match
= expected
->object_name
== NULL
;
227 case BT_CTF_OBJECT_TYPE_STREAM_CLASS
:
228 object_name
= bt_ctf_stream_class_get_name(
229 bt_ctf_object_get_object(object
));
235 names_match
= !strcmp(object_name
, expected
->object_name
);
237 case BT_CTF_OBJECT_TYPE_EVENT_CLASS
:
238 object_name
= bt_ctf_event_class_get_name(
239 bt_ctf_object_get_object(object
));
245 names_match
= !strcmp(object_name
, expected
->object_name
);
248 diag("Encountered an unexpected type while visiting trace");
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
)));
257 ok(names_match
, "Element name is %s, expected %s",
258 object_name
? : "NULL",
259 expected
->object_name
? : "NULL");
264 int main(int argc
, char **argv
)
267 struct bt_ctf_trace
*trace
;
268 struct visitor_state state
= { 0 };
270 plan_tests(NR_TESTS
);
273 * Initialize a reference trace which we'll walk using the
274 * bt_ctf_*_visit() interface.
276 trace
= init_trace();
278 diag("Failed to initialize reference trace, aborting.");
282 ret
= bt_ctf_trace_visit(trace
, visitor
, &state
);
283 ok(!ret
, "bt_ctf_trace_visit returned success");
286 return exit_status();