| 1 | /* |
| 2 | * test_ctf_writer.c |
| 3 | * |
| 4 | * CTF Writer test |
| 5 | * |
| 6 | * Copyright 2013 - 2017 - 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 <babeltrace/ctf-writer/writer.h> |
| 23 | #include <babeltrace/ctf-writer/clock.h> |
| 24 | #include <babeltrace/ctf-writer/stream.h> |
| 25 | #include <babeltrace/ctf-writer/event.h> |
| 26 | #include <babeltrace/ctf-writer/event-types.h> |
| 27 | #include <babeltrace/ctf-writer/event-fields.h> |
| 28 | #include <babeltrace/ctf-writer/stream-class.h> |
| 29 | #include <babeltrace/ctf-ir/packet.h> |
| 30 | #include <babeltrace/ctf-ir/clock-class.h> |
| 31 | #include <babeltrace/ctf-ir/trace.h> |
| 32 | #include <babeltrace/ref.h> |
| 33 | #include <babeltrace/ctf/events.h> |
| 34 | #include <babeltrace/values.h> |
| 35 | #include <unistd.h> |
| 36 | #include <babeltrace/compat/stdlib-internal.h> |
| 37 | #include <stdio.h> |
| 38 | #include <babeltrace/compat/utsname-internal.h> |
| 39 | #include <babeltrace/compat/limits-internal.h> |
| 40 | #include <babeltrace/compat/stdio-internal.h> |
| 41 | #include <string.h> |
| 42 | #include <assert.h> |
| 43 | #include <sys/wait.h> |
| 44 | #include <fcntl.h> |
| 45 | #include "tap/tap.h" |
| 46 | #include <math.h> |
| 47 | #include <float.h> |
| 48 | #include "common.h" |
| 49 | |
| 50 | #define METADATA_LINE_SIZE 512 |
| 51 | #define SEQUENCE_TEST_LENGTH 10 |
| 52 | #define ARRAY_TEST_LENGTH 5 |
| 53 | #define PACKET_RESIZE_TEST_DEF_LENGTH 100000 |
| 54 | |
| 55 | #define DEFAULT_CLOCK_FREQ 1000000000 |
| 56 | #define DEFAULT_CLOCK_PRECISION 1 |
| 57 | #define DEFAULT_CLOCK_OFFSET 0 |
| 58 | #define DEFAULT_CLOCK_OFFSET_S 0 |
| 59 | #define DEFAULT_CLOCK_IS_ABSOLUTE 0 |
| 60 | #define DEFAULT_CLOCK_TIME 0 |
| 61 | #define DEFAULT_CLOCK_VALUE 0 |
| 62 | |
| 63 | #define NR_TESTS 623 |
| 64 | |
| 65 | static int64_t current_time = 42; |
| 66 | static unsigned int packet_resize_test_length = PACKET_RESIZE_TEST_DEF_LENGTH; |
| 67 | |
| 68 | /* Return 1 if uuids match, zero if different. */ |
| 69 | static |
| 70 | int uuid_match(const unsigned char *uuid_a, const unsigned char *uuid_b) |
| 71 | { |
| 72 | int ret = 0; |
| 73 | int i; |
| 74 | |
| 75 | if (!uuid_a || !uuid_b) { |
| 76 | goto end; |
| 77 | } |
| 78 | |
| 79 | for (i = 0; i < 16; i++) { |
| 80 | if (uuid_a[i] != uuid_b[i]) { |
| 81 | goto end; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | ret = 1; |
| 86 | end: |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | static |
| 91 | void validate_trace(char *parser_path, char *trace_path) |
| 92 | { |
| 93 | int ret = 0; |
| 94 | char babeltrace_output_path[] = "/tmp/babeltrace_output_XXXXXX"; |
| 95 | int babeltrace_output_fd = -1; |
| 96 | |
| 97 | if (!trace_path) { |
| 98 | ret = -1; |
| 99 | goto result; |
| 100 | } |
| 101 | |
| 102 | babeltrace_output_fd = mkstemp(babeltrace_output_path); |
| 103 | unlink(babeltrace_output_path); |
| 104 | |
| 105 | if (babeltrace_output_fd == -1) { |
| 106 | diag("Failed to create a temporary file for trace parsing."); |
| 107 | ret = -1; |
| 108 | goto result; |
| 109 | } |
| 110 | |
| 111 | pid_t pid = fork(); |
| 112 | if (pid) { |
| 113 | int status = 0; |
| 114 | waitpid(pid, &status, 0); |
| 115 | ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1; |
| 116 | } else { |
| 117 | ret = dup2(babeltrace_output_fd, STDOUT_FILENO); |
| 118 | if (ret < 0) { |
| 119 | perror("# dup2 babeltrace_output_fd to STDOUT"); |
| 120 | goto result; |
| 121 | } |
| 122 | |
| 123 | ret = dup2(babeltrace_output_fd, STDERR_FILENO); |
| 124 | if (ret < 0) { |
| 125 | perror("# dup2 babeltrace_output_fd to STDERR"); |
| 126 | goto result; |
| 127 | } |
| 128 | |
| 129 | execl(parser_path, parser_path, trace_path, NULL); |
| 130 | perror("# Could not launch the babeltrace process"); |
| 131 | exit(-1); |
| 132 | } |
| 133 | result: |
| 134 | ok(ret == 0, "Babeltrace could read the resulting trace"); |
| 135 | |
| 136 | if (ret && babeltrace_output_fd >= 0) { |
| 137 | char *line; |
| 138 | size_t len = METADATA_LINE_SIZE; |
| 139 | FILE *babeltrace_output_fp = NULL; |
| 140 | |
| 141 | babeltrace_output_fp = fdopen(babeltrace_output_fd, "r"); |
| 142 | if (!babeltrace_output_fp) { |
| 143 | perror("fdopen on babeltrace_output_fd"); |
| 144 | goto close_fp; |
| 145 | } |
| 146 | babeltrace_output_fd = -1; |
| 147 | |
| 148 | line = malloc(len); |
| 149 | if (!line) { |
| 150 | diag("malloc error"); |
| 151 | } |
| 152 | rewind(babeltrace_output_fp); |
| 153 | while (bt_getline(&line, &len, babeltrace_output_fp) > 0) { |
| 154 | diag("%s", line); |
| 155 | } |
| 156 | |
| 157 | free(line); |
| 158 | close_fp: |
| 159 | if (babeltrace_output_fp) { |
| 160 | if (fclose(babeltrace_output_fp)) { |
| 161 | diag("fclose error"); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if (babeltrace_output_fd >= 0) { |
| 167 | if (close(babeltrace_output_fd)) { |
| 168 | diag("close error"); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | static |
| 174 | void append_simple_event(struct bt_ctf_stream_class *stream_class, |
| 175 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) |
| 176 | { |
| 177 | /* Create and add a simple event class */ |
| 178 | struct bt_ctf_event_class *simple_event_class = |
| 179 | bt_ctf_event_class_create("Simple Event"); |
| 180 | struct bt_ctf_field_type *uint_12_type = |
| 181 | bt_ctf_field_type_integer_create(12); |
| 182 | struct bt_ctf_field_type *int_64_type = |
| 183 | bt_ctf_field_type_integer_create(64); |
| 184 | struct bt_ctf_field_type *float_type = |
| 185 | bt_ctf_field_type_floating_point_create(); |
| 186 | struct bt_ctf_field_type *enum_type; |
| 187 | struct bt_ctf_field_type *enum_type_unsigned = |
| 188 | bt_ctf_field_type_enumeration_create(uint_12_type); |
| 189 | struct bt_ctf_field_type *event_context_type = |
| 190 | bt_ctf_field_type_structure_create(); |
| 191 | struct bt_ctf_field_type *event_payload_type = NULL; |
| 192 | struct bt_ctf_field_type *returned_type; |
| 193 | struct bt_ctf_event *simple_event; |
| 194 | struct bt_ctf_field *integer_field; |
| 195 | struct bt_ctf_field *float_field; |
| 196 | struct bt_ctf_field *enum_field; |
| 197 | struct bt_ctf_field *enum_field_unsigned; |
| 198 | struct bt_ctf_field *enum_container_field; |
| 199 | const char *mapping_name_test = "truie"; |
| 200 | const double double_test_value = 3.1415; |
| 201 | struct bt_ctf_field *enum_container_field_unsigned; |
| 202 | const char *mapping_name_negative_test = "negative_value"; |
| 203 | const char *ret_char; |
| 204 | double ret_double; |
| 205 | int64_t ret_range_start_int64_t, ret_range_end_int64_t; |
| 206 | uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t; |
| 207 | struct bt_ctf_event_class *ret_event_class; |
| 208 | struct bt_ctf_field *packet_context; |
| 209 | struct bt_ctf_field *packet_context_field; |
| 210 | struct bt_ctf_field *stream_event_context; |
| 211 | struct bt_ctf_field *stream_event_context_field; |
| 212 | struct bt_ctf_field *event_context; |
| 213 | struct bt_ctf_field *event_context_field; |
| 214 | struct bt_ctf_field_type *ep_integer_field_type = NULL; |
| 215 | struct bt_ctf_field_type *ep_enum_field_type = NULL; |
| 216 | struct bt_ctf_field_type *ep_enum_field_unsigned_type = NULL; |
| 217 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL; |
| 218 | int ret; |
| 219 | |
| 220 | ok(uint_12_type, "Create an unsigned integer type"); |
| 221 | |
| 222 | ok(!bt_ctf_field_type_integer_set_signed(int_64_type, 1), |
| 223 | "Set signed 64 bit integer signedness to true"); |
| 224 | ok(int_64_type, "Create a signed integer type"); |
| 225 | enum_type = bt_ctf_field_type_enumeration_create(int_64_type); |
| 226 | |
| 227 | returned_type = bt_ctf_field_type_enumeration_get_container_type(enum_type); |
| 228 | ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_type returns the right type"); |
| 229 | ok(!bt_ctf_field_type_enumeration_get_container_type(NULL), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly"); |
| 230 | ok(!bt_ctf_field_type_enumeration_create(enum_type), |
| 231 | "bt_ctf_field_enumeration_type_create rejects non-integer container field types"); |
| 232 | bt_put(returned_type); |
| 233 | |
| 234 | bt_ctf_field_type_set_alignment(float_type, 32); |
| 235 | ok(bt_ctf_field_type_get_alignment(NULL) < 0, |
| 236 | "bt_ctf_field_type_get_alignment handles NULL correctly"); |
| 237 | ok(bt_ctf_field_type_get_alignment(float_type) == 32, |
| 238 | "bt_ctf_field_type_get_alignment returns a correct value"); |
| 239 | |
| 240 | ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0, |
| 241 | "Set a floating point type's exponent digit count"); |
| 242 | ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0, |
| 243 | "Set a floating point type's mantissa digit count"); |
| 244 | |
| 245 | ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL) < 0, |
| 246 | "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly"); |
| 247 | ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL) < 0, |
| 248 | "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly"); |
| 249 | ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11, |
| 250 | "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value"); |
| 251 | ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53, |
| 252 | "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value"); |
| 253 | |
| 254 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
| 255 | mapping_name_negative_test, -12345, 0) == 0, |
| 256 | "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings"); |
| 257 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
| 258 | "escaping; \"test\"", 1, 1) == 0, |
| 259 | "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes"); |
| 260 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
| 261 | "\tanother \'escaping\'\n test\"", 2, 4) == 0, |
| 262 | "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters"); |
| 263 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
| 264 | "event clock int float", 5, 22) == 0, |
| 265 | "Accept enumeration mapping strings containing reserved keywords"); |
| 266 | bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, |
| 267 | 42, 42); |
| 268 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, |
| 269 | 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names"); |
| 270 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something", |
| 271 | -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries"); |
| 272 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, |
| 273 | -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start"); |
| 274 | bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000); |
| 275 | |
| 276 | iter = bt_ctf_field_type_enumeration_find_mappings_by_signed_value(NULL, -42); |
| 277 | ok(iter == NULL, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly"); |
| 278 | |
| 279 | iter = bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type, -4200000); |
| 280 | ok(iter == NULL, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values"); |
| 281 | |
| 282 | iter = bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type, 3); |
| 283 | ok(iter != NULL, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value"); |
| 284 | ok(bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter, NULL, NULL, NULL) == 0, |
| 285 | "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly"); |
| 286 | BT_PUT(iter); |
| 287 | |
| 288 | ok(bt_ctf_event_class_add_field(simple_event_class, enum_type, |
| 289 | "enum_field") == 0, "Add signed enumeration field to event"); |
| 290 | |
| 291 | ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL, 0, &ret_char, |
| 292 | &ret_range_start_int64_t, &ret_range_end_int64_t) < 0, |
| 293 | "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly"); |
| 294 | ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type, 0, NULL, |
| 295 | &ret_range_start_int64_t, &ret_range_end_int64_t) == 0, |
| 296 | "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly"); |
| 297 | ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type, 0, &ret_char, |
| 298 | NULL, &ret_range_end_int64_t) == 0, |
| 299 | "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly"); |
| 300 | ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type, 0, &ret_char, |
| 301 | &ret_range_start_int64_t, NULL) == 0, |
| 302 | "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly"); |
| 303 | /* Assumes entries are sorted by range_start values. */ |
| 304 | ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type, 6, &ret_char, |
| 305 | &ret_range_start_int64_t, &ret_range_end_int64_t) == 0, |
| 306 | "bt_ctf_field_type_enumeration_get_mapping_signed returns a value"); |
| 307 | ok(!strcmp(ret_char, mapping_name_test), |
| 308 | "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name"); |
| 309 | ok(ret_range_start_int64_t == 42, |
| 310 | "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start"); |
| 311 | ok(ret_range_end_int64_t == 42, |
| 312 | "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end"); |
| 313 | |
| 314 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, |
| 315 | "escaping; \"test\"", 0, 0) == 0, |
| 316 | "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes"); |
| 317 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, |
| 318 | "\tanother \'escaping\'\n test\"", 1, 4) == 0, |
| 319 | "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters"); |
| 320 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, |
| 321 | "event clock int float", 5, 22) == 0, |
| 322 | "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords"); |
| 323 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, |
| 324 | 42, 42) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts single-value ranges"); |
| 325 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, |
| 326 | 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names"); |
| 327 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something", |
| 328 | 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries"); |
| 329 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, |
| 330 | 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start"); |
| 331 | ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned, |
| 332 | "enum_field_unsigned") == 0, "Add unsigned enumeration field to event"); |
| 333 | |
| 334 | ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL) < 0, |
| 335 | "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly"); |
| 336 | ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 6, |
| 337 | "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value"); |
| 338 | |
| 339 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char, |
| 340 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0, |
| 341 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly"); |
| 342 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL, |
| 343 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0, |
| 344 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly"); |
| 345 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char, |
| 346 | NULL, &ret_range_end_uint64_t) == 0, |
| 347 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly"); |
| 348 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char, |
| 349 | &ret_range_start_uint64_t, NULL) == 0, |
| 350 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly"); |
| 351 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 4, &ret_char, |
| 352 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0, |
| 353 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value"); |
| 354 | ok(!strcmp(ret_char, mapping_name_test), |
| 355 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name"); |
| 356 | ok(ret_range_start_uint64_t == 42, |
| 357 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start"); |
| 358 | ok(ret_range_end_uint64_t == 42, |
| 359 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end"); |
| 360 | |
| 361 | bt_ctf_event_class_add_field(simple_event_class, uint_12_type, |
| 362 | "integer_field"); |
| 363 | bt_ctf_event_class_add_field(simple_event_class, float_type, |
| 364 | "float_field"); |
| 365 | |
| 366 | assert(!bt_ctf_event_class_set_id(simple_event_class, 13)); |
| 367 | |
| 368 | /* Set an event context type which will contain a single integer. */ |
| 369 | ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type, |
| 370 | "event_specific_context"), |
| 371 | "Add event specific context field"); |
| 372 | ok(bt_ctf_event_class_get_context_type(NULL) == NULL, |
| 373 | "bt_ctf_event_class_get_context_type handles NULL correctly"); |
| 374 | |
| 375 | ok(bt_ctf_event_class_set_context_type(NULL, event_context_type) < 0, |
| 376 | "bt_ctf_event_class_set_context_type handles a NULL event class correctly"); |
| 377 | ok(!bt_ctf_event_class_set_context_type(simple_event_class, event_context_type), |
| 378 | "Set an event class' context type successfully"); |
| 379 | returned_type = bt_ctf_event_class_get_context_type(simple_event_class); |
| 380 | ok(returned_type == event_context_type, |
| 381 | "bt_ctf_event_class_get_context_type returns the appropriate type"); |
| 382 | bt_put(returned_type); |
| 383 | |
| 384 | ok(!bt_ctf_stream_class_add_event_class(stream_class, simple_event_class), |
| 385 | "Adding simple event class to stream class"); |
| 386 | |
| 387 | /* |
| 388 | * bt_ctf_stream_class_add_event_class() copies the field types |
| 389 | * of simple_event_class, so we retrieve the new ones to create |
| 390 | * the appropriate fields. |
| 391 | */ |
| 392 | BT_PUT(event_context_type); |
| 393 | BT_PUT(event_payload_type); |
| 394 | event_payload_type = bt_ctf_event_class_get_payload_type( |
| 395 | simple_event_class); |
| 396 | assert(event_payload_type); |
| 397 | event_context_type = bt_ctf_event_class_get_context_type( |
| 398 | simple_event_class); |
| 399 | assert(event_context_type); |
| 400 | ep_integer_field_type = |
| 401 | bt_ctf_field_type_structure_get_field_type_by_name( |
| 402 | event_payload_type, "integer_field"); |
| 403 | assert(ep_integer_field_type); |
| 404 | ep_enum_field_type = |
| 405 | bt_ctf_field_type_structure_get_field_type_by_name( |
| 406 | event_payload_type, "enum_field"); |
| 407 | assert(ep_enum_field_type); |
| 408 | ep_enum_field_unsigned_type = |
| 409 | bt_ctf_field_type_structure_get_field_type_by_name( |
| 410 | event_payload_type, "enum_field_unsigned"); |
| 411 | assert(ep_enum_field_unsigned_type); |
| 412 | |
| 413 | ok(bt_ctf_stream_class_get_event_class_count(NULL) < 0, |
| 414 | "bt_ctf_stream_class_get_event_class_count handles NULL correctly"); |
| 415 | ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1, |
| 416 | "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes"); |
| 417 | ok(bt_ctf_stream_class_get_event_class_by_index(NULL, 0) == NULL, |
| 418 | "bt_ctf_stream_class_get_event_class handles NULL correctly"); |
| 419 | ok(bt_ctf_stream_class_get_event_class_by_index(stream_class, 8724) == NULL, |
| 420 | "bt_ctf_stream_class_get_event_class handles invalid indexes correctly"); |
| 421 | ret_event_class = bt_ctf_stream_class_get_event_class_by_index(stream_class, 0); |
| 422 | ok(ret_event_class == simple_event_class, |
| 423 | "bt_ctf_stream_class_get_event_class returns the correct event class"); |
| 424 | bt_put(ret_event_class); |
| 425 | ok(!bt_ctf_stream_class_get_event_class_by_id(NULL, 0), |
| 426 | "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly"); |
| 427 | ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class, 2), |
| 428 | "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist"); |
| 429 | ret_event_class = |
| 430 | bt_ctf_stream_class_get_event_class_by_id(stream_class, 13); |
| 431 | ok(ret_event_class == simple_event_class, |
| 432 | "bt_ctf_stream_class_get_event_class_by_id returns a correct event class"); |
| 433 | bt_put(ret_event_class); |
| 434 | |
| 435 | simple_event = bt_ctf_event_create(simple_event_class); |
| 436 | ok(simple_event, |
| 437 | "Instantiate an event containing a single integer field"); |
| 438 | |
| 439 | integer_field = bt_ctf_field_create(ep_integer_field_type); |
| 440 | bt_ctf_field_unsigned_integer_set_value(integer_field, 42); |
| 441 | ok(bt_ctf_event_set_payload(simple_event, "integer_field", |
| 442 | integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field"); |
| 443 | |
| 444 | float_field = bt_ctf_event_get_payload(simple_event, "float_field"); |
| 445 | ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double), |
| 446 | "bt_ctf_field_floating_point_get_value fails on an unset float field"); |
| 447 | bt_ctf_field_floating_point_set_value(float_field, double_test_value); |
| 448 | ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double), |
| 449 | "bt_ctf_field_floating_point_get_value properly handles a NULL field"); |
| 450 | ok(bt_ctf_field_floating_point_get_value(float_field, NULL), |
| 451 | "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer"); |
| 452 | ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double), |
| 453 | "bt_ctf_field_floating_point_get_value returns a double value"); |
| 454 | ok(fabs(ret_double - double_test_value) <= DBL_EPSILON, |
| 455 | "bt_ctf_field_floating_point_get_value returns a correct value"); |
| 456 | |
| 457 | enum_field = bt_ctf_field_create(ep_enum_field_type); |
| 458 | assert(enum_field); |
| 459 | |
| 460 | iter = bt_ctf_field_enumeration_get_mappings(NULL); |
| 461 | ok(!iter, "bt_ctf_field_enumeration_get_mappings handles NULL correctly"); |
| 462 | iter = bt_ctf_field_enumeration_get_mappings(enum_field); |
| 463 | ok(!iter, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset"); |
| 464 | enum_container_field = bt_ctf_field_enumeration_get_container( |
| 465 | enum_field); |
| 466 | ok(bt_ctf_field_signed_integer_set_value( |
| 467 | enum_container_field, -42) == 0, |
| 468 | "Set signed enumeration container value"); |
| 469 | iter = bt_ctf_field_enumeration_get_mappings(enum_field); |
| 470 | ok(iter, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings"); |
| 471 | ret = bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter, &ret_char, NULL, NULL); |
| 472 | ok(!ret && ret_char, "bt_ctf_field_type_enumeration_mapping_iterator_get_signed return a mapping name"); |
| 473 | assert(ret_char); |
| 474 | ok(!strcmp(ret_char, mapping_name_negative_test), |
| 475 | "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container"); |
| 476 | ret = bt_ctf_event_set_payload(simple_event, "enum_field", enum_field); |
| 477 | assert(!ret); |
| 478 | BT_PUT(iter); |
| 479 | |
| 480 | enum_field_unsigned = bt_ctf_field_create(ep_enum_field_unsigned_type); |
| 481 | assert(enum_field_unsigned); |
| 482 | enum_container_field_unsigned = bt_ctf_field_enumeration_get_container( |
| 483 | enum_field_unsigned); |
| 484 | ok(bt_ctf_field_unsigned_integer_set_value( |
| 485 | enum_container_field_unsigned, 42) == 0, |
| 486 | "Set unsigned enumeration container value"); |
| 487 | ret = bt_ctf_event_set_payload(simple_event, "enum_field_unsigned", |
| 488 | enum_field_unsigned); |
| 489 | assert(!ret); |
| 490 | iter = bt_ctf_field_enumeration_get_mappings(enum_field_unsigned); |
| 491 | assert(iter); |
| 492 | (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter, &ret_char, NULL, NULL); |
| 493 | ok(ret_char && !strcmp(ret_char, mapping_name_test), |
| 494 | "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container"); |
| 495 | |
| 496 | ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time"); |
| 497 | |
| 498 | /* Populate stream event context */ |
| 499 | stream_event_context = |
| 500 | bt_ctf_event_get_stream_event_context(simple_event); |
| 501 | assert(stream_event_context); |
| 502 | stream_event_context_field = bt_ctf_field_structure_get_field( |
| 503 | stream_event_context, "common_event_context"); |
| 504 | bt_ctf_field_unsigned_integer_set_value(stream_event_context_field, 42); |
| 505 | |
| 506 | /* Populate the event's context */ |
| 507 | ok(bt_ctf_event_get_event_context(NULL) == NULL, |
| 508 | "bt_ctf_event_get_event_context handles NULL correctly"); |
| 509 | event_context = bt_ctf_event_get_event_context(simple_event); |
| 510 | ok(event_context, |
| 511 | "bt_ctf_event_get_event_context returns a field"); |
| 512 | returned_type = bt_ctf_field_get_type(event_context); |
| 513 | ok(returned_type == event_context_type, |
| 514 | "bt_ctf_event_get_event_context returns a field of the appropriate type"); |
| 515 | event_context_field = bt_ctf_field_structure_get_field(event_context, |
| 516 | "event_specific_context"); |
| 517 | ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field, 1234), |
| 518 | "Successfully set an event context's value"); |
| 519 | ok(bt_ctf_event_set_event_context(NULL, event_context) < 0, |
| 520 | "bt_ctf_event_set_event_context handles a NULL event correctly"); |
| 521 | ok(bt_ctf_event_set_event_context(simple_event, NULL) < 0, |
| 522 | "bt_ctf_event_set_event_context handles a NULL event context correctly"); |
| 523 | ok(bt_ctf_event_set_event_context(simple_event, event_context_field) < 0, |
| 524 | "bt_ctf_event_set_event_context rejects a context of the wrong type"); |
| 525 | ok(!bt_ctf_event_set_event_context(simple_event, event_context), |
| 526 | "Set an event context successfully"); |
| 527 | |
| 528 | ok(bt_ctf_stream_append_event(stream, simple_event) == 0, |
| 529 | "Append simple event to trace stream"); |
| 530 | |
| 531 | ok(bt_ctf_stream_get_packet_context(NULL) == NULL, |
| 532 | "bt_ctf_stream_get_packet_context handles NULL correctly"); |
| 533 | packet_context = bt_ctf_stream_get_packet_context(stream); |
| 534 | ok(packet_context, |
| 535 | "bt_ctf_stream_get_packet_context returns a packet context"); |
| 536 | |
| 537 | packet_context_field = bt_ctf_field_structure_get_field(packet_context, |
| 538 | "packet_size"); |
| 539 | ok(packet_context_field, |
| 540 | "Packet context contains the default packet_size field."); |
| 541 | bt_put(packet_context_field); |
| 542 | packet_context_field = bt_ctf_field_structure_get_field(packet_context, |
| 543 | "custom_packet_context_field"); |
| 544 | ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field, 8) == 0, |
| 545 | "Custom packet context field value successfully set."); |
| 546 | |
| 547 | ok(bt_ctf_stream_set_packet_context(NULL, packet_context_field) < 0, |
| 548 | "bt_ctf_stream_set_packet_context handles a NULL stream correctly"); |
| 549 | ok(bt_ctf_stream_set_packet_context(stream, NULL) < 0, |
| 550 | "bt_ctf_stream_set_packet_context handles a NULL packet context correctly"); |
| 551 | ok(bt_ctf_stream_set_packet_context(stream, packet_context) == 0, |
| 552 | "Successfully set a stream's packet context"); |
| 553 | |
| 554 | ok(bt_ctf_stream_flush(stream) == 0, |
| 555 | "Flush trace stream with one event"); |
| 556 | |
| 557 | bt_put(simple_event_class); |
| 558 | bt_put(simple_event); |
| 559 | bt_put(uint_12_type); |
| 560 | bt_put(int_64_type); |
| 561 | bt_put(float_type); |
| 562 | bt_put(enum_type); |
| 563 | bt_put(enum_type_unsigned); |
| 564 | bt_put(returned_type); |
| 565 | bt_put(event_context_type); |
| 566 | bt_put(integer_field); |
| 567 | bt_put(float_field); |
| 568 | bt_put(enum_field); |
| 569 | bt_put(enum_field_unsigned); |
| 570 | bt_put(enum_container_field); |
| 571 | bt_put(enum_container_field_unsigned); |
| 572 | bt_put(packet_context); |
| 573 | bt_put(packet_context_field); |
| 574 | bt_put(stream_event_context); |
| 575 | bt_put(stream_event_context_field); |
| 576 | bt_put(event_context); |
| 577 | bt_put(event_context_field); |
| 578 | bt_put(event_payload_type); |
| 579 | bt_put(ep_integer_field_type); |
| 580 | bt_put(ep_enum_field_type); |
| 581 | bt_put(ep_enum_field_unsigned_type); |
| 582 | bt_put(iter); |
| 583 | } |
| 584 | |
| 585 | static |
| 586 | void append_complex_event(struct bt_ctf_stream_class *stream_class, |
| 587 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) |
| 588 | { |
| 589 | int i; |
| 590 | struct event_class_attrs_counts ; |
| 591 | const char *complex_test_event_string = "Complex Test Event"; |
| 592 | const char *test_string_1 = "Test "; |
| 593 | const char *test_string_2 = "string "; |
| 594 | const char *test_string_3 = "abcdefghi"; |
| 595 | const char *test_string_4 = "abcd\0efg\0hi"; |
| 596 | const char *test_string_cat = "Test string abcdeabcd"; |
| 597 | struct bt_ctf_field_type *uint_35_type = |
| 598 | bt_ctf_field_type_integer_create(35); |
| 599 | struct bt_ctf_field_type *int_16_type = |
| 600 | bt_ctf_field_type_integer_create(16); |
| 601 | struct bt_ctf_field_type *uint_3_type = |
| 602 | bt_ctf_field_type_integer_create(3); |
| 603 | struct bt_ctf_field_type *enum_variant_type = |
| 604 | bt_ctf_field_type_enumeration_create(uint_3_type); |
| 605 | struct bt_ctf_field_type *variant_type = |
| 606 | bt_ctf_field_type_variant_create(enum_variant_type, |
| 607 | "variant_selector"); |
| 608 | struct bt_ctf_field_type *string_type = |
| 609 | bt_ctf_field_type_string_create(); |
| 610 | struct bt_ctf_field_type *sequence_type; |
| 611 | struct bt_ctf_field_type *array_type; |
| 612 | struct bt_ctf_field_type *inner_structure_type = |
| 613 | bt_ctf_field_type_structure_create(); |
| 614 | struct bt_ctf_field_type *complex_structure_type = |
| 615 | bt_ctf_field_type_structure_create(); |
| 616 | struct bt_ctf_field_type *ret_field_type; |
| 617 | struct bt_ctf_event_class *event_class; |
| 618 | struct bt_ctf_event *event; |
| 619 | struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field, |
| 620 | *inner_structure_field, *complex_structure_field, |
| 621 | *a_sequence_field, *enum_variant_field, *enum_container_field, |
| 622 | *variant_field, *an_array_field, *stream_event_ctx_field, |
| 623 | *stream_event_ctx_int_field, *ret_field; |
| 624 | uint64_t ret_unsigned_int; |
| 625 | int64_t ret_signed_int; |
| 626 | const char *ret_string; |
| 627 | struct bt_ctf_stream_class *ret_stream_class; |
| 628 | struct bt_ctf_event_class *ret_event_class; |
| 629 | struct bt_ctf_field *packet_context, *packet_context_field; |
| 630 | struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL; |
| 631 | |
| 632 | ok(bt_ctf_field_type_set_alignment(int_16_type, 0), |
| 633 | "bt_ctf_field_type_set_alignment handles 0-alignment correctly"); |
| 634 | ok(bt_ctf_field_type_set_alignment(int_16_type, 3), |
| 635 | "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)"); |
| 636 | ok(bt_ctf_field_type_set_alignment(int_16_type, 24), |
| 637 | "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)"); |
| 638 | ok(!bt_ctf_field_type_set_alignment(int_16_type, 4), |
| 639 | "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)"); |
| 640 | ok(!bt_ctf_field_type_set_alignment(int_16_type, 32), |
| 641 | "Set alignment of signed 16 bit integer to 32"); |
| 642 | ok(!bt_ctf_field_type_integer_set_signed(int_16_type, 1), |
| 643 | "Set integer signedness to true"); |
| 644 | ok(!bt_ctf_field_type_integer_set_base(uint_35_type, |
| 645 | BT_CTF_INTEGER_BASE_HEXADECIMAL), |
| 646 | "Set signed 16 bit integer base to hexadecimal"); |
| 647 | |
| 648 | array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH); |
| 649 | sequence_type = bt_ctf_field_type_sequence_create(int_16_type, |
| 650 | "seq_len"); |
| 651 | |
| 652 | ok(bt_ctf_field_type_array_get_element_type(NULL) == NULL, |
| 653 | "bt_ctf_field_type_array_get_element_type handles NULL correctly"); |
| 654 | ret_field_type = bt_ctf_field_type_array_get_element_type( |
| 655 | array_type); |
| 656 | ok(ret_field_type == int_16_type, |
| 657 | "bt_ctf_field_type_array_get_element_type returns the correct type"); |
| 658 | bt_put(ret_field_type); |
| 659 | |
| 660 | ok(bt_ctf_field_type_array_get_length(NULL) < 0, |
| 661 | "bt_ctf_field_type_array_get_length handles NULL correctly"); |
| 662 | ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH, |
| 663 | "bt_ctf_field_type_array_get_length returns the correct length"); |
| 664 | |
| 665 | ok(bt_ctf_field_type_structure_add_field(inner_structure_type, |
| 666 | inner_structure_type, "yes"), "Cannot add self to structure"); |
| 667 | ok(!bt_ctf_field_type_structure_add_field(inner_structure_type, |
| 668 | uint_35_type, "seq_len"), "Add seq_len field to inner structure"); |
| 669 | ok(!bt_ctf_field_type_structure_add_field(inner_structure_type, |
| 670 | sequence_type, "a_sequence"), "Add a_sequence field to inner structure"); |
| 671 | ok(!bt_ctf_field_type_structure_add_field(inner_structure_type, |
| 672 | array_type, "an_array"), "Add an_array field to inner structure"); |
| 673 | |
| 674 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, |
| 675 | "UINT3_TYPE", 0, 0); |
| 676 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, |
| 677 | "INT16_TYPE", 1, 1); |
| 678 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, |
| 679 | "UINT35_TYPE", 2, 7); |
| 680 | |
| 681 | iter = bt_ctf_field_type_enumeration_find_mappings_by_name(NULL, "INT16_TYPE"); |
| 682 | ok(iter == NULL, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly"); |
| 683 | |
| 684 | iter = bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type, "INT16_TYPE"); |
| 685 | ok(iter != NULL, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly"); |
| 686 | ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter, NULL, NULL, NULL) == 0, |
| 687 | "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly"); |
| 688 | BT_PUT(iter); |
| 689 | |
| 690 | iter = bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type, NULL); |
| 691 | ok(iter == NULL, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly"); |
| 692 | |
| 693 | iter = bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL, 1); |
| 694 | ok(iter == NULL, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly"); |
| 695 | |
| 696 | iter = bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type, -42); |
| 697 | ok(iter == NULL, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly"); |
| 698 | ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter, NULL, NULL, NULL) != 0, |
| 699 | "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly"); |
| 700 | BT_PUT(iter); |
| 701 | |
| 702 | iter = bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type, 5); |
| 703 | ok(iter != NULL, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly"); |
| 704 | ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter, NULL, NULL, NULL) == 0, |
| 705 | "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly"); |
| 706 | BT_PUT(iter); |
| 707 | |
| 708 | ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type, |
| 709 | "An unknown entry"), "Reject a variant field based on an unknown tag value"); |
| 710 | ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type, |
| 711 | "UINT3_TYPE") == 0, "Add a field to a variant"); |
| 712 | ok(!bt_ctf_field_type_variant_add_field(variant_type, int_16_type, |
| 713 | "INT16_TYPE"), "Add INT16_TYPE field to variant"); |
| 714 | ok(!bt_ctf_field_type_variant_add_field(variant_type, uint_35_type, |
| 715 | "UINT35_TYPE"), "Add UINT35_TYPE field to variant"); |
| 716 | |
| 717 | ok(bt_ctf_field_type_variant_get_tag_type(NULL) == NULL, |
| 718 | "bt_ctf_field_type_variant_get_tag_type handles NULL correctly"); |
| 719 | ret_field_type = bt_ctf_field_type_variant_get_tag_type(variant_type); |
| 720 | ok(ret_field_type == enum_variant_type, |
| 721 | "bt_ctf_field_type_variant_get_tag_type returns a correct tag type"); |
| 722 | bt_put(ret_field_type); |
| 723 | |
| 724 | ok(bt_ctf_field_type_variant_get_tag_name(NULL) == NULL, |
| 725 | "bt_ctf_field_type_variant_get_tag_name handles NULL correctly"); |
| 726 | ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type); |
| 727 | ok(ret_string ? !strcmp(ret_string, "variant_selector") : 0, |
| 728 | "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name"); |
| 729 | ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL, |
| 730 | "INT16_TYPE") == NULL, |
| 731 | "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly"); |
| 732 | ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type, |
| 733 | NULL) == NULL, |
| 734 | "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly"); |
| 735 | ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name( |
| 736 | variant_type, "INT16_TYPE"); |
| 737 | ok(ret_field_type == int_16_type, |
| 738 | "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type"); |
| 739 | bt_put(ret_field_type); |
| 740 | |
| 741 | ok(bt_ctf_field_type_variant_get_field_count(NULL) < 0, |
| 742 | "bt_ctf_field_type_variant_get_field_count handles NULL correctly"); |
| 743 | ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3, |
| 744 | "bt_ctf_field_type_variant_get_field_count returns the correct count"); |
| 745 | |
| 746 | ok(bt_ctf_field_type_variant_get_field(NULL, &ret_string, &ret_field_type, 0) < 0, |
| 747 | "bt_ctf_field_type_variant_get_field handles a NULL type correctly"); |
| 748 | ok(bt_ctf_field_type_variant_get_field(variant_type, NULL, &ret_field_type, 0) == 0, |
| 749 | "bt_ctf_field_type_variant_get_field handles a NULL field name correctly"); |
| 750 | bt_put(ret_field_type); |
| 751 | ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, NULL, 0) == 0, |
| 752 | "bt_ctf_field_type_variant_get_field handles a NULL field type correctly"); |
| 753 | ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 200) < 0, |
| 754 | "bt_ctf_field_type_variant_get_field handles an invalid index correctly"); |
| 755 | ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 1) == 0, |
| 756 | "bt_ctf_field_type_variant_get_field returns a field"); |
| 757 | ok(!strcmp("INT16_TYPE", ret_string), |
| 758 | "bt_ctf_field_type_variant_get_field returns a correct field name"); |
| 759 | ok(ret_field_type == int_16_type, |
| 760 | "bt_ctf_field_type_variant_get_field returns a correct field type"); |
| 761 | bt_put(ret_field_type); |
| 762 | |
| 763 | ok(!bt_ctf_field_type_structure_add_field(complex_structure_type, |
| 764 | enum_variant_type, "variant_selector"), |
| 765 | "Add variant_selector field to complex structure"); |
| 766 | ok(!bt_ctf_field_type_structure_add_field(complex_structure_type, |
| 767 | string_type, "a_string"), "Add a_string field to complex structure"); |
| 768 | ok(!bt_ctf_field_type_structure_add_field(complex_structure_type, |
| 769 | variant_type, "variant_value"), |
| 770 | "Add variant_value field to complex structure"); |
| 771 | ok(!bt_ctf_field_type_structure_add_field(complex_structure_type, |
| 772 | inner_structure_type, "inner_structure"), |
| 773 | "Add inner_structure field to complex structure"); |
| 774 | |
| 775 | event_class = bt_ctf_event_class_create(complex_test_event_string); |
| 776 | ok(event_class, "Create an event class"); |
| 777 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""), |
| 778 | "Reject addition of a field with an empty name to an event"); |
| 779 | ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"), |
| 780 | "Reject addition of a field with a NULL type to an event"); |
| 781 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, |
| 782 | "int"), |
| 783 | "Reject addition of a type with an illegal name to an event"); |
| 784 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, |
| 785 | "uint_35") == 0, |
| 786 | "Add field of type unsigned integer to an event"); |
| 787 | ok(bt_ctf_event_class_add_field(event_class, int_16_type, |
| 788 | "int_16") == 0, "Add field of type signed integer to an event"); |
| 789 | ok(bt_ctf_event_class_add_field(event_class, complex_structure_type, |
| 790 | "complex_structure") == 0, |
| 791 | "Add composite structure to an event"); |
| 792 | |
| 793 | ok(bt_ctf_event_class_get_name(NULL) == NULL, |
| 794 | "bt_ctf_event_class_get_name handles NULL correctly"); |
| 795 | ret_string = bt_ctf_event_class_get_name(event_class); |
| 796 | ok(!strcmp(ret_string, complex_test_event_string), |
| 797 | "bt_ctf_event_class_get_name returns a correct name"); |
| 798 | ok(bt_ctf_event_class_get_id(event_class) < 0, |
| 799 | "bt_ctf_event_class_get_id returns a negative value when not set"); |
| 800 | ok(bt_ctf_event_class_get_id(NULL) < 0, |
| 801 | "bt_ctf_event_class_get_id handles NULL correctly"); |
| 802 | ok(bt_ctf_event_class_set_id(NULL, 42) < 0, |
| 803 | "bt_ctf_event_class_set_id handles NULL correctly"); |
| 804 | ok(bt_ctf_event_class_set_id(event_class, 42) == 0, |
| 805 | "Set an event class' id"); |
| 806 | ok(bt_ctf_event_class_get_id(event_class) == 42, |
| 807 | "bt_ctf_event_class_get_id returns the correct value"); |
| 808 | |
| 809 | /* Test event class attributes */ |
| 810 | ok(bt_ctf_event_class_get_log_level(event_class) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED, |
| 811 | "event class has the expected initial log level"); |
| 812 | ok(!bt_ctf_event_class_get_emf_uri(event_class), |
| 813 | "as expected, event class has no initial EMF URI"); |
| 814 | ok(bt_ctf_event_class_set_log_level(NULL, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO), |
| 815 | "bt_ctf_event_class_set_log_level handles a NULL event class correctly"); |
| 816 | ok(bt_ctf_event_class_set_log_level(event_class, BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN), |
| 817 | "bt_ctf_event_class_set_log_level handles an unknown log level correctly"); |
| 818 | ok(!bt_ctf_event_class_set_log_level(event_class, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO), |
| 819 | "bt_ctf_event_class_set_log_level succeeds with a valid log level"); |
| 820 | ok(bt_ctf_event_class_get_log_level(NULL) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN, |
| 821 | "bt_ctf_event_class_get_log_level handles a NULL event class correctly"); |
| 822 | ok(bt_ctf_event_class_get_log_level(event_class) == BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO, |
| 823 | "bt_ctf_event_class_get_log_level returns the expected log level"); |
| 824 | ok(bt_ctf_event_class_set_emf_uri(NULL, "http://diamon.org/babeltrace/"), |
| 825 | "bt_ctf_event_class_set_emf_uri handles a NULL event class correctly"); |
| 826 | ok(!bt_ctf_event_class_set_emf_uri(event_class, "http://diamon.org/babeltrace/"), |
| 827 | "bt_ctf_event_class_set_emf_uri succeeds with a valid EMF URI"); |
| 828 | ok(!bt_ctf_event_class_get_emf_uri(NULL), |
| 829 | "bt_ctf_event_class_get_emf_uri handles a NULL event class correctly"); |
| 830 | ok(strcmp(bt_ctf_event_class_get_emf_uri(event_class), "http://diamon.org/babeltrace/") == 0, |
| 831 | "bt_ctf_event_class_get_emf_uri returns the expected EMF URI"); |
| 832 | ok(!bt_ctf_event_class_set_emf_uri(event_class, NULL), |
| 833 | "bt_ctf_event_class_set_emf_uri succeeds with NULL (to reset)"); |
| 834 | ok(!bt_ctf_event_class_get_emf_uri(event_class), |
| 835 | "as expected, event class has no EMF URI after reset"); |
| 836 | |
| 837 | /* Add event class to the stream class */ |
| 838 | ok(bt_ctf_stream_class_add_event_class(stream_class, NULL), |
| 839 | "Reject addition of NULL event class to a stream class"); |
| 840 | ok(bt_ctf_stream_class_add_event_class(stream_class, |
| 841 | event_class) == 0, "Add an event class to stream class"); |
| 842 | |
| 843 | ok(bt_ctf_event_class_get_stream_class(NULL) == NULL, |
| 844 | "bt_ctf_event_class_get_stream_class handles NULL correctly"); |
| 845 | ret_stream_class = bt_ctf_event_class_get_stream_class(event_class); |
| 846 | ok(ret_stream_class == stream_class, |
| 847 | "bt_ctf_event_class_get_stream_class returns the correct stream class"); |
| 848 | bt_put(ret_stream_class); |
| 849 | |
| 850 | ok(bt_ctf_event_class_get_payload_type_field_count(NULL) < 0, |
| 851 | "bt_ctf_event_class_get_field_count handles NULL correctly"); |
| 852 | ok(bt_ctf_event_class_get_payload_type_field_count(event_class) == 3, |
| 853 | "bt_ctf_event_class_get_field_count returns a correct value"); |
| 854 | |
| 855 | ok(bt_ctf_event_class_get_payload_type_field_by_index(NULL, &ret_string, |
| 856 | &ret_field_type, 0) < 0, |
| 857 | "bt_ctf_event_class_get_field handles a NULL event class correctly"); |
| 858 | ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class, NULL, |
| 859 | &ret_field_type, 0) == 0, |
| 860 | "bt_ctf_event_class_get_field handles a NULL field name correctly"); |
| 861 | bt_put(ret_field_type); |
| 862 | ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class, &ret_string, |
| 863 | NULL, 0) == 0, |
| 864 | "bt_ctf_event_class_get_field handles a NULL field type correctly"); |
| 865 | ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class, &ret_string, |
| 866 | &ret_field_type, 42) < 0, |
| 867 | "bt_ctf_event_class_get_field handles an invalid index correctly"); |
| 868 | ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class, &ret_string, |
| 869 | &ret_field_type, 0) == 0, |
| 870 | "bt_ctf_event_class_get_field returns a field"); |
| 871 | ok(bt_ctf_field_type_compare(ret_field_type, uint_35_type) == 0, |
| 872 | "bt_ctf_event_class_get_field returns a correct field type"); |
| 873 | bt_put(ret_field_type); |
| 874 | ok(!strcmp(ret_string, "uint_35"), |
| 875 | "bt_ctf_event_class_get_field returns a correct field name"); |
| 876 | ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL, |
| 877 | "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly"); |
| 878 | ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL, |
| 879 | "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly"); |
| 880 | ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL, |
| 881 | "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly"); |
| 882 | ret_field_type = bt_ctf_event_class_get_field_by_name(event_class, |
| 883 | "complex_structure"); |
| 884 | ok(bt_ctf_field_type_compare(ret_field_type, complex_structure_type) == 0, |
| 885 | "bt_ctf_event_class_get_field_by_name returns a correct field type"); |
| 886 | bt_put(ret_field_type); |
| 887 | |
| 888 | event = bt_ctf_event_create(event_class); |
| 889 | ok(event, "Instanciate a complex event"); |
| 890 | |
| 891 | ok(bt_ctf_event_get_class(NULL) == NULL, |
| 892 | "bt_ctf_event_get_class handles NULL correctly"); |
| 893 | ret_event_class = bt_ctf_event_get_class(event); |
| 894 | ok(ret_event_class == event_class, |
| 895 | "bt_ctf_event_get_class returns the correct event class"); |
| 896 | bt_put(ret_event_class); |
| 897 | |
| 898 | uint_35_field = bt_ctf_event_get_payload(event, "uint_35"); |
| 899 | if (!uint_35_field) { |
| 900 | printf("uint_35_field is NULL\n"); |
| 901 | } |
| 902 | |
| 903 | ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance "); |
| 904 | bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D); |
| 905 | ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0, |
| 906 | "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field."); |
| 907 | ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0, |
| 908 | "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value"); |
| 909 | ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, |
| 910 | &ret_unsigned_int) == 0, |
| 911 | "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value"); |
| 912 | ok(ret_unsigned_int == 0x0DDF00D, |
| 913 | "bt_ctf_field_unsigned_integer_get_value returns the correct value"); |
| 914 | ok(bt_ctf_field_signed_integer_get_value(uint_35_field, |
| 915 | &ret_signed_int) < 0, |
| 916 | "bt_ctf_field_signed_integer_get_value fails on an unsigned field"); |
| 917 | bt_put(uint_35_field); |
| 918 | |
| 919 | int_16_field = bt_ctf_event_get_payload(event, "int_16"); |
| 920 | bt_ctf_field_signed_integer_set_value(int_16_field, -12345); |
| 921 | ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0, |
| 922 | "bt_ctf_field_signed_integer_get_value properly handles a NULL field"); |
| 923 | ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0, |
| 924 | "bt_ctf_field_signed_integer_get_value properly handles a NULL return value"); |
| 925 | ok(bt_ctf_field_signed_integer_get_value(int_16_field, |
| 926 | &ret_signed_int) == 0, |
| 927 | "bt_ctf_field_signed_integer_get_value succeeds after setting a value"); |
| 928 | ok(ret_signed_int == -12345, |
| 929 | "bt_ctf_field_signed_integer_get_value returns the correct value"); |
| 930 | ok(bt_ctf_field_unsigned_integer_get_value(int_16_field, |
| 931 | &ret_unsigned_int) < 0, |
| 932 | "bt_ctf_field_unsigned_integer_get_value fails on a signed field"); |
| 933 | bt_put(int_16_field); |
| 934 | |
| 935 | complex_structure_field = bt_ctf_event_get_payload(event, |
| 936 | "complex_structure"); |
| 937 | |
| 938 | ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL, |
| 939 | "bt_ctf_field_structure_get_field_by_index handles NULL correctly"); |
| 940 | ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL, |
| 941 | "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly"); |
| 942 | inner_structure_field = bt_ctf_field_structure_get_field_by_index( |
| 943 | complex_structure_field, 3); |
| 944 | ret_field_type = bt_ctf_field_get_type(inner_structure_field); |
| 945 | bt_put(inner_structure_field); |
| 946 | ok(bt_ctf_field_type_compare(ret_field_type, inner_structure_type) == 0, |
| 947 | "bt_ctf_field_structure_get_field_by_index returns a correct field"); |
| 948 | bt_put(ret_field_type); |
| 949 | |
| 950 | inner_structure_field = bt_ctf_field_structure_get_field( |
| 951 | complex_structure_field, "inner_structure"); |
| 952 | a_string_field = bt_ctf_field_structure_get_field( |
| 953 | complex_structure_field, "a_string"); |
| 954 | enum_variant_field = bt_ctf_field_structure_get_field( |
| 955 | complex_structure_field, "variant_selector"); |
| 956 | variant_field = bt_ctf_field_structure_get_field( |
| 957 | complex_structure_field, "variant_value"); |
| 958 | uint_35_field = bt_ctf_field_structure_get_field( |
| 959 | inner_structure_field, "seq_len"); |
| 960 | a_sequence_field = bt_ctf_field_structure_get_field( |
| 961 | inner_structure_field, "a_sequence"); |
| 962 | an_array_field = bt_ctf_field_structure_get_field( |
| 963 | inner_structure_field, "an_array"); |
| 964 | |
| 965 | enum_container_field = bt_ctf_field_enumeration_get_container( |
| 966 | enum_variant_field); |
| 967 | bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1); |
| 968 | int_16_field = bt_ctf_field_variant_get_field(variant_field, |
| 969 | enum_variant_field); |
| 970 | bt_ctf_field_signed_integer_set_value(int_16_field, -200); |
| 971 | bt_put(int_16_field); |
| 972 | ok(!bt_ctf_field_string_get_value(a_string_field), |
| 973 | "bt_ctf_field_string_get_value returns NULL on an unset field"); |
| 974 | bt_ctf_field_string_set_value(a_string_field, |
| 975 | test_string_1); |
| 976 | ok(!bt_ctf_field_string_get_value(NULL), |
| 977 | "bt_ctf_field_string_get_value correctly handles NULL"); |
| 978 | ok(bt_ctf_field_string_append(NULL, "yeah"), |
| 979 | "bt_ctf_field_string_append correctly handles a NULL string field"); |
| 980 | ok(bt_ctf_field_string_append(a_string_field, NULL), |
| 981 | "bt_ctf_field_string_append correctly handles a NULL string value"); |
| 982 | ok(!bt_ctf_field_string_append(a_string_field, test_string_2), |
| 983 | "bt_ctf_field_string_append succeeds"); |
| 984 | ok(bt_ctf_field_string_append_len(NULL, "oh noes", 3), |
| 985 | "bt_ctf_field_string_append_len correctly handles a NULL string field"); |
| 986 | ok(bt_ctf_field_string_append_len(a_string_field, NULL, 3), |
| 987 | "bt_ctf_field_string_append_len correctly handles a NULL string value"); |
| 988 | ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 5), |
| 989 | "bt_ctf_field_string_append_len succeeds (append 5 characters)"); |
| 990 | ok(!bt_ctf_field_string_append_len(a_string_field, test_string_4, 10), |
| 991 | "bt_ctf_field_string_append_len succeeds (append 4 characters)"); |
| 992 | ok(!bt_ctf_field_string_append_len(a_string_field, &test_string_4[4], 3), |
| 993 | "bt_ctf_field_string_append_len succeeds (append 0 characters)"); |
| 994 | ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 0), |
| 995 | "bt_ctf_field_string_append_len succeeds (append 0 characters)"); |
| 996 | |
| 997 | ret_string = bt_ctf_field_string_get_value(a_string_field); |
| 998 | ok(ret_string, "bt_ctf_field_string_get_value returns a string"); |
| 999 | ok(ret_string ? !strcmp(ret_string, test_string_cat) : 0, |
| 1000 | "bt_ctf_field_string_get_value returns a correct value"); |
| 1001 | bt_ctf_field_unsigned_integer_set_value(uint_35_field, |
| 1002 | SEQUENCE_TEST_LENGTH); |
| 1003 | |
| 1004 | ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL, |
| 1005 | enum_container_field) == NULL, |
| 1006 | "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly"); |
| 1007 | ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type, |
| 1008 | NULL) == NULL, |
| 1009 | "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly"); |
| 1010 | ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag( |
| 1011 | variant_type, enum_variant_field); |
| 1012 | ok(ret_field_type == int_16_type, |
| 1013 | "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type"); |
| 1014 | |
| 1015 | ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL, |
| 1016 | "bt_ctf_field_sequence_get_length returns NULL when length is unset"); |
| 1017 | ok(bt_ctf_field_sequence_set_length(a_sequence_field, |
| 1018 | uint_35_field) == 0, "Set a sequence field's length"); |
| 1019 | ret_field = bt_ctf_field_sequence_get_length(a_sequence_field); |
| 1020 | ok(ret_field == uint_35_field, |
| 1021 | "bt_ctf_field_sequence_get_length returns the correct length field"); |
| 1022 | ok(bt_ctf_field_sequence_get_length(NULL) == NULL, |
| 1023 | "bt_ctf_field_sequence_get_length properly handles NULL"); |
| 1024 | |
| 1025 | for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) { |
| 1026 | int_16_field = bt_ctf_field_sequence_get_field( |
| 1027 | a_sequence_field, i); |
| 1028 | bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i); |
| 1029 | bt_put(int_16_field); |
| 1030 | } |
| 1031 | |
| 1032 | for (i = 0; i < ARRAY_TEST_LENGTH; i++) { |
| 1033 | int_16_field = bt_ctf_field_array_get_field( |
| 1034 | an_array_field, i); |
| 1035 | bt_ctf_field_signed_integer_set_value(int_16_field, i); |
| 1036 | bt_put(int_16_field); |
| 1037 | } |
| 1038 | |
| 1039 | stream_event_ctx_field = bt_ctf_event_get_stream_event_context(event); |
| 1040 | assert(stream_event_ctx_field); |
| 1041 | stream_event_ctx_int_field = bt_ctf_field_structure_get_field( |
| 1042 | stream_event_ctx_field, "common_event_context"); |
| 1043 | BT_PUT(stream_event_ctx_field); |
| 1044 | bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field, 17); |
| 1045 | BT_PUT(stream_event_ctx_int_field); |
| 1046 | |
| 1047 | bt_ctf_clock_set_time(clock, ++current_time); |
| 1048 | ok(bt_ctf_stream_append_event(stream, event) == 0, |
| 1049 | "Append a complex event to a stream"); |
| 1050 | |
| 1051 | /* |
| 1052 | * Populate the custom packet context field with a dummy value |
| 1053 | * otherwise flush will fail. |
| 1054 | */ |
| 1055 | packet_context = bt_ctf_stream_get_packet_context(stream); |
| 1056 | packet_context_field = bt_ctf_field_structure_get_field(packet_context, |
| 1057 | "custom_packet_context_field"); |
| 1058 | bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1); |
| 1059 | |
| 1060 | ok(bt_ctf_stream_flush(stream) == 0, |
| 1061 | "Flush a stream containing a complex event"); |
| 1062 | |
| 1063 | bt_put(uint_35_field); |
| 1064 | bt_put(a_string_field); |
| 1065 | bt_put(inner_structure_field); |
| 1066 | bt_put(complex_structure_field); |
| 1067 | bt_put(a_sequence_field); |
| 1068 | bt_put(an_array_field); |
| 1069 | bt_put(enum_variant_field); |
| 1070 | bt_put(enum_container_field); |
| 1071 | bt_put(variant_field); |
| 1072 | bt_put(ret_field); |
| 1073 | bt_put(packet_context_field); |
| 1074 | bt_put(packet_context); |
| 1075 | bt_put(uint_35_type); |
| 1076 | bt_put(int_16_type); |
| 1077 | bt_put(string_type); |
| 1078 | bt_put(sequence_type); |
| 1079 | bt_put(array_type); |
| 1080 | bt_put(inner_structure_type); |
| 1081 | bt_put(complex_structure_type); |
| 1082 | bt_put(uint_3_type); |
| 1083 | bt_put(enum_variant_type); |
| 1084 | bt_put(variant_type); |
| 1085 | bt_put(ret_field_type); |
| 1086 | bt_put(event_class); |
| 1087 | bt_put(event); |
| 1088 | } |
| 1089 | |
| 1090 | static |
| 1091 | void field_copy_tests_validate_same_type(struct bt_ctf_field *field, |
| 1092 | struct bt_ctf_field_type *expected_type, const char *name) |
| 1093 | { |
| 1094 | struct bt_ctf_field_type *copy_type; |
| 1095 | |
| 1096 | copy_type = bt_ctf_field_get_type(field); |
| 1097 | ok(copy_type == expected_type, |
| 1098 | "bt_ctf_field_copy does not copy the type (%s)", name); |
| 1099 | bt_put(copy_type); |
| 1100 | } |
| 1101 | |
| 1102 | static |
| 1103 | void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field *field_a, |
| 1104 | struct bt_ctf_field *field_b, const char *name) |
| 1105 | { |
| 1106 | ok(field_a != field_b, |
| 1107 | "bt_ctf_field_copy creates different pointers (%s)", name); |
| 1108 | } |
| 1109 | |
| 1110 | static |
| 1111 | void field_copy_tests() |
| 1112 | { |
| 1113 | struct bt_ctf_field_type *len_type = NULL; |
| 1114 | struct bt_ctf_field_type *fp_type = NULL; |
| 1115 | struct bt_ctf_field_type *s_type = NULL; |
| 1116 | struct bt_ctf_field_type *e_int_type = NULL; |
| 1117 | struct bt_ctf_field_type *e_type = NULL; |
| 1118 | struct bt_ctf_field_type *v_type = NULL; |
| 1119 | struct bt_ctf_field_type *v_label1_type = NULL; |
| 1120 | struct bt_ctf_field_type *v_label1_array_type = NULL; |
| 1121 | struct bt_ctf_field_type *v_label2_type = NULL; |
| 1122 | struct bt_ctf_field_type *v_label2_seq_type = NULL; |
| 1123 | struct bt_ctf_field_type *strct_type = NULL; |
| 1124 | struct bt_ctf_field *len = NULL; |
| 1125 | struct bt_ctf_field *fp = NULL; |
| 1126 | struct bt_ctf_field *s = NULL; |
| 1127 | struct bt_ctf_field *e_int = NULL; |
| 1128 | struct bt_ctf_field *e = NULL; |
| 1129 | struct bt_ctf_field *v = NULL; |
| 1130 | struct bt_ctf_field *v_selected = NULL; |
| 1131 | struct bt_ctf_field *v_selected_cur = NULL; |
| 1132 | struct bt_ctf_field *v_selected_0 = NULL; |
| 1133 | struct bt_ctf_field *v_selected_1 = NULL; |
| 1134 | struct bt_ctf_field *v_selected_2 = NULL; |
| 1135 | struct bt_ctf_field *v_selected_3 = NULL; |
| 1136 | struct bt_ctf_field *v_selected_4 = NULL; |
| 1137 | struct bt_ctf_field *v_selected_5 = NULL; |
| 1138 | struct bt_ctf_field *v_selected_6 = NULL; |
| 1139 | struct bt_ctf_field *a = NULL; |
| 1140 | struct bt_ctf_field *a_0 = NULL; |
| 1141 | struct bt_ctf_field *a_1 = NULL; |
| 1142 | struct bt_ctf_field *a_2 = NULL; |
| 1143 | struct bt_ctf_field *a_3 = NULL; |
| 1144 | struct bt_ctf_field *a_4 = NULL; |
| 1145 | struct bt_ctf_field *strct = NULL; |
| 1146 | struct bt_ctf_field *len_copy = NULL; |
| 1147 | struct bt_ctf_field *fp_copy = NULL; |
| 1148 | struct bt_ctf_field *s_copy = NULL; |
| 1149 | struct bt_ctf_field *e_int_copy = NULL; |
| 1150 | struct bt_ctf_field *e_copy = NULL; |
| 1151 | struct bt_ctf_field *v_copy = NULL; |
| 1152 | struct bt_ctf_field *v_selected_copy = NULL; |
| 1153 | struct bt_ctf_field *v_selected_copy_len = NULL; |
| 1154 | struct bt_ctf_field *v_selected_0_copy = NULL; |
| 1155 | struct bt_ctf_field *v_selected_1_copy = NULL; |
| 1156 | struct bt_ctf_field *v_selected_2_copy = NULL; |
| 1157 | struct bt_ctf_field *v_selected_3_copy = NULL; |
| 1158 | struct bt_ctf_field *v_selected_4_copy = NULL; |
| 1159 | struct bt_ctf_field *v_selected_5_copy = NULL; |
| 1160 | struct bt_ctf_field *v_selected_6_copy = NULL; |
| 1161 | struct bt_ctf_field *a_copy = NULL; |
| 1162 | struct bt_ctf_field *a_0_copy = NULL; |
| 1163 | struct bt_ctf_field *a_1_copy = NULL; |
| 1164 | struct bt_ctf_field *a_2_copy = NULL; |
| 1165 | struct bt_ctf_field *a_3_copy = NULL; |
| 1166 | struct bt_ctf_field *a_4_copy = NULL; |
| 1167 | struct bt_ctf_field *strct_copy = NULL; |
| 1168 | struct bt_ctf_field_type_enumeration_mapping_iterator *e_iter = NULL; |
| 1169 | uint64_t uint64_t_val; |
| 1170 | const char *str_val; |
| 1171 | double double_val; |
| 1172 | int ret; |
| 1173 | |
| 1174 | /* create len type */ |
| 1175 | len_type = bt_ctf_field_type_integer_create(32); |
| 1176 | assert(len_type); |
| 1177 | |
| 1178 | /* create fp type */ |
| 1179 | fp_type = bt_ctf_field_type_floating_point_create(); |
| 1180 | assert(fp_type); |
| 1181 | |
| 1182 | /* create s type */ |
| 1183 | s_type = bt_ctf_field_type_string_create(); |
| 1184 | assert(s_type); |
| 1185 | |
| 1186 | /* create e_int type */ |
| 1187 | e_int_type = bt_ctf_field_type_integer_create(8); |
| 1188 | assert(e_int_type); |
| 1189 | |
| 1190 | /* create e type */ |
| 1191 | e_type = bt_ctf_field_type_enumeration_create(e_int_type); |
| 1192 | assert(e_type); |
| 1193 | ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL1", |
| 1194 | 10, 15); |
| 1195 | assert(!ret); |
| 1196 | ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL2", |
| 1197 | 23, 23); |
| 1198 | assert(!ret); |
| 1199 | |
| 1200 | /* create v_label1 type */ |
| 1201 | v_label1_type = bt_ctf_field_type_string_create(); |
| 1202 | assert(v_label1_type); |
| 1203 | |
| 1204 | /* create v_label1_array type */ |
| 1205 | v_label1_array_type = bt_ctf_field_type_array_create(v_label1_type, 5); |
| 1206 | assert(v_label1_array_type); |
| 1207 | |
| 1208 | /* create v_label2 type */ |
| 1209 | v_label2_type = bt_ctf_field_type_integer_create(16); |
| 1210 | assert(v_label2_type); |
| 1211 | |
| 1212 | /* create v_label2_seq type */ |
| 1213 | v_label2_seq_type = bt_ctf_field_type_sequence_create(v_label2_type, |
| 1214 | "len"); |
| 1215 | assert(v_label2_seq_type); |
| 1216 | |
| 1217 | /* create v type */ |
| 1218 | v_type = bt_ctf_field_type_variant_create(e_type, "e"); |
| 1219 | assert(v_type); |
| 1220 | ret = bt_ctf_field_type_variant_add_field(v_type, v_label1_array_type, |
| 1221 | "LABEL1"); |
| 1222 | assert(!ret); |
| 1223 | ret = bt_ctf_field_type_variant_add_field(v_type, v_label2_seq_type, |
| 1224 | "LABEL2"); |
| 1225 | assert(!ret); |
| 1226 | |
| 1227 | /* create strct type */ |
| 1228 | strct_type = bt_ctf_field_type_structure_create(); |
| 1229 | assert(strct_type); |
| 1230 | ret = bt_ctf_field_type_structure_add_field(strct_type, len_type, |
| 1231 | "len"); |
| 1232 | assert(!ret); |
| 1233 | ret = bt_ctf_field_type_structure_add_field(strct_type, fp_type, "fp"); |
| 1234 | assert(!ret); |
| 1235 | ret = bt_ctf_field_type_structure_add_field(strct_type, s_type, "s"); |
| 1236 | assert(!ret); |
| 1237 | ret = bt_ctf_field_type_structure_add_field(strct_type, e_type, "e"); |
| 1238 | assert(!ret); |
| 1239 | ret = bt_ctf_field_type_structure_add_field(strct_type, v_type, "v"); |
| 1240 | assert(!ret); |
| 1241 | ret = bt_ctf_field_type_structure_add_field(strct_type, |
| 1242 | v_label1_array_type, "a"); |
| 1243 | assert(!ret); |
| 1244 | |
| 1245 | /* create strct */ |
| 1246 | strct = bt_ctf_field_create(strct_type); |
| 1247 | assert(strct); |
| 1248 | |
| 1249 | /* get len field */ |
| 1250 | len = bt_ctf_field_structure_get_field(strct, "len"); |
| 1251 | assert(len); |
| 1252 | |
| 1253 | /* get fp field */ |
| 1254 | fp = bt_ctf_field_structure_get_field(strct, "fp"); |
| 1255 | assert(fp); |
| 1256 | |
| 1257 | /* get s field */ |
| 1258 | s = bt_ctf_field_structure_get_field(strct, "s"); |
| 1259 | assert(s); |
| 1260 | |
| 1261 | /* get e field */ |
| 1262 | e = bt_ctf_field_structure_get_field(strct, "e"); |
| 1263 | assert(e); |
| 1264 | |
| 1265 | /* get e_int (underlying integer) */ |
| 1266 | e_int = bt_ctf_field_enumeration_get_container(e); |
| 1267 | assert(e_int); |
| 1268 | |
| 1269 | /* get v field */ |
| 1270 | v = bt_ctf_field_structure_get_field(strct, "v"); |
| 1271 | assert(v); |
| 1272 | |
| 1273 | /* get a field */ |
| 1274 | a = bt_ctf_field_structure_get_field(strct, "a"); |
| 1275 | assert(a); |
| 1276 | |
| 1277 | /* set len field */ |
| 1278 | ret = bt_ctf_field_unsigned_integer_set_value(len, 7); |
| 1279 | assert(!ret); |
| 1280 | |
| 1281 | /* set fp field */ |
| 1282 | ret = bt_ctf_field_floating_point_set_value(fp, 3.14); |
| 1283 | assert(!ret); |
| 1284 | |
| 1285 | /* set s field */ |
| 1286 | ret = bt_ctf_field_string_set_value(s, "btbt"); |
| 1287 | assert(!ret); |
| 1288 | |
| 1289 | /* set e field (LABEL2) */ |
| 1290 | ret = bt_ctf_field_unsigned_integer_set_value(e_int, 23); |
| 1291 | assert(!ret); |
| 1292 | |
| 1293 | /* set v field */ |
| 1294 | v_selected = bt_ctf_field_variant_get_field(v, e); |
| 1295 | assert(v_selected); |
| 1296 | ok(!bt_ctf_field_variant_get_current_field(NULL), |
| 1297 | "bt_ctf_field_variant_get_current_field handles NULL correctly"); |
| 1298 | v_selected_cur = bt_ctf_field_variant_get_current_field(v); |
| 1299 | ok(v_selected_cur == v_selected, |
| 1300 | "bt_ctf_field_variant_get_current_field returns the current field"); |
| 1301 | bt_put(v_selected_cur); |
| 1302 | |
| 1303 | /* set selected v field */ |
| 1304 | ret = bt_ctf_field_sequence_set_length(v_selected, len); |
| 1305 | assert(!ret); |
| 1306 | v_selected_0 = bt_ctf_field_sequence_get_field(v_selected, 0); |
| 1307 | assert(v_selected_0); |
| 1308 | ret = bt_ctf_field_unsigned_integer_set_value(v_selected_0, 7); |
| 1309 | assert(!ret); |
| 1310 | v_selected_1 = bt_ctf_field_sequence_get_field(v_selected, 1); |
| 1311 | assert(v_selected_1); |
| 1312 | ret = bt_ctf_field_unsigned_integer_set_value(v_selected_1, 6); |
| 1313 | assert(!ret); |
| 1314 | v_selected_2 = bt_ctf_field_sequence_get_field(v_selected, 2); |
| 1315 | assert(v_selected_2); |
| 1316 | ret = bt_ctf_field_unsigned_integer_set_value(v_selected_2, 5); |
| 1317 | assert(!ret); |
| 1318 | v_selected_3 = bt_ctf_field_sequence_get_field(v_selected, 3); |
| 1319 | assert(v_selected_3); |
| 1320 | ret = bt_ctf_field_unsigned_integer_set_value(v_selected_3, 4); |
| 1321 | assert(!ret); |
| 1322 | v_selected_4 = bt_ctf_field_sequence_get_field(v_selected, 4); |
| 1323 | assert(v_selected_4); |
| 1324 | ret = bt_ctf_field_unsigned_integer_set_value(v_selected_4, 3); |
| 1325 | assert(!ret); |
| 1326 | v_selected_5 = bt_ctf_field_sequence_get_field(v_selected, 5); |
| 1327 | assert(v_selected_5); |
| 1328 | ret = bt_ctf_field_unsigned_integer_set_value(v_selected_5, 2); |
| 1329 | assert(!ret); |
| 1330 | v_selected_6 = bt_ctf_field_sequence_get_field(v_selected, 6); |
| 1331 | assert(v_selected_6); |
| 1332 | ret = bt_ctf_field_unsigned_integer_set_value(v_selected_6, 1); |
| 1333 | assert(!ret); |
| 1334 | |
| 1335 | /* set a field */ |
| 1336 | a_0 = bt_ctf_field_array_get_field(a, 0); |
| 1337 | assert(a_0); |
| 1338 | ret = bt_ctf_field_string_set_value(a_0, "a_0"); |
| 1339 | assert(!ret); |
| 1340 | a_1 = bt_ctf_field_array_get_field(a, 1); |
| 1341 | assert(a_1); |
| 1342 | ret = bt_ctf_field_string_set_value(a_1, "a_1"); |
| 1343 | assert(!ret); |
| 1344 | a_2 = bt_ctf_field_array_get_field(a, 2); |
| 1345 | assert(a_2); |
| 1346 | ret = bt_ctf_field_string_set_value(a_2, "a_2"); |
| 1347 | assert(!ret); |
| 1348 | a_3 = bt_ctf_field_array_get_field(a, 3); |
| 1349 | assert(a_3); |
| 1350 | ret = bt_ctf_field_string_set_value(a_3, "a_3"); |
| 1351 | assert(!ret); |
| 1352 | a_4 = bt_ctf_field_array_get_field(a, 4); |
| 1353 | assert(a_4); |
| 1354 | ret = bt_ctf_field_string_set_value(a_4, "a_4"); |
| 1355 | assert(!ret); |
| 1356 | |
| 1357 | /* create copy of strct */ |
| 1358 | ok(!bt_ctf_field_copy(NULL), |
| 1359 | "bt_ctf_field_copy handles NULL correctly"); |
| 1360 | strct_copy = bt_ctf_field_copy(strct); |
| 1361 | ok(strct_copy, |
| 1362 | "bt_ctf_field_copy returns a valid pointer"); |
| 1363 | |
| 1364 | /* get all copied fields */ |
| 1365 | len_copy = bt_ctf_field_structure_get_field(strct_copy, "len"); |
| 1366 | assert(len_copy); |
| 1367 | fp_copy = bt_ctf_field_structure_get_field(strct_copy, "fp"); |
| 1368 | assert(fp_copy); |
| 1369 | s_copy = bt_ctf_field_structure_get_field(strct_copy, "s"); |
| 1370 | assert(s_copy); |
| 1371 | e_copy = bt_ctf_field_structure_get_field(strct_copy, "e"); |
| 1372 | assert(e_copy); |
| 1373 | e_int_copy = bt_ctf_field_enumeration_get_container(e_copy); |
| 1374 | assert(e_int_copy); |
| 1375 | v_copy = bt_ctf_field_structure_get_field(strct_copy, "v"); |
| 1376 | assert(v_copy); |
| 1377 | v_selected_copy = bt_ctf_field_variant_get_field(v_copy, e_copy); |
| 1378 | assert(v_selected_copy); |
| 1379 | v_selected_0_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 0); |
| 1380 | assert(v_selected_0_copy); |
| 1381 | v_selected_1_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 1); |
| 1382 | assert(v_selected_1_copy); |
| 1383 | v_selected_2_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 2); |
| 1384 | assert(v_selected_2_copy); |
| 1385 | v_selected_3_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 3); |
| 1386 | assert(v_selected_3_copy); |
| 1387 | v_selected_4_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 4); |
| 1388 | assert(v_selected_4_copy); |
| 1389 | v_selected_5_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 5); |
| 1390 | assert(v_selected_5_copy); |
| 1391 | v_selected_6_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 6); |
| 1392 | assert(v_selected_6_copy); |
| 1393 | ok(!bt_ctf_field_sequence_get_field(v_selected_copy, 7), |
| 1394 | "sequence field copy is not too large"); |
| 1395 | a_copy = bt_ctf_field_structure_get_field(strct_copy, "a"); |
| 1396 | assert(a_copy); |
| 1397 | a_0_copy = bt_ctf_field_array_get_field(a_copy, 0); |
| 1398 | assert(a_0_copy); |
| 1399 | a_1_copy = bt_ctf_field_array_get_field(a_copy, 1); |
| 1400 | assert(a_1_copy); |
| 1401 | a_2_copy = bt_ctf_field_array_get_field(a_copy, 2); |
| 1402 | assert(a_2_copy); |
| 1403 | a_3_copy = bt_ctf_field_array_get_field(a_copy, 3); |
| 1404 | assert(a_3_copy); |
| 1405 | a_4_copy = bt_ctf_field_array_get_field(a_copy, 4); |
| 1406 | assert(a_4_copy); |
| 1407 | ok(!bt_ctf_field_array_get_field(v_selected_copy, 5), |
| 1408 | "array field copy is not too large"); |
| 1409 | |
| 1410 | /* make sure copied fields are different pointers */ |
| 1411 | field_copy_tests_validate_diff_ptrs(strct_copy, strct, "strct"); |
| 1412 | field_copy_tests_validate_diff_ptrs(len_copy, len, "len"); |
| 1413 | field_copy_tests_validate_diff_ptrs(fp_copy, fp, "fp"); |
| 1414 | field_copy_tests_validate_diff_ptrs(s_copy, s, "s"); |
| 1415 | field_copy_tests_validate_diff_ptrs(e_int_copy, e_int, "e_int"); |
| 1416 | field_copy_tests_validate_diff_ptrs(e_copy, e, "e"); |
| 1417 | field_copy_tests_validate_diff_ptrs(v_copy, v, "v"); |
| 1418 | field_copy_tests_validate_diff_ptrs(v_selected_copy, v_selected, |
| 1419 | "v_selected"); |
| 1420 | field_copy_tests_validate_diff_ptrs(v_selected_0_copy, v_selected_0, |
| 1421 | "v_selected_0"); |
| 1422 | field_copy_tests_validate_diff_ptrs(v_selected_1_copy, v_selected_1, |
| 1423 | "v_selected_1"); |
| 1424 | field_copy_tests_validate_diff_ptrs(v_selected_2_copy, v_selected_2, |
| 1425 | "v_selected_2"); |
| 1426 | field_copy_tests_validate_diff_ptrs(v_selected_3_copy, v_selected_3, |
| 1427 | "v_selected_3"); |
| 1428 | field_copy_tests_validate_diff_ptrs(v_selected_4_copy, v_selected_4, |
| 1429 | "v_selected_4"); |
| 1430 | field_copy_tests_validate_diff_ptrs(v_selected_5_copy, v_selected_5, |
| 1431 | "v_selected_5"); |
| 1432 | field_copy_tests_validate_diff_ptrs(v_selected_6_copy, v_selected_6, |
| 1433 | "v_selected_6"); |
| 1434 | field_copy_tests_validate_diff_ptrs(a_copy, a, "a"); |
| 1435 | field_copy_tests_validate_diff_ptrs(a_0_copy, a_0, "a_0"); |
| 1436 | field_copy_tests_validate_diff_ptrs(a_1_copy, a_1, "a_1"); |
| 1437 | field_copy_tests_validate_diff_ptrs(a_2_copy, a_2, "a_2"); |
| 1438 | field_copy_tests_validate_diff_ptrs(a_3_copy, a_3, "a_3"); |
| 1439 | field_copy_tests_validate_diff_ptrs(a_4_copy, a_4, "a_4"); |
| 1440 | |
| 1441 | /* make sure copied fields share the same types */ |
| 1442 | field_copy_tests_validate_same_type(strct_copy, strct_type, "strct"); |
| 1443 | field_copy_tests_validate_same_type(len_copy, len_type, "len"); |
| 1444 | field_copy_tests_validate_same_type(fp_copy, fp_type, "fp"); |
| 1445 | field_copy_tests_validate_same_type(e_int_copy, e_int_type, "e_int"); |
| 1446 | field_copy_tests_validate_same_type(e_copy, e_type, "e"); |
| 1447 | field_copy_tests_validate_same_type(v_copy, v_type, "v"); |
| 1448 | field_copy_tests_validate_same_type(v_selected_copy, v_label2_seq_type, |
| 1449 | "v_selected"); |
| 1450 | field_copy_tests_validate_same_type(v_selected_0_copy, v_label2_type, |
| 1451 | "v_selected_0"); |
| 1452 | field_copy_tests_validate_same_type(v_selected_1_copy, v_label2_type, |
| 1453 | "v_selected_1"); |
| 1454 | field_copy_tests_validate_same_type(v_selected_2_copy, v_label2_type, |
| 1455 | "v_selected_2"); |
| 1456 | field_copy_tests_validate_same_type(v_selected_3_copy, v_label2_type, |
| 1457 | "v_selected_3"); |
| 1458 | field_copy_tests_validate_same_type(v_selected_4_copy, v_label2_type, |
| 1459 | "v_selected_4"); |
| 1460 | field_copy_tests_validate_same_type(v_selected_5_copy, v_label2_type, |
| 1461 | "v_selected_5"); |
| 1462 | field_copy_tests_validate_same_type(v_selected_6_copy, v_label2_type, |
| 1463 | "v_selected_6"); |
| 1464 | field_copy_tests_validate_same_type(a_copy, v_label1_array_type, "a"); |
| 1465 | field_copy_tests_validate_same_type(a_0_copy, v_label1_type, "a_0"); |
| 1466 | field_copy_tests_validate_same_type(a_1_copy, v_label1_type, "a_1"); |
| 1467 | field_copy_tests_validate_same_type(a_2_copy, v_label1_type, "a_2"); |
| 1468 | field_copy_tests_validate_same_type(a_3_copy, v_label1_type, "a_3"); |
| 1469 | field_copy_tests_validate_same_type(a_4_copy, v_label1_type, "a_4"); |
| 1470 | |
| 1471 | /* validate len copy */ |
| 1472 | ret = bt_ctf_field_unsigned_integer_get_value(len_copy, &uint64_t_val); |
| 1473 | assert(!ret); |
| 1474 | ok(uint64_t_val == 7, |
| 1475 | "bt_ctf_field_copy creates a valid integer field copy"); |
| 1476 | |
| 1477 | /* validate fp copy */ |
| 1478 | ret = bt_ctf_field_floating_point_get_value(fp_copy, &double_val); |
| 1479 | assert(!ret); |
| 1480 | ok(double_val == 3.14, |
| 1481 | "bt_ctf_field_copy creates a valid floating point number field copy"); |
| 1482 | |
| 1483 | /* validate s copy */ |
| 1484 | str_val = bt_ctf_field_string_get_value(s_copy); |
| 1485 | ok(str_val && !strcmp(str_val, "btbt"), |
| 1486 | "bt_ctf_field_copy creates a valid string field copy"); |
| 1487 | |
| 1488 | /* validate e_int copy */ |
| 1489 | ret = bt_ctf_field_unsigned_integer_get_value(e_int_copy, |
| 1490 | &uint64_t_val); |
| 1491 | assert(!ret); |
| 1492 | ok(uint64_t_val == 23, |
| 1493 | "bt_ctf_field_copy creates a valid enum's integer field copy"); |
| 1494 | |
| 1495 | /* validate e copy */ |
| 1496 | e_iter = bt_ctf_field_enumeration_get_mappings(e_copy); |
| 1497 | (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter, |
| 1498 | &str_val, NULL, NULL); |
| 1499 | ok(str_val && !strcmp(str_val, "LABEL2"), |
| 1500 | "bt_ctf_field_copy creates a valid enum field copy"); |
| 1501 | |
| 1502 | /* validate v_selected copy */ |
| 1503 | v_selected_copy_len = bt_ctf_field_sequence_get_length(v_selected); |
| 1504 | assert(v_selected_copy_len); |
| 1505 | ret = bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len, |
| 1506 | &uint64_t_val); |
| 1507 | assert(!ret); |
| 1508 | ok(uint64_t_val == 7, |
| 1509 | "bt_ctf_field_copy creates a sequence field copy with the proper length"); |
| 1510 | bt_put(v_selected_copy_len); |
| 1511 | v_selected_copy_len = NULL; |
| 1512 | |
| 1513 | /* validate v_selected copy fields */ |
| 1514 | ret = bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy, |
| 1515 | &uint64_t_val); |
| 1516 | assert(!ret); |
| 1517 | ok(uint64_t_val == 7, |
| 1518 | "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)"); |
| 1519 | ret = bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy, |
| 1520 | &uint64_t_val); |
| 1521 | assert(!ret); |
| 1522 | ok(uint64_t_val == 6, |
| 1523 | "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)"); |
| 1524 | ret = bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy, |
| 1525 | &uint64_t_val); |
| 1526 | assert(!ret); |
| 1527 | ok(uint64_t_val == 5, |
| 1528 | "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)"); |
| 1529 | ret = bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy, |
| 1530 | &uint64_t_val); |
| 1531 | assert(!ret); |
| 1532 | ok(uint64_t_val == 4, |
| 1533 | "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)"); |
| 1534 | ret = bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy, |
| 1535 | &uint64_t_val); |
| 1536 | assert(!ret); |
| 1537 | ok(uint64_t_val == 3, |
| 1538 | "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)"); |
| 1539 | ret = bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy, |
| 1540 | &uint64_t_val); |
| 1541 | assert(!ret); |
| 1542 | ok(uint64_t_val == 2, |
| 1543 | "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)"); |
| 1544 | ret = bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy, |
| 1545 | &uint64_t_val); |
| 1546 | assert(!ret); |
| 1547 | ok(uint64_t_val == 1, |
| 1548 | "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)"); |
| 1549 | |
| 1550 | /* validate a copy fields */ |
| 1551 | str_val = bt_ctf_field_string_get_value(a_0_copy); |
| 1552 | ok(str_val && !strcmp(str_val, "a_0"), |
| 1553 | "bt_ctf_field_copy creates a valid array field element copy (a_0)"); |
| 1554 | str_val = bt_ctf_field_string_get_value(a_1_copy); |
| 1555 | ok(str_val && !strcmp(str_val, "a_1"), |
| 1556 | "bt_ctf_field_copy creates a valid array field element copy (a_1)"); |
| 1557 | str_val = bt_ctf_field_string_get_value(a_2_copy); |
| 1558 | ok(str_val && !strcmp(str_val, "a_2"), |
| 1559 | "bt_ctf_field_copy creates a valid array field element copy (a_2)"); |
| 1560 | str_val = bt_ctf_field_string_get_value(a_3_copy); |
| 1561 | ok(str_val && !strcmp(str_val, "a_3"), |
| 1562 | "bt_ctf_field_copy creates a valid array field element copy (a_3)"); |
| 1563 | str_val = bt_ctf_field_string_get_value(a_4_copy); |
| 1564 | ok(str_val && !strcmp(str_val, "a_4"), |
| 1565 | "bt_ctf_field_copy creates a valid array field element copy (a_4)"); |
| 1566 | |
| 1567 | /* put everything */ |
| 1568 | bt_put(len_type); |
| 1569 | bt_put(fp_type); |
| 1570 | bt_put(s_type); |
| 1571 | bt_put(e_int_type); |
| 1572 | bt_put(e_type); |
| 1573 | bt_put(v_type); |
| 1574 | bt_put(v_label1_type); |
| 1575 | bt_put(v_label1_array_type); |
| 1576 | bt_put(v_label2_type); |
| 1577 | bt_put(v_label2_seq_type); |
| 1578 | bt_put(strct_type); |
| 1579 | bt_put(len); |
| 1580 | bt_put(fp); |
| 1581 | bt_put(s); |
| 1582 | bt_put(e_int); |
| 1583 | bt_put(e); |
| 1584 | bt_put(v); |
| 1585 | bt_put(v_selected); |
| 1586 | bt_put(v_selected_0); |
| 1587 | bt_put(v_selected_1); |
| 1588 | bt_put(v_selected_2); |
| 1589 | bt_put(v_selected_3); |
| 1590 | bt_put(v_selected_4); |
| 1591 | bt_put(v_selected_5); |
| 1592 | bt_put(v_selected_6); |
| 1593 | bt_put(a); |
| 1594 | bt_put(a_0); |
| 1595 | bt_put(a_1); |
| 1596 | bt_put(a_2); |
| 1597 | bt_put(a_3); |
| 1598 | bt_put(a_4); |
| 1599 | bt_put(strct); |
| 1600 | bt_put(len_copy); |
| 1601 | bt_put(fp_copy); |
| 1602 | bt_put(s_copy); |
| 1603 | bt_put(e_int_copy); |
| 1604 | bt_put(e_copy); |
| 1605 | bt_put(v_copy); |
| 1606 | bt_put(v_selected_copy); |
| 1607 | bt_put(v_selected_0_copy); |
| 1608 | bt_put(v_selected_1_copy); |
| 1609 | bt_put(v_selected_2_copy); |
| 1610 | bt_put(v_selected_3_copy); |
| 1611 | bt_put(v_selected_4_copy); |
| 1612 | bt_put(v_selected_5_copy); |
| 1613 | bt_put(v_selected_6_copy); |
| 1614 | bt_put(a_copy); |
| 1615 | bt_put(a_0_copy); |
| 1616 | bt_put(a_1_copy); |
| 1617 | bt_put(a_2_copy); |
| 1618 | bt_put(a_3_copy); |
| 1619 | bt_put(a_4_copy); |
| 1620 | bt_put(strct_copy); |
| 1621 | bt_put(e_iter); |
| 1622 | } |
| 1623 | |
| 1624 | static |
| 1625 | void type_field_tests() |
| 1626 | { |
| 1627 | struct bt_ctf_field *uint_12; |
| 1628 | struct bt_ctf_field *int_16; |
| 1629 | struct bt_ctf_field *string; |
| 1630 | struct bt_ctf_field *enumeration; |
| 1631 | struct bt_ctf_field_type *composite_structure_type; |
| 1632 | struct bt_ctf_field_type *structure_seq_type; |
| 1633 | struct bt_ctf_field_type *string_type; |
| 1634 | struct bt_ctf_field_type *sequence_type; |
| 1635 | struct bt_ctf_field_type *uint_8_type; |
| 1636 | struct bt_ctf_field_type *int_16_type; |
| 1637 | struct bt_ctf_field_type *uint_12_type = |
| 1638 | bt_ctf_field_type_integer_create(12); |
| 1639 | struct bt_ctf_field_type *enumeration_type; |
| 1640 | struct bt_ctf_field_type *returned_type; |
| 1641 | const char *ret_string; |
| 1642 | |
| 1643 | returned_type = bt_ctf_field_get_type(NULL); |
| 1644 | ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly"); |
| 1645 | |
| 1646 | ok(uint_12_type, "Create an unsigned integer type"); |
| 1647 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, |
| 1648 | BT_CTF_INTEGER_BASE_BINARY) == 0, |
| 1649 | "Set integer type's base as binary"); |
| 1650 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, |
| 1651 | BT_CTF_INTEGER_BASE_DECIMAL) == 0, |
| 1652 | "Set integer type's base as decimal"); |
| 1653 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, |
| 1654 | BT_CTF_INTEGER_BASE_UNKNOWN), |
| 1655 | "Reject integer type's base set as unknown"); |
| 1656 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, |
| 1657 | BT_CTF_INTEGER_BASE_OCTAL) == 0, |
| 1658 | "Set integer type's base as octal"); |
| 1659 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, |
| 1660 | BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0, |
| 1661 | "Set integer type's base as hexadecimal"); |
| 1662 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417), |
| 1663 | "Reject unknown integer base value"); |
| 1664 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0, |
| 1665 | "Set integer type signedness to signed"); |
| 1666 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0, |
| 1667 | "Set integer type signedness to unsigned"); |
| 1668 | ok(bt_ctf_field_type_integer_get_size(NULL) < 0, |
| 1669 | "bt_ctf_field_type_integer_get_size handles NULL correctly"); |
| 1670 | ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12, |
| 1671 | "bt_ctf_field_type_integer_get_size returns a correct value"); |
| 1672 | ok(bt_ctf_field_type_integer_get_signed(NULL) < 0, |
| 1673 | "bt_ctf_field_type_integer_get_signed handles NULL correctly"); |
| 1674 | ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0, |
| 1675 | "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types"); |
| 1676 | |
| 1677 | ok(bt_ctf_field_type_set_byte_order(NULL, |
| 1678 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0, |
| 1679 | "bt_ctf_field_type_set_byte_order handles NULL correctly"); |
| 1680 | ok(bt_ctf_field_type_set_byte_order(uint_12_type, |
| 1681 | (enum bt_ctf_byte_order) 42) < 0, |
| 1682 | "bt_ctf_field_type_set_byte_order rejects invalid values"); |
| 1683 | ok(bt_ctf_field_type_set_byte_order(uint_12_type, |
| 1684 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0, |
| 1685 | "Set an integer's byte order to little endian"); |
| 1686 | ok(bt_ctf_field_type_set_byte_order(uint_12_type, |
| 1687 | BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0, |
| 1688 | "Set an integer's byte order to big endian"); |
| 1689 | ok(bt_ctf_field_type_get_byte_order(uint_12_type) == |
| 1690 | BT_CTF_BYTE_ORDER_BIG_ENDIAN, |
| 1691 | "bt_ctf_field_type_get_byte_order returns a correct value"); |
| 1692 | ok(bt_ctf_field_type_get_byte_order(NULL) == |
| 1693 | BT_CTF_BYTE_ORDER_UNKNOWN, |
| 1694 | "bt_ctf_field_type_get_byte_order handles NULL correctly"); |
| 1695 | |
| 1696 | ok(bt_ctf_field_type_get_type_id(NULL) == |
| 1697 | BT_CTF_FIELD_TYPE_ID_UNKNOWN, |
| 1698 | "bt_ctf_field_type_get_type_id handles NULL correctly"); |
| 1699 | ok(bt_ctf_field_type_get_type_id(uint_12_type) == |
| 1700 | BT_CTF_FIELD_TYPE_ID_INTEGER, |
| 1701 | "bt_ctf_field_type_get_type_id returns a correct value with an integer type"); |
| 1702 | |
| 1703 | ok(bt_ctf_field_type_integer_get_base(NULL) == |
| 1704 | BT_CTF_INTEGER_BASE_UNKNOWN, |
| 1705 | "bt_ctf_field_type_integer_get_base handles NULL correctly"); |
| 1706 | ok(bt_ctf_field_type_integer_get_base(uint_12_type) == |
| 1707 | BT_CTF_INTEGER_BASE_HEXADECIMAL, |
| 1708 | "bt_ctf_field_type_integer_get_base returns a correct value"); |
| 1709 | |
| 1710 | ok(bt_ctf_field_type_integer_set_encoding(NULL, |
| 1711 | BT_CTF_STRING_ENCODING_ASCII) < 0, |
| 1712 | "bt_ctf_field_type_integer_set_encoding handles NULL correctly"); |
| 1713 | ok(bt_ctf_field_type_integer_set_encoding(uint_12_type, |
| 1714 | (enum bt_ctf_string_encoding) 123) < 0, |
| 1715 | "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly"); |
| 1716 | ok(bt_ctf_field_type_integer_set_encoding(uint_12_type, |
| 1717 | BT_CTF_STRING_ENCODING_UTF8) == 0, |
| 1718 | "Set integer type encoding to UTF8"); |
| 1719 | ok(bt_ctf_field_type_integer_get_encoding(NULL) == |
| 1720 | BT_CTF_STRING_ENCODING_UNKNOWN, |
| 1721 | "bt_ctf_field_type_integer_get_encoding handles NULL correctly"); |
| 1722 | ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) == |
| 1723 | BT_CTF_STRING_ENCODING_UTF8, |
| 1724 | "bt_ctf_field_type_integer_get_encoding returns a correct value"); |
| 1725 | |
| 1726 | int_16_type = bt_ctf_field_type_integer_create(16); |
| 1727 | assert(int_16_type); |
| 1728 | ok(!bt_ctf_field_type_integer_set_signed(int_16_type, 1), |
| 1729 | "Set signedness of 16 bit integer to true"); |
| 1730 | ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1, |
| 1731 | "bt_ctf_field_type_integer_get_signed returns a correct value for signed types"); |
| 1732 | uint_8_type = bt_ctf_field_type_integer_create(8); |
| 1733 | sequence_type = |
| 1734 | bt_ctf_field_type_sequence_create(int_16_type, "seq_len"); |
| 1735 | ok(sequence_type, "Create a sequence of int16_t type"); |
| 1736 | ok(bt_ctf_field_type_get_type_id(sequence_type) == |
| 1737 | BT_CTF_FIELD_TYPE_ID_SEQUENCE, |
| 1738 | "bt_ctf_field_type_get_type_id returns a correct value with a sequence type"); |
| 1739 | |
| 1740 | ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL, |
| 1741 | "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly"); |
| 1742 | ret_string = bt_ctf_field_type_sequence_get_length_field_name( |
| 1743 | sequence_type); |
| 1744 | ok(!strcmp(ret_string, "seq_len"), |
| 1745 | "bt_ctf_field_type_sequence_get_length_field_name returns the correct value"); |
| 1746 | ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL, |
| 1747 | "bt_ctf_field_type_sequence_get_element_type handles NULL correctly"); |
| 1748 | returned_type = bt_ctf_field_type_sequence_get_element_type( |
| 1749 | sequence_type); |
| 1750 | ok(returned_type == int_16_type, |
| 1751 | "bt_ctf_field_type_sequence_get_element_type returns the correct type"); |
| 1752 | bt_put(returned_type); |
| 1753 | |
| 1754 | string_type = bt_ctf_field_type_string_create(); |
| 1755 | ok(string_type, "Create a string type"); |
| 1756 | ok(bt_ctf_field_type_string_set_encoding(string_type, |
| 1757 | BT_CTF_STRING_ENCODING_NONE), |
| 1758 | "Reject invalid \"None\" string encoding"); |
| 1759 | ok(bt_ctf_field_type_string_set_encoding(string_type, |
| 1760 | 42), |
| 1761 | "Reject invalid string encoding"); |
| 1762 | ok(bt_ctf_field_type_string_set_encoding(string_type, |
| 1763 | BT_CTF_STRING_ENCODING_ASCII) == 0, |
| 1764 | "Set string encoding to ASCII"); |
| 1765 | |
| 1766 | ok(bt_ctf_field_type_string_get_encoding(NULL) == |
| 1767 | BT_CTF_STRING_ENCODING_UNKNOWN, |
| 1768 | "bt_ctf_field_type_string_get_encoding handles NULL correctly"); |
| 1769 | ok(bt_ctf_field_type_string_get_encoding(string_type) == |
| 1770 | BT_CTF_STRING_ENCODING_ASCII, |
| 1771 | "bt_ctf_field_type_string_get_encoding returns the correct value"); |
| 1772 | |
| 1773 | structure_seq_type = bt_ctf_field_type_structure_create(); |
| 1774 | ok(bt_ctf_field_type_get_type_id(structure_seq_type) == |
| 1775 | BT_CTF_FIELD_TYPE_ID_STRUCT, |
| 1776 | "bt_ctf_field_type_get_type_id returns a correct value with a structure type"); |
| 1777 | ok(structure_seq_type, "Create a structure type"); |
| 1778 | ok(bt_ctf_field_type_structure_add_field(structure_seq_type, |
| 1779 | uint_8_type, "seq_len") == 0, |
| 1780 | "Add a uint8_t type to a structure"); |
| 1781 | ok(bt_ctf_field_type_structure_add_field(structure_seq_type, |
| 1782 | sequence_type, "a_sequence") == 0, |
| 1783 | "Add a sequence type to a structure"); |
| 1784 | |
| 1785 | ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0, |
| 1786 | "bt_ctf_field_type_structure_get_field_count handles NULL correctly"); |
| 1787 | ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2, |
| 1788 | "bt_ctf_field_type_structure_get_field_count returns a correct value"); |
| 1789 | |
| 1790 | ok(bt_ctf_field_type_structure_get_field(NULL, |
| 1791 | &ret_string, &returned_type, 1) < 0, |
| 1792 | "bt_ctf_field_type_structure_get_field handles a NULL type correctly"); |
| 1793 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, |
| 1794 | NULL, &returned_type, 1) == 0, |
| 1795 | "bt_ctf_field_type_structure_get_field handles a NULL name correctly"); |
| 1796 | bt_put(returned_type); |
| 1797 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, |
| 1798 | &ret_string, NULL, 1) == 0, |
| 1799 | "bt_ctf_field_type_structure_get_field handles a NULL return type correctly"); |
| 1800 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, |
| 1801 | &ret_string, &returned_type, 10) < 0, |
| 1802 | "bt_ctf_field_type_structure_get_field handles an invalid index correctly"); |
| 1803 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, |
| 1804 | &ret_string, &returned_type, 1) == 0, |
| 1805 | "bt_ctf_field_type_structure_get_field returns a field"); |
| 1806 | ok(!strcmp(ret_string, "a_sequence"), |
| 1807 | "bt_ctf_field_type_structure_get_field returns a correct field name"); |
| 1808 | ok(returned_type == sequence_type, |
| 1809 | "bt_ctf_field_type_structure_get_field returns a correct field type"); |
| 1810 | bt_put(returned_type); |
| 1811 | |
| 1812 | ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL, |
| 1813 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly"); |
| 1814 | ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL, |
| 1815 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly"); |
| 1816 | returned_type = bt_ctf_field_type_structure_get_field_type_by_name( |
| 1817 | structure_seq_type, "a_sequence"); |
| 1818 | ok(returned_type == sequence_type, |
| 1819 | "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type"); |
| 1820 | bt_put(returned_type); |
| 1821 | |
| 1822 | composite_structure_type = bt_ctf_field_type_structure_create(); |
| 1823 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, |
| 1824 | string_type, "a_string") == 0, |
| 1825 | "Add a string type to a structure"); |
| 1826 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, |
| 1827 | structure_seq_type, "inner_structure") == 0, |
| 1828 | "Add a structure type to a structure"); |
| 1829 | |
| 1830 | ok(bt_ctf_field_type_structure_get_field_type_by_name( |
| 1831 | NULL, "a_sequence") == NULL, |
| 1832 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly"); |
| 1833 | ok(bt_ctf_field_type_structure_get_field_type_by_name( |
| 1834 | structure_seq_type, NULL) == NULL, |
| 1835 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly"); |
| 1836 | returned_type = bt_ctf_field_type_structure_get_field_type_by_name( |
| 1837 | structure_seq_type, "a_sequence"); |
| 1838 | ok(returned_type == sequence_type, |
| 1839 | "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type"); |
| 1840 | bt_put(returned_type); |
| 1841 | |
| 1842 | int_16 = bt_ctf_field_create(int_16_type); |
| 1843 | ok(int_16, "Instanciate a signed 16-bit integer"); |
| 1844 | uint_12 = bt_ctf_field_create(uint_12_type); |
| 1845 | ok(uint_12, "Instanciate an unsigned 12-bit integer"); |
| 1846 | returned_type = bt_ctf_field_get_type(int_16); |
| 1847 | ok(returned_type == int_16_type, |
| 1848 | "bt_ctf_field_get_type returns the correct type"); |
| 1849 | |
| 1850 | /* Can't modify types after instanciating them */ |
| 1851 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, |
| 1852 | BT_CTF_INTEGER_BASE_DECIMAL), |
| 1853 | "Check an integer type' base can't be modified after instanciation"); |
| 1854 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0), |
| 1855 | "Check an integer type's signedness can't be modified after instanciation"); |
| 1856 | |
| 1857 | /* Check signed property is checked */ |
| 1858 | ok(bt_ctf_field_signed_integer_set_value(uint_12, -52), |
| 1859 | "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer"); |
| 1860 | ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42), |
| 1861 | "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer"); |
| 1862 | |
| 1863 | /* Check overflows are properly tested for */ |
| 1864 | ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0, |
| 1865 | "Check -32768 is allowed for a signed 16-bit integer"); |
| 1866 | ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0, |
| 1867 | "Check 32767 is allowed for a signed 16-bit integer"); |
| 1868 | ok(bt_ctf_field_signed_integer_set_value(int_16, 32768), |
| 1869 | "Check 32768 is not allowed for a signed 16-bit integer"); |
| 1870 | ok(bt_ctf_field_signed_integer_set_value(int_16, -32769), |
| 1871 | "Check -32769 is not allowed for a signed 16-bit integer"); |
| 1872 | ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0, |
| 1873 | "Check -42 is allowed for a signed 16-bit integer"); |
| 1874 | |
| 1875 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0, |
| 1876 | "Check 4095 is allowed for an unsigned 12-bit integer"); |
| 1877 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096), |
| 1878 | "Check 4096 is not allowed for a unsigned 12-bit integer"); |
| 1879 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0, |
| 1880 | "Check 0 is allowed for an unsigned 12-bit integer"); |
| 1881 | |
| 1882 | string = bt_ctf_field_create(string_type); |
| 1883 | ok(string, "Instanciate a string field"); |
| 1884 | ok(bt_ctf_field_string_set_value(string, "A value") == 0, |
| 1885 | "Set a string's value"); |
| 1886 | |
| 1887 | enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type); |
| 1888 | ok(enumeration_type, |
| 1889 | "Create an enumeration type with an unsigned 12-bit integer as container"); |
| 1890 | enumeration = bt_ctf_field_create(enumeration_type); |
| 1891 | ok(!enumeration, |
| 1892 | "Check enumeration types are validated before instantiation"); |
| 1893 | |
| 1894 | bt_put(string); |
| 1895 | bt_put(uint_12); |
| 1896 | bt_put(int_16); |
| 1897 | bt_put(enumeration); |
| 1898 | bt_put(composite_structure_type); |
| 1899 | bt_put(structure_seq_type); |
| 1900 | bt_put(string_type); |
| 1901 | bt_put(sequence_type); |
| 1902 | bt_put(uint_8_type); |
| 1903 | bt_put(int_16_type); |
| 1904 | bt_put(uint_12_type); |
| 1905 | bt_put(enumeration_type); |
| 1906 | bt_put(returned_type); |
| 1907 | } |
| 1908 | |
| 1909 | static |
| 1910 | void packet_resize_test(struct bt_ctf_stream_class *stream_class, |
| 1911 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) |
| 1912 | { |
| 1913 | /* |
| 1914 | * Append enough events to force the underlying packet to be resized. |
| 1915 | * Also tests that a new event can be declared after a stream has been |
| 1916 | * instantiated and used/flushed. |
| 1917 | */ |
| 1918 | int ret = 0; |
| 1919 | int i; |
| 1920 | struct bt_ctf_event_class *event_class = bt_ctf_event_class_create( |
| 1921 | "Spammy_Event"); |
| 1922 | struct bt_ctf_field_type *integer_type = |
| 1923 | bt_ctf_field_type_integer_create(17); |
| 1924 | struct bt_ctf_field_type *string_type = |
| 1925 | bt_ctf_field_type_string_create(); |
| 1926 | struct bt_ctf_event *event = NULL; |
| 1927 | struct bt_ctf_field *ret_field = NULL; |
| 1928 | struct bt_ctf_field_type *ret_field_type = NULL; |
| 1929 | uint64_t ret_uint64; |
| 1930 | int events_appended = 0; |
| 1931 | struct bt_ctf_field *packet_context = NULL, |
| 1932 | *packet_context_field = NULL, *stream_event_context = NULL; |
| 1933 | struct bt_ctf_field_type *ep_field_1_type = NULL; |
| 1934 | struct bt_ctf_field_type *ep_a_string_type = NULL; |
| 1935 | struct bt_ctf_field_type *ep_type = NULL; |
| 1936 | |
| 1937 | ret |= bt_ctf_event_class_add_field(event_class, integer_type, |
| 1938 | "field_1"); |
| 1939 | ret |= bt_ctf_event_class_add_field(event_class, string_type, |
| 1940 | "a_string"); |
| 1941 | ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class); |
| 1942 | ok(ret == 0, "Add a new event class to a stream class after writing an event"); |
| 1943 | if (ret) { |
| 1944 | goto end; |
| 1945 | } |
| 1946 | |
| 1947 | /* |
| 1948 | * bt_ctf_stream_class_add_event_class() copies the field types |
| 1949 | * of event_class, so we retrieve the new ones to create the |
| 1950 | * appropriate fields. |
| 1951 | */ |
| 1952 | ep_type = bt_ctf_event_class_get_payload_type(event_class); |
| 1953 | assert(ep_type); |
| 1954 | ep_field_1_type = bt_ctf_field_type_structure_get_field_type_by_name( |
| 1955 | ep_type, "field_1"); |
| 1956 | assert(ep_field_1_type); |
| 1957 | ep_a_string_type = bt_ctf_field_type_structure_get_field_type_by_name( |
| 1958 | ep_type, "a_string"); |
| 1959 | assert(ep_a_string_type); |
| 1960 | |
| 1961 | event = bt_ctf_event_create(event_class); |
| 1962 | ret_field = bt_ctf_event_get_payload_by_index(event, 0); |
| 1963 | ret_field_type = bt_ctf_field_get_type(ret_field); |
| 1964 | ok(bt_ctf_field_type_compare(ret_field_type, integer_type) == 0, |
| 1965 | "bt_ctf_event_get_payload_by_index returns a correct field"); |
| 1966 | bt_put(ret_field_type); |
| 1967 | bt_put(ret_field); |
| 1968 | |
| 1969 | ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL, |
| 1970 | "bt_ctf_event_get_payload_by_index handles NULL correctly"); |
| 1971 | ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL, |
| 1972 | "bt_ctf_event_get_payload_by_index handles an invalid index correctly"); |
| 1973 | bt_put(event); |
| 1974 | |
| 1975 | for (i = 0; i < packet_resize_test_length; i++) { |
| 1976 | event = bt_ctf_event_create(event_class); |
| 1977 | struct bt_ctf_field *integer = |
| 1978 | bt_ctf_field_create(ep_field_1_type); |
| 1979 | struct bt_ctf_field *string = |
| 1980 | bt_ctf_field_create(ep_a_string_type); |
| 1981 | |
| 1982 | ret |= bt_ctf_clock_set_time(clock, ++current_time); |
| 1983 | ret |= bt_ctf_field_unsigned_integer_set_value(integer, i); |
| 1984 | ret |= bt_ctf_event_set_payload(event, "field_1", |
| 1985 | integer); |
| 1986 | bt_put(integer); |
| 1987 | ret |= bt_ctf_field_string_set_value(string, "This is a test"); |
| 1988 | ret |= bt_ctf_event_set_payload(event, "a_string", |
| 1989 | string); |
| 1990 | bt_put(string); |
| 1991 | |
| 1992 | /* Populate stream event context */ |
| 1993 | stream_event_context = |
| 1994 | bt_ctf_event_get_stream_event_context(event); |
| 1995 | integer = bt_ctf_field_structure_get_field(stream_event_context, |
| 1996 | "common_event_context"); |
| 1997 | BT_PUT(stream_event_context); |
| 1998 | ret |= bt_ctf_field_unsigned_integer_set_value(integer, |
| 1999 | i % 42); |
| 2000 | bt_put(integer); |
| 2001 | |
| 2002 | ret |= bt_ctf_stream_append_event(stream, event); |
| 2003 | bt_put(event); |
| 2004 | |
| 2005 | if (ret) { |
| 2006 | break; |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | events_appended = !!(i == packet_resize_test_length); |
| 2011 | ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0, |
| 2012 | "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly"); |
| 2013 | ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0, |
| 2014 | "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly"); |
| 2015 | ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64); |
| 2016 | ok(ret == 0 && ret_uint64 == 0, |
| 2017 | "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded"); |
| 2018 | bt_ctf_stream_append_discarded_events(stream, 1000); |
| 2019 | ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64); |
| 2020 | ok(ret == 0 && ret_uint64 == 1000, |
| 2021 | "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded"); |
| 2022 | |
| 2023 | end: |
| 2024 | ok(events_appended, "Append 100 000 events to a stream"); |
| 2025 | |
| 2026 | /* |
| 2027 | * Populate the custom packet context field with a dummy value |
| 2028 | * otherwise flush will fail. |
| 2029 | */ |
| 2030 | packet_context = bt_ctf_stream_get_packet_context(stream); |
| 2031 | packet_context_field = bt_ctf_field_structure_get_field(packet_context, |
| 2032 | "custom_packet_context_field"); |
| 2033 | bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2); |
| 2034 | |
| 2035 | ok(bt_ctf_stream_flush(stream) == 0, |
| 2036 | "Flush a stream that forces a packet resize"); |
| 2037 | ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64); |
| 2038 | ok(ret == 0 && ret_uint64 == 1000, |
| 2039 | "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush"); |
| 2040 | bt_put(integer_type); |
| 2041 | bt_put(string_type); |
| 2042 | bt_put(packet_context); |
| 2043 | bt_put(packet_context_field); |
| 2044 | bt_put(stream_event_context); |
| 2045 | bt_put(event_class); |
| 2046 | bt_put(ep_field_1_type); |
| 2047 | bt_put(ep_a_string_type); |
| 2048 | bt_put(ep_type); |
| 2049 | } |
| 2050 | |
| 2051 | static |
| 2052 | void test_empty_stream(struct bt_ctf_writer *writer) |
| 2053 | { |
| 2054 | int ret = 0; |
| 2055 | struct bt_ctf_trace *trace = NULL, *ret_trace = NULL; |
| 2056 | struct bt_ctf_stream_class *stream_class = NULL; |
| 2057 | struct bt_ctf_stream *stream = NULL; |
| 2058 | |
| 2059 | trace = bt_ctf_writer_get_trace(writer); |
| 2060 | if (!trace) { |
| 2061 | diag("Failed to get trace from writer"); |
| 2062 | ret = -1; |
| 2063 | goto end; |
| 2064 | } |
| 2065 | |
| 2066 | stream_class = bt_ctf_stream_class_create("empty_stream"); |
| 2067 | if (!stream_class) { |
| 2068 | diag("Failed to create stream class"); |
| 2069 | ret = -1; |
| 2070 | goto end; |
| 2071 | } |
| 2072 | |
| 2073 | ret = bt_ctf_stream_class_set_packet_context_type(stream_class, NULL); |
| 2074 | assert(ret == 0); |
| 2075 | ret = bt_ctf_stream_class_set_event_header_type(stream_class, NULL); |
| 2076 | assert(ret == 0); |
| 2077 | |
| 2078 | ok(bt_ctf_stream_class_get_trace(NULL) == NULL, |
| 2079 | "bt_ctf_stream_class_get_trace handles NULL correctly"); |
| 2080 | ok(bt_ctf_stream_class_get_trace(stream_class) == NULL, |
| 2081 | "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned"); |
| 2082 | |
| 2083 | stream = bt_ctf_writer_create_stream(writer, stream_class); |
| 2084 | if (!stream) { |
| 2085 | diag("Failed to create writer stream"); |
| 2086 | ret = -1; |
| 2087 | goto end; |
| 2088 | } |
| 2089 | |
| 2090 | ret_trace = bt_ctf_stream_class_get_trace(stream_class); |
| 2091 | ok(ret_trace == trace, |
| 2092 | "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created"); |
| 2093 | end: |
| 2094 | ok(ret == 0, |
| 2095 | "Created a stream class with default attributes and an empty stream"); |
| 2096 | bt_put(trace); |
| 2097 | bt_put(ret_trace); |
| 2098 | bt_put(stream); |
| 2099 | bt_put(stream_class); |
| 2100 | } |
| 2101 | |
| 2102 | static |
| 2103 | void test_custom_event_header_stream(struct bt_ctf_writer *writer, |
| 2104 | struct bt_ctf_clock *clock) |
| 2105 | { |
| 2106 | int i, ret; |
| 2107 | struct bt_ctf_stream_class *stream_class = NULL; |
| 2108 | struct bt_ctf_stream *stream = NULL; |
| 2109 | struct bt_ctf_field_type *integer_type = NULL, |
| 2110 | *sequence_type = NULL, *event_header_type = NULL; |
| 2111 | struct bt_ctf_field *integer = NULL, *sequence = NULL, |
| 2112 | *event_header = NULL, *packet_header = NULL; |
| 2113 | struct bt_ctf_event_class *event_class = NULL; |
| 2114 | struct bt_ctf_event *event = NULL; |
| 2115 | |
| 2116 | stream_class = bt_ctf_stream_class_create("custom_event_header_stream"); |
| 2117 | if (!stream_class) { |
| 2118 | fail("Failed to create stream class"); |
| 2119 | goto end; |
| 2120 | } |
| 2121 | |
| 2122 | ret = bt_ctf_stream_class_set_clock(stream_class, clock); |
| 2123 | if (ret) { |
| 2124 | fail("Failed to set stream class clock"); |
| 2125 | goto end; |
| 2126 | } |
| 2127 | |
| 2128 | /* |
| 2129 | * Customize event header to add an "seq_len" integer member |
| 2130 | * which will be used as the length of a sequence in an event of this |
| 2131 | * stream. |
| 2132 | */ |
| 2133 | event_header_type = bt_ctf_stream_class_get_event_header_type( |
| 2134 | stream_class); |
| 2135 | if (!event_header_type) { |
| 2136 | fail("Failed to get event header type"); |
| 2137 | goto end; |
| 2138 | } |
| 2139 | |
| 2140 | integer_type = bt_ctf_field_type_integer_create(13); |
| 2141 | if (!integer_type) { |
| 2142 | fail("Failed to create length integer type"); |
| 2143 | goto end; |
| 2144 | } |
| 2145 | |
| 2146 | ret = bt_ctf_field_type_structure_add_field(event_header_type, |
| 2147 | integer_type, "seq_len"); |
| 2148 | if (ret) { |
| 2149 | fail("Failed to add a new field to stream event header"); |
| 2150 | goto end; |
| 2151 | } |
| 2152 | |
| 2153 | event_class = bt_ctf_event_class_create("sequence_event"); |
| 2154 | if (!event_class) { |
| 2155 | fail("Failed to create event class"); |
| 2156 | goto end; |
| 2157 | } |
| 2158 | |
| 2159 | /* |
| 2160 | * This event's payload will contain a sequence which references |
| 2161 | * stream.event.header.seq_len as its length field. |
| 2162 | */ |
| 2163 | sequence_type = bt_ctf_field_type_sequence_create(integer_type, |
| 2164 | "stream.event.header.seq_len"); |
| 2165 | if (!sequence_type) { |
| 2166 | fail("Failed to create a sequence"); |
| 2167 | goto end; |
| 2168 | } |
| 2169 | |
| 2170 | ret = bt_ctf_event_class_add_field(event_class, sequence_type, |
| 2171 | "some_sequence"); |
| 2172 | if (ret) { |
| 2173 | fail("Failed to add a sequence to an event class"); |
| 2174 | goto end; |
| 2175 | } |
| 2176 | |
| 2177 | ret = bt_ctf_stream_class_add_event_class(stream_class, event_class); |
| 2178 | if (ret) { |
| 2179 | fail("Failed to add event class to stream class"); |
| 2180 | goto end; |
| 2181 | } |
| 2182 | |
| 2183 | stream = bt_ctf_writer_create_stream(writer, stream_class); |
| 2184 | if (!stream) { |
| 2185 | fail("Failed to create stream") |
| 2186 | goto end; |
| 2187 | } |
| 2188 | |
| 2189 | /* |
| 2190 | * We have defined a custom packet header field. We have to populate it |
| 2191 | * explicitly. |
| 2192 | */ |
| 2193 | packet_header = bt_ctf_stream_get_packet_header(stream); |
| 2194 | if (!packet_header) { |
| 2195 | fail("Failed to get stream packet header"); |
| 2196 | goto end; |
| 2197 | } |
| 2198 | |
| 2199 | integer = bt_ctf_field_structure_get_field(packet_header, |
| 2200 | "custom_trace_packet_header_field"); |
| 2201 | if (!integer) { |
| 2202 | fail("Failed to retrieve custom_trace_packet_header_field"); |
| 2203 | goto end; |
| 2204 | } |
| 2205 | |
| 2206 | ret = bt_ctf_field_unsigned_integer_set_value(integer, 3487); |
| 2207 | if (ret) { |
| 2208 | fail("Failed to set custom_trace_packet_header_field value"); |
| 2209 | goto end; |
| 2210 | } |
| 2211 | bt_put(integer); |
| 2212 | |
| 2213 | event = bt_ctf_event_create(event_class); |
| 2214 | if (!event) { |
| 2215 | fail("Failed to create event"); |
| 2216 | goto end; |
| 2217 | } |
| 2218 | |
| 2219 | event_header = bt_ctf_event_get_header(event); |
| 2220 | if (!event_header) { |
| 2221 | fail("Failed to get event header"); |
| 2222 | goto end; |
| 2223 | } |
| 2224 | |
| 2225 | integer = bt_ctf_field_structure_get_field(event_header, |
| 2226 | "seq_len"); |
| 2227 | if (!integer) { |
| 2228 | fail("Failed to get seq_len field from event header"); |
| 2229 | goto end; |
| 2230 | } |
| 2231 | |
| 2232 | ret = bt_ctf_field_unsigned_integer_set_value(integer, 2); |
| 2233 | if (ret) { |
| 2234 | fail("Failed to set seq_len value in event header"); |
| 2235 | goto end; |
| 2236 | } |
| 2237 | |
| 2238 | /* Populate both sequence integer fields */ |
| 2239 | sequence = bt_ctf_event_get_payload(event, "some_sequence"); |
| 2240 | if (!sequence) { |
| 2241 | fail("Failed to retrieve sequence from event"); |
| 2242 | goto end; |
| 2243 | } |
| 2244 | |
| 2245 | ret = bt_ctf_field_sequence_set_length(sequence, integer); |
| 2246 | if (ret) { |
| 2247 | fail("Failed to set sequence length"); |
| 2248 | goto end; |
| 2249 | } |
| 2250 | bt_put(integer); |
| 2251 | |
| 2252 | for (i = 0; i < 2; i++) { |
| 2253 | integer = bt_ctf_field_sequence_get_field(sequence, i); |
| 2254 | if (ret) { |
| 2255 | fail("Failed to retrieve sequence element"); |
| 2256 | goto end; |
| 2257 | } |
| 2258 | |
| 2259 | ret = bt_ctf_field_unsigned_integer_set_value(integer, i); |
| 2260 | if (ret) { |
| 2261 | fail("Failed to set sequence element value"); |
| 2262 | goto end; |
| 2263 | } |
| 2264 | |
| 2265 | bt_put(integer); |
| 2266 | integer = NULL; |
| 2267 | } |
| 2268 | |
| 2269 | ret = bt_ctf_stream_append_event(stream, event); |
| 2270 | if (ret) { |
| 2271 | fail("Failed to append event to stream"); |
| 2272 | goto end; |
| 2273 | } |
| 2274 | |
| 2275 | ret = bt_ctf_stream_flush(stream); |
| 2276 | if (ret) { |
| 2277 | fail("Failed to flush custom_event_header stream"); |
| 2278 | } |
| 2279 | end: |
| 2280 | bt_put(stream); |
| 2281 | bt_put(stream_class); |
| 2282 | bt_put(event_class); |
| 2283 | bt_put(event); |
| 2284 | bt_put(integer); |
| 2285 | bt_put(sequence); |
| 2286 | bt_put(event_header); |
| 2287 | bt_put(packet_header); |
| 2288 | bt_put(sequence_type); |
| 2289 | bt_put(integer_type); |
| 2290 | bt_put(event_header_type); |
| 2291 | } |
| 2292 | |
| 2293 | static |
| 2294 | void test_instanciate_event_before_stream(struct bt_ctf_writer *writer, |
| 2295 | struct bt_ctf_clock *clock) |
| 2296 | { |
| 2297 | int ret = 0; |
| 2298 | struct bt_ctf_stream_class *stream_class = NULL; |
| 2299 | struct bt_ctf_stream *stream = NULL, |
| 2300 | *ret_stream = NULL; |
| 2301 | struct bt_ctf_event_class *event_class = NULL; |
| 2302 | struct bt_ctf_event *event = NULL; |
| 2303 | struct bt_ctf_field_type *integer_type = NULL; |
| 2304 | struct bt_ctf_field *integer = NULL; |
| 2305 | |
| 2306 | stream_class = bt_ctf_stream_class_create("event_before_stream_test"); |
| 2307 | if (!stream_class) { |
| 2308 | diag("Failed to create stream class"); |
| 2309 | ret = -1; |
| 2310 | goto end; |
| 2311 | } |
| 2312 | |
| 2313 | ret = bt_ctf_stream_class_set_clock(stream_class, clock); |
| 2314 | if (ret) { |
| 2315 | diag("Failed to set stream class clock"); |
| 2316 | goto end; |
| 2317 | } |
| 2318 | |
| 2319 | event_class = bt_ctf_event_class_create("some_event_class_name"); |
| 2320 | integer_type = bt_ctf_field_type_integer_create(32); |
| 2321 | if (!integer_type) { |
| 2322 | diag("Failed to create integer field type"); |
| 2323 | ret = -1; |
| 2324 | goto end; |
| 2325 | } |
| 2326 | |
| 2327 | ret = bt_ctf_event_class_add_field(event_class, integer_type, |
| 2328 | "integer_field"); |
| 2329 | if (ret) { |
| 2330 | diag("Failed to add field to event class"); |
| 2331 | goto end; |
| 2332 | } |
| 2333 | |
| 2334 | ret = bt_ctf_stream_class_add_event_class(stream_class, |
| 2335 | event_class); |
| 2336 | if (ret) { |
| 2337 | diag("Failed to add event class to stream class"); |
| 2338 | } |
| 2339 | |
| 2340 | event = bt_ctf_event_create(event_class); |
| 2341 | if (!event) { |
| 2342 | diag("Failed to create event"); |
| 2343 | ret = -1; |
| 2344 | goto end; |
| 2345 | } |
| 2346 | |
| 2347 | integer = bt_ctf_event_get_payload_by_index(event, 0); |
| 2348 | if (!integer) { |
| 2349 | diag("Failed to get integer field payload from event"); |
| 2350 | ret = -1; |
| 2351 | goto end; |
| 2352 | } |
| 2353 | |
| 2354 | ret = bt_ctf_field_unsigned_integer_set_value(integer, 1234); |
| 2355 | if (ret) { |
| 2356 | diag("Failed to set integer field value"); |
| 2357 | goto end; |
| 2358 | } |
| 2359 | |
| 2360 | stream = bt_ctf_writer_create_stream(writer, stream_class); |
| 2361 | if (!stream) { |
| 2362 | diag("Failed to create writer stream"); |
| 2363 | ret = -1; |
| 2364 | goto end; |
| 2365 | } |
| 2366 | |
| 2367 | ok(bt_ctf_event_get_stream(NULL) == NULL, |
| 2368 | "bt_ctf_event_get_stream handles NULL correctly"); |
| 2369 | ok(bt_ctf_event_get_stream(event) == NULL, |
| 2370 | "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream"); |
| 2371 | |
| 2372 | ret = bt_ctf_stream_append_event(stream, event); |
| 2373 | if (ret) { |
| 2374 | diag("Failed to append event to stream"); |
| 2375 | goto end; |
| 2376 | } |
| 2377 | |
| 2378 | ret_stream = bt_ctf_event_get_stream(event); |
| 2379 | ok(ret_stream == stream, |
| 2380 | "bt_ctf_event_get_stream returns an event's stream after it has been appended"); |
| 2381 | end: |
| 2382 | ok(ret == 0, |
| 2383 | "Create an event before instanciating its associated stream"); |
| 2384 | bt_put(stream); |
| 2385 | bt_put(ret_stream); |
| 2386 | bt_put(stream_class); |
| 2387 | bt_put(event_class); |
| 2388 | bt_put(event); |
| 2389 | bt_put(integer_type); |
| 2390 | bt_put(integer); |
| 2391 | } |
| 2392 | |
| 2393 | static |
| 2394 | void append_existing_event_class(struct bt_ctf_stream_class *stream_class) |
| 2395 | { |
| 2396 | struct bt_ctf_event_class *event_class; |
| 2397 | |
| 2398 | event_class = bt_ctf_event_class_create("Simple Event"); |
| 2399 | assert(event_class); |
| 2400 | ok(bt_ctf_stream_class_add_event_class(stream_class, event_class) == 0, |
| 2401 | "two event classes with the same name may cohabit within the same stream class"); |
| 2402 | bt_put(event_class); |
| 2403 | |
| 2404 | event_class = bt_ctf_event_class_create("different name, ok"); |
| 2405 | assert(event_class); |
| 2406 | assert(!bt_ctf_event_class_set_id(event_class, 13)); |
| 2407 | ok(bt_ctf_stream_class_add_event_class(stream_class, event_class), |
| 2408 | "two event classes with the same ID cannot cohabit within the same stream class"); |
| 2409 | bt_put(event_class); |
| 2410 | } |
| 2411 | |
| 2412 | static |
| 2413 | struct bt_ctf_event_class *create_minimal_event_class(void) |
| 2414 | { |
| 2415 | struct bt_ctf_event_class *ec = NULL; |
| 2416 | struct bt_ctf_field_type *int_ft = NULL; |
| 2417 | int ret; |
| 2418 | |
| 2419 | int_ft = bt_ctf_field_type_integer_create(23); |
| 2420 | assert(int_ft); |
| 2421 | ec = bt_ctf_event_class_create("minimal"); |
| 2422 | assert(ec); |
| 2423 | ret = bt_ctf_event_class_add_field(ec, int_ft, "field"); |
| 2424 | assert(!ret); |
| 2425 | BT_PUT(int_ft); |
| 2426 | |
| 2427 | return ec; |
| 2428 | } |
| 2429 | |
| 2430 | static |
| 2431 | void test_create_writer_vs_non_writer_mode(void) |
| 2432 | { |
| 2433 | int ret; |
| 2434 | char trace_path[] = "/tmp/ctfwriter_XXXXXX"; |
| 2435 | const char *writer_stream_name = "writer stream instance"; |
| 2436 | struct bt_ctf_writer *writer = NULL; |
| 2437 | struct bt_ctf_trace *writer_trace = NULL; |
| 2438 | struct bt_ctf_stream_class *writer_sc = NULL; |
| 2439 | struct bt_ctf_stream *writer_stream = NULL; |
| 2440 | struct bt_ctf_stream *writer_stream2 = NULL; |
| 2441 | struct bt_ctf_stream *packet_stream = NULL; |
| 2442 | struct bt_ctf_trace *non_writer_trace = NULL; |
| 2443 | struct bt_ctf_stream_class *non_writer_sc = NULL; |
| 2444 | struct bt_ctf_stream *non_writer_stream = NULL; |
| 2445 | struct bt_ctf_stream *non_writer_stream2 = NULL; |
| 2446 | struct bt_ctf_event_class *writer_ec = NULL; |
| 2447 | struct bt_ctf_event_class *non_writer_ec = NULL; |
| 2448 | struct bt_ctf_event *event = NULL; |
| 2449 | struct bt_ctf_event *event2 = NULL; |
| 2450 | struct bt_ctf_field_type *empty_struct_ft = NULL; |
| 2451 | struct bt_ctf_field *int_field = NULL; |
| 2452 | struct bt_ctf_clock *writer_clock = NULL; |
| 2453 | struct bt_ctf_clock_class *non_writer_clock_class = NULL; |
| 2454 | struct bt_ctf_packet *packet = NULL; |
| 2455 | struct bt_ctf_packet *packet2 = NULL; |
| 2456 | |
| 2457 | if (!bt_mkdtemp(trace_path)) { |
| 2458 | perror("# perror"); |
| 2459 | } |
| 2460 | |
| 2461 | /* Create empty structure field type (event header) */ |
| 2462 | empty_struct_ft = bt_ctf_field_type_structure_create(); |
| 2463 | assert(empty_struct_ft); |
| 2464 | |
| 2465 | /* Create writer, writer stream class, stream, and clock */ |
| 2466 | writer = bt_ctf_writer_create(trace_path); |
| 2467 | assert(writer); |
| 2468 | writer_clock = bt_ctf_clock_create("writer_clock"); |
| 2469 | assert(writer_clock); |
| 2470 | ret = bt_ctf_writer_add_clock(writer, writer_clock); |
| 2471 | assert(!ret); |
| 2472 | ret = bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN); |
| 2473 | assert(!ret); |
| 2474 | writer_trace = bt_ctf_writer_get_trace(writer); |
| 2475 | ok(writer_trace, "bt_ctf_writer_get_trace() returns a trace"); |
| 2476 | writer_sc = bt_ctf_stream_class_create("writer_sc"); |
| 2477 | assert(writer_sc); |
| 2478 | ret = bt_ctf_stream_class_set_event_header_type(writer_sc, |
| 2479 | empty_struct_ft); |
| 2480 | assert(!ret); |
| 2481 | ret = bt_ctf_stream_class_set_clock(writer_sc, writer_clock); |
| 2482 | assert(!ret); |
| 2483 | ret = bt_ctf_trace_add_stream_class(writer_trace, writer_sc); |
| 2484 | assert(!ret); |
| 2485 | writer_stream = bt_ctf_stream_create(writer_sc, writer_stream_name); |
| 2486 | assert(writer_stream); |
| 2487 | ok(!strcmp(bt_ctf_stream_get_name(writer_stream), writer_stream_name), |
| 2488 | "bt_ctf_stream_get_name() returns the stream's name"); |
| 2489 | |
| 2490 | /* Create non-writer trace, stream class, stream, and clock */ |
| 2491 | non_writer_trace = bt_ctf_trace_create(); |
| 2492 | assert(non_writer_trace); |
| 2493 | non_writer_sc = bt_ctf_stream_class_create("nonwriter_sc"); |
| 2494 | assert(non_writer_sc); |
| 2495 | ret = bt_ctf_stream_class_set_event_header_type(non_writer_sc, |
| 2496 | empty_struct_ft); |
| 2497 | assert(!ret); |
| 2498 | ret = bt_ctf_stream_class_set_packet_context_type(non_writer_sc, NULL); |
| 2499 | assert(!ret); |
| 2500 | ret = bt_ctf_trace_add_stream_class(non_writer_trace, non_writer_sc); |
| 2501 | assert(!ret); |
| 2502 | non_writer_stream = bt_ctf_stream_create(non_writer_sc, NULL); |
| 2503 | assert(non_writer_stream); |
| 2504 | non_writer_clock_class = |
| 2505 | bt_ctf_clock_class_create("non_writer_clock_class", |
| 2506 | 1000000000); |
| 2507 | assert(non_writer_clock_class); |
| 2508 | ret = bt_ctf_trace_add_clock_class(non_writer_trace, |
| 2509 | non_writer_clock_class); |
| 2510 | assert(!ret); |
| 2511 | |
| 2512 | /* Create event class and event */ |
| 2513 | writer_ec = create_minimal_event_class(); |
| 2514 | assert(writer_ec); |
| 2515 | ret = bt_ctf_stream_class_add_event_class(writer_sc, writer_ec); |
| 2516 | assert(!ret); |
| 2517 | event = bt_ctf_event_create(writer_ec); |
| 2518 | assert(event); |
| 2519 | int_field = bt_ctf_event_get_payload_by_index(event, 0); |
| 2520 | assert(int_field); |
| 2521 | bt_ctf_field_unsigned_integer_set_value(int_field, 17); |
| 2522 | |
| 2523 | /* |
| 2524 | * Verify non-writer stream: it should be impossible to append |
| 2525 | * an event to it. |
| 2526 | */ |
| 2527 | ok(bt_ctf_stream_append_event(non_writer_stream, event), |
| 2528 | "bt_ctf_stream_append_event() fails with a non-writer stream"); |
| 2529 | |
| 2530 | /* |
| 2531 | * Verify writer stream: it should be possible to append an |
| 2532 | * event to it. |
| 2533 | */ |
| 2534 | ok(!bt_ctf_stream_append_event(writer_stream, event), |
| 2535 | "bt_ctf_stream_append_event() succeeds with a writer stream"); |
| 2536 | |
| 2537 | /* |
| 2538 | * It should be possible to create a packet from a non-writer |
| 2539 | * stream, but not from a writer stream. |
| 2540 | */ |
| 2541 | packet = bt_ctf_packet_create(writer_stream); |
| 2542 | ok(!packet, "bt_ctf_packet_create() fails with a writer stream"); |
| 2543 | packet = bt_ctf_packet_create(non_writer_stream); |
| 2544 | ok(packet, "bt_ctf_packet_create() succeeds with a non-writer stream"); |
| 2545 | packet_stream = bt_ctf_packet_get_stream(packet); |
| 2546 | ok(packet_stream == non_writer_stream, |
| 2547 | "bt_ctf_packet_get_stream() returns the correct stream"); |
| 2548 | |
| 2549 | /* |
| 2550 | * It should not be possible to append an event associated to |
| 2551 | * a stream to a different stream. |
| 2552 | */ |
| 2553 | writer_stream2 = bt_ctf_stream_create(writer_sc, "zoo"); |
| 2554 | assert(writer_stream2); |
| 2555 | ok(bt_ctf_stream_append_event(writer_stream2, event), |
| 2556 | "bt_ctf_stream_append_event() fails with an event associated to another stream"); |
| 2557 | |
| 2558 | /* |
| 2559 | * It should not be possible to set the packet of an event |
| 2560 | * associated to a given stream to a packet associated with |
| 2561 | * a different stream. |
| 2562 | */ |
| 2563 | ok(bt_ctf_event_set_packet(event, packet), |
| 2564 | "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream"); |
| 2565 | |
| 2566 | /* |
| 2567 | * It should be possible to set the packet of a fresh event, as |
| 2568 | * long as the originating stream classes are the same. |
| 2569 | */ |
| 2570 | event2 = bt_ctf_event_create(writer_ec); |
| 2571 | assert(event2); |
| 2572 | ok(bt_ctf_event_set_packet(event2, packet), |
| 2573 | "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ"); |
| 2574 | non_writer_ec = create_minimal_event_class(); |
| 2575 | assert(non_writer_ec); |
| 2576 | ret = bt_ctf_stream_class_add_event_class(non_writer_sc, non_writer_ec); |
| 2577 | assert(!ret); |
| 2578 | BT_PUT(event2); |
| 2579 | event2 = bt_ctf_event_create(non_writer_ec); |
| 2580 | assert(event2); |
| 2581 | ok(!bt_ctf_event_set_packet(event2, packet), |
| 2582 | "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same"); |
| 2583 | |
| 2584 | /* |
| 2585 | * It should be possible to set a packet created from the same |
| 2586 | * stream to an event with an existing packet. |
| 2587 | */ |
| 2588 | packet2 = bt_ctf_packet_create(non_writer_stream); |
| 2589 | assert(packet2); |
| 2590 | ok(!bt_ctf_event_set_packet(event2, packet2), |
| 2591 | "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream"); |
| 2592 | BT_PUT(packet2); |
| 2593 | |
| 2594 | /* |
| 2595 | * It should not be possible to set a packet created from a |
| 2596 | * different stream to an event with an existing packet. |
| 2597 | */ |
| 2598 | non_writer_stream2 = bt_ctf_stream_create(non_writer_sc, "rj45"); |
| 2599 | assert(non_writer_stream2); |
| 2600 | packet2 = bt_ctf_packet_create(non_writer_stream); |
| 2601 | assert(packet2); |
| 2602 | ok(!bt_ctf_event_set_packet(event2, packet2), |
| 2603 | "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream"); |
| 2604 | |
| 2605 | bt_put(writer); |
| 2606 | bt_put(writer_trace); |
| 2607 | bt_put(writer_sc); |
| 2608 | bt_put(writer_stream); |
| 2609 | bt_put(writer_stream2); |
| 2610 | bt_put(non_writer_trace); |
| 2611 | bt_put(non_writer_sc); |
| 2612 | bt_put(non_writer_stream); |
| 2613 | bt_put(non_writer_stream2); |
| 2614 | bt_put(packet_stream); |
| 2615 | bt_put(writer_ec); |
| 2616 | bt_put(non_writer_ec); |
| 2617 | bt_put(event); |
| 2618 | bt_put(event2); |
| 2619 | bt_put(int_field); |
| 2620 | bt_put(empty_struct_ft); |
| 2621 | bt_put(writer_clock); |
| 2622 | bt_put(non_writer_clock_class); |
| 2623 | bt_put(packet); |
| 2624 | bt_put(packet2); |
| 2625 | recursive_rmdir(trace_path); |
| 2626 | } |
| 2627 | |
| 2628 | static |
| 2629 | void test_clock_utils(void) |
| 2630 | { |
| 2631 | int ret; |
| 2632 | struct bt_ctf_clock *clock = NULL; |
| 2633 | |
| 2634 | clock = bt_ctf_clock_create("water"); |
| 2635 | assert(clock); |
| 2636 | ret = bt_ctf_clock_set_offset_s(clock, 1234); |
| 2637 | assert(!ret); |
| 2638 | ret = bt_ctf_clock_set_offset(clock, 1000); |
| 2639 | assert(!ret); |
| 2640 | ret = bt_ctf_clock_set_frequency(clock, 1000000000); |
| 2641 | assert(!ret); |
| 2642 | ret = bt_ctf_clock_set_frequency(clock, 1534); |
| 2643 | assert(!ret); |
| 2644 | |
| 2645 | BT_PUT(clock); |
| 2646 | } |
| 2647 | |
| 2648 | void test_set_clock_non_writer_stream_class(void) |
| 2649 | { |
| 2650 | struct bt_ctf_clock *clock; |
| 2651 | struct bt_ctf_trace *trace; |
| 2652 | struct bt_ctf_stream_class *sc; |
| 2653 | int ret; |
| 2654 | |
| 2655 | clock = bt_ctf_clock_create("the_clock"); |
| 2656 | assert(clock); |
| 2657 | |
| 2658 | trace = bt_ctf_trace_create(); |
| 2659 | assert(trace); |
| 2660 | |
| 2661 | sc = bt_ctf_stream_class_create(NULL); |
| 2662 | assert(sc); |
| 2663 | |
| 2664 | ret = bt_ctf_stream_class_set_clock(sc, clock); |
| 2665 | assert(ret == 0); |
| 2666 | |
| 2667 | ret = bt_ctf_trace_add_stream_class(trace, sc); |
| 2668 | ok(ret < 0, |
| 2669 | "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock"); |
| 2670 | |
| 2671 | bt_put(clock); |
| 2672 | bt_put(trace); |
| 2673 | bt_put(sc); |
| 2674 | } |
| 2675 | |
| 2676 | static |
| 2677 | void test_static_trace(void) |
| 2678 | { |
| 2679 | struct bt_ctf_trace *trace; |
| 2680 | struct bt_ctf_stream_class *stream_class; |
| 2681 | struct bt_ctf_stream_class *stream_class2; |
| 2682 | struct bt_ctf_stream *stream; |
| 2683 | struct bt_ctf_clock_class *clock_class; |
| 2684 | int ret; |
| 2685 | |
| 2686 | trace = bt_ctf_trace_create(); |
| 2687 | assert(trace); |
| 2688 | stream_class = bt_ctf_stream_class_create(NULL); |
| 2689 | assert(stream_class); |
| 2690 | ret = bt_ctf_stream_class_set_packet_context_type(stream_class, NULL); |
| 2691 | assert(ret == 0); |
| 2692 | ret = bt_ctf_trace_add_stream_class(trace, stream_class); |
| 2693 | assert(ret == 0); |
| 2694 | stream = bt_ctf_stream_create(stream_class, "hello"); |
| 2695 | ok(stream, "bt_ctf_stream_create() succeeds with a non-static trace"); |
| 2696 | bt_put(stream); |
| 2697 | ok(!bt_ctf_trace_is_static(trace), |
| 2698 | "bt_ctf_trace_is_static() returns the expected value"); |
| 2699 | ok(bt_ctf_trace_set_is_static(trace) == 0, |
| 2700 | "bt_ctf_trace_set_is_static() succeeds"); |
| 2701 | ok(bt_ctf_trace_is_static(trace), |
| 2702 | "bt_ctf_trace_is_static() returns the expected value"); |
| 2703 | clock_class = bt_ctf_clock_class_create("yes", 1000000000); |
| 2704 | assert(clock_class); |
| 2705 | stream_class2 = bt_ctf_stream_class_create(NULL); |
| 2706 | assert(stream_class2); |
| 2707 | ok(bt_ctf_trace_add_stream_class(trace, stream_class2), |
| 2708 | "bt_ctf_trace_add_stream_class() fails with a static trace"); |
| 2709 | ok(bt_ctf_trace_add_clock_class(trace, clock_class), |
| 2710 | "bt_ctf_trace_add_clock_class() fails with a static trace"); |
| 2711 | ok(!bt_ctf_stream_create(stream_class, "hello2"), |
| 2712 | "bt_ctf_stream_create() fails with a static trace"); |
| 2713 | |
| 2714 | bt_put(trace); |
| 2715 | bt_put(stream_class); |
| 2716 | bt_put(stream_class2); |
| 2717 | bt_put(clock_class); |
| 2718 | } |
| 2719 | |
| 2720 | static |
| 2721 | void trace_is_static_listener(struct bt_ctf_trace *trace, void *data) |
| 2722 | { |
| 2723 | *((int *) data) |= 1; |
| 2724 | } |
| 2725 | |
| 2726 | static |
| 2727 | void trace_listener_removed(struct bt_ctf_trace *trace, void *data) |
| 2728 | { |
| 2729 | *((int *) data) |= 2; |
| 2730 | } |
| 2731 | |
| 2732 | static |
| 2733 | void test_trace_is_static_listener(void) |
| 2734 | { |
| 2735 | struct bt_ctf_trace *trace; |
| 2736 | int ret; |
| 2737 | int called1 = 0; |
| 2738 | int called2 = 0; |
| 2739 | int called3 = 0; |
| 2740 | int called4 = 0; |
| 2741 | int listener1_id; |
| 2742 | int listener2_id; |
| 2743 | int listener3_id; |
| 2744 | int listener4_id; |
| 2745 | |
| 2746 | trace = bt_ctf_trace_create(); |
| 2747 | assert(trace); |
| 2748 | ret = bt_ctf_trace_add_is_static_listener(NULL, |
| 2749 | trace_is_static_listener, trace_listener_removed, &called1); |
| 2750 | ok(ret < 0, "bt_ctf_trace_add_is_static_listener() handles NULL (trace)"); |
| 2751 | ret = bt_ctf_trace_add_is_static_listener(trace, NULL, |
| 2752 | trace_listener_removed, &called1); |
| 2753 | ok(ret < 0, "bt_ctf_trace_add_is_static_listener() handles NULL (listener)"); |
| 2754 | listener1_id = bt_ctf_trace_add_is_static_listener(trace, |
| 2755 | trace_is_static_listener, trace_listener_removed, &called1); |
| 2756 | ok(listener1_id >= 0, "bt_ctf_trace_add_is_static_listener() succeeds (1)"); |
| 2757 | listener2_id = bt_ctf_trace_add_is_static_listener(trace, |
| 2758 | trace_is_static_listener, trace_listener_removed, &called2); |
| 2759 | ok(listener2_id >= 0, "bt_ctf_trace_add_is_static_listener() succeeds (2)"); |
| 2760 | listener3_id = bt_ctf_trace_add_is_static_listener(trace, |
| 2761 | trace_is_static_listener, trace_listener_removed, &called3); |
| 2762 | ok(listener3_id >= 0, "bt_ctf_trace_add_is_static_listener() succeeds (3)"); |
| 2763 | ret = bt_ctf_trace_remove_is_static_listener(NULL, 0); |
| 2764 | ok(ret < 0, "bt_ctf_trace_remove_is_static_listener() handles NULL (trace)"); |
| 2765 | ret = bt_ctf_trace_remove_is_static_listener(trace, -2); |
| 2766 | ok(ret < 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (negative)"); |
| 2767 | ret = bt_ctf_trace_remove_is_static_listener(trace, 77); |
| 2768 | ok(ret < 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (non existing)"); |
| 2769 | ret = bt_ctf_trace_remove_is_static_listener(trace, listener2_id); |
| 2770 | ok(ret == 0, "bt_ctf_trace_remove_is_static_listener() succeeds"); |
| 2771 | ok(called2 == 2, "bt_ctf_trace_remove_is_static_listener() calls the remove listener"); |
| 2772 | listener4_id = bt_ctf_trace_add_is_static_listener(trace, |
| 2773 | trace_is_static_listener, NULL, &called4); |
| 2774 | ok(listener4_id >= 0, "bt_ctf_trace_add_is_static_listener() succeeds (4)"); |
| 2775 | ok(called1 == 0, "\"trace is static\" listener not called before the trace is made static (1)"); |
| 2776 | ok(called2 == 2, "\"trace is static\" listener not called before the trace is made static (2)"); |
| 2777 | ok(called3 == 0, "\"trace is static\" listener not called before the trace is made static (3)"); |
| 2778 | ok(called4 == 0, "\"trace is static\" listener not called before the trace is made static (4)"); |
| 2779 | ret = bt_ctf_trace_set_is_static(trace); |
| 2780 | assert(ret == 0); |
| 2781 | ret = bt_ctf_trace_add_is_static_listener(trace, |
| 2782 | trace_is_static_listener, trace_listener_removed, &called1); |
| 2783 | ok(ret < 0, |
| 2784 | "bt_ctf_trace_add_is_static_listener() fails when the trace is static"); |
| 2785 | ok(called1 == 1, "\"trace is static\" listener called when the trace is made static (1)"); |
| 2786 | ok(called2 == 2, "\"trace is static\" listener not called when the trace is made static (2)"); |
| 2787 | ok(called3 == 1, "\"trace is static\" listener called when the trace is made static (3)"); |
| 2788 | ok(called4 == 1, "\"trace is static\" listener called when the trace is made static (4)"); |
| 2789 | called1 = 0; |
| 2790 | called2 = 0; |
| 2791 | called3 = 0; |
| 2792 | called4 = 0; |
| 2793 | bt_put(trace); |
| 2794 | ok(called1 == 2, "\"trace is static\" listener not called after the trace is put (1)"); |
| 2795 | ok(called2 == 0, "\"trace is static\" listener not called after the trace is put (2)"); |
| 2796 | ok(called3 == 2, "\"trace is static\" listener not called after the trace is put (3)"); |
| 2797 | ok(called4 == 0, "\"trace is static\" listener not called after the trace is put (4)"); |
| 2798 | } |
| 2799 | |
| 2800 | static |
| 2801 | void test_trace_uuid(void) |
| 2802 | { |
| 2803 | struct bt_ctf_trace *trace; |
| 2804 | const unsigned char uuid[] = { |
| 2805 | 0x35, 0x92, 0x63, 0xab, 0xb4, 0xbe, 0x40, 0xb4, |
| 2806 | 0xb2, 0x60, 0xd3, 0xf1, 0x3b, 0xb0, 0xd8, 0x59, |
| 2807 | }; |
| 2808 | const unsigned char *ret_uuid; |
| 2809 | |
| 2810 | trace = bt_ctf_trace_create(); |
| 2811 | assert(trace); |
| 2812 | ok(!bt_ctf_trace_get_uuid(NULL), |
| 2813 | "bt_ctf_trace_get_uuid() handles NULL"); |
| 2814 | ok(!bt_ctf_trace_get_uuid(trace), |
| 2815 | "bt_ctf_trace_get_uuid() returns NULL initially"); |
| 2816 | ok(bt_ctf_trace_set_uuid(NULL, uuid), |
| 2817 | "bt_ctf_trace_set_uuid() handles NULL (trace)"); |
| 2818 | ok(bt_ctf_trace_set_uuid(trace, NULL), |
| 2819 | "bt_ctf_trace_set_uuid() handles NULL (UUID)"); |
| 2820 | ok(bt_ctf_trace_set_uuid(trace, uuid) == 0, |
| 2821 | "bt_ctf_trace_set_uuid() succeeds with a valid UUID"); |
| 2822 | ret_uuid = bt_ctf_trace_get_uuid(trace); |
| 2823 | ok(ret_uuid, "bt_ctf_trace_get_uuid() returns a UUID"); |
| 2824 | assert(ret_uuid); |
| 2825 | ok(memcmp(uuid, ret_uuid, 16) == 0, |
| 2826 | "bt_ctf_trace_get_uuid() returns the expected UUID"); |
| 2827 | |
| 2828 | bt_put(trace); |
| 2829 | } |
| 2830 | |
| 2831 | int main(int argc, char **argv) |
| 2832 | { |
| 2833 | const char *env_resize_length; |
| 2834 | char trace_path[] = "/tmp/ctfwriter_XXXXXX"; |
| 2835 | char metadata_path[sizeof(trace_path) + 9]; |
| 2836 | const char *clock_name = "test_clock"; |
| 2837 | const char *clock_description = "This is a test clock"; |
| 2838 | const char *returned_clock_name; |
| 2839 | const char *returned_clock_description; |
| 2840 | const uint64_t frequency = 1123456789; |
| 2841 | const int64_t offset_s = 13515309; |
| 2842 | const int64_t offset = 1234567; |
| 2843 | int64_t get_offset_s, |
| 2844 | get_offset; |
| 2845 | const uint64_t precision = 10; |
| 2846 | const int is_absolute = 0xFF; |
| 2847 | char *metadata_string; |
| 2848 | struct bt_ctf_writer *writer; |
| 2849 | struct utsname name; |
| 2850 | char hostname[BABELTRACE_HOST_NAME_MAX]; |
| 2851 | struct bt_ctf_clock *clock, *ret_clock; |
| 2852 | struct bt_ctf_clock_class *ret_clock_class; |
| 2853 | struct bt_ctf_stream_class *stream_class, *ret_stream_class; |
| 2854 | struct bt_ctf_stream *stream1; |
| 2855 | struct bt_ctf_stream *stream; |
| 2856 | const char *ret_string; |
| 2857 | const unsigned char *ret_uuid; |
| 2858 | unsigned char tmp_uuid[16] = { 0 }; |
| 2859 | struct bt_ctf_field_type *packet_context_type, |
| 2860 | *packet_context_field_type, |
| 2861 | *packet_header_type, |
| 2862 | *packet_header_field_type, |
| 2863 | *integer_type, |
| 2864 | *stream_event_context_type, |
| 2865 | *ret_field_type, |
| 2866 | *event_header_field_type; |
| 2867 | struct bt_ctf_field *packet_header, *packet_header_field; |
| 2868 | struct bt_ctf_trace *trace; |
| 2869 | int ret; |
| 2870 | int64_t ret_int64_t; |
| 2871 | struct bt_value *obj; |
| 2872 | |
| 2873 | if (argc < 2) { |
| 2874 | printf("Usage: tests-ctf-writer path_to_babeltrace\n"); |
| 2875 | return -1; |
| 2876 | } |
| 2877 | |
| 2878 | env_resize_length = getenv("PACKET_RESIZE_TEST_LENGTH"); |
| 2879 | if (env_resize_length) { |
| 2880 | packet_resize_test_length = |
| 2881 | (unsigned int) atoi(env_resize_length); |
| 2882 | } |
| 2883 | |
| 2884 | plan_tests(NR_TESTS); |
| 2885 | |
| 2886 | if (!bt_mkdtemp(trace_path)) { |
| 2887 | perror("# perror"); |
| 2888 | } |
| 2889 | |
| 2890 | strcpy(metadata_path, trace_path); |
| 2891 | strcat(metadata_path + sizeof(trace_path) - 1, "/metadata"); |
| 2892 | |
| 2893 | writer = bt_ctf_writer_create(trace_path); |
| 2894 | ok(writer, "bt_ctf_create succeeds in creating trace with path"); |
| 2895 | |
| 2896 | ok(!bt_ctf_writer_get_trace(NULL), |
| 2897 | "bt_ctf_writer_get_trace correctly handles NULL"); |
| 2898 | trace = bt_ctf_writer_get_trace(writer); |
| 2899 | ok(bt_ctf_trace_set_native_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE), |
| 2900 | "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_NATIVE"); |
| 2901 | ok(bt_ctf_trace_set_native_byte_order(trace, BT_CTF_BYTE_ORDER_UNSPECIFIED), |
| 2902 | "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_UNSPECIFIED"); |
| 2903 | ok(trace, |
| 2904 | "bt_ctf_writer_get_trace returns a bt_ctf_trace object"); |
| 2905 | ok(bt_ctf_trace_set_native_byte_order(trace, BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0, |
| 2906 | "Set a trace's byte order to big endian"); |
| 2907 | ok(bt_ctf_trace_get_native_byte_order(trace) == BT_CTF_BYTE_ORDER_BIG_ENDIAN, |
| 2908 | "bt_ctf_trace_get_native_byte_order returns a correct endianness"); |
| 2909 | |
| 2910 | /* Add environment context to the trace */ |
| 2911 | ret = gethostname(hostname, sizeof(hostname)); |
| 2912 | if (ret < 0) { |
| 2913 | return ret; |
| 2914 | } |
| 2915 | ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0, |
| 2916 | "Add host (%s) environment field to writer instance", |
| 2917 | hostname); |
| 2918 | ok(bt_ctf_writer_add_environment_field(NULL, "test_field", |
| 2919 | "test_value"), |
| 2920 | "bt_ctf_writer_add_environment_field error with NULL writer"); |
| 2921 | ok(bt_ctf_writer_add_environment_field(writer, NULL, |
| 2922 | "test_value"), |
| 2923 | "bt_ctf_writer_add_environment_field error with NULL field name"); |
| 2924 | ok(bt_ctf_writer_add_environment_field(writer, "test_field", |
| 2925 | NULL), |
| 2926 | "bt_ctf_writer_add_environment_field error with NULL field value"); |
| 2927 | |
| 2928 | /* Test bt_ctf_trace_set_environment_field with an integer object */ |
| 2929 | obj = bt_value_integer_create_init(23); |
| 2930 | assert(obj); |
| 2931 | ok(bt_ctf_trace_set_environment_field(NULL, "test_env_int_obj", obj), |
| 2932 | "bt_ctf_trace_set_environment_field handles a NULL trace correctly"); |
| 2933 | ok(bt_ctf_trace_set_environment_field(trace, NULL, obj), |
| 2934 | "bt_ctf_trace_set_environment_field handles a NULL name correctly"); |
| 2935 | ok(bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", NULL), |
| 2936 | "bt_ctf_trace_set_environment_field handles a NULL value correctly"); |
| 2937 | ok(!bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", obj), |
| 2938 | "bt_ctf_trace_set_environment_field succeeds in adding an integer object"); |
| 2939 | BT_PUT(obj); |
| 2940 | |
| 2941 | /* Test bt_ctf_trace_set_environment_field with a string object */ |
| 2942 | obj = bt_value_string_create_init("the value"); |
| 2943 | assert(obj); |
| 2944 | ok(!bt_ctf_trace_set_environment_field(trace, "test_env_str_obj", obj), |
| 2945 | "bt_ctf_trace_set_environment_field succeeds in adding a string object"); |
| 2946 | BT_PUT(obj); |
| 2947 | |
| 2948 | /* Test bt_ctf_trace_set_environment_field_integer */ |
| 2949 | ok(bt_ctf_trace_set_environment_field_integer(NULL, "test_env_int", |
| 2950 | -194875), |
| 2951 | "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly"); |
| 2952 | ok(bt_ctf_trace_set_environment_field_integer(trace, NULL, -194875), |
| 2953 | "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly"); |
| 2954 | ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int", |
| 2955 | -164973), |
| 2956 | "bt_ctf_trace_set_environment_field_integer succeeds"); |
| 2957 | |
| 2958 | /* Test bt_ctf_trace_set_environment_field_string */ |
| 2959 | ok(bt_ctf_trace_set_environment_field_string(NULL, "test_env_str", |
| 2960 | "yeah"), |
| 2961 | "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly"); |
| 2962 | ok(bt_ctf_trace_set_environment_field_string(trace, NULL, "yeah"), |
| 2963 | "bt_ctf_trace_set_environment_field_string handles a NULL name correctly"); |
| 2964 | ok(bt_ctf_trace_set_environment_field_string(trace, "test_env_str", |
| 2965 | NULL), |
| 2966 | "bt_ctf_trace_set_environment_field_string handles a NULL value correctly"); |
| 2967 | ok(!bt_ctf_trace_set_environment_field_string(trace, "test_env_str", |
| 2968 | "oh yeah"), |
| 2969 | "bt_ctf_trace_set_environment_field_string succeeds"); |
| 2970 | |
| 2971 | /* Test bt_ctf_trace_get_environment_field_count */ |
| 2972 | ok(bt_ctf_trace_get_environment_field_count(NULL) < 0, |
| 2973 | "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly"); |
| 2974 | ok(bt_ctf_trace_get_environment_field_count(trace) == 5, |
| 2975 | "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields"); |
| 2976 | |
| 2977 | /* Test bt_ctf_trace_get_environment_field_name */ |
| 2978 | ok(bt_ctf_trace_get_environment_field_name_by_index(NULL, 0) == NULL, |
| 2979 | "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly"); |
| 2980 | ok(bt_ctf_trace_get_environment_field_name_by_index(trace, 5) == NULL, |
| 2981 | "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)"); |
| 2982 | ret_string = bt_ctf_trace_get_environment_field_name_by_index(trace, 0); |
| 2983 | ok(ret_string && !strcmp(ret_string, "host"), |
| 2984 | "bt_ctf_trace_get_environment_field_name returns a correct field name"); |
| 2985 | ret_string = bt_ctf_trace_get_environment_field_name_by_index(trace, 1); |
| 2986 | ok(ret_string && !strcmp(ret_string, "test_env_int_obj"), |
| 2987 | "bt_ctf_trace_get_environment_field_name returns a correct field name"); |
| 2988 | ret_string = bt_ctf_trace_get_environment_field_name_by_index(trace, 2); |
| 2989 | ok(ret_string && !strcmp(ret_string, "test_env_str_obj"), |
| 2990 | "bt_ctf_trace_get_environment_field_name returns a correct field name"); |
| 2991 | ret_string = bt_ctf_trace_get_environment_field_name_by_index(trace, 3); |
| 2992 | ok(ret_string && !strcmp(ret_string, "test_env_int"), |
| 2993 | "bt_ctf_trace_get_environment_field_name returns a correct field name"); |
| 2994 | ret_string = bt_ctf_trace_get_environment_field_name_by_index(trace, 4); |
| 2995 | ok(ret_string && !strcmp(ret_string, "test_env_str"), |
| 2996 | "bt_ctf_trace_get_environment_field_name returns a correct field name"); |
| 2997 | |
| 2998 | /* Test bt_ctf_trace_get_environment_field_value */ |
| 2999 | ok(bt_ctf_trace_get_environment_field_value_by_index(NULL, 0) == NULL, |
| 3000 | "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly"); |
| 3001 | ok(bt_ctf_trace_get_environment_field_value_by_index(trace, 5) == NULL, |
| 3002 | "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)"); |
| 3003 | obj = bt_ctf_trace_get_environment_field_value_by_index(trace, 1); |
| 3004 | ret = bt_value_integer_get(obj, &ret_int64_t); |
| 3005 | ok(!ret && ret_int64_t == 23, |
| 3006 | "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value"); |
| 3007 | BT_PUT(obj); |
| 3008 | obj = bt_ctf_trace_get_environment_field_value_by_index(trace, 2); |
| 3009 | ret = bt_value_string_get(obj, &ret_string); |
| 3010 | ok(!ret && ret_string && !strcmp(ret_string, "the value"), |
| 3011 | "bt_ctf_trace_get_environment_field_value succeeds in getting a string value"); |
| 3012 | BT_PUT(obj); |
| 3013 | |
| 3014 | /* Test bt_ctf_trace_get_environment_field_value_by_name */ |
| 3015 | ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL, |
| 3016 | "test_env_str"), |
| 3017 | "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly"); |
| 3018 | ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, NULL), |
| 3019 | "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly"); |
| 3020 | ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, "oh oh"), |
| 3021 | "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name"); |
| 3022 | obj = bt_ctf_trace_get_environment_field_value_by_name(trace, |
| 3023 | "test_env_str"); |
| 3024 | ret = bt_value_string_get(obj, &ret_string); |
| 3025 | ok(!ret && ret_string && !strcmp(ret_string, "oh yeah"), |
| 3026 | "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field"); |
| 3027 | BT_PUT(obj); |
| 3028 | |
| 3029 | /* Test environment field replacement */ |
| 3030 | ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int", |
| 3031 | 654321), |
| 3032 | "bt_ctf_trace_set_environment_field_integer succeeds with an existing name"); |
| 3033 | ok(bt_ctf_trace_get_environment_field_count(trace) == 5, |
| 3034 | "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size"); |
| 3035 | obj = bt_ctf_trace_get_environment_field_value_by_index(trace, 3); |
| 3036 | ret = bt_value_integer_get(obj, &ret_int64_t); |
| 3037 | ok(!ret && ret_int64_t == 654321, |
| 3038 | "bt_ctf_trace_get_environment_field_value successfully replaces an existing field"); |
| 3039 | BT_PUT(obj); |
| 3040 | |
| 3041 | /* On Solaris, uname() can return any positive value on success */ |
| 3042 | if (uname(&name) < 0) { |
| 3043 | perror("uname"); |
| 3044 | return -1; |
| 3045 | } |
| 3046 | |
| 3047 | ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname) |
| 3048 | == 0, "Add sysname (%s) environment field to writer instance", |
| 3049 | name.sysname); |
| 3050 | ok(bt_ctf_writer_add_environment_field(writer, "nodename", |
| 3051 | name.nodename) == 0, |
| 3052 | "Add nodename (%s) environment field to writer instance", |
| 3053 | name.nodename); |
| 3054 | ok(bt_ctf_writer_add_environment_field(writer, "release", name.release) |
| 3055 | == 0, "Add release (%s) environment field to writer instance", |
| 3056 | name.release); |
| 3057 | ok(bt_ctf_writer_add_environment_field(writer, "version", name.version) |
| 3058 | == 0, "Add version (%s) environment field to writer instance", |
| 3059 | name.version); |
| 3060 | ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine) |
| 3061 | == 0, "Add machine (%s) environment field to writer istance", |
| 3062 | name.machine); |
| 3063 | |
| 3064 | /* Define a clock and add it to the trace */ |
| 3065 | ok(bt_ctf_clock_create("signed") == NULL, |
| 3066 | "Illegal clock name rejected"); |
| 3067 | clock = bt_ctf_clock_create(clock_name); |
| 3068 | ok(clock, "Clock created sucessfully"); |
| 3069 | returned_clock_name = bt_ctf_clock_get_name(clock); |
| 3070 | ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name"); |
| 3071 | ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0, |
| 3072 | "Returned clock name is valid"); |
| 3073 | |
| 3074 | returned_clock_description = bt_ctf_clock_get_description(clock); |
| 3075 | ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description"); |
| 3076 | ok(bt_ctf_clock_set_description(clock, clock_description) == 0, |
| 3077 | "Clock description set successfully"); |
| 3078 | |
| 3079 | returned_clock_description = bt_ctf_clock_get_description(clock); |
| 3080 | ok(returned_clock_description, |
| 3081 | "bt_ctf_clock_get_description returns a description."); |
| 3082 | ok(returned_clock_description ? |
| 3083 | !strcmp(returned_clock_description, clock_description) : 0, |
| 3084 | "Returned clock description is valid"); |
| 3085 | |
| 3086 | ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ, |
| 3087 | "bt_ctf_clock_get_frequency returns the correct default frequency"); |
| 3088 | ok(bt_ctf_clock_set_frequency(clock, frequency) == 0, |
| 3089 | "Set clock frequency"); |
| 3090 | ok(bt_ctf_clock_get_frequency(clock) == frequency, |
| 3091 | "bt_ctf_clock_get_frequency returns the correct frequency once it is set"); |
| 3092 | |
| 3093 | ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0, |
| 3094 | "bt_ctf_clock_get_offset_s succeeds"); |
| 3095 | ok(get_offset_s == DEFAULT_CLOCK_OFFSET_S, |
| 3096 | "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)"); |
| 3097 | ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0, |
| 3098 | "Set clock offset (seconds)"); |
| 3099 | ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0, |
| 3100 | "bt_ctf_clock_get_offset_s succeeds"); |
| 3101 | ok(get_offset_s == offset_s, |
| 3102 | "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set"); |
| 3103 | |
| 3104 | ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0, |
| 3105 | "bt_ctf_clock_get_offset succeeds"); |
| 3106 | ok(get_offset == DEFAULT_CLOCK_OFFSET, |
| 3107 | "bt_ctf_clock_get_offset returns the correct default offset (in ticks)"); |
| 3108 | ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset"); |
| 3109 | ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0, |
| 3110 | "bt_ctf_clock_get_offset succeeds"); |
| 3111 | ok(get_offset == offset, |
| 3112 | "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set"); |
| 3113 | |
| 3114 | ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION, |
| 3115 | "bt_ctf_clock_get_precision returns the correct default precision"); |
| 3116 | ok(bt_ctf_clock_set_precision(clock, precision) == 0, |
| 3117 | "Set clock precision"); |
| 3118 | ok(bt_ctf_clock_get_precision(clock) == precision, |
| 3119 | "bt_ctf_clock_get_precision returns the correct precision once it is set"); |
| 3120 | |
| 3121 | ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE, |
| 3122 | "bt_ctf_clock_get_precision returns the correct default is_absolute attribute"); |
| 3123 | ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0, |
| 3124 | "Set clock absolute property"); |
| 3125 | ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute, |
| 3126 | "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set"); |
| 3127 | |
| 3128 | ok(bt_ctf_clock_set_time(clock, current_time) == 0, |
| 3129 | "Set clock time"); |
| 3130 | |
| 3131 | ok(!bt_ctf_clock_get_name(NULL), |
| 3132 | "bt_ctf_clock_get_name correctly handles NULL"); |
| 3133 | ok(!bt_ctf_clock_get_description(NULL), |
| 3134 | "bt_ctf_clock_get_description correctly handles NULL"); |
| 3135 | ok(bt_ctf_clock_get_frequency(NULL) == -1ULL, |
| 3136 | "bt_ctf_clock_get_frequency correctly handles NULL"); |
| 3137 | ok(bt_ctf_clock_get_precision(NULL) == -1ULL, |
| 3138 | "bt_ctf_clock_get_precision correctly handles NULL"); |
| 3139 | ok(bt_ctf_clock_get_offset_s(NULL, &get_offset_s) < 0, |
| 3140 | "bt_ctf_clock_get_offset_s correctly handles NULL clock"); |
| 3141 | ok(bt_ctf_clock_get_offset_s(clock, NULL) < 0, |
| 3142 | "bt_ctf_clock_get_offset_s correctly handles NULL output"); |
| 3143 | ok(bt_ctf_clock_get_offset(NULL, &get_offset) < 0, |
| 3144 | "bt_ctf_clock_get_offset correctly handles NULL clock"); |
| 3145 | ok(bt_ctf_clock_get_offset(clock, NULL) < 0, |
| 3146 | "bt_ctf_clock_get_offset correctly handles NULL output"); |
| 3147 | ok(bt_ctf_clock_get_is_absolute(NULL) < 0, |
| 3148 | "bt_ctf_clock_get_is_absolute correctly handles NULL"); |
| 3149 | |
| 3150 | ok(bt_ctf_clock_set_description(NULL, NULL) < 0, |
| 3151 | "bt_ctf_clock_set_description correctly handles NULL clock"); |
| 3152 | ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0, |
| 3153 | "bt_ctf_clock_set_frequency correctly handles NULL clock"); |
| 3154 | ok(bt_ctf_clock_set_precision(NULL, precision) < 0, |
| 3155 | "bt_ctf_clock_get_precision correctly handles NULL clock"); |
| 3156 | ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0, |
| 3157 | "bt_ctf_clock_set_offset_s correctly handles NULL clock"); |
| 3158 | ok(bt_ctf_clock_set_offset(NULL, offset) < 0, |
| 3159 | "bt_ctf_clock_set_offset correctly handles NULL clock"); |
| 3160 | ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0, |
| 3161 | "bt_ctf_clock_set_is_absolute correctly handles NULL clock"); |
| 3162 | ok(bt_ctf_clock_set_time(NULL, current_time) < 0, |
| 3163 | "bt_ctf_clock_set_time correctly handles NULL clock"); |
| 3164 | ok(bt_ctf_clock_get_uuid(NULL) == NULL, |
| 3165 | "bt_ctf_clock_get_uuid correctly handles NULL clock"); |
| 3166 | ret_uuid = bt_ctf_clock_get_uuid(clock); |
| 3167 | ok(ret_uuid, |
| 3168 | "bt_ctf_clock_get_uuid returns a UUID"); |
| 3169 | if (ret_uuid) { |
| 3170 | memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid)); |
| 3171 | /* Slightly modify UUID */ |
| 3172 | tmp_uuid[sizeof(tmp_uuid) - 1]++; |
| 3173 | } |
| 3174 | |
| 3175 | ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0, |
| 3176 | "bt_ctf_clock_set_uuid correctly handles a NULL clock"); |
| 3177 | ok(bt_ctf_clock_set_uuid(clock, NULL) < 0, |
| 3178 | "bt_ctf_clock_set_uuid correctly handles a NULL UUID"); |
| 3179 | ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0, |
| 3180 | "bt_ctf_clock_set_uuid sets a new uuid successfully"); |
| 3181 | ret_uuid = bt_ctf_clock_get_uuid(clock); |
| 3182 | ok(ret_uuid, |
| 3183 | "bt_ctf_clock_get_uuid returns a UUID after setting a new one"); |
| 3184 | ok(uuid_match(ret_uuid, tmp_uuid), |
| 3185 | "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one"); |
| 3186 | |
| 3187 | /* Define a stream class */ |
| 3188 | stream_class = bt_ctf_stream_class_create("test_stream"); |
| 3189 | |
| 3190 | ok(bt_ctf_stream_class_get_name(NULL) == NULL, |
| 3191 | "bt_ctf_stream_class_get_name handles NULL correctly"); |
| 3192 | ret_string = bt_ctf_stream_class_get_name(stream_class); |
| 3193 | ok(ret_string && !strcmp(ret_string, "test_stream"), |
| 3194 | "bt_ctf_stream_class_get_name returns a correct stream class name"); |
| 3195 | |
| 3196 | ok(bt_ctf_stream_class_get_clock(stream_class) == NULL, |
| 3197 | "bt_ctf_stream_class_get_clock returns NULL when a clock was not set"); |
| 3198 | ok(bt_ctf_stream_class_get_clock(NULL) == NULL, |
| 3199 | "bt_ctf_stream_class_get_clock handles NULL correctly"); |
| 3200 | |
| 3201 | ok(stream_class, "Create stream class"); |
| 3202 | ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0, |
| 3203 | "Set a stream class' clock"); |
| 3204 | ret_clock = bt_ctf_stream_class_get_clock(stream_class); |
| 3205 | ok(ret_clock == clock, |
| 3206 | "bt_ctf_stream_class_get_clock returns a correct clock"); |
| 3207 | bt_put(ret_clock); |
| 3208 | |
| 3209 | /* Test the event fields and event types APIs */ |
| 3210 | type_field_tests(); |
| 3211 | |
| 3212 | /* Test fields copying */ |
| 3213 | field_copy_tests(); |
| 3214 | |
| 3215 | ok(bt_ctf_stream_class_get_id(stream_class) < 0, |
| 3216 | "bt_ctf_stream_class_get_id returns an error when no id is set"); |
| 3217 | ok(bt_ctf_stream_class_get_id(NULL) < 0, |
| 3218 | "bt_ctf_stream_class_get_id handles NULL correctly"); |
| 3219 | ok(bt_ctf_stream_class_set_id(NULL, 123) < 0, |
| 3220 | "bt_ctf_stream_class_set_id handles NULL correctly"); |
| 3221 | ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0, |
| 3222 | "Set an stream class' id"); |
| 3223 | ok(bt_ctf_stream_class_get_id(stream_class) == 123, |
| 3224 | "bt_ctf_stream_class_get_id returns the correct value"); |
| 3225 | |
| 3226 | /* Validate default event header fields */ |
| 3227 | ok(bt_ctf_stream_class_get_event_header_type(NULL) == NULL, |
| 3228 | "bt_ctf_stream_class_get_event_header_type handles NULL correctly"); |
| 3229 | ret_field_type = bt_ctf_stream_class_get_event_header_type( |
| 3230 | stream_class); |
| 3231 | ok(ret_field_type, |
| 3232 | "bt_ctf_stream_class_get_event_header_type returns an event header type"); |
| 3233 | ok(bt_ctf_field_type_get_type_id(ret_field_type) == BT_CTF_FIELD_TYPE_ID_STRUCT, |
| 3234 | "Default event header type is a structure"); |
| 3235 | event_header_field_type = |
| 3236 | bt_ctf_field_type_structure_get_field_type_by_name( |
| 3237 | ret_field_type, "id"); |
| 3238 | ok(event_header_field_type, |
| 3239 | "Default event header type contains an \"id\" field"); |
| 3240 | ok(bt_ctf_field_type_get_type_id( |
| 3241 | event_header_field_type) == BT_CTF_FIELD_TYPE_ID_INTEGER, |
| 3242 | "Default event header \"id\" field is an integer"); |
| 3243 | bt_put(event_header_field_type); |
| 3244 | event_header_field_type = |
| 3245 | bt_ctf_field_type_structure_get_field_type_by_name( |
| 3246 | ret_field_type, "timestamp"); |
| 3247 | ok(event_header_field_type, |
| 3248 | "Default event header type contains a \"timestamp\" field"); |
| 3249 | ok(bt_ctf_field_type_get_type_id( |
| 3250 | event_header_field_type) == BT_CTF_FIELD_TYPE_ID_INTEGER, |
| 3251 | "Default event header \"timestamp\" field is an integer"); |
| 3252 | bt_put(event_header_field_type); |
| 3253 | bt_put(ret_field_type); |
| 3254 | |
| 3255 | /* Add a custom trace packet header field */ |
| 3256 | ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL, |
| 3257 | "bt_ctf_trace_get_packet_header_type handles NULL correctly"); |
| 3258 | packet_header_type = bt_ctf_trace_get_packet_header_type(trace); |
| 3259 | ok(packet_header_type, |
| 3260 | "bt_ctf_trace_get_packet_header_type returns a packet header"); |
| 3261 | ok(bt_ctf_field_type_get_type_id(packet_header_type) == BT_CTF_FIELD_TYPE_ID_STRUCT, |
| 3262 | "bt_ctf_trace_get_packet_header_type returns a packet header of type struct"); |
| 3263 | ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name( |
| 3264 | packet_header_type, "magic"); |
| 3265 | ok(ret_field_type, "Default packet header type contains a \"magic\" field"); |
| 3266 | bt_put(ret_field_type); |
| 3267 | ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name( |
| 3268 | packet_header_type, "uuid"); |
| 3269 | ok(ret_field_type, "Default packet header type contains a \"uuid\" field"); |
| 3270 | bt_put(ret_field_type); |
| 3271 | ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name( |
| 3272 | packet_header_type, "stream_id"); |
| 3273 | ok(ret_field_type, "Default packet header type contains a \"stream_id\" field"); |
| 3274 | bt_put(ret_field_type); |
| 3275 | |
| 3276 | packet_header_field_type = bt_ctf_field_type_integer_create(22); |
| 3277 | ok(!bt_ctf_field_type_structure_add_field(packet_header_type, |
| 3278 | packet_header_field_type, "custom_trace_packet_header_field"), |
| 3279 | "Added a custom trace packet header field successfully"); |
| 3280 | |
| 3281 | ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0, |
| 3282 | "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly"); |
| 3283 | ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type), |
| 3284 | "Set a trace packet_header_type successfully"); |
| 3285 | |
| 3286 | ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL, |
| 3287 | "bt_ctf_stream_class_get_packet_context_type handles NULL correctly"); |
| 3288 | |
| 3289 | /* Add a custom field to the stream class' packet context */ |
| 3290 | packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class); |
| 3291 | ok(packet_context_type, |
| 3292 | "bt_ctf_stream_class_get_packet_context_type returns a packet context type."); |
| 3293 | ok(bt_ctf_field_type_get_type_id(packet_context_type) == BT_CTF_FIELD_TYPE_ID_STRUCT, |
| 3294 | "Packet context is a structure"); |
| 3295 | |
| 3296 | ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type), |
| 3297 | "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly"); |
| 3298 | |
| 3299 | integer_type = bt_ctf_field_type_integer_create(32); |
| 3300 | ok(bt_ctf_stream_class_set_packet_context_type(stream_class, |
| 3301 | integer_type) < 0, |
| 3302 | "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure"); |
| 3303 | /* Create a "uint5_t" equivalent custom packet context field */ |
| 3304 | packet_context_field_type = bt_ctf_field_type_integer_create(5); |
| 3305 | |
| 3306 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 3307 | packet_context_field_type, "custom_packet_context_field"); |
| 3308 | ok(ret == 0, "Packet context field added successfully"); |
| 3309 | |
| 3310 | /* Define a stream event context containing a my_integer field. */ |
| 3311 | ok(bt_ctf_stream_class_get_event_context_type(NULL) == NULL, |
| 3312 | "bt_ctf_stream_class_get_event_context_type handles NULL correctly"); |
| 3313 | stream_event_context_type = bt_ctf_field_type_structure_create(); |
| 3314 | bt_ctf_field_type_structure_add_field(stream_event_context_type, |
| 3315 | integer_type, "common_event_context"); |
| 3316 | |
| 3317 | ok(bt_ctf_stream_class_set_event_context_type(NULL, |
| 3318 | stream_event_context_type) < 0, |
| 3319 | "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly"); |
| 3320 | ok(bt_ctf_stream_class_set_event_context_type(stream_class, |
| 3321 | integer_type) < 0, |
| 3322 | "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure"); |
| 3323 | |
| 3324 | ok(bt_ctf_stream_class_set_event_context_type( |
| 3325 | stream_class, stream_event_context_type) == 0, |
| 3326 | "Set a new stream event context type"); |
| 3327 | ret_field_type = bt_ctf_stream_class_get_event_context_type( |
| 3328 | stream_class); |
| 3329 | ok(ret_field_type == stream_event_context_type, |
| 3330 | "bt_ctf_stream_class_get_event_context_type returns the correct field type."); |
| 3331 | bt_put(ret_field_type); |
| 3332 | |
| 3333 | /* Instantiate a stream and append events */ |
| 3334 | ret = bt_ctf_writer_add_clock(writer, clock); |
| 3335 | assert(ret == 0); |
| 3336 | ok(bt_ctf_trace_get_stream_count(trace) == 0, |
| 3337 | "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)"); |
| 3338 | stream1 = bt_ctf_writer_create_stream(writer, stream_class); |
| 3339 | ok(stream1, "Instanciate a stream class from writer"); |
| 3340 | ok(bt_ctf_trace_get_stream_count(trace) == 1, |
| 3341 | "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)"); |
| 3342 | stream = bt_ctf_trace_get_stream_by_index(trace, 0); |
| 3343 | ok(stream == stream1, |
| 3344 | "bt_ctf_trace_get_stream_by_index() succeeds and returns the correct value"); |
| 3345 | BT_PUT(stream); |
| 3346 | |
| 3347 | /* |
| 3348 | * Creating a stream through a writer adds the given stream |
| 3349 | * class to the writer's trace, thus registering the stream |
| 3350 | * class's clock to the trace. |
| 3351 | */ |
| 3352 | ok(bt_ctf_trace_get_clock_class_count(NULL) < 0, |
| 3353 | "bt_ctf_trace_get_clock_class_count correctly handles NULL"); |
| 3354 | ok(bt_ctf_trace_get_clock_class_count(trace) == 1, |
| 3355 | "bt_ctf_trace_get_clock_class_count returns the correct number of clocks"); |
| 3356 | ok(!bt_ctf_trace_get_clock_class_by_index(NULL, 0), |
| 3357 | "bt_ctf_trace_get_clock_class correctly handles NULL"); |
| 3358 | ok(!bt_ctf_trace_get_clock_class_by_index(trace, 1), |
| 3359 | "bt_ctf_trace_get_clock_class correctly handles out of bound accesses"); |
| 3360 | ret_clock_class = bt_ctf_trace_get_clock_class_by_index(trace, 0); |
| 3361 | ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class), |
| 3362 | bt_ctf_clock_get_name(clock)) == 0, |
| 3363 | "bt_ctf_trace_get_clock_class returns the right clock instance"); |
| 3364 | bt_put(ret_clock_class); |
| 3365 | ok(!bt_ctf_trace_get_clock_class_by_name(trace, NULL), |
| 3366 | "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)"); |
| 3367 | ok(!bt_ctf_trace_get_clock_class_by_name(NULL, clock_name), |
| 3368 | "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)"); |
| 3369 | ok(!bt_ctf_trace_get_clock_class_by_name(NULL, NULL), |
| 3370 | "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)"); |
| 3371 | ret_clock_class = bt_ctf_trace_get_clock_class_by_name(trace, clock_name); |
| 3372 | ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class), |
| 3373 | bt_ctf_clock_get_name(clock)) == 0, |
| 3374 | "bt_ctf_trace_get_clock_class returns the right clock instance"); |
| 3375 | bt_put(ret_clock_class); |
| 3376 | ok(!bt_ctf_trace_get_clock_class_by_name(trace, "random"), |
| 3377 | "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist"); |
| 3378 | |
| 3379 | ok(bt_ctf_stream_get_class(NULL) == NULL, |
| 3380 | "bt_ctf_stream_get_class correctly handles NULL"); |
| 3381 | ret_stream_class = bt_ctf_stream_get_class(stream1); |
| 3382 | ok(ret_stream_class, |
| 3383 | "bt_ctf_stream_get_class returns a stream class"); |
| 3384 | ok(ret_stream_class == stream_class, |
| 3385 | "Returned stream class is of the correct type"); |
| 3386 | |
| 3387 | /* |
| 3388 | * Packet header, packet context, event header, and stream |
| 3389 | * event context types were copied for the resolving |
| 3390 | * process |
| 3391 | */ |
| 3392 | BT_PUT(packet_header_type); |
| 3393 | BT_PUT(packet_context_type); |
| 3394 | BT_PUT(stream_event_context_type); |
| 3395 | packet_header_type = bt_ctf_trace_get_packet_header_type(trace); |
| 3396 | assert(packet_header_type); |
| 3397 | packet_context_type = |
| 3398 | bt_ctf_stream_class_get_packet_context_type(stream_class); |
| 3399 | assert(packet_context_type); |
| 3400 | stream_event_context_type = |
| 3401 | bt_ctf_stream_class_get_event_context_type(stream_class); |
| 3402 | assert(stream_event_context_type); |
| 3403 | |
| 3404 | /* |
| 3405 | * Try to modify the packet context type after a stream has been |
| 3406 | * created. |
| 3407 | */ |
| 3408 | ret = bt_ctf_field_type_structure_add_field(packet_header_type, |
| 3409 | packet_header_field_type, "should_fail"); |
| 3410 | ok(ret < 0, |
| 3411 | "Trace packet header type can't be modified once a stream has been instanciated"); |
| 3412 | |
| 3413 | /* |
| 3414 | * Try to modify the packet context type after a stream has been |
| 3415 | * created. |
| 3416 | */ |
| 3417 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
| 3418 | packet_context_field_type, "should_fail"); |
| 3419 | ok(ret < 0, |
| 3420 | "Packet context type can't be modified once a stream has been instanciated"); |
| 3421 | |
| 3422 | /* |
| 3423 | * Try to modify the stream event context type after a stream has been |
| 3424 | * created. |
| 3425 | */ |
| 3426 | ret = bt_ctf_field_type_structure_add_field(stream_event_context_type, |
| 3427 | integer_type, "should_fail"); |
| 3428 | ok(ret < 0, |
| 3429 | "Stream event context type can't be modified once a stream has been instanciated"); |
| 3430 | |
| 3431 | /* Should fail after instanciating a stream (frozen) */ |
| 3432 | ok(bt_ctf_stream_class_set_clock(stream_class, clock), |
| 3433 | "Changes to a stream class that was already instantiated fail"); |
| 3434 | |
| 3435 | /* Populate the custom packet header field only once for all tests */ |
| 3436 | ok(bt_ctf_stream_get_packet_header(NULL) == NULL, |
| 3437 | "bt_ctf_stream_get_packet_header handles NULL correctly"); |
| 3438 | packet_header = bt_ctf_stream_get_packet_header(stream1); |
| 3439 | ok(packet_header, |
| 3440 | "bt_ctf_stream_get_packet_header returns a packet header"); |
| 3441 | ret_field_type = bt_ctf_field_get_type(packet_header); |
| 3442 | ok(ret_field_type == packet_header_type, |
| 3443 | "Stream returns a packet header of the appropriate type"); |
| 3444 | bt_put(ret_field_type); |
| 3445 | packet_header_field = bt_ctf_field_structure_get_field(packet_header, |
| 3446 | "custom_trace_packet_header_field"); |
| 3447 | ok(packet_header_field, |
| 3448 | "Packet header structure contains a custom field with the appropriate name"); |
| 3449 | ret_field_type = bt_ctf_field_get_type(packet_header_field); |
| 3450 | ok(bt_ctf_field_type_compare(ret_field_type, packet_header_field_type) == 0, |
| 3451 | "Custom packet header field is of the expected type"); |
| 3452 | ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field, |
| 3453 | 54321), "Set custom packet header value successfully"); |
| 3454 | ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0, |
| 3455 | "bt_ctf_stream_set_packet_header handles a NULL packet header correctly"); |
| 3456 | ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0, |
| 3457 | "bt_ctf_stream_set_packet_header handles a NULL stream correctly"); |
| 3458 | ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0, |
| 3459 | "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type"); |
| 3460 | ok(!bt_ctf_stream_set_packet_header(stream1, packet_header), |
| 3461 | "Successfully set a stream's packet header"); |
| 3462 | |
| 3463 | ok(bt_ctf_writer_add_environment_field(writer, "new_field", "test") == 0, |
| 3464 | "Add environment field to writer after stream creation"); |
| 3465 | |
| 3466 | test_clock_utils(); |
| 3467 | |
| 3468 | test_create_writer_vs_non_writer_mode(); |
| 3469 | |
| 3470 | test_set_clock_non_writer_stream_class(); |
| 3471 | |
| 3472 | test_instanciate_event_before_stream(writer, clock); |
| 3473 | |
| 3474 | append_simple_event(stream_class, stream1, clock); |
| 3475 | |
| 3476 | packet_resize_test(stream_class, stream1, clock); |
| 3477 | |
| 3478 | append_complex_event(stream_class, stream1, clock); |
| 3479 | |
| 3480 | append_existing_event_class(stream_class); |
| 3481 | |
| 3482 | test_empty_stream(writer); |
| 3483 | |
| 3484 | test_custom_event_header_stream(writer, clock); |
| 3485 | |
| 3486 | test_static_trace(); |
| 3487 | |
| 3488 | test_trace_is_static_listener(); |
| 3489 | |
| 3490 | test_trace_uuid(); |
| 3491 | |
| 3492 | metadata_string = bt_ctf_writer_get_metadata_string(writer); |
| 3493 | ok(metadata_string, "Get metadata string"); |
| 3494 | |
| 3495 | bt_ctf_writer_flush_metadata(writer); |
| 3496 | |
| 3497 | bt_put(clock); |
| 3498 | bt_put(ret_stream_class); |
| 3499 | bt_put(writer); |
| 3500 | bt_put(stream1); |
| 3501 | bt_put(packet_context_type); |
| 3502 | bt_put(packet_context_field_type); |
| 3503 | bt_put(integer_type); |
| 3504 | bt_put(stream_event_context_type); |
| 3505 | bt_put(ret_field_type); |
| 3506 | bt_put(packet_header_type); |
| 3507 | bt_put(packet_header_field_type); |
| 3508 | bt_put(packet_header); |
| 3509 | bt_put(packet_header_field); |
| 3510 | bt_put(trace); |
| 3511 | free(metadata_string); |
| 3512 | bt_put(stream_class); |
| 3513 | |
| 3514 | validate_trace(argv[1], trace_path); |
| 3515 | |
| 3516 | //recursive_rmdir(trace_path); |
| 3517 | return 0; |
| 3518 | } |