6 * Copyright 2013 - 2015 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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.
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.
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-ir/stream-class.h>
29 #include <babeltrace/ref.h>
30 #include <babeltrace/ctf/events.h>
31 #include <babeltrace/values.h>
33 #include <babeltrace/compat/stdlib.h>
35 #include <sys/utsname.h>
36 #include <babeltrace/compat/limits.h>
37 #include <babeltrace/compat/stdio.h>
42 #include <babeltrace/compat/dirent.h>
48 #define METADATA_LINE_SIZE 512
49 #define SEQUENCE_TEST_LENGTH 10
50 #define ARRAY_TEST_LENGTH 5
51 #define PACKET_RESIZE_TEST_LENGTH 100000
53 #define DEFAULT_CLOCK_FREQ 1000000000
54 #define DEFAULT_CLOCK_PRECISION 1
55 #define DEFAULT_CLOCK_OFFSET 0
56 #define DEFAULT_CLOCK_OFFSET_S 0
57 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
58 #define DEFAULT_CLOCK_TIME 0
62 static int64_t current_time
= 42;
64 /* Return 1 if uuids match, zero if different. */
65 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
70 if (!uuid_a
|| !uuid_b
) {
74 for (i
= 0; i
< 16; i
++) {
75 if (uuid_a
[i
] != uuid_b
[i
]) {
85 void validate_metadata(char *parser_path
, char *metadata_path
)
88 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
89 int parser_output_fd
= -1, metadata_fd
= -1;
96 parser_output_fd
= mkstemp(parser_output_path
);
97 metadata_fd
= open(metadata_path
, O_RDONLY
);
99 unlink(parser_output_path
);
101 if (parser_output_fd
== -1 || metadata_fd
== -1) {
102 diag("Failed create temporary files for metadata parsing.");
110 waitpid(pid
, &status
, 0);
111 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
113 /* ctf-parser-test expects a metadata string on stdin. */
114 ret
= dup2(metadata_fd
, STDIN_FILENO
);
116 perror("# dup2 metadata_fd to STDIN");
120 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
122 perror("# dup2 parser_output_fd to STDOUT");
126 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
128 perror("# dup2 parser_output_fd to STDERR");
132 execl(parser_path
, "ctf-parser-test", NULL
);
133 perror("# Could not launch the ctf metadata parser process");
137 ok(ret
== 0, "Metadata string is valid");
139 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
141 size_t len
= METADATA_LINE_SIZE
;
142 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
144 metadata_fp
= fdopen(metadata_fd
, "r");
146 perror("fdopen on metadata_fd");
151 parser_output_fp
= fdopen(parser_output_fd
, "r");
152 if (!parser_output_fp
) {
153 perror("fdopen on parser_output_fd");
156 parser_output_fd
= -1;
160 diag("malloc failure");
165 /* Output the metadata and parser output as diagnostic */
166 while (bt_getline(&line
, &len
, metadata_fp
) > 0) {
167 fprintf(stderr
, "# %s", line
);
170 rewind(parser_output_fp
);
171 while (bt_getline(&line
, &len
, parser_output_fp
) > 0) {
172 fprintf(stderr
, "# %s", line
);
178 if (fclose(metadata_fp
)) {
179 diag("fclose failure");
182 if (parser_output_fp
) {
183 if (fclose(parser_output_fp
)) {
184 diag("fclose failure");
189 if (parser_output_fd
>= 0) {
190 if (close(parser_output_fd
)) {
194 if (metadata_fd
>= 0) {
195 if (close(metadata_fd
)) {
201 void validate_trace(char *parser_path
, char *trace_path
)
204 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
205 int babeltrace_output_fd
= -1;
212 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
213 unlink(babeltrace_output_path
);
215 if (babeltrace_output_fd
== -1) {
216 diag("Failed to create a temporary file for trace parsing.");
224 waitpid(pid
, &status
, 0);
225 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
227 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
229 perror("# dup2 babeltrace_output_fd to STDOUT");
233 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
235 perror("# dup2 babeltrace_output_fd to STDERR");
239 execl(parser_path
, "babeltrace", trace_path
, NULL
);
240 perror("# Could not launch the babeltrace process");
244 ok(ret
== 0, "Babeltrace could read the resulting trace");
246 if (ret
&& babeltrace_output_fd
>= 0) {
248 size_t len
= METADATA_LINE_SIZE
;
249 FILE *babeltrace_output_fp
= NULL
;
251 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
252 if (!babeltrace_output_fp
) {
253 perror("fdopen on babeltrace_output_fd");
256 babeltrace_output_fd
= -1;
260 diag("malloc error");
262 rewind(babeltrace_output_fp
);
263 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
269 if (babeltrace_output_fp
) {
270 if (fclose(babeltrace_output_fp
)) {
271 diag("fclose error");
276 if (babeltrace_output_fd
>= 0) {
277 if (close(babeltrace_output_fd
)) {
283 void event_copy_tests(struct bt_ctf_event
*event
)
285 struct bt_ctf_event
*copy
;
286 struct bt_ctf_event_class
*orig_event_class
;
287 struct bt_ctf_event_class
*copy_event_class
;
288 struct bt_ctf_stream
*orig_stream
;
289 struct bt_ctf_stream
*copy_stream
;
290 struct bt_ctf_field
*orig_field
;
291 struct bt_ctf_field
*copy_field
;
294 ok(!bt_ctf_event_copy(NULL
),
295 "bt_ctf_event_copy handles NULL correctly");
296 copy
= bt_ctf_event_copy(event
);
297 ok(copy
, "bt_ctf_event_copy returns a valid pointer");
299 /* validate event class */
300 orig_event_class
= bt_ctf_event_get_class(event
);
301 assert(orig_event_class
);
302 copy_event_class
= bt_ctf_event_get_class(copy
);
303 ok(orig_event_class
== copy_event_class
,
304 "original and copied events share the same event class pointer");
305 bt_put(orig_event_class
);
306 bt_put(copy_event_class
);
308 /* validate stream */
309 orig_stream
= bt_ctf_event_get_stream(event
);
310 copy_stream
= bt_ctf_event_get_stream(copy
);
313 ok(!copy_stream
, "original and copied events have no stream");
315 ok(orig_stream
== copy_stream
,
316 "original and copied events share the same stream pointer");
322 orig_field
= bt_ctf_event_get_header(event
);
323 copy_field
= bt_ctf_event_get_header(copy
);
326 ok(!copy_field
, "original and copied events have no header");
328 ok(orig_field
!= copy_field
,
329 "original and copied events headers are different pointers");
336 orig_field
= bt_ctf_event_get_event_context(event
);
337 copy_field
= bt_ctf_event_get_event_context(copy
);
340 ok(!copy_field
, "original and copied events have no context");
342 ok(orig_field
!= copy_field
,
343 "original and copied events contexts are different pointers");
350 orig_field
= bt_ctf_event_get_payload_field(event
);
351 copy_field
= bt_ctf_event_get_payload_field(copy
);
354 ok(!copy_field
, "original and copied events have no payload");
356 ok(orig_field
!= copy_field
,
357 "original and copied events payloads are different pointers");
366 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
367 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
369 /* Create and add a simple event class */
370 struct bt_ctf_event_class
*simple_event_class
=
371 bt_ctf_event_class_create("Simple Event");
372 struct bt_ctf_field_type
*uint_12_type
=
373 bt_ctf_field_type_integer_create(12);
374 struct bt_ctf_field_type
*int_64_type
=
375 bt_ctf_field_type_integer_create(64);
376 struct bt_ctf_field_type
*float_type
=
377 bt_ctf_field_type_floating_point_create();
378 struct bt_ctf_field_type
*enum_type
;
379 struct bt_ctf_field_type
*enum_type_unsigned
=
380 bt_ctf_field_type_enumeration_create(uint_12_type
);
381 struct bt_ctf_field_type
*event_context_type
=
382 bt_ctf_field_type_structure_create();
383 struct bt_ctf_field_type
*returned_type
;
384 struct bt_ctf_event
*simple_event
;
385 struct bt_ctf_field
*integer_field
;
386 struct bt_ctf_field
*float_field
;
387 struct bt_ctf_field
*enum_field
;
388 struct bt_ctf_field
*enum_field_unsigned
;
389 struct bt_ctf_field
*enum_container_field
;
390 const char *mapping_name_test
= "truie";
391 const double double_test_value
= 3.1415;
392 struct bt_ctf_field
*enum_container_field_unsigned
;
393 const char *mapping_name_negative_test
= "negative_value";
394 const char *ret_char
;
396 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
397 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
398 struct bt_ctf_clock
*ret_clock
;
399 struct bt_ctf_event_class
*ret_event_class
;
400 struct bt_ctf_field
*packet_context
;
401 struct bt_ctf_field
*packet_context_field
;
402 struct bt_ctf_field
*stream_event_context
;
403 struct bt_ctf_field
*stream_event_context_field
;
404 struct bt_ctf_field
*event_context
;
405 struct bt_ctf_field
*event_context_field
;
407 ok(uint_12_type
, "Create an unsigned integer type");
409 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
410 ok(int_64_type
, "Create a signed integer type");
411 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
413 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
414 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
415 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
416 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
417 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
418 bt_put(returned_type
);
420 bt_ctf_field_type_set_alignment(float_type
, 32);
421 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
422 "bt_ctf_field_type_get_alignment handles NULL correctly");
423 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
424 "bt_ctf_field_type_get_alignment returns a correct value");
426 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
427 "Set a floating point type's exponent digit count");
428 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
429 "Set a floating point type's mantissa digit count");
431 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
432 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
433 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
434 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
435 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
436 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
437 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
438 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
440 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
441 mapping_name_negative_test
, -12345, 0) == 0,
442 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
443 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
444 "escaping; \"test\"", 1, 1) == 0,
445 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
446 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
447 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
448 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
449 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
450 "event clock int float", 5, 22) == 0,
451 "Accept enumeration mapping strings containing reserved keywords");
452 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
454 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
455 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
456 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
457 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
458 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
459 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
460 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
462 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
463 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
464 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
465 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
466 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
467 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
469 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
470 "enum_field") == 0, "Add signed enumeration field to event");
472 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
473 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
474 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
475 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
476 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
477 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
478 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
479 NULL
, &ret_range_end_int64_t
) < 0,
480 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
481 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
482 &ret_range_start_int64_t
, NULL
) < 0,
483 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
484 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
485 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
486 "bt_ctf_field_type_enumeration_get_mapping returns a value");
487 ok(!strcmp(ret_char
, mapping_name_test
),
488 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
489 ok(ret_range_start_int64_t
== 42,
490 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
491 ok(ret_range_end_int64_t
== 42,
492 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
494 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
495 "escaping; \"test\"", 0, 0) == 0,
496 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
497 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
498 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
499 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
500 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
501 "event clock int float", 5, 22) == 0,
502 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
503 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
505 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
506 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
507 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
508 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
509 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
510 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
511 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
512 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
514 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
515 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
516 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
517 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
519 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
520 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
521 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
522 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
523 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
524 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
525 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
526 NULL
, &ret_range_end_uint64_t
) < 0,
527 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
528 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
529 &ret_range_start_uint64_t
, NULL
) < 0,
530 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
531 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
532 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
533 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
534 ok(!strcmp(ret_char
, mapping_name_test
),
535 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
536 ok(ret_range_start_uint64_t
== 42,
537 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
538 ok(ret_range_end_uint64_t
== 42,
539 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
541 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
543 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
546 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
548 /* Set an event context type which will contain a single integer*/
549 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
550 "event_specific_context"),
551 "Add event specific context field");
552 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
553 "bt_ctf_event_class_get_context_type handles NULL correctly");
554 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
555 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
557 ok(bt_ctf_event_class_set_context_type(simple_event_class
, NULL
) < 0,
558 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
559 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
560 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
561 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
562 "Set an event class' context type successfully");
563 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
564 ok(returned_type
== event_context_type
,
565 "bt_ctf_event_class_get_context_type returns the appropriate type");
566 bt_put(returned_type
);
568 bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
);
570 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
571 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
572 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
573 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
574 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
575 "bt_ctf_stream_class_get_event_class handles NULL correctly");
576 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
577 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
578 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
579 ok(ret_event_class
== simple_event_class
,
580 "bt_ctf_stream_class_get_event_class returns the correct event class");
581 bt_put(ret_event_class
);
582 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
583 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
584 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
585 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
587 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
588 ok(ret_event_class
== simple_event_class
,
589 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
590 bt_put(ret_event_class
);
592 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
593 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
594 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
595 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
596 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
597 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
598 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
599 ok(ret_event_class
== simple_event_class
,
600 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
601 bt_put(ret_event_class
);
603 simple_event
= bt_ctf_event_create(simple_event_class
);
605 "Instantiate an event containing a single integer field");
607 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
608 "bt_ctf_event_get_clock handles NULL correctly");
609 ret_clock
= bt_ctf_event_get_clock(simple_event
);
610 ok(ret_clock
== clock
,
611 "bt_ctf_event_get_clock returns a correct clock");
614 integer_field
= bt_ctf_field_create(uint_12_type
);
615 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
616 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
617 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
619 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
620 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
621 "bt_ctf_field_floating_point_get_value fails on an unset float field");
622 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
623 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
624 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
625 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
626 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
627 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
628 "bt_ctf_field_floating_point_get_value returns a double value");
629 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
630 "bt_ctf_field_floating_point_get_value returns a correct value");
632 enum_field
= bt_ctf_field_create(enum_type
);
633 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
634 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
635 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
636 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
637 enum_container_field
= bt_ctf_field_enumeration_get_container(
639 ok(bt_ctf_field_signed_integer_set_value(
640 enum_container_field
, -42) == 0,
641 "Set signed enumeration container value");
642 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
643 ok(!strcmp(ret_char
, mapping_name_negative_test
),
644 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
645 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
647 enum_field_unsigned
= bt_ctf_field_create(enum_type_unsigned
);
648 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
649 enum_field_unsigned
);
650 ok(bt_ctf_field_unsigned_integer_set_value(
651 enum_container_field_unsigned
, 42) == 0,
652 "Set unsigned enumeration container value");
653 bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
654 enum_field_unsigned
);
655 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
656 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
657 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
659 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
661 /* Populate stream event context */
662 stream_event_context
= bt_ctf_stream_get_event_context(stream
);
663 stream_event_context_field
= bt_ctf_field_structure_get_field(
664 stream_event_context
, "common_event_context");
665 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
667 /* Populate the event's context */
668 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
669 "bt_ctf_event_get_event_context handles NULL correctly");
670 event_context
= bt_ctf_event_get_event_context(simple_event
);
672 "bt_ctf_event_get_event_context returns a field");
673 returned_type
= bt_ctf_field_get_type(event_context
);
674 ok(returned_type
== event_context_type
,
675 "bt_ctf_event_get_event_context returns a field of the appropriate type");
676 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
677 "event_specific_context");
678 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
679 "Successfully set an event context's value");
680 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
681 "bt_ctf_event_set_event_context handles a NULL event correctly");
682 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
683 "bt_ctf_event_set_event_context handles a NULL event context correctly");
684 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
685 "bt_ctf_event_set_event_context rejects a context of the wrong type");
686 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
687 "Set an event context successfully");
689 event_copy_tests(simple_event
);
690 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
691 "Append simple event to trace stream");
693 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
694 "bt_ctf_stream_get_packet_context handles NULL correctly");
695 packet_context
= bt_ctf_stream_get_packet_context(stream
);
697 "bt_ctf_stream_get_packet_context returns a packet context");
699 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
701 ok(packet_context_field
,
702 "Packet context contains the default packet_size field.");
703 bt_put(packet_context_field
);
704 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
705 "custom_packet_context_field");
706 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
707 "Custom packet context field value successfully set.");
709 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
710 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
711 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
712 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
713 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
714 "Successfully set a stream's packet context");
716 ok(bt_ctf_stream_flush(stream
) == 0,
717 "Flush trace stream with one event");
719 bt_put(simple_event_class
);
720 bt_put(simple_event
);
721 bt_put(uint_12_type
);
725 bt_put(enum_type_unsigned
);
726 bt_put(returned_type
);
727 bt_put(event_context_type
);
728 bt_put(integer_field
);
731 bt_put(enum_field_unsigned
);
732 bt_put(enum_container_field
);
733 bt_put(enum_container_field_unsigned
);
734 bt_put(packet_context
);
735 bt_put(packet_context_field
);
736 bt_put(stream_event_context
);
737 bt_put(stream_event_context_field
);
738 bt_put(event_context
);
739 bt_put(event_context_field
);
742 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
743 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
745 struct event_class_attrs_counts
{
756 struct event_class_attrs_counts
;
757 const char *complex_test_event_string
= "Complex Test Event";
758 const char *test_string_1
= "Test ";
759 const char *test_string_2
= "string ";
760 const char *test_string_3
= "abcdefghi";
761 const char *test_string_4
= "abcd\0efg\0hi";
762 const char *test_string_cat
= "Test string abcdeabcd";
763 struct bt_ctf_field_type
*uint_35_type
=
764 bt_ctf_field_type_integer_create(35);
765 struct bt_ctf_field_type
*int_16_type
=
766 bt_ctf_field_type_integer_create(16);
767 struct bt_ctf_field_type
*uint_3_type
=
768 bt_ctf_field_type_integer_create(3);
769 struct bt_ctf_field_type
*enum_variant_type
=
770 bt_ctf_field_type_enumeration_create(uint_3_type
);
771 struct bt_ctf_field_type
*variant_type
=
772 bt_ctf_field_type_variant_create(enum_variant_type
,
774 struct bt_ctf_field_type
*string_type
=
775 bt_ctf_field_type_string_create();
776 struct bt_ctf_field_type
*sequence_type
;
777 struct bt_ctf_field_type
*array_type
;
778 struct bt_ctf_field_type
*inner_structure_type
=
779 bt_ctf_field_type_structure_create();
780 struct bt_ctf_field_type
*complex_structure_type
=
781 bt_ctf_field_type_structure_create();
782 struct bt_ctf_field_type
*ret_field_type
;
783 struct bt_ctf_event_class
*event_class
;
784 struct bt_ctf_event
*event
;
785 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
786 *inner_structure_field
, *complex_structure_field
,
787 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
788 *variant_field
, *an_array_field
, *ret_field
;
789 uint64_t ret_unsigned_int
;
790 int64_t ret_signed_int
;
791 const char *ret_string
;
792 struct bt_ctf_stream_class
*ret_stream_class
;
793 struct bt_ctf_event_class
*ret_event_class
;
794 struct bt_ctf_field
*packet_context
, *packet_context_field
;
795 struct bt_value
*obj
;
797 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
798 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
799 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
800 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
801 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
802 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
803 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
804 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
805 bt_ctf_field_type_set_alignment(int_16_type
, 32);
806 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
807 bt_ctf_field_type_integer_set_base(uint_35_type
,
808 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
810 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
811 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
814 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
815 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
816 ret_field_type
= bt_ctf_field_type_array_get_element_type(
818 ok(ret_field_type
== int_16_type
,
819 "bt_ctf_field_type_array_get_element_type returns the correct type");
820 bt_put(ret_field_type
);
822 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
823 "bt_ctf_field_type_array_get_length handles NULL correctly");
824 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
825 "bt_ctf_field_type_array_get_length returns the correct length");
827 bt_ctf_field_type_structure_add_field(inner_structure_type
,
828 uint_35_type
, "seq_len");
829 bt_ctf_field_type_structure_add_field(inner_structure_type
,
830 sequence_type
, "a_sequence");
831 bt_ctf_field_type_structure_add_field(inner_structure_type
,
832 array_type
, "an_array");
834 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
836 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
838 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
839 "UINT35_TYPE", 2, 7);
841 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
843 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
844 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
845 enum_variant_type
, NULL
) < 0,
846 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
847 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
848 enum_variant_type
, "INT16_TYPE") == 1,
849 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
851 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
852 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
853 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
854 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
855 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
856 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
858 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
859 "An unknown entry"), "Reject a variant field based on an unknown tag value");
860 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
861 "UINT3_TYPE") == 0, "Add a field to a variant");
862 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
864 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
867 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
868 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
869 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
870 ok(ret_field_type
== enum_variant_type
,
871 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
872 bt_put(ret_field_type
);
874 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
875 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
876 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
877 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
878 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
879 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
880 "INT16_TYPE") == NULL
,
881 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
882 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
884 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
885 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
886 variant_type
, "INT16_TYPE");
887 ok(ret_field_type
== int_16_type
,
888 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
889 bt_put(ret_field_type
);
891 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
892 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
893 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
894 "bt_ctf_field_type_variant_get_field_count returns the correct count");
896 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
897 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
898 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
899 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
900 bt_put(ret_field_type
);
901 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
902 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
903 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
904 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
905 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
906 "bt_ctf_field_type_variant_get_field returns a field");
907 ok(!strcmp("INT16_TYPE", ret_string
),
908 "bt_ctf_field_type_variant_get_field returns a correct field name");
909 ok(ret_field_type
== int_16_type
,
910 "bt_ctf_field_type_variant_get_field returns a correct field type");
911 bt_put(ret_field_type
);
913 bt_ctf_field_type_structure_add_field(complex_structure_type
,
914 enum_variant_type
, "variant_selector");
915 bt_ctf_field_type_structure_add_field(complex_structure_type
,
916 string_type
, "a_string");
917 bt_ctf_field_type_structure_add_field(complex_structure_type
,
918 variant_type
, "variant_value");
919 bt_ctf_field_type_structure_add_field(complex_structure_type
,
920 inner_structure_type
, "inner_structure");
922 ok(bt_ctf_event_class_create("clock") == NULL
,
923 "Reject creation of an event class with an illegal name");
924 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
925 ok(event_class
, "Create an event class");
926 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
927 "Reject addition of a field with an empty name to an event");
928 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
929 "Reject addition of a field with a NULL type to an event");
930 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
932 "Reject addition of a type with an illegal name to an event");
933 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
935 "Add field of type unsigned integer to an event");
936 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
937 "int_16") == 0, "Add field of type signed integer to an event");
938 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
939 "complex_structure") == 0,
940 "Add composite structure to an event");
942 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
943 "bt_ctf_event_class_get_name handles NULL correctly");
944 ret_string
= bt_ctf_event_class_get_name(event_class
);
945 ok(!strcmp(ret_string
, complex_test_event_string
),
946 "bt_ctf_event_class_get_name returns a correct name");
947 ok(bt_ctf_event_class_get_id(event_class
) < 0,
948 "bt_ctf_event_class_get_id returns a negative value when not set");
949 ok(bt_ctf_event_class_get_id(NULL
) < 0,
950 "bt_ctf_event_class_get_id handles NULL correctly");
951 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
952 "bt_ctf_event_class_set_id handles NULL correctly");
953 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
954 "Set an event class' id");
955 ok(bt_ctf_event_class_get_id(event_class
) == 42,
956 "bt_ctf_event_class_get_id returns the correct value");
958 /* Test event class attributes */
959 obj
= bt_value_integer_create_init(15);
961 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
962 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
963 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
964 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
965 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
966 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
967 assert(!bt_value_integer_set(obj
, -3));
968 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
969 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
970 assert(!bt_value_integer_set(obj
, 11));
971 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
972 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
973 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
974 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
975 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
977 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
980 obj
= bt_value_integer_create_init(5);
982 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
983 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
985 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
986 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
987 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
988 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
989 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
990 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
991 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
994 ret
= bt_value_integer_get(obj
, &int64_value
);
995 ok(obj
&& !ret
&& int64_value
== 5,
996 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
999 obj
= bt_value_string_create_init("nu name");
1001 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
1002 ret_string
= bt_ctf_event_class_get_name(event_class
);
1003 ok(!strcmp(ret_string
, "nu name"),
1004 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
1005 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
1006 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
1008 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
1010 obj
= bt_value_string_create_init("http://kernel.org/");
1012 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
1015 ok(bt_ctf_event_class_get_attribute_count(NULL
),
1016 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1017 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
1018 "bt_ctf_event_class_get_attribute_count returns the correct count");
1019 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
1020 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1021 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
1022 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1023 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
1024 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1025 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
1026 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1028 memset(&attrs_count
, 0, sizeof(attrs_count
));
1030 for (i
= 0; i
< 4; ++i
) {
1031 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
1033 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
1034 assert(ret_string
&& obj
);
1036 if (!strcmp(ret_string
, "id")) {
1038 ok(bt_value_is_integer(obj
),
1039 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1041 } else if (!strcmp(ret_string
, "name")) {
1043 ok(bt_value_is_string(obj
),
1044 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1046 } else if (!strcmp(ret_string
, "loglevel")) {
1047 attrs_count
.loglevel
++;
1048 ok(bt_value_is_integer(obj
),
1049 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1051 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1052 attrs_count
.modelemfuri
++;
1053 ok(bt_value_is_string(obj
),
1054 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1057 attrs_count
.unknown
++;
1063 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1064 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1065 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1066 "event class has one instance of each known attribute");
1068 /* Add event class to the stream class */
1069 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1070 "Reject addition of NULL event class to a stream class");
1071 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1072 event_class
) == 0, "Add an event class to stream class");
1074 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1075 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1076 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1077 ok(ret_stream_class
== stream_class
,
1078 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1079 bt_put(ret_stream_class
);
1081 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1082 "bt_ctf_event_class_get_field_count handles NULL correctly");
1083 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1084 "bt_ctf_event_class_get_field_count returns a correct value");
1086 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1087 &ret_field_type
, 0) < 0,
1088 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1089 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1090 &ret_field_type
, 0) == 0,
1091 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1092 bt_put(ret_field_type
);
1093 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1095 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1096 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1097 &ret_field_type
, 42) < 0,
1098 "bt_ctf_event_class_get_field handles an invalid index correctly");
1099 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1100 &ret_field_type
, 0) == 0,
1101 "bt_ctf_event_class_get_field returns a field");
1102 ok(ret_field_type
== uint_35_type
,
1103 "bt_ctf_event_class_get_field returns a correct field type");
1104 bt_put(ret_field_type
);
1105 ok(!strcmp(ret_string
, "uint_35"),
1106 "bt_ctf_event_class_get_field returns a correct field name");
1107 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1108 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1109 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1110 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1111 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1112 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1113 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1114 "complex_structure");
1115 ok(ret_field_type
== complex_structure_type
,
1116 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1117 bt_put(ret_field_type
);
1119 event
= bt_ctf_event_create(event_class
);
1120 ok(event
, "Instanciate a complex event");
1122 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1123 "bt_ctf_event_get_class handles NULL correctly");
1124 ret_event_class
= bt_ctf_event_get_class(event
);
1125 ok(ret_event_class
== event_class
,
1126 "bt_ctf_event_get_class returns the correct event class");
1127 bt_put(ret_event_class
);
1129 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1130 if (!uint_35_field
) {
1131 printf("uint_35_field is NULL\n");
1134 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1135 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1136 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1137 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1138 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1139 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1140 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1141 &ret_unsigned_int
) == 0,
1142 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1143 ok(ret_unsigned_int
== 0x0DDF00D,
1144 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1145 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1146 &ret_signed_int
) < 0,
1147 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1148 bt_put(uint_35_field
);
1150 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1151 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1152 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1153 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1154 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1155 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1156 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1157 &ret_signed_int
) == 0,
1158 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1159 ok(ret_signed_int
== -12345,
1160 "bt_ctf_field_signed_integer_get_value returns the correct value");
1161 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1162 &ret_unsigned_int
) < 0,
1163 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1164 bt_put(int_16_field
);
1166 complex_structure_field
= bt_ctf_event_get_payload(event
,
1167 "complex_structure");
1169 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1170 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1171 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1172 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1173 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1174 complex_structure_field
, 3);
1175 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1176 bt_put(inner_structure_field
);
1177 ok(ret_field_type
== inner_structure_type
,
1178 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1179 bt_put(ret_field_type
);
1181 inner_structure_field
= bt_ctf_field_structure_get_field(
1182 complex_structure_field
, "inner_structure");
1183 a_string_field
= bt_ctf_field_structure_get_field(
1184 complex_structure_field
, "a_string");
1185 enum_variant_field
= bt_ctf_field_structure_get_field(
1186 complex_structure_field
, "variant_selector");
1187 variant_field
= bt_ctf_field_structure_get_field(
1188 complex_structure_field
, "variant_value");
1189 uint_35_field
= bt_ctf_field_structure_get_field(
1190 inner_structure_field
, "seq_len");
1191 a_sequence_field
= bt_ctf_field_structure_get_field(
1192 inner_structure_field
, "a_sequence");
1193 an_array_field
= bt_ctf_field_structure_get_field(
1194 inner_structure_field
, "an_array");
1196 enum_container_field
= bt_ctf_field_enumeration_get_container(
1197 enum_variant_field
);
1198 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1199 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1200 enum_variant_field
);
1201 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1202 bt_put(int_16_field
);
1203 ok(!bt_ctf_field_string_get_value(a_string_field
),
1204 "bt_ctf_field_string_get_value returns NULL on an unset field");
1205 bt_ctf_field_string_set_value(a_string_field
,
1207 ok(!bt_ctf_field_string_get_value(NULL
),
1208 "bt_ctf_field_string_get_value correctly handles NULL");
1209 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1210 "bt_ctf_field_string_append correctly handles a NULL string field");
1211 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1212 "bt_ctf_field_string_append correctly handles a NULL string value");
1213 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1214 "bt_ctf_field_string_append succeeds");
1215 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1216 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1217 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1218 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1219 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1220 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1221 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1222 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1223 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1224 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1225 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1226 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1228 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1229 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1230 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1231 "bt_ctf_field_string_get_value returns a correct value");
1232 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1233 SEQUENCE_TEST_LENGTH
);
1235 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1236 enum_container_field
) == NULL
,
1237 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1238 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1240 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1241 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1242 variant_type
, enum_variant_field
);
1243 ok(ret_field_type
== int_16_type
,
1244 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1246 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1247 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1248 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1249 uint_35_field
) == 0, "Set a sequence field's length");
1250 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1251 ok(ret_field
== uint_35_field
,
1252 "bt_ctf_field_sequence_get_length returns the correct length field");
1253 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1254 "bt_ctf_field_sequence_get_length properly handles NULL");
1256 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1257 int_16_field
= bt_ctf_field_sequence_get_field(
1258 a_sequence_field
, i
);
1259 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1260 bt_put(int_16_field
);
1263 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1264 int_16_field
= bt_ctf_field_array_get_field(
1266 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1267 bt_put(int_16_field
);
1270 bt_ctf_clock_set_time(clock
, ++current_time
);
1271 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1272 "Append a complex event to a stream");
1275 * Populate the custom packet context field with a dummy value
1276 * otherwise flush will fail.
1278 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1279 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1280 "custom_packet_context_field");
1281 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1283 ok(bt_ctf_stream_flush(stream
) == 0,
1284 "Flush a stream containing a complex event");
1286 bt_put(uint_35_field
);
1287 bt_put(a_string_field
);
1288 bt_put(inner_structure_field
);
1289 bt_put(complex_structure_field
);
1290 bt_put(a_sequence_field
);
1291 bt_put(an_array_field
);
1292 bt_put(enum_variant_field
);
1293 bt_put(enum_container_field
);
1294 bt_put(variant_field
);
1296 bt_put(packet_context_field
);
1297 bt_put(packet_context
);
1298 bt_put(uint_35_type
);
1299 bt_put(int_16_type
);
1300 bt_put(string_type
);
1301 bt_put(sequence_type
);
1303 bt_put(inner_structure_type
);
1304 bt_put(complex_structure_type
);
1305 bt_put(uint_3_type
);
1306 bt_put(enum_variant_type
);
1307 bt_put(variant_type
);
1308 bt_put(ret_field_type
);
1309 bt_put(event_class
);
1313 static void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1314 struct bt_ctf_field_type
*expected_type
, const char *name
)
1316 struct bt_ctf_field_type
*copy_type
;
1318 copy_type
= bt_ctf_field_get_type(field
);
1319 ok(copy_type
== expected_type
,
1320 "bt_ctf_field_copy does not copy the type (%s)", name
);
1324 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1325 struct bt_ctf_field
*field_b
, const char *name
)
1327 ok(field_a
!= field_b
,
1328 "bt_ctf_field_copy creates different pointers (%s)", name
);
1331 void field_copy_tests()
1333 struct bt_ctf_field_type
*len_type
= NULL
;
1334 struct bt_ctf_field_type
*fp_type
= NULL
;
1335 struct bt_ctf_field_type
*s_type
= NULL
;
1336 struct bt_ctf_field_type
*e_int_type
= NULL
;
1337 struct bt_ctf_field_type
*e_type
= NULL
;
1338 struct bt_ctf_field_type
*v_type
= NULL
;
1339 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1340 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1341 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1342 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1343 struct bt_ctf_field_type
*strct_type
= NULL
;
1344 struct bt_ctf_field
*len
= NULL
;
1345 struct bt_ctf_field
*fp
= NULL
;
1346 struct bt_ctf_field
*s
= NULL
;
1347 struct bt_ctf_field
*e_int
= NULL
;
1348 struct bt_ctf_field
*e
= NULL
;
1349 struct bt_ctf_field
*v
= NULL
;
1350 struct bt_ctf_field
*v_selected
= NULL
;
1351 struct bt_ctf_field
*v_selected_cur
= NULL
;
1352 struct bt_ctf_field
*v_selected_0
= NULL
;
1353 struct bt_ctf_field
*v_selected_1
= NULL
;
1354 struct bt_ctf_field
*v_selected_2
= NULL
;
1355 struct bt_ctf_field
*v_selected_3
= NULL
;
1356 struct bt_ctf_field
*v_selected_4
= NULL
;
1357 struct bt_ctf_field
*v_selected_5
= NULL
;
1358 struct bt_ctf_field
*v_selected_6
= NULL
;
1359 struct bt_ctf_field
*a
= NULL
;
1360 struct bt_ctf_field
*a_0
= NULL
;
1361 struct bt_ctf_field
*a_1
= NULL
;
1362 struct bt_ctf_field
*a_2
= NULL
;
1363 struct bt_ctf_field
*a_3
= NULL
;
1364 struct bt_ctf_field
*a_4
= NULL
;
1365 struct bt_ctf_field
*strct
= NULL
;
1366 struct bt_ctf_field
*len_copy
= NULL
;
1367 struct bt_ctf_field
*fp_copy
= NULL
;
1368 struct bt_ctf_field
*s_copy
= NULL
;
1369 struct bt_ctf_field
*e_int_copy
= NULL
;
1370 struct bt_ctf_field
*e_copy
= NULL
;
1371 struct bt_ctf_field
*v_copy
= NULL
;
1372 struct bt_ctf_field
*v_selected_copy
= NULL
;
1373 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1374 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1375 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1376 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1377 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1378 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1379 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1380 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1381 struct bt_ctf_field
*a_copy
= NULL
;
1382 struct bt_ctf_field
*a_0_copy
= NULL
;
1383 struct bt_ctf_field
*a_1_copy
= NULL
;
1384 struct bt_ctf_field
*a_2_copy
= NULL
;
1385 struct bt_ctf_field
*a_3_copy
= NULL
;
1386 struct bt_ctf_field
*a_4_copy
= NULL
;
1387 struct bt_ctf_field
*strct_copy
= NULL
;
1388 uint64_t uint64_t_val
;
1389 const char *str_val
;
1393 /* create len type */
1394 len_type
= bt_ctf_field_type_integer_create(32);
1397 /* create fp type */
1398 fp_type
= bt_ctf_field_type_floating_point_create();
1402 s_type
= bt_ctf_field_type_string_create();
1405 /* create e_int type */
1406 e_int_type
= bt_ctf_field_type_integer_create(8);
1410 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1412 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1415 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1419 /* create v_label1 type */
1420 v_label1_type
= bt_ctf_field_type_string_create();
1421 assert(v_label1_type
);
1423 /* create v_label1_array type */
1424 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1425 assert(v_label1_array_type
);
1427 /* create v_label2 type */
1428 v_label2_type
= bt_ctf_field_type_integer_create(16);
1429 assert(v_label2_type
);
1431 /* create v_label2_seq type */
1432 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1434 assert(v_label2_seq_type
);
1437 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1439 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1442 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1446 /* create strct type */
1447 strct_type
= bt_ctf_field_type_structure_create();
1449 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1452 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1454 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1456 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1458 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1460 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1461 v_label1_array_type
, "a");
1465 strct
= bt_ctf_field_create(strct_type
);
1469 len
= bt_ctf_field_structure_get_field(strct
, "len");
1473 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1477 s
= bt_ctf_field_structure_get_field(strct
, "s");
1481 e
= bt_ctf_field_structure_get_field(strct
, "e");
1484 /* get e_int (underlying integer) */
1485 e_int
= bt_ctf_field_enumeration_get_container(e
);
1489 v
= bt_ctf_field_structure_get_field(strct
, "v");
1493 a
= bt_ctf_field_structure_get_field(strct
, "a");
1497 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1501 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1505 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1508 /* set e field (LABEL2) */
1509 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1513 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1515 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1516 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1517 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1518 ok(v_selected_cur
== v_selected
,
1519 "bt_ctf_field_variant_get_current_field returns the current field");
1520 bt_put(v_selected_cur
);
1522 /* set selected v field */
1523 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1525 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1526 assert(v_selected_0
);
1527 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1529 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1530 assert(v_selected_1
);
1531 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1533 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1534 assert(v_selected_2
);
1535 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1537 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1538 assert(v_selected_3
);
1539 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1541 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1542 assert(v_selected_4
);
1543 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1545 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1546 assert(v_selected_5
);
1547 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1549 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1550 assert(v_selected_6
);
1551 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1555 a_0
= bt_ctf_field_array_get_field(a
, 0);
1557 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1559 a_1
= bt_ctf_field_array_get_field(a
, 1);
1561 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1563 a_2
= bt_ctf_field_array_get_field(a
, 2);
1565 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1567 a_3
= bt_ctf_field_array_get_field(a
, 3);
1569 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1571 a_4
= bt_ctf_field_array_get_field(a
, 4);
1573 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1576 /* create copy of strct */
1577 ok(!bt_ctf_field_copy(NULL
),
1578 "bt_ctf_field_copy handles NULL correctly");
1579 strct_copy
= bt_ctf_field_copy(strct
);
1581 "bt_ctf_field_copy returns a valid pointer");
1583 /* get all copied fields */
1584 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1586 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1588 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1590 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1592 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1594 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1596 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1597 assert(v_selected_copy
);
1598 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1599 assert(v_selected_0_copy
);
1600 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1601 assert(v_selected_1_copy
);
1602 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1603 assert(v_selected_2_copy
);
1604 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1605 assert(v_selected_3_copy
);
1606 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1607 assert(v_selected_4_copy
);
1608 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1609 assert(v_selected_5_copy
);
1610 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1611 assert(v_selected_6_copy
);
1612 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1613 "sequence field copy is not too large");
1614 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1616 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1618 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1620 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1622 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1624 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1626 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1627 "array field copy is not too large");
1629 /* make sure copied fields are different pointers */
1630 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1631 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1632 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1633 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1634 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1635 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1636 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1637 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1639 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1641 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1643 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1645 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1647 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1649 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1651 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1653 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1654 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1655 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1656 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1657 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1658 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1660 /* make sure copied fields share the same types */
1661 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1662 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1663 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1664 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1665 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1666 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1667 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1669 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1671 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1673 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1675 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1677 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1679 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1681 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1683 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1684 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1685 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1686 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1687 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1688 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1690 /* validate len copy */
1691 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1693 ok(uint64_t_val
== 7,
1694 "bt_ctf_field_copy creates a valid integer field copy");
1696 /* validate fp copy */
1697 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1699 ok(double_val
== 3.14,
1700 "bt_ctf_field_copy creates a valid floating point number field copy");
1702 /* validate s copy */
1703 str_val
= bt_ctf_field_string_get_value(s_copy
);
1704 ok(str_val
&& !strcmp(str_val
, "btbt"),
1705 "bt_ctf_field_copy creates a valid string field copy");
1707 /* validate e_int copy */
1708 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1711 ok(uint64_t_val
== 23,
1712 "bt_ctf_field_copy creates a valid enum's integer field copy");
1714 /* validate e copy */
1715 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1716 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1717 "bt_ctf_field_copy creates a valid enum field copy");
1719 /* validate v_selected copy */
1720 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1721 assert(v_selected_copy_len
);
1722 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1725 ok(uint64_t_val
== 7,
1726 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1727 bt_put(v_selected_copy_len
);
1728 v_selected_copy_len
= NULL
;
1730 /* validate v_selected copy fields */
1731 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1734 ok(uint64_t_val
== 7,
1735 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1736 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1739 ok(uint64_t_val
== 6,
1740 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1741 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1744 ok(uint64_t_val
== 5,
1745 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1746 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1749 ok(uint64_t_val
== 4,
1750 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1751 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1754 ok(uint64_t_val
== 3,
1755 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1756 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1759 ok(uint64_t_val
== 2,
1760 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1761 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1764 ok(uint64_t_val
== 1,
1765 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1767 /* validate a copy fields */
1768 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1769 ok(str_val
&& !strcmp(str_val
, "a_0"),
1770 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1771 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1772 ok(str_val
&& !strcmp(str_val
, "a_1"),
1773 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1774 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1775 ok(str_val
&& !strcmp(str_val
, "a_2"),
1776 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1777 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1778 ok(str_val
&& !strcmp(str_val
, "a_3"),
1779 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1780 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1781 ok(str_val
&& !strcmp(str_val
, "a_4"),
1782 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1784 /* put everything */
1791 bt_put(v_label1_type
);
1792 bt_put(v_label1_array_type
);
1793 bt_put(v_label2_type
);
1794 bt_put(v_label2_seq_type
);
1803 bt_put(v_selected_0
);
1804 bt_put(v_selected_1
);
1805 bt_put(v_selected_2
);
1806 bt_put(v_selected_3
);
1807 bt_put(v_selected_4
);
1808 bt_put(v_selected_5
);
1809 bt_put(v_selected_6
);
1823 bt_put(v_selected_copy
);
1824 bt_put(v_selected_0_copy
);
1825 bt_put(v_selected_1_copy
);
1826 bt_put(v_selected_2_copy
);
1827 bt_put(v_selected_3_copy
);
1828 bt_put(v_selected_4_copy
);
1829 bt_put(v_selected_5_copy
);
1830 bt_put(v_selected_6_copy
);
1840 void type_field_tests()
1842 struct bt_ctf_field
*uint_12
;
1843 struct bt_ctf_field
*int_16
;
1844 struct bt_ctf_field
*string
;
1845 struct bt_ctf_field
*enumeration
;
1846 struct bt_ctf_field_type
*composite_structure_type
;
1847 struct bt_ctf_field_type
*structure_seq_type
;
1848 struct bt_ctf_field_type
*string_type
;
1849 struct bt_ctf_field_type
*sequence_type
;
1850 struct bt_ctf_field_type
*uint_8_type
;
1851 struct bt_ctf_field_type
*int_16_type
;
1852 struct bt_ctf_field_type
*uint_12_type
=
1853 bt_ctf_field_type_integer_create(12);
1854 struct bt_ctf_field_type
*enumeration_type
;
1855 struct bt_ctf_field_type
*enumeration_sequence_type
;
1856 struct bt_ctf_field_type
*enumeration_array_type
;
1857 struct bt_ctf_field_type
*returned_type
;
1858 const char *ret_string
;
1860 returned_type
= bt_ctf_field_get_type(NULL
);
1861 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1863 ok(uint_12_type
, "Create an unsigned integer type");
1864 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1865 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1866 "Set integer type's base as binary");
1867 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1868 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1869 "Set integer type's base as decimal");
1870 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1871 BT_CTF_INTEGER_BASE_UNKNOWN
),
1872 "Reject integer type's base set as unknown");
1873 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1874 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1875 "Set integer type's base as octal");
1876 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1877 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1878 "Set integer type's base as hexadecimal");
1879 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1880 "Reject unknown integer base value");
1881 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1882 "Set integer type signedness to signed");
1883 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1884 "Set integer type signedness to unsigned");
1885 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1886 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1887 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1888 "bt_ctf_field_type_integer_get_size returns a correct value");
1889 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1890 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1891 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1892 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1894 ok(bt_ctf_field_type_set_byte_order(NULL
,
1895 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1896 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1897 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1898 (enum bt_ctf_byte_order
) 42) < 0,
1899 "bt_ctf_field_type_set_byte_order rejects invalid values");
1900 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1901 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1902 "Set an integer's byte order to little endian");
1903 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1904 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1905 "Set an integer's byte order to big endian");
1906 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1907 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1908 "bt_ctf_field_type_get_byte_order returns a correct value");
1909 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1910 BT_CTF_BYTE_ORDER_UNKNOWN
,
1911 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1913 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1915 "bt_ctf_field_type_get_type_id handles NULL correctly");
1916 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1918 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1920 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1921 BT_CTF_INTEGER_BASE_UNKNOWN
,
1922 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1923 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1924 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1925 "bt_ctf_field_type_integer_get_base returns a correct value");
1927 ok(bt_ctf_field_type_integer_set_encoding(NULL
, CTF_STRING_ASCII
) < 0,
1928 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1929 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1930 (enum ctf_string_encoding
) 123) < 0,
1931 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1932 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1933 CTF_STRING_UTF8
) == 0,
1934 "Set integer type encoding to UTF8");
1935 ok(bt_ctf_field_type_integer_get_encoding(NULL
) == CTF_STRING_UNKNOWN
,
1936 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1937 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) == CTF_STRING_UTF8
,
1938 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1940 int_16_type
= bt_ctf_field_type_integer_create(16);
1941 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1942 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1943 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1944 uint_8_type
= bt_ctf_field_type_integer_create(8);
1946 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1947 ok(sequence_type
, "Create a sequence of int16_t type");
1948 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1950 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1952 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1953 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1954 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1956 ok(!strcmp(ret_string
, "seq_len"),
1957 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1958 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1959 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1960 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1962 ok(returned_type
== int_16_type
,
1963 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1964 bt_put(returned_type
);
1966 string_type
= bt_ctf_field_type_string_create();
1967 ok(string_type
, "Create a string type");
1968 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1970 "Reject invalid \"None\" string encoding");
1971 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1973 "Reject invalid string encoding");
1974 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1975 CTF_STRING_ASCII
) == 0,
1976 "Set string encoding to ASCII");
1978 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1980 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1981 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1983 "bt_ctf_field_type_string_get_encoding returns the correct value");
1985 structure_seq_type
= bt_ctf_field_type_structure_create();
1986 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1988 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1989 ok(structure_seq_type
, "Create a structure type");
1990 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1991 uint_8_type
, "seq_len") == 0,
1992 "Add a uint8_t type to a structure");
1993 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1994 sequence_type
, "a_sequence") == 0,
1995 "Add a sequence type to a structure");
1997 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1998 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1999 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
2000 "bt_ctf_field_type_structure_get_field_count returns a correct value");
2002 ok(bt_ctf_field_type_structure_get_field(NULL
,
2003 &ret_string
, &returned_type
, 1) < 0,
2004 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2005 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2006 NULL
, &returned_type
, 1) == 0,
2007 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2008 bt_put(returned_type
);
2009 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2010 &ret_string
, NULL
, 1) == 0,
2011 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2012 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2013 &ret_string
, &returned_type
, 10) < 0,
2014 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2015 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2016 &ret_string
, &returned_type
, 1) == 0,
2017 "bt_ctf_field_type_structure_get_field returns a field");
2018 ok(!strcmp(ret_string
, "a_sequence"),
2019 "bt_ctf_field_type_structure_get_field returns a correct field name");
2020 ok(returned_type
== sequence_type
,
2021 "bt_ctf_field_type_structure_get_field returns a correct field type");
2022 bt_put(returned_type
);
2024 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2025 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2026 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2027 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2028 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2029 structure_seq_type
, "a_sequence");
2030 ok(returned_type
== sequence_type
,
2031 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2032 bt_put(returned_type
);
2034 composite_structure_type
= bt_ctf_field_type_structure_create();
2035 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2036 string_type
, "a_string") == 0,
2037 "Add a string type to a structure");
2038 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2039 structure_seq_type
, "inner_structure") == 0,
2040 "Add a structure type to a structure");
2042 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2043 NULL
, "a_sequence") == NULL
,
2044 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2045 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2046 structure_seq_type
, NULL
) == NULL
,
2047 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2048 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2049 structure_seq_type
, "a_sequence");
2050 ok(returned_type
== sequence_type
,
2051 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2052 bt_put(returned_type
);
2054 int_16
= bt_ctf_field_create(int_16_type
);
2055 ok(int_16
, "Instanciate a signed 16-bit integer");
2056 uint_12
= bt_ctf_field_create(uint_12_type
);
2057 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2058 returned_type
= bt_ctf_field_get_type(int_16
);
2059 ok(returned_type
== int_16_type
,
2060 "bt_ctf_field_get_type returns the correct type");
2062 /* Can't modify types after instanciating them */
2063 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2064 BT_CTF_INTEGER_BASE_DECIMAL
),
2065 "Check an integer type' base can't be modified after instanciation");
2066 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2067 "Check an integer type's signedness can't be modified after instanciation");
2069 /* Check signed property is checked */
2070 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2071 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2072 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2073 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2075 /* Check overflows are properly tested for */
2076 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2077 "Check -32768 is allowed for a signed 16-bit integer");
2078 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2079 "Check 32767 is allowed for a signed 16-bit integer");
2080 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2081 "Check 32768 is not allowed for a signed 16-bit integer");
2082 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2083 "Check -32769 is not allowed for a signed 16-bit integer");
2084 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2085 "Check -42 is allowed for a signed 16-bit integer");
2087 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2088 "Check 4095 is allowed for an unsigned 12-bit integer");
2089 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2090 "Check 4096 is not allowed for a unsigned 12-bit integer");
2091 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2092 "Check 0 is allowed for an unsigned 12-bit integer");
2094 string
= bt_ctf_field_create(string_type
);
2095 ok(string
, "Instanciate a string field");
2096 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2097 "Set a string's value");
2099 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2100 ok(enumeration_type
,
2101 "Create an enumeration type with an unsigned 12-bit integer as container");
2102 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
2103 enumeration_type
, "count");
2104 ok(!enumeration_sequence_type
,
2105 "Check enumeration types are validated when creating a sequence");
2106 enumeration_array_type
= bt_ctf_field_type_array_create(
2107 enumeration_type
, 10);
2108 ok(!enumeration_array_type
,
2109 "Check enumeration types are validated when creating an array");
2110 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2111 enumeration_type
, "enumeration"),
2112 "Check enumeration types are validated when adding them as structure members");
2113 enumeration
= bt_ctf_field_create(enumeration_type
);
2115 "Check enumeration types are validated before instantiation");
2120 bt_put(enumeration
);
2121 bt_put(composite_structure_type
);
2122 bt_put(structure_seq_type
);
2123 bt_put(string_type
);
2124 bt_put(sequence_type
);
2125 bt_put(uint_8_type
);
2126 bt_put(int_16_type
);
2127 bt_put(uint_12_type
);
2128 bt_put(enumeration_type
);
2129 bt_put(enumeration_sequence_type
);
2130 bt_put(enumeration_array_type
);
2131 bt_put(returned_type
);
2134 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2135 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2138 * Append enough events to force the underlying packet to be resized.
2139 * Also tests that a new event can be declared after a stream has been
2140 * instantiated and used/flushed.
2144 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2146 struct bt_ctf_field_type
*integer_type
=
2147 bt_ctf_field_type_integer_create(17);
2148 struct bt_ctf_field_type
*string_type
=
2149 bt_ctf_field_type_string_create();
2150 struct bt_ctf_event
*event
= NULL
;
2151 struct bt_ctf_field
*ret_field
= NULL
;
2152 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2153 uint64_t ret_uint64
;
2154 int events_appended
= 0;
2155 struct bt_ctf_field
*packet_context
= NULL
,
2156 *packet_context_field
= NULL
, *event_context
= NULL
;
2158 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2160 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2162 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2163 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2168 event
= bt_ctf_event_create(event_class
);
2169 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2170 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2171 ok(ret_field_type
== integer_type
,
2172 "bt_ctf_event_get_payload_by_index returns a correct field");
2173 bt_put(ret_field_type
);
2176 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2177 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2178 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2179 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2182 ok(bt_ctf_stream_get_event_context(NULL
) == NULL
,
2183 "bt_ctf_stream_get_event_context handles NULL correctly");
2184 event_context
= bt_ctf_stream_get_event_context(stream
);
2186 "bt_ctf_stream_get_event_context returns a stream event context");
2187 ok(bt_ctf_stream_set_event_context(NULL
, event_context
) < 0,
2188 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
2189 ok(bt_ctf_stream_set_event_context(stream
, NULL
) < 0,
2190 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
2191 ok(!bt_ctf_stream_set_event_context(stream
, event_context
),
2192 "bt_ctf_stream_set_event_context correctly set a stream event context");
2193 ret_field
= bt_ctf_field_create(integer_type
);
2194 ok(bt_ctf_stream_set_event_context(stream
, ret_field
) < 0,
2195 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
2198 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2199 event
= bt_ctf_event_create(event_class
);
2200 struct bt_ctf_field
*integer
=
2201 bt_ctf_field_create(integer_type
);
2202 struct bt_ctf_field
*string
=
2203 bt_ctf_field_create(string_type
);
2205 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2206 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2207 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2210 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2211 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2215 /* Populate stream event context */
2216 integer
= bt_ctf_field_structure_get_field(event_context
,
2217 "common_event_context");
2218 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2222 ret
|= bt_ctf_stream_append_event(stream
, event
);
2230 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2231 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2232 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2233 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2234 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2235 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2236 ok(ret
== 0 && ret_uint64
== 0,
2237 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2238 bt_ctf_stream_append_discarded_events(stream
, 1000);
2239 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2240 ok(ret
== 0 && ret_uint64
== 1000,
2241 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2244 ok(events_appended
, "Append 100 000 events to a stream");
2247 * Populate the custom packet context field with a dummy value
2248 * otherwise flush will fail.
2250 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2251 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2252 "custom_packet_context_field");
2253 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2255 ok(bt_ctf_stream_flush(stream
) == 0,
2256 "Flush a stream that forces a packet resize");
2257 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2258 ok(ret
== 0 && ret_uint64
== 1000,
2259 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2260 bt_put(integer_type
);
2261 bt_put(string_type
);
2262 bt_put(packet_context
);
2263 bt_put(packet_context_field
);
2264 bt_put(event_context
);
2265 bt_put(event_class
);
2268 void test_empty_stream(struct bt_ctf_writer
*writer
)
2271 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2272 struct bt_ctf_stream_class
*stream_class
= NULL
;
2273 struct bt_ctf_stream
*stream
= NULL
;
2275 trace
= bt_ctf_writer_get_trace(writer
);
2277 diag("Failed to get trace from writer");
2282 stream_class
= bt_ctf_stream_class_create("empty_stream");
2283 if (!stream_class
) {
2284 diag("Failed to create stream class");
2289 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2290 "bt_ctf_stream_class_get_trace handles NULL correctly");
2291 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2292 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2294 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2296 diag("Failed to create writer stream");
2301 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2302 ok(ret_trace
== trace
,
2303 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2306 "Created a stream class with default attributes and an empty stream");
2310 bt_put(stream_class
);
2313 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2316 struct bt_ctf_trace
*trace
= NULL
;
2317 struct bt_ctf_clock
*clock
= NULL
;
2318 struct bt_ctf_stream_class
*stream_class
= NULL
;
2319 struct bt_ctf_stream
*stream
= NULL
;
2320 struct bt_ctf_field_type
*integer_type
= NULL
,
2321 *sequence_type
= NULL
, *event_header_type
= NULL
;
2322 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2323 *event_header
= NULL
, *packet_header
= NULL
;
2324 struct bt_ctf_event_class
*event_class
= NULL
;
2325 struct bt_ctf_event
*event
= NULL
;
2327 trace
= bt_ctf_writer_get_trace(writer
);
2329 fail("Failed to get trace from writer");
2333 clock
= bt_ctf_trace_get_clock(trace
, 0);
2335 fail("Failed to get clock from trace");
2339 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2340 if (!stream_class
) {
2341 fail("Failed to create stream class");
2345 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2347 fail("Failed to set stream class clock");
2352 * Customize event header to add an "seq_len" integer member
2353 * which will be used as the length of a sequence in an event of this
2356 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2358 if (!event_header_type
) {
2359 fail("Failed to get event header type");
2363 integer_type
= bt_ctf_field_type_integer_create(13);
2364 if (!integer_type
) {
2365 fail("Failed to create length integer type");
2369 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2370 integer_type
, "seq_len");
2372 fail("Failed to add a new field to stream event header");
2376 event_class
= bt_ctf_event_class_create("sequence_event");
2378 fail("Failed to create event class");
2383 * This event's payload will contain a sequence which references
2384 * stream.event.header.seq_len as its length field.
2386 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2387 "stream.event.header.seq_len");
2388 if (!sequence_type
) {
2389 fail("Failed to create a sequence");
2393 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2396 fail("Failed to add a sequence to an event class");
2400 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2402 fail("Failed to add event class to stream class");
2406 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2408 fail("Failed to create stream")
2413 * We have defined a custom packet header field. We have to populate it
2416 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2417 if (!packet_header
) {
2418 fail("Failed to get stream packet header");
2422 integer
= bt_ctf_field_structure_get_field(packet_header
,
2423 "custom_trace_packet_header_field");
2425 fail("Failed to retrieve custom_trace_packet_header_field");
2429 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2431 fail("Failed to set custom_trace_packet_header_field value");
2436 event
= bt_ctf_event_create(event_class
);
2438 fail("Failed to create event");
2442 event_header
= bt_ctf_event_get_header(event
);
2443 if (!event_header
) {
2444 fail("Failed to get event header");
2448 integer
= bt_ctf_field_structure_get_field(event_header
,
2451 fail("Failed to get seq_len field from event header");
2455 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2457 fail("Failed to set seq_len value in event header");
2461 /* Populate both sequence integer fields */
2462 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2464 fail("Failed to retrieve sequence from event");
2468 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2470 fail("Failed to set sequence length");
2475 for (i
= 0; i
< 2; i
++) {
2476 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2478 fail("Failed to retrieve sequence element");
2482 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2484 fail("Failed to set sequence element value");
2492 ret
= bt_ctf_stream_append_event(stream
, event
);
2494 fail("Failed to append event to stream");
2498 ret
= bt_ctf_stream_flush(stream
);
2500 fail("Failed to flush custom_event_header stream");
2506 bt_put(stream_class
);
2507 bt_put(event_class
);
2511 bt_put(event_header
);
2512 bt_put(packet_header
);
2513 bt_put(sequence_type
);
2514 bt_put(integer_type
);
2515 bt_put(event_header_type
);
2518 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2521 struct bt_ctf_trace
*trace
= NULL
;
2522 struct bt_ctf_clock
*clock
= NULL
;
2523 struct bt_ctf_stream_class
*stream_class
= NULL
;
2524 struct bt_ctf_stream
*stream
= NULL
,
2526 struct bt_ctf_event_class
*event_class
= NULL
;
2527 struct bt_ctf_event
*event
= NULL
;
2528 struct bt_ctf_field_type
*integer_type
= NULL
;
2529 struct bt_ctf_field
*integer
= NULL
;
2531 trace
= bt_ctf_writer_get_trace(writer
);
2533 diag("Failed to get trace from writer");
2538 clock
= bt_ctf_trace_get_clock(trace
, 0);
2540 diag("Failed to get clock from trace");
2545 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2546 if (!stream_class
) {
2547 diag("Failed to create stream class");
2552 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2554 diag("Failed to set stream class clock");
2558 event_class
= bt_ctf_event_class_create("some_event_class_name");
2559 integer_type
= bt_ctf_field_type_integer_create(32);
2560 if (!integer_type
) {
2561 diag("Failed to create integer field type");
2566 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2569 diag("Failed to add field to event class");
2573 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2576 diag("Failed to add event class to stream class");
2579 event
= bt_ctf_event_create(event_class
);
2581 diag("Failed to create event");
2586 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2588 diag("Failed to get integer field payload from event");
2593 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2595 diag("Failed to set integer field value");
2599 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2601 diag("Failed to create writer stream");
2606 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2607 "bt_ctf_event_get_stream handles NULL correctly");
2608 ok(bt_ctf_event_get_stream(event
) == NULL
,
2609 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2611 ret
= bt_ctf_stream_append_event(stream
, event
);
2613 diag("Failed to append event to stream");
2617 ret_stream
= bt_ctf_event_get_stream(event
);
2618 ok(ret_stream
== stream
,
2619 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2622 "Create an event before instanciating its associated stream");
2626 bt_put(stream_class
);
2627 bt_put(event_class
);
2629 bt_put(integer_type
);
2634 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2636 struct bt_ctf_event_class
*event_class
;
2638 event_class
= bt_ctf_event_class_create("Simple Event");
2639 assert(event_class
);
2640 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2641 "two event classes with the same name cannot cohabit within the same stream class");
2642 bt_put(event_class
);
2644 event_class
= bt_ctf_event_class_create("different name, ok");
2645 assert(event_class
);
2646 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2647 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2648 "two event classes with the same ID cannot cohabit within the same stream class");
2649 bt_put(event_class
);
2652 int main(int argc
, char **argv
)
2654 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2655 char metadata_path
[sizeof(trace_path
) + 9];
2656 const char *clock_name
= "test_clock";
2657 const char *clock_description
= "This is a test clock";
2658 const char *returned_clock_name
;
2659 const char *returned_clock_description
;
2660 const uint64_t frequency
= 1123456789;
2661 const int64_t offset_s
= 1351530929945824323;
2662 const int64_t offset
= 1234567;
2663 int64_t get_offset_s
,
2666 const uint64_t precision
= 10;
2667 const int is_absolute
= 0xFF;
2668 char *metadata_string
;
2669 struct bt_ctf_writer
*writer
;
2670 struct utsname name
;
2671 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2672 struct bt_ctf_clock
*clock
, *ret_clock
;
2673 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2674 struct bt_ctf_stream
*stream1
;
2675 const char *ret_string
;
2676 const unsigned char *ret_uuid
;
2677 unsigned char tmp_uuid
[16] = { 0 };
2678 struct bt_ctf_field_type
*packet_context_type
,
2679 *packet_context_field_type
,
2680 *packet_header_type
,
2681 *packet_header_field_type
,
2683 *stream_event_context_type
,
2685 *event_header_field_type
;
2686 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2687 struct bt_ctf_trace
*trace
;
2689 int64_t ret_int64_t
;
2690 struct bt_value
*obj
;
2693 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2697 plan_tests(NR_TESTS
);
2699 if (!bt_mkdtemp(trace_path
)) {
2703 strcpy(metadata_path
, trace_path
);
2704 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2706 writer
= bt_ctf_writer_create(trace_path
);
2707 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2709 ok(!bt_ctf_writer_get_trace(NULL
),
2710 "bt_ctf_writer_get_trace correctly handles NULL");
2711 trace
= bt_ctf_writer_get_trace(writer
);
2713 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2714 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2715 "Set a trace's byte order to big endian");
2716 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2717 "bt_ctf_trace_get_byte_order returns a correct endianness");
2719 /* Add environment context to the trace */
2720 ret
= gethostname(hostname
, sizeof(hostname
));
2724 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2725 "Add host (%s) environment field to writer instance",
2727 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2729 "bt_ctf_writer_add_environment_field error with NULL writer");
2730 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2732 "bt_ctf_writer_add_environment_field error with NULL field name");
2733 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2735 "bt_ctf_writer_add_environment_field error with NULL field value");
2737 /* Test bt_ctf_trace_set_environment_field with an integer object */
2738 obj
= bt_value_integer_create_init(23);
2740 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2741 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2742 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2743 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2744 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2745 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2746 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2747 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2750 /* Test bt_ctf_trace_set_environment_field with a string object */
2751 obj
= bt_value_string_create_init("the value");
2753 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2754 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2757 /* Test bt_ctf_trace_set_environment_field_integer */
2758 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2760 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2761 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2762 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2763 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2765 "bt_ctf_trace_set_environment_field_integer succeeds");
2767 /* Test bt_ctf_trace_set_environment_field_string */
2768 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2770 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2771 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2772 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2773 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2775 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2776 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2778 "bt_ctf_trace_set_environment_field_string succeeds");
2780 /* Test bt_ctf_trace_get_environment_field_count */
2781 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2782 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2783 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2784 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2786 /* Test bt_ctf_trace_get_environment_field_name */
2787 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
2788 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2789 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
2790 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2791 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
2792 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2793 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
2794 ok(ret_string
&& !strcmp(ret_string
, "host"),
2795 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2796 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
2797 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2798 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2799 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
2800 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2801 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2802 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
2803 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2804 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2805 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
2806 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2807 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2809 /* Test bt_ctf_trace_get_environment_field_value */
2810 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
2811 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2812 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
2813 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2814 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
2815 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2816 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
2817 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2818 ok(!ret
&& ret_int64_t
== 23,
2819 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2821 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
2822 ret
= bt_value_string_get(obj
, &ret_string
);
2823 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2824 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2827 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2828 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2830 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2831 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
2832 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2833 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
2834 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2835 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
2837 ret
= bt_value_string_get(obj
, &ret_string
);
2838 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
2839 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2842 /* Test environment field replacement */
2843 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2845 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2846 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2847 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2848 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
2849 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2850 ok(!ret
&& ret_int64_t
== 654321,
2851 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2854 /* On Solaris, uname() can return any positive value on success */
2855 if (uname(&name
) < 0) {
2860 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
2861 == 0, "Add sysname (%s) environment field to writer instance",
2863 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
2864 name
.nodename
) == 0,
2865 "Add nodename (%s) environment field to writer instance",
2867 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
2868 == 0, "Add release (%s) environment field to writer instance",
2870 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
2871 == 0, "Add version (%s) environment field to writer instance",
2873 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
2874 == 0, "Add machine (%s) environment field to writer istance",
2877 /* Define a clock and add it to the trace */
2878 ok(bt_ctf_clock_create("signed") == NULL
,
2879 "Illegal clock name rejected");
2880 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
2881 clock
= bt_ctf_clock_create(clock_name
);
2882 ok(clock
, "Clock created sucessfully");
2883 returned_clock_name
= bt_ctf_clock_get_name(clock
);
2884 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
2885 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
2886 "Returned clock name is valid");
2888 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2889 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
2890 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
2891 "Clock description set successfully");
2893 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2894 ok(returned_clock_description
,
2895 "bt_ctf_clock_get_description returns a description.");
2896 ok(returned_clock_description
?
2897 !strcmp(returned_clock_description
, clock_description
) : 0,
2898 "Returned clock description is valid");
2900 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
2901 "bt_ctf_clock_get_frequency returns the correct default frequency");
2902 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
2903 "Set clock frequency");
2904 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
2905 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
2907 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
2908 "bt_ctf_clock_get_offset_s succeeds");
2909 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
2910 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
2911 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
2912 "Set clock offset (seconds)");
2913 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
2914 "bt_ctf_clock_get_offset_s succeeds");
2915 ok(get_offset_s
== offset_s
,
2916 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
2918 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
2919 "bt_ctf_clock_get_offset succeeds");
2920 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
2921 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
2922 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
2923 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
2924 "bt_ctf_clock_get_offset succeeds");
2925 ok(get_offset
== offset
,
2926 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
2928 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
2929 "bt_ctf_clock_get_precision returns the correct default precision");
2930 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
2931 "Set clock precision");
2932 ok(bt_ctf_clock_get_precision(clock
) == precision
,
2933 "bt_ctf_clock_get_precision returns the correct precision once it is set");
2935 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
2936 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
2937 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
2938 "Set clock absolute property");
2939 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
2940 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
2942 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
2943 "bt_ctf_clock_get_time succeeds");
2944 ok(get_time
== DEFAULT_CLOCK_TIME
,
2945 "bt_ctf_clock_get_time returns the correct default time");
2946 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
2948 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
2949 "bt_ctf_clock_get_time succeeds");
2950 ok(get_time
== current_time
,
2951 "bt_ctf_clock_get_time returns the correct time once it is set");
2953 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
2954 "Add clock to writer instance");
2955 ok(bt_ctf_writer_add_clock(writer
, clock
),
2956 "Verify a clock can't be added twice to a writer instance");
2958 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
2959 "bt_ctf_trace_get_clock_count correctly handles NULL");
2960 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
2961 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
2962 ok(!bt_ctf_trace_get_clock(NULL
, 0),
2963 "bt_ctf_trace_get_clock correctly handles NULL");
2964 ok(!bt_ctf_trace_get_clock(trace
, -1),
2965 "bt_ctf_trace_get_clock correctly handles negative indexes");
2966 ok(!bt_ctf_trace_get_clock(trace
, 1),
2967 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
2968 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
2969 ok(ret_clock
== clock
,
2970 "bt_ctf_trace_get_clock returns the right clock instance");
2972 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
2973 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
2974 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
2975 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
2976 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
2977 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
2978 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
2979 ok(ret_clock
== clock
,
2980 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
2982 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
2983 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
2985 ok(!bt_ctf_clock_get_name(NULL
),
2986 "bt_ctf_clock_get_name correctly handles NULL");
2987 ok(!bt_ctf_clock_get_description(NULL
),
2988 "bt_ctf_clock_get_description correctly handles NULL");
2989 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
2990 "bt_ctf_clock_get_frequency correctly handles NULL");
2991 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
2992 "bt_ctf_clock_get_precision correctly handles NULL");
2993 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
2994 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
2995 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
2996 "bt_ctf_clock_get_offset_s correctly handles NULL output");
2997 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
2998 "bt_ctf_clock_get_offset correctly handles NULL clock");
2999 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3000 "bt_ctf_clock_get_offset correctly handles NULL output");
3001 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3002 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3003 ok(bt_ctf_clock_get_time(NULL
, &get_time
) < 0,
3004 "bt_ctf_clock_get_time correctly handles NULL clock");
3005 ok(bt_ctf_clock_get_time(clock
, NULL
) < 0,
3006 "bt_ctf_clock_get_time correctly handles NULL output");
3008 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3009 "bt_ctf_clock_set_description correctly handles NULL clock");
3010 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3011 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3012 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3013 "bt_ctf_clock_get_precision correctly handles NULL clock");
3014 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3015 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3016 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3017 "bt_ctf_clock_set_offset correctly handles NULL clock");
3018 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3019 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3020 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3021 "bt_ctf_clock_set_time correctly handles NULL clock");
3022 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3023 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3024 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3026 "bt_ctf_clock_get_uuid returns a UUID");
3028 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3029 /* Slightly modify UUID */
3030 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3033 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3034 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3035 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3036 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3037 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3038 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3039 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3041 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3042 ok(uuid_match(ret_uuid
, tmp_uuid
),
3043 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3045 /* Define a stream class */
3046 stream_class
= bt_ctf_stream_class_create("test_stream");
3048 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3049 "bt_ctf_stream_class_get_name handles NULL correctly");
3050 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3051 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3052 "bt_ctf_stream_class_get_name returns a correct stream class name");
3054 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3055 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3056 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3057 "bt_ctf_stream_class_get_clock handles NULL correctly");
3059 ok(stream_class
, "Create stream class");
3060 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3061 "Set a stream class' clock");
3062 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3063 ok(ret_clock
== clock
,
3064 "bt_ctf_stream_class_get_clock returns a correct clock");
3067 /* Test the event fields and event types APIs */
3070 /* Test fields copying */
3073 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3074 "bt_ctf_stream_class_get_id returns an error when no id is set");
3075 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3076 "bt_ctf_stream_class_get_id handles NULL correctly");
3077 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3078 "bt_ctf_stream_class_set_id handles NULL correctly");
3079 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3080 "Set an stream class' id");
3081 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3082 "bt_ctf_stream_class_get_id returns the correct value");
3084 /* Validate default event header fields */
3085 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3086 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3087 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3090 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3091 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == CTF_TYPE_STRUCT
,
3092 "Default event header type is a structure");
3093 event_header_field_type
=
3094 bt_ctf_field_type_structure_get_field_type_by_name(
3095 ret_field_type
, "id");
3096 ok(event_header_field_type
,
3097 "Default event header type contains an \"id\" field");
3098 ok(bt_ctf_field_type_get_type_id(
3099 event_header_field_type
) == CTF_TYPE_INTEGER
,
3100 "Default event header \"id\" field is an integer");
3101 bt_put(event_header_field_type
);
3102 event_header_field_type
=
3103 bt_ctf_field_type_structure_get_field_type_by_name(
3104 ret_field_type
, "timestamp");
3105 ok(event_header_field_type
,
3106 "Default event header type contains a \"timestamp\" field");
3107 ok(bt_ctf_field_type_get_type_id(
3108 event_header_field_type
) == CTF_TYPE_INTEGER
,
3109 "Default event header \"timestamp\" field is an integer");
3110 bt_put(event_header_field_type
);
3111 bt_put(ret_field_type
);
3113 /* Add a custom trace packet header field */
3114 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3115 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3116 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3117 ok(packet_header_type
,
3118 "bt_ctf_trace_get_packet_header_type returns a packet header");
3119 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == CTF_TYPE_STRUCT
,
3120 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3121 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3122 packet_header_type
, "magic");
3123 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3124 bt_put(ret_field_type
);
3125 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3126 packet_header_type
, "uuid");
3127 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3128 bt_put(ret_field_type
);
3129 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3130 packet_header_type
, "stream_id");
3131 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3132 bt_put(ret_field_type
);
3134 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3135 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3136 packet_header_field_type
, "custom_trace_packet_header_field"),
3137 "Added a custom trace packet header field successfully");
3139 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3140 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3141 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3142 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3143 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3144 "Set a trace packet_header_type successfully");
3146 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3147 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3149 /* Add a custom field to the stream class' packet context */
3150 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3151 ok(packet_context_type
,
3152 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3153 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == CTF_TYPE_STRUCT
,
3154 "Packet context is a structure");
3156 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3157 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3158 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3159 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3161 integer_type
= bt_ctf_field_type_integer_create(32);
3162 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3164 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3165 /* Create a "uint5_t" equivalent custom packet context field */
3166 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3168 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3169 packet_context_field_type
, "custom_packet_context_field");
3170 ok(ret
== 0, "Packet context field added successfully");
3172 /* Define a stream event context containing a my_integer field. */
3173 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3174 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3175 ok(bt_ctf_stream_class_get_event_context_type(
3176 stream_class
) == NULL
,
3177 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3178 stream_event_context_type
= bt_ctf_field_type_structure_create();
3179 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3180 integer_type
, "common_event_context");
3182 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3183 stream_event_context_type
) < 0,
3184 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3185 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3187 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3188 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3190 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3192 ok(bt_ctf_stream_class_set_event_context_type(
3193 stream_class
, stream_event_context_type
) == 0,
3194 "Set a new stream event context type");
3195 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3197 ok(ret_field_type
== stream_event_context_type
,
3198 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3199 bt_put(ret_field_type
);
3201 /* Instantiate a stream and append events */
3202 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3203 ok(stream1
, "Instanciate a stream class from writer");
3205 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3206 "bt_ctf_stream_get_class correctly handles NULL");
3207 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3208 ok(ret_stream_class
,
3209 "bt_ctf_stream_get_class returns a stream class");
3210 ok(ret_stream_class
== stream_class
,
3211 "Returned stream class is of the correct type");
3214 * Try to modify the packet context type after a stream has been
3217 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3218 packet_header_field_type
, "should_fail");
3220 "Trace packet header type can't be modified once a stream has been instanciated");
3223 * Try to modify the packet context type after a stream has been
3226 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3227 packet_context_field_type
, "should_fail");
3229 "Packet context type can't be modified once a stream has been instanciated");
3232 * Try to modify the stream event context type after a stream has been
3235 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3236 integer_type
, "should_fail");
3238 "Stream event context type can't be modified once a stream has been instanciated");
3240 /* Should fail after instanciating a stream (frozen) */
3241 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3242 "Changes to a stream class that was already instantiated fail");
3244 /* Populate the custom packet header field only once for all tests */
3245 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3246 "bt_ctf_stream_get_packet_header handles NULL correctly");
3247 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3249 "bt_ctf_stream_get_packet_header returns a packet header");
3250 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3251 ok(ret_field_type
== packet_header_type
,
3252 "Stream returns a packet header of the appropriate type");
3253 bt_put(ret_field_type
);
3254 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3255 "custom_trace_packet_header_field");
3256 ok(packet_header_field
,
3257 "Packet header structure contains a custom field with the appropriate name");
3258 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3259 ok(ret_field_type
== packet_header_field_type
,
3260 "Custom packet header field is of the expected type");
3261 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3262 54321), "Set custom packet header value successfully");
3263 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3264 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3265 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3266 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3267 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3268 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3269 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3270 "Successfully set a stream's packet header");
3272 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3273 "Add environment field to writer after stream creation");
3275 test_instanciate_event_before_stream(writer
);
3277 append_simple_event(stream_class
, stream1
, clock
);
3279 packet_resize_test(stream_class
, stream1
, clock
);
3281 append_complex_event(stream_class
, stream1
, clock
);
3283 append_existing_event_class(stream_class
);
3285 test_empty_stream(writer
);
3287 test_custom_event_header_stream(writer
);
3289 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3290 ok(metadata_string
, "Get metadata string");
3292 bt_ctf_writer_flush_metadata(writer
);
3293 validate_metadata(argv
[1], metadata_path
);
3294 validate_trace(argv
[2], trace_path
);
3297 bt_put(ret_stream_class
);
3300 bt_put(packet_context_type
);
3301 bt_put(packet_context_field_type
);
3302 bt_put(integer_type
);
3303 bt_put(stream_event_context_type
);
3304 bt_put(ret_field_type
);
3305 bt_put(packet_header_type
);
3306 bt_put(packet_header_field_type
);
3307 bt_put(packet_header
);
3308 bt_put(packet_header_field
);
3310 free(metadata_string
);
3311 bt_put(stream_class
);
3313 /* Remove all trace files and delete temporary trace directory */
3314 DIR *trace_dir
= opendir(trace_path
);
3316 perror("# opendir");
3320 struct dirent
*entry
;
3321 while ((entry
= readdir(trace_dir
))) {
3323 char filename
[PATH_MAX
];
3325 if (snprintf(filename
, sizeof(filename
), "%s/%s",
3326 trace_path
, entry
->d_name
) <= 0) {
3330 if (stat(entry
->d_name
, &st
)) {
3334 if (S_ISREG(st
.st_mode
)) {
3335 unlinkat(bt_dirfd(trace_dir
), entry
->d_name
, 0);
3340 closedir(trace_dir
);