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 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
497 "Adding simple event class to stream class");
500 * bt_ctf_stream_class_add_event_class() copies the field types
501 * of simple_event_class, so we retrieve the new ones to create
502 * the appropriate fields.
504 BT_PUT(event_context_type
);
505 BT_PUT(event_payload_type
);
506 event_payload_type
= bt_ctf_event_class_get_payload_type(
508 assert(event_payload_type
);
509 event_context_type
= bt_ctf_event_class_get_context_type(
511 assert(event_context_type
);
512 ep_integer_field_type
=
513 bt_ctf_field_type_structure_get_field_type_by_name(
514 event_payload_type
, "integer_field");
515 assert(ep_integer_field_type
);
517 bt_ctf_field_type_structure_get_field_type_by_name(
518 event_payload_type
, "enum_field");
519 assert(ep_enum_field_type
);
520 ep_enum_field_unsigned_type
=
521 bt_ctf_field_type_structure_get_field_type_by_name(
522 event_payload_type
, "enum_field_unsigned");
523 assert(ep_enum_field_unsigned_type
);
525 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
526 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
527 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
528 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
529 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
530 "bt_ctf_stream_class_get_event_class handles NULL correctly");
531 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
532 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
533 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
534 ok(ret_event_class
== simple_event_class
,
535 "bt_ctf_stream_class_get_event_class returns the correct event class");
536 bt_put(ret_event_class
);
537 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
538 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
539 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
540 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
542 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
543 ok(ret_event_class
== simple_event_class
,
544 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
545 bt_put(ret_event_class
);
547 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
548 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
549 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
550 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
551 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
552 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
553 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
554 ok(ret_event_class
== simple_event_class
,
555 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
556 bt_put(ret_event_class
);
558 simple_event
= bt_ctf_event_create(simple_event_class
);
560 "Instantiate an event containing a single integer field");
562 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
563 "bt_ctf_event_get_clock handles NULL correctly");
564 ret_clock
= bt_ctf_event_get_clock(simple_event
);
565 ok(ret_clock
== clock
,
566 "bt_ctf_event_get_clock returns a correct clock");
569 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
570 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
571 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
572 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
574 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
575 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
576 "bt_ctf_field_floating_point_get_value fails on an unset float field");
577 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
578 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
579 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
580 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
581 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
582 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
583 "bt_ctf_field_floating_point_get_value returns a double value");
584 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
585 "bt_ctf_field_floating_point_get_value returns a correct value");
587 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
589 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
590 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
591 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
592 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
593 enum_container_field
= bt_ctf_field_enumeration_get_container(
595 ok(bt_ctf_field_signed_integer_set_value(
596 enum_container_field
, -42) == 0,
597 "Set signed enumeration container value");
598 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
599 ok(!strcmp(ret_char
, mapping_name_negative_test
),
600 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
601 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
604 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
605 assert(enum_field_unsigned
);
606 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
607 enum_field_unsigned
);
608 ok(bt_ctf_field_unsigned_integer_set_value(
609 enum_container_field_unsigned
, 42) == 0,
610 "Set unsigned enumeration container value");
611 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
612 enum_field_unsigned
);
614 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
615 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
616 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
618 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
620 /* Populate stream event context */
621 stream_event_context
=
622 bt_ctf_event_get_stream_event_context(simple_event
);
623 assert(stream_event_context
);
624 stream_event_context_field
= bt_ctf_field_structure_get_field(
625 stream_event_context
, "common_event_context");
626 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
628 /* Populate the event's context */
629 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
630 "bt_ctf_event_get_event_context handles NULL correctly");
631 event_context
= bt_ctf_event_get_event_context(simple_event
);
633 "bt_ctf_event_get_event_context returns a field");
634 returned_type
= bt_ctf_field_get_type(event_context
);
635 ok(returned_type
== event_context_type
,
636 "bt_ctf_event_get_event_context returns a field of the appropriate type");
637 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
638 "event_specific_context");
639 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
640 "Successfully set an event context's value");
641 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
642 "bt_ctf_event_set_event_context handles a NULL event correctly");
643 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
644 "bt_ctf_event_set_event_context handles a NULL event context correctly");
645 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
646 "bt_ctf_event_set_event_context rejects a context of the wrong type");
647 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
648 "Set an event context successfully");
650 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
651 "Append simple event to trace stream");
653 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
654 "bt_ctf_stream_get_packet_context handles NULL correctly");
655 packet_context
= bt_ctf_stream_get_packet_context(stream
);
657 "bt_ctf_stream_get_packet_context returns a packet context");
659 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
661 ok(packet_context_field
,
662 "Packet context contains the default packet_size field.");
663 bt_put(packet_context_field
);
664 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
665 "custom_packet_context_field");
666 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
667 "Custom packet context field value successfully set.");
669 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
670 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
671 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
672 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
673 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
674 "Successfully set a stream's packet context");
676 ok(bt_ctf_stream_flush(stream
) == 0,
677 "Flush trace stream with one event");
679 bt_put(simple_event_class
);
680 bt_put(simple_event
);
681 bt_put(uint_12_type
);
685 bt_put(enum_type_unsigned
);
686 bt_put(returned_type
);
687 bt_put(event_context_type
);
688 bt_put(integer_field
);
691 bt_put(enum_field_unsigned
);
692 bt_put(enum_container_field
);
693 bt_put(enum_container_field_unsigned
);
694 bt_put(packet_context
);
695 bt_put(packet_context_field
);
696 bt_put(stream_event_context
);
697 bt_put(stream_event_context_field
);
698 bt_put(event_context
);
699 bt_put(event_context_field
);
700 bt_put(event_payload_type
);
701 bt_put(ep_integer_field_type
);
702 bt_put(ep_enum_field_type
);
703 bt_put(ep_enum_field_unsigned_type
);
707 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
708 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
710 struct event_class_attrs_counts
{
721 struct event_class_attrs_counts
;
722 const char *complex_test_event_string
= "Complex Test Event";
723 const char *test_string_1
= "Test ";
724 const char *test_string_2
= "string ";
725 const char *test_string_3
= "abcdefghi";
726 const char *test_string_4
= "abcd\0efg\0hi";
727 const char *test_string_cat
= "Test string abcdeabcd";
728 struct bt_ctf_field_type
*uint_35_type
=
729 bt_ctf_field_type_integer_create(35);
730 struct bt_ctf_field_type
*int_16_type
=
731 bt_ctf_field_type_integer_create(16);
732 struct bt_ctf_field_type
*uint_3_type
=
733 bt_ctf_field_type_integer_create(3);
734 struct bt_ctf_field_type
*enum_variant_type
=
735 bt_ctf_field_type_enumeration_create(uint_3_type
);
736 struct bt_ctf_field_type
*variant_type
=
737 bt_ctf_field_type_variant_create(enum_variant_type
,
739 struct bt_ctf_field_type
*string_type
=
740 bt_ctf_field_type_string_create();
741 struct bt_ctf_field_type
*sequence_type
;
742 struct bt_ctf_field_type
*array_type
;
743 struct bt_ctf_field_type
*inner_structure_type
=
744 bt_ctf_field_type_structure_create();
745 struct bt_ctf_field_type
*complex_structure_type
=
746 bt_ctf_field_type_structure_create();
747 struct bt_ctf_field_type
*ret_field_type
;
748 struct bt_ctf_event_class
*event_class
;
749 struct bt_ctf_event
*event
;
750 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
751 *inner_structure_field
, *complex_structure_field
,
752 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
753 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
754 *stream_event_ctx_int_field
, *ret_field
;
755 uint64_t ret_unsigned_int
;
756 int64_t ret_signed_int
;
757 const char *ret_string
;
758 struct bt_ctf_stream_class
*ret_stream_class
;
759 struct bt_ctf_event_class
*ret_event_class
;
760 struct bt_ctf_field
*packet_context
, *packet_context_field
;
761 struct bt_value
*obj
;
763 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
764 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
765 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
766 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
767 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
768 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
769 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
770 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
771 bt_ctf_field_type_set_alignment(int_16_type
, 32);
772 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
773 bt_ctf_field_type_integer_set_base(uint_35_type
,
774 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
776 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
777 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
780 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
781 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
782 ret_field_type
= bt_ctf_field_type_array_get_element_type(
784 ok(ret_field_type
== int_16_type
,
785 "bt_ctf_field_type_array_get_element_type returns the correct type");
786 bt_put(ret_field_type
);
788 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
789 "bt_ctf_field_type_array_get_length handles NULL correctly");
790 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
791 "bt_ctf_field_type_array_get_length returns the correct length");
793 bt_ctf_field_type_structure_add_field(inner_structure_type
,
794 uint_35_type
, "seq_len");
795 bt_ctf_field_type_structure_add_field(inner_structure_type
,
796 sequence_type
, "a_sequence");
797 bt_ctf_field_type_structure_add_field(inner_structure_type
,
798 array_type
, "an_array");
800 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
802 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
804 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
805 "UINT35_TYPE", 2, 7);
807 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
809 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
810 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
811 enum_variant_type
, NULL
) < 0,
812 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
813 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
814 enum_variant_type
, "INT16_TYPE") == 1,
815 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
817 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
818 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
819 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
820 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
821 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
822 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
824 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
825 "An unknown entry"), "Reject a variant field based on an unknown tag value");
826 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
827 "UINT3_TYPE") == 0, "Add a field to a variant");
828 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
830 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
833 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
834 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
835 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
836 ok(ret_field_type
== enum_variant_type
,
837 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
838 bt_put(ret_field_type
);
840 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
841 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
842 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
843 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
844 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
845 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
846 "INT16_TYPE") == NULL
,
847 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
848 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
850 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
851 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
852 variant_type
, "INT16_TYPE");
853 ok(ret_field_type
== int_16_type
,
854 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
855 bt_put(ret_field_type
);
857 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
858 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
859 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
860 "bt_ctf_field_type_variant_get_field_count returns the correct count");
862 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
863 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
864 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
865 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
866 bt_put(ret_field_type
);
867 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
868 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
869 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
870 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
871 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
872 "bt_ctf_field_type_variant_get_field returns a field");
873 ok(!strcmp("INT16_TYPE", ret_string
),
874 "bt_ctf_field_type_variant_get_field returns a correct field name");
875 ok(ret_field_type
== int_16_type
,
876 "bt_ctf_field_type_variant_get_field returns a correct field type");
877 bt_put(ret_field_type
);
879 bt_ctf_field_type_structure_add_field(complex_structure_type
,
880 enum_variant_type
, "variant_selector");
881 bt_ctf_field_type_structure_add_field(complex_structure_type
,
882 string_type
, "a_string");
883 bt_ctf_field_type_structure_add_field(complex_structure_type
,
884 variant_type
, "variant_value");
885 bt_ctf_field_type_structure_add_field(complex_structure_type
,
886 inner_structure_type
, "inner_structure");
888 ok(bt_ctf_event_class_create("clock") == NULL
,
889 "Reject creation of an event class with an illegal name");
890 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
891 ok(event_class
, "Create an event class");
892 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
893 "Reject addition of a field with an empty name to an event");
894 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
895 "Reject addition of a field with a NULL type to an event");
896 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
898 "Reject addition of a type with an illegal name to an event");
899 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
901 "Add field of type unsigned integer to an event");
902 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
903 "int_16") == 0, "Add field of type signed integer to an event");
904 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
905 "complex_structure") == 0,
906 "Add composite structure to an event");
908 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
909 "bt_ctf_event_class_get_name handles NULL correctly");
910 ret_string
= bt_ctf_event_class_get_name(event_class
);
911 ok(!strcmp(ret_string
, complex_test_event_string
),
912 "bt_ctf_event_class_get_name returns a correct name");
913 ok(bt_ctf_event_class_get_id(event_class
) < 0,
914 "bt_ctf_event_class_get_id returns a negative value when not set");
915 ok(bt_ctf_event_class_get_id(NULL
) < 0,
916 "bt_ctf_event_class_get_id handles NULL correctly");
917 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
918 "bt_ctf_event_class_set_id handles NULL correctly");
919 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
920 "Set an event class' id");
921 ok(bt_ctf_event_class_get_id(event_class
) == 42,
922 "bt_ctf_event_class_get_id returns the correct value");
924 /* Test event class attributes */
925 obj
= bt_value_integer_create_init(15);
927 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
928 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
929 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
930 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
931 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
932 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
933 assert(!bt_value_integer_set(obj
, -3));
934 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
935 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
936 assert(!bt_value_integer_set(obj
, 11));
937 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
938 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
939 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
940 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
941 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
943 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
946 obj
= bt_value_integer_create_init(5);
948 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
949 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
951 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
952 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
953 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
954 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
955 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
956 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
957 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
960 ret
= bt_value_integer_get(obj
, &int64_value
);
961 ok(obj
&& !ret
&& int64_value
== 5,
962 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
965 obj
= bt_value_string_create_init("nu name");
967 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
968 ret_string
= bt_ctf_event_class_get_name(event_class
);
969 ok(!strcmp(ret_string
, "nu name"),
970 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
971 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
972 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
974 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
976 obj
= bt_value_string_create_init("http://kernel.org/");
978 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
981 ok(bt_ctf_event_class_get_attribute_count(NULL
),
982 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
983 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
984 "bt_ctf_event_class_get_attribute_count returns the correct count");
985 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
986 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
987 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
988 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
989 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
990 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
991 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
992 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
994 memset(&attrs_count
, 0, sizeof(attrs_count
));
996 for (i
= 0; i
< 4; ++i
) {
997 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
999 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
1000 assert(ret_string
&& obj
);
1002 if (!strcmp(ret_string
, "id")) {
1004 ok(bt_value_is_integer(obj
),
1005 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1007 } else if (!strcmp(ret_string
, "name")) {
1009 ok(bt_value_is_string(obj
),
1010 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1012 } else if (!strcmp(ret_string
, "loglevel")) {
1013 attrs_count
.loglevel
++;
1014 ok(bt_value_is_integer(obj
),
1015 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1017 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1018 attrs_count
.modelemfuri
++;
1019 ok(bt_value_is_string(obj
),
1020 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1023 attrs_count
.unknown
++;
1029 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1030 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1031 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1032 "event class has one instance of each known attribute");
1034 /* Add event class to the stream class */
1035 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1036 "Reject addition of NULL event class to a stream class");
1037 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1038 event_class
) == 0, "Add an event class to stream class");
1040 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1041 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1042 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1043 ok(ret_stream_class
== stream_class
,
1044 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1045 bt_put(ret_stream_class
);
1047 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1048 "bt_ctf_event_class_get_field_count handles NULL correctly");
1049 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1050 "bt_ctf_event_class_get_field_count returns a correct value");
1052 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1053 &ret_field_type
, 0) < 0,
1054 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1055 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1056 &ret_field_type
, 0) == 0,
1057 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1058 bt_put(ret_field_type
);
1059 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1061 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1062 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1063 &ret_field_type
, 42) < 0,
1064 "bt_ctf_event_class_get_field handles an invalid index correctly");
1065 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1066 &ret_field_type
, 0) == 0,
1067 "bt_ctf_event_class_get_field returns a field");
1068 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
1069 "bt_ctf_event_class_get_field returns a correct field type");
1070 bt_put(ret_field_type
);
1071 ok(!strcmp(ret_string
, "uint_35"),
1072 "bt_ctf_event_class_get_field returns a correct field name");
1073 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1074 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1075 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1076 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1077 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1078 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1079 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1080 "complex_structure");
1081 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
1082 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1083 bt_put(ret_field_type
);
1085 event
= bt_ctf_event_create(event_class
);
1086 ok(event
, "Instanciate a complex event");
1088 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1089 "bt_ctf_event_get_class handles NULL correctly");
1090 ret_event_class
= bt_ctf_event_get_class(event
);
1091 ok(ret_event_class
== event_class
,
1092 "bt_ctf_event_get_class returns the correct event class");
1093 bt_put(ret_event_class
);
1095 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1096 if (!uint_35_field
) {
1097 printf("uint_35_field is NULL\n");
1100 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1101 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1102 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1103 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1104 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1105 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1106 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1107 &ret_unsigned_int
) == 0,
1108 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1109 ok(ret_unsigned_int
== 0x0DDF00D,
1110 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1111 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1112 &ret_signed_int
) < 0,
1113 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1114 bt_put(uint_35_field
);
1116 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1117 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1118 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1119 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1120 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1121 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1122 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1123 &ret_signed_int
) == 0,
1124 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1125 ok(ret_signed_int
== -12345,
1126 "bt_ctf_field_signed_integer_get_value returns the correct value");
1127 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1128 &ret_unsigned_int
) < 0,
1129 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1130 bt_put(int_16_field
);
1132 complex_structure_field
= bt_ctf_event_get_payload(event
,
1133 "complex_structure");
1135 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1136 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1137 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1138 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1139 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1140 complex_structure_field
, 3);
1141 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1142 bt_put(inner_structure_field
);
1143 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1144 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1145 bt_put(ret_field_type
);
1147 inner_structure_field
= bt_ctf_field_structure_get_field(
1148 complex_structure_field
, "inner_structure");
1149 a_string_field
= bt_ctf_field_structure_get_field(
1150 complex_structure_field
, "a_string");
1151 enum_variant_field
= bt_ctf_field_structure_get_field(
1152 complex_structure_field
, "variant_selector");
1153 variant_field
= bt_ctf_field_structure_get_field(
1154 complex_structure_field
, "variant_value");
1155 uint_35_field
= bt_ctf_field_structure_get_field(
1156 inner_structure_field
, "seq_len");
1157 a_sequence_field
= bt_ctf_field_structure_get_field(
1158 inner_structure_field
, "a_sequence");
1159 an_array_field
= bt_ctf_field_structure_get_field(
1160 inner_structure_field
, "an_array");
1162 enum_container_field
= bt_ctf_field_enumeration_get_container(
1163 enum_variant_field
);
1164 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1165 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1166 enum_variant_field
);
1167 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1168 bt_put(int_16_field
);
1169 ok(!bt_ctf_field_string_get_value(a_string_field
),
1170 "bt_ctf_field_string_get_value returns NULL on an unset field");
1171 bt_ctf_field_string_set_value(a_string_field
,
1173 ok(!bt_ctf_field_string_get_value(NULL
),
1174 "bt_ctf_field_string_get_value correctly handles NULL");
1175 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1176 "bt_ctf_field_string_append correctly handles a NULL string field");
1177 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1178 "bt_ctf_field_string_append correctly handles a NULL string value");
1179 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1180 "bt_ctf_field_string_append succeeds");
1181 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1182 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1183 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1184 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1185 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1186 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1187 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1188 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1189 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1190 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1191 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1192 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1194 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1195 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1196 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1197 "bt_ctf_field_string_get_value returns a correct value");
1198 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1199 SEQUENCE_TEST_LENGTH
);
1201 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1202 enum_container_field
) == NULL
,
1203 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1204 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1206 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1207 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1208 variant_type
, enum_variant_field
);
1209 ok(ret_field_type
== int_16_type
,
1210 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1212 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1213 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1214 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1215 uint_35_field
) == 0, "Set a sequence field's length");
1216 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1217 ok(ret_field
== uint_35_field
,
1218 "bt_ctf_field_sequence_get_length returns the correct length field");
1219 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1220 "bt_ctf_field_sequence_get_length properly handles NULL");
1222 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1223 int_16_field
= bt_ctf_field_sequence_get_field(
1224 a_sequence_field
, i
);
1225 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1226 bt_put(int_16_field
);
1229 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1230 int_16_field
= bt_ctf_field_array_get_field(
1232 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1233 bt_put(int_16_field
);
1236 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1237 assert(stream_event_ctx_field
);
1238 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1239 stream_event_ctx_field
, "common_event_context");
1240 BT_PUT(stream_event_ctx_field
);
1241 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1242 BT_PUT(stream_event_ctx_int_field
);
1244 bt_ctf_clock_set_time(clock
, ++current_time
);
1245 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1246 "Append a complex event to a stream");
1249 * Populate the custom packet context field with a dummy value
1250 * otherwise flush will fail.
1252 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1253 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1254 "custom_packet_context_field");
1255 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1257 ok(bt_ctf_stream_flush(stream
) == 0,
1258 "Flush a stream containing a complex event");
1260 bt_put(uint_35_field
);
1261 bt_put(a_string_field
);
1262 bt_put(inner_structure_field
);
1263 bt_put(complex_structure_field
);
1264 bt_put(a_sequence_field
);
1265 bt_put(an_array_field
);
1266 bt_put(enum_variant_field
);
1267 bt_put(enum_container_field
);
1268 bt_put(variant_field
);
1270 bt_put(packet_context_field
);
1271 bt_put(packet_context
);
1272 bt_put(uint_35_type
);
1273 bt_put(int_16_type
);
1274 bt_put(string_type
);
1275 bt_put(sequence_type
);
1277 bt_put(inner_structure_type
);
1278 bt_put(complex_structure_type
);
1279 bt_put(uint_3_type
);
1280 bt_put(enum_variant_type
);
1281 bt_put(variant_type
);
1282 bt_put(ret_field_type
);
1283 bt_put(event_class
);
1288 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1289 struct bt_ctf_field_type
*expected_type
, const char *name
)
1291 struct bt_ctf_field_type
*copy_type
;
1293 copy_type
= bt_ctf_field_get_type(field
);
1294 ok(copy_type
== expected_type
,
1295 "bt_ctf_field_copy does not copy the type (%s)", name
);
1300 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1301 struct bt_ctf_field
*field_b
, const char *name
)
1303 ok(field_a
!= field_b
,
1304 "bt_ctf_field_copy creates different pointers (%s)", name
);
1308 void field_copy_tests()
1310 struct bt_ctf_field_type
*len_type
= NULL
;
1311 struct bt_ctf_field_type
*fp_type
= NULL
;
1312 struct bt_ctf_field_type
*s_type
= NULL
;
1313 struct bt_ctf_field_type
*e_int_type
= NULL
;
1314 struct bt_ctf_field_type
*e_type
= NULL
;
1315 struct bt_ctf_field_type
*v_type
= NULL
;
1316 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1317 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1318 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1319 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1320 struct bt_ctf_field_type
*strct_type
= NULL
;
1321 struct bt_ctf_field
*len
= NULL
;
1322 struct bt_ctf_field
*fp
= NULL
;
1323 struct bt_ctf_field
*s
= NULL
;
1324 struct bt_ctf_field
*e_int
= NULL
;
1325 struct bt_ctf_field
*e
= NULL
;
1326 struct bt_ctf_field
*v
= NULL
;
1327 struct bt_ctf_field
*v_selected
= NULL
;
1328 struct bt_ctf_field
*v_selected_cur
= NULL
;
1329 struct bt_ctf_field
*v_selected_0
= NULL
;
1330 struct bt_ctf_field
*v_selected_1
= NULL
;
1331 struct bt_ctf_field
*v_selected_2
= NULL
;
1332 struct bt_ctf_field
*v_selected_3
= NULL
;
1333 struct bt_ctf_field
*v_selected_4
= NULL
;
1334 struct bt_ctf_field
*v_selected_5
= NULL
;
1335 struct bt_ctf_field
*v_selected_6
= NULL
;
1336 struct bt_ctf_field
*a
= NULL
;
1337 struct bt_ctf_field
*a_0
= NULL
;
1338 struct bt_ctf_field
*a_1
= NULL
;
1339 struct bt_ctf_field
*a_2
= NULL
;
1340 struct bt_ctf_field
*a_3
= NULL
;
1341 struct bt_ctf_field
*a_4
= NULL
;
1342 struct bt_ctf_field
*strct
= NULL
;
1343 struct bt_ctf_field
*len_copy
= NULL
;
1344 struct bt_ctf_field
*fp_copy
= NULL
;
1345 struct bt_ctf_field
*s_copy
= NULL
;
1346 struct bt_ctf_field
*e_int_copy
= NULL
;
1347 struct bt_ctf_field
*e_copy
= NULL
;
1348 struct bt_ctf_field
*v_copy
= NULL
;
1349 struct bt_ctf_field
*v_selected_copy
= NULL
;
1350 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1351 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1352 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1353 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1354 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1355 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1356 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1357 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1358 struct bt_ctf_field
*a_copy
= NULL
;
1359 struct bt_ctf_field
*a_0_copy
= NULL
;
1360 struct bt_ctf_field
*a_1_copy
= NULL
;
1361 struct bt_ctf_field
*a_2_copy
= NULL
;
1362 struct bt_ctf_field
*a_3_copy
= NULL
;
1363 struct bt_ctf_field
*a_4_copy
= NULL
;
1364 struct bt_ctf_field
*strct_copy
= NULL
;
1365 uint64_t uint64_t_val
;
1366 const char *str_val
;
1370 /* create len type */
1371 len_type
= bt_ctf_field_type_integer_create(32);
1374 /* create fp type */
1375 fp_type
= bt_ctf_field_type_floating_point_create();
1379 s_type
= bt_ctf_field_type_string_create();
1382 /* create e_int type */
1383 e_int_type
= bt_ctf_field_type_integer_create(8);
1387 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1389 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1392 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1396 /* create v_label1 type */
1397 v_label1_type
= bt_ctf_field_type_string_create();
1398 assert(v_label1_type
);
1400 /* create v_label1_array type */
1401 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1402 assert(v_label1_array_type
);
1404 /* create v_label2 type */
1405 v_label2_type
= bt_ctf_field_type_integer_create(16);
1406 assert(v_label2_type
);
1408 /* create v_label2_seq type */
1409 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1411 assert(v_label2_seq_type
);
1414 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1416 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1419 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1423 /* create strct type */
1424 strct_type
= bt_ctf_field_type_structure_create();
1426 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1429 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1431 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1433 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1435 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1437 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1438 v_label1_array_type
, "a");
1442 strct
= bt_ctf_field_create(strct_type
);
1446 len
= bt_ctf_field_structure_get_field(strct
, "len");
1450 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1454 s
= bt_ctf_field_structure_get_field(strct
, "s");
1458 e
= bt_ctf_field_structure_get_field(strct
, "e");
1461 /* get e_int (underlying integer) */
1462 e_int
= bt_ctf_field_enumeration_get_container(e
);
1466 v
= bt_ctf_field_structure_get_field(strct
, "v");
1470 a
= bt_ctf_field_structure_get_field(strct
, "a");
1474 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1478 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1482 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1485 /* set e field (LABEL2) */
1486 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1490 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1492 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1493 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1494 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1495 ok(v_selected_cur
== v_selected
,
1496 "bt_ctf_field_variant_get_current_field returns the current field");
1497 bt_put(v_selected_cur
);
1499 /* set selected v field */
1500 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1502 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1503 assert(v_selected_0
);
1504 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1506 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1507 assert(v_selected_1
);
1508 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1510 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1511 assert(v_selected_2
);
1512 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1514 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1515 assert(v_selected_3
);
1516 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1518 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1519 assert(v_selected_4
);
1520 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1522 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1523 assert(v_selected_5
);
1524 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1526 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1527 assert(v_selected_6
);
1528 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1532 a_0
= bt_ctf_field_array_get_field(a
, 0);
1534 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1536 a_1
= bt_ctf_field_array_get_field(a
, 1);
1538 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1540 a_2
= bt_ctf_field_array_get_field(a
, 2);
1542 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1544 a_3
= bt_ctf_field_array_get_field(a
, 3);
1546 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1548 a_4
= bt_ctf_field_array_get_field(a
, 4);
1550 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1553 /* create copy of strct */
1554 ok(!bt_ctf_field_copy(NULL
),
1555 "bt_ctf_field_copy handles NULL correctly");
1556 strct_copy
= bt_ctf_field_copy(strct
);
1558 "bt_ctf_field_copy returns a valid pointer");
1560 /* get all copied fields */
1561 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1563 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1565 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1567 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1569 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1571 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1573 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1574 assert(v_selected_copy
);
1575 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1576 assert(v_selected_0_copy
);
1577 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1578 assert(v_selected_1_copy
);
1579 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1580 assert(v_selected_2_copy
);
1581 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1582 assert(v_selected_3_copy
);
1583 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1584 assert(v_selected_4_copy
);
1585 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1586 assert(v_selected_5_copy
);
1587 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1588 assert(v_selected_6_copy
);
1589 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1590 "sequence field copy is not too large");
1591 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1593 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1595 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1597 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1599 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1601 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1603 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1604 "array field copy is not too large");
1606 /* make sure copied fields are different pointers */
1607 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1608 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1609 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1610 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1611 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1612 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1613 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1614 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1616 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1618 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1620 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1622 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1624 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1626 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1628 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1630 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1631 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1632 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1633 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1634 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1635 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1637 /* make sure copied fields share the same types */
1638 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1639 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1640 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1641 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1642 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1643 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1644 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1646 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1648 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1650 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1652 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1654 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1656 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1658 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1660 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1661 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1662 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1663 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1664 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1665 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1667 /* validate len copy */
1668 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1670 ok(uint64_t_val
== 7,
1671 "bt_ctf_field_copy creates a valid integer field copy");
1673 /* validate fp copy */
1674 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1676 ok(double_val
== 3.14,
1677 "bt_ctf_field_copy creates a valid floating point number field copy");
1679 /* validate s copy */
1680 str_val
= bt_ctf_field_string_get_value(s_copy
);
1681 ok(str_val
&& !strcmp(str_val
, "btbt"),
1682 "bt_ctf_field_copy creates a valid string field copy");
1684 /* validate e_int copy */
1685 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1688 ok(uint64_t_val
== 23,
1689 "bt_ctf_field_copy creates a valid enum's integer field copy");
1691 /* validate e copy */
1692 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1693 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1694 "bt_ctf_field_copy creates a valid enum field copy");
1696 /* validate v_selected copy */
1697 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1698 assert(v_selected_copy_len
);
1699 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1702 ok(uint64_t_val
== 7,
1703 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1704 bt_put(v_selected_copy_len
);
1705 v_selected_copy_len
= NULL
;
1707 /* validate v_selected copy fields */
1708 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1711 ok(uint64_t_val
== 7,
1712 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1713 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1716 ok(uint64_t_val
== 6,
1717 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1718 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1721 ok(uint64_t_val
== 5,
1722 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1723 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1726 ok(uint64_t_val
== 4,
1727 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1728 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1731 ok(uint64_t_val
== 3,
1732 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1733 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1736 ok(uint64_t_val
== 2,
1737 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1738 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1741 ok(uint64_t_val
== 1,
1742 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1744 /* validate a copy fields */
1745 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1746 ok(str_val
&& !strcmp(str_val
, "a_0"),
1747 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1748 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1749 ok(str_val
&& !strcmp(str_val
, "a_1"),
1750 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1751 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1752 ok(str_val
&& !strcmp(str_val
, "a_2"),
1753 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1754 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1755 ok(str_val
&& !strcmp(str_val
, "a_3"),
1756 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1757 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1758 ok(str_val
&& !strcmp(str_val
, "a_4"),
1759 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1761 /* put everything */
1768 bt_put(v_label1_type
);
1769 bt_put(v_label1_array_type
);
1770 bt_put(v_label2_type
);
1771 bt_put(v_label2_seq_type
);
1780 bt_put(v_selected_0
);
1781 bt_put(v_selected_1
);
1782 bt_put(v_selected_2
);
1783 bt_put(v_selected_3
);
1784 bt_put(v_selected_4
);
1785 bt_put(v_selected_5
);
1786 bt_put(v_selected_6
);
1800 bt_put(v_selected_copy
);
1801 bt_put(v_selected_0_copy
);
1802 bt_put(v_selected_1_copy
);
1803 bt_put(v_selected_2_copy
);
1804 bt_put(v_selected_3_copy
);
1805 bt_put(v_selected_4_copy
);
1806 bt_put(v_selected_5_copy
);
1807 bt_put(v_selected_6_copy
);
1818 void type_field_tests()
1820 struct bt_ctf_field
*uint_12
;
1821 struct bt_ctf_field
*int_16
;
1822 struct bt_ctf_field
*string
;
1823 struct bt_ctf_field
*enumeration
;
1824 struct bt_ctf_field_type
*composite_structure_type
;
1825 struct bt_ctf_field_type
*structure_seq_type
;
1826 struct bt_ctf_field_type
*string_type
;
1827 struct bt_ctf_field_type
*sequence_type
;
1828 struct bt_ctf_field_type
*uint_8_type
;
1829 struct bt_ctf_field_type
*int_16_type
;
1830 struct bt_ctf_field_type
*uint_12_type
=
1831 bt_ctf_field_type_integer_create(12);
1832 struct bt_ctf_field_type
*enumeration_type
;
1833 struct bt_ctf_field_type
*returned_type
;
1834 const char *ret_string
;
1836 returned_type
= bt_ctf_field_get_type(NULL
);
1837 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1839 ok(uint_12_type
, "Create an unsigned integer type");
1840 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1841 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1842 "Set integer type's base as binary");
1843 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1844 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1845 "Set integer type's base as decimal");
1846 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1847 BT_CTF_INTEGER_BASE_UNKNOWN
),
1848 "Reject integer type's base set as unknown");
1849 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1850 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1851 "Set integer type's base as octal");
1852 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1853 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1854 "Set integer type's base as hexadecimal");
1855 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1856 "Reject unknown integer base value");
1857 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1858 "Set integer type signedness to signed");
1859 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1860 "Set integer type signedness to unsigned");
1861 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1862 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1863 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1864 "bt_ctf_field_type_integer_get_size returns a correct value");
1865 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1866 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1867 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1868 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1870 ok(bt_ctf_field_type_set_byte_order(NULL
,
1871 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1872 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1873 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1874 (enum bt_ctf_byte_order
) 42) < 0,
1875 "bt_ctf_field_type_set_byte_order rejects invalid values");
1876 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1877 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1878 "Set an integer's byte order to little endian");
1879 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1880 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1881 "Set an integer's byte order to big endian");
1882 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1883 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1884 "bt_ctf_field_type_get_byte_order returns a correct value");
1885 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1886 BT_CTF_BYTE_ORDER_UNKNOWN
,
1887 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1889 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1890 BT_CTF_TYPE_ID_UNKNOWN
,
1891 "bt_ctf_field_type_get_type_id handles NULL correctly");
1892 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1893 BT_CTF_TYPE_ID_INTEGER
,
1894 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1896 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1897 BT_CTF_INTEGER_BASE_UNKNOWN
,
1898 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1899 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1900 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1901 "bt_ctf_field_type_integer_get_base returns a correct value");
1903 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1904 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1905 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1906 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1907 (enum bt_ctf_string_encoding
) 123) < 0,
1908 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1909 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1910 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1911 "Set integer type encoding to UTF8");
1912 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1913 BT_CTF_STRING_ENCODING_UNKNOWN
,
1914 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1915 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1916 BT_CTF_STRING_ENCODING_UTF8
,
1917 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1919 int_16_type
= bt_ctf_field_type_integer_create(16);
1920 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1921 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1922 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1923 uint_8_type
= bt_ctf_field_type_integer_create(8);
1925 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1926 ok(sequence_type
, "Create a sequence of int16_t type");
1927 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1928 BT_CTF_TYPE_ID_SEQUENCE
,
1929 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1931 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1932 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1933 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1935 ok(!strcmp(ret_string
, "seq_len"),
1936 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1937 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1938 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1939 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1941 ok(returned_type
== int_16_type
,
1942 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1943 bt_put(returned_type
);
1945 string_type
= bt_ctf_field_type_string_create();
1946 ok(string_type
, "Create a string type");
1947 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1948 BT_CTF_STRING_ENCODING_NONE
),
1949 "Reject invalid \"None\" string encoding");
1950 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1952 "Reject invalid string encoding");
1953 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1954 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1955 "Set string encoding to ASCII");
1957 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1958 BT_CTF_STRING_ENCODING_UNKNOWN
,
1959 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1960 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1961 BT_CTF_STRING_ENCODING_ASCII
,
1962 "bt_ctf_field_type_string_get_encoding returns the correct value");
1964 structure_seq_type
= bt_ctf_field_type_structure_create();
1965 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1966 BT_CTF_TYPE_ID_STRUCT
,
1967 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1968 ok(structure_seq_type
, "Create a structure type");
1969 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1970 uint_8_type
, "seq_len") == 0,
1971 "Add a uint8_t type to a structure");
1972 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1973 sequence_type
, "a_sequence") == 0,
1974 "Add a sequence type to a structure");
1976 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1977 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1978 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1979 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1981 ok(bt_ctf_field_type_structure_get_field(NULL
,
1982 &ret_string
, &returned_type
, 1) < 0,
1983 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1984 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1985 NULL
, &returned_type
, 1) == 0,
1986 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1987 bt_put(returned_type
);
1988 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1989 &ret_string
, NULL
, 1) == 0,
1990 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1991 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1992 &ret_string
, &returned_type
, 10) < 0,
1993 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1994 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1995 &ret_string
, &returned_type
, 1) == 0,
1996 "bt_ctf_field_type_structure_get_field returns a field");
1997 ok(!strcmp(ret_string
, "a_sequence"),
1998 "bt_ctf_field_type_structure_get_field returns a correct field name");
1999 ok(returned_type
== sequence_type
,
2000 "bt_ctf_field_type_structure_get_field returns a correct field type");
2001 bt_put(returned_type
);
2003 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
2004 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2005 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
2006 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2007 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2008 structure_seq_type
, "a_sequence");
2009 ok(returned_type
== sequence_type
,
2010 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2011 bt_put(returned_type
);
2013 composite_structure_type
= bt_ctf_field_type_structure_create();
2014 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2015 string_type
, "a_string") == 0,
2016 "Add a string type to a structure");
2017 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2018 structure_seq_type
, "inner_structure") == 0,
2019 "Add a structure type to a structure");
2021 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2022 NULL
, "a_sequence") == NULL
,
2023 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2024 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2025 structure_seq_type
, NULL
) == NULL
,
2026 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2027 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2028 structure_seq_type
, "a_sequence");
2029 ok(returned_type
== sequence_type
,
2030 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2031 bt_put(returned_type
);
2033 int_16
= bt_ctf_field_create(int_16_type
);
2034 ok(int_16
, "Instanciate a signed 16-bit integer");
2035 uint_12
= bt_ctf_field_create(uint_12_type
);
2036 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2037 returned_type
= bt_ctf_field_get_type(int_16
);
2038 ok(returned_type
== int_16_type
,
2039 "bt_ctf_field_get_type returns the correct type");
2041 /* Can't modify types after instanciating them */
2042 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2043 BT_CTF_INTEGER_BASE_DECIMAL
),
2044 "Check an integer type' base can't be modified after instanciation");
2045 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2046 "Check an integer type's signedness can't be modified after instanciation");
2048 /* Check signed property is checked */
2049 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2050 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2051 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2052 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2054 /* Check overflows are properly tested for */
2055 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2056 "Check -32768 is allowed for a signed 16-bit integer");
2057 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2058 "Check 32767 is allowed for a signed 16-bit integer");
2059 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2060 "Check 32768 is not allowed for a signed 16-bit integer");
2061 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2062 "Check -32769 is not allowed for a signed 16-bit integer");
2063 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2064 "Check -42 is allowed for a signed 16-bit integer");
2066 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2067 "Check 4095 is allowed for an unsigned 12-bit integer");
2068 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2069 "Check 4096 is not allowed for a unsigned 12-bit integer");
2070 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2071 "Check 0 is allowed for an unsigned 12-bit integer");
2073 string
= bt_ctf_field_create(string_type
);
2074 ok(string
, "Instanciate a string field");
2075 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2076 "Set a string's value");
2078 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2079 ok(enumeration_type
,
2080 "Create an enumeration type with an unsigned 12-bit integer as container");
2081 enumeration
= bt_ctf_field_create(enumeration_type
);
2083 "Check enumeration types are validated before instantiation");
2088 bt_put(enumeration
);
2089 bt_put(composite_structure_type
);
2090 bt_put(structure_seq_type
);
2091 bt_put(string_type
);
2092 bt_put(sequence_type
);
2093 bt_put(uint_8_type
);
2094 bt_put(int_16_type
);
2095 bt_put(uint_12_type
);
2096 bt_put(enumeration_type
);
2097 bt_put(returned_type
);
2101 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2102 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2105 * Append enough events to force the underlying packet to be resized.
2106 * Also tests that a new event can be declared after a stream has been
2107 * instantiated and used/flushed.
2111 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2113 struct bt_ctf_field_type
*integer_type
=
2114 bt_ctf_field_type_integer_create(17);
2115 struct bt_ctf_field_type
*string_type
=
2116 bt_ctf_field_type_string_create();
2117 struct bt_ctf_event
*event
= NULL
;
2118 struct bt_ctf_field
*ret_field
= NULL
;
2119 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2120 uint64_t ret_uint64
;
2121 int events_appended
= 0;
2122 struct bt_ctf_field
*packet_context
= NULL
,
2123 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
2124 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2125 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2126 struct bt_ctf_field_type
*ep_type
= NULL
;
2128 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2130 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2132 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2133 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2139 * bt_ctf_stream_class_add_event_class() copies the field types
2140 * of event_class, so we retrieve the new ones to create the
2141 * appropriate fields.
2143 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2145 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2146 ep_type
, "field_1");
2147 assert(ep_field_1_type
);
2148 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2149 ep_type
, "a_string");
2150 assert(ep_a_string_type
);
2152 event
= bt_ctf_event_create(event_class
);
2153 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2154 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2155 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2156 "bt_ctf_event_get_payload_by_index returns a correct field");
2157 bt_put(ret_field_type
);
2160 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2161 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2162 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2163 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2166 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2167 event
= bt_ctf_event_create(event_class
);
2168 struct bt_ctf_field
*integer
=
2169 bt_ctf_field_create(ep_field_1_type
);
2170 struct bt_ctf_field
*string
=
2171 bt_ctf_field_create(ep_a_string_type
);
2173 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2174 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2175 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2178 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2179 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2183 /* Populate stream event context */
2184 stream_event_context
=
2185 bt_ctf_event_get_stream_event_context(event
);
2186 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2187 "common_event_context");
2188 BT_PUT(stream_event_context
);
2189 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2193 ret
|= bt_ctf_stream_append_event(stream
, event
);
2201 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2202 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2203 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2204 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2205 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2206 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2207 ok(ret
== 0 && ret_uint64
== 0,
2208 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2209 bt_ctf_stream_append_discarded_events(stream
, 1000);
2210 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2211 ok(ret
== 0 && ret_uint64
== 1000,
2212 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2215 ok(events_appended
, "Append 100 000 events to a stream");
2218 * Populate the custom packet context field with a dummy value
2219 * otherwise flush will fail.
2221 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2222 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2223 "custom_packet_context_field");
2224 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2226 ok(bt_ctf_stream_flush(stream
) == 0,
2227 "Flush a stream that forces a packet resize");
2228 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2229 ok(ret
== 0 && ret_uint64
== 1000,
2230 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2231 bt_put(integer_type
);
2232 bt_put(string_type
);
2233 bt_put(packet_context
);
2234 bt_put(packet_context_field
);
2235 bt_put(stream_event_context
);
2236 bt_put(event_class
);
2237 bt_put(ep_field_1_type
);
2238 bt_put(ep_a_string_type
);
2243 void test_empty_stream(struct bt_ctf_writer
*writer
)
2246 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2247 struct bt_ctf_stream_class
*stream_class
= NULL
;
2248 struct bt_ctf_stream
*stream
= NULL
;
2250 trace
= bt_ctf_writer_get_trace(writer
);
2252 diag("Failed to get trace from writer");
2257 stream_class
= bt_ctf_stream_class_create("empty_stream");
2258 if (!stream_class
) {
2259 diag("Failed to create stream class");
2264 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2265 "bt_ctf_stream_class_get_trace handles NULL correctly");
2266 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2267 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2269 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2271 diag("Failed to create writer stream");
2276 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2277 ok(ret_trace
== trace
,
2278 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2281 "Created a stream class with default attributes and an empty stream");
2285 bt_put(stream_class
);
2289 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2292 struct bt_ctf_trace
*trace
= NULL
;
2293 struct bt_ctf_clock
*clock
= NULL
;
2294 struct bt_ctf_stream_class
*stream_class
= NULL
;
2295 struct bt_ctf_stream
*stream
= NULL
;
2296 struct bt_ctf_field_type
*integer_type
= NULL
,
2297 *sequence_type
= NULL
, *event_header_type
= NULL
;
2298 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2299 *event_header
= NULL
, *packet_header
= NULL
;
2300 struct bt_ctf_event_class
*event_class
= NULL
;
2301 struct bt_ctf_event
*event
= NULL
;
2303 trace
= bt_ctf_writer_get_trace(writer
);
2305 fail("Failed to get trace from writer");
2309 clock
= bt_ctf_trace_get_clock(trace
, 0);
2311 fail("Failed to get clock from trace");
2315 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2316 if (!stream_class
) {
2317 fail("Failed to create stream class");
2321 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2323 fail("Failed to set stream class clock");
2328 * Customize event header to add an "seq_len" integer member
2329 * which will be used as the length of a sequence in an event of this
2332 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2334 if (!event_header_type
) {
2335 fail("Failed to get event header type");
2339 integer_type
= bt_ctf_field_type_integer_create(13);
2340 if (!integer_type
) {
2341 fail("Failed to create length integer type");
2345 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2346 integer_type
, "seq_len");
2348 fail("Failed to add a new field to stream event header");
2352 event_class
= bt_ctf_event_class_create("sequence_event");
2354 fail("Failed to create event class");
2359 * This event's payload will contain a sequence which references
2360 * stream.event.header.seq_len as its length field.
2362 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2363 "stream.event.header.seq_len");
2364 if (!sequence_type
) {
2365 fail("Failed to create a sequence");
2369 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2372 fail("Failed to add a sequence to an event class");
2376 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2378 fail("Failed to add event class to stream class");
2382 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2384 fail("Failed to create stream")
2389 * We have defined a custom packet header field. We have to populate it
2392 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2393 if (!packet_header
) {
2394 fail("Failed to get stream packet header");
2398 integer
= bt_ctf_field_structure_get_field(packet_header
,
2399 "custom_trace_packet_header_field");
2401 fail("Failed to retrieve custom_trace_packet_header_field");
2405 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2407 fail("Failed to set custom_trace_packet_header_field value");
2412 event
= bt_ctf_event_create(event_class
);
2414 fail("Failed to create event");
2418 event_header
= bt_ctf_event_get_header(event
);
2419 if (!event_header
) {
2420 fail("Failed to get event header");
2424 integer
= bt_ctf_field_structure_get_field(event_header
,
2427 fail("Failed to get seq_len field from event header");
2431 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2433 fail("Failed to set seq_len value in event header");
2437 /* Populate both sequence integer fields */
2438 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2440 fail("Failed to retrieve sequence from event");
2444 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2446 fail("Failed to set sequence length");
2451 for (i
= 0; i
< 2; i
++) {
2452 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2454 fail("Failed to retrieve sequence element");
2458 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2460 fail("Failed to set sequence element value");
2468 ret
= bt_ctf_stream_append_event(stream
, event
);
2470 fail("Failed to append event to stream");
2474 ret
= bt_ctf_stream_flush(stream
);
2476 fail("Failed to flush custom_event_header stream");
2482 bt_put(stream_class
);
2483 bt_put(event_class
);
2487 bt_put(event_header
);
2488 bt_put(packet_header
);
2489 bt_put(sequence_type
);
2490 bt_put(integer_type
);
2491 bt_put(event_header_type
);
2495 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2498 struct bt_ctf_trace
*trace
= NULL
;
2499 struct bt_ctf_clock
*clock
= NULL
;
2500 struct bt_ctf_stream_class
*stream_class
= NULL
;
2501 struct bt_ctf_stream
*stream
= NULL
,
2503 struct bt_ctf_event_class
*event_class
= NULL
;
2504 struct bt_ctf_event
*event
= NULL
;
2505 struct bt_ctf_field_type
*integer_type
= NULL
;
2506 struct bt_ctf_field
*integer
= NULL
;
2508 trace
= bt_ctf_writer_get_trace(writer
);
2510 diag("Failed to get trace from writer");
2515 clock
= bt_ctf_trace_get_clock(trace
, 0);
2517 diag("Failed to get clock from trace");
2522 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2523 if (!stream_class
) {
2524 diag("Failed to create stream class");
2529 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2531 diag("Failed to set stream class clock");
2535 event_class
= bt_ctf_event_class_create("some_event_class_name");
2536 integer_type
= bt_ctf_field_type_integer_create(32);
2537 if (!integer_type
) {
2538 diag("Failed to create integer field type");
2543 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2546 diag("Failed to add field to event class");
2550 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2553 diag("Failed to add event class to stream class");
2556 event
= bt_ctf_event_create(event_class
);
2558 diag("Failed to create event");
2563 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2565 diag("Failed to get integer field payload from event");
2570 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2572 diag("Failed to set integer field value");
2576 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2578 diag("Failed to create writer stream");
2583 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2584 "bt_ctf_event_get_stream handles NULL correctly");
2585 ok(bt_ctf_event_get_stream(event
) == NULL
,
2586 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2588 ret
= bt_ctf_stream_append_event(stream
, event
);
2590 diag("Failed to append event to stream");
2594 ret_stream
= bt_ctf_event_get_stream(event
);
2595 ok(ret_stream
== stream
,
2596 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2599 "Create an event before instanciating its associated stream");
2603 bt_put(stream_class
);
2604 bt_put(event_class
);
2606 bt_put(integer_type
);
2612 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2614 struct bt_ctf_event_class
*event_class
;
2616 event_class
= bt_ctf_event_class_create("Simple Event");
2617 assert(event_class
);
2618 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2619 "two event classes with the same name cannot cohabit within the same stream class");
2620 bt_put(event_class
);
2622 event_class
= bt_ctf_event_class_create("different name, ok");
2623 assert(event_class
);
2624 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2625 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2626 "two event classes with the same ID cannot cohabit within the same stream class");
2627 bt_put(event_class
);
2631 void test_trace_stream_class_clock(void)
2633 struct bt_ctf_trace
*trace
= NULL
;
2634 struct bt_ctf_stream_class
*sc1
= NULL
;
2635 struct bt_ctf_stream_class
*sc2
= NULL
;
2636 struct bt_ctf_clock
*sc1_clock
= NULL
;
2637 struct bt_ctf_clock
*sc2_clock
= NULL
;
2638 const char *clock_name
= "hello";
2640 trace
= bt_ctf_trace_create();
2642 sc1
= bt_ctf_stream_class_create(NULL
);
2644 sc2
= bt_ctf_stream_class_create(NULL
);
2646 sc1_clock
= bt_ctf_clock_create(clock_name
);
2648 sc2_clock
= bt_ctf_clock_create(clock_name
);
2651 ok(!bt_ctf_stream_class_set_clock(sc1
, sc1_clock
),
2652 "bt_ctf_stream_class_set_clock() succeeds for sc1");
2653 ok(!bt_ctf_stream_class_set_clock(sc2
, sc2_clock
),
2654 "bt_ctf_stream_class_set_clock() succeeds for sc2");
2655 ok(!bt_ctf_trace_add_stream_class(trace
, sc1
),
2656 "bt_ctf_trace_add_stream_class() succeeds with sc1");
2657 ok(bt_ctf_trace_add_stream_class(trace
, sc2
),
2658 "bt_ctf_trace_add_stream_class() fails with sc2 (different clock, same name)");
2668 struct bt_ctf_event_class
*create_minimal_event_class(void)
2670 struct bt_ctf_event_class
*ec
= NULL
;
2671 struct bt_ctf_field_type
*int_ft
= NULL
;
2674 int_ft
= bt_ctf_field_type_integer_create(23);
2676 ec
= bt_ctf_event_class_create("minimal");
2678 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2686 void test_create_writer_vs_non_writer_mode(void)
2689 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2690 const char *writer_stream_name
= "writer stream instance";
2691 struct bt_ctf_writer
*writer
= NULL
;
2692 struct bt_ctf_trace
*writer_trace
= NULL
;
2693 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2694 struct bt_ctf_stream
*writer_stream
= NULL
;
2695 struct bt_ctf_stream
*writer_stream2
= NULL
;
2696 struct bt_ctf_stream
*packet_stream
= NULL
;
2697 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2698 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2699 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2700 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2701 struct bt_ctf_event_class
*writer_ec
= NULL
;
2702 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2703 struct bt_ctf_event
*event
= NULL
;
2704 struct bt_ctf_event
*event2
= NULL
;
2705 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2706 struct bt_ctf_field
*int_field
= NULL
;
2707 struct bt_ctf_clock
*writer_clock
= NULL
;
2708 struct bt_ctf_clock
*non_writer_clock
= NULL
;
2709 struct bt_ctf_packet
*packet
= NULL
;
2710 struct bt_ctf_packet
*packet2
= NULL
;
2712 if (!bt_mkdtemp(trace_path
)) {
2716 /* Create empty structure field type (event header) */
2717 empty_struct_ft
= bt_ctf_field_type_structure_create();
2718 assert(empty_struct_ft
);
2720 /* Create writer, writer stream class, stream, and clock */
2721 writer
= bt_ctf_writer_create(trace_path
);
2723 writer_trace
= bt_ctf_writer_get_trace(writer
);
2724 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2725 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2727 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2730 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2732 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2733 assert(writer_stream
);
2734 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2735 "bt_ctf_stream_get_name() returns the stream's name");
2736 writer_clock
= bt_ctf_clock_create("writer_clock");
2737 assert(writer_clock
);
2738 ret
= bt_ctf_trace_add_clock(writer_trace
, writer_clock
);
2741 /* Create non-writer trace, stream class, stream, and clock */
2742 non_writer_trace
= bt_ctf_trace_create();
2743 assert(non_writer_trace
);
2744 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2745 assert(non_writer_sc
);
2746 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2749 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2751 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2752 assert(non_writer_stream
);
2753 non_writer_clock
= bt_ctf_clock_create("non_writer_clock");
2754 assert(non_writer_clock
);
2755 ret
= bt_ctf_trace_add_clock(non_writer_trace
, non_writer_clock
);
2758 /* Create event class and event */
2759 writer_ec
= create_minimal_event_class();
2761 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2763 event
= bt_ctf_event_create(writer_ec
);
2765 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2767 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2770 * Verify non-writer stream: it should be impossible to append
2773 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2774 "bt_ctf_stream_append_event() fails with a non-writer stream");
2777 * Verify writer stream: it should be possible to append an
2780 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2781 "bt_ctf_stream_append_event() succeeds with a writer stream");
2784 * Verify that it's possible to get and set the value of a
2785 * writer mode clock.
2787 ok (!bt_ctf_clock_set_value(writer_clock
, 1000),
2788 "bt_ctf_clock_set_value() succeeds with a writer mode clock");
2789 ok (bt_ctf_clock_get_value(writer_clock
) == 1000,
2790 "bt_ctf_clock_get_value() succeeds with a writer mode clock");
2793 * Verify that it's impossible to get and set the value of a
2794 * non-writer mode clock.
2796 ok (bt_ctf_clock_set_value(non_writer_clock
, 1000),
2797 "bt_ctf_clock_set_value() fails with a non-writer mode clock");
2798 ok (bt_ctf_clock_get_value(non_writer_clock
) == -1ULL,
2799 "bt_ctf_clock_get_value() fails with a non-writer mode clock");
2802 * It should be possible to create a packet from a non-writer
2803 * stream, but not from a writer stream.
2805 packet
= bt_ctf_packet_create(writer_stream
);
2806 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2807 packet
= bt_ctf_packet_create(non_writer_stream
);
2808 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2809 packet_stream
= bt_ctf_packet_get_stream(packet
);
2810 ok(packet_stream
== non_writer_stream
,
2811 "bt_ctf_packet_get_stream() returns the correct stream");
2814 * It should not be possible to append an event associated to
2815 * a stream to a different stream.
2817 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2818 assert(writer_stream2
);
2819 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2820 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2823 * It should not be possible to set the packet of an event
2824 * associated to a given stream to a packet associated with
2825 * a different stream.
2827 ok(bt_ctf_event_set_packet(event
, packet
),
2828 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2831 * It should be possible to set the packet of a fresh event, as
2832 * long as the originating stream classes are the same.
2834 event2
= bt_ctf_event_create(writer_ec
);
2836 ok(bt_ctf_event_set_packet(event2
, packet
),
2837 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2838 non_writer_ec
= create_minimal_event_class();
2839 assert(non_writer_ec
);
2840 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2843 event2
= bt_ctf_event_create(non_writer_ec
);
2845 ok(!bt_ctf_event_set_packet(event2
, packet
),
2846 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2849 * It should be possible to set a packet created from the same
2850 * stream to an event with an existing packet.
2852 packet2
= bt_ctf_packet_create(non_writer_stream
);
2854 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2855 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2859 * It should not be possible to set a packet created from a
2860 * different stream to an event with an existing packet.
2862 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2863 assert(non_writer_stream2
);
2864 packet2
= bt_ctf_packet_create(non_writer_stream
);
2866 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2867 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2870 bt_put(writer_trace
);
2872 bt_put(writer_stream
);
2873 bt_put(writer_stream2
);
2874 bt_put(non_writer_trace
);
2875 bt_put(non_writer_sc
);
2876 bt_put(non_writer_stream
);
2877 bt_put(non_writer_stream2
);
2878 bt_put(packet_stream
);
2880 bt_put(non_writer_ec
);
2884 bt_put(empty_struct_ft
);
2885 bt_put(writer_clock
);
2886 bt_put(non_writer_clock
);
2889 recursive_rmdir(trace_path
);
2893 void test_clock_utils(void)
2896 struct bt_ctf_clock
*clock
= NULL
;
2898 clock
= bt_ctf_clock_create("water");
2900 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2902 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2904 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2906 ok(bt_ctf_clock_ns_from_value(clock
, 4321) == 1234000005321ULL,
2907 "bt_ctf_clock_ns_from_value() returns the correct value with a 1 GHz frequency");
2908 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2910 ok(bt_ctf_clock_ns_from_value(clock
, 4321) ==
2911 (uint64_t) 1237468709256.845,
2912 "bt_ctf_clock_ns_from_value() returns the correct value with a non-1 GHz frequency");
2917 int main(int argc
, char **argv
)
2919 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2920 char metadata_path
[sizeof(trace_path
) + 9];
2921 const char *clock_name
= "test_clock";
2922 const char *clock_description
= "This is a test clock";
2923 const char *returned_clock_name
;
2924 const char *returned_clock_description
;
2925 const uint64_t frequency
= 1123456789;
2926 const int64_t offset_s
= 1351530929945824323;
2927 const int64_t offset
= 1234567;
2928 int64_t get_offset_s
,
2931 const uint64_t precision
= 10;
2932 const int is_absolute
= 0xFF;
2933 char *metadata_string
;
2934 struct bt_ctf_writer
*writer
;
2935 struct utsname name
;
2936 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2937 struct bt_ctf_clock
*clock
, *ret_clock
;
2938 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2939 struct bt_ctf_stream
*stream1
;
2940 const char *ret_string
;
2941 const unsigned char *ret_uuid
;
2942 unsigned char tmp_uuid
[16] = { 0 };
2943 struct bt_ctf_field_type
*packet_context_type
,
2944 *packet_context_field_type
,
2945 *packet_header_type
,
2946 *packet_header_field_type
,
2948 *stream_event_context_type
,
2950 *event_header_field_type
;
2951 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2952 struct bt_ctf_trace
*trace
;
2954 int64_t ret_int64_t
;
2955 struct bt_value
*obj
;
2958 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2962 plan_tests(NR_TESTS
);
2964 if (!bt_mkdtemp(trace_path
)) {
2968 strcpy(metadata_path
, trace_path
);
2969 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2971 writer
= bt_ctf_writer_create(trace_path
);
2972 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2974 ok(!bt_ctf_writer_get_trace(NULL
),
2975 "bt_ctf_writer_get_trace correctly handles NULL");
2976 trace
= bt_ctf_writer_get_trace(writer
);
2978 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2979 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2980 "Set a trace's byte order to big endian");
2981 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2982 "bt_ctf_trace_get_byte_order returns a correct endianness");
2984 /* Add environment context to the trace */
2985 ret
= gethostname(hostname
, sizeof(hostname
));
2989 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2990 "Add host (%s) environment field to writer instance",
2992 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2994 "bt_ctf_writer_add_environment_field error with NULL writer");
2995 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2997 "bt_ctf_writer_add_environment_field error with NULL field name");
2998 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
3000 "bt_ctf_writer_add_environment_field error with NULL field value");
3002 /* Test bt_ctf_trace_set_environment_field with an integer object */
3003 obj
= bt_value_integer_create_init(23);
3005 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
3006 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
3007 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
3008 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
3009 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
3010 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
3011 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
3012 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
3015 /* Test bt_ctf_trace_set_environment_field with a string object */
3016 obj
= bt_value_string_create_init("the value");
3018 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
3019 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
3022 /* Test bt_ctf_trace_set_environment_field_integer */
3023 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
3025 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
3026 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
3027 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
3028 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3030 "bt_ctf_trace_set_environment_field_integer succeeds");
3032 /* Test bt_ctf_trace_set_environment_field_string */
3033 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
3035 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
3036 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
3037 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
3038 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3040 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
3041 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
3043 "bt_ctf_trace_set_environment_field_string succeeds");
3045 /* Test bt_ctf_trace_get_environment_field_count */
3046 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
3047 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
3048 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3049 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
3051 /* Test bt_ctf_trace_get_environment_field_name */
3052 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
3053 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
3054 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
3055 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
3056 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
3057 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
3058 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
3059 ok(ret_string
&& !strcmp(ret_string
, "host"),
3060 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3061 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
3062 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
3063 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3064 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
3065 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
3066 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3067 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
3068 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
3069 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3070 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
3071 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
3072 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3074 /* Test bt_ctf_trace_get_environment_field_value */
3075 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
3076 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
3077 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
3078 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
3079 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
3080 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
3081 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
3082 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3083 ok(!ret
&& ret_int64_t
== 23,
3084 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
3086 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
3087 ret
= bt_value_string_get(obj
, &ret_string
);
3088 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
3089 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
3092 /* Test bt_ctf_trace_get_environment_field_value_by_name */
3093 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
3095 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3096 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3097 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3098 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3099 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3100 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3102 ret
= bt_value_string_get(obj
, &ret_string
);
3103 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3104 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3107 /* Test environment field replacement */
3108 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3110 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3111 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3112 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3113 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
3114 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3115 ok(!ret
&& ret_int64_t
== 654321,
3116 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3119 /* On Solaris, uname() can return any positive value on success */
3120 if (uname(&name
) < 0) {
3125 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3126 == 0, "Add sysname (%s) environment field to writer instance",
3128 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3129 name
.nodename
) == 0,
3130 "Add nodename (%s) environment field to writer instance",
3132 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3133 == 0, "Add release (%s) environment field to writer instance",
3135 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3136 == 0, "Add version (%s) environment field to writer instance",
3138 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3139 == 0, "Add machine (%s) environment field to writer istance",
3142 /* Define a clock and add it to the trace */
3143 ok(bt_ctf_clock_create("signed") == NULL
,
3144 "Illegal clock name rejected");
3145 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
3146 clock
= bt_ctf_clock_create(clock_name
);
3147 ok(clock
, "Clock created sucessfully");
3148 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3149 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3150 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3151 "Returned clock name is valid");
3153 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3154 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3155 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3156 "Clock description set successfully");
3158 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3159 ok(returned_clock_description
,
3160 "bt_ctf_clock_get_description returns a description.");
3161 ok(returned_clock_description
?
3162 !strcmp(returned_clock_description
, clock_description
) : 0,
3163 "Returned clock description is valid");
3165 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3166 "bt_ctf_clock_get_frequency returns the correct default frequency");
3167 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3168 "Set clock frequency");
3169 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3170 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3172 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3173 "bt_ctf_clock_get_offset_s succeeds");
3174 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3175 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3176 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3177 "Set clock offset (seconds)");
3178 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3179 "bt_ctf_clock_get_offset_s succeeds");
3180 ok(get_offset_s
== offset_s
,
3181 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3183 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3184 "bt_ctf_clock_get_offset succeeds");
3185 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3186 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3187 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3188 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3189 "bt_ctf_clock_get_offset succeeds");
3190 ok(get_offset
== offset
,
3191 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3193 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3194 "bt_ctf_clock_get_precision returns the correct default precision");
3195 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3196 "Set clock precision");
3197 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3198 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3200 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3201 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3202 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3203 "Set clock absolute property");
3204 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3205 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3207 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3208 "bt_ctf_clock_get_time succeeds");
3209 ok(get_time
== DEFAULT_CLOCK_TIME
,
3210 "bt_ctf_clock_get_time returns the correct default time");
3211 ok(bt_ctf_clock_get_value(clock
) == DEFAULT_CLOCK_VALUE
,
3212 "bt_ctf_clock_get_value returns the correct default value");
3213 ok(bt_ctf_clock_set_value(clock
, current_time
) == 0,
3215 ok(bt_ctf_clock_get_value(clock
) == current_time
,
3216 "bt_ctf_clock_get_value returns the correct value once it is set");
3217 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3219 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3220 "bt_ctf_clock_get_time succeeds");
3221 ok(get_time
>= current_time
- 1 && get_time
<= current_time
+ 1,
3222 "bt_ctf_clock_get_time returns the correct time once it is set");
3224 ok(!bt_ctf_clock_get_name(NULL
),
3225 "bt_ctf_clock_get_name correctly handles NULL");
3226 ok(!bt_ctf_clock_get_description(NULL
),
3227 "bt_ctf_clock_get_description correctly handles NULL");
3228 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3229 "bt_ctf_clock_get_frequency correctly handles NULL");
3230 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3231 "bt_ctf_clock_get_precision correctly handles NULL");
3232 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3233 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3234 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3235 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3236 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3237 "bt_ctf_clock_get_offset correctly handles NULL clock");
3238 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3239 "bt_ctf_clock_get_offset correctly handles NULL output");
3240 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3241 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3242 ok(bt_ctf_clock_get_time(NULL
, &get_time
) < 0,
3243 "bt_ctf_clock_get_time correctly handles NULL clock");
3244 ok(bt_ctf_clock_get_time(clock
, NULL
) < 0,
3245 "bt_ctf_clock_get_time correctly handles NULL output");
3247 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3248 "bt_ctf_clock_set_description correctly handles NULL clock");
3249 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3250 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3251 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3252 "bt_ctf_clock_get_precision correctly handles NULL clock");
3253 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3254 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3255 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3256 "bt_ctf_clock_set_offset correctly handles NULL clock");
3257 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3258 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3259 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3260 "bt_ctf_clock_set_time correctly handles NULL clock");
3261 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3262 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3263 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3265 "bt_ctf_clock_get_uuid returns a UUID");
3267 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3268 /* Slightly modify UUID */
3269 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3272 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3273 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3274 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3275 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3276 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3277 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3278 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3280 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3281 ok(uuid_match(ret_uuid
, tmp_uuid
),
3282 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3284 /* Define a stream class */
3285 stream_class
= bt_ctf_stream_class_create("test_stream");
3287 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3288 "bt_ctf_stream_class_get_name handles NULL correctly");
3289 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3290 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3291 "bt_ctf_stream_class_get_name returns a correct stream class name");
3293 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3294 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3295 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3296 "bt_ctf_stream_class_get_clock handles NULL correctly");
3298 ok(stream_class
, "Create stream class");
3299 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3300 "Set a stream class' clock");
3301 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3302 ok(ret_clock
== clock
,
3303 "bt_ctf_stream_class_get_clock returns a correct clock");
3306 /* Test the event fields and event types APIs */
3309 /* Test fields copying */
3312 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3313 "bt_ctf_stream_class_get_id returns an error when no id is set");
3314 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3315 "bt_ctf_stream_class_get_id handles NULL correctly");
3316 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3317 "bt_ctf_stream_class_set_id handles NULL correctly");
3318 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3319 "Set an stream class' id");
3320 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3321 "bt_ctf_stream_class_get_id returns the correct value");
3323 /* Validate default event header fields */
3324 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3325 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3326 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3329 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3330 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_TYPE_ID_STRUCT
,
3331 "Default event header type is a structure");
3332 event_header_field_type
=
3333 bt_ctf_field_type_structure_get_field_type_by_name(
3334 ret_field_type
, "id");
3335 ok(event_header_field_type
,
3336 "Default event header type contains an \"id\" field");
3337 ok(bt_ctf_field_type_get_type_id(
3338 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3339 "Default event header \"id\" field is an integer");
3340 bt_put(event_header_field_type
);
3341 event_header_field_type
=
3342 bt_ctf_field_type_structure_get_field_type_by_name(
3343 ret_field_type
, "timestamp");
3344 ok(event_header_field_type
,
3345 "Default event header type contains a \"timestamp\" field");
3346 ok(bt_ctf_field_type_get_type_id(
3347 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3348 "Default event header \"timestamp\" field is an integer");
3349 bt_put(event_header_field_type
);
3350 bt_put(ret_field_type
);
3352 /* Add a custom trace packet header field */
3353 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3354 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3355 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3356 ok(packet_header_type
,
3357 "bt_ctf_trace_get_packet_header_type returns a packet header");
3358 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_TYPE_ID_STRUCT
,
3359 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3360 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3361 packet_header_type
, "magic");
3362 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3363 bt_put(ret_field_type
);
3364 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3365 packet_header_type
, "uuid");
3366 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3367 bt_put(ret_field_type
);
3368 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3369 packet_header_type
, "stream_id");
3370 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3371 bt_put(ret_field_type
);
3373 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3374 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3375 packet_header_field_type
, "custom_trace_packet_header_field"),
3376 "Added a custom trace packet header field successfully");
3378 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3379 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3380 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3381 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3382 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3383 "Set a trace packet_header_type successfully");
3385 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3386 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3388 /* Add a custom field to the stream class' packet context */
3389 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3390 ok(packet_context_type
,
3391 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3392 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_TYPE_ID_STRUCT
,
3393 "Packet context is a structure");
3395 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3396 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3397 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3398 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3400 integer_type
= bt_ctf_field_type_integer_create(32);
3401 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3403 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3404 /* Create a "uint5_t" equivalent custom packet context field */
3405 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3407 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3408 packet_context_field_type
, "custom_packet_context_field");
3409 ok(ret
== 0, "Packet context field added successfully");
3411 /* Define a stream event context containing a my_integer field. */
3412 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3413 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3414 ok(bt_ctf_stream_class_get_event_context_type(
3415 stream_class
) == NULL
,
3416 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3417 stream_event_context_type
= bt_ctf_field_type_structure_create();
3418 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3419 integer_type
, "common_event_context");
3421 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3422 stream_event_context_type
) < 0,
3423 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3424 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3426 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3427 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3429 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3431 ok(bt_ctf_stream_class_set_event_context_type(
3432 stream_class
, stream_event_context_type
) == 0,
3433 "Set a new stream event context type");
3434 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3436 ok(ret_field_type
== stream_event_context_type
,
3437 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3438 bt_put(ret_field_type
);
3440 /* Instantiate a stream and append events */
3441 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3442 ok(stream1
, "Instanciate a stream class from writer");
3445 * Creating a stream through a writer adds the given stream
3446 * class to the writer's trace, thus registering the stream
3447 * class's clock to the trace.
3449 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
3450 "bt_ctf_trace_get_clock_count correctly handles NULL");
3451 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
3452 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3453 ok(!bt_ctf_trace_get_clock(NULL
, 0),
3454 "bt_ctf_trace_get_clock correctly handles NULL");
3455 ok(!bt_ctf_trace_get_clock(trace
, -1),
3456 "bt_ctf_trace_get_clock correctly handles negative indexes");
3457 ok(!bt_ctf_trace_get_clock(trace
, 1),
3458 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3459 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
3460 ok(ret_clock
== clock
,
3461 "bt_ctf_trace_get_clock returns the right clock instance");
3463 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
3464 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3465 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
3466 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3467 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
3468 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3469 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
3470 ok(ret_clock
== clock
,
3471 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3473 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
3474 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3476 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3477 "bt_ctf_stream_get_class correctly handles NULL");
3478 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3479 ok(ret_stream_class
,
3480 "bt_ctf_stream_get_class returns a stream class");
3481 ok(ret_stream_class
== stream_class
,
3482 "Returned stream class is of the correct type");
3485 * Packet header, packet context, event header, and stream
3486 * event context types were copied for the resolving
3489 BT_PUT(packet_header_type
);
3490 BT_PUT(packet_context_type
);
3491 BT_PUT(stream_event_context_type
);
3492 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3493 assert(packet_header_type
);
3494 packet_context_type
=
3495 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3496 assert(packet_context_type
);
3497 stream_event_context_type
=
3498 bt_ctf_stream_class_get_event_context_type(stream_class
);
3499 assert(stream_event_context_type
);
3502 * Try to modify the packet context type after a stream has been
3505 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3506 packet_header_field_type
, "should_fail");
3508 "Trace packet header type can't be modified once a stream has been instanciated");
3511 * Try to modify the packet context type after a stream has been
3514 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3515 packet_context_field_type
, "should_fail");
3517 "Packet context type can't be modified once a stream has been instanciated");
3520 * Try to modify the stream event context type after a stream has been
3523 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3524 integer_type
, "should_fail");
3526 "Stream event context type can't be modified once a stream has been instanciated");
3528 /* Should fail after instanciating a stream (frozen) */
3529 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3530 "Changes to a stream class that was already instantiated fail");
3532 /* Populate the custom packet header field only once for all tests */
3533 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3534 "bt_ctf_stream_get_packet_header handles NULL correctly");
3535 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3537 "bt_ctf_stream_get_packet_header returns a packet header");
3538 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3539 ok(ret_field_type
== packet_header_type
,
3540 "Stream returns a packet header of the appropriate type");
3541 bt_put(ret_field_type
);
3542 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3543 "custom_trace_packet_header_field");
3544 ok(packet_header_field
,
3545 "Packet header structure contains a custom field with the appropriate name");
3546 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3547 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3548 "Custom packet header field is of the expected type");
3549 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3550 54321), "Set custom packet header value successfully");
3551 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3552 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3553 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3554 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3555 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3556 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3557 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3558 "Successfully set a stream's packet header");
3560 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3561 "Add environment field to writer after stream creation");
3565 test_trace_stream_class_clock();
3567 test_create_writer_vs_non_writer_mode();
3569 test_instanciate_event_before_stream(writer
);
3571 append_simple_event(stream_class
, stream1
, clock
);
3573 packet_resize_test(stream_class
, stream1
, clock
);
3575 append_complex_event(stream_class
, stream1
, clock
);
3577 append_existing_event_class(stream_class
);
3579 test_empty_stream(writer
);
3581 test_custom_event_header_stream(writer
);
3583 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3584 ok(metadata_string
, "Get metadata string");
3586 bt_ctf_writer_flush_metadata(writer
);
3587 validate_metadata(argv
[1], metadata_path
);
3588 validate_trace(argv
[2], trace_path
);
3591 bt_put(ret_stream_class
);
3594 bt_put(packet_context_type
);
3595 bt_put(packet_context_field_type
);
3596 bt_put(integer_type
);
3597 bt_put(stream_event_context_type
);
3598 bt_put(ret_field_type
);
3599 bt_put(packet_header_type
);
3600 bt_put(packet_header_field_type
);
3601 bt_put(packet_header
);
3602 bt_put(packet_header_field
);
3604 free(metadata_string
);
3605 bt_put(stream_class
);
3607 recursive_rmdir(trace_path
);