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. */
101 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
106 if (!uuid_a
|| !uuid_b
) {
110 for (i
= 0; i
< 16; i
++) {
111 if (uuid_a
[i
] != uuid_b
[i
]) {
121 void validate_metadata(char *parser_path
, char *metadata_path
)
124 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
125 int parser_output_fd
= -1, metadata_fd
= -1;
127 if (!metadata_path
) {
132 parser_output_fd
= mkstemp(parser_output_path
);
133 metadata_fd
= open(metadata_path
, O_RDONLY
);
135 unlink(parser_output_path
);
137 if (parser_output_fd
== -1 || metadata_fd
== -1) {
138 diag("Failed create temporary files for metadata parsing.");
146 waitpid(pid
, &status
, 0);
147 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
149 /* ctf-parser-test expects a metadata string on stdin. */
150 ret
= dup2(metadata_fd
, STDIN_FILENO
);
152 perror("# dup2 metadata_fd to STDIN");
156 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
158 perror("# dup2 parser_output_fd to STDOUT");
162 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
164 perror("# dup2 parser_output_fd to STDERR");
168 execl(parser_path
, "ctf-parser-test", NULL
);
169 perror("# Could not launch the ctf metadata parser process");
173 ok(ret
== 0, "Metadata string is valid");
175 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
177 size_t len
= METADATA_LINE_SIZE
;
178 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
180 metadata_fp
= fdopen(metadata_fd
, "r");
182 perror("fdopen on metadata_fd");
187 parser_output_fp
= fdopen(parser_output_fd
, "r");
188 if (!parser_output_fp
) {
189 perror("fdopen on parser_output_fd");
192 parser_output_fd
= -1;
196 diag("malloc failure");
201 /* Output the metadata and parser output as diagnostic */
202 while (bt_getline(&line
, &len
, metadata_fp
) > 0) {
203 fprintf(stderr
, "# %s", line
);
206 rewind(parser_output_fp
);
207 while (bt_getline(&line
, &len
, parser_output_fp
) > 0) {
208 fprintf(stderr
, "# %s", line
);
214 if (fclose(metadata_fp
)) {
215 diag("fclose failure");
218 if (parser_output_fp
) {
219 if (fclose(parser_output_fp
)) {
220 diag("fclose failure");
225 if (parser_output_fd
>= 0) {
226 if (close(parser_output_fd
)) {
230 if (metadata_fd
>= 0) {
231 if (close(metadata_fd
)) {
237 void validate_trace(char *parser_path
, char *trace_path
)
240 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
241 int babeltrace_output_fd
= -1;
248 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
249 unlink(babeltrace_output_path
);
251 if (babeltrace_output_fd
== -1) {
252 diag("Failed to create a temporary file for trace parsing.");
260 waitpid(pid
, &status
, 0);
261 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
263 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
265 perror("# dup2 babeltrace_output_fd to STDOUT");
269 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
271 perror("# dup2 babeltrace_output_fd to STDERR");
275 execl(parser_path
, "babeltrace", trace_path
, NULL
);
276 perror("# Could not launch the babeltrace process");
280 ok(ret
== 0, "Babeltrace could read the resulting trace");
282 if (ret
&& babeltrace_output_fd
>= 0) {
284 size_t len
= METADATA_LINE_SIZE
;
285 FILE *babeltrace_output_fp
= NULL
;
287 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
288 if (!babeltrace_output_fp
) {
289 perror("fdopen on babeltrace_output_fd");
292 babeltrace_output_fd
= -1;
296 diag("malloc error");
298 rewind(babeltrace_output_fp
);
299 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
305 if (babeltrace_output_fp
) {
306 if (fclose(babeltrace_output_fp
)) {
307 diag("fclose error");
312 if (babeltrace_output_fd
>= 0) {
313 if (close(babeltrace_output_fd
)) {
319 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
320 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
322 /* Create and add a simple event class */
323 struct bt_ctf_event_class
*simple_event_class
=
324 bt_ctf_event_class_create("Simple Event");
325 struct bt_ctf_field_type
*uint_12_type
=
326 bt_ctf_field_type_integer_create(12);
327 struct bt_ctf_field_type
*int_64_type
=
328 bt_ctf_field_type_integer_create(64);
329 struct bt_ctf_field_type
*float_type
=
330 bt_ctf_field_type_floating_point_create();
331 struct bt_ctf_field_type
*enum_type
;
332 struct bt_ctf_field_type
*enum_type_unsigned
=
333 bt_ctf_field_type_enumeration_create(uint_12_type
);
334 struct bt_ctf_field_type
*event_context_type
=
335 bt_ctf_field_type_structure_create();
336 struct bt_ctf_field_type
*event_payload_type
= NULL
;
337 struct bt_ctf_field_type
*returned_type
;
338 struct bt_ctf_event
*simple_event
;
339 struct bt_ctf_field
*integer_field
;
340 struct bt_ctf_field
*float_field
;
341 struct bt_ctf_field
*enum_field
;
342 struct bt_ctf_field
*enum_field_unsigned
;
343 struct bt_ctf_field
*enum_container_field
;
344 const char *mapping_name_test
= "truie";
345 const double double_test_value
= 3.1415;
346 struct bt_ctf_field
*enum_container_field_unsigned
;
347 const char *mapping_name_negative_test
= "negative_value";
348 const char *ret_char
;
350 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
351 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
352 struct bt_ctf_clock
*ret_clock
;
353 struct bt_ctf_event_class
*ret_event_class
;
354 struct bt_ctf_field
*packet_context
;
355 struct bt_ctf_field
*packet_context_field
;
356 struct bt_ctf_field
*stream_event_context
;
357 struct bt_ctf_field
*stream_event_context_field
;
358 struct bt_ctf_field
*event_context
;
359 struct bt_ctf_field
*event_context_field
;
360 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
361 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
362 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
365 ok(uint_12_type
, "Create an unsigned integer type");
367 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
368 ok(int_64_type
, "Create a signed integer type");
369 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
371 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
372 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
373 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
374 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
375 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
376 bt_put(returned_type
);
378 bt_ctf_field_type_set_alignment(float_type
, 32);
379 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
380 "bt_ctf_field_type_get_alignment handles NULL correctly");
381 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
382 "bt_ctf_field_type_get_alignment returns a correct value");
384 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
385 "Set a floating point type's exponent digit count");
386 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
387 "Set a floating point type's mantissa digit count");
389 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
390 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
391 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
392 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
393 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
394 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
395 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
396 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
398 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
399 mapping_name_negative_test
, -12345, 0) == 0,
400 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
401 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
402 "escaping; \"test\"", 1, 1) == 0,
403 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
404 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
405 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
406 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
407 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
408 "event clock int float", 5, 22) == 0,
409 "Accept enumeration mapping strings containing reserved keywords");
410 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
412 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
413 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
414 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
415 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
416 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
417 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
418 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
420 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
421 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
422 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
423 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
424 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
425 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
427 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
428 "enum_field") == 0, "Add signed enumeration field to event");
430 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
431 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
432 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
433 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
434 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
435 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
436 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
437 NULL
, &ret_range_end_int64_t
) < 0,
438 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
439 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
440 &ret_range_start_int64_t
, NULL
) < 0,
441 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
442 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
443 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
444 "bt_ctf_field_type_enumeration_get_mapping returns a value");
445 ok(!strcmp(ret_char
, mapping_name_test
),
446 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
447 ok(ret_range_start_int64_t
== 42,
448 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
449 ok(ret_range_end_int64_t
== 42,
450 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
452 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
453 "escaping; \"test\"", 0, 0) == 0,
454 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
455 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
456 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
457 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
458 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
459 "event clock int float", 5, 22) == 0,
460 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
461 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
463 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
464 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
465 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
466 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
467 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
468 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
469 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
470 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
472 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
473 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
474 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
475 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
477 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
478 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
479 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
480 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
481 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
482 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
483 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
484 NULL
, &ret_range_end_uint64_t
) < 0,
485 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
486 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
487 &ret_range_start_uint64_t
, NULL
) < 0,
488 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
489 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
490 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
491 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
492 ok(!strcmp(ret_char
, mapping_name_test
),
493 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
494 ok(ret_range_start_uint64_t
== 42,
495 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
496 ok(ret_range_end_uint64_t
== 42,
497 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
499 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
501 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
504 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
506 /* Set an event context type which will contain a single integer*/
507 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
508 "event_specific_context"),
509 "Add event specific context field");
510 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
511 "bt_ctf_event_class_get_context_type handles NULL correctly");
512 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
513 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
515 ok(bt_ctf_event_class_set_context_type(simple_event_class
, NULL
) < 0,
516 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
517 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
518 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
519 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
520 "Set an event class' context type successfully");
521 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
522 ok(returned_type
== event_context_type
,
523 "bt_ctf_event_class_get_context_type returns the appropriate type");
524 bt_put(returned_type
);
526 bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
);
529 * bt_ctf_stream_class_add_event_class() copies the field types
530 * of simple_event_class, so we retrieve the new ones to create
531 * the appropriate fields.
533 BT_PUT(event_context_type
);
534 BT_PUT(event_payload_type
);
535 event_payload_type
= bt_ctf_event_class_get_payload_type(
537 assert(event_payload_type
);
538 event_context_type
= bt_ctf_event_class_get_context_type(
540 assert(event_context_type
);
541 ep_integer_field_type
=
542 bt_ctf_field_type_structure_get_field_type_by_name(
543 event_payload_type
, "integer_field");
544 assert(ep_integer_field_type
);
546 bt_ctf_field_type_structure_get_field_type_by_name(
547 event_payload_type
, "enum_field");
548 assert(ep_enum_field_type
);
549 ep_enum_field_unsigned_type
=
550 bt_ctf_field_type_structure_get_field_type_by_name(
551 event_payload_type
, "enum_field_unsigned");
552 assert(ep_enum_field_unsigned_type
);
554 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
555 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
556 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
557 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
558 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
559 "bt_ctf_stream_class_get_event_class handles NULL correctly");
560 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
561 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
562 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
563 ok(ret_event_class
== simple_event_class
,
564 "bt_ctf_stream_class_get_event_class returns the correct event class");
565 bt_put(ret_event_class
);
566 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
567 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
568 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
569 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
571 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
572 ok(ret_event_class
== simple_event_class
,
573 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
574 bt_put(ret_event_class
);
576 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
577 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
578 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
579 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
580 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
581 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
582 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
583 ok(ret_event_class
== simple_event_class
,
584 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
585 bt_put(ret_event_class
);
587 simple_event
= bt_ctf_event_create(simple_event_class
);
589 "Instantiate an event containing a single integer field");
591 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
592 "bt_ctf_event_get_clock handles NULL correctly");
593 ret_clock
= bt_ctf_event_get_clock(simple_event
);
594 ok(ret_clock
== clock
,
595 "bt_ctf_event_get_clock returns a correct clock");
598 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
599 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
600 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
601 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
603 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
604 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
605 "bt_ctf_field_floating_point_get_value fails on an unset float field");
606 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
607 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
608 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
609 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
610 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
611 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
612 "bt_ctf_field_floating_point_get_value returns a double value");
613 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
614 "bt_ctf_field_floating_point_get_value returns a correct value");
616 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
618 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
619 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
620 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
621 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
622 enum_container_field
= bt_ctf_field_enumeration_get_container(
624 ok(bt_ctf_field_signed_integer_set_value(
625 enum_container_field
, -42) == 0,
626 "Set signed enumeration container value");
627 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
628 ok(!strcmp(ret_char
, mapping_name_negative_test
),
629 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
630 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
633 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
634 assert(enum_field_unsigned
);
635 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
636 enum_field_unsigned
);
637 ok(bt_ctf_field_unsigned_integer_set_value(
638 enum_container_field_unsigned
, 42) == 0,
639 "Set unsigned enumeration container value");
640 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
641 enum_field_unsigned
);
643 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
644 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
645 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
647 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
649 /* Populate stream event context */
650 stream_event_context
=
651 bt_ctf_event_get_stream_event_context(simple_event
);
652 assert(stream_event_context
);
653 stream_event_context_field
= bt_ctf_field_structure_get_field(
654 stream_event_context
, "common_event_context");
655 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
657 /* Populate the event's context */
658 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
659 "bt_ctf_event_get_event_context handles NULL correctly");
660 event_context
= bt_ctf_event_get_event_context(simple_event
);
662 "bt_ctf_event_get_event_context returns a field");
663 returned_type
= bt_ctf_field_get_type(event_context
);
664 ok(returned_type
== event_context_type
,
665 "bt_ctf_event_get_event_context returns a field of the appropriate type");
666 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
667 "event_specific_context");
668 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
669 "Successfully set an event context's value");
670 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
671 "bt_ctf_event_set_event_context handles a NULL event correctly");
672 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
673 "bt_ctf_event_set_event_context handles a NULL event context correctly");
674 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
675 "bt_ctf_event_set_event_context rejects a context of the wrong type");
676 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
677 "Set an event context successfully");
679 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
680 "Append simple event to trace stream");
682 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
683 "bt_ctf_stream_get_packet_context handles NULL correctly");
684 packet_context
= bt_ctf_stream_get_packet_context(stream
);
686 "bt_ctf_stream_get_packet_context returns a packet context");
688 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
690 ok(packet_context_field
,
691 "Packet context contains the default packet_size field.");
692 bt_put(packet_context_field
);
693 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
694 "custom_packet_context_field");
695 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
696 "Custom packet context field value successfully set.");
698 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
699 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
700 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
701 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
702 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
703 "Successfully set a stream's packet context");
705 ok(bt_ctf_stream_flush(stream
) == 0,
706 "Flush trace stream with one event");
708 bt_put(simple_event_class
);
709 bt_put(simple_event
);
710 bt_put(uint_12_type
);
714 bt_put(enum_type_unsigned
);
715 bt_put(returned_type
);
716 bt_put(event_context_type
);
717 bt_put(integer_field
);
720 bt_put(enum_field_unsigned
);
721 bt_put(enum_container_field
);
722 bt_put(enum_container_field_unsigned
);
723 bt_put(packet_context
);
724 bt_put(packet_context_field
);
725 bt_put(stream_event_context
);
726 bt_put(stream_event_context_field
);
727 bt_put(event_context
);
728 bt_put(event_context_field
);
729 bt_put(event_payload_type
);
730 bt_put(ep_integer_field_type
);
731 bt_put(ep_enum_field_type
);
732 bt_put(ep_enum_field_unsigned_type
);
735 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
736 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
738 struct event_class_attrs_counts
{
749 struct event_class_attrs_counts
;
750 const char *complex_test_event_string
= "Complex Test Event";
751 const char *test_string_1
= "Test ";
752 const char *test_string_2
= "string ";
753 const char *test_string_3
= "abcdefghi";
754 const char *test_string_4
= "abcd\0efg\0hi";
755 const char *test_string_cat
= "Test string abcdeabcd";
756 struct bt_ctf_field_type
*uint_35_type
=
757 bt_ctf_field_type_integer_create(35);
758 struct bt_ctf_field_type
*int_16_type
=
759 bt_ctf_field_type_integer_create(16);
760 struct bt_ctf_field_type
*uint_3_type
=
761 bt_ctf_field_type_integer_create(3);
762 struct bt_ctf_field_type
*enum_variant_type
=
763 bt_ctf_field_type_enumeration_create(uint_3_type
);
764 struct bt_ctf_field_type
*variant_type
=
765 bt_ctf_field_type_variant_create(enum_variant_type
,
767 struct bt_ctf_field_type
*string_type
=
768 bt_ctf_field_type_string_create();
769 struct bt_ctf_field_type
*sequence_type
;
770 struct bt_ctf_field_type
*array_type
;
771 struct bt_ctf_field_type
*inner_structure_type
=
772 bt_ctf_field_type_structure_create();
773 struct bt_ctf_field_type
*complex_structure_type
=
774 bt_ctf_field_type_structure_create();
775 struct bt_ctf_field_type
*ret_field_type
;
776 struct bt_ctf_event_class
*event_class
;
777 struct bt_ctf_event
*event
;
778 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
779 *inner_structure_field
, *complex_structure_field
,
780 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
781 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
782 *stream_event_ctx_int_field
, *ret_field
;
783 uint64_t ret_unsigned_int
;
784 int64_t ret_signed_int
;
785 const char *ret_string
;
786 struct bt_ctf_stream_class
*ret_stream_class
;
787 struct bt_ctf_event_class
*ret_event_class
;
788 struct bt_ctf_field
*packet_context
, *packet_context_field
;
789 struct bt_value
*obj
;
791 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
792 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
793 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
794 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
795 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
796 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
797 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
798 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
799 bt_ctf_field_type_set_alignment(int_16_type
, 32);
800 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
801 bt_ctf_field_type_integer_set_base(uint_35_type
,
802 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
804 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
805 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
808 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
809 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
810 ret_field_type
= bt_ctf_field_type_array_get_element_type(
812 ok(ret_field_type
== int_16_type
,
813 "bt_ctf_field_type_array_get_element_type returns the correct type");
814 bt_put(ret_field_type
);
816 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
817 "bt_ctf_field_type_array_get_length handles NULL correctly");
818 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
819 "bt_ctf_field_type_array_get_length returns the correct length");
821 bt_ctf_field_type_structure_add_field(inner_structure_type
,
822 uint_35_type
, "seq_len");
823 bt_ctf_field_type_structure_add_field(inner_structure_type
,
824 sequence_type
, "a_sequence");
825 bt_ctf_field_type_structure_add_field(inner_structure_type
,
826 array_type
, "an_array");
828 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
830 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
832 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
833 "UINT35_TYPE", 2, 7);
835 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
837 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
838 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
839 enum_variant_type
, NULL
) < 0,
840 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
841 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
842 enum_variant_type
, "INT16_TYPE") == 1,
843 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
845 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
846 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
847 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
848 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
849 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
850 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
852 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
853 "An unknown entry"), "Reject a variant field based on an unknown tag value");
854 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
855 "UINT3_TYPE") == 0, "Add a field to a variant");
856 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
858 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
861 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
862 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
863 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
864 ok(ret_field_type
== enum_variant_type
,
865 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
866 bt_put(ret_field_type
);
868 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
869 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
870 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
871 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
872 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
873 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
874 "INT16_TYPE") == NULL
,
875 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
876 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
878 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
879 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
880 variant_type
, "INT16_TYPE");
881 ok(ret_field_type
== int_16_type
,
882 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
883 bt_put(ret_field_type
);
885 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
886 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
887 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
888 "bt_ctf_field_type_variant_get_field_count returns the correct count");
890 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
891 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
892 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
893 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
894 bt_put(ret_field_type
);
895 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
896 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
897 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
898 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
899 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
900 "bt_ctf_field_type_variant_get_field returns a field");
901 ok(!strcmp("INT16_TYPE", ret_string
),
902 "bt_ctf_field_type_variant_get_field returns a correct field name");
903 ok(ret_field_type
== int_16_type
,
904 "bt_ctf_field_type_variant_get_field returns a correct field type");
905 bt_put(ret_field_type
);
907 bt_ctf_field_type_structure_add_field(complex_structure_type
,
908 enum_variant_type
, "variant_selector");
909 bt_ctf_field_type_structure_add_field(complex_structure_type
,
910 string_type
, "a_string");
911 bt_ctf_field_type_structure_add_field(complex_structure_type
,
912 variant_type
, "variant_value");
913 bt_ctf_field_type_structure_add_field(complex_structure_type
,
914 inner_structure_type
, "inner_structure");
916 ok(bt_ctf_event_class_create("clock") == NULL
,
917 "Reject creation of an event class with an illegal name");
918 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
919 ok(event_class
, "Create an event class");
920 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
921 "Reject addition of a field with an empty name to an event");
922 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
923 "Reject addition of a field with a NULL type to an event");
924 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
926 "Reject addition of a type with an illegal name to an event");
927 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
929 "Add field of type unsigned integer to an event");
930 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
931 "int_16") == 0, "Add field of type signed integer to an event");
932 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
933 "complex_structure") == 0,
934 "Add composite structure to an event");
936 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
937 "bt_ctf_event_class_get_name handles NULL correctly");
938 ret_string
= bt_ctf_event_class_get_name(event_class
);
939 ok(!strcmp(ret_string
, complex_test_event_string
),
940 "bt_ctf_event_class_get_name returns a correct name");
941 ok(bt_ctf_event_class_get_id(event_class
) < 0,
942 "bt_ctf_event_class_get_id returns a negative value when not set");
943 ok(bt_ctf_event_class_get_id(NULL
) < 0,
944 "bt_ctf_event_class_get_id handles NULL correctly");
945 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
946 "bt_ctf_event_class_set_id handles NULL correctly");
947 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
948 "Set an event class' id");
949 ok(bt_ctf_event_class_get_id(event_class
) == 42,
950 "bt_ctf_event_class_get_id returns the correct value");
952 /* Test event class attributes */
953 obj
= bt_value_integer_create_init(15);
955 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
956 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
957 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
958 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
959 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
960 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
961 assert(!bt_value_integer_set(obj
, -3));
962 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
963 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
964 assert(!bt_value_integer_set(obj
, 11));
965 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
966 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
967 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
968 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
969 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
971 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
974 obj
= bt_value_integer_create_init(5);
976 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
977 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
979 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
980 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
981 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
982 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
983 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
984 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
985 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
988 ret
= bt_value_integer_get(obj
, &int64_value
);
989 ok(obj
&& !ret
&& int64_value
== 5,
990 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
993 obj
= bt_value_string_create_init("nu name");
995 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
996 ret_string
= bt_ctf_event_class_get_name(event_class
);
997 ok(!strcmp(ret_string
, "nu name"),
998 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
999 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
1000 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
1002 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
1004 obj
= bt_value_string_create_init("http://kernel.org/");
1006 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
1009 ok(bt_ctf_event_class_get_attribute_count(NULL
),
1010 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1011 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
1012 "bt_ctf_event_class_get_attribute_count returns the correct count");
1013 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
1014 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1015 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
1016 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1017 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
1018 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1019 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
1020 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1022 memset(&attrs_count
, 0, sizeof(attrs_count
));
1024 for (i
= 0; i
< 4; ++i
) {
1025 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
1027 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
1028 assert(ret_string
&& obj
);
1030 if (!strcmp(ret_string
, "id")) {
1032 ok(bt_value_is_integer(obj
),
1033 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1035 } else if (!strcmp(ret_string
, "name")) {
1037 ok(bt_value_is_string(obj
),
1038 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1040 } else if (!strcmp(ret_string
, "loglevel")) {
1041 attrs_count
.loglevel
++;
1042 ok(bt_value_is_integer(obj
),
1043 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1045 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1046 attrs_count
.modelemfuri
++;
1047 ok(bt_value_is_string(obj
),
1048 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1051 attrs_count
.unknown
++;
1057 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1058 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1059 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1060 "event class has one instance of each known attribute");
1062 /* Add event class to the stream class */
1063 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1064 "Reject addition of NULL event class to a stream class");
1065 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1066 event_class
) == 0, "Add an event class to stream class");
1068 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1069 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1070 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1071 ok(ret_stream_class
== stream_class
,
1072 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1073 bt_put(ret_stream_class
);
1075 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1076 "bt_ctf_event_class_get_field_count handles NULL correctly");
1077 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1078 "bt_ctf_event_class_get_field_count returns a correct value");
1080 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1081 &ret_field_type
, 0) < 0,
1082 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1083 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1084 &ret_field_type
, 0) == 0,
1085 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1086 bt_put(ret_field_type
);
1087 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1089 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1090 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1091 &ret_field_type
, 42) < 0,
1092 "bt_ctf_event_class_get_field handles an invalid index correctly");
1093 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1094 &ret_field_type
, 0) == 0,
1095 "bt_ctf_event_class_get_field returns a field");
1096 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
1097 "bt_ctf_event_class_get_field returns a correct field type");
1098 bt_put(ret_field_type
);
1099 ok(!strcmp(ret_string
, "uint_35"),
1100 "bt_ctf_event_class_get_field returns a correct field name");
1101 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1102 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1103 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1104 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1105 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1106 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1107 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1108 "complex_structure");
1109 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
1110 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1111 bt_put(ret_field_type
);
1113 event
= bt_ctf_event_create(event_class
);
1114 ok(event
, "Instanciate a complex event");
1116 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1117 "bt_ctf_event_get_class handles NULL correctly");
1118 ret_event_class
= bt_ctf_event_get_class(event
);
1119 ok(ret_event_class
== event_class
,
1120 "bt_ctf_event_get_class returns the correct event class");
1121 bt_put(ret_event_class
);
1123 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1124 if (!uint_35_field
) {
1125 printf("uint_35_field is NULL\n");
1128 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1129 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1130 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1131 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1132 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1133 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1134 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1135 &ret_unsigned_int
) == 0,
1136 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1137 ok(ret_unsigned_int
== 0x0DDF00D,
1138 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1139 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1140 &ret_signed_int
) < 0,
1141 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1142 bt_put(uint_35_field
);
1144 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1145 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1146 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1147 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1148 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1149 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1150 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1151 &ret_signed_int
) == 0,
1152 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1153 ok(ret_signed_int
== -12345,
1154 "bt_ctf_field_signed_integer_get_value returns the correct value");
1155 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1156 &ret_unsigned_int
) < 0,
1157 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1158 bt_put(int_16_field
);
1160 complex_structure_field
= bt_ctf_event_get_payload(event
,
1161 "complex_structure");
1163 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1164 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1165 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1166 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1167 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1168 complex_structure_field
, 3);
1169 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1170 bt_put(inner_structure_field
);
1171 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1172 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1173 bt_put(ret_field_type
);
1175 inner_structure_field
= bt_ctf_field_structure_get_field(
1176 complex_structure_field
, "inner_structure");
1177 a_string_field
= bt_ctf_field_structure_get_field(
1178 complex_structure_field
, "a_string");
1179 enum_variant_field
= bt_ctf_field_structure_get_field(
1180 complex_structure_field
, "variant_selector");
1181 variant_field
= bt_ctf_field_structure_get_field(
1182 complex_structure_field
, "variant_value");
1183 uint_35_field
= bt_ctf_field_structure_get_field(
1184 inner_structure_field
, "seq_len");
1185 a_sequence_field
= bt_ctf_field_structure_get_field(
1186 inner_structure_field
, "a_sequence");
1187 an_array_field
= bt_ctf_field_structure_get_field(
1188 inner_structure_field
, "an_array");
1190 enum_container_field
= bt_ctf_field_enumeration_get_container(
1191 enum_variant_field
);
1192 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1193 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1194 enum_variant_field
);
1195 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1196 bt_put(int_16_field
);
1197 ok(!bt_ctf_field_string_get_value(a_string_field
),
1198 "bt_ctf_field_string_get_value returns NULL on an unset field");
1199 bt_ctf_field_string_set_value(a_string_field
,
1201 ok(!bt_ctf_field_string_get_value(NULL
),
1202 "bt_ctf_field_string_get_value correctly handles NULL");
1203 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1204 "bt_ctf_field_string_append correctly handles a NULL string field");
1205 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1206 "bt_ctf_field_string_append correctly handles a NULL string value");
1207 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1208 "bt_ctf_field_string_append succeeds");
1209 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1210 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1211 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1212 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1213 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1214 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1215 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1216 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1217 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1218 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1219 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1220 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1222 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1223 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1224 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1225 "bt_ctf_field_string_get_value returns a correct value");
1226 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1227 SEQUENCE_TEST_LENGTH
);
1229 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1230 enum_container_field
) == NULL
,
1231 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1232 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1234 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1235 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1236 variant_type
, enum_variant_field
);
1237 ok(ret_field_type
== int_16_type
,
1238 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1240 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1241 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1242 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1243 uint_35_field
) == 0, "Set a sequence field's length");
1244 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1245 ok(ret_field
== uint_35_field
,
1246 "bt_ctf_field_sequence_get_length returns the correct length field");
1247 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1248 "bt_ctf_field_sequence_get_length properly handles NULL");
1250 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1251 int_16_field
= bt_ctf_field_sequence_get_field(
1252 a_sequence_field
, i
);
1253 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1254 bt_put(int_16_field
);
1257 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1258 int_16_field
= bt_ctf_field_array_get_field(
1260 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1261 bt_put(int_16_field
);
1264 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1265 assert(stream_event_ctx_field
);
1266 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1267 stream_event_ctx_field
, "common_event_context");
1268 BT_PUT(stream_event_ctx_field
);
1269 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1270 BT_PUT(stream_event_ctx_int_field
);
1272 bt_ctf_clock_set_time(clock
, ++current_time
);
1273 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1274 "Append a complex event to a stream");
1277 * Populate the custom packet context field with a dummy value
1278 * otherwise flush will fail.
1280 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1281 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1282 "custom_packet_context_field");
1283 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1285 ok(bt_ctf_stream_flush(stream
) == 0,
1286 "Flush a stream containing a complex event");
1288 bt_put(uint_35_field
);
1289 bt_put(a_string_field
);
1290 bt_put(inner_structure_field
);
1291 bt_put(complex_structure_field
);
1292 bt_put(a_sequence_field
);
1293 bt_put(an_array_field
);
1294 bt_put(enum_variant_field
);
1295 bt_put(enum_container_field
);
1296 bt_put(variant_field
);
1298 bt_put(packet_context_field
);
1299 bt_put(packet_context
);
1300 bt_put(uint_35_type
);
1301 bt_put(int_16_type
);
1302 bt_put(string_type
);
1303 bt_put(sequence_type
);
1305 bt_put(inner_structure_type
);
1306 bt_put(complex_structure_type
);
1307 bt_put(uint_3_type
);
1308 bt_put(enum_variant_type
);
1309 bt_put(variant_type
);
1310 bt_put(ret_field_type
);
1311 bt_put(event_class
);
1315 static void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1316 struct bt_ctf_field_type
*expected_type
, const char *name
)
1318 struct bt_ctf_field_type
*copy_type
;
1320 copy_type
= bt_ctf_field_get_type(field
);
1321 ok(copy_type
== expected_type
,
1322 "bt_ctf_field_copy does not copy the type (%s)", name
);
1326 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1327 struct bt_ctf_field
*field_b
, const char *name
)
1329 ok(field_a
!= field_b
,
1330 "bt_ctf_field_copy creates different pointers (%s)", name
);
1333 void field_copy_tests()
1335 struct bt_ctf_field_type
*len_type
= NULL
;
1336 struct bt_ctf_field_type
*fp_type
= NULL
;
1337 struct bt_ctf_field_type
*s_type
= NULL
;
1338 struct bt_ctf_field_type
*e_int_type
= NULL
;
1339 struct bt_ctf_field_type
*e_type
= NULL
;
1340 struct bt_ctf_field_type
*v_type
= NULL
;
1341 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1342 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1343 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1344 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1345 struct bt_ctf_field_type
*strct_type
= NULL
;
1346 struct bt_ctf_field
*len
= NULL
;
1347 struct bt_ctf_field
*fp
= NULL
;
1348 struct bt_ctf_field
*s
= NULL
;
1349 struct bt_ctf_field
*e_int
= NULL
;
1350 struct bt_ctf_field
*e
= NULL
;
1351 struct bt_ctf_field
*v
= NULL
;
1352 struct bt_ctf_field
*v_selected
= NULL
;
1353 struct bt_ctf_field
*v_selected_cur
= NULL
;
1354 struct bt_ctf_field
*v_selected_0
= NULL
;
1355 struct bt_ctf_field
*v_selected_1
= NULL
;
1356 struct bt_ctf_field
*v_selected_2
= NULL
;
1357 struct bt_ctf_field
*v_selected_3
= NULL
;
1358 struct bt_ctf_field
*v_selected_4
= NULL
;
1359 struct bt_ctf_field
*v_selected_5
= NULL
;
1360 struct bt_ctf_field
*v_selected_6
= NULL
;
1361 struct bt_ctf_field
*a
= NULL
;
1362 struct bt_ctf_field
*a_0
= NULL
;
1363 struct bt_ctf_field
*a_1
= NULL
;
1364 struct bt_ctf_field
*a_2
= NULL
;
1365 struct bt_ctf_field
*a_3
= NULL
;
1366 struct bt_ctf_field
*a_4
= NULL
;
1367 struct bt_ctf_field
*strct
= NULL
;
1368 struct bt_ctf_field
*len_copy
= NULL
;
1369 struct bt_ctf_field
*fp_copy
= NULL
;
1370 struct bt_ctf_field
*s_copy
= NULL
;
1371 struct bt_ctf_field
*e_int_copy
= NULL
;
1372 struct bt_ctf_field
*e_copy
= NULL
;
1373 struct bt_ctf_field
*v_copy
= NULL
;
1374 struct bt_ctf_field
*v_selected_copy
= NULL
;
1375 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1376 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1377 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1378 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1379 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1380 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1381 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1382 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1383 struct bt_ctf_field
*a_copy
= NULL
;
1384 struct bt_ctf_field
*a_0_copy
= NULL
;
1385 struct bt_ctf_field
*a_1_copy
= NULL
;
1386 struct bt_ctf_field
*a_2_copy
= NULL
;
1387 struct bt_ctf_field
*a_3_copy
= NULL
;
1388 struct bt_ctf_field
*a_4_copy
= NULL
;
1389 struct bt_ctf_field
*strct_copy
= NULL
;
1390 uint64_t uint64_t_val
;
1391 const char *str_val
;
1395 /* create len type */
1396 len_type
= bt_ctf_field_type_integer_create(32);
1399 /* create fp type */
1400 fp_type
= bt_ctf_field_type_floating_point_create();
1404 s_type
= bt_ctf_field_type_string_create();
1407 /* create e_int type */
1408 e_int_type
= bt_ctf_field_type_integer_create(8);
1412 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1414 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1417 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1421 /* create v_label1 type */
1422 v_label1_type
= bt_ctf_field_type_string_create();
1423 assert(v_label1_type
);
1425 /* create v_label1_array type */
1426 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1427 assert(v_label1_array_type
);
1429 /* create v_label2 type */
1430 v_label2_type
= bt_ctf_field_type_integer_create(16);
1431 assert(v_label2_type
);
1433 /* create v_label2_seq type */
1434 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1436 assert(v_label2_seq_type
);
1439 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1441 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1444 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1448 /* create strct type */
1449 strct_type
= bt_ctf_field_type_structure_create();
1451 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1454 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1456 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1458 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1460 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1462 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1463 v_label1_array_type
, "a");
1467 strct
= bt_ctf_field_create(strct_type
);
1471 len
= bt_ctf_field_structure_get_field(strct
, "len");
1475 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1479 s
= bt_ctf_field_structure_get_field(strct
, "s");
1483 e
= bt_ctf_field_structure_get_field(strct
, "e");
1486 /* get e_int (underlying integer) */
1487 e_int
= bt_ctf_field_enumeration_get_container(e
);
1491 v
= bt_ctf_field_structure_get_field(strct
, "v");
1495 a
= bt_ctf_field_structure_get_field(strct
, "a");
1499 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1503 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1507 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1510 /* set e field (LABEL2) */
1511 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1515 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1517 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1518 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1519 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1520 ok(v_selected_cur
== v_selected
,
1521 "bt_ctf_field_variant_get_current_field returns the current field");
1522 bt_put(v_selected_cur
);
1524 /* set selected v field */
1525 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1527 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1528 assert(v_selected_0
);
1529 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1531 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1532 assert(v_selected_1
);
1533 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1535 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1536 assert(v_selected_2
);
1537 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1539 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1540 assert(v_selected_3
);
1541 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1543 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1544 assert(v_selected_4
);
1545 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1547 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1548 assert(v_selected_5
);
1549 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1551 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1552 assert(v_selected_6
);
1553 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1557 a_0
= bt_ctf_field_array_get_field(a
, 0);
1559 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1561 a_1
= bt_ctf_field_array_get_field(a
, 1);
1563 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1565 a_2
= bt_ctf_field_array_get_field(a
, 2);
1567 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1569 a_3
= bt_ctf_field_array_get_field(a
, 3);
1571 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1573 a_4
= bt_ctf_field_array_get_field(a
, 4);
1575 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1578 /* create copy of strct */
1579 ok(!bt_ctf_field_copy(NULL
),
1580 "bt_ctf_field_copy handles NULL correctly");
1581 strct_copy
= bt_ctf_field_copy(strct
);
1583 "bt_ctf_field_copy returns a valid pointer");
1585 /* get all copied fields */
1586 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1588 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1590 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1592 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1594 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1596 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1598 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1599 assert(v_selected_copy
);
1600 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1601 assert(v_selected_0_copy
);
1602 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1603 assert(v_selected_1_copy
);
1604 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1605 assert(v_selected_2_copy
);
1606 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1607 assert(v_selected_3_copy
);
1608 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1609 assert(v_selected_4_copy
);
1610 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1611 assert(v_selected_5_copy
);
1612 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1613 assert(v_selected_6_copy
);
1614 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1615 "sequence field copy is not too large");
1616 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1618 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1620 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1622 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1624 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1626 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1628 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1629 "array field copy is not too large");
1631 /* make sure copied fields are different pointers */
1632 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1633 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1634 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1635 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1636 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1637 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1638 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1639 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1641 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1643 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1645 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1647 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1649 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1651 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1653 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1655 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1656 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1657 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1658 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1659 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1660 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1662 /* make sure copied fields share the same types */
1663 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1664 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1665 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1666 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1667 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1668 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1669 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1671 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1673 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1675 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1677 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1679 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1681 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1683 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1685 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1686 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1687 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1688 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1689 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1690 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1692 /* validate len copy */
1693 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1695 ok(uint64_t_val
== 7,
1696 "bt_ctf_field_copy creates a valid integer field copy");
1698 /* validate fp copy */
1699 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1701 ok(double_val
== 3.14,
1702 "bt_ctf_field_copy creates a valid floating point number field copy");
1704 /* validate s copy */
1705 str_val
= bt_ctf_field_string_get_value(s_copy
);
1706 ok(str_val
&& !strcmp(str_val
, "btbt"),
1707 "bt_ctf_field_copy creates a valid string field copy");
1709 /* validate e_int copy */
1710 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1713 ok(uint64_t_val
== 23,
1714 "bt_ctf_field_copy creates a valid enum's integer field copy");
1716 /* validate e copy */
1717 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1718 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1719 "bt_ctf_field_copy creates a valid enum field copy");
1721 /* validate v_selected copy */
1722 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1723 assert(v_selected_copy_len
);
1724 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1727 ok(uint64_t_val
== 7,
1728 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1729 bt_put(v_selected_copy_len
);
1730 v_selected_copy_len
= NULL
;
1732 /* validate v_selected copy fields */
1733 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1736 ok(uint64_t_val
== 7,
1737 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1738 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1741 ok(uint64_t_val
== 6,
1742 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1743 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1746 ok(uint64_t_val
== 5,
1747 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1748 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1751 ok(uint64_t_val
== 4,
1752 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1753 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1756 ok(uint64_t_val
== 3,
1757 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1758 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1761 ok(uint64_t_val
== 2,
1762 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1763 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1766 ok(uint64_t_val
== 1,
1767 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1769 /* validate a copy fields */
1770 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1771 ok(str_val
&& !strcmp(str_val
, "a_0"),
1772 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1773 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1774 ok(str_val
&& !strcmp(str_val
, "a_1"),
1775 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1776 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1777 ok(str_val
&& !strcmp(str_val
, "a_2"),
1778 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1779 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1780 ok(str_val
&& !strcmp(str_val
, "a_3"),
1781 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1782 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1783 ok(str_val
&& !strcmp(str_val
, "a_4"),
1784 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1786 /* put everything */
1793 bt_put(v_label1_type
);
1794 bt_put(v_label1_array_type
);
1795 bt_put(v_label2_type
);
1796 bt_put(v_label2_seq_type
);
1805 bt_put(v_selected_0
);
1806 bt_put(v_selected_1
);
1807 bt_put(v_selected_2
);
1808 bt_put(v_selected_3
);
1809 bt_put(v_selected_4
);
1810 bt_put(v_selected_5
);
1811 bt_put(v_selected_6
);
1825 bt_put(v_selected_copy
);
1826 bt_put(v_selected_0_copy
);
1827 bt_put(v_selected_1_copy
);
1828 bt_put(v_selected_2_copy
);
1829 bt_put(v_selected_3_copy
);
1830 bt_put(v_selected_4_copy
);
1831 bt_put(v_selected_5_copy
);
1832 bt_put(v_selected_6_copy
);
1842 void type_field_tests()
1844 struct bt_ctf_field
*uint_12
;
1845 struct bt_ctf_field
*int_16
;
1846 struct bt_ctf_field
*string
;
1847 struct bt_ctf_field
*enumeration
;
1848 struct bt_ctf_field_type
*composite_structure_type
;
1849 struct bt_ctf_field_type
*structure_seq_type
;
1850 struct bt_ctf_field_type
*string_type
;
1851 struct bt_ctf_field_type
*sequence_type
;
1852 struct bt_ctf_field_type
*uint_8_type
;
1853 struct bt_ctf_field_type
*int_16_type
;
1854 struct bt_ctf_field_type
*uint_12_type
=
1855 bt_ctf_field_type_integer_create(12);
1856 struct bt_ctf_field_type
*enumeration_type
;
1857 struct bt_ctf_field_type
*returned_type
;
1858 const char *ret_string
;
1860 returned_type
= bt_ctf_field_get_type(NULL
);
1861 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1863 ok(uint_12_type
, "Create an unsigned integer type");
1864 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1865 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1866 "Set integer type's base as binary");
1867 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1868 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1869 "Set integer type's base as decimal");
1870 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1871 BT_CTF_INTEGER_BASE_UNKNOWN
),
1872 "Reject integer type's base set as unknown");
1873 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1874 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1875 "Set integer type's base as octal");
1876 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1877 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1878 "Set integer type's base as hexadecimal");
1879 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1880 "Reject unknown integer base value");
1881 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1882 "Set integer type signedness to signed");
1883 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1884 "Set integer type signedness to unsigned");
1885 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1886 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1887 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1888 "bt_ctf_field_type_integer_get_size returns a correct value");
1889 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1890 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1891 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1892 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1894 ok(bt_ctf_field_type_set_byte_order(NULL
,
1895 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1896 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1897 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1898 (enum bt_ctf_byte_order
) 42) < 0,
1899 "bt_ctf_field_type_set_byte_order rejects invalid values");
1900 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1901 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1902 "Set an integer's byte order to little endian");
1903 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1904 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1905 "Set an integer's byte order to big endian");
1906 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1907 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1908 "bt_ctf_field_type_get_byte_order returns a correct value");
1909 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1910 BT_CTF_BYTE_ORDER_UNKNOWN
,
1911 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1913 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1914 BT_CTF_TYPE_ID_UNKNOWN
,
1915 "bt_ctf_field_type_get_type_id handles NULL correctly");
1916 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1917 BT_CTF_TYPE_ID_INTEGER
,
1918 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1920 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1921 BT_CTF_INTEGER_BASE_UNKNOWN
,
1922 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1923 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1924 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1925 "bt_ctf_field_type_integer_get_base returns a correct value");
1927 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1928 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1929 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1930 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1931 (enum bt_ctf_string_encoding
) 123) < 0,
1932 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1933 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1934 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1935 "Set integer type encoding to UTF8");
1936 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1937 BT_CTF_STRING_ENCODING_UNKNOWN
,
1938 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1939 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1940 BT_CTF_STRING_ENCODING_UTF8
,
1941 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1943 int_16_type
= bt_ctf_field_type_integer_create(16);
1944 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1945 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1946 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1947 uint_8_type
= bt_ctf_field_type_integer_create(8);
1949 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1950 ok(sequence_type
, "Create a sequence of int16_t type");
1951 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1952 BT_CTF_TYPE_ID_SEQUENCE
,
1953 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1955 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1956 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1957 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1959 ok(!strcmp(ret_string
, "seq_len"),
1960 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1961 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1962 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1963 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1965 ok(returned_type
== int_16_type
,
1966 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1967 bt_put(returned_type
);
1969 string_type
= bt_ctf_field_type_string_create();
1970 ok(string_type
, "Create a string type");
1971 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1972 BT_CTF_STRING_ENCODING_NONE
),
1973 "Reject invalid \"None\" string encoding");
1974 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1976 "Reject invalid string encoding");
1977 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1978 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1979 "Set string encoding to ASCII");
1981 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1982 BT_CTF_STRING_ENCODING_UNKNOWN
,
1983 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1984 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1985 BT_CTF_STRING_ENCODING_ASCII
,
1986 "bt_ctf_field_type_string_get_encoding returns the correct value");
1988 structure_seq_type
= bt_ctf_field_type_structure_create();
1989 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1990 BT_CTF_TYPE_ID_STRUCT
,
1991 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1992 ok(structure_seq_type
, "Create a structure type");
1993 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1994 uint_8_type
, "seq_len") == 0,
1995 "Add a uint8_t type to a structure");
1996 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1997 sequence_type
, "a_sequence") == 0,
1998 "Add a sequence type to a structure");
2000 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
2001 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
2002 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
2003 "bt_ctf_field_type_structure_get_field_count returns a correct value");
2005 ok(bt_ctf_field_type_structure_get_field(NULL
,
2006 &ret_string
, &returned_type
, 1) < 0,
2007 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2008 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2009 NULL
, &returned_type
, 1) == 0,
2010 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2011 bt_put(returned_type
);
2012 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2013 &ret_string
, NULL
, 1) == 0,
2014 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2015 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2016 &ret_string
, &returned_type
, 10) < 0,
2017 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2018 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2019 &ret_string
, &returned_type
, 1) == 0,
2020 "bt_ctf_field_type_structure_get_field returns a field");
2021 ok(!strcmp(ret_string
, "a_sequence"),
2022 "bt_ctf_field_type_structure_get_field returns a correct field name");
2023 ok(returned_type
== sequence_type
,
2024 "bt_ctf_field_type_structure_get_field returns a correct field type");
2025 bt_put(returned_type
);
2027 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2028 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2029 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2030 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2031 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2032 structure_seq_type
, "a_sequence");
2033 ok(returned_type
== sequence_type
,
2034 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2035 bt_put(returned_type
);
2037 composite_structure_type
= bt_ctf_field_type_structure_create();
2038 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2039 string_type
, "a_string") == 0,
2040 "Add a string type to a structure");
2041 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2042 structure_seq_type
, "inner_structure") == 0,
2043 "Add a structure type to a structure");
2045 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2046 NULL
, "a_sequence") == NULL
,
2047 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2048 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2049 structure_seq_type
, NULL
) == NULL
,
2050 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2051 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2052 structure_seq_type
, "a_sequence");
2053 ok(returned_type
== sequence_type
,
2054 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2055 bt_put(returned_type
);
2057 int_16
= bt_ctf_field_create(int_16_type
);
2058 ok(int_16
, "Instanciate a signed 16-bit integer");
2059 uint_12
= bt_ctf_field_create(uint_12_type
);
2060 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2061 returned_type
= bt_ctf_field_get_type(int_16
);
2062 ok(returned_type
== int_16_type
,
2063 "bt_ctf_field_get_type returns the correct type");
2065 /* Can't modify types after instanciating them */
2066 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2067 BT_CTF_INTEGER_BASE_DECIMAL
),
2068 "Check an integer type' base can't be modified after instanciation");
2069 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2070 "Check an integer type's signedness can't be modified after instanciation");
2072 /* Check signed property is checked */
2073 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2074 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2075 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2076 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2078 /* Check overflows are properly tested for */
2079 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2080 "Check -32768 is allowed for a signed 16-bit integer");
2081 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2082 "Check 32767 is allowed for a signed 16-bit integer");
2083 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2084 "Check 32768 is not allowed for a signed 16-bit integer");
2085 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2086 "Check -32769 is not allowed for a signed 16-bit integer");
2087 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2088 "Check -42 is allowed for a signed 16-bit integer");
2090 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2091 "Check 4095 is allowed for an unsigned 12-bit integer");
2092 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2093 "Check 4096 is not allowed for a unsigned 12-bit integer");
2094 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2095 "Check 0 is allowed for an unsigned 12-bit integer");
2097 string
= bt_ctf_field_create(string_type
);
2098 ok(string
, "Instanciate a string field");
2099 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2100 "Set a string's value");
2102 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2103 ok(enumeration_type
,
2104 "Create an enumeration type with an unsigned 12-bit integer as container");
2105 enumeration
= bt_ctf_field_create(enumeration_type
);
2107 "Check enumeration types are validated before instantiation");
2112 bt_put(enumeration
);
2113 bt_put(composite_structure_type
);
2114 bt_put(structure_seq_type
);
2115 bt_put(string_type
);
2116 bt_put(sequence_type
);
2117 bt_put(uint_8_type
);
2118 bt_put(int_16_type
);
2119 bt_put(uint_12_type
);
2120 bt_put(enumeration_type
);
2121 bt_put(returned_type
);
2124 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2125 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2128 * Append enough events to force the underlying packet to be resized.
2129 * Also tests that a new event can be declared after a stream has been
2130 * instantiated and used/flushed.
2134 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2136 struct bt_ctf_field_type
*integer_type
=
2137 bt_ctf_field_type_integer_create(17);
2138 struct bt_ctf_field_type
*string_type
=
2139 bt_ctf_field_type_string_create();
2140 struct bt_ctf_event
*event
= NULL
;
2141 struct bt_ctf_field
*ret_field
= NULL
;
2142 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2143 uint64_t ret_uint64
;
2144 int events_appended
= 0;
2145 struct bt_ctf_field
*packet_context
= NULL
,
2146 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
2147 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2148 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2149 struct bt_ctf_field_type
*ep_type
= NULL
;
2151 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2153 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2155 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2156 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2162 * bt_ctf_stream_class_add_event_class() copies the field types
2163 * of event_class, so we retrieve the new ones to create the
2164 * appropriate fields.
2166 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2168 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2169 ep_type
, "field_1");
2170 assert(ep_field_1_type
);
2171 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2172 ep_type
, "a_string");
2173 assert(ep_a_string_type
);
2175 event
= bt_ctf_event_create(event_class
);
2176 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2177 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2178 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2179 "bt_ctf_event_get_payload_by_index returns a correct field");
2180 bt_put(ret_field_type
);
2183 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2184 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2185 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2186 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2189 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2190 event
= bt_ctf_event_create(event_class
);
2191 struct bt_ctf_field
*integer
=
2192 bt_ctf_field_create(ep_field_1_type
);
2193 struct bt_ctf_field
*string
=
2194 bt_ctf_field_create(ep_a_string_type
);
2196 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2197 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2198 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2201 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2202 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2206 /* Populate stream event context */
2207 stream_event_context
=
2208 bt_ctf_event_get_stream_event_context(event
);
2209 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2210 "common_event_context");
2211 BT_PUT(stream_event_context
);
2212 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2216 ret
|= bt_ctf_stream_append_event(stream
, event
);
2224 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2225 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2226 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2227 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2228 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2229 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2230 ok(ret
== 0 && ret_uint64
== 0,
2231 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2232 bt_ctf_stream_append_discarded_events(stream
, 1000);
2233 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2234 ok(ret
== 0 && ret_uint64
== 1000,
2235 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2238 ok(events_appended
, "Append 100 000 events to a stream");
2241 * Populate the custom packet context field with a dummy value
2242 * otherwise flush will fail.
2244 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2245 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2246 "custom_packet_context_field");
2247 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2249 ok(bt_ctf_stream_flush(stream
) == 0,
2250 "Flush a stream that forces a packet resize");
2251 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2252 ok(ret
== 0 && ret_uint64
== 1000,
2253 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2254 bt_put(integer_type
);
2255 bt_put(string_type
);
2256 bt_put(packet_context
);
2257 bt_put(packet_context_field
);
2258 bt_put(stream_event_context
);
2259 bt_put(event_class
);
2260 bt_put(ep_field_1_type
);
2261 bt_put(ep_a_string_type
);
2265 void test_empty_stream(struct bt_ctf_writer
*writer
)
2268 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2269 struct bt_ctf_stream_class
*stream_class
= NULL
;
2270 struct bt_ctf_stream
*stream
= NULL
;
2272 trace
= bt_ctf_writer_get_trace(writer
);
2274 diag("Failed to get trace from writer");
2279 stream_class
= bt_ctf_stream_class_create("empty_stream");
2280 if (!stream_class
) {
2281 diag("Failed to create stream class");
2286 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2287 "bt_ctf_stream_class_get_trace handles NULL correctly");
2288 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2289 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2291 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2293 diag("Failed to create writer stream");
2298 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2299 ok(ret_trace
== trace
,
2300 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2303 "Created a stream class with default attributes and an empty stream");
2307 bt_put(stream_class
);
2310 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2313 struct bt_ctf_trace
*trace
= NULL
;
2314 struct bt_ctf_clock
*clock
= NULL
;
2315 struct bt_ctf_stream_class
*stream_class
= NULL
;
2316 struct bt_ctf_stream
*stream
= NULL
;
2317 struct bt_ctf_field_type
*integer_type
= NULL
,
2318 *sequence_type
= NULL
, *event_header_type
= NULL
;
2319 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2320 *event_header
= NULL
, *packet_header
= NULL
;
2321 struct bt_ctf_event_class
*event_class
= NULL
;
2322 struct bt_ctf_event
*event
= NULL
;
2324 trace
= bt_ctf_writer_get_trace(writer
);
2326 fail("Failed to get trace from writer");
2330 clock
= bt_ctf_trace_get_clock(trace
, 0);
2332 fail("Failed to get clock from trace");
2336 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2337 if (!stream_class
) {
2338 fail("Failed to create stream class");
2342 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2344 fail("Failed to set stream class clock");
2349 * Customize event header to add an "seq_len" integer member
2350 * which will be used as the length of a sequence in an event of this
2353 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2355 if (!event_header_type
) {
2356 fail("Failed to get event header type");
2360 integer_type
= bt_ctf_field_type_integer_create(13);
2361 if (!integer_type
) {
2362 fail("Failed to create length integer type");
2366 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2367 integer_type
, "seq_len");
2369 fail("Failed to add a new field to stream event header");
2373 event_class
= bt_ctf_event_class_create("sequence_event");
2375 fail("Failed to create event class");
2380 * This event's payload will contain a sequence which references
2381 * stream.event.header.seq_len as its length field.
2383 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2384 "stream.event.header.seq_len");
2385 if (!sequence_type
) {
2386 fail("Failed to create a sequence");
2390 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2393 fail("Failed to add a sequence to an event class");
2397 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2399 fail("Failed to add event class to stream class");
2403 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2405 fail("Failed to create stream")
2410 * We have defined a custom packet header field. We have to populate it
2413 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2414 if (!packet_header
) {
2415 fail("Failed to get stream packet header");
2419 integer
= bt_ctf_field_structure_get_field(packet_header
,
2420 "custom_trace_packet_header_field");
2422 fail("Failed to retrieve custom_trace_packet_header_field");
2426 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2428 fail("Failed to set custom_trace_packet_header_field value");
2433 event
= bt_ctf_event_create(event_class
);
2435 fail("Failed to create event");
2439 event_header
= bt_ctf_event_get_header(event
);
2440 if (!event_header
) {
2441 fail("Failed to get event header");
2445 integer
= bt_ctf_field_structure_get_field(event_header
,
2448 fail("Failed to get seq_len field from event header");
2452 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2454 fail("Failed to set seq_len value in event header");
2458 /* Populate both sequence integer fields */
2459 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2461 fail("Failed to retrieve sequence from event");
2465 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2467 fail("Failed to set sequence length");
2472 for (i
= 0; i
< 2; i
++) {
2473 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2475 fail("Failed to retrieve sequence element");
2479 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2481 fail("Failed to set sequence element value");
2489 ret
= bt_ctf_stream_append_event(stream
, event
);
2491 fail("Failed to append event to stream");
2495 ret
= bt_ctf_stream_flush(stream
);
2497 fail("Failed to flush custom_event_header stream");
2503 bt_put(stream_class
);
2504 bt_put(event_class
);
2508 bt_put(event_header
);
2509 bt_put(packet_header
);
2510 bt_put(sequence_type
);
2511 bt_put(integer_type
);
2512 bt_put(event_header_type
);
2515 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2518 struct bt_ctf_trace
*trace
= NULL
;
2519 struct bt_ctf_clock
*clock
= NULL
;
2520 struct bt_ctf_stream_class
*stream_class
= NULL
;
2521 struct bt_ctf_stream
*stream
= NULL
,
2523 struct bt_ctf_event_class
*event_class
= NULL
;
2524 struct bt_ctf_event
*event
= NULL
;
2525 struct bt_ctf_field_type
*integer_type
= NULL
;
2526 struct bt_ctf_field
*integer
= NULL
;
2528 trace
= bt_ctf_writer_get_trace(writer
);
2530 diag("Failed to get trace from writer");
2535 clock
= bt_ctf_trace_get_clock(trace
, 0);
2537 diag("Failed to get clock from trace");
2542 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2543 if (!stream_class
) {
2544 diag("Failed to create stream class");
2549 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2551 diag("Failed to set stream class clock");
2555 event_class
= bt_ctf_event_class_create("some_event_class_name");
2556 integer_type
= bt_ctf_field_type_integer_create(32);
2557 if (!integer_type
) {
2558 diag("Failed to create integer field type");
2563 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2566 diag("Failed to add field to event class");
2570 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2573 diag("Failed to add event class to stream class");
2576 event
= bt_ctf_event_create(event_class
);
2578 diag("Failed to create event");
2583 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2585 diag("Failed to get integer field payload from event");
2590 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2592 diag("Failed to set integer field value");
2596 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2598 diag("Failed to create writer stream");
2603 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2604 "bt_ctf_event_get_stream handles NULL correctly");
2605 ok(bt_ctf_event_get_stream(event
) == NULL
,
2606 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2608 ret
= bt_ctf_stream_append_event(stream
, event
);
2610 diag("Failed to append event to stream");
2614 ret_stream
= bt_ctf_event_get_stream(event
);
2615 ok(ret_stream
== stream
,
2616 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2619 "Create an event before instanciating its associated stream");
2623 bt_put(stream_class
);
2624 bt_put(event_class
);
2626 bt_put(integer_type
);
2631 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2633 struct bt_ctf_event_class
*event_class
;
2635 event_class
= bt_ctf_event_class_create("Simple Event");
2636 assert(event_class
);
2637 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2638 "two event classes with the same name cannot cohabit within the same stream class");
2639 bt_put(event_class
);
2641 event_class
= bt_ctf_event_class_create("different name, ok");
2642 assert(event_class
);
2643 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2644 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2645 "two event classes with the same ID cannot cohabit within the same stream class");
2646 bt_put(event_class
);
2649 void test_trace_stream_class_clock(void)
2651 struct bt_ctf_trace
*trace
= NULL
;
2652 struct bt_ctf_stream_class
*sc1
= NULL
;
2653 struct bt_ctf_stream_class
*sc2
= NULL
;
2654 struct bt_ctf_clock
*sc1_clock
= NULL
;
2655 struct bt_ctf_clock
*sc2_clock
= NULL
;
2656 const char *clock_name
= "hello";
2658 trace
= bt_ctf_trace_create();
2660 sc1
= bt_ctf_stream_class_create(NULL
);
2662 sc2
= bt_ctf_stream_class_create(NULL
);
2664 sc1_clock
= bt_ctf_clock_create(clock_name
);
2666 sc2_clock
= bt_ctf_clock_create(clock_name
);
2669 ok(!bt_ctf_stream_class_set_clock(sc1
, sc1_clock
),
2670 "bt_ctf_stream_class_set_clock() succeeds for sc1");
2671 ok(!bt_ctf_stream_class_set_clock(sc2
, sc2_clock
),
2672 "bt_ctf_stream_class_set_clock() succeeds for sc2");
2673 ok(!bt_ctf_trace_add_stream_class(trace
, sc1
),
2674 "bt_ctf_trace_add_stream_class() succeeds with sc1");
2675 ok(bt_ctf_trace_add_stream_class(trace
, sc2
),
2676 "bt_ctf_trace_add_stream_class() fails with sc2 (different clock, same name)");
2686 struct bt_ctf_event_class
*create_minimal_event_class(void)
2688 struct bt_ctf_event_class
*ec
= NULL
;
2689 struct bt_ctf_field_type
*int_ft
= NULL
;
2692 int_ft
= bt_ctf_field_type_integer_create(23);
2694 ec
= bt_ctf_event_class_create("minimal");
2696 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2704 void test_create_writer_vs_non_writer_mode(void)
2707 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2708 const char *writer_stream_name
= "writer stream instance";
2709 struct bt_ctf_writer
*writer
= NULL
;
2710 struct bt_ctf_trace
*writer_trace
= NULL
;
2711 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2712 struct bt_ctf_stream
*writer_stream
= NULL
;
2713 struct bt_ctf_stream
*writer_stream2
= NULL
;
2714 struct bt_ctf_stream
*packet_stream
= NULL
;
2715 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2716 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2717 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2718 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2719 struct bt_ctf_event_class
*writer_ec
= NULL
;
2720 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2721 struct bt_ctf_event
*event
= NULL
;
2722 struct bt_ctf_event
*event2
= NULL
;
2723 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2724 struct bt_ctf_field
*int_field
= NULL
;
2725 struct bt_ctf_clock
*writer_clock
= NULL
;
2726 struct bt_ctf_clock
*non_writer_clock
= NULL
;
2727 struct bt_ctf_packet
*packet
= NULL
;
2728 struct bt_ctf_packet
*packet2
= NULL
;
2730 if (!bt_mkdtemp(trace_path
)) {
2734 /* Create empty structure field type (event header) */
2735 empty_struct_ft
= bt_ctf_field_type_structure_create();
2736 assert(empty_struct_ft
);
2738 /* Create writer, writer stream class, stream, and clock */
2739 writer
= bt_ctf_writer_create(trace_path
);
2741 writer_trace
= bt_ctf_writer_get_trace(writer
);
2742 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2743 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2745 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2748 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2750 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2751 assert(writer_stream
);
2752 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2753 "bt_ctf_stream_get_name() returns the stream's name");
2754 writer_clock
= bt_ctf_clock_create("writer_clock");
2755 assert(writer_clock
);
2756 ret
= bt_ctf_trace_add_clock(writer_trace
, writer_clock
);
2759 /* Create non-writer trace, stream class, stream, and clock */
2760 non_writer_trace
= bt_ctf_trace_create();
2761 assert(non_writer_trace
);
2762 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2763 assert(non_writer_sc
);
2764 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2767 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2769 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2770 assert(non_writer_stream
);
2771 non_writer_clock
= bt_ctf_clock_create("non_writer_clock");
2772 assert(non_writer_clock
);
2773 ret
= bt_ctf_trace_add_clock(non_writer_trace
, non_writer_clock
);
2776 /* Create event class and event */
2777 writer_ec
= create_minimal_event_class();
2779 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2781 event
= bt_ctf_event_create(writer_ec
);
2783 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2785 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2788 * Verify non-writer stream: it should be impossible to append
2791 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2792 "bt_ctf_stream_append_event() fails with a non-writer stream");
2795 * Verify writer stream: it should be possible to append an
2798 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2799 "bt_ctf_stream_append_event() succeeds with a writer stream");
2802 * Verify that it's possible to get and set the value of a
2803 * writer mode clock.
2805 ok (!bt_ctf_clock_set_value(writer_clock
, 1000),
2806 "bt_ctf_clock_set_value() succeeds with a writer mode clock");
2807 ok (bt_ctf_clock_get_value(writer_clock
) == 1000,
2808 "bt_ctf_clock_get_value() succeeds with a writer mode clock");
2811 * Verify that it's impossible to get and set the value of a
2812 * non-writer mode clock.
2814 ok (bt_ctf_clock_set_value(non_writer_clock
, 1000),
2815 "bt_ctf_clock_set_value() fails with a non-writer mode clock");
2816 ok (bt_ctf_clock_get_value(non_writer_clock
) == -1ULL,
2817 "bt_ctf_clock_get_value() fails with a non-writer mode clock");
2820 * It should be possible to create a packet from a non-writer
2821 * stream, but not from a writer stream.
2823 packet
= bt_ctf_packet_create(writer_stream
);
2824 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2825 packet
= bt_ctf_packet_create(non_writer_stream
);
2826 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2827 packet_stream
= bt_ctf_packet_get_stream(packet
);
2828 ok(packet_stream
== non_writer_stream
,
2829 "bt_ctf_packet_get_stream() returns the correct stream");
2832 * It should not be possible to append an event associated to
2833 * a stream to a different stream.
2835 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2836 assert(writer_stream2
);
2837 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2838 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2841 * It should not be possible to set the packet of an event
2842 * associated to a given stream to a packet associated with
2843 * a different stream.
2845 ok(bt_ctf_event_set_packet(event
, packet
),
2846 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2849 * It should be possible to set the packet of a fresh event, as
2850 * long as the originating stream classes are the same.
2852 event2
= bt_ctf_event_create(writer_ec
);
2854 ok(bt_ctf_event_set_packet(event2
, packet
),
2855 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2856 non_writer_ec
= create_minimal_event_class();
2857 assert(non_writer_ec
);
2858 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2861 event2
= bt_ctf_event_create(non_writer_ec
);
2863 ok(!bt_ctf_event_set_packet(event2
, packet
),
2864 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2867 * It should be possible to set a packet created from the same
2868 * stream to an event with an existing packet.
2870 packet2
= bt_ctf_packet_create(non_writer_stream
);
2872 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2873 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2877 * It should not be possible to set a packet created from a
2878 * different stream to an event with an existing packet.
2880 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2881 assert(non_writer_stream2
);
2882 packet2
= bt_ctf_packet_create(non_writer_stream
);
2884 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2885 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2888 bt_put(writer_trace
);
2890 bt_put(writer_stream
);
2891 bt_put(writer_stream2
);
2892 bt_put(non_writer_trace
);
2893 bt_put(non_writer_sc
);
2894 bt_put(non_writer_stream
);
2895 bt_put(non_writer_stream2
);
2896 bt_put(packet_stream
);
2898 bt_put(non_writer_ec
);
2902 bt_put(empty_struct_ft
);
2903 bt_put(writer_clock
);
2904 bt_put(non_writer_clock
);
2907 delete_trace(trace_path
);
2910 void test_clock_utils(void)
2913 struct bt_ctf_clock
*clock
= NULL
;
2915 clock
= bt_ctf_clock_create("water");
2917 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2919 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2921 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2923 ok(bt_ctf_clock_ns_from_value(clock
, 4321) == 1234000005321ULL,
2924 "bt_ctf_clock_ns_from_value() returns the correct value with a 1 GHz frequency");
2925 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2927 ok(bt_ctf_clock_ns_from_value(clock
, 4321) ==
2928 (uint64_t) 1237468709256.845,
2929 "bt_ctf_clock_ns_from_value() returns the correct value with a non-1 GHz frequency");
2934 int main(int argc
, char **argv
)
2936 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2937 char metadata_path
[sizeof(trace_path
) + 9];
2938 const char *clock_name
= "test_clock";
2939 const char *clock_description
= "This is a test clock";
2940 const char *returned_clock_name
;
2941 const char *returned_clock_description
;
2942 const uint64_t frequency
= 1123456789;
2943 const int64_t offset_s
= 1351530929945824323;
2944 const int64_t offset
= 1234567;
2945 int64_t get_offset_s
,
2948 const uint64_t precision
= 10;
2949 const int is_absolute
= 0xFF;
2950 char *metadata_string
;
2951 struct bt_ctf_writer
*writer
;
2952 struct utsname name
;
2953 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2954 struct bt_ctf_clock
*clock
, *ret_clock
;
2955 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2956 struct bt_ctf_stream
*stream1
;
2957 const char *ret_string
;
2958 const unsigned char *ret_uuid
;
2959 unsigned char tmp_uuid
[16] = { 0 };
2960 struct bt_ctf_field_type
*packet_context_type
,
2961 *packet_context_field_type
,
2962 *packet_header_type
,
2963 *packet_header_field_type
,
2965 *stream_event_context_type
,
2967 *event_header_field_type
;
2968 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2969 struct bt_ctf_trace
*trace
;
2971 int64_t ret_int64_t
;
2972 struct bt_value
*obj
;
2975 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2979 plan_tests(NR_TESTS
);
2981 if (!bt_mkdtemp(trace_path
)) {
2985 strcpy(metadata_path
, trace_path
);
2986 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2988 writer
= bt_ctf_writer_create(trace_path
);
2989 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2991 ok(!bt_ctf_writer_get_trace(NULL
),
2992 "bt_ctf_writer_get_trace correctly handles NULL");
2993 trace
= bt_ctf_writer_get_trace(writer
);
2995 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2996 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2997 "Set a trace's byte order to big endian");
2998 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2999 "bt_ctf_trace_get_byte_order returns a correct endianness");
3001 /* Add environment context to the trace */
3002 ret
= gethostname(hostname
, sizeof(hostname
));
3006 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
3007 "Add host (%s) environment field to writer instance",
3009 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
3011 "bt_ctf_writer_add_environment_field error with NULL writer");
3012 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
3014 "bt_ctf_writer_add_environment_field error with NULL field name");
3015 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
3017 "bt_ctf_writer_add_environment_field error with NULL field value");
3019 /* Test bt_ctf_trace_set_environment_field with an integer object */
3020 obj
= bt_value_integer_create_init(23);
3022 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
3023 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
3024 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
3025 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
3026 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
3027 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
3028 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
3029 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
3032 /* Test bt_ctf_trace_set_environment_field with a string object */
3033 obj
= bt_value_string_create_init("the value");
3035 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
3036 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
3039 /* Test bt_ctf_trace_set_environment_field_integer */
3040 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
3042 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
3043 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
3044 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
3045 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3047 "bt_ctf_trace_set_environment_field_integer succeeds");
3049 /* Test bt_ctf_trace_set_environment_field_string */
3050 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
3052 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
3053 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
3054 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
3055 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3057 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
3058 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3060 "bt_ctf_trace_set_environment_field_string succeeds");
3062 /* Test bt_ctf_trace_get_environment_field_count */
3063 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
3064 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
3065 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3066 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
3068 /* Test bt_ctf_trace_get_environment_field_name */
3069 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
3070 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
3071 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
3072 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
3073 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
3074 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
3075 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
3076 ok(ret_string
&& !strcmp(ret_string
, "host"),
3077 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3078 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
3079 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
3080 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3081 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
3082 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
3083 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3084 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
3085 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
3086 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3087 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
3088 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
3089 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3091 /* Test bt_ctf_trace_get_environment_field_value */
3092 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
3093 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
3094 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
3095 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
3096 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
3097 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
3098 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
3099 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3100 ok(!ret
&& ret_int64_t
== 23,
3101 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
3103 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
3104 ret
= bt_value_string_get(obj
, &ret_string
);
3105 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
3106 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
3109 /* Test bt_ctf_trace_get_environment_field_value_by_name */
3110 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
3112 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3113 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3114 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3115 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3116 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3117 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3119 ret
= bt_value_string_get(obj
, &ret_string
);
3120 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3121 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3124 /* Test environment field replacement */
3125 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3127 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3128 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3129 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3130 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
3131 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3132 ok(!ret
&& ret_int64_t
== 654321,
3133 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3136 /* On Solaris, uname() can return any positive value on success */
3137 if (uname(&name
) < 0) {
3142 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3143 == 0, "Add sysname (%s) environment field to writer instance",
3145 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3146 name
.nodename
) == 0,
3147 "Add nodename (%s) environment field to writer instance",
3149 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3150 == 0, "Add release (%s) environment field to writer instance",
3152 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3153 == 0, "Add version (%s) environment field to writer instance",
3155 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3156 == 0, "Add machine (%s) environment field to writer istance",
3159 /* Define a clock and add it to the trace */
3160 ok(bt_ctf_clock_create("signed") == NULL
,
3161 "Illegal clock name rejected");
3162 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
3163 clock
= bt_ctf_clock_create(clock_name
);
3164 ok(clock
, "Clock created sucessfully");
3165 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3166 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3167 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3168 "Returned clock name is valid");
3170 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3171 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3172 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3173 "Clock description set successfully");
3175 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3176 ok(returned_clock_description
,
3177 "bt_ctf_clock_get_description returns a description.");
3178 ok(returned_clock_description
?
3179 !strcmp(returned_clock_description
, clock_description
) : 0,
3180 "Returned clock description is valid");
3182 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3183 "bt_ctf_clock_get_frequency returns the correct default frequency");
3184 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3185 "Set clock frequency");
3186 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3187 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3189 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3190 "bt_ctf_clock_get_offset_s succeeds");
3191 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3192 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3193 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3194 "Set clock offset (seconds)");
3195 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3196 "bt_ctf_clock_get_offset_s succeeds");
3197 ok(get_offset_s
== offset_s
,
3198 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3200 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3201 "bt_ctf_clock_get_offset succeeds");
3202 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3203 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3204 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3205 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3206 "bt_ctf_clock_get_offset succeeds");
3207 ok(get_offset
== offset
,
3208 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3210 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3211 "bt_ctf_clock_get_precision returns the correct default precision");
3212 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3213 "Set clock precision");
3214 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3215 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3217 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3218 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3219 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3220 "Set clock absolute property");
3221 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3222 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3224 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3225 "bt_ctf_clock_get_time succeeds");
3226 ok(get_time
== DEFAULT_CLOCK_TIME
,
3227 "bt_ctf_clock_get_time returns the correct default time");
3228 ok(bt_ctf_clock_get_value(clock
) == DEFAULT_CLOCK_VALUE
,
3229 "bt_ctf_clock_get_value returns the correct default value");
3230 ok(bt_ctf_clock_set_value(clock
, current_time
) == 0,
3232 ok(bt_ctf_clock_get_value(clock
) == current_time
,
3233 "bt_ctf_clock_get_value returns the correct value once it is set");
3234 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3236 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3237 "bt_ctf_clock_get_time succeeds");
3238 ok(get_time
>= current_time
- 1 && get_time
<= current_time
+ 1,
3239 "bt_ctf_clock_get_time returns the correct time once it is set");
3241 ok(!bt_ctf_clock_get_name(NULL
),
3242 "bt_ctf_clock_get_name correctly handles NULL");
3243 ok(!bt_ctf_clock_get_description(NULL
),
3244 "bt_ctf_clock_get_description correctly handles NULL");
3245 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3246 "bt_ctf_clock_get_frequency correctly handles NULL");
3247 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3248 "bt_ctf_clock_get_precision correctly handles NULL");
3249 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3250 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3251 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3252 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3253 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3254 "bt_ctf_clock_get_offset correctly handles NULL clock");
3255 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3256 "bt_ctf_clock_get_offset correctly handles NULL output");
3257 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3258 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3259 ok(bt_ctf_clock_get_time(NULL
, &get_time
) < 0,
3260 "bt_ctf_clock_get_time correctly handles NULL clock");
3261 ok(bt_ctf_clock_get_time(clock
, NULL
) < 0,
3262 "bt_ctf_clock_get_time correctly handles NULL output");
3264 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3265 "bt_ctf_clock_set_description correctly handles NULL clock");
3266 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3267 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3268 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3269 "bt_ctf_clock_get_precision correctly handles NULL clock");
3270 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3271 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3272 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3273 "bt_ctf_clock_set_offset correctly handles NULL clock");
3274 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3275 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3276 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3277 "bt_ctf_clock_set_time correctly handles NULL clock");
3278 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3279 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3280 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3282 "bt_ctf_clock_get_uuid returns a UUID");
3284 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3285 /* Slightly modify UUID */
3286 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3289 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3290 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3291 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3292 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3293 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3294 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3295 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3297 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3298 ok(uuid_match(ret_uuid
, tmp_uuid
),
3299 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3301 /* Define a stream class */
3302 stream_class
= bt_ctf_stream_class_create("test_stream");
3304 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3305 "bt_ctf_stream_class_get_name handles NULL correctly");
3306 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3307 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3308 "bt_ctf_stream_class_get_name returns a correct stream class name");
3310 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3311 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3312 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3313 "bt_ctf_stream_class_get_clock handles NULL correctly");
3315 ok(stream_class
, "Create stream class");
3316 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3317 "Set a stream class' clock");
3318 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3319 ok(ret_clock
== clock
,
3320 "bt_ctf_stream_class_get_clock returns a correct clock");
3323 /* Test the event fields and event types APIs */
3326 /* Test fields copying */
3329 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3330 "bt_ctf_stream_class_get_id returns an error when no id is set");
3331 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3332 "bt_ctf_stream_class_get_id handles NULL correctly");
3333 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3334 "bt_ctf_stream_class_set_id handles NULL correctly");
3335 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3336 "Set an stream class' id");
3337 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3338 "bt_ctf_stream_class_get_id returns the correct value");
3340 /* Validate default event header fields */
3341 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3342 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3343 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3346 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3347 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_TYPE_ID_STRUCT
,
3348 "Default event header type is a structure");
3349 event_header_field_type
=
3350 bt_ctf_field_type_structure_get_field_type_by_name(
3351 ret_field_type
, "id");
3352 ok(event_header_field_type
,
3353 "Default event header type contains an \"id\" field");
3354 ok(bt_ctf_field_type_get_type_id(
3355 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3356 "Default event header \"id\" field is an integer");
3357 bt_put(event_header_field_type
);
3358 event_header_field_type
=
3359 bt_ctf_field_type_structure_get_field_type_by_name(
3360 ret_field_type
, "timestamp");
3361 ok(event_header_field_type
,
3362 "Default event header type contains a \"timestamp\" field");
3363 ok(bt_ctf_field_type_get_type_id(
3364 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3365 "Default event header \"timestamp\" field is an integer");
3366 bt_put(event_header_field_type
);
3367 bt_put(ret_field_type
);
3369 /* Add a custom trace packet header field */
3370 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3371 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3372 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3373 ok(packet_header_type
,
3374 "bt_ctf_trace_get_packet_header_type returns a packet header");
3375 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_TYPE_ID_STRUCT
,
3376 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3377 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3378 packet_header_type
, "magic");
3379 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3380 bt_put(ret_field_type
);
3381 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3382 packet_header_type
, "uuid");
3383 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3384 bt_put(ret_field_type
);
3385 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3386 packet_header_type
, "stream_id");
3387 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3388 bt_put(ret_field_type
);
3390 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3391 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3392 packet_header_field_type
, "custom_trace_packet_header_field"),
3393 "Added a custom trace packet header field successfully");
3395 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3396 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3397 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3398 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3399 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3400 "Set a trace packet_header_type successfully");
3402 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3403 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3405 /* Add a custom field to the stream class' packet context */
3406 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3407 ok(packet_context_type
,
3408 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3409 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_TYPE_ID_STRUCT
,
3410 "Packet context is a structure");
3412 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3413 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3414 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3415 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3417 integer_type
= bt_ctf_field_type_integer_create(32);
3418 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3420 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3421 /* Create a "uint5_t" equivalent custom packet context field */
3422 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3424 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3425 packet_context_field_type
, "custom_packet_context_field");
3426 ok(ret
== 0, "Packet context field added successfully");
3428 /* Define a stream event context containing a my_integer field. */
3429 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3430 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3431 ok(bt_ctf_stream_class_get_event_context_type(
3432 stream_class
) == NULL
,
3433 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3434 stream_event_context_type
= bt_ctf_field_type_structure_create();
3435 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3436 integer_type
, "common_event_context");
3438 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3439 stream_event_context_type
) < 0,
3440 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3441 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3443 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3444 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3446 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3448 ok(bt_ctf_stream_class_set_event_context_type(
3449 stream_class
, stream_event_context_type
) == 0,
3450 "Set a new stream event context type");
3451 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3453 ok(ret_field_type
== stream_event_context_type
,
3454 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3455 bt_put(ret_field_type
);
3457 /* Instantiate a stream and append events */
3458 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3459 ok(stream1
, "Instanciate a stream class from writer");
3462 * Creating a stream through a writer adds the given stream
3463 * class to the writer's trace, thus registering the stream
3464 * class's clock to the trace.
3466 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
3467 "bt_ctf_trace_get_clock_count correctly handles NULL");
3468 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
3469 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3470 ok(!bt_ctf_trace_get_clock(NULL
, 0),
3471 "bt_ctf_trace_get_clock correctly handles NULL");
3472 ok(!bt_ctf_trace_get_clock(trace
, -1),
3473 "bt_ctf_trace_get_clock correctly handles negative indexes");
3474 ok(!bt_ctf_trace_get_clock(trace
, 1),
3475 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3476 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
3477 ok(ret_clock
== clock
,
3478 "bt_ctf_trace_get_clock returns the right clock instance");
3480 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
3481 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3482 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
3483 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3484 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
3485 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3486 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
3487 ok(ret_clock
== clock
,
3488 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3490 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
3491 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3493 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3494 "bt_ctf_stream_get_class correctly handles NULL");
3495 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3496 ok(ret_stream_class
,
3497 "bt_ctf_stream_get_class returns a stream class");
3498 ok(ret_stream_class
== stream_class
,
3499 "Returned stream class is of the correct type");
3502 * Packet header, packet context, event header, and stream
3503 * event context types were copied for the resolving
3506 BT_PUT(packet_header_type
);
3507 BT_PUT(packet_context_type
);
3508 BT_PUT(stream_event_context_type
);
3509 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3510 assert(packet_header_type
);
3511 packet_context_type
=
3512 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3513 assert(packet_context_type
);
3514 stream_event_context_type
=
3515 bt_ctf_stream_class_get_event_context_type(stream_class
);
3516 assert(stream_event_context_type
);
3519 * Try to modify the packet context type after a stream has been
3522 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3523 packet_header_field_type
, "should_fail");
3525 "Trace packet header type can't be modified once a stream has been instanciated");
3528 * Try to modify the packet context type after a stream has been
3531 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3532 packet_context_field_type
, "should_fail");
3534 "Packet context type can't be modified once a stream has been instanciated");
3537 * Try to modify the stream event context type after a stream has been
3540 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3541 integer_type
, "should_fail");
3543 "Stream event context type can't be modified once a stream has been instanciated");
3545 /* Should fail after instanciating a stream (frozen) */
3546 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3547 "Changes to a stream class that was already instantiated fail");
3549 /* Populate the custom packet header field only once for all tests */
3550 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3551 "bt_ctf_stream_get_packet_header handles NULL correctly");
3552 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3554 "bt_ctf_stream_get_packet_header returns a packet header");
3555 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3556 ok(ret_field_type
== packet_header_type
,
3557 "Stream returns a packet header of the appropriate type");
3558 bt_put(ret_field_type
);
3559 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3560 "custom_trace_packet_header_field");
3561 ok(packet_header_field
,
3562 "Packet header structure contains a custom field with the appropriate name");
3563 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3564 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3565 "Custom packet header field is of the expected type");
3566 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3567 54321), "Set custom packet header value successfully");
3568 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3569 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3570 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3571 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3572 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3573 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3574 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3575 "Successfully set a stream's packet header");
3577 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3578 "Add environment field to writer after stream creation");
3582 test_trace_stream_class_clock();
3584 test_create_writer_vs_non_writer_mode();
3586 test_instanciate_event_before_stream(writer
);
3588 append_simple_event(stream_class
, stream1
, clock
);
3590 packet_resize_test(stream_class
, stream1
, clock
);
3592 append_complex_event(stream_class
, stream1
, clock
);
3594 append_existing_event_class(stream_class
);
3596 test_empty_stream(writer
);
3598 test_custom_event_header_stream(writer
);
3600 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3601 ok(metadata_string
, "Get metadata string");
3603 bt_ctf_writer_flush_metadata(writer
);
3604 validate_metadata(argv
[1], metadata_path
);
3605 validate_trace(argv
[2], trace_path
);
3608 bt_put(ret_stream_class
);
3611 bt_put(packet_context_type
);
3612 bt_put(packet_context_field_type
);
3613 bt_put(integer_type
);
3614 bt_put(stream_event_context_type
);
3615 bt_put(ret_field_type
);
3616 bt_put(packet_header_type
);
3617 bt_put(packet_header_field_type
);
3618 bt_put(packet_header
);
3619 bt_put(packet_header_field
);
3621 free(metadata_string
);
3622 bt_put(stream_class
);
3624 delete_trace(trace_path
);