6 * Copyright 2013 - 2015 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf-writer/stream-class.h>
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ref.h>
31 #include <babeltrace/ctf/events.h>
32 #include <babeltrace/values.h>
34 #include <babeltrace/compat/stdlib.h>
36 #include <sys/utsname.h>
37 #include <babeltrace/compat/limits.h>
38 #include <babeltrace/compat/stdio.h>
43 #include <babeltrace/compat/dirent.h>
49 #define METADATA_LINE_SIZE 512
50 #define SEQUENCE_TEST_LENGTH 10
51 #define ARRAY_TEST_LENGTH 5
52 #define PACKET_RESIZE_TEST_LENGTH 100000
54 #define DEFAULT_CLOCK_FREQ 1000000000
55 #define DEFAULT_CLOCK_PRECISION 1
56 #define DEFAULT_CLOCK_OFFSET 0
57 #define DEFAULT_CLOCK_OFFSET_S 0
58 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
59 #define DEFAULT_CLOCK_TIME 0
60 #define DEFAULT_CLOCK_VALUE 0
64 static int64_t current_time
= 42;
67 void delete_trace(const char *trace_path
)
69 /* Remove all trace files and delete temporary trace directory */
71 DIR *trace_dir
= opendir(trace_path
);
78 while ((entry
= readdir(trace_dir
))) {
80 char filename
[PATH_MAX
];
82 if (snprintf(filename
, sizeof(filename
), "%s/%s",
83 trace_path
, entry
->d_name
) <= 0) {
87 if (stat(filename
, &st
)) {
91 if (S_ISREG(st
.st_mode
)) {
92 unlinkat(bt_dirfd(trace_dir
), entry
->d_name
, 0);
100 /* Return 1 if uuids match, zero if different. */
102 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
107 if (!uuid_a
|| !uuid_b
) {
111 for (i
= 0; i
< 16; i
++) {
112 if (uuid_a
[i
] != uuid_b
[i
]) {
123 void validate_metadata(char *parser_path
, char *metadata_path
)
126 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
127 int parser_output_fd
= -1, metadata_fd
= -1;
129 if (!metadata_path
) {
134 parser_output_fd
= mkstemp(parser_output_path
);
135 metadata_fd
= open(metadata_path
, O_RDONLY
);
137 unlink(parser_output_path
);
139 if (parser_output_fd
== -1 || metadata_fd
== -1) {
140 diag("Failed create temporary files for metadata parsing.");
148 waitpid(pid
, &status
, 0);
149 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
151 /* ctf-parser-test expects a metadata string on stdin. */
152 ret
= dup2(metadata_fd
, STDIN_FILENO
);
154 perror("# dup2 metadata_fd to STDIN");
158 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
160 perror("# dup2 parser_output_fd to STDOUT");
164 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
166 perror("# dup2 parser_output_fd to STDERR");
170 execl(parser_path
, "ctf-parser-test", NULL
);
171 perror("# Could not launch the ctf metadata parser process");
175 ok(ret
== 0, "Metadata string is valid");
177 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
179 size_t len
= METADATA_LINE_SIZE
;
180 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
182 metadata_fp
= fdopen(metadata_fd
, "r");
184 perror("fdopen on metadata_fd");
189 parser_output_fp
= fdopen(parser_output_fd
, "r");
190 if (!parser_output_fp
) {
191 perror("fdopen on parser_output_fd");
194 parser_output_fd
= -1;
198 diag("malloc failure");
203 /* Output the metadata and parser output as diagnostic */
204 while (bt_getline(&line
, &len
, metadata_fp
) > 0) {
205 fprintf(stderr
, "# %s", line
);
208 rewind(parser_output_fp
);
209 while (bt_getline(&line
, &len
, parser_output_fp
) > 0) {
210 fprintf(stderr
, "# %s", line
);
216 if (fclose(metadata_fp
)) {
217 diag("fclose failure");
220 if (parser_output_fp
) {
221 if (fclose(parser_output_fp
)) {
222 diag("fclose failure");
227 if (parser_output_fd
>= 0) {
228 if (close(parser_output_fd
)) {
232 if (metadata_fd
>= 0) {
233 if (close(metadata_fd
)) {
240 void validate_trace(char *parser_path
, char *trace_path
)
243 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
244 int babeltrace_output_fd
= -1;
251 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
252 unlink(babeltrace_output_path
);
254 if (babeltrace_output_fd
== -1) {
255 diag("Failed to create a temporary file for trace parsing.");
263 waitpid(pid
, &status
, 0);
264 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
266 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
268 perror("# dup2 babeltrace_output_fd to STDOUT");
272 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
274 perror("# dup2 babeltrace_output_fd to STDERR");
278 execl(parser_path
, "babeltrace", trace_path
, NULL
);
279 perror("# Could not launch the babeltrace process");
283 ok(ret
== 0, "Babeltrace could read the resulting trace");
285 if (ret
&& babeltrace_output_fd
>= 0) {
287 size_t len
= METADATA_LINE_SIZE
;
288 FILE *babeltrace_output_fp
= NULL
;
290 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
291 if (!babeltrace_output_fp
) {
292 perror("fdopen on babeltrace_output_fd");
295 babeltrace_output_fd
= -1;
299 diag("malloc error");
301 rewind(babeltrace_output_fp
);
302 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
308 if (babeltrace_output_fp
) {
309 if (fclose(babeltrace_output_fp
)) {
310 diag("fclose error");
315 if (babeltrace_output_fd
>= 0) {
316 if (close(babeltrace_output_fd
)) {
323 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
324 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
326 /* Create and add a simple event class */
327 struct bt_ctf_event_class
*simple_event_class
=
328 bt_ctf_event_class_create("Simple Event");
329 struct bt_ctf_field_type
*uint_12_type
=
330 bt_ctf_field_type_integer_create(12);
331 struct bt_ctf_field_type
*int_64_type
=
332 bt_ctf_field_type_integer_create(64);
333 struct bt_ctf_field_type
*float_type
=
334 bt_ctf_field_type_floating_point_create();
335 struct bt_ctf_field_type
*enum_type
;
336 struct bt_ctf_field_type
*enum_type_unsigned
=
337 bt_ctf_field_type_enumeration_create(uint_12_type
);
338 struct bt_ctf_field_type
*event_context_type
=
339 bt_ctf_field_type_structure_create();
340 struct bt_ctf_field_type
*event_payload_type
= NULL
;
341 struct bt_ctf_field_type
*returned_type
;
342 struct bt_ctf_event
*simple_event
;
343 struct bt_ctf_field
*integer_field
;
344 struct bt_ctf_field
*float_field
;
345 struct bt_ctf_field
*enum_field
;
346 struct bt_ctf_field
*enum_field_unsigned
;
347 struct bt_ctf_field
*enum_container_field
;
348 const char *mapping_name_test
= "truie";
349 const double double_test_value
= 3.1415;
350 struct bt_ctf_field
*enum_container_field_unsigned
;
351 const char *mapping_name_negative_test
= "negative_value";
352 const char *ret_char
;
354 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
355 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
356 struct bt_ctf_clock
*ret_clock
;
357 struct bt_ctf_event_class
*ret_event_class
;
358 struct bt_ctf_field
*packet_context
;
359 struct bt_ctf_field
*packet_context_field
;
360 struct bt_ctf_field
*stream_event_context
;
361 struct bt_ctf_field
*stream_event_context_field
;
362 struct bt_ctf_field
*event_context
;
363 struct bt_ctf_field
*event_context_field
;
364 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
365 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
366 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
369 ok(uint_12_type
, "Create an unsigned integer type");
371 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
372 ok(int_64_type
, "Create a signed integer type");
373 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
375 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
376 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
377 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
378 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
379 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
380 bt_put(returned_type
);
382 bt_ctf_field_type_set_alignment(float_type
, 32);
383 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
384 "bt_ctf_field_type_get_alignment handles NULL correctly");
385 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
386 "bt_ctf_field_type_get_alignment returns a correct value");
388 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
389 "Set a floating point type's exponent digit count");
390 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
391 "Set a floating point type's mantissa digit count");
393 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
394 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
395 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
396 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
397 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
398 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
399 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
400 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
402 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
403 mapping_name_negative_test
, -12345, 0) == 0,
404 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
405 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
406 "escaping; \"test\"", 1, 1) == 0,
407 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
408 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
409 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
410 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
411 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
412 "event clock int float", 5, 22) == 0,
413 "Accept enumeration mapping strings containing reserved keywords");
414 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
416 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
417 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
418 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
419 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
420 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
421 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
422 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
424 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
425 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
426 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
427 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
428 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
429 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
431 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
432 "enum_field") == 0, "Add signed enumeration field to event");
434 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
435 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
436 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
437 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
438 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
439 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
440 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
441 NULL
, &ret_range_end_int64_t
) < 0,
442 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
443 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
444 &ret_range_start_int64_t
, NULL
) < 0,
445 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
446 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
447 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
448 "bt_ctf_field_type_enumeration_get_mapping returns a value");
449 ok(!strcmp(ret_char
, mapping_name_test
),
450 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
451 ok(ret_range_start_int64_t
== 42,
452 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
453 ok(ret_range_end_int64_t
== 42,
454 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
456 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
457 "escaping; \"test\"", 0, 0) == 0,
458 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
459 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
460 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
461 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
462 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
463 "event clock int float", 5, 22) == 0,
464 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
465 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
467 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
468 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
469 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
470 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
471 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
472 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
473 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
474 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
476 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
477 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
478 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
479 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
481 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
482 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
483 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
484 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
485 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
486 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
487 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
488 NULL
, &ret_range_end_uint64_t
) < 0,
489 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
490 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
491 &ret_range_start_uint64_t
, NULL
) < 0,
492 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
493 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
494 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
495 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
496 ok(!strcmp(ret_char
, mapping_name_test
),
497 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
498 ok(ret_range_start_uint64_t
== 42,
499 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
500 ok(ret_range_end_uint64_t
== 42,
501 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
503 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
505 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
508 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
510 /* Set an event context type which will contain a single integer*/
511 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
512 "event_specific_context"),
513 "Add event specific context field");
514 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
515 "bt_ctf_event_class_get_context_type handles NULL correctly");
516 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
517 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
519 ok(bt_ctf_event_class_set_context_type(simple_event_class
, NULL
) < 0,
520 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
521 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
522 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
523 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
524 "Set an event class' context type successfully");
525 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
526 ok(returned_type
== event_context_type
,
527 "bt_ctf_event_class_get_context_type returns the appropriate type");
528 bt_put(returned_type
);
530 bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
);
533 * bt_ctf_stream_class_add_event_class() copies the field types
534 * of simple_event_class, so we retrieve the new ones to create
535 * the appropriate fields.
537 BT_PUT(event_context_type
);
538 BT_PUT(event_payload_type
);
539 event_payload_type
= bt_ctf_event_class_get_payload_type(
541 assert(event_payload_type
);
542 event_context_type
= bt_ctf_event_class_get_context_type(
544 assert(event_context_type
);
545 ep_integer_field_type
=
546 bt_ctf_field_type_structure_get_field_type_by_name(
547 event_payload_type
, "integer_field");
548 assert(ep_integer_field_type
);
550 bt_ctf_field_type_structure_get_field_type_by_name(
551 event_payload_type
, "enum_field");
552 assert(ep_enum_field_type
);
553 ep_enum_field_unsigned_type
=
554 bt_ctf_field_type_structure_get_field_type_by_name(
555 event_payload_type
, "enum_field_unsigned");
556 assert(ep_enum_field_unsigned_type
);
558 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
559 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
560 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
561 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
562 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
563 "bt_ctf_stream_class_get_event_class handles NULL correctly");
564 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
565 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
566 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
567 ok(ret_event_class
== simple_event_class
,
568 "bt_ctf_stream_class_get_event_class returns the correct event class");
569 bt_put(ret_event_class
);
570 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
571 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
572 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
573 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
575 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
576 ok(ret_event_class
== simple_event_class
,
577 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
578 bt_put(ret_event_class
);
580 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
581 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
582 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
583 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
584 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
585 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
586 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
587 ok(ret_event_class
== simple_event_class
,
588 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
589 bt_put(ret_event_class
);
591 simple_event
= bt_ctf_event_create(simple_event_class
);
593 "Instantiate an event containing a single integer field");
595 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
596 "bt_ctf_event_get_clock handles NULL correctly");
597 ret_clock
= bt_ctf_event_get_clock(simple_event
);
598 ok(ret_clock
== clock
,
599 "bt_ctf_event_get_clock returns a correct clock");
602 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
603 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
604 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
605 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
607 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
608 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
609 "bt_ctf_field_floating_point_get_value fails on an unset float field");
610 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
611 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
612 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
613 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
614 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
615 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
616 "bt_ctf_field_floating_point_get_value returns a double value");
617 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
618 "bt_ctf_field_floating_point_get_value returns a correct value");
620 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
622 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
623 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
624 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
625 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
626 enum_container_field
= bt_ctf_field_enumeration_get_container(
628 ok(bt_ctf_field_signed_integer_set_value(
629 enum_container_field
, -42) == 0,
630 "Set signed enumeration container value");
631 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
632 ok(!strcmp(ret_char
, mapping_name_negative_test
),
633 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
634 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
637 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
638 assert(enum_field_unsigned
);
639 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
640 enum_field_unsigned
);
641 ok(bt_ctf_field_unsigned_integer_set_value(
642 enum_container_field_unsigned
, 42) == 0,
643 "Set unsigned enumeration container value");
644 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
645 enum_field_unsigned
);
647 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
648 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
649 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
651 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
653 /* Populate stream event context */
654 stream_event_context
=
655 bt_ctf_event_get_stream_event_context(simple_event
);
656 assert(stream_event_context
);
657 stream_event_context_field
= bt_ctf_field_structure_get_field(
658 stream_event_context
, "common_event_context");
659 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
661 /* Populate the event's context */
662 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
663 "bt_ctf_event_get_event_context handles NULL correctly");
664 event_context
= bt_ctf_event_get_event_context(simple_event
);
666 "bt_ctf_event_get_event_context returns a field");
667 returned_type
= bt_ctf_field_get_type(event_context
);
668 ok(returned_type
== event_context_type
,
669 "bt_ctf_event_get_event_context returns a field of the appropriate type");
670 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
671 "event_specific_context");
672 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
673 "Successfully set an event context's value");
674 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
675 "bt_ctf_event_set_event_context handles a NULL event correctly");
676 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
677 "bt_ctf_event_set_event_context handles a NULL event context correctly");
678 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
679 "bt_ctf_event_set_event_context rejects a context of the wrong type");
680 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
681 "Set an event context successfully");
683 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
684 "Append simple event to trace stream");
686 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
687 "bt_ctf_stream_get_packet_context handles NULL correctly");
688 packet_context
= bt_ctf_stream_get_packet_context(stream
);
690 "bt_ctf_stream_get_packet_context returns a packet context");
692 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
694 ok(packet_context_field
,
695 "Packet context contains the default packet_size field.");
696 bt_put(packet_context_field
);
697 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
698 "custom_packet_context_field");
699 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
700 "Custom packet context field value successfully set.");
702 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
703 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
704 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
705 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
706 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
707 "Successfully set a stream's packet context");
709 ok(bt_ctf_stream_flush(stream
) == 0,
710 "Flush trace stream with one event");
712 bt_put(simple_event_class
);
713 bt_put(simple_event
);
714 bt_put(uint_12_type
);
718 bt_put(enum_type_unsigned
);
719 bt_put(returned_type
);
720 bt_put(event_context_type
);
721 bt_put(integer_field
);
724 bt_put(enum_field_unsigned
);
725 bt_put(enum_container_field
);
726 bt_put(enum_container_field_unsigned
);
727 bt_put(packet_context
);
728 bt_put(packet_context_field
);
729 bt_put(stream_event_context
);
730 bt_put(stream_event_context_field
);
731 bt_put(event_context
);
732 bt_put(event_context_field
);
733 bt_put(event_payload_type
);
734 bt_put(ep_integer_field_type
);
735 bt_put(ep_enum_field_type
);
736 bt_put(ep_enum_field_unsigned_type
);
740 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
741 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
743 struct event_class_attrs_counts
{
754 struct event_class_attrs_counts
;
755 const char *complex_test_event_string
= "Complex Test Event";
756 const char *test_string_1
= "Test ";
757 const char *test_string_2
= "string ";
758 const char *test_string_3
= "abcdefghi";
759 const char *test_string_4
= "abcd\0efg\0hi";
760 const char *test_string_cat
= "Test string abcdeabcd";
761 struct bt_ctf_field_type
*uint_35_type
=
762 bt_ctf_field_type_integer_create(35);
763 struct bt_ctf_field_type
*int_16_type
=
764 bt_ctf_field_type_integer_create(16);
765 struct bt_ctf_field_type
*uint_3_type
=
766 bt_ctf_field_type_integer_create(3);
767 struct bt_ctf_field_type
*enum_variant_type
=
768 bt_ctf_field_type_enumeration_create(uint_3_type
);
769 struct bt_ctf_field_type
*variant_type
=
770 bt_ctf_field_type_variant_create(enum_variant_type
,
772 struct bt_ctf_field_type
*string_type
=
773 bt_ctf_field_type_string_create();
774 struct bt_ctf_field_type
*sequence_type
;
775 struct bt_ctf_field_type
*array_type
;
776 struct bt_ctf_field_type
*inner_structure_type
=
777 bt_ctf_field_type_structure_create();
778 struct bt_ctf_field_type
*complex_structure_type
=
779 bt_ctf_field_type_structure_create();
780 struct bt_ctf_field_type
*ret_field_type
;
781 struct bt_ctf_event_class
*event_class
;
782 struct bt_ctf_event
*event
;
783 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
784 *inner_structure_field
, *complex_structure_field
,
785 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
786 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
787 *stream_event_ctx_int_field
, *ret_field
;
788 uint64_t ret_unsigned_int
;
789 int64_t ret_signed_int
;
790 const char *ret_string
;
791 struct bt_ctf_stream_class
*ret_stream_class
;
792 struct bt_ctf_event_class
*ret_event_class
;
793 struct bt_ctf_field
*packet_context
, *packet_context_field
;
794 struct bt_value
*obj
;
796 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
797 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
798 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
799 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
800 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
801 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
802 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
803 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
804 bt_ctf_field_type_set_alignment(int_16_type
, 32);
805 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
806 bt_ctf_field_type_integer_set_base(uint_35_type
,
807 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
809 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
810 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
813 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
814 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
815 ret_field_type
= bt_ctf_field_type_array_get_element_type(
817 ok(ret_field_type
== int_16_type
,
818 "bt_ctf_field_type_array_get_element_type returns the correct type");
819 bt_put(ret_field_type
);
821 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
822 "bt_ctf_field_type_array_get_length handles NULL correctly");
823 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
824 "bt_ctf_field_type_array_get_length returns the correct length");
826 bt_ctf_field_type_structure_add_field(inner_structure_type
,
827 uint_35_type
, "seq_len");
828 bt_ctf_field_type_structure_add_field(inner_structure_type
,
829 sequence_type
, "a_sequence");
830 bt_ctf_field_type_structure_add_field(inner_structure_type
,
831 array_type
, "an_array");
833 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
835 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
837 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
838 "UINT35_TYPE", 2, 7);
840 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
842 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
843 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
844 enum_variant_type
, NULL
) < 0,
845 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
846 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
847 enum_variant_type
, "INT16_TYPE") == 1,
848 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
850 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
851 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
852 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
853 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
854 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
855 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
857 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
858 "An unknown entry"), "Reject a variant field based on an unknown tag value");
859 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
860 "UINT3_TYPE") == 0, "Add a field to a variant");
861 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
863 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
866 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
867 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
868 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
869 ok(ret_field_type
== enum_variant_type
,
870 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
871 bt_put(ret_field_type
);
873 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
874 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
875 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
876 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
877 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
878 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
879 "INT16_TYPE") == NULL
,
880 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
881 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
883 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
884 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
885 variant_type
, "INT16_TYPE");
886 ok(ret_field_type
== int_16_type
,
887 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
888 bt_put(ret_field_type
);
890 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
891 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
892 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
893 "bt_ctf_field_type_variant_get_field_count returns the correct count");
895 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
896 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
897 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
898 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
899 bt_put(ret_field_type
);
900 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
901 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
902 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
903 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
904 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
905 "bt_ctf_field_type_variant_get_field returns a field");
906 ok(!strcmp("INT16_TYPE", ret_string
),
907 "bt_ctf_field_type_variant_get_field returns a correct field name");
908 ok(ret_field_type
== int_16_type
,
909 "bt_ctf_field_type_variant_get_field returns a correct field type");
910 bt_put(ret_field_type
);
912 bt_ctf_field_type_structure_add_field(complex_structure_type
,
913 enum_variant_type
, "variant_selector");
914 bt_ctf_field_type_structure_add_field(complex_structure_type
,
915 string_type
, "a_string");
916 bt_ctf_field_type_structure_add_field(complex_structure_type
,
917 variant_type
, "variant_value");
918 bt_ctf_field_type_structure_add_field(complex_structure_type
,
919 inner_structure_type
, "inner_structure");
921 ok(bt_ctf_event_class_create("clock") == NULL
,
922 "Reject creation of an event class with an illegal name");
923 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
924 ok(event_class
, "Create an event class");
925 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
926 "Reject addition of a field with an empty name to an event");
927 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
928 "Reject addition of a field with a NULL type to an event");
929 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
931 "Reject addition of a type with an illegal name to an event");
932 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
934 "Add field of type unsigned integer to an event");
935 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
936 "int_16") == 0, "Add field of type signed integer to an event");
937 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
938 "complex_structure") == 0,
939 "Add composite structure to an event");
941 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
942 "bt_ctf_event_class_get_name handles NULL correctly");
943 ret_string
= bt_ctf_event_class_get_name(event_class
);
944 ok(!strcmp(ret_string
, complex_test_event_string
),
945 "bt_ctf_event_class_get_name returns a correct name");
946 ok(bt_ctf_event_class_get_id(event_class
) < 0,
947 "bt_ctf_event_class_get_id returns a negative value when not set");
948 ok(bt_ctf_event_class_get_id(NULL
) < 0,
949 "bt_ctf_event_class_get_id handles NULL correctly");
950 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
951 "bt_ctf_event_class_set_id handles NULL correctly");
952 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
953 "Set an event class' id");
954 ok(bt_ctf_event_class_get_id(event_class
) == 42,
955 "bt_ctf_event_class_get_id returns the correct value");
957 /* Test event class attributes */
958 obj
= bt_value_integer_create_init(15);
960 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
961 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
962 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
963 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
964 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
965 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
966 assert(!bt_value_integer_set(obj
, -3));
967 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
968 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
969 assert(!bt_value_integer_set(obj
, 11));
970 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
971 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
972 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
973 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
974 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
976 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
979 obj
= bt_value_integer_create_init(5);
981 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
982 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
984 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
985 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
986 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
987 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
988 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
989 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
990 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
993 ret
= bt_value_integer_get(obj
, &int64_value
);
994 ok(obj
&& !ret
&& int64_value
== 5,
995 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
998 obj
= bt_value_string_create_init("nu name");
1000 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
1001 ret_string
= bt_ctf_event_class_get_name(event_class
);
1002 ok(!strcmp(ret_string
, "nu name"),
1003 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
1004 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
1005 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
1007 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
1009 obj
= bt_value_string_create_init("http://kernel.org/");
1011 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
1014 ok(bt_ctf_event_class_get_attribute_count(NULL
),
1015 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1016 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
1017 "bt_ctf_event_class_get_attribute_count returns the correct count");
1018 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
1019 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1020 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
1021 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1022 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
1023 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1024 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
1025 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1027 memset(&attrs_count
, 0, sizeof(attrs_count
));
1029 for (i
= 0; i
< 4; ++i
) {
1030 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
1032 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
1033 assert(ret_string
&& obj
);
1035 if (!strcmp(ret_string
, "id")) {
1037 ok(bt_value_is_integer(obj
),
1038 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1040 } else if (!strcmp(ret_string
, "name")) {
1042 ok(bt_value_is_string(obj
),
1043 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1045 } else if (!strcmp(ret_string
, "loglevel")) {
1046 attrs_count
.loglevel
++;
1047 ok(bt_value_is_integer(obj
),
1048 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1050 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1051 attrs_count
.modelemfuri
++;
1052 ok(bt_value_is_string(obj
),
1053 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1056 attrs_count
.unknown
++;
1062 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1063 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1064 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1065 "event class has one instance of each known attribute");
1067 /* Add event class to the stream class */
1068 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1069 "Reject addition of NULL event class to a stream class");
1070 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1071 event_class
) == 0, "Add an event class to stream class");
1073 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1074 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1075 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1076 ok(ret_stream_class
== stream_class
,
1077 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1078 bt_put(ret_stream_class
);
1080 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1081 "bt_ctf_event_class_get_field_count handles NULL correctly");
1082 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1083 "bt_ctf_event_class_get_field_count returns a correct value");
1085 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1086 &ret_field_type
, 0) < 0,
1087 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1088 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1089 &ret_field_type
, 0) == 0,
1090 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1091 bt_put(ret_field_type
);
1092 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1094 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1095 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1096 &ret_field_type
, 42) < 0,
1097 "bt_ctf_event_class_get_field handles an invalid index correctly");
1098 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1099 &ret_field_type
, 0) == 0,
1100 "bt_ctf_event_class_get_field returns a field");
1101 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
1102 "bt_ctf_event_class_get_field returns a correct field type");
1103 bt_put(ret_field_type
);
1104 ok(!strcmp(ret_string
, "uint_35"),
1105 "bt_ctf_event_class_get_field returns a correct field name");
1106 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1107 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1108 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1109 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1110 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1111 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1112 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1113 "complex_structure");
1114 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
1115 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1116 bt_put(ret_field_type
);
1118 event
= bt_ctf_event_create(event_class
);
1119 ok(event
, "Instanciate a complex event");
1121 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1122 "bt_ctf_event_get_class handles NULL correctly");
1123 ret_event_class
= bt_ctf_event_get_class(event
);
1124 ok(ret_event_class
== event_class
,
1125 "bt_ctf_event_get_class returns the correct event class");
1126 bt_put(ret_event_class
);
1128 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1129 if (!uint_35_field
) {
1130 printf("uint_35_field is NULL\n");
1133 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1134 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1135 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1136 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1137 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1138 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1139 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1140 &ret_unsigned_int
) == 0,
1141 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1142 ok(ret_unsigned_int
== 0x0DDF00D,
1143 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1144 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1145 &ret_signed_int
) < 0,
1146 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1147 bt_put(uint_35_field
);
1149 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1150 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1151 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1152 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1153 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1154 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1155 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1156 &ret_signed_int
) == 0,
1157 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1158 ok(ret_signed_int
== -12345,
1159 "bt_ctf_field_signed_integer_get_value returns the correct value");
1160 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1161 &ret_unsigned_int
) < 0,
1162 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1163 bt_put(int_16_field
);
1165 complex_structure_field
= bt_ctf_event_get_payload(event
,
1166 "complex_structure");
1168 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1169 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1170 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1171 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1172 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1173 complex_structure_field
, 3);
1174 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1175 bt_put(inner_structure_field
);
1176 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1177 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1178 bt_put(ret_field_type
);
1180 inner_structure_field
= bt_ctf_field_structure_get_field(
1181 complex_structure_field
, "inner_structure");
1182 a_string_field
= bt_ctf_field_structure_get_field(
1183 complex_structure_field
, "a_string");
1184 enum_variant_field
= bt_ctf_field_structure_get_field(
1185 complex_structure_field
, "variant_selector");
1186 variant_field
= bt_ctf_field_structure_get_field(
1187 complex_structure_field
, "variant_value");
1188 uint_35_field
= bt_ctf_field_structure_get_field(
1189 inner_structure_field
, "seq_len");
1190 a_sequence_field
= bt_ctf_field_structure_get_field(
1191 inner_structure_field
, "a_sequence");
1192 an_array_field
= bt_ctf_field_structure_get_field(
1193 inner_structure_field
, "an_array");
1195 enum_container_field
= bt_ctf_field_enumeration_get_container(
1196 enum_variant_field
);
1197 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1198 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1199 enum_variant_field
);
1200 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1201 bt_put(int_16_field
);
1202 ok(!bt_ctf_field_string_get_value(a_string_field
),
1203 "bt_ctf_field_string_get_value returns NULL on an unset field");
1204 bt_ctf_field_string_set_value(a_string_field
,
1206 ok(!bt_ctf_field_string_get_value(NULL
),
1207 "bt_ctf_field_string_get_value correctly handles NULL");
1208 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1209 "bt_ctf_field_string_append correctly handles a NULL string field");
1210 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1211 "bt_ctf_field_string_append correctly handles a NULL string value");
1212 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1213 "bt_ctf_field_string_append succeeds");
1214 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1215 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1216 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1217 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1218 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1219 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1220 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1221 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1222 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1223 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1224 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1225 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1227 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1228 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1229 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1230 "bt_ctf_field_string_get_value returns a correct value");
1231 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1232 SEQUENCE_TEST_LENGTH
);
1234 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1235 enum_container_field
) == NULL
,
1236 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1237 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1239 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1240 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1241 variant_type
, enum_variant_field
);
1242 ok(ret_field_type
== int_16_type
,
1243 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1245 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1246 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1247 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1248 uint_35_field
) == 0, "Set a sequence field's length");
1249 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1250 ok(ret_field
== uint_35_field
,
1251 "bt_ctf_field_sequence_get_length returns the correct length field");
1252 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1253 "bt_ctf_field_sequence_get_length properly handles NULL");
1255 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1256 int_16_field
= bt_ctf_field_sequence_get_field(
1257 a_sequence_field
, i
);
1258 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1259 bt_put(int_16_field
);
1262 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1263 int_16_field
= bt_ctf_field_array_get_field(
1265 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1266 bt_put(int_16_field
);
1269 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1270 assert(stream_event_ctx_field
);
1271 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1272 stream_event_ctx_field
, "common_event_context");
1273 BT_PUT(stream_event_ctx_field
);
1274 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1275 BT_PUT(stream_event_ctx_int_field
);
1277 bt_ctf_clock_set_time(clock
, ++current_time
);
1278 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1279 "Append a complex event to a stream");
1282 * Populate the custom packet context field with a dummy value
1283 * otherwise flush will fail.
1285 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1286 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1287 "custom_packet_context_field");
1288 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1290 ok(bt_ctf_stream_flush(stream
) == 0,
1291 "Flush a stream containing a complex event");
1293 bt_put(uint_35_field
);
1294 bt_put(a_string_field
);
1295 bt_put(inner_structure_field
);
1296 bt_put(complex_structure_field
);
1297 bt_put(a_sequence_field
);
1298 bt_put(an_array_field
);
1299 bt_put(enum_variant_field
);
1300 bt_put(enum_container_field
);
1301 bt_put(variant_field
);
1303 bt_put(packet_context_field
);
1304 bt_put(packet_context
);
1305 bt_put(uint_35_type
);
1306 bt_put(int_16_type
);
1307 bt_put(string_type
);
1308 bt_put(sequence_type
);
1310 bt_put(inner_structure_type
);
1311 bt_put(complex_structure_type
);
1312 bt_put(uint_3_type
);
1313 bt_put(enum_variant_type
);
1314 bt_put(variant_type
);
1315 bt_put(ret_field_type
);
1316 bt_put(event_class
);
1321 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1322 struct bt_ctf_field_type
*expected_type
, const char *name
)
1324 struct bt_ctf_field_type
*copy_type
;
1326 copy_type
= bt_ctf_field_get_type(field
);
1327 ok(copy_type
== expected_type
,
1328 "bt_ctf_field_copy does not copy the type (%s)", name
);
1333 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1334 struct bt_ctf_field
*field_b
, const char *name
)
1336 ok(field_a
!= field_b
,
1337 "bt_ctf_field_copy creates different pointers (%s)", name
);
1341 void field_copy_tests()
1343 struct bt_ctf_field_type
*len_type
= NULL
;
1344 struct bt_ctf_field_type
*fp_type
= NULL
;
1345 struct bt_ctf_field_type
*s_type
= NULL
;
1346 struct bt_ctf_field_type
*e_int_type
= NULL
;
1347 struct bt_ctf_field_type
*e_type
= NULL
;
1348 struct bt_ctf_field_type
*v_type
= NULL
;
1349 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1350 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1351 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1352 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1353 struct bt_ctf_field_type
*strct_type
= NULL
;
1354 struct bt_ctf_field
*len
= NULL
;
1355 struct bt_ctf_field
*fp
= NULL
;
1356 struct bt_ctf_field
*s
= NULL
;
1357 struct bt_ctf_field
*e_int
= NULL
;
1358 struct bt_ctf_field
*e
= NULL
;
1359 struct bt_ctf_field
*v
= NULL
;
1360 struct bt_ctf_field
*v_selected
= NULL
;
1361 struct bt_ctf_field
*v_selected_cur
= NULL
;
1362 struct bt_ctf_field
*v_selected_0
= NULL
;
1363 struct bt_ctf_field
*v_selected_1
= NULL
;
1364 struct bt_ctf_field
*v_selected_2
= NULL
;
1365 struct bt_ctf_field
*v_selected_3
= NULL
;
1366 struct bt_ctf_field
*v_selected_4
= NULL
;
1367 struct bt_ctf_field
*v_selected_5
= NULL
;
1368 struct bt_ctf_field
*v_selected_6
= NULL
;
1369 struct bt_ctf_field
*a
= NULL
;
1370 struct bt_ctf_field
*a_0
= NULL
;
1371 struct bt_ctf_field
*a_1
= NULL
;
1372 struct bt_ctf_field
*a_2
= NULL
;
1373 struct bt_ctf_field
*a_3
= NULL
;
1374 struct bt_ctf_field
*a_4
= NULL
;
1375 struct bt_ctf_field
*strct
= NULL
;
1376 struct bt_ctf_field
*len_copy
= NULL
;
1377 struct bt_ctf_field
*fp_copy
= NULL
;
1378 struct bt_ctf_field
*s_copy
= NULL
;
1379 struct bt_ctf_field
*e_int_copy
= NULL
;
1380 struct bt_ctf_field
*e_copy
= NULL
;
1381 struct bt_ctf_field
*v_copy
= NULL
;
1382 struct bt_ctf_field
*v_selected_copy
= NULL
;
1383 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1384 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1385 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1386 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1387 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1388 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1389 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1390 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1391 struct bt_ctf_field
*a_copy
= NULL
;
1392 struct bt_ctf_field
*a_0_copy
= NULL
;
1393 struct bt_ctf_field
*a_1_copy
= NULL
;
1394 struct bt_ctf_field
*a_2_copy
= NULL
;
1395 struct bt_ctf_field
*a_3_copy
= NULL
;
1396 struct bt_ctf_field
*a_4_copy
= NULL
;
1397 struct bt_ctf_field
*strct_copy
= NULL
;
1398 uint64_t uint64_t_val
;
1399 const char *str_val
;
1403 /* create len type */
1404 len_type
= bt_ctf_field_type_integer_create(32);
1407 /* create fp type */
1408 fp_type
= bt_ctf_field_type_floating_point_create();
1412 s_type
= bt_ctf_field_type_string_create();
1415 /* create e_int type */
1416 e_int_type
= bt_ctf_field_type_integer_create(8);
1420 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1422 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1425 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1429 /* create v_label1 type */
1430 v_label1_type
= bt_ctf_field_type_string_create();
1431 assert(v_label1_type
);
1433 /* create v_label1_array type */
1434 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1435 assert(v_label1_array_type
);
1437 /* create v_label2 type */
1438 v_label2_type
= bt_ctf_field_type_integer_create(16);
1439 assert(v_label2_type
);
1441 /* create v_label2_seq type */
1442 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1444 assert(v_label2_seq_type
);
1447 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1449 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1452 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1456 /* create strct type */
1457 strct_type
= bt_ctf_field_type_structure_create();
1459 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1462 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1464 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1466 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1468 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1470 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1471 v_label1_array_type
, "a");
1475 strct
= bt_ctf_field_create(strct_type
);
1479 len
= bt_ctf_field_structure_get_field(strct
, "len");
1483 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1487 s
= bt_ctf_field_structure_get_field(strct
, "s");
1491 e
= bt_ctf_field_structure_get_field(strct
, "e");
1494 /* get e_int (underlying integer) */
1495 e_int
= bt_ctf_field_enumeration_get_container(e
);
1499 v
= bt_ctf_field_structure_get_field(strct
, "v");
1503 a
= bt_ctf_field_structure_get_field(strct
, "a");
1507 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1511 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1515 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1518 /* set e field (LABEL2) */
1519 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1523 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1525 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1526 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1527 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1528 ok(v_selected_cur
== v_selected
,
1529 "bt_ctf_field_variant_get_current_field returns the current field");
1530 bt_put(v_selected_cur
);
1532 /* set selected v field */
1533 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1535 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1536 assert(v_selected_0
);
1537 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1539 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1540 assert(v_selected_1
);
1541 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1543 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1544 assert(v_selected_2
);
1545 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1547 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1548 assert(v_selected_3
);
1549 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1551 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1552 assert(v_selected_4
);
1553 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1555 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1556 assert(v_selected_5
);
1557 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1559 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1560 assert(v_selected_6
);
1561 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1565 a_0
= bt_ctf_field_array_get_field(a
, 0);
1567 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1569 a_1
= bt_ctf_field_array_get_field(a
, 1);
1571 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1573 a_2
= bt_ctf_field_array_get_field(a
, 2);
1575 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1577 a_3
= bt_ctf_field_array_get_field(a
, 3);
1579 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1581 a_4
= bt_ctf_field_array_get_field(a
, 4);
1583 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1586 /* create copy of strct */
1587 ok(!bt_ctf_field_copy(NULL
),
1588 "bt_ctf_field_copy handles NULL correctly");
1589 strct_copy
= bt_ctf_field_copy(strct
);
1591 "bt_ctf_field_copy returns a valid pointer");
1593 /* get all copied fields */
1594 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1596 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1598 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1600 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1602 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1604 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1606 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1607 assert(v_selected_copy
);
1608 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1609 assert(v_selected_0_copy
);
1610 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1611 assert(v_selected_1_copy
);
1612 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1613 assert(v_selected_2_copy
);
1614 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1615 assert(v_selected_3_copy
);
1616 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1617 assert(v_selected_4_copy
);
1618 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1619 assert(v_selected_5_copy
);
1620 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1621 assert(v_selected_6_copy
);
1622 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1623 "sequence field copy is not too large");
1624 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1626 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1628 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1630 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1632 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1634 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1636 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1637 "array field copy is not too large");
1639 /* make sure copied fields are different pointers */
1640 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1641 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1642 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1643 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1644 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1645 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1646 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1647 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1649 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1651 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1653 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1655 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1657 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1659 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1661 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1663 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1664 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1665 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1666 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1667 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1668 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1670 /* make sure copied fields share the same types */
1671 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1672 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1673 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1674 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1675 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1676 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1677 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1679 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1681 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1683 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1685 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1687 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1689 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1691 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1693 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1694 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1695 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1696 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1697 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1698 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1700 /* validate len copy */
1701 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1703 ok(uint64_t_val
== 7,
1704 "bt_ctf_field_copy creates a valid integer field copy");
1706 /* validate fp copy */
1707 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1709 ok(double_val
== 3.14,
1710 "bt_ctf_field_copy creates a valid floating point number field copy");
1712 /* validate s copy */
1713 str_val
= bt_ctf_field_string_get_value(s_copy
);
1714 ok(str_val
&& !strcmp(str_val
, "btbt"),
1715 "bt_ctf_field_copy creates a valid string field copy");
1717 /* validate e_int copy */
1718 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1721 ok(uint64_t_val
== 23,
1722 "bt_ctf_field_copy creates a valid enum's integer field copy");
1724 /* validate e copy */
1725 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1726 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1727 "bt_ctf_field_copy creates a valid enum field copy");
1729 /* validate v_selected copy */
1730 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1731 assert(v_selected_copy_len
);
1732 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1735 ok(uint64_t_val
== 7,
1736 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1737 bt_put(v_selected_copy_len
);
1738 v_selected_copy_len
= NULL
;
1740 /* validate v_selected copy fields */
1741 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1744 ok(uint64_t_val
== 7,
1745 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1746 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1749 ok(uint64_t_val
== 6,
1750 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1751 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1754 ok(uint64_t_val
== 5,
1755 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1756 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1759 ok(uint64_t_val
== 4,
1760 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1761 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1764 ok(uint64_t_val
== 3,
1765 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1766 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1769 ok(uint64_t_val
== 2,
1770 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1771 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1774 ok(uint64_t_val
== 1,
1775 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1777 /* validate a copy fields */
1778 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1779 ok(str_val
&& !strcmp(str_val
, "a_0"),
1780 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1781 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1782 ok(str_val
&& !strcmp(str_val
, "a_1"),
1783 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1784 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1785 ok(str_val
&& !strcmp(str_val
, "a_2"),
1786 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1787 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1788 ok(str_val
&& !strcmp(str_val
, "a_3"),
1789 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1790 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1791 ok(str_val
&& !strcmp(str_val
, "a_4"),
1792 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1794 /* put everything */
1801 bt_put(v_label1_type
);
1802 bt_put(v_label1_array_type
);
1803 bt_put(v_label2_type
);
1804 bt_put(v_label2_seq_type
);
1813 bt_put(v_selected_0
);
1814 bt_put(v_selected_1
);
1815 bt_put(v_selected_2
);
1816 bt_put(v_selected_3
);
1817 bt_put(v_selected_4
);
1818 bt_put(v_selected_5
);
1819 bt_put(v_selected_6
);
1833 bt_put(v_selected_copy
);
1834 bt_put(v_selected_0_copy
);
1835 bt_put(v_selected_1_copy
);
1836 bt_put(v_selected_2_copy
);
1837 bt_put(v_selected_3_copy
);
1838 bt_put(v_selected_4_copy
);
1839 bt_put(v_selected_5_copy
);
1840 bt_put(v_selected_6_copy
);
1851 void type_field_tests()
1853 struct bt_ctf_field
*uint_12
;
1854 struct bt_ctf_field
*int_16
;
1855 struct bt_ctf_field
*string
;
1856 struct bt_ctf_field
*enumeration
;
1857 struct bt_ctf_field_type
*composite_structure_type
;
1858 struct bt_ctf_field_type
*structure_seq_type
;
1859 struct bt_ctf_field_type
*string_type
;
1860 struct bt_ctf_field_type
*sequence_type
;
1861 struct bt_ctf_field_type
*uint_8_type
;
1862 struct bt_ctf_field_type
*int_16_type
;
1863 struct bt_ctf_field_type
*uint_12_type
=
1864 bt_ctf_field_type_integer_create(12);
1865 struct bt_ctf_field_type
*enumeration_type
;
1866 struct bt_ctf_field_type
*returned_type
;
1867 const char *ret_string
;
1869 returned_type
= bt_ctf_field_get_type(NULL
);
1870 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1872 ok(uint_12_type
, "Create an unsigned integer type");
1873 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1874 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1875 "Set integer type's base as binary");
1876 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1877 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1878 "Set integer type's base as decimal");
1879 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1880 BT_CTF_INTEGER_BASE_UNKNOWN
),
1881 "Reject integer type's base set as unknown");
1882 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1883 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1884 "Set integer type's base as octal");
1885 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1886 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1887 "Set integer type's base as hexadecimal");
1888 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1889 "Reject unknown integer base value");
1890 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1891 "Set integer type signedness to signed");
1892 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1893 "Set integer type signedness to unsigned");
1894 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1895 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1896 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1897 "bt_ctf_field_type_integer_get_size returns a correct value");
1898 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1899 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1900 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1901 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1903 ok(bt_ctf_field_type_set_byte_order(NULL
,
1904 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1905 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1906 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1907 (enum bt_ctf_byte_order
) 42) < 0,
1908 "bt_ctf_field_type_set_byte_order rejects invalid values");
1909 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1910 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1911 "Set an integer's byte order to little endian");
1912 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1913 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1914 "Set an integer's byte order to big endian");
1915 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1916 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1917 "bt_ctf_field_type_get_byte_order returns a correct value");
1918 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1919 BT_CTF_BYTE_ORDER_UNKNOWN
,
1920 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1922 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1923 BT_CTF_TYPE_ID_UNKNOWN
,
1924 "bt_ctf_field_type_get_type_id handles NULL correctly");
1925 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1926 BT_CTF_TYPE_ID_INTEGER
,
1927 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1929 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1930 BT_CTF_INTEGER_BASE_UNKNOWN
,
1931 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1932 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1933 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1934 "bt_ctf_field_type_integer_get_base returns a correct value");
1936 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1937 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1938 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1939 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1940 (enum bt_ctf_string_encoding
) 123) < 0,
1941 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1942 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1943 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1944 "Set integer type encoding to UTF8");
1945 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1946 BT_CTF_STRING_ENCODING_UNKNOWN
,
1947 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1948 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1949 BT_CTF_STRING_ENCODING_UTF8
,
1950 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1952 int_16_type
= bt_ctf_field_type_integer_create(16);
1953 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1954 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1955 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1956 uint_8_type
= bt_ctf_field_type_integer_create(8);
1958 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1959 ok(sequence_type
, "Create a sequence of int16_t type");
1960 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1961 BT_CTF_TYPE_ID_SEQUENCE
,
1962 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1964 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1965 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1966 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1968 ok(!strcmp(ret_string
, "seq_len"),
1969 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1970 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1971 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1972 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1974 ok(returned_type
== int_16_type
,
1975 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1976 bt_put(returned_type
);
1978 string_type
= bt_ctf_field_type_string_create();
1979 ok(string_type
, "Create a string type");
1980 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1981 BT_CTF_STRING_ENCODING_NONE
),
1982 "Reject invalid \"None\" string encoding");
1983 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1985 "Reject invalid string encoding");
1986 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1987 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1988 "Set string encoding to ASCII");
1990 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1991 BT_CTF_STRING_ENCODING_UNKNOWN
,
1992 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1993 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1994 BT_CTF_STRING_ENCODING_ASCII
,
1995 "bt_ctf_field_type_string_get_encoding returns the correct value");
1997 structure_seq_type
= bt_ctf_field_type_structure_create();
1998 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1999 BT_CTF_TYPE_ID_STRUCT
,
2000 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
2001 ok(structure_seq_type
, "Create a structure type");
2002 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
2003 uint_8_type
, "seq_len") == 0,
2004 "Add a uint8_t type to a structure");
2005 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
2006 sequence_type
, "a_sequence") == 0,
2007 "Add a sequence type to a structure");
2009 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
2010 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
2011 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
2012 "bt_ctf_field_type_structure_get_field_count returns a correct value");
2014 ok(bt_ctf_field_type_structure_get_field(NULL
,
2015 &ret_string
, &returned_type
, 1) < 0,
2016 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2017 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2018 NULL
, &returned_type
, 1) == 0,
2019 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2020 bt_put(returned_type
);
2021 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2022 &ret_string
, NULL
, 1) == 0,
2023 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2024 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2025 &ret_string
, &returned_type
, 10) < 0,
2026 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2027 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2028 &ret_string
, &returned_type
, 1) == 0,
2029 "bt_ctf_field_type_structure_get_field returns a field");
2030 ok(!strcmp(ret_string
, "a_sequence"),
2031 "bt_ctf_field_type_structure_get_field returns a correct field name");
2032 ok(returned_type
== sequence_type
,
2033 "bt_ctf_field_type_structure_get_field returns a correct field type");
2034 bt_put(returned_type
);
2036 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2037 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2038 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2039 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2040 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2041 structure_seq_type
, "a_sequence");
2042 ok(returned_type
== sequence_type
,
2043 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2044 bt_put(returned_type
);
2046 composite_structure_type
= bt_ctf_field_type_structure_create();
2047 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2048 string_type
, "a_string") == 0,
2049 "Add a string type to a structure");
2050 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2051 structure_seq_type
, "inner_structure") == 0,
2052 "Add a structure type to a structure");
2054 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2055 NULL
, "a_sequence") == NULL
,
2056 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2057 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2058 structure_seq_type
, NULL
) == NULL
,
2059 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2060 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2061 structure_seq_type
, "a_sequence");
2062 ok(returned_type
== sequence_type
,
2063 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2064 bt_put(returned_type
);
2066 int_16
= bt_ctf_field_create(int_16_type
);
2067 ok(int_16
, "Instanciate a signed 16-bit integer");
2068 uint_12
= bt_ctf_field_create(uint_12_type
);
2069 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2070 returned_type
= bt_ctf_field_get_type(int_16
);
2071 ok(returned_type
== int_16_type
,
2072 "bt_ctf_field_get_type returns the correct type");
2074 /* Can't modify types after instanciating them */
2075 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2076 BT_CTF_INTEGER_BASE_DECIMAL
),
2077 "Check an integer type' base can't be modified after instanciation");
2078 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2079 "Check an integer type's signedness can't be modified after instanciation");
2081 /* Check signed property is checked */
2082 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2083 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2084 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2085 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2087 /* Check overflows are properly tested for */
2088 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2089 "Check -32768 is allowed for a signed 16-bit integer");
2090 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2091 "Check 32767 is allowed for a signed 16-bit integer");
2092 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2093 "Check 32768 is not allowed for a signed 16-bit integer");
2094 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2095 "Check -32769 is not allowed for a signed 16-bit integer");
2096 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2097 "Check -42 is allowed for a signed 16-bit integer");
2099 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2100 "Check 4095 is allowed for an unsigned 12-bit integer");
2101 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2102 "Check 4096 is not allowed for a unsigned 12-bit integer");
2103 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2104 "Check 0 is allowed for an unsigned 12-bit integer");
2106 string
= bt_ctf_field_create(string_type
);
2107 ok(string
, "Instanciate a string field");
2108 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2109 "Set a string's value");
2111 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2112 ok(enumeration_type
,
2113 "Create an enumeration type with an unsigned 12-bit integer as container");
2114 enumeration
= bt_ctf_field_create(enumeration_type
);
2116 "Check enumeration types are validated before instantiation");
2121 bt_put(enumeration
);
2122 bt_put(composite_structure_type
);
2123 bt_put(structure_seq_type
);
2124 bt_put(string_type
);
2125 bt_put(sequence_type
);
2126 bt_put(uint_8_type
);
2127 bt_put(int_16_type
);
2128 bt_put(uint_12_type
);
2129 bt_put(enumeration_type
);
2130 bt_put(returned_type
);
2134 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2135 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2138 * Append enough events to force the underlying packet to be resized.
2139 * Also tests that a new event can be declared after a stream has been
2140 * instantiated and used/flushed.
2144 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2146 struct bt_ctf_field_type
*integer_type
=
2147 bt_ctf_field_type_integer_create(17);
2148 struct bt_ctf_field_type
*string_type
=
2149 bt_ctf_field_type_string_create();
2150 struct bt_ctf_event
*event
= NULL
;
2151 struct bt_ctf_field
*ret_field
= NULL
;
2152 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2153 uint64_t ret_uint64
;
2154 int events_appended
= 0;
2155 struct bt_ctf_field
*packet_context
= NULL
,
2156 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
2157 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2158 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2159 struct bt_ctf_field_type
*ep_type
= NULL
;
2161 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2163 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2165 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2166 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2172 * bt_ctf_stream_class_add_event_class() copies the field types
2173 * of event_class, so we retrieve the new ones to create the
2174 * appropriate fields.
2176 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2178 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2179 ep_type
, "field_1");
2180 assert(ep_field_1_type
);
2181 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2182 ep_type
, "a_string");
2183 assert(ep_a_string_type
);
2185 event
= bt_ctf_event_create(event_class
);
2186 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2187 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2188 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2189 "bt_ctf_event_get_payload_by_index returns a correct field");
2190 bt_put(ret_field_type
);
2193 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2194 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2195 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2196 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2199 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2200 event
= bt_ctf_event_create(event_class
);
2201 struct bt_ctf_field
*integer
=
2202 bt_ctf_field_create(ep_field_1_type
);
2203 struct bt_ctf_field
*string
=
2204 bt_ctf_field_create(ep_a_string_type
);
2206 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2207 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2208 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2211 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2212 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2216 /* Populate stream event context */
2217 stream_event_context
=
2218 bt_ctf_event_get_stream_event_context(event
);
2219 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2220 "common_event_context");
2221 BT_PUT(stream_event_context
);
2222 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2226 ret
|= bt_ctf_stream_append_event(stream
, event
);
2234 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2235 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2236 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2237 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2238 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2239 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2240 ok(ret
== 0 && ret_uint64
== 0,
2241 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2242 bt_ctf_stream_append_discarded_events(stream
, 1000);
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 when some were discarded");
2248 ok(events_appended
, "Append 100 000 events to a stream");
2251 * Populate the custom packet context field with a dummy value
2252 * otherwise flush will fail.
2254 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2255 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2256 "custom_packet_context_field");
2257 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2259 ok(bt_ctf_stream_flush(stream
) == 0,
2260 "Flush a stream that forces a packet resize");
2261 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2262 ok(ret
== 0 && ret_uint64
== 1000,
2263 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2264 bt_put(integer_type
);
2265 bt_put(string_type
);
2266 bt_put(packet_context
);
2267 bt_put(packet_context_field
);
2268 bt_put(stream_event_context
);
2269 bt_put(event_class
);
2270 bt_put(ep_field_1_type
);
2271 bt_put(ep_a_string_type
);
2276 void test_empty_stream(struct bt_ctf_writer
*writer
)
2279 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2280 struct bt_ctf_stream_class
*stream_class
= NULL
;
2281 struct bt_ctf_stream
*stream
= NULL
;
2283 trace
= bt_ctf_writer_get_trace(writer
);
2285 diag("Failed to get trace from writer");
2290 stream_class
= bt_ctf_stream_class_create("empty_stream");
2291 if (!stream_class
) {
2292 diag("Failed to create stream class");
2297 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2298 "bt_ctf_stream_class_get_trace handles NULL correctly");
2299 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2300 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2302 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2304 diag("Failed to create writer stream");
2309 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2310 ok(ret_trace
== trace
,
2311 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2314 "Created a stream class with default attributes and an empty stream");
2318 bt_put(stream_class
);
2322 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2325 struct bt_ctf_trace
*trace
= NULL
;
2326 struct bt_ctf_clock
*clock
= NULL
;
2327 struct bt_ctf_stream_class
*stream_class
= NULL
;
2328 struct bt_ctf_stream
*stream
= NULL
;
2329 struct bt_ctf_field_type
*integer_type
= NULL
,
2330 *sequence_type
= NULL
, *event_header_type
= NULL
;
2331 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2332 *event_header
= NULL
, *packet_header
= NULL
;
2333 struct bt_ctf_event_class
*event_class
= NULL
;
2334 struct bt_ctf_event
*event
= NULL
;
2336 trace
= bt_ctf_writer_get_trace(writer
);
2338 fail("Failed to get trace from writer");
2342 clock
= bt_ctf_trace_get_clock(trace
, 0);
2344 fail("Failed to get clock from trace");
2348 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2349 if (!stream_class
) {
2350 fail("Failed to create stream class");
2354 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2356 fail("Failed to set stream class clock");
2361 * Customize event header to add an "seq_len" integer member
2362 * which will be used as the length of a sequence in an event of this
2365 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2367 if (!event_header_type
) {
2368 fail("Failed to get event header type");
2372 integer_type
= bt_ctf_field_type_integer_create(13);
2373 if (!integer_type
) {
2374 fail("Failed to create length integer type");
2378 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2379 integer_type
, "seq_len");
2381 fail("Failed to add a new field to stream event header");
2385 event_class
= bt_ctf_event_class_create("sequence_event");
2387 fail("Failed to create event class");
2392 * This event's payload will contain a sequence which references
2393 * stream.event.header.seq_len as its length field.
2395 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2396 "stream.event.header.seq_len");
2397 if (!sequence_type
) {
2398 fail("Failed to create a sequence");
2402 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2405 fail("Failed to add a sequence to an event class");
2409 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2411 fail("Failed to add event class to stream class");
2415 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2417 fail("Failed to create stream")
2422 * We have defined a custom packet header field. We have to populate it
2425 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2426 if (!packet_header
) {
2427 fail("Failed to get stream packet header");
2431 integer
= bt_ctf_field_structure_get_field(packet_header
,
2432 "custom_trace_packet_header_field");
2434 fail("Failed to retrieve custom_trace_packet_header_field");
2438 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2440 fail("Failed to set custom_trace_packet_header_field value");
2445 event
= bt_ctf_event_create(event_class
);
2447 fail("Failed to create event");
2451 event_header
= bt_ctf_event_get_header(event
);
2452 if (!event_header
) {
2453 fail("Failed to get event header");
2457 integer
= bt_ctf_field_structure_get_field(event_header
,
2460 fail("Failed to get seq_len field from event header");
2464 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2466 fail("Failed to set seq_len value in event header");
2470 /* Populate both sequence integer fields */
2471 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2473 fail("Failed to retrieve sequence from event");
2477 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2479 fail("Failed to set sequence length");
2484 for (i
= 0; i
< 2; i
++) {
2485 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2487 fail("Failed to retrieve sequence element");
2491 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2493 fail("Failed to set sequence element value");
2501 ret
= bt_ctf_stream_append_event(stream
, event
);
2503 fail("Failed to append event to stream");
2507 ret
= bt_ctf_stream_flush(stream
);
2509 fail("Failed to flush custom_event_header stream");
2515 bt_put(stream_class
);
2516 bt_put(event_class
);
2520 bt_put(event_header
);
2521 bt_put(packet_header
);
2522 bt_put(sequence_type
);
2523 bt_put(integer_type
);
2524 bt_put(event_header_type
);
2528 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2531 struct bt_ctf_trace
*trace
= NULL
;
2532 struct bt_ctf_clock
*clock
= NULL
;
2533 struct bt_ctf_stream_class
*stream_class
= NULL
;
2534 struct bt_ctf_stream
*stream
= NULL
,
2536 struct bt_ctf_event_class
*event_class
= NULL
;
2537 struct bt_ctf_event
*event
= NULL
;
2538 struct bt_ctf_field_type
*integer_type
= NULL
;
2539 struct bt_ctf_field
*integer
= NULL
;
2541 trace
= bt_ctf_writer_get_trace(writer
);
2543 diag("Failed to get trace from writer");
2548 clock
= bt_ctf_trace_get_clock(trace
, 0);
2550 diag("Failed to get clock from trace");
2555 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2556 if (!stream_class
) {
2557 diag("Failed to create stream class");
2562 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2564 diag("Failed to set stream class clock");
2568 event_class
= bt_ctf_event_class_create("some_event_class_name");
2569 integer_type
= bt_ctf_field_type_integer_create(32);
2570 if (!integer_type
) {
2571 diag("Failed to create integer field type");
2576 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2579 diag("Failed to add field to event class");
2583 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2586 diag("Failed to add event class to stream class");
2589 event
= bt_ctf_event_create(event_class
);
2591 diag("Failed to create event");
2596 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2598 diag("Failed to get integer field payload from event");
2603 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2605 diag("Failed to set integer field value");
2609 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2611 diag("Failed to create writer stream");
2616 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2617 "bt_ctf_event_get_stream handles NULL correctly");
2618 ok(bt_ctf_event_get_stream(event
) == NULL
,
2619 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2621 ret
= bt_ctf_stream_append_event(stream
, event
);
2623 diag("Failed to append event to stream");
2627 ret_stream
= bt_ctf_event_get_stream(event
);
2628 ok(ret_stream
== stream
,
2629 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2632 "Create an event before instanciating its associated stream");
2636 bt_put(stream_class
);
2637 bt_put(event_class
);
2639 bt_put(integer_type
);
2645 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2647 struct bt_ctf_event_class
*event_class
;
2649 event_class
= bt_ctf_event_class_create("Simple Event");
2650 assert(event_class
);
2651 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2652 "two event classes with the same name cannot cohabit within the same stream class");
2653 bt_put(event_class
);
2655 event_class
= bt_ctf_event_class_create("different name, ok");
2656 assert(event_class
);
2657 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2658 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2659 "two event classes with the same ID cannot cohabit within the same stream class");
2660 bt_put(event_class
);
2664 void test_trace_stream_class_clock(void)
2666 struct bt_ctf_trace
*trace
= NULL
;
2667 struct bt_ctf_stream_class
*sc1
= NULL
;
2668 struct bt_ctf_stream_class
*sc2
= NULL
;
2669 struct bt_ctf_clock
*sc1_clock
= NULL
;
2670 struct bt_ctf_clock
*sc2_clock
= NULL
;
2671 const char *clock_name
= "hello";
2673 trace
= bt_ctf_trace_create();
2675 sc1
= bt_ctf_stream_class_create(NULL
);
2677 sc2
= bt_ctf_stream_class_create(NULL
);
2679 sc1_clock
= bt_ctf_clock_create(clock_name
);
2681 sc2_clock
= bt_ctf_clock_create(clock_name
);
2684 ok(!bt_ctf_stream_class_set_clock(sc1
, sc1_clock
),
2685 "bt_ctf_stream_class_set_clock() succeeds for sc1");
2686 ok(!bt_ctf_stream_class_set_clock(sc2
, sc2_clock
),
2687 "bt_ctf_stream_class_set_clock() succeeds for sc2");
2688 ok(!bt_ctf_trace_add_stream_class(trace
, sc1
),
2689 "bt_ctf_trace_add_stream_class() succeeds with sc1");
2690 ok(bt_ctf_trace_add_stream_class(trace
, sc2
),
2691 "bt_ctf_trace_add_stream_class() fails with sc2 (different clock, same name)");
2701 struct bt_ctf_event_class
*create_minimal_event_class(void)
2703 struct bt_ctf_event_class
*ec
= NULL
;
2704 struct bt_ctf_field_type
*int_ft
= NULL
;
2707 int_ft
= bt_ctf_field_type_integer_create(23);
2709 ec
= bt_ctf_event_class_create("minimal");
2711 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2719 void test_create_writer_vs_non_writer_mode(void)
2722 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2723 const char *writer_stream_name
= "writer stream instance";
2724 struct bt_ctf_writer
*writer
= NULL
;
2725 struct bt_ctf_trace
*writer_trace
= NULL
;
2726 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2727 struct bt_ctf_stream
*writer_stream
= NULL
;
2728 struct bt_ctf_stream
*writer_stream2
= NULL
;
2729 struct bt_ctf_stream
*packet_stream
= NULL
;
2730 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2731 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2732 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2733 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2734 struct bt_ctf_event_class
*writer_ec
= NULL
;
2735 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2736 struct bt_ctf_event
*event
= NULL
;
2737 struct bt_ctf_event
*event2
= NULL
;
2738 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2739 struct bt_ctf_field
*int_field
= NULL
;
2740 struct bt_ctf_clock
*writer_clock
= NULL
;
2741 struct bt_ctf_clock
*non_writer_clock
= NULL
;
2742 struct bt_ctf_packet
*packet
= NULL
;
2743 struct bt_ctf_packet
*packet2
= NULL
;
2745 if (!bt_mkdtemp(trace_path
)) {
2749 /* Create empty structure field type (event header) */
2750 empty_struct_ft
= bt_ctf_field_type_structure_create();
2751 assert(empty_struct_ft
);
2753 /* Create writer, writer stream class, stream, and clock */
2754 writer
= bt_ctf_writer_create(trace_path
);
2756 writer_trace
= bt_ctf_writer_get_trace(writer
);
2757 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2758 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2760 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2763 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2765 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2766 assert(writer_stream
);
2767 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2768 "bt_ctf_stream_get_name() returns the stream's name");
2769 writer_clock
= bt_ctf_clock_create("writer_clock");
2770 assert(writer_clock
);
2771 ret
= bt_ctf_trace_add_clock(writer_trace
, writer_clock
);
2774 /* Create non-writer trace, stream class, stream, and clock */
2775 non_writer_trace
= bt_ctf_trace_create();
2776 assert(non_writer_trace
);
2777 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2778 assert(non_writer_sc
);
2779 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2782 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2784 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2785 assert(non_writer_stream
);
2786 non_writer_clock
= bt_ctf_clock_create("non_writer_clock");
2787 assert(non_writer_clock
);
2788 ret
= bt_ctf_trace_add_clock(non_writer_trace
, non_writer_clock
);
2791 /* Create event class and event */
2792 writer_ec
= create_minimal_event_class();
2794 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2796 event
= bt_ctf_event_create(writer_ec
);
2798 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2800 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2803 * Verify non-writer stream: it should be impossible to append
2806 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2807 "bt_ctf_stream_append_event() fails with a non-writer stream");
2810 * Verify writer stream: it should be possible to append an
2813 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2814 "bt_ctf_stream_append_event() succeeds with a writer stream");
2817 * Verify that it's possible to get and set the value of a
2818 * writer mode clock.
2820 ok (!bt_ctf_clock_set_value(writer_clock
, 1000),
2821 "bt_ctf_clock_set_value() succeeds with a writer mode clock");
2822 ok (bt_ctf_clock_get_value(writer_clock
) == 1000,
2823 "bt_ctf_clock_get_value() succeeds with a writer mode clock");
2826 * Verify that it's impossible to get and set the value of a
2827 * non-writer mode clock.
2829 ok (bt_ctf_clock_set_value(non_writer_clock
, 1000),
2830 "bt_ctf_clock_set_value() fails with a non-writer mode clock");
2831 ok (bt_ctf_clock_get_value(non_writer_clock
) == -1ULL,
2832 "bt_ctf_clock_get_value() fails with a non-writer mode clock");
2835 * It should be possible to create a packet from a non-writer
2836 * stream, but not from a writer stream.
2838 packet
= bt_ctf_packet_create(writer_stream
);
2839 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2840 packet
= bt_ctf_packet_create(non_writer_stream
);
2841 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2842 packet_stream
= bt_ctf_packet_get_stream(packet
);
2843 ok(packet_stream
== non_writer_stream
,
2844 "bt_ctf_packet_get_stream() returns the correct stream");
2847 * It should not be possible to append an event associated to
2848 * a stream to a different stream.
2850 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2851 assert(writer_stream2
);
2852 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2853 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2856 * It should not be possible to set the packet of an event
2857 * associated to a given stream to a packet associated with
2858 * a different stream.
2860 ok(bt_ctf_event_set_packet(event
, packet
),
2861 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2864 * It should be possible to set the packet of a fresh event, as
2865 * long as the originating stream classes are the same.
2867 event2
= bt_ctf_event_create(writer_ec
);
2869 ok(bt_ctf_event_set_packet(event2
, packet
),
2870 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2871 non_writer_ec
= create_minimal_event_class();
2872 assert(non_writer_ec
);
2873 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2876 event2
= bt_ctf_event_create(non_writer_ec
);
2878 ok(!bt_ctf_event_set_packet(event2
, packet
),
2879 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2882 * It should be possible to set a packet created from the same
2883 * stream to an event with an existing packet.
2885 packet2
= bt_ctf_packet_create(non_writer_stream
);
2887 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2888 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2892 * It should not be possible to set a packet created from a
2893 * different stream to an event with an existing packet.
2895 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2896 assert(non_writer_stream2
);
2897 packet2
= bt_ctf_packet_create(non_writer_stream
);
2899 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2900 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2903 bt_put(writer_trace
);
2905 bt_put(writer_stream
);
2906 bt_put(writer_stream2
);
2907 bt_put(non_writer_trace
);
2908 bt_put(non_writer_sc
);
2909 bt_put(non_writer_stream
);
2910 bt_put(non_writer_stream2
);
2911 bt_put(packet_stream
);
2913 bt_put(non_writer_ec
);
2917 bt_put(empty_struct_ft
);
2918 bt_put(writer_clock
);
2919 bt_put(non_writer_clock
);
2922 delete_trace(trace_path
);
2926 void test_clock_utils(void)
2929 struct bt_ctf_clock
*clock
= NULL
;
2931 clock
= bt_ctf_clock_create("water");
2933 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2935 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2937 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2939 ok(bt_ctf_clock_ns_from_value(clock
, 4321) == 1234000005321ULL,
2940 "bt_ctf_clock_ns_from_value() returns the correct value with a 1 GHz frequency");
2941 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2943 ok(bt_ctf_clock_ns_from_value(clock
, 4321) ==
2944 (uint64_t) 1237468709256.845,
2945 "bt_ctf_clock_ns_from_value() returns the correct value with a non-1 GHz frequency");
2950 int main(int argc
, char **argv
)
2952 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2953 char metadata_path
[sizeof(trace_path
) + 9];
2954 const char *clock_name
= "test_clock";
2955 const char *clock_description
= "This is a test clock";
2956 const char *returned_clock_name
;
2957 const char *returned_clock_description
;
2958 const uint64_t frequency
= 1123456789;
2959 const int64_t offset_s
= 1351530929945824323;
2960 const int64_t offset
= 1234567;
2961 int64_t get_offset_s
,
2964 const uint64_t precision
= 10;
2965 const int is_absolute
= 0xFF;
2966 char *metadata_string
;
2967 struct bt_ctf_writer
*writer
;
2968 struct utsname name
;
2969 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2970 struct bt_ctf_clock
*clock
, *ret_clock
;
2971 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2972 struct bt_ctf_stream
*stream1
;
2973 const char *ret_string
;
2974 const unsigned char *ret_uuid
;
2975 unsigned char tmp_uuid
[16] = { 0 };
2976 struct bt_ctf_field_type
*packet_context_type
,
2977 *packet_context_field_type
,
2978 *packet_header_type
,
2979 *packet_header_field_type
,
2981 *stream_event_context_type
,
2983 *event_header_field_type
;
2984 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2985 struct bt_ctf_trace
*trace
;
2987 int64_t ret_int64_t
;
2988 struct bt_value
*obj
;
2991 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2995 plan_tests(NR_TESTS
);
2997 if (!bt_mkdtemp(trace_path
)) {
3001 strcpy(metadata_path
, trace_path
);
3002 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
3004 writer
= bt_ctf_writer_create(trace_path
);
3005 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
3007 ok(!bt_ctf_writer_get_trace(NULL
),
3008 "bt_ctf_writer_get_trace correctly handles NULL");
3009 trace
= bt_ctf_writer_get_trace(writer
);
3011 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
3012 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
3013 "Set a trace's byte order to big endian");
3014 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
3015 "bt_ctf_trace_get_byte_order returns a correct endianness");
3017 /* Add environment context to the trace */
3018 ret
= gethostname(hostname
, sizeof(hostname
));
3022 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
3023 "Add host (%s) environment field to writer instance",
3025 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
3027 "bt_ctf_writer_add_environment_field error with NULL writer");
3028 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
3030 "bt_ctf_writer_add_environment_field error with NULL field name");
3031 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
3033 "bt_ctf_writer_add_environment_field error with NULL field value");
3035 /* Test bt_ctf_trace_set_environment_field with an integer object */
3036 obj
= bt_value_integer_create_init(23);
3038 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
3039 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
3040 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
3041 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
3042 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
3043 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
3044 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
3045 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
3048 /* Test bt_ctf_trace_set_environment_field with a string object */
3049 obj
= bt_value_string_create_init("the value");
3051 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
3052 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
3055 /* Test bt_ctf_trace_set_environment_field_integer */
3056 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
3058 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
3059 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
3060 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
3061 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3063 "bt_ctf_trace_set_environment_field_integer succeeds");
3065 /* Test bt_ctf_trace_set_environment_field_string */
3066 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
3068 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
3069 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
3070 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
3071 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3073 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
3074 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3076 "bt_ctf_trace_set_environment_field_string succeeds");
3078 /* Test bt_ctf_trace_get_environment_field_count */
3079 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
3080 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
3081 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3082 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
3084 /* Test bt_ctf_trace_get_environment_field_name */
3085 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
3086 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
3087 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
3088 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
3089 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
3090 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
3091 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
3092 ok(ret_string
&& !strcmp(ret_string
, "host"),
3093 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3094 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
3095 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
3096 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3097 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
3098 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
3099 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3100 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
3101 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
3102 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3103 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
3104 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
3105 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3107 /* Test bt_ctf_trace_get_environment_field_value */
3108 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
3109 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
3110 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
3111 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
3112 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
3113 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
3114 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
3115 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3116 ok(!ret
&& ret_int64_t
== 23,
3117 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
3119 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
3120 ret
= bt_value_string_get(obj
, &ret_string
);
3121 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
3122 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
3125 /* Test bt_ctf_trace_get_environment_field_value_by_name */
3126 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
3128 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3129 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3130 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3131 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3132 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3133 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3135 ret
= bt_value_string_get(obj
, &ret_string
);
3136 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3137 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3140 /* Test environment field replacement */
3141 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3143 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3144 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3145 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3146 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
3147 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3148 ok(!ret
&& ret_int64_t
== 654321,
3149 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3152 /* On Solaris, uname() can return any positive value on success */
3153 if (uname(&name
) < 0) {
3158 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3159 == 0, "Add sysname (%s) environment field to writer instance",
3161 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3162 name
.nodename
) == 0,
3163 "Add nodename (%s) environment field to writer instance",
3165 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3166 == 0, "Add release (%s) environment field to writer instance",
3168 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3169 == 0, "Add version (%s) environment field to writer instance",
3171 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3172 == 0, "Add machine (%s) environment field to writer istance",
3175 /* Define a clock and add it to the trace */
3176 ok(bt_ctf_clock_create("signed") == NULL
,
3177 "Illegal clock name rejected");
3178 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
3179 clock
= bt_ctf_clock_create(clock_name
);
3180 ok(clock
, "Clock created sucessfully");
3181 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3182 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3183 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3184 "Returned clock name is valid");
3186 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3187 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3188 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3189 "Clock description set successfully");
3191 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3192 ok(returned_clock_description
,
3193 "bt_ctf_clock_get_description returns a description.");
3194 ok(returned_clock_description
?
3195 !strcmp(returned_clock_description
, clock_description
) : 0,
3196 "Returned clock description is valid");
3198 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3199 "bt_ctf_clock_get_frequency returns the correct default frequency");
3200 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3201 "Set clock frequency");
3202 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3203 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3205 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3206 "bt_ctf_clock_get_offset_s succeeds");
3207 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3208 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3209 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3210 "Set clock offset (seconds)");
3211 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3212 "bt_ctf_clock_get_offset_s succeeds");
3213 ok(get_offset_s
== offset_s
,
3214 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3216 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3217 "bt_ctf_clock_get_offset succeeds");
3218 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3219 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3220 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3221 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3222 "bt_ctf_clock_get_offset succeeds");
3223 ok(get_offset
== offset
,
3224 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3226 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3227 "bt_ctf_clock_get_precision returns the correct default precision");
3228 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3229 "Set clock precision");
3230 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3231 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3233 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3234 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3235 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3236 "Set clock absolute property");
3237 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3238 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3240 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3241 "bt_ctf_clock_get_time succeeds");
3242 ok(get_time
== DEFAULT_CLOCK_TIME
,
3243 "bt_ctf_clock_get_time returns the correct default time");
3244 ok(bt_ctf_clock_get_value(clock
) == DEFAULT_CLOCK_VALUE
,
3245 "bt_ctf_clock_get_value returns the correct default value");
3246 ok(bt_ctf_clock_set_value(clock
, current_time
) == 0,
3248 ok(bt_ctf_clock_get_value(clock
) == current_time
,
3249 "bt_ctf_clock_get_value returns the correct value once it is set");
3250 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3252 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3253 "bt_ctf_clock_get_time succeeds");
3254 ok(get_time
>= current_time
- 1 && get_time
<= current_time
+ 1,
3255 "bt_ctf_clock_get_time returns the correct time once it is set");
3257 ok(!bt_ctf_clock_get_name(NULL
),
3258 "bt_ctf_clock_get_name correctly handles NULL");
3259 ok(!bt_ctf_clock_get_description(NULL
),
3260 "bt_ctf_clock_get_description correctly handles NULL");
3261 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3262 "bt_ctf_clock_get_frequency correctly handles NULL");
3263 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3264 "bt_ctf_clock_get_precision correctly handles NULL");
3265 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3266 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3267 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3268 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3269 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3270 "bt_ctf_clock_get_offset correctly handles NULL clock");
3271 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3272 "bt_ctf_clock_get_offset correctly handles NULL output");
3273 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3274 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3275 ok(bt_ctf_clock_get_time(NULL
, &get_time
) < 0,
3276 "bt_ctf_clock_get_time correctly handles NULL clock");
3277 ok(bt_ctf_clock_get_time(clock
, NULL
) < 0,
3278 "bt_ctf_clock_get_time correctly handles NULL output");
3280 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3281 "bt_ctf_clock_set_description correctly handles NULL clock");
3282 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3283 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3284 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3285 "bt_ctf_clock_get_precision correctly handles NULL clock");
3286 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3287 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3288 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3289 "bt_ctf_clock_set_offset correctly handles NULL clock");
3290 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3291 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3292 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3293 "bt_ctf_clock_set_time correctly handles NULL clock");
3294 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3295 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3296 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3298 "bt_ctf_clock_get_uuid returns a UUID");
3300 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3301 /* Slightly modify UUID */
3302 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3305 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3306 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3307 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3308 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3309 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3310 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3311 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3313 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3314 ok(uuid_match(ret_uuid
, tmp_uuid
),
3315 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3317 /* Define a stream class */
3318 stream_class
= bt_ctf_stream_class_create("test_stream");
3320 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3321 "bt_ctf_stream_class_get_name handles NULL correctly");
3322 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3323 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3324 "bt_ctf_stream_class_get_name returns a correct stream class name");
3326 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3327 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3328 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3329 "bt_ctf_stream_class_get_clock handles NULL correctly");
3331 ok(stream_class
, "Create stream class");
3332 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3333 "Set a stream class' clock");
3334 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3335 ok(ret_clock
== clock
,
3336 "bt_ctf_stream_class_get_clock returns a correct clock");
3339 /* Test the event fields and event types APIs */
3342 /* Test fields copying */
3345 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3346 "bt_ctf_stream_class_get_id returns an error when no id is set");
3347 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3348 "bt_ctf_stream_class_get_id handles NULL correctly");
3349 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3350 "bt_ctf_stream_class_set_id handles NULL correctly");
3351 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3352 "Set an stream class' id");
3353 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3354 "bt_ctf_stream_class_get_id returns the correct value");
3356 /* Validate default event header fields */
3357 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3358 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3359 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3362 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3363 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_TYPE_ID_STRUCT
,
3364 "Default event header type is a structure");
3365 event_header_field_type
=
3366 bt_ctf_field_type_structure_get_field_type_by_name(
3367 ret_field_type
, "id");
3368 ok(event_header_field_type
,
3369 "Default event header type contains an \"id\" field");
3370 ok(bt_ctf_field_type_get_type_id(
3371 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3372 "Default event header \"id\" field is an integer");
3373 bt_put(event_header_field_type
);
3374 event_header_field_type
=
3375 bt_ctf_field_type_structure_get_field_type_by_name(
3376 ret_field_type
, "timestamp");
3377 ok(event_header_field_type
,
3378 "Default event header type contains a \"timestamp\" field");
3379 ok(bt_ctf_field_type_get_type_id(
3380 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3381 "Default event header \"timestamp\" field is an integer");
3382 bt_put(event_header_field_type
);
3383 bt_put(ret_field_type
);
3385 /* Add a custom trace packet header field */
3386 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3387 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3388 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3389 ok(packet_header_type
,
3390 "bt_ctf_trace_get_packet_header_type returns a packet header");
3391 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_TYPE_ID_STRUCT
,
3392 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3393 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3394 packet_header_type
, "magic");
3395 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3396 bt_put(ret_field_type
);
3397 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3398 packet_header_type
, "uuid");
3399 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3400 bt_put(ret_field_type
);
3401 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3402 packet_header_type
, "stream_id");
3403 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3404 bt_put(ret_field_type
);
3406 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3407 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3408 packet_header_field_type
, "custom_trace_packet_header_field"),
3409 "Added a custom trace packet header field successfully");
3411 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3412 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3413 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3414 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3415 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3416 "Set a trace packet_header_type successfully");
3418 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3419 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3421 /* Add a custom field to the stream class' packet context */
3422 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3423 ok(packet_context_type
,
3424 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3425 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_TYPE_ID_STRUCT
,
3426 "Packet context is a structure");
3428 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3429 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3430 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3431 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3433 integer_type
= bt_ctf_field_type_integer_create(32);
3434 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3436 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3437 /* Create a "uint5_t" equivalent custom packet context field */
3438 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3440 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3441 packet_context_field_type
, "custom_packet_context_field");
3442 ok(ret
== 0, "Packet context field added successfully");
3444 /* Define a stream event context containing a my_integer field. */
3445 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3446 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3447 ok(bt_ctf_stream_class_get_event_context_type(
3448 stream_class
) == NULL
,
3449 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3450 stream_event_context_type
= bt_ctf_field_type_structure_create();
3451 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3452 integer_type
, "common_event_context");
3454 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3455 stream_event_context_type
) < 0,
3456 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3457 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3459 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3460 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3462 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3464 ok(bt_ctf_stream_class_set_event_context_type(
3465 stream_class
, stream_event_context_type
) == 0,
3466 "Set a new stream event context type");
3467 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3469 ok(ret_field_type
== stream_event_context_type
,
3470 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3471 bt_put(ret_field_type
);
3473 /* Instantiate a stream and append events */
3474 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3475 ok(stream1
, "Instanciate a stream class from writer");
3478 * Creating a stream through a writer adds the given stream
3479 * class to the writer's trace, thus registering the stream
3480 * class's clock to the trace.
3482 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
3483 "bt_ctf_trace_get_clock_count correctly handles NULL");
3484 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
3485 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3486 ok(!bt_ctf_trace_get_clock(NULL
, 0),
3487 "bt_ctf_trace_get_clock correctly handles NULL");
3488 ok(!bt_ctf_trace_get_clock(trace
, -1),
3489 "bt_ctf_trace_get_clock correctly handles negative indexes");
3490 ok(!bt_ctf_trace_get_clock(trace
, 1),
3491 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3492 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
3493 ok(ret_clock
== clock
,
3494 "bt_ctf_trace_get_clock returns the right clock instance");
3496 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
3497 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3498 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
3499 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3500 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
3501 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3502 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
3503 ok(ret_clock
== clock
,
3504 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3506 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
3507 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3509 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3510 "bt_ctf_stream_get_class correctly handles NULL");
3511 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3512 ok(ret_stream_class
,
3513 "bt_ctf_stream_get_class returns a stream class");
3514 ok(ret_stream_class
== stream_class
,
3515 "Returned stream class is of the correct type");
3518 * Packet header, packet context, event header, and stream
3519 * event context types were copied for the resolving
3522 BT_PUT(packet_header_type
);
3523 BT_PUT(packet_context_type
);
3524 BT_PUT(stream_event_context_type
);
3525 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3526 assert(packet_header_type
);
3527 packet_context_type
=
3528 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3529 assert(packet_context_type
);
3530 stream_event_context_type
=
3531 bt_ctf_stream_class_get_event_context_type(stream_class
);
3532 assert(stream_event_context_type
);
3535 * Try to modify the packet context type after a stream has been
3538 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3539 packet_header_field_type
, "should_fail");
3541 "Trace packet header type can't be modified once a stream has been instanciated");
3544 * Try to modify the packet context type after a stream has been
3547 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3548 packet_context_field_type
, "should_fail");
3550 "Packet context type can't be modified once a stream has been instanciated");
3553 * Try to modify the stream event context type after a stream has been
3556 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3557 integer_type
, "should_fail");
3559 "Stream event context type can't be modified once a stream has been instanciated");
3561 /* Should fail after instanciating a stream (frozen) */
3562 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3563 "Changes to a stream class that was already instantiated fail");
3565 /* Populate the custom packet header field only once for all tests */
3566 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3567 "bt_ctf_stream_get_packet_header handles NULL correctly");
3568 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3570 "bt_ctf_stream_get_packet_header returns a packet header");
3571 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3572 ok(ret_field_type
== packet_header_type
,
3573 "Stream returns a packet header of the appropriate type");
3574 bt_put(ret_field_type
);
3575 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3576 "custom_trace_packet_header_field");
3577 ok(packet_header_field
,
3578 "Packet header structure contains a custom field with the appropriate name");
3579 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3580 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3581 "Custom packet header field is of the expected type");
3582 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3583 54321), "Set custom packet header value successfully");
3584 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3585 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3586 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3587 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3588 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3589 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3590 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3591 "Successfully set a stream's packet header");
3593 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3594 "Add environment field to writer after stream creation");
3598 test_trace_stream_class_clock();
3600 test_create_writer_vs_non_writer_mode();
3602 test_instanciate_event_before_stream(writer
);
3604 append_simple_event(stream_class
, stream1
, clock
);
3606 packet_resize_test(stream_class
, stream1
, clock
);
3608 append_complex_event(stream_class
, stream1
, clock
);
3610 append_existing_event_class(stream_class
);
3612 test_empty_stream(writer
);
3614 test_custom_event_header_stream(writer
);
3616 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3617 ok(metadata_string
, "Get metadata string");
3619 bt_ctf_writer_flush_metadata(writer
);
3620 validate_metadata(argv
[1], metadata_path
);
3621 validate_trace(argv
[2], trace_path
);
3624 bt_put(ret_stream_class
);
3627 bt_put(packet_context_type
);
3628 bt_put(packet_context_field_type
);
3629 bt_put(integer_type
);
3630 bt_put(stream_event_context_type
);
3631 bt_put(ret_field_type
);
3632 bt_put(packet_header_type
);
3633 bt_put(packet_header_field_type
);
3634 bt_put(packet_header
);
3635 bt_put(packet_header_field
);
3637 free(metadata_string
);
3638 bt_put(stream_class
);
3640 delete_trace(trace_path
);