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
) ==
1951 BT_CTF_TYPE_ID_UNKNOWN
,
1952 "bt_ctf_field_type_get_type_id handles NULL correctly");
1953 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1954 BT_CTF_TYPE_ID_INTEGER
,
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
,
1965 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1966 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1967 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1968 (enum bt_ctf_string_encoding
) 123) < 0,
1969 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1970 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1971 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1972 "Set integer type encoding to UTF8");
1973 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1974 BT_CTF_STRING_ENCODING_UNKNOWN
,
1975 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1976 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1977 BT_CTF_STRING_ENCODING_UTF8
,
1978 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1980 int_16_type
= bt_ctf_field_type_integer_create(16);
1981 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1982 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1983 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1984 uint_8_type
= bt_ctf_field_type_integer_create(8);
1986 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1987 ok(sequence_type
, "Create a sequence of int16_t type");
1988 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1989 BT_CTF_TYPE_ID_SEQUENCE
,
1990 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1992 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1993 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1994 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1996 ok(!strcmp(ret_string
, "seq_len"),
1997 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1998 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1999 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
2000 returned_type
= bt_ctf_field_type_sequence_get_element_type(
2002 ok(returned_type
== int_16_type
,
2003 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
2004 bt_put(returned_type
);
2006 string_type
= bt_ctf_field_type_string_create();
2007 ok(string_type
, "Create a string type");
2008 ok(bt_ctf_field_type_string_set_encoding(string_type
,
2009 BT_CTF_STRING_ENCODING_NONE
),
2010 "Reject invalid \"None\" string encoding");
2011 ok(bt_ctf_field_type_string_set_encoding(string_type
,
2013 "Reject invalid string encoding");
2014 ok(bt_ctf_field_type_string_set_encoding(string_type
,
2015 BT_CTF_STRING_ENCODING_ASCII
) == 0,
2016 "Set string encoding to ASCII");
2018 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
2019 BT_CTF_STRING_ENCODING_UNKNOWN
,
2020 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
2021 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
2022 BT_CTF_STRING_ENCODING_ASCII
,
2023 "bt_ctf_field_type_string_get_encoding returns the correct value");
2025 structure_seq_type
= bt_ctf_field_type_structure_create();
2026 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
2027 BT_CTF_TYPE_ID_STRUCT
,
2028 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
2029 ok(structure_seq_type
, "Create a structure type");
2030 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
2031 uint_8_type
, "seq_len") == 0,
2032 "Add a uint8_t type to a structure");
2033 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
2034 sequence_type
, "a_sequence") == 0,
2035 "Add a sequence type to a structure");
2037 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
2038 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
2039 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
2040 "bt_ctf_field_type_structure_get_field_count returns a correct value");
2042 ok(bt_ctf_field_type_structure_get_field(NULL
,
2043 &ret_string
, &returned_type
, 1) < 0,
2044 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2045 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2046 NULL
, &returned_type
, 1) == 0,
2047 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2048 bt_put(returned_type
);
2049 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2050 &ret_string
, NULL
, 1) == 0,
2051 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2052 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2053 &ret_string
, &returned_type
, 10) < 0,
2054 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2055 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2056 &ret_string
, &returned_type
, 1) == 0,
2057 "bt_ctf_field_type_structure_get_field returns a field");
2058 ok(!strcmp(ret_string
, "a_sequence"),
2059 "bt_ctf_field_type_structure_get_field returns a correct field name");
2060 ok(returned_type
== sequence_type
,
2061 "bt_ctf_field_type_structure_get_field returns a correct field type");
2062 bt_put(returned_type
);
2064 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2065 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2066 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2067 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2068 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2069 structure_seq_type
, "a_sequence");
2070 ok(returned_type
== sequence_type
,
2071 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2072 bt_put(returned_type
);
2074 composite_structure_type
= bt_ctf_field_type_structure_create();
2075 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2076 string_type
, "a_string") == 0,
2077 "Add a string type to a structure");
2078 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2079 structure_seq_type
, "inner_structure") == 0,
2080 "Add a structure type to a structure");
2082 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2083 NULL
, "a_sequence") == NULL
,
2084 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2085 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2086 structure_seq_type
, NULL
) == NULL
,
2087 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2088 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2089 structure_seq_type
, "a_sequence");
2090 ok(returned_type
== sequence_type
,
2091 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2092 bt_put(returned_type
);
2094 int_16
= bt_ctf_field_create(int_16_type
);
2095 ok(int_16
, "Instanciate a signed 16-bit integer");
2096 uint_12
= bt_ctf_field_create(uint_12_type
);
2097 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2098 returned_type
= bt_ctf_field_get_type(int_16
);
2099 ok(returned_type
== int_16_type
,
2100 "bt_ctf_field_get_type returns the correct type");
2102 /* Can't modify types after instanciating them */
2103 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2104 BT_CTF_INTEGER_BASE_DECIMAL
),
2105 "Check an integer type' base can't be modified after instanciation");
2106 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2107 "Check an integer type's signedness can't be modified after instanciation");
2109 /* Check signed property is checked */
2110 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2111 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2112 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2113 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2115 /* Check overflows are properly tested for */
2116 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2117 "Check -32768 is allowed for a signed 16-bit integer");
2118 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2119 "Check 32767 is allowed for a signed 16-bit integer");
2120 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2121 "Check 32768 is not allowed for a signed 16-bit integer");
2122 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2123 "Check -32769 is not allowed for a signed 16-bit integer");
2124 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2125 "Check -42 is allowed for a signed 16-bit integer");
2127 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2128 "Check 4095 is allowed for an unsigned 12-bit integer");
2129 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2130 "Check 4096 is not allowed for a unsigned 12-bit integer");
2131 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2132 "Check 0 is allowed for an unsigned 12-bit integer");
2134 string
= bt_ctf_field_create(string_type
);
2135 ok(string
, "Instanciate a string field");
2136 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2137 "Set a string's value");
2139 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2140 ok(enumeration_type
,
2141 "Create an enumeration type with an unsigned 12-bit integer as container");
2142 enumeration
= bt_ctf_field_create(enumeration_type
);
2144 "Check enumeration types are validated before instantiation");
2149 bt_put(enumeration
);
2150 bt_put(composite_structure_type
);
2151 bt_put(structure_seq_type
);
2152 bt_put(string_type
);
2153 bt_put(sequence_type
);
2154 bt_put(uint_8_type
);
2155 bt_put(int_16_type
);
2156 bt_put(uint_12_type
);
2157 bt_put(enumeration_type
);
2158 bt_put(returned_type
);
2161 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2162 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2165 * Append enough events to force the underlying packet to be resized.
2166 * Also tests that a new event can be declared after a stream has been
2167 * instantiated and used/flushed.
2171 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2173 struct bt_ctf_field_type
*integer_type
=
2174 bt_ctf_field_type_integer_create(17);
2175 struct bt_ctf_field_type
*string_type
=
2176 bt_ctf_field_type_string_create();
2177 struct bt_ctf_event
*event
= NULL
;
2178 struct bt_ctf_field
*ret_field
= NULL
;
2179 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2180 uint64_t ret_uint64
;
2181 int events_appended
= 0;
2182 struct bt_ctf_field
*packet_context
= NULL
,
2183 *packet_context_field
= NULL
, *event_context
= NULL
;
2184 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2185 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2186 struct bt_ctf_field_type
*ep_type
= NULL
;
2188 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2190 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2192 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2193 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2199 * bt_ctf_stream_class_add_event_class() copies the field types
2200 * of event_class, so we retrieve the new ones to create the
2201 * appropriate fields.
2203 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2205 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2206 ep_type
, "field_1");
2207 assert(ep_field_1_type
);
2208 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2209 ep_type
, "a_string");
2210 assert(ep_a_string_type
);
2212 event
= bt_ctf_event_create(event_class
);
2213 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2214 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2215 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2216 "bt_ctf_event_get_payload_by_index returns a correct field");
2217 bt_put(ret_field_type
);
2220 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2221 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2222 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2223 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2226 ok(bt_ctf_stream_get_event_context(NULL
) == NULL
,
2227 "bt_ctf_stream_get_event_context handles NULL correctly");
2228 event_context
= bt_ctf_stream_get_event_context(stream
);
2230 "bt_ctf_stream_get_event_context returns a stream event context");
2231 ok(bt_ctf_stream_set_event_context(NULL
, event_context
) < 0,
2232 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
2233 ok(bt_ctf_stream_set_event_context(stream
, NULL
) < 0,
2234 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
2235 ok(!bt_ctf_stream_set_event_context(stream
, event_context
),
2236 "bt_ctf_stream_set_event_context correctly set a stream event context");
2237 ret_field
= bt_ctf_field_create(ep_field_1_type
);
2238 ok(bt_ctf_stream_set_event_context(stream
, ret_field
) < 0,
2239 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
2242 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2243 event
= bt_ctf_event_create(event_class
);
2244 struct bt_ctf_field
*integer
=
2245 bt_ctf_field_create(ep_field_1_type
);
2246 struct bt_ctf_field
*string
=
2247 bt_ctf_field_create(ep_a_string_type
);
2249 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2250 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2251 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2254 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2255 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2259 /* Populate stream event context */
2260 integer
= bt_ctf_field_structure_get_field(event_context
,
2261 "common_event_context");
2262 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2266 ret
|= bt_ctf_stream_append_event(stream
, event
);
2274 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2275 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2276 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2277 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2278 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2279 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2280 ok(ret
== 0 && ret_uint64
== 0,
2281 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2282 bt_ctf_stream_append_discarded_events(stream
, 1000);
2283 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2284 ok(ret
== 0 && ret_uint64
== 1000,
2285 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2288 ok(events_appended
, "Append 100 000 events to a stream");
2291 * Populate the custom packet context field with a dummy value
2292 * otherwise flush will fail.
2294 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2295 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2296 "custom_packet_context_field");
2297 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2299 ok(bt_ctf_stream_flush(stream
) == 0,
2300 "Flush a stream that forces a packet resize");
2301 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2302 ok(ret
== 0 && ret_uint64
== 1000,
2303 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2304 bt_put(integer_type
);
2305 bt_put(string_type
);
2306 bt_put(packet_context
);
2307 bt_put(packet_context_field
);
2308 bt_put(event_context
);
2309 bt_put(event_class
);
2310 bt_put(ep_field_1_type
);
2311 bt_put(ep_a_string_type
);
2315 void test_empty_stream(struct bt_ctf_writer
*writer
)
2318 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2319 struct bt_ctf_stream_class
*stream_class
= NULL
;
2320 struct bt_ctf_stream
*stream
= NULL
;
2322 trace
= bt_ctf_writer_get_trace(writer
);
2324 diag("Failed to get trace from writer");
2329 stream_class
= bt_ctf_stream_class_create("empty_stream");
2330 if (!stream_class
) {
2331 diag("Failed to create stream class");
2336 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2337 "bt_ctf_stream_class_get_trace handles NULL correctly");
2338 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2339 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2341 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2343 diag("Failed to create writer stream");
2348 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2349 ok(ret_trace
== trace
,
2350 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2353 "Created a stream class with default attributes and an empty stream");
2357 bt_put(stream_class
);
2360 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2363 struct bt_ctf_trace
*trace
= NULL
;
2364 struct bt_ctf_clock
*clock
= NULL
;
2365 struct bt_ctf_stream_class
*stream_class
= NULL
;
2366 struct bt_ctf_stream
*stream
= NULL
;
2367 struct bt_ctf_field_type
*integer_type
= NULL
,
2368 *sequence_type
= NULL
, *event_header_type
= NULL
;
2369 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2370 *event_header
= NULL
, *packet_header
= NULL
;
2371 struct bt_ctf_event_class
*event_class
= NULL
;
2372 struct bt_ctf_event
*event
= NULL
;
2374 trace
= bt_ctf_writer_get_trace(writer
);
2376 fail("Failed to get trace from writer");
2380 clock
= bt_ctf_trace_get_clock(trace
, 0);
2382 fail("Failed to get clock from trace");
2386 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2387 if (!stream_class
) {
2388 fail("Failed to create stream class");
2392 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2394 fail("Failed to set stream class clock");
2399 * Customize event header to add an "seq_len" integer member
2400 * which will be used as the length of a sequence in an event of this
2403 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2405 if (!event_header_type
) {
2406 fail("Failed to get event header type");
2410 integer_type
= bt_ctf_field_type_integer_create(13);
2411 if (!integer_type
) {
2412 fail("Failed to create length integer type");
2416 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2417 integer_type
, "seq_len");
2419 fail("Failed to add a new field to stream event header");
2423 event_class
= bt_ctf_event_class_create("sequence_event");
2425 fail("Failed to create event class");
2430 * This event's payload will contain a sequence which references
2431 * stream.event.header.seq_len as its length field.
2433 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2434 "stream.event.header.seq_len");
2435 if (!sequence_type
) {
2436 fail("Failed to create a sequence");
2440 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2443 fail("Failed to add a sequence to an event class");
2447 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2449 fail("Failed to add event class to stream class");
2453 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2455 fail("Failed to create stream")
2460 * We have defined a custom packet header field. We have to populate it
2463 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2464 if (!packet_header
) {
2465 fail("Failed to get stream packet header");
2469 integer
= bt_ctf_field_structure_get_field(packet_header
,
2470 "custom_trace_packet_header_field");
2472 fail("Failed to retrieve custom_trace_packet_header_field");
2476 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2478 fail("Failed to set custom_trace_packet_header_field value");
2483 event
= bt_ctf_event_create(event_class
);
2485 fail("Failed to create event");
2489 event_header
= bt_ctf_event_get_header(event
);
2490 if (!event_header
) {
2491 fail("Failed to get event header");
2495 integer
= bt_ctf_field_structure_get_field(event_header
,
2498 fail("Failed to get seq_len field from event header");
2502 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2504 fail("Failed to set seq_len value in event header");
2508 /* Populate both sequence integer fields */
2509 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2511 fail("Failed to retrieve sequence from event");
2515 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2517 fail("Failed to set sequence length");
2522 for (i
= 0; i
< 2; i
++) {
2523 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2525 fail("Failed to retrieve sequence element");
2529 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2531 fail("Failed to set sequence element value");
2539 ret
= bt_ctf_stream_append_event(stream
, event
);
2541 fail("Failed to append event to stream");
2545 ret
= bt_ctf_stream_flush(stream
);
2547 fail("Failed to flush custom_event_header stream");
2553 bt_put(stream_class
);
2554 bt_put(event_class
);
2558 bt_put(event_header
);
2559 bt_put(packet_header
);
2560 bt_put(sequence_type
);
2561 bt_put(integer_type
);
2562 bt_put(event_header_type
);
2565 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2568 struct bt_ctf_trace
*trace
= NULL
;
2569 struct bt_ctf_clock
*clock
= NULL
;
2570 struct bt_ctf_stream_class
*stream_class
= NULL
;
2571 struct bt_ctf_stream
*stream
= NULL
,
2573 struct bt_ctf_event_class
*event_class
= NULL
;
2574 struct bt_ctf_event
*event
= NULL
;
2575 struct bt_ctf_field_type
*integer_type
= NULL
;
2576 struct bt_ctf_field
*integer
= NULL
;
2578 trace
= bt_ctf_writer_get_trace(writer
);
2580 diag("Failed to get trace from writer");
2585 clock
= bt_ctf_trace_get_clock(trace
, 0);
2587 diag("Failed to get clock from trace");
2592 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2593 if (!stream_class
) {
2594 diag("Failed to create stream class");
2599 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2601 diag("Failed to set stream class clock");
2605 event_class
= bt_ctf_event_class_create("some_event_class_name");
2606 integer_type
= bt_ctf_field_type_integer_create(32);
2607 if (!integer_type
) {
2608 diag("Failed to create integer field type");
2613 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2616 diag("Failed to add field to event class");
2620 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2623 diag("Failed to add event class to stream class");
2626 event
= bt_ctf_event_create(event_class
);
2628 diag("Failed to create event");
2633 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2635 diag("Failed to get integer field payload from event");
2640 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2642 diag("Failed to set integer field value");
2646 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2648 diag("Failed to create writer stream");
2653 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2654 "bt_ctf_event_get_stream handles NULL correctly");
2655 ok(bt_ctf_event_get_stream(event
) == NULL
,
2656 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2658 ret
= bt_ctf_stream_append_event(stream
, event
);
2660 diag("Failed to append event to stream");
2664 ret_stream
= bt_ctf_event_get_stream(event
);
2665 ok(ret_stream
== stream
,
2666 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2669 "Create an event before instanciating its associated stream");
2673 bt_put(stream_class
);
2674 bt_put(event_class
);
2676 bt_put(integer_type
);
2681 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2683 struct bt_ctf_event_class
*event_class
;
2685 event_class
= bt_ctf_event_class_create("Simple Event");
2686 assert(event_class
);
2687 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2688 "two event classes with the same name cannot cohabit within the same stream class");
2689 bt_put(event_class
);
2691 event_class
= bt_ctf_event_class_create("different name, ok");
2692 assert(event_class
);
2693 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2694 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2695 "two event classes with the same ID cannot cohabit within the same stream class");
2696 bt_put(event_class
);
2699 int main(int argc
, char **argv
)
2701 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2702 char metadata_path
[sizeof(trace_path
) + 9];
2703 const char *clock_name
= "test_clock";
2704 const char *clock_description
= "This is a test clock";
2705 const char *returned_clock_name
;
2706 const char *returned_clock_description
;
2707 const uint64_t frequency
= 1123456789;
2708 const int64_t offset_s
= 1351530929945824323;
2709 const int64_t offset
= 1234567;
2710 int64_t get_offset_s
,
2713 const uint64_t precision
= 10;
2714 const int is_absolute
= 0xFF;
2715 char *metadata_string
;
2716 struct bt_ctf_writer
*writer
;
2717 struct utsname name
;
2718 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2719 struct bt_ctf_clock
*clock
, *ret_clock
;
2720 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2721 struct bt_ctf_stream
*stream1
;
2722 const char *ret_string
;
2723 const unsigned char *ret_uuid
;
2724 unsigned char tmp_uuid
[16] = { 0 };
2725 struct bt_ctf_field_type
*packet_context_type
,
2726 *packet_context_field_type
,
2727 *packet_header_type
,
2728 *packet_header_field_type
,
2730 *stream_event_context_type
,
2732 *event_header_field_type
;
2733 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2734 struct bt_ctf_trace
*trace
;
2736 int64_t ret_int64_t
;
2737 struct bt_value
*obj
;
2740 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2744 plan_tests(NR_TESTS
);
2746 if (!bt_mkdtemp(trace_path
)) {
2750 strcpy(metadata_path
, trace_path
);
2751 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2753 writer
= bt_ctf_writer_create(trace_path
);
2754 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2756 ok(!bt_ctf_writer_get_trace(NULL
),
2757 "bt_ctf_writer_get_trace correctly handles NULL");
2758 trace
= bt_ctf_writer_get_trace(writer
);
2760 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2761 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2762 "Set a trace's byte order to big endian");
2763 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2764 "bt_ctf_trace_get_byte_order returns a correct endianness");
2766 /* Add environment context to the trace */
2767 ret
= gethostname(hostname
, sizeof(hostname
));
2771 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2772 "Add host (%s) environment field to writer instance",
2774 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2776 "bt_ctf_writer_add_environment_field error with NULL writer");
2777 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2779 "bt_ctf_writer_add_environment_field error with NULL field name");
2780 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2782 "bt_ctf_writer_add_environment_field error with NULL field value");
2784 /* Test bt_ctf_trace_set_environment_field with an integer object */
2785 obj
= bt_value_integer_create_init(23);
2787 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2788 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2789 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2790 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2791 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2792 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2793 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2794 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2797 /* Test bt_ctf_trace_set_environment_field with a string object */
2798 obj
= bt_value_string_create_init("the value");
2800 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2801 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2804 /* Test bt_ctf_trace_set_environment_field_integer */
2805 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2807 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2808 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2809 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2810 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2812 "bt_ctf_trace_set_environment_field_integer succeeds");
2814 /* Test bt_ctf_trace_set_environment_field_string */
2815 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2817 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2818 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2819 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2820 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2822 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2823 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2825 "bt_ctf_trace_set_environment_field_string succeeds");
2827 /* Test bt_ctf_trace_get_environment_field_count */
2828 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2829 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2830 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2831 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2833 /* Test bt_ctf_trace_get_environment_field_name */
2834 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
2835 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2836 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
2837 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2838 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
2839 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2840 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
2841 ok(ret_string
&& !strcmp(ret_string
, "host"),
2842 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2843 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
2844 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_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
, 2);
2847 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2848 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2849 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
2850 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2851 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2852 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
2853 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2854 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2856 /* Test bt_ctf_trace_get_environment_field_value */
2857 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
2858 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2859 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
2860 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2861 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
2862 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2863 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
2864 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2865 ok(!ret
&& ret_int64_t
== 23,
2866 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2868 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
2869 ret
= bt_value_string_get(obj
, &ret_string
);
2870 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2871 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2874 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2875 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2877 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2878 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
2879 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2880 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
2881 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2882 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
2884 ret
= bt_value_string_get(obj
, &ret_string
);
2885 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
2886 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2889 /* Test environment field replacement */
2890 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2892 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2893 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2894 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2895 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
2896 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2897 ok(!ret
&& ret_int64_t
== 654321,
2898 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2901 /* On Solaris, uname() can return any positive value on success */
2902 if (uname(&name
) < 0) {
2907 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
2908 == 0, "Add sysname (%s) environment field to writer instance",
2910 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
2911 name
.nodename
) == 0,
2912 "Add nodename (%s) environment field to writer instance",
2914 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
2915 == 0, "Add release (%s) environment field to writer instance",
2917 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
2918 == 0, "Add version (%s) environment field to writer instance",
2920 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
2921 == 0, "Add machine (%s) environment field to writer istance",
2924 /* Define a clock and add it to the trace */
2925 ok(bt_ctf_clock_create("signed") == NULL
,
2926 "Illegal clock name rejected");
2927 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
2928 clock
= bt_ctf_clock_create(clock_name
);
2929 ok(clock
, "Clock created sucessfully");
2930 returned_clock_name
= bt_ctf_clock_get_name(clock
);
2931 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
2932 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
2933 "Returned clock name is valid");
2935 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2936 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
2937 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
2938 "Clock description set successfully");
2940 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2941 ok(returned_clock_description
,
2942 "bt_ctf_clock_get_description returns a description.");
2943 ok(returned_clock_description
?
2944 !strcmp(returned_clock_description
, clock_description
) : 0,
2945 "Returned clock description is valid");
2947 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
2948 "bt_ctf_clock_get_frequency returns the correct default frequency");
2949 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
2950 "Set clock frequency");
2951 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
2952 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
2954 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
2955 "bt_ctf_clock_get_offset_s succeeds");
2956 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
2957 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
2958 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
2959 "Set clock offset (seconds)");
2960 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
2961 "bt_ctf_clock_get_offset_s succeeds");
2962 ok(get_offset_s
== offset_s
,
2963 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
2965 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
2966 "bt_ctf_clock_get_offset succeeds");
2967 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
2968 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
2969 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
2970 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
2971 "bt_ctf_clock_get_offset succeeds");
2972 ok(get_offset
== offset
,
2973 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
2975 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
2976 "bt_ctf_clock_get_precision returns the correct default precision");
2977 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
2978 "Set clock precision");
2979 ok(bt_ctf_clock_get_precision(clock
) == precision
,
2980 "bt_ctf_clock_get_precision returns the correct precision once it is set");
2982 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
2983 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
2984 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
2985 "Set clock absolute property");
2986 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
2987 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
2989 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
2990 "bt_ctf_clock_get_time succeeds");
2991 ok(get_time
== DEFAULT_CLOCK_TIME
,
2992 "bt_ctf_clock_get_time returns the correct default time");
2993 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
2995 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
2996 "bt_ctf_clock_get_time succeeds");
2997 ok(get_time
== current_time
,
2998 "bt_ctf_clock_get_time returns the correct time once it is set");
3000 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
3001 "Add clock to writer instance");
3002 ok(bt_ctf_writer_add_clock(writer
, clock
),
3003 "Verify a clock can't be added twice to a writer instance");
3005 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
3006 "bt_ctf_trace_get_clock_count correctly handles NULL");
3007 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
3008 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3009 ok(!bt_ctf_trace_get_clock(NULL
, 0),
3010 "bt_ctf_trace_get_clock correctly handles NULL");
3011 ok(!bt_ctf_trace_get_clock(trace
, -1),
3012 "bt_ctf_trace_get_clock correctly handles negative indexes");
3013 ok(!bt_ctf_trace_get_clock(trace
, 1),
3014 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3015 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
3016 ok(ret_clock
== clock
,
3017 "bt_ctf_trace_get_clock returns the right clock instance");
3019 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
3020 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3021 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
3022 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3023 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
3024 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3025 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
3026 ok(ret_clock
== clock
,
3027 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3029 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
3030 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3032 ok(!bt_ctf_clock_get_name(NULL
),
3033 "bt_ctf_clock_get_name correctly handles NULL");
3034 ok(!bt_ctf_clock_get_description(NULL
),
3035 "bt_ctf_clock_get_description correctly handles NULL");
3036 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3037 "bt_ctf_clock_get_frequency correctly handles NULL");
3038 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3039 "bt_ctf_clock_get_precision correctly handles NULL");
3040 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3041 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3042 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3043 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3044 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3045 "bt_ctf_clock_get_offset correctly handles NULL clock");
3046 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3047 "bt_ctf_clock_get_offset correctly handles NULL output");
3048 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3049 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3050 ok(bt_ctf_clock_get_time(NULL
, &get_time
) < 0,
3051 "bt_ctf_clock_get_time correctly handles NULL clock");
3052 ok(bt_ctf_clock_get_time(clock
, NULL
) < 0,
3053 "bt_ctf_clock_get_time correctly handles NULL output");
3055 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3056 "bt_ctf_clock_set_description correctly handles NULL clock");
3057 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3058 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3059 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3060 "bt_ctf_clock_get_precision correctly handles NULL clock");
3061 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3062 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3063 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3064 "bt_ctf_clock_set_offset correctly handles NULL clock");
3065 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3066 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3067 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3068 "bt_ctf_clock_set_time correctly handles NULL clock");
3069 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3070 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3071 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3073 "bt_ctf_clock_get_uuid returns a UUID");
3075 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3076 /* Slightly modify UUID */
3077 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3080 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3081 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3082 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3083 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3084 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3085 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3086 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3088 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3089 ok(uuid_match(ret_uuid
, tmp_uuid
),
3090 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3092 /* Define a stream class */
3093 stream_class
= bt_ctf_stream_class_create("test_stream");
3095 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3096 "bt_ctf_stream_class_get_name handles NULL correctly");
3097 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3098 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3099 "bt_ctf_stream_class_get_name returns a correct stream class name");
3101 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3102 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3103 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3104 "bt_ctf_stream_class_get_clock handles NULL correctly");
3106 ok(stream_class
, "Create stream class");
3107 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3108 "Set a stream class' clock");
3109 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3110 ok(ret_clock
== clock
,
3111 "bt_ctf_stream_class_get_clock returns a correct clock");
3114 /* Test the event fields and event types APIs */
3117 /* Test fields copying */
3120 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3121 "bt_ctf_stream_class_get_id returns an error when no id is set");
3122 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3123 "bt_ctf_stream_class_get_id handles NULL correctly");
3124 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3125 "bt_ctf_stream_class_set_id handles NULL correctly");
3126 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3127 "Set an stream class' id");
3128 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3129 "bt_ctf_stream_class_get_id returns the correct value");
3131 /* Validate default event header fields */
3132 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3133 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3134 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3137 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3138 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_TYPE_ID_STRUCT
,
3139 "Default event header type is a structure");
3140 event_header_field_type
=
3141 bt_ctf_field_type_structure_get_field_type_by_name(
3142 ret_field_type
, "id");
3143 ok(event_header_field_type
,
3144 "Default event header type contains an \"id\" field");
3145 ok(bt_ctf_field_type_get_type_id(
3146 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3147 "Default event header \"id\" field is an integer");
3148 bt_put(event_header_field_type
);
3149 event_header_field_type
=
3150 bt_ctf_field_type_structure_get_field_type_by_name(
3151 ret_field_type
, "timestamp");
3152 ok(event_header_field_type
,
3153 "Default event header type contains a \"timestamp\" field");
3154 ok(bt_ctf_field_type_get_type_id(
3155 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3156 "Default event header \"timestamp\" field is an integer");
3157 bt_put(event_header_field_type
);
3158 bt_put(ret_field_type
);
3160 /* Add a custom trace packet header field */
3161 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3162 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3163 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3164 ok(packet_header_type
,
3165 "bt_ctf_trace_get_packet_header_type returns a packet header");
3166 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_TYPE_ID_STRUCT
,
3167 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3168 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3169 packet_header_type
, "magic");
3170 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3171 bt_put(ret_field_type
);
3172 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3173 packet_header_type
, "uuid");
3174 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3175 bt_put(ret_field_type
);
3176 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3177 packet_header_type
, "stream_id");
3178 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3179 bt_put(ret_field_type
);
3181 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3182 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3183 packet_header_field_type
, "custom_trace_packet_header_field"),
3184 "Added a custom trace packet header field successfully");
3186 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3187 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3188 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3189 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3190 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3191 "Set a trace packet_header_type successfully");
3193 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3194 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3196 /* Add a custom field to the stream class' packet context */
3197 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3198 ok(packet_context_type
,
3199 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3200 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_TYPE_ID_STRUCT
,
3201 "Packet context is a structure");
3203 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3204 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3205 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3206 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3208 integer_type
= bt_ctf_field_type_integer_create(32);
3209 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3211 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3212 /* Create a "uint5_t" equivalent custom packet context field */
3213 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3215 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3216 packet_context_field_type
, "custom_packet_context_field");
3217 ok(ret
== 0, "Packet context field added successfully");
3219 /* Define a stream event context containing a my_integer field. */
3220 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3221 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3222 ok(bt_ctf_stream_class_get_event_context_type(
3223 stream_class
) == NULL
,
3224 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3225 stream_event_context_type
= bt_ctf_field_type_structure_create();
3226 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3227 integer_type
, "common_event_context");
3229 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3230 stream_event_context_type
) < 0,
3231 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3232 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3234 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3235 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3237 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3239 ok(bt_ctf_stream_class_set_event_context_type(
3240 stream_class
, stream_event_context_type
) == 0,
3241 "Set a new stream event context type");
3242 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3244 ok(ret_field_type
== stream_event_context_type
,
3245 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3246 bt_put(ret_field_type
);
3248 /* Instantiate a stream and append events */
3249 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3250 ok(stream1
, "Instanciate a stream class from writer");
3252 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3253 "bt_ctf_stream_get_class correctly handles NULL");
3254 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3255 ok(ret_stream_class
,
3256 "bt_ctf_stream_get_class returns a stream class");
3257 ok(ret_stream_class
== stream_class
,
3258 "Returned stream class is of the correct type");
3261 * Packet header, packet context, event header, and stream
3262 * event context types were copied for the resolving
3265 BT_PUT(packet_header_type
);
3266 BT_PUT(packet_context_type
);
3267 BT_PUT(stream_event_context_type
);
3268 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3269 assert(packet_header_type
);
3270 packet_context_type
=
3271 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3272 assert(packet_context_type
);
3273 stream_event_context_type
=
3274 bt_ctf_stream_class_get_event_context_type(stream_class
);
3275 assert(stream_event_context_type
);
3278 * Try to modify the packet context type after a stream has been
3281 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3282 packet_header_field_type
, "should_fail");
3284 "Trace packet header type can't be modified once a stream has been instanciated");
3287 * Try to modify the packet context type after a stream has been
3290 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3291 packet_context_field_type
, "should_fail");
3293 "Packet context type can't be modified once a stream has been instanciated");
3296 * Try to modify the stream event context type after a stream has been
3299 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3300 integer_type
, "should_fail");
3302 "Stream event context type can't be modified once a stream has been instanciated");
3304 /* Should fail after instanciating a stream (frozen) */
3305 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3306 "Changes to a stream class that was already instantiated fail");
3308 /* Populate the custom packet header field only once for all tests */
3309 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3310 "bt_ctf_stream_get_packet_header handles NULL correctly");
3311 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3313 "bt_ctf_stream_get_packet_header returns a packet header");
3314 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3315 ok(ret_field_type
== packet_header_type
,
3316 "Stream returns a packet header of the appropriate type");
3317 bt_put(ret_field_type
);
3318 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3319 "custom_trace_packet_header_field");
3320 ok(packet_header_field
,
3321 "Packet header structure contains a custom field with the appropriate name");
3322 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3323 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3324 "Custom packet header field is of the expected type");
3325 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3326 54321), "Set custom packet header value successfully");
3327 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3328 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3329 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3330 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3331 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3332 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3333 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3334 "Successfully set a stream's packet header");
3336 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3337 "Add environment field to writer after stream creation");
3339 test_instanciate_event_before_stream(writer
);
3341 append_simple_event(stream_class
, stream1
, clock
);
3343 packet_resize_test(stream_class
, stream1
, clock
);
3345 append_complex_event(stream_class
, stream1
, clock
);
3347 append_existing_event_class(stream_class
);
3349 test_empty_stream(writer
);
3351 test_custom_event_header_stream(writer
);
3353 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3354 ok(metadata_string
, "Get metadata string");
3356 bt_ctf_writer_flush_metadata(writer
);
3357 validate_metadata(argv
[1], metadata_path
);
3358 validate_trace(argv
[2], trace_path
);
3361 bt_put(ret_stream_class
);
3364 bt_put(packet_context_type
);
3365 bt_put(packet_context_field_type
);
3366 bt_put(integer_type
);
3367 bt_put(stream_event_context_type
);
3368 bt_put(ret_field_type
);
3369 bt_put(packet_header_type
);
3370 bt_put(packet_header_field_type
);
3371 bt_put(packet_header
);
3372 bt_put(packet_header_field
);
3374 free(metadata_string
);
3375 bt_put(stream_class
);
3377 /* Remove all trace files and delete temporary trace directory */
3378 DIR *trace_dir
= opendir(trace_path
);
3380 perror("# opendir");
3384 struct dirent
*entry
;
3385 while ((entry
= readdir(trace_dir
))) {
3387 char filename
[PATH_MAX
];
3389 if (snprintf(filename
, sizeof(filename
), "%s/%s",
3390 trace_path
, entry
->d_name
) <= 0) {
3394 if (stat(entry
->d_name
, &st
)) {
3398 if (S_ISREG(st
.st_mode
)) {
3399 unlinkat(bt_dirfd(trace_dir
), entry
->d_name
, 0);
3404 closedir(trace_dir
);