| 1 | /* |
| 2 | * test_ir_visit.c |
| 3 | * |
| 4 | * Trace 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/ref.h> |
| 24 | #include <babeltrace/trace-ir/event-class.h> |
| 25 | #include <babeltrace/trace-ir/field-types.h> |
| 26 | #include <babeltrace/trace-ir/stream-class.h> |
| 27 | #include <babeltrace/trace-ir/trace.h> |
| 28 | #include <babeltrace/trace-ir/visitor.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdbool.h> |
| 31 | #include <string.h> |
| 32 | #include <babeltrace/assert-internal.h> |
| 33 | |
| 34 | #define NR_TESTS 13 |
| 35 | |
| 36 | struct visitor_state { |
| 37 | int i; |
| 38 | }; |
| 39 | |
| 40 | struct expected_result { |
| 41 | const char *object_name; |
| 42 | enum bt_visitor_object_type object_type; |
| 43 | }; |
| 44 | |
| 45 | struct expected_result expected_results[] = { |
| 46 | { NULL, BT_VISITOR_OBJECT_TYPE_TRACE }, |
| 47 | { "sc1", BT_VISITOR_OBJECT_TYPE_STREAM_CLASS }, |
| 48 | { "ec1", BT_VISITOR_OBJECT_TYPE_EVENT_CLASS }, |
| 49 | { "sc2", BT_VISITOR_OBJECT_TYPE_STREAM_CLASS }, |
| 50 | { "ec2", BT_VISITOR_OBJECT_TYPE_EVENT_CLASS }, |
| 51 | { "ec3", BT_VISITOR_OBJECT_TYPE_EVENT_CLASS }, |
| 52 | }; |
| 53 | |
| 54 | const char *object_type_str(enum bt_visitor_object_type type) |
| 55 | { |
| 56 | switch (type) { |
| 57 | case BT_VISITOR_OBJECT_TYPE_TRACE: |
| 58 | return "trace"; |
| 59 | case BT_VISITOR_OBJECT_TYPE_STREAM_CLASS: |
| 60 | return "stream class"; |
| 61 | case BT_VISITOR_OBJECT_TYPE_STREAM: |
| 62 | return "stream"; |
| 63 | case BT_VISITOR_OBJECT_TYPE_EVENT_CLASS: |
| 64 | return "event class"; |
| 65 | case BT_VISITOR_OBJECT_TYPE_EVENT: |
| 66 | return "event"; |
| 67 | default: |
| 68 | return "unknown"; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | struct bt_event_class *init_event_class(const char *name) |
| 73 | { |
| 74 | int ret; |
| 75 | struct bt_event_class *ec = bt_event_class_create(name); |
| 76 | struct bt_field_type *payload_ft = NULL;; |
| 77 | struct bt_field_type *int_field = |
| 78 | bt_field_type_integer_create(8); |
| 79 | |
| 80 | if (!ec || !int_field) { |
| 81 | goto error; |
| 82 | } |
| 83 | |
| 84 | payload_ft = bt_event_class_get_payload_field_type(ec); |
| 85 | ret = bt_field_type_structure_add_field(payload_ft, |
| 86 | int_field, "an_int_field"); |
| 87 | if (ret) { |
| 88 | goto error; |
| 89 | } |
| 90 | |
| 91 | BT_PUT(int_field); |
| 92 | BT_PUT(payload_ft); |
| 93 | return ec; |
| 94 | error: |
| 95 | BT_PUT(ec); |
| 96 | BT_PUT(int_field); |
| 97 | BT_PUT(payload_ft); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | static void set_stream_class_field_types( |
| 102 | struct bt_stream_class *stream_class) |
| 103 | { |
| 104 | struct bt_field_type *packet_context_type; |
| 105 | struct bt_field_type *event_header_type; |
| 106 | struct bt_field_type *ft; |
| 107 | int ret; |
| 108 | |
| 109 | packet_context_type = bt_field_type_structure_create(); |
| 110 | BT_ASSERT(packet_context_type); |
| 111 | ft = bt_field_type_integer_create(32); |
| 112 | BT_ASSERT(ft); |
| 113 | ret = bt_field_type_structure_add_field(packet_context_type, |
| 114 | ft, "packet_size"); |
| 115 | BT_ASSERT(ret == 0); |
| 116 | bt_put(ft); |
| 117 | ft = bt_field_type_integer_create(32); |
| 118 | BT_ASSERT(ft); |
| 119 | ret = bt_field_type_structure_add_field(packet_context_type, |
| 120 | ft, "content_size"); |
| 121 | BT_ASSERT(ret == 0); |
| 122 | bt_put(ft); |
| 123 | |
| 124 | event_header_type = bt_field_type_structure_create(); |
| 125 | BT_ASSERT(event_header_type); |
| 126 | ft = bt_field_type_integer_create(32); |
| 127 | BT_ASSERT(ft); |
| 128 | ret = bt_field_type_structure_add_field(event_header_type, |
| 129 | ft, "id"); |
| 130 | BT_ASSERT(ret == 0); |
| 131 | bt_put(ft); |
| 132 | |
| 133 | ret = bt_stream_class_set_packet_context_field_type(stream_class, |
| 134 | packet_context_type); |
| 135 | BT_ASSERT(ret == 0); |
| 136 | ret = bt_stream_class_set_event_header_field_type(stream_class, |
| 137 | event_header_type); |
| 138 | BT_ASSERT(ret == 0); |
| 139 | |
| 140 | bt_put(packet_context_type); |
| 141 | bt_put(event_header_type); |
| 142 | } |
| 143 | |
| 144 | static void set_trace_packet_header(struct bt_trace *trace) |
| 145 | { |
| 146 | struct bt_field_type *packet_header_type; |
| 147 | struct bt_field_type *ft; |
| 148 | int ret; |
| 149 | |
| 150 | packet_header_type = bt_field_type_structure_create(); |
| 151 | BT_ASSERT(packet_header_type); |
| 152 | ft = bt_field_type_integer_create(32); |
| 153 | BT_ASSERT(ft); |
| 154 | ret = bt_field_type_structure_add_field(packet_header_type, |
| 155 | ft, "stream_id"); |
| 156 | BT_ASSERT(ret == 0); |
| 157 | bt_put(ft); |
| 158 | |
| 159 | ret = bt_trace_set_packet_header_field_type(trace, |
| 160 | packet_header_type); |
| 161 | BT_ASSERT(ret == 0); |
| 162 | |
| 163 | bt_put(packet_header_type); |
| 164 | } |
| 165 | |
| 166 | struct bt_trace *init_trace(void) |
| 167 | { |
| 168 | int ret; |
| 169 | struct bt_trace *trace = bt_trace_create(); |
| 170 | struct bt_stream_class *sc1 = bt_stream_class_create("sc1"); |
| 171 | struct bt_stream_class *sc2 = bt_stream_class_create("sc2"); |
| 172 | struct bt_event_class *ec1 = init_event_class("ec1"); |
| 173 | struct bt_event_class *ec2 = init_event_class("ec2"); |
| 174 | struct bt_event_class *ec3 = init_event_class("ec3"); |
| 175 | |
| 176 | if (!trace || !sc1 || !sc2 || !ec1 || !ec2 || !ec3) { |
| 177 | goto end; |
| 178 | } |
| 179 | |
| 180 | set_trace_packet_header(trace); |
| 181 | set_stream_class_field_types(sc1); |
| 182 | set_stream_class_field_types(sc2); |
| 183 | ret = bt_stream_class_add_event_class(sc1, ec1); |
| 184 | if (ret) { |
| 185 | goto error; |
| 186 | } |
| 187 | |
| 188 | ret = bt_stream_class_add_event_class(sc2, ec2); |
| 189 | if (ret) { |
| 190 | goto error; |
| 191 | } |
| 192 | |
| 193 | ret = bt_stream_class_add_event_class(sc2, ec3); |
| 194 | if (ret) { |
| 195 | goto error; |
| 196 | } |
| 197 | |
| 198 | ret = bt_trace_add_stream_class(trace, sc1); |
| 199 | if (ret) { |
| 200 | goto error; |
| 201 | } |
| 202 | |
| 203 | ret = bt_trace_add_stream_class(trace, sc2); |
| 204 | if (ret) { |
| 205 | goto error; |
| 206 | } |
| 207 | end: |
| 208 | BT_PUT(sc1); |
| 209 | BT_PUT(sc2); |
| 210 | BT_PUT(ec1); |
| 211 | BT_PUT(ec2); |
| 212 | BT_PUT(ec3); |
| 213 | return trace; |
| 214 | error: |
| 215 | BT_PUT(trace); |
| 216 | goto end; |
| 217 | } |
| 218 | |
| 219 | int visitor(struct bt_visitor_object *object, void *data) |
| 220 | { |
| 221 | int ret = 0; |
| 222 | bool names_match; |
| 223 | const char *object_name; |
| 224 | struct visitor_state *state = data; |
| 225 | struct expected_result *expected = &expected_results[state->i++]; |
| 226 | |
| 227 | switch (bt_visitor_object_get_type(object)) { |
| 228 | case BT_VISITOR_OBJECT_TYPE_TRACE: |
| 229 | object_name = NULL; |
| 230 | names_match = expected->object_name == NULL; |
| 231 | break; |
| 232 | case BT_VISITOR_OBJECT_TYPE_STREAM_CLASS: |
| 233 | object_name = bt_stream_class_get_name( |
| 234 | bt_visitor_object_get_object(object)); |
| 235 | if (!object_name) { |
| 236 | ret = -1; |
| 237 | goto end; |
| 238 | } |
| 239 | |
| 240 | names_match = !strcmp(object_name, expected->object_name); |
| 241 | break; |
| 242 | case BT_VISITOR_OBJECT_TYPE_EVENT_CLASS: |
| 243 | object_name = bt_event_class_get_name( |
| 244 | bt_visitor_object_get_object(object)); |
| 245 | if (!object_name) { |
| 246 | ret = -1; |
| 247 | goto end; |
| 248 | } |
| 249 | |
| 250 | names_match = !strcmp(object_name, expected->object_name); |
| 251 | break; |
| 252 | default: |
| 253 | diag("Encountered an unexpected type while visiting trace"); |
| 254 | ret = -1; |
| 255 | goto end; |
| 256 | } |
| 257 | |
| 258 | ok(expected->object_type == bt_visitor_object_get_type(object), |
| 259 | "Encoutered object type %s, expected %s", |
| 260 | object_type_str(expected->object_type), |
| 261 | object_type_str(bt_visitor_object_get_type(object))); |
| 262 | ok(names_match, "Element name is %s, expected %s", |
| 263 | object_name ? : "NULL", |
| 264 | expected->object_name ? : "NULL"); |
| 265 | end: |
| 266 | return ret; |
| 267 | } |
| 268 | |
| 269 | int main(int argc, char **argv) |
| 270 | { |
| 271 | int ret; |
| 272 | struct bt_trace *trace; |
| 273 | struct visitor_state state = { 0 }; |
| 274 | |
| 275 | plan_tests(NR_TESTS); |
| 276 | |
| 277 | /* |
| 278 | * Initialize a reference trace which we'll walk using the |
| 279 | * bt_*_visit() interface. |
| 280 | */ |
| 281 | trace = init_trace(); |
| 282 | if (!trace) { |
| 283 | diag("Failed to initialize reference trace, aborting."); |
| 284 | exit(-1); |
| 285 | } |
| 286 | |
| 287 | ret = bt_trace_visit(trace, visitor, &state); |
| 288 | ok(!ret, "bt_trace_visit returned success"); |
| 289 | |
| 290 | BT_PUT(trace); |
| 291 | return exit_status(); |
| 292 | } |
| 293 | |