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.
23 #include <babeltrace/ctf-writer/writer.h>
24 #include <babeltrace/ctf-writer/clock.h>
25 #include <babeltrace/ctf-writer/stream.h>
26 #include <babeltrace/ctf-writer/event.h>
27 #include <babeltrace/ctf-writer/event-types.h>
28 #include <babeltrace/ctf-writer/event-fields.h>
29 #include <babeltrace/ctf-ir/stream-class.h>
30 #include <babeltrace/ctf-ir/ref.h>
31 #include <babeltrace/ctf/events.h>
32 #include <babeltrace/values.h>
36 #include <sys/utsname.h>
37 #include <babeltrace/compat/limits.h>
47 #define METADATA_LINE_SIZE 512
48 #define SEQUENCE_TEST_LENGTH 10
49 #define ARRAY_TEST_LENGTH 5
50 #define PACKET_RESIZE_TEST_LENGTH 100000
52 #define DEFAULT_CLOCK_FREQ 1000000000
53 #define DEFAULT_CLOCK_PRECISION 1
54 #define DEFAULT_CLOCK_OFFSET 0
55 #define DEFAULT_CLOCK_OFFSET_S 0
56 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
57 #define DEFAULT_CLOCK_TIME 0
59 static uint64_t current_time
= 42;
61 /* Return 1 if uuids match, zero if different. */
62 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
67 if (!uuid_a
|| !uuid_b
) {
71 for (i
= 0; i
< 16; i
++) {
72 if (uuid_a
[i
] != uuid_b
[i
]) {
82 void validate_metadata(char *parser_path
, char *metadata_path
)
85 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
86 int parser_output_fd
= -1, metadata_fd
= -1;
93 parser_output_fd
= mkstemp(parser_output_path
);
94 metadata_fd
= open(metadata_path
, O_RDONLY
);
96 unlink(parser_output_path
);
98 if (parser_output_fd
== -1 || metadata_fd
== -1) {
99 diag("Failed create temporary files for metadata parsing.");
107 waitpid(pid
, &status
, 0);
108 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
110 /* ctf-parser-test expects a metadata string on stdin. */
111 ret
= dup2(metadata_fd
, STDIN_FILENO
);
113 perror("# dup2 metadata_fd to STDIN");
117 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
119 perror("# dup2 parser_output_fd to STDOUT");
123 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
125 perror("# dup2 parser_output_fd to STDERR");
129 execl(parser_path
, "ctf-parser-test", NULL
);
130 perror("# Could not launch the ctf metadata parser process");
134 ok(ret
== 0, "Metadata string is valid");
136 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
138 size_t len
= METADATA_LINE_SIZE
;
139 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
141 metadata_fp
= fdopen(metadata_fd
, "r");
143 perror("fdopen on metadata_fd");
148 parser_output_fp
= fdopen(parser_output_fd
, "r");
149 if (!parser_output_fp
) {
150 perror("fdopen on parser_output_fd");
153 parser_output_fd
= -1;
157 diag("malloc failure");
162 /* Output the metadata and parser output as diagnostic */
163 while (getline(&line
, &len
, metadata_fp
) > 0) {
164 fprintf(stderr
, "# %s", line
);
167 rewind(parser_output_fp
);
168 while (getline(&line
, &len
, parser_output_fp
) > 0) {
169 fprintf(stderr
, "# %s", line
);
175 if (fclose(metadata_fp
)) {
176 diag("fclose failure");
179 if (parser_output_fp
) {
180 if (fclose(parser_output_fp
)) {
181 diag("fclose failure");
186 if (parser_output_fd
>= 0) {
187 if (close(parser_output_fd
)) {
191 if (metadata_fd
>= 0) {
192 if (close(metadata_fd
)) {
198 void validate_trace(char *parser_path
, char *trace_path
)
201 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
202 int babeltrace_output_fd
= -1;
209 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
210 unlink(babeltrace_output_path
);
212 if (babeltrace_output_fd
== -1) {
213 diag("Failed to create a temporary file for trace parsing.");
221 waitpid(pid
, &status
, 0);
222 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
224 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
226 perror("# dup2 babeltrace_output_fd to STDOUT");
230 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
232 perror("# dup2 babeltrace_output_fd to STDERR");
236 execl(parser_path
, "babeltrace", trace_path
, NULL
);
237 perror("# Could not launch the babeltrace process");
241 ok(ret
== 0, "Babeltrace could read the resulting trace");
243 if (ret
&& babeltrace_output_fd
>= 0) {
245 size_t len
= METADATA_LINE_SIZE
;
246 FILE *babeltrace_output_fp
= NULL
;
248 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
249 if (!babeltrace_output_fp
) {
250 perror("fdopen on babeltrace_output_fd");
253 babeltrace_output_fd
= -1;
257 diag("malloc error");
259 rewind(babeltrace_output_fp
);
260 while (getline(&line
, &len
, babeltrace_output_fp
) > 0) {
266 if (babeltrace_output_fp
) {
267 if (fclose(babeltrace_output_fp
)) {
268 diag("fclose error");
273 if (babeltrace_output_fd
>= 0) {
274 if (close(babeltrace_output_fd
)) {
280 void event_copy_tests(struct bt_ctf_event
*event
)
282 struct bt_ctf_event
*copy
;
283 struct bt_ctf_event_class
*orig_event_class
;
284 struct bt_ctf_event_class
*copy_event_class
;
285 struct bt_ctf_stream
*orig_stream
;
286 struct bt_ctf_stream
*copy_stream
;
287 struct bt_ctf_field
*orig_field
;
288 struct bt_ctf_field
*copy_field
;
291 ok(!bt_ctf_event_copy(NULL
),
292 "bt_ctf_event_copy handles NULL correctly");
293 copy
= bt_ctf_event_copy(event
);
294 ok(copy
, "bt_ctf_event_copy returns a valid pointer");
296 /* validate event class */
297 orig_event_class
= bt_ctf_event_get_class(event
);
298 assert(orig_event_class
);
299 copy_event_class
= bt_ctf_event_get_class(copy
);
300 ok(orig_event_class
== copy_event_class
,
301 "original and copied events share the same event class pointer");
302 bt_ctf_put(orig_event_class
);
303 bt_ctf_put(copy_event_class
);
305 /* validate stream */
306 orig_stream
= bt_ctf_event_get_stream(event
);
307 copy_stream
= bt_ctf_event_get_stream(copy
);
310 ok(!copy_stream
, "original and copied events have no stream");
312 ok(orig_stream
== copy_stream
,
313 "original and copied events share the same stream pointer");
315 bt_ctf_put(orig_stream
);
316 bt_ctf_put(copy_stream
);
319 orig_field
= bt_ctf_event_get_header(event
);
320 copy_field
= bt_ctf_event_get_header(copy
);
323 ok(!copy_field
, "original and copied events have no header");
325 ok(orig_field
!= copy_field
,
326 "original and copied events headers are different pointers");
329 bt_ctf_put(orig_field
);
330 bt_ctf_put(copy_field
);
333 orig_field
= bt_ctf_event_get_event_context(event
);
334 copy_field
= bt_ctf_event_get_event_context(copy
);
337 ok(!copy_field
, "original and copied events have no context");
339 ok(orig_field
!= copy_field
,
340 "original and copied events contexts are different pointers");
343 bt_ctf_put(orig_field
);
344 bt_ctf_put(copy_field
);
347 orig_field
= bt_ctf_event_get_payload_field(event
);
348 copy_field
= bt_ctf_event_get_payload_field(copy
);
351 ok(!copy_field
, "original and copied events have no payload");
353 ok(orig_field
!= copy_field
,
354 "original and copied events payloads are different pointers");
357 bt_ctf_put(orig_field
);
358 bt_ctf_put(copy_field
);
363 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
364 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
366 /* Create and add a simple event class */
367 struct bt_ctf_event_class
*simple_event_class
=
368 bt_ctf_event_class_create("Simple Event");
369 struct bt_ctf_field_type
*uint_12_type
=
370 bt_ctf_field_type_integer_create(12);
371 struct bt_ctf_field_type
*int_64_type
=
372 bt_ctf_field_type_integer_create(64);
373 struct bt_ctf_field_type
*float_type
=
374 bt_ctf_field_type_floating_point_create();
375 struct bt_ctf_field_type
*enum_type
;
376 struct bt_ctf_field_type
*enum_type_unsigned
=
377 bt_ctf_field_type_enumeration_create(uint_12_type
);
378 struct bt_ctf_field_type
*event_context_type
=
379 bt_ctf_field_type_structure_create();
380 struct bt_ctf_field_type
*returned_type
;
381 struct bt_ctf_event
*simple_event
;
382 struct bt_ctf_field
*integer_field
;
383 struct bt_ctf_field
*float_field
;
384 struct bt_ctf_field
*enum_field
;
385 struct bt_ctf_field
*enum_field_unsigned
;
386 struct bt_ctf_field
*enum_container_field
;
387 const char *mapping_name_test
= "truie";
388 const double double_test_value
= 3.1415;
389 struct bt_ctf_field
*enum_container_field_unsigned
;
390 const char *mapping_name_negative_test
= "negative_value";
391 const char *ret_char
;
393 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
394 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
395 struct bt_ctf_clock
*ret_clock
;
396 struct bt_ctf_event_class
*ret_event_class
;
397 struct bt_ctf_field
*packet_context
;
398 struct bt_ctf_field
*packet_context_field
;
399 struct bt_ctf_field
*stream_event_context
;
400 struct bt_ctf_field
*stream_event_context_field
;
401 struct bt_ctf_field
*event_context
;
402 struct bt_ctf_field
*event_context_field
;
404 ok(uint_12_type
, "Create an unsigned integer type");
406 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
407 ok(int_64_type
, "Create a signed integer type");
408 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
410 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
411 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
412 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
413 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
414 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
415 bt_ctf_put(returned_type
);
417 bt_ctf_field_type_set_alignment(float_type
, 32);
418 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
419 "bt_ctf_field_type_get_alignment handles NULL correctly");
420 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
421 "bt_ctf_field_type_get_alignment returns a correct value");
423 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
424 "Set a floating point type's exponent digit count");
425 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
426 "Set a floating point type's mantissa digit count");
428 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
429 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
430 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
431 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
432 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
433 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
434 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
435 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
437 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
438 mapping_name_negative_test
, -12345, 0) == 0,
439 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
440 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
441 "escaping; \"test\"", 1, 1) == 0,
442 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
443 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
444 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
445 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
446 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
447 "event clock int float", 5, 22) == 0,
448 "Accept enumeration mapping strings containing reserved keywords");
449 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
451 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
452 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
453 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
454 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
455 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
456 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
457 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
459 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
460 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
461 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
462 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
463 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
464 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
466 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
467 "enum_field") == 0, "Add signed enumeration field to event");
469 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
470 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
471 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
472 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
473 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
474 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
475 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
476 NULL
, &ret_range_end_int64_t
) < 0,
477 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
478 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
479 &ret_range_start_int64_t
, NULL
) < 0,
480 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
481 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
482 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
483 "bt_ctf_field_type_enumeration_get_mapping returns a value");
484 ok(!strcmp(ret_char
, mapping_name_test
),
485 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
486 ok(ret_range_start_int64_t
== 42,
487 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
488 ok(ret_range_end_int64_t
== 42,
489 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
491 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
492 "escaping; \"test\"", 0, 0) == 0,
493 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
494 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
495 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
496 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
497 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
498 "event clock int float", 5, 22) == 0,
499 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
500 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
502 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
503 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
504 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
505 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
506 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
507 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
508 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
509 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
511 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
512 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
513 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
514 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
516 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
517 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
518 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
519 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
520 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
521 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
522 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
523 NULL
, &ret_range_end_uint64_t
) < 0,
524 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
525 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
526 &ret_range_start_uint64_t
, NULL
) < 0,
527 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
528 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
529 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
530 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
531 ok(!strcmp(ret_char
, mapping_name_test
),
532 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
533 ok(ret_range_start_uint64_t
== 42,
534 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
535 ok(ret_range_end_uint64_t
== 42,
536 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
538 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
540 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
543 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
545 /* Set an event context type which will contain a single integer*/
546 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
547 "event_specific_context"),
548 "Add event specific context field");
549 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
550 "bt_ctf_event_class_get_context_type handles NULL correctly");
551 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
552 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
554 ok(bt_ctf_event_class_set_context_type(simple_event_class
, NULL
) < 0,
555 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
556 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
557 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
558 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
559 "Set an event class' context type successfully");
560 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
561 ok(returned_type
== event_context_type
,
562 "bt_ctf_event_class_get_context_type returns the appropriate type");
563 bt_ctf_put(returned_type
);
565 bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
);
567 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
568 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
569 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
570 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
571 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
572 "bt_ctf_stream_class_get_event_class handles NULL correctly");
573 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
574 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
575 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
576 ok(ret_event_class
== simple_event_class
,
577 "bt_ctf_stream_class_get_event_class returns the correct event class");
578 bt_ctf_put(ret_event_class
);
579 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
580 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
581 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
582 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
584 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
585 ok(ret_event_class
== simple_event_class
,
586 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
587 bt_ctf_put(ret_event_class
);
589 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
590 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
591 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
592 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
593 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
594 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
595 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
596 ok(ret_event_class
== simple_event_class
,
597 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
598 bt_ctf_put(ret_event_class
);
600 simple_event
= bt_ctf_event_create(simple_event_class
);
602 "Instantiate an event containing a single integer field");
604 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
605 "bt_ctf_event_get_clock handles NULL correctly");
606 ret_clock
= bt_ctf_event_get_clock(simple_event
);
607 ok(ret_clock
== clock
,
608 "bt_ctf_event_get_clock returns a correct clock");
611 integer_field
= bt_ctf_field_create(uint_12_type
);
612 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
613 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
614 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
616 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
617 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
618 "bt_ctf_field_floating_point_get_value fails on an unset float field");
619 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
620 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
621 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
622 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
623 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
624 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
625 "bt_ctf_field_floating_point_get_value returns a double value");
626 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
627 "bt_ctf_field_floating_point_get_value returns a correct value");
629 enum_field
= bt_ctf_field_create(enum_type
);
630 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
631 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
632 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
633 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
634 enum_container_field
= bt_ctf_field_enumeration_get_container(
636 ok(bt_ctf_field_signed_integer_set_value(
637 enum_container_field
, -42) == 0,
638 "Set signed enumeration container value");
639 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
640 ok(!strcmp(ret_char
, mapping_name_negative_test
),
641 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
642 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
644 enum_field_unsigned
= bt_ctf_field_create(enum_type_unsigned
);
645 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
646 enum_field_unsigned
);
647 ok(bt_ctf_field_unsigned_integer_set_value(
648 enum_container_field_unsigned
, 42) == 0,
649 "Set unsigned enumeration container value");
650 bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
651 enum_field_unsigned
);
652 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
653 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
654 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
656 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
658 /* Populate stream event context */
659 stream_event_context
= bt_ctf_stream_get_event_context(stream
);
660 stream_event_context_field
= bt_ctf_field_structure_get_field(
661 stream_event_context
, "common_event_context");
662 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
664 /* Populate the event's context */
665 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
666 "bt_ctf_event_get_event_context handles NULL correctly");
667 event_context
= bt_ctf_event_get_event_context(simple_event
);
669 "bt_ctf_event_get_event_context returns a field");
670 returned_type
= bt_ctf_field_get_type(event_context
);
671 ok(returned_type
== event_context_type
,
672 "bt_ctf_event_get_event_context returns a field of the appropriate type");
673 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
674 "event_specific_context");
675 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
676 "Successfully set an event context's value");
677 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
678 "bt_ctf_event_set_event_context handles a NULL event correctly");
679 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
680 "bt_ctf_event_set_event_context handles a NULL event context correctly");
681 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
682 "bt_ctf_event_set_event_context rejects a context of the wrong type");
683 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
684 "Set an event context successfully");
686 event_copy_tests(simple_event
);
687 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
688 "Append simple event to trace stream");
690 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
691 "bt_ctf_stream_get_packet_context handles NULL correctly");
692 packet_context
= bt_ctf_stream_get_packet_context(stream
);
694 "bt_ctf_stream_get_packet_context returns a packet context");
696 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
698 ok(packet_context_field
,
699 "Packet context contains the default packet_size field.");
700 bt_ctf_put(packet_context_field
);
701 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
702 "custom_packet_context_field");
703 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
704 "Custom packet context field value successfully set.");
706 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
707 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
708 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
709 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
710 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
711 "Successfully set a stream's packet context");
713 ok(bt_ctf_stream_flush(stream
) == 0,
714 "Flush trace stream with one event");
716 bt_ctf_put(simple_event_class
);
717 bt_ctf_put(simple_event
);
718 bt_ctf_put(uint_12_type
);
719 bt_ctf_put(int_64_type
);
720 bt_ctf_put(float_type
);
721 bt_ctf_put(enum_type
);
722 bt_ctf_put(enum_type_unsigned
);
723 bt_ctf_put(returned_type
);
724 bt_ctf_put(event_context_type
);
725 bt_ctf_put(integer_field
);
726 bt_ctf_put(float_field
);
727 bt_ctf_put(enum_field
);
728 bt_ctf_put(enum_field_unsigned
);
729 bt_ctf_put(enum_container_field
);
730 bt_ctf_put(enum_container_field_unsigned
);
731 bt_ctf_put(packet_context
);
732 bt_ctf_put(packet_context_field
);
733 bt_ctf_put(stream_event_context
);
734 bt_ctf_put(stream_event_context_field
);
735 bt_ctf_put(event_context
);
736 bt_ctf_put(event_context_field
);
739 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
740 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
742 struct event_class_attrs_counts
{
753 struct event_class_attrs_counts
;
754 const char *complex_test_event_string
= "Complex Test Event";
755 const char *test_string_1
= "Test ";
756 const char *test_string_2
= "string ";
757 const char *test_string_3
= "abcdefghi";
758 const char *test_string_4
= "abcd\0efg\0hi";
759 const char *test_string_cat
= "Test string abcdeabcd";
760 struct bt_ctf_field_type
*uint_35_type
=
761 bt_ctf_field_type_integer_create(35);
762 struct bt_ctf_field_type
*int_16_type
=
763 bt_ctf_field_type_integer_create(16);
764 struct bt_ctf_field_type
*uint_3_type
=
765 bt_ctf_field_type_integer_create(3);
766 struct bt_ctf_field_type
*enum_variant_type
=
767 bt_ctf_field_type_enumeration_create(uint_3_type
);
768 struct bt_ctf_field_type
*variant_type
=
769 bt_ctf_field_type_variant_create(enum_variant_type
,
771 struct bt_ctf_field_type
*string_type
=
772 bt_ctf_field_type_string_create();
773 struct bt_ctf_field_type
*sequence_type
;
774 struct bt_ctf_field_type
*array_type
;
775 struct bt_ctf_field_type
*inner_structure_type
=
776 bt_ctf_field_type_structure_create();
777 struct bt_ctf_field_type
*complex_structure_type
=
778 bt_ctf_field_type_structure_create();
779 struct bt_ctf_field_type
*ret_field_type
;
780 struct bt_ctf_event_class
*event_class
;
781 struct bt_ctf_event
*event
;
782 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
783 *inner_structure_field
, *complex_structure_field
,
784 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
785 *variant_field
, *an_array_field
, *ret_field
;
786 uint64_t ret_unsigned_int
;
787 int64_t ret_signed_int
;
788 const char *ret_string
;
789 struct bt_ctf_stream_class
*ret_stream_class
;
790 struct bt_ctf_event_class
*ret_event_class
;
791 struct bt_ctf_field
*packet_context
, *packet_context_field
;
792 struct bt_value
*obj
;
794 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
795 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
796 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
797 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
798 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
799 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
800 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
801 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
802 bt_ctf_field_type_set_alignment(int_16_type
, 32);
803 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
804 bt_ctf_field_type_integer_set_base(uint_35_type
,
805 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
807 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
808 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
811 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
812 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
813 ret_field_type
= bt_ctf_field_type_array_get_element_type(
815 ok(ret_field_type
== int_16_type
,
816 "bt_ctf_field_type_array_get_element_type returns the correct type");
817 bt_ctf_put(ret_field_type
);
819 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
820 "bt_ctf_field_type_array_get_length handles NULL correctly");
821 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
822 "bt_ctf_field_type_array_get_length returns the correct length");
824 bt_ctf_field_type_structure_add_field(inner_structure_type
,
825 uint_35_type
, "seq_len");
826 bt_ctf_field_type_structure_add_field(inner_structure_type
,
827 sequence_type
, "a_sequence");
828 bt_ctf_field_type_structure_add_field(inner_structure_type
,
829 array_type
, "an_array");
831 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
833 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
835 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
836 "UINT35_TYPE", 2, 7);
838 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
840 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
841 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
842 enum_variant_type
, NULL
) < 0,
843 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
844 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
845 enum_variant_type
, "INT16_TYPE") == 1,
846 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
848 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
849 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
850 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
851 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
852 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
853 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
855 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
856 "An unknown entry"), "Reject a variant field based on an unknown tag value");
857 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
858 "UINT3_TYPE") == 0, "Add a field to a variant");
859 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
861 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
864 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
865 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
866 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
867 ok(ret_field_type
== enum_variant_type
,
868 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
869 bt_ctf_put(ret_field_type
);
871 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
872 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
873 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
874 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
875 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
876 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
877 "INT16_TYPE") == NULL
,
878 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
879 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
881 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
882 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
883 variant_type
, "INT16_TYPE");
884 ok(ret_field_type
== int_16_type
,
885 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
886 bt_ctf_put(ret_field_type
);
888 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
889 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
890 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
891 "bt_ctf_field_type_variant_get_field_count returns the correct count");
893 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
894 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
895 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
896 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
897 bt_ctf_put(ret_field_type
);
898 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
899 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
900 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
901 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
902 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
903 "bt_ctf_field_type_variant_get_field returns a field");
904 ok(!strcmp("INT16_TYPE", ret_string
),
905 "bt_ctf_field_type_variant_get_field returns a correct field name");
906 ok(ret_field_type
== int_16_type
,
907 "bt_ctf_field_type_variant_get_field returns a correct field type");
908 bt_ctf_put(ret_field_type
);
910 bt_ctf_field_type_structure_add_field(complex_structure_type
,
911 enum_variant_type
, "variant_selector");
912 bt_ctf_field_type_structure_add_field(complex_structure_type
,
913 string_type
, "a_string");
914 bt_ctf_field_type_structure_add_field(complex_structure_type
,
915 variant_type
, "variant_value");
916 bt_ctf_field_type_structure_add_field(complex_structure_type
,
917 inner_structure_type
, "inner_structure");
919 ok(bt_ctf_event_class_create("clock") == NULL
,
920 "Reject creation of an event class with an illegal name");
921 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
922 ok(event_class
, "Create an event class");
923 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
924 "Reject addition of a field with an empty name to an event");
925 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
926 "Reject addition of a field with a NULL type to an event");
927 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
929 "Reject addition of a type with an illegal name to an event");
930 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
932 "Add field of type unsigned integer to an event");
933 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
934 "int_16") == 0, "Add field of type signed integer to an event");
935 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
936 "complex_structure") == 0,
937 "Add composite structure to an event");
939 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
940 "bt_ctf_event_class_get_name handles NULL correctly");
941 ret_string
= bt_ctf_event_class_get_name(event_class
);
942 ok(!strcmp(ret_string
, complex_test_event_string
),
943 "bt_ctf_event_class_get_name returns a correct name");
944 ok(bt_ctf_event_class_get_id(event_class
) < 0,
945 "bt_ctf_event_class_get_id returns a negative value when not set");
946 ok(bt_ctf_event_class_get_id(NULL
) < 0,
947 "bt_ctf_event_class_get_id handles NULL correctly");
948 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
949 "bt_ctf_event_class_set_id handles NULL correctly");
950 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
951 "Set an event class' id");
952 ok(bt_ctf_event_class_get_id(event_class
) == 42,
953 "bt_ctf_event_class_get_id returns the correct value");
955 /* Test event class attributes */
956 assert(obj
= bt_value_integer_create_init(15));
957 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
958 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
959 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
960 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
961 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
962 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
963 assert(!bt_value_integer_set(obj
, -3));
964 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
965 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
966 assert(!bt_value_integer_set(obj
, 11));
967 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
968 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
969 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
970 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
971 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
973 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
976 obj
= bt_value_integer_create_init(5);
978 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
979 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
981 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
982 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
983 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
984 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
985 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
986 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
987 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
990 ret
= bt_value_integer_get(obj
, &int64_value
);
991 ok(obj
&& !ret
&& int64_value
== 5,
992 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
995 assert(obj
= bt_value_string_create_init("nu name"));
996 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
997 ret_string
= bt_ctf_event_class_get_name(event_class
);
998 ok(!strcmp(ret_string
, "nu name"),
999 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
1000 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
1001 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
1003 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
1005 obj
= bt_value_string_create_init("http://kernel.org/");
1007 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
1010 ok(bt_ctf_event_class_get_attribute_count(NULL
),
1011 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1012 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
1013 "bt_ctf_event_class_get_attribute_count returns the correct count");
1014 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
1015 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1016 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
1017 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1018 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
1019 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1020 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
1021 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1023 memset(&attrs_count
, 0, sizeof(attrs_count
));
1025 for (i
= 0; i
< 4; ++i
) {
1026 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
1028 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
1029 assert(ret_string
&& obj
);
1031 if (!strcmp(ret_string
, "id")) {
1033 ok(bt_value_is_integer(obj
),
1034 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1036 } else if (!strcmp(ret_string
, "name")) {
1038 ok(bt_value_is_string(obj
),
1039 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1041 } else if (!strcmp(ret_string
, "loglevel")) {
1042 attrs_count
.loglevel
++;
1043 ok(bt_value_is_integer(obj
),
1044 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1046 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1047 attrs_count
.modelemfuri
++;
1048 ok(bt_value_is_string(obj
),
1049 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1052 attrs_count
.unknown
++;
1058 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1059 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1060 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1061 "event class has one instance of each known attribute");
1063 /* Add event class to the stream class */
1064 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1065 "Reject addition of NULL event class to a stream class");
1066 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1067 event_class
) == 0, "Add an event class to stream class");
1069 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1070 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1071 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1072 ok(ret_stream_class
== stream_class
,
1073 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1074 bt_ctf_put(ret_stream_class
);
1076 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1077 "bt_ctf_event_class_get_field_count handles NULL correctly");
1078 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1079 "bt_ctf_event_class_get_field_count returns a correct value");
1081 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1082 &ret_field_type
, 0) < 0,
1083 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1084 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1085 &ret_field_type
, 0) == 0,
1086 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1087 bt_ctf_put(ret_field_type
);
1088 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1090 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1091 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1092 &ret_field_type
, 42) < 0,
1093 "bt_ctf_event_class_get_field handles an invalid index correctly");
1094 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1095 &ret_field_type
, 0) == 0,
1096 "bt_ctf_event_class_get_field returns a field");
1097 ok(ret_field_type
== uint_35_type
,
1098 "bt_ctf_event_class_get_field returns a correct field type");
1099 bt_ctf_put(ret_field_type
);
1100 ok(!strcmp(ret_string
, "uint_35"),
1101 "bt_ctf_event_class_get_field returns a correct field name");
1102 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1103 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1104 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1105 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1106 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1107 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1108 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1109 "complex_structure");
1110 ok(ret_field_type
== complex_structure_type
,
1111 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1112 bt_ctf_put(ret_field_type
);
1114 event
= bt_ctf_event_create(event_class
);
1115 ok(event
, "Instanciate a complex event");
1117 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1118 "bt_ctf_event_get_class handles NULL correctly");
1119 ret_event_class
= bt_ctf_event_get_class(event
);
1120 ok(ret_event_class
== event_class
,
1121 "bt_ctf_event_get_class returns the correct event class");
1122 bt_ctf_put(ret_event_class
);
1124 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1125 if (!uint_35_field
) {
1126 printf("uint_35_field is NULL\n");
1129 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1130 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1131 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1132 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1133 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1134 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1135 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1136 &ret_unsigned_int
) == 0,
1137 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1138 ok(ret_unsigned_int
== 0x0DDF00D,
1139 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1140 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1141 &ret_signed_int
) < 0,
1142 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1143 bt_ctf_put(uint_35_field
);
1145 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1146 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1147 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1148 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1149 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1150 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1151 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1152 &ret_signed_int
) == 0,
1153 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1154 ok(ret_signed_int
== -12345,
1155 "bt_ctf_field_signed_integer_get_value returns the correct value");
1156 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1157 &ret_unsigned_int
) < 0,
1158 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1159 bt_ctf_put(int_16_field
);
1161 complex_structure_field
= bt_ctf_event_get_payload(event
,
1162 "complex_structure");
1164 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1165 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1166 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1167 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1168 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1169 complex_structure_field
, 3);
1170 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1171 bt_ctf_put(inner_structure_field
);
1172 ok(ret_field_type
== inner_structure_type
,
1173 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1174 bt_ctf_put(ret_field_type
);
1176 inner_structure_field
= bt_ctf_field_structure_get_field(
1177 complex_structure_field
, "inner_structure");
1178 a_string_field
= bt_ctf_field_structure_get_field(
1179 complex_structure_field
, "a_string");
1180 enum_variant_field
= bt_ctf_field_structure_get_field(
1181 complex_structure_field
, "variant_selector");
1182 variant_field
= bt_ctf_field_structure_get_field(
1183 complex_structure_field
, "variant_value");
1184 uint_35_field
= bt_ctf_field_structure_get_field(
1185 inner_structure_field
, "seq_len");
1186 a_sequence_field
= bt_ctf_field_structure_get_field(
1187 inner_structure_field
, "a_sequence");
1188 an_array_field
= bt_ctf_field_structure_get_field(
1189 inner_structure_field
, "an_array");
1191 enum_container_field
= bt_ctf_field_enumeration_get_container(
1192 enum_variant_field
);
1193 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1194 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1195 enum_variant_field
);
1196 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1197 bt_ctf_put(int_16_field
);
1198 ok(!bt_ctf_field_string_get_value(a_string_field
),
1199 "bt_ctf_field_string_get_value returns NULL on an unset field");
1200 bt_ctf_field_string_set_value(a_string_field
,
1202 ok(!bt_ctf_field_string_get_value(NULL
),
1203 "bt_ctf_field_string_get_value correctly handles NULL");
1204 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1205 "bt_ctf_field_string_append correctly handles a NULL string field");
1206 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1207 "bt_ctf_field_string_append correctly handles a NULL string value");
1208 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1209 "bt_ctf_field_string_append succeeds");
1210 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1211 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1212 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1213 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1214 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1215 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1216 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1217 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1218 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1219 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1220 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1221 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1223 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1224 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1225 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1226 "bt_ctf_field_string_get_value returns a correct value");
1227 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1228 SEQUENCE_TEST_LENGTH
);
1230 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1231 enum_container_field
) == NULL
,
1232 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1233 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1235 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1236 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1237 variant_type
, enum_variant_field
);
1238 ok(ret_field_type
== int_16_type
,
1239 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1241 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1242 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1243 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1244 uint_35_field
) == 0, "Set a sequence field's length");
1245 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1246 ok(ret_field
== uint_35_field
,
1247 "bt_ctf_field_sequence_get_length returns the correct length field");
1248 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1249 "bt_ctf_field_sequence_get_length properly handles NULL");
1251 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1252 int_16_field
= bt_ctf_field_sequence_get_field(
1253 a_sequence_field
, i
);
1254 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1255 bt_ctf_put(int_16_field
);
1258 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1259 int_16_field
= bt_ctf_field_array_get_field(
1261 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1262 bt_ctf_put(int_16_field
);
1265 bt_ctf_clock_set_time(clock
, ++current_time
);
1266 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1267 "Append a complex event to a stream");
1270 * Populate the custom packet context field with a dummy value
1271 * otherwise flush will fail.
1273 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1274 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1275 "custom_packet_context_field");
1276 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1278 ok(bt_ctf_stream_flush(stream
) == 0,
1279 "Flush a stream containing a complex event");
1281 bt_ctf_put(uint_35_field
);
1282 bt_ctf_put(a_string_field
);
1283 bt_ctf_put(inner_structure_field
);
1284 bt_ctf_put(complex_structure_field
);
1285 bt_ctf_put(a_sequence_field
);
1286 bt_ctf_put(an_array_field
);
1287 bt_ctf_put(enum_variant_field
);
1288 bt_ctf_put(enum_container_field
);
1289 bt_ctf_put(variant_field
);
1290 bt_ctf_put(ret_field
);
1291 bt_ctf_put(packet_context_field
);
1292 bt_ctf_put(packet_context
);
1293 bt_ctf_put(uint_35_type
);
1294 bt_ctf_put(int_16_type
);
1295 bt_ctf_put(string_type
);
1296 bt_ctf_put(sequence_type
);
1297 bt_ctf_put(array_type
);
1298 bt_ctf_put(inner_structure_type
);
1299 bt_ctf_put(complex_structure_type
);
1300 bt_ctf_put(uint_3_type
);
1301 bt_ctf_put(enum_variant_type
);
1302 bt_ctf_put(variant_type
);
1303 bt_ctf_put(ret_field_type
);
1304 bt_ctf_put(event_class
);
1308 static void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1309 struct bt_ctf_field_type
*expected_type
, const char *name
)
1311 struct bt_ctf_field_type
*copy_type
;
1313 copy_type
= bt_ctf_field_get_type(field
);
1314 ok(copy_type
== expected_type
,
1315 "bt_ctf_field_copy does not copy the type (%s)", name
);
1316 bt_ctf_put(copy_type
);
1319 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1320 struct bt_ctf_field
*field_b
, const char *name
)
1322 ok(field_a
!= field_b
,
1323 "bt_ctf_field_copy creates different pointers (%s)", name
);
1326 void field_copy_tests()
1328 struct bt_ctf_field_type
*len_type
= NULL
;
1329 struct bt_ctf_field_type
*fp_type
= NULL
;
1330 struct bt_ctf_field_type
*s_type
= NULL
;
1331 struct bt_ctf_field_type
*e_int_type
= NULL
;
1332 struct bt_ctf_field_type
*e_type
= NULL
;
1333 struct bt_ctf_field_type
*v_type
= NULL
;
1334 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1335 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1336 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1337 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1338 struct bt_ctf_field_type
*strct_type
= NULL
;
1339 struct bt_ctf_field
*len
= NULL
;
1340 struct bt_ctf_field
*fp
= NULL
;
1341 struct bt_ctf_field
*s
= NULL
;
1342 struct bt_ctf_field
*e_int
= NULL
;
1343 struct bt_ctf_field
*e
= NULL
;
1344 struct bt_ctf_field
*v
= NULL
;
1345 struct bt_ctf_field
*v_selected
= NULL
;
1346 struct bt_ctf_field
*v_selected_cur
= NULL
;
1347 struct bt_ctf_field
*v_selected_0
= NULL
;
1348 struct bt_ctf_field
*v_selected_1
= NULL
;
1349 struct bt_ctf_field
*v_selected_2
= NULL
;
1350 struct bt_ctf_field
*v_selected_3
= NULL
;
1351 struct bt_ctf_field
*v_selected_4
= NULL
;
1352 struct bt_ctf_field
*v_selected_5
= NULL
;
1353 struct bt_ctf_field
*v_selected_6
= NULL
;
1354 struct bt_ctf_field
*a
= NULL
;
1355 struct bt_ctf_field
*a_0
= NULL
;
1356 struct bt_ctf_field
*a_1
= NULL
;
1357 struct bt_ctf_field
*a_2
= NULL
;
1358 struct bt_ctf_field
*a_3
= NULL
;
1359 struct bt_ctf_field
*a_4
= NULL
;
1360 struct bt_ctf_field
*strct
= NULL
;
1361 struct bt_ctf_field
*len_copy
= NULL
;
1362 struct bt_ctf_field
*fp_copy
= NULL
;
1363 struct bt_ctf_field
*s_copy
= NULL
;
1364 struct bt_ctf_field
*e_int_copy
= NULL
;
1365 struct bt_ctf_field
*e_copy
= NULL
;
1366 struct bt_ctf_field
*v_copy
= NULL
;
1367 struct bt_ctf_field
*v_selected_copy
= NULL
;
1368 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1369 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1370 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1371 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1372 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1373 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1374 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1375 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1376 struct bt_ctf_field
*a_copy
= NULL
;
1377 struct bt_ctf_field
*a_0_copy
= NULL
;
1378 struct bt_ctf_field
*a_1_copy
= NULL
;
1379 struct bt_ctf_field
*a_2_copy
= NULL
;
1380 struct bt_ctf_field
*a_3_copy
= NULL
;
1381 struct bt_ctf_field
*a_4_copy
= NULL
;
1382 struct bt_ctf_field
*strct_copy
= NULL
;
1383 uint64_t uint64_t_val
;
1384 const char *str_val
;
1388 /* create len type */
1389 len_type
= bt_ctf_field_type_integer_create(32);
1392 /* create fp type */
1393 fp_type
= bt_ctf_field_type_floating_point_create();
1397 s_type
= bt_ctf_field_type_string_create();
1400 /* create e_int type */
1401 e_int_type
= bt_ctf_field_type_integer_create(8);
1405 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1407 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1410 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1414 /* create v_label1 type */
1415 v_label1_type
= bt_ctf_field_type_string_create();
1416 assert(v_label1_type
);
1418 /* create v_label1_array type */
1419 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1420 assert(v_label1_array_type
);
1422 /* create v_label2 type */
1423 v_label2_type
= bt_ctf_field_type_integer_create(16);
1424 assert(v_label2_type
);
1426 /* create v_label2_seq type */
1427 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1429 assert(v_label2_seq_type
);
1432 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1434 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1437 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1441 /* create strct type */
1442 strct_type
= bt_ctf_field_type_structure_create();
1444 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1447 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1449 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1451 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1453 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1455 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1456 v_label1_array_type
, "a");
1460 strct
= bt_ctf_field_create(strct_type
);
1464 len
= bt_ctf_field_structure_get_field(strct
, "len");
1468 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1472 s
= bt_ctf_field_structure_get_field(strct
, "s");
1476 e
= bt_ctf_field_structure_get_field(strct
, "e");
1479 /* get e_int (underlying integer) */
1480 e_int
= bt_ctf_field_enumeration_get_container(e
);
1484 v
= bt_ctf_field_structure_get_field(strct
, "v");
1488 a
= bt_ctf_field_structure_get_field(strct
, "a");
1492 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1496 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1500 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1503 /* set e field (LABEL2) */
1504 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1508 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1510 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1511 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1512 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1513 ok(v_selected_cur
== v_selected
,
1514 "bt_ctf_field_variant_get_current_field returns the current field");
1515 bt_ctf_put(v_selected_cur
);
1517 /* set selected v field */
1518 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1520 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1521 assert(v_selected_0
);
1522 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1524 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1525 assert(v_selected_1
);
1526 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1528 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1529 assert(v_selected_2
);
1530 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1532 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1533 assert(v_selected_3
);
1534 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1536 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1537 assert(v_selected_4
);
1538 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1540 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1541 assert(v_selected_5
);
1542 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1544 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1545 assert(v_selected_6
);
1546 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1550 a_0
= bt_ctf_field_array_get_field(a
, 0);
1552 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1554 a_1
= bt_ctf_field_array_get_field(a
, 1);
1556 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1558 a_2
= bt_ctf_field_array_get_field(a
, 2);
1560 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1562 a_3
= bt_ctf_field_array_get_field(a
, 3);
1564 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1566 a_4
= bt_ctf_field_array_get_field(a
, 4);
1568 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1571 /* create copy of strct */
1572 ok(!bt_ctf_field_copy(NULL
),
1573 "bt_ctf_field_copy handles NULL correctly");
1574 strct_copy
= bt_ctf_field_copy(strct
);
1576 "bt_ctf_field_copy returns a valid pointer");
1578 /* get all copied fields */
1579 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1581 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1583 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1585 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1587 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1589 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1591 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1592 assert(v_selected_copy
);
1593 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1594 assert(v_selected_0_copy
);
1595 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1596 assert(v_selected_1_copy
);
1597 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1598 assert(v_selected_2_copy
);
1599 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1600 assert(v_selected_3_copy
);
1601 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1602 assert(v_selected_4_copy
);
1603 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1604 assert(v_selected_5_copy
);
1605 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1606 assert(v_selected_6_copy
);
1607 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1608 "sequence field copy is not too large");
1609 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1611 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1613 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1615 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1617 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1619 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1621 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1622 "array field copy is not too large");
1624 /* make sure copied fields are different pointers */
1625 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1626 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1627 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1628 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1629 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1630 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1631 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1632 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1634 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1636 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1638 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1640 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1642 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1644 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1646 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1648 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1649 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1650 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1651 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1652 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1653 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1655 /* make sure copied fields share the same types */
1656 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1657 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1658 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1659 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1660 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1661 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1662 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1664 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1666 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1668 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1670 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1672 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1674 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1676 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1678 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1679 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1680 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1681 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1682 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1683 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1685 /* validate len copy */
1686 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1688 ok(uint64_t_val
== 7,
1689 "bt_ctf_field_copy creates a valid integer field copy");
1691 /* validate fp copy */
1692 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1694 ok(double_val
== 3.14,
1695 "bt_ctf_field_copy creates a valid floating point number field copy");
1697 /* validate s copy */
1698 str_val
= bt_ctf_field_string_get_value(s_copy
);
1699 ok(str_val
&& !strcmp(str_val
, "btbt"),
1700 "bt_ctf_field_copy creates a valid string field copy");
1702 /* validate e_int copy */
1703 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1706 ok(uint64_t_val
== 23,
1707 "bt_ctf_field_copy creates a valid enum's integer field copy");
1709 /* validate e copy */
1710 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1711 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1712 "bt_ctf_field_copy creates a valid enum field copy");
1714 /* validate v_selected copy */
1715 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1716 assert(v_selected_copy_len
);
1717 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1720 ok(uint64_t_val
== 7,
1721 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1722 bt_ctf_put(v_selected_copy_len
);
1723 v_selected_copy_len
= NULL
;
1725 /* validate v_selected copy fields */
1726 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1729 ok(uint64_t_val
== 7,
1730 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1731 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1734 ok(uint64_t_val
== 6,
1735 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1736 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1739 ok(uint64_t_val
== 5,
1740 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1741 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1744 ok(uint64_t_val
== 4,
1745 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1746 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1749 ok(uint64_t_val
== 3,
1750 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1751 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1754 ok(uint64_t_val
== 2,
1755 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1756 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1759 ok(uint64_t_val
== 1,
1760 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1762 /* validate a copy fields */
1763 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1764 ok(str_val
&& !strcmp(str_val
, "a_0"),
1765 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1766 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1767 ok(str_val
&& !strcmp(str_val
, "a_1"),
1768 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1769 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1770 ok(str_val
&& !strcmp(str_val
, "a_2"),
1771 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1772 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1773 ok(str_val
&& !strcmp(str_val
, "a_3"),
1774 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1775 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1776 ok(str_val
&& !strcmp(str_val
, "a_4"),
1777 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1779 /* put everything */
1780 bt_ctf_put(len_type
);
1781 bt_ctf_put(fp_type
);
1783 bt_ctf_put(e_int_type
);
1786 bt_ctf_put(v_label1_type
);
1787 bt_ctf_put(v_label1_array_type
);
1788 bt_ctf_put(v_label2_type
);
1789 bt_ctf_put(v_label2_seq_type
);
1790 bt_ctf_put(strct_type
);
1797 bt_ctf_put(v_selected
);
1798 bt_ctf_put(v_selected_0
);
1799 bt_ctf_put(v_selected_1
);
1800 bt_ctf_put(v_selected_2
);
1801 bt_ctf_put(v_selected_3
);
1802 bt_ctf_put(v_selected_4
);
1803 bt_ctf_put(v_selected_5
);
1804 bt_ctf_put(v_selected_6
);
1812 bt_ctf_put(len_copy
);
1813 bt_ctf_put(fp_copy
);
1815 bt_ctf_put(e_int_copy
);
1818 bt_ctf_put(v_selected_copy
);
1819 bt_ctf_put(v_selected_0_copy
);
1820 bt_ctf_put(v_selected_1_copy
);
1821 bt_ctf_put(v_selected_2_copy
);
1822 bt_ctf_put(v_selected_3_copy
);
1823 bt_ctf_put(v_selected_4_copy
);
1824 bt_ctf_put(v_selected_5_copy
);
1825 bt_ctf_put(v_selected_6_copy
);
1827 bt_ctf_put(a_0_copy
);
1828 bt_ctf_put(a_1_copy
);
1829 bt_ctf_put(a_2_copy
);
1830 bt_ctf_put(a_3_copy
);
1831 bt_ctf_put(a_4_copy
);
1832 bt_ctf_put(strct_copy
);
1835 void type_field_tests()
1837 struct bt_ctf_field
*uint_12
;
1838 struct bt_ctf_field
*int_16
;
1839 struct bt_ctf_field
*string
;
1840 struct bt_ctf_field
*enumeration
;
1841 struct bt_ctf_field_type
*composite_structure_type
;
1842 struct bt_ctf_field_type
*structure_seq_type
;
1843 struct bt_ctf_field_type
*string_type
;
1844 struct bt_ctf_field_type
*sequence_type
;
1845 struct bt_ctf_field_type
*uint_8_type
;
1846 struct bt_ctf_field_type
*int_16_type
;
1847 struct bt_ctf_field_type
*uint_12_type
=
1848 bt_ctf_field_type_integer_create(12);
1849 struct bt_ctf_field_type
*enumeration_type
;
1850 struct bt_ctf_field_type
*enumeration_sequence_type
;
1851 struct bt_ctf_field_type
*enumeration_array_type
;
1852 struct bt_ctf_field_type
*returned_type
;
1853 const char *ret_string
;
1855 returned_type
= bt_ctf_field_get_type(NULL
);
1856 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1858 ok(uint_12_type
, "Create an unsigned integer type");
1859 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1860 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1861 "Set integer type's base as binary");
1862 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1863 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1864 "Set integer type's base as decimal");
1865 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1866 BT_CTF_INTEGER_BASE_UNKNOWN
),
1867 "Reject integer type's base set as unknown");
1868 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1869 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1870 "Set integer type's base as octal");
1871 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1872 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1873 "Set integer type's base as hexadecimal");
1874 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1875 "Reject unknown integer base value");
1876 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1877 "Set integer type signedness to signed");
1878 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1879 "Set integer type signedness to unsigned");
1880 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1881 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1882 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1883 "bt_ctf_field_type_integer_get_size returns a correct value");
1884 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1885 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1886 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1887 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1889 ok(bt_ctf_field_type_set_byte_order(NULL
,
1890 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1891 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1892 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1893 (enum bt_ctf_byte_order
) 42) < 0,
1894 "bt_ctf_field_type_set_byte_order rejects invalid values");
1895 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1896 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1897 "Set an integer's byte order to little endian");
1898 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1899 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1900 "Set an integer's byte order to big endian");
1901 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1902 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1903 "bt_ctf_field_type_get_byte_order returns a correct value");
1904 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1905 BT_CTF_BYTE_ORDER_UNKNOWN
,
1906 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1908 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1910 "bt_ctf_field_type_get_type_id handles NULL correctly");
1911 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1913 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1915 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1916 BT_CTF_INTEGER_BASE_UNKNOWN
,
1917 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1918 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1919 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1920 "bt_ctf_field_type_integer_get_base returns a correct value");
1922 ok(bt_ctf_field_type_integer_set_encoding(NULL
, CTF_STRING_ASCII
) < 0,
1923 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1924 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1925 (enum ctf_string_encoding
) 123) < 0,
1926 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1927 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1928 CTF_STRING_UTF8
) == 0,
1929 "Set integer type encoding to UTF8");
1930 ok(bt_ctf_field_type_integer_get_encoding(NULL
) == CTF_STRING_UNKNOWN
,
1931 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1932 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) == CTF_STRING_UTF8
,
1933 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1935 int_16_type
= bt_ctf_field_type_integer_create(16);
1936 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1937 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1938 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1939 uint_8_type
= bt_ctf_field_type_integer_create(8);
1941 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1942 ok(sequence_type
, "Create a sequence of int16_t type");
1943 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1945 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1947 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1948 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1949 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1951 ok(!strcmp(ret_string
, "seq_len"),
1952 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1953 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1954 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1955 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1957 ok(returned_type
== int_16_type
,
1958 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1959 bt_ctf_put(returned_type
);
1961 string_type
= bt_ctf_field_type_string_create();
1962 ok(string_type
, "Create a string type");
1963 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1965 "Reject invalid \"None\" string encoding");
1966 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1968 "Reject invalid string encoding");
1969 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1970 CTF_STRING_ASCII
) == 0,
1971 "Set string encoding to ASCII");
1973 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1975 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1976 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1978 "bt_ctf_field_type_string_get_encoding returns the correct value");
1980 structure_seq_type
= bt_ctf_field_type_structure_create();
1981 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1983 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1984 ok(structure_seq_type
, "Create a structure type");
1985 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1986 uint_8_type
, "seq_len") == 0,
1987 "Add a uint8_t type to a structure");
1988 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1989 sequence_type
, "a_sequence") == 0,
1990 "Add a sequence type to a structure");
1992 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1993 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1994 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1995 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1997 ok(bt_ctf_field_type_structure_get_field(NULL
,
1998 &ret_string
, &returned_type
, 1) < 0,
1999 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2000 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2001 NULL
, &returned_type
, 1) == 0,
2002 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2003 bt_ctf_put(returned_type
);
2004 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2005 &ret_string
, NULL
, 1) == 0,
2006 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2007 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2008 &ret_string
, &returned_type
, 10) < 0,
2009 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2010 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2011 &ret_string
, &returned_type
, 1) == 0,
2012 "bt_ctf_field_type_structure_get_field returns a field");
2013 ok(!strcmp(ret_string
, "a_sequence"),
2014 "bt_ctf_field_type_structure_get_field returns a correct field name");
2015 ok(returned_type
== sequence_type
,
2016 "bt_ctf_field_type_structure_get_field returns a correct field type");
2017 bt_ctf_put(returned_type
);
2019 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2020 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2021 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2022 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2023 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2024 structure_seq_type
, "a_sequence");
2025 ok(returned_type
== sequence_type
,
2026 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2027 bt_ctf_put(returned_type
);
2029 composite_structure_type
= bt_ctf_field_type_structure_create();
2030 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2031 string_type
, "a_string") == 0,
2032 "Add a string type to a structure");
2033 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2034 structure_seq_type
, "inner_structure") == 0,
2035 "Add a structure type to a structure");
2037 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2038 NULL
, "a_sequence") == NULL
,
2039 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2040 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2041 structure_seq_type
, NULL
) == NULL
,
2042 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2043 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2044 structure_seq_type
, "a_sequence");
2045 ok(returned_type
== sequence_type
,
2046 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2047 bt_ctf_put(returned_type
);
2049 int_16
= bt_ctf_field_create(int_16_type
);
2050 ok(int_16
, "Instanciate a signed 16-bit integer");
2051 uint_12
= bt_ctf_field_create(uint_12_type
);
2052 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2053 returned_type
= bt_ctf_field_get_type(int_16
);
2054 ok(returned_type
== int_16_type
,
2055 "bt_ctf_field_get_type returns the correct type");
2057 /* Can't modify types after instanciating them */
2058 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2059 BT_CTF_INTEGER_BASE_DECIMAL
),
2060 "Check an integer type' base can't be modified after instanciation");
2061 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2062 "Check an integer type's signedness can't be modified after instanciation");
2064 /* Check signed property is checked */
2065 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2066 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2067 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2068 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2070 /* Check overflows are properly tested for */
2071 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2072 "Check -32768 is allowed for a signed 16-bit integer");
2073 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2074 "Check 32767 is allowed for a signed 16-bit integer");
2075 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2076 "Check 32768 is not allowed for a signed 16-bit integer");
2077 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2078 "Check -32769 is not allowed for a signed 16-bit integer");
2079 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2080 "Check -42 is allowed for a signed 16-bit integer");
2082 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2083 "Check 4095 is allowed for an unsigned 12-bit integer");
2084 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2085 "Check 4096 is not allowed for a unsigned 12-bit integer");
2086 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2087 "Check 0 is allowed for an unsigned 12-bit integer");
2089 string
= bt_ctf_field_create(string_type
);
2090 ok(string
, "Instanciate a string field");
2091 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2092 "Set a string's value");
2094 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2095 ok(enumeration_type
,
2096 "Create an enumeration type with an unsigned 12-bit integer as container");
2097 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
2098 enumeration_type
, "count");
2099 ok(!enumeration_sequence_type
,
2100 "Check enumeration types are validated when creating a sequence");
2101 enumeration_array_type
= bt_ctf_field_type_array_create(
2102 enumeration_type
, 10);
2103 ok(!enumeration_array_type
,
2104 "Check enumeration types are validated when creating an array");
2105 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2106 enumeration_type
, "enumeration"),
2107 "Check enumeration types are validated when adding them as structure members");
2108 enumeration
= bt_ctf_field_create(enumeration_type
);
2110 "Check enumeration types are validated before instantiation");
2113 bt_ctf_put(uint_12
);
2115 bt_ctf_put(enumeration
);
2116 bt_ctf_put(composite_structure_type
);
2117 bt_ctf_put(structure_seq_type
);
2118 bt_ctf_put(string_type
);
2119 bt_ctf_put(sequence_type
);
2120 bt_ctf_put(uint_8_type
);
2121 bt_ctf_put(int_16_type
);
2122 bt_ctf_put(uint_12_type
);
2123 bt_ctf_put(enumeration_type
);
2124 bt_ctf_put(enumeration_sequence_type
);
2125 bt_ctf_put(enumeration_array_type
);
2126 bt_ctf_put(returned_type
);
2129 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2130 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2133 * Append enough events to force the underlying packet to be resized.
2134 * Also tests that a new event can be declared after a stream has been
2135 * instantiated and used/flushed.
2139 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2141 struct bt_ctf_field_type
*integer_type
=
2142 bt_ctf_field_type_integer_create(17);
2143 struct bt_ctf_field_type
*string_type
=
2144 bt_ctf_field_type_string_create();
2145 struct bt_ctf_event
*event
= NULL
;
2146 struct bt_ctf_field
*ret_field
= NULL
;
2147 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2148 uint64_t ret_uint64
;
2149 int events_appended
= 0;
2150 struct bt_ctf_field
*packet_context
= NULL
,
2151 *packet_context_field
= NULL
, *event_context
= NULL
;
2153 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2155 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2157 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2158 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2163 event
= bt_ctf_event_create(event_class
);
2164 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2165 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2166 ok(ret_field_type
== integer_type
,
2167 "bt_ctf_event_get_payload_by_index returns a correct field");
2168 bt_ctf_put(ret_field_type
);
2169 bt_ctf_put(ret_field
);
2171 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2172 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2173 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2174 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2177 ok(bt_ctf_stream_get_event_context(NULL
) == NULL
,
2178 "bt_ctf_stream_get_event_context handles NULL correctly");
2179 event_context
= bt_ctf_stream_get_event_context(stream
);
2181 "bt_ctf_stream_get_event_context returns a stream event context");
2182 ok(bt_ctf_stream_set_event_context(NULL
, event_context
) < 0,
2183 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
2184 ok(bt_ctf_stream_set_event_context(stream
, NULL
) < 0,
2185 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
2186 ok(!bt_ctf_stream_set_event_context(stream
, event_context
),
2187 "bt_ctf_stream_set_event_context correctly set a stream event context");
2188 ret_field
= bt_ctf_field_create(integer_type
);
2189 ok(bt_ctf_stream_set_event_context(stream
, ret_field
) < 0,
2190 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
2191 bt_ctf_put(ret_field
);
2193 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2194 event
= bt_ctf_event_create(event_class
);
2195 struct bt_ctf_field
*integer
=
2196 bt_ctf_field_create(integer_type
);
2197 struct bt_ctf_field
*string
=
2198 bt_ctf_field_create(string_type
);
2200 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2201 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2202 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2204 bt_ctf_put(integer
);
2205 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2206 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2210 /* Populate stream event context */
2211 integer
= bt_ctf_field_structure_get_field(event_context
,
2212 "common_event_context");
2213 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2215 bt_ctf_put(integer
);
2217 ret
|= bt_ctf_stream_append_event(stream
, event
);
2225 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2226 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2227 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2228 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2229 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2230 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2231 ok(ret
== 0 && ret_uint64
== 0,
2232 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2233 bt_ctf_stream_append_discarded_events(stream
, 1000);
2234 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2235 ok(ret
== 0 && ret_uint64
== 1000,
2236 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2239 ok(events_appended
, "Append 100 000 events to a stream");
2242 * Populate the custom packet context field with a dummy value
2243 * otherwise flush will fail.
2245 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2246 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2247 "custom_packet_context_field");
2248 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2250 ok(bt_ctf_stream_flush(stream
) == 0,
2251 "Flush a stream that forces a packet resize");
2252 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2253 ok(ret
== 0 && ret_uint64
== 1000,
2254 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2255 bt_ctf_put(integer_type
);
2256 bt_ctf_put(string_type
);
2257 bt_ctf_put(packet_context
);
2258 bt_ctf_put(packet_context_field
);
2259 bt_ctf_put(event_context
);
2260 bt_ctf_put(event_class
);
2263 void test_empty_stream(struct bt_ctf_writer
*writer
)
2266 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2267 struct bt_ctf_stream_class
*stream_class
= NULL
;
2268 struct bt_ctf_stream
*stream
= NULL
;
2270 trace
= bt_ctf_writer_get_trace(writer
);
2272 diag("Failed to get trace from writer");
2277 stream_class
= bt_ctf_stream_class_create("empty_stream");
2278 if (!stream_class
) {
2279 diag("Failed to create stream class");
2284 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2285 "bt_ctf_stream_class_get_trace handles NULL correctly");
2286 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2287 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2289 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2291 diag("Failed to create writer stream");
2296 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2297 ok(ret_trace
== trace
,
2298 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2301 "Created a stream class with default attributes and an empty stream");
2303 bt_ctf_put(ret_trace
);
2305 bt_ctf_put(stream_class
);
2308 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2311 struct bt_ctf_trace
*trace
= NULL
;
2312 struct bt_ctf_clock
*clock
= NULL
;
2313 struct bt_ctf_stream_class
*stream_class
= NULL
;
2314 struct bt_ctf_stream
*stream
= NULL
;
2315 struct bt_ctf_field_type
*integer_type
= NULL
,
2316 *sequence_type
= NULL
, *event_header_type
= NULL
;
2317 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2318 *event_header
= NULL
, *packet_header
= NULL
;
2319 struct bt_ctf_event_class
*event_class
= NULL
;
2320 struct bt_ctf_event
*event
= NULL
;
2322 trace
= bt_ctf_writer_get_trace(writer
);
2324 fail("Failed to get trace from writer");
2328 clock
= bt_ctf_trace_get_clock(trace
, 0);
2330 fail("Failed to get clock from trace");
2334 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2335 if (!stream_class
) {
2336 fail("Failed to create stream class");
2340 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2342 fail("Failed to set stream class clock");
2347 * Customize event header to add an "seq_len" integer member
2348 * which will be used as the length of a sequence in an event of this
2351 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2353 if (!event_header_type
) {
2354 fail("Failed to get event header type");
2358 integer_type
= bt_ctf_field_type_integer_create(13);
2359 if (!integer_type
) {
2360 fail("Failed to create length integer type");
2364 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2365 integer_type
, "seq_len");
2367 fail("Failed to add a new field to stream event header");
2371 event_class
= bt_ctf_event_class_create("sequence_event");
2373 fail("Failed to create event class");
2378 * This event's payload will contain a sequence which references
2379 * stream.event.header.seq_len as its length field.
2381 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2382 "stream.event.header.seq_len");
2383 if (!sequence_type
) {
2384 fail("Failed to create a sequence");
2388 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2391 fail("Failed to add a sequence to an event class");
2395 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2397 fail("Failed to add event class to stream class");
2401 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2403 fail("Failed to create stream")
2408 * We have defined a custom packet header field. We have to populate it
2411 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2412 if (!packet_header
) {
2413 fail("Failed to get stream packet header");
2417 integer
= bt_ctf_field_structure_get_field(packet_header
,
2418 "custom_trace_packet_header_field");
2420 fail("Failed to retrieve custom_trace_packet_header_field");
2424 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2426 fail("Failed to set custom_trace_packet_header_field value");
2429 bt_ctf_put(integer
);
2431 event
= bt_ctf_event_create(event_class
);
2433 fail("Failed to create event");
2437 event_header
= bt_ctf_event_get_header(event
);
2438 if (!event_header
) {
2439 fail("Failed to get event header");
2443 integer
= bt_ctf_field_structure_get_field(event_header
,
2446 fail("Failed to get seq_len field from event header");
2450 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2452 fail("Failed to set seq_len value in event header");
2456 /* Populate both sequence integer fields */
2457 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2459 fail("Failed to retrieve sequence from event");
2463 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2465 fail("Failed to set sequence length");
2468 bt_ctf_put(integer
);
2470 for (i
= 0; i
< 2; i
++) {
2471 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2473 fail("Failed to retrieve sequence element");
2477 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2479 fail("Failed to set sequence element value");
2483 bt_ctf_put(integer
);
2487 ret
= bt_ctf_stream_append_event(stream
, event
);
2489 fail("Failed to append event to stream");
2493 ret
= bt_ctf_stream_flush(stream
);
2495 fail("Failed to flush custom_event_header stream");
2501 bt_ctf_put(stream_class
);
2502 bt_ctf_put(event_class
);
2504 bt_ctf_put(integer
);
2505 bt_ctf_put(sequence
);
2506 bt_ctf_put(event_header
);
2507 bt_ctf_put(packet_header
);
2508 bt_ctf_put(sequence_type
);
2509 bt_ctf_put(integer_type
);
2510 bt_ctf_put(event_header_type
);
2513 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2516 struct bt_ctf_trace
*trace
= NULL
;
2517 struct bt_ctf_clock
*clock
= NULL
;
2518 struct bt_ctf_stream_class
*stream_class
= NULL
;
2519 struct bt_ctf_stream
*stream
= NULL
,
2521 struct bt_ctf_event_class
*event_class
= NULL
;
2522 struct bt_ctf_event
*event
= NULL
;
2523 struct bt_ctf_field_type
*integer_type
= NULL
;
2524 struct bt_ctf_field
*integer
= NULL
;
2526 trace
= bt_ctf_writer_get_trace(writer
);
2528 diag("Failed to get trace from writer");
2533 clock
= bt_ctf_trace_get_clock(trace
, 0);
2535 diag("Failed to get clock from trace");
2540 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2541 if (!stream_class
) {
2542 diag("Failed to create stream class");
2547 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2549 diag("Failed to set stream class clock");
2553 event_class
= bt_ctf_event_class_create("some_event_class_name");
2554 integer_type
= bt_ctf_field_type_integer_create(32);
2555 if (!integer_type
) {
2556 diag("Failed to create integer field type");
2561 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2564 diag("Failed to add field to event class");
2568 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2571 diag("Failed to add event class to stream class");
2574 event
= bt_ctf_event_create(event_class
);
2576 diag("Failed to create event");
2581 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2583 diag("Failed to get integer field payload from event");
2588 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2590 diag("Failed to set integer field value");
2594 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2596 diag("Failed to create writer stream");
2601 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2602 "bt_ctf_event_get_stream handles NULL correctly");
2603 ok(bt_ctf_event_get_stream(event
) == NULL
,
2604 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2606 ret
= bt_ctf_stream_append_event(stream
, event
);
2608 diag("Failed to append event to stream");
2612 ret_stream
= bt_ctf_event_get_stream(event
);
2613 ok(ret_stream
== stream
,
2614 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2617 "Create an event before instanciating its associated stream");
2620 bt_ctf_put(ret_stream
);
2621 bt_ctf_put(stream_class
);
2622 bt_ctf_put(event_class
);
2624 bt_ctf_put(integer_type
);
2625 bt_ctf_put(integer
);
2629 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2631 struct bt_ctf_event_class
*event_class
;
2633 assert(event_class
= bt_ctf_event_class_create("Simple Event"));
2634 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2635 "two event classes with the same name cannot cohabit within the same stream class");
2636 bt_ctf_put(event_class
);
2638 event_class
= bt_ctf_event_class_create("different name, ok");
2639 assert(event_class
);
2640 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2641 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2642 "two event classes with the same ID cannot cohabit within the same stream class");
2643 bt_ctf_put(event_class
);
2646 int main(int argc
, char **argv
)
2648 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2649 char metadata_path
[sizeof(trace_path
) + 9];
2650 const char *clock_name
= "test_clock";
2651 const char *clock_description
= "This is a test clock";
2652 const char *returned_clock_name
;
2653 const char *returned_clock_description
;
2654 const uint64_t frequency
= 1123456789;
2655 const uint64_t offset_s
= 1351530929945824323;
2656 const uint64_t offset
= 1234567;
2657 const uint64_t precision
= 10;
2658 const int is_absolute
= 0xFF;
2659 char *metadata_string
;
2660 struct bt_ctf_writer
*writer
;
2661 struct utsname name
;
2662 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2663 struct bt_ctf_clock
*clock
, *ret_clock
;
2664 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2665 struct bt_ctf_stream
*stream1
;
2666 const char *ret_string
;
2667 const unsigned char *ret_uuid
;
2668 unsigned char tmp_uuid
[16] = { 0 };
2669 struct bt_ctf_field_type
*packet_context_type
,
2670 *packet_context_field_type
,
2671 *packet_header_type
,
2672 *packet_header_field_type
,
2674 *stream_event_context_type
,
2676 *event_header_field_type
;
2677 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2678 struct bt_ctf_trace
*trace
;
2680 int64_t ret_int64_t
;
2681 struct bt_value
*obj
;
2684 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2690 if (!mkdtemp(trace_path
)) {
2694 strcpy(metadata_path
, trace_path
);
2695 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2697 writer
= bt_ctf_writer_create(trace_path
);
2698 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2700 ok(!bt_ctf_writer_get_trace(NULL
),
2701 "bt_ctf_writer_get_trace correctly handles NULL");
2702 trace
= bt_ctf_writer_get_trace(writer
);
2704 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2705 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2706 "Set a trace's byte order to big endian");
2707 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2708 "bt_ctf_trace_get_byte_order returns a correct endianness");
2710 /* Add environment context to the trace */
2711 ret
= gethostname(hostname
, sizeof(hostname
));
2715 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2716 "Add host (%s) environment field to writer instance",
2718 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2720 "bt_ctf_writer_add_environment_field error with NULL writer");
2721 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2723 "bt_ctf_writer_add_environment_field error with NULL field name");
2724 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2726 "bt_ctf_writer_add_environment_field error with NULL field value");
2728 /* Test bt_ctf_trace_set_environment_field with an integer object */
2729 obj
= bt_value_integer_create_init(23);
2731 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2732 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2733 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2734 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2735 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2736 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2737 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2738 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2741 /* Test bt_ctf_trace_set_environment_field with a string object */
2742 obj
= bt_value_string_create_init("the value");
2744 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2745 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2748 /* Test bt_ctf_trace_set_environment_field_integer */
2749 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2751 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2752 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2753 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2754 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2756 "bt_ctf_trace_set_environment_field_integer succeeds");
2758 /* Test bt_ctf_trace_set_environment_field_string */
2759 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2761 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2762 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2763 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2764 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2766 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2767 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2769 "bt_ctf_trace_set_environment_field_string succeeds");
2771 /* Test bt_ctf_trace_get_environment_field_count */
2772 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2773 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2774 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2775 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2777 /* Test bt_ctf_trace_get_environment_field_name */
2778 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
2779 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2780 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
2781 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2782 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
2783 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2784 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
2785 ok(ret_string
&& !strcmp(ret_string
, "host"),
2786 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2787 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
2788 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2789 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2790 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
2791 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2792 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2793 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
2794 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2795 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2796 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
2797 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2798 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2800 /* Test bt_ctf_trace_get_environment_field_value */
2801 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
2802 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2803 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
2804 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2805 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
2806 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2807 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
2808 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2809 ok(!ret
&& ret_int64_t
== 23,
2810 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2812 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
2813 ret
= bt_value_string_get(obj
, &ret_string
);
2814 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2815 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2818 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2819 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2821 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2822 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
2823 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2824 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
2825 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2826 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
2828 ret
= bt_value_string_get(obj
, &ret_string
);
2829 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
2830 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2833 /* Test environment field replacement */
2834 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2836 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2837 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2838 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2839 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
2840 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2841 ok(!ret
&& ret_int64_t
== 654321,
2842 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2850 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
2851 == 0, "Add sysname (%s) environment field to writer instance",
2853 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
2854 name
.nodename
) == 0,
2855 "Add nodename (%s) environment field to writer instance",
2857 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
2858 == 0, "Add release (%s) environment field to writer instance",
2860 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
2861 == 0, "Add version (%s) environment field to writer instance",
2863 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
2864 == 0, "Add machine (%s) environment field to writer istance",
2867 /* Define a clock and add it to the trace */
2868 ok(bt_ctf_clock_create("signed") == NULL
,
2869 "Illegal clock name rejected");
2870 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
2871 clock
= bt_ctf_clock_create(clock_name
);
2872 ok(clock
, "Clock created sucessfully");
2873 returned_clock_name
= bt_ctf_clock_get_name(clock
);
2874 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
2875 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
2876 "Returned clock name is valid");
2878 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2879 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
2880 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
2881 "Clock description set successfully");
2883 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2884 ok(returned_clock_description
,
2885 "bt_ctf_clock_get_description returns a description.");
2886 ok(returned_clock_description
?
2887 !strcmp(returned_clock_description
, clock_description
) : 0,
2888 "Returned clock description is valid");
2890 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
2891 "bt_ctf_clock_get_frequency returns the correct default frequency");
2892 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
2893 "Set clock frequency");
2894 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
2895 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
2897 ok(bt_ctf_clock_get_offset_s(clock
) == DEFAULT_CLOCK_OFFSET_S
,
2898 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
2899 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
2900 "Set clock offset (seconds)");
2901 ok(bt_ctf_clock_get_offset_s(clock
) == offset_s
,
2902 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
2904 ok(bt_ctf_clock_get_offset(clock
) == DEFAULT_CLOCK_OFFSET
,
2905 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
2906 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
2907 ok(bt_ctf_clock_get_offset(clock
) == offset
,
2908 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
2910 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
2911 "bt_ctf_clock_get_precision returns the correct default precision");
2912 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
2913 "Set clock precision");
2914 ok(bt_ctf_clock_get_precision(clock
) == precision
,
2915 "bt_ctf_clock_get_precision returns the correct precision once it is set");
2917 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
2918 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
2919 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
2920 "Set clock absolute property");
2921 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
2922 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
2924 ok(bt_ctf_clock_get_time(clock
) == DEFAULT_CLOCK_TIME
,
2925 "bt_ctf_clock_get_time returns the correct default time");
2926 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
2928 ok(bt_ctf_clock_get_time(clock
) == current_time
,
2929 "bt_ctf_clock_get_time returns the correct time once it is set");
2931 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
2932 "Add clock to writer instance");
2933 ok(bt_ctf_writer_add_clock(writer
, clock
),
2934 "Verify a clock can't be added twice to a writer instance");
2936 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
2937 "bt_ctf_trace_get_clock_count correctly handles NULL");
2938 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
2939 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
2940 ok(!bt_ctf_trace_get_clock(NULL
, 0),
2941 "bt_ctf_trace_get_clock correctly handles NULL");
2942 ok(!bt_ctf_trace_get_clock(trace
, -1),
2943 "bt_ctf_trace_get_clock correctly handles negative indexes");
2944 ok(!bt_ctf_trace_get_clock(trace
, 1),
2945 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
2946 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
2947 ok(ret_clock
== clock
,
2948 "bt_ctf_trace_get_clock returns the right clock instance");
2949 bt_ctf_put(ret_clock
);
2950 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
2951 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
2952 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
2953 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
2954 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
2955 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
2956 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
2957 ok(ret_clock
== clock
,
2958 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
2959 bt_ctf_put(ret_clock
);
2960 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
2961 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
2963 ok(!bt_ctf_clock_get_name(NULL
),
2964 "bt_ctf_clock_get_name correctly handles NULL");
2965 ok(!bt_ctf_clock_get_description(NULL
),
2966 "bt_ctf_clock_get_description correctly handles NULL");
2967 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
2968 "bt_ctf_clock_get_frequency correctly handles NULL");
2969 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
2970 "bt_ctf_clock_get_precision correctly handles NULL");
2971 ok(bt_ctf_clock_get_offset_s(NULL
) == -1ULL,
2972 "bt_ctf_clock_get_offset_s correctly handles NULL");
2973 ok(bt_ctf_clock_get_offset(NULL
) == -1ULL,
2974 "bt_ctf_clock_get_offset correctly handles NULL");
2975 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
2976 "bt_ctf_clock_get_is_absolute correctly handles NULL");
2977 ok(bt_ctf_clock_get_time(NULL
) == -1ULL,
2978 "bt_ctf_clock_get_time correctly handles NULL");
2980 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
2981 "bt_ctf_clock_set_description correctly handles NULL clock");
2982 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
2983 "bt_ctf_clock_set_frequency correctly handles NULL clock");
2984 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
2985 "bt_ctf_clock_get_precision correctly handles NULL clock");
2986 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
2987 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
2988 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
2989 "bt_ctf_clock_set_offset correctly handles NULL clock");
2990 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
2991 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
2992 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
2993 "bt_ctf_clock_set_time correctly handles NULL clock");
2994 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
2995 "bt_ctf_clock_get_uuid correctly handles NULL clock");
2996 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
2998 "bt_ctf_clock_get_uuid returns a UUID");
3000 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3001 /* Slightly modify UUID */
3002 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3005 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3006 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3007 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3008 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3009 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3010 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3011 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3013 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3014 ok(uuid_match(ret_uuid
, tmp_uuid
),
3015 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3017 /* Define a stream class */
3018 stream_class
= bt_ctf_stream_class_create("test_stream");
3020 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3021 "bt_ctf_stream_class_get_name handles NULL correctly");
3022 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3023 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3024 "bt_ctf_stream_class_get_name returns a correct stream class name");
3026 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3027 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3028 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3029 "bt_ctf_stream_class_get_clock handles NULL correctly");
3031 ok(stream_class
, "Create stream class");
3032 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3033 "Set a stream class' clock");
3034 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3035 ok(ret_clock
== clock
,
3036 "bt_ctf_stream_class_get_clock returns a correct clock");
3037 bt_ctf_put(ret_clock
);
3039 /* Test the event fields and event types APIs */
3042 /* Test fields copying */
3045 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3046 "bt_ctf_stream_class_get_id returns an error when no id is set");
3047 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3048 "bt_ctf_stream_class_get_id handles NULL correctly");
3049 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3050 "bt_ctf_stream_class_set_id handles NULL correctly");
3051 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3052 "Set an stream class' id");
3053 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3054 "bt_ctf_stream_class_get_id returns the correct value");
3056 /* Validate default event header fields */
3057 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3058 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3059 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3062 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3063 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == CTF_TYPE_STRUCT
,
3064 "Default event header type is a structure");
3065 event_header_field_type
=
3066 bt_ctf_field_type_structure_get_field_type_by_name(
3067 ret_field_type
, "id");
3068 ok(event_header_field_type
,
3069 "Default event header type contains an \"id\" field");
3070 ok(bt_ctf_field_type_get_type_id(
3071 event_header_field_type
) == CTF_TYPE_INTEGER
,
3072 "Default event header \"id\" field is an integer");
3073 bt_ctf_put(event_header_field_type
);
3074 event_header_field_type
=
3075 bt_ctf_field_type_structure_get_field_type_by_name(
3076 ret_field_type
, "timestamp");
3077 ok(event_header_field_type
,
3078 "Default event header type contains a \"timestamp\" field");
3079 ok(bt_ctf_field_type_get_type_id(
3080 event_header_field_type
) == CTF_TYPE_INTEGER
,
3081 "Default event header \"timestamp\" field is an integer");
3082 bt_ctf_put(event_header_field_type
);
3083 bt_ctf_put(ret_field_type
);
3085 /* Add a custom trace packet header field */
3086 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3087 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3088 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3089 ok(packet_header_type
,
3090 "bt_ctf_trace_get_packet_header_type returns a packet header");
3091 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == CTF_TYPE_STRUCT
,
3092 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3093 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3094 packet_header_type
, "magic");
3095 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3096 bt_ctf_put(ret_field_type
);
3097 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3098 packet_header_type
, "uuid");
3099 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3100 bt_ctf_put(ret_field_type
);
3101 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3102 packet_header_type
, "stream_id");
3103 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3104 bt_ctf_put(ret_field_type
);
3106 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3107 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3108 packet_header_field_type
, "custom_trace_packet_header_field"),
3109 "Added a custom trace packet header field successfully");
3111 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3112 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3113 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3114 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3115 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3116 "Set a trace packet_header_type successfully");
3118 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3119 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3121 /* Add a custom field to the stream class' packet context */
3122 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3123 ok(packet_context_type
,
3124 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3125 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == CTF_TYPE_STRUCT
,
3126 "Packet context is a structure");
3128 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3129 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3130 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3131 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3133 integer_type
= bt_ctf_field_type_integer_create(32);
3134 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3136 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3137 /* Create a "uint5_t" equivalent custom packet context field */
3138 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3140 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3141 packet_context_field_type
, "custom_packet_context_field");
3142 ok(ret
== 0, "Packet context field added successfully");
3144 /* Define a stream event context containing a my_integer field. */
3145 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3146 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3147 ok(bt_ctf_stream_class_get_event_context_type(
3148 stream_class
) == NULL
,
3149 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3150 stream_event_context_type
= bt_ctf_field_type_structure_create();
3151 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3152 integer_type
, "common_event_context");
3154 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3155 stream_event_context_type
) < 0,
3156 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3157 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3159 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3160 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3162 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3164 ok(bt_ctf_stream_class_set_event_context_type(
3165 stream_class
, stream_event_context_type
) == 0,
3166 "Set a new stream event context type");
3167 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3169 ok(ret_field_type
== stream_event_context_type
,
3170 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3171 bt_ctf_put(ret_field_type
);
3173 /* Instantiate a stream and append events */
3174 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3175 ok(stream1
, "Instanciate a stream class from writer");
3177 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3178 "bt_ctf_stream_get_class correctly handles NULL");
3179 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3180 ok(ret_stream_class
,
3181 "bt_ctf_stream_get_class returns a stream class");
3182 ok(ret_stream_class
== stream_class
,
3183 "Returned stream class is of the correct type");
3186 * Try to modify the packet context type after a stream has been
3189 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3190 packet_header_field_type
, "should_fail");
3192 "Trace packet header type can't be modified once a stream has been instanciated");
3195 * Try to modify the packet context type after a stream has been
3198 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3199 packet_context_field_type
, "should_fail");
3201 "Packet context type can't be modified once a stream has been instanciated");
3204 * Try to modify the stream event context type after a stream has been
3207 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3208 integer_type
, "should_fail");
3210 "Stream event context type can't be modified once a stream has been instanciated");
3212 /* Should fail after instanciating a stream (frozen) */
3213 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3214 "Changes to a stream class that was already instantiated fail");
3216 /* Populate the custom packet header field only once for all tests */
3217 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3218 "bt_ctf_stream_get_packet_header handles NULL correctly");
3219 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3221 "bt_ctf_stream_get_packet_header returns a packet header");
3222 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3223 ok(ret_field_type
== packet_header_type
,
3224 "Stream returns a packet header of the appropriate type");
3225 bt_ctf_put(ret_field_type
);
3226 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3227 "custom_trace_packet_header_field");
3228 ok(packet_header_field
,
3229 "Packet header structure contains a custom field with the appropriate name");
3230 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3231 ok(ret_field_type
== packet_header_field_type
,
3232 "Custom packet header field is of the expected type");
3233 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3234 54321), "Set custom packet header value successfully");
3235 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3236 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3237 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3238 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3239 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3240 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3241 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3242 "Successfully set a stream's packet header");
3244 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3245 "Add environment field to writer after stream creation");
3247 test_instanciate_event_before_stream(writer
);
3249 append_simple_event(stream_class
, stream1
, clock
);
3251 packet_resize_test(stream_class
, stream1
, clock
);
3253 append_complex_event(stream_class
, stream1
, clock
);
3255 append_existing_event_class(stream_class
);
3257 test_empty_stream(writer
);
3259 test_custom_event_header_stream(writer
);
3261 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3262 ok(metadata_string
, "Get metadata string");
3264 bt_ctf_writer_flush_metadata(writer
);
3265 validate_metadata(argv
[1], metadata_path
);
3266 validate_trace(argv
[2], trace_path
);
3269 bt_ctf_put(ret_stream_class
);
3271 bt_ctf_put(stream1
);
3272 bt_ctf_put(packet_context_type
);
3273 bt_ctf_put(packet_context_field_type
);
3274 bt_ctf_put(integer_type
);
3275 bt_ctf_put(stream_event_context_type
);
3276 bt_ctf_put(ret_field_type
);
3277 bt_ctf_put(packet_header_type
);
3278 bt_ctf_put(packet_header_field_type
);
3279 bt_ctf_put(packet_header
);
3280 bt_ctf_put(packet_header_field
);
3282 free(metadata_string
);
3284 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
3285 "bt_ctf_stream_class_get_trace returns NULL after its trace has been reclaimed");
3286 bt_ctf_put(stream_class
);
3288 /* Remove all trace files and delete temporary trace directory */
3289 DIR *trace_dir
= opendir(trace_path
);
3291 perror("# opendir");
3295 struct dirent
*entry
;
3296 while ((entry
= readdir(trace_dir
))) {
3297 if (entry
->d_type
== DT_REG
) {
3298 unlinkat(dirfd(trace_dir
), entry
->d_name
, 0);
3303 closedir(trace_dir
);