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>
33 struct visitor_state
{
37 struct expected_result
{
38 const char *object_name
;
39 enum bt_ctf_object_type object_type
;
42 struct expected_result expected_results
[] = {
43 { NULL
, BT_CTF_OBJECT_TYPE_TRACE
},
44 { "sc1", BT_CTF_OBJECT_TYPE_STREAM_CLASS
},
45 { "ec1", BT_CTF_OBJECT_TYPE_EVENT_CLASS
},
46 { "sc2", BT_CTF_OBJECT_TYPE_STREAM_CLASS
},
47 { "ec2", BT_CTF_OBJECT_TYPE_EVENT_CLASS
},
48 { "ec3", BT_CTF_OBJECT_TYPE_EVENT_CLASS
},
51 const char *object_type_str(enum bt_ctf_object_type type
)
54 case BT_CTF_OBJECT_TYPE_TRACE
:
56 case BT_CTF_OBJECT_TYPE_STREAM_CLASS
:
57 return "stream class";
58 case BT_CTF_OBJECT_TYPE_STREAM
:
60 case BT_CTF_OBJECT_TYPE_EVENT_CLASS
:
62 case BT_CTF_OBJECT_TYPE_EVENT
:
69 struct bt_ctf_event_class
*init_event_class(const char *name
)
72 struct bt_ctf_event_class
*ec
= bt_ctf_event_class_create(name
);
73 struct bt_ctf_field_type
*int_field
=
74 bt_ctf_field_type_integer_create(8);
76 if (!ec
|| !int_field
) {
80 ret
= bt_ctf_event_class_add_field(ec
, int_field
, "an_int_field");
93 struct bt_ctf_trace
*init_trace(void)
96 struct bt_ctf_trace
*trace
= bt_ctf_trace_create();
97 struct bt_ctf_stream_class
*sc1
= bt_ctf_stream_class_create("sc1");
98 struct bt_ctf_stream_class
*sc2
= bt_ctf_stream_class_create("sc2");
99 struct bt_ctf_event_class
*ec1
= init_event_class("ec1");
100 struct bt_ctf_event_class
*ec2
= init_event_class("ec2");
101 struct bt_ctf_event_class
*ec3
= init_event_class("ec3");
103 if (!trace
|| !sc1
|| !sc2
|| !ec1
|| !ec2
|| !ec3
) {
107 ret
= bt_ctf_stream_class_add_event_class(sc1
, ec1
);
112 ret
= bt_ctf_stream_class_add_event_class(sc2
, ec2
);
117 ret
= bt_ctf_stream_class_add_event_class(sc2
, ec3
);
122 ret
= bt_ctf_trace_add_stream_class(trace
, sc1
);
127 ret
= bt_ctf_trace_add_stream_class(trace
, sc2
);
143 int visitor(struct bt_ctf_object
*object
, void *data
)
147 const char *object_name
;
148 struct visitor_state
*state
= data
;
149 struct expected_result
*expected
= &expected_results
[state
->i
++];
151 switch (bt_ctf_object_get_type(object
)) {
152 case BT_CTF_OBJECT_TYPE_TRACE
:
154 names_match
= expected
->object_name
== NULL
;
156 case BT_CTF_OBJECT_TYPE_STREAM_CLASS
:
157 object_name
= bt_ctf_stream_class_get_name(
158 bt_ctf_object_get_object(object
));
164 names_match
= !strcmp(object_name
, expected
->object_name
);
166 case BT_CTF_OBJECT_TYPE_EVENT_CLASS
:
167 object_name
= bt_ctf_event_class_get_name(
168 bt_ctf_object_get_object(object
));
174 names_match
= !strcmp(object_name
, expected
->object_name
);
177 diag("Encountered an unexpected type while visiting trace");
182 ok(expected
->object_type
== bt_ctf_object_get_type(object
),
183 "Encoutered object type %s, expected %s",
184 object_type_str(expected
->object_type
),
185 object_type_str(bt_ctf_object_get_type(object
)));
186 ok(names_match
, "Element name is %s, expected %s",
187 object_name
? : "NULL",
188 expected
->object_name
? : "NULL");
193 int main(int argc
, char **argv
)
196 struct bt_ctf_trace
*trace
;
197 struct visitor_state state
= { 0 };
199 plan_tests(NR_TESTS
);
202 * Initialize a reference trace which we'll walk using the
203 * bt_ctf_*_visit() interface.
205 trace
= init_trace();
207 diag("Failed to initialize reference trace, aborting.");
211 ret
= bt_ctf_trace_visit(trace
, visitor
, &state
);
212 ok(!ret
, "bt_ctf_trace_visit returned success");
215 return exit_status();