Do not use `bool` type; use new `bt_bool` instead
[babeltrace.git] / tests / lib / test_ir_visit.c
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"
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>
28 #include <stdlib.h>
29 #include <stdbool.h>
30 #include <string.h>
31 #include <assert.h>
32
33 #define NR_TESTS 13
34
35 struct visitor_state {
36 int i;
37 };
38
39 struct expected_result {
40 const char *object_name;
41 enum bt_ctf_object_type object_type;
42 };
43
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 },
51 };
52
53 const char *object_type_str(enum bt_ctf_object_type type)
54 {
55 switch (type) {
56 case BT_CTF_OBJECT_TYPE_TRACE:
57 return "trace";
58 case BT_CTF_OBJECT_TYPE_STREAM_CLASS:
59 return "stream class";
60 case BT_CTF_OBJECT_TYPE_STREAM:
61 return "stream";
62 case BT_CTF_OBJECT_TYPE_EVENT_CLASS:
63 return "event class";
64 case BT_CTF_OBJECT_TYPE_EVENT:
65 return "event";
66 default:
67 return "unknown";
68 }
69 }
70
71 struct bt_ctf_event_class *init_event_class(const char *name)
72 {
73 int ret;
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);
77
78 if (!ec || !int_field) {
79 goto error;
80 }
81
82 ret = bt_ctf_event_class_add_field(ec, int_field, "an_int_field");
83 if (ret) {
84 goto error;
85 }
86
87 BT_PUT(int_field);
88 return ec;
89 error:
90 BT_PUT(ec);
91 BT_PUT(int_field);
92 return NULL;
93 }
94
95 struct bt_ctf_trace *init_trace(void)
96 {
97 int ret;
98 struct bt_ctf_trace *trace = bt_ctf_trace_create();
99 struct bt_ctf_stream_class *sc1 = bt_ctf_stream_class_create("sc1");
100 struct bt_ctf_stream_class *sc2 = bt_ctf_stream_class_create("sc2");
101 struct bt_ctf_event_class *ec1 = init_event_class("ec1");
102 struct bt_ctf_event_class *ec2 = init_event_class("ec2");
103 struct bt_ctf_event_class *ec3 = init_event_class("ec3");
104
105 if (!trace || !sc1 || !sc2 || !ec1 || !ec2 || !ec3) {
106 goto end;
107 }
108
109 ret = bt_ctf_trace_set_native_byte_order(trace,
110 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
111 assert(ret == 0);
112 ret = bt_ctf_stream_class_add_event_class(sc1, ec1);
113 if (ret) {
114 goto error;
115 }
116
117 ret = bt_ctf_stream_class_add_event_class(sc2, ec2);
118 if (ret) {
119 goto error;
120 }
121
122 ret = bt_ctf_stream_class_add_event_class(sc2, ec3);
123 if (ret) {
124 goto error;
125 }
126
127 ret = bt_ctf_trace_add_stream_class(trace, sc1);
128 if (ret) {
129 goto error;
130 }
131
132 ret = bt_ctf_trace_add_stream_class(trace, sc2);
133 if (ret) {
134 goto error;
135 }
136 end:
137 BT_PUT(sc1);
138 BT_PUT(sc2);
139 BT_PUT(ec1);
140 BT_PUT(ec2);
141 BT_PUT(ec3);
142 return trace;
143 error:
144 BT_PUT(trace);
145 goto end;
146 }
147
148 int visitor(struct bt_ctf_object *object, void *data)
149 {
150 int ret = 0;
151 bool names_match;
152 const char *object_name;
153 struct visitor_state *state = data;
154 struct expected_result *expected = &expected_results[state->i++];
155
156 switch (bt_ctf_object_get_type(object)) {
157 case BT_CTF_OBJECT_TYPE_TRACE:
158 object_name = NULL;
159 names_match = expected->object_name == NULL;
160 break;
161 case BT_CTF_OBJECT_TYPE_STREAM_CLASS:
162 object_name = bt_ctf_stream_class_get_name(
163 bt_ctf_object_get_object(object));
164 if (!object_name) {
165 ret = -1;
166 goto end;
167 }
168
169 names_match = !strcmp(object_name, expected->object_name);
170 break;
171 case BT_CTF_OBJECT_TYPE_EVENT_CLASS:
172 object_name = bt_ctf_event_class_get_name(
173 bt_ctf_object_get_object(object));
174 if (!object_name) {
175 ret = -1;
176 goto end;
177 }
178
179 names_match = !strcmp(object_name, expected->object_name);
180 break;
181 default:
182 diag("Encountered an unexpected type while visiting trace");
183 ret = -1;
184 goto end;
185 }
186
187 ok(expected->object_type == bt_ctf_object_get_type(object),
188 "Encoutered object type %s, expected %s",
189 object_type_str(expected->object_type),
190 object_type_str(bt_ctf_object_get_type(object)));
191 ok(names_match, "Element name is %s, expected %s",
192 object_name ? : "NULL",
193 expected->object_name ? : "NULL");
194 end:
195 return ret;
196 }
197
198 int main(int argc, char **argv)
199 {
200 int ret;
201 struct bt_ctf_trace *trace;
202 struct visitor_state state = { 0 };
203
204 plan_tests(NR_TESTS);
205
206 /*
207 * Initialize a reference trace which we'll walk using the
208 * bt_ctf_*_visit() interface.
209 */
210 trace = init_trace();
211 if (!trace) {
212 diag("Failed to initialize reference trace, aborting.");
213 exit(-1);
214 }
215
216 ret = bt_ctf_trace_visit(trace, visitor, &state);
217 ok(!ret, "bt_ctf_trace_visit returned success");
218
219 BT_PUT(trace);
220 return exit_status();
221 }
222
This page took 0.03312 seconds and 4 git commands to generate.