6 * Copyright 2013 - 2015 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf-writer/stream-class.h>
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ref.h>
31 #include <babeltrace/ctf/events.h>
32 #include <babeltrace/values.h>
34 #include <babeltrace/compat/stdlib.h>
36 #include <sys/utsname.h>
37 #include <babeltrace/compat/limits.h>
38 #include <babeltrace/compat/stdio.h>
43 #include <babeltrace/compat/dirent.h>
49 #define METADATA_LINE_SIZE 512
50 #define SEQUENCE_TEST_LENGTH 10
51 #define ARRAY_TEST_LENGTH 5
52 #define PACKET_RESIZE_TEST_LENGTH 100000
54 #define DEFAULT_CLOCK_FREQ 1000000000
55 #define DEFAULT_CLOCK_PRECISION 1
56 #define DEFAULT_CLOCK_OFFSET 0
57 #define DEFAULT_CLOCK_OFFSET_S 0
58 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
59 #define DEFAULT_CLOCK_TIME 0
60 #define DEFAULT_CLOCK_VALUE 0
64 static int64_t current_time
= 42;
66 /* Return 1 if uuids match, zero if different. */
68 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
73 if (!uuid_a
|| !uuid_b
) {
77 for (i
= 0; i
< 16; i
++) {
78 if (uuid_a
[i
] != uuid_b
[i
]) {
89 void validate_metadata(char *parser_path
, char *metadata_path
)
92 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
93 int parser_output_fd
= -1, metadata_fd
= -1;
100 parser_output_fd
= mkstemp(parser_output_path
);
101 metadata_fd
= open(metadata_path
, O_RDONLY
);
103 unlink(parser_output_path
);
105 if (parser_output_fd
== -1 || metadata_fd
== -1) {
106 diag("Failed create temporary files for metadata parsing.");
114 waitpid(pid
, &status
, 0);
115 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
117 /* ctf-parser-test expects a metadata string on stdin. */
118 ret
= dup2(metadata_fd
, STDIN_FILENO
);
120 perror("# dup2 metadata_fd to STDIN");
124 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
126 perror("# dup2 parser_output_fd to STDOUT");
130 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
132 perror("# dup2 parser_output_fd to STDERR");
136 execl(parser_path
, "ctf-parser-test", NULL
);
137 perror("# Could not launch the ctf metadata parser process");
141 ok(ret
== 0, "Metadata string is valid");
143 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
145 size_t len
= METADATA_LINE_SIZE
;
146 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
148 metadata_fp
= fdopen(metadata_fd
, "r");
150 perror("fdopen on metadata_fd");
155 parser_output_fp
= fdopen(parser_output_fd
, "r");
156 if (!parser_output_fp
) {
157 perror("fdopen on parser_output_fd");
160 parser_output_fd
= -1;
164 diag("malloc failure");
169 /* Output the metadata and parser output as diagnostic */
170 while (bt_getline(&line
, &len
, metadata_fp
) > 0) {
171 fprintf(stderr
, "# %s", line
);
174 rewind(parser_output_fp
);
175 while (bt_getline(&line
, &len
, parser_output_fp
) > 0) {
176 fprintf(stderr
, "# %s", line
);
182 if (fclose(metadata_fp
)) {
183 diag("fclose failure");
186 if (parser_output_fp
) {
187 if (fclose(parser_output_fp
)) {
188 diag("fclose failure");
193 if (parser_output_fd
>= 0) {
194 if (close(parser_output_fd
)) {
198 if (metadata_fd
>= 0) {
199 if (close(metadata_fd
)) {
206 void validate_trace(char *parser_path
, char *trace_path
)
209 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
210 int babeltrace_output_fd
= -1;
217 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
218 unlink(babeltrace_output_path
);
220 if (babeltrace_output_fd
== -1) {
221 diag("Failed to create a temporary file for trace parsing.");
229 waitpid(pid
, &status
, 0);
230 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
232 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
234 perror("# dup2 babeltrace_output_fd to STDOUT");
238 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
240 perror("# dup2 babeltrace_output_fd to STDERR");
244 execl(parser_path
, "babeltrace", trace_path
, NULL
);
245 perror("# Could not launch the babeltrace process");
249 ok(ret
== 0, "Babeltrace could read the resulting trace");
251 if (ret
&& babeltrace_output_fd
>= 0) {
253 size_t len
= METADATA_LINE_SIZE
;
254 FILE *babeltrace_output_fp
= NULL
;
256 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
257 if (!babeltrace_output_fp
) {
258 perror("fdopen on babeltrace_output_fd");
261 babeltrace_output_fd
= -1;
265 diag("malloc error");
267 rewind(babeltrace_output_fp
);
268 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
274 if (babeltrace_output_fp
) {
275 if (fclose(babeltrace_output_fp
)) {
276 diag("fclose error");
281 if (babeltrace_output_fd
>= 0) {
282 if (close(babeltrace_output_fd
)) {
289 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
290 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
292 /* Create and add a simple event class */
293 struct bt_ctf_event_class
*simple_event_class
=
294 bt_ctf_event_class_create("Simple Event");
295 struct bt_ctf_field_type
*uint_12_type
=
296 bt_ctf_field_type_integer_create(12);
297 struct bt_ctf_field_type
*int_64_type
=
298 bt_ctf_field_type_integer_create(64);
299 struct bt_ctf_field_type
*float_type
=
300 bt_ctf_field_type_floating_point_create();
301 struct bt_ctf_field_type
*enum_type
;
302 struct bt_ctf_field_type
*enum_type_unsigned
=
303 bt_ctf_field_type_enumeration_create(uint_12_type
);
304 struct bt_ctf_field_type
*event_context_type
=
305 bt_ctf_field_type_structure_create();
306 struct bt_ctf_field_type
*event_payload_type
= NULL
;
307 struct bt_ctf_field_type
*returned_type
;
308 struct bt_ctf_event
*simple_event
;
309 struct bt_ctf_field
*integer_field
;
310 struct bt_ctf_field
*float_field
;
311 struct bt_ctf_field
*enum_field
;
312 struct bt_ctf_field
*enum_field_unsigned
;
313 struct bt_ctf_field
*enum_container_field
;
314 const char *mapping_name_test
= "truie";
315 const double double_test_value
= 3.1415;
316 struct bt_ctf_field
*enum_container_field_unsigned
;
317 const char *mapping_name_negative_test
= "negative_value";
318 const char *ret_char
;
320 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
321 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
322 struct bt_ctf_clock
*ret_clock
;
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
;
335 ok(uint_12_type
, "Create an unsigned integer type");
337 ok(!bt_ctf_field_type_integer_set_signed(int_64_type
, 1),
338 "Set signed 64 bit integer signedness to true");
339 ok(int_64_type
, "Create a signed integer type");
340 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
342 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
343 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
344 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
345 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
346 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
347 bt_put(returned_type
);
349 bt_ctf_field_type_set_alignment(float_type
, 32);
350 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
351 "bt_ctf_field_type_get_alignment handles NULL correctly");
352 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
353 "bt_ctf_field_type_get_alignment returns a correct value");
355 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
356 "Set a floating point type's exponent digit count");
357 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
358 "Set a floating point type's mantissa digit count");
360 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
361 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
362 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
363 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
364 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
365 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
366 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
367 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
369 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
370 mapping_name_negative_test
, -12345, 0) == 0,
371 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
372 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
373 "escaping; \"test\"", 1, 1) == 0,
374 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
375 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
376 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
377 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
378 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
379 "event clock int float", 5, 22) == 0,
380 "Accept enumeration mapping strings containing reserved keywords");
381 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
383 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
384 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
385 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
386 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
387 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
388 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
389 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
391 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
392 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
393 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
394 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
395 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
396 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
398 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
399 "enum_field") == 0, "Add signed enumeration field to event");
401 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
402 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
403 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
404 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
405 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
406 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
407 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
408 NULL
, &ret_range_end_int64_t
) < 0,
409 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
410 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
411 &ret_range_start_int64_t
, NULL
) < 0,
412 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
413 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
414 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
415 "bt_ctf_field_type_enumeration_get_mapping returns a value");
416 ok(!strcmp(ret_char
, mapping_name_test
),
417 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
418 ok(ret_range_start_int64_t
== 42,
419 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
420 ok(ret_range_end_int64_t
== 42,
421 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
423 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
424 "escaping; \"test\"", 0, 0) == 0,
425 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
426 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
427 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
428 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
429 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
430 "event clock int float", 5, 22) == 0,
431 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
432 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
434 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
435 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
436 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
437 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
438 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
439 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
440 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
441 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
443 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
444 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
445 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
446 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
448 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
449 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
450 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
451 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
452 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
453 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
454 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
455 NULL
, &ret_range_end_uint64_t
) < 0,
456 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
457 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
458 &ret_range_start_uint64_t
, NULL
) < 0,
459 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
460 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
461 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
462 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
463 ok(!strcmp(ret_char
, mapping_name_test
),
464 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
465 ok(ret_range_start_uint64_t
== 42,
466 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
467 ok(ret_range_end_uint64_t
== 42,
468 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
470 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
472 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
475 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
477 /* Set an event context type which will contain a single integer*/
478 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
479 "event_specific_context"),
480 "Add event specific context field");
481 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
482 "bt_ctf_event_class_get_context_type handles NULL correctly");
483 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
484 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
486 ok(bt_ctf_event_class_set_context_type(simple_event_class
, NULL
) < 0,
487 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
488 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
489 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
490 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
491 "Set an event class' context type successfully");
492 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
493 ok(returned_type
== event_context_type
,
494 "bt_ctf_event_class_get_context_type returns the appropriate type");
495 bt_put(returned_type
);
497 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
498 "Adding simple event class to stream class");
501 * bt_ctf_stream_class_add_event_class() copies the field types
502 * of simple_event_class, so we retrieve the new ones to create
503 * the appropriate fields.
505 BT_PUT(event_context_type
);
506 BT_PUT(event_payload_type
);
507 event_payload_type
= bt_ctf_event_class_get_payload_type(
509 assert(event_payload_type
);
510 event_context_type
= bt_ctf_event_class_get_context_type(
512 assert(event_context_type
);
513 ep_integer_field_type
=
514 bt_ctf_field_type_structure_get_field_type_by_name(
515 event_payload_type
, "integer_field");
516 assert(ep_integer_field_type
);
518 bt_ctf_field_type_structure_get_field_type_by_name(
519 event_payload_type
, "enum_field");
520 assert(ep_enum_field_type
);
521 ep_enum_field_unsigned_type
=
522 bt_ctf_field_type_structure_get_field_type_by_name(
523 event_payload_type
, "enum_field_unsigned");
524 assert(ep_enum_field_unsigned_type
);
526 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
527 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
528 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
529 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
530 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
531 "bt_ctf_stream_class_get_event_class handles NULL correctly");
532 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
533 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
534 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
535 ok(ret_event_class
== simple_event_class
,
536 "bt_ctf_stream_class_get_event_class returns the correct event class");
537 bt_put(ret_event_class
);
538 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
539 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
540 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
541 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
543 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
544 ok(ret_event_class
== simple_event_class
,
545 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
546 bt_put(ret_event_class
);
548 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
549 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
550 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
551 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
552 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
553 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
554 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
555 ok(ret_event_class
== simple_event_class
,
556 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
557 bt_put(ret_event_class
);
559 simple_event
= bt_ctf_event_create(simple_event_class
);
561 "Instantiate an event containing a single integer field");
563 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
564 "bt_ctf_event_get_clock handles NULL correctly");
565 ret_clock
= bt_ctf_event_get_clock(simple_event
);
566 ok(ret_clock
== clock
,
567 "bt_ctf_event_get_clock returns a correct clock");
570 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
571 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
572 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
573 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
575 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
576 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
577 "bt_ctf_field_floating_point_get_value fails on an unset float field");
578 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
579 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
580 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
581 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
582 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
583 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
584 "bt_ctf_field_floating_point_get_value returns a double value");
585 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
586 "bt_ctf_field_floating_point_get_value returns a correct value");
588 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
590 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
591 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
592 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
593 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
594 enum_container_field
= bt_ctf_field_enumeration_get_container(
596 ok(bt_ctf_field_signed_integer_set_value(
597 enum_container_field
, -42) == 0,
598 "Set signed enumeration container value");
599 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
600 ok(!strcmp(ret_char
, mapping_name_negative_test
),
601 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
602 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
605 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
606 assert(enum_field_unsigned
);
607 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
608 enum_field_unsigned
);
609 ok(bt_ctf_field_unsigned_integer_set_value(
610 enum_container_field_unsigned
, 42) == 0,
611 "Set unsigned enumeration container value");
612 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
613 enum_field_unsigned
);
615 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
616 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
617 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
619 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
621 /* Populate stream event context */
622 stream_event_context
=
623 bt_ctf_event_get_stream_event_context(simple_event
);
624 assert(stream_event_context
);
625 stream_event_context_field
= bt_ctf_field_structure_get_field(
626 stream_event_context
, "common_event_context");
627 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
629 /* Populate the event's context */
630 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
631 "bt_ctf_event_get_event_context handles NULL correctly");
632 event_context
= bt_ctf_event_get_event_context(simple_event
);
634 "bt_ctf_event_get_event_context returns a field");
635 returned_type
= bt_ctf_field_get_type(event_context
);
636 ok(returned_type
== event_context_type
,
637 "bt_ctf_event_get_event_context returns a field of the appropriate type");
638 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
639 "event_specific_context");
640 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
641 "Successfully set an event context's value");
642 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
643 "bt_ctf_event_set_event_context handles a NULL event correctly");
644 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
645 "bt_ctf_event_set_event_context handles a NULL event context correctly");
646 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
647 "bt_ctf_event_set_event_context rejects a context of the wrong type");
648 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
649 "Set an event context successfully");
651 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
652 "Append simple event to trace stream");
654 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
655 "bt_ctf_stream_get_packet_context handles NULL correctly");
656 packet_context
= bt_ctf_stream_get_packet_context(stream
);
658 "bt_ctf_stream_get_packet_context returns a packet context");
660 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
662 ok(packet_context_field
,
663 "Packet context contains the default packet_size field.");
664 bt_put(packet_context_field
);
665 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
666 "custom_packet_context_field");
667 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
668 "Custom packet context field value successfully set.");
670 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
671 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
672 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
673 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
674 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
675 "Successfully set a stream's packet context");
677 ok(bt_ctf_stream_flush(stream
) == 0,
678 "Flush trace stream with one event");
680 bt_put(simple_event_class
);
681 bt_put(simple_event
);
682 bt_put(uint_12_type
);
686 bt_put(enum_type_unsigned
);
687 bt_put(returned_type
);
688 bt_put(event_context_type
);
689 bt_put(integer_field
);
692 bt_put(enum_field_unsigned
);
693 bt_put(enum_container_field
);
694 bt_put(enum_container_field_unsigned
);
695 bt_put(packet_context
);
696 bt_put(packet_context_field
);
697 bt_put(stream_event_context
);
698 bt_put(stream_event_context_field
);
699 bt_put(event_context
);
700 bt_put(event_context_field
);
701 bt_put(event_payload_type
);
702 bt_put(ep_integer_field_type
);
703 bt_put(ep_enum_field_type
);
704 bt_put(ep_enum_field_unsigned_type
);
708 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
709 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
711 struct event_class_attrs_counts
{
722 struct event_class_attrs_counts
;
723 const char *complex_test_event_string
= "Complex Test Event";
724 const char *test_string_1
= "Test ";
725 const char *test_string_2
= "string ";
726 const char *test_string_3
= "abcdefghi";
727 const char *test_string_4
= "abcd\0efg\0hi";
728 const char *test_string_cat
= "Test string abcdeabcd";
729 struct bt_ctf_field_type
*uint_35_type
=
730 bt_ctf_field_type_integer_create(35);
731 struct bt_ctf_field_type
*int_16_type
=
732 bt_ctf_field_type_integer_create(16);
733 struct bt_ctf_field_type
*uint_3_type
=
734 bt_ctf_field_type_integer_create(3);
735 struct bt_ctf_field_type
*enum_variant_type
=
736 bt_ctf_field_type_enumeration_create(uint_3_type
);
737 struct bt_ctf_field_type
*variant_type
=
738 bt_ctf_field_type_variant_create(enum_variant_type
,
740 struct bt_ctf_field_type
*string_type
=
741 bt_ctf_field_type_string_create();
742 struct bt_ctf_field_type
*sequence_type
;
743 struct bt_ctf_field_type
*array_type
;
744 struct bt_ctf_field_type
*inner_structure_type
=
745 bt_ctf_field_type_structure_create();
746 struct bt_ctf_field_type
*complex_structure_type
=
747 bt_ctf_field_type_structure_create();
748 struct bt_ctf_field_type
*ret_field_type
;
749 struct bt_ctf_event_class
*event_class
;
750 struct bt_ctf_event
*event
;
751 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
752 *inner_structure_field
, *complex_structure_field
,
753 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
754 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
755 *stream_event_ctx_int_field
, *ret_field
;
756 uint64_t ret_unsigned_int
;
757 int64_t ret_signed_int
;
758 const char *ret_string
;
759 struct bt_ctf_stream_class
*ret_stream_class
;
760 struct bt_ctf_event_class
*ret_event_class
;
761 struct bt_ctf_field
*packet_context
, *packet_context_field
;
762 struct bt_value
*obj
;
764 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
765 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
766 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
767 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
768 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
769 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
770 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
771 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
772 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
773 "Set alignment of signed 16 bit integer to 32");
774 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
775 "Set integer signedness to true");
776 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
777 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
778 "Set signed 16 bit integer base to hexadecimal");
780 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
781 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
784 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
785 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
786 ret_field_type
= bt_ctf_field_type_array_get_element_type(
788 ok(ret_field_type
== int_16_type
,
789 "bt_ctf_field_type_array_get_element_type returns the correct type");
790 bt_put(ret_field_type
);
792 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
793 "bt_ctf_field_type_array_get_length handles NULL correctly");
794 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
795 "bt_ctf_field_type_array_get_length returns the correct length");
797 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
798 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
799 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
800 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
801 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
802 array_type
, "an_array"), "Add an_array field to inner structure");
804 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
806 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
808 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
809 "UINT35_TYPE", 2, 7);
811 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
813 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
814 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
815 enum_variant_type
, NULL
) < 0,
816 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
817 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
818 enum_variant_type
, "INT16_TYPE") == 1,
819 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
821 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
822 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
823 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
824 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
825 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
826 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
828 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
829 "An unknown entry"), "Reject a variant field based on an unknown tag value");
830 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
831 "UINT3_TYPE") == 0, "Add a field to a variant");
832 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
833 "INT16_TYPE"), "Add INT16_TYPE field to variant");
834 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
835 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
837 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
838 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
839 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
840 ok(ret_field_type
== enum_variant_type
,
841 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
842 bt_put(ret_field_type
);
844 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
845 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
846 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
847 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
848 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
849 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
850 "INT16_TYPE") == NULL
,
851 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
852 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
854 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
855 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
856 variant_type
, "INT16_TYPE");
857 ok(ret_field_type
== int_16_type
,
858 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
859 bt_put(ret_field_type
);
861 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
862 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
863 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
864 "bt_ctf_field_type_variant_get_field_count returns the correct count");
866 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
867 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
868 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
869 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
870 bt_put(ret_field_type
);
871 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
872 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
873 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
874 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
875 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
876 "bt_ctf_field_type_variant_get_field returns a field");
877 ok(!strcmp("INT16_TYPE", ret_string
),
878 "bt_ctf_field_type_variant_get_field returns a correct field name");
879 ok(ret_field_type
== int_16_type
,
880 "bt_ctf_field_type_variant_get_field returns a correct field type");
881 bt_put(ret_field_type
);
883 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
884 enum_variant_type
, "variant_selector"),
885 "Add variant_selector field to complex structure");
886 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
887 string_type
, "a_string"), "Add a_string field to complex structure");
888 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
889 variant_type
, "variant_value"),
890 "Add variant_value field to complex structure");
891 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
892 inner_structure_type
, "inner_structure"),
893 "Add inner_structure field to complex structure");
895 ok(bt_ctf_event_class_create("clock") == NULL
,
896 "Reject creation of an event class with an illegal name");
897 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
898 ok(event_class
, "Create an event class");
899 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
900 "Reject addition of a field with an empty name to an event");
901 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
902 "Reject addition of a field with a NULL type to an event");
903 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
905 "Reject addition of a type with an illegal name to an event");
906 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
908 "Add field of type unsigned integer to an event");
909 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
910 "int_16") == 0, "Add field of type signed integer to an event");
911 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
912 "complex_structure") == 0,
913 "Add composite structure to an event");
915 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
916 "bt_ctf_event_class_get_name handles NULL correctly");
917 ret_string
= bt_ctf_event_class_get_name(event_class
);
918 ok(!strcmp(ret_string
, complex_test_event_string
),
919 "bt_ctf_event_class_get_name returns a correct name");
920 ok(bt_ctf_event_class_get_id(event_class
) < 0,
921 "bt_ctf_event_class_get_id returns a negative value when not set");
922 ok(bt_ctf_event_class_get_id(NULL
) < 0,
923 "bt_ctf_event_class_get_id handles NULL correctly");
924 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
925 "bt_ctf_event_class_set_id handles NULL correctly");
926 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
927 "Set an event class' id");
928 ok(bt_ctf_event_class_get_id(event_class
) == 42,
929 "bt_ctf_event_class_get_id returns the correct value");
931 /* Test event class attributes */
932 obj
= bt_value_integer_create_init(15);
934 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
935 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
936 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
937 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
938 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
939 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
940 assert(!bt_value_integer_set(obj
, -3));
941 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
942 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
943 assert(!bt_value_integer_set(obj
, 11));
944 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
945 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
946 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
947 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
948 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
950 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
953 obj
= bt_value_integer_create_init(5);
955 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
956 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
958 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
959 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
960 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
961 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
962 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
963 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
964 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
967 ret
= bt_value_integer_get(obj
, &int64_value
);
968 ok(obj
&& !ret
&& int64_value
== 5,
969 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
972 obj
= bt_value_string_create_init("nu name");
974 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
975 ret_string
= bt_ctf_event_class_get_name(event_class
);
976 ok(!strcmp(ret_string
, "nu name"),
977 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
978 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
979 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
981 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
983 obj
= bt_value_string_create_init("http://kernel.org/");
985 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
988 ok(bt_ctf_event_class_get_attribute_count(NULL
),
989 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
990 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
991 "bt_ctf_event_class_get_attribute_count returns the correct count");
992 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
993 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
994 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
995 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
996 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
997 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
998 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
999 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1001 memset(&attrs_count
, 0, sizeof(attrs_count
));
1003 for (i
= 0; i
< 4; ++i
) {
1004 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
1006 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
1007 assert(ret_string
&& obj
);
1009 if (!strcmp(ret_string
, "id")) {
1011 ok(bt_value_is_integer(obj
),
1012 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1014 } else if (!strcmp(ret_string
, "name")) {
1016 ok(bt_value_is_string(obj
),
1017 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1019 } else if (!strcmp(ret_string
, "loglevel")) {
1020 attrs_count
.loglevel
++;
1021 ok(bt_value_is_integer(obj
),
1022 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1024 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1025 attrs_count
.modelemfuri
++;
1026 ok(bt_value_is_string(obj
),
1027 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1030 attrs_count
.unknown
++;
1036 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1037 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1038 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1039 "event class has one instance of each known attribute");
1041 /* Add event class to the stream class */
1042 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1043 "Reject addition of NULL event class to a stream class");
1044 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1045 event_class
) == 0, "Add an event class to stream class");
1047 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1048 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1049 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1050 ok(ret_stream_class
== stream_class
,
1051 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1052 bt_put(ret_stream_class
);
1054 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1055 "bt_ctf_event_class_get_field_count handles NULL correctly");
1056 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1057 "bt_ctf_event_class_get_field_count returns a correct value");
1059 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1060 &ret_field_type
, 0) < 0,
1061 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1062 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1063 &ret_field_type
, 0) == 0,
1064 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1065 bt_put(ret_field_type
);
1066 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1068 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1069 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1070 &ret_field_type
, 42) < 0,
1071 "bt_ctf_event_class_get_field handles an invalid index correctly");
1072 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1073 &ret_field_type
, 0) == 0,
1074 "bt_ctf_event_class_get_field returns a field");
1075 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
1076 "bt_ctf_event_class_get_field returns a correct field type");
1077 bt_put(ret_field_type
);
1078 ok(!strcmp(ret_string
, "uint_35"),
1079 "bt_ctf_event_class_get_field returns a correct field name");
1080 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1081 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1082 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1083 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1084 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1085 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1086 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1087 "complex_structure");
1088 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
1089 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1090 bt_put(ret_field_type
);
1092 event
= bt_ctf_event_create(event_class
);
1093 ok(event
, "Instanciate a complex event");
1095 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1096 "bt_ctf_event_get_class handles NULL correctly");
1097 ret_event_class
= bt_ctf_event_get_class(event
);
1098 ok(ret_event_class
== event_class
,
1099 "bt_ctf_event_get_class returns the correct event class");
1100 bt_put(ret_event_class
);
1102 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1103 if (!uint_35_field
) {
1104 printf("uint_35_field is NULL\n");
1107 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1108 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1109 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1110 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1111 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1112 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1113 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1114 &ret_unsigned_int
) == 0,
1115 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1116 ok(ret_unsigned_int
== 0x0DDF00D,
1117 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1118 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1119 &ret_signed_int
) < 0,
1120 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1121 bt_put(uint_35_field
);
1123 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1124 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1125 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1126 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1127 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1128 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1129 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1130 &ret_signed_int
) == 0,
1131 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1132 ok(ret_signed_int
== -12345,
1133 "bt_ctf_field_signed_integer_get_value returns the correct value");
1134 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1135 &ret_unsigned_int
) < 0,
1136 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1137 bt_put(int_16_field
);
1139 complex_structure_field
= bt_ctf_event_get_payload(event
,
1140 "complex_structure");
1142 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1143 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1144 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1145 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1146 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1147 complex_structure_field
, 3);
1148 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1149 bt_put(inner_structure_field
);
1150 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1151 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1152 bt_put(ret_field_type
);
1154 inner_structure_field
= bt_ctf_field_structure_get_field(
1155 complex_structure_field
, "inner_structure");
1156 a_string_field
= bt_ctf_field_structure_get_field(
1157 complex_structure_field
, "a_string");
1158 enum_variant_field
= bt_ctf_field_structure_get_field(
1159 complex_structure_field
, "variant_selector");
1160 variant_field
= bt_ctf_field_structure_get_field(
1161 complex_structure_field
, "variant_value");
1162 uint_35_field
= bt_ctf_field_structure_get_field(
1163 inner_structure_field
, "seq_len");
1164 a_sequence_field
= bt_ctf_field_structure_get_field(
1165 inner_structure_field
, "a_sequence");
1166 an_array_field
= bt_ctf_field_structure_get_field(
1167 inner_structure_field
, "an_array");
1169 enum_container_field
= bt_ctf_field_enumeration_get_container(
1170 enum_variant_field
);
1171 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1172 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1173 enum_variant_field
);
1174 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1175 bt_put(int_16_field
);
1176 ok(!bt_ctf_field_string_get_value(a_string_field
),
1177 "bt_ctf_field_string_get_value returns NULL on an unset field");
1178 bt_ctf_field_string_set_value(a_string_field
,
1180 ok(!bt_ctf_field_string_get_value(NULL
),
1181 "bt_ctf_field_string_get_value correctly handles NULL");
1182 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1183 "bt_ctf_field_string_append correctly handles a NULL string field");
1184 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1185 "bt_ctf_field_string_append correctly handles a NULL string value");
1186 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1187 "bt_ctf_field_string_append succeeds");
1188 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1189 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1190 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1191 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1192 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1193 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1194 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1195 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1196 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1197 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1198 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1199 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1201 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1202 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1203 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1204 "bt_ctf_field_string_get_value returns a correct value");
1205 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1206 SEQUENCE_TEST_LENGTH
);
1208 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1209 enum_container_field
) == NULL
,
1210 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1211 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1213 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1214 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1215 variant_type
, enum_variant_field
);
1216 ok(ret_field_type
== int_16_type
,
1217 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1219 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1220 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1221 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1222 uint_35_field
) == 0, "Set a sequence field's length");
1223 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1224 ok(ret_field
== uint_35_field
,
1225 "bt_ctf_field_sequence_get_length returns the correct length field");
1226 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1227 "bt_ctf_field_sequence_get_length properly handles NULL");
1229 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1230 int_16_field
= bt_ctf_field_sequence_get_field(
1231 a_sequence_field
, i
);
1232 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1233 bt_put(int_16_field
);
1236 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1237 int_16_field
= bt_ctf_field_array_get_field(
1239 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1240 bt_put(int_16_field
);
1243 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1244 assert(stream_event_ctx_field
);
1245 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1246 stream_event_ctx_field
, "common_event_context");
1247 BT_PUT(stream_event_ctx_field
);
1248 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1249 BT_PUT(stream_event_ctx_int_field
);
1251 bt_ctf_clock_set_time(clock
, ++current_time
);
1252 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1253 "Append a complex event to a stream");
1256 * Populate the custom packet context field with a dummy value
1257 * otherwise flush will fail.
1259 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1260 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1261 "custom_packet_context_field");
1262 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1264 ok(bt_ctf_stream_flush(stream
) == 0,
1265 "Flush a stream containing a complex event");
1267 bt_put(uint_35_field
);
1268 bt_put(a_string_field
);
1269 bt_put(inner_structure_field
);
1270 bt_put(complex_structure_field
);
1271 bt_put(a_sequence_field
);
1272 bt_put(an_array_field
);
1273 bt_put(enum_variant_field
);
1274 bt_put(enum_container_field
);
1275 bt_put(variant_field
);
1277 bt_put(packet_context_field
);
1278 bt_put(packet_context
);
1279 bt_put(uint_35_type
);
1280 bt_put(int_16_type
);
1281 bt_put(string_type
);
1282 bt_put(sequence_type
);
1284 bt_put(inner_structure_type
);
1285 bt_put(complex_structure_type
);
1286 bt_put(uint_3_type
);
1287 bt_put(enum_variant_type
);
1288 bt_put(variant_type
);
1289 bt_put(ret_field_type
);
1290 bt_put(event_class
);
1295 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1296 struct bt_ctf_field_type
*expected_type
, const char *name
)
1298 struct bt_ctf_field_type
*copy_type
;
1300 copy_type
= bt_ctf_field_get_type(field
);
1301 ok(copy_type
== expected_type
,
1302 "bt_ctf_field_copy does not copy the type (%s)", name
);
1307 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1308 struct bt_ctf_field
*field_b
, const char *name
)
1310 ok(field_a
!= field_b
,
1311 "bt_ctf_field_copy creates different pointers (%s)", name
);
1315 void field_copy_tests()
1317 struct bt_ctf_field_type
*len_type
= NULL
;
1318 struct bt_ctf_field_type
*fp_type
= NULL
;
1319 struct bt_ctf_field_type
*s_type
= NULL
;
1320 struct bt_ctf_field_type
*e_int_type
= NULL
;
1321 struct bt_ctf_field_type
*e_type
= NULL
;
1322 struct bt_ctf_field_type
*v_type
= NULL
;
1323 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1324 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1325 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1326 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1327 struct bt_ctf_field_type
*strct_type
= NULL
;
1328 struct bt_ctf_field
*len
= NULL
;
1329 struct bt_ctf_field
*fp
= NULL
;
1330 struct bt_ctf_field
*s
= NULL
;
1331 struct bt_ctf_field
*e_int
= NULL
;
1332 struct bt_ctf_field
*e
= NULL
;
1333 struct bt_ctf_field
*v
= NULL
;
1334 struct bt_ctf_field
*v_selected
= NULL
;
1335 struct bt_ctf_field
*v_selected_cur
= NULL
;
1336 struct bt_ctf_field
*v_selected_0
= NULL
;
1337 struct bt_ctf_field
*v_selected_1
= NULL
;
1338 struct bt_ctf_field
*v_selected_2
= NULL
;
1339 struct bt_ctf_field
*v_selected_3
= NULL
;
1340 struct bt_ctf_field
*v_selected_4
= NULL
;
1341 struct bt_ctf_field
*v_selected_5
= NULL
;
1342 struct bt_ctf_field
*v_selected_6
= NULL
;
1343 struct bt_ctf_field
*a
= NULL
;
1344 struct bt_ctf_field
*a_0
= NULL
;
1345 struct bt_ctf_field
*a_1
= NULL
;
1346 struct bt_ctf_field
*a_2
= NULL
;
1347 struct bt_ctf_field
*a_3
= NULL
;
1348 struct bt_ctf_field
*a_4
= NULL
;
1349 struct bt_ctf_field
*strct
= NULL
;
1350 struct bt_ctf_field
*len_copy
= NULL
;
1351 struct bt_ctf_field
*fp_copy
= NULL
;
1352 struct bt_ctf_field
*s_copy
= NULL
;
1353 struct bt_ctf_field
*e_int_copy
= NULL
;
1354 struct bt_ctf_field
*e_copy
= NULL
;
1355 struct bt_ctf_field
*v_copy
= NULL
;
1356 struct bt_ctf_field
*v_selected_copy
= NULL
;
1357 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1358 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1359 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1360 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1361 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1362 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1363 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1364 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1365 struct bt_ctf_field
*a_copy
= NULL
;
1366 struct bt_ctf_field
*a_0_copy
= NULL
;
1367 struct bt_ctf_field
*a_1_copy
= NULL
;
1368 struct bt_ctf_field
*a_2_copy
= NULL
;
1369 struct bt_ctf_field
*a_3_copy
= NULL
;
1370 struct bt_ctf_field
*a_4_copy
= NULL
;
1371 struct bt_ctf_field
*strct_copy
= NULL
;
1372 uint64_t uint64_t_val
;
1373 const char *str_val
;
1377 /* create len type */
1378 len_type
= bt_ctf_field_type_integer_create(32);
1381 /* create fp type */
1382 fp_type
= bt_ctf_field_type_floating_point_create();
1386 s_type
= bt_ctf_field_type_string_create();
1389 /* create e_int type */
1390 e_int_type
= bt_ctf_field_type_integer_create(8);
1394 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1396 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1399 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1403 /* create v_label1 type */
1404 v_label1_type
= bt_ctf_field_type_string_create();
1405 assert(v_label1_type
);
1407 /* create v_label1_array type */
1408 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1409 assert(v_label1_array_type
);
1411 /* create v_label2 type */
1412 v_label2_type
= bt_ctf_field_type_integer_create(16);
1413 assert(v_label2_type
);
1415 /* create v_label2_seq type */
1416 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1418 assert(v_label2_seq_type
);
1421 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1423 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1426 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1430 /* create strct type */
1431 strct_type
= bt_ctf_field_type_structure_create();
1433 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1436 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1438 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1440 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1442 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1444 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1445 v_label1_array_type
, "a");
1449 strct
= bt_ctf_field_create(strct_type
);
1453 len
= bt_ctf_field_structure_get_field(strct
, "len");
1457 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1461 s
= bt_ctf_field_structure_get_field(strct
, "s");
1465 e
= bt_ctf_field_structure_get_field(strct
, "e");
1468 /* get e_int (underlying integer) */
1469 e_int
= bt_ctf_field_enumeration_get_container(e
);
1473 v
= bt_ctf_field_structure_get_field(strct
, "v");
1477 a
= bt_ctf_field_structure_get_field(strct
, "a");
1481 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1485 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1489 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1492 /* set e field (LABEL2) */
1493 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1497 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1499 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1500 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1501 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1502 ok(v_selected_cur
== v_selected
,
1503 "bt_ctf_field_variant_get_current_field returns the current field");
1504 bt_put(v_selected_cur
);
1506 /* set selected v field */
1507 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1509 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1510 assert(v_selected_0
);
1511 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1513 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1514 assert(v_selected_1
);
1515 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1517 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1518 assert(v_selected_2
);
1519 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1521 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1522 assert(v_selected_3
);
1523 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1525 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1526 assert(v_selected_4
);
1527 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1529 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1530 assert(v_selected_5
);
1531 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1533 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1534 assert(v_selected_6
);
1535 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1539 a_0
= bt_ctf_field_array_get_field(a
, 0);
1541 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1543 a_1
= bt_ctf_field_array_get_field(a
, 1);
1545 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1547 a_2
= bt_ctf_field_array_get_field(a
, 2);
1549 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1551 a_3
= bt_ctf_field_array_get_field(a
, 3);
1553 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1555 a_4
= bt_ctf_field_array_get_field(a
, 4);
1557 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1560 /* create copy of strct */
1561 ok(!bt_ctf_field_copy(NULL
),
1562 "bt_ctf_field_copy handles NULL correctly");
1563 strct_copy
= bt_ctf_field_copy(strct
);
1565 "bt_ctf_field_copy returns a valid pointer");
1567 /* get all copied fields */
1568 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1570 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1572 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1574 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1576 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1578 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1580 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1581 assert(v_selected_copy
);
1582 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1583 assert(v_selected_0_copy
);
1584 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1585 assert(v_selected_1_copy
);
1586 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1587 assert(v_selected_2_copy
);
1588 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1589 assert(v_selected_3_copy
);
1590 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1591 assert(v_selected_4_copy
);
1592 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1593 assert(v_selected_5_copy
);
1594 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1595 assert(v_selected_6_copy
);
1596 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1597 "sequence field copy is not too large");
1598 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1600 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1602 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1604 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1606 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1608 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1610 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1611 "array field copy is not too large");
1613 /* make sure copied fields are different pointers */
1614 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1615 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1616 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1617 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1618 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1619 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1620 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1621 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1623 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1625 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1627 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1629 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1631 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1633 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1635 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1637 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1638 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1639 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1640 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1641 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1642 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1644 /* make sure copied fields share the same types */
1645 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1646 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1647 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1648 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1649 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1650 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1651 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1653 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1655 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1657 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1659 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1661 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1663 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1665 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1667 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1668 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1669 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1670 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1671 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1672 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1674 /* validate len copy */
1675 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1677 ok(uint64_t_val
== 7,
1678 "bt_ctf_field_copy creates a valid integer field copy");
1680 /* validate fp copy */
1681 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1683 ok(double_val
== 3.14,
1684 "bt_ctf_field_copy creates a valid floating point number field copy");
1686 /* validate s copy */
1687 str_val
= bt_ctf_field_string_get_value(s_copy
);
1688 ok(str_val
&& !strcmp(str_val
, "btbt"),
1689 "bt_ctf_field_copy creates a valid string field copy");
1691 /* validate e_int copy */
1692 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1695 ok(uint64_t_val
== 23,
1696 "bt_ctf_field_copy creates a valid enum's integer field copy");
1698 /* validate e copy */
1699 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1700 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1701 "bt_ctf_field_copy creates a valid enum field copy");
1703 /* validate v_selected copy */
1704 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1705 assert(v_selected_copy_len
);
1706 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1709 ok(uint64_t_val
== 7,
1710 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1711 bt_put(v_selected_copy_len
);
1712 v_selected_copy_len
= NULL
;
1714 /* validate v_selected copy fields */
1715 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1718 ok(uint64_t_val
== 7,
1719 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1720 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1723 ok(uint64_t_val
== 6,
1724 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1725 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1728 ok(uint64_t_val
== 5,
1729 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1730 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1733 ok(uint64_t_val
== 4,
1734 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1735 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1738 ok(uint64_t_val
== 3,
1739 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1740 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1743 ok(uint64_t_val
== 2,
1744 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1745 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1748 ok(uint64_t_val
== 1,
1749 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1751 /* validate a copy fields */
1752 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1753 ok(str_val
&& !strcmp(str_val
, "a_0"),
1754 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1755 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1756 ok(str_val
&& !strcmp(str_val
, "a_1"),
1757 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1758 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1759 ok(str_val
&& !strcmp(str_val
, "a_2"),
1760 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1761 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1762 ok(str_val
&& !strcmp(str_val
, "a_3"),
1763 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1764 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1765 ok(str_val
&& !strcmp(str_val
, "a_4"),
1766 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1768 /* put everything */
1775 bt_put(v_label1_type
);
1776 bt_put(v_label1_array_type
);
1777 bt_put(v_label2_type
);
1778 bt_put(v_label2_seq_type
);
1787 bt_put(v_selected_0
);
1788 bt_put(v_selected_1
);
1789 bt_put(v_selected_2
);
1790 bt_put(v_selected_3
);
1791 bt_put(v_selected_4
);
1792 bt_put(v_selected_5
);
1793 bt_put(v_selected_6
);
1807 bt_put(v_selected_copy
);
1808 bt_put(v_selected_0_copy
);
1809 bt_put(v_selected_1_copy
);
1810 bt_put(v_selected_2_copy
);
1811 bt_put(v_selected_3_copy
);
1812 bt_put(v_selected_4_copy
);
1813 bt_put(v_selected_5_copy
);
1814 bt_put(v_selected_6_copy
);
1825 void type_field_tests()
1827 struct bt_ctf_field
*uint_12
;
1828 struct bt_ctf_field
*int_16
;
1829 struct bt_ctf_field
*string
;
1830 struct bt_ctf_field
*enumeration
;
1831 struct bt_ctf_field_type
*composite_structure_type
;
1832 struct bt_ctf_field_type
*structure_seq_type
;
1833 struct bt_ctf_field_type
*string_type
;
1834 struct bt_ctf_field_type
*sequence_type
;
1835 struct bt_ctf_field_type
*uint_8_type
;
1836 struct bt_ctf_field_type
*int_16_type
;
1837 struct bt_ctf_field_type
*uint_12_type
=
1838 bt_ctf_field_type_integer_create(12);
1839 struct bt_ctf_field_type
*enumeration_type
;
1840 struct bt_ctf_field_type
*returned_type
;
1841 const char *ret_string
;
1843 returned_type
= bt_ctf_field_get_type(NULL
);
1844 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1846 ok(uint_12_type
, "Create an unsigned integer type");
1847 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1848 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1849 "Set integer type's base as binary");
1850 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1851 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1852 "Set integer type's base as decimal");
1853 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1854 BT_CTF_INTEGER_BASE_UNKNOWN
),
1855 "Reject integer type's base set as unknown");
1856 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1857 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1858 "Set integer type's base as octal");
1859 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1860 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1861 "Set integer type's base as hexadecimal");
1862 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1863 "Reject unknown integer base value");
1864 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1865 "Set integer type signedness to signed");
1866 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1867 "Set integer type signedness to unsigned");
1868 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1869 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1870 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1871 "bt_ctf_field_type_integer_get_size returns a correct value");
1872 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1873 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1874 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1875 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1877 ok(bt_ctf_field_type_set_byte_order(NULL
,
1878 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1879 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1880 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1881 (enum bt_ctf_byte_order
) 42) < 0,
1882 "bt_ctf_field_type_set_byte_order rejects invalid values");
1883 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1884 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1885 "Set an integer's byte order to little endian");
1886 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1887 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1888 "Set an integer's byte order to big endian");
1889 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1890 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1891 "bt_ctf_field_type_get_byte_order returns a correct value");
1892 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1893 BT_CTF_BYTE_ORDER_UNKNOWN
,
1894 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1896 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1897 BT_CTF_TYPE_ID_UNKNOWN
,
1898 "bt_ctf_field_type_get_type_id handles NULL correctly");
1899 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1900 BT_CTF_TYPE_ID_INTEGER
,
1901 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1903 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1904 BT_CTF_INTEGER_BASE_UNKNOWN
,
1905 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1906 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1907 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1908 "bt_ctf_field_type_integer_get_base returns a correct value");
1910 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1911 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1912 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1913 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1914 (enum bt_ctf_string_encoding
) 123) < 0,
1915 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1916 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1917 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1918 "Set integer type encoding to UTF8");
1919 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1920 BT_CTF_STRING_ENCODING_UNKNOWN
,
1921 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1922 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1923 BT_CTF_STRING_ENCODING_UTF8
,
1924 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1926 int_16_type
= bt_ctf_field_type_integer_create(16);
1927 assert(int_16_type
);
1928 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1929 "Set signedness of 16 bit integer to true");
1930 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1931 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1932 uint_8_type
= bt_ctf_field_type_integer_create(8);
1934 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1935 ok(sequence_type
, "Create a sequence of int16_t type");
1936 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1937 BT_CTF_TYPE_ID_SEQUENCE
,
1938 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1940 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1941 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1942 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1944 ok(!strcmp(ret_string
, "seq_len"),
1945 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1946 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1947 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1948 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1950 ok(returned_type
== int_16_type
,
1951 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1952 bt_put(returned_type
);
1954 string_type
= bt_ctf_field_type_string_create();
1955 ok(string_type
, "Create a string type");
1956 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1957 BT_CTF_STRING_ENCODING_NONE
),
1958 "Reject invalid \"None\" string encoding");
1959 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1961 "Reject invalid string encoding");
1962 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1963 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1964 "Set string encoding to ASCII");
1966 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1967 BT_CTF_STRING_ENCODING_UNKNOWN
,
1968 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1969 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1970 BT_CTF_STRING_ENCODING_ASCII
,
1971 "bt_ctf_field_type_string_get_encoding returns the correct value");
1973 structure_seq_type
= bt_ctf_field_type_structure_create();
1974 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1975 BT_CTF_TYPE_ID_STRUCT
,
1976 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1977 ok(structure_seq_type
, "Create a structure type");
1978 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1979 uint_8_type
, "seq_len") == 0,
1980 "Add a uint8_t type to a structure");
1981 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1982 sequence_type
, "a_sequence") == 0,
1983 "Add a sequence type to a structure");
1985 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1986 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1987 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1988 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1990 ok(bt_ctf_field_type_structure_get_field(NULL
,
1991 &ret_string
, &returned_type
, 1) < 0,
1992 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1993 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1994 NULL
, &returned_type
, 1) == 0,
1995 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1996 bt_put(returned_type
);
1997 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1998 &ret_string
, NULL
, 1) == 0,
1999 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2000 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2001 &ret_string
, &returned_type
, 10) < 0,
2002 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2003 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
2004 &ret_string
, &returned_type
, 1) == 0,
2005 "bt_ctf_field_type_structure_get_field returns a field");
2006 ok(!strcmp(ret_string
, "a_sequence"),
2007 "bt_ctf_field_type_structure_get_field returns a correct field name");
2008 ok(returned_type
== sequence_type
,
2009 "bt_ctf_field_type_structure_get_field returns a correct field type");
2010 bt_put(returned_type
);
2012 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2013 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2014 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2015 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2016 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2017 structure_seq_type
, "a_sequence");
2018 ok(returned_type
== sequence_type
,
2019 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2020 bt_put(returned_type
);
2022 composite_structure_type
= bt_ctf_field_type_structure_create();
2023 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2024 string_type
, "a_string") == 0,
2025 "Add a string type to a structure");
2026 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2027 structure_seq_type
, "inner_structure") == 0,
2028 "Add a structure type to a structure");
2030 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2031 NULL
, "a_sequence") == NULL
,
2032 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2033 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2034 structure_seq_type
, NULL
) == NULL
,
2035 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2036 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2037 structure_seq_type
, "a_sequence");
2038 ok(returned_type
== sequence_type
,
2039 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2040 bt_put(returned_type
);
2042 int_16
= bt_ctf_field_create(int_16_type
);
2043 ok(int_16
, "Instanciate a signed 16-bit integer");
2044 uint_12
= bt_ctf_field_create(uint_12_type
);
2045 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2046 returned_type
= bt_ctf_field_get_type(int_16
);
2047 ok(returned_type
== int_16_type
,
2048 "bt_ctf_field_get_type returns the correct type");
2050 /* Can't modify types after instanciating them */
2051 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2052 BT_CTF_INTEGER_BASE_DECIMAL
),
2053 "Check an integer type' base can't be modified after instanciation");
2054 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2055 "Check an integer type's signedness can't be modified after instanciation");
2057 /* Check signed property is checked */
2058 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2059 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2060 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2061 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2063 /* Check overflows are properly tested for */
2064 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2065 "Check -32768 is allowed for a signed 16-bit integer");
2066 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2067 "Check 32767 is allowed for a signed 16-bit integer");
2068 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2069 "Check 32768 is not allowed for a signed 16-bit integer");
2070 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2071 "Check -32769 is not allowed for a signed 16-bit integer");
2072 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2073 "Check -42 is allowed for a signed 16-bit integer");
2075 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2076 "Check 4095 is allowed for an unsigned 12-bit integer");
2077 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2078 "Check 4096 is not allowed for a unsigned 12-bit integer");
2079 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2080 "Check 0 is allowed for an unsigned 12-bit integer");
2082 string
= bt_ctf_field_create(string_type
);
2083 ok(string
, "Instanciate a string field");
2084 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2085 "Set a string's value");
2087 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2088 ok(enumeration_type
,
2089 "Create an enumeration type with an unsigned 12-bit integer as container");
2090 enumeration
= bt_ctf_field_create(enumeration_type
);
2092 "Check enumeration types are validated before instantiation");
2097 bt_put(enumeration
);
2098 bt_put(composite_structure_type
);
2099 bt_put(structure_seq_type
);
2100 bt_put(string_type
);
2101 bt_put(sequence_type
);
2102 bt_put(uint_8_type
);
2103 bt_put(int_16_type
);
2104 bt_put(uint_12_type
);
2105 bt_put(enumeration_type
);
2106 bt_put(returned_type
);
2110 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2111 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2114 * Append enough events to force the underlying packet to be resized.
2115 * Also tests that a new event can be declared after a stream has been
2116 * instantiated and used/flushed.
2120 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2122 struct bt_ctf_field_type
*integer_type
=
2123 bt_ctf_field_type_integer_create(17);
2124 struct bt_ctf_field_type
*string_type
=
2125 bt_ctf_field_type_string_create();
2126 struct bt_ctf_event
*event
= NULL
;
2127 struct bt_ctf_field
*ret_field
= NULL
;
2128 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2129 uint64_t ret_uint64
;
2130 int events_appended
= 0;
2131 struct bt_ctf_field
*packet_context
= NULL
,
2132 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
2133 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2134 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2135 struct bt_ctf_field_type
*ep_type
= NULL
;
2137 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2139 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2141 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2142 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2148 * bt_ctf_stream_class_add_event_class() copies the field types
2149 * of event_class, so we retrieve the new ones to create the
2150 * appropriate fields.
2152 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2154 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2155 ep_type
, "field_1");
2156 assert(ep_field_1_type
);
2157 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2158 ep_type
, "a_string");
2159 assert(ep_a_string_type
);
2161 event
= bt_ctf_event_create(event_class
);
2162 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2163 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2164 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2165 "bt_ctf_event_get_payload_by_index returns a correct field");
2166 bt_put(ret_field_type
);
2169 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2170 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2171 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2172 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2175 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2176 event
= bt_ctf_event_create(event_class
);
2177 struct bt_ctf_field
*integer
=
2178 bt_ctf_field_create(ep_field_1_type
);
2179 struct bt_ctf_field
*string
=
2180 bt_ctf_field_create(ep_a_string_type
);
2182 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2183 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2184 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2187 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2188 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2192 /* Populate stream event context */
2193 stream_event_context
=
2194 bt_ctf_event_get_stream_event_context(event
);
2195 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2196 "common_event_context");
2197 BT_PUT(stream_event_context
);
2198 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2202 ret
|= bt_ctf_stream_append_event(stream
, event
);
2210 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2211 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2212 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2213 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2214 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2215 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2216 ok(ret
== 0 && ret_uint64
== 0,
2217 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2218 bt_ctf_stream_append_discarded_events(stream
, 1000);
2219 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2220 ok(ret
== 0 && ret_uint64
== 1000,
2221 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2224 ok(events_appended
, "Append 100 000 events to a stream");
2227 * Populate the custom packet context field with a dummy value
2228 * otherwise flush will fail.
2230 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2231 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2232 "custom_packet_context_field");
2233 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2235 ok(bt_ctf_stream_flush(stream
) == 0,
2236 "Flush a stream that forces a packet resize");
2237 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2238 ok(ret
== 0 && ret_uint64
== 1000,
2239 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2240 bt_put(integer_type
);
2241 bt_put(string_type
);
2242 bt_put(packet_context
);
2243 bt_put(packet_context_field
);
2244 bt_put(stream_event_context
);
2245 bt_put(event_class
);
2246 bt_put(ep_field_1_type
);
2247 bt_put(ep_a_string_type
);
2252 void test_empty_stream(struct bt_ctf_writer
*writer
)
2255 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2256 struct bt_ctf_stream_class
*stream_class
= NULL
;
2257 struct bt_ctf_stream
*stream
= NULL
;
2259 trace
= bt_ctf_writer_get_trace(writer
);
2261 diag("Failed to get trace from writer");
2266 stream_class
= bt_ctf_stream_class_create("empty_stream");
2267 if (!stream_class
) {
2268 diag("Failed to create stream class");
2273 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2274 "bt_ctf_stream_class_get_trace handles NULL correctly");
2275 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2276 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2278 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2280 diag("Failed to create writer stream");
2285 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2286 ok(ret_trace
== trace
,
2287 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2290 "Created a stream class with default attributes and an empty stream");
2294 bt_put(stream_class
);
2298 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2301 struct bt_ctf_trace
*trace
= NULL
;
2302 struct bt_ctf_clock
*clock
= NULL
;
2303 struct bt_ctf_stream_class
*stream_class
= NULL
;
2304 struct bt_ctf_stream
*stream
= NULL
;
2305 struct bt_ctf_field_type
*integer_type
= NULL
,
2306 *sequence_type
= NULL
, *event_header_type
= NULL
;
2307 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2308 *event_header
= NULL
, *packet_header
= NULL
;
2309 struct bt_ctf_event_class
*event_class
= NULL
;
2310 struct bt_ctf_event
*event
= NULL
;
2312 trace
= bt_ctf_writer_get_trace(writer
);
2314 fail("Failed to get trace from writer");
2318 clock
= bt_ctf_trace_get_clock(trace
, 0);
2320 fail("Failed to get clock from trace");
2324 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2325 if (!stream_class
) {
2326 fail("Failed to create stream class");
2330 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2332 fail("Failed to set stream class clock");
2337 * Customize event header to add an "seq_len" integer member
2338 * which will be used as the length of a sequence in an event of this
2341 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2343 if (!event_header_type
) {
2344 fail("Failed to get event header type");
2348 integer_type
= bt_ctf_field_type_integer_create(13);
2349 if (!integer_type
) {
2350 fail("Failed to create length integer type");
2354 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2355 integer_type
, "seq_len");
2357 fail("Failed to add a new field to stream event header");
2361 event_class
= bt_ctf_event_class_create("sequence_event");
2363 fail("Failed to create event class");
2368 * This event's payload will contain a sequence which references
2369 * stream.event.header.seq_len as its length field.
2371 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2372 "stream.event.header.seq_len");
2373 if (!sequence_type
) {
2374 fail("Failed to create a sequence");
2378 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2381 fail("Failed to add a sequence to an event class");
2385 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2387 fail("Failed to add event class to stream class");
2391 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2393 fail("Failed to create stream")
2398 * We have defined a custom packet header field. We have to populate it
2401 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2402 if (!packet_header
) {
2403 fail("Failed to get stream packet header");
2407 integer
= bt_ctf_field_structure_get_field(packet_header
,
2408 "custom_trace_packet_header_field");
2410 fail("Failed to retrieve custom_trace_packet_header_field");
2414 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2416 fail("Failed to set custom_trace_packet_header_field value");
2421 event
= bt_ctf_event_create(event_class
);
2423 fail("Failed to create event");
2427 event_header
= bt_ctf_event_get_header(event
);
2428 if (!event_header
) {
2429 fail("Failed to get event header");
2433 integer
= bt_ctf_field_structure_get_field(event_header
,
2436 fail("Failed to get seq_len field from event header");
2440 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2442 fail("Failed to set seq_len value in event header");
2446 /* Populate both sequence integer fields */
2447 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2449 fail("Failed to retrieve sequence from event");
2453 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2455 fail("Failed to set sequence length");
2460 for (i
= 0; i
< 2; i
++) {
2461 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2463 fail("Failed to retrieve sequence element");
2467 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2469 fail("Failed to set sequence element value");
2477 ret
= bt_ctf_stream_append_event(stream
, event
);
2479 fail("Failed to append event to stream");
2483 ret
= bt_ctf_stream_flush(stream
);
2485 fail("Failed to flush custom_event_header stream");
2491 bt_put(stream_class
);
2492 bt_put(event_class
);
2496 bt_put(event_header
);
2497 bt_put(packet_header
);
2498 bt_put(sequence_type
);
2499 bt_put(integer_type
);
2500 bt_put(event_header_type
);
2504 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2507 struct bt_ctf_trace
*trace
= NULL
;
2508 struct bt_ctf_clock
*clock
= NULL
;
2509 struct bt_ctf_stream_class
*stream_class
= NULL
;
2510 struct bt_ctf_stream
*stream
= NULL
,
2512 struct bt_ctf_event_class
*event_class
= NULL
;
2513 struct bt_ctf_event
*event
= NULL
;
2514 struct bt_ctf_field_type
*integer_type
= NULL
;
2515 struct bt_ctf_field
*integer
= NULL
;
2517 trace
= bt_ctf_writer_get_trace(writer
);
2519 diag("Failed to get trace from writer");
2524 clock
= bt_ctf_trace_get_clock(trace
, 0);
2526 diag("Failed to get clock from trace");
2531 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2532 if (!stream_class
) {
2533 diag("Failed to create stream class");
2538 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2540 diag("Failed to set stream class clock");
2544 event_class
= bt_ctf_event_class_create("some_event_class_name");
2545 integer_type
= bt_ctf_field_type_integer_create(32);
2546 if (!integer_type
) {
2547 diag("Failed to create integer field type");
2552 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2555 diag("Failed to add field to event class");
2559 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2562 diag("Failed to add event class to stream class");
2565 event
= bt_ctf_event_create(event_class
);
2567 diag("Failed to create event");
2572 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2574 diag("Failed to get integer field payload from event");
2579 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2581 diag("Failed to set integer field value");
2585 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2587 diag("Failed to create writer stream");
2592 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2593 "bt_ctf_event_get_stream handles NULL correctly");
2594 ok(bt_ctf_event_get_stream(event
) == NULL
,
2595 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2597 ret
= bt_ctf_stream_append_event(stream
, event
);
2599 diag("Failed to append event to stream");
2603 ret_stream
= bt_ctf_event_get_stream(event
);
2604 ok(ret_stream
== stream
,
2605 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2608 "Create an event before instanciating its associated stream");
2612 bt_put(stream_class
);
2613 bt_put(event_class
);
2615 bt_put(integer_type
);
2621 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2623 struct bt_ctf_event_class
*event_class
;
2625 event_class
= bt_ctf_event_class_create("Simple Event");
2626 assert(event_class
);
2627 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2628 "two event classes with the same name cannot cohabit within the same stream class");
2629 bt_put(event_class
);
2631 event_class
= bt_ctf_event_class_create("different name, ok");
2632 assert(event_class
);
2633 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2634 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2635 "two event classes with the same ID cannot cohabit within the same stream class");
2636 bt_put(event_class
);
2640 void test_trace_stream_class_clock(void)
2642 struct bt_ctf_trace
*trace
= NULL
;
2643 struct bt_ctf_stream_class
*sc1
= NULL
;
2644 struct bt_ctf_stream_class
*sc2
= NULL
;
2645 struct bt_ctf_clock
*sc1_clock
= NULL
;
2646 struct bt_ctf_clock
*sc2_clock
= NULL
;
2647 const char *clock_name
= "hello";
2649 trace
= bt_ctf_trace_create();
2651 sc1
= bt_ctf_stream_class_create(NULL
);
2653 sc2
= bt_ctf_stream_class_create(NULL
);
2655 sc1_clock
= bt_ctf_clock_create(clock_name
);
2657 sc2_clock
= bt_ctf_clock_create(clock_name
);
2660 ok(!bt_ctf_stream_class_set_clock(sc1
, sc1_clock
),
2661 "bt_ctf_stream_class_set_clock() succeeds for sc1");
2662 ok(!bt_ctf_stream_class_set_clock(sc2
, sc2_clock
),
2663 "bt_ctf_stream_class_set_clock() succeeds for sc2");
2664 ok(!bt_ctf_trace_add_stream_class(trace
, sc1
),
2665 "bt_ctf_trace_add_stream_class() succeeds with sc1");
2666 ok(bt_ctf_trace_add_stream_class(trace
, sc2
),
2667 "bt_ctf_trace_add_stream_class() fails with sc2 (different clock, same name)");
2677 struct bt_ctf_event_class
*create_minimal_event_class(void)
2679 struct bt_ctf_event_class
*ec
= NULL
;
2680 struct bt_ctf_field_type
*int_ft
= NULL
;
2683 int_ft
= bt_ctf_field_type_integer_create(23);
2685 ec
= bt_ctf_event_class_create("minimal");
2687 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2695 void test_create_writer_vs_non_writer_mode(void)
2698 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2699 const char *writer_stream_name
= "writer stream instance";
2700 struct bt_ctf_writer
*writer
= NULL
;
2701 struct bt_ctf_trace
*writer_trace
= NULL
;
2702 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2703 struct bt_ctf_stream
*writer_stream
= NULL
;
2704 struct bt_ctf_stream
*writer_stream2
= NULL
;
2705 struct bt_ctf_stream
*packet_stream
= NULL
;
2706 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2707 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2708 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2709 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2710 struct bt_ctf_event_class
*writer_ec
= NULL
;
2711 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2712 struct bt_ctf_event
*event
= NULL
;
2713 struct bt_ctf_event
*event2
= NULL
;
2714 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2715 struct bt_ctf_field
*int_field
= NULL
;
2716 struct bt_ctf_clock
*writer_clock
= NULL
;
2717 struct bt_ctf_clock
*non_writer_clock
= NULL
;
2718 struct bt_ctf_packet
*packet
= NULL
;
2719 struct bt_ctf_packet
*packet2
= NULL
;
2721 if (!bt_mkdtemp(trace_path
)) {
2725 /* Create empty structure field type (event header) */
2726 empty_struct_ft
= bt_ctf_field_type_structure_create();
2727 assert(empty_struct_ft
);
2729 /* Create writer, writer stream class, stream, and clock */
2730 writer
= bt_ctf_writer_create(trace_path
);
2732 writer_trace
= bt_ctf_writer_get_trace(writer
);
2733 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2734 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2736 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2739 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2741 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2742 assert(writer_stream
);
2743 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2744 "bt_ctf_stream_get_name() returns the stream's name");
2745 writer_clock
= bt_ctf_clock_create("writer_clock");
2746 assert(writer_clock
);
2747 ret
= bt_ctf_trace_add_clock(writer_trace
, writer_clock
);
2750 /* Create non-writer trace, stream class, stream, and clock */
2751 non_writer_trace
= bt_ctf_trace_create();
2752 assert(non_writer_trace
);
2753 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2754 assert(non_writer_sc
);
2755 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2758 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2760 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2761 assert(non_writer_stream
);
2762 non_writer_clock
= bt_ctf_clock_create("non_writer_clock");
2763 assert(non_writer_clock
);
2764 ret
= bt_ctf_trace_add_clock(non_writer_trace
, non_writer_clock
);
2767 /* Create event class and event */
2768 writer_ec
= create_minimal_event_class();
2770 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2772 event
= bt_ctf_event_create(writer_ec
);
2774 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2776 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2779 * Verify non-writer stream: it should be impossible to append
2782 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2783 "bt_ctf_stream_append_event() fails with a non-writer stream");
2786 * Verify writer stream: it should be possible to append an
2789 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2790 "bt_ctf_stream_append_event() succeeds with a writer stream");
2793 * Verify that it's possible to get and set the value of a
2794 * writer mode clock.
2796 ok (!bt_ctf_clock_set_value(writer_clock
, 1000),
2797 "bt_ctf_clock_set_value() succeeds with a writer mode clock");
2798 ok (bt_ctf_clock_get_value(writer_clock
) == 1000,
2799 "bt_ctf_clock_get_value() succeeds with a writer mode clock");
2802 * Verify that it's impossible to get and set the value of a
2803 * non-writer mode clock.
2805 ok (bt_ctf_clock_set_value(non_writer_clock
, 1000),
2806 "bt_ctf_clock_set_value() fails with a non-writer mode clock");
2807 ok (bt_ctf_clock_get_value(non_writer_clock
) == -1ULL,
2808 "bt_ctf_clock_get_value() fails with a non-writer mode clock");
2811 * It should be possible to create a packet from a non-writer
2812 * stream, but not from a writer stream.
2814 packet
= bt_ctf_packet_create(writer_stream
);
2815 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2816 packet
= bt_ctf_packet_create(non_writer_stream
);
2817 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2818 packet_stream
= bt_ctf_packet_get_stream(packet
);
2819 ok(packet_stream
== non_writer_stream
,
2820 "bt_ctf_packet_get_stream() returns the correct stream");
2823 * It should not be possible to append an event associated to
2824 * a stream to a different stream.
2826 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2827 assert(writer_stream2
);
2828 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2829 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2832 * It should not be possible to set the packet of an event
2833 * associated to a given stream to a packet associated with
2834 * a different stream.
2836 ok(bt_ctf_event_set_packet(event
, packet
),
2837 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2840 * It should be possible to set the packet of a fresh event, as
2841 * long as the originating stream classes are the same.
2843 event2
= bt_ctf_event_create(writer_ec
);
2845 ok(bt_ctf_event_set_packet(event2
, packet
),
2846 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2847 non_writer_ec
= create_minimal_event_class();
2848 assert(non_writer_ec
);
2849 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2852 event2
= bt_ctf_event_create(non_writer_ec
);
2854 ok(!bt_ctf_event_set_packet(event2
, packet
),
2855 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2858 * It should be possible to set a packet created from the same
2859 * stream to an event with an existing packet.
2861 packet2
= bt_ctf_packet_create(non_writer_stream
);
2863 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2864 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2868 * It should not be possible to set a packet created from a
2869 * different stream to an event with an existing packet.
2871 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2872 assert(non_writer_stream2
);
2873 packet2
= bt_ctf_packet_create(non_writer_stream
);
2875 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2876 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2879 bt_put(writer_trace
);
2881 bt_put(writer_stream
);
2882 bt_put(writer_stream2
);
2883 bt_put(non_writer_trace
);
2884 bt_put(non_writer_sc
);
2885 bt_put(non_writer_stream
);
2886 bt_put(non_writer_stream2
);
2887 bt_put(packet_stream
);
2889 bt_put(non_writer_ec
);
2893 bt_put(empty_struct_ft
);
2894 bt_put(writer_clock
);
2895 bt_put(non_writer_clock
);
2898 recursive_rmdir(trace_path
);
2902 void test_clock_utils(void)
2905 struct bt_ctf_clock
*clock
= NULL
;
2907 clock
= bt_ctf_clock_create("water");
2909 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2911 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2913 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2915 ok(bt_ctf_clock_ns_from_value(clock
, 4321) == 1234000005321ULL,
2916 "bt_ctf_clock_ns_from_value() returns the correct value with a 1 GHz frequency");
2917 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2919 ok(bt_ctf_clock_ns_from_value(clock
, 4321) ==
2920 (uint64_t) 1237468709256.845,
2921 "bt_ctf_clock_ns_from_value() returns the correct value with a non-1 GHz frequency");
2926 int main(int argc
, char **argv
)
2928 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2929 char metadata_path
[sizeof(trace_path
) + 9];
2930 const char *clock_name
= "test_clock";
2931 const char *clock_description
= "This is a test clock";
2932 const char *returned_clock_name
;
2933 const char *returned_clock_description
;
2934 const uint64_t frequency
= 1123456789;
2935 const int64_t offset_s
= 1351530929945824323;
2936 const int64_t offset
= 1234567;
2937 int64_t get_offset_s
,
2940 const uint64_t precision
= 10;
2941 const int is_absolute
= 0xFF;
2942 char *metadata_string
;
2943 struct bt_ctf_writer
*writer
;
2944 struct utsname name
;
2945 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2946 struct bt_ctf_clock
*clock
, *ret_clock
;
2947 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2948 struct bt_ctf_stream
*stream1
;
2949 const char *ret_string
;
2950 const unsigned char *ret_uuid
;
2951 unsigned char tmp_uuid
[16] = { 0 };
2952 struct bt_ctf_field_type
*packet_context_type
,
2953 *packet_context_field_type
,
2954 *packet_header_type
,
2955 *packet_header_field_type
,
2957 *stream_event_context_type
,
2959 *event_header_field_type
;
2960 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2961 struct bt_ctf_trace
*trace
;
2963 int64_t ret_int64_t
;
2964 struct bt_value
*obj
;
2967 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2971 plan_tests(NR_TESTS
);
2973 if (!bt_mkdtemp(trace_path
)) {
2977 strcpy(metadata_path
, trace_path
);
2978 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2980 writer
= bt_ctf_writer_create(trace_path
);
2981 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2983 ok(!bt_ctf_writer_get_trace(NULL
),
2984 "bt_ctf_writer_get_trace correctly handles NULL");
2985 trace
= bt_ctf_writer_get_trace(writer
);
2987 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2988 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2989 "Set a trace's byte order to big endian");
2990 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2991 "bt_ctf_trace_get_byte_order returns a correct endianness");
2993 /* Add environment context to the trace */
2994 ret
= gethostname(hostname
, sizeof(hostname
));
2998 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2999 "Add host (%s) environment field to writer instance",
3001 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
3003 "bt_ctf_writer_add_environment_field error with NULL writer");
3004 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
3006 "bt_ctf_writer_add_environment_field error with NULL field name");
3007 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
3009 "bt_ctf_writer_add_environment_field error with NULL field value");
3011 /* Test bt_ctf_trace_set_environment_field with an integer object */
3012 obj
= bt_value_integer_create_init(23);
3014 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
3015 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
3016 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
3017 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
3018 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
3019 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
3020 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
3021 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
3024 /* Test bt_ctf_trace_set_environment_field with a string object */
3025 obj
= bt_value_string_create_init("the value");
3027 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
3028 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
3031 /* Test bt_ctf_trace_set_environment_field_integer */
3032 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
3034 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
3035 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
3036 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
3037 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3039 "bt_ctf_trace_set_environment_field_integer succeeds");
3041 /* Test bt_ctf_trace_set_environment_field_string */
3042 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
3044 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
3045 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
3046 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
3047 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3049 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
3050 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3052 "bt_ctf_trace_set_environment_field_string succeeds");
3054 /* Test bt_ctf_trace_get_environment_field_count */
3055 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
3056 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
3057 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3058 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
3060 /* Test bt_ctf_trace_get_environment_field_name */
3061 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
3062 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
3063 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
3064 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
3065 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
3066 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
3067 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
3068 ok(ret_string
&& !strcmp(ret_string
, "host"),
3069 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3070 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
3071 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
3072 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3073 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
3074 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
3075 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3076 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
3077 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
3078 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3079 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
3080 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
3081 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3083 /* Test bt_ctf_trace_get_environment_field_value */
3084 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
3085 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
3086 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
3087 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
3088 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
3089 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
3090 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
3091 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3092 ok(!ret
&& ret_int64_t
== 23,
3093 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
3095 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
3096 ret
= bt_value_string_get(obj
, &ret_string
);
3097 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
3098 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
3101 /* Test bt_ctf_trace_get_environment_field_value_by_name */
3102 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
3104 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3105 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3106 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3107 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3108 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3109 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3111 ret
= bt_value_string_get(obj
, &ret_string
);
3112 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3113 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3116 /* Test environment field replacement */
3117 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3119 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3120 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3121 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3122 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
3123 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3124 ok(!ret
&& ret_int64_t
== 654321,
3125 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3128 /* On Solaris, uname() can return any positive value on success */
3129 if (uname(&name
) < 0) {
3134 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3135 == 0, "Add sysname (%s) environment field to writer instance",
3137 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3138 name
.nodename
) == 0,
3139 "Add nodename (%s) environment field to writer instance",
3141 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3142 == 0, "Add release (%s) environment field to writer instance",
3144 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3145 == 0, "Add version (%s) environment field to writer instance",
3147 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3148 == 0, "Add machine (%s) environment field to writer istance",
3151 /* Define a clock and add it to the trace */
3152 ok(bt_ctf_clock_create("signed") == NULL
,
3153 "Illegal clock name rejected");
3154 clock
= bt_ctf_clock_create(clock_name
);
3155 ok(clock
, "Clock created sucessfully");
3156 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3157 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3158 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3159 "Returned clock name is valid");
3161 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3162 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3163 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3164 "Clock description set successfully");
3166 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3167 ok(returned_clock_description
,
3168 "bt_ctf_clock_get_description returns a description.");
3169 ok(returned_clock_description
?
3170 !strcmp(returned_clock_description
, clock_description
) : 0,
3171 "Returned clock description is valid");
3173 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3174 "bt_ctf_clock_get_frequency returns the correct default frequency");
3175 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3176 "Set clock frequency");
3177 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3178 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3180 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3181 "bt_ctf_clock_get_offset_s succeeds");
3182 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3183 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3184 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3185 "Set clock offset (seconds)");
3186 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3187 "bt_ctf_clock_get_offset_s succeeds");
3188 ok(get_offset_s
== offset_s
,
3189 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3191 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3192 "bt_ctf_clock_get_offset succeeds");
3193 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3194 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3195 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3196 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3197 "bt_ctf_clock_get_offset succeeds");
3198 ok(get_offset
== offset
,
3199 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3201 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3202 "bt_ctf_clock_get_precision returns the correct default precision");
3203 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3204 "Set clock precision");
3205 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3206 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3208 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3209 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3210 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3211 "Set clock absolute property");
3212 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3213 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3215 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3216 "bt_ctf_clock_get_time succeeds");
3217 ok(get_time
== DEFAULT_CLOCK_TIME
,
3218 "bt_ctf_clock_get_time returns the correct default time");
3219 ok(bt_ctf_clock_get_value(clock
) == DEFAULT_CLOCK_VALUE
,
3220 "bt_ctf_clock_get_value returns the correct default value");
3221 ok(bt_ctf_clock_set_value(clock
, current_time
) == 0,
3223 ok(bt_ctf_clock_get_value(clock
) == current_time
,
3224 "bt_ctf_clock_get_value returns the correct value once it is set");
3225 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3227 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3228 "bt_ctf_clock_get_time succeeds");
3229 ok(get_time
>= current_time
- 1 && get_time
<= current_time
+ 1,
3230 "bt_ctf_clock_get_time returns the correct time once it is set");
3232 ok(!bt_ctf_clock_get_name(NULL
),
3233 "bt_ctf_clock_get_name correctly handles NULL");
3234 ok(!bt_ctf_clock_get_description(NULL
),
3235 "bt_ctf_clock_get_description correctly handles NULL");
3236 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3237 "bt_ctf_clock_get_frequency correctly handles NULL");
3238 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3239 "bt_ctf_clock_get_precision correctly handles NULL");
3240 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3241 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3242 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3243 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3244 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3245 "bt_ctf_clock_get_offset correctly handles NULL clock");
3246 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3247 "bt_ctf_clock_get_offset correctly handles NULL output");
3248 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3249 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3250 ok(bt_ctf_clock_get_time(NULL
, &get_time
) < 0,
3251 "bt_ctf_clock_get_time correctly handles NULL clock");
3252 ok(bt_ctf_clock_get_time(clock
, NULL
) < 0,
3253 "bt_ctf_clock_get_time correctly handles NULL output");
3255 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3256 "bt_ctf_clock_set_description correctly handles NULL clock");
3257 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3258 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3259 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3260 "bt_ctf_clock_get_precision correctly handles NULL clock");
3261 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3262 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3263 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3264 "bt_ctf_clock_set_offset correctly handles NULL clock");
3265 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3266 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3267 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3268 "bt_ctf_clock_set_time correctly handles NULL clock");
3269 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3270 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3271 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3273 "bt_ctf_clock_get_uuid returns a UUID");
3275 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3276 /* Slightly modify UUID */
3277 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3280 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3281 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3282 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3283 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3284 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3285 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3286 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3288 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3289 ok(uuid_match(ret_uuid
, tmp_uuid
),
3290 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3292 /* Define a stream class */
3293 stream_class
= bt_ctf_stream_class_create("test_stream");
3295 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3296 "bt_ctf_stream_class_get_name handles NULL correctly");
3297 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3298 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3299 "bt_ctf_stream_class_get_name returns a correct stream class name");
3301 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3302 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3303 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3304 "bt_ctf_stream_class_get_clock handles NULL correctly");
3306 ok(stream_class
, "Create stream class");
3307 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3308 "Set a stream class' clock");
3309 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3310 ok(ret_clock
== clock
,
3311 "bt_ctf_stream_class_get_clock returns a correct clock");
3314 /* Test the event fields and event types APIs */
3317 /* Test fields copying */
3320 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3321 "bt_ctf_stream_class_get_id returns an error when no id is set");
3322 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3323 "bt_ctf_stream_class_get_id handles NULL correctly");
3324 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3325 "bt_ctf_stream_class_set_id handles NULL correctly");
3326 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3327 "Set an stream class' id");
3328 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3329 "bt_ctf_stream_class_get_id returns the correct value");
3331 /* Validate default event header fields */
3332 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3333 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3334 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3337 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3338 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_TYPE_ID_STRUCT
,
3339 "Default event header type is a structure");
3340 event_header_field_type
=
3341 bt_ctf_field_type_structure_get_field_type_by_name(
3342 ret_field_type
, "id");
3343 ok(event_header_field_type
,
3344 "Default event header type contains an \"id\" field");
3345 ok(bt_ctf_field_type_get_type_id(
3346 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3347 "Default event header \"id\" field is an integer");
3348 bt_put(event_header_field_type
);
3349 event_header_field_type
=
3350 bt_ctf_field_type_structure_get_field_type_by_name(
3351 ret_field_type
, "timestamp");
3352 ok(event_header_field_type
,
3353 "Default event header type contains a \"timestamp\" field");
3354 ok(bt_ctf_field_type_get_type_id(
3355 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3356 "Default event header \"timestamp\" field is an integer");
3357 bt_put(event_header_field_type
);
3358 bt_put(ret_field_type
);
3360 /* Add a custom trace packet header field */
3361 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3362 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3363 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3364 ok(packet_header_type
,
3365 "bt_ctf_trace_get_packet_header_type returns a packet header");
3366 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_TYPE_ID_STRUCT
,
3367 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3368 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3369 packet_header_type
, "magic");
3370 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3371 bt_put(ret_field_type
);
3372 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3373 packet_header_type
, "uuid");
3374 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3375 bt_put(ret_field_type
);
3376 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3377 packet_header_type
, "stream_id");
3378 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3379 bt_put(ret_field_type
);
3381 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3382 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3383 packet_header_field_type
, "custom_trace_packet_header_field"),
3384 "Added a custom trace packet header field successfully");
3386 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3387 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3388 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3389 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3390 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3391 "Set a trace packet_header_type successfully");
3393 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3394 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3396 /* Add a custom field to the stream class' packet context */
3397 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3398 ok(packet_context_type
,
3399 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3400 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_TYPE_ID_STRUCT
,
3401 "Packet context is a structure");
3403 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3404 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3405 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3406 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3408 integer_type
= bt_ctf_field_type_integer_create(32);
3409 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3411 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3412 /* Create a "uint5_t" equivalent custom packet context field */
3413 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3415 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3416 packet_context_field_type
, "custom_packet_context_field");
3417 ok(ret
== 0, "Packet context field added successfully");
3419 /* Define a stream event context containing a my_integer field. */
3420 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3421 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3422 ok(bt_ctf_stream_class_get_event_context_type(
3423 stream_class
) == NULL
,
3424 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3425 stream_event_context_type
= bt_ctf_field_type_structure_create();
3426 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3427 integer_type
, "common_event_context");
3429 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3430 stream_event_context_type
) < 0,
3431 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3432 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3434 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3435 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3437 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3439 ok(bt_ctf_stream_class_set_event_context_type(
3440 stream_class
, stream_event_context_type
) == 0,
3441 "Set a new stream event context type");
3442 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3444 ok(ret_field_type
== stream_event_context_type
,
3445 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3446 bt_put(ret_field_type
);
3448 /* Instantiate a stream and append events */
3449 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3450 ok(stream1
, "Instanciate a stream class from writer");
3453 * Creating a stream through a writer adds the given stream
3454 * class to the writer's trace, thus registering the stream
3455 * class's clock to the trace.
3457 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
3458 "bt_ctf_trace_get_clock_count correctly handles NULL");
3459 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
3460 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3461 ok(!bt_ctf_trace_get_clock(NULL
, 0),
3462 "bt_ctf_trace_get_clock correctly handles NULL");
3463 ok(!bt_ctf_trace_get_clock(trace
, -1),
3464 "bt_ctf_trace_get_clock correctly handles negative indexes");
3465 ok(!bt_ctf_trace_get_clock(trace
, 1),
3466 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3467 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
3468 ok(ret_clock
== clock
,
3469 "bt_ctf_trace_get_clock returns the right clock instance");
3471 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
3472 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3473 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
3474 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3475 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
3476 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3477 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
3478 ok(ret_clock
== clock
,
3479 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3481 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
3482 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3484 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3485 "bt_ctf_stream_get_class correctly handles NULL");
3486 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3487 ok(ret_stream_class
,
3488 "bt_ctf_stream_get_class returns a stream class");
3489 ok(ret_stream_class
== stream_class
,
3490 "Returned stream class is of the correct type");
3493 * Packet header, packet context, event header, and stream
3494 * event context types were copied for the resolving
3497 BT_PUT(packet_header_type
);
3498 BT_PUT(packet_context_type
);
3499 BT_PUT(stream_event_context_type
);
3500 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3501 assert(packet_header_type
);
3502 packet_context_type
=
3503 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3504 assert(packet_context_type
);
3505 stream_event_context_type
=
3506 bt_ctf_stream_class_get_event_context_type(stream_class
);
3507 assert(stream_event_context_type
);
3510 * Try to modify the packet context type after a stream has been
3513 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3514 packet_header_field_type
, "should_fail");
3516 "Trace packet header type can't be modified once a stream has been instanciated");
3519 * Try to modify the packet context type after a stream has been
3522 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3523 packet_context_field_type
, "should_fail");
3525 "Packet context type can't be modified once a stream has been instanciated");
3528 * Try to modify the stream event context type after a stream has been
3531 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3532 integer_type
, "should_fail");
3534 "Stream event context type can't be modified once a stream has been instanciated");
3536 /* Should fail after instanciating a stream (frozen) */
3537 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3538 "Changes to a stream class that was already instantiated fail");
3540 /* Populate the custom packet header field only once for all tests */
3541 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3542 "bt_ctf_stream_get_packet_header handles NULL correctly");
3543 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3545 "bt_ctf_stream_get_packet_header returns a packet header");
3546 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3547 ok(ret_field_type
== packet_header_type
,
3548 "Stream returns a packet header of the appropriate type");
3549 bt_put(ret_field_type
);
3550 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3551 "custom_trace_packet_header_field");
3552 ok(packet_header_field
,
3553 "Packet header structure contains a custom field with the appropriate name");
3554 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3555 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3556 "Custom packet header field is of the expected type");
3557 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3558 54321), "Set custom packet header value successfully");
3559 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3560 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3561 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3562 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3563 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3564 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3565 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3566 "Successfully set a stream's packet header");
3568 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3569 "Add environment field to writer after stream creation");
3573 test_trace_stream_class_clock();
3575 test_create_writer_vs_non_writer_mode();
3577 test_instanciate_event_before_stream(writer
);
3579 append_simple_event(stream_class
, stream1
, clock
);
3581 packet_resize_test(stream_class
, stream1
, clock
);
3583 append_complex_event(stream_class
, stream1
, clock
);
3585 append_existing_event_class(stream_class
);
3587 test_empty_stream(writer
);
3589 test_custom_event_header_stream(writer
);
3591 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3592 ok(metadata_string
, "Get metadata string");
3594 bt_ctf_writer_flush_metadata(writer
);
3595 validate_metadata(argv
[1], metadata_path
);
3596 validate_trace(argv
[2], trace_path
);
3599 bt_put(ret_stream_class
);
3602 bt_put(packet_context_type
);
3603 bt_put(packet_context_field_type
);
3604 bt_put(integer_type
);
3605 bt_put(stream_event_context_type
);
3606 bt_put(ret_field_type
);
3607 bt_put(packet_header_type
);
3608 bt_put(packet_header_field_type
);
3609 bt_put(packet_header
);
3610 bt_put(packet_header_field
);
3612 free(metadata_string
);
3613 bt_put(stream_class
);
3615 recursive_rmdir(trace_path
);