6 * Copyright 2013 - 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.
23 #include <babeltrace/ctf-writer/writer.h>
24 #include <babeltrace/ctf-writer/clock.h>
25 #include <babeltrace/ctf-writer/stream.h>
26 #include <babeltrace/ctf-writer/event.h>
27 #include <babeltrace/ctf-writer/event-types.h>
28 #include <babeltrace/ctf-writer/event-fields.h>
29 #include <babeltrace/ctf/events.h>
33 #include <sys/utsname.h>
34 #include <babeltrace/compat/limits.h>
44 #define METADATA_LINE_SIZE 512
45 #define SEQUENCE_TEST_LENGTH 10
46 #define ARRAY_TEST_LENGTH 5
47 #define PACKET_RESIZE_TEST_LENGTH 100000
49 #define DEFAULT_CLOCK_FREQ 1000000000
50 #define DEFAULT_CLOCK_PRECISION 1
51 #define DEFAULT_CLOCK_OFFSET 0
52 #define DEFAULT_CLOCK_OFFSET_S 0
53 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
54 #define DEFAULT_CLOCK_TIME 0
56 static uint64_t current_time
= 42;
58 /* Return 1 if uuids match, zero if different. */
59 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
64 if (!uuid_a
|| !uuid_b
) {
68 for (i
= 0; i
< 16; i
++) {
69 if (uuid_a
[i
] != uuid_b
[i
]) {
79 void validate_metadata(char *parser_path
, char *metadata_path
)
82 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
83 int parser_output_fd
= -1, metadata_fd
= -1;
90 parser_output_fd
= mkstemp(parser_output_path
);
91 metadata_fd
= open(metadata_path
, O_RDONLY
);
93 unlink(parser_output_path
);
95 if (parser_output_fd
== -1 || metadata_fd
== -1) {
96 diag("Failed create temporary files for metadata parsing.");
104 waitpid(pid
, &status
, 0);
105 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
107 /* ctf-parser-test expects a metadata string on stdin. */
108 ret
= dup2(metadata_fd
, STDIN_FILENO
);
110 perror("# dup2 metadata_fd to STDIN");
114 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
116 perror("# dup2 parser_output_fd to STDOUT");
120 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
122 perror("# dup2 parser_output_fd to STDERR");
126 execl(parser_path
, "ctf-parser-test", NULL
);
127 perror("# Could not launch the ctf metadata parser process");
131 ok(ret
== 0, "Metadata string is valid");
133 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
135 size_t len
= METADATA_LINE_SIZE
;
136 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
138 metadata_fp
= fdopen(metadata_fd
, "r");
140 perror("fdopen on metadata_fd");
145 parser_output_fp
= fdopen(parser_output_fd
, "r");
146 if (!parser_output_fp
) {
147 perror("fdopen on parser_output_fd");
150 parser_output_fd
= -1;
154 diag("malloc failure");
159 /* Output the metadata and parser output as diagnostic */
160 while (getline(&line
, &len
, metadata_fp
) > 0) {
164 rewind(parser_output_fp
);
165 while (getline(&line
, &len
, parser_output_fp
) > 0) {
172 if (fclose(metadata_fp
)) {
173 diag("fclose failure");
176 if (parser_output_fp
) {
177 if (fclose(parser_output_fp
)) {
178 diag("fclose failure");
183 if (parser_output_fd
>= 0) {
184 if (close(parser_output_fd
)) {
188 if (metadata_fd
>= 0) {
189 if (close(metadata_fd
)) {
195 void validate_trace(char *parser_path
, char *trace_path
)
198 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
199 int babeltrace_output_fd
= -1;
206 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
207 unlink(babeltrace_output_path
);
209 if (babeltrace_output_fd
== -1) {
210 diag("Failed to create a temporary file for trace parsing.");
218 waitpid(pid
, &status
, 0);
219 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
221 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
223 perror("# dup2 babeltrace_output_fd to STDOUT");
227 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
229 perror("# dup2 babeltrace_output_fd to STDERR");
233 execl(parser_path
, "babeltrace", trace_path
, NULL
);
234 perror("# Could not launch the babeltrace process");
238 ok(ret
== 0, "Babeltrace could read the resulting trace");
240 if (ret
&& babeltrace_output_fd
>= 0) {
242 size_t len
= METADATA_LINE_SIZE
;
243 FILE *babeltrace_output_fp
= NULL
;
245 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
246 if (!babeltrace_output_fp
) {
247 perror("fdopen on babeltrace_output_fd");
250 babeltrace_output_fd
= -1;
254 diag("malloc error");
256 rewind(babeltrace_output_fp
);
257 while (getline(&line
, &len
, babeltrace_output_fp
) > 0) {
263 if (babeltrace_output_fp
) {
264 if (fclose(babeltrace_output_fp
)) {
265 diag("fclose error");
270 if (babeltrace_output_fd
>= 0) {
271 if (close(babeltrace_output_fd
)) {
277 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
278 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
280 /* Create and add a simple event class */
281 struct bt_ctf_event_class
*simple_event_class
=
282 bt_ctf_event_class_create("Simple Event");
283 struct bt_ctf_field_type
*uint_12_type
=
284 bt_ctf_field_type_integer_create(12);
285 struct bt_ctf_field_type
*int_64_type
=
286 bt_ctf_field_type_integer_create(64);
287 struct bt_ctf_field_type
*float_type
=
288 bt_ctf_field_type_floating_point_create();
289 struct bt_ctf_field_type
*enum_type
;
290 struct bt_ctf_field_type
*enum_type_unsigned
=
291 bt_ctf_field_type_enumeration_create(uint_12_type
);
292 struct bt_ctf_field_type
*returned_type
;
293 struct bt_ctf_event
*simple_event
;
294 struct bt_ctf_field
*integer_field
;
295 struct bt_ctf_field
*float_field
;
296 struct bt_ctf_field
*enum_field
;
297 struct bt_ctf_field
*enum_field_unsigned
;
298 struct bt_ctf_field
*enum_container_field
;
299 const char *mapping_name_test
= "truie";
300 const double double_test_value
= 3.1415;
301 struct bt_ctf_field
*enum_container_field_unsigned
;
302 const char *mapping_name_negative_test
= "negative_value";
303 const char *ret_char
;
305 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
306 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
307 struct bt_ctf_clock
*ret_clock
;
308 struct bt_ctf_event_class
*ret_event_class
;
309 struct bt_ctf_field
*packet_context
;
310 struct bt_ctf_field
*packet_context_field
;
311 struct bt_ctf_field
*event_context
;
312 struct bt_ctf_field
*event_context_field
;
314 ok(uint_12_type
, "Create an unsigned integer type");
316 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
317 ok(int_64_type
, "Create a signed integer type");
318 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
320 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
321 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
322 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
323 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
324 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
326 bt_ctf_field_type_set_alignment(float_type
, 32);
327 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
328 "bt_ctf_field_type_get_alignment handles NULL correctly");
329 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
330 "bt_ctf_field_type_get_alignment returns a correct value");
332 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
333 "Set a floating point type's exponent digit count");
334 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
335 "Set a floating point type's mantissa digit count");
337 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
338 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
339 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
340 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
341 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
342 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
343 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
344 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
346 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
347 mapping_name_negative_test
, -12345, 0) == 0,
348 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
349 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
350 "escaping; \"test\"", 1, 1) == 0,
351 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
352 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
353 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
354 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
355 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
356 "event clock int float", 5, 22) == 0,
357 "Accept enumeration mapping strings containing reserved keywords");
358 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
360 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
361 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
362 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
363 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
364 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
365 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
366 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
368 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
369 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
370 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
371 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
372 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
373 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
375 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
376 "enum_field") == 0, "Add signed enumeration field to event");
378 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
379 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
380 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
381 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
382 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
383 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
384 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
385 NULL
, &ret_range_end_int64_t
) < 0,
386 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
387 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
388 &ret_range_start_int64_t
, NULL
) < 0,
389 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
390 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
391 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
392 "bt_ctf_field_type_enumeration_get_mapping returns a value");
393 ok(!strcmp(ret_char
, mapping_name_test
),
394 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
395 ok(ret_range_start_int64_t
== 42,
396 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
397 ok(ret_range_end_int64_t
== 42,
398 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
400 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
401 "escaping; \"test\"", 0, 0) == 0,
402 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
403 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
404 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
405 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
406 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
407 "event clock int float", 5, 22) == 0,
408 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
409 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
411 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
412 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
413 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
414 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
415 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
416 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
417 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
418 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
420 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
421 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
422 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
423 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
425 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
426 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
427 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
428 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
429 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
430 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
431 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
432 NULL
, &ret_range_end_uint64_t
) < 0,
433 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
434 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
435 &ret_range_start_uint64_t
, NULL
) < 0,
436 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
437 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
438 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
439 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
440 ok(!strcmp(ret_char
, mapping_name_test
),
441 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
442 ok(ret_range_start_uint64_t
== 42,
443 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
444 ok(ret_range_end_uint64_t
== 42,
445 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
447 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
449 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
451 bt_ctf_stream_class_add_event_class(stream_class
,
454 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
455 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
456 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
457 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
458 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
459 "bt_ctf_stream_class_get_event_class handles NULL correctly");
460 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
461 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
462 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
463 ok(ret_event_class
== simple_event_class
,
464 "bt_ctf_stream_class_get_event_class returns the correct event class");
465 bt_ctf_event_class_put(ret_event_class
);
467 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
468 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
469 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
470 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
471 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
472 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
473 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
474 ok(ret_event_class
== simple_event_class
,
475 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
476 bt_ctf_event_class_put(ret_event_class
);
478 simple_event
= bt_ctf_event_create(simple_event_class
);
480 "Instantiate an event containing a single integer field");
482 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
483 "bt_ctf_event_get_clock handles NULL correctly");
484 ret_clock
= bt_ctf_event_get_clock(simple_event
);
485 ok(ret_clock
== clock
,
486 "bt_ctf_event_get_clock returns a correct clock");
487 bt_ctf_clock_put(clock
);
489 integer_field
= bt_ctf_field_create(uint_12_type
);
490 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
491 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
492 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
494 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
495 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
496 "bt_ctf_field_floating_point_get_value fails on an unset float field");
497 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
498 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
499 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
500 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
501 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
502 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
503 "bt_ctf_field_floating_point_get_value returns a double value");
504 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
505 "bt_ctf_field_floating_point_get_value returns a correct value");
507 enum_field
= bt_ctf_field_create(enum_type
);
508 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
509 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
510 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
511 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
512 enum_container_field
= bt_ctf_field_enumeration_get_container(
514 ok(bt_ctf_field_signed_integer_set_value(
515 enum_container_field
, -42) == 0,
516 "Set signed enumeration container value");
517 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
518 ok(!strcmp(ret_char
, mapping_name_negative_test
),
519 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
520 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
522 enum_field_unsigned
= bt_ctf_field_create(enum_type_unsigned
);
523 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
524 enum_field_unsigned
);
525 ok(bt_ctf_field_unsigned_integer_set_value(
526 enum_container_field_unsigned
, 42) == 0,
527 "Set unsigned enumeration container value");
528 bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
529 enum_field_unsigned
);
530 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
531 ok(!strcmp(ret_char
, mapping_name_test
),
532 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
534 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
536 /* Populate stream event context */
537 event_context
= bt_ctf_stream_get_event_context(stream
);
538 event_context_field
= bt_ctf_field_structure_get_field(
539 event_context
, "common_event_context");
540 bt_ctf_field_unsigned_integer_set_value(event_context_field
, 42);
542 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
543 "Append simple event to trace stream");
545 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
546 "bt_ctf_stream_get_packet_context handles NULL correctly");
547 packet_context
= bt_ctf_stream_get_packet_context(stream
);
549 "bt_ctf_stream_get_packet_context returns a packet context");
551 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
553 ok(packet_context_field
,
554 "Packet context contains the default packet_size field.");
555 bt_ctf_field_put(packet_context_field
);
556 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
557 "custom_packet_context_field");
558 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
559 "Custom packet context field value successfully set.");
561 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
562 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
563 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
564 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
565 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
566 "Successfully set a stream's packet context");
568 ok(bt_ctf_stream_flush(stream
) == 0,
569 "Flush trace stream with one event");
571 bt_ctf_event_class_put(simple_event_class
);
572 bt_ctf_event_put(simple_event
);
573 bt_ctf_field_type_put(uint_12_type
);
574 bt_ctf_field_type_put(int_64_type
);
575 bt_ctf_field_type_put(float_type
);
576 bt_ctf_field_type_put(enum_type
);
577 bt_ctf_field_type_put(enum_type_unsigned
);
578 bt_ctf_field_type_put(returned_type
);
579 bt_ctf_field_put(integer_field
);
580 bt_ctf_field_put(float_field
);
581 bt_ctf_field_put(enum_field
);
582 bt_ctf_field_put(enum_field_unsigned
);
583 bt_ctf_field_put(enum_container_field
);
584 bt_ctf_field_put(enum_container_field_unsigned
);
585 bt_ctf_field_put(packet_context
);
586 bt_ctf_field_put(packet_context_field
);
587 bt_ctf_field_put(event_context
);
588 bt_ctf_field_put(event_context_field
);
591 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
592 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
595 const char *complex_test_event_string
= "Complex Test Event";
596 const char *test_string
= "Test string";
597 struct bt_ctf_field_type
*uint_35_type
=
598 bt_ctf_field_type_integer_create(35);
599 struct bt_ctf_field_type
*int_16_type
=
600 bt_ctf_field_type_integer_create(16);
601 struct bt_ctf_field_type
*uint_3_type
=
602 bt_ctf_field_type_integer_create(3);
603 struct bt_ctf_field_type
*enum_variant_type
=
604 bt_ctf_field_type_enumeration_create(uint_3_type
);
605 struct bt_ctf_field_type
*variant_type
=
606 bt_ctf_field_type_variant_create(enum_variant_type
,
608 struct bt_ctf_field_type
*string_type
=
609 bt_ctf_field_type_string_create();
610 struct bt_ctf_field_type
*sequence_type
;
611 struct bt_ctf_field_type
*array_type
;
612 struct bt_ctf_field_type
*inner_structure_type
=
613 bt_ctf_field_type_structure_create();
614 struct bt_ctf_field_type
*complex_structure_type
=
615 bt_ctf_field_type_structure_create();
616 struct bt_ctf_field_type
*ret_field_type
;
617 struct bt_ctf_event_class
*event_class
;
618 struct bt_ctf_event
*event
;
619 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
620 *inner_structure_field
, *complex_structure_field
,
621 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
622 *variant_field
, *an_array_field
, *ret_field
;
623 uint64_t ret_unsigned_int
;
624 int64_t ret_signed_int
;
625 const char *ret_string
;
626 struct bt_ctf_stream_class
*ret_stream_class
;
627 struct bt_ctf_event_class
*ret_event_class
;
628 struct bt_ctf_field
*packet_context
, *packet_context_field
;
630 bt_ctf_field_type_set_alignment(int_16_type
, 32);
631 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
632 bt_ctf_field_type_integer_set_base(uint_35_type
,
633 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
635 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
636 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
639 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
640 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
641 ret_field_type
= bt_ctf_field_type_array_get_element_type(
643 ok(ret_field_type
== int_16_type
,
644 "bt_ctf_field_type_array_get_element_type returns the correct type");
645 bt_ctf_field_type_put(ret_field_type
);
647 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
648 "bt_ctf_field_type_array_get_length handles NULL correctly");
649 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
650 "bt_ctf_field_type_array_get_length returns the correct length");
652 bt_ctf_field_type_structure_add_field(inner_structure_type
,
653 uint_35_type
, "seq_len");
654 bt_ctf_field_type_structure_add_field(inner_structure_type
,
655 sequence_type
, "a_sequence");
656 bt_ctf_field_type_structure_add_field(inner_structure_type
,
657 array_type
, "an_array");
659 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
661 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
663 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
664 "UINT35_TYPE", 2, 7);
666 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
668 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
669 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
670 enum_variant_type
, NULL
) < 0,
671 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
672 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
673 enum_variant_type
, "INT16_TYPE") == 1,
674 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
676 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
677 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
678 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
679 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
680 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
681 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
683 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
684 "An unknown entry"), "Reject a variant field based on an unknown tag value");
685 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
686 "UINT3_TYPE") == 0, "Add a field to a variant");
687 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
689 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
692 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
693 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
694 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
695 ok(ret_field_type
== enum_variant_type
,
696 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
697 bt_ctf_field_type_put(ret_field_type
);
699 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
700 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
701 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
702 ok(!strcmp(ret_string
, "variant_selector"),
703 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
704 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
705 "INT16_TYPE") == NULL
,
706 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
707 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
709 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
710 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
711 variant_type
, "INT16_TYPE");
712 ok(ret_field_type
== int_16_type
,
713 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
714 bt_ctf_field_type_put(ret_field_type
);
716 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
717 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
718 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
719 "bt_ctf_field_type_variant_get_field_count returns the correct count");
721 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
722 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
723 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) < 0,
724 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
725 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) < 0,
726 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
727 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
728 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
729 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
730 "bt_ctf_field_type_variant_get_field returns a field");
731 ok(!strcmp("INT16_TYPE", ret_string
),
732 "bt_ctf_field_type_variant_get_field returns a correct field name");
733 ok(ret_field_type
== int_16_type
,
734 "bt_ctf_field_type_variant_get_field returns a correct field type");
735 bt_ctf_field_type_put(ret_field_type
);
737 bt_ctf_field_type_structure_add_field(complex_structure_type
,
738 enum_variant_type
, "variant_selector");
739 bt_ctf_field_type_structure_add_field(complex_structure_type
,
740 string_type
, "a_string");
741 bt_ctf_field_type_structure_add_field(complex_structure_type
,
742 variant_type
, "variant_value");
743 bt_ctf_field_type_structure_add_field(complex_structure_type
,
744 inner_structure_type
, "inner_structure");
746 ok(bt_ctf_event_class_create("clock") == NULL
,
747 "Reject creation of an event class with an illegal name");
748 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
749 ok(event_class
, "Create an event class");
750 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
751 "Reject addition of a field with an empty name to an event");
752 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
753 "Reject addition of a field with a NULL type to an event");
754 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
756 "Reject addition of a type with an illegal name to an event");
757 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
759 "Add field of type unsigned integer to an event");
760 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
761 "int_16") == 0, "Add field of type signed integer to an event");
762 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
763 "complex_structure") == 0,
764 "Add composite structure to an event");
766 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
767 "bt_ctf_event_class_get_name handles NULL correctly");
768 ret_string
= bt_ctf_event_class_get_name(event_class
);
769 ok(!strcmp(ret_string
, complex_test_event_string
),
770 "bt_ctf_event_class_get_name returns a correct name");
771 ok(bt_ctf_event_class_get_id(event_class
) < 0,
772 "bt_ctf_event_class_get_id returns a negative value when not set");
773 ok(bt_ctf_event_class_get_id(NULL
) < 0,
774 "bt_ctf_event_class_get_id handles NULL correctly");
775 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
776 "bt_ctf_event_class_set_id handles NULL correctly");
777 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
778 "Set an event class' id");
779 ok(bt_ctf_event_class_get_id(event_class
) == 42,
780 "bt_ctf_event_class_get_id returns the correct value");
782 /* Add event class to the stream class */
783 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
784 "Reject addition of NULL event class to a stream class");
785 ok(bt_ctf_stream_class_add_event_class(stream_class
,
786 event_class
) == 0, "Add an event class to stream class");
788 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
789 "bt_ctf_event_class_get_stream_class handles NULL correctly");
790 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
791 ok(ret_stream_class
== stream_class
,
792 "bt_ctf_event_class_get_stream_class returns the correct stream class");
793 bt_ctf_stream_class_put(ret_stream_class
);
795 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
796 "bt_ctf_event_class_get_field_count handles NULL correctly");
797 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
798 "bt_ctf_event_class_get_field_count returns a correct value");
800 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
801 &ret_field_type
, 0) < 0,
802 "bt_ctf_event_class_get_field handles a NULL event class correctly");
803 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
804 &ret_field_type
, 0) < 0,
805 "bt_ctf_event_class_get_field handles a NULL field name correctly");
806 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
808 "bt_ctf_event_class_get_field handles a NULL field type correctly");
809 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
810 &ret_field_type
, 42) < 0,
811 "bt_ctf_event_class_get_field handles an invalid index correctly");
812 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
813 &ret_field_type
, 0) == 0,
814 "bt_ctf_event_class_get_field returns a field");
815 ok(ret_field_type
== uint_35_type
,
816 "bt_ctf_event_class_get_field returns a correct field type");
817 bt_ctf_field_type_put(ret_field_type
);
818 ok(!strcmp(ret_string
, "uint_35"),
819 "bt_ctf_event_class_get_field returns a correct field name");
820 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
821 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
822 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
823 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
824 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
825 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
826 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
827 "complex_structure");
828 ok(ret_field_type
== complex_structure_type
,
829 "bt_ctf_event_class_get_field_by_name returns a correct field type");
830 bt_ctf_field_type_put(ret_field_type
);
832 event
= bt_ctf_event_create(event_class
);
833 ok(event
, "Instanciate a complex event");
835 ok(bt_ctf_event_get_class(NULL
) == NULL
,
836 "bt_ctf_event_get_class handles NULL correctly");
837 ret_event_class
= bt_ctf_event_get_class(event
);
838 ok(ret_event_class
== event_class
,
839 "bt_ctf_event_get_class returns the correct event class");
840 bt_ctf_event_class_put(ret_event_class
);
842 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
843 if (!uint_35_field
) {
844 printf("uint_35_field is NULL\n");
847 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
848 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
849 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
850 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
851 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
852 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
853 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
854 &ret_unsigned_int
) == 0,
855 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
856 ok(ret_unsigned_int
== 0x0DDF00D,
857 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
858 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
859 &ret_signed_int
) < 0,
860 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
861 bt_ctf_field_put(uint_35_field
);
863 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
864 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
865 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
866 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
867 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
868 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
869 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
870 &ret_signed_int
) == 0,
871 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
872 ok(ret_signed_int
== -12345,
873 "bt_ctf_field_signed_integer_get_value returns the correct value");
874 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
875 &ret_unsigned_int
) < 0,
876 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
877 bt_ctf_field_put(int_16_field
);
879 complex_structure_field
= bt_ctf_event_get_payload(event
,
880 "complex_structure");
882 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
883 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
884 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
885 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
886 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
887 complex_structure_field
, 3);
888 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
889 bt_ctf_field_put(inner_structure_field
);
890 ok(ret_field_type
== inner_structure_type
,
891 "bt_ctf_field_structure_get_field_by_index returns a correct field");
892 bt_ctf_field_type_put(ret_field_type
);
894 inner_structure_field
= bt_ctf_field_structure_get_field(
895 complex_structure_field
, "inner_structure");
896 a_string_field
= bt_ctf_field_structure_get_field(
897 complex_structure_field
, "a_string");
898 enum_variant_field
= bt_ctf_field_structure_get_field(
899 complex_structure_field
, "variant_selector");
900 variant_field
= bt_ctf_field_structure_get_field(
901 complex_structure_field
, "variant_value");
902 uint_35_field
= bt_ctf_field_structure_get_field(
903 inner_structure_field
, "seq_len");
904 a_sequence_field
= bt_ctf_field_structure_get_field(
905 inner_structure_field
, "a_sequence");
906 an_array_field
= bt_ctf_field_structure_get_field(
907 inner_structure_field
, "an_array");
909 enum_container_field
= bt_ctf_field_enumeration_get_container(
911 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
912 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
914 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
915 bt_ctf_field_put(int_16_field
);
916 ok(!bt_ctf_field_string_get_value(a_string_field
),
917 "bt_ctf_field_string_get_value returns NULL on an unset field");
918 bt_ctf_field_string_set_value(a_string_field
,
920 ok(!bt_ctf_field_string_get_value(NULL
),
921 "bt_ctf_field_string_get_value correctly handles NULL");
922 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
923 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
924 ok(ret_string
? !strcmp(ret_string
, test_string
) : 0,
925 "bt_ctf_field_string_get_value returns a correct value");
926 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
927 SEQUENCE_TEST_LENGTH
);
929 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
930 enum_container_field
) == NULL
,
931 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
932 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
934 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
935 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
936 variant_type
, enum_variant_field
);
937 ok(ret_field_type
== int_16_type
,
938 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
940 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
941 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
942 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
943 uint_35_field
) == 0, "Set a sequence field's length");
944 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
945 ok(ret_field
== uint_35_field
,
946 "bt_ctf_field_sequence_get_length returns the correct length field");
947 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
948 "bt_ctf_field_sequence_get_length properly handles NULL");
950 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
951 int_16_field
= bt_ctf_field_sequence_get_field(
952 a_sequence_field
, i
);
953 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
954 bt_ctf_field_put(int_16_field
);
957 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
958 int_16_field
= bt_ctf_field_array_get_field(
960 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
961 bt_ctf_field_put(int_16_field
);
964 bt_ctf_clock_set_time(clock
, ++current_time
);
965 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
966 "Append a complex event to a stream");
969 * Populate the custom packet context field with a dummy value
970 * otherwise flush will fail.
972 packet_context
= bt_ctf_stream_get_packet_context(stream
);
973 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
974 "custom_packet_context_field");
975 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
977 ok(bt_ctf_stream_flush(stream
) == 0,
978 "Flush a stream containing a complex event");
980 bt_ctf_field_put(uint_35_field
);
981 bt_ctf_field_put(a_string_field
);
982 bt_ctf_field_put(inner_structure_field
);
983 bt_ctf_field_put(complex_structure_field
);
984 bt_ctf_field_put(a_sequence_field
);
985 bt_ctf_field_put(an_array_field
);
986 bt_ctf_field_put(enum_variant_field
);
987 bt_ctf_field_put(enum_container_field
);
988 bt_ctf_field_put(variant_field
);
989 bt_ctf_field_put(ret_field
);
990 bt_ctf_field_put(packet_context_field
);
991 bt_ctf_field_put(packet_context
);
992 bt_ctf_field_type_put(uint_35_type
);
993 bt_ctf_field_type_put(int_16_type
);
994 bt_ctf_field_type_put(string_type
);
995 bt_ctf_field_type_put(sequence_type
);
996 bt_ctf_field_type_put(array_type
);
997 bt_ctf_field_type_put(inner_structure_type
);
998 bt_ctf_field_type_put(complex_structure_type
);
999 bt_ctf_field_type_put(uint_3_type
);
1000 bt_ctf_field_type_put(enum_variant_type
);
1001 bt_ctf_field_type_put(variant_type
);
1002 bt_ctf_field_type_put(ret_field_type
);
1003 bt_ctf_event_class_put(event_class
);
1004 bt_ctf_event_put(event
);
1007 void type_field_tests()
1009 struct bt_ctf_field
*uint_12
;
1010 struct bt_ctf_field
*int_16
;
1011 struct bt_ctf_field
*string
;
1012 struct bt_ctf_field
*enumeration
;
1013 struct bt_ctf_field_type
*composite_structure_type
;
1014 struct bt_ctf_field_type
*structure_seq_type
;
1015 struct bt_ctf_field_type
*string_type
;
1016 struct bt_ctf_field_type
*sequence_type
;
1017 struct bt_ctf_field_type
*uint_8_type
;
1018 struct bt_ctf_field_type
*int_16_type
;
1019 struct bt_ctf_field_type
*uint_12_type
=
1020 bt_ctf_field_type_integer_create(12);
1021 struct bt_ctf_field_type
*enumeration_type
;
1022 struct bt_ctf_field_type
*enumeration_sequence_type
;
1023 struct bt_ctf_field_type
*enumeration_array_type
;
1024 struct bt_ctf_field_type
*returned_type
;
1025 const char *ret_string
;
1027 returned_type
= bt_ctf_field_get_type(NULL
);
1028 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1030 ok(uint_12_type
, "Create an unsigned integer type");
1031 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1032 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1033 "Set integer type's base as binary");
1034 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1035 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1036 "Set integer type's base as decimal");
1037 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1038 BT_CTF_INTEGER_BASE_UNKNOWN
),
1039 "Reject integer type's base set as unknown");
1040 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1041 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1042 "Set integer type's base as octal");
1043 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1044 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1045 "Set integer type's base as hexadecimal");
1046 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1047 "Reject unknown integer base value");
1048 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1049 "Set integer type signedness to signed");
1050 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1051 "Set integer type signedness to unsigned");
1052 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1053 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1054 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1055 "bt_ctf_field_type_integer_get_size returns a correct value");
1056 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1057 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1058 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1059 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1061 ok(bt_ctf_field_type_set_byte_order(NULL
,
1062 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1063 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1064 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1065 (enum bt_ctf_byte_order
) 42) < 0,
1066 "bt_ctf_field_type_set_byte_order rejects invalid values");
1067 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1068 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1069 "Set an integer's byte order to little endian");
1070 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1071 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1072 "Set an integer's byte order to big endian");
1073 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1074 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1075 "bt_ctf_field_type_get_byte_order returns a correct value");
1076 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1077 BT_CTF_BYTE_ORDER_UNKNOWN
,
1078 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1080 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1082 "bt_ctf_field_type_get_type_id handles NULL correctly");
1083 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1085 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1087 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1088 BT_CTF_INTEGER_BASE_UNKNOWN
,
1089 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1090 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1091 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1092 "bt_ctf_field_type_integer_get_base returns a correct value");
1094 ok(bt_ctf_field_type_integer_set_encoding(NULL
, CTF_STRING_ASCII
) < 0,
1095 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1096 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1097 (enum ctf_string_encoding
) 123) < 0,
1098 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1099 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1100 CTF_STRING_UTF8
) == 0,
1101 "Set integer type encoding to UTF8");
1102 ok(bt_ctf_field_type_integer_get_encoding(NULL
) == CTF_STRING_UNKNOWN
,
1103 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1104 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) == CTF_STRING_UTF8
,
1105 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1107 int_16_type
= bt_ctf_field_type_integer_create(16);
1108 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1109 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1110 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1111 uint_8_type
= bt_ctf_field_type_integer_create(8);
1113 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1114 ok(sequence_type
, "Create a sequence of int16_t type");
1115 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1117 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1119 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1120 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1121 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1123 ok(!strcmp(ret_string
, "seq_len"),
1124 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1125 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1126 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1127 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1129 ok(returned_type
== int_16_type
,
1130 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1131 bt_ctf_field_type_put(returned_type
);
1133 string_type
= bt_ctf_field_type_string_create();
1134 ok(string_type
, "Create a string type");
1135 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1137 "Reject invalid \"None\" string encoding");
1138 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1140 "Reject invalid string encoding");
1141 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1142 CTF_STRING_ASCII
) == 0,
1143 "Set string encoding to ASCII");
1145 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1147 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1148 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1150 "bt_ctf_field_type_string_get_encoding returns the correct value");
1152 structure_seq_type
= bt_ctf_field_type_structure_create();
1153 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1155 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1156 ok(structure_seq_type
, "Create a structure type");
1157 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1158 uint_8_type
, "seq_len") == 0,
1159 "Add a uint8_t type to a structure");
1160 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1161 sequence_type
, "a_sequence") == 0,
1162 "Add a sequence type to a structure");
1164 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1165 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1166 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1167 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1169 ok(bt_ctf_field_type_structure_get_field(NULL
,
1170 &ret_string
, &returned_type
, 1) < 0,
1171 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1172 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1173 NULL
, &returned_type
, 1) < 0,
1174 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1175 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1176 &ret_string
, NULL
, 1) < 0,
1177 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1178 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1179 &ret_string
, &returned_type
, 10) < 0,
1180 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1181 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1182 &ret_string
, &returned_type
, 1) == 0,
1183 "bt_ctf_field_type_structure_get_field returns a field");
1184 ok(!strcmp(ret_string
, "a_sequence"),
1185 "bt_ctf_field_type_structure_get_field returns a correct field name");
1186 ok(returned_type
== sequence_type
,
1187 "bt_ctf_field_type_structure_get_field returns a correct field type");
1188 bt_ctf_field_type_put(returned_type
);
1190 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1191 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1192 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1193 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1194 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1195 structure_seq_type
, "a_sequence");
1196 ok(returned_type
== sequence_type
,
1197 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1198 bt_ctf_field_type_put(returned_type
);
1200 composite_structure_type
= bt_ctf_field_type_structure_create();
1201 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1202 string_type
, "a_string") == 0,
1203 "Add a string type to a structure");
1204 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1205 structure_seq_type
, "inner_structure") == 0,
1206 "Add a structure type to a structure");
1208 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1209 NULL
, "a_sequence") == NULL
,
1210 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1211 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1212 structure_seq_type
, NULL
) == NULL
,
1213 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1214 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1215 structure_seq_type
, "a_sequence");
1216 ok(returned_type
== sequence_type
,
1217 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1218 bt_ctf_field_type_put(returned_type
);
1220 int_16
= bt_ctf_field_create(int_16_type
);
1221 ok(int_16
, "Instanciate a signed 16-bit integer");
1222 uint_12
= bt_ctf_field_create(uint_12_type
);
1223 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1224 returned_type
= bt_ctf_field_get_type(int_16
);
1225 ok(returned_type
== int_16_type
,
1226 "bt_ctf_field_get_type returns the correct type");
1228 /* Can't modify types after instanciating them */
1229 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1230 BT_CTF_INTEGER_BASE_DECIMAL
),
1231 "Check an integer type' base can't be modified after instanciation");
1232 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1233 "Check an integer type's signedness can't be modified after instanciation");
1235 /* Check signed property is checked */
1236 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1237 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1238 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1239 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1241 /* Check overflows are properly tested for */
1242 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1243 "Check -32768 is allowed for a signed 16-bit integer");
1244 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1245 "Check 32767 is allowed for a signed 16-bit integer");
1246 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1247 "Check 32768 is not allowed for a signed 16-bit integer");
1248 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1249 "Check -32769 is not allowed for a signed 16-bit integer");
1250 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1251 "Check -42 is allowed for a signed 16-bit integer");
1253 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1254 "Check 4095 is allowed for an unsigned 12-bit integer");
1255 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1256 "Check 4096 is not allowed for a unsigned 12-bit integer");
1257 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1258 "Check 0 is allowed for an unsigned 12-bit integer");
1260 string
= bt_ctf_field_create(string_type
);
1261 ok(string
, "Instanciate a string field");
1262 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1263 "Set a string's value");
1265 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1266 ok(enumeration_type
,
1267 "Create an enumeration type with an unsigned 12-bit integer as container");
1268 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
1269 enumeration_type
, "count");
1270 ok(!enumeration_sequence_type
,
1271 "Check enumeration types are validated when creating a sequence");
1272 enumeration_array_type
= bt_ctf_field_type_array_create(
1273 enumeration_type
, 10);
1274 ok(!enumeration_array_type
,
1275 "Check enumeration types are validated when creating an array");
1276 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1277 enumeration_type
, "enumeration"),
1278 "Check enumeration types are validated when adding them as structure members");
1279 enumeration
= bt_ctf_field_create(enumeration_type
);
1281 "Check enumeration types are validated before instantiation");
1283 bt_ctf_field_put(string
);
1284 bt_ctf_field_put(uint_12
);
1285 bt_ctf_field_put(int_16
);
1286 bt_ctf_field_put(enumeration
);
1287 bt_ctf_field_type_put(composite_structure_type
);
1288 bt_ctf_field_type_put(structure_seq_type
);
1289 bt_ctf_field_type_put(string_type
);
1290 bt_ctf_field_type_put(sequence_type
);
1291 bt_ctf_field_type_put(uint_8_type
);
1292 bt_ctf_field_type_put(int_16_type
);
1293 bt_ctf_field_type_put(uint_12_type
);
1294 bt_ctf_field_type_put(enumeration_type
);
1295 bt_ctf_field_type_put(enumeration_sequence_type
);
1296 bt_ctf_field_type_put(enumeration_array_type
);
1297 bt_ctf_field_type_put(returned_type
);
1300 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1301 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1304 * Append enough events to force the underlying packet to be resized.
1305 * Also tests that a new event can be declared after a stream has been
1306 * instantiated and used/flushed.
1310 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1312 struct bt_ctf_field_type
*integer_type
=
1313 bt_ctf_field_type_integer_create(17);
1314 struct bt_ctf_field_type
*string_type
=
1315 bt_ctf_field_type_string_create();
1316 struct bt_ctf_event
*event
= NULL
;
1317 struct bt_ctf_field
*ret_field
= NULL
;
1318 struct bt_ctf_field_type
*ret_field_type
= NULL
;
1319 uint64_t ret_uint64
;
1320 int events_appended
= 0;
1321 struct bt_ctf_field
*packet_context
= NULL
,
1322 *packet_context_field
= NULL
, *event_context
= NULL
;
1324 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1326 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1328 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1329 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1334 event
= bt_ctf_event_create(event_class
);
1335 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
1336 ret_field_type
= bt_ctf_field_get_type(ret_field
);
1337 ok(ret_field_type
== integer_type
,
1338 "bt_ctf_event_get_payload_by_index returns a correct field");
1339 bt_ctf_field_type_put(ret_field_type
);
1340 bt_ctf_field_put(ret_field
);
1342 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
1343 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1344 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
1345 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1346 bt_ctf_event_put(event
);
1348 ok(bt_ctf_stream_get_event_context(NULL
) == NULL
,
1349 "bt_ctf_stream_get_event_context handles NULL correctly");
1350 event_context
= bt_ctf_stream_get_event_context(stream
);
1352 "bt_ctf_stream_get_event_context returns a stream event context");
1353 ok(bt_ctf_stream_set_event_context(NULL
, event_context
) < 0,
1354 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
1355 ok(bt_ctf_stream_set_event_context(stream
, NULL
) < 0,
1356 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
1357 ok(!bt_ctf_stream_set_event_context(stream
, event_context
),
1358 "bt_ctf_stream_set_event_context correctly set a stream event context");
1359 ret_field
= bt_ctf_field_create(integer_type
);
1360 ok(bt_ctf_stream_set_event_context(stream
, ret_field
) < 0,
1361 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
1362 bt_ctf_field_put(ret_field
);
1364 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
1365 event
= bt_ctf_event_create(event_class
);
1366 struct bt_ctf_field
*integer
=
1367 bt_ctf_field_create(integer_type
);
1368 struct bt_ctf_field
*string
=
1369 bt_ctf_field_create(string_type
);
1371 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1372 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1373 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1375 bt_ctf_field_put(integer
);
1376 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1377 ret
|= bt_ctf_event_set_payload(event
, "a_string",
1379 bt_ctf_field_put(string
);
1381 /* Populate stream event context */
1382 integer
= bt_ctf_field_structure_get_field(event_context
,
1383 "common_event_context");
1384 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
1386 bt_ctf_field_put(integer
);
1388 ret
|= bt_ctf_stream_append_event(stream
, event
);
1389 bt_ctf_event_put(event
);
1396 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
1397 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
1398 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
1399 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
1400 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
1401 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
1402 ok(ret
== 0 && ret_uint64
== 0,
1403 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
1404 bt_ctf_stream_append_discarded_events(stream
, 1000);
1405 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
1406 ok(ret
== 0 && ret_uint64
== 1000,
1407 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
1410 ok(events_appended
, "Append 100 000 events to a stream");
1413 * Populate the custom packet context field with a dummy value
1414 * otherwise flush will fail.
1416 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1417 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1418 "custom_packet_context_field");
1419 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
1421 ok(bt_ctf_stream_flush(stream
) == 0,
1422 "Flush a stream that forces a packet resize");
1423 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
1424 ok(ret
== 0 && ret_uint64
== 1000,
1425 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
1426 bt_ctf_field_type_put(integer_type
);
1427 bt_ctf_field_type_put(string_type
);
1428 bt_ctf_field_put(packet_context
);
1429 bt_ctf_field_put(packet_context_field
);
1430 bt_ctf_field_put(event_context
);
1431 bt_ctf_event_class_put(event_class
);
1434 int main(int argc
, char **argv
)
1436 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
1437 char metadata_path
[sizeof(trace_path
) + 9];
1438 const char *clock_name
= "test_clock";
1439 const char *clock_description
= "This is a test clock";
1440 const char *returned_clock_name
;
1441 const char *returned_clock_description
;
1442 const uint64_t frequency
= 1123456789;
1443 const uint64_t offset_s
= 1351530929945824323;
1444 const uint64_t offset
= 1234567;
1445 const uint64_t precision
= 10;
1446 const int is_absolute
= 0xFF;
1447 char *metadata_string
;
1448 struct bt_ctf_writer
*writer
;
1449 struct utsname name
;
1450 char hostname
[BABELTRACE_HOST_NAME_MAX
];
1451 struct bt_ctf_clock
*clock
, *ret_clock
;
1452 struct bt_ctf_stream_class
*stream_class
;
1453 struct bt_ctf_stream
*stream1
;
1454 const char *ret_string
;
1455 const unsigned char *ret_uuid
;
1456 unsigned char tmp_uuid
[16] = { 0 };
1457 struct bt_ctf_field_type
*packet_context_type
,
1458 *packet_context_field_type
,
1460 *stream_event_context_type
,
1462 struct bt_ctf_trace
*trace
;
1466 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
1472 if (!mkdtemp(trace_path
)) {
1476 strcpy(metadata_path
, trace_path
);
1477 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
1479 writer
= bt_ctf_writer_create(trace_path
);
1480 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
1482 /* Add environment context to the trace */
1483 ret
= gethostname(hostname
, sizeof(hostname
));
1487 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
1488 "Add host (%s) environment field to writer instance",
1490 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
1492 "bt_ctf_writer_add_environment_field error with NULL writer");
1493 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
1495 "bt_ctf_writer_add_environment_field error with NULL field name");
1496 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
1498 "bt_ctf_writer_add_environment_field error with NULL field value");
1505 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
1506 == 0, "Add sysname (%s) environment field to writer instance",
1508 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
1509 name
.nodename
) == 0,
1510 "Add nodename (%s) environment field to writer instance",
1512 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
1513 == 0, "Add release (%s) environment field to writer instance",
1515 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
1516 == 0, "Add version (%s) environment field to writer instance",
1518 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
1519 == 0, "Add machine (%s) environment field to writer istance",
1522 /* Define a clock and add it to the trace */
1523 ok(bt_ctf_clock_create("signed") == NULL
,
1524 "Illegal clock name rejected");
1525 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
1526 clock
= bt_ctf_clock_create(clock_name
);
1527 ok(clock
, "Clock created sucessfully");
1528 returned_clock_name
= bt_ctf_clock_get_name(clock
);
1529 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
1530 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
1531 "Returned clock name is valid");
1533 returned_clock_description
= bt_ctf_clock_get_description(clock
);
1534 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
1535 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
1536 "Clock description set successfully");
1538 returned_clock_description
= bt_ctf_clock_get_description(clock
);
1539 ok(returned_clock_description
,
1540 "bt_ctf_clock_get_description returns a description.");
1541 ok(returned_clock_description
?
1542 !strcmp(returned_clock_description
, clock_description
) : 0,
1543 "Returned clock description is valid");
1545 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
1546 "bt_ctf_clock_get_frequency returns the correct default frequency");
1547 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
1548 "Set clock frequency");
1549 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
1550 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
1552 ok(bt_ctf_clock_get_offset_s(clock
) == DEFAULT_CLOCK_OFFSET_S
,
1553 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
1554 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
1555 "Set clock offset (seconds)");
1556 ok(bt_ctf_clock_get_offset_s(clock
) == offset_s
,
1557 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
1559 ok(bt_ctf_clock_get_offset(clock
) == DEFAULT_CLOCK_OFFSET
,
1560 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
1561 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
1562 ok(bt_ctf_clock_get_offset(clock
) == offset
,
1563 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
1565 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
1566 "bt_ctf_clock_get_precision returns the correct default precision");
1567 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
1568 "Set clock precision");
1569 ok(bt_ctf_clock_get_precision(clock
) == precision
,
1570 "bt_ctf_clock_get_precision returns the correct precision once it is set");
1572 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
1573 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
1574 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
1575 "Set clock absolute property");
1576 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
1577 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
1579 ok(bt_ctf_clock_get_time(clock
) == DEFAULT_CLOCK_TIME
,
1580 "bt_ctf_clock_get_time returns the correct default time");
1581 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
1583 ok(bt_ctf_clock_get_time(clock
) == current_time
,
1584 "bt_ctf_clock_get_time returns the correct time once it is set");
1586 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
1587 "Add clock to writer instance");
1588 ok(bt_ctf_writer_add_clock(writer
, clock
),
1589 "Verify a clock can't be added twice to a writer instance");
1591 ok(!bt_ctf_writer_get_trace(NULL
),
1592 "bt_ctf_writer_get_trace correctly handles NULL");
1593 trace
= bt_ctf_writer_get_trace(writer
);
1595 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
1596 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
1597 "bt_ctf_trace_get_clock_count correctly handles NULL");
1598 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
1599 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
1600 ok(!bt_ctf_trace_get_clock(NULL
, 0),
1601 "bt_ctf_trace_get_clock correctly handles NULL");
1602 ok(!bt_ctf_trace_get_clock(trace
, -1),
1603 "bt_ctf_trace_get_clock correctly handles negative indexes");
1604 ok(!bt_ctf_trace_get_clock(trace
, 1),
1605 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
1606 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
1607 ok(ret_clock
== clock
,
1608 "bt_ctf_trace_get_clock returns the right clock instance");
1609 bt_ctf_clock_put(ret_clock
);
1611 ok(!bt_ctf_clock_get_name(NULL
),
1612 "bt_ctf_clock_get_name correctly handles NULL");
1613 ok(!bt_ctf_clock_get_description(NULL
),
1614 "bt_ctf_clock_get_description correctly handles NULL");
1615 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
1616 "bt_ctf_clock_get_frequency correctly handles NULL");
1617 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
1618 "bt_ctf_clock_get_precision correctly handles NULL");
1619 ok(bt_ctf_clock_get_offset_s(NULL
) == -1ULL,
1620 "bt_ctf_clock_get_offset_s correctly handles NULL");
1621 ok(bt_ctf_clock_get_offset(NULL
) == -1ULL,
1622 "bt_ctf_clock_get_offset correctly handles NULL");
1623 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
1624 "bt_ctf_clock_get_is_absolute correctly handles NULL");
1625 ok(bt_ctf_clock_get_time(NULL
) == -1ULL,
1626 "bt_ctf_clock_get_time correctly handles NULL");
1628 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
1629 "bt_ctf_clock_set_description correctly handles NULL clock");
1630 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
1631 "bt_ctf_clock_set_frequency correctly handles NULL clock");
1632 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
1633 "bt_ctf_clock_get_precision correctly handles NULL clock");
1634 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
1635 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
1636 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
1637 "bt_ctf_clock_set_offset correctly handles NULL clock");
1638 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
1639 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
1640 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
1641 "bt_ctf_clock_set_time correctly handles NULL clock");
1642 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
1643 "bt_ctf_clock_get_uuid correctly handles NULL clock");
1644 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
1646 "bt_ctf_clock_get_uuid returns a UUID");
1648 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
1649 /* Slightly modify UUID */
1650 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
1653 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
1654 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
1655 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
1656 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
1657 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
1658 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
1659 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
1661 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
1662 ok(uuid_match(ret_uuid
, tmp_uuid
),
1663 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
1665 /* Define a stream class */
1666 stream_class
= bt_ctf_stream_class_create("test_stream");
1668 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
1669 "bt_ctf_stream_class_get_name handles NULL correctly");
1670 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
1671 ok(!strcmp(ret_string
, "test_stream"),
1672 "bt_ctf_stream_class_get_name returns a correct stream class name");
1674 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
1675 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
1676 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
1677 "bt_ctf_stream_class_get_clock handles NULL correctly");
1679 ok(stream_class
, "Create stream class");
1680 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
1681 "Set a stream class' clock");
1682 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
1683 ok(ret_clock
== clock
,
1684 "bt_ctf_stream_class_get_clock returns a correct clock");
1685 bt_ctf_clock_put(ret_clock
);
1687 /* Test the event fields and event types APIs */
1690 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
1691 "bt_ctf_stream_class_get_id returns an error when no id is set");
1692 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
1693 "bt_ctf_stream_class_get_id handles NULL correctly");
1694 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
1695 "bt_ctf_stream_class_set_id handles NULL correctly");
1696 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
1697 "Set an stream class' id");
1698 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
1699 "bt_ctf_stream_class_get_id returns the correct value");
1701 /* Create a "uint5_t" equivalent custom packet context field */
1702 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
1704 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
1705 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
1707 /* Add a custom field to the stream class' packet context */
1708 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
1709 ok(packet_context_type
,
1710 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
1711 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == CTF_TYPE_STRUCT
,
1712 "Packet context is a structure");
1714 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
1715 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
1716 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
1717 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
1719 integer_type
= bt_ctf_field_type_integer_create(32);
1720 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
1722 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
1723 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
1724 packet_context_field_type
, "custom_packet_context_field");
1725 ok(ret
== 0, "Packet context field added successfully");
1727 /* Define a stream event context containing a my_integer field. */
1728 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
1729 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
1730 ok(bt_ctf_stream_class_get_event_context_type(
1731 stream_class
) == NULL
,
1732 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
1733 stream_event_context_type
= bt_ctf_field_type_structure_create();
1734 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
1735 integer_type
, "common_event_context");
1737 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
1738 stream_event_context_type
) < 0,
1739 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
1740 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
1742 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
1743 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
1745 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
1747 ok(bt_ctf_stream_class_set_event_context_type(
1748 stream_class
, stream_event_context_type
) == 0,
1749 "Set a new stream event context type");
1750 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
1752 ok(ret_field_type
== stream_event_context_type
,
1753 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
1755 /* Instantiate a stream and append events */
1756 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
1757 ok(stream1
, "Instanciate a stream class from writer");
1760 * Try to modify the packet context type after a stream has been
1763 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
1764 packet_context_field_type
, "should_fail");
1766 "Packet context type can't be modified once a stream class has been instanciated");
1769 * Try to modify the stream event context type after a stream has been
1772 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
1773 integer_type
, "should_fail");
1775 "Stream event context type can't be modified once a stream class has been instanciated");
1777 /* Should fail after instanciating a stream (frozen) */
1778 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
1779 "Changes to a stream class that was already instantiated fail");
1781 append_simple_event(stream_class
, stream1
, clock
);
1783 packet_resize_test(stream_class
, stream1
, clock
);
1785 append_complex_event(stream_class
, stream1
, clock
);
1787 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
1788 ok(metadata_string
, "Get metadata string");
1790 bt_ctf_writer_flush_metadata(writer
);
1791 validate_metadata(argv
[1], metadata_path
);
1792 validate_trace(argv
[2], trace_path
);
1794 bt_ctf_clock_put(clock
);
1795 bt_ctf_stream_class_put(stream_class
);
1796 bt_ctf_writer_put(writer
);
1797 bt_ctf_stream_put(stream1
);
1798 bt_ctf_field_type_put(packet_context_type
);
1799 bt_ctf_field_type_put(packet_context_field_type
);
1800 bt_ctf_field_type_put(integer_type
);
1801 bt_ctf_field_type_put(stream_event_context_type
);
1802 bt_ctf_field_type_put(ret_field_type
);
1803 bt_ctf_trace_put(trace
);
1804 free(metadata_string
);
1806 /* Remove all trace files and delete temporary trace directory */
1807 DIR *trace_dir
= opendir(trace_path
);
1809 perror("# opendir");
1813 struct dirent
*entry
;
1814 while ((entry
= readdir(trace_dir
))) {
1815 if (entry
->d_type
== DT_REG
) {
1816 unlinkat(dirfd(trace_dir
), entry
->d_name
, 0);
1821 closedir(trace_dir
);