| 1 | /* |
| 2 | * test_trace_lister.c |
| 3 | * |
| 4 | * CTF IR trace listener 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 <stdlib.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #define NR_TESTS 21 |
| 31 | |
| 32 | struct visitor_state { |
| 33 | int i; |
| 34 | }; |
| 35 | |
| 36 | struct expected_result { |
| 37 | const char *object_name; |
| 38 | enum bt_ctf_object_type object_type; |
| 39 | }; |
| 40 | |
| 41 | struct expected_result expected_results[] = { |
| 42 | { NULL, BT_CTF_OBJECT_TYPE_TRACE }, |
| 43 | { "sc1", BT_CTF_OBJECT_TYPE_STREAM_CLASS }, |
| 44 | { "ec1", BT_CTF_OBJECT_TYPE_EVENT_CLASS }, |
| 45 | { "sc2", BT_CTF_OBJECT_TYPE_STREAM_CLASS }, |
| 46 | { "ec2", BT_CTF_OBJECT_TYPE_EVENT_CLASS }, |
| 47 | { "ec3", BT_CTF_OBJECT_TYPE_EVENT_CLASS }, |
| 48 | /* Elements added after the initial add_listener call. */ |
| 49 | { "sc3", BT_CTF_OBJECT_TYPE_STREAM_CLASS }, |
| 50 | { "ec4", BT_CTF_OBJECT_TYPE_EVENT_CLASS }, |
| 51 | { "ec5", BT_CTF_OBJECT_TYPE_EVENT_CLASS }, |
| 52 | }; |
| 53 | |
| 54 | const char *object_type_str(enum bt_ctf_object_type type) |
| 55 | { |
| 56 | switch (type) { |
| 57 | case BT_CTF_OBJECT_TYPE_TRACE: |
| 58 | return "trace"; |
| 59 | case BT_CTF_OBJECT_TYPE_STREAM_CLASS: |
| 60 | return "stream class"; |
| 61 | case BT_CTF_OBJECT_TYPE_STREAM: |
| 62 | return "stream"; |
| 63 | case BT_CTF_OBJECT_TYPE_EVENT_CLASS: |
| 64 | return "event class"; |
| 65 | case BT_CTF_OBJECT_TYPE_EVENT: |
| 66 | return "event"; |
| 67 | default: |
| 68 | return "unknown"; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | struct bt_ctf_event_class *init_event_class(const char *name) |
| 73 | { |
| 74 | int ret; |
| 75 | struct bt_ctf_event_class *ec = bt_ctf_event_class_create(name); |
| 76 | struct bt_ctf_field_type *int_field = |
| 77 | bt_ctf_field_type_integer_create(8); |
| 78 | |
| 79 | if (!ec || !int_field) { |
| 80 | goto error; |
| 81 | } |
| 82 | |
| 83 | ret = bt_ctf_event_class_add_field(ec, int_field, "an_int_field"); |
| 84 | if (ret) { |
| 85 | goto error; |
| 86 | } |
| 87 | |
| 88 | BT_PUT(int_field); |
| 89 | return ec; |
| 90 | error: |
| 91 | BT_PUT(ec); |
| 92 | BT_PUT(int_field); |
| 93 | return NULL; |
| 94 | } |
| 95 | |
| 96 | struct bt_ctf_trace *init_trace(void) |
| 97 | { |
| 98 | int ret; |
| 99 | struct bt_ctf_trace *trace = bt_ctf_trace_create(); |
| 100 | struct bt_ctf_stream_class *sc1 = bt_ctf_stream_class_create("sc1"); |
| 101 | struct bt_ctf_stream_class *sc2 = bt_ctf_stream_class_create("sc2"); |
| 102 | struct bt_ctf_event_class *ec1 = init_event_class("ec1"); |
| 103 | struct bt_ctf_event_class *ec2 = init_event_class("ec2"); |
| 104 | struct bt_ctf_event_class *ec3 = init_event_class("ec3"); |
| 105 | |
| 106 | if (!trace || !sc1 || !sc2 || !ec1 || !ec2 || !ec3) { |
| 107 | goto end; |
| 108 | } |
| 109 | |
| 110 | ret = bt_ctf_stream_class_add_event_class(sc1, ec1); |
| 111 | if (ret) { |
| 112 | goto error; |
| 113 | } |
| 114 | |
| 115 | ret = bt_ctf_stream_class_add_event_class(sc2, ec2); |
| 116 | if (ret) { |
| 117 | goto error; |
| 118 | } |
| 119 | |
| 120 | ret = bt_ctf_stream_class_add_event_class(sc2, ec3); |
| 121 | if (ret) { |
| 122 | goto error; |
| 123 | } |
| 124 | |
| 125 | ret = bt_ctf_trace_add_stream_class(trace, sc1); |
| 126 | if (ret) { |
| 127 | goto error; |
| 128 | } |
| 129 | |
| 130 | ret = bt_ctf_trace_add_stream_class(trace, sc2); |
| 131 | if (ret) { |
| 132 | goto error; |
| 133 | } |
| 134 | end: |
| 135 | BT_PUT(sc1); |
| 136 | BT_PUT(sc2); |
| 137 | BT_PUT(ec1); |
| 138 | BT_PUT(ec2); |
| 139 | BT_PUT(ec3); |
| 140 | return trace; |
| 141 | error: |
| 142 | BT_PUT(trace); |
| 143 | goto end; |
| 144 | } |
| 145 | |
| 146 | void visitor(struct bt_ctf_object *object, void *data) |
| 147 | { |
| 148 | bool names_match; |
| 149 | const char *object_name; |
| 150 | struct visitor_state *state = data; |
| 151 | struct expected_result *expected = &expected_results[state->i++]; |
| 152 | |
| 153 | switch (bt_ctf_object_get_type(object)) { |
| 154 | case BT_CTF_OBJECT_TYPE_TRACE: |
| 155 | object_name = NULL; |
| 156 | names_match = expected->object_name == NULL; |
| 157 | break; |
| 158 | case BT_CTF_OBJECT_TYPE_STREAM_CLASS: |
| 159 | object_name = bt_ctf_stream_class_get_name( |
| 160 | bt_ctf_object_get_object(object)); |
| 161 | if (!object_name) { |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | names_match = !strcmp(object_name, expected->object_name); |
| 166 | break; |
| 167 | case BT_CTF_OBJECT_TYPE_EVENT_CLASS: |
| 168 | object_name = bt_ctf_event_class_get_name( |
| 169 | bt_ctf_object_get_object(object)); |
| 170 | if (!object_name) { |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | names_match = !strcmp(object_name, expected->object_name); |
| 175 | break; |
| 176 | default: |
| 177 | diag("Encountered an unexpected type while visiting trace"); |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | ok(expected->object_type == bt_ctf_object_get_type(object), |
| 182 | "Encoutered object type %s, expected %s", |
| 183 | object_type_str(expected->object_type), |
| 184 | object_type_str(bt_ctf_object_get_type(object))); |
| 185 | ok(names_match, "Element name is %s, expected %s", |
| 186 | object_name ? : "NULL", |
| 187 | expected->object_name ? : "NULL"); |
| 188 | } |
| 189 | |
| 190 | int main(int argc, char **argv) |
| 191 | { |
| 192 | int ret, index; |
| 193 | struct bt_ctf_trace *trace; |
| 194 | struct visitor_state state = { 0 }; |
| 195 | struct bt_ctf_stream_class *sc3; |
| 196 | struct bt_ctf_event_class *ec4, *ec5; |
| 197 | |
| 198 | plan_tests(NR_TESTS); |
| 199 | |
| 200 | trace = init_trace(); |
| 201 | if (!trace) { |
| 202 | diag("Failed to initialize reference trace, aborting."); |
| 203 | exit(-1); |
| 204 | } |
| 205 | |
| 206 | ret = bt_ctf_trace_add_listener(trace, visitor, &state); |
| 207 | ok(!ret, "bt_ctf_trace_add_listener returned success"); |
| 208 | |
| 209 | /* |
| 210 | * Validate that listeners are notified when new objects are added to a |
| 211 | * trace. |
| 212 | */ |
| 213 | sc3 = bt_ctf_stream_class_create("sc3"); |
| 214 | if (!sc3) { |
| 215 | diag("Failed to create stream class, aborting."); |
| 216 | exit(-1); |
| 217 | } |
| 218 | |
| 219 | ec4 = init_event_class("ec4"); |
| 220 | ec5 = init_event_class("ec5"); |
| 221 | if (!ec4 || !ec5) { |
| 222 | diag("Failed to create event classes, aborting."); |
| 223 | exit(-1); |
| 224 | } |
| 225 | |
| 226 | ret = bt_ctf_stream_class_add_event_class(sc3, ec4); |
| 227 | if (ret) { |
| 228 | diag("Failed to add event class to stream class, aborting."); |
| 229 | } |
| 230 | |
| 231 | index = state.i; |
| 232 | ret = bt_ctf_trace_add_stream_class(trace, sc3); |
| 233 | if (ret) { |
| 234 | diag("Failed to add stream class sc3 to trace, aborting."); |
| 235 | exit(-1); |
| 236 | } |
| 237 | |
| 238 | /* Listener should have been invoked two times (sc3 + ec4). */ |
| 239 | ok(index + 2 == state.i, "trace modification listener has been invoked twice after addition of a stream class"); |
| 240 | |
| 241 | index = state.i; |
| 242 | ret = bt_ctf_stream_class_add_event_class(sc3, ec5); |
| 243 | if (ret) { |
| 244 | diag("Failed to add event class to stream class, aborting."); |
| 245 | exit(-1); |
| 246 | } |
| 247 | |
| 248 | ok(index + 1 == state.i, "trace modification has been invoked once after addition of an event class"); |
| 249 | |
| 250 | BT_PUT(sc3); |
| 251 | BT_PUT(ec5); |
| 252 | BT_PUT(ec4); |
| 253 | BT_PUT(trace); |
| 254 | return exit_status(); |
| 255 | } |
| 256 | |