6 * Copyright 2013 - 2015 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf-ir/stream-class.h>
29 #include <babeltrace/ref.h>
30 #include <babeltrace/ctf/events.h>
31 #include <babeltrace/values.h>
33 #include <babeltrace/compat/stdlib.h>
35 #include <sys/utsname.h>
36 #include <babeltrace/compat/limits.h>
37 #include <babeltrace/compat/stdio.h>
42 #include <babeltrace/compat/dirent.h>
48 #define METADATA_LINE_SIZE 512
49 #define SEQUENCE_TEST_LENGTH 10
50 #define ARRAY_TEST_LENGTH 5
51 #define PACKET_RESIZE_TEST_LENGTH 100000
53 #define DEFAULT_CLOCK_FREQ 1000000000
54 #define DEFAULT_CLOCK_PRECISION 1
55 #define DEFAULT_CLOCK_OFFSET 0
56 #define DEFAULT_CLOCK_OFFSET_S 0
57 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
58 #define DEFAULT_CLOCK_TIME 0
62 static int64_t current_time
= 42;
64 /* Return 1 if uuids match, zero if different. */
65 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
70 if (!uuid_a
|| !uuid_b
) {
74 for (i
= 0; i
< 16; i
++) {
75 if (uuid_a
[i
] != uuid_b
[i
]) {
85 void validate_metadata(char *parser_path
, char *metadata_path
)
88 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
89 int parser_output_fd
= -1, metadata_fd
= -1;
96 parser_output_fd
= mkstemp(parser_output_path
);
97 metadata_fd
= open(metadata_path
, O_RDONLY
);
99 unlink(parser_output_path
);
101 if (parser_output_fd
== -1 || metadata_fd
== -1) {
102 diag("Failed create temporary files for metadata parsing.");
110 waitpid(pid
, &status
, 0);
111 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
113 /* ctf-parser-test expects a metadata string on stdin. */
114 ret
= dup2(metadata_fd
, STDIN_FILENO
);
116 perror("# dup2 metadata_fd to STDIN");
120 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
122 perror("# dup2 parser_output_fd to STDOUT");
126 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
128 perror("# dup2 parser_output_fd to STDERR");
132 execl(parser_path
, "ctf-parser-test", NULL
);
133 perror("# Could not launch the ctf metadata parser process");
137 ok(ret
== 0, "Metadata string is valid");
139 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
141 size_t len
= METADATA_LINE_SIZE
;
142 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
144 metadata_fp
= fdopen(metadata_fd
, "r");
146 perror("fdopen on metadata_fd");
151 parser_output_fp
= fdopen(parser_output_fd
, "r");
152 if (!parser_output_fp
) {
153 perror("fdopen on parser_output_fd");
156 parser_output_fd
= -1;
160 diag("malloc failure");
165 /* Output the metadata and parser output as diagnostic */
166 while (bt_getline(&line
, &len
, metadata_fp
) > 0) {
167 fprintf(stderr
, "# %s", line
);
170 rewind(parser_output_fp
);
171 while (bt_getline(&line
, &len
, parser_output_fp
) > 0) {
172 fprintf(stderr
, "# %s", line
);
178 if (fclose(metadata_fp
)) {
179 diag("fclose failure");
182 if (parser_output_fp
) {
183 if (fclose(parser_output_fp
)) {
184 diag("fclose failure");
189 if (parser_output_fd
>= 0) {
190 if (close(parser_output_fd
)) {
194 if (metadata_fd
>= 0) {
195 if (close(metadata_fd
)) {
201 void validate_trace(char *parser_path
, char *trace_path
)
204 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
205 int babeltrace_output_fd
= -1;
212 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
213 unlink(babeltrace_output_path
);
215 if (babeltrace_output_fd
== -1) {
216 diag("Failed to create a temporary file for trace parsing.");
224 waitpid(pid
, &status
, 0);
225 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
227 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
229 perror("# dup2 babeltrace_output_fd to STDOUT");
233 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
235 perror("# dup2 babeltrace_output_fd to STDERR");
239 execl(parser_path
, "babeltrace", trace_path
, NULL
);
240 perror("# Could not launch the babeltrace process");
244 ok(ret
== 0, "Babeltrace could read the resulting trace");
246 if (ret
&& babeltrace_output_fd
>= 0) {
248 size_t len
= METADATA_LINE_SIZE
;
249 FILE *babeltrace_output_fp
= NULL
;
251 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
252 if (!babeltrace_output_fp
) {
253 perror("fdopen on babeltrace_output_fd");
256 babeltrace_output_fd
= -1;
260 diag("malloc error");
262 rewind(babeltrace_output_fp
);
263 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
269 if (babeltrace_output_fp
) {
270 if (fclose(babeltrace_output_fp
)) {
271 diag("fclose error");
276 if (babeltrace_output_fd
>= 0) {
277 if (close(babeltrace_output_fd
)) {
283 void event_copy_tests(struct bt_ctf_event
*event
)
285 struct bt_ctf_event
*copy
;
286 struct bt_ctf_event_class
*orig_event_class
;
287 struct bt_ctf_event_class
*copy_event_class
;
288 struct bt_ctf_stream
*orig_stream
;
289 struct bt_ctf_stream
*copy_stream
;
290 struct bt_ctf_field
*orig_field
;
291 struct bt_ctf_field
*copy_field
;
294 ok(!bt_ctf_event_copy(NULL
),
295 "bt_ctf_event_copy handles NULL correctly");
296 copy
= bt_ctf_event_copy(event
);
297 ok(copy
, "bt_ctf_event_copy returns a valid pointer");
299 /* validate event class */
300 orig_event_class
= bt_ctf_event_get_class(event
);
301 assert(orig_event_class
);
302 copy_event_class
= bt_ctf_event_get_class(copy
);
303 ok(orig_event_class
== copy_event_class
,
304 "original and copied events share the same event class pointer");
305 bt_put(orig_event_class
);
306 bt_put(copy_event_class
);
308 /* validate stream */
309 orig_stream
= bt_ctf_event_get_stream(event
);
310 copy_stream
= bt_ctf_event_get_stream(copy
);
313 ok(!copy_stream
, "original and copied events have no stream");
315 ok(orig_stream
== copy_stream
,
316 "original and copied events share the same stream pointer");
322 orig_field
= bt_ctf_event_get_header(event
);
323 copy_field
= bt_ctf_event_get_header(copy
);
326 ok(!copy_field
, "original and copied events have no header");
328 ok(orig_field
!= copy_field
,
329 "original and copied events headers are different pointers");
336 orig_field
= bt_ctf_event_get_event_context(event
);
337 copy_field
= bt_ctf_event_get_event_context(copy
);
340 ok(!copy_field
, "original and copied events have no context");
342 ok(orig_field
!= copy_field
,
343 "original and copied events contexts are different pointers");
350 orig_field
= bt_ctf_event_get_payload_field(event
);
351 copy_field
= bt_ctf_event_get_payload_field(copy
);
354 ok(!copy_field
, "original and copied events have no payload");
356 ok(orig_field
!= copy_field
,
357 "original and copied events payloads are different pointers");
366 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
367 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
369 /* Create and add a simple event class */
370 struct bt_ctf_event_class
*simple_event_class
=
371 bt_ctf_event_class_create("Simple Event");
372 struct bt_ctf_field_type
*uint_12_type
=
373 bt_ctf_field_type_integer_create(12);
374 struct bt_ctf_field_type
*int_64_type
=
375 bt_ctf_field_type_integer_create(64);
376 struct bt_ctf_field_type
*float_type
=
377 bt_ctf_field_type_floating_point_create();
378 struct bt_ctf_field_type
*enum_type
;
379 struct bt_ctf_field_type
*enum_type_unsigned
=
380 bt_ctf_field_type_enumeration_create(uint_12_type
);
381 struct bt_ctf_field_type
*event_context_type
=
382 bt_ctf_field_type_structure_create();
383 struct bt_ctf_field_type
*event_payload_type
= NULL
;
384 struct bt_ctf_field_type
*returned_type
;
385 struct bt_ctf_event
*simple_event
;
386 struct bt_ctf_field
*integer_field
;
387 struct bt_ctf_field
*float_field
;
388 struct bt_ctf_field
*enum_field
;
389 struct bt_ctf_field
*enum_field_unsigned
;
390 struct bt_ctf_field
*enum_container_field
;
391 const char *mapping_name_test
= "truie";
392 const double double_test_value
= 3.1415;
393 struct bt_ctf_field
*enum_container_field_unsigned
;
394 const char *mapping_name_negative_test
= "negative_value";
395 const char *ret_char
;
397 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
398 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
399 struct bt_ctf_clock
*ret_clock
;
400 struct bt_ctf_event_class
*ret_event_class
;
401 struct bt_ctf_field
*packet_context
;
402 struct bt_ctf_field
*packet_context_field
;
403 struct bt_ctf_field
*stream_event_context
;
404 struct bt_ctf_field
*stream_event_context_field
;
405 struct bt_ctf_field
*event_context
;
406 struct bt_ctf_field
*event_context_field
;
407 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
408 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
409 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
412 ok(uint_12_type
, "Create an unsigned integer type");
414 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
415 ok(int_64_type
, "Create a signed integer type");
416 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
418 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
419 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
420 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
421 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
422 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
423 bt_put(returned_type
);
425 bt_ctf_field_type_set_alignment(float_type
, 32);
426 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
427 "bt_ctf_field_type_get_alignment handles NULL correctly");
428 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
429 "bt_ctf_field_type_get_alignment returns a correct value");
431 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
432 "Set a floating point type's exponent digit count");
433 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
434 "Set a floating point type's mantissa digit count");
436 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
437 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
438 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
439 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
440 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
441 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
442 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
443 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
445 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
446 mapping_name_negative_test
, -12345, 0) == 0,
447 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
448 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
449 "escaping; \"test\"", 1, 1) == 0,
450 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
451 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
452 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
453 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
454 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
455 "event clock int float", 5, 22) == 0,
456 "Accept enumeration mapping strings containing reserved keywords");
457 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
459 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
460 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
461 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
462 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
463 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
464 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
465 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
467 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
468 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
469 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
470 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
471 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
472 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
474 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
475 "enum_field") == 0, "Add signed enumeration field to event");
477 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
478 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
479 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
480 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
481 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
482 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
483 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
484 NULL
, &ret_range_end_int64_t
) < 0,
485 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
486 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
487 &ret_range_start_int64_t
, NULL
) < 0,
488 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
489 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
490 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
491 "bt_ctf_field_type_enumeration_get_mapping returns a value");
492 ok(!strcmp(ret_char
, mapping_name_test
),
493 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
494 ok(ret_range_start_int64_t
== 42,
495 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
496 ok(ret_range_end_int64_t
== 42,
497 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
499 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
500 "escaping; \"test\"", 0, 0) == 0,
501 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
502 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
503 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
504 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
505 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
506 "event clock int float", 5, 22) == 0,
507 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
508 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
510 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
511 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
512 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
513 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
514 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
515 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
516 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
517 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
519 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
520 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
521 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
522 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
524 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
525 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
526 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
527 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
528 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
529 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
530 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
531 NULL
, &ret_range_end_uint64_t
) < 0,
532 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
533 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
534 &ret_range_start_uint64_t
, NULL
) < 0,
535 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
536 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
537 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
538 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
539 ok(!strcmp(ret_char
, mapping_name_test
),
540 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
541 ok(ret_range_start_uint64_t
== 42,
542 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
543 ok(ret_range_end_uint64_t
== 42,
544 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
546 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
548 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
551 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
553 /* Set an event context type which will contain a single integer*/
554 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
555 "event_specific_context"),
556 "Add event specific context field");
557 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
558 "bt_ctf_event_class_get_context_type handles NULL correctly");
559 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
560 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
562 ok(bt_ctf_event_class_set_context_type(simple_event_class
, NULL
) < 0,
563 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
564 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
565 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
566 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
567 "Set an event class' context type successfully");
568 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
569 ok(returned_type
== event_context_type
,
570 "bt_ctf_event_class_get_context_type returns the appropriate type");
571 bt_put(returned_type
);
573 bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
);
576 * bt_ctf_stream_class_add_event_class() copies the field types
577 * of simple_event_class, so we retrieve the new ones to create
578 * the appropriate fields.
580 BT_PUT(event_context_type
);
581 BT_PUT(event_payload_type
);
582 event_payload_type
= bt_ctf_event_class_get_payload_type(
584 assert(event_payload_type
);
585 event_context_type
= bt_ctf_event_class_get_context_type(
587 assert(event_context_type
);
588 ep_integer_field_type
=
589 bt_ctf_field_type_structure_get_field_type_by_name(
590 event_payload_type
, "integer_field");
591 assert(ep_integer_field_type
);
593 bt_ctf_field_type_structure_get_field_type_by_name(
594 event_payload_type
, "enum_field");
595 assert(ep_enum_field_type
);
596 ep_enum_field_unsigned_type
=
597 bt_ctf_field_type_structure_get_field_type_by_name(
598 event_payload_type
, "enum_field_unsigned");
599 assert(ep_enum_field_unsigned_type
);
601 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
602 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
603 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
604 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
605 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
606 "bt_ctf_stream_class_get_event_class handles NULL correctly");
607 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
608 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
609 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
610 ok(ret_event_class
== simple_event_class
,
611 "bt_ctf_stream_class_get_event_class returns the correct event class");
612 bt_put(ret_event_class
);
613 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
614 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
615 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
616 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
618 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
619 ok(ret_event_class
== simple_event_class
,
620 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
621 bt_put(ret_event_class
);
623 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
624 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
625 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
626 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
627 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
628 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
629 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
630 ok(ret_event_class
== simple_event_class
,
631 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
632 bt_put(ret_event_class
);
634 simple_event
= bt_ctf_event_create(simple_event_class
);
636 "Instantiate an event containing a single integer field");
638 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
639 "bt_ctf_event_get_clock handles NULL correctly");
640 ret_clock
= bt_ctf_event_get_clock(simple_event
);
641 ok(ret_clock
== clock
,
642 "bt_ctf_event_get_clock returns a correct clock");
645 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
646 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
647 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
648 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
650 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
651 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
652 "bt_ctf_field_floating_point_get_value fails on an unset float field");
653 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
654 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
655 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
656 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
657 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
658 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
659 "bt_ctf_field_floating_point_get_value returns a double value");
660 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
661 "bt_ctf_field_floating_point_get_value returns a correct value");
663 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
665 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
666 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
667 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
668 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
669 enum_container_field
= bt_ctf_field_enumeration_get_container(
671 ok(bt_ctf_field_signed_integer_set_value(
672 enum_container_field
, -42) == 0,
673 "Set signed enumeration container value");
674 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
675 ok(!strcmp(ret_char
, mapping_name_negative_test
),
676 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
677 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
680 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
681 assert(enum_field_unsigned
);
682 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
683 enum_field_unsigned
);
684 ok(bt_ctf_field_unsigned_integer_set_value(
685 enum_container_field_unsigned
, 42) == 0,
686 "Set unsigned enumeration container value");
687 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
688 enum_field_unsigned
);
690 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
691 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
692 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
694 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
696 /* Populate stream event context */
697 stream_event_context
= bt_ctf_stream_get_event_context(stream
);
698 stream_event_context_field
= bt_ctf_field_structure_get_field(
699 stream_event_context
, "common_event_context");
700 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
702 /* Populate the event's context */
703 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
704 "bt_ctf_event_get_event_context handles NULL correctly");
705 event_context
= bt_ctf_event_get_event_context(simple_event
);
707 "bt_ctf_event_get_event_context returns a field");
708 returned_type
= bt_ctf_field_get_type(event_context
);
709 ok(returned_type
== event_context_type
,
710 "bt_ctf_event_get_event_context returns a field of the appropriate type");
711 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
712 "event_specific_context");
713 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
714 "Successfully set an event context's value");
715 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
716 "bt_ctf_event_set_event_context handles a NULL event correctly");
717 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
718 "bt_ctf_event_set_event_context handles a NULL event context correctly");
719 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
720 "bt_ctf_event_set_event_context rejects a context of the wrong type");
721 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
722 "Set an event context successfully");
724 event_copy_tests(simple_event
);
725 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
726 "Append simple event to trace stream");
728 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
729 "bt_ctf_stream_get_packet_context handles NULL correctly");
730 packet_context
= bt_ctf_stream_get_packet_context(stream
);
732 "bt_ctf_stream_get_packet_context returns a packet context");
734 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
736 ok(packet_context_field
,
737 "Packet context contains the default packet_size field.");
738 bt_put(packet_context_field
);
739 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
740 "custom_packet_context_field");
741 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
742 "Custom packet context field value successfully set.");
744 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
745 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
746 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
747 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
748 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
749 "Successfully set a stream's packet context");
751 ok(bt_ctf_stream_flush(stream
) == 0,
752 "Flush trace stream with one event");
754 bt_put(simple_event_class
);
755 bt_put(simple_event
);
756 bt_put(uint_12_type
);
760 bt_put(enum_type_unsigned
);
761 bt_put(returned_type
);
762 bt_put(event_context_type
);
763 bt_put(integer_field
);
766 bt_put(enum_field_unsigned
);
767 bt_put(enum_container_field
);
768 bt_put(enum_container_field_unsigned
);
769 bt_put(packet_context
);
770 bt_put(packet_context_field
);
771 bt_put(stream_event_context
);
772 bt_put(stream_event_context_field
);
773 bt_put(event_context
);
774 bt_put(event_context_field
);
775 bt_put(event_payload_type
);
776 bt_put(ep_integer_field_type
);
777 bt_put(ep_enum_field_type
);
778 bt_put(ep_enum_field_unsigned_type
);
781 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
782 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
784 struct event_class_attrs_counts
{
795 struct event_class_attrs_counts
;
796 const char *complex_test_event_string
= "Complex Test Event";
797 const char *test_string_1
= "Test ";
798 const char *test_string_2
= "string ";
799 const char *test_string_3
= "abcdefghi";
800 const char *test_string_4
= "abcd\0efg\0hi";
801 const char *test_string_cat
= "Test string abcdeabcd";
802 struct bt_ctf_field_type
*uint_35_type
=
803 bt_ctf_field_type_integer_create(35);
804 struct bt_ctf_field_type
*int_16_type
=
805 bt_ctf_field_type_integer_create(16);
806 struct bt_ctf_field_type
*uint_3_type
=
807 bt_ctf_field_type_integer_create(3);
808 struct bt_ctf_field_type
*enum_variant_type
=
809 bt_ctf_field_type_enumeration_create(uint_3_type
);
810 struct bt_ctf_field_type
*variant_type
=
811 bt_ctf_field_type_variant_create(enum_variant_type
,
813 struct bt_ctf_field_type
*string_type
=
814 bt_ctf_field_type_string_create();
815 struct bt_ctf_field_type
*sequence_type
;
816 struct bt_ctf_field_type
*array_type
;
817 struct bt_ctf_field_type
*inner_structure_type
=
818 bt_ctf_field_type_structure_create();
819 struct bt_ctf_field_type
*complex_structure_type
=
820 bt_ctf_field_type_structure_create();
821 struct bt_ctf_field_type
*ret_field_type
;
822 struct bt_ctf_event_class
*event_class
;
823 struct bt_ctf_event
*event
;
824 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
825 *inner_structure_field
, *complex_structure_field
,
826 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
827 *variant_field
, *an_array_field
, *ret_field
;
828 uint64_t ret_unsigned_int
;
829 int64_t ret_signed_int
;
830 const char *ret_string
;
831 struct bt_ctf_stream_class
*ret_stream_class
;
832 struct bt_ctf_event_class
*ret_event_class
;
833 struct bt_ctf_field
*packet_context
, *packet_context_field
;
834 struct bt_value
*obj
;
836 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
837 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
838 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
839 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
840 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
841 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
842 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
843 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
844 bt_ctf_field_type_set_alignment(int_16_type
, 32);
845 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
846 bt_ctf_field_type_integer_set_base(uint_35_type
,
847 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
849 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
850 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
853 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
854 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
855 ret_field_type
= bt_ctf_field_type_array_get_element_type(
857 ok(ret_field_type
== int_16_type
,
858 "bt_ctf_field_type_array_get_element_type returns the correct type");
859 bt_put(ret_field_type
);
861 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
862 "bt_ctf_field_type_array_get_length handles NULL correctly");
863 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
864 "bt_ctf_field_type_array_get_length returns the correct length");
866 bt_ctf_field_type_structure_add_field(inner_structure_type
,
867 uint_35_type
, "seq_len");
868 bt_ctf_field_type_structure_add_field(inner_structure_type
,
869 sequence_type
, "a_sequence");
870 bt_ctf_field_type_structure_add_field(inner_structure_type
,
871 array_type
, "an_array");
873 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
875 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
877 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
878 "UINT35_TYPE", 2, 7);
880 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
882 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
883 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
884 enum_variant_type
, NULL
) < 0,
885 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
886 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
887 enum_variant_type
, "INT16_TYPE") == 1,
888 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
890 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
891 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
892 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
893 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
894 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
895 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
897 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
898 "An unknown entry"), "Reject a variant field based on an unknown tag value");
899 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
900 "UINT3_TYPE") == 0, "Add a field to a variant");
901 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
903 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
906 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
907 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
908 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
909 ok(ret_field_type
== enum_variant_type
,
910 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
911 bt_put(ret_field_type
);
913 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
914 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
915 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
916 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
917 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
918 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
919 "INT16_TYPE") == NULL
,
920 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
921 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
923 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
924 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
925 variant_type
, "INT16_TYPE");
926 ok(ret_field_type
== int_16_type
,
927 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
928 bt_put(ret_field_type
);
930 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
931 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
932 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
933 "bt_ctf_field_type_variant_get_field_count returns the correct count");
935 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
936 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
937 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
938 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
939 bt_put(ret_field_type
);
940 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
941 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
942 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
943 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
944 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
945 "bt_ctf_field_type_variant_get_field returns a field");
946 ok(!strcmp("INT16_TYPE", ret_string
),
947 "bt_ctf_field_type_variant_get_field returns a correct field name");
948 ok(ret_field_type
== int_16_type
,
949 "bt_ctf_field_type_variant_get_field returns a correct field type");
950 bt_put(ret_field_type
);
952 bt_ctf_field_type_structure_add_field(complex_structure_type
,
953 enum_variant_type
, "variant_selector");
954 bt_ctf_field_type_structure_add_field(complex_structure_type
,
955 string_type
, "a_string");
956 bt_ctf_field_type_structure_add_field(complex_structure_type
,
957 variant_type
, "variant_value");
958 bt_ctf_field_type_structure_add_field(complex_structure_type
,
959 inner_structure_type
, "inner_structure");
961 ok(bt_ctf_event_class_create("clock") == NULL
,
962 "Reject creation of an event class with an illegal name");
963 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
964 ok(event_class
, "Create an event class");
965 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
966 "Reject addition of a field with an empty name to an event");
967 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
968 "Reject addition of a field with a NULL type to an event");
969 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
971 "Reject addition of a type with an illegal name to an event");
972 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
974 "Add field of type unsigned integer to an event");
975 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
976 "int_16") == 0, "Add field of type signed integer to an event");
977 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
978 "complex_structure") == 0,
979 "Add composite structure to an event");
981 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
982 "bt_ctf_event_class_get_name handles NULL correctly");
983 ret_string
= bt_ctf_event_class_get_name(event_class
);
984 ok(!strcmp(ret_string
, complex_test_event_string
),
985 "bt_ctf_event_class_get_name returns a correct name");
986 ok(bt_ctf_event_class_get_id(event_class
) < 0,
987 "bt_ctf_event_class_get_id returns a negative value when not set");
988 ok(bt_ctf_event_class_get_id(NULL
) < 0,
989 "bt_ctf_event_class_get_id handles NULL correctly");
990 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
991 "bt_ctf_event_class_set_id handles NULL correctly");
992 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
993 "Set an event class' id");
994 ok(bt_ctf_event_class_get_id(event_class
) == 42,
995 "bt_ctf_event_class_get_id returns the correct value");
997 /* Test event class attributes */
998 obj
= bt_value_integer_create_init(15);
1000 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
1001 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
1002 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
1003 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
1004 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
1005 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
1006 assert(!bt_value_integer_set(obj
, -3));
1007 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
1008 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
1009 assert(!bt_value_integer_set(obj
, 11));
1010 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
1011 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
1012 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
1013 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
1014 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
1016 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
1019 obj
= bt_value_integer_create_init(5);
1021 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
1022 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
1024 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
1025 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
1026 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
1027 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
1028 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
1029 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
1030 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
1033 ret
= bt_value_integer_get(obj
, &int64_value
);
1034 ok(obj
&& !ret
&& int64_value
== 5,
1035 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
1038 obj
= bt_value_string_create_init("nu name");
1040 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
1041 ret_string
= bt_ctf_event_class_get_name(event_class
);
1042 ok(!strcmp(ret_string
, "nu name"),
1043 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
1044 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
1045 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
1047 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
1049 obj
= bt_value_string_create_init("http://kernel.org/");
1051 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
1054 ok(bt_ctf_event_class_get_attribute_count(NULL
),
1055 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1056 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
1057 "bt_ctf_event_class_get_attribute_count returns the correct count");
1058 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
1059 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1060 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
1061 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1062 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
1063 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1064 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
1065 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1067 memset(&attrs_count
, 0, sizeof(attrs_count
));
1069 for (i
= 0; i
< 4; ++i
) {
1070 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
1072 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
1073 assert(ret_string
&& obj
);
1075 if (!strcmp(ret_string
, "id")) {
1077 ok(bt_value_is_integer(obj
),
1078 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1080 } else if (!strcmp(ret_string
, "name")) {
1082 ok(bt_value_is_string(obj
),
1083 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1085 } else if (!strcmp(ret_string
, "loglevel")) {
1086 attrs_count
.loglevel
++;
1087 ok(bt_value_is_integer(obj
),
1088 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1090 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1091 attrs_count
.modelemfuri
++;
1092 ok(bt_value_is_string(obj
),
1093 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1096 attrs_count
.unknown
++;
1102 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1103 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1104 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1105 "event class has one instance of each known attribute");
1107 /* Add event class to the stream class */
1108 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1109 "Reject addition of NULL event class to a stream class");
1110 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1111 event_class
) == 0, "Add an event class to stream class");
1113 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1114 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1115 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1116 ok(ret_stream_class
== stream_class
,
1117 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1118 bt_put(ret_stream_class
);
1120 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1121 "bt_ctf_event_class_get_field_count handles NULL correctly");
1122 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1123 "bt_ctf_event_class_get_field_count returns a correct value");
1125 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1126 &ret_field_type
, 0) < 0,
1127 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1128 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1129 &ret_field_type
, 0) == 0,
1130 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1131 bt_put(ret_field_type
);
1132 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1134 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1135 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1136 &ret_field_type
, 42) < 0,
1137 "bt_ctf_event_class_get_field handles an invalid index correctly");
1138 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1139 &ret_field_type
, 0) == 0,
1140 "bt_ctf_event_class_get_field returns a field");
1141 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
1142 "bt_ctf_event_class_get_field returns a correct field type");
1143 bt_put(ret_field_type
);
1144 ok(!strcmp(ret_string
, "uint_35"),
1145 "bt_ctf_event_class_get_field returns a correct field name");
1146 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1147 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1148 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1149 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1150 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1151 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1152 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1153 "complex_structure");
1154 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
1155 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1156 bt_put(ret_field_type
);
1158 event
= bt_ctf_event_create(event_class
);
1159 ok(event
, "Instanciate a complex event");
1161 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1162 "bt_ctf_event_get_class handles NULL correctly");
1163 ret_event_class
= bt_ctf_event_get_class(event
);
1164 ok(ret_event_class
== event_class
,
1165 "bt_ctf_event_get_class returns the correct event class");
1166 bt_put(ret_event_class
);
1168 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1169 if (!uint_35_field
) {
1170 printf("uint_35_field is NULL\n");
1173 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1174 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1175 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1176 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1177 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1178 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1179 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1180 &ret_unsigned_int
) == 0,
1181 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1182 ok(ret_unsigned_int
== 0x0DDF00D,
1183 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1184 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1185 &ret_signed_int
) < 0,
1186 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1187 bt_put(uint_35_field
);
1189 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1190 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1191 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1192 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1193 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1194 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1195 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1196 &ret_signed_int
) == 0,
1197 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1198 ok(ret_signed_int
== -12345,
1199 "bt_ctf_field_signed_integer_get_value returns the correct value");
1200 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1201 &ret_unsigned_int
) < 0,
1202 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1203 bt_put(int_16_field
);
1205 complex_structure_field
= bt_ctf_event_get_payload(event
,
1206 "complex_structure");
1208 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1209 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1210 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1211 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1212 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1213 complex_structure_field
, 3);
1214 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1215 bt_put(inner_structure_field
);
1216 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1217 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1218 bt_put(ret_field_type
);
1220 inner_structure_field
= bt_ctf_field_structure_get_field(
1221 complex_structure_field
, "inner_structure");
1222 a_string_field
= bt_ctf_field_structure_get_field(
1223 complex_structure_field
, "a_string");
1224 enum_variant_field
= bt_ctf_field_structure_get_field(
1225 complex_structure_field
, "variant_selector");
1226 variant_field
= bt_ctf_field_structure_get_field(
1227 complex_structure_field
, "variant_value");
1228 uint_35_field
= bt_ctf_field_structure_get_field(
1229 inner_structure_field
, "seq_len");
1230 a_sequence_field
= bt_ctf_field_structure_get_field(
1231 inner_structure_field
, "a_sequence");
1232 an_array_field
= bt_ctf_field_structure_get_field(
1233 inner_structure_field
, "an_array");
1235 enum_container_field
= bt_ctf_field_enumeration_get_container(
1236 enum_variant_field
);
1237 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1238 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1239 enum_variant_field
);
1240 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1241 bt_put(int_16_field
);
1242 ok(!bt_ctf_field_string_get_value(a_string_field
),
1243 "bt_ctf_field_string_get_value returns NULL on an unset field");
1244 bt_ctf_field_string_set_value(a_string_field
,
1246 ok(!bt_ctf_field_string_get_value(NULL
),
1247 "bt_ctf_field_string_get_value correctly handles NULL");
1248 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1249 "bt_ctf_field_string_append correctly handles a NULL string field");
1250 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1251 "bt_ctf_field_string_append correctly handles a NULL string value");
1252 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1253 "bt_ctf_field_string_append succeeds");
1254 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1255 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1256 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1257 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1258 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1259 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1260 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1261 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1262 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1263 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1264 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1265 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1267 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1268 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1269 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1270 "bt_ctf_field_string_get_value returns a correct value");
1271 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1272 SEQUENCE_TEST_LENGTH
);
1274 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1275 enum_container_field
) == NULL
,
1276 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1277 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1279 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1280 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1281 variant_type
, enum_variant_field
);
1282 ok(ret_field_type
== int_16_type
,
1283 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1285 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1286 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1287 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1288 uint_35_field
) == 0, "Set a sequence field's length");
1289 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1290 ok(ret_field
== uint_35_field
,
1291 "bt_ctf_field_sequence_get_length returns the correct length field");
1292 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1293 "bt_ctf_field_sequence_get_length properly handles NULL");
1295 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1296 int_16_field
= bt_ctf_field_sequence_get_field(
1297 a_sequence_field
, i
);
1298 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1299 bt_put(int_16_field
);
1302 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1303 int_16_field
= bt_ctf_field_array_get_field(
1305 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1306 bt_put(int_16_field
);
1309 bt_ctf_clock_set_time(clock
, ++current_time
);
1310 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1311 "Append a complex event to a stream");
1314 * Populate the custom packet context field with a dummy value
1315 * otherwise flush will fail.
1317 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1318 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1319 "custom_packet_context_field");
1320 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1322 ok(bt_ctf_stream_flush(stream
) == 0,
1323 "Flush a stream containing a complex event");
1325 bt_put(uint_35_field
);
1326 bt_put(a_string_field
);
1327 bt_put(inner_structure_field
);
1328 bt_put(complex_structure_field
);
1329 bt_put(a_sequence_field
);
1330 bt_put(an_array_field
);
1331 bt_put(enum_variant_field
);
1332 bt_put(enum_container_field
);
1333 bt_put(variant_field
);
1335 bt_put(packet_context_field
);
1336 bt_put(packet_context
);
1337 bt_put(uint_35_type
);
1338 bt_put(int_16_type
);
1339 bt_put(string_type
);
1340 bt_put(sequence_type
);
1342 bt_put(inner_structure_type
);
1343 bt_put(complex_structure_type
);
1344 bt_put(uint_3_type
);
1345 bt_put(enum_variant_type
);
1346 bt_put(variant_type
);
1347 bt_put(ret_field_type
);
1348 bt_put(event_class
);
1352 static void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1353 struct bt_ctf_field_type
*expected_type
, const char *name
)
1355 struct bt_ctf_field_type
*copy_type
;
1357 copy_type
= bt_ctf_field_get_type(field
);
1358 ok(copy_type
== expected_type
,
1359 "bt_ctf_field_copy does not copy the type (%s)", name
);
1363 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1364 struct bt_ctf_field
*field_b
, const char *name
)
1366 ok(field_a
!= field_b
,
1367 "bt_ctf_field_copy creates different pointers (%s)", name
);
1370 void field_copy_tests()
1372 struct bt_ctf_field_type
*len_type
= NULL
;
1373 struct bt_ctf_field_type
*fp_type
= NULL
;
1374 struct bt_ctf_field_type
*s_type
= NULL
;
1375 struct bt_ctf_field_type
*e_int_type
= NULL
;
1376 struct bt_ctf_field_type
*e_type
= NULL
;
1377 struct bt_ctf_field_type
*v_type
= NULL
;
1378 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1379 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1380 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1381 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1382 struct bt_ctf_field_type
*strct_type
= NULL
;
1383 struct bt_ctf_field
*len
= NULL
;
1384 struct bt_ctf_field
*fp
= NULL
;
1385 struct bt_ctf_field
*s
= NULL
;
1386 struct bt_ctf_field
*e_int
= NULL
;
1387 struct bt_ctf_field
*e
= NULL
;
1388 struct bt_ctf_field
*v
= NULL
;
1389 struct bt_ctf_field
*v_selected
= NULL
;
1390 struct bt_ctf_field
*v_selected_cur
= NULL
;
1391 struct bt_ctf_field
*v_selected_0
= NULL
;
1392 struct bt_ctf_field
*v_selected_1
= NULL
;
1393 struct bt_ctf_field
*v_selected_2
= NULL
;
1394 struct bt_ctf_field
*v_selected_3
= NULL
;
1395 struct bt_ctf_field
*v_selected_4
= NULL
;
1396 struct bt_ctf_field
*v_selected_5
= NULL
;
1397 struct bt_ctf_field
*v_selected_6
= NULL
;
1398 struct bt_ctf_field
*a
= NULL
;
1399 struct bt_ctf_field
*a_0
= NULL
;
1400 struct bt_ctf_field
*a_1
= NULL
;
1401 struct bt_ctf_field
*a_2
= NULL
;
1402 struct bt_ctf_field
*a_3
= NULL
;
1403 struct bt_ctf_field
*a_4
= NULL
;
1404 struct bt_ctf_field
*strct
= NULL
;
1405 struct bt_ctf_field
*len_copy
= NULL
;
1406 struct bt_ctf_field
*fp_copy
= NULL
;
1407 struct bt_ctf_field
*s_copy
= NULL
;
1408 struct bt_ctf_field
*e_int_copy
= NULL
;
1409 struct bt_ctf_field
*e_copy
= NULL
;
1410 struct bt_ctf_field
*v_copy
= NULL
;
1411 struct bt_ctf_field
*v_selected_copy
= NULL
;
1412 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1413 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1414 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1415 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1416 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1417 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1418 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1419 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1420 struct bt_ctf_field
*a_copy
= NULL
;
1421 struct bt_ctf_field
*a_0_copy
= NULL
;
1422 struct bt_ctf_field
*a_1_copy
= NULL
;
1423 struct bt_ctf_field
*a_2_copy
= NULL
;
1424 struct bt_ctf_field
*a_3_copy
= NULL
;
1425 struct bt_ctf_field
*a_4_copy
= NULL
;
1426 struct bt_ctf_field
*strct_copy
= NULL
;
1427 uint64_t uint64_t_val
;
1428 const char *str_val
;
1432 /* create len type */
1433 len_type
= bt_ctf_field_type_integer_create(32);
1436 /* create fp type */
1437 fp_type
= bt_ctf_field_type_floating_point_create();
1441 s_type
= bt_ctf_field_type_string_create();
1444 /* create e_int type */
1445 e_int_type
= bt_ctf_field_type_integer_create(8);
1449 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1451 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1454 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1458 /* create v_label1 type */
1459 v_label1_type
= bt_ctf_field_type_string_create();
1460 assert(v_label1_type
);
1462 /* create v_label1_array type */
1463 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1464 assert(v_label1_array_type
);
1466 /* create v_label2 type */
1467 v_label2_type
= bt_ctf_field_type_integer_create(16);
1468 assert(v_label2_type
);
1470 /* create v_label2_seq type */
1471 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1473 assert(v_label2_seq_type
);
1476 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1478 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1481 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1485 /* create strct type */
1486 strct_type
= bt_ctf_field_type_structure_create();
1488 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1491 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1493 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1495 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1497 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1499 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1500 v_label1_array_type
, "a");
1504 strct
= bt_ctf_field_create(strct_type
);
1508 len
= bt_ctf_field_structure_get_field(strct
, "len");
1512 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1516 s
= bt_ctf_field_structure_get_field(strct
, "s");
1520 e
= bt_ctf_field_structure_get_field(strct
, "e");
1523 /* get e_int (underlying integer) */
1524 e_int
= bt_ctf_field_enumeration_get_container(e
);
1528 v
= bt_ctf_field_structure_get_field(strct
, "v");
1532 a
= bt_ctf_field_structure_get_field(strct
, "a");
1536 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1540 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1544 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1547 /* set e field (LABEL2) */
1548 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1552 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1554 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1555 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1556 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1557 ok(v_selected_cur
== v_selected
,
1558 "bt_ctf_field_variant_get_current_field returns the current field");
1559 bt_put(v_selected_cur
);
1561 /* set selected v field */
1562 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1564 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1565 assert(v_selected_0
);
1566 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1568 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1569 assert(v_selected_1
);
1570 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1572 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1573 assert(v_selected_2
);
1574 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1576 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1577 assert(v_selected_3
);
1578 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1580 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1581 assert(v_selected_4
);
1582 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1584 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1585 assert(v_selected_5
);
1586 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1588 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1589 assert(v_selected_6
);
1590 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1594 a_0
= bt_ctf_field_array_get_field(a
, 0);
1596 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1598 a_1
= bt_ctf_field_array_get_field(a
, 1);
1600 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1602 a_2
= bt_ctf_field_array_get_field(a
, 2);
1604 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1606 a_3
= bt_ctf_field_array_get_field(a
, 3);
1608 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1610 a_4
= bt_ctf_field_array_get_field(a
, 4);
1612 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1615 /* create copy of strct */
1616 ok(!bt_ctf_field_copy(NULL
),
1617 "bt_ctf_field_copy handles NULL correctly");
1618 strct_copy
= bt_ctf_field_copy(strct
);
1620 "bt_ctf_field_copy returns a valid pointer");
1622 /* get all copied fields */
1623 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1625 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1627 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1629 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1631 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1633 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1635 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1636 assert(v_selected_copy
);
1637 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1638 assert(v_selected_0_copy
);
1639 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1640 assert(v_selected_1_copy
);
1641 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1642 assert(v_selected_2_copy
);
1643 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1644 assert(v_selected_3_copy
);
1645 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1646 assert(v_selected_4_copy
);
1647 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1648 assert(v_selected_5_copy
);
1649 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1650 assert(v_selected_6_copy
);
1651 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1652 "sequence field copy is not too large");
1653 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1655 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1657 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1659 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1661 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1663 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1665 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1666 "array field copy is not too large");
1668 /* make sure copied fields are different pointers */
1669 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1670 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1671 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1672 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1673 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1674 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1675 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1676 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1678 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1680 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1682 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1684 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1686 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1688 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1690 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1692 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1693 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1694 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1695 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1696 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1697 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1699 /* make sure copied fields share the same types */
1700 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1701 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1702 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1703 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1704 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1705 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1706 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1708 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1710 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1712 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1714 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1716 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1718 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1720 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1722 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1723 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1724 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1725 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1726 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1727 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1729 /* validate len copy */
1730 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1732 ok(uint64_t_val
== 7,
1733 "bt_ctf_field_copy creates a valid integer field copy");
1735 /* validate fp copy */
1736 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1738 ok(double_val
== 3.14,
1739 "bt_ctf_field_copy creates a valid floating point number field copy");
1741 /* validate s copy */
1742 str_val
= bt_ctf_field_string_get_value(s_copy
);
1743 ok(str_val
&& !strcmp(str_val
, "btbt"),
1744 "bt_ctf_field_copy creates a valid string field copy");
1746 /* validate e_int copy */
1747 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1750 ok(uint64_t_val
== 23,
1751 "bt_ctf_field_copy creates a valid enum's integer field copy");
1753 /* validate e copy */
1754 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1755 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1756 "bt_ctf_field_copy creates a valid enum field copy");
1758 /* validate v_selected copy */
1759 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1760 assert(v_selected_copy_len
);
1761 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1764 ok(uint64_t_val
== 7,
1765 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1766 bt_put(v_selected_copy_len
);
1767 v_selected_copy_len
= NULL
;
1769 /* validate v_selected copy fields */
1770 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1773 ok(uint64_t_val
== 7,
1774 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1775 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1778 ok(uint64_t_val
== 6,
1779 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1780 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1783 ok(uint64_t_val
== 5,
1784 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1785 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1788 ok(uint64_t_val
== 4,
1789 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1790 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1793 ok(uint64_t_val
== 3,
1794 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1795 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1798 ok(uint64_t_val
== 2,
1799 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1800 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1803 ok(uint64_t_val
== 1,
1804 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1806 /* validate a copy fields */
1807 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1808 ok(str_val
&& !strcmp(str_val
, "a_0"),
1809 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1810 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1811 ok(str_val
&& !strcmp(str_val
, "a_1"),
1812 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1813 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1814 ok(str_val
&& !strcmp(str_val
, "a_2"),
1815 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1816 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1817 ok(str_val
&& !strcmp(str_val
, "a_3"),
1818 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1819 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1820 ok(str_val
&& !strcmp(str_val
, "a_4"),
1821 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1823 /* put everything */
1830 bt_put(v_label1_type
);
1831 bt_put(v_label1_array_type
);
1832 bt_put(v_label2_type
);
1833 bt_put(v_label2_seq_type
);
1842 bt_put(v_selected_0
);
1843 bt_put(v_selected_1
);
1844 bt_put(v_selected_2
);
1845 bt_put(v_selected_3
);
1846 bt_put(v_selected_4
);
1847 bt_put(v_selected_5
);
1848 bt_put(v_selected_6
);
1862 bt_put(v_selected_copy
);
1863 bt_put(v_selected_0_copy
);
1864 bt_put(v_selected_1_copy
);
1865 bt_put(v_selected_2_copy
);
1866 bt_put(v_selected_3_copy
);
1867 bt_put(v_selected_4_copy
);
1868 bt_put(v_selected_5_copy
);
1869 bt_put(v_selected_6_copy
);
1879 void type_field_tests()
1881 struct bt_ctf_field
*uint_12
;
1882 struct bt_ctf_field
*int_16
;
1883 struct bt_ctf_field
*string
;
1884 struct bt_ctf_field
*enumeration
;
1885 struct bt_ctf_field_type
*composite_structure_type
;
1886 struct bt_ctf_field_type
*structure_seq_type
;
1887 struct bt_ctf_field_type
*string_type
;
1888 struct bt_ctf_field_type
*sequence_type
;
1889 struct bt_ctf_field_type
*uint_8_type
;
1890 struct bt_ctf_field_type
*int_16_type
;
1891 struct bt_ctf_field_type
*uint_12_type
=
1892 bt_ctf_field_type_integer_create(12);
1893 struct bt_ctf_field_type
*enumeration_type
;
1894 struct bt_ctf_field_type
*returned_type
;
1895 const char *ret_string
;
1897 returned_type
= bt_ctf_field_get_type(NULL
);
1898 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1900 ok(uint_12_type
, "Create an unsigned integer type");
1901 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1902 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1903 "Set integer type's base as binary");
1904 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1905 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1906 "Set integer type's base as decimal");
1907 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1908 BT_CTF_INTEGER_BASE_UNKNOWN
),
1909 "Reject integer type's base set as unknown");
1910 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1911 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1912 "Set integer type's base as octal");
1913 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1914 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1915 "Set integer type's base as hexadecimal");
1916 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1917 "Reject unknown integer base value");
1918 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1919 "Set integer type signedness to signed");
1920 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1921 "Set integer type signedness to unsigned");
1922 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1923 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1924 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1925 "bt_ctf_field_type_integer_get_size returns a correct value");
1926 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1927 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1928 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1929 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1931 ok(bt_ctf_field_type_set_byte_order(NULL
,
1932 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1933 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1934 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1935 (enum bt_ctf_byte_order
) 42) < 0,
1936 "bt_ctf_field_type_set_byte_order rejects invalid values");
1937 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1938 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1939 "Set an integer's byte order to little endian");
1940 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1941 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1942 "Set an integer's byte order to big endian");
1943 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1944 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1945 "bt_ctf_field_type_get_byte_order returns a correct value");
1946 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1947 BT_CTF_BYTE_ORDER_UNKNOWN
,
1948 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1950 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1952 "bt_ctf_field_type_get_type_id handles NULL correctly");
1953 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1955 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1957 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1958 BT_CTF_INTEGER_BASE_UNKNOWN
,
1959 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1960 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1961 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1962 "bt_ctf_field_type_integer_get_base returns a correct value");
1964 ok(bt_ctf_field_type_integer_set_encoding(NULL
, CTF_STRING_ASCII
) < 0,
1965 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1966 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1967 (enum ctf_string_encoding
) 123) < 0,
1968 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1969 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1970 CTF_STRING_UTF8
) == 0,
1971 "Set integer type encoding to UTF8");
1972 ok(bt_ctf_field_type_integer_get_encoding(NULL
) == CTF_STRING_UNKNOWN
,
1973 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1974 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) == CTF_STRING_UTF8
,
1975 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1977 int_16_type
= bt_ctf_field_type_integer_create(16);
1978 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1979 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1980 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1981 uint_8_type
= bt_ctf_field_type_integer_create(8);
1983 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1984 ok(sequence_type
, "Create a sequence of int16_t type");
1985 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1987 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1989 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1990 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1991 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1993 ok(!strcmp(ret_string
, "seq_len"),
1994 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1995 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1996 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1997 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1999 ok(returned_type
== int_16_type
,
2000 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
2001 bt_put(returned_type
);
2003 string_type
= bt_ctf_field_type_string_create();
2004 ok(string_type
, "Create a string type");
2005 ok(bt_ctf_field_type_string_set_encoding(string_type
,
2007 "Reject invalid \"None\" string encoding");
2008 ok(bt_ctf_field_type_string_set_encoding(string_type
,
2010 "Reject invalid string encoding");
2011 ok(bt_ctf_field_type_string_set_encoding(string_type
,
2012 CTF_STRING_ASCII
) == 0,
2013 "Set string encoding to ASCII");
2015 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
2017 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
2018 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
2020 "bt_ctf_field_type_string_get_encoding returns the correct value");
2022 structure_seq_type
= bt_ctf_field_type_structure_create();
2023 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
2025 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
2026 ok(structure_seq_type
, "Create a structure type");
2027 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
2028 uint_8_type
, "seq_len") == 0,
2029 "Add a uint8_t type to a structure");
2030 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
2031 sequence_type
, "a_sequence") == 0,
2032 "Add a sequence type to a structure");
2034 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
2035 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
2036 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
2037 "bt_ctf_field_type_structure_get_field_count returns a correct value");
2039 ok(bt_ctf_field_type_structure_get_field(NULL
,
2040 &ret_string
, &returned_type
, 1) < 0,
2041 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2042 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2043 NULL
, &returned_type
, 1) == 0,
2044 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2045 bt_put(returned_type
);
2046 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2047 &ret_string
, NULL
, 1) == 0,
2048 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2049 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2050 &ret_string
, &returned_type
, 10) < 0,
2051 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2052 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2053 &ret_string
, &returned_type
, 1) == 0,
2054 "bt_ctf_field_type_structure_get_field returns a field");
2055 ok(!strcmp(ret_string
, "a_sequence"),
2056 "bt_ctf_field_type_structure_get_field returns a correct field name");
2057 ok(returned_type
== sequence_type
,
2058 "bt_ctf_field_type_structure_get_field returns a correct field type");
2059 bt_put(returned_type
);
2061 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2062 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2063 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2064 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2065 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2066 structure_seq_type
, "a_sequence");
2067 ok(returned_type
== sequence_type
,
2068 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2069 bt_put(returned_type
);
2071 composite_structure_type
= bt_ctf_field_type_structure_create();
2072 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2073 string_type
, "a_string") == 0,
2074 "Add a string type to a structure");
2075 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2076 structure_seq_type
, "inner_structure") == 0,
2077 "Add a structure type to a structure");
2079 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2080 NULL
, "a_sequence") == NULL
,
2081 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2082 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2083 structure_seq_type
, NULL
) == NULL
,
2084 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2085 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2086 structure_seq_type
, "a_sequence");
2087 ok(returned_type
== sequence_type
,
2088 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2089 bt_put(returned_type
);
2091 int_16
= bt_ctf_field_create(int_16_type
);
2092 ok(int_16
, "Instanciate a signed 16-bit integer");
2093 uint_12
= bt_ctf_field_create(uint_12_type
);
2094 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2095 returned_type
= bt_ctf_field_get_type(int_16
);
2096 ok(returned_type
== int_16_type
,
2097 "bt_ctf_field_get_type returns the correct type");
2099 /* Can't modify types after instanciating them */
2100 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2101 BT_CTF_INTEGER_BASE_DECIMAL
),
2102 "Check an integer type' base can't be modified after instanciation");
2103 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2104 "Check an integer type's signedness can't be modified after instanciation");
2106 /* Check signed property is checked */
2107 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2108 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2109 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2110 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2112 /* Check overflows are properly tested for */
2113 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2114 "Check -32768 is allowed for a signed 16-bit integer");
2115 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2116 "Check 32767 is allowed for a signed 16-bit integer");
2117 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2118 "Check 32768 is not allowed for a signed 16-bit integer");
2119 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2120 "Check -32769 is not allowed for a signed 16-bit integer");
2121 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2122 "Check -42 is allowed for a signed 16-bit integer");
2124 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2125 "Check 4095 is allowed for an unsigned 12-bit integer");
2126 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2127 "Check 4096 is not allowed for a unsigned 12-bit integer");
2128 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2129 "Check 0 is allowed for an unsigned 12-bit integer");
2131 string
= bt_ctf_field_create(string_type
);
2132 ok(string
, "Instanciate a string field");
2133 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2134 "Set a string's value");
2136 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2137 ok(enumeration_type
,
2138 "Create an enumeration type with an unsigned 12-bit integer as container");
2139 enumeration
= bt_ctf_field_create(enumeration_type
);
2141 "Check enumeration types are validated before instantiation");
2146 bt_put(enumeration
);
2147 bt_put(composite_structure_type
);
2148 bt_put(structure_seq_type
);
2149 bt_put(string_type
);
2150 bt_put(sequence_type
);
2151 bt_put(uint_8_type
);
2152 bt_put(int_16_type
);
2153 bt_put(uint_12_type
);
2154 bt_put(enumeration_type
);
2155 bt_put(returned_type
);
2158 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2159 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2162 * Append enough events to force the underlying packet to be resized.
2163 * Also tests that a new event can be declared after a stream has been
2164 * instantiated and used/flushed.
2168 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2170 struct bt_ctf_field_type
*integer_type
=
2171 bt_ctf_field_type_integer_create(17);
2172 struct bt_ctf_field_type
*string_type
=
2173 bt_ctf_field_type_string_create();
2174 struct bt_ctf_event
*event
= NULL
;
2175 struct bt_ctf_field
*ret_field
= NULL
;
2176 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2177 uint64_t ret_uint64
;
2178 int events_appended
= 0;
2179 struct bt_ctf_field
*packet_context
= NULL
,
2180 *packet_context_field
= NULL
, *event_context
= NULL
;
2181 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2182 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2183 struct bt_ctf_field_type
*ep_type
= NULL
;
2185 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2187 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2189 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2190 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2196 * bt_ctf_stream_class_add_event_class() copies the field types
2197 * of event_class, so we retrieve the new ones to create the
2198 * appropriate fields.
2200 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2202 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2203 ep_type
, "field_1");
2204 assert(ep_field_1_type
);
2205 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2206 ep_type
, "a_string");
2207 assert(ep_a_string_type
);
2209 event
= bt_ctf_event_create(event_class
);
2210 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2211 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2212 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2213 "bt_ctf_event_get_payload_by_index returns a correct field");
2214 bt_put(ret_field_type
);
2217 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2218 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2219 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2220 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2223 ok(bt_ctf_stream_get_event_context(NULL
) == NULL
,
2224 "bt_ctf_stream_get_event_context handles NULL correctly");
2225 event_context
= bt_ctf_stream_get_event_context(stream
);
2227 "bt_ctf_stream_get_event_context returns a stream event context");
2228 ok(bt_ctf_stream_set_event_context(NULL
, event_context
) < 0,
2229 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
2230 ok(bt_ctf_stream_set_event_context(stream
, NULL
) < 0,
2231 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
2232 ok(!bt_ctf_stream_set_event_context(stream
, event_context
),
2233 "bt_ctf_stream_set_event_context correctly set a stream event context");
2234 ret_field
= bt_ctf_field_create(ep_field_1_type
);
2235 ok(bt_ctf_stream_set_event_context(stream
, ret_field
) < 0,
2236 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
2239 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2240 event
= bt_ctf_event_create(event_class
);
2241 struct bt_ctf_field
*integer
=
2242 bt_ctf_field_create(ep_field_1_type
);
2243 struct bt_ctf_field
*string
=
2244 bt_ctf_field_create(ep_a_string_type
);
2246 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2247 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2248 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2251 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2252 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2256 /* Populate stream event context */
2257 integer
= bt_ctf_field_structure_get_field(event_context
,
2258 "common_event_context");
2259 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2263 ret
|= bt_ctf_stream_append_event(stream
, event
);
2271 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2272 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2273 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2274 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2275 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2276 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2277 ok(ret
== 0 && ret_uint64
== 0,
2278 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2279 bt_ctf_stream_append_discarded_events(stream
, 1000);
2280 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2281 ok(ret
== 0 && ret_uint64
== 1000,
2282 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2285 ok(events_appended
, "Append 100 000 events to a stream");
2288 * Populate the custom packet context field with a dummy value
2289 * otherwise flush will fail.
2291 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2292 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2293 "custom_packet_context_field");
2294 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2296 ok(bt_ctf_stream_flush(stream
) == 0,
2297 "Flush a stream that forces a packet resize");
2298 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2299 ok(ret
== 0 && ret_uint64
== 1000,
2300 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2301 bt_put(integer_type
);
2302 bt_put(string_type
);
2303 bt_put(packet_context
);
2304 bt_put(packet_context_field
);
2305 bt_put(event_context
);
2306 bt_put(event_class
);
2307 bt_put(ep_field_1_type
);
2308 bt_put(ep_a_string_type
);
2312 void test_empty_stream(struct bt_ctf_writer
*writer
)
2315 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2316 struct bt_ctf_stream_class
*stream_class
= NULL
;
2317 struct bt_ctf_stream
*stream
= NULL
;
2319 trace
= bt_ctf_writer_get_trace(writer
);
2321 diag("Failed to get trace from writer");
2326 stream_class
= bt_ctf_stream_class_create("empty_stream");
2327 if (!stream_class
) {
2328 diag("Failed to create stream class");
2333 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2334 "bt_ctf_stream_class_get_trace handles NULL correctly");
2335 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2336 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2338 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2340 diag("Failed to create writer stream");
2345 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2346 ok(ret_trace
== trace
,
2347 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2350 "Created a stream class with default attributes and an empty stream");
2354 bt_put(stream_class
);
2357 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2360 struct bt_ctf_trace
*trace
= NULL
;
2361 struct bt_ctf_clock
*clock
= NULL
;
2362 struct bt_ctf_stream_class
*stream_class
= NULL
;
2363 struct bt_ctf_stream
*stream
= NULL
;
2364 struct bt_ctf_field_type
*integer_type
= NULL
,
2365 *sequence_type
= NULL
, *event_header_type
= NULL
;
2366 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2367 *event_header
= NULL
, *packet_header
= NULL
;
2368 struct bt_ctf_event_class
*event_class
= NULL
;
2369 struct bt_ctf_event
*event
= NULL
;
2371 trace
= bt_ctf_writer_get_trace(writer
);
2373 fail("Failed to get trace from writer");
2377 clock
= bt_ctf_trace_get_clock(trace
, 0);
2379 fail("Failed to get clock from trace");
2383 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2384 if (!stream_class
) {
2385 fail("Failed to create stream class");
2389 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2391 fail("Failed to set stream class clock");
2396 * Customize event header to add an "seq_len" integer member
2397 * which will be used as the length of a sequence in an event of this
2400 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2402 if (!event_header_type
) {
2403 fail("Failed to get event header type");
2407 integer_type
= bt_ctf_field_type_integer_create(13);
2408 if (!integer_type
) {
2409 fail("Failed to create length integer type");
2413 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2414 integer_type
, "seq_len");
2416 fail("Failed to add a new field to stream event header");
2420 event_class
= bt_ctf_event_class_create("sequence_event");
2422 fail("Failed to create event class");
2427 * This event's payload will contain a sequence which references
2428 * stream.event.header.seq_len as its length field.
2430 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2431 "stream.event.header.seq_len");
2432 if (!sequence_type
) {
2433 fail("Failed to create a sequence");
2437 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2440 fail("Failed to add a sequence to an event class");
2444 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2446 fail("Failed to add event class to stream class");
2450 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2452 fail("Failed to create stream")
2457 * We have defined a custom packet header field. We have to populate it
2460 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2461 if (!packet_header
) {
2462 fail("Failed to get stream packet header");
2466 integer
= bt_ctf_field_structure_get_field(packet_header
,
2467 "custom_trace_packet_header_field");
2469 fail("Failed to retrieve custom_trace_packet_header_field");
2473 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2475 fail("Failed to set custom_trace_packet_header_field value");
2480 event
= bt_ctf_event_create(event_class
);
2482 fail("Failed to create event");
2486 event_header
= bt_ctf_event_get_header(event
);
2487 if (!event_header
) {
2488 fail("Failed to get event header");
2492 integer
= bt_ctf_field_structure_get_field(event_header
,
2495 fail("Failed to get seq_len field from event header");
2499 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2501 fail("Failed to set seq_len value in event header");
2505 /* Populate both sequence integer fields */
2506 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2508 fail("Failed to retrieve sequence from event");
2512 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2514 fail("Failed to set sequence length");
2519 for (i
= 0; i
< 2; i
++) {
2520 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2522 fail("Failed to retrieve sequence element");
2526 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2528 fail("Failed to set sequence element value");
2536 ret
= bt_ctf_stream_append_event(stream
, event
);
2538 fail("Failed to append event to stream");
2542 ret
= bt_ctf_stream_flush(stream
);
2544 fail("Failed to flush custom_event_header stream");
2550 bt_put(stream_class
);
2551 bt_put(event_class
);
2555 bt_put(event_header
);
2556 bt_put(packet_header
);
2557 bt_put(sequence_type
);
2558 bt_put(integer_type
);
2559 bt_put(event_header_type
);
2562 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2565 struct bt_ctf_trace
*trace
= NULL
;
2566 struct bt_ctf_clock
*clock
= NULL
;
2567 struct bt_ctf_stream_class
*stream_class
= NULL
;
2568 struct bt_ctf_stream
*stream
= NULL
,
2570 struct bt_ctf_event_class
*event_class
= NULL
;
2571 struct bt_ctf_event
*event
= NULL
;
2572 struct bt_ctf_field_type
*integer_type
= NULL
;
2573 struct bt_ctf_field
*integer
= NULL
;
2575 trace
= bt_ctf_writer_get_trace(writer
);
2577 diag("Failed to get trace from writer");
2582 clock
= bt_ctf_trace_get_clock(trace
, 0);
2584 diag("Failed to get clock from trace");
2589 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2590 if (!stream_class
) {
2591 diag("Failed to create stream class");
2596 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2598 diag("Failed to set stream class clock");
2602 event_class
= bt_ctf_event_class_create("some_event_class_name");
2603 integer_type
= bt_ctf_field_type_integer_create(32);
2604 if (!integer_type
) {
2605 diag("Failed to create integer field type");
2610 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2613 diag("Failed to add field to event class");
2617 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2620 diag("Failed to add event class to stream class");
2623 event
= bt_ctf_event_create(event_class
);
2625 diag("Failed to create event");
2630 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2632 diag("Failed to get integer field payload from event");
2637 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2639 diag("Failed to set integer field value");
2643 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2645 diag("Failed to create writer stream");
2650 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2651 "bt_ctf_event_get_stream handles NULL correctly");
2652 ok(bt_ctf_event_get_stream(event
) == NULL
,
2653 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2655 ret
= bt_ctf_stream_append_event(stream
, event
);
2657 diag("Failed to append event to stream");
2661 ret_stream
= bt_ctf_event_get_stream(event
);
2662 ok(ret_stream
== stream
,
2663 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2666 "Create an event before instanciating its associated stream");
2670 bt_put(stream_class
);
2671 bt_put(event_class
);
2673 bt_put(integer_type
);
2678 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2680 struct bt_ctf_event_class
*event_class
;
2682 event_class
= bt_ctf_event_class_create("Simple Event");
2683 assert(event_class
);
2684 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2685 "two event classes with the same name cannot cohabit within the same stream class");
2686 bt_put(event_class
);
2688 event_class
= bt_ctf_event_class_create("different name, ok");
2689 assert(event_class
);
2690 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2691 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2692 "two event classes with the same ID cannot cohabit within the same stream class");
2693 bt_put(event_class
);
2696 int main(int argc
, char **argv
)
2698 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2699 char metadata_path
[sizeof(trace_path
) + 9];
2700 const char *clock_name
= "test_clock";
2701 const char *clock_description
= "This is a test clock";
2702 const char *returned_clock_name
;
2703 const char *returned_clock_description
;
2704 const uint64_t frequency
= 1123456789;
2705 const int64_t offset_s
= 1351530929945824323;
2706 const int64_t offset
= 1234567;
2707 int64_t get_offset_s
,
2710 const uint64_t precision
= 10;
2711 const int is_absolute
= 0xFF;
2712 char *metadata_string
;
2713 struct bt_ctf_writer
*writer
;
2714 struct utsname name
;
2715 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2716 struct bt_ctf_clock
*clock
, *ret_clock
;
2717 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2718 struct bt_ctf_stream
*stream1
;
2719 const char *ret_string
;
2720 const unsigned char *ret_uuid
;
2721 unsigned char tmp_uuid
[16] = { 0 };
2722 struct bt_ctf_field_type
*packet_context_type
,
2723 *packet_context_field_type
,
2724 *packet_header_type
,
2725 *packet_header_field_type
,
2727 *stream_event_context_type
,
2729 *event_header_field_type
;
2730 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2731 struct bt_ctf_trace
*trace
;
2733 int64_t ret_int64_t
;
2734 struct bt_value
*obj
;
2737 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2741 plan_tests(NR_TESTS
);
2743 if (!bt_mkdtemp(trace_path
)) {
2747 strcpy(metadata_path
, trace_path
);
2748 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2750 writer
= bt_ctf_writer_create(trace_path
);
2751 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2753 ok(!bt_ctf_writer_get_trace(NULL
),
2754 "bt_ctf_writer_get_trace correctly handles NULL");
2755 trace
= bt_ctf_writer_get_trace(writer
);
2757 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2758 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2759 "Set a trace's byte order to big endian");
2760 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2761 "bt_ctf_trace_get_byte_order returns a correct endianness");
2763 /* Add environment context to the trace */
2764 ret
= gethostname(hostname
, sizeof(hostname
));
2768 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2769 "Add host (%s) environment field to writer instance",
2771 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2773 "bt_ctf_writer_add_environment_field error with NULL writer");
2774 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2776 "bt_ctf_writer_add_environment_field error with NULL field name");
2777 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2779 "bt_ctf_writer_add_environment_field error with NULL field value");
2781 /* Test bt_ctf_trace_set_environment_field with an integer object */
2782 obj
= bt_value_integer_create_init(23);
2784 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2785 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2786 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2787 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2788 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2789 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2790 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2791 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2794 /* Test bt_ctf_trace_set_environment_field with a string object */
2795 obj
= bt_value_string_create_init("the value");
2797 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2798 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2801 /* Test bt_ctf_trace_set_environment_field_integer */
2802 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2804 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2805 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2806 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2807 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2809 "bt_ctf_trace_set_environment_field_integer succeeds");
2811 /* Test bt_ctf_trace_set_environment_field_string */
2812 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2814 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2815 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2816 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2817 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2819 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2820 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2822 "bt_ctf_trace_set_environment_field_string succeeds");
2824 /* Test bt_ctf_trace_get_environment_field_count */
2825 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2826 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2827 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2828 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2830 /* Test bt_ctf_trace_get_environment_field_name */
2831 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
2832 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2833 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
2834 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2835 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
2836 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2837 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
2838 ok(ret_string
&& !strcmp(ret_string
, "host"),
2839 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2840 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
2841 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2842 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2843 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
2844 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2845 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2846 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
2847 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2848 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2849 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
2850 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2851 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2853 /* Test bt_ctf_trace_get_environment_field_value */
2854 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
2855 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2856 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
2857 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2858 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
2859 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2860 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
2861 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2862 ok(!ret
&& ret_int64_t
== 23,
2863 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2865 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
2866 ret
= bt_value_string_get(obj
, &ret_string
);
2867 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2868 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2871 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2872 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2874 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2875 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
2876 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2877 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
2878 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2879 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
2881 ret
= bt_value_string_get(obj
, &ret_string
);
2882 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
2883 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2886 /* Test environment field replacement */
2887 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2889 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2890 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2891 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2892 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
2893 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2894 ok(!ret
&& ret_int64_t
== 654321,
2895 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2898 /* On Solaris, uname() can return any positive value on success */
2899 if (uname(&name
) < 0) {
2904 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
2905 == 0, "Add sysname (%s) environment field to writer instance",
2907 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
2908 name
.nodename
) == 0,
2909 "Add nodename (%s) environment field to writer instance",
2911 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
2912 == 0, "Add release (%s) environment field to writer instance",
2914 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
2915 == 0, "Add version (%s) environment field to writer instance",
2917 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
2918 == 0, "Add machine (%s) environment field to writer istance",
2921 /* Define a clock and add it to the trace */
2922 ok(bt_ctf_clock_create("signed") == NULL
,
2923 "Illegal clock name rejected");
2924 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
2925 clock
= bt_ctf_clock_create(clock_name
);
2926 ok(clock
, "Clock created sucessfully");
2927 returned_clock_name
= bt_ctf_clock_get_name(clock
);
2928 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
2929 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
2930 "Returned clock name is valid");
2932 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2933 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
2934 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
2935 "Clock description set successfully");
2937 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2938 ok(returned_clock_description
,
2939 "bt_ctf_clock_get_description returns a description.");
2940 ok(returned_clock_description
?
2941 !strcmp(returned_clock_description
, clock_description
) : 0,
2942 "Returned clock description is valid");
2944 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
2945 "bt_ctf_clock_get_frequency returns the correct default frequency");
2946 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
2947 "Set clock frequency");
2948 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
2949 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
2951 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
2952 "bt_ctf_clock_get_offset_s succeeds");
2953 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
2954 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
2955 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
2956 "Set clock offset (seconds)");
2957 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
2958 "bt_ctf_clock_get_offset_s succeeds");
2959 ok(get_offset_s
== offset_s
,
2960 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
2962 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
2963 "bt_ctf_clock_get_offset succeeds");
2964 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
2965 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
2966 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
2967 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
2968 "bt_ctf_clock_get_offset succeeds");
2969 ok(get_offset
== offset
,
2970 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
2972 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
2973 "bt_ctf_clock_get_precision returns the correct default precision");
2974 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
2975 "Set clock precision");
2976 ok(bt_ctf_clock_get_precision(clock
) == precision
,
2977 "bt_ctf_clock_get_precision returns the correct precision once it is set");
2979 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
2980 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
2981 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
2982 "Set clock absolute property");
2983 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
2984 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
2986 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
2987 "bt_ctf_clock_get_time succeeds");
2988 ok(get_time
== DEFAULT_CLOCK_TIME
,
2989 "bt_ctf_clock_get_time returns the correct default time");
2990 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
2992 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
2993 "bt_ctf_clock_get_time succeeds");
2994 ok(get_time
== current_time
,
2995 "bt_ctf_clock_get_time returns the correct time once it is set");
2997 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
2998 "Add clock to writer instance");
2999 ok(bt_ctf_writer_add_clock(writer
, clock
),
3000 "Verify a clock can't be added twice to a writer instance");
3002 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
3003 "bt_ctf_trace_get_clock_count correctly handles NULL");
3004 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
3005 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3006 ok(!bt_ctf_trace_get_clock(NULL
, 0),
3007 "bt_ctf_trace_get_clock correctly handles NULL");
3008 ok(!bt_ctf_trace_get_clock(trace
, -1),
3009 "bt_ctf_trace_get_clock correctly handles negative indexes");
3010 ok(!bt_ctf_trace_get_clock(trace
, 1),
3011 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3012 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
3013 ok(ret_clock
== clock
,
3014 "bt_ctf_trace_get_clock returns the right clock instance");
3016 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
3017 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3018 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
3019 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3020 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
3021 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3022 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
3023 ok(ret_clock
== clock
,
3024 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3026 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
3027 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3029 ok(!bt_ctf_clock_get_name(NULL
),
3030 "bt_ctf_clock_get_name correctly handles NULL");
3031 ok(!bt_ctf_clock_get_description(NULL
),
3032 "bt_ctf_clock_get_description correctly handles NULL");
3033 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3034 "bt_ctf_clock_get_frequency correctly handles NULL");
3035 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3036 "bt_ctf_clock_get_precision correctly handles NULL");
3037 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3038 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3039 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3040 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3041 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3042 "bt_ctf_clock_get_offset correctly handles NULL clock");
3043 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3044 "bt_ctf_clock_get_offset correctly handles NULL output");
3045 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3046 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3047 ok(bt_ctf_clock_get_time(NULL
, &get_time
) < 0,
3048 "bt_ctf_clock_get_time correctly handles NULL clock");
3049 ok(bt_ctf_clock_get_time(clock
, NULL
) < 0,
3050 "bt_ctf_clock_get_time correctly handles NULL output");
3052 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3053 "bt_ctf_clock_set_description correctly handles NULL clock");
3054 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3055 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3056 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3057 "bt_ctf_clock_get_precision correctly handles NULL clock");
3058 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3059 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3060 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3061 "bt_ctf_clock_set_offset correctly handles NULL clock");
3062 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3063 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3064 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3065 "bt_ctf_clock_set_time correctly handles NULL clock");
3066 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3067 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3068 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3070 "bt_ctf_clock_get_uuid returns a UUID");
3072 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3073 /* Slightly modify UUID */
3074 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3077 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3078 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3079 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3080 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3081 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3082 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3083 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3085 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3086 ok(uuid_match(ret_uuid
, tmp_uuid
),
3087 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3089 /* Define a stream class */
3090 stream_class
= bt_ctf_stream_class_create("test_stream");
3092 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3093 "bt_ctf_stream_class_get_name handles NULL correctly");
3094 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3095 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3096 "bt_ctf_stream_class_get_name returns a correct stream class name");
3098 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3099 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3100 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3101 "bt_ctf_stream_class_get_clock handles NULL correctly");
3103 ok(stream_class
, "Create stream class");
3104 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3105 "Set a stream class' clock");
3106 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3107 ok(ret_clock
== clock
,
3108 "bt_ctf_stream_class_get_clock returns a correct clock");
3111 /* Test the event fields and event types APIs */
3114 /* Test fields copying */
3117 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3118 "bt_ctf_stream_class_get_id returns an error when no id is set");
3119 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3120 "bt_ctf_stream_class_get_id handles NULL correctly");
3121 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3122 "bt_ctf_stream_class_set_id handles NULL correctly");
3123 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3124 "Set an stream class' id");
3125 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3126 "bt_ctf_stream_class_get_id returns the correct value");
3128 /* Validate default event header fields */
3129 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3130 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3131 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3134 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3135 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == CTF_TYPE_STRUCT
,
3136 "Default event header type is a structure");
3137 event_header_field_type
=
3138 bt_ctf_field_type_structure_get_field_type_by_name(
3139 ret_field_type
, "id");
3140 ok(event_header_field_type
,
3141 "Default event header type contains an \"id\" field");
3142 ok(bt_ctf_field_type_get_type_id(
3143 event_header_field_type
) == CTF_TYPE_INTEGER
,
3144 "Default event header \"id\" field is an integer");
3145 bt_put(event_header_field_type
);
3146 event_header_field_type
=
3147 bt_ctf_field_type_structure_get_field_type_by_name(
3148 ret_field_type
, "timestamp");
3149 ok(event_header_field_type
,
3150 "Default event header type contains a \"timestamp\" field");
3151 ok(bt_ctf_field_type_get_type_id(
3152 event_header_field_type
) == CTF_TYPE_INTEGER
,
3153 "Default event header \"timestamp\" field is an integer");
3154 bt_put(event_header_field_type
);
3155 bt_put(ret_field_type
);
3157 /* Add a custom trace packet header field */
3158 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3159 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3160 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3161 ok(packet_header_type
,
3162 "bt_ctf_trace_get_packet_header_type returns a packet header");
3163 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == CTF_TYPE_STRUCT
,
3164 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3165 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3166 packet_header_type
, "magic");
3167 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3168 bt_put(ret_field_type
);
3169 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3170 packet_header_type
, "uuid");
3171 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3172 bt_put(ret_field_type
);
3173 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3174 packet_header_type
, "stream_id");
3175 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3176 bt_put(ret_field_type
);
3178 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3179 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3180 packet_header_field_type
, "custom_trace_packet_header_field"),
3181 "Added a custom trace packet header field successfully");
3183 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3184 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3185 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3186 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3187 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3188 "Set a trace packet_header_type successfully");
3190 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3191 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3193 /* Add a custom field to the stream class' packet context */
3194 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3195 ok(packet_context_type
,
3196 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3197 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == CTF_TYPE_STRUCT
,
3198 "Packet context is a structure");
3200 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3201 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3202 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3203 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3205 integer_type
= bt_ctf_field_type_integer_create(32);
3206 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3208 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3209 /* Create a "uint5_t" equivalent custom packet context field */
3210 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3212 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3213 packet_context_field_type
, "custom_packet_context_field");
3214 ok(ret
== 0, "Packet context field added successfully");
3216 /* Define a stream event context containing a my_integer field. */
3217 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3218 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3219 ok(bt_ctf_stream_class_get_event_context_type(
3220 stream_class
) == NULL
,
3221 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3222 stream_event_context_type
= bt_ctf_field_type_structure_create();
3223 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3224 integer_type
, "common_event_context");
3226 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3227 stream_event_context_type
) < 0,
3228 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3229 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3231 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3232 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3234 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3236 ok(bt_ctf_stream_class_set_event_context_type(
3237 stream_class
, stream_event_context_type
) == 0,
3238 "Set a new stream event context type");
3239 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3241 ok(ret_field_type
== stream_event_context_type
,
3242 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3243 bt_put(ret_field_type
);
3245 /* Instantiate a stream and append events */
3246 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3247 ok(stream1
, "Instanciate a stream class from writer");
3249 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3250 "bt_ctf_stream_get_class correctly handles NULL");
3251 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3252 ok(ret_stream_class
,
3253 "bt_ctf_stream_get_class returns a stream class");
3254 ok(ret_stream_class
== stream_class
,
3255 "Returned stream class is of the correct type");
3258 * Packet header, packet context, event header, and stream
3259 * event context types were copied for the resolving
3262 BT_PUT(packet_header_type
);
3263 BT_PUT(packet_context_type
);
3264 BT_PUT(stream_event_context_type
);
3265 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3266 assert(packet_header_type
);
3267 packet_context_type
=
3268 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3269 assert(packet_context_type
);
3270 stream_event_context_type
=
3271 bt_ctf_stream_class_get_event_context_type(stream_class
);
3272 assert(stream_event_context_type
);
3275 * Try to modify the packet context type after a stream has been
3278 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3279 packet_header_field_type
, "should_fail");
3281 "Trace packet header type can't be modified once a stream has been instanciated");
3284 * Try to modify the packet context type after a stream has been
3287 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3288 packet_context_field_type
, "should_fail");
3290 "Packet context type can't be modified once a stream has been instanciated");
3293 * Try to modify the stream event context type after a stream has been
3296 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3297 integer_type
, "should_fail");
3299 "Stream event context type can't be modified once a stream has been instanciated");
3301 /* Should fail after instanciating a stream (frozen) */
3302 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3303 "Changes to a stream class that was already instantiated fail");
3305 /* Populate the custom packet header field only once for all tests */
3306 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3307 "bt_ctf_stream_get_packet_header handles NULL correctly");
3308 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3310 "bt_ctf_stream_get_packet_header returns a packet header");
3311 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3312 ok(ret_field_type
== packet_header_type
,
3313 "Stream returns a packet header of the appropriate type");
3314 bt_put(ret_field_type
);
3315 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3316 "custom_trace_packet_header_field");
3317 ok(packet_header_field
,
3318 "Packet header structure contains a custom field with the appropriate name");
3319 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3320 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3321 "Custom packet header field is of the expected type");
3322 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3323 54321), "Set custom packet header value successfully");
3324 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3325 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3326 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3327 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3328 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3329 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3330 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3331 "Successfully set a stream's packet header");
3333 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3334 "Add environment field to writer after stream creation");
3336 test_instanciate_event_before_stream(writer
);
3338 append_simple_event(stream_class
, stream1
, clock
);
3340 packet_resize_test(stream_class
, stream1
, clock
);
3342 append_complex_event(stream_class
, stream1
, clock
);
3344 append_existing_event_class(stream_class
);
3346 test_empty_stream(writer
);
3348 test_custom_event_header_stream(writer
);
3350 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3351 ok(metadata_string
, "Get metadata string");
3353 bt_ctf_writer_flush_metadata(writer
);
3354 validate_metadata(argv
[1], metadata_path
);
3355 validate_trace(argv
[2], trace_path
);
3358 bt_put(ret_stream_class
);
3361 bt_put(packet_context_type
);
3362 bt_put(packet_context_field_type
);
3363 bt_put(integer_type
);
3364 bt_put(stream_event_context_type
);
3365 bt_put(ret_field_type
);
3366 bt_put(packet_header_type
);
3367 bt_put(packet_header_field_type
);
3368 bt_put(packet_header
);
3369 bt_put(packet_header_field
);
3371 free(metadata_string
);
3372 bt_put(stream_class
);
3374 /* Remove all trace files and delete temporary trace directory */
3375 DIR *trace_dir
= opendir(trace_path
);
3377 perror("# opendir");
3381 struct dirent
*entry
;
3382 while ((entry
= readdir(trace_dir
))) {
3384 char filename
[PATH_MAX
];
3386 if (snprintf(filename
, sizeof(filename
), "%s/%s",
3387 trace_path
, entry
->d_name
) <= 0) {
3391 if (stat(entry
->d_name
, &st
)) {
3395 if (S_ISREG(st
.st_mode
)) {
3396 unlinkat(bt_dirfd(trace_dir
), entry
->d_name
, 0);
3401 closedir(trace_dir
);