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 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
338 ok(int_64_type
, "Create a signed integer type");
339 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
341 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
342 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
343 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
344 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
345 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
346 bt_put(returned_type
);
348 bt_ctf_field_type_set_alignment(float_type
, 32);
349 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
350 "bt_ctf_field_type_get_alignment handles NULL correctly");
351 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
352 "bt_ctf_field_type_get_alignment returns a correct value");
354 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
355 "Set a floating point type's exponent digit count");
356 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
357 "Set a floating point type's mantissa digit count");
359 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
360 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
361 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
362 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
363 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
364 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
365 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
366 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
368 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
369 mapping_name_negative_test
, -12345, 0) == 0,
370 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
371 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
372 "escaping; \"test\"", 1, 1) == 0,
373 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
374 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
375 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
376 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
377 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
378 "event clock int float", 5, 22) == 0,
379 "Accept enumeration mapping strings containing reserved keywords");
380 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
382 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
383 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
384 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
385 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
386 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
387 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
388 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
390 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
391 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
392 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
393 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
394 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
395 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
397 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
398 "enum_field") == 0, "Add signed enumeration field to event");
400 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
401 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
402 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
403 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
404 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
405 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
406 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
407 NULL
, &ret_range_end_int64_t
) < 0,
408 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
409 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
410 &ret_range_start_int64_t
, NULL
) < 0,
411 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
412 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
413 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
414 "bt_ctf_field_type_enumeration_get_mapping returns a value");
415 ok(!strcmp(ret_char
, mapping_name_test
),
416 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
417 ok(ret_range_start_int64_t
== 42,
418 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
419 ok(ret_range_end_int64_t
== 42,
420 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
422 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
423 "escaping; \"test\"", 0, 0) == 0,
424 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
425 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
426 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
427 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
428 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
429 "event clock int float", 5, 22) == 0,
430 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
431 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
433 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
434 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
435 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
436 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
437 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
438 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
439 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
440 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
442 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
443 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
444 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
445 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
447 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
448 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
449 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
450 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
451 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
452 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
453 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
454 NULL
, &ret_range_end_uint64_t
) < 0,
455 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
456 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
457 &ret_range_start_uint64_t
, NULL
) < 0,
458 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
459 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
460 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
461 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
462 ok(!strcmp(ret_char
, mapping_name_test
),
463 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
464 ok(ret_range_start_uint64_t
== 42,
465 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
466 ok(ret_range_end_uint64_t
== 42,
467 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
469 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
471 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
474 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
476 /* Set an event context type which will contain a single integer*/
477 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
478 "event_specific_context"),
479 "Add event specific context field");
480 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
481 "bt_ctf_event_class_get_context_type handles NULL correctly");
482 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
483 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
485 ok(bt_ctf_event_class_set_context_type(simple_event_class
, NULL
) < 0,
486 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
487 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
488 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
489 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
490 "Set an event class' context type successfully");
491 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
492 ok(returned_type
== event_context_type
,
493 "bt_ctf_event_class_get_context_type returns the appropriate type");
494 bt_put(returned_type
);
496 bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
);
499 * bt_ctf_stream_class_add_event_class() copies the field types
500 * of simple_event_class, so we retrieve the new ones to create
501 * the appropriate fields.
503 BT_PUT(event_context_type
);
504 BT_PUT(event_payload_type
);
505 event_payload_type
= bt_ctf_event_class_get_payload_type(
507 assert(event_payload_type
);
508 event_context_type
= bt_ctf_event_class_get_context_type(
510 assert(event_context_type
);
511 ep_integer_field_type
=
512 bt_ctf_field_type_structure_get_field_type_by_name(
513 event_payload_type
, "integer_field");
514 assert(ep_integer_field_type
);
516 bt_ctf_field_type_structure_get_field_type_by_name(
517 event_payload_type
, "enum_field");
518 assert(ep_enum_field_type
);
519 ep_enum_field_unsigned_type
=
520 bt_ctf_field_type_structure_get_field_type_by_name(
521 event_payload_type
, "enum_field_unsigned");
522 assert(ep_enum_field_unsigned_type
);
524 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
525 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
526 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
527 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
528 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
529 "bt_ctf_stream_class_get_event_class handles NULL correctly");
530 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
531 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
532 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
533 ok(ret_event_class
== simple_event_class
,
534 "bt_ctf_stream_class_get_event_class returns the correct event class");
535 bt_put(ret_event_class
);
536 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
537 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
538 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
539 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
541 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
542 ok(ret_event_class
== simple_event_class
,
543 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
544 bt_put(ret_event_class
);
546 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
547 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
548 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
549 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
550 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
551 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
552 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
553 ok(ret_event_class
== simple_event_class
,
554 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
555 bt_put(ret_event_class
);
557 simple_event
= bt_ctf_event_create(simple_event_class
);
559 "Instantiate an event containing a single integer field");
561 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
562 "bt_ctf_event_get_clock handles NULL correctly");
563 ret_clock
= bt_ctf_event_get_clock(simple_event
);
564 ok(ret_clock
== clock
,
565 "bt_ctf_event_get_clock returns a correct clock");
568 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
569 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
570 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
571 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
573 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
574 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
575 "bt_ctf_field_floating_point_get_value fails on an unset float field");
576 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
577 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
578 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
579 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
580 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
581 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
582 "bt_ctf_field_floating_point_get_value returns a double value");
583 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
584 "bt_ctf_field_floating_point_get_value returns a correct value");
586 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
588 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
589 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
590 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
591 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
592 enum_container_field
= bt_ctf_field_enumeration_get_container(
594 ok(bt_ctf_field_signed_integer_set_value(
595 enum_container_field
, -42) == 0,
596 "Set signed enumeration container value");
597 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
598 ok(!strcmp(ret_char
, mapping_name_negative_test
),
599 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
600 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
603 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
604 assert(enum_field_unsigned
);
605 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
606 enum_field_unsigned
);
607 ok(bt_ctf_field_unsigned_integer_set_value(
608 enum_container_field_unsigned
, 42) == 0,
609 "Set unsigned enumeration container value");
610 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
611 enum_field_unsigned
);
613 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
614 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
615 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
617 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
619 /* Populate stream event context */
620 stream_event_context
=
621 bt_ctf_event_get_stream_event_context(simple_event
);
622 assert(stream_event_context
);
623 stream_event_context_field
= bt_ctf_field_structure_get_field(
624 stream_event_context
, "common_event_context");
625 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
627 /* Populate the event's context */
628 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
629 "bt_ctf_event_get_event_context handles NULL correctly");
630 event_context
= bt_ctf_event_get_event_context(simple_event
);
632 "bt_ctf_event_get_event_context returns a field");
633 returned_type
= bt_ctf_field_get_type(event_context
);
634 ok(returned_type
== event_context_type
,
635 "bt_ctf_event_get_event_context returns a field of the appropriate type");
636 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
637 "event_specific_context");
638 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
639 "Successfully set an event context's value");
640 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
641 "bt_ctf_event_set_event_context handles a NULL event correctly");
642 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
643 "bt_ctf_event_set_event_context handles a NULL event context correctly");
644 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
645 "bt_ctf_event_set_event_context rejects a context of the wrong type");
646 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
647 "Set an event context successfully");
649 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
650 "Append simple event to trace stream");
652 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
653 "bt_ctf_stream_get_packet_context handles NULL correctly");
654 packet_context
= bt_ctf_stream_get_packet_context(stream
);
656 "bt_ctf_stream_get_packet_context returns a packet context");
658 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
660 ok(packet_context_field
,
661 "Packet context contains the default packet_size field.");
662 bt_put(packet_context_field
);
663 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
664 "custom_packet_context_field");
665 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
666 "Custom packet context field value successfully set.");
668 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
669 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
670 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
671 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
672 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
673 "Successfully set a stream's packet context");
675 ok(bt_ctf_stream_flush(stream
) == 0,
676 "Flush trace stream with one event");
678 bt_put(simple_event_class
);
679 bt_put(simple_event
);
680 bt_put(uint_12_type
);
684 bt_put(enum_type_unsigned
);
685 bt_put(returned_type
);
686 bt_put(event_context_type
);
687 bt_put(integer_field
);
690 bt_put(enum_field_unsigned
);
691 bt_put(enum_container_field
);
692 bt_put(enum_container_field_unsigned
);
693 bt_put(packet_context
);
694 bt_put(packet_context_field
);
695 bt_put(stream_event_context
);
696 bt_put(stream_event_context_field
);
697 bt_put(event_context
);
698 bt_put(event_context_field
);
699 bt_put(event_payload_type
);
700 bt_put(ep_integer_field_type
);
701 bt_put(ep_enum_field_type
);
702 bt_put(ep_enum_field_unsigned_type
);
706 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
707 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
709 struct event_class_attrs_counts
{
720 struct event_class_attrs_counts
;
721 const char *complex_test_event_string
= "Complex Test Event";
722 const char *test_string_1
= "Test ";
723 const char *test_string_2
= "string ";
724 const char *test_string_3
= "abcdefghi";
725 const char *test_string_4
= "abcd\0efg\0hi";
726 const char *test_string_cat
= "Test string abcdeabcd";
727 struct bt_ctf_field_type
*uint_35_type
=
728 bt_ctf_field_type_integer_create(35);
729 struct bt_ctf_field_type
*int_16_type
=
730 bt_ctf_field_type_integer_create(16);
731 struct bt_ctf_field_type
*uint_3_type
=
732 bt_ctf_field_type_integer_create(3);
733 struct bt_ctf_field_type
*enum_variant_type
=
734 bt_ctf_field_type_enumeration_create(uint_3_type
);
735 struct bt_ctf_field_type
*variant_type
=
736 bt_ctf_field_type_variant_create(enum_variant_type
,
738 struct bt_ctf_field_type
*string_type
=
739 bt_ctf_field_type_string_create();
740 struct bt_ctf_field_type
*sequence_type
;
741 struct bt_ctf_field_type
*array_type
;
742 struct bt_ctf_field_type
*inner_structure_type
=
743 bt_ctf_field_type_structure_create();
744 struct bt_ctf_field_type
*complex_structure_type
=
745 bt_ctf_field_type_structure_create();
746 struct bt_ctf_field_type
*ret_field_type
;
747 struct bt_ctf_event_class
*event_class
;
748 struct bt_ctf_event
*event
;
749 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
750 *inner_structure_field
, *complex_structure_field
,
751 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
752 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
753 *stream_event_ctx_int_field
, *ret_field
;
754 uint64_t ret_unsigned_int
;
755 int64_t ret_signed_int
;
756 const char *ret_string
;
757 struct bt_ctf_stream_class
*ret_stream_class
;
758 struct bt_ctf_event_class
*ret_event_class
;
759 struct bt_ctf_field
*packet_context
, *packet_context_field
;
760 struct bt_value
*obj
;
762 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
763 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
764 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
765 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
766 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
767 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
768 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
769 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
770 bt_ctf_field_type_set_alignment(int_16_type
, 32);
771 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
772 bt_ctf_field_type_integer_set_base(uint_35_type
,
773 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
775 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
776 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
779 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
780 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
781 ret_field_type
= bt_ctf_field_type_array_get_element_type(
783 ok(ret_field_type
== int_16_type
,
784 "bt_ctf_field_type_array_get_element_type returns the correct type");
785 bt_put(ret_field_type
);
787 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
788 "bt_ctf_field_type_array_get_length handles NULL correctly");
789 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
790 "bt_ctf_field_type_array_get_length returns the correct length");
792 bt_ctf_field_type_structure_add_field(inner_structure_type
,
793 uint_35_type
, "seq_len");
794 bt_ctf_field_type_structure_add_field(inner_structure_type
,
795 sequence_type
, "a_sequence");
796 bt_ctf_field_type_structure_add_field(inner_structure_type
,
797 array_type
, "an_array");
799 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
801 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
803 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
804 "UINT35_TYPE", 2, 7);
806 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
808 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
809 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
810 enum_variant_type
, NULL
) < 0,
811 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
812 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
813 enum_variant_type
, "INT16_TYPE") == 1,
814 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
816 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
817 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
818 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
819 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
820 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
821 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
823 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
824 "An unknown entry"), "Reject a variant field based on an unknown tag value");
825 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
826 "UINT3_TYPE") == 0, "Add a field to a variant");
827 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
829 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
832 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
833 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
834 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
835 ok(ret_field_type
== enum_variant_type
,
836 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
837 bt_put(ret_field_type
);
839 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
840 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
841 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
842 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
843 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
844 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
845 "INT16_TYPE") == NULL
,
846 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
847 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
849 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
850 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
851 variant_type
, "INT16_TYPE");
852 ok(ret_field_type
== int_16_type
,
853 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
854 bt_put(ret_field_type
);
856 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
857 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
858 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
859 "bt_ctf_field_type_variant_get_field_count returns the correct count");
861 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
862 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
863 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
864 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
865 bt_put(ret_field_type
);
866 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
867 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
868 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
869 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
870 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
871 "bt_ctf_field_type_variant_get_field returns a field");
872 ok(!strcmp("INT16_TYPE", ret_string
),
873 "bt_ctf_field_type_variant_get_field returns a correct field name");
874 ok(ret_field_type
== int_16_type
,
875 "bt_ctf_field_type_variant_get_field returns a correct field type");
876 bt_put(ret_field_type
);
878 bt_ctf_field_type_structure_add_field(complex_structure_type
,
879 enum_variant_type
, "variant_selector");
880 bt_ctf_field_type_structure_add_field(complex_structure_type
,
881 string_type
, "a_string");
882 bt_ctf_field_type_structure_add_field(complex_structure_type
,
883 variant_type
, "variant_value");
884 bt_ctf_field_type_structure_add_field(complex_structure_type
,
885 inner_structure_type
, "inner_structure");
887 ok(bt_ctf_event_class_create("clock") == NULL
,
888 "Reject creation of an event class with an illegal name");
889 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
890 ok(event_class
, "Create an event class");
891 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
892 "Reject addition of a field with an empty name to an event");
893 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
894 "Reject addition of a field with a NULL type to an event");
895 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
897 "Reject addition of a type with an illegal name to an event");
898 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
900 "Add field of type unsigned integer to an event");
901 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
902 "int_16") == 0, "Add field of type signed integer to an event");
903 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
904 "complex_structure") == 0,
905 "Add composite structure to an event");
907 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
908 "bt_ctf_event_class_get_name handles NULL correctly");
909 ret_string
= bt_ctf_event_class_get_name(event_class
);
910 ok(!strcmp(ret_string
, complex_test_event_string
),
911 "bt_ctf_event_class_get_name returns a correct name");
912 ok(bt_ctf_event_class_get_id(event_class
) < 0,
913 "bt_ctf_event_class_get_id returns a negative value when not set");
914 ok(bt_ctf_event_class_get_id(NULL
) < 0,
915 "bt_ctf_event_class_get_id handles NULL correctly");
916 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
917 "bt_ctf_event_class_set_id handles NULL correctly");
918 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
919 "Set an event class' id");
920 ok(bt_ctf_event_class_get_id(event_class
) == 42,
921 "bt_ctf_event_class_get_id returns the correct value");
923 /* Test event class attributes */
924 obj
= bt_value_integer_create_init(15);
926 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
927 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
928 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
929 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
930 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
931 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
932 assert(!bt_value_integer_set(obj
, -3));
933 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
934 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
935 assert(!bt_value_integer_set(obj
, 11));
936 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
937 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
938 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
939 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
940 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
942 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
945 obj
= bt_value_integer_create_init(5);
947 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
948 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
950 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
951 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
952 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
953 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
954 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
955 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
956 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
959 ret
= bt_value_integer_get(obj
, &int64_value
);
960 ok(obj
&& !ret
&& int64_value
== 5,
961 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
964 obj
= bt_value_string_create_init("nu name");
966 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
967 ret_string
= bt_ctf_event_class_get_name(event_class
);
968 ok(!strcmp(ret_string
, "nu name"),
969 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
970 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
971 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
973 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
975 obj
= bt_value_string_create_init("http://kernel.org/");
977 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
980 ok(bt_ctf_event_class_get_attribute_count(NULL
),
981 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
982 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
983 "bt_ctf_event_class_get_attribute_count returns the correct count");
984 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
985 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
986 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
987 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
988 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
989 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
990 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
991 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
993 memset(&attrs_count
, 0, sizeof(attrs_count
));
995 for (i
= 0; i
< 4; ++i
) {
996 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
998 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
999 assert(ret_string
&& obj
);
1001 if (!strcmp(ret_string
, "id")) {
1003 ok(bt_value_is_integer(obj
),
1004 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1006 } else if (!strcmp(ret_string
, "name")) {
1008 ok(bt_value_is_string(obj
),
1009 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1011 } else if (!strcmp(ret_string
, "loglevel")) {
1012 attrs_count
.loglevel
++;
1013 ok(bt_value_is_integer(obj
),
1014 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1016 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1017 attrs_count
.modelemfuri
++;
1018 ok(bt_value_is_string(obj
),
1019 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1022 attrs_count
.unknown
++;
1028 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1029 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1030 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1031 "event class has one instance of each known attribute");
1033 /* Add event class to the stream class */
1034 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1035 "Reject addition of NULL event class to a stream class");
1036 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1037 event_class
) == 0, "Add an event class to stream class");
1039 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1040 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1041 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1042 ok(ret_stream_class
== stream_class
,
1043 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1044 bt_put(ret_stream_class
);
1046 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1047 "bt_ctf_event_class_get_field_count handles NULL correctly");
1048 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1049 "bt_ctf_event_class_get_field_count returns a correct value");
1051 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1052 &ret_field_type
, 0) < 0,
1053 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1054 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1055 &ret_field_type
, 0) == 0,
1056 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1057 bt_put(ret_field_type
);
1058 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1060 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1061 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1062 &ret_field_type
, 42) < 0,
1063 "bt_ctf_event_class_get_field handles an invalid index correctly");
1064 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1065 &ret_field_type
, 0) == 0,
1066 "bt_ctf_event_class_get_field returns a field");
1067 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
1068 "bt_ctf_event_class_get_field returns a correct field type");
1069 bt_put(ret_field_type
);
1070 ok(!strcmp(ret_string
, "uint_35"),
1071 "bt_ctf_event_class_get_field returns a correct field name");
1072 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1073 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1074 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1075 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1076 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1077 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1078 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1079 "complex_structure");
1080 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
1081 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1082 bt_put(ret_field_type
);
1084 event
= bt_ctf_event_create(event_class
);
1085 ok(event
, "Instanciate a complex event");
1087 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1088 "bt_ctf_event_get_class handles NULL correctly");
1089 ret_event_class
= bt_ctf_event_get_class(event
);
1090 ok(ret_event_class
== event_class
,
1091 "bt_ctf_event_get_class returns the correct event class");
1092 bt_put(ret_event_class
);
1094 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1095 if (!uint_35_field
) {
1096 printf("uint_35_field is NULL\n");
1099 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1100 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1101 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1102 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1103 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1104 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1105 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1106 &ret_unsigned_int
) == 0,
1107 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1108 ok(ret_unsigned_int
== 0x0DDF00D,
1109 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1110 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1111 &ret_signed_int
) < 0,
1112 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1113 bt_put(uint_35_field
);
1115 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1116 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1117 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1118 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1119 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1120 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1121 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1122 &ret_signed_int
) == 0,
1123 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1124 ok(ret_signed_int
== -12345,
1125 "bt_ctf_field_signed_integer_get_value returns the correct value");
1126 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1127 &ret_unsigned_int
) < 0,
1128 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1129 bt_put(int_16_field
);
1131 complex_structure_field
= bt_ctf_event_get_payload(event
,
1132 "complex_structure");
1134 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1135 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1136 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1137 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1138 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1139 complex_structure_field
, 3);
1140 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1141 bt_put(inner_structure_field
);
1142 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1143 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1144 bt_put(ret_field_type
);
1146 inner_structure_field
= bt_ctf_field_structure_get_field(
1147 complex_structure_field
, "inner_structure");
1148 a_string_field
= bt_ctf_field_structure_get_field(
1149 complex_structure_field
, "a_string");
1150 enum_variant_field
= bt_ctf_field_structure_get_field(
1151 complex_structure_field
, "variant_selector");
1152 variant_field
= bt_ctf_field_structure_get_field(
1153 complex_structure_field
, "variant_value");
1154 uint_35_field
= bt_ctf_field_structure_get_field(
1155 inner_structure_field
, "seq_len");
1156 a_sequence_field
= bt_ctf_field_structure_get_field(
1157 inner_structure_field
, "a_sequence");
1158 an_array_field
= bt_ctf_field_structure_get_field(
1159 inner_structure_field
, "an_array");
1161 enum_container_field
= bt_ctf_field_enumeration_get_container(
1162 enum_variant_field
);
1163 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1164 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1165 enum_variant_field
);
1166 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1167 bt_put(int_16_field
);
1168 ok(!bt_ctf_field_string_get_value(a_string_field
),
1169 "bt_ctf_field_string_get_value returns NULL on an unset field");
1170 bt_ctf_field_string_set_value(a_string_field
,
1172 ok(!bt_ctf_field_string_get_value(NULL
),
1173 "bt_ctf_field_string_get_value correctly handles NULL");
1174 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1175 "bt_ctf_field_string_append correctly handles a NULL string field");
1176 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1177 "bt_ctf_field_string_append correctly handles a NULL string value");
1178 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1179 "bt_ctf_field_string_append succeeds");
1180 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1181 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1182 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1183 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1184 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1185 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1186 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1187 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1188 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1189 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1190 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1191 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1193 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1194 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1195 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1196 "bt_ctf_field_string_get_value returns a correct value");
1197 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1198 SEQUENCE_TEST_LENGTH
);
1200 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1201 enum_container_field
) == NULL
,
1202 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1203 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1205 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1206 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1207 variant_type
, enum_variant_field
);
1208 ok(ret_field_type
== int_16_type
,
1209 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1211 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1212 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1213 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1214 uint_35_field
) == 0, "Set a sequence field's length");
1215 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1216 ok(ret_field
== uint_35_field
,
1217 "bt_ctf_field_sequence_get_length returns the correct length field");
1218 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1219 "bt_ctf_field_sequence_get_length properly handles NULL");
1221 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1222 int_16_field
= bt_ctf_field_sequence_get_field(
1223 a_sequence_field
, i
);
1224 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1225 bt_put(int_16_field
);
1228 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1229 int_16_field
= bt_ctf_field_array_get_field(
1231 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1232 bt_put(int_16_field
);
1235 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1236 assert(stream_event_ctx_field
);
1237 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1238 stream_event_ctx_field
, "common_event_context");
1239 BT_PUT(stream_event_ctx_field
);
1240 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1241 BT_PUT(stream_event_ctx_int_field
);
1243 bt_ctf_clock_set_time(clock
, ++current_time
);
1244 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1245 "Append a complex event to a stream");
1248 * Populate the custom packet context field with a dummy value
1249 * otherwise flush will fail.
1251 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1252 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1253 "custom_packet_context_field");
1254 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1256 ok(bt_ctf_stream_flush(stream
) == 0,
1257 "Flush a stream containing a complex event");
1259 bt_put(uint_35_field
);
1260 bt_put(a_string_field
);
1261 bt_put(inner_structure_field
);
1262 bt_put(complex_structure_field
);
1263 bt_put(a_sequence_field
);
1264 bt_put(an_array_field
);
1265 bt_put(enum_variant_field
);
1266 bt_put(enum_container_field
);
1267 bt_put(variant_field
);
1269 bt_put(packet_context_field
);
1270 bt_put(packet_context
);
1271 bt_put(uint_35_type
);
1272 bt_put(int_16_type
);
1273 bt_put(string_type
);
1274 bt_put(sequence_type
);
1276 bt_put(inner_structure_type
);
1277 bt_put(complex_structure_type
);
1278 bt_put(uint_3_type
);
1279 bt_put(enum_variant_type
);
1280 bt_put(variant_type
);
1281 bt_put(ret_field_type
);
1282 bt_put(event_class
);
1287 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1288 struct bt_ctf_field_type
*expected_type
, const char *name
)
1290 struct bt_ctf_field_type
*copy_type
;
1292 copy_type
= bt_ctf_field_get_type(field
);
1293 ok(copy_type
== expected_type
,
1294 "bt_ctf_field_copy does not copy the type (%s)", name
);
1299 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1300 struct bt_ctf_field
*field_b
, const char *name
)
1302 ok(field_a
!= field_b
,
1303 "bt_ctf_field_copy creates different pointers (%s)", name
);
1307 void field_copy_tests()
1309 struct bt_ctf_field_type
*len_type
= NULL
;
1310 struct bt_ctf_field_type
*fp_type
= NULL
;
1311 struct bt_ctf_field_type
*s_type
= NULL
;
1312 struct bt_ctf_field_type
*e_int_type
= NULL
;
1313 struct bt_ctf_field_type
*e_type
= NULL
;
1314 struct bt_ctf_field_type
*v_type
= NULL
;
1315 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1316 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1317 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1318 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1319 struct bt_ctf_field_type
*strct_type
= NULL
;
1320 struct bt_ctf_field
*len
= NULL
;
1321 struct bt_ctf_field
*fp
= NULL
;
1322 struct bt_ctf_field
*s
= NULL
;
1323 struct bt_ctf_field
*e_int
= NULL
;
1324 struct bt_ctf_field
*e
= NULL
;
1325 struct bt_ctf_field
*v
= NULL
;
1326 struct bt_ctf_field
*v_selected
= NULL
;
1327 struct bt_ctf_field
*v_selected_cur
= NULL
;
1328 struct bt_ctf_field
*v_selected_0
= NULL
;
1329 struct bt_ctf_field
*v_selected_1
= NULL
;
1330 struct bt_ctf_field
*v_selected_2
= NULL
;
1331 struct bt_ctf_field
*v_selected_3
= NULL
;
1332 struct bt_ctf_field
*v_selected_4
= NULL
;
1333 struct bt_ctf_field
*v_selected_5
= NULL
;
1334 struct bt_ctf_field
*v_selected_6
= NULL
;
1335 struct bt_ctf_field
*a
= NULL
;
1336 struct bt_ctf_field
*a_0
= NULL
;
1337 struct bt_ctf_field
*a_1
= NULL
;
1338 struct bt_ctf_field
*a_2
= NULL
;
1339 struct bt_ctf_field
*a_3
= NULL
;
1340 struct bt_ctf_field
*a_4
= NULL
;
1341 struct bt_ctf_field
*strct
= NULL
;
1342 struct bt_ctf_field
*len_copy
= NULL
;
1343 struct bt_ctf_field
*fp_copy
= NULL
;
1344 struct bt_ctf_field
*s_copy
= NULL
;
1345 struct bt_ctf_field
*e_int_copy
= NULL
;
1346 struct bt_ctf_field
*e_copy
= NULL
;
1347 struct bt_ctf_field
*v_copy
= NULL
;
1348 struct bt_ctf_field
*v_selected_copy
= NULL
;
1349 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1350 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1351 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1352 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1353 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1354 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1355 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1356 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1357 struct bt_ctf_field
*a_copy
= NULL
;
1358 struct bt_ctf_field
*a_0_copy
= NULL
;
1359 struct bt_ctf_field
*a_1_copy
= NULL
;
1360 struct bt_ctf_field
*a_2_copy
= NULL
;
1361 struct bt_ctf_field
*a_3_copy
= NULL
;
1362 struct bt_ctf_field
*a_4_copy
= NULL
;
1363 struct bt_ctf_field
*strct_copy
= NULL
;
1364 uint64_t uint64_t_val
;
1365 const char *str_val
;
1369 /* create len type */
1370 len_type
= bt_ctf_field_type_integer_create(32);
1373 /* create fp type */
1374 fp_type
= bt_ctf_field_type_floating_point_create();
1378 s_type
= bt_ctf_field_type_string_create();
1381 /* create e_int type */
1382 e_int_type
= bt_ctf_field_type_integer_create(8);
1386 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1388 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1391 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1395 /* create v_label1 type */
1396 v_label1_type
= bt_ctf_field_type_string_create();
1397 assert(v_label1_type
);
1399 /* create v_label1_array type */
1400 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1401 assert(v_label1_array_type
);
1403 /* create v_label2 type */
1404 v_label2_type
= bt_ctf_field_type_integer_create(16);
1405 assert(v_label2_type
);
1407 /* create v_label2_seq type */
1408 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1410 assert(v_label2_seq_type
);
1413 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1415 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1418 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1422 /* create strct type */
1423 strct_type
= bt_ctf_field_type_structure_create();
1425 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1428 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1430 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1432 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1434 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1436 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1437 v_label1_array_type
, "a");
1441 strct
= bt_ctf_field_create(strct_type
);
1445 len
= bt_ctf_field_structure_get_field(strct
, "len");
1449 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1453 s
= bt_ctf_field_structure_get_field(strct
, "s");
1457 e
= bt_ctf_field_structure_get_field(strct
, "e");
1460 /* get e_int (underlying integer) */
1461 e_int
= bt_ctf_field_enumeration_get_container(e
);
1465 v
= bt_ctf_field_structure_get_field(strct
, "v");
1469 a
= bt_ctf_field_structure_get_field(strct
, "a");
1473 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1477 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1481 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1484 /* set e field (LABEL2) */
1485 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1489 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1491 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1492 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1493 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1494 ok(v_selected_cur
== v_selected
,
1495 "bt_ctf_field_variant_get_current_field returns the current field");
1496 bt_put(v_selected_cur
);
1498 /* set selected v field */
1499 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1501 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1502 assert(v_selected_0
);
1503 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1505 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1506 assert(v_selected_1
);
1507 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1509 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1510 assert(v_selected_2
);
1511 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1513 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1514 assert(v_selected_3
);
1515 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1517 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1518 assert(v_selected_4
);
1519 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1521 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1522 assert(v_selected_5
);
1523 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1525 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1526 assert(v_selected_6
);
1527 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1531 a_0
= bt_ctf_field_array_get_field(a
, 0);
1533 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1535 a_1
= bt_ctf_field_array_get_field(a
, 1);
1537 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1539 a_2
= bt_ctf_field_array_get_field(a
, 2);
1541 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1543 a_3
= bt_ctf_field_array_get_field(a
, 3);
1545 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1547 a_4
= bt_ctf_field_array_get_field(a
, 4);
1549 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1552 /* create copy of strct */
1553 ok(!bt_ctf_field_copy(NULL
),
1554 "bt_ctf_field_copy handles NULL correctly");
1555 strct_copy
= bt_ctf_field_copy(strct
);
1557 "bt_ctf_field_copy returns a valid pointer");
1559 /* get all copied fields */
1560 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1562 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1564 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1566 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1568 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1570 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1572 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1573 assert(v_selected_copy
);
1574 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1575 assert(v_selected_0_copy
);
1576 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1577 assert(v_selected_1_copy
);
1578 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1579 assert(v_selected_2_copy
);
1580 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1581 assert(v_selected_3_copy
);
1582 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1583 assert(v_selected_4_copy
);
1584 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1585 assert(v_selected_5_copy
);
1586 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1587 assert(v_selected_6_copy
);
1588 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1589 "sequence field copy is not too large");
1590 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1592 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1594 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1596 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1598 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1600 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1602 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1603 "array field copy is not too large");
1605 /* make sure copied fields are different pointers */
1606 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1607 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1608 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1609 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1610 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1611 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1612 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1613 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1615 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1617 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1619 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1621 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1623 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1625 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1627 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1629 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1630 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1631 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1632 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1633 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1634 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1636 /* make sure copied fields share the same types */
1637 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1638 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1639 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1640 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1641 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1642 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1643 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1645 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1647 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1649 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1651 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1653 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1655 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1657 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1659 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1660 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1661 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1662 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1663 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1664 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1666 /* validate len copy */
1667 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1669 ok(uint64_t_val
== 7,
1670 "bt_ctf_field_copy creates a valid integer field copy");
1672 /* validate fp copy */
1673 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1675 ok(double_val
== 3.14,
1676 "bt_ctf_field_copy creates a valid floating point number field copy");
1678 /* validate s copy */
1679 str_val
= bt_ctf_field_string_get_value(s_copy
);
1680 ok(str_val
&& !strcmp(str_val
, "btbt"),
1681 "bt_ctf_field_copy creates a valid string field copy");
1683 /* validate e_int copy */
1684 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1687 ok(uint64_t_val
== 23,
1688 "bt_ctf_field_copy creates a valid enum's integer field copy");
1690 /* validate e copy */
1691 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1692 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1693 "bt_ctf_field_copy creates a valid enum field copy");
1695 /* validate v_selected copy */
1696 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1697 assert(v_selected_copy_len
);
1698 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1701 ok(uint64_t_val
== 7,
1702 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1703 bt_put(v_selected_copy_len
);
1704 v_selected_copy_len
= NULL
;
1706 /* validate v_selected copy fields */
1707 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1710 ok(uint64_t_val
== 7,
1711 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1712 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1715 ok(uint64_t_val
== 6,
1716 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1717 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1720 ok(uint64_t_val
== 5,
1721 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1722 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1725 ok(uint64_t_val
== 4,
1726 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1727 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1730 ok(uint64_t_val
== 3,
1731 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1732 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1735 ok(uint64_t_val
== 2,
1736 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1737 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1740 ok(uint64_t_val
== 1,
1741 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1743 /* validate a copy fields */
1744 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1745 ok(str_val
&& !strcmp(str_val
, "a_0"),
1746 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1747 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1748 ok(str_val
&& !strcmp(str_val
, "a_1"),
1749 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1750 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1751 ok(str_val
&& !strcmp(str_val
, "a_2"),
1752 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1753 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1754 ok(str_val
&& !strcmp(str_val
, "a_3"),
1755 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1756 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1757 ok(str_val
&& !strcmp(str_val
, "a_4"),
1758 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1760 /* put everything */
1767 bt_put(v_label1_type
);
1768 bt_put(v_label1_array_type
);
1769 bt_put(v_label2_type
);
1770 bt_put(v_label2_seq_type
);
1779 bt_put(v_selected_0
);
1780 bt_put(v_selected_1
);
1781 bt_put(v_selected_2
);
1782 bt_put(v_selected_3
);
1783 bt_put(v_selected_4
);
1784 bt_put(v_selected_5
);
1785 bt_put(v_selected_6
);
1799 bt_put(v_selected_copy
);
1800 bt_put(v_selected_0_copy
);
1801 bt_put(v_selected_1_copy
);
1802 bt_put(v_selected_2_copy
);
1803 bt_put(v_selected_3_copy
);
1804 bt_put(v_selected_4_copy
);
1805 bt_put(v_selected_5_copy
);
1806 bt_put(v_selected_6_copy
);
1817 void type_field_tests()
1819 struct bt_ctf_field
*uint_12
;
1820 struct bt_ctf_field
*int_16
;
1821 struct bt_ctf_field
*string
;
1822 struct bt_ctf_field
*enumeration
;
1823 struct bt_ctf_field_type
*composite_structure_type
;
1824 struct bt_ctf_field_type
*structure_seq_type
;
1825 struct bt_ctf_field_type
*string_type
;
1826 struct bt_ctf_field_type
*sequence_type
;
1827 struct bt_ctf_field_type
*uint_8_type
;
1828 struct bt_ctf_field_type
*int_16_type
;
1829 struct bt_ctf_field_type
*uint_12_type
=
1830 bt_ctf_field_type_integer_create(12);
1831 struct bt_ctf_field_type
*enumeration_type
;
1832 struct bt_ctf_field_type
*returned_type
;
1833 const char *ret_string
;
1835 returned_type
= bt_ctf_field_get_type(NULL
);
1836 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1838 ok(uint_12_type
, "Create an unsigned integer type");
1839 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1840 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1841 "Set integer type's base as binary");
1842 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1843 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1844 "Set integer type's base as decimal");
1845 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1846 BT_CTF_INTEGER_BASE_UNKNOWN
),
1847 "Reject integer type's base set as unknown");
1848 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1849 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1850 "Set integer type's base as octal");
1851 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1852 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1853 "Set integer type's base as hexadecimal");
1854 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1855 "Reject unknown integer base value");
1856 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1857 "Set integer type signedness to signed");
1858 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1859 "Set integer type signedness to unsigned");
1860 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1861 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1862 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1863 "bt_ctf_field_type_integer_get_size returns a correct value");
1864 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1865 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1866 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1867 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1869 ok(bt_ctf_field_type_set_byte_order(NULL
,
1870 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1871 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1872 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1873 (enum bt_ctf_byte_order
) 42) < 0,
1874 "bt_ctf_field_type_set_byte_order rejects invalid values");
1875 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1876 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1877 "Set an integer's byte order to little endian");
1878 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1879 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1880 "Set an integer's byte order to big endian");
1881 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1882 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1883 "bt_ctf_field_type_get_byte_order returns a correct value");
1884 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1885 BT_CTF_BYTE_ORDER_UNKNOWN
,
1886 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1888 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1889 BT_CTF_TYPE_ID_UNKNOWN
,
1890 "bt_ctf_field_type_get_type_id handles NULL correctly");
1891 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1892 BT_CTF_TYPE_ID_INTEGER
,
1893 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1895 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1896 BT_CTF_INTEGER_BASE_UNKNOWN
,
1897 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1898 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1899 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1900 "bt_ctf_field_type_integer_get_base returns a correct value");
1902 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1903 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1904 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1905 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1906 (enum bt_ctf_string_encoding
) 123) < 0,
1907 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1908 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1909 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1910 "Set integer type encoding to UTF8");
1911 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1912 BT_CTF_STRING_ENCODING_UNKNOWN
,
1913 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1914 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1915 BT_CTF_STRING_ENCODING_UTF8
,
1916 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1918 int_16_type
= bt_ctf_field_type_integer_create(16);
1919 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1920 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1921 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1922 uint_8_type
= bt_ctf_field_type_integer_create(8);
1924 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1925 ok(sequence_type
, "Create a sequence of int16_t type");
1926 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1927 BT_CTF_TYPE_ID_SEQUENCE
,
1928 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1930 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1931 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1932 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1934 ok(!strcmp(ret_string
, "seq_len"),
1935 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1936 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1937 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1938 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1940 ok(returned_type
== int_16_type
,
1941 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1942 bt_put(returned_type
);
1944 string_type
= bt_ctf_field_type_string_create();
1945 ok(string_type
, "Create a string type");
1946 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1947 BT_CTF_STRING_ENCODING_NONE
),
1948 "Reject invalid \"None\" string encoding");
1949 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1951 "Reject invalid string encoding");
1952 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1953 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1954 "Set string encoding to ASCII");
1956 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1957 BT_CTF_STRING_ENCODING_UNKNOWN
,
1958 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1959 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1960 BT_CTF_STRING_ENCODING_ASCII
,
1961 "bt_ctf_field_type_string_get_encoding returns the correct value");
1963 structure_seq_type
= bt_ctf_field_type_structure_create();
1964 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1965 BT_CTF_TYPE_ID_STRUCT
,
1966 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1967 ok(structure_seq_type
, "Create a structure type");
1968 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1969 uint_8_type
, "seq_len") == 0,
1970 "Add a uint8_t type to a structure");
1971 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1972 sequence_type
, "a_sequence") == 0,
1973 "Add a sequence type to a structure");
1975 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1976 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1977 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1978 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1980 ok(bt_ctf_field_type_structure_get_field(NULL
,
1981 &ret_string
, &returned_type
, 1) < 0,
1982 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1983 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1984 NULL
, &returned_type
, 1) == 0,
1985 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1986 bt_put(returned_type
);
1987 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1988 &ret_string
, NULL
, 1) == 0,
1989 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1990 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1991 &ret_string
, &returned_type
, 10) < 0,
1992 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1993 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1994 &ret_string
, &returned_type
, 1) == 0,
1995 "bt_ctf_field_type_structure_get_field returns a field");
1996 ok(!strcmp(ret_string
, "a_sequence"),
1997 "bt_ctf_field_type_structure_get_field returns a correct field name");
1998 ok(returned_type
== sequence_type
,
1999 "bt_ctf_field_type_structure_get_field returns a correct field type");
2000 bt_put(returned_type
);
2002 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2003 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2004 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2005 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2006 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2007 structure_seq_type
, "a_sequence");
2008 ok(returned_type
== sequence_type
,
2009 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2010 bt_put(returned_type
);
2012 composite_structure_type
= bt_ctf_field_type_structure_create();
2013 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2014 string_type
, "a_string") == 0,
2015 "Add a string type to a structure");
2016 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2017 structure_seq_type
, "inner_structure") == 0,
2018 "Add a structure type to a structure");
2020 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2021 NULL
, "a_sequence") == NULL
,
2022 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2023 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2024 structure_seq_type
, NULL
) == NULL
,
2025 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2026 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2027 structure_seq_type
, "a_sequence");
2028 ok(returned_type
== sequence_type
,
2029 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2030 bt_put(returned_type
);
2032 int_16
= bt_ctf_field_create(int_16_type
);
2033 ok(int_16
, "Instanciate a signed 16-bit integer");
2034 uint_12
= bt_ctf_field_create(uint_12_type
);
2035 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2036 returned_type
= bt_ctf_field_get_type(int_16
);
2037 ok(returned_type
== int_16_type
,
2038 "bt_ctf_field_get_type returns the correct type");
2040 /* Can't modify types after instanciating them */
2041 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2042 BT_CTF_INTEGER_BASE_DECIMAL
),
2043 "Check an integer type' base can't be modified after instanciation");
2044 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2045 "Check an integer type's signedness can't be modified after instanciation");
2047 /* Check signed property is checked */
2048 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2049 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2050 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2051 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2053 /* Check overflows are properly tested for */
2054 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2055 "Check -32768 is allowed for a signed 16-bit integer");
2056 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2057 "Check 32767 is allowed for a signed 16-bit integer");
2058 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2059 "Check 32768 is not allowed for a signed 16-bit integer");
2060 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2061 "Check -32769 is not allowed for a signed 16-bit integer");
2062 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2063 "Check -42 is allowed for a signed 16-bit integer");
2065 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2066 "Check 4095 is allowed for an unsigned 12-bit integer");
2067 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2068 "Check 4096 is not allowed for a unsigned 12-bit integer");
2069 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2070 "Check 0 is allowed for an unsigned 12-bit integer");
2072 string
= bt_ctf_field_create(string_type
);
2073 ok(string
, "Instanciate a string field");
2074 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2075 "Set a string's value");
2077 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2078 ok(enumeration_type
,
2079 "Create an enumeration type with an unsigned 12-bit integer as container");
2080 enumeration
= bt_ctf_field_create(enumeration_type
);
2082 "Check enumeration types are validated before instantiation");
2087 bt_put(enumeration
);
2088 bt_put(composite_structure_type
);
2089 bt_put(structure_seq_type
);
2090 bt_put(string_type
);
2091 bt_put(sequence_type
);
2092 bt_put(uint_8_type
);
2093 bt_put(int_16_type
);
2094 bt_put(uint_12_type
);
2095 bt_put(enumeration_type
);
2096 bt_put(returned_type
);
2100 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2101 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2104 * Append enough events to force the underlying packet to be resized.
2105 * Also tests that a new event can be declared after a stream has been
2106 * instantiated and used/flushed.
2110 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2112 struct bt_ctf_field_type
*integer_type
=
2113 bt_ctf_field_type_integer_create(17);
2114 struct bt_ctf_field_type
*string_type
=
2115 bt_ctf_field_type_string_create();
2116 struct bt_ctf_event
*event
= NULL
;
2117 struct bt_ctf_field
*ret_field
= NULL
;
2118 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2119 uint64_t ret_uint64
;
2120 int events_appended
= 0;
2121 struct bt_ctf_field
*packet_context
= NULL
,
2122 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
2123 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2124 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2125 struct bt_ctf_field_type
*ep_type
= NULL
;
2127 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2129 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2131 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2132 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2138 * bt_ctf_stream_class_add_event_class() copies the field types
2139 * of event_class, so we retrieve the new ones to create the
2140 * appropriate fields.
2142 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2144 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2145 ep_type
, "field_1");
2146 assert(ep_field_1_type
);
2147 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2148 ep_type
, "a_string");
2149 assert(ep_a_string_type
);
2151 event
= bt_ctf_event_create(event_class
);
2152 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2153 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2154 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2155 "bt_ctf_event_get_payload_by_index returns a correct field");
2156 bt_put(ret_field_type
);
2159 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2160 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2161 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2162 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2165 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2166 event
= bt_ctf_event_create(event_class
);
2167 struct bt_ctf_field
*integer
=
2168 bt_ctf_field_create(ep_field_1_type
);
2169 struct bt_ctf_field
*string
=
2170 bt_ctf_field_create(ep_a_string_type
);
2172 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2173 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2174 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2177 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2178 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2182 /* Populate stream event context */
2183 stream_event_context
=
2184 bt_ctf_event_get_stream_event_context(event
);
2185 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2186 "common_event_context");
2187 BT_PUT(stream_event_context
);
2188 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2192 ret
|= bt_ctf_stream_append_event(stream
, event
);
2200 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2201 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2202 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2203 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2204 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2205 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2206 ok(ret
== 0 && ret_uint64
== 0,
2207 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2208 bt_ctf_stream_append_discarded_events(stream
, 1000);
2209 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2210 ok(ret
== 0 && ret_uint64
== 1000,
2211 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2214 ok(events_appended
, "Append 100 000 events to a stream");
2217 * Populate the custom packet context field with a dummy value
2218 * otherwise flush will fail.
2220 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2221 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2222 "custom_packet_context_field");
2223 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2225 ok(bt_ctf_stream_flush(stream
) == 0,
2226 "Flush a stream that forces a packet resize");
2227 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2228 ok(ret
== 0 && ret_uint64
== 1000,
2229 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2230 bt_put(integer_type
);
2231 bt_put(string_type
);
2232 bt_put(packet_context
);
2233 bt_put(packet_context_field
);
2234 bt_put(stream_event_context
);
2235 bt_put(event_class
);
2236 bt_put(ep_field_1_type
);
2237 bt_put(ep_a_string_type
);
2242 void test_empty_stream(struct bt_ctf_writer
*writer
)
2245 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2246 struct bt_ctf_stream_class
*stream_class
= NULL
;
2247 struct bt_ctf_stream
*stream
= NULL
;
2249 trace
= bt_ctf_writer_get_trace(writer
);
2251 diag("Failed to get trace from writer");
2256 stream_class
= bt_ctf_stream_class_create("empty_stream");
2257 if (!stream_class
) {
2258 diag("Failed to create stream class");
2263 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2264 "bt_ctf_stream_class_get_trace handles NULL correctly");
2265 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2266 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2268 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2270 diag("Failed to create writer stream");
2275 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2276 ok(ret_trace
== trace
,
2277 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2280 "Created a stream class with default attributes and an empty stream");
2284 bt_put(stream_class
);
2288 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2291 struct bt_ctf_trace
*trace
= NULL
;
2292 struct bt_ctf_clock
*clock
= NULL
;
2293 struct bt_ctf_stream_class
*stream_class
= NULL
;
2294 struct bt_ctf_stream
*stream
= NULL
;
2295 struct bt_ctf_field_type
*integer_type
= NULL
,
2296 *sequence_type
= NULL
, *event_header_type
= NULL
;
2297 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2298 *event_header
= NULL
, *packet_header
= NULL
;
2299 struct bt_ctf_event_class
*event_class
= NULL
;
2300 struct bt_ctf_event
*event
= NULL
;
2302 trace
= bt_ctf_writer_get_trace(writer
);
2304 fail("Failed to get trace from writer");
2308 clock
= bt_ctf_trace_get_clock(trace
, 0);
2310 fail("Failed to get clock from trace");
2314 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2315 if (!stream_class
) {
2316 fail("Failed to create stream class");
2320 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2322 fail("Failed to set stream class clock");
2327 * Customize event header to add an "seq_len" integer member
2328 * which will be used as the length of a sequence in an event of this
2331 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2333 if (!event_header_type
) {
2334 fail("Failed to get event header type");
2338 integer_type
= bt_ctf_field_type_integer_create(13);
2339 if (!integer_type
) {
2340 fail("Failed to create length integer type");
2344 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2345 integer_type
, "seq_len");
2347 fail("Failed to add a new field to stream event header");
2351 event_class
= bt_ctf_event_class_create("sequence_event");
2353 fail("Failed to create event class");
2358 * This event's payload will contain a sequence which references
2359 * stream.event.header.seq_len as its length field.
2361 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2362 "stream.event.header.seq_len");
2363 if (!sequence_type
) {
2364 fail("Failed to create a sequence");
2368 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2371 fail("Failed to add a sequence to an event class");
2375 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2377 fail("Failed to add event class to stream class");
2381 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2383 fail("Failed to create stream")
2388 * We have defined a custom packet header field. We have to populate it
2391 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2392 if (!packet_header
) {
2393 fail("Failed to get stream packet header");
2397 integer
= bt_ctf_field_structure_get_field(packet_header
,
2398 "custom_trace_packet_header_field");
2400 fail("Failed to retrieve custom_trace_packet_header_field");
2404 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2406 fail("Failed to set custom_trace_packet_header_field value");
2411 event
= bt_ctf_event_create(event_class
);
2413 fail("Failed to create event");
2417 event_header
= bt_ctf_event_get_header(event
);
2418 if (!event_header
) {
2419 fail("Failed to get event header");
2423 integer
= bt_ctf_field_structure_get_field(event_header
,
2426 fail("Failed to get seq_len field from event header");
2430 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2432 fail("Failed to set seq_len value in event header");
2436 /* Populate both sequence integer fields */
2437 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2439 fail("Failed to retrieve sequence from event");
2443 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2445 fail("Failed to set sequence length");
2450 for (i
= 0; i
< 2; i
++) {
2451 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2453 fail("Failed to retrieve sequence element");
2457 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2459 fail("Failed to set sequence element value");
2467 ret
= bt_ctf_stream_append_event(stream
, event
);
2469 fail("Failed to append event to stream");
2473 ret
= bt_ctf_stream_flush(stream
);
2475 fail("Failed to flush custom_event_header stream");
2481 bt_put(stream_class
);
2482 bt_put(event_class
);
2486 bt_put(event_header
);
2487 bt_put(packet_header
);
2488 bt_put(sequence_type
);
2489 bt_put(integer_type
);
2490 bt_put(event_header_type
);
2494 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2497 struct bt_ctf_trace
*trace
= NULL
;
2498 struct bt_ctf_clock
*clock
= NULL
;
2499 struct bt_ctf_stream_class
*stream_class
= NULL
;
2500 struct bt_ctf_stream
*stream
= NULL
,
2502 struct bt_ctf_event_class
*event_class
= NULL
;
2503 struct bt_ctf_event
*event
= NULL
;
2504 struct bt_ctf_field_type
*integer_type
= NULL
;
2505 struct bt_ctf_field
*integer
= NULL
;
2507 trace
= bt_ctf_writer_get_trace(writer
);
2509 diag("Failed to get trace from writer");
2514 clock
= bt_ctf_trace_get_clock(trace
, 0);
2516 diag("Failed to get clock from trace");
2521 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2522 if (!stream_class
) {
2523 diag("Failed to create stream class");
2528 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2530 diag("Failed to set stream class clock");
2534 event_class
= bt_ctf_event_class_create("some_event_class_name");
2535 integer_type
= bt_ctf_field_type_integer_create(32);
2536 if (!integer_type
) {
2537 diag("Failed to create integer field type");
2542 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2545 diag("Failed to add field to event class");
2549 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2552 diag("Failed to add event class to stream class");
2555 event
= bt_ctf_event_create(event_class
);
2557 diag("Failed to create event");
2562 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2564 diag("Failed to get integer field payload from event");
2569 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2571 diag("Failed to set integer field value");
2575 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2577 diag("Failed to create writer stream");
2582 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2583 "bt_ctf_event_get_stream handles NULL correctly");
2584 ok(bt_ctf_event_get_stream(event
) == NULL
,
2585 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2587 ret
= bt_ctf_stream_append_event(stream
, event
);
2589 diag("Failed to append event to stream");
2593 ret_stream
= bt_ctf_event_get_stream(event
);
2594 ok(ret_stream
== stream
,
2595 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2598 "Create an event before instanciating its associated stream");
2602 bt_put(stream_class
);
2603 bt_put(event_class
);
2605 bt_put(integer_type
);
2611 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2613 struct bt_ctf_event_class
*event_class
;
2615 event_class
= bt_ctf_event_class_create("Simple Event");
2616 assert(event_class
);
2617 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2618 "two event classes with the same name cannot cohabit within the same stream class");
2619 bt_put(event_class
);
2621 event_class
= bt_ctf_event_class_create("different name, ok");
2622 assert(event_class
);
2623 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2624 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2625 "two event classes with the same ID cannot cohabit within the same stream class");
2626 bt_put(event_class
);
2630 void test_trace_stream_class_clock(void)
2632 struct bt_ctf_trace
*trace
= NULL
;
2633 struct bt_ctf_stream_class
*sc1
= NULL
;
2634 struct bt_ctf_stream_class
*sc2
= NULL
;
2635 struct bt_ctf_clock
*sc1_clock
= NULL
;
2636 struct bt_ctf_clock
*sc2_clock
= NULL
;
2637 const char *clock_name
= "hello";
2639 trace
= bt_ctf_trace_create();
2641 sc1
= bt_ctf_stream_class_create(NULL
);
2643 sc2
= bt_ctf_stream_class_create(NULL
);
2645 sc1_clock
= bt_ctf_clock_create(clock_name
);
2647 sc2_clock
= bt_ctf_clock_create(clock_name
);
2650 ok(!bt_ctf_stream_class_set_clock(sc1
, sc1_clock
),
2651 "bt_ctf_stream_class_set_clock() succeeds for sc1");
2652 ok(!bt_ctf_stream_class_set_clock(sc2
, sc2_clock
),
2653 "bt_ctf_stream_class_set_clock() succeeds for sc2");
2654 ok(!bt_ctf_trace_add_stream_class(trace
, sc1
),
2655 "bt_ctf_trace_add_stream_class() succeeds with sc1");
2656 ok(bt_ctf_trace_add_stream_class(trace
, sc2
),
2657 "bt_ctf_trace_add_stream_class() fails with sc2 (different clock, same name)");
2667 struct bt_ctf_event_class
*create_minimal_event_class(void)
2669 struct bt_ctf_event_class
*ec
= NULL
;
2670 struct bt_ctf_field_type
*int_ft
= NULL
;
2673 int_ft
= bt_ctf_field_type_integer_create(23);
2675 ec
= bt_ctf_event_class_create("minimal");
2677 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2685 void test_create_writer_vs_non_writer_mode(void)
2688 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2689 const char *writer_stream_name
= "writer stream instance";
2690 struct bt_ctf_writer
*writer
= NULL
;
2691 struct bt_ctf_trace
*writer_trace
= NULL
;
2692 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2693 struct bt_ctf_stream
*writer_stream
= NULL
;
2694 struct bt_ctf_stream
*writer_stream2
= NULL
;
2695 struct bt_ctf_stream
*packet_stream
= NULL
;
2696 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2697 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2698 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2699 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2700 struct bt_ctf_event_class
*writer_ec
= NULL
;
2701 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2702 struct bt_ctf_event
*event
= NULL
;
2703 struct bt_ctf_event
*event2
= NULL
;
2704 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2705 struct bt_ctf_field
*int_field
= NULL
;
2706 struct bt_ctf_clock
*writer_clock
= NULL
;
2707 struct bt_ctf_clock
*non_writer_clock
= NULL
;
2708 struct bt_ctf_packet
*packet
= NULL
;
2709 struct bt_ctf_packet
*packet2
= NULL
;
2711 if (!bt_mkdtemp(trace_path
)) {
2715 /* Create empty structure field type (event header) */
2716 empty_struct_ft
= bt_ctf_field_type_structure_create();
2717 assert(empty_struct_ft
);
2719 /* Create writer, writer stream class, stream, and clock */
2720 writer
= bt_ctf_writer_create(trace_path
);
2722 writer_trace
= bt_ctf_writer_get_trace(writer
);
2723 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2724 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2726 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2729 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2731 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2732 assert(writer_stream
);
2733 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2734 "bt_ctf_stream_get_name() returns the stream's name");
2735 writer_clock
= bt_ctf_clock_create("writer_clock");
2736 assert(writer_clock
);
2737 ret
= bt_ctf_trace_add_clock(writer_trace
, writer_clock
);
2740 /* Create non-writer trace, stream class, stream, and clock */
2741 non_writer_trace
= bt_ctf_trace_create();
2742 assert(non_writer_trace
);
2743 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2744 assert(non_writer_sc
);
2745 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2748 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2750 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2751 assert(non_writer_stream
);
2752 non_writer_clock
= bt_ctf_clock_create("non_writer_clock");
2753 assert(non_writer_clock
);
2754 ret
= bt_ctf_trace_add_clock(non_writer_trace
, non_writer_clock
);
2757 /* Create event class and event */
2758 writer_ec
= create_minimal_event_class();
2760 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2762 event
= bt_ctf_event_create(writer_ec
);
2764 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2766 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2769 * Verify non-writer stream: it should be impossible to append
2772 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2773 "bt_ctf_stream_append_event() fails with a non-writer stream");
2776 * Verify writer stream: it should be possible to append an
2779 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2780 "bt_ctf_stream_append_event() succeeds with a writer stream");
2783 * Verify that it's possible to get and set the value of a
2784 * writer mode clock.
2786 ok (!bt_ctf_clock_set_value(writer_clock
, 1000),
2787 "bt_ctf_clock_set_value() succeeds with a writer mode clock");
2788 ok (bt_ctf_clock_get_value(writer_clock
) == 1000,
2789 "bt_ctf_clock_get_value() succeeds with a writer mode clock");
2792 * Verify that it's impossible to get and set the value of a
2793 * non-writer mode clock.
2795 ok (bt_ctf_clock_set_value(non_writer_clock
, 1000),
2796 "bt_ctf_clock_set_value() fails with a non-writer mode clock");
2797 ok (bt_ctf_clock_get_value(non_writer_clock
) == -1ULL,
2798 "bt_ctf_clock_get_value() fails with a non-writer mode clock");
2801 * It should be possible to create a packet from a non-writer
2802 * stream, but not from a writer stream.
2804 packet
= bt_ctf_packet_create(writer_stream
);
2805 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2806 packet
= bt_ctf_packet_create(non_writer_stream
);
2807 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2808 packet_stream
= bt_ctf_packet_get_stream(packet
);
2809 ok(packet_stream
== non_writer_stream
,
2810 "bt_ctf_packet_get_stream() returns the correct stream");
2813 * It should not be possible to append an event associated to
2814 * a stream to a different stream.
2816 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2817 assert(writer_stream2
);
2818 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2819 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2822 * It should not be possible to set the packet of an event
2823 * associated to a given stream to a packet associated with
2824 * a different stream.
2826 ok(bt_ctf_event_set_packet(event
, packet
),
2827 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2830 * It should be possible to set the packet of a fresh event, as
2831 * long as the originating stream classes are the same.
2833 event2
= bt_ctf_event_create(writer_ec
);
2835 ok(bt_ctf_event_set_packet(event2
, packet
),
2836 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2837 non_writer_ec
= create_minimal_event_class();
2838 assert(non_writer_ec
);
2839 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2842 event2
= bt_ctf_event_create(non_writer_ec
);
2844 ok(!bt_ctf_event_set_packet(event2
, packet
),
2845 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2848 * It should be possible to set a packet created from the same
2849 * stream to an event with an existing packet.
2851 packet2
= bt_ctf_packet_create(non_writer_stream
);
2853 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2854 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2858 * It should not be possible to set a packet created from a
2859 * different stream to an event with an existing packet.
2861 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2862 assert(non_writer_stream2
);
2863 packet2
= bt_ctf_packet_create(non_writer_stream
);
2865 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2866 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2869 bt_put(writer_trace
);
2871 bt_put(writer_stream
);
2872 bt_put(writer_stream2
);
2873 bt_put(non_writer_trace
);
2874 bt_put(non_writer_sc
);
2875 bt_put(non_writer_stream
);
2876 bt_put(non_writer_stream2
);
2877 bt_put(packet_stream
);
2879 bt_put(non_writer_ec
);
2883 bt_put(empty_struct_ft
);
2884 bt_put(writer_clock
);
2885 bt_put(non_writer_clock
);
2888 recursive_rmdir(trace_path
);
2892 void test_clock_utils(void)
2895 struct bt_ctf_clock
*clock
= NULL
;
2897 clock
= bt_ctf_clock_create("water");
2899 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2901 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2903 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2905 ok(bt_ctf_clock_ns_from_value(clock
, 4321) == 1234000005321ULL,
2906 "bt_ctf_clock_ns_from_value() returns the correct value with a 1 GHz frequency");
2907 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2909 ok(bt_ctf_clock_ns_from_value(clock
, 4321) ==
2910 (uint64_t) 1237468709256.845,
2911 "bt_ctf_clock_ns_from_value() returns the correct value with a non-1 GHz frequency");
2916 int main(int argc
, char **argv
)
2918 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2919 char metadata_path
[sizeof(trace_path
) + 9];
2920 const char *clock_name
= "test_clock";
2921 const char *clock_description
= "This is a test clock";
2922 const char *returned_clock_name
;
2923 const char *returned_clock_description
;
2924 const uint64_t frequency
= 1123456789;
2925 const int64_t offset_s
= 1351530929945824323;
2926 const int64_t offset
= 1234567;
2927 int64_t get_offset_s
,
2930 const uint64_t precision
= 10;
2931 const int is_absolute
= 0xFF;
2932 char *metadata_string
;
2933 struct bt_ctf_writer
*writer
;
2934 struct utsname name
;
2935 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2936 struct bt_ctf_clock
*clock
, *ret_clock
;
2937 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2938 struct bt_ctf_stream
*stream1
;
2939 const char *ret_string
;
2940 const unsigned char *ret_uuid
;
2941 unsigned char tmp_uuid
[16] = { 0 };
2942 struct bt_ctf_field_type
*packet_context_type
,
2943 *packet_context_field_type
,
2944 *packet_header_type
,
2945 *packet_header_field_type
,
2947 *stream_event_context_type
,
2949 *event_header_field_type
;
2950 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2951 struct bt_ctf_trace
*trace
;
2953 int64_t ret_int64_t
;
2954 struct bt_value
*obj
;
2957 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2961 plan_tests(NR_TESTS
);
2963 if (!bt_mkdtemp(trace_path
)) {
2967 strcpy(metadata_path
, trace_path
);
2968 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2970 writer
= bt_ctf_writer_create(trace_path
);
2971 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2973 ok(!bt_ctf_writer_get_trace(NULL
),
2974 "bt_ctf_writer_get_trace correctly handles NULL");
2975 trace
= bt_ctf_writer_get_trace(writer
);
2977 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2978 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2979 "Set a trace's byte order to big endian");
2980 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2981 "bt_ctf_trace_get_byte_order returns a correct endianness");
2983 /* Add environment context to the trace */
2984 ret
= gethostname(hostname
, sizeof(hostname
));
2988 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2989 "Add host (%s) environment field to writer instance",
2991 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2993 "bt_ctf_writer_add_environment_field error with NULL writer");
2994 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2996 "bt_ctf_writer_add_environment_field error with NULL field name");
2997 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2999 "bt_ctf_writer_add_environment_field error with NULL field value");
3001 /* Test bt_ctf_trace_set_environment_field with an integer object */
3002 obj
= bt_value_integer_create_init(23);
3004 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
3005 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
3006 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
3007 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
3008 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
3009 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
3010 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
3011 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
3014 /* Test bt_ctf_trace_set_environment_field with a string object */
3015 obj
= bt_value_string_create_init("the value");
3017 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
3018 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
3021 /* Test bt_ctf_trace_set_environment_field_integer */
3022 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
3024 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
3025 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
3026 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
3027 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3029 "bt_ctf_trace_set_environment_field_integer succeeds");
3031 /* Test bt_ctf_trace_set_environment_field_string */
3032 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
3034 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
3035 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
3036 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
3037 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3039 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
3040 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3042 "bt_ctf_trace_set_environment_field_string succeeds");
3044 /* Test bt_ctf_trace_get_environment_field_count */
3045 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
3046 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
3047 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3048 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
3050 /* Test bt_ctf_trace_get_environment_field_name */
3051 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
3052 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
3053 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
3054 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
3055 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
3056 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
3057 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
3058 ok(ret_string
&& !strcmp(ret_string
, "host"),
3059 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3060 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
3061 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
3062 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3063 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
3064 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
3065 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3066 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
3067 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
3068 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3069 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
3070 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
3071 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3073 /* Test bt_ctf_trace_get_environment_field_value */
3074 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
3075 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
3076 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
3077 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
3078 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
3079 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
3080 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
3081 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3082 ok(!ret
&& ret_int64_t
== 23,
3083 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
3085 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
3086 ret
= bt_value_string_get(obj
, &ret_string
);
3087 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
3088 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
3091 /* Test bt_ctf_trace_get_environment_field_value_by_name */
3092 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
3094 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3095 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3096 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3097 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3098 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3099 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3101 ret
= bt_value_string_get(obj
, &ret_string
);
3102 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3103 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3106 /* Test environment field replacement */
3107 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3109 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3110 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3111 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3112 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
3113 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3114 ok(!ret
&& ret_int64_t
== 654321,
3115 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3118 /* On Solaris, uname() can return any positive value on success */
3119 if (uname(&name
) < 0) {
3124 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3125 == 0, "Add sysname (%s) environment field to writer instance",
3127 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3128 name
.nodename
) == 0,
3129 "Add nodename (%s) environment field to writer instance",
3131 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3132 == 0, "Add release (%s) environment field to writer instance",
3134 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3135 == 0, "Add version (%s) environment field to writer instance",
3137 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3138 == 0, "Add machine (%s) environment field to writer istance",
3141 /* Define a clock and add it to the trace */
3142 ok(bt_ctf_clock_create("signed") == NULL
,
3143 "Illegal clock name rejected");
3144 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
3145 clock
= bt_ctf_clock_create(clock_name
);
3146 ok(clock
, "Clock created sucessfully");
3147 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3148 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3149 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3150 "Returned clock name is valid");
3152 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3153 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3154 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3155 "Clock description set successfully");
3157 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3158 ok(returned_clock_description
,
3159 "bt_ctf_clock_get_description returns a description.");
3160 ok(returned_clock_description
?
3161 !strcmp(returned_clock_description
, clock_description
) : 0,
3162 "Returned clock description is valid");
3164 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3165 "bt_ctf_clock_get_frequency returns the correct default frequency");
3166 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3167 "Set clock frequency");
3168 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3169 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3171 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3172 "bt_ctf_clock_get_offset_s succeeds");
3173 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3174 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3175 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3176 "Set clock offset (seconds)");
3177 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3178 "bt_ctf_clock_get_offset_s succeeds");
3179 ok(get_offset_s
== offset_s
,
3180 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3182 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3183 "bt_ctf_clock_get_offset succeeds");
3184 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3185 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3186 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3187 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3188 "bt_ctf_clock_get_offset succeeds");
3189 ok(get_offset
== offset
,
3190 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3192 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3193 "bt_ctf_clock_get_precision returns the correct default precision");
3194 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3195 "Set clock precision");
3196 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3197 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3199 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3200 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3201 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3202 "Set clock absolute property");
3203 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3204 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3206 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3207 "bt_ctf_clock_get_time succeeds");
3208 ok(get_time
== DEFAULT_CLOCK_TIME
,
3209 "bt_ctf_clock_get_time returns the correct default time");
3210 ok(bt_ctf_clock_get_value(clock
) == DEFAULT_CLOCK_VALUE
,
3211 "bt_ctf_clock_get_value returns the correct default value");
3212 ok(bt_ctf_clock_set_value(clock
, current_time
) == 0,
3214 ok(bt_ctf_clock_get_value(clock
) == current_time
,
3215 "bt_ctf_clock_get_value returns the correct value once it is set");
3216 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3218 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3219 "bt_ctf_clock_get_time succeeds");
3220 ok(get_time
>= current_time
- 1 && get_time
<= current_time
+ 1,
3221 "bt_ctf_clock_get_time returns the correct time once it is set");
3223 ok(!bt_ctf_clock_get_name(NULL
),
3224 "bt_ctf_clock_get_name correctly handles NULL");
3225 ok(!bt_ctf_clock_get_description(NULL
),
3226 "bt_ctf_clock_get_description correctly handles NULL");
3227 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3228 "bt_ctf_clock_get_frequency correctly handles NULL");
3229 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3230 "bt_ctf_clock_get_precision correctly handles NULL");
3231 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3232 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3233 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3234 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3235 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3236 "bt_ctf_clock_get_offset correctly handles NULL clock");
3237 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3238 "bt_ctf_clock_get_offset correctly handles NULL output");
3239 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3240 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3241 ok(bt_ctf_clock_get_time(NULL
, &get_time
) < 0,
3242 "bt_ctf_clock_get_time correctly handles NULL clock");
3243 ok(bt_ctf_clock_get_time(clock
, NULL
) < 0,
3244 "bt_ctf_clock_get_time correctly handles NULL output");
3246 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3247 "bt_ctf_clock_set_description correctly handles NULL clock");
3248 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3249 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3250 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3251 "bt_ctf_clock_get_precision correctly handles NULL clock");
3252 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3253 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3254 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3255 "bt_ctf_clock_set_offset correctly handles NULL clock");
3256 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3257 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3258 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3259 "bt_ctf_clock_set_time correctly handles NULL clock");
3260 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3261 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3262 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3264 "bt_ctf_clock_get_uuid returns a UUID");
3266 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3267 /* Slightly modify UUID */
3268 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3271 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3272 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3273 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3274 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3275 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3276 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3277 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3279 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3280 ok(uuid_match(ret_uuid
, tmp_uuid
),
3281 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3283 /* Define a stream class */
3284 stream_class
= bt_ctf_stream_class_create("test_stream");
3286 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3287 "bt_ctf_stream_class_get_name handles NULL correctly");
3288 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3289 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3290 "bt_ctf_stream_class_get_name returns a correct stream class name");
3292 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3293 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3294 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3295 "bt_ctf_stream_class_get_clock handles NULL correctly");
3297 ok(stream_class
, "Create stream class");
3298 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3299 "Set a stream class' clock");
3300 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3301 ok(ret_clock
== clock
,
3302 "bt_ctf_stream_class_get_clock returns a correct clock");
3305 /* Test the event fields and event types APIs */
3308 /* Test fields copying */
3311 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3312 "bt_ctf_stream_class_get_id returns an error when no id is set");
3313 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3314 "bt_ctf_stream_class_get_id handles NULL correctly");
3315 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3316 "bt_ctf_stream_class_set_id handles NULL correctly");
3317 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3318 "Set an stream class' id");
3319 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3320 "bt_ctf_stream_class_get_id returns the correct value");
3322 /* Validate default event header fields */
3323 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3324 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3325 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3328 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3329 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_TYPE_ID_STRUCT
,
3330 "Default event header type is a structure");
3331 event_header_field_type
=
3332 bt_ctf_field_type_structure_get_field_type_by_name(
3333 ret_field_type
, "id");
3334 ok(event_header_field_type
,
3335 "Default event header type contains an \"id\" field");
3336 ok(bt_ctf_field_type_get_type_id(
3337 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3338 "Default event header \"id\" field is an integer");
3339 bt_put(event_header_field_type
);
3340 event_header_field_type
=
3341 bt_ctf_field_type_structure_get_field_type_by_name(
3342 ret_field_type
, "timestamp");
3343 ok(event_header_field_type
,
3344 "Default event header type contains a \"timestamp\" field");
3345 ok(bt_ctf_field_type_get_type_id(
3346 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3347 "Default event header \"timestamp\" field is an integer");
3348 bt_put(event_header_field_type
);
3349 bt_put(ret_field_type
);
3351 /* Add a custom trace packet header field */
3352 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3353 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3354 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3355 ok(packet_header_type
,
3356 "bt_ctf_trace_get_packet_header_type returns a packet header");
3357 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_TYPE_ID_STRUCT
,
3358 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3359 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3360 packet_header_type
, "magic");
3361 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3362 bt_put(ret_field_type
);
3363 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3364 packet_header_type
, "uuid");
3365 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3366 bt_put(ret_field_type
);
3367 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3368 packet_header_type
, "stream_id");
3369 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3370 bt_put(ret_field_type
);
3372 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3373 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3374 packet_header_field_type
, "custom_trace_packet_header_field"),
3375 "Added a custom trace packet header field successfully");
3377 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3378 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3379 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3380 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3381 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3382 "Set a trace packet_header_type successfully");
3384 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3385 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3387 /* Add a custom field to the stream class' packet context */
3388 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3389 ok(packet_context_type
,
3390 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3391 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_TYPE_ID_STRUCT
,
3392 "Packet context is a structure");
3394 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3395 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3396 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3397 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3399 integer_type
= bt_ctf_field_type_integer_create(32);
3400 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3402 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3403 /* Create a "uint5_t" equivalent custom packet context field */
3404 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3406 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3407 packet_context_field_type
, "custom_packet_context_field");
3408 ok(ret
== 0, "Packet context field added successfully");
3410 /* Define a stream event context containing a my_integer field. */
3411 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3412 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3413 ok(bt_ctf_stream_class_get_event_context_type(
3414 stream_class
) == NULL
,
3415 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3416 stream_event_context_type
= bt_ctf_field_type_structure_create();
3417 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3418 integer_type
, "common_event_context");
3420 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3421 stream_event_context_type
) < 0,
3422 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3423 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3425 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3426 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3428 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3430 ok(bt_ctf_stream_class_set_event_context_type(
3431 stream_class
, stream_event_context_type
) == 0,
3432 "Set a new stream event context type");
3433 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3435 ok(ret_field_type
== stream_event_context_type
,
3436 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3437 bt_put(ret_field_type
);
3439 /* Instantiate a stream and append events */
3440 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3441 ok(stream1
, "Instanciate a stream class from writer");
3444 * Creating a stream through a writer adds the given stream
3445 * class to the writer's trace, thus registering the stream
3446 * class's clock to the trace.
3448 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
3449 "bt_ctf_trace_get_clock_count correctly handles NULL");
3450 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
3451 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3452 ok(!bt_ctf_trace_get_clock(NULL
, 0),
3453 "bt_ctf_trace_get_clock correctly handles NULL");
3454 ok(!bt_ctf_trace_get_clock(trace
, -1),
3455 "bt_ctf_trace_get_clock correctly handles negative indexes");
3456 ok(!bt_ctf_trace_get_clock(trace
, 1),
3457 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3458 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
3459 ok(ret_clock
== clock
,
3460 "bt_ctf_trace_get_clock returns the right clock instance");
3462 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
3463 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3464 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
3465 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3466 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
3467 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3468 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
3469 ok(ret_clock
== clock
,
3470 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3472 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
3473 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3475 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3476 "bt_ctf_stream_get_class correctly handles NULL");
3477 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3478 ok(ret_stream_class
,
3479 "bt_ctf_stream_get_class returns a stream class");
3480 ok(ret_stream_class
== stream_class
,
3481 "Returned stream class is of the correct type");
3484 * Packet header, packet context, event header, and stream
3485 * event context types were copied for the resolving
3488 BT_PUT(packet_header_type
);
3489 BT_PUT(packet_context_type
);
3490 BT_PUT(stream_event_context_type
);
3491 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3492 assert(packet_header_type
);
3493 packet_context_type
=
3494 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3495 assert(packet_context_type
);
3496 stream_event_context_type
=
3497 bt_ctf_stream_class_get_event_context_type(stream_class
);
3498 assert(stream_event_context_type
);
3501 * Try to modify the packet context type after a stream has been
3504 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3505 packet_header_field_type
, "should_fail");
3507 "Trace packet header type can't be modified once a stream has been instanciated");
3510 * Try to modify the packet context type after a stream has been
3513 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3514 packet_context_field_type
, "should_fail");
3516 "Packet context type can't be modified once a stream has been instanciated");
3519 * Try to modify the stream event context type after a stream has been
3522 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3523 integer_type
, "should_fail");
3525 "Stream event context type can't be modified once a stream has been instanciated");
3527 /* Should fail after instanciating a stream (frozen) */
3528 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3529 "Changes to a stream class that was already instantiated fail");
3531 /* Populate the custom packet header field only once for all tests */
3532 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3533 "bt_ctf_stream_get_packet_header handles NULL correctly");
3534 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3536 "bt_ctf_stream_get_packet_header returns a packet header");
3537 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3538 ok(ret_field_type
== packet_header_type
,
3539 "Stream returns a packet header of the appropriate type");
3540 bt_put(ret_field_type
);
3541 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3542 "custom_trace_packet_header_field");
3543 ok(packet_header_field
,
3544 "Packet header structure contains a custom field with the appropriate name");
3545 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3546 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3547 "Custom packet header field is of the expected type");
3548 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3549 54321), "Set custom packet header value successfully");
3550 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3551 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3552 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3553 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3554 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3555 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3556 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3557 "Successfully set a stream's packet header");
3559 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3560 "Add environment field to writer after stream creation");
3564 test_trace_stream_class_clock();
3566 test_create_writer_vs_non_writer_mode();
3568 test_instanciate_event_before_stream(writer
);
3570 append_simple_event(stream_class
, stream1
, clock
);
3572 packet_resize_test(stream_class
, stream1
, clock
);
3574 append_complex_event(stream_class
, stream1
, clock
);
3576 append_existing_event_class(stream_class
);
3578 test_empty_stream(writer
);
3580 test_custom_event_header_stream(writer
);
3582 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3583 ok(metadata_string
, "Get metadata string");
3585 bt_ctf_writer_flush_metadata(writer
);
3586 validate_metadata(argv
[1], metadata_path
);
3587 validate_trace(argv
[2], trace_path
);
3590 bt_put(ret_stream_class
);
3593 bt_put(packet_context_type
);
3594 bt_put(packet_context_field_type
);
3595 bt_put(integer_type
);
3596 bt_put(stream_event_context_type
);
3597 bt_put(ret_field_type
);
3598 bt_put(packet_header_type
);
3599 bt_put(packet_header_field_type
);
3600 bt_put(packet_header
);
3601 bt_put(packet_header_field
);
3603 free(metadata_string
);
3604 bt_put(stream_class
);
3606 recursive_rmdir(trace_path
);