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>
45 #define METADATA_LINE_SIZE 512
46 #define SEQUENCE_TEST_LENGTH 10
47 #define ARRAY_TEST_LENGTH 5
48 #define PACKET_RESIZE_TEST_LENGTH 100000
50 #define DEFAULT_CLOCK_FREQ 1000000000
51 #define DEFAULT_CLOCK_PRECISION 1
52 #define DEFAULT_CLOCK_OFFSET 0
53 #define DEFAULT_CLOCK_OFFSET_S 0
54 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
55 #define DEFAULT_CLOCK_TIME 0
57 static uint64_t current_time
= 42;
59 /* Return 1 if uuids match, zero if different. */
60 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
65 if (!uuid_a
|| !uuid_b
) {
69 for (i
= 0; i
< 16; i
++) {
70 if (uuid_a
[i
] != uuid_b
[i
]) {
80 void validate_metadata(char *parser_path
, char *metadata_path
)
83 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
84 int parser_output_fd
= -1, metadata_fd
= -1;
91 parser_output_fd
= mkstemp(parser_output_path
);
92 metadata_fd
= open(metadata_path
, O_RDONLY
);
94 unlink(parser_output_path
);
96 if (parser_output_fd
== -1 || metadata_fd
== -1) {
97 diag("Failed create temporary files for metadata parsing.");
105 waitpid(pid
, &status
, 0);
106 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
108 /* ctf-parser-test expects a metadata string on stdin. */
109 ret
= dup2(metadata_fd
, STDIN_FILENO
);
111 perror("# dup2 metadata_fd to STDIN");
115 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
117 perror("# dup2 parser_output_fd to STDOUT");
121 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
123 perror("# dup2 parser_output_fd to STDERR");
127 execl(parser_path
, "ctf-parser-test", NULL
);
128 perror("# Could not launch the ctf metadata parser process");
132 ok(ret
== 0, "Metadata string is valid");
134 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
136 size_t len
= METADATA_LINE_SIZE
;
137 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
139 metadata_fp
= fdopen(metadata_fd
, "r");
141 perror("fdopen on metadata_fd");
146 parser_output_fp
= fdopen(parser_output_fd
, "r");
147 if (!parser_output_fp
) {
148 perror("fdopen on parser_output_fd");
151 parser_output_fd
= -1;
155 diag("malloc failure");
160 /* Output the metadata and parser output as diagnostic */
161 while (getline(&line
, &len
, metadata_fp
) > 0) {
165 rewind(parser_output_fp
);
166 while (getline(&line
, &len
, parser_output_fp
) > 0) {
173 if (fclose(metadata_fp
)) {
174 diag("fclose failure");
177 if (parser_output_fp
) {
178 if (fclose(parser_output_fp
)) {
179 diag("fclose failure");
184 if (parser_output_fd
>= 0) {
185 if (close(parser_output_fd
)) {
189 if (metadata_fd
>= 0) {
190 if (close(metadata_fd
)) {
196 void validate_trace(char *parser_path
, char *trace_path
)
199 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
200 int babeltrace_output_fd
= -1;
207 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
208 unlink(babeltrace_output_path
);
210 if (babeltrace_output_fd
== -1) {
211 diag("Failed to create a temporary file for trace parsing.");
219 waitpid(pid
, &status
, 0);
220 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
222 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
224 perror("# dup2 babeltrace_output_fd to STDOUT");
228 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
230 perror("# dup2 babeltrace_output_fd to STDERR");
234 execl(parser_path
, "babeltrace", trace_path
, NULL
);
235 perror("# Could not launch the babeltrace process");
239 ok(ret
== 0, "Babeltrace could read the resulting trace");
241 if (ret
&& babeltrace_output_fd
>= 0) {
243 size_t len
= METADATA_LINE_SIZE
;
244 FILE *babeltrace_output_fp
= NULL
;
246 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
247 if (!babeltrace_output_fp
) {
248 perror("fdopen on babeltrace_output_fd");
251 babeltrace_output_fd
= -1;
255 diag("malloc error");
257 rewind(babeltrace_output_fp
);
258 while (getline(&line
, &len
, babeltrace_output_fp
) > 0) {
264 if (babeltrace_output_fp
) {
265 if (fclose(babeltrace_output_fp
)) {
266 diag("fclose error");
271 if (babeltrace_output_fd
>= 0) {
272 if (close(babeltrace_output_fd
)) {
278 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
279 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
281 /* Create and add a simple event class */
282 struct bt_ctf_event_class
*simple_event_class
=
283 bt_ctf_event_class_create("Simple Event");
284 struct bt_ctf_field_type
*uint_12_type
=
285 bt_ctf_field_type_integer_create(12);
286 struct bt_ctf_field_type
*int_64_type
=
287 bt_ctf_field_type_integer_create(64);
288 struct bt_ctf_field_type
*float_type
=
289 bt_ctf_field_type_floating_point_create();
290 struct bt_ctf_field_type
*enum_type
;
291 struct bt_ctf_field_type
*enum_type_unsigned
=
292 bt_ctf_field_type_enumeration_create(uint_12_type
);
293 struct bt_ctf_field_type
*returned_type
;
294 struct bt_ctf_event
*simple_event
;
295 struct bt_ctf_field
*integer_field
;
296 struct bt_ctf_field
*float_field
;
297 struct bt_ctf_field
*enum_field
;
298 struct bt_ctf_field
*enum_field_unsigned
;
299 struct bt_ctf_field
*enum_container_field
;
300 const char *mapping_name_test
= "truie";
301 const double double_test_value
= 3.1415;
302 struct bt_ctf_field
*enum_container_field_unsigned
;
303 const char *mapping_name_negative_test
= "negative_value";
304 const char *ret_char
;
307 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
308 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
309 struct bt_ctf_clock
*ret_clock
;
310 struct bt_ctf_event_class
*ret_event_class
;
311 struct bt_ctf_field
*packet_context
;
312 struct bt_ctf_field
*packet_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, &ret_size_t
) < 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
, -42, NULL
) < 0,
371 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL index correctly");
372 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000, &ret_size_t
) < 0,
373 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
374 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55, &ret_size_t
) == 0,
375 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
377 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
379 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
380 "enum_field") == 0, "Add signed enumeration field to event");
382 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
383 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
384 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
385 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
386 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
387 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
388 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
389 NULL
, &ret_range_end_int64_t
) < 0,
390 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
391 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
392 &ret_range_start_int64_t
, NULL
) < 0,
393 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
394 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
395 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
396 "bt_ctf_field_type_enumeration_get_mapping returns a value");
397 ok(!strcmp(ret_char
, mapping_name_test
),
398 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
399 ok(ret_range_start_int64_t
== 42,
400 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
401 ok(ret_range_end_int64_t
== 42,
402 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
404 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
405 "escaping; \"test\"", 0, 0) == 0,
406 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
407 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
408 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
409 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
410 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
411 "event clock int float", 5, 22) == 0,
412 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
413 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
415 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
416 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
417 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
418 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
419 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
420 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
421 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
422 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
424 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
425 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
426 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
427 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
429 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
430 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
431 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
432 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
433 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
434 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
435 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
436 NULL
, &ret_range_end_uint64_t
) < 0,
437 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
438 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
439 &ret_range_start_uint64_t
, NULL
) < 0,
440 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
441 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
442 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
443 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
444 ok(!strcmp(ret_char
, mapping_name_test
),
445 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
446 ok(ret_range_start_uint64_t
== 42,
447 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
448 ok(ret_range_end_uint64_t
== 42,
449 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
451 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
453 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
455 bt_ctf_stream_class_add_event_class(stream_class
,
458 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
459 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
460 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
461 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
462 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
463 "bt_ctf_stream_class_get_event_class handles NULL correctly");
464 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
465 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
466 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
467 ok(ret_event_class
== simple_event_class
,
468 "bt_ctf_stream_class_get_event_class returns the correct event class");
469 bt_ctf_event_class_put(ret_event_class
);
471 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
472 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
473 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
474 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
475 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
476 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
477 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
478 ok(ret_event_class
== simple_event_class
,
479 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
480 bt_ctf_event_class_put(ret_event_class
);
482 simple_event
= bt_ctf_event_create(simple_event_class
);
484 "Instantiate an event containing a single integer field");
486 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
487 "bt_ctf_event_get_clock handles NULL correctly");
488 ret_clock
= bt_ctf_event_get_clock(simple_event
);
489 ok(ret_clock
== clock
,
490 "bt_ctf_event_get_clock returns a correct clock");
491 bt_ctf_clock_put(clock
);
493 integer_field
= bt_ctf_field_create(uint_12_type
);
494 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
495 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
496 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
498 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
499 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
500 "bt_ctf_field_floating_point_get_value fails on an unset float field");
501 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
502 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
503 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
504 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
505 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
506 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
507 "bt_ctf_field_floating_point_get_value returns a double value");
508 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
509 "bt_ctf_field_floating_point_get_value returns a correct value");
511 enum_field
= bt_ctf_field_create(enum_type
);
512 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
513 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
514 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
515 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
516 enum_container_field
= bt_ctf_field_enumeration_get_container(
518 ok(bt_ctf_field_signed_integer_set_value(
519 enum_container_field
, -42) == 0,
520 "Set signed enumeration container value");
521 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
522 ok(!strcmp(ret_char
, mapping_name_negative_test
),
523 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
524 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
526 enum_field_unsigned
= bt_ctf_field_create(enum_type_unsigned
);
527 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
528 enum_field_unsigned
);
529 ok(bt_ctf_field_unsigned_integer_set_value(
530 enum_container_field_unsigned
, 42) == 0,
531 "Set unsigned enumeration container value");
532 bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
533 enum_field_unsigned
);
534 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
535 ok(!strcmp(ret_char
, mapping_name_test
),
536 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
538 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
540 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
541 "Append simple event to trace stream");
543 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
544 "bt_ctf_stream_get_packet_context handles NULL correctly");
545 packet_context
= bt_ctf_stream_get_packet_context(stream
);
547 "bt_ctf_stream_get_packet_context returns a packet context");
549 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
551 ok(packet_context_field
,
552 "Packet context contains the default packet_size field.");
553 bt_ctf_field_put(packet_context_field
);
554 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
556 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
557 "Custom packet context field value successfully set.");
559 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
560 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
561 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
562 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
563 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
564 "Successfully set a stream's packet context");
566 ok(bt_ctf_stream_flush(stream
) == 0,
567 "Flush trace stream with one event");
569 bt_ctf_event_class_put(simple_event_class
);
570 bt_ctf_event_put(simple_event
);
571 bt_ctf_field_type_put(uint_12_type
);
572 bt_ctf_field_type_put(int_64_type
);
573 bt_ctf_field_type_put(float_type
);
574 bt_ctf_field_type_put(enum_type
);
575 bt_ctf_field_type_put(enum_type_unsigned
);
576 bt_ctf_field_type_put(returned_type
);
577 bt_ctf_field_put(integer_field
);
578 bt_ctf_field_put(float_field
);
579 bt_ctf_field_put(enum_field
);
580 bt_ctf_field_put(enum_field_unsigned
);
581 bt_ctf_field_put(enum_container_field
);
582 bt_ctf_field_put(enum_container_field_unsigned
);
583 bt_ctf_field_put(packet_context
);
584 bt_ctf_field_put(packet_context_field
);
587 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
588 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
591 const char *complex_test_event_string
= "Complex Test Event";
592 const char *test_string
= "Test string";
593 struct bt_ctf_field_type
*uint_35_type
=
594 bt_ctf_field_type_integer_create(35);
595 struct bt_ctf_field_type
*int_16_type
=
596 bt_ctf_field_type_integer_create(16);
597 struct bt_ctf_field_type
*uint_3_type
=
598 bt_ctf_field_type_integer_create(3);
599 struct bt_ctf_field_type
*enum_variant_type
=
600 bt_ctf_field_type_enumeration_create(uint_3_type
);
601 struct bt_ctf_field_type
*variant_type
=
602 bt_ctf_field_type_variant_create(enum_variant_type
,
604 struct bt_ctf_field_type
*string_type
=
605 bt_ctf_field_type_string_create();
606 struct bt_ctf_field_type
*sequence_type
;
607 struct bt_ctf_field_type
*array_type
;
608 struct bt_ctf_field_type
*inner_structure_type
=
609 bt_ctf_field_type_structure_create();
610 struct bt_ctf_field_type
*complex_structure_type
=
611 bt_ctf_field_type_structure_create();
612 struct bt_ctf_field_type
*ret_field_type
;
613 struct bt_ctf_event_class
*event_class
;
614 struct bt_ctf_event
*event
;
615 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
616 *inner_structure_field
, *complex_structure_field
,
617 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
618 *variant_field
, *an_array_field
, *ret_field
;
619 uint64_t ret_unsigned_int
;
620 int64_t ret_signed_int
;
621 const char *ret_string
;
623 struct bt_ctf_stream_class
*ret_stream_class
;
624 struct bt_ctf_event_class
*ret_event_class
;
625 struct bt_ctf_field
*packet_context
, *packet_context_field
;
627 bt_ctf_field_type_set_alignment(int_16_type
, 32);
628 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
629 bt_ctf_field_type_integer_set_base(uint_35_type
,
630 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
632 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
633 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
636 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
637 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
638 ret_field_type
= bt_ctf_field_type_array_get_element_type(
640 ok(ret_field_type
== int_16_type
,
641 "bt_ctf_field_type_array_get_element_type returns the correct type");
642 bt_ctf_field_type_put(ret_field_type
);
644 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
645 "bt_ctf_field_type_array_get_length handles NULL correctly");
646 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
647 "bt_ctf_field_type_array_get_length returns the correct length");
649 bt_ctf_field_type_structure_add_field(inner_structure_type
,
650 uint_35_type
, "seq_len");
651 bt_ctf_field_type_structure_add_field(inner_structure_type
,
652 sequence_type
, "a_sequence");
653 bt_ctf_field_type_structure_add_field(inner_structure_type
,
654 array_type
, "an_array");
656 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
658 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
660 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
661 "UINT35_TYPE", 2, 7);
663 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
, "INT16_TYPE",
665 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
666 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type
, NULL
,
668 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
669 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type
, "INT16_TYPE",
671 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL index correctly");
672 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type
, "INT16_TYPE",
674 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns a value");
676 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
678 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1, &ret_size_t
) < 0,
679 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
680 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 1, NULL
) < 0,
681 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL index correctly");
682 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42, &ret_size_t
) < 0,
683 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
684 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5, &ret_size_t
) == 0,
685 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
687 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
689 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
690 "An unknown entry"), "Reject a variant field based on an unknown tag value");
691 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
692 "UINT3_TYPE") == 0, "Add a field to a variant");
693 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
695 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
698 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
699 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
700 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
701 ok(ret_field_type
== enum_variant_type
,
702 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
703 bt_ctf_field_type_put(ret_field_type
);
705 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
706 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
707 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
708 ok(!strcmp(ret_string
, "variant_selector"),
709 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
710 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
711 "INT16_TYPE") == NULL
,
712 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
713 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
715 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
716 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
717 variant_type
, "INT16_TYPE");
718 ok(ret_field_type
== int_16_type
,
719 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
720 bt_ctf_field_type_put(ret_field_type
);
722 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
723 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
724 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
725 "bt_ctf_field_type_variant_get_field_count returns the correct count");
727 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
728 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
729 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) < 0,
730 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
731 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) < 0,
732 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
733 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
734 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
735 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
736 "bt_ctf_field_type_variant_get_field returns a field");
737 ok(!strcmp("INT16_TYPE", ret_string
),
738 "bt_ctf_field_type_variant_get_field returns a correct field name");
739 ok(ret_field_type
== int_16_type
,
740 "bt_ctf_field_type_variant_get_field returns a correct field type");
741 bt_ctf_field_type_put(ret_field_type
);
743 bt_ctf_field_type_structure_add_field(complex_structure_type
,
744 enum_variant_type
, "variant_selector");
745 bt_ctf_field_type_structure_add_field(complex_structure_type
,
746 string_type
, "a_string");
747 bt_ctf_field_type_structure_add_field(complex_structure_type
,
748 variant_type
, "variant_value");
749 bt_ctf_field_type_structure_add_field(complex_structure_type
,
750 inner_structure_type
, "inner_structure");
752 ok(bt_ctf_event_class_create("clock") == NULL
,
753 "Reject creation of an event class with an illegal name");
754 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
755 ok(event_class
, "Create an event class");
756 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
757 "Reject addition of a field with an empty name to an event");
758 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
759 "Reject addition of a field with a NULL type to an event");
760 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
762 "Reject addition of a type with an illegal name to an event");
763 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
765 "Add field of type unsigned integer to an event");
766 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
767 "int_16") == 0, "Add field of type signed integer to an event");
768 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
769 "complex_structure") == 0,
770 "Add composite structure to an event");
772 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
773 "bt_ctf_event_class_get_name handles NULL correctly");
774 ret_string
= bt_ctf_event_class_get_name(event_class
);
775 ok(!strcmp(ret_string
, complex_test_event_string
),
776 "bt_ctf_event_class_get_name returns a correct name");
777 ok(bt_ctf_event_class_get_id(event_class
) < 0,
778 "bt_ctf_event_class_get_id returns a negative value when not set");
779 ok(bt_ctf_event_class_get_id(NULL
) < 0,
780 "bt_ctf_event_class_get_id handles NULL correctly");
781 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
782 "bt_ctf_event_class_set_id handles NULL correctly");
783 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
784 "Set an event class' id");
785 ok(bt_ctf_event_class_get_id(event_class
) == 42,
786 "bt_ctf_event_class_get_id returns the correct value");
788 /* Add event class to the stream class */
789 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
790 "Reject addition of NULL event class to a stream class");
791 ok(bt_ctf_stream_class_add_event_class(stream_class
,
792 event_class
) == 0, "Add an event class to stream class");
794 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
795 "bt_ctf_event_class_get_stream_class handles NULL correctly");
796 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
797 ok(ret_stream_class
== stream_class
,
798 "bt_ctf_event_class_get_stream_class returns the correct stream class");
799 bt_ctf_stream_class_put(ret_stream_class
);
801 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
802 "bt_ctf_event_class_get_field_count handles NULL correctly");
803 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
804 "bt_ctf_event_class_get_field_count returns a correct value");
806 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
807 &ret_field_type
, 0) < 0,
808 "bt_ctf_event_class_get_field handles a NULL event class correctly");
809 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
810 &ret_field_type
, 0) < 0,
811 "bt_ctf_event_class_get_field handles a NULL field name correctly");
812 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
814 "bt_ctf_event_class_get_field handles a NULL field type correctly");
815 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
816 &ret_field_type
, 42) < 0,
817 "bt_ctf_event_class_get_field handles an invalid index correctly");
818 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
819 &ret_field_type
, 0) == 0,
820 "bt_ctf_event_class_get_field returns a field");
821 ok(ret_field_type
== uint_35_type
,
822 "bt_ctf_event_class_get_field returns a correct field type");
823 bt_ctf_field_type_put(ret_field_type
);
824 ok(!strcmp(ret_string
, "uint_35"),
825 "bt_ctf_event_class_get_field returns a correct field name");
826 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
827 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
828 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
829 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
830 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
831 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
832 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
833 "complex_structure");
834 ok(ret_field_type
== complex_structure_type
,
835 "bt_ctf_event_class_get_field_by_name returns a correct field type");
836 bt_ctf_field_type_put(ret_field_type
);
838 event
= bt_ctf_event_create(event_class
);
839 ok(event
, "Instanciate a complex event");
841 ok(bt_ctf_event_get_class(NULL
) == NULL
,
842 "bt_ctf_event_get_class handles NULL correctly");
843 ret_event_class
= bt_ctf_event_get_class(event
);
844 ok(ret_event_class
== event_class
,
845 "bt_ctf_event_get_class returns the correct event class");
846 bt_ctf_event_class_put(ret_event_class
);
848 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
849 if (!uint_35_field
) {
850 printf("uint_35_field is NULL\n");
853 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
854 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
855 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
856 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
857 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
858 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
859 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
860 &ret_unsigned_int
) == 0,
861 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
862 ok(ret_unsigned_int
== 0x0DDF00D,
863 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
864 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
865 &ret_signed_int
) < 0,
866 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
867 bt_ctf_field_put(uint_35_field
);
869 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
870 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
871 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
872 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
873 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
874 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
875 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
876 &ret_signed_int
) == 0,
877 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
878 ok(ret_signed_int
== -12345,
879 "bt_ctf_field_signed_integer_get_value returns the correct value");
880 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
881 &ret_unsigned_int
) < 0,
882 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
883 bt_ctf_field_put(int_16_field
);
885 complex_structure_field
= bt_ctf_event_get_payload(event
,
886 "complex_structure");
888 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
889 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
890 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
891 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
892 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
893 complex_structure_field
, 3);
894 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
895 bt_ctf_field_put(inner_structure_field
);
896 ok(ret_field_type
== inner_structure_type
,
897 "bt_ctf_field_structure_get_field_by_index returns a correct field");
898 bt_ctf_field_type_put(ret_field_type
);
900 inner_structure_field
= bt_ctf_field_structure_get_field(
901 complex_structure_field
, "inner_structure");
902 a_string_field
= bt_ctf_field_structure_get_field(
903 complex_structure_field
, "a_string");
904 enum_variant_field
= bt_ctf_field_structure_get_field(
905 complex_structure_field
, "variant_selector");
906 variant_field
= bt_ctf_field_structure_get_field(
907 complex_structure_field
, "variant_value");
908 uint_35_field
= bt_ctf_field_structure_get_field(
909 inner_structure_field
, "seq_len");
910 a_sequence_field
= bt_ctf_field_structure_get_field(
911 inner_structure_field
, "a_sequence");
912 an_array_field
= bt_ctf_field_structure_get_field(
913 inner_structure_field
, "an_array");
915 enum_container_field
= bt_ctf_field_enumeration_get_container(
917 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
918 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
920 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
921 bt_ctf_field_put(int_16_field
);
922 ok(!bt_ctf_field_string_get_value(a_string_field
),
923 "bt_ctf_field_string_get_value returns NULL on an unset field");
924 bt_ctf_field_string_set_value(a_string_field
,
926 ok(!bt_ctf_field_string_get_value(NULL
),
927 "bt_ctf_field_string_get_value correctly handles NULL");
928 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
929 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
930 ok(ret_string
? !strcmp(ret_string
, test_string
) : 0,
931 "bt_ctf_field_string_get_value returns a correct value");
932 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
933 SEQUENCE_TEST_LENGTH
);
935 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
936 enum_container_field
) == NULL
,
937 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
938 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
940 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
941 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
942 variant_type
, enum_variant_field
);
943 ok(ret_field_type
== int_16_type
,
944 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
946 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
947 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
948 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
949 uint_35_field
) == 0, "Set a sequence field's length");
950 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
951 ok(ret_field
== uint_35_field
,
952 "bt_ctf_field_sequence_get_length returns the correct length field");
953 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
954 "bt_ctf_field_sequence_get_length properly handles NULL");
956 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
957 int_16_field
= bt_ctf_field_sequence_get_field(
958 a_sequence_field
, i
);
959 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
960 bt_ctf_field_put(int_16_field
);
963 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
964 int_16_field
= bt_ctf_field_array_get_field(
966 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
967 bt_ctf_field_put(int_16_field
);
970 bt_ctf_clock_set_time(clock
, ++current_time
);
971 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
972 "Append a complex event to a stream");
975 * Populate the custom packet context field with a dummy value
976 * otherwise flush will fail.
978 packet_context
= bt_ctf_stream_get_packet_context(stream
);
979 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
981 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
983 ok(bt_ctf_stream_flush(stream
) == 0,
984 "Flush a stream containing a complex event");
986 bt_ctf_field_put(uint_35_field
);
987 bt_ctf_field_put(a_string_field
);
988 bt_ctf_field_put(inner_structure_field
);
989 bt_ctf_field_put(complex_structure_field
);
990 bt_ctf_field_put(a_sequence_field
);
991 bt_ctf_field_put(an_array_field
);
992 bt_ctf_field_put(enum_variant_field
);
993 bt_ctf_field_put(enum_container_field
);
994 bt_ctf_field_put(variant_field
);
995 bt_ctf_field_put(ret_field
);
996 bt_ctf_field_put(packet_context_field
);
997 bt_ctf_field_put(packet_context
);
998 bt_ctf_field_type_put(uint_35_type
);
999 bt_ctf_field_type_put(int_16_type
);
1000 bt_ctf_field_type_put(string_type
);
1001 bt_ctf_field_type_put(sequence_type
);
1002 bt_ctf_field_type_put(array_type
);
1003 bt_ctf_field_type_put(inner_structure_type
);
1004 bt_ctf_field_type_put(complex_structure_type
);
1005 bt_ctf_field_type_put(uint_3_type
);
1006 bt_ctf_field_type_put(enum_variant_type
);
1007 bt_ctf_field_type_put(variant_type
);
1008 bt_ctf_field_type_put(ret_field_type
);
1009 bt_ctf_event_class_put(event_class
);
1010 bt_ctf_event_put(event
);
1013 void type_field_tests()
1015 struct bt_ctf_field
*uint_12
;
1016 struct bt_ctf_field
*int_16
;
1017 struct bt_ctf_field
*string
;
1018 struct bt_ctf_field
*enumeration
;
1019 struct bt_ctf_field_type
*composite_structure_type
;
1020 struct bt_ctf_field_type
*structure_seq_type
;
1021 struct bt_ctf_field_type
*string_type
;
1022 struct bt_ctf_field_type
*sequence_type
;
1023 struct bt_ctf_field_type
*uint_8_type
;
1024 struct bt_ctf_field_type
*int_16_type
;
1025 struct bt_ctf_field_type
*uint_12_type
=
1026 bt_ctf_field_type_integer_create(12);
1027 struct bt_ctf_field_type
*enumeration_type
;
1028 struct bt_ctf_field_type
*enumeration_sequence_type
;
1029 struct bt_ctf_field_type
*enumeration_array_type
;
1030 struct bt_ctf_field_type
*returned_type
;
1031 const char *ret_string
;
1033 returned_type
= bt_ctf_field_get_type(NULL
);
1034 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1036 ok(uint_12_type
, "Create an unsigned integer type");
1037 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1038 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1039 "Set integer type's base as binary");
1040 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1041 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1042 "Set integer type's base as decimal");
1043 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1044 BT_CTF_INTEGER_BASE_UNKNOWN
),
1045 "Reject integer type's base set as unknown");
1046 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1047 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1048 "Set integer type's base as octal");
1049 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1050 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1051 "Set integer type's base as hexadecimal");
1052 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1053 "Reject unknown integer base value");
1054 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1055 "Set integer type signedness to signed");
1056 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1057 "Set integer type signedness to unsigned");
1058 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1059 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1060 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1061 "bt_ctf_field_type_integer_get_size returns a correct value");
1062 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1063 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1064 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1065 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1067 ok(bt_ctf_field_type_set_byte_order(NULL
,
1068 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1069 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1070 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1071 (enum bt_ctf_byte_order
) 42) < 0,
1072 "bt_ctf_field_type_set_byte_order rejects invalid values");
1073 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1074 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1075 "Set an integer's byte order to little endian");
1076 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1077 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1078 "Set an integer's byte order to big endian");
1079 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1080 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1081 "bt_ctf_field_type_get_byte_order returns a correct value");
1082 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1083 BT_CTF_BYTE_ORDER_UNKNOWN
,
1084 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1086 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1088 "bt_ctf_field_type_get_type_id handles NULL correctly");
1089 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1091 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1093 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1094 BT_CTF_INTEGER_BASE_UNKNOWN
,
1095 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1096 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1097 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1098 "bt_ctf_field_type_integer_get_base returns a correct value");
1100 ok(bt_ctf_field_type_integer_set_encoding(NULL
, CTF_STRING_ASCII
) < 0,
1101 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1102 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1103 (enum ctf_string_encoding
) 123) < 0,
1104 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1105 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1106 CTF_STRING_UTF8
) == 0,
1107 "Set integer type encoding to UTF8");
1108 ok(bt_ctf_field_type_integer_get_encoding(NULL
) == CTF_STRING_UNKNOWN
,
1109 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1110 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) == CTF_STRING_UTF8
,
1111 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1113 int_16_type
= bt_ctf_field_type_integer_create(16);
1114 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1115 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1116 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1117 uint_8_type
= bt_ctf_field_type_integer_create(8);
1119 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1120 ok(sequence_type
, "Create a sequence of int16_t type");
1121 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1123 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1125 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1126 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1127 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1129 ok(!strcmp(ret_string
, "seq_len"),
1130 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1131 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1132 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1133 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1135 ok(returned_type
== int_16_type
,
1136 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1137 bt_ctf_field_type_put(returned_type
);
1139 string_type
= bt_ctf_field_type_string_create();
1140 ok(string_type
, "Create a string type");
1141 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1143 "Reject invalid \"None\" string encoding");
1144 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1146 "Reject invalid string encoding");
1147 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1148 CTF_STRING_ASCII
) == 0,
1149 "Set string encoding to ASCII");
1151 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1153 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1154 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1156 "bt_ctf_field_type_string_get_encoding returns the correct value");
1158 structure_seq_type
= bt_ctf_field_type_structure_create();
1159 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1161 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1162 ok(structure_seq_type
, "Create a structure type");
1163 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1164 uint_8_type
, "seq_len") == 0,
1165 "Add a uint8_t type to a structure");
1166 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1167 sequence_type
, "a_sequence") == 0,
1168 "Add a sequence type to a structure");
1170 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1171 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1172 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1173 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1175 ok(bt_ctf_field_type_structure_get_field(NULL
,
1176 &ret_string
, &returned_type
, 1) < 0,
1177 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1178 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1179 NULL
, &returned_type
, 1) < 0,
1180 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1181 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1182 &ret_string
, NULL
, 1) < 0,
1183 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1184 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1185 &ret_string
, &returned_type
, 10) < 0,
1186 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1187 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1188 &ret_string
, &returned_type
, 1) == 0,
1189 "bt_ctf_field_type_structure_get_field returns a field");
1190 ok(!strcmp(ret_string
, "a_sequence"),
1191 "bt_ctf_field_type_structure_get_field returns a correct field name");
1192 ok(returned_type
== sequence_type
,
1193 "bt_ctf_field_type_structure_get_field returns a correct field type");
1194 bt_ctf_field_type_put(returned_type
);
1196 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1197 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1198 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1199 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1200 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1201 structure_seq_type
, "a_sequence");
1202 ok(returned_type
== sequence_type
,
1203 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1204 bt_ctf_field_type_put(returned_type
);
1206 composite_structure_type
= bt_ctf_field_type_structure_create();
1207 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1208 string_type
, "a_string") == 0,
1209 "Add a string type to a structure");
1210 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1211 structure_seq_type
, "inner_structure") == 0,
1212 "Add a structure type to a structure");
1214 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1215 NULL
, "a_sequence") == NULL
,
1216 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1217 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1218 structure_seq_type
, NULL
) == NULL
,
1219 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1220 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1221 structure_seq_type
, "a_sequence");
1222 ok(returned_type
== sequence_type
,
1223 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1224 bt_ctf_field_type_put(returned_type
);
1226 int_16
= bt_ctf_field_create(int_16_type
);
1227 ok(int_16
, "Instanciate a signed 16-bit integer");
1228 uint_12
= bt_ctf_field_create(uint_12_type
);
1229 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1230 returned_type
= bt_ctf_field_get_type(int_16
);
1231 ok(returned_type
== int_16_type
,
1232 "bt_ctf_field_get_type returns the correct type");
1234 /* Can't modify types after instanciating them */
1235 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1236 BT_CTF_INTEGER_BASE_DECIMAL
),
1237 "Check an integer type' base can't be modified after instanciation");
1238 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1239 "Check an integer type's signedness can't be modified after instanciation");
1241 /* Check signed property is checked */
1242 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1243 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1244 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1245 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1247 /* Check overflows are properly tested for */
1248 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1249 "Check -32768 is allowed for a signed 16-bit integer");
1250 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1251 "Check 32767 is allowed for a signed 16-bit integer");
1252 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1253 "Check 32768 is not allowed for a signed 16-bit integer");
1254 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1255 "Check -32769 is not allowed for a signed 16-bit integer");
1256 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1257 "Check -42 is allowed for a signed 16-bit integer");
1259 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1260 "Check 4095 is allowed for an unsigned 12-bit integer");
1261 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1262 "Check 4096 is not allowed for a unsigned 12-bit integer");
1263 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1264 "Check 0 is allowed for an unsigned 12-bit integer");
1266 string
= bt_ctf_field_create(string_type
);
1267 ok(string
, "Instanciate a string field");
1268 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1269 "Set a string's value");
1271 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1272 ok(enumeration_type
,
1273 "Create an enumeration type with an unsigned 12-bit integer as container");
1274 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
1275 enumeration_type
, "count");
1276 ok(!enumeration_sequence_type
,
1277 "Check enumeration types are validated when creating a sequence");
1278 enumeration_array_type
= bt_ctf_field_type_array_create(
1279 enumeration_type
, 10);
1280 ok(!enumeration_array_type
,
1281 "Check enumeration types are validated when creating an array");
1282 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1283 enumeration_type
, "enumeration"),
1284 "Check enumeration types are validated when adding them as structure members");
1285 enumeration
= bt_ctf_field_create(enumeration_type
);
1287 "Check enumeration types are validated before instantiation");
1289 bt_ctf_field_put(string
);
1290 bt_ctf_field_put(uint_12
);
1291 bt_ctf_field_put(int_16
);
1292 bt_ctf_field_put(enumeration
);
1293 bt_ctf_field_type_put(composite_structure_type
);
1294 bt_ctf_field_type_put(structure_seq_type
);
1295 bt_ctf_field_type_put(string_type
);
1296 bt_ctf_field_type_put(sequence_type
);
1297 bt_ctf_field_type_put(uint_8_type
);
1298 bt_ctf_field_type_put(int_16_type
);
1299 bt_ctf_field_type_put(uint_12_type
);
1300 bt_ctf_field_type_put(enumeration_type
);
1301 bt_ctf_field_type_put(enumeration_sequence_type
);
1302 bt_ctf_field_type_put(enumeration_array_type
);
1303 bt_ctf_field_type_put(returned_type
);
1306 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1307 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1310 * Append enough events to force the underlying packet to be resized.
1311 * Also tests that a new event can be declared after a stream has been
1312 * instantiated and used/flushed.
1316 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1318 struct bt_ctf_field_type
*integer_type
=
1319 bt_ctf_field_type_integer_create(17);
1320 struct bt_ctf_field_type
*string_type
=
1321 bt_ctf_field_type_string_create();
1322 struct bt_ctf_event
*event
;
1323 struct bt_ctf_field
*ret_field
;
1324 struct bt_ctf_field_type
*ret_field_type
;
1325 uint64_t ret_uint64
;
1326 int events_appended
= 0;
1327 struct bt_ctf_field
*packet_context
, *packet_context_field
;
1329 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1331 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1333 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1334 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1339 event
= bt_ctf_event_create(event_class
);
1340 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
1341 ret_field_type
= bt_ctf_field_get_type(ret_field
);
1342 ok(ret_field_type
== integer_type
,
1343 "bt_ctf_event_get_payload_by_index returns a correct field");
1344 bt_ctf_field_type_put(ret_field_type
);
1345 bt_ctf_field_put(ret_field
);
1347 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
1348 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1349 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
1350 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1351 bt_ctf_event_put(event
);
1353 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
1354 event
= bt_ctf_event_create(event_class
);
1355 struct bt_ctf_field
*integer
=
1356 bt_ctf_field_create(integer_type
);
1357 struct bt_ctf_field
*string
=
1358 bt_ctf_field_create(string_type
);
1360 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1361 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1362 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1364 bt_ctf_field_put(integer
);
1365 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1366 ret
|= bt_ctf_event_set_payload(event
, "a_string",
1368 bt_ctf_field_put(string
);
1369 ret
|= bt_ctf_stream_append_event(stream
, event
);
1370 bt_ctf_event_put(event
);
1377 events_appended
= 1;
1378 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
1379 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
1380 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
1381 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
1382 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
1383 ok(ret
== 0 && ret_uint64
== 0,
1384 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
1385 bt_ctf_stream_append_discarded_events(stream
, 1000);
1386 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
1387 ok(ret
== 0 && ret_uint64
== 1000,
1388 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
1391 ok(events_appended
, "Append 100 000 events to a stream");
1394 * Populate the custom packet context field with a dummy value
1395 * otherwise flush will fail.
1397 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1398 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1400 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
1402 ok(bt_ctf_stream_flush(stream
) == 0,
1403 "Flush a stream that forces a packet resize");
1404 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
1405 ok(ret
== 0 && ret_uint64
== 1000,
1406 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
1407 bt_ctf_field_type_put(integer_type
);
1408 bt_ctf_field_type_put(string_type
);
1409 bt_ctf_field_put(packet_context
);
1410 bt_ctf_field_put(packet_context_field
);
1411 bt_ctf_event_class_put(event_class
);
1414 int main(int argc
, char **argv
)
1416 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
1417 char metadata_path
[sizeof(trace_path
) + 9];
1418 const char *clock_name
= "test_clock";
1419 const char *clock_description
= "This is a test clock";
1420 const char *returned_clock_name
;
1421 const char *returned_clock_description
;
1422 const uint64_t frequency
= 1123456789;
1423 const uint64_t offset_s
= 1351530929945824323;
1424 const uint64_t offset
= 1234567;
1425 const uint64_t precision
= 10;
1426 const int is_absolute
= 0xFF;
1427 char *metadata_string
;
1428 struct bt_ctf_writer
*writer
;
1429 struct utsname name
;
1430 char hostname
[BABELTRACE_HOST_NAME_MAX
];
1431 struct bt_ctf_clock
*clock
, *ret_clock
;
1432 struct bt_ctf_stream_class
*stream_class
;
1433 struct bt_ctf_stream
*stream1
;
1434 const char *ret_string
;
1435 const unsigned char *ret_uuid
;
1436 unsigned char tmp_uuid
[16] = { 0 };
1437 struct bt_ctf_field_type
*packet_context_type
, *packet_context_field_type
;
1438 struct bt_ctf_trace
*trace
;
1442 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
1448 if (!mkdtemp(trace_path
)) {
1452 strcpy(metadata_path
, trace_path
);
1453 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
1455 writer
= bt_ctf_writer_create(trace_path
);
1456 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
1458 /* Add environment context to the trace */
1459 ret
= gethostname(hostname
, sizeof(hostname
));
1463 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
1464 "Add host (%s) environment field to writer instance",
1466 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
1468 "bt_ctf_writer_add_environment_field error with NULL writer");
1469 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
1471 "bt_ctf_writer_add_environment_field error with NULL field name");
1472 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
1474 "bt_ctf_writer_add_environment_field error with NULL field value");
1481 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
1482 == 0, "Add sysname (%s) environment field to writer instance",
1484 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
1485 name
.nodename
) == 0,
1486 "Add nodename (%s) environment field to writer instance",
1488 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
1489 == 0, "Add release (%s) environment field to writer instance",
1491 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
1492 == 0, "Add version (%s) environment field to writer instance",
1494 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
1495 == 0, "Add machine (%s) environment field to writer istance",
1498 /* Define a clock and add it to the trace */
1499 ok(bt_ctf_clock_create("signed") == NULL
,
1500 "Illegal clock name rejected");
1501 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
1502 clock
= bt_ctf_clock_create(clock_name
);
1503 ok(clock
, "Clock created sucessfully");
1504 returned_clock_name
= bt_ctf_clock_get_name(clock
);
1505 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
1506 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
1507 "Returned clock name is valid");
1509 returned_clock_description
= bt_ctf_clock_get_description(clock
);
1510 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
1511 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
1512 "Clock description set successfully");
1514 returned_clock_description
= bt_ctf_clock_get_description(clock
);
1515 ok(returned_clock_description
,
1516 "bt_ctf_clock_get_description returns a description.");
1517 ok(returned_clock_description
?
1518 !strcmp(returned_clock_description
, clock_description
) : 0,
1519 "Returned clock description is valid");
1521 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
1522 "bt_ctf_clock_get_frequency returns the correct default frequency");
1523 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
1524 "Set clock frequency");
1525 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
1526 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
1528 ok(bt_ctf_clock_get_offset_s(clock
) == DEFAULT_CLOCK_OFFSET_S
,
1529 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
1530 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
1531 "Set clock offset (seconds)");
1532 ok(bt_ctf_clock_get_offset_s(clock
) == offset_s
,
1533 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
1535 ok(bt_ctf_clock_get_offset(clock
) == DEFAULT_CLOCK_OFFSET
,
1536 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
1537 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
1538 ok(bt_ctf_clock_get_offset(clock
) == offset
,
1539 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
1541 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
1542 "bt_ctf_clock_get_precision returns the correct default precision");
1543 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
1544 "Set clock precision");
1545 ok(bt_ctf_clock_get_precision(clock
) == precision
,
1546 "bt_ctf_clock_get_precision returns the correct precision once it is set");
1548 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
1549 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
1550 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
1551 "Set clock absolute property");
1552 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
1553 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
1555 ok(bt_ctf_clock_get_time(clock
) == DEFAULT_CLOCK_TIME
,
1556 "bt_ctf_clock_get_time returns the correct default time");
1557 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
1559 ok(bt_ctf_clock_get_time(clock
) == current_time
,
1560 "bt_ctf_clock_get_time returns the correct time once it is set");
1562 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
1563 "Add clock to writer instance");
1564 ok(bt_ctf_writer_add_clock(writer
, clock
),
1565 "Verify a clock can't be added twice to a writer instance");
1567 ok(!bt_ctf_writer_get_trace(NULL
),
1568 "bt_ctf_writer_get_trace correctly handles NULL");
1569 trace
= bt_ctf_writer_get_trace(writer
);
1571 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
1572 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
1573 "bt_ctf_trace_get_clock_count correctly handles NULL");
1574 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
1575 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
1576 ok(!bt_ctf_trace_get_clock(NULL
, 0),
1577 "bt_ctf_trace_get_clock correctly handles NULL");
1578 ok(!bt_ctf_trace_get_clock(trace
, -1),
1579 "bt_ctf_trace_get_clock correctly handles negative indexes");
1580 ok(!bt_ctf_trace_get_clock(trace
, 1),
1581 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
1582 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
1583 ok(ret_clock
== clock
,
1584 "bt_ctf_trace_get_clock returns the right clock instance");
1585 bt_ctf_clock_put(ret_clock
);
1587 ok(!bt_ctf_clock_get_name(NULL
),
1588 "bt_ctf_clock_get_name correctly handles NULL");
1589 ok(!bt_ctf_clock_get_description(NULL
),
1590 "bt_ctf_clock_get_description correctly handles NULL");
1591 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
1592 "bt_ctf_clock_get_frequency correctly handles NULL");
1593 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
1594 "bt_ctf_clock_get_precision correctly handles NULL");
1595 ok(bt_ctf_clock_get_offset_s(NULL
) == -1ULL,
1596 "bt_ctf_clock_get_offset_s correctly handles NULL");
1597 ok(bt_ctf_clock_get_offset(NULL
) == -1ULL,
1598 "bt_ctf_clock_get_offset correctly handles NULL");
1599 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
1600 "bt_ctf_clock_get_is_absolute correctly handles NULL");
1601 ok(bt_ctf_clock_get_time(NULL
) == -1ULL,
1602 "bt_ctf_clock_get_time correctly handles NULL");
1604 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
1605 "bt_ctf_clock_set_description correctly handles NULL clock");
1606 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
1607 "bt_ctf_clock_set_frequency correctly handles NULL clock");
1608 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
1609 "bt_ctf_clock_get_precision correctly handles NULL clock");
1610 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
1611 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
1612 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
1613 "bt_ctf_clock_set_offset correctly handles NULL clock");
1614 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
1615 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
1616 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
1617 "bt_ctf_clock_set_time correctly handles NULL clock");
1618 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
1619 "bt_ctf_clock_get_uuid correctly handles NULL clock");
1620 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
1622 "bt_ctf_clock_get_uuid returns a UUID");
1624 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
1625 /* Slightly modify UUID */
1626 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
1629 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
1630 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
1631 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
1632 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
1633 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
1634 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
1635 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
1637 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
1638 ok(uuid_match(ret_uuid
, tmp_uuid
),
1639 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
1641 /* Define a stream class */
1642 stream_class
= bt_ctf_stream_class_create("test_stream");
1644 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
1645 "bt_ctf_stream_class_get_name handles NULL correctly");
1646 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
1647 ok(!strcmp(ret_string
, "test_stream"),
1648 "bt_ctf_stream_class_get_name returns a correct stream class name");
1650 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
1651 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
1652 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
1653 "bt_ctf_stream_class_get_clock handles NULL correctly");
1655 ok(stream_class
, "Create stream class");
1656 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
1657 "Set a stream class' clock");
1658 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
1659 ok(ret_clock
== clock
,
1660 "bt_ctf_stream_class_get_clock returns a correct clock");
1661 bt_ctf_clock_put(ret_clock
);
1663 /* Test the event fields and event types APIs */
1666 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
1667 "bt_ctf_stream_class_get_id returns an error when no id is set");
1668 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
1669 "bt_ctf_stream_class_get_id handles NULL correctly");
1670 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
1671 "bt_ctf_stream_class_set_id handles NULL correctly");
1672 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
1673 "Set an stream class' id");
1674 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
1675 "bt_ctf_stream_class_get_id returns the correct value");
1677 /* Create a "uint5_t" equivalent custom packet context field */
1678 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
1680 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
1681 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
1683 /* Add a custom field to the stream class' packet context */
1684 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
1685 ok(packet_context_type
,
1686 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
1687 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == CTF_TYPE_STRUCT
,
1688 "Packet context is a structure");
1690 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
1691 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
1692 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
1693 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
1694 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
1695 packet_context_field_type
, "custom_field");
1696 ok(ret
== 0, "Packet context field added successfully");
1699 /* Instantiate a stream and append events */
1700 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
1701 ok(stream1
, "Instanciate a stream class from writer");
1704 * Try to modify the packet context type after a stream has been
1707 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
1708 packet_context_field_type
, "should_fail");
1710 "Packet context type can't be modified once a stream class has been instanciated");
1712 /* Should fail after instanciating a stream (locked)*/
1713 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
1714 "Changes to a stream class that was already instantiated fail");
1716 append_simple_event(stream_class
, stream1
, clock
);
1718 packet_resize_test(stream_class
, stream1
, clock
);
1720 append_complex_event(stream_class
, stream1
, clock
);
1722 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
1723 ok(metadata_string
, "Get metadata string");
1725 bt_ctf_writer_flush_metadata(writer
);
1726 validate_metadata(argv
[1], metadata_path
);
1727 validate_trace(argv
[2], trace_path
);
1729 bt_ctf_clock_put(clock
);
1730 bt_ctf_stream_class_put(stream_class
);
1731 bt_ctf_writer_put(writer
);
1732 bt_ctf_stream_put(stream1
);
1733 bt_ctf_field_type_put(packet_context_type
);
1734 bt_ctf_field_type_put(packet_context_field_type
);
1735 bt_ctf_trace_put(trace
);
1736 free(metadata_string
);
1738 /* Remove all trace files and delete temporary trace directory */
1739 DIR *trace_dir
= opendir(trace_path
);
1741 perror("# opendir");
1745 struct dirent
*entry
;
1746 while ((entry
= readdir(trace_dir
))) {
1747 if (entry
->d_type
== DT_REG
) {
1748 unlinkat(dirfd(trace_dir
), entry
->d_name
, 0);
1753 closedir(trace_dir
);