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