4 * CTF IR trace listener 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/trace-internal.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
},
49 /* Elements added after the initial add_listener call. */
50 { "sc3", BT_CTF_OBJECT_TYPE_STREAM_CLASS
},
51 { "ec4", BT_CTF_OBJECT_TYPE_EVENT_CLASS
},
52 { "ec5", BT_CTF_OBJECT_TYPE_EVENT_CLASS
},
55 const char *object_type_str(enum bt_ctf_object_type type
)
58 case BT_CTF_OBJECT_TYPE_TRACE
:
60 case BT_CTF_OBJECT_TYPE_STREAM_CLASS
:
61 return "stream class";
62 case BT_CTF_OBJECT_TYPE_STREAM
:
64 case BT_CTF_OBJECT_TYPE_EVENT_CLASS
:
66 case BT_CTF_OBJECT_TYPE_EVENT
:
73 struct bt_ctf_event_class
*init_event_class(const char *name
)
76 struct bt_ctf_event_class
*ec
= bt_ctf_event_class_create(name
);
77 struct bt_ctf_field_type
*int_field
=
78 bt_ctf_field_type_integer_create(8);
80 if (!ec
|| !int_field
) {
84 ret
= bt_ctf_event_class_add_field(ec
, int_field
, "an_int_field");
97 struct bt_ctf_trace
*init_trace(void)
100 struct bt_ctf_trace
*trace
= bt_ctf_trace_create();
101 struct bt_ctf_stream_class
*sc1
= bt_ctf_stream_class_create("sc1");
102 struct bt_ctf_stream_class
*sc2
= bt_ctf_stream_class_create("sc2");
103 struct bt_ctf_event_class
*ec1
= init_event_class("ec1");
104 struct bt_ctf_event_class
*ec2
= init_event_class("ec2");
105 struct bt_ctf_event_class
*ec3
= init_event_class("ec3");
107 if (!trace
|| !sc1
|| !sc2
|| !ec1
|| !ec2
|| !ec3
) {
111 ret
= bt_ctf_stream_class_add_event_class(sc1
, ec1
);
116 ret
= bt_ctf_stream_class_add_event_class(sc2
, ec2
);
121 ret
= bt_ctf_stream_class_add_event_class(sc2
, ec3
);
126 ret
= bt_ctf_trace_add_stream_class(trace
, sc1
);
131 ret
= bt_ctf_trace_add_stream_class(trace
, sc2
);
147 void visitor(struct bt_ctf_object
*object
, void *data
)
150 const char *object_name
;
151 struct visitor_state
*state
= data
;
152 struct expected_result
*expected
= &expected_results
[state
->i
++];
154 switch (bt_ctf_object_get_type(object
)) {
155 case BT_CTF_OBJECT_TYPE_TRACE
:
157 names_match
= expected
->object_name
== NULL
;
159 case BT_CTF_OBJECT_TYPE_STREAM_CLASS
:
160 object_name
= bt_ctf_stream_class_get_name(
161 bt_ctf_object_get_object(object
));
166 names_match
= !strcmp(object_name
, expected
->object_name
);
168 case BT_CTF_OBJECT_TYPE_EVENT_CLASS
:
169 object_name
= bt_ctf_event_class_get_name(
170 bt_ctf_object_get_object(object
));
175 names_match
= !strcmp(object_name
, expected
->object_name
);
178 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");
191 int main(int argc
, char **argv
)
194 struct bt_ctf_trace
*trace
;
195 struct visitor_state state
= { 0 };
196 struct bt_ctf_stream_class
*sc3
;
197 struct bt_ctf_event_class
*ec4
, *ec5
;
199 plan_tests(NR_TESTS
);
201 trace
= init_trace();
203 diag("Failed to initialize reference trace, aborting.");
207 ret
= bt_ctf_trace_add_listener(trace
, visitor
, &state
);
208 ok(!ret
, "bt_ctf_trace_add_listener returned success");
211 * Validate that listeners are notified when new objects are added to a
214 sc3
= bt_ctf_stream_class_create("sc3");
216 diag("Failed to create stream class, aborting.");
220 ec4
= init_event_class("ec4");
221 ec5
= init_event_class("ec5");
223 diag("Failed to create event classes, aborting.");
227 ret
= bt_ctf_stream_class_add_event_class(sc3
, ec4
);
229 diag("Failed to add event class to stream class, aborting.");
233 ret
= bt_ctf_trace_add_stream_class(trace
, sc3
);
235 diag("Failed to add stream class sc3 to trace, aborting.");
239 /* Listener should have been invoked two times (sc3 + ec4). */
240 ok(index
+ 2 == state
.i
, "trace modification listener has been invoked twice after addition of a stream class");
243 ret
= bt_ctf_stream_class_add_event_class(sc3
, ec5
);
245 diag("Failed to add event class to stream class, aborting.");
249 ok(index
+ 1 == state
.i
, "trace modification has been invoked once after addition of an event class");
255 return exit_status();