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/events.h>
31 #include <babeltrace/objects.h>
35 #include <sys/utsname.h>
36 #include <babeltrace/compat/limits.h>
46 #define METADATA_LINE_SIZE 512
47 #define SEQUENCE_TEST_LENGTH 10
48 #define ARRAY_TEST_LENGTH 5
49 #define PACKET_RESIZE_TEST_LENGTH 100000
51 #define DEFAULT_CLOCK_FREQ 1000000000
52 #define DEFAULT_CLOCK_PRECISION 1
53 #define DEFAULT_CLOCK_OFFSET 0
54 #define DEFAULT_CLOCK_OFFSET_S 0
55 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
56 #define DEFAULT_CLOCK_TIME 0
58 static uint64_t current_time
= 42;
60 /* Return 1 if uuids match, zero if different. */
61 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
66 if (!uuid_a
|| !uuid_b
) {
70 for (i
= 0; i
< 16; i
++) {
71 if (uuid_a
[i
] != uuid_b
[i
]) {
81 void validate_metadata(char *parser_path
, char *metadata_path
)
84 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
85 int parser_output_fd
= -1, metadata_fd
= -1;
92 parser_output_fd
= mkstemp(parser_output_path
);
93 metadata_fd
= open(metadata_path
, O_RDONLY
);
95 unlink(parser_output_path
);
97 if (parser_output_fd
== -1 || metadata_fd
== -1) {
98 diag("Failed create temporary files for metadata parsing.");
106 waitpid(pid
, &status
, 0);
107 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
109 /* ctf-parser-test expects a metadata string on stdin. */
110 ret
= dup2(metadata_fd
, STDIN_FILENO
);
112 perror("# dup2 metadata_fd to STDIN");
116 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
118 perror("# dup2 parser_output_fd to STDOUT");
122 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
124 perror("# dup2 parser_output_fd to STDERR");
128 execl(parser_path
, "ctf-parser-test", NULL
);
129 perror("# Could not launch the ctf metadata parser process");
133 ok(ret
== 0, "Metadata string is valid");
135 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
137 size_t len
= METADATA_LINE_SIZE
;
138 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
140 metadata_fp
= fdopen(metadata_fd
, "r");
142 perror("fdopen on metadata_fd");
147 parser_output_fp
= fdopen(parser_output_fd
, "r");
148 if (!parser_output_fp
) {
149 perror("fdopen on parser_output_fd");
152 parser_output_fd
= -1;
156 diag("malloc failure");
161 /* Output the metadata and parser output as diagnostic */
162 while (getline(&line
, &len
, metadata_fp
) > 0) {
163 fprintf(stderr
, "# %s", line
);
166 rewind(parser_output_fp
);
167 while (getline(&line
, &len
, parser_output_fp
) > 0) {
168 fprintf(stderr
, "# %s", line
);
174 if (fclose(metadata_fp
)) {
175 diag("fclose failure");
178 if (parser_output_fp
) {
179 if (fclose(parser_output_fp
)) {
180 diag("fclose failure");
185 if (parser_output_fd
>= 0) {
186 if (close(parser_output_fd
)) {
190 if (metadata_fd
>= 0) {
191 if (close(metadata_fd
)) {
197 void validate_trace(char *parser_path
, char *trace_path
)
200 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
201 int babeltrace_output_fd
= -1;
208 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
209 unlink(babeltrace_output_path
);
211 if (babeltrace_output_fd
== -1) {
212 diag("Failed to create a temporary file for trace parsing.");
220 waitpid(pid
, &status
, 0);
221 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
223 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
225 perror("# dup2 babeltrace_output_fd to STDOUT");
229 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
231 perror("# dup2 babeltrace_output_fd to STDERR");
235 execl(parser_path
, "babeltrace", trace_path
, NULL
);
236 perror("# Could not launch the babeltrace process");
240 ok(ret
== 0, "Babeltrace could read the resulting trace");
242 if (ret
&& babeltrace_output_fd
>= 0) {
244 size_t len
= METADATA_LINE_SIZE
;
245 FILE *babeltrace_output_fp
= NULL
;
247 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
248 if (!babeltrace_output_fp
) {
249 perror("fdopen on babeltrace_output_fd");
252 babeltrace_output_fd
= -1;
256 diag("malloc error");
258 rewind(babeltrace_output_fp
);
259 while (getline(&line
, &len
, babeltrace_output_fp
) > 0) {
265 if (babeltrace_output_fp
) {
266 if (fclose(babeltrace_output_fp
)) {
267 diag("fclose error");
272 if (babeltrace_output_fd
>= 0) {
273 if (close(babeltrace_output_fd
)) {
279 void event_copy_tests(struct bt_ctf_event
*event
)
281 struct bt_ctf_event
*copy
;
282 struct bt_ctf_event_class
*orig_event_class
;
283 struct bt_ctf_event_class
*copy_event_class
;
284 struct bt_ctf_stream
*orig_stream
;
285 struct bt_ctf_stream
*copy_stream
;
286 struct bt_ctf_field
*orig_field
;
287 struct bt_ctf_field
*copy_field
;
290 ok(!bt_ctf_event_copy(NULL
),
291 "bt_ctf_event_copy handles NULL correctly");
292 copy
= bt_ctf_event_copy(event
);
293 ok(copy
, "bt_ctf_event_copy returns a valid pointer");
295 /* validate event class */
296 orig_event_class
= bt_ctf_event_get_class(event
);
297 assert(orig_event_class
);
298 copy_event_class
= bt_ctf_event_get_class(copy
);
299 ok(orig_event_class
== copy_event_class
,
300 "original and copied events share the same event class pointer");
301 bt_ctf_event_class_put(orig_event_class
);
302 bt_ctf_event_class_put(copy_event_class
);
304 /* validate stream */
305 orig_stream
= bt_ctf_event_get_stream(event
);
306 copy_stream
= bt_ctf_event_get_stream(copy
);
309 ok(!copy_stream
, "original and copied events have no stream");
311 ok(orig_stream
== copy_stream
,
312 "original and copied events share the same stream pointer");
314 bt_ctf_stream_put(orig_stream
);
315 bt_ctf_stream_put(copy_stream
);
318 orig_field
= bt_ctf_event_get_header(event
);
319 copy_field
= bt_ctf_event_get_header(copy
);
322 ok(!copy_field
, "original and copied events have no header");
324 ok(orig_field
!= copy_field
,
325 "original and copied events headers are different pointers");
328 bt_ctf_field_put(orig_field
);
329 bt_ctf_field_put(copy_field
);
332 orig_field
= bt_ctf_event_get_event_context(event
);
333 copy_field
= bt_ctf_event_get_event_context(copy
);
336 ok(!copy_field
, "original and copied events have no context");
338 ok(orig_field
!= copy_field
,
339 "original and copied events contexts are different pointers");
342 bt_ctf_field_put(orig_field
);
343 bt_ctf_field_put(copy_field
);
346 orig_field
= bt_ctf_event_get_payload_field(event
);
347 copy_field
= bt_ctf_event_get_payload_field(copy
);
350 ok(!copy_field
, "original and copied events have no payload");
352 ok(orig_field
!= copy_field
,
353 "original and copied events payloads are different pointers");
356 bt_ctf_field_put(orig_field
);
357 bt_ctf_field_put(copy_field
);
359 bt_ctf_event_put(copy
);
362 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
363 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
365 /* Create and add a simple event class */
366 struct bt_ctf_event_class
*simple_event_class
=
367 bt_ctf_event_class_create("Simple Event");
368 struct bt_ctf_field_type
*uint_12_type
=
369 bt_ctf_field_type_integer_create(12);
370 struct bt_ctf_field_type
*int_64_type
=
371 bt_ctf_field_type_integer_create(64);
372 struct bt_ctf_field_type
*float_type
=
373 bt_ctf_field_type_floating_point_create();
374 struct bt_ctf_field_type
*enum_type
;
375 struct bt_ctf_field_type
*enum_type_unsigned
=
376 bt_ctf_field_type_enumeration_create(uint_12_type
);
377 struct bt_ctf_field_type
*event_context_type
=
378 bt_ctf_field_type_structure_create();
379 struct bt_ctf_field_type
*returned_type
;
380 struct bt_ctf_event
*simple_event
;
381 struct bt_ctf_field
*integer_field
;
382 struct bt_ctf_field
*float_field
;
383 struct bt_ctf_field
*enum_field
;
384 struct bt_ctf_field
*enum_field_unsigned
;
385 struct bt_ctf_field
*enum_container_field
;
386 const char *mapping_name_test
= "truie";
387 const double double_test_value
= 3.1415;
388 struct bt_ctf_field
*enum_container_field_unsigned
;
389 const char *mapping_name_negative_test
= "negative_value";
390 const char *ret_char
;
392 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
393 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
394 struct bt_ctf_clock
*ret_clock
;
395 struct bt_ctf_event_class
*ret_event_class
;
396 struct bt_ctf_field
*packet_context
;
397 struct bt_ctf_field
*packet_context_field
;
398 struct bt_ctf_field
*stream_event_context
;
399 struct bt_ctf_field
*stream_event_context_field
;
400 struct bt_ctf_field
*event_context
;
401 struct bt_ctf_field
*event_context_field
;
403 ok(uint_12_type
, "Create an unsigned integer type");
405 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
406 ok(int_64_type
, "Create a signed integer type");
407 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
409 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
410 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
411 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
412 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
413 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
414 bt_ctf_field_type_put(returned_type
);
416 bt_ctf_field_type_set_alignment(float_type
, 32);
417 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
418 "bt_ctf_field_type_get_alignment handles NULL correctly");
419 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
420 "bt_ctf_field_type_get_alignment returns a correct value");
422 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
423 "Set a floating point type's exponent digit count");
424 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
425 "Set a floating point type's mantissa digit count");
427 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
428 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
429 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
430 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
431 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
432 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
433 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
434 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
436 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
437 mapping_name_negative_test
, -12345, 0) == 0,
438 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
439 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
440 "escaping; \"test\"", 1, 1) == 0,
441 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
442 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
443 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
444 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
445 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
446 "event clock int float", 5, 22) == 0,
447 "Accept enumeration mapping strings containing reserved keywords");
448 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
450 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
451 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
452 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
453 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
454 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
455 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
456 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
458 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
459 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
460 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
461 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
462 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
463 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
465 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
466 "enum_field") == 0, "Add signed enumeration field to event");
468 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
469 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
470 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
471 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
472 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
473 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
474 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
475 NULL
, &ret_range_end_int64_t
) < 0,
476 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
477 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
478 &ret_range_start_int64_t
, NULL
) < 0,
479 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
480 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
481 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
482 "bt_ctf_field_type_enumeration_get_mapping returns a value");
483 ok(!strcmp(ret_char
, mapping_name_test
),
484 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
485 ok(ret_range_start_int64_t
== 42,
486 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
487 ok(ret_range_end_int64_t
== 42,
488 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
490 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
491 "escaping; \"test\"", 0, 0) == 0,
492 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
493 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
494 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
495 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
496 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
497 "event clock int float", 5, 22) == 0,
498 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
499 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
501 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
502 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
503 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
504 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
505 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
506 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
507 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
508 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
510 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
511 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
512 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
513 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
515 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
516 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
517 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
518 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
519 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
520 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
521 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
522 NULL
, &ret_range_end_uint64_t
) < 0,
523 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
524 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
525 &ret_range_start_uint64_t
, NULL
) < 0,
526 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
527 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
528 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
529 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
530 ok(!strcmp(ret_char
, mapping_name_test
),
531 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
532 ok(ret_range_start_uint64_t
== 42,
533 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
534 ok(ret_range_end_uint64_t
== 42,
535 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
537 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
539 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
542 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
544 /* Set an event context type which will contain a single integer*/
545 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
546 "event_specific_context"),
547 "Add event specific context field");
548 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
549 "bt_ctf_event_class_get_context_type handles NULL correctly");
550 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
551 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
553 ok(bt_ctf_event_class_set_context_type(simple_event_class
, NULL
) < 0,
554 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
555 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
556 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
557 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
558 "Set an event class' context type successfully");
559 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
560 ok(returned_type
== event_context_type
,
561 "bt_ctf_event_class_get_context_type returns the appropriate type");
562 bt_ctf_field_type_put(returned_type
);
564 bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
);
566 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
567 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
568 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
569 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
570 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
571 "bt_ctf_stream_class_get_event_class handles NULL correctly");
572 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
573 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
574 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
575 ok(ret_event_class
== simple_event_class
,
576 "bt_ctf_stream_class_get_event_class returns the correct event class");
577 bt_ctf_event_class_put(ret_event_class
);
578 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
579 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
580 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
581 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
583 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
584 ok(ret_event_class
== simple_event_class
,
585 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
586 bt_ctf_event_class_put(ret_event_class
);
588 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
589 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
590 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
591 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
592 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
593 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
594 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
595 ok(ret_event_class
== simple_event_class
,
596 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
597 bt_ctf_event_class_put(ret_event_class
);
599 simple_event
= bt_ctf_event_create(simple_event_class
);
601 "Instantiate an event containing a single integer field");
603 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
604 "bt_ctf_event_get_clock handles NULL correctly");
605 ret_clock
= bt_ctf_event_get_clock(simple_event
);
606 ok(ret_clock
== clock
,
607 "bt_ctf_event_get_clock returns a correct clock");
608 bt_ctf_clock_put(clock
);
610 integer_field
= bt_ctf_field_create(uint_12_type
);
611 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
612 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
613 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
615 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
616 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
617 "bt_ctf_field_floating_point_get_value fails on an unset float field");
618 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
619 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
620 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
621 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
622 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
623 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
624 "bt_ctf_field_floating_point_get_value returns a double value");
625 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
626 "bt_ctf_field_floating_point_get_value returns a correct value");
628 enum_field
= bt_ctf_field_create(enum_type
);
629 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
630 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
631 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
632 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
633 enum_container_field
= bt_ctf_field_enumeration_get_container(
635 ok(bt_ctf_field_signed_integer_set_value(
636 enum_container_field
, -42) == 0,
637 "Set signed enumeration container value");
638 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
639 ok(!strcmp(ret_char
, mapping_name_negative_test
),
640 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
641 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
643 enum_field_unsigned
= bt_ctf_field_create(enum_type_unsigned
);
644 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
645 enum_field_unsigned
);
646 ok(bt_ctf_field_unsigned_integer_set_value(
647 enum_container_field_unsigned
, 42) == 0,
648 "Set unsigned enumeration container value");
649 bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
650 enum_field_unsigned
);
651 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
652 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
653 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
655 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
657 /* Populate stream event context */
658 stream_event_context
= bt_ctf_stream_get_event_context(stream
);
659 stream_event_context_field
= bt_ctf_field_structure_get_field(
660 stream_event_context
, "common_event_context");
661 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
663 /* Populate the event's context */
664 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
665 "bt_ctf_event_get_event_context handles NULL correctly");
666 event_context
= bt_ctf_event_get_event_context(simple_event
);
668 "bt_ctf_event_get_event_context returns a field");
669 returned_type
= bt_ctf_field_get_type(event_context
);
670 ok(returned_type
== event_context_type
,
671 "bt_ctf_event_get_event_context returns a field of the appropriate type");
672 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
673 "event_specific_context");
674 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
675 "Successfully set an event context's value");
676 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
677 "bt_ctf_event_set_event_context handles a NULL event correctly");
678 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
679 "bt_ctf_event_set_event_context handles a NULL event context correctly");
680 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
681 "bt_ctf_event_set_event_context rejects a context of the wrong type");
682 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
683 "Set an event context successfully");
685 event_copy_tests(simple_event
);
686 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
687 "Append simple event to trace stream");
689 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
690 "bt_ctf_stream_get_packet_context handles NULL correctly");
691 packet_context
= bt_ctf_stream_get_packet_context(stream
);
693 "bt_ctf_stream_get_packet_context returns a packet context");
695 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
697 ok(packet_context_field
,
698 "Packet context contains the default packet_size field.");
699 bt_ctf_field_put(packet_context_field
);
700 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
701 "custom_packet_context_field");
702 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
703 "Custom packet context field value successfully set.");
705 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
706 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
707 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
708 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
709 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
710 "Successfully set a stream's packet context");
712 ok(bt_ctf_stream_flush(stream
) == 0,
713 "Flush trace stream with one event");
715 bt_ctf_event_class_put(simple_event_class
);
716 bt_ctf_event_put(simple_event
);
717 bt_ctf_field_type_put(uint_12_type
);
718 bt_ctf_field_type_put(int_64_type
);
719 bt_ctf_field_type_put(float_type
);
720 bt_ctf_field_type_put(enum_type
);
721 bt_ctf_field_type_put(enum_type_unsigned
);
722 bt_ctf_field_type_put(returned_type
);
723 bt_ctf_field_type_put(event_context_type
);
724 bt_ctf_field_put(integer_field
);
725 bt_ctf_field_put(float_field
);
726 bt_ctf_field_put(enum_field
);
727 bt_ctf_field_put(enum_field_unsigned
);
728 bt_ctf_field_put(enum_container_field
);
729 bt_ctf_field_put(enum_container_field_unsigned
);
730 bt_ctf_field_put(packet_context
);
731 bt_ctf_field_put(packet_context_field
);
732 bt_ctf_field_put(stream_event_context
);
733 bt_ctf_field_put(stream_event_context_field
);
734 bt_ctf_field_put(event_context
);
735 bt_ctf_field_put(event_context_field
);
738 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
739 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
741 struct event_class_attrs_counts
{
752 struct event_class_attrs_counts
;
753 const char *complex_test_event_string
= "Complex Test Event";
754 const char *test_string_1
= "Test ";
755 const char *test_string_2
= "string ";
756 const char *test_string_3
= "abcdefghi";
757 const char *test_string_4
= "abcd\0efg\0hi";
758 const char *test_string_cat
= "Test string abcdeabcd";
759 struct bt_ctf_field_type
*uint_35_type
=
760 bt_ctf_field_type_integer_create(35);
761 struct bt_ctf_field_type
*int_16_type
=
762 bt_ctf_field_type_integer_create(16);
763 struct bt_ctf_field_type
*uint_3_type
=
764 bt_ctf_field_type_integer_create(3);
765 struct bt_ctf_field_type
*enum_variant_type
=
766 bt_ctf_field_type_enumeration_create(uint_3_type
);
767 struct bt_ctf_field_type
*variant_type
=
768 bt_ctf_field_type_variant_create(enum_variant_type
,
770 struct bt_ctf_field_type
*string_type
=
771 bt_ctf_field_type_string_create();
772 struct bt_ctf_field_type
*sequence_type
;
773 struct bt_ctf_field_type
*array_type
;
774 struct bt_ctf_field_type
*inner_structure_type
=
775 bt_ctf_field_type_structure_create();
776 struct bt_ctf_field_type
*complex_structure_type
=
777 bt_ctf_field_type_structure_create();
778 struct bt_ctf_field_type
*ret_field_type
;
779 struct bt_ctf_event_class
*event_class
;
780 struct bt_ctf_event
*event
;
781 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
782 *inner_structure_field
, *complex_structure_field
,
783 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
784 *variant_field
, *an_array_field
, *ret_field
;
785 uint64_t ret_unsigned_int
;
786 int64_t ret_signed_int
;
787 const char *ret_string
;
788 struct bt_ctf_stream_class
*ret_stream_class
;
789 struct bt_ctf_event_class
*ret_event_class
;
790 struct bt_ctf_field
*packet_context
, *packet_context_field
;
791 struct bt_object
*obj
;
793 bt_ctf_field_type_set_alignment(int_16_type
, 32);
794 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
795 bt_ctf_field_type_integer_set_base(uint_35_type
,
796 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
798 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
799 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
802 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
803 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
804 ret_field_type
= bt_ctf_field_type_array_get_element_type(
806 ok(ret_field_type
== int_16_type
,
807 "bt_ctf_field_type_array_get_element_type returns the correct type");
808 bt_ctf_field_type_put(ret_field_type
);
810 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
811 "bt_ctf_field_type_array_get_length handles NULL correctly");
812 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
813 "bt_ctf_field_type_array_get_length returns the correct length");
815 bt_ctf_field_type_structure_add_field(inner_structure_type
,
816 uint_35_type
, "seq_len");
817 bt_ctf_field_type_structure_add_field(inner_structure_type
,
818 sequence_type
, "a_sequence");
819 bt_ctf_field_type_structure_add_field(inner_structure_type
,
820 array_type
, "an_array");
822 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
824 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
826 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
827 "UINT35_TYPE", 2, 7);
829 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
831 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
832 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
833 enum_variant_type
, NULL
) < 0,
834 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
835 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
836 enum_variant_type
, "INT16_TYPE") == 1,
837 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
839 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
840 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
841 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
842 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
843 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
844 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
846 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
847 "An unknown entry"), "Reject a variant field based on an unknown tag value");
848 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
849 "UINT3_TYPE") == 0, "Add a field to a variant");
850 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
852 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
855 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
856 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
857 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
858 ok(ret_field_type
== enum_variant_type
,
859 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
860 bt_ctf_field_type_put(ret_field_type
);
862 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
863 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
864 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
865 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
866 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
867 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
868 "INT16_TYPE") == NULL
,
869 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
870 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
872 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
873 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
874 variant_type
, "INT16_TYPE");
875 ok(ret_field_type
== int_16_type
,
876 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
877 bt_ctf_field_type_put(ret_field_type
);
879 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
880 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
881 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
882 "bt_ctf_field_type_variant_get_field_count returns the correct count");
884 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
885 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
886 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
887 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
888 bt_ctf_field_type_put(ret_field_type
);
889 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
890 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
891 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
892 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
893 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
894 "bt_ctf_field_type_variant_get_field returns a field");
895 ok(!strcmp("INT16_TYPE", ret_string
),
896 "bt_ctf_field_type_variant_get_field returns a correct field name");
897 ok(ret_field_type
== int_16_type
,
898 "bt_ctf_field_type_variant_get_field returns a correct field type");
899 bt_ctf_field_type_put(ret_field_type
);
901 bt_ctf_field_type_structure_add_field(complex_structure_type
,
902 enum_variant_type
, "variant_selector");
903 bt_ctf_field_type_structure_add_field(complex_structure_type
,
904 string_type
, "a_string");
905 bt_ctf_field_type_structure_add_field(complex_structure_type
,
906 variant_type
, "variant_value");
907 bt_ctf_field_type_structure_add_field(complex_structure_type
,
908 inner_structure_type
, "inner_structure");
910 ok(bt_ctf_event_class_create("clock") == NULL
,
911 "Reject creation of an event class with an illegal name");
912 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
913 ok(event_class
, "Create an event class");
914 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
915 "Reject addition of a field with an empty name to an event");
916 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
917 "Reject addition of a field with a NULL type to an event");
918 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
920 "Reject addition of a type with an illegal name to an event");
921 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
923 "Add field of type unsigned integer to an event");
924 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
925 "int_16") == 0, "Add field of type signed integer to an event");
926 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
927 "complex_structure") == 0,
928 "Add composite structure to an event");
930 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
931 "bt_ctf_event_class_get_name handles NULL correctly");
932 ret_string
= bt_ctf_event_class_get_name(event_class
);
933 ok(!strcmp(ret_string
, complex_test_event_string
),
934 "bt_ctf_event_class_get_name returns a correct name");
935 ok(bt_ctf_event_class_get_id(event_class
) < 0,
936 "bt_ctf_event_class_get_id returns a negative value when not set");
937 ok(bt_ctf_event_class_get_id(NULL
) < 0,
938 "bt_ctf_event_class_get_id handles NULL correctly");
939 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
940 "bt_ctf_event_class_set_id handles NULL correctly");
941 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
942 "Set an event class' id");
943 ok(bt_ctf_event_class_get_id(event_class
) == 42,
944 "bt_ctf_event_class_get_id returns the correct value");
946 /* Test event class attributes */
947 assert(obj
= bt_object_integer_create_init(15));
948 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
949 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
950 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
951 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
952 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
953 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
954 assert(!bt_object_integer_set(obj
, -3));
955 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
956 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
957 assert(!bt_object_integer_set(obj
, 11));
958 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
959 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
960 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
961 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
962 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
964 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
967 obj
= bt_object_integer_create_init(5);
969 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
970 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
972 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
973 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
974 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
975 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
976 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
977 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
978 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
981 ret
= bt_object_integer_get(obj
, &int64_value
);
982 ok(obj
&& !ret
&& int64_value
== 5,
983 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
986 assert(obj
= bt_object_string_create_init("nu name"));
987 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
988 ret_string
= bt_ctf_event_class_get_name(event_class
);
989 ok(!strcmp(ret_string
, "nu name"),
990 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
991 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
992 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
994 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
996 obj
= bt_object_string_create_init("http://kernel.org/");
998 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
1001 ok(bt_ctf_event_class_get_attribute_count(NULL
),
1002 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1003 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
1004 "bt_ctf_event_class_get_attribute_count returns the correct count");
1005 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
1006 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1007 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
1008 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1009 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
1010 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1011 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
1012 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1014 memset(&attrs_count
, 0, sizeof(attrs_count
));
1016 for (i
= 0; i
< 4; ++i
) {
1017 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
1019 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
1020 assert(ret_string
&& obj
);
1022 if (!strcmp(ret_string
, "id")) {
1024 ok(bt_object_is_integer(obj
),
1025 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1027 } else if (!strcmp(ret_string
, "name")) {
1029 ok(bt_object_is_string(obj
),
1030 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1032 } else if (!strcmp(ret_string
, "loglevel")) {
1033 attrs_count
.loglevel
++;
1034 ok(bt_object_is_integer(obj
),
1035 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1037 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1038 attrs_count
.modelemfuri
++;
1039 ok(bt_object_is_string(obj
),
1040 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1043 attrs_count
.unknown
++;
1049 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1050 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1051 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1052 "event class has one instance of each known attribute");
1054 /* Add event class to the stream class */
1055 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1056 "Reject addition of NULL event class to a stream class");
1057 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1058 event_class
) == 0, "Add an event class to stream class");
1060 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1061 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1062 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1063 ok(ret_stream_class
== stream_class
,
1064 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1065 bt_ctf_stream_class_put(ret_stream_class
);
1067 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1068 "bt_ctf_event_class_get_field_count handles NULL correctly");
1069 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1070 "bt_ctf_event_class_get_field_count returns a correct value");
1072 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1073 &ret_field_type
, 0) < 0,
1074 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1075 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1076 &ret_field_type
, 0) == 0,
1077 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1078 bt_ctf_field_type_put(ret_field_type
);
1079 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1081 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1082 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1083 &ret_field_type
, 42) < 0,
1084 "bt_ctf_event_class_get_field handles an invalid index correctly");
1085 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1086 &ret_field_type
, 0) == 0,
1087 "bt_ctf_event_class_get_field returns a field");
1088 ok(ret_field_type
== uint_35_type
,
1089 "bt_ctf_event_class_get_field returns a correct field type");
1090 bt_ctf_field_type_put(ret_field_type
);
1091 ok(!strcmp(ret_string
, "uint_35"),
1092 "bt_ctf_event_class_get_field returns a correct field name");
1093 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1094 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1095 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1096 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1097 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1098 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1099 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1100 "complex_structure");
1101 ok(ret_field_type
== complex_structure_type
,
1102 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1103 bt_ctf_field_type_put(ret_field_type
);
1105 event
= bt_ctf_event_create(event_class
);
1106 ok(event
, "Instanciate a complex event");
1108 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1109 "bt_ctf_event_get_class handles NULL correctly");
1110 ret_event_class
= bt_ctf_event_get_class(event
);
1111 ok(ret_event_class
== event_class
,
1112 "bt_ctf_event_get_class returns the correct event class");
1113 bt_ctf_event_class_put(ret_event_class
);
1115 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1116 if (!uint_35_field
) {
1117 printf("uint_35_field is NULL\n");
1120 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1121 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1122 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1123 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1124 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1125 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1126 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1127 &ret_unsigned_int
) == 0,
1128 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1129 ok(ret_unsigned_int
== 0x0DDF00D,
1130 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1131 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1132 &ret_signed_int
) < 0,
1133 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1134 bt_ctf_field_put(uint_35_field
);
1136 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1137 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1138 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1139 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1140 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1141 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1142 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1143 &ret_signed_int
) == 0,
1144 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1145 ok(ret_signed_int
== -12345,
1146 "bt_ctf_field_signed_integer_get_value returns the correct value");
1147 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1148 &ret_unsigned_int
) < 0,
1149 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1150 bt_ctf_field_put(int_16_field
);
1152 complex_structure_field
= bt_ctf_event_get_payload(event
,
1153 "complex_structure");
1155 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1156 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1157 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1158 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1159 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1160 complex_structure_field
, 3);
1161 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1162 bt_ctf_field_put(inner_structure_field
);
1163 ok(ret_field_type
== inner_structure_type
,
1164 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1165 bt_ctf_field_type_put(ret_field_type
);
1167 inner_structure_field
= bt_ctf_field_structure_get_field(
1168 complex_structure_field
, "inner_structure");
1169 a_string_field
= bt_ctf_field_structure_get_field(
1170 complex_structure_field
, "a_string");
1171 enum_variant_field
= bt_ctf_field_structure_get_field(
1172 complex_structure_field
, "variant_selector");
1173 variant_field
= bt_ctf_field_structure_get_field(
1174 complex_structure_field
, "variant_value");
1175 uint_35_field
= bt_ctf_field_structure_get_field(
1176 inner_structure_field
, "seq_len");
1177 a_sequence_field
= bt_ctf_field_structure_get_field(
1178 inner_structure_field
, "a_sequence");
1179 an_array_field
= bt_ctf_field_structure_get_field(
1180 inner_structure_field
, "an_array");
1182 enum_container_field
= bt_ctf_field_enumeration_get_container(
1183 enum_variant_field
);
1184 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1185 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1186 enum_variant_field
);
1187 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1188 bt_ctf_field_put(int_16_field
);
1189 ok(!bt_ctf_field_string_get_value(a_string_field
),
1190 "bt_ctf_field_string_get_value returns NULL on an unset field");
1191 bt_ctf_field_string_set_value(a_string_field
,
1193 ok(!bt_ctf_field_string_get_value(NULL
),
1194 "bt_ctf_field_string_get_value correctly handles NULL");
1195 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1196 "bt_ctf_field_string_append correctly handles a NULL string field");
1197 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1198 "bt_ctf_field_string_append correctly handles a NULL string value");
1199 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1200 "bt_ctf_field_string_append succeeds");
1201 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1202 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1203 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1204 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1205 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1206 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1207 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1208 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1209 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1210 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1211 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1212 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1214 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1215 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1216 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1217 "bt_ctf_field_string_get_value returns a correct value");
1218 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1219 SEQUENCE_TEST_LENGTH
);
1221 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1222 enum_container_field
) == NULL
,
1223 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1224 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1226 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1227 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1228 variant_type
, enum_variant_field
);
1229 ok(ret_field_type
== int_16_type
,
1230 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1232 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1233 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1234 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1235 uint_35_field
) == 0, "Set a sequence field's length");
1236 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1237 ok(ret_field
== uint_35_field
,
1238 "bt_ctf_field_sequence_get_length returns the correct length field");
1239 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1240 "bt_ctf_field_sequence_get_length properly handles NULL");
1242 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1243 int_16_field
= bt_ctf_field_sequence_get_field(
1244 a_sequence_field
, i
);
1245 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1246 bt_ctf_field_put(int_16_field
);
1249 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1250 int_16_field
= bt_ctf_field_array_get_field(
1252 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1253 bt_ctf_field_put(int_16_field
);
1256 bt_ctf_clock_set_time(clock
, ++current_time
);
1257 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1258 "Append a complex event to a stream");
1261 * Populate the custom packet context field with a dummy value
1262 * otherwise flush will fail.
1264 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1265 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1266 "custom_packet_context_field");
1267 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1269 ok(bt_ctf_stream_flush(stream
) == 0,
1270 "Flush a stream containing a complex event");
1272 bt_ctf_field_put(uint_35_field
);
1273 bt_ctf_field_put(a_string_field
);
1274 bt_ctf_field_put(inner_structure_field
);
1275 bt_ctf_field_put(complex_structure_field
);
1276 bt_ctf_field_put(a_sequence_field
);
1277 bt_ctf_field_put(an_array_field
);
1278 bt_ctf_field_put(enum_variant_field
);
1279 bt_ctf_field_put(enum_container_field
);
1280 bt_ctf_field_put(variant_field
);
1281 bt_ctf_field_put(ret_field
);
1282 bt_ctf_field_put(packet_context_field
);
1283 bt_ctf_field_put(packet_context
);
1284 bt_ctf_field_type_put(uint_35_type
);
1285 bt_ctf_field_type_put(int_16_type
);
1286 bt_ctf_field_type_put(string_type
);
1287 bt_ctf_field_type_put(sequence_type
);
1288 bt_ctf_field_type_put(array_type
);
1289 bt_ctf_field_type_put(inner_structure_type
);
1290 bt_ctf_field_type_put(complex_structure_type
);
1291 bt_ctf_field_type_put(uint_3_type
);
1292 bt_ctf_field_type_put(enum_variant_type
);
1293 bt_ctf_field_type_put(variant_type
);
1294 bt_ctf_field_type_put(ret_field_type
);
1295 bt_ctf_event_class_put(event_class
);
1296 bt_ctf_event_put(event
);
1299 static void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1300 struct bt_ctf_field_type
*expected_type
, const char *name
)
1302 struct bt_ctf_field_type
*copy_type
;
1304 copy_type
= bt_ctf_field_get_type(field
);
1305 ok(copy_type
== expected_type
,
1306 "bt_ctf_field_copy does not copy the type (%s)", name
);
1307 bt_ctf_field_type_put(copy_type
);
1310 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1311 struct bt_ctf_field
*field_b
, const char *name
)
1313 ok(field_a
!= field_b
,
1314 "bt_ctf_field_copy creates different pointers (%s)", name
);
1317 void field_copy_tests()
1319 struct bt_ctf_field_type
*len_type
= NULL
;
1320 struct bt_ctf_field_type
*fp_type
= NULL
;
1321 struct bt_ctf_field_type
*s_type
= NULL
;
1322 struct bt_ctf_field_type
*e_int_type
= NULL
;
1323 struct bt_ctf_field_type
*e_type
= NULL
;
1324 struct bt_ctf_field_type
*v_type
= NULL
;
1325 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1326 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1327 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1328 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1329 struct bt_ctf_field_type
*strct_type
= NULL
;
1330 struct bt_ctf_field
*len
= NULL
;
1331 struct bt_ctf_field
*fp
= NULL
;
1332 struct bt_ctf_field
*s
= NULL
;
1333 struct bt_ctf_field
*e_int
= NULL
;
1334 struct bt_ctf_field
*e
= NULL
;
1335 struct bt_ctf_field
*v
= NULL
;
1336 struct bt_ctf_field
*v_selected
= NULL
;
1337 struct bt_ctf_field
*v_selected_cur
= NULL
;
1338 struct bt_ctf_field
*v_selected_0
= NULL
;
1339 struct bt_ctf_field
*v_selected_1
= NULL
;
1340 struct bt_ctf_field
*v_selected_2
= NULL
;
1341 struct bt_ctf_field
*v_selected_3
= NULL
;
1342 struct bt_ctf_field
*v_selected_4
= NULL
;
1343 struct bt_ctf_field
*v_selected_5
= NULL
;
1344 struct bt_ctf_field
*v_selected_6
= NULL
;
1345 struct bt_ctf_field
*a
= NULL
;
1346 struct bt_ctf_field
*a_0
= NULL
;
1347 struct bt_ctf_field
*a_1
= NULL
;
1348 struct bt_ctf_field
*a_2
= NULL
;
1349 struct bt_ctf_field
*a_3
= NULL
;
1350 struct bt_ctf_field
*a_4
= NULL
;
1351 struct bt_ctf_field
*strct
= NULL
;
1352 struct bt_ctf_field
*len_copy
= NULL
;
1353 struct bt_ctf_field
*fp_copy
= NULL
;
1354 struct bt_ctf_field
*s_copy
= NULL
;
1355 struct bt_ctf_field
*e_int_copy
= NULL
;
1356 struct bt_ctf_field
*e_copy
= NULL
;
1357 struct bt_ctf_field
*v_copy
= NULL
;
1358 struct bt_ctf_field
*v_selected_copy
= NULL
;
1359 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1360 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1361 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1362 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1363 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1364 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1365 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1366 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1367 struct bt_ctf_field
*a_copy
= NULL
;
1368 struct bt_ctf_field
*a_0_copy
= NULL
;
1369 struct bt_ctf_field
*a_1_copy
= NULL
;
1370 struct bt_ctf_field
*a_2_copy
= NULL
;
1371 struct bt_ctf_field
*a_3_copy
= NULL
;
1372 struct bt_ctf_field
*a_4_copy
= NULL
;
1373 struct bt_ctf_field
*strct_copy
= NULL
;
1374 uint64_t uint64_t_val
;
1375 const char *str_val
;
1379 /* create len type */
1380 len_type
= bt_ctf_field_type_integer_create(32);
1383 /* create fp type */
1384 fp_type
= bt_ctf_field_type_floating_point_create();
1388 s_type
= bt_ctf_field_type_string_create();
1391 /* create e_int type */
1392 e_int_type
= bt_ctf_field_type_integer_create(8);
1396 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1398 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1401 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1405 /* create v_label1 type */
1406 v_label1_type
= bt_ctf_field_type_string_create();
1407 assert(v_label1_type
);
1409 /* create v_label1_array type */
1410 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1411 assert(v_label1_array_type
);
1413 /* create v_label2 type */
1414 v_label2_type
= bt_ctf_field_type_integer_create(16);
1415 assert(v_label2_type
);
1417 /* create v_label2_seq type */
1418 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1420 assert(v_label2_seq_type
);
1423 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1425 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1428 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1432 /* create strct type */
1433 strct_type
= bt_ctf_field_type_structure_create();
1435 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1438 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1440 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1442 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1444 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1446 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1447 v_label1_array_type
, "a");
1451 strct
= bt_ctf_field_create(strct_type
);
1455 len
= bt_ctf_field_structure_get_field(strct
, "len");
1459 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1463 s
= bt_ctf_field_structure_get_field(strct
, "s");
1467 e
= bt_ctf_field_structure_get_field(strct
, "e");
1470 /* get e_int (underlying integer) */
1471 e_int
= bt_ctf_field_enumeration_get_container(e
);
1475 v
= bt_ctf_field_structure_get_field(strct
, "v");
1479 a
= bt_ctf_field_structure_get_field(strct
, "a");
1483 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1487 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1491 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1494 /* set e field (LABEL2) */
1495 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1499 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1501 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1502 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1503 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1504 ok(v_selected_cur
== v_selected
,
1505 "bt_ctf_field_variant_get_current_field returns the current field");
1506 bt_ctf_field_put(v_selected_cur
);
1508 /* set selected v field */
1509 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1511 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1512 assert(v_selected_0
);
1513 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1515 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1516 assert(v_selected_1
);
1517 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1519 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1520 assert(v_selected_2
);
1521 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1523 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1524 assert(v_selected_3
);
1525 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1527 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1528 assert(v_selected_4
);
1529 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1531 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1532 assert(v_selected_5
);
1533 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1535 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1536 assert(v_selected_6
);
1537 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1541 a_0
= bt_ctf_field_array_get_field(a
, 0);
1543 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1545 a_1
= bt_ctf_field_array_get_field(a
, 1);
1547 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1549 a_2
= bt_ctf_field_array_get_field(a
, 2);
1551 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1553 a_3
= bt_ctf_field_array_get_field(a
, 3);
1555 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1557 a_4
= bt_ctf_field_array_get_field(a
, 4);
1559 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1562 /* create copy of strct */
1563 ok(!bt_ctf_field_copy(NULL
),
1564 "bt_ctf_field_copy handles NULL correctly");
1565 strct_copy
= bt_ctf_field_copy(strct
);
1567 "bt_ctf_field_copy returns a valid pointer");
1569 /* get all copied fields */
1570 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1572 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1574 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1576 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1578 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1580 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1582 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1583 assert(v_selected_copy
);
1584 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1585 assert(v_selected_0_copy
);
1586 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1587 assert(v_selected_1_copy
);
1588 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1589 assert(v_selected_2_copy
);
1590 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1591 assert(v_selected_3_copy
);
1592 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1593 assert(v_selected_4_copy
);
1594 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1595 assert(v_selected_5_copy
);
1596 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1597 assert(v_selected_6_copy
);
1598 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1599 "sequence field copy is not too large");
1600 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1602 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1604 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1606 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1608 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1610 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1612 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1613 "array field copy is not too large");
1615 /* make sure copied fields are different pointers */
1616 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1617 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1618 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1619 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1620 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1621 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1622 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1623 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1625 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1627 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1629 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1631 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1633 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1635 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1637 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1639 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1640 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1641 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1642 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1643 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1644 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1646 /* make sure copied fields share the same types */
1647 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1648 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1649 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1650 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1651 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1652 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1653 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1655 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1657 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1659 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1661 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1663 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1665 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1667 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1669 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1670 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1671 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1672 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1673 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1674 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1676 /* validate len copy */
1677 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1679 ok(uint64_t_val
== 7,
1680 "bt_ctf_field_copy creates a valid integer field copy");
1682 /* validate fp copy */
1683 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1685 ok(double_val
== 3.14,
1686 "bt_ctf_field_copy creates a valid floating point number field copy");
1688 /* validate s copy */
1689 str_val
= bt_ctf_field_string_get_value(s_copy
);
1690 ok(str_val
&& !strcmp(str_val
, "btbt"),
1691 "bt_ctf_field_copy creates a valid string field copy");
1693 /* validate e_int copy */
1694 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1697 ok(uint64_t_val
== 23,
1698 "bt_ctf_field_copy creates a valid enum's integer field copy");
1700 /* validate e copy */
1701 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1702 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1703 "bt_ctf_field_copy creates a valid enum field copy");
1705 /* validate v_selected copy */
1706 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1707 assert(v_selected_copy_len
);
1708 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1711 ok(uint64_t_val
== 7,
1712 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1713 bt_ctf_field_put(v_selected_copy_len
);
1714 v_selected_copy_len
= NULL
;
1716 /* validate v_selected copy fields */
1717 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1720 ok(uint64_t_val
== 7,
1721 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1722 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1725 ok(uint64_t_val
== 6,
1726 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1727 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1730 ok(uint64_t_val
== 5,
1731 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1732 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1735 ok(uint64_t_val
== 4,
1736 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1737 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1740 ok(uint64_t_val
== 3,
1741 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1742 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1745 ok(uint64_t_val
== 2,
1746 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1747 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1750 ok(uint64_t_val
== 1,
1751 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1753 /* validate a copy fields */
1754 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1755 ok(str_val
&& !strcmp(str_val
, "a_0"),
1756 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1757 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1758 ok(str_val
&& !strcmp(str_val
, "a_1"),
1759 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1760 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1761 ok(str_val
&& !strcmp(str_val
, "a_2"),
1762 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1763 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1764 ok(str_val
&& !strcmp(str_val
, "a_3"),
1765 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1766 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1767 ok(str_val
&& !strcmp(str_val
, "a_4"),
1768 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1770 /* put everything */
1771 bt_ctf_field_type_put(len_type
);
1772 bt_ctf_field_type_put(fp_type
);
1773 bt_ctf_field_type_put(s_type
);
1774 bt_ctf_field_type_put(e_int_type
);
1775 bt_ctf_field_type_put(e_type
);
1776 bt_ctf_field_type_put(v_type
);
1777 bt_ctf_field_type_put(v_label1_type
);
1778 bt_ctf_field_type_put(v_label1_array_type
);
1779 bt_ctf_field_type_put(v_label2_type
);
1780 bt_ctf_field_type_put(v_label2_seq_type
);
1781 bt_ctf_field_type_put(strct_type
);
1782 bt_ctf_field_put(len
);
1783 bt_ctf_field_put(fp
);
1784 bt_ctf_field_put(s
);
1785 bt_ctf_field_put(e_int
);
1786 bt_ctf_field_put(e
);
1787 bt_ctf_field_put(v
);
1788 bt_ctf_field_put(v_selected
);
1789 bt_ctf_field_put(v_selected_0
);
1790 bt_ctf_field_put(v_selected_1
);
1791 bt_ctf_field_put(v_selected_2
);
1792 bt_ctf_field_put(v_selected_3
);
1793 bt_ctf_field_put(v_selected_4
);
1794 bt_ctf_field_put(v_selected_5
);
1795 bt_ctf_field_put(v_selected_6
);
1796 bt_ctf_field_put(a
);
1797 bt_ctf_field_put(a_0
);
1798 bt_ctf_field_put(a_1
);
1799 bt_ctf_field_put(a_2
);
1800 bt_ctf_field_put(a_3
);
1801 bt_ctf_field_put(a_4
);
1802 bt_ctf_field_put(strct
);
1803 bt_ctf_field_put(len_copy
);
1804 bt_ctf_field_put(fp_copy
);
1805 bt_ctf_field_put(s_copy
);
1806 bt_ctf_field_put(e_int_copy
);
1807 bt_ctf_field_put(e_copy
);
1808 bt_ctf_field_put(v_copy
);
1809 bt_ctf_field_put(v_selected_copy
);
1810 bt_ctf_field_put(v_selected_0_copy
);
1811 bt_ctf_field_put(v_selected_1_copy
);
1812 bt_ctf_field_put(v_selected_2_copy
);
1813 bt_ctf_field_put(v_selected_3_copy
);
1814 bt_ctf_field_put(v_selected_4_copy
);
1815 bt_ctf_field_put(v_selected_5_copy
);
1816 bt_ctf_field_put(v_selected_6_copy
);
1817 bt_ctf_field_put(a_copy
);
1818 bt_ctf_field_put(a_0_copy
);
1819 bt_ctf_field_put(a_1_copy
);
1820 bt_ctf_field_put(a_2_copy
);
1821 bt_ctf_field_put(a_3_copy
);
1822 bt_ctf_field_put(a_4_copy
);
1823 bt_ctf_field_put(strct_copy
);
1826 void type_field_tests()
1828 struct bt_ctf_field
*uint_12
;
1829 struct bt_ctf_field
*int_16
;
1830 struct bt_ctf_field
*string
;
1831 struct bt_ctf_field
*enumeration
;
1832 struct bt_ctf_field_type
*composite_structure_type
;
1833 struct bt_ctf_field_type
*structure_seq_type
;
1834 struct bt_ctf_field_type
*string_type
;
1835 struct bt_ctf_field_type
*sequence_type
;
1836 struct bt_ctf_field_type
*uint_8_type
;
1837 struct bt_ctf_field_type
*int_16_type
;
1838 struct bt_ctf_field_type
*uint_12_type
=
1839 bt_ctf_field_type_integer_create(12);
1840 struct bt_ctf_field_type
*enumeration_type
;
1841 struct bt_ctf_field_type
*enumeration_sequence_type
;
1842 struct bt_ctf_field_type
*enumeration_array_type
;
1843 struct bt_ctf_field_type
*returned_type
;
1844 const char *ret_string
;
1846 returned_type
= bt_ctf_field_get_type(NULL
);
1847 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1849 ok(uint_12_type
, "Create an unsigned integer type");
1850 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1851 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1852 "Set integer type's base as binary");
1853 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1854 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1855 "Set integer type's base as decimal");
1856 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1857 BT_CTF_INTEGER_BASE_UNKNOWN
),
1858 "Reject integer type's base set as unknown");
1859 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1860 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1861 "Set integer type's base as octal");
1862 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1863 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1864 "Set integer type's base as hexadecimal");
1865 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1866 "Reject unknown integer base value");
1867 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1868 "Set integer type signedness to signed");
1869 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1870 "Set integer type signedness to unsigned");
1871 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1872 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1873 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1874 "bt_ctf_field_type_integer_get_size returns a correct value");
1875 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1876 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1877 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1878 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1880 ok(bt_ctf_field_type_set_byte_order(NULL
,
1881 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1882 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1883 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1884 (enum bt_ctf_byte_order
) 42) < 0,
1885 "bt_ctf_field_type_set_byte_order rejects invalid values");
1886 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1887 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1888 "Set an integer's byte order to little endian");
1889 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1890 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1891 "Set an integer's byte order to big endian");
1892 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1893 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1894 "bt_ctf_field_type_get_byte_order returns a correct value");
1895 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1896 BT_CTF_BYTE_ORDER_UNKNOWN
,
1897 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1899 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1901 "bt_ctf_field_type_get_type_id handles NULL correctly");
1902 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1904 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1906 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1907 BT_CTF_INTEGER_BASE_UNKNOWN
,
1908 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1909 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1910 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1911 "bt_ctf_field_type_integer_get_base returns a correct value");
1913 ok(bt_ctf_field_type_integer_set_encoding(NULL
, CTF_STRING_ASCII
) < 0,
1914 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1915 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1916 (enum ctf_string_encoding
) 123) < 0,
1917 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1918 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1919 CTF_STRING_UTF8
) == 0,
1920 "Set integer type encoding to UTF8");
1921 ok(bt_ctf_field_type_integer_get_encoding(NULL
) == CTF_STRING_UNKNOWN
,
1922 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1923 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) == CTF_STRING_UTF8
,
1924 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1926 int_16_type
= bt_ctf_field_type_integer_create(16);
1927 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1928 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1929 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1930 uint_8_type
= bt_ctf_field_type_integer_create(8);
1932 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1933 ok(sequence_type
, "Create a sequence of int16_t type");
1934 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1936 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1938 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1939 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1940 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1942 ok(!strcmp(ret_string
, "seq_len"),
1943 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1944 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1945 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1946 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1948 ok(returned_type
== int_16_type
,
1949 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1950 bt_ctf_field_type_put(returned_type
);
1952 string_type
= bt_ctf_field_type_string_create();
1953 ok(string_type
, "Create a string type");
1954 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1956 "Reject invalid \"None\" string encoding");
1957 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1959 "Reject invalid string encoding");
1960 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1961 CTF_STRING_ASCII
) == 0,
1962 "Set string encoding to ASCII");
1964 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1966 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1967 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1969 "bt_ctf_field_type_string_get_encoding returns the correct value");
1971 structure_seq_type
= bt_ctf_field_type_structure_create();
1972 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1974 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1975 ok(structure_seq_type
, "Create a structure type");
1976 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1977 uint_8_type
, "seq_len") == 0,
1978 "Add a uint8_t type to a structure");
1979 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1980 sequence_type
, "a_sequence") == 0,
1981 "Add a sequence type to a structure");
1983 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1984 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1985 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1986 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1988 ok(bt_ctf_field_type_structure_get_field(NULL
,
1989 &ret_string
, &returned_type
, 1) < 0,
1990 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1991 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1992 NULL
, &returned_type
, 1) == 0,
1993 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1994 bt_ctf_field_type_put(returned_type
);
1995 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1996 &ret_string
, NULL
, 1) == 0,
1997 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1998 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1999 &ret_string
, &returned_type
, 10) < 0,
2000 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2001 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2002 &ret_string
, &returned_type
, 1) == 0,
2003 "bt_ctf_field_type_structure_get_field returns a field");
2004 ok(!strcmp(ret_string
, "a_sequence"),
2005 "bt_ctf_field_type_structure_get_field returns a correct field name");
2006 ok(returned_type
== sequence_type
,
2007 "bt_ctf_field_type_structure_get_field returns a correct field type");
2008 bt_ctf_field_type_put(returned_type
);
2010 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2011 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2012 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2013 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2014 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2015 structure_seq_type
, "a_sequence");
2016 ok(returned_type
== sequence_type
,
2017 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2018 bt_ctf_field_type_put(returned_type
);
2020 composite_structure_type
= bt_ctf_field_type_structure_create();
2021 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2022 string_type
, "a_string") == 0,
2023 "Add a string type to a structure");
2024 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2025 structure_seq_type
, "inner_structure") == 0,
2026 "Add a structure type to a structure");
2028 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2029 NULL
, "a_sequence") == NULL
,
2030 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2031 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2032 structure_seq_type
, NULL
) == NULL
,
2033 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2034 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2035 structure_seq_type
, "a_sequence");
2036 ok(returned_type
== sequence_type
,
2037 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2038 bt_ctf_field_type_put(returned_type
);
2040 int_16
= bt_ctf_field_create(int_16_type
);
2041 ok(int_16
, "Instanciate a signed 16-bit integer");
2042 uint_12
= bt_ctf_field_create(uint_12_type
);
2043 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2044 returned_type
= bt_ctf_field_get_type(int_16
);
2045 ok(returned_type
== int_16_type
,
2046 "bt_ctf_field_get_type returns the correct type");
2048 /* Can't modify types after instanciating them */
2049 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2050 BT_CTF_INTEGER_BASE_DECIMAL
),
2051 "Check an integer type' base can't be modified after instanciation");
2052 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2053 "Check an integer type's signedness can't be modified after instanciation");
2055 /* Check signed property is checked */
2056 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2057 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2058 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2059 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2061 /* Check overflows are properly tested for */
2062 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2063 "Check -32768 is allowed for a signed 16-bit integer");
2064 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2065 "Check 32767 is allowed for a signed 16-bit integer");
2066 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2067 "Check 32768 is not allowed for a signed 16-bit integer");
2068 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2069 "Check -32769 is not allowed for a signed 16-bit integer");
2070 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2071 "Check -42 is allowed for a signed 16-bit integer");
2073 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2074 "Check 4095 is allowed for an unsigned 12-bit integer");
2075 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2076 "Check 4096 is not allowed for a unsigned 12-bit integer");
2077 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2078 "Check 0 is allowed for an unsigned 12-bit integer");
2080 string
= bt_ctf_field_create(string_type
);
2081 ok(string
, "Instanciate a string field");
2082 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2083 "Set a string's value");
2085 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2086 ok(enumeration_type
,
2087 "Create an enumeration type with an unsigned 12-bit integer as container");
2088 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
2089 enumeration_type
, "count");
2090 ok(!enumeration_sequence_type
,
2091 "Check enumeration types are validated when creating a sequence");
2092 enumeration_array_type
= bt_ctf_field_type_array_create(
2093 enumeration_type
, 10);
2094 ok(!enumeration_array_type
,
2095 "Check enumeration types are validated when creating an array");
2096 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2097 enumeration_type
, "enumeration"),
2098 "Check enumeration types are validated when adding them as structure members");
2099 enumeration
= bt_ctf_field_create(enumeration_type
);
2101 "Check enumeration types are validated before instantiation");
2103 bt_ctf_field_put(string
);
2104 bt_ctf_field_put(uint_12
);
2105 bt_ctf_field_put(int_16
);
2106 bt_ctf_field_put(enumeration
);
2107 bt_ctf_field_type_put(composite_structure_type
);
2108 bt_ctf_field_type_put(structure_seq_type
);
2109 bt_ctf_field_type_put(string_type
);
2110 bt_ctf_field_type_put(sequence_type
);
2111 bt_ctf_field_type_put(uint_8_type
);
2112 bt_ctf_field_type_put(int_16_type
);
2113 bt_ctf_field_type_put(uint_12_type
);
2114 bt_ctf_field_type_put(enumeration_type
);
2115 bt_ctf_field_type_put(enumeration_sequence_type
);
2116 bt_ctf_field_type_put(enumeration_array_type
);
2117 bt_ctf_field_type_put(returned_type
);
2120 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2121 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2124 * Append enough events to force the underlying packet to be resized.
2125 * Also tests that a new event can be declared after a stream has been
2126 * instantiated and used/flushed.
2130 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2132 struct bt_ctf_field_type
*integer_type
=
2133 bt_ctf_field_type_integer_create(17);
2134 struct bt_ctf_field_type
*string_type
=
2135 bt_ctf_field_type_string_create();
2136 struct bt_ctf_event
*event
= NULL
;
2137 struct bt_ctf_field
*ret_field
= NULL
;
2138 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2139 uint64_t ret_uint64
;
2140 int events_appended
= 0;
2141 struct bt_ctf_field
*packet_context
= NULL
,
2142 *packet_context_field
= NULL
, *event_context
= NULL
;
2144 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2146 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2148 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2149 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2154 event
= bt_ctf_event_create(event_class
);
2155 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2156 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2157 ok(ret_field_type
== integer_type
,
2158 "bt_ctf_event_get_payload_by_index returns a correct field");
2159 bt_ctf_field_type_put(ret_field_type
);
2160 bt_ctf_field_put(ret_field
);
2162 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2163 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2164 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2165 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2166 bt_ctf_event_put(event
);
2168 ok(bt_ctf_stream_get_event_context(NULL
) == NULL
,
2169 "bt_ctf_stream_get_event_context handles NULL correctly");
2170 event_context
= bt_ctf_stream_get_event_context(stream
);
2172 "bt_ctf_stream_get_event_context returns a stream event context");
2173 ok(bt_ctf_stream_set_event_context(NULL
, event_context
) < 0,
2174 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
2175 ok(bt_ctf_stream_set_event_context(stream
, NULL
) < 0,
2176 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
2177 ok(!bt_ctf_stream_set_event_context(stream
, event_context
),
2178 "bt_ctf_stream_set_event_context correctly set a stream event context");
2179 ret_field
= bt_ctf_field_create(integer_type
);
2180 ok(bt_ctf_stream_set_event_context(stream
, ret_field
) < 0,
2181 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
2182 bt_ctf_field_put(ret_field
);
2184 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2185 event
= bt_ctf_event_create(event_class
);
2186 struct bt_ctf_field
*integer
=
2187 bt_ctf_field_create(integer_type
);
2188 struct bt_ctf_field
*string
=
2189 bt_ctf_field_create(string_type
);
2191 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2192 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2193 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2195 bt_ctf_field_put(integer
);
2196 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2197 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2199 bt_ctf_field_put(string
);
2201 /* Populate stream event context */
2202 integer
= bt_ctf_field_structure_get_field(event_context
,
2203 "common_event_context");
2204 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2206 bt_ctf_field_put(integer
);
2208 ret
|= bt_ctf_stream_append_event(stream
, event
);
2209 bt_ctf_event_put(event
);
2216 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2217 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2218 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2219 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2220 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2221 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2222 ok(ret
== 0 && ret_uint64
== 0,
2223 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2224 bt_ctf_stream_append_discarded_events(stream
, 1000);
2225 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2226 ok(ret
== 0 && ret_uint64
== 1000,
2227 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2230 ok(events_appended
, "Append 100 000 events to a stream");
2233 * Populate the custom packet context field with a dummy value
2234 * otherwise flush will fail.
2236 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2237 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2238 "custom_packet_context_field");
2239 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2241 ok(bt_ctf_stream_flush(stream
) == 0,
2242 "Flush a stream that forces a packet resize");
2243 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2244 ok(ret
== 0 && ret_uint64
== 1000,
2245 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2246 bt_ctf_field_type_put(integer_type
);
2247 bt_ctf_field_type_put(string_type
);
2248 bt_ctf_field_put(packet_context
);
2249 bt_ctf_field_put(packet_context_field
);
2250 bt_ctf_field_put(event_context
);
2251 bt_ctf_event_class_put(event_class
);
2254 void test_empty_stream(struct bt_ctf_writer
*writer
)
2257 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2258 struct bt_ctf_stream_class
*stream_class
= NULL
;
2259 struct bt_ctf_stream
*stream
= NULL
;
2261 trace
= bt_ctf_writer_get_trace(writer
);
2263 diag("Failed to get trace from writer");
2268 stream_class
= bt_ctf_stream_class_create("empty_stream");
2269 if (!stream_class
) {
2270 diag("Failed to create stream class");
2275 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2276 "bt_ctf_stream_class_get_trace handles NULL correctly");
2277 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2278 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2280 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2282 diag("Failed to create writer stream");
2287 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2288 ok(ret_trace
== trace
,
2289 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2292 "Created a stream class with default attributes and an empty stream");
2293 bt_ctf_trace_put(trace
);
2294 bt_ctf_trace_put(ret_trace
);
2295 bt_ctf_stream_put(stream
);
2296 bt_ctf_stream_class_put(stream_class
);
2299 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2302 struct bt_ctf_trace
*trace
= NULL
;
2303 struct bt_ctf_clock
*clock
= NULL
;
2304 struct bt_ctf_stream_class
*stream_class
= NULL
;
2305 struct bt_ctf_stream
*stream
= NULL
;
2306 struct bt_ctf_field_type
*integer_type
= NULL
,
2307 *sequence_type
= NULL
, *event_header_type
= NULL
;
2308 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2309 *event_header
= NULL
, *packet_header
= NULL
;
2310 struct bt_ctf_event_class
*event_class
= NULL
;
2311 struct bt_ctf_event
*event
= NULL
;
2313 trace
= bt_ctf_writer_get_trace(writer
);
2315 fail("Failed to get trace from writer");
2319 clock
= bt_ctf_trace_get_clock(trace
, 0);
2321 fail("Failed to get clock from trace");
2325 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2326 if (!stream_class
) {
2327 fail("Failed to create stream class");
2331 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2333 fail("Failed to set stream class clock");
2338 * Customize event header to add an "seq_len" integer member
2339 * which will be used as the length of a sequence in an event of this
2342 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2344 if (!event_header_type
) {
2345 fail("Failed to get event header type");
2349 integer_type
= bt_ctf_field_type_integer_create(13);
2350 if (!integer_type
) {
2351 fail("Failed to create length integer type");
2355 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2356 integer_type
, "seq_len");
2358 fail("Failed to add a new field to stream event header");
2362 event_class
= bt_ctf_event_class_create("sequence_event");
2364 fail("Failed to create event class");
2369 * This event's payload will contain a sequence which references
2370 * stream.event.header.seq_len as its length field.
2372 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2373 "stream.event.header.seq_len");
2374 if (!sequence_type
) {
2375 fail("Failed to create a sequence");
2379 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2382 fail("Failed to add a sequence to an event class");
2386 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2388 fail("Failed to add event class to stream class");
2392 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2394 fail("Failed to create stream")
2399 * We have defined a custom packet header field. We have to populate it
2402 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2403 if (!packet_header
) {
2404 fail("Failed to get stream packet header");
2408 integer
= bt_ctf_field_structure_get_field(packet_header
,
2409 "custom_trace_packet_header_field");
2411 fail("Failed to retrieve custom_trace_packet_header_field");
2415 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2417 fail("Failed to set custom_trace_packet_header_field value");
2420 bt_ctf_field_put(integer
);
2422 event
= bt_ctf_event_create(event_class
);
2424 fail("Failed to create event");
2428 event_header
= bt_ctf_event_get_header(event
);
2429 if (!event_header
) {
2430 fail("Failed to get event header");
2434 integer
= bt_ctf_field_structure_get_field(event_header
,
2437 fail("Failed to get seq_len field from event header");
2441 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2443 fail("Failed to set seq_len value in event header");
2447 /* Populate both sequence integer fields */
2448 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2450 fail("Failed to retrieve sequence from event");
2454 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2456 fail("Failed to set sequence length");
2459 bt_ctf_field_put(integer
);
2461 for (i
= 0; i
< 2; i
++) {
2462 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2464 fail("Failed to retrieve sequence element");
2468 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2470 fail("Failed to set sequence element value");
2474 bt_ctf_field_put(integer
);
2478 ret
= bt_ctf_stream_append_event(stream
, event
);
2480 fail("Failed to append event to stream");
2484 ret
= bt_ctf_stream_flush(stream
);
2486 fail("Failed to flush custom_event_header stream");
2489 bt_ctf_clock_put(clock
);
2490 bt_ctf_trace_put(trace
);
2491 bt_ctf_stream_put(stream
);
2492 bt_ctf_stream_class_put(stream_class
);
2493 bt_ctf_event_class_put(event_class
);
2494 bt_ctf_event_put(event
);
2495 bt_ctf_field_put(integer
);
2496 bt_ctf_field_put(sequence
);
2497 bt_ctf_field_put(event_header
);
2498 bt_ctf_field_put(packet_header
);
2499 bt_ctf_field_type_put(sequence_type
);
2500 bt_ctf_field_type_put(integer_type
);
2501 bt_ctf_field_type_put(event_header_type
);
2504 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2507 struct bt_ctf_trace
*trace
= NULL
;
2508 struct bt_ctf_clock
*clock
= NULL
;
2509 struct bt_ctf_stream_class
*stream_class
= NULL
;
2510 struct bt_ctf_stream
*stream
= NULL
,
2512 struct bt_ctf_event_class
*event_class
= NULL
;
2513 struct bt_ctf_event
*event
= NULL
;
2514 struct bt_ctf_field_type
*integer_type
= NULL
;
2515 struct bt_ctf_field
*integer
= NULL
;
2517 trace
= bt_ctf_writer_get_trace(writer
);
2519 diag("Failed to get trace from writer");
2524 clock
= bt_ctf_trace_get_clock(trace
, 0);
2526 diag("Failed to get clock from trace");
2531 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2532 if (!stream_class
) {
2533 diag("Failed to create stream class");
2538 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2540 diag("Failed to set stream class clock");
2544 event_class
= bt_ctf_event_class_create("some_event_class_name");
2545 integer_type
= bt_ctf_field_type_integer_create(32);
2546 if (!integer_type
) {
2547 diag("Failed to create integer field type");
2552 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2555 diag("Failed to add field to event class");
2559 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2562 diag("Failed to add event class to stream class");
2565 event
= bt_ctf_event_create(event_class
);
2567 diag("Failed to create event");
2572 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2574 diag("Failed to get integer field payload from event");
2579 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2581 diag("Failed to set integer field value");
2585 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2587 diag("Failed to create writer stream");
2592 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2593 "bt_ctf_event_get_stream handles NULL correctly");
2594 ok(bt_ctf_event_get_stream(event
) == NULL
,
2595 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2597 ret
= bt_ctf_stream_append_event(stream
, event
);
2599 diag("Failed to append event to stream");
2603 ret_stream
= bt_ctf_event_get_stream(event
);
2604 ok(ret_stream
== stream
,
2605 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2608 "Create an event before instanciating its associated stream");
2609 bt_ctf_trace_put(trace
);
2610 bt_ctf_stream_put(stream
);
2611 bt_ctf_stream_put(ret_stream
);
2612 bt_ctf_stream_class_put(stream_class
);
2613 bt_ctf_event_class_put(event_class
);
2614 bt_ctf_event_put(event
);
2615 bt_ctf_field_type_put(integer_type
);
2616 bt_ctf_field_put(integer
);
2617 bt_ctf_clock_put(clock
);
2620 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2622 struct bt_ctf_event_class
*event_class
;
2624 assert(event_class
= bt_ctf_event_class_create("Simple Event"));
2625 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2626 "two event classes with the same name cannot cohabit within the same stream class");
2627 bt_ctf_event_class_put(event_class
);
2629 event_class
= bt_ctf_event_class_create("different name, ok");
2630 assert(event_class
);
2631 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2632 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2633 "two event classes with the same ID cannot cohabit within the same stream class");
2634 bt_ctf_event_class_put(event_class
);
2637 int main(int argc
, char **argv
)
2639 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2640 char metadata_path
[sizeof(trace_path
) + 9];
2641 const char *clock_name
= "test_clock";
2642 const char *clock_description
= "This is a test clock";
2643 const char *returned_clock_name
;
2644 const char *returned_clock_description
;
2645 const uint64_t frequency
= 1123456789;
2646 const uint64_t offset_s
= 1351530929945824323;
2647 const uint64_t offset
= 1234567;
2648 const uint64_t precision
= 10;
2649 const int is_absolute
= 0xFF;
2650 char *metadata_string
;
2651 struct bt_ctf_writer
*writer
;
2652 struct utsname name
;
2653 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2654 struct bt_ctf_clock
*clock
, *ret_clock
;
2655 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2656 struct bt_ctf_stream
*stream1
;
2657 const char *ret_string
;
2658 const unsigned char *ret_uuid
;
2659 unsigned char tmp_uuid
[16] = { 0 };
2660 struct bt_ctf_field_type
*packet_context_type
,
2661 *packet_context_field_type
,
2662 *packet_header_type
,
2663 *packet_header_field_type
,
2665 *stream_event_context_type
,
2667 *event_header_field_type
;
2668 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2669 struct bt_ctf_trace
*trace
;
2671 int64_t ret_int64_t
;
2672 struct bt_object
*obj
;
2675 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2681 if (!mkdtemp(trace_path
)) {
2685 strcpy(metadata_path
, trace_path
);
2686 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2688 writer
= bt_ctf_writer_create(trace_path
);
2689 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2691 ok(!bt_ctf_writer_get_trace(NULL
),
2692 "bt_ctf_writer_get_trace correctly handles NULL");
2693 trace
= bt_ctf_writer_get_trace(writer
);
2695 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2696 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2697 "Set a trace's byte order to big endian");
2698 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2699 "bt_ctf_trace_get_byte_order returns a correct endianness");
2701 /* Add environment context to the trace */
2702 ret
= gethostname(hostname
, sizeof(hostname
));
2706 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2707 "Add host (%s) environment field to writer instance",
2709 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2711 "bt_ctf_writer_add_environment_field error with NULL writer");
2712 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2714 "bt_ctf_writer_add_environment_field error with NULL field name");
2715 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2717 "bt_ctf_writer_add_environment_field error with NULL field value");
2719 /* Test bt_ctf_trace_set_environment_field with an integer object */
2720 obj
= bt_object_integer_create_init(23);
2722 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2723 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2724 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2725 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2726 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2727 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2728 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2729 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2732 /* Test bt_ctf_trace_set_environment_field with a string object */
2733 obj
= bt_object_string_create_init("the value");
2735 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2736 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2739 /* Test bt_ctf_trace_set_environment_field_integer */
2740 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2742 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2743 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2744 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2745 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2747 "bt_ctf_trace_set_environment_field_integer succeeds");
2749 /* Test bt_ctf_trace_set_environment_field_string */
2750 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2752 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2753 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2754 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2755 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2757 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2758 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2760 "bt_ctf_trace_set_environment_field_string succeeds");
2762 /* Test bt_ctf_trace_get_environment_field_count */
2763 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2764 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2765 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2766 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2768 /* Test bt_ctf_trace_get_environment_field_name */
2769 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
2770 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2771 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
2772 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2773 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
2774 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2775 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
2776 ok(ret_string
&& !strcmp(ret_string
, "host"),
2777 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2778 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
2779 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2780 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2781 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
2782 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2783 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2784 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
2785 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2786 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2787 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
2788 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2789 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2791 /* Test bt_ctf_trace_get_environment_field_value */
2792 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
2793 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2794 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
2795 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2796 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
2797 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2798 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
2799 ret
= bt_object_integer_get(obj
, &ret_int64_t
);
2800 ok(!ret
&& ret_int64_t
== 23,
2801 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2803 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
2804 ret
= bt_object_string_get(obj
, &ret_string
);
2805 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2806 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2809 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2810 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2812 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2813 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
2814 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2815 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
2816 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2817 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
2819 ret
= bt_object_string_get(obj
, &ret_string
);
2820 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
2821 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2824 /* Test environment field replacement */
2825 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2827 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2828 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2829 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2830 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
2831 ret
= bt_object_integer_get(obj
, &ret_int64_t
);
2832 ok(!ret
&& ret_int64_t
== 654321,
2833 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2841 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
2842 == 0, "Add sysname (%s) environment field to writer instance",
2844 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
2845 name
.nodename
) == 0,
2846 "Add nodename (%s) environment field to writer instance",
2848 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
2849 == 0, "Add release (%s) environment field to writer instance",
2851 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
2852 == 0, "Add version (%s) environment field to writer instance",
2854 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
2855 == 0, "Add machine (%s) environment field to writer istance",
2858 /* Define a clock and add it to the trace */
2859 ok(bt_ctf_clock_create("signed") == NULL
,
2860 "Illegal clock name rejected");
2861 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
2862 clock
= bt_ctf_clock_create(clock_name
);
2863 ok(clock
, "Clock created sucessfully");
2864 returned_clock_name
= bt_ctf_clock_get_name(clock
);
2865 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
2866 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
2867 "Returned clock name is valid");
2869 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2870 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
2871 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
2872 "Clock description set successfully");
2874 returned_clock_description
= bt_ctf_clock_get_description(clock
);
2875 ok(returned_clock_description
,
2876 "bt_ctf_clock_get_description returns a description.");
2877 ok(returned_clock_description
?
2878 !strcmp(returned_clock_description
, clock_description
) : 0,
2879 "Returned clock description is valid");
2881 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
2882 "bt_ctf_clock_get_frequency returns the correct default frequency");
2883 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
2884 "Set clock frequency");
2885 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
2886 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
2888 ok(bt_ctf_clock_get_offset_s(clock
) == DEFAULT_CLOCK_OFFSET_S
,
2889 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
2890 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
2891 "Set clock offset (seconds)");
2892 ok(bt_ctf_clock_get_offset_s(clock
) == offset_s
,
2893 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
2895 ok(bt_ctf_clock_get_offset(clock
) == DEFAULT_CLOCK_OFFSET
,
2896 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
2897 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
2898 ok(bt_ctf_clock_get_offset(clock
) == offset
,
2899 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
2901 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
2902 "bt_ctf_clock_get_precision returns the correct default precision");
2903 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
2904 "Set clock precision");
2905 ok(bt_ctf_clock_get_precision(clock
) == precision
,
2906 "bt_ctf_clock_get_precision returns the correct precision once it is set");
2908 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
2909 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
2910 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
2911 "Set clock absolute property");
2912 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
2913 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
2915 ok(bt_ctf_clock_get_time(clock
) == DEFAULT_CLOCK_TIME
,
2916 "bt_ctf_clock_get_time returns the correct default time");
2917 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
2919 ok(bt_ctf_clock_get_time(clock
) == current_time
,
2920 "bt_ctf_clock_get_time returns the correct time once it is set");
2922 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
2923 "Add clock to writer instance");
2924 ok(bt_ctf_writer_add_clock(writer
, clock
),
2925 "Verify a clock can't be added twice to a writer instance");
2927 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
2928 "bt_ctf_trace_get_clock_count correctly handles NULL");
2929 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
2930 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
2931 ok(!bt_ctf_trace_get_clock(NULL
, 0),
2932 "bt_ctf_trace_get_clock correctly handles NULL");
2933 ok(!bt_ctf_trace_get_clock(trace
, -1),
2934 "bt_ctf_trace_get_clock correctly handles negative indexes");
2935 ok(!bt_ctf_trace_get_clock(trace
, 1),
2936 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
2937 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
2938 ok(ret_clock
== clock
,
2939 "bt_ctf_trace_get_clock returns the right clock instance");
2940 bt_ctf_clock_put(ret_clock
);
2941 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
2942 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
2943 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
2944 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
2945 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
2946 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
2947 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
2948 ok(ret_clock
== clock
,
2949 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
2950 bt_ctf_clock_put(ret_clock
);
2951 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
2952 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
2954 ok(!bt_ctf_clock_get_name(NULL
),
2955 "bt_ctf_clock_get_name correctly handles NULL");
2956 ok(!bt_ctf_clock_get_description(NULL
),
2957 "bt_ctf_clock_get_description correctly handles NULL");
2958 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
2959 "bt_ctf_clock_get_frequency correctly handles NULL");
2960 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
2961 "bt_ctf_clock_get_precision correctly handles NULL");
2962 ok(bt_ctf_clock_get_offset_s(NULL
) == -1ULL,
2963 "bt_ctf_clock_get_offset_s correctly handles NULL");
2964 ok(bt_ctf_clock_get_offset(NULL
) == -1ULL,
2965 "bt_ctf_clock_get_offset correctly handles NULL");
2966 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
2967 "bt_ctf_clock_get_is_absolute correctly handles NULL");
2968 ok(bt_ctf_clock_get_time(NULL
) == -1ULL,
2969 "bt_ctf_clock_get_time correctly handles NULL");
2971 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
2972 "bt_ctf_clock_set_description correctly handles NULL clock");
2973 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
2974 "bt_ctf_clock_set_frequency correctly handles NULL clock");
2975 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
2976 "bt_ctf_clock_get_precision correctly handles NULL clock");
2977 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
2978 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
2979 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
2980 "bt_ctf_clock_set_offset correctly handles NULL clock");
2981 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
2982 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
2983 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
2984 "bt_ctf_clock_set_time correctly handles NULL clock");
2985 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
2986 "bt_ctf_clock_get_uuid correctly handles NULL clock");
2987 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
2989 "bt_ctf_clock_get_uuid returns a UUID");
2991 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
2992 /* Slightly modify UUID */
2993 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
2996 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
2997 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
2998 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
2999 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3000 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3001 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3002 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3004 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3005 ok(uuid_match(ret_uuid
, tmp_uuid
),
3006 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3008 /* Define a stream class */
3009 stream_class
= bt_ctf_stream_class_create("test_stream");
3011 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3012 "bt_ctf_stream_class_get_name handles NULL correctly");
3013 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3014 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3015 "bt_ctf_stream_class_get_name returns a correct stream class name");
3017 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3018 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3019 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3020 "bt_ctf_stream_class_get_clock handles NULL correctly");
3022 ok(stream_class
, "Create stream class");
3023 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3024 "Set a stream class' clock");
3025 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3026 ok(ret_clock
== clock
,
3027 "bt_ctf_stream_class_get_clock returns a correct clock");
3028 bt_ctf_clock_put(ret_clock
);
3030 /* Test the event fields and event types APIs */
3033 /* Test fields copying */
3036 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3037 "bt_ctf_stream_class_get_id returns an error when no id is set");
3038 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3039 "bt_ctf_stream_class_get_id handles NULL correctly");
3040 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3041 "bt_ctf_stream_class_set_id handles NULL correctly");
3042 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3043 "Set an stream class' id");
3044 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3045 "bt_ctf_stream_class_get_id returns the correct value");
3047 /* Validate default event header fields */
3048 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3049 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3050 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3053 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3054 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == CTF_TYPE_STRUCT
,
3055 "Default event header type is a structure");
3056 event_header_field_type
=
3057 bt_ctf_field_type_structure_get_field_type_by_name(
3058 ret_field_type
, "id");
3059 ok(event_header_field_type
,
3060 "Default event header type contains an \"id\" field");
3061 ok(bt_ctf_field_type_get_type_id(
3062 event_header_field_type
) == CTF_TYPE_INTEGER
,
3063 "Default event header \"id\" field is an integer");
3064 bt_ctf_field_type_put(event_header_field_type
);
3065 event_header_field_type
=
3066 bt_ctf_field_type_structure_get_field_type_by_name(
3067 ret_field_type
, "timestamp");
3068 ok(event_header_field_type
,
3069 "Default event header type contains a \"timestamp\" field");
3070 ok(bt_ctf_field_type_get_type_id(
3071 event_header_field_type
) == CTF_TYPE_INTEGER
,
3072 "Default event header \"timestamp\" field is an integer");
3073 bt_ctf_field_type_put(event_header_field_type
);
3074 bt_ctf_field_type_put(ret_field_type
);
3076 /* Add a custom trace packet header field */
3077 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3078 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3079 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3080 ok(packet_header_type
,
3081 "bt_ctf_trace_get_packet_header_type returns a packet header");
3082 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == CTF_TYPE_STRUCT
,
3083 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3084 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3085 packet_header_type
, "magic");
3086 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3087 bt_ctf_field_type_put(ret_field_type
);
3088 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3089 packet_header_type
, "uuid");
3090 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3091 bt_ctf_field_type_put(ret_field_type
);
3092 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3093 packet_header_type
, "stream_id");
3094 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3095 bt_ctf_field_type_put(ret_field_type
);
3097 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3098 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3099 packet_header_field_type
, "custom_trace_packet_header_field"),
3100 "Added a custom trace packet header field successfully");
3102 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3103 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3104 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3105 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3106 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3107 "Set a trace packet_header_type successfully");
3109 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3110 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3112 /* Add a custom field to the stream class' packet context */
3113 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3114 ok(packet_context_type
,
3115 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3116 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == CTF_TYPE_STRUCT
,
3117 "Packet context is a structure");
3119 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3120 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3121 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3122 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3124 integer_type
= bt_ctf_field_type_integer_create(32);
3125 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3127 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3128 /* Create a "uint5_t" equivalent custom packet context field */
3129 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3131 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3132 packet_context_field_type
, "custom_packet_context_field");
3133 ok(ret
== 0, "Packet context field added successfully");
3135 /* Define a stream event context containing a my_integer field. */
3136 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3137 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3138 ok(bt_ctf_stream_class_get_event_context_type(
3139 stream_class
) == NULL
,
3140 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3141 stream_event_context_type
= bt_ctf_field_type_structure_create();
3142 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3143 integer_type
, "common_event_context");
3145 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3146 stream_event_context_type
) < 0,
3147 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3148 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3150 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3151 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3153 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3155 ok(bt_ctf_stream_class_set_event_context_type(
3156 stream_class
, stream_event_context_type
) == 0,
3157 "Set a new stream event context type");
3158 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3160 ok(ret_field_type
== stream_event_context_type
,
3161 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3162 bt_ctf_field_type_put(ret_field_type
);
3164 /* Instantiate a stream and append events */
3165 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3166 ok(stream1
, "Instanciate a stream class from writer");
3168 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3169 "bt_ctf_stream_get_class correctly handles NULL");
3170 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3171 ok(ret_stream_class
,
3172 "bt_ctf_stream_get_class returns a stream class");
3173 ok(ret_stream_class
== stream_class
,
3174 "Returned stream class is of the correct type");
3177 * Try to modify the packet context type after a stream has been
3180 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3181 packet_header_field_type
, "should_fail");
3183 "Trace packet header type can't be modified once a stream has been instanciated");
3186 * Try to modify the packet context type after a stream has been
3189 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3190 packet_context_field_type
, "should_fail");
3192 "Packet context type can't be modified once a stream has been instanciated");
3195 * Try to modify the stream event context type after a stream has been
3198 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3199 integer_type
, "should_fail");
3201 "Stream event context type can't be modified once a stream has been instanciated");
3203 /* Should fail after instanciating a stream (frozen) */
3204 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3205 "Changes to a stream class that was already instantiated fail");
3207 /* Populate the custom packet header field only once for all tests */
3208 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3209 "bt_ctf_stream_get_packet_header handles NULL correctly");
3210 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3212 "bt_ctf_stream_get_packet_header returns a packet header");
3213 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3214 ok(ret_field_type
== packet_header_type
,
3215 "Stream returns a packet header of the appropriate type");
3216 bt_ctf_field_type_put(ret_field_type
);
3217 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3218 "custom_trace_packet_header_field");
3219 ok(packet_header_field
,
3220 "Packet header structure contains a custom field with the appropriate name");
3221 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3222 ok(ret_field_type
== packet_header_field_type
,
3223 "Custom packet header field is of the expected type");
3224 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3225 54321), "Set custom packet header value successfully");
3226 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3227 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3228 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3229 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3230 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3231 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3232 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3233 "Successfully set a stream's packet header");
3235 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3236 "Add environment field to writer after stream creation");
3238 test_instanciate_event_before_stream(writer
);
3240 append_simple_event(stream_class
, stream1
, clock
);
3242 packet_resize_test(stream_class
, stream1
, clock
);
3244 append_complex_event(stream_class
, stream1
, clock
);
3246 append_existing_event_class(stream_class
);
3248 test_empty_stream(writer
);
3250 test_custom_event_header_stream(writer
);
3252 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3253 ok(metadata_string
, "Get metadata string");
3255 bt_ctf_writer_flush_metadata(writer
);
3256 validate_metadata(argv
[1], metadata_path
);
3257 validate_trace(argv
[2], trace_path
);
3259 bt_ctf_clock_put(clock
);
3260 bt_ctf_stream_class_put(ret_stream_class
);
3261 bt_ctf_writer_put(writer
);
3262 bt_ctf_stream_put(stream1
);
3263 bt_ctf_field_type_put(packet_context_type
);
3264 bt_ctf_field_type_put(packet_context_field_type
);
3265 bt_ctf_field_type_put(integer_type
);
3266 bt_ctf_field_type_put(stream_event_context_type
);
3267 bt_ctf_field_type_put(ret_field_type
);
3268 bt_ctf_field_type_put(packet_header_type
);
3269 bt_ctf_field_type_put(packet_header_field_type
);
3270 bt_ctf_field_put(packet_header
);
3271 bt_ctf_field_put(packet_header_field
);
3272 bt_ctf_trace_put(trace
);
3273 free(metadata_string
);
3275 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
3276 "bt_ctf_stream_class_get_trace returns NULL after its trace has been reclaimed");
3277 bt_ctf_stream_class_put(stream_class
);
3279 /* Remove all trace files and delete temporary trace directory */
3280 DIR *trace_dir
= opendir(trace_path
);
3282 perror("# opendir");
3286 struct dirent
*entry
;
3287 while ((entry
= readdir(trace_dir
))) {
3288 if (entry
->d_type
== DT_REG
) {
3289 unlinkat(dirfd(trace_dir
), entry
->d_name
, 0);
3294 closedir(trace_dir
);