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/ctf-ir/clock-class.h>
31 #include <babeltrace/ref.h>
32 #include <babeltrace/ctf/events.h>
33 #include <babeltrace/values.h>
35 #include <babeltrace/compat/stdlib.h>
37 #include <sys/utsname.h>
38 #include <babeltrace/compat/limits.h>
39 #include <babeltrace/compat/stdio.h>
44 #include <babeltrace/compat/dirent.h>
50 #define METADATA_LINE_SIZE 512
51 #define SEQUENCE_TEST_LENGTH 10
52 #define ARRAY_TEST_LENGTH 5
53 #define PACKET_RESIZE_TEST_LENGTH 100000
55 #define DEFAULT_CLOCK_FREQ 1000000000
56 #define DEFAULT_CLOCK_PRECISION 1
57 #define DEFAULT_CLOCK_OFFSET 0
58 #define DEFAULT_CLOCK_OFFSET_S 0
59 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
60 #define DEFAULT_CLOCK_TIME 0
61 #define DEFAULT_CLOCK_VALUE 0
65 static int64_t current_time
= 42;
67 /* Return 1 if uuids match, zero if different. */
69 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
74 if (!uuid_a
|| !uuid_b
) {
78 for (i
= 0; i
< 16; i
++) {
79 if (uuid_a
[i
] != uuid_b
[i
]) {
90 void validate_metadata(char *parser_path
, char *metadata_path
)
93 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
94 int parser_output_fd
= -1, metadata_fd
= -1;
101 parser_output_fd
= mkstemp(parser_output_path
);
102 metadata_fd
= open(metadata_path
, O_RDONLY
);
104 unlink(parser_output_path
);
106 if (parser_output_fd
== -1 || metadata_fd
== -1) {
107 diag("Failed create temporary files for metadata parsing.");
115 waitpid(pid
, &status
, 0);
116 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
118 /* ctf-parser-test expects a metadata string on stdin. */
119 ret
= dup2(metadata_fd
, STDIN_FILENO
);
121 perror("# dup2 metadata_fd to STDIN");
125 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
127 perror("# dup2 parser_output_fd to STDOUT");
131 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
133 perror("# dup2 parser_output_fd to STDERR");
137 execl(parser_path
, "ctf-parser-test", NULL
);
138 perror("# Could not launch the ctf metadata parser process");
142 ok(ret
== 0, "Metadata string is valid");
144 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
146 size_t len
= METADATA_LINE_SIZE
;
147 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
149 metadata_fp
= fdopen(metadata_fd
, "r");
151 perror("fdopen on metadata_fd");
156 parser_output_fp
= fdopen(parser_output_fd
, "r");
157 if (!parser_output_fp
) {
158 perror("fdopen on parser_output_fd");
161 parser_output_fd
= -1;
165 diag("malloc failure");
170 /* Output the metadata and parser output as diagnostic */
171 while (bt_getline(&line
, &len
, metadata_fp
) > 0) {
172 fprintf(stderr
, "# %s", line
);
175 rewind(parser_output_fp
);
176 while (bt_getline(&line
, &len
, parser_output_fp
) > 0) {
177 fprintf(stderr
, "# %s", line
);
183 if (fclose(metadata_fp
)) {
184 diag("fclose failure");
187 if (parser_output_fp
) {
188 if (fclose(parser_output_fp
)) {
189 diag("fclose failure");
194 if (parser_output_fd
>= 0) {
195 if (close(parser_output_fd
)) {
199 if (metadata_fd
>= 0) {
200 if (close(metadata_fd
)) {
207 void validate_trace(char *parser_path
, char *trace_path
)
210 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
211 int babeltrace_output_fd
= -1;
218 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
219 unlink(babeltrace_output_path
);
221 if (babeltrace_output_fd
== -1) {
222 diag("Failed to create a temporary file for trace parsing.");
230 waitpid(pid
, &status
, 0);
231 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
233 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
235 perror("# dup2 babeltrace_output_fd to STDOUT");
239 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
241 perror("# dup2 babeltrace_output_fd to STDERR");
245 execl(parser_path
, "babeltrace", trace_path
, NULL
);
246 perror("# Could not launch the babeltrace process");
250 ok(ret
== 0, "Babeltrace could read the resulting trace");
252 if (ret
&& babeltrace_output_fd
>= 0) {
254 size_t len
= METADATA_LINE_SIZE
;
255 FILE *babeltrace_output_fp
= NULL
;
257 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
258 if (!babeltrace_output_fp
) {
259 perror("fdopen on babeltrace_output_fd");
262 babeltrace_output_fd
= -1;
266 diag("malloc error");
268 rewind(babeltrace_output_fp
);
269 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
275 if (babeltrace_output_fp
) {
276 if (fclose(babeltrace_output_fp
)) {
277 diag("fclose error");
282 if (babeltrace_output_fd
>= 0) {
283 if (close(babeltrace_output_fd
)) {
290 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
291 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
293 /* Create and add a simple event class */
294 struct bt_ctf_event_class
*simple_event_class
=
295 bt_ctf_event_class_create("Simple Event");
296 struct bt_ctf_field_type
*uint_12_type
=
297 bt_ctf_field_type_integer_create(12);
298 struct bt_ctf_field_type
*int_64_type
=
299 bt_ctf_field_type_integer_create(64);
300 struct bt_ctf_field_type
*float_type
=
301 bt_ctf_field_type_floating_point_create();
302 struct bt_ctf_field_type
*enum_type
;
303 struct bt_ctf_field_type
*enum_type_unsigned
=
304 bt_ctf_field_type_enumeration_create(uint_12_type
);
305 struct bt_ctf_field_type
*event_context_type
=
306 bt_ctf_field_type_structure_create();
307 struct bt_ctf_field_type
*event_payload_type
= NULL
;
308 struct bt_ctf_field_type
*returned_type
;
309 struct bt_ctf_event
*simple_event
;
310 struct bt_ctf_field
*integer_field
;
311 struct bt_ctf_field
*float_field
;
312 struct bt_ctf_field
*enum_field
;
313 struct bt_ctf_field
*enum_field_unsigned
;
314 struct bt_ctf_field
*enum_container_field
;
315 const char *mapping_name_test
= "truie";
316 const double double_test_value
= 3.1415;
317 struct bt_ctf_field
*enum_container_field_unsigned
;
318 const char *mapping_name_negative_test
= "negative_value";
319 const char *ret_char
;
321 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
322 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
323 struct bt_ctf_event_class
*ret_event_class
;
324 struct bt_ctf_field
*packet_context
;
325 struct bt_ctf_field
*packet_context_field
;
326 struct bt_ctf_field
*stream_event_context
;
327 struct bt_ctf_field
*stream_event_context_field
;
328 struct bt_ctf_field
*event_context
;
329 struct bt_ctf_field
*event_context_field
;
330 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
331 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
332 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
333 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
336 ok(uint_12_type
, "Create an unsigned integer type");
338 ok(!bt_ctf_field_type_integer_set_signed(int_64_type
, 1),
339 "Set signed 64 bit integer signedness to true");
340 ok(int_64_type
, "Create a signed integer type");
341 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
343 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
344 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
345 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
346 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
347 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
348 bt_put(returned_type
);
350 bt_ctf_field_type_set_alignment(float_type
, 32);
351 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
352 "bt_ctf_field_type_get_alignment handles NULL correctly");
353 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
354 "bt_ctf_field_type_get_alignment returns a correct value");
356 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
357 "Set a floating point type's exponent digit count");
358 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
359 "Set a floating point type's mantissa digit count");
361 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
362 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
363 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
364 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
365 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
366 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
367 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
368 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
370 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
371 mapping_name_negative_test
, -12345, 0) == 0,
372 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
373 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
374 "escaping; \"test\"", 1, 1) == 0,
375 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
376 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
377 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
378 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
379 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
380 "event clock int float", 5, 22) == 0,
381 "Accept enumeration mapping strings containing reserved keywords");
382 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
384 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
385 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
386 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
387 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
388 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
389 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
390 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
392 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(NULL
, -42);
393 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly");
395 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, -4200000);
396 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values");
398 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, 3);
399 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value");
400 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, NULL
, NULL
, NULL
) == 0,
401 "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly");
404 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
405 "enum_field") == 0, "Add signed enumeration field to event");
407 ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL
, 0, &ret_char
,
408 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
409 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly");
410 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, NULL
,
411 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
412 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly");
413 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
414 NULL
, &ret_range_end_int64_t
) == 0,
415 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly");
416 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
417 &ret_range_start_int64_t
, NULL
) == 0,
418 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly");
419 /* Assumes entries are sorted by range_start values. */
420 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 6, &ret_char
,
421 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
422 "bt_ctf_field_type_enumeration_get_mapping_signed returns a value");
423 ok(!strcmp(ret_char
, mapping_name_test
),
424 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name");
425 ok(ret_range_start_int64_t
== 42,
426 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start");
427 ok(ret_range_end_int64_t
== 42,
428 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end");
430 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
431 "escaping; \"test\"", 0, 0) == 0,
432 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
433 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
434 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
435 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
436 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
437 "event clock int float", 5, 22) == 0,
438 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
439 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
441 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
442 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names");
443 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
444 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries");
445 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
446 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
447 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
448 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
450 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
451 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
452 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 6,
453 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
455 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
456 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
457 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
458 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
459 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
460 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
461 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
462 NULL
, &ret_range_end_uint64_t
) == 0,
463 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
464 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
465 &ret_range_start_uint64_t
, NULL
) == 0,
466 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
467 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 4, &ret_char
,
468 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
469 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
470 ok(!strcmp(ret_char
, mapping_name_test
),
471 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
472 ok(ret_range_start_uint64_t
== 42,
473 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
474 ok(ret_range_end_uint64_t
== 42,
475 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
477 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
479 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
482 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
484 /* Set an event context type which will contain a single integer. */
485 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
486 "event_specific_context"),
487 "Add event specific context field");
488 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
489 "bt_ctf_event_class_get_context_type handles NULL correctly");
490 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
491 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
493 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
494 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
495 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
496 "Set an event class' context type successfully");
497 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
498 ok(returned_type
== event_context_type
,
499 "bt_ctf_event_class_get_context_type returns the appropriate type");
500 bt_put(returned_type
);
502 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
503 "Adding simple event class to stream class");
506 * bt_ctf_stream_class_add_event_class() copies the field types
507 * of simple_event_class, so we retrieve the new ones to create
508 * the appropriate fields.
510 BT_PUT(event_context_type
);
511 BT_PUT(event_payload_type
);
512 event_payload_type
= bt_ctf_event_class_get_payload_type(
514 assert(event_payload_type
);
515 event_context_type
= bt_ctf_event_class_get_context_type(
517 assert(event_context_type
);
518 ep_integer_field_type
=
519 bt_ctf_field_type_structure_get_field_type_by_name(
520 event_payload_type
, "integer_field");
521 assert(ep_integer_field_type
);
523 bt_ctf_field_type_structure_get_field_type_by_name(
524 event_payload_type
, "enum_field");
525 assert(ep_enum_field_type
);
526 ep_enum_field_unsigned_type
=
527 bt_ctf_field_type_structure_get_field_type_by_name(
528 event_payload_type
, "enum_field_unsigned");
529 assert(ep_enum_field_unsigned_type
);
531 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
532 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
533 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
534 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
535 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
536 "bt_ctf_stream_class_get_event_class handles NULL correctly");
537 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
538 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
539 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
540 ok(ret_event_class
== simple_event_class
,
541 "bt_ctf_stream_class_get_event_class returns the correct event class");
542 bt_put(ret_event_class
);
543 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
544 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
545 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
546 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
548 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
549 ok(ret_event_class
== simple_event_class
,
550 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
551 bt_put(ret_event_class
);
553 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
554 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
555 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
556 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
557 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
558 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
559 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
560 ok(ret_event_class
== simple_event_class
,
561 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
562 bt_put(ret_event_class
);
564 simple_event
= bt_ctf_event_create(simple_event_class
);
566 "Instantiate an event containing a single integer field");
568 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
569 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
570 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
571 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
573 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
574 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
575 "bt_ctf_field_floating_point_get_value fails on an unset float field");
576 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
577 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
578 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
579 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
580 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
581 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
582 "bt_ctf_field_floating_point_get_value returns a double value");
583 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
584 "bt_ctf_field_floating_point_get_value returns a correct value");
586 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
589 iter
= bt_ctf_field_enumeration_get_mappings(NULL
);
590 ok(!iter
, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
591 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
592 ok(!iter
, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
593 enum_container_field
= bt_ctf_field_enumeration_get_container(
595 ok(bt_ctf_field_signed_integer_set_value(
596 enum_container_field
, -42) == 0,
597 "Set signed enumeration container value");
598 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
599 ok(iter
, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
600 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_name(iter
, &ret_char
);
601 ok(!ret
&& ret_char
, "bt_ctf_field_type_enumeration_mapping_iterator_get_name return a mapping name");
602 ok(!strcmp(ret_char
, mapping_name_negative_test
),
603 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
604 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
608 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
609 assert(enum_field_unsigned
);
610 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
611 enum_field_unsigned
);
612 ok(bt_ctf_field_unsigned_integer_set_value(
613 enum_container_field_unsigned
, 42) == 0,
614 "Set unsigned enumeration container value");
615 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
616 enum_field_unsigned
);
618 iter
= bt_ctf_field_enumeration_get_mappings(enum_field_unsigned
);
620 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_name(iter
, &ret_char
);
621 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
622 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an unsigned container");
624 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
626 /* Populate stream event context */
627 stream_event_context
=
628 bt_ctf_event_get_stream_event_context(simple_event
);
629 assert(stream_event_context
);
630 stream_event_context_field
= bt_ctf_field_structure_get_field(
631 stream_event_context
, "common_event_context");
632 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
634 /* Populate the event's context */
635 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
636 "bt_ctf_event_get_event_context handles NULL correctly");
637 event_context
= bt_ctf_event_get_event_context(simple_event
);
639 "bt_ctf_event_get_event_context returns a field");
640 returned_type
= bt_ctf_field_get_type(event_context
);
641 ok(returned_type
== event_context_type
,
642 "bt_ctf_event_get_event_context returns a field of the appropriate type");
643 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
644 "event_specific_context");
645 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
646 "Successfully set an event context's value");
647 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
648 "bt_ctf_event_set_event_context handles a NULL event correctly");
649 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
650 "bt_ctf_event_set_event_context handles a NULL event context correctly");
651 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
652 "bt_ctf_event_set_event_context rejects a context of the wrong type");
653 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
654 "Set an event context successfully");
656 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
657 "Append simple event to trace stream");
659 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
660 "bt_ctf_stream_get_packet_context handles NULL correctly");
661 packet_context
= bt_ctf_stream_get_packet_context(stream
);
663 "bt_ctf_stream_get_packet_context returns a packet context");
665 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
667 ok(packet_context_field
,
668 "Packet context contains the default packet_size field.");
669 bt_put(packet_context_field
);
670 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
671 "custom_packet_context_field");
672 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
673 "Custom packet context field value successfully set.");
675 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
676 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
677 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
678 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
679 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
680 "Successfully set a stream's packet context");
682 ok(bt_ctf_stream_flush(stream
) == 0,
683 "Flush trace stream with one event");
685 bt_put(simple_event_class
);
686 bt_put(simple_event
);
687 bt_put(uint_12_type
);
691 bt_put(enum_type_unsigned
);
692 bt_put(returned_type
);
693 bt_put(event_context_type
);
694 bt_put(integer_field
);
697 bt_put(enum_field_unsigned
);
698 bt_put(enum_container_field
);
699 bt_put(enum_container_field_unsigned
);
700 bt_put(packet_context
);
701 bt_put(packet_context_field
);
702 bt_put(stream_event_context
);
703 bt_put(stream_event_context_field
);
704 bt_put(event_context
);
705 bt_put(event_context_field
);
706 bt_put(event_payload_type
);
707 bt_put(ep_integer_field_type
);
708 bt_put(ep_enum_field_type
);
709 bt_put(ep_enum_field_unsigned_type
);
714 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
715 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
717 struct event_class_attrs_counts
{
728 struct event_class_attrs_counts
;
729 const char *complex_test_event_string
= "Complex Test Event";
730 const char *test_string_1
= "Test ";
731 const char *test_string_2
= "string ";
732 const char *test_string_3
= "abcdefghi";
733 const char *test_string_4
= "abcd\0efg\0hi";
734 const char *test_string_cat
= "Test string abcdeabcd";
735 struct bt_ctf_field_type
*uint_35_type
=
736 bt_ctf_field_type_integer_create(35);
737 struct bt_ctf_field_type
*int_16_type
=
738 bt_ctf_field_type_integer_create(16);
739 struct bt_ctf_field_type
*uint_3_type
=
740 bt_ctf_field_type_integer_create(3);
741 struct bt_ctf_field_type
*enum_variant_type
=
742 bt_ctf_field_type_enumeration_create(uint_3_type
);
743 struct bt_ctf_field_type
*variant_type
=
744 bt_ctf_field_type_variant_create(enum_variant_type
,
746 struct bt_ctf_field_type
*string_type
=
747 bt_ctf_field_type_string_create();
748 struct bt_ctf_field_type
*sequence_type
;
749 struct bt_ctf_field_type
*array_type
;
750 struct bt_ctf_field_type
*inner_structure_type
=
751 bt_ctf_field_type_structure_create();
752 struct bt_ctf_field_type
*complex_structure_type
=
753 bt_ctf_field_type_structure_create();
754 struct bt_ctf_field_type
*ret_field_type
;
755 struct bt_ctf_event_class
*event_class
;
756 struct bt_ctf_event
*event
;
757 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
758 *inner_structure_field
, *complex_structure_field
,
759 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
760 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
761 *stream_event_ctx_int_field
, *ret_field
;
762 uint64_t ret_unsigned_int
;
763 int64_t ret_signed_int
;
764 const char *ret_string
;
765 struct bt_ctf_stream_class
*ret_stream_class
;
766 struct bt_ctf_event_class
*ret_event_class
;
767 struct bt_ctf_field
*packet_context
, *packet_context_field
;
768 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
769 struct bt_value
*obj
;
771 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
772 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
773 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
774 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
775 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
776 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
777 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
778 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
779 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
780 "Set alignment of signed 16 bit integer to 32");
781 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
782 "Set integer signedness to true");
783 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
784 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
785 "Set signed 16 bit integer base to hexadecimal");
787 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
788 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
791 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
792 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
793 ret_field_type
= bt_ctf_field_type_array_get_element_type(
795 ok(ret_field_type
== int_16_type
,
796 "bt_ctf_field_type_array_get_element_type returns the correct type");
797 bt_put(ret_field_type
);
799 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
800 "bt_ctf_field_type_array_get_length handles NULL correctly");
801 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
802 "bt_ctf_field_type_array_get_length returns the correct length");
804 ok(bt_ctf_field_type_structure_add_field(inner_structure_type
,
805 inner_structure_type
, "yes"), "Cannot add self to structure");
806 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
807 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
808 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
809 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
810 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
811 array_type
, "an_array"), "Add an_array field to inner structure");
813 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
815 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
817 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
818 "UINT35_TYPE", 2, 7);
820 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(NULL
, "INT16_TYPE");
821 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly");
823 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, "INT16_TYPE");
824 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly");
825 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
826 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly");
829 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, NULL
);
830 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly");
832 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL
, 1);
833 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly");
835 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, -42);
836 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly");
837 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) != 0,
838 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly");
841 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, 5);
842 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly");
843 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
844 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly");
847 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
848 "An unknown entry"), "Reject a variant field based on an unknown tag value");
849 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
850 "UINT3_TYPE") == 0, "Add a field to a variant");
851 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
852 "INT16_TYPE"), "Add INT16_TYPE field to variant");
853 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
854 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
856 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
857 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
858 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
859 ok(ret_field_type
== enum_variant_type
,
860 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
861 bt_put(ret_field_type
);
863 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
864 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
865 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
866 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
867 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
868 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
869 "INT16_TYPE") == NULL
,
870 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
871 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
873 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
874 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
875 variant_type
, "INT16_TYPE");
876 ok(ret_field_type
== int_16_type
,
877 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
878 bt_put(ret_field_type
);
880 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
881 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
882 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
883 "bt_ctf_field_type_variant_get_field_count returns the correct count");
885 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
886 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
887 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
888 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
889 bt_put(ret_field_type
);
890 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
891 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
892 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
893 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
894 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
895 "bt_ctf_field_type_variant_get_field returns a field");
896 ok(!strcmp("INT16_TYPE", ret_string
),
897 "bt_ctf_field_type_variant_get_field returns a correct field name");
898 ok(ret_field_type
== int_16_type
,
899 "bt_ctf_field_type_variant_get_field returns a correct field type");
900 bt_put(ret_field_type
);
902 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
903 enum_variant_type
, "variant_selector"),
904 "Add variant_selector field to complex structure");
905 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
906 string_type
, "a_string"), "Add a_string field to complex structure");
907 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
908 variant_type
, "variant_value"),
909 "Add variant_value field to complex structure");
910 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
911 inner_structure_type
, "inner_structure"),
912 "Add inner_structure field to complex structure");
914 ok(bt_ctf_event_class_create("clock") == NULL
,
915 "Reject creation of an event class with an illegal name");
916 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
917 ok(event_class
, "Create an event class");
918 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
919 "Reject addition of a field with an empty name to an event");
920 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
921 "Reject addition of a field with a NULL type to an event");
922 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
924 "Reject addition of a type with an illegal name to an event");
925 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
927 "Add field of type unsigned integer to an event");
928 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
929 "int_16") == 0, "Add field of type signed integer to an event");
930 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
931 "complex_structure") == 0,
932 "Add composite structure to an event");
934 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
935 "bt_ctf_event_class_get_name handles NULL correctly");
936 ret_string
= bt_ctf_event_class_get_name(event_class
);
937 ok(!strcmp(ret_string
, complex_test_event_string
),
938 "bt_ctf_event_class_get_name returns a correct name");
939 ok(bt_ctf_event_class_get_id(event_class
) < 0,
940 "bt_ctf_event_class_get_id returns a negative value when not set");
941 ok(bt_ctf_event_class_get_id(NULL
) < 0,
942 "bt_ctf_event_class_get_id handles NULL correctly");
943 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
944 "bt_ctf_event_class_set_id handles NULL correctly");
945 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
946 "Set an event class' id");
947 ok(bt_ctf_event_class_get_id(event_class
) == 42,
948 "bt_ctf_event_class_get_id returns the correct value");
950 /* Test event class attributes */
951 obj
= bt_value_integer_create_init(15);
953 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
954 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
955 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
956 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
957 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
958 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
959 assert(!bt_value_integer_set(obj
, -3));
960 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
961 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
962 assert(!bt_value_integer_set(obj
, 11));
963 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
964 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
965 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
966 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
967 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
969 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
972 obj
= bt_value_integer_create_init(5);
974 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
975 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
977 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
978 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
979 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
980 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
981 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
982 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
983 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
986 ret
= bt_value_integer_get(obj
, &int64_value
);
987 ok(obj
&& !ret
&& int64_value
== 5,
988 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
991 obj
= bt_value_string_create_init("nu name");
993 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
994 ret_string
= bt_ctf_event_class_get_name(event_class
);
995 ok(!strcmp(ret_string
, "nu name"),
996 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
997 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
998 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
1000 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
1002 obj
= bt_value_string_create_init("http://kernel.org/");
1004 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
1007 ok(bt_ctf_event_class_get_attribute_count(NULL
),
1008 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1009 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
1010 "bt_ctf_event_class_get_attribute_count returns the correct count");
1011 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
1012 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1013 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
1014 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1015 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
1016 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1017 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
1018 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1020 memset(&attrs_count
, 0, sizeof(attrs_count
));
1022 for (i
= 0; i
< 4; ++i
) {
1023 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
1025 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
1026 assert(ret_string
&& obj
);
1028 if (!strcmp(ret_string
, "id")) {
1030 ok(bt_value_is_integer(obj
),
1031 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1033 } else if (!strcmp(ret_string
, "name")) {
1035 ok(bt_value_is_string(obj
),
1036 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1038 } else if (!strcmp(ret_string
, "loglevel")) {
1039 attrs_count
.loglevel
++;
1040 ok(bt_value_is_integer(obj
),
1041 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1043 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1044 attrs_count
.modelemfuri
++;
1045 ok(bt_value_is_string(obj
),
1046 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1049 attrs_count
.unknown
++;
1055 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1056 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1057 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1058 "event class has one instance of each known attribute");
1060 /* Add event class to the stream class */
1061 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1062 "Reject addition of NULL event class to a stream class");
1063 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1064 event_class
) == 0, "Add an event class to stream class");
1066 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1067 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1068 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1069 ok(ret_stream_class
== stream_class
,
1070 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1071 bt_put(ret_stream_class
);
1073 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1074 "bt_ctf_event_class_get_field_count handles NULL correctly");
1075 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1076 "bt_ctf_event_class_get_field_count returns a correct value");
1078 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1079 &ret_field_type
, 0) < 0,
1080 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1081 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1082 &ret_field_type
, 0) == 0,
1083 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1084 bt_put(ret_field_type
);
1085 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1087 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1088 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1089 &ret_field_type
, 42) < 0,
1090 "bt_ctf_event_class_get_field handles an invalid index correctly");
1091 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1092 &ret_field_type
, 0) == 0,
1093 "bt_ctf_event_class_get_field returns a field");
1094 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
1095 "bt_ctf_event_class_get_field returns a correct field type");
1096 bt_put(ret_field_type
);
1097 ok(!strcmp(ret_string
, "uint_35"),
1098 "bt_ctf_event_class_get_field returns a correct field name");
1099 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1100 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1101 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1102 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1103 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1104 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1105 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1106 "complex_structure");
1107 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
1108 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1109 bt_put(ret_field_type
);
1111 event
= bt_ctf_event_create(event_class
);
1112 ok(event
, "Instanciate a complex event");
1114 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1115 "bt_ctf_event_get_class handles NULL correctly");
1116 ret_event_class
= bt_ctf_event_get_class(event
);
1117 ok(ret_event_class
== event_class
,
1118 "bt_ctf_event_get_class returns the correct event class");
1119 bt_put(ret_event_class
);
1121 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1122 if (!uint_35_field
) {
1123 printf("uint_35_field is NULL\n");
1126 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1127 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1128 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1129 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1130 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1131 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1132 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1133 &ret_unsigned_int
) == 0,
1134 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1135 ok(ret_unsigned_int
== 0x0DDF00D,
1136 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1137 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1138 &ret_signed_int
) < 0,
1139 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1140 bt_put(uint_35_field
);
1142 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1143 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1144 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1145 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1146 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1147 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1148 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1149 &ret_signed_int
) == 0,
1150 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1151 ok(ret_signed_int
== -12345,
1152 "bt_ctf_field_signed_integer_get_value returns the correct value");
1153 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1154 &ret_unsigned_int
) < 0,
1155 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1156 bt_put(int_16_field
);
1158 complex_structure_field
= bt_ctf_event_get_payload(event
,
1159 "complex_structure");
1161 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1162 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1163 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1164 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1165 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1166 complex_structure_field
, 3);
1167 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1168 bt_put(inner_structure_field
);
1169 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1170 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1171 bt_put(ret_field_type
);
1173 inner_structure_field
= bt_ctf_field_structure_get_field(
1174 complex_structure_field
, "inner_structure");
1175 a_string_field
= bt_ctf_field_structure_get_field(
1176 complex_structure_field
, "a_string");
1177 enum_variant_field
= bt_ctf_field_structure_get_field(
1178 complex_structure_field
, "variant_selector");
1179 variant_field
= bt_ctf_field_structure_get_field(
1180 complex_structure_field
, "variant_value");
1181 uint_35_field
= bt_ctf_field_structure_get_field(
1182 inner_structure_field
, "seq_len");
1183 a_sequence_field
= bt_ctf_field_structure_get_field(
1184 inner_structure_field
, "a_sequence");
1185 an_array_field
= bt_ctf_field_structure_get_field(
1186 inner_structure_field
, "an_array");
1188 enum_container_field
= bt_ctf_field_enumeration_get_container(
1189 enum_variant_field
);
1190 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1191 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1192 enum_variant_field
);
1193 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1194 bt_put(int_16_field
);
1195 ok(!bt_ctf_field_string_get_value(a_string_field
),
1196 "bt_ctf_field_string_get_value returns NULL on an unset field");
1197 bt_ctf_field_string_set_value(a_string_field
,
1199 ok(!bt_ctf_field_string_get_value(NULL
),
1200 "bt_ctf_field_string_get_value correctly handles NULL");
1201 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1202 "bt_ctf_field_string_append correctly handles a NULL string field");
1203 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1204 "bt_ctf_field_string_append correctly handles a NULL string value");
1205 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1206 "bt_ctf_field_string_append succeeds");
1207 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1208 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1209 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1210 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1211 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1212 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1213 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1214 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1215 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1216 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1217 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1218 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1220 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1221 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1222 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1223 "bt_ctf_field_string_get_value returns a correct value");
1224 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1225 SEQUENCE_TEST_LENGTH
);
1227 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1228 enum_container_field
) == NULL
,
1229 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1230 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1232 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1233 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1234 variant_type
, enum_variant_field
);
1235 ok(ret_field_type
== int_16_type
,
1236 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1238 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1239 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1240 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1241 uint_35_field
) == 0, "Set a sequence field's length");
1242 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1243 ok(ret_field
== uint_35_field
,
1244 "bt_ctf_field_sequence_get_length returns the correct length field");
1245 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1246 "bt_ctf_field_sequence_get_length properly handles NULL");
1248 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1249 int_16_field
= bt_ctf_field_sequence_get_field(
1250 a_sequence_field
, i
);
1251 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1252 bt_put(int_16_field
);
1255 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1256 int_16_field
= bt_ctf_field_array_get_field(
1258 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1259 bt_put(int_16_field
);
1262 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1263 assert(stream_event_ctx_field
);
1264 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1265 stream_event_ctx_field
, "common_event_context");
1266 BT_PUT(stream_event_ctx_field
);
1267 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1268 BT_PUT(stream_event_ctx_int_field
);
1270 bt_ctf_clock_set_time(clock
, ++current_time
);
1271 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1272 "Append a complex event to a stream");
1275 * Populate the custom packet context field with a dummy value
1276 * otherwise flush will fail.
1278 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1279 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1280 "custom_packet_context_field");
1281 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1283 ok(bt_ctf_stream_flush(stream
) == 0,
1284 "Flush a stream containing a complex event");
1286 bt_put(uint_35_field
);
1287 bt_put(a_string_field
);
1288 bt_put(inner_structure_field
);
1289 bt_put(complex_structure_field
);
1290 bt_put(a_sequence_field
);
1291 bt_put(an_array_field
);
1292 bt_put(enum_variant_field
);
1293 bt_put(enum_container_field
);
1294 bt_put(variant_field
);
1296 bt_put(packet_context_field
);
1297 bt_put(packet_context
);
1298 bt_put(uint_35_type
);
1299 bt_put(int_16_type
);
1300 bt_put(string_type
);
1301 bt_put(sequence_type
);
1303 bt_put(inner_structure_type
);
1304 bt_put(complex_structure_type
);
1305 bt_put(uint_3_type
);
1306 bt_put(enum_variant_type
);
1307 bt_put(variant_type
);
1308 bt_put(ret_field_type
);
1309 bt_put(event_class
);
1314 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1315 struct bt_ctf_field_type
*expected_type
, const char *name
)
1317 struct bt_ctf_field_type
*copy_type
;
1319 copy_type
= bt_ctf_field_get_type(field
);
1320 ok(copy_type
== expected_type
,
1321 "bt_ctf_field_copy does not copy the type (%s)", name
);
1326 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
);
1334 void field_copy_tests()
1336 struct bt_ctf_field_type
*len_type
= NULL
;
1337 struct bt_ctf_field_type
*fp_type
= NULL
;
1338 struct bt_ctf_field_type
*s_type
= NULL
;
1339 struct bt_ctf_field_type
*e_int_type
= NULL
;
1340 struct bt_ctf_field_type
*e_type
= NULL
;
1341 struct bt_ctf_field_type
*v_type
= NULL
;
1342 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1343 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1344 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1345 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1346 struct bt_ctf_field_type
*strct_type
= NULL
;
1347 struct bt_ctf_field
*len
= NULL
;
1348 struct bt_ctf_field
*fp
= NULL
;
1349 struct bt_ctf_field
*s
= NULL
;
1350 struct bt_ctf_field
*e_int
= NULL
;
1351 struct bt_ctf_field
*e
= NULL
;
1352 struct bt_ctf_field
*v
= NULL
;
1353 struct bt_ctf_field
*v_selected
= NULL
;
1354 struct bt_ctf_field
*v_selected_cur
= NULL
;
1355 struct bt_ctf_field
*v_selected_0
= NULL
;
1356 struct bt_ctf_field
*v_selected_1
= NULL
;
1357 struct bt_ctf_field
*v_selected_2
= NULL
;
1358 struct bt_ctf_field
*v_selected_3
= NULL
;
1359 struct bt_ctf_field
*v_selected_4
= NULL
;
1360 struct bt_ctf_field
*v_selected_5
= NULL
;
1361 struct bt_ctf_field
*v_selected_6
= NULL
;
1362 struct bt_ctf_field
*a
= NULL
;
1363 struct bt_ctf_field
*a_0
= NULL
;
1364 struct bt_ctf_field
*a_1
= NULL
;
1365 struct bt_ctf_field
*a_2
= NULL
;
1366 struct bt_ctf_field
*a_3
= NULL
;
1367 struct bt_ctf_field
*a_4
= NULL
;
1368 struct bt_ctf_field
*strct
= NULL
;
1369 struct bt_ctf_field
*len_copy
= NULL
;
1370 struct bt_ctf_field
*fp_copy
= NULL
;
1371 struct bt_ctf_field
*s_copy
= NULL
;
1372 struct bt_ctf_field
*e_int_copy
= NULL
;
1373 struct bt_ctf_field
*e_copy
= NULL
;
1374 struct bt_ctf_field
*v_copy
= NULL
;
1375 struct bt_ctf_field
*v_selected_copy
= NULL
;
1376 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1377 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1378 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1379 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1380 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1381 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1382 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1383 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1384 struct bt_ctf_field
*a_copy
= NULL
;
1385 struct bt_ctf_field
*a_0_copy
= NULL
;
1386 struct bt_ctf_field
*a_1_copy
= NULL
;
1387 struct bt_ctf_field
*a_2_copy
= NULL
;
1388 struct bt_ctf_field
*a_3_copy
= NULL
;
1389 struct bt_ctf_field
*a_4_copy
= NULL
;
1390 struct bt_ctf_field
*strct_copy
= NULL
;
1391 struct bt_ctf_field_type_enumeration_mapping_iterator
*e_iter
= NULL
;
1392 uint64_t uint64_t_val
;
1393 const char *str_val
;
1397 /* create len type */
1398 len_type
= bt_ctf_field_type_integer_create(32);
1401 /* create fp type */
1402 fp_type
= bt_ctf_field_type_floating_point_create();
1406 s_type
= bt_ctf_field_type_string_create();
1409 /* create e_int type */
1410 e_int_type
= bt_ctf_field_type_integer_create(8);
1414 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1416 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1419 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1423 /* create v_label1 type */
1424 v_label1_type
= bt_ctf_field_type_string_create();
1425 assert(v_label1_type
);
1427 /* create v_label1_array type */
1428 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1429 assert(v_label1_array_type
);
1431 /* create v_label2 type */
1432 v_label2_type
= bt_ctf_field_type_integer_create(16);
1433 assert(v_label2_type
);
1435 /* create v_label2_seq type */
1436 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1438 assert(v_label2_seq_type
);
1441 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1443 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1446 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1450 /* create strct type */
1451 strct_type
= bt_ctf_field_type_structure_create();
1453 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1456 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1458 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1460 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1462 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1464 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1465 v_label1_array_type
, "a");
1469 strct
= bt_ctf_field_create(strct_type
);
1473 len
= bt_ctf_field_structure_get_field(strct
, "len");
1477 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1481 s
= bt_ctf_field_structure_get_field(strct
, "s");
1485 e
= bt_ctf_field_structure_get_field(strct
, "e");
1488 /* get e_int (underlying integer) */
1489 e_int
= bt_ctf_field_enumeration_get_container(e
);
1493 v
= bt_ctf_field_structure_get_field(strct
, "v");
1497 a
= bt_ctf_field_structure_get_field(strct
, "a");
1501 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1505 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1509 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1512 /* set e field (LABEL2) */
1513 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1517 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1519 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1520 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1521 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1522 ok(v_selected_cur
== v_selected
,
1523 "bt_ctf_field_variant_get_current_field returns the current field");
1524 bt_put(v_selected_cur
);
1526 /* set selected v field */
1527 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1529 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1530 assert(v_selected_0
);
1531 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1533 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1534 assert(v_selected_1
);
1535 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1537 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1538 assert(v_selected_2
);
1539 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1541 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1542 assert(v_selected_3
);
1543 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1545 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1546 assert(v_selected_4
);
1547 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1549 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1550 assert(v_selected_5
);
1551 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1553 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1554 assert(v_selected_6
);
1555 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1559 a_0
= bt_ctf_field_array_get_field(a
, 0);
1561 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1563 a_1
= bt_ctf_field_array_get_field(a
, 1);
1565 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1567 a_2
= bt_ctf_field_array_get_field(a
, 2);
1569 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1571 a_3
= bt_ctf_field_array_get_field(a
, 3);
1573 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1575 a_4
= bt_ctf_field_array_get_field(a
, 4);
1577 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1580 /* create copy of strct */
1581 ok(!bt_ctf_field_copy(NULL
),
1582 "bt_ctf_field_copy handles NULL correctly");
1583 strct_copy
= bt_ctf_field_copy(strct
);
1585 "bt_ctf_field_copy returns a valid pointer");
1587 /* get all copied fields */
1588 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1590 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1592 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1594 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1596 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1598 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1600 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1601 assert(v_selected_copy
);
1602 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1603 assert(v_selected_0_copy
);
1604 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1605 assert(v_selected_1_copy
);
1606 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1607 assert(v_selected_2_copy
);
1608 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1609 assert(v_selected_3_copy
);
1610 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1611 assert(v_selected_4_copy
);
1612 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1613 assert(v_selected_5_copy
);
1614 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1615 assert(v_selected_6_copy
);
1616 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1617 "sequence field copy is not too large");
1618 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1620 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1622 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1624 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1626 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1628 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1630 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1631 "array field copy is not too large");
1633 /* make sure copied fields are different pointers */
1634 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1635 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1636 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1637 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1638 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1639 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1640 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1641 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1643 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1645 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1647 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1649 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1651 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1653 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1655 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1657 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1658 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1659 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1660 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1661 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1662 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1664 /* make sure copied fields share the same types */
1665 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1666 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1667 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1668 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1669 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1670 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1671 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1673 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1675 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1677 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1679 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1681 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1683 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1685 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1687 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1688 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1689 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1690 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1691 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1692 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1694 /* validate len copy */
1695 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1697 ok(uint64_t_val
== 7,
1698 "bt_ctf_field_copy creates a valid integer field copy");
1700 /* validate fp copy */
1701 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1703 ok(double_val
== 3.14,
1704 "bt_ctf_field_copy creates a valid floating point number field copy");
1706 /* validate s copy */
1707 str_val
= bt_ctf_field_string_get_value(s_copy
);
1708 ok(str_val
&& !strcmp(str_val
, "btbt"),
1709 "bt_ctf_field_copy creates a valid string field copy");
1711 /* validate e_int copy */
1712 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1715 ok(uint64_t_val
== 23,
1716 "bt_ctf_field_copy creates a valid enum's integer field copy");
1718 /* validate e copy */
1719 e_iter
= bt_ctf_field_enumeration_get_mappings(e_copy
);
1720 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_name(e_iter
,
1722 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1723 "bt_ctf_field_copy creates a valid enum field copy");
1725 /* validate v_selected copy */
1726 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1727 assert(v_selected_copy_len
);
1728 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1731 ok(uint64_t_val
== 7,
1732 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1733 bt_put(v_selected_copy_len
);
1734 v_selected_copy_len
= NULL
;
1736 /* validate v_selected copy fields */
1737 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1740 ok(uint64_t_val
== 7,
1741 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1742 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1745 ok(uint64_t_val
== 6,
1746 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1747 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1750 ok(uint64_t_val
== 5,
1751 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1752 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1755 ok(uint64_t_val
== 4,
1756 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1757 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1760 ok(uint64_t_val
== 3,
1761 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1762 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1765 ok(uint64_t_val
== 2,
1766 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1767 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1770 ok(uint64_t_val
== 1,
1771 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1773 /* validate a copy fields */
1774 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1775 ok(str_val
&& !strcmp(str_val
, "a_0"),
1776 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1777 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1778 ok(str_val
&& !strcmp(str_val
, "a_1"),
1779 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1780 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1781 ok(str_val
&& !strcmp(str_val
, "a_2"),
1782 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1783 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1784 ok(str_val
&& !strcmp(str_val
, "a_3"),
1785 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1786 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1787 ok(str_val
&& !strcmp(str_val
, "a_4"),
1788 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1790 /* put everything */
1797 bt_put(v_label1_type
);
1798 bt_put(v_label1_array_type
);
1799 bt_put(v_label2_type
);
1800 bt_put(v_label2_seq_type
);
1809 bt_put(v_selected_0
);
1810 bt_put(v_selected_1
);
1811 bt_put(v_selected_2
);
1812 bt_put(v_selected_3
);
1813 bt_put(v_selected_4
);
1814 bt_put(v_selected_5
);
1815 bt_put(v_selected_6
);
1829 bt_put(v_selected_copy
);
1830 bt_put(v_selected_0_copy
);
1831 bt_put(v_selected_1_copy
);
1832 bt_put(v_selected_2_copy
);
1833 bt_put(v_selected_3_copy
);
1834 bt_put(v_selected_4_copy
);
1835 bt_put(v_selected_5_copy
);
1836 bt_put(v_selected_6_copy
);
1848 void type_field_tests()
1850 struct bt_ctf_field
*uint_12
;
1851 struct bt_ctf_field
*int_16
;
1852 struct bt_ctf_field
*string
;
1853 struct bt_ctf_field
*enumeration
;
1854 struct bt_ctf_field_type
*composite_structure_type
;
1855 struct bt_ctf_field_type
*structure_seq_type
;
1856 struct bt_ctf_field_type
*string_type
;
1857 struct bt_ctf_field_type
*sequence_type
;
1858 struct bt_ctf_field_type
*uint_8_type
;
1859 struct bt_ctf_field_type
*int_16_type
;
1860 struct bt_ctf_field_type
*uint_12_type
=
1861 bt_ctf_field_type_integer_create(12);
1862 struct bt_ctf_field_type
*enumeration_type
;
1863 struct bt_ctf_field_type
*returned_type
;
1864 const char *ret_string
;
1866 returned_type
= bt_ctf_field_get_type(NULL
);
1867 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1869 ok(uint_12_type
, "Create an unsigned integer type");
1870 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1871 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1872 "Set integer type's base as binary");
1873 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1874 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1875 "Set integer type's base as decimal");
1876 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1877 BT_CTF_INTEGER_BASE_UNKNOWN
),
1878 "Reject integer type's base set as unknown");
1879 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1880 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1881 "Set integer type's base as octal");
1882 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1883 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1884 "Set integer type's base as hexadecimal");
1885 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1886 "Reject unknown integer base value");
1887 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1888 "Set integer type signedness to signed");
1889 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1890 "Set integer type signedness to unsigned");
1891 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1892 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1893 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1894 "bt_ctf_field_type_integer_get_size returns a correct value");
1895 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1896 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1897 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1898 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1900 ok(bt_ctf_field_type_set_byte_order(NULL
,
1901 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1902 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1903 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1904 (enum bt_ctf_byte_order
) 42) < 0,
1905 "bt_ctf_field_type_set_byte_order rejects invalid values");
1906 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1907 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1908 "Set an integer's byte order to little endian");
1909 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1910 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1911 "Set an integer's byte order to big endian");
1912 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1913 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1914 "bt_ctf_field_type_get_byte_order returns a correct value");
1915 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1916 BT_CTF_BYTE_ORDER_UNKNOWN
,
1917 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1919 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1920 BT_CTF_TYPE_ID_UNKNOWN
,
1921 "bt_ctf_field_type_get_type_id handles NULL correctly");
1922 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1923 BT_CTF_TYPE_ID_INTEGER
,
1924 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1926 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1927 BT_CTF_INTEGER_BASE_UNKNOWN
,
1928 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1929 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1930 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1931 "bt_ctf_field_type_integer_get_base returns a correct value");
1933 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1934 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1935 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1936 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1937 (enum bt_ctf_string_encoding
) 123) < 0,
1938 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1939 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1940 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1941 "Set integer type encoding to UTF8");
1942 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1943 BT_CTF_STRING_ENCODING_UNKNOWN
,
1944 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1945 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1946 BT_CTF_STRING_ENCODING_UTF8
,
1947 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1949 int_16_type
= bt_ctf_field_type_integer_create(16);
1950 assert(int_16_type
);
1951 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1952 "Set signedness of 16 bit integer to true");
1953 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1954 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1955 uint_8_type
= bt_ctf_field_type_integer_create(8);
1957 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1958 ok(sequence_type
, "Create a sequence of int16_t type");
1959 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1960 BT_CTF_TYPE_ID_SEQUENCE
,
1961 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1963 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1964 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1965 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1967 ok(!strcmp(ret_string
, "seq_len"),
1968 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1969 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1970 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1971 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1973 ok(returned_type
== int_16_type
,
1974 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1975 bt_put(returned_type
);
1977 string_type
= bt_ctf_field_type_string_create();
1978 ok(string_type
, "Create a string type");
1979 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1980 BT_CTF_STRING_ENCODING_NONE
),
1981 "Reject invalid \"None\" string encoding");
1982 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1984 "Reject invalid string encoding");
1985 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1986 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1987 "Set string encoding to ASCII");
1989 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1990 BT_CTF_STRING_ENCODING_UNKNOWN
,
1991 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1992 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1993 BT_CTF_STRING_ENCODING_ASCII
,
1994 "bt_ctf_field_type_string_get_encoding returns the correct value");
1996 structure_seq_type
= bt_ctf_field_type_structure_create();
1997 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1998 BT_CTF_TYPE_ID_STRUCT
,
1999 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
2000 ok(structure_seq_type
, "Create a structure type");
2001 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
2002 uint_8_type
, "seq_len") == 0,
2003 "Add a uint8_t type to a structure");
2004 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
2005 sequence_type
, "a_sequence") == 0,
2006 "Add a sequence type to a structure");
2008 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
2009 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
2010 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
2011 "bt_ctf_field_type_structure_get_field_count returns a correct value");
2013 ok(bt_ctf_field_type_structure_get_field(NULL
,
2014 &ret_string
, &returned_type
, 1) < 0,
2015 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2016 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2017 NULL
, &returned_type
, 1) == 0,
2018 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2019 bt_put(returned_type
);
2020 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2021 &ret_string
, NULL
, 1) == 0,
2022 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2023 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2024 &ret_string
, &returned_type
, 10) < 0,
2025 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2026 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2027 &ret_string
, &returned_type
, 1) == 0,
2028 "bt_ctf_field_type_structure_get_field returns a field");
2029 ok(!strcmp(ret_string
, "a_sequence"),
2030 "bt_ctf_field_type_structure_get_field returns a correct field name");
2031 ok(returned_type
== sequence_type
,
2032 "bt_ctf_field_type_structure_get_field returns a correct field type");
2033 bt_put(returned_type
);
2035 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2036 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2037 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2038 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2039 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2040 structure_seq_type
, "a_sequence");
2041 ok(returned_type
== sequence_type
,
2042 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2043 bt_put(returned_type
);
2045 composite_structure_type
= bt_ctf_field_type_structure_create();
2046 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2047 string_type
, "a_string") == 0,
2048 "Add a string type to a structure");
2049 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2050 structure_seq_type
, "inner_structure") == 0,
2051 "Add a structure type to a structure");
2053 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2054 NULL
, "a_sequence") == NULL
,
2055 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2056 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2057 structure_seq_type
, NULL
) == NULL
,
2058 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2059 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2060 structure_seq_type
, "a_sequence");
2061 ok(returned_type
== sequence_type
,
2062 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2063 bt_put(returned_type
);
2065 int_16
= bt_ctf_field_create(int_16_type
);
2066 ok(int_16
, "Instanciate a signed 16-bit integer");
2067 uint_12
= bt_ctf_field_create(uint_12_type
);
2068 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2069 returned_type
= bt_ctf_field_get_type(int_16
);
2070 ok(returned_type
== int_16_type
,
2071 "bt_ctf_field_get_type returns the correct type");
2073 /* Can't modify types after instanciating them */
2074 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2075 BT_CTF_INTEGER_BASE_DECIMAL
),
2076 "Check an integer type' base can't be modified after instanciation");
2077 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2078 "Check an integer type's signedness can't be modified after instanciation");
2080 /* Check signed property is checked */
2081 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2082 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2083 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2084 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2086 /* Check overflows are properly tested for */
2087 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2088 "Check -32768 is allowed for a signed 16-bit integer");
2089 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2090 "Check 32767 is allowed for a signed 16-bit integer");
2091 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2092 "Check 32768 is not allowed for a signed 16-bit integer");
2093 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2094 "Check -32769 is not allowed for a signed 16-bit integer");
2095 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2096 "Check -42 is allowed for a signed 16-bit integer");
2098 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2099 "Check 4095 is allowed for an unsigned 12-bit integer");
2100 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2101 "Check 4096 is not allowed for a unsigned 12-bit integer");
2102 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2103 "Check 0 is allowed for an unsigned 12-bit integer");
2105 string
= bt_ctf_field_create(string_type
);
2106 ok(string
, "Instanciate a string field");
2107 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2108 "Set a string's value");
2110 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2111 ok(enumeration_type
,
2112 "Create an enumeration type with an unsigned 12-bit integer as container");
2113 enumeration
= bt_ctf_field_create(enumeration_type
);
2115 "Check enumeration types are validated before instantiation");
2120 bt_put(enumeration
);
2121 bt_put(composite_structure_type
);
2122 bt_put(structure_seq_type
);
2123 bt_put(string_type
);
2124 bt_put(sequence_type
);
2125 bt_put(uint_8_type
);
2126 bt_put(int_16_type
);
2127 bt_put(uint_12_type
);
2128 bt_put(enumeration_type
);
2129 bt_put(returned_type
);
2133 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2134 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2137 * Append enough events to force the underlying packet to be resized.
2138 * Also tests that a new event can be declared after a stream has been
2139 * instantiated and used/flushed.
2143 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2145 struct bt_ctf_field_type
*integer_type
=
2146 bt_ctf_field_type_integer_create(17);
2147 struct bt_ctf_field_type
*string_type
=
2148 bt_ctf_field_type_string_create();
2149 struct bt_ctf_event
*event
= NULL
;
2150 struct bt_ctf_field
*ret_field
= NULL
;
2151 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2152 uint64_t ret_uint64
;
2153 int events_appended
= 0;
2154 struct bt_ctf_field
*packet_context
= NULL
,
2155 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
2156 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2157 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2158 struct bt_ctf_field_type
*ep_type
= NULL
;
2160 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2162 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2164 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2165 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2171 * bt_ctf_stream_class_add_event_class() copies the field types
2172 * of event_class, so we retrieve the new ones to create the
2173 * appropriate fields.
2175 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2177 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2178 ep_type
, "field_1");
2179 assert(ep_field_1_type
);
2180 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2181 ep_type
, "a_string");
2182 assert(ep_a_string_type
);
2184 event
= bt_ctf_event_create(event_class
);
2185 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2186 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2187 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2188 "bt_ctf_event_get_payload_by_index returns a correct field");
2189 bt_put(ret_field_type
);
2192 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2193 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2194 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2195 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2198 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2199 event
= bt_ctf_event_create(event_class
);
2200 struct bt_ctf_field
*integer
=
2201 bt_ctf_field_create(ep_field_1_type
);
2202 struct bt_ctf_field
*string
=
2203 bt_ctf_field_create(ep_a_string_type
);
2205 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2206 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2207 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2210 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2211 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2215 /* Populate stream event context */
2216 stream_event_context
=
2217 bt_ctf_event_get_stream_event_context(event
);
2218 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2219 "common_event_context");
2220 BT_PUT(stream_event_context
);
2221 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2225 ret
|= bt_ctf_stream_append_event(stream
, event
);
2233 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2234 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2235 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2236 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2237 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2238 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2239 ok(ret
== 0 && ret_uint64
== 0,
2240 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2241 bt_ctf_stream_append_discarded_events(stream
, 1000);
2242 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2243 ok(ret
== 0 && ret_uint64
== 1000,
2244 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2247 ok(events_appended
, "Append 100 000 events to a stream");
2250 * Populate the custom packet context field with a dummy value
2251 * otherwise flush will fail.
2253 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2254 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2255 "custom_packet_context_field");
2256 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2258 ok(bt_ctf_stream_flush(stream
) == 0,
2259 "Flush a stream that forces a packet resize");
2260 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2261 ok(ret
== 0 && ret_uint64
== 1000,
2262 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2263 bt_put(integer_type
);
2264 bt_put(string_type
);
2265 bt_put(packet_context
);
2266 bt_put(packet_context_field
);
2267 bt_put(stream_event_context
);
2268 bt_put(event_class
);
2269 bt_put(ep_field_1_type
);
2270 bt_put(ep_a_string_type
);
2275 void test_empty_stream(struct bt_ctf_writer
*writer
)
2278 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2279 struct bt_ctf_stream_class
*stream_class
= NULL
;
2280 struct bt_ctf_stream
*stream
= NULL
;
2282 trace
= bt_ctf_writer_get_trace(writer
);
2284 diag("Failed to get trace from writer");
2289 stream_class
= bt_ctf_stream_class_create("empty_stream");
2290 if (!stream_class
) {
2291 diag("Failed to create stream class");
2296 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2297 "bt_ctf_stream_class_get_trace handles NULL correctly");
2298 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2299 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2301 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2303 diag("Failed to create writer stream");
2308 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2309 ok(ret_trace
== trace
,
2310 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2313 "Created a stream class with default attributes and an empty stream");
2317 bt_put(stream_class
);
2321 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
,
2322 struct bt_ctf_clock
*clock
)
2325 struct bt_ctf_stream_class
*stream_class
= NULL
;
2326 struct bt_ctf_stream
*stream
= NULL
;
2327 struct bt_ctf_field_type
*integer_type
= NULL
,
2328 *sequence_type
= NULL
, *event_header_type
= NULL
;
2329 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2330 *event_header
= NULL
, *packet_header
= NULL
;
2331 struct bt_ctf_event_class
*event_class
= NULL
;
2332 struct bt_ctf_event
*event
= NULL
;
2334 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2335 if (!stream_class
) {
2336 fail("Failed to create stream class");
2340 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2342 fail("Failed to set stream class clock");
2347 * Customize event header to add an "seq_len" integer member
2348 * which will be used as the length of a sequence in an event of this
2351 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2353 if (!event_header_type
) {
2354 fail("Failed to get event header type");
2358 integer_type
= bt_ctf_field_type_integer_create(13);
2359 if (!integer_type
) {
2360 fail("Failed to create length integer type");
2364 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2365 integer_type
, "seq_len");
2367 fail("Failed to add a new field to stream event header");
2371 event_class
= bt_ctf_event_class_create("sequence_event");
2373 fail("Failed to create event class");
2378 * This event's payload will contain a sequence which references
2379 * stream.event.header.seq_len as its length field.
2381 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2382 "stream.event.header.seq_len");
2383 if (!sequence_type
) {
2384 fail("Failed to create a sequence");
2388 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2391 fail("Failed to add a sequence to an event class");
2395 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2397 fail("Failed to add event class to stream class");
2401 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2403 fail("Failed to create stream")
2408 * We have defined a custom packet header field. We have to populate it
2411 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2412 if (!packet_header
) {
2413 fail("Failed to get stream packet header");
2417 integer
= bt_ctf_field_structure_get_field(packet_header
,
2418 "custom_trace_packet_header_field");
2420 fail("Failed to retrieve custom_trace_packet_header_field");
2424 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2426 fail("Failed to set custom_trace_packet_header_field value");
2431 event
= bt_ctf_event_create(event_class
);
2433 fail("Failed to create event");
2437 event_header
= bt_ctf_event_get_header(event
);
2438 if (!event_header
) {
2439 fail("Failed to get event header");
2443 integer
= bt_ctf_field_structure_get_field(event_header
,
2446 fail("Failed to get seq_len field from event header");
2450 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2452 fail("Failed to set seq_len value in event header");
2456 /* Populate both sequence integer fields */
2457 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2459 fail("Failed to retrieve sequence from event");
2463 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2465 fail("Failed to set sequence length");
2470 for (i
= 0; i
< 2; i
++) {
2471 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2473 fail("Failed to retrieve sequence element");
2477 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2479 fail("Failed to set sequence element value");
2487 ret
= bt_ctf_stream_append_event(stream
, event
);
2489 fail("Failed to append event to stream");
2493 ret
= bt_ctf_stream_flush(stream
);
2495 fail("Failed to flush custom_event_header stream");
2499 bt_put(stream_class
);
2500 bt_put(event_class
);
2504 bt_put(event_header
);
2505 bt_put(packet_header
);
2506 bt_put(sequence_type
);
2507 bt_put(integer_type
);
2508 bt_put(event_header_type
);
2512 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
,
2513 struct bt_ctf_clock
*clock
)
2516 struct bt_ctf_stream_class
*stream_class
= NULL
;
2517 struct bt_ctf_stream
*stream
= NULL
,
2519 struct bt_ctf_event_class
*event_class
= NULL
;
2520 struct bt_ctf_event
*event
= NULL
;
2521 struct bt_ctf_field_type
*integer_type
= NULL
;
2522 struct bt_ctf_field
*integer
= NULL
;
2524 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2525 if (!stream_class
) {
2526 diag("Failed to create stream class");
2531 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2533 diag("Failed to set stream class clock");
2537 event_class
= bt_ctf_event_class_create("some_event_class_name");
2538 integer_type
= bt_ctf_field_type_integer_create(32);
2539 if (!integer_type
) {
2540 diag("Failed to create integer field type");
2545 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2548 diag("Failed to add field to event class");
2552 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2555 diag("Failed to add event class to stream class");
2558 event
= bt_ctf_event_create(event_class
);
2560 diag("Failed to create event");
2565 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2567 diag("Failed to get integer field payload from event");
2572 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2574 diag("Failed to set integer field value");
2578 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2580 diag("Failed to create writer stream");
2585 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2586 "bt_ctf_event_get_stream handles NULL correctly");
2587 ok(bt_ctf_event_get_stream(event
) == NULL
,
2588 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2590 ret
= bt_ctf_stream_append_event(stream
, event
);
2592 diag("Failed to append event to stream");
2596 ret_stream
= bt_ctf_event_get_stream(event
);
2597 ok(ret_stream
== stream
,
2598 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2601 "Create an event before instanciating its associated stream");
2604 bt_put(stream_class
);
2605 bt_put(event_class
);
2607 bt_put(integer_type
);
2612 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2614 struct bt_ctf_event_class
*event_class
;
2616 event_class
= bt_ctf_event_class_create("Simple Event");
2617 assert(event_class
);
2618 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2619 "two event classes with the same name cannot cohabit within the same stream class");
2620 bt_put(event_class
);
2622 event_class
= bt_ctf_event_class_create("different name, ok");
2623 assert(event_class
);
2624 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2625 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2626 "two event classes with the same ID cannot cohabit within the same stream class");
2627 bt_put(event_class
);
2631 struct bt_ctf_event_class
*create_minimal_event_class(void)
2633 struct bt_ctf_event_class
*ec
= NULL
;
2634 struct bt_ctf_field_type
*int_ft
= NULL
;
2637 int_ft
= bt_ctf_field_type_integer_create(23);
2639 ec
= bt_ctf_event_class_create("minimal");
2641 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2649 void test_create_writer_vs_non_writer_mode(void)
2652 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2653 const char *writer_stream_name
= "writer stream instance";
2654 struct bt_ctf_writer
*writer
= NULL
;
2655 struct bt_ctf_trace
*writer_trace
= NULL
;
2656 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2657 struct bt_ctf_stream
*writer_stream
= NULL
;
2658 struct bt_ctf_stream
*writer_stream2
= NULL
;
2659 struct bt_ctf_stream
*packet_stream
= NULL
;
2660 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2661 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2662 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2663 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2664 struct bt_ctf_event_class
*writer_ec
= NULL
;
2665 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2666 struct bt_ctf_event
*event
= NULL
;
2667 struct bt_ctf_event
*event2
= NULL
;
2668 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2669 struct bt_ctf_field
*int_field
= NULL
;
2670 struct bt_ctf_clock
*writer_clock
= NULL
;
2671 struct bt_ctf_clock_class
*non_writer_clock_class
= NULL
;
2672 struct bt_ctf_packet
*packet
= NULL
;
2673 struct bt_ctf_packet
*packet2
= NULL
;
2675 if (!bt_mkdtemp(trace_path
)) {
2679 /* Create empty structure field type (event header) */
2680 empty_struct_ft
= bt_ctf_field_type_structure_create();
2681 assert(empty_struct_ft
);
2683 /* Create writer, writer stream class, stream, and clock */
2684 writer
= bt_ctf_writer_create(trace_path
);
2686 writer_trace
= bt_ctf_writer_get_trace(writer
);
2687 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2688 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2690 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2693 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2695 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2696 assert(writer_stream
);
2697 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2698 "bt_ctf_stream_get_name() returns the stream's name");
2699 writer_clock
= bt_ctf_clock_create("writer_clock");
2700 assert(writer_clock
);
2701 ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
2704 /* Create non-writer trace, stream class, stream, and clock */
2705 non_writer_trace
= bt_ctf_trace_create();
2706 assert(non_writer_trace
);
2707 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2708 assert(non_writer_sc
);
2709 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2712 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2714 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2715 assert(non_writer_stream
);
2716 non_writer_clock_class
=
2717 bt_ctf_clock_class_create("non_writer_clock_class");
2718 assert(non_writer_clock_class
);
2719 ret
= bt_ctf_trace_add_clock_class(non_writer_trace
,
2720 non_writer_clock_class
);
2723 /* Create event class and event */
2724 writer_ec
= create_minimal_event_class();
2726 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2728 event
= bt_ctf_event_create(writer_ec
);
2730 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2732 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2735 * Verify non-writer stream: it should be impossible to append
2738 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2739 "bt_ctf_stream_append_event() fails with a non-writer stream");
2742 * Verify writer stream: it should be possible to append an
2745 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2746 "bt_ctf_stream_append_event() succeeds with a writer stream");
2749 * It should be possible to create a packet from a non-writer
2750 * stream, but not from a writer stream.
2752 packet
= bt_ctf_packet_create(writer_stream
);
2753 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2754 packet
= bt_ctf_packet_create(non_writer_stream
);
2755 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2756 packet_stream
= bt_ctf_packet_get_stream(packet
);
2757 ok(packet_stream
== non_writer_stream
,
2758 "bt_ctf_packet_get_stream() returns the correct stream");
2761 * It should not be possible to append an event associated to
2762 * a stream to a different stream.
2764 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2765 assert(writer_stream2
);
2766 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2767 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2770 * It should not be possible to set the packet of an event
2771 * associated to a given stream to a packet associated with
2772 * a different stream.
2774 ok(bt_ctf_event_set_packet(event
, packet
),
2775 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2778 * It should be possible to set the packet of a fresh event, as
2779 * long as the originating stream classes are the same.
2781 event2
= bt_ctf_event_create(writer_ec
);
2783 ok(bt_ctf_event_set_packet(event2
, packet
),
2784 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2785 non_writer_ec
= create_minimal_event_class();
2786 assert(non_writer_ec
);
2787 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2790 event2
= bt_ctf_event_create(non_writer_ec
);
2792 ok(!bt_ctf_event_set_packet(event2
, packet
),
2793 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2796 * It should be possible to set a packet created from the same
2797 * stream to an event with an existing packet.
2799 packet2
= bt_ctf_packet_create(non_writer_stream
);
2801 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2802 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2806 * It should not be possible to set a packet created from a
2807 * different stream to an event with an existing packet.
2809 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2810 assert(non_writer_stream2
);
2811 packet2
= bt_ctf_packet_create(non_writer_stream
);
2813 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2814 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2817 bt_put(writer_trace
);
2819 bt_put(writer_stream
);
2820 bt_put(writer_stream2
);
2821 bt_put(non_writer_trace
);
2822 bt_put(non_writer_sc
);
2823 bt_put(non_writer_stream
);
2824 bt_put(non_writer_stream2
);
2825 bt_put(packet_stream
);
2827 bt_put(non_writer_ec
);
2831 bt_put(empty_struct_ft
);
2832 bt_put(writer_clock
);
2833 bt_put(non_writer_clock_class
);
2836 recursive_rmdir(trace_path
);
2840 void test_clock_utils(void)
2843 struct bt_ctf_clock
*clock
= NULL
;
2845 clock
= bt_ctf_clock_create("water");
2847 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2849 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2851 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2853 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2859 void test_set_clock_non_writer_stream_class(void)
2861 struct bt_ctf_clock
*clock
;
2862 struct bt_ctf_trace
*trace
;
2863 struct bt_ctf_stream_class
*sc
;
2866 clock
= bt_ctf_clock_create("the_clock");
2869 trace
= bt_ctf_trace_create();
2872 sc
= bt_ctf_stream_class_create(NULL
);
2875 ret
= bt_ctf_stream_class_set_clock(sc
, clock
);
2878 ret
= bt_ctf_trace_add_stream_class(trace
, sc
);
2880 "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock");
2887 int main(int argc
, char **argv
)
2889 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2890 char metadata_path
[sizeof(trace_path
) + 9];
2891 const char *clock_name
= "test_clock";
2892 const char *clock_description
= "This is a test clock";
2893 const char *returned_clock_name
;
2894 const char *returned_clock_description
;
2895 const uint64_t frequency
= 1123456789;
2896 const int64_t offset_s
= 1351530929945824323;
2897 const int64_t offset
= 1234567;
2898 int64_t get_offset_s
,
2900 const uint64_t precision
= 10;
2901 const int is_absolute
= 0xFF;
2902 char *metadata_string
;
2903 struct bt_ctf_writer
*writer
;
2904 struct utsname name
;
2905 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2906 struct bt_ctf_clock
*clock
, *ret_clock
;
2907 struct bt_ctf_clock_class
*ret_clock_class
;
2908 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2909 struct bt_ctf_stream
*stream1
;
2910 const char *ret_string
;
2911 const unsigned char *ret_uuid
;
2912 unsigned char tmp_uuid
[16] = { 0 };
2913 struct bt_ctf_field_type
*packet_context_type
,
2914 *packet_context_field_type
,
2915 *packet_header_type
,
2916 *packet_header_field_type
,
2918 *stream_event_context_type
,
2920 *event_header_field_type
;
2921 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2922 struct bt_ctf_trace
*trace
;
2924 int64_t ret_int64_t
;
2925 struct bt_value
*obj
;
2928 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2932 plan_tests(NR_TESTS
);
2934 if (!bt_mkdtemp(trace_path
)) {
2938 strcpy(metadata_path
, trace_path
);
2939 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2941 writer
= bt_ctf_writer_create(trace_path
);
2942 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2944 ok(!bt_ctf_writer_get_trace(NULL
),
2945 "bt_ctf_writer_get_trace correctly handles NULL");
2946 trace
= bt_ctf_writer_get_trace(writer
);
2948 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2949 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2950 "Set a trace's byte order to big endian");
2951 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2952 "bt_ctf_trace_get_byte_order returns a correct endianness");
2954 /* Add environment context to the trace */
2955 ret
= gethostname(hostname
, sizeof(hostname
));
2959 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2960 "Add host (%s) environment field to writer instance",
2962 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2964 "bt_ctf_writer_add_environment_field error with NULL writer");
2965 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2967 "bt_ctf_writer_add_environment_field error with NULL field name");
2968 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2970 "bt_ctf_writer_add_environment_field error with NULL field value");
2972 /* Test bt_ctf_trace_set_environment_field with an integer object */
2973 obj
= bt_value_integer_create_init(23);
2975 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2976 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2977 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2978 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2979 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2980 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2981 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2982 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2985 /* Test bt_ctf_trace_set_environment_field with a string object */
2986 obj
= bt_value_string_create_init("the value");
2988 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2989 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2992 /* Test bt_ctf_trace_set_environment_field_integer */
2993 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2995 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2996 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2997 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2998 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3000 "bt_ctf_trace_set_environment_field_integer succeeds");
3002 /* Test bt_ctf_trace_set_environment_field_string */
3003 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
3005 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
3006 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
3007 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
3008 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3010 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
3011 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3013 "bt_ctf_trace_set_environment_field_string succeeds");
3015 /* Test bt_ctf_trace_get_environment_field_count */
3016 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
3017 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
3018 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3019 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
3021 /* Test bt_ctf_trace_get_environment_field_name */
3022 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
3023 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
3024 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
3025 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
3026 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
3027 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
3028 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
3029 ok(ret_string
&& !strcmp(ret_string
, "host"),
3030 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3031 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
3032 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
3033 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3034 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
3035 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
3036 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3037 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
3038 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
3039 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3040 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
3041 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
3042 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3044 /* Test bt_ctf_trace_get_environment_field_value */
3045 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
3046 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
3047 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
3048 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
3049 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
3050 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
3051 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
3052 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3053 ok(!ret
&& ret_int64_t
== 23,
3054 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
3056 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
3057 ret
= bt_value_string_get(obj
, &ret_string
);
3058 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
3059 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
3062 /* Test bt_ctf_trace_get_environment_field_value_by_name */
3063 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
3065 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3066 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3067 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3068 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3069 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3070 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3072 ret
= bt_value_string_get(obj
, &ret_string
);
3073 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3074 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3077 /* Test environment field replacement */
3078 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3080 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3081 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3082 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3083 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
3084 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3085 ok(!ret
&& ret_int64_t
== 654321,
3086 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3089 /* On Solaris, uname() can return any positive value on success */
3090 if (uname(&name
) < 0) {
3095 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3096 == 0, "Add sysname (%s) environment field to writer instance",
3098 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3099 name
.nodename
) == 0,
3100 "Add nodename (%s) environment field to writer instance",
3102 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3103 == 0, "Add release (%s) environment field to writer instance",
3105 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3106 == 0, "Add version (%s) environment field to writer instance",
3108 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3109 == 0, "Add machine (%s) environment field to writer istance",
3112 /* Define a clock and add it to the trace */
3113 ok(bt_ctf_clock_create("signed") == NULL
,
3114 "Illegal clock name rejected");
3115 clock
= bt_ctf_clock_create(clock_name
);
3116 ok(clock
, "Clock created sucessfully");
3117 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3118 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3119 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3120 "Returned clock name is valid");
3122 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3123 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3124 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3125 "Clock description set successfully");
3127 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3128 ok(returned_clock_description
,
3129 "bt_ctf_clock_get_description returns a description.");
3130 ok(returned_clock_description
?
3131 !strcmp(returned_clock_description
, clock_description
) : 0,
3132 "Returned clock description is valid");
3134 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3135 "bt_ctf_clock_get_frequency returns the correct default frequency");
3136 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3137 "Set clock frequency");
3138 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3139 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3141 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3142 "bt_ctf_clock_get_offset_s succeeds");
3143 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3144 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3145 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3146 "Set clock offset (seconds)");
3147 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3148 "bt_ctf_clock_get_offset_s succeeds");
3149 ok(get_offset_s
== offset_s
,
3150 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3152 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3153 "bt_ctf_clock_get_offset succeeds");
3154 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3155 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3156 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3157 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3158 "bt_ctf_clock_get_offset succeeds");
3159 ok(get_offset
== offset
,
3160 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3162 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3163 "bt_ctf_clock_get_precision returns the correct default precision");
3164 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3165 "Set clock precision");
3166 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3167 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3169 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3170 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3171 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3172 "Set clock absolute property");
3173 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3174 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3176 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3179 ok(!bt_ctf_clock_get_name(NULL
),
3180 "bt_ctf_clock_get_name correctly handles NULL");
3181 ok(!bt_ctf_clock_get_description(NULL
),
3182 "bt_ctf_clock_get_description correctly handles NULL");
3183 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3184 "bt_ctf_clock_get_frequency correctly handles NULL");
3185 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3186 "bt_ctf_clock_get_precision correctly handles NULL");
3187 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3188 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3189 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3190 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3191 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3192 "bt_ctf_clock_get_offset correctly handles NULL clock");
3193 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3194 "bt_ctf_clock_get_offset correctly handles NULL output");
3195 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3196 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3198 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3199 "bt_ctf_clock_set_description correctly handles NULL clock");
3200 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3201 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3202 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3203 "bt_ctf_clock_get_precision correctly handles NULL clock");
3204 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3205 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3206 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3207 "bt_ctf_clock_set_offset correctly handles NULL clock");
3208 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3209 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3210 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3211 "bt_ctf_clock_set_time correctly handles NULL clock");
3212 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3213 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3214 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3216 "bt_ctf_clock_get_uuid returns a UUID");
3218 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3219 /* Slightly modify UUID */
3220 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3223 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3224 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3225 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3226 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3227 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3228 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3229 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3231 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3232 ok(uuid_match(ret_uuid
, tmp_uuid
),
3233 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3235 /* Define a stream class */
3236 stream_class
= bt_ctf_stream_class_create("test_stream");
3238 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3239 "bt_ctf_stream_class_get_name handles NULL correctly");
3240 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3241 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3242 "bt_ctf_stream_class_get_name returns a correct stream class name");
3244 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3245 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3246 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3247 "bt_ctf_stream_class_get_clock handles NULL correctly");
3249 ok(stream_class
, "Create stream class");
3250 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3251 "Set a stream class' clock");
3252 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3253 ok(ret_clock
== clock
,
3254 "bt_ctf_stream_class_get_clock returns a correct clock");
3257 /* Test the event fields and event types APIs */
3260 /* Test fields copying */
3263 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3264 "bt_ctf_stream_class_get_id returns an error when no id is set");
3265 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3266 "bt_ctf_stream_class_get_id handles NULL correctly");
3267 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3268 "bt_ctf_stream_class_set_id handles NULL correctly");
3269 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3270 "Set an stream class' id");
3271 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3272 "bt_ctf_stream_class_get_id returns the correct value");
3274 /* Validate default event header fields */
3275 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3276 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3277 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3280 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3281 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_TYPE_ID_STRUCT
,
3282 "Default event header type is a structure");
3283 event_header_field_type
=
3284 bt_ctf_field_type_structure_get_field_type_by_name(
3285 ret_field_type
, "id");
3286 ok(event_header_field_type
,
3287 "Default event header type contains an \"id\" field");
3288 ok(bt_ctf_field_type_get_type_id(
3289 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3290 "Default event header \"id\" field is an integer");
3291 bt_put(event_header_field_type
);
3292 event_header_field_type
=
3293 bt_ctf_field_type_structure_get_field_type_by_name(
3294 ret_field_type
, "timestamp");
3295 ok(event_header_field_type
,
3296 "Default event header type contains a \"timestamp\" field");
3297 ok(bt_ctf_field_type_get_type_id(
3298 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3299 "Default event header \"timestamp\" field is an integer");
3300 bt_put(event_header_field_type
);
3301 bt_put(ret_field_type
);
3303 /* Add a custom trace packet header field */
3304 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3305 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3306 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3307 ok(packet_header_type
,
3308 "bt_ctf_trace_get_packet_header_type returns a packet header");
3309 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_TYPE_ID_STRUCT
,
3310 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3311 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3312 packet_header_type
, "magic");
3313 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3314 bt_put(ret_field_type
);
3315 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3316 packet_header_type
, "uuid");
3317 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3318 bt_put(ret_field_type
);
3319 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3320 packet_header_type
, "stream_id");
3321 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3322 bt_put(ret_field_type
);
3324 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3325 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3326 packet_header_field_type
, "custom_trace_packet_header_field"),
3327 "Added a custom trace packet header field successfully");
3329 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3330 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3331 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3332 "Set a trace packet_header_type successfully");
3334 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3335 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3337 /* Add a custom field to the stream class' packet context */
3338 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3339 ok(packet_context_type
,
3340 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3341 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_TYPE_ID_STRUCT
,
3342 "Packet context is a structure");
3344 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3345 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3347 integer_type
= bt_ctf_field_type_integer_create(32);
3348 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3350 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3351 /* Create a "uint5_t" equivalent custom packet context field */
3352 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3354 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3355 packet_context_field_type
, "custom_packet_context_field");
3356 ok(ret
== 0, "Packet context field added successfully");
3358 /* Define a stream event context containing a my_integer field. */
3359 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3360 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3361 ok(bt_ctf_stream_class_get_event_context_type(
3362 stream_class
) == NULL
,
3363 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3364 stream_event_context_type
= bt_ctf_field_type_structure_create();
3365 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3366 integer_type
, "common_event_context");
3368 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3369 stream_event_context_type
) < 0,
3370 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3371 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3373 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3375 ok(bt_ctf_stream_class_set_event_context_type(
3376 stream_class
, stream_event_context_type
) == 0,
3377 "Set a new stream event context type");
3378 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3380 ok(ret_field_type
== stream_event_context_type
,
3381 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3382 bt_put(ret_field_type
);
3384 /* Instantiate a stream and append events */
3385 ret
= bt_ctf_writer_add_clock(writer
, clock
);
3387 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3388 ok(stream1
, "Instanciate a stream class from writer");
3391 * Creating a stream through a writer adds the given stream
3392 * class to the writer's trace, thus registering the stream
3393 * class's clock to the trace.
3395 ok(bt_ctf_trace_get_clock_class_count(NULL
) < 0,
3396 "bt_ctf_trace_get_clock_class_count correctly handles NULL");
3397 ok(bt_ctf_trace_get_clock_class_count(trace
) == 1,
3398 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
3399 ok(!bt_ctf_trace_get_clock_class(NULL
, 0),
3400 "bt_ctf_trace_get_clock_class correctly handles NULL");
3401 ok(!bt_ctf_trace_get_clock_class(trace
, -1),
3402 "bt_ctf_trace_get_clock_class correctly handles negative indexes");
3403 ok(!bt_ctf_trace_get_clock_class(trace
, 1),
3404 "bt_ctf_trace_get_clock_class correctly handles out of bound accesses");
3405 ret_clock_class
= bt_ctf_trace_get_clock_class(trace
, 0);
3406 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3407 bt_ctf_clock_get_name(clock
)) == 0,
3408 "bt_ctf_trace_get_clock_class returns the right clock instance");
3409 bt_put(ret_clock_class
);
3410 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, NULL
),
3411 "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)");
3412 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, clock_name
),
3413 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3414 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, NULL
),
3415 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3416 ret_clock_class
= bt_ctf_trace_get_clock_class_by_name(trace
, clock_name
);
3417 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3418 bt_ctf_clock_get_name(clock
)) == 0,
3419 "bt_ctf_trace_get_clock_class returns the right clock instance");
3420 bt_put(ret_clock_class
);
3421 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, "random"),
3422 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3424 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3425 "bt_ctf_stream_get_class correctly handles NULL");
3426 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3427 ok(ret_stream_class
,
3428 "bt_ctf_stream_get_class returns a stream class");
3429 ok(ret_stream_class
== stream_class
,
3430 "Returned stream class is of the correct type");
3433 * Packet header, packet context, event header, and stream
3434 * event context types were copied for the resolving
3437 BT_PUT(packet_header_type
);
3438 BT_PUT(packet_context_type
);
3439 BT_PUT(stream_event_context_type
);
3440 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3441 assert(packet_header_type
);
3442 packet_context_type
=
3443 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3444 assert(packet_context_type
);
3445 stream_event_context_type
=
3446 bt_ctf_stream_class_get_event_context_type(stream_class
);
3447 assert(stream_event_context_type
);
3450 * Try to modify the packet context type after a stream has been
3453 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3454 packet_header_field_type
, "should_fail");
3456 "Trace packet header type can't be modified once a stream has been instanciated");
3459 * Try to modify the packet context type after a stream has been
3462 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3463 packet_context_field_type
, "should_fail");
3465 "Packet context type can't be modified once a stream has been instanciated");
3468 * Try to modify the stream event context type after a stream has been
3471 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3472 integer_type
, "should_fail");
3474 "Stream event context type can't be modified once a stream has been instanciated");
3476 /* Should fail after instanciating a stream (frozen) */
3477 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3478 "Changes to a stream class that was already instantiated fail");
3480 /* Populate the custom packet header field only once for all tests */
3481 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3482 "bt_ctf_stream_get_packet_header handles NULL correctly");
3483 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3485 "bt_ctf_stream_get_packet_header returns a packet header");
3486 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3487 ok(ret_field_type
== packet_header_type
,
3488 "Stream returns a packet header of the appropriate type");
3489 bt_put(ret_field_type
);
3490 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3491 "custom_trace_packet_header_field");
3492 ok(packet_header_field
,
3493 "Packet header structure contains a custom field with the appropriate name");
3494 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3495 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3496 "Custom packet header field is of the expected type");
3497 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3498 54321), "Set custom packet header value successfully");
3499 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3500 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3501 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3502 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3503 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3504 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3505 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3506 "Successfully set a stream's packet header");
3508 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3509 "Add environment field to writer after stream creation");
3513 test_create_writer_vs_non_writer_mode();
3515 test_set_clock_non_writer_stream_class();
3517 test_instanciate_event_before_stream(writer
, clock
);
3519 append_simple_event(stream_class
, stream1
, clock
);
3521 packet_resize_test(stream_class
, stream1
, clock
);
3523 append_complex_event(stream_class
, stream1
, clock
);
3525 append_existing_event_class(stream_class
);
3527 test_empty_stream(writer
);
3529 test_custom_event_header_stream(writer
, clock
);
3531 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3532 ok(metadata_string
, "Get metadata string");
3534 bt_ctf_writer_flush_metadata(writer
);
3535 validate_metadata(argv
[1], metadata_path
);
3536 validate_trace(argv
[2], trace_path
);
3539 bt_put(ret_stream_class
);
3542 bt_put(packet_context_type
);
3543 bt_put(packet_context_field_type
);
3544 bt_put(integer_type
);
3545 bt_put(stream_event_context_type
);
3546 bt_put(ret_field_type
);
3547 bt_put(packet_header_type
);
3548 bt_put(packet_header_field_type
);
3549 bt_put(packet_header
);
3550 bt_put(packet_header_field
);
3552 free(metadata_string
);
3553 bt_put(stream_class
);
3555 recursive_rmdir(trace_path
);