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
59 #define DEFAULT_CLOCK_VALUE 0
63 static int64_t current_time
= 42;
65 /* Return 1 if uuids match, zero if different. */
66 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
71 if (!uuid_a
|| !uuid_b
) {
75 for (i
= 0; i
< 16; i
++) {
76 if (uuid_a
[i
] != uuid_b
[i
]) {
86 void validate_metadata(char *parser_path
, char *metadata_path
)
89 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
90 int parser_output_fd
= -1, metadata_fd
= -1;
97 parser_output_fd
= mkstemp(parser_output_path
);
98 metadata_fd
= open(metadata_path
, O_RDONLY
);
100 unlink(parser_output_path
);
102 if (parser_output_fd
== -1 || metadata_fd
== -1) {
103 diag("Failed create temporary files for metadata parsing.");
111 waitpid(pid
, &status
, 0);
112 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
114 /* ctf-parser-test expects a metadata string on stdin. */
115 ret
= dup2(metadata_fd
, STDIN_FILENO
);
117 perror("# dup2 metadata_fd to STDIN");
121 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
123 perror("# dup2 parser_output_fd to STDOUT");
127 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
129 perror("# dup2 parser_output_fd to STDERR");
133 execl(parser_path
, "ctf-parser-test", NULL
);
134 perror("# Could not launch the ctf metadata parser process");
138 ok(ret
== 0, "Metadata string is valid");
140 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
142 size_t len
= METADATA_LINE_SIZE
;
143 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
145 metadata_fp
= fdopen(metadata_fd
, "r");
147 perror("fdopen on metadata_fd");
152 parser_output_fp
= fdopen(parser_output_fd
, "r");
153 if (!parser_output_fp
) {
154 perror("fdopen on parser_output_fd");
157 parser_output_fd
= -1;
161 diag("malloc failure");
166 /* Output the metadata and parser output as diagnostic */
167 while (bt_getline(&line
, &len
, metadata_fp
) > 0) {
168 fprintf(stderr
, "# %s", line
);
171 rewind(parser_output_fp
);
172 while (bt_getline(&line
, &len
, parser_output_fp
) > 0) {
173 fprintf(stderr
, "# %s", line
);
179 if (fclose(metadata_fp
)) {
180 diag("fclose failure");
183 if (parser_output_fp
) {
184 if (fclose(parser_output_fp
)) {
185 diag("fclose failure");
190 if (parser_output_fd
>= 0) {
191 if (close(parser_output_fd
)) {
195 if (metadata_fd
>= 0) {
196 if (close(metadata_fd
)) {
202 void validate_trace(char *parser_path
, char *trace_path
)
205 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
206 int babeltrace_output_fd
= -1;
213 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
214 unlink(babeltrace_output_path
);
216 if (babeltrace_output_fd
== -1) {
217 diag("Failed to create a temporary file for trace parsing.");
225 waitpid(pid
, &status
, 0);
226 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
228 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
230 perror("# dup2 babeltrace_output_fd to STDOUT");
234 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
236 perror("# dup2 babeltrace_output_fd to STDERR");
240 execl(parser_path
, "babeltrace", trace_path
, NULL
);
241 perror("# Could not launch the babeltrace process");
245 ok(ret
== 0, "Babeltrace could read the resulting trace");
247 if (ret
&& babeltrace_output_fd
>= 0) {
249 size_t len
= METADATA_LINE_SIZE
;
250 FILE *babeltrace_output_fp
= NULL
;
252 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
253 if (!babeltrace_output_fp
) {
254 perror("fdopen on babeltrace_output_fd");
257 babeltrace_output_fd
= -1;
261 diag("malloc error");
263 rewind(babeltrace_output_fp
);
264 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
270 if (babeltrace_output_fp
) {
271 if (fclose(babeltrace_output_fp
)) {
272 diag("fclose error");
277 if (babeltrace_output_fd
>= 0) {
278 if (close(babeltrace_output_fd
)) {
284 void event_copy_tests(struct bt_ctf_event
*event
)
286 struct bt_ctf_event
*copy
;
287 struct bt_ctf_event_class
*orig_event_class
;
288 struct bt_ctf_event_class
*copy_event_class
;
289 struct bt_ctf_stream
*orig_stream
;
290 struct bt_ctf_stream
*copy_stream
;
291 struct bt_ctf_field
*orig_field
;
292 struct bt_ctf_field
*copy_field
;
295 ok(!bt_ctf_event_copy(NULL
),
296 "bt_ctf_event_copy handles NULL correctly");
297 copy
= bt_ctf_event_copy(event
);
298 ok(copy
, "bt_ctf_event_copy returns a valid pointer");
300 /* validate event class */
301 orig_event_class
= bt_ctf_event_get_class(event
);
302 assert(orig_event_class
);
303 copy_event_class
= bt_ctf_event_get_class(copy
);
304 ok(orig_event_class
== copy_event_class
,
305 "original and copied events share the same event class pointer");
306 bt_put(orig_event_class
);
307 bt_put(copy_event_class
);
309 /* validate stream */
310 orig_stream
= bt_ctf_event_get_stream(event
);
311 copy_stream
= bt_ctf_event_get_stream(copy
);
314 ok(!copy_stream
, "original and copied events have no stream");
316 ok(orig_stream
== copy_stream
,
317 "original and copied events share the same stream pointer");
323 orig_field
= bt_ctf_event_get_header(event
);
324 copy_field
= bt_ctf_event_get_header(copy
);
327 ok(!copy_field
, "original and copied events have no header");
329 ok(orig_field
!= copy_field
,
330 "original and copied events headers are different pointers");
337 orig_field
= bt_ctf_event_get_event_context(event
);
338 copy_field
= bt_ctf_event_get_event_context(copy
);
341 ok(!copy_field
, "original and copied events have no context");
343 ok(orig_field
!= copy_field
,
344 "original and copied events contexts are different pointers");
351 orig_field
= bt_ctf_event_get_payload_field(event
);
352 copy_field
= bt_ctf_event_get_payload_field(copy
);
355 ok(!copy_field
, "original and copied events have no payload");
357 ok(orig_field
!= copy_field
,
358 "original and copied events payloads are different pointers");
367 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
368 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
370 /* Create and add a simple event class */
371 struct bt_ctf_event_class
*simple_event_class
=
372 bt_ctf_event_class_create("Simple Event");
373 struct bt_ctf_field_type
*uint_12_type
=
374 bt_ctf_field_type_integer_create(12);
375 struct bt_ctf_field_type
*int_64_type
=
376 bt_ctf_field_type_integer_create(64);
377 struct bt_ctf_field_type
*float_type
=
378 bt_ctf_field_type_floating_point_create();
379 struct bt_ctf_field_type
*enum_type
;
380 struct bt_ctf_field_type
*enum_type_unsigned
=
381 bt_ctf_field_type_enumeration_create(uint_12_type
);
382 struct bt_ctf_field_type
*event_context_type
=
383 bt_ctf_field_type_structure_create();
384 struct bt_ctf_field_type
*event_payload_type
= NULL
;
385 struct bt_ctf_field_type
*returned_type
;
386 struct bt_ctf_event
*simple_event
;
387 struct bt_ctf_field
*integer_field
;
388 struct bt_ctf_field
*float_field
;
389 struct bt_ctf_field
*enum_field
;
390 struct bt_ctf_field
*enum_field_unsigned
;
391 struct bt_ctf_field
*enum_container_field
;
392 const char *mapping_name_test
= "truie";
393 const double double_test_value
= 3.1415;
394 struct bt_ctf_field
*enum_container_field_unsigned
;
395 const char *mapping_name_negative_test
= "negative_value";
396 const char *ret_char
;
398 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
399 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
400 struct bt_ctf_clock
*ret_clock
;
401 struct bt_ctf_event_class
*ret_event_class
;
402 struct bt_ctf_field
*packet_context
;
403 struct bt_ctf_field
*packet_context_field
;
404 struct bt_ctf_field
*stream_event_context
;
405 struct bt_ctf_field
*stream_event_context_field
;
406 struct bt_ctf_field
*event_context
;
407 struct bt_ctf_field
*event_context_field
;
408 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
409 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
410 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
413 ok(uint_12_type
, "Create an unsigned integer type");
415 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
416 ok(int_64_type
, "Create a signed integer type");
417 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
419 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
420 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
421 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
422 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
423 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
424 bt_put(returned_type
);
426 bt_ctf_field_type_set_alignment(float_type
, 32);
427 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
428 "bt_ctf_field_type_get_alignment handles NULL correctly");
429 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
430 "bt_ctf_field_type_get_alignment returns a correct value");
432 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
433 "Set a floating point type's exponent digit count");
434 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
435 "Set a floating point type's mantissa digit count");
437 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
438 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
439 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
440 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
441 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
442 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
443 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
444 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
446 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
447 mapping_name_negative_test
, -12345, 0) == 0,
448 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
449 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
450 "escaping; \"test\"", 1, 1) == 0,
451 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
452 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
453 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
454 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
455 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
456 "event clock int float", 5, 22) == 0,
457 "Accept enumeration mapping strings containing reserved keywords");
458 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
460 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
461 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
462 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
463 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
464 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
465 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
466 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
468 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
469 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
470 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
471 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
472 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
473 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
475 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
476 "enum_field") == 0, "Add signed enumeration field to event");
478 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
479 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
480 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
481 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
482 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
483 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
484 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
485 NULL
, &ret_range_end_int64_t
) < 0,
486 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
487 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
488 &ret_range_start_int64_t
, NULL
) < 0,
489 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
490 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
491 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
492 "bt_ctf_field_type_enumeration_get_mapping returns a value");
493 ok(!strcmp(ret_char
, mapping_name_test
),
494 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
495 ok(ret_range_start_int64_t
== 42,
496 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
497 ok(ret_range_end_int64_t
== 42,
498 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
500 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
501 "escaping; \"test\"", 0, 0) == 0,
502 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
503 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
504 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
505 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
506 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
507 "event clock int float", 5, 22) == 0,
508 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
509 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
511 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
512 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
513 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
514 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
515 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
516 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
517 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
518 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
520 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
521 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
522 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
523 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
525 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
526 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
527 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
528 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
529 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
530 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
531 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
532 NULL
, &ret_range_end_uint64_t
) < 0,
533 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
534 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
535 &ret_range_start_uint64_t
, NULL
) < 0,
536 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
537 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
538 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
539 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
540 ok(!strcmp(ret_char
, mapping_name_test
),
541 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
542 ok(ret_range_start_uint64_t
== 42,
543 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
544 ok(ret_range_end_uint64_t
== 42,
545 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
547 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
549 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
552 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
554 /* Set an event context type which will contain a single integer*/
555 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
556 "event_specific_context"),
557 "Add event specific context field");
558 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
559 "bt_ctf_event_class_get_context_type handles NULL correctly");
560 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
561 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
563 ok(bt_ctf_event_class_set_context_type(simple_event_class
, NULL
) < 0,
564 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
565 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
566 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
567 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
568 "Set an event class' context type successfully");
569 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
570 ok(returned_type
== event_context_type
,
571 "bt_ctf_event_class_get_context_type returns the appropriate type");
572 bt_put(returned_type
);
574 bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
);
577 * bt_ctf_stream_class_add_event_class() copies the field types
578 * of simple_event_class, so we retrieve the new ones to create
579 * the appropriate fields.
581 BT_PUT(event_context_type
);
582 BT_PUT(event_payload_type
);
583 event_payload_type
= bt_ctf_event_class_get_payload_type(
585 assert(event_payload_type
);
586 event_context_type
= bt_ctf_event_class_get_context_type(
588 assert(event_context_type
);
589 ep_integer_field_type
=
590 bt_ctf_field_type_structure_get_field_type_by_name(
591 event_payload_type
, "integer_field");
592 assert(ep_integer_field_type
);
594 bt_ctf_field_type_structure_get_field_type_by_name(
595 event_payload_type
, "enum_field");
596 assert(ep_enum_field_type
);
597 ep_enum_field_unsigned_type
=
598 bt_ctf_field_type_structure_get_field_type_by_name(
599 event_payload_type
, "enum_field_unsigned");
600 assert(ep_enum_field_unsigned_type
);
602 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
603 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
604 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
605 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
606 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
607 "bt_ctf_stream_class_get_event_class handles NULL correctly");
608 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
609 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
610 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
611 ok(ret_event_class
== simple_event_class
,
612 "bt_ctf_stream_class_get_event_class returns the correct event class");
613 bt_put(ret_event_class
);
614 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
615 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
616 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
617 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
619 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
620 ok(ret_event_class
== simple_event_class
,
621 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
622 bt_put(ret_event_class
);
624 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
625 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
626 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
627 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
628 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
629 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
630 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
631 ok(ret_event_class
== simple_event_class
,
632 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
633 bt_put(ret_event_class
);
635 simple_event
= bt_ctf_event_create(simple_event_class
);
637 "Instantiate an event containing a single integer field");
639 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
640 "bt_ctf_event_get_clock handles NULL correctly");
641 ret_clock
= bt_ctf_event_get_clock(simple_event
);
642 ok(ret_clock
== clock
,
643 "bt_ctf_event_get_clock returns a correct clock");
646 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
647 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
648 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
649 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
651 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
652 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
653 "bt_ctf_field_floating_point_get_value fails on an unset float field");
654 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
655 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
656 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
657 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
658 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
659 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
660 "bt_ctf_field_floating_point_get_value returns a double value");
661 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
662 "bt_ctf_field_floating_point_get_value returns a correct value");
664 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
666 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
667 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
668 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
669 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
670 enum_container_field
= bt_ctf_field_enumeration_get_container(
672 ok(bt_ctf_field_signed_integer_set_value(
673 enum_container_field
, -42) == 0,
674 "Set signed enumeration container value");
675 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
676 ok(!strcmp(ret_char
, mapping_name_negative_test
),
677 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
678 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
681 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
682 assert(enum_field_unsigned
);
683 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
684 enum_field_unsigned
);
685 ok(bt_ctf_field_unsigned_integer_set_value(
686 enum_container_field_unsigned
, 42) == 0,
687 "Set unsigned enumeration container value");
688 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
689 enum_field_unsigned
);
691 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
692 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
693 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
695 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
697 /* Populate stream event context */
698 stream_event_context
=
699 bt_ctf_event_get_stream_event_context(simple_event
);
700 assert(stream_event_context
);
701 stream_event_context_field
= bt_ctf_field_structure_get_field(
702 stream_event_context
, "common_event_context");
703 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
705 /* Populate the event's context */
706 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
707 "bt_ctf_event_get_event_context handles NULL correctly");
708 event_context
= bt_ctf_event_get_event_context(simple_event
);
710 "bt_ctf_event_get_event_context returns a field");
711 returned_type
= bt_ctf_field_get_type(event_context
);
712 ok(returned_type
== event_context_type
,
713 "bt_ctf_event_get_event_context returns a field of the appropriate type");
714 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
715 "event_specific_context");
716 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
717 "Successfully set an event context's value");
718 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
719 "bt_ctf_event_set_event_context handles a NULL event correctly");
720 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
721 "bt_ctf_event_set_event_context handles a NULL event context correctly");
722 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
723 "bt_ctf_event_set_event_context rejects a context of the wrong type");
724 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
725 "Set an event context successfully");
727 event_copy_tests(simple_event
);
728 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
729 "Append simple event to trace stream");
731 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
732 "bt_ctf_stream_get_packet_context handles NULL correctly");
733 packet_context
= bt_ctf_stream_get_packet_context(stream
);
735 "bt_ctf_stream_get_packet_context returns a packet context");
737 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
739 ok(packet_context_field
,
740 "Packet context contains the default packet_size field.");
741 bt_put(packet_context_field
);
742 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
743 "custom_packet_context_field");
744 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
745 "Custom packet context field value successfully set.");
747 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
748 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
749 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
750 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
751 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
752 "Successfully set a stream's packet context");
754 ok(bt_ctf_stream_flush(stream
) == 0,
755 "Flush trace stream with one event");
757 bt_put(simple_event_class
);
758 bt_put(simple_event
);
759 bt_put(uint_12_type
);
763 bt_put(enum_type_unsigned
);
764 bt_put(returned_type
);
765 bt_put(event_context_type
);
766 bt_put(integer_field
);
769 bt_put(enum_field_unsigned
);
770 bt_put(enum_container_field
);
771 bt_put(enum_container_field_unsigned
);
772 bt_put(packet_context
);
773 bt_put(packet_context_field
);
774 bt_put(stream_event_context
);
775 bt_put(stream_event_context_field
);
776 bt_put(event_context
);
777 bt_put(event_context_field
);
778 bt_put(event_payload_type
);
779 bt_put(ep_integer_field_type
);
780 bt_put(ep_enum_field_type
);
781 bt_put(ep_enum_field_unsigned_type
);
784 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
785 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
787 struct event_class_attrs_counts
{
798 struct event_class_attrs_counts
;
799 const char *complex_test_event_string
= "Complex Test Event";
800 const char *test_string_1
= "Test ";
801 const char *test_string_2
= "string ";
802 const char *test_string_3
= "abcdefghi";
803 const char *test_string_4
= "abcd\0efg\0hi";
804 const char *test_string_cat
= "Test string abcdeabcd";
805 struct bt_ctf_field_type
*uint_35_type
=
806 bt_ctf_field_type_integer_create(35);
807 struct bt_ctf_field_type
*int_16_type
=
808 bt_ctf_field_type_integer_create(16);
809 struct bt_ctf_field_type
*uint_3_type
=
810 bt_ctf_field_type_integer_create(3);
811 struct bt_ctf_field_type
*enum_variant_type
=
812 bt_ctf_field_type_enumeration_create(uint_3_type
);
813 struct bt_ctf_field_type
*variant_type
=
814 bt_ctf_field_type_variant_create(enum_variant_type
,
816 struct bt_ctf_field_type
*string_type
=
817 bt_ctf_field_type_string_create();
818 struct bt_ctf_field_type
*sequence_type
;
819 struct bt_ctf_field_type
*array_type
;
820 struct bt_ctf_field_type
*inner_structure_type
=
821 bt_ctf_field_type_structure_create();
822 struct bt_ctf_field_type
*complex_structure_type
=
823 bt_ctf_field_type_structure_create();
824 struct bt_ctf_field_type
*ret_field_type
;
825 struct bt_ctf_event_class
*event_class
;
826 struct bt_ctf_event
*event
;
827 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
828 *inner_structure_field
, *complex_structure_field
,
829 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
830 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
831 *stream_event_ctx_int_field
, *ret_field
;
832 uint64_t ret_unsigned_int
;
833 int64_t ret_signed_int
;
834 const char *ret_string
;
835 struct bt_ctf_stream_class
*ret_stream_class
;
836 struct bt_ctf_event_class
*ret_event_class
;
837 struct bt_ctf_field
*packet_context
, *packet_context_field
;
838 struct bt_value
*obj
;
840 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
841 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
842 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
843 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
844 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
845 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
846 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
847 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
848 bt_ctf_field_type_set_alignment(int_16_type
, 32);
849 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
850 bt_ctf_field_type_integer_set_base(uint_35_type
,
851 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
853 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
854 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
857 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
858 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
859 ret_field_type
= bt_ctf_field_type_array_get_element_type(
861 ok(ret_field_type
== int_16_type
,
862 "bt_ctf_field_type_array_get_element_type returns the correct type");
863 bt_put(ret_field_type
);
865 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
866 "bt_ctf_field_type_array_get_length handles NULL correctly");
867 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
868 "bt_ctf_field_type_array_get_length returns the correct length");
870 bt_ctf_field_type_structure_add_field(inner_structure_type
,
871 uint_35_type
, "seq_len");
872 bt_ctf_field_type_structure_add_field(inner_structure_type
,
873 sequence_type
, "a_sequence");
874 bt_ctf_field_type_structure_add_field(inner_structure_type
,
875 array_type
, "an_array");
877 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
879 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
881 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
882 "UINT35_TYPE", 2, 7);
884 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
886 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
887 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
888 enum_variant_type
, NULL
) < 0,
889 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
890 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
891 enum_variant_type
, "INT16_TYPE") == 1,
892 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
894 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
895 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
896 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
897 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
898 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
899 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
901 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
902 "An unknown entry"), "Reject a variant field based on an unknown tag value");
903 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
904 "UINT3_TYPE") == 0, "Add a field to a variant");
905 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
907 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
910 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
911 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
912 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
913 ok(ret_field_type
== enum_variant_type
,
914 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
915 bt_put(ret_field_type
);
917 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
918 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
919 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
920 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
921 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
922 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
923 "INT16_TYPE") == NULL
,
924 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
925 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
927 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
928 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
929 variant_type
, "INT16_TYPE");
930 ok(ret_field_type
== int_16_type
,
931 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
932 bt_put(ret_field_type
);
934 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
935 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
936 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
937 "bt_ctf_field_type_variant_get_field_count returns the correct count");
939 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
940 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
941 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
942 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
943 bt_put(ret_field_type
);
944 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
945 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
946 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
947 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
948 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
949 "bt_ctf_field_type_variant_get_field returns a field");
950 ok(!strcmp("INT16_TYPE", ret_string
),
951 "bt_ctf_field_type_variant_get_field returns a correct field name");
952 ok(ret_field_type
== int_16_type
,
953 "bt_ctf_field_type_variant_get_field returns a correct field type");
954 bt_put(ret_field_type
);
956 bt_ctf_field_type_structure_add_field(complex_structure_type
,
957 enum_variant_type
, "variant_selector");
958 bt_ctf_field_type_structure_add_field(complex_structure_type
,
959 string_type
, "a_string");
960 bt_ctf_field_type_structure_add_field(complex_structure_type
,
961 variant_type
, "variant_value");
962 bt_ctf_field_type_structure_add_field(complex_structure_type
,
963 inner_structure_type
, "inner_structure");
965 ok(bt_ctf_event_class_create("clock") == NULL
,
966 "Reject creation of an event class with an illegal name");
967 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
968 ok(event_class
, "Create an event class");
969 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
970 "Reject addition of a field with an empty name to an event");
971 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
972 "Reject addition of a field with a NULL type to an event");
973 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
975 "Reject addition of a type with an illegal name to an event");
976 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
978 "Add field of type unsigned integer to an event");
979 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
980 "int_16") == 0, "Add field of type signed integer to an event");
981 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
982 "complex_structure") == 0,
983 "Add composite structure to an event");
985 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
986 "bt_ctf_event_class_get_name handles NULL correctly");
987 ret_string
= bt_ctf_event_class_get_name(event_class
);
988 ok(!strcmp(ret_string
, complex_test_event_string
),
989 "bt_ctf_event_class_get_name returns a correct name");
990 ok(bt_ctf_event_class_get_id(event_class
) < 0,
991 "bt_ctf_event_class_get_id returns a negative value when not set");
992 ok(bt_ctf_event_class_get_id(NULL
) < 0,
993 "bt_ctf_event_class_get_id handles NULL correctly");
994 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
995 "bt_ctf_event_class_set_id handles NULL correctly");
996 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
997 "Set an event class' id");
998 ok(bt_ctf_event_class_get_id(event_class
) == 42,
999 "bt_ctf_event_class_get_id returns the correct value");
1001 /* Test event class attributes */
1002 obj
= bt_value_integer_create_init(15);
1004 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
1005 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
1006 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
1007 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
1008 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
1009 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
1010 assert(!bt_value_integer_set(obj
, -3));
1011 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
1012 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
1013 assert(!bt_value_integer_set(obj
, 11));
1014 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
1015 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
1016 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
1017 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
1018 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
1020 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
1023 obj
= bt_value_integer_create_init(5);
1025 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
1026 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
1028 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
1029 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
1030 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
1031 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
1032 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
1033 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
1034 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
1037 ret
= bt_value_integer_get(obj
, &int64_value
);
1038 ok(obj
&& !ret
&& int64_value
== 5,
1039 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
1042 obj
= bt_value_string_create_init("nu name");
1044 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
1045 ret_string
= bt_ctf_event_class_get_name(event_class
);
1046 ok(!strcmp(ret_string
, "nu name"),
1047 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
1048 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
1049 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
1051 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
1053 obj
= bt_value_string_create_init("http://kernel.org/");
1055 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
1058 ok(bt_ctf_event_class_get_attribute_count(NULL
),
1059 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1060 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
1061 "bt_ctf_event_class_get_attribute_count returns the correct count");
1062 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
1063 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1064 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
1065 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1066 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
1067 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1068 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
1069 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1071 memset(&attrs_count
, 0, sizeof(attrs_count
));
1073 for (i
= 0; i
< 4; ++i
) {
1074 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
1076 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
1077 assert(ret_string
&& obj
);
1079 if (!strcmp(ret_string
, "id")) {
1081 ok(bt_value_is_integer(obj
),
1082 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1084 } else if (!strcmp(ret_string
, "name")) {
1086 ok(bt_value_is_string(obj
),
1087 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1089 } else if (!strcmp(ret_string
, "loglevel")) {
1090 attrs_count
.loglevel
++;
1091 ok(bt_value_is_integer(obj
),
1092 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1094 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1095 attrs_count
.modelemfuri
++;
1096 ok(bt_value_is_string(obj
),
1097 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1100 attrs_count
.unknown
++;
1106 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1107 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1108 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1109 "event class has one instance of each known attribute");
1111 /* Add event class to the stream class */
1112 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1113 "Reject addition of NULL event class to a stream class");
1114 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1115 event_class
) == 0, "Add an event class to stream class");
1117 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1118 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1119 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1120 ok(ret_stream_class
== stream_class
,
1121 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1122 bt_put(ret_stream_class
);
1124 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1125 "bt_ctf_event_class_get_field_count handles NULL correctly");
1126 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1127 "bt_ctf_event_class_get_field_count returns a correct value");
1129 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1130 &ret_field_type
, 0) < 0,
1131 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1132 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1133 &ret_field_type
, 0) == 0,
1134 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1135 bt_put(ret_field_type
);
1136 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1138 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1139 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1140 &ret_field_type
, 42) < 0,
1141 "bt_ctf_event_class_get_field handles an invalid index correctly");
1142 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1143 &ret_field_type
, 0) == 0,
1144 "bt_ctf_event_class_get_field returns a field");
1145 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
1146 "bt_ctf_event_class_get_field returns a correct field type");
1147 bt_put(ret_field_type
);
1148 ok(!strcmp(ret_string
, "uint_35"),
1149 "bt_ctf_event_class_get_field returns a correct field name");
1150 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1151 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1152 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1153 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1154 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1155 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1156 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1157 "complex_structure");
1158 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
1159 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1160 bt_put(ret_field_type
);
1162 event
= bt_ctf_event_create(event_class
);
1163 ok(event
, "Instanciate a complex event");
1165 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1166 "bt_ctf_event_get_class handles NULL correctly");
1167 ret_event_class
= bt_ctf_event_get_class(event
);
1168 ok(ret_event_class
== event_class
,
1169 "bt_ctf_event_get_class returns the correct event class");
1170 bt_put(ret_event_class
);
1172 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1173 if (!uint_35_field
) {
1174 printf("uint_35_field is NULL\n");
1177 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1178 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1179 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1180 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1181 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1182 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1183 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1184 &ret_unsigned_int
) == 0,
1185 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1186 ok(ret_unsigned_int
== 0x0DDF00D,
1187 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1188 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1189 &ret_signed_int
) < 0,
1190 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1191 bt_put(uint_35_field
);
1193 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1194 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1195 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1196 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1197 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1198 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1199 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1200 &ret_signed_int
) == 0,
1201 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1202 ok(ret_signed_int
== -12345,
1203 "bt_ctf_field_signed_integer_get_value returns the correct value");
1204 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1205 &ret_unsigned_int
) < 0,
1206 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1207 bt_put(int_16_field
);
1209 complex_structure_field
= bt_ctf_event_get_payload(event
,
1210 "complex_structure");
1212 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1213 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1214 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1215 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1216 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1217 complex_structure_field
, 3);
1218 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1219 bt_put(inner_structure_field
);
1220 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1221 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1222 bt_put(ret_field_type
);
1224 inner_structure_field
= bt_ctf_field_structure_get_field(
1225 complex_structure_field
, "inner_structure");
1226 a_string_field
= bt_ctf_field_structure_get_field(
1227 complex_structure_field
, "a_string");
1228 enum_variant_field
= bt_ctf_field_structure_get_field(
1229 complex_structure_field
, "variant_selector");
1230 variant_field
= bt_ctf_field_structure_get_field(
1231 complex_structure_field
, "variant_value");
1232 uint_35_field
= bt_ctf_field_structure_get_field(
1233 inner_structure_field
, "seq_len");
1234 a_sequence_field
= bt_ctf_field_structure_get_field(
1235 inner_structure_field
, "a_sequence");
1236 an_array_field
= bt_ctf_field_structure_get_field(
1237 inner_structure_field
, "an_array");
1239 enum_container_field
= bt_ctf_field_enumeration_get_container(
1240 enum_variant_field
);
1241 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1242 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1243 enum_variant_field
);
1244 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1245 bt_put(int_16_field
);
1246 ok(!bt_ctf_field_string_get_value(a_string_field
),
1247 "bt_ctf_field_string_get_value returns NULL on an unset field");
1248 bt_ctf_field_string_set_value(a_string_field
,
1250 ok(!bt_ctf_field_string_get_value(NULL
),
1251 "bt_ctf_field_string_get_value correctly handles NULL");
1252 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1253 "bt_ctf_field_string_append correctly handles a NULL string field");
1254 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1255 "bt_ctf_field_string_append correctly handles a NULL string value");
1256 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1257 "bt_ctf_field_string_append succeeds");
1258 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1259 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1260 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1261 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1262 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1263 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1264 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1265 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1266 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1267 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1268 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1269 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1271 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1272 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1273 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1274 "bt_ctf_field_string_get_value returns a correct value");
1275 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1276 SEQUENCE_TEST_LENGTH
);
1278 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1279 enum_container_field
) == NULL
,
1280 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1281 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1283 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1284 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1285 variant_type
, enum_variant_field
);
1286 ok(ret_field_type
== int_16_type
,
1287 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1289 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1290 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1291 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1292 uint_35_field
) == 0, "Set a sequence field's length");
1293 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1294 ok(ret_field
== uint_35_field
,
1295 "bt_ctf_field_sequence_get_length returns the correct length field");
1296 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1297 "bt_ctf_field_sequence_get_length properly handles NULL");
1299 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1300 int_16_field
= bt_ctf_field_sequence_get_field(
1301 a_sequence_field
, i
);
1302 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1303 bt_put(int_16_field
);
1306 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1307 int_16_field
= bt_ctf_field_array_get_field(
1309 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1310 bt_put(int_16_field
);
1313 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1314 assert(stream_event_ctx_field
);
1315 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1316 stream_event_ctx_field
, "common_event_context");
1317 BT_PUT(stream_event_ctx_field
);
1318 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1319 BT_PUT(stream_event_ctx_int_field
);
1321 bt_ctf_clock_set_time(clock
, ++current_time
);
1322 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1323 "Append a complex event to a stream");
1326 * Populate the custom packet context field with a dummy value
1327 * otherwise flush will fail.
1329 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1330 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1331 "custom_packet_context_field");
1332 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1334 ok(bt_ctf_stream_flush(stream
) == 0,
1335 "Flush a stream containing a complex event");
1337 bt_put(uint_35_field
);
1338 bt_put(a_string_field
);
1339 bt_put(inner_structure_field
);
1340 bt_put(complex_structure_field
);
1341 bt_put(a_sequence_field
);
1342 bt_put(an_array_field
);
1343 bt_put(enum_variant_field
);
1344 bt_put(enum_container_field
);
1345 bt_put(variant_field
);
1347 bt_put(packet_context_field
);
1348 bt_put(packet_context
);
1349 bt_put(uint_35_type
);
1350 bt_put(int_16_type
);
1351 bt_put(string_type
);
1352 bt_put(sequence_type
);
1354 bt_put(inner_structure_type
);
1355 bt_put(complex_structure_type
);
1356 bt_put(uint_3_type
);
1357 bt_put(enum_variant_type
);
1358 bt_put(variant_type
);
1359 bt_put(ret_field_type
);
1360 bt_put(event_class
);
1364 static void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1365 struct bt_ctf_field_type
*expected_type
, const char *name
)
1367 struct bt_ctf_field_type
*copy_type
;
1369 copy_type
= bt_ctf_field_get_type(field
);
1370 ok(copy_type
== expected_type
,
1371 "bt_ctf_field_copy does not copy the type (%s)", name
);
1375 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1376 struct bt_ctf_field
*field_b
, const char *name
)
1378 ok(field_a
!= field_b
,
1379 "bt_ctf_field_copy creates different pointers (%s)", name
);
1382 void field_copy_tests()
1384 struct bt_ctf_field_type
*len_type
= NULL
;
1385 struct bt_ctf_field_type
*fp_type
= NULL
;
1386 struct bt_ctf_field_type
*s_type
= NULL
;
1387 struct bt_ctf_field_type
*e_int_type
= NULL
;
1388 struct bt_ctf_field_type
*e_type
= NULL
;
1389 struct bt_ctf_field_type
*v_type
= NULL
;
1390 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1391 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1392 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1393 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1394 struct bt_ctf_field_type
*strct_type
= NULL
;
1395 struct bt_ctf_field
*len
= NULL
;
1396 struct bt_ctf_field
*fp
= NULL
;
1397 struct bt_ctf_field
*s
= NULL
;
1398 struct bt_ctf_field
*e_int
= NULL
;
1399 struct bt_ctf_field
*e
= NULL
;
1400 struct bt_ctf_field
*v
= NULL
;
1401 struct bt_ctf_field
*v_selected
= NULL
;
1402 struct bt_ctf_field
*v_selected_cur
= NULL
;
1403 struct bt_ctf_field
*v_selected_0
= NULL
;
1404 struct bt_ctf_field
*v_selected_1
= NULL
;
1405 struct bt_ctf_field
*v_selected_2
= NULL
;
1406 struct bt_ctf_field
*v_selected_3
= NULL
;
1407 struct bt_ctf_field
*v_selected_4
= NULL
;
1408 struct bt_ctf_field
*v_selected_5
= NULL
;
1409 struct bt_ctf_field
*v_selected_6
= NULL
;
1410 struct bt_ctf_field
*a
= NULL
;
1411 struct bt_ctf_field
*a_0
= NULL
;
1412 struct bt_ctf_field
*a_1
= NULL
;
1413 struct bt_ctf_field
*a_2
= NULL
;
1414 struct bt_ctf_field
*a_3
= NULL
;
1415 struct bt_ctf_field
*a_4
= NULL
;
1416 struct bt_ctf_field
*strct
= NULL
;
1417 struct bt_ctf_field
*len_copy
= NULL
;
1418 struct bt_ctf_field
*fp_copy
= NULL
;
1419 struct bt_ctf_field
*s_copy
= NULL
;
1420 struct bt_ctf_field
*e_int_copy
= NULL
;
1421 struct bt_ctf_field
*e_copy
= NULL
;
1422 struct bt_ctf_field
*v_copy
= NULL
;
1423 struct bt_ctf_field
*v_selected_copy
= NULL
;
1424 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1425 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1426 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1427 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1428 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1429 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1430 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1431 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1432 struct bt_ctf_field
*a_copy
= NULL
;
1433 struct bt_ctf_field
*a_0_copy
= NULL
;
1434 struct bt_ctf_field
*a_1_copy
= NULL
;
1435 struct bt_ctf_field
*a_2_copy
= NULL
;
1436 struct bt_ctf_field
*a_3_copy
= NULL
;
1437 struct bt_ctf_field
*a_4_copy
= NULL
;
1438 struct bt_ctf_field
*strct_copy
= NULL
;
1439 uint64_t uint64_t_val
;
1440 const char *str_val
;
1444 /* create len type */
1445 len_type
= bt_ctf_field_type_integer_create(32);
1448 /* create fp type */
1449 fp_type
= bt_ctf_field_type_floating_point_create();
1453 s_type
= bt_ctf_field_type_string_create();
1456 /* create e_int type */
1457 e_int_type
= bt_ctf_field_type_integer_create(8);
1461 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1463 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1466 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1470 /* create v_label1 type */
1471 v_label1_type
= bt_ctf_field_type_string_create();
1472 assert(v_label1_type
);
1474 /* create v_label1_array type */
1475 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1476 assert(v_label1_array_type
);
1478 /* create v_label2 type */
1479 v_label2_type
= bt_ctf_field_type_integer_create(16);
1480 assert(v_label2_type
);
1482 /* create v_label2_seq type */
1483 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1485 assert(v_label2_seq_type
);
1488 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1490 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1493 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1497 /* create strct type */
1498 strct_type
= bt_ctf_field_type_structure_create();
1500 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1503 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1505 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1507 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1509 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1511 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1512 v_label1_array_type
, "a");
1516 strct
= bt_ctf_field_create(strct_type
);
1520 len
= bt_ctf_field_structure_get_field(strct
, "len");
1524 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1528 s
= bt_ctf_field_structure_get_field(strct
, "s");
1532 e
= bt_ctf_field_structure_get_field(strct
, "e");
1535 /* get e_int (underlying integer) */
1536 e_int
= bt_ctf_field_enumeration_get_container(e
);
1540 v
= bt_ctf_field_structure_get_field(strct
, "v");
1544 a
= bt_ctf_field_structure_get_field(strct
, "a");
1548 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1552 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1556 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1559 /* set e field (LABEL2) */
1560 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1564 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1566 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1567 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1568 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1569 ok(v_selected_cur
== v_selected
,
1570 "bt_ctf_field_variant_get_current_field returns the current field");
1571 bt_put(v_selected_cur
);
1573 /* set selected v field */
1574 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1576 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1577 assert(v_selected_0
);
1578 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1580 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1581 assert(v_selected_1
);
1582 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1584 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1585 assert(v_selected_2
);
1586 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1588 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1589 assert(v_selected_3
);
1590 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1592 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1593 assert(v_selected_4
);
1594 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1596 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1597 assert(v_selected_5
);
1598 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1600 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1601 assert(v_selected_6
);
1602 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1606 a_0
= bt_ctf_field_array_get_field(a
, 0);
1608 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1610 a_1
= bt_ctf_field_array_get_field(a
, 1);
1612 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1614 a_2
= bt_ctf_field_array_get_field(a
, 2);
1616 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1618 a_3
= bt_ctf_field_array_get_field(a
, 3);
1620 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1622 a_4
= bt_ctf_field_array_get_field(a
, 4);
1624 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1627 /* create copy of strct */
1628 ok(!bt_ctf_field_copy(NULL
),
1629 "bt_ctf_field_copy handles NULL correctly");
1630 strct_copy
= bt_ctf_field_copy(strct
);
1632 "bt_ctf_field_copy returns a valid pointer");
1634 /* get all copied fields */
1635 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1637 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1639 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1641 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1643 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1645 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1647 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1648 assert(v_selected_copy
);
1649 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1650 assert(v_selected_0_copy
);
1651 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1652 assert(v_selected_1_copy
);
1653 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1654 assert(v_selected_2_copy
);
1655 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1656 assert(v_selected_3_copy
);
1657 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1658 assert(v_selected_4_copy
);
1659 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1660 assert(v_selected_5_copy
);
1661 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1662 assert(v_selected_6_copy
);
1663 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1664 "sequence field copy is not too large");
1665 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1667 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1669 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1671 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1673 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1675 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1677 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1678 "array field copy is not too large");
1680 /* make sure copied fields are different pointers */
1681 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1682 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1683 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1684 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1685 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1686 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1687 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1688 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1690 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1692 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1694 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1696 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1698 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1700 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1702 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1704 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1705 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1706 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1707 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1708 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1709 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1711 /* make sure copied fields share the same types */
1712 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1713 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1714 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1715 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1716 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1717 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1718 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1720 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1722 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1724 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1726 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1728 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1730 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1732 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1734 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1735 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1736 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1737 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1738 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1739 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1741 /* validate len copy */
1742 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1744 ok(uint64_t_val
== 7,
1745 "bt_ctf_field_copy creates a valid integer field copy");
1747 /* validate fp copy */
1748 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1750 ok(double_val
== 3.14,
1751 "bt_ctf_field_copy creates a valid floating point number field copy");
1753 /* validate s copy */
1754 str_val
= bt_ctf_field_string_get_value(s_copy
);
1755 ok(str_val
&& !strcmp(str_val
, "btbt"),
1756 "bt_ctf_field_copy creates a valid string field copy");
1758 /* validate e_int copy */
1759 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1762 ok(uint64_t_val
== 23,
1763 "bt_ctf_field_copy creates a valid enum's integer field copy");
1765 /* validate e copy */
1766 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1767 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1768 "bt_ctf_field_copy creates a valid enum field copy");
1770 /* validate v_selected copy */
1771 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1772 assert(v_selected_copy_len
);
1773 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1776 ok(uint64_t_val
== 7,
1777 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1778 bt_put(v_selected_copy_len
);
1779 v_selected_copy_len
= NULL
;
1781 /* validate v_selected copy fields */
1782 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1785 ok(uint64_t_val
== 7,
1786 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1787 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1790 ok(uint64_t_val
== 6,
1791 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1792 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1795 ok(uint64_t_val
== 5,
1796 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1797 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1800 ok(uint64_t_val
== 4,
1801 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1802 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1805 ok(uint64_t_val
== 3,
1806 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1807 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1810 ok(uint64_t_val
== 2,
1811 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1812 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1815 ok(uint64_t_val
== 1,
1816 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1818 /* validate a copy fields */
1819 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1820 ok(str_val
&& !strcmp(str_val
, "a_0"),
1821 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1822 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1823 ok(str_val
&& !strcmp(str_val
, "a_1"),
1824 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1825 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1826 ok(str_val
&& !strcmp(str_val
, "a_2"),
1827 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1828 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1829 ok(str_val
&& !strcmp(str_val
, "a_3"),
1830 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1831 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1832 ok(str_val
&& !strcmp(str_val
, "a_4"),
1833 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1835 /* put everything */
1842 bt_put(v_label1_type
);
1843 bt_put(v_label1_array_type
);
1844 bt_put(v_label2_type
);
1845 bt_put(v_label2_seq_type
);
1854 bt_put(v_selected_0
);
1855 bt_put(v_selected_1
);
1856 bt_put(v_selected_2
);
1857 bt_put(v_selected_3
);
1858 bt_put(v_selected_4
);
1859 bt_put(v_selected_5
);
1860 bt_put(v_selected_6
);
1874 bt_put(v_selected_copy
);
1875 bt_put(v_selected_0_copy
);
1876 bt_put(v_selected_1_copy
);
1877 bt_put(v_selected_2_copy
);
1878 bt_put(v_selected_3_copy
);
1879 bt_put(v_selected_4_copy
);
1880 bt_put(v_selected_5_copy
);
1881 bt_put(v_selected_6_copy
);
1891 void type_field_tests()
1893 struct bt_ctf_field
*uint_12
;
1894 struct bt_ctf_field
*int_16
;
1895 struct bt_ctf_field
*string
;
1896 struct bt_ctf_field
*enumeration
;
1897 struct bt_ctf_field_type
*composite_structure_type
;
1898 struct bt_ctf_field_type
*structure_seq_type
;
1899 struct bt_ctf_field_type
*string_type
;
1900 struct bt_ctf_field_type
*sequence_type
;
1901 struct bt_ctf_field_type
*uint_8_type
;
1902 struct bt_ctf_field_type
*int_16_type
;
1903 struct bt_ctf_field_type
*uint_12_type
=
1904 bt_ctf_field_type_integer_create(12);
1905 struct bt_ctf_field_type
*enumeration_type
;
1906 struct bt_ctf_field_type
*returned_type
;
1907 const char *ret_string
;
1909 returned_type
= bt_ctf_field_get_type(NULL
);
1910 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1912 ok(uint_12_type
, "Create an unsigned integer type");
1913 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1914 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1915 "Set integer type's base as binary");
1916 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1917 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1918 "Set integer type's base as decimal");
1919 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1920 BT_CTF_INTEGER_BASE_UNKNOWN
),
1921 "Reject integer type's base set as unknown");
1922 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1923 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1924 "Set integer type's base as octal");
1925 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1926 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1927 "Set integer type's base as hexadecimal");
1928 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1929 "Reject unknown integer base value");
1930 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1931 "Set integer type signedness to signed");
1932 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1933 "Set integer type signedness to unsigned");
1934 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1935 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1936 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1937 "bt_ctf_field_type_integer_get_size returns a correct value");
1938 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1939 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1940 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1941 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1943 ok(bt_ctf_field_type_set_byte_order(NULL
,
1944 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1945 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1946 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1947 (enum bt_ctf_byte_order
) 42) < 0,
1948 "bt_ctf_field_type_set_byte_order rejects invalid values");
1949 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1950 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1951 "Set an integer's byte order to little endian");
1952 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1953 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1954 "Set an integer's byte order to big endian");
1955 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1956 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1957 "bt_ctf_field_type_get_byte_order returns a correct value");
1958 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1959 BT_CTF_BYTE_ORDER_UNKNOWN
,
1960 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1962 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1963 BT_CTF_TYPE_ID_UNKNOWN
,
1964 "bt_ctf_field_type_get_type_id handles NULL correctly");
1965 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1966 BT_CTF_TYPE_ID_INTEGER
,
1967 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1969 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1970 BT_CTF_INTEGER_BASE_UNKNOWN
,
1971 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1972 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1973 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1974 "bt_ctf_field_type_integer_get_base returns a correct value");
1976 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1977 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1978 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1979 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1980 (enum bt_ctf_string_encoding
) 123) < 0,
1981 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1982 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1983 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1984 "Set integer type encoding to UTF8");
1985 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1986 BT_CTF_STRING_ENCODING_UNKNOWN
,
1987 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1988 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1989 BT_CTF_STRING_ENCODING_UTF8
,
1990 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1992 int_16_type
= bt_ctf_field_type_integer_create(16);
1993 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1994 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1995 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1996 uint_8_type
= bt_ctf_field_type_integer_create(8);
1998 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1999 ok(sequence_type
, "Create a sequence of int16_t type");
2000 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
2001 BT_CTF_TYPE_ID_SEQUENCE
,
2002 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
2004 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
2005 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
2006 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
2008 ok(!strcmp(ret_string
, "seq_len"),
2009 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
2010 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
2011 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
2012 returned_type
= bt_ctf_field_type_sequence_get_element_type(
2014 ok(returned_type
== int_16_type
,
2015 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
2016 bt_put(returned_type
);
2018 string_type
= bt_ctf_field_type_string_create();
2019 ok(string_type
, "Create a string type");
2020 ok(bt_ctf_field_type_string_set_encoding(string_type
,
2021 BT_CTF_STRING_ENCODING_NONE
),
2022 "Reject invalid \"None\" string encoding");
2023 ok(bt_ctf_field_type_string_set_encoding(string_type
,
2025 "Reject invalid string encoding");
2026 ok(bt_ctf_field_type_string_set_encoding(string_type
,
2027 BT_CTF_STRING_ENCODING_ASCII
) == 0,
2028 "Set string encoding to ASCII");
2030 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
2031 BT_CTF_STRING_ENCODING_UNKNOWN
,
2032 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
2033 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
2034 BT_CTF_STRING_ENCODING_ASCII
,
2035 "bt_ctf_field_type_string_get_encoding returns the correct value");
2037 structure_seq_type
= bt_ctf_field_type_structure_create();
2038 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
2039 BT_CTF_TYPE_ID_STRUCT
,
2040 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
2041 ok(structure_seq_type
, "Create a structure type");
2042 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
2043 uint_8_type
, "seq_len") == 0,
2044 "Add a uint8_t type to a structure");
2045 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
2046 sequence_type
, "a_sequence") == 0,
2047 "Add a sequence type to a structure");
2049 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
2050 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
2051 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
2052 "bt_ctf_field_type_structure_get_field_count returns a correct value");
2054 ok(bt_ctf_field_type_structure_get_field(NULL
,
2055 &ret_string
, &returned_type
, 1) < 0,
2056 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2057 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2058 NULL
, &returned_type
, 1) == 0,
2059 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2060 bt_put(returned_type
);
2061 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2062 &ret_string
, NULL
, 1) == 0,
2063 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2064 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2065 &ret_string
, &returned_type
, 10) < 0,
2066 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2067 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2068 &ret_string
, &returned_type
, 1) == 0,
2069 "bt_ctf_field_type_structure_get_field returns a field");
2070 ok(!strcmp(ret_string
, "a_sequence"),
2071 "bt_ctf_field_type_structure_get_field returns a correct field name");
2072 ok(returned_type
== sequence_type
,
2073 "bt_ctf_field_type_structure_get_field returns a correct field type");
2074 bt_put(returned_type
);
2076 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2077 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2078 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2079 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2080 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2081 structure_seq_type
, "a_sequence");
2082 ok(returned_type
== sequence_type
,
2083 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2084 bt_put(returned_type
);
2086 composite_structure_type
= bt_ctf_field_type_structure_create();
2087 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2088 string_type
, "a_string") == 0,
2089 "Add a string type to a structure");
2090 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2091 structure_seq_type
, "inner_structure") == 0,
2092 "Add a structure type to a structure");
2094 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2095 NULL
, "a_sequence") == NULL
,
2096 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2097 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2098 structure_seq_type
, NULL
) == NULL
,
2099 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2100 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2101 structure_seq_type
, "a_sequence");
2102 ok(returned_type
== sequence_type
,
2103 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2104 bt_put(returned_type
);
2106 int_16
= bt_ctf_field_create(int_16_type
);
2107 ok(int_16
, "Instanciate a signed 16-bit integer");
2108 uint_12
= bt_ctf_field_create(uint_12_type
);
2109 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2110 returned_type
= bt_ctf_field_get_type(int_16
);
2111 ok(returned_type
== int_16_type
,
2112 "bt_ctf_field_get_type returns the correct type");
2114 /* Can't modify types after instanciating them */
2115 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2116 BT_CTF_INTEGER_BASE_DECIMAL
),
2117 "Check an integer type' base can't be modified after instanciation");
2118 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2119 "Check an integer type's signedness can't be modified after instanciation");
2121 /* Check signed property is checked */
2122 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2123 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2124 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2125 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2127 /* Check overflows are properly tested for */
2128 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2129 "Check -32768 is allowed for a signed 16-bit integer");
2130 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2131 "Check 32767 is allowed for a signed 16-bit integer");
2132 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2133 "Check 32768 is not allowed for a signed 16-bit integer");
2134 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2135 "Check -32769 is not allowed for a signed 16-bit integer");
2136 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2137 "Check -42 is allowed for a signed 16-bit integer");
2139 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2140 "Check 4095 is allowed for an unsigned 12-bit integer");
2141 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2142 "Check 4096 is not allowed for a unsigned 12-bit integer");
2143 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2144 "Check 0 is allowed for an unsigned 12-bit integer");
2146 string
= bt_ctf_field_create(string_type
);
2147 ok(string
, "Instanciate a string field");
2148 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2149 "Set a string's value");
2151 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2152 ok(enumeration_type
,
2153 "Create an enumeration type with an unsigned 12-bit integer as container");
2154 enumeration
= bt_ctf_field_create(enumeration_type
);
2156 "Check enumeration types are validated before instantiation");
2161 bt_put(enumeration
);
2162 bt_put(composite_structure_type
);
2163 bt_put(structure_seq_type
);
2164 bt_put(string_type
);
2165 bt_put(sequence_type
);
2166 bt_put(uint_8_type
);
2167 bt_put(int_16_type
);
2168 bt_put(uint_12_type
);
2169 bt_put(enumeration_type
);
2170 bt_put(returned_type
);
2173 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2174 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2177 * Append enough events to force the underlying packet to be resized.
2178 * Also tests that a new event can be declared after a stream has been
2179 * instantiated and used/flushed.
2183 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2185 struct bt_ctf_field_type
*integer_type
=
2186 bt_ctf_field_type_integer_create(17);
2187 struct bt_ctf_field_type
*string_type
=
2188 bt_ctf_field_type_string_create();
2189 struct bt_ctf_event
*event
= NULL
;
2190 struct bt_ctf_field
*ret_field
= NULL
;
2191 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2192 uint64_t ret_uint64
;
2193 int events_appended
= 0;
2194 struct bt_ctf_field
*packet_context
= NULL
,
2195 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
2196 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2197 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2198 struct bt_ctf_field_type
*ep_type
= NULL
;
2200 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2202 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2204 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2205 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2211 * bt_ctf_stream_class_add_event_class() copies the field types
2212 * of event_class, so we retrieve the new ones to create the
2213 * appropriate fields.
2215 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2217 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2218 ep_type
, "field_1");
2219 assert(ep_field_1_type
);
2220 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2221 ep_type
, "a_string");
2222 assert(ep_a_string_type
);
2224 event
= bt_ctf_event_create(event_class
);
2225 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2226 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2227 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2228 "bt_ctf_event_get_payload_by_index returns a correct field");
2229 bt_put(ret_field_type
);
2232 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2233 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2234 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2235 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2238 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2239 event
= bt_ctf_event_create(event_class
);
2240 struct bt_ctf_field
*integer
=
2241 bt_ctf_field_create(ep_field_1_type
);
2242 struct bt_ctf_field
*string
=
2243 bt_ctf_field_create(ep_a_string_type
);
2245 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2246 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2247 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2250 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2251 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2255 /* Populate stream event context */
2256 stream_event_context
=
2257 bt_ctf_event_get_stream_event_context(event
);
2258 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2259 "common_event_context");
2260 BT_PUT(stream_event_context
);
2261 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2265 ret
|= bt_ctf_stream_append_event(stream
, event
);
2273 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2274 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2275 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2276 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2277 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2278 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2279 ok(ret
== 0 && ret_uint64
== 0,
2280 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2281 bt_ctf_stream_append_discarded_events(stream
, 1000);
2282 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2283 ok(ret
== 0 && ret_uint64
== 1000,
2284 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2287 ok(events_appended
, "Append 100 000 events to a stream");
2290 * Populate the custom packet context field with a dummy value
2291 * otherwise flush will fail.
2293 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2294 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2295 "custom_packet_context_field");
2296 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2298 ok(bt_ctf_stream_flush(stream
) == 0,
2299 "Flush a stream that forces a packet resize");
2300 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2301 ok(ret
== 0 && ret_uint64
== 1000,
2302 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2303 bt_put(integer_type
);
2304 bt_put(string_type
);
2305 bt_put(packet_context
);
2306 bt_put(packet_context_field
);
2307 bt_put(stream_event_context
);
2308 bt_put(event_class
);
2309 bt_put(ep_field_1_type
);
2310 bt_put(ep_a_string_type
);
2314 void test_empty_stream(struct bt_ctf_writer
*writer
)
2317 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2318 struct bt_ctf_stream_class
*stream_class
= NULL
;
2319 struct bt_ctf_stream
*stream
= NULL
;
2321 trace
= bt_ctf_writer_get_trace(writer
);
2323 diag("Failed to get trace from writer");
2328 stream_class
= bt_ctf_stream_class_create("empty_stream");
2329 if (!stream_class
) {
2330 diag("Failed to create stream class");
2335 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2336 "bt_ctf_stream_class_get_trace handles NULL correctly");
2337 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2338 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2340 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2342 diag("Failed to create writer stream");
2347 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2348 ok(ret_trace
== trace
,
2349 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2352 "Created a stream class with default attributes and an empty stream");
2356 bt_put(stream_class
);
2359 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2362 struct bt_ctf_trace
*trace
= NULL
;
2363 struct bt_ctf_clock
*clock
= NULL
;
2364 struct bt_ctf_stream_class
*stream_class
= NULL
;
2365 struct bt_ctf_stream
*stream
= NULL
;
2366 struct bt_ctf_field_type
*integer_type
= NULL
,
2367 *sequence_type
= NULL
, *event_header_type
= NULL
;
2368 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2369 *event_header
= NULL
, *packet_header
= NULL
;
2370 struct bt_ctf_event_class
*event_class
= NULL
;
2371 struct bt_ctf_event
*event
= NULL
;
2373 trace
= bt_ctf_writer_get_trace(writer
);
2375 fail("Failed to get trace from writer");
2379 clock
= bt_ctf_trace_get_clock(trace
, 0);
2381 fail("Failed to get clock from trace");
2385 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2386 if (!stream_class
) {
2387 fail("Failed to create stream class");
2391 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2393 fail("Failed to set stream class clock");
2398 * Customize event header to add an "seq_len" integer member
2399 * which will be used as the length of a sequence in an event of this
2402 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2404 if (!event_header_type
) {
2405 fail("Failed to get event header type");
2409 integer_type
= bt_ctf_field_type_integer_create(13);
2410 if (!integer_type
) {
2411 fail("Failed to create length integer type");
2415 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2416 integer_type
, "seq_len");
2418 fail("Failed to add a new field to stream event header");
2422 event_class
= bt_ctf_event_class_create("sequence_event");
2424 fail("Failed to create event class");
2429 * This event's payload will contain a sequence which references
2430 * stream.event.header.seq_len as its length field.
2432 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2433 "stream.event.header.seq_len");
2434 if (!sequence_type
) {
2435 fail("Failed to create a sequence");
2439 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2442 fail("Failed to add a sequence to an event class");
2446 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2448 fail("Failed to add event class to stream class");
2452 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2454 fail("Failed to create stream")
2459 * We have defined a custom packet header field. We have to populate it
2462 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2463 if (!packet_header
) {
2464 fail("Failed to get stream packet header");
2468 integer
= bt_ctf_field_structure_get_field(packet_header
,
2469 "custom_trace_packet_header_field");
2471 fail("Failed to retrieve custom_trace_packet_header_field");
2475 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2477 fail("Failed to set custom_trace_packet_header_field value");
2482 event
= bt_ctf_event_create(event_class
);
2484 fail("Failed to create event");
2488 event_header
= bt_ctf_event_get_header(event
);
2489 if (!event_header
) {
2490 fail("Failed to get event header");
2494 integer
= bt_ctf_field_structure_get_field(event_header
,
2497 fail("Failed to get seq_len field from event header");
2501 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2503 fail("Failed to set seq_len value in event header");
2507 /* Populate both sequence integer fields */
2508 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2510 fail("Failed to retrieve sequence from event");
2514 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2516 fail("Failed to set sequence length");
2521 for (i
= 0; i
< 2; i
++) {
2522 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2524 fail("Failed to retrieve sequence element");
2528 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2530 fail("Failed to set sequence element value");
2538 ret
= bt_ctf_stream_append_event(stream
, event
);
2540 fail("Failed to append event to stream");
2544 ret
= bt_ctf_stream_flush(stream
);
2546 fail("Failed to flush custom_event_header stream");
2552 bt_put(stream_class
);
2553 bt_put(event_class
);
2557 bt_put(event_header
);
2558 bt_put(packet_header
);
2559 bt_put(sequence_type
);
2560 bt_put(integer_type
);
2561 bt_put(event_header_type
);
2564 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2567 struct bt_ctf_trace
*trace
= NULL
;
2568 struct bt_ctf_clock
*clock
= NULL
;
2569 struct bt_ctf_stream_class
*stream_class
= NULL
;
2570 struct bt_ctf_stream
*stream
= NULL
,
2572 struct bt_ctf_event_class
*event_class
= NULL
;
2573 struct bt_ctf_event
*event
= NULL
;
2574 struct bt_ctf_field_type
*integer_type
= NULL
;
2575 struct bt_ctf_field
*integer
= NULL
;
2577 trace
= bt_ctf_writer_get_trace(writer
);
2579 diag("Failed to get trace from writer");
2584 clock
= bt_ctf_trace_get_clock(trace
, 0);
2586 diag("Failed to get clock from trace");
2591 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2592 if (!stream_class
) {
2593 diag("Failed to create stream class");
2598 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2600 diag("Failed to set stream class clock");
2604 event_class
= bt_ctf_event_class_create("some_event_class_name");
2605 integer_type
= bt_ctf_field_type_integer_create(32);
2606 if (!integer_type
) {
2607 diag("Failed to create integer field type");
2612 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2615 diag("Failed to add field to event class");
2619 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2622 diag("Failed to add event class to stream class");
2625 event
= bt_ctf_event_create(event_class
);
2627 diag("Failed to create event");
2632 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2634 diag("Failed to get integer field payload from event");
2639 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2641 diag("Failed to set integer field value");
2645 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2647 diag("Failed to create writer stream");
2652 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2653 "bt_ctf_event_get_stream handles NULL correctly");
2654 ok(bt_ctf_event_get_stream(event
) == NULL
,
2655 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2657 ret
= bt_ctf_stream_append_event(stream
, event
);
2659 diag("Failed to append event to stream");
2663 ret_stream
= bt_ctf_event_get_stream(event
);
2664 ok(ret_stream
== stream
,
2665 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2668 "Create an event before instanciating its associated stream");
2672 bt_put(stream_class
);
2673 bt_put(event_class
);
2675 bt_put(integer_type
);
2680 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2682 struct bt_ctf_event_class
*event_class
;
2684 event_class
= bt_ctf_event_class_create("Simple Event");
2685 assert(event_class
);
2686 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2687 "two event classes with the same name cannot cohabit within the same stream class");
2688 bt_put(event_class
);
2690 event_class
= bt_ctf_event_class_create("different name, ok");
2691 assert(event_class
);
2692 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2693 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2694 "two event classes with the same ID cannot cohabit within the same stream class");
2695 bt_put(event_class
);
2698 int main(int argc
, char **argv
)
2700 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2701 char metadata_path
[sizeof(trace_path
) + 9];
2702 const char *clock_name
= "test_clock";
2703 const char *clock_description
= "This is a test clock";
2704 const char *returned_clock_name
;
2705 const char *returned_clock_description
;
2706 const uint64_t frequency
= 1123456789;
2707 const int64_t offset_s
= 1351530929945824323;
2708 const int64_t offset
= 1234567;
2709 int64_t get_offset_s
,
2712 const uint64_t precision
= 10;
2713 const int is_absolute
= 0xFF;
2714 char *metadata_string
;
2715 struct bt_ctf_writer
*writer
;
2716 struct utsname name
;
2717 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2718 struct bt_ctf_clock
*clock
, *ret_clock
;
2719 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2720 struct bt_ctf_stream
*stream1
;
2721 const char *ret_string
;
2722 const unsigned char *ret_uuid
;
2723 unsigned char tmp_uuid
[16] = { 0 };
2724 struct bt_ctf_field_type
*packet_context_type
,
2725 *packet_context_field_type
,
2726 *packet_header_type
,
2727 *packet_header_field_type
,
2729 *stream_event_context_type
,
2731 *event_header_field_type
;
2732 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2733 struct bt_ctf_trace
*trace
;
2735 int64_t ret_int64_t
;
2736 struct bt_value
*obj
;
2739 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2743 plan_tests(NR_TESTS
);
2745 if (!bt_mkdtemp(trace_path
)) {
2749 strcpy(metadata_path
, trace_path
);
2750 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2752 writer
= bt_ctf_writer_create(trace_path
);
2753 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2755 ok(!bt_ctf_writer_get_trace(NULL
),
2756 "bt_ctf_writer_get_trace correctly handles NULL");
2757 trace
= bt_ctf_writer_get_trace(writer
);
2759 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2760 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2761 "Set a trace's byte order to big endian");
2762 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2763 "bt_ctf_trace_get_byte_order returns a correct endianness");
2765 /* Add environment context to the trace */
2766 ret
= gethostname(hostname
, sizeof(hostname
));
2770 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2771 "Add host (%s) environment field to writer instance",
2773 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2775 "bt_ctf_writer_add_environment_field error with NULL writer");
2776 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2778 "bt_ctf_writer_add_environment_field error with NULL field name");
2779 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2781 "bt_ctf_writer_add_environment_field error with NULL field value");
2783 /* Test bt_ctf_trace_set_environment_field with an integer object */
2784 obj
= bt_value_integer_create_init(23);
2786 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2787 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2788 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2789 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2790 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2791 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2792 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2793 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2796 /* Test bt_ctf_trace_set_environment_field with a string object */
2797 obj
= bt_value_string_create_init("the value");
2799 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2800 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2803 /* Test bt_ctf_trace_set_environment_field_integer */
2804 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2806 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2807 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2808 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2809 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2811 "bt_ctf_trace_set_environment_field_integer succeeds");
2813 /* Test bt_ctf_trace_set_environment_field_string */
2814 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2816 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2817 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2818 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2819 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2821 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2822 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2824 "bt_ctf_trace_set_environment_field_string succeeds");
2826 /* Test bt_ctf_trace_get_environment_field_count */
2827 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2828 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2829 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2830 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2832 /* Test bt_ctf_trace_get_environment_field_name */
2833 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
2834 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2835 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
2836 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2837 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
2838 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2839 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
2840 ok(ret_string
&& !strcmp(ret_string
, "host"),
2841 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2842 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
2843 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2844 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2845 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
2846 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2847 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2848 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
2849 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2850 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2851 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
2852 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2853 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2855 /* Test bt_ctf_trace_get_environment_field_value */
2856 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
2857 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2858 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
2859 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2860 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
2861 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2862 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
2863 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2864 ok(!ret
&& ret_int64_t
== 23,
2865 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2867 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
2868 ret
= bt_value_string_get(obj
, &ret_string
);
2869 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2870 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2873 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2874 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2876 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2877 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
2878 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2879 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
2880 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2881 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
2883 ret
= bt_value_string_get(obj
, &ret_string
);
2884 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
2885 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2888 /* Test environment field replacement */
2889 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2891 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2892 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2893 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2894 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
2895 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2896 ok(!ret
&& ret_int64_t
== 654321,
2897 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2900 /* On Solaris, uname() can return any positive value on success */
2901 if (uname(&name
) < 0) {
2906 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
2907 == 0, "Add sysname (%s) environment field to writer instance",
2909 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
2910 name
.nodename
) == 0,
2911 "Add nodename (%s) environment field to writer instance",
2913 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
2914 == 0, "Add release (%s) environment field to writer instance",
2916 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
2917 == 0, "Add version (%s) environment field to writer instance",
2919 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
2920 == 0, "Add machine (%s) environment field to writer istance",
2923 /* Define a clock and add it to the trace */
2924 ok(bt_ctf_clock_create("signed") == NULL
,
2925 "Illegal clock name rejected");
2926 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
2927 clock
= bt_ctf_clock_create(clock_name
);
2928 ok(clock
, "Clock created sucessfully");
2929 returned_clock_name
= bt_ctf_clock_get_name(clock
);
2930 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
2931 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
2932 "Returned clock name is valid");
2934 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2935 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
2936 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
2937 "Clock description set successfully");
2939 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2940 ok(returned_clock_description
,
2941 "bt_ctf_clock_get_description returns a description.");
2942 ok(returned_clock_description
?
2943 !strcmp(returned_clock_description
, clock_description
) : 0,
2944 "Returned clock description is valid");
2946 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
2947 "bt_ctf_clock_get_frequency returns the correct default frequency");
2948 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
2949 "Set clock frequency");
2950 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
2951 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
2953 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
2954 "bt_ctf_clock_get_offset_s succeeds");
2955 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
2956 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
2957 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
2958 "Set clock offset (seconds)");
2959 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
2960 "bt_ctf_clock_get_offset_s succeeds");
2961 ok(get_offset_s
== offset_s
,
2962 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
2964 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
2965 "bt_ctf_clock_get_offset succeeds");
2966 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
2967 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
2968 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
2969 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
2970 "bt_ctf_clock_get_offset succeeds");
2971 ok(get_offset
== offset
,
2972 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
2974 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
2975 "bt_ctf_clock_get_precision returns the correct default precision");
2976 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
2977 "Set clock precision");
2978 ok(bt_ctf_clock_get_precision(clock
) == precision
,
2979 "bt_ctf_clock_get_precision returns the correct precision once it is set");
2981 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
2982 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
2983 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
2984 "Set clock absolute property");
2985 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
2986 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
2988 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
2989 "bt_ctf_clock_get_time succeeds");
2990 ok(get_time
== DEFAULT_CLOCK_TIME
,
2991 "bt_ctf_clock_get_time returns the correct default time");
2992 ok(bt_ctf_clock_get_value(clock
) == DEFAULT_CLOCK_VALUE
,
2993 "bt_ctf_clock_get_value returns the correct default value");
2994 ok(bt_ctf_clock_set_value(clock
, current_time
) == 0,
2996 ok(bt_ctf_clock_get_value(clock
) == current_time
,
2997 "bt_ctf_clock_get_value returns the correct value once it is set");
2998 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3000 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3001 "bt_ctf_clock_get_time succeeds");
3002 ok(get_time
>= current_time
- 1 && get_time
<= current_time
+ 1,
3003 "bt_ctf_clock_get_time returns the correct time once it is set");
3005 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
3006 "Add clock to writer instance");
3007 ok(bt_ctf_writer_add_clock(writer
, clock
),
3008 "Verify a clock can't be added twice to a writer instance");
3010 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
3011 "bt_ctf_trace_get_clock_count correctly handles NULL");
3012 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
3013 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3014 ok(!bt_ctf_trace_get_clock(NULL
, 0),
3015 "bt_ctf_trace_get_clock correctly handles NULL");
3016 ok(!bt_ctf_trace_get_clock(trace
, -1),
3017 "bt_ctf_trace_get_clock correctly handles negative indexes");
3018 ok(!bt_ctf_trace_get_clock(trace
, 1),
3019 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3020 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
3021 ok(ret_clock
== clock
,
3022 "bt_ctf_trace_get_clock returns the right clock instance");
3024 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
3025 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3026 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
3027 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3028 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
3029 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3030 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
3031 ok(ret_clock
== clock
,
3032 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3034 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
3035 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3037 ok(!bt_ctf_clock_get_name(NULL
),
3038 "bt_ctf_clock_get_name correctly handles NULL");
3039 ok(!bt_ctf_clock_get_description(NULL
),
3040 "bt_ctf_clock_get_description correctly handles NULL");
3041 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3042 "bt_ctf_clock_get_frequency correctly handles NULL");
3043 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3044 "bt_ctf_clock_get_precision correctly handles NULL");
3045 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3046 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3047 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3048 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3049 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3050 "bt_ctf_clock_get_offset correctly handles NULL clock");
3051 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3052 "bt_ctf_clock_get_offset correctly handles NULL output");
3053 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3054 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3055 ok(bt_ctf_clock_get_time(NULL
, &get_time
) < 0,
3056 "bt_ctf_clock_get_time correctly handles NULL clock");
3057 ok(bt_ctf_clock_get_time(clock
, NULL
) < 0,
3058 "bt_ctf_clock_get_time correctly handles NULL output");
3060 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3061 "bt_ctf_clock_set_description correctly handles NULL clock");
3062 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3063 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3064 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3065 "bt_ctf_clock_get_precision correctly handles NULL clock");
3066 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3067 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3068 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3069 "bt_ctf_clock_set_offset correctly handles NULL clock");
3070 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3071 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3072 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3073 "bt_ctf_clock_set_time correctly handles NULL clock");
3074 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3075 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3076 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3078 "bt_ctf_clock_get_uuid returns a UUID");
3080 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3081 /* Slightly modify UUID */
3082 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3085 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3086 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3087 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3088 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3089 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3090 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3091 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3093 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3094 ok(uuid_match(ret_uuid
, tmp_uuid
),
3095 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3097 /* Define a stream class */
3098 stream_class
= bt_ctf_stream_class_create("test_stream");
3100 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3101 "bt_ctf_stream_class_get_name handles NULL correctly");
3102 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3103 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3104 "bt_ctf_stream_class_get_name returns a correct stream class name");
3106 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3107 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3108 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3109 "bt_ctf_stream_class_get_clock handles NULL correctly");
3111 ok(stream_class
, "Create stream class");
3112 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3113 "Set a stream class' clock");
3114 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3115 ok(ret_clock
== clock
,
3116 "bt_ctf_stream_class_get_clock returns a correct clock");
3119 /* Test the event fields and event types APIs */
3122 /* Test fields copying */
3125 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3126 "bt_ctf_stream_class_get_id returns an error when no id is set");
3127 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3128 "bt_ctf_stream_class_get_id handles NULL correctly");
3129 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3130 "bt_ctf_stream_class_set_id handles NULL correctly");
3131 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3132 "Set an stream class' id");
3133 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3134 "bt_ctf_stream_class_get_id returns the correct value");
3136 /* Validate default event header fields */
3137 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3138 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3139 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3142 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3143 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_TYPE_ID_STRUCT
,
3144 "Default event header type is a structure");
3145 event_header_field_type
=
3146 bt_ctf_field_type_structure_get_field_type_by_name(
3147 ret_field_type
, "id");
3148 ok(event_header_field_type
,
3149 "Default event header type contains an \"id\" field");
3150 ok(bt_ctf_field_type_get_type_id(
3151 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3152 "Default event header \"id\" field is an integer");
3153 bt_put(event_header_field_type
);
3154 event_header_field_type
=
3155 bt_ctf_field_type_structure_get_field_type_by_name(
3156 ret_field_type
, "timestamp");
3157 ok(event_header_field_type
,
3158 "Default event header type contains a \"timestamp\" field");
3159 ok(bt_ctf_field_type_get_type_id(
3160 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3161 "Default event header \"timestamp\" field is an integer");
3162 bt_put(event_header_field_type
);
3163 bt_put(ret_field_type
);
3165 /* Add a custom trace packet header field */
3166 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3167 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3168 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3169 ok(packet_header_type
,
3170 "bt_ctf_trace_get_packet_header_type returns a packet header");
3171 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_TYPE_ID_STRUCT
,
3172 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3173 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3174 packet_header_type
, "magic");
3175 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3176 bt_put(ret_field_type
);
3177 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3178 packet_header_type
, "uuid");
3179 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3180 bt_put(ret_field_type
);
3181 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3182 packet_header_type
, "stream_id");
3183 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3184 bt_put(ret_field_type
);
3186 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3187 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3188 packet_header_field_type
, "custom_trace_packet_header_field"),
3189 "Added a custom trace packet header field successfully");
3191 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3192 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3193 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3194 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3195 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3196 "Set a trace packet_header_type successfully");
3198 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3199 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3201 /* Add a custom field to the stream class' packet context */
3202 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3203 ok(packet_context_type
,
3204 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3205 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_TYPE_ID_STRUCT
,
3206 "Packet context is a structure");
3208 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3209 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3210 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3211 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3213 integer_type
= bt_ctf_field_type_integer_create(32);
3214 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3216 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3217 /* Create a "uint5_t" equivalent custom packet context field */
3218 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3220 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3221 packet_context_field_type
, "custom_packet_context_field");
3222 ok(ret
== 0, "Packet context field added successfully");
3224 /* Define a stream event context containing a my_integer field. */
3225 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3226 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3227 ok(bt_ctf_stream_class_get_event_context_type(
3228 stream_class
) == NULL
,
3229 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3230 stream_event_context_type
= bt_ctf_field_type_structure_create();
3231 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3232 integer_type
, "common_event_context");
3234 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3235 stream_event_context_type
) < 0,
3236 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3237 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3239 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3240 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3242 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3244 ok(bt_ctf_stream_class_set_event_context_type(
3245 stream_class
, stream_event_context_type
) == 0,
3246 "Set a new stream event context type");
3247 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3249 ok(ret_field_type
== stream_event_context_type
,
3250 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3251 bt_put(ret_field_type
);
3253 /* Instantiate a stream and append events */
3254 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3255 ok(stream1
, "Instanciate a stream class from writer");
3257 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3258 "bt_ctf_stream_get_class correctly handles NULL");
3259 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3260 ok(ret_stream_class
,
3261 "bt_ctf_stream_get_class returns a stream class");
3262 ok(ret_stream_class
== stream_class
,
3263 "Returned stream class is of the correct type");
3266 * Packet header, packet context, event header, and stream
3267 * event context types were copied for the resolving
3270 BT_PUT(packet_header_type
);
3271 BT_PUT(packet_context_type
);
3272 BT_PUT(stream_event_context_type
);
3273 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3274 assert(packet_header_type
);
3275 packet_context_type
=
3276 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3277 assert(packet_context_type
);
3278 stream_event_context_type
=
3279 bt_ctf_stream_class_get_event_context_type(stream_class
);
3280 assert(stream_event_context_type
);
3283 * Try to modify the packet context type after a stream has been
3286 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3287 packet_header_field_type
, "should_fail");
3289 "Trace packet header type can't be modified once a stream has been instanciated");
3292 * Try to modify the packet context type after a stream has been
3295 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3296 packet_context_field_type
, "should_fail");
3298 "Packet context type can't be modified once a stream has been instanciated");
3301 * Try to modify the stream event context type after a stream has been
3304 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3305 integer_type
, "should_fail");
3307 "Stream event context type can't be modified once a stream has been instanciated");
3309 /* Should fail after instanciating a stream (frozen) */
3310 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3311 "Changes to a stream class that was already instantiated fail");
3313 /* Populate the custom packet header field only once for all tests */
3314 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3315 "bt_ctf_stream_get_packet_header handles NULL correctly");
3316 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3318 "bt_ctf_stream_get_packet_header returns a packet header");
3319 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3320 ok(ret_field_type
== packet_header_type
,
3321 "Stream returns a packet header of the appropriate type");
3322 bt_put(ret_field_type
);
3323 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3324 "custom_trace_packet_header_field");
3325 ok(packet_header_field
,
3326 "Packet header structure contains a custom field with the appropriate name");
3327 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3328 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3329 "Custom packet header field is of the expected type");
3330 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3331 54321), "Set custom packet header value successfully");
3332 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3333 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3334 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3335 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3336 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3337 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3338 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3339 "Successfully set a stream's packet header");
3341 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3342 "Add environment field to writer after stream creation");
3344 test_instanciate_event_before_stream(writer
);
3346 append_simple_event(stream_class
, stream1
, clock
);
3348 packet_resize_test(stream_class
, stream1
, clock
);
3350 append_complex_event(stream_class
, stream1
, clock
);
3352 append_existing_event_class(stream_class
);
3354 test_empty_stream(writer
);
3356 test_custom_event_header_stream(writer
);
3358 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3359 ok(metadata_string
, "Get metadata string");
3361 bt_ctf_writer_flush_metadata(writer
);
3362 validate_metadata(argv
[1], metadata_path
);
3363 validate_trace(argv
[2], trace_path
);
3366 bt_put(ret_stream_class
);
3369 bt_put(packet_context_type
);
3370 bt_put(packet_context_field_type
);
3371 bt_put(integer_type
);
3372 bt_put(stream_event_context_type
);
3373 bt_put(ret_field_type
);
3374 bt_put(packet_header_type
);
3375 bt_put(packet_header_field_type
);
3376 bt_put(packet_header
);
3377 bt_put(packet_header_field
);
3379 free(metadata_string
);
3380 bt_put(stream_class
);
3382 /* Remove all trace files and delete temporary trace directory */
3383 DIR *trace_dir
= opendir(trace_path
);
3385 perror("# opendir");
3389 struct dirent
*entry
;
3390 while ((entry
= readdir(trace_dir
))) {
3392 char filename
[PATH_MAX
];
3394 if (snprintf(filename
, sizeof(filename
), "%s/%s",
3395 trace_path
, entry
->d_name
) <= 0) {
3399 if (stat(entry
->d_name
, &st
)) {
3403 if (S_ISREG(st
.st_mode
)) {
3404 unlinkat(bt_dirfd(trace_dir
), entry
->d_name
, 0);
3409 closedir(trace_dir
);