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>
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 void validate_metadata(char *parser_path
, char *metadata_path
)
62 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
63 int parser_output_fd
= -1, metadata_fd
= -1;
70 parser_output_fd
= mkstemp(parser_output_path
);
71 metadata_fd
= open(metadata_path
, O_RDONLY
);
73 unlink(parser_output_path
);
75 if (parser_output_fd
== -1 || metadata_fd
== -1) {
76 diag("Failed create temporary files for metadata parsing.");
84 waitpid(pid
, &status
, 0);
85 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
87 /* ctf-parser-test expects a metadata string on stdin. */
88 ret
= dup2(metadata_fd
, STDIN_FILENO
);
90 perror("# dup2 metadata_fd to STDIN");
94 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
96 perror("# dup2 parser_output_fd to STDOUT");
100 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
102 perror("# dup2 parser_output_fd to STDERR");
106 execl(parser_path
, "ctf-parser-test", NULL
);
107 perror("# Could not launch the ctf metadata parser process");
111 ok(ret
== 0, "Metadata string is valid");
113 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
115 size_t len
= METADATA_LINE_SIZE
;
116 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
118 metadata_fp
= fdopen(metadata_fd
, "r");
120 perror("fdopen on metadata_fd");
125 parser_output_fp
= fdopen(parser_output_fd
, "r");
126 if (!parser_output_fp
) {
127 perror("fdopen on parser_output_fd");
130 parser_output_fd
= -1;
134 diag("malloc failure");
139 /* Output the metadata and parser output as diagnostic */
140 while (getline(&line
, &len
, metadata_fp
) > 0) {
144 rewind(parser_output_fp
);
145 while (getline(&line
, &len
, parser_output_fp
) > 0) {
152 if (fclose(metadata_fp
)) {
153 diag("fclose failure");
156 if (parser_output_fp
) {
157 if (fclose(parser_output_fp
)) {
158 diag("fclose failure");
163 if (parser_output_fd
>= 0) {
164 if (close(parser_output_fd
)) {
168 if (metadata_fd
>= 0) {
169 if (close(metadata_fd
)) {
175 void validate_trace(char *parser_path
, char *trace_path
)
178 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
179 int babeltrace_output_fd
= -1;
186 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
187 unlink(babeltrace_output_path
);
189 if (babeltrace_output_fd
== -1) {
190 diag("Failed to create a temporary file for trace parsing.");
198 waitpid(pid
, &status
, 0);
199 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
201 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
203 perror("# dup2 babeltrace_output_fd to STDOUT");
207 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
209 perror("# dup2 babeltrace_output_fd to STDERR");
213 execl(parser_path
, "babeltrace", trace_path
, NULL
);
214 perror("# Could not launch the babeltrace process");
218 ok(ret
== 0, "Babeltrace could read the resulting trace");
220 if (ret
&& babeltrace_output_fd
>= 0) {
222 size_t len
= METADATA_LINE_SIZE
;
223 FILE *babeltrace_output_fp
= NULL
;
225 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
226 if (!babeltrace_output_fp
) {
227 perror("fdopen on babeltrace_output_fd");
230 babeltrace_output_fd
= -1;
234 diag("malloc error");
236 rewind(babeltrace_output_fp
);
237 while (getline(&line
, &len
, babeltrace_output_fp
) > 0) {
243 if (babeltrace_output_fp
) {
244 if (fclose(babeltrace_output_fp
)) {
245 diag("fclose error");
250 if (babeltrace_output_fd
>= 0) {
251 if (close(babeltrace_output_fd
)) {
257 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
258 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
260 /* Create and add a simple event class */
261 struct bt_ctf_event_class
*simple_event_class
=
262 bt_ctf_event_class_create("Simple Event");
263 struct bt_ctf_field_type
*uint_12_type
=
264 bt_ctf_field_type_integer_create(12);
265 struct bt_ctf_field_type
*int_64_type
=
266 bt_ctf_field_type_integer_create(64);
267 struct bt_ctf_field_type
*float_type
=
268 bt_ctf_field_type_floating_point_create();
269 struct bt_ctf_field_type
*enum_type
;
270 struct bt_ctf_field_type
*enum_type_unsigned
=
271 bt_ctf_field_type_enumeration_create(uint_12_type
);
272 struct bt_ctf_field_type
*returned_type
;
273 struct bt_ctf_event
*simple_event
;
274 struct bt_ctf_field
*integer_field
;
275 struct bt_ctf_field
*float_field
;
276 struct bt_ctf_field
*enum_field
;
277 struct bt_ctf_field
*enum_field_unsigned
;
278 struct bt_ctf_field
*enum_container_field
;
279 const char *mapping_name_test
= "truie";
280 const double double_test_value
= 3.1415;
281 struct bt_ctf_field
*enum_container_field_unsigned
;
282 const char *mapping_name_negative_test
= "negative_value";
283 const char *ret_char
;
286 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
287 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
288 struct bt_ctf_clock
*ret_clock
;
289 struct bt_ctf_event_class
*ret_event_class
;
291 ok(uint_12_type
, "Create an unsigned integer type");
293 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
294 ok(int_64_type
, "Create a signed integer type");
295 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
297 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
298 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
299 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
300 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
301 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
303 bt_ctf_field_type_set_alignment(float_type
, 32);
304 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
305 "bt_ctf_field_type_get_alignment handles NULL correctly");
306 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
307 "bt_ctf_field_type_get_alignment returns a correct value");
309 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
310 "Set a floating point type's exponent digit count");
311 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
312 "Set a floating point type's mantissa digit count");
314 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
315 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
316 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
317 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
318 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
319 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
320 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
321 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
323 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
324 mapping_name_negative_test
, -12345, 0) == 0,
325 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
326 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
327 "escaping; \"test\"", 1, 1) == 0,
328 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
329 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
330 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
331 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
332 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
333 "event clock int float", 5, 22) == 0,
334 "Accept enumeration mapping strings containing reserved keywords");
335 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
337 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
338 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
339 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
340 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
341 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
342 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
343 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
345 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42, &ret_size_t
) < 0,
346 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
347 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -42, NULL
) < 0,
348 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL index correctly");
349 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000, &ret_size_t
) < 0,
350 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
351 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55, &ret_size_t
) == 0,
352 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
354 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
356 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
357 "enum_field") == 0, "Add signed enumeration field to event");
359 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
360 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
361 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
362 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
363 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
364 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
365 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
366 NULL
, &ret_range_end_int64_t
) < 0,
367 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
368 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
369 &ret_range_start_int64_t
, NULL
) < 0,
370 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
371 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
372 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
373 "bt_ctf_field_type_enumeration_get_mapping returns a value");
374 ok(!strcmp(ret_char
, mapping_name_test
),
375 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
376 ok(ret_range_start_int64_t
== 42,
377 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
378 ok(ret_range_end_int64_t
== 42,
379 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
381 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
382 "escaping; \"test\"", 0, 0) == 0,
383 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
384 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
385 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
386 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
387 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
388 "event clock int float", 5, 22) == 0,
389 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
390 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
392 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
393 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
394 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
395 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
396 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
397 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
398 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
399 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
401 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
402 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
403 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
404 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
406 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
407 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
408 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
409 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
410 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
411 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
412 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
413 NULL
, &ret_range_end_uint64_t
) < 0,
414 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
415 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
416 &ret_range_start_uint64_t
, NULL
) < 0,
417 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
418 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
419 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
420 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
421 ok(!strcmp(ret_char
, mapping_name_test
),
422 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
423 ok(ret_range_start_uint64_t
== 42,
424 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
425 ok(ret_range_end_uint64_t
== 42,
426 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
428 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
430 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
432 bt_ctf_stream_class_add_event_class(stream_class
,
435 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
436 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
437 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
438 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
439 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
440 "bt_ctf_stream_class_get_event_class handles NULL correctly");
441 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
442 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
443 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
444 ok(ret_event_class
== simple_event_class
,
445 "bt_ctf_stream_class_get_event_class returns the correct event class");
446 bt_ctf_event_class_put(ret_event_class
);
448 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
449 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
450 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
451 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
452 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
453 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
454 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
455 ok(ret_event_class
== simple_event_class
,
456 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
457 bt_ctf_event_class_put(ret_event_class
);
459 simple_event
= bt_ctf_event_create(simple_event_class
);
461 "Instantiate an event containing a single integer field");
463 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
464 "bt_ctf_event_get_clock handles NULL correctly");
465 ret_clock
= bt_ctf_event_get_clock(simple_event
);
466 ok(ret_clock
== clock
,
467 "bt_ctf_event_get_clock returns a correct clock");
468 bt_ctf_clock_put(clock
);
470 integer_field
= bt_ctf_field_create(uint_12_type
);
471 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
472 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
473 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
475 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
476 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
477 "bt_ctf_field_floating_point_get_value fails on an unset float field");
478 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
479 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
480 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
481 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
482 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
483 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
484 "bt_ctf_field_floating_point_get_value returns a double value");
485 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
486 "bt_ctf_field_floating_point_get_value returns a correct value");
488 enum_field
= bt_ctf_field_create(enum_type
);
489 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
490 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
491 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
492 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
493 enum_container_field
= bt_ctf_field_enumeration_get_container(
495 ok(bt_ctf_field_signed_integer_set_value(
496 enum_container_field
, -42) == 0,
497 "Set signed enumeration container value");
498 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
499 ok(!strcmp(ret_char
, mapping_name_negative_test
),
500 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
501 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
503 enum_field_unsigned
= bt_ctf_field_create(enum_type_unsigned
);
504 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
505 enum_field_unsigned
);
506 ok(bt_ctf_field_unsigned_integer_set_value(
507 enum_container_field_unsigned
, 42) == 0,
508 "Set unsigned enumeration container value");
509 bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
510 enum_field_unsigned
);
511 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
512 ok(!strcmp(ret_char
, mapping_name_test
),
513 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
515 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
517 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
518 "Append simple event to trace stream");
520 ok(bt_ctf_stream_flush(stream
) == 0,
521 "Flush trace stream with one event");
523 bt_ctf_event_class_put(simple_event_class
);
524 bt_ctf_event_put(simple_event
);
525 bt_ctf_field_type_put(uint_12_type
);
526 bt_ctf_field_type_put(int_64_type
);
527 bt_ctf_field_type_put(float_type
);
528 bt_ctf_field_type_put(enum_type
);
529 bt_ctf_field_type_put(enum_type_unsigned
);
530 bt_ctf_field_type_put(returned_type
);
531 bt_ctf_field_put(integer_field
);
532 bt_ctf_field_put(float_field
);
533 bt_ctf_field_put(enum_field
);
534 bt_ctf_field_put(enum_field_unsigned
);
535 bt_ctf_field_put(enum_container_field
);
536 bt_ctf_field_put(enum_container_field_unsigned
);
539 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
540 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
543 const char *complex_test_event_string
= "Complex Test Event";
544 const char *test_string
= "Test string";
545 struct bt_ctf_field_type
*uint_35_type
=
546 bt_ctf_field_type_integer_create(35);
547 struct bt_ctf_field_type
*int_16_type
=
548 bt_ctf_field_type_integer_create(16);
549 struct bt_ctf_field_type
*uint_3_type
=
550 bt_ctf_field_type_integer_create(3);
551 struct bt_ctf_field_type
*enum_variant_type
=
552 bt_ctf_field_type_enumeration_create(uint_3_type
);
553 struct bt_ctf_field_type
*variant_type
=
554 bt_ctf_field_type_variant_create(enum_variant_type
,
556 struct bt_ctf_field_type
*string_type
=
557 bt_ctf_field_type_string_create();
558 struct bt_ctf_field_type
*sequence_type
;
559 struct bt_ctf_field_type
*array_type
;
560 struct bt_ctf_field_type
*inner_structure_type
=
561 bt_ctf_field_type_structure_create();
562 struct bt_ctf_field_type
*complex_structure_type
=
563 bt_ctf_field_type_structure_create();
564 struct bt_ctf_field_type
*ret_field_type
;
565 struct bt_ctf_event_class
*event_class
;
566 struct bt_ctf_event
*event
;
567 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
568 *inner_structure_field
, *complex_structure_field
,
569 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
570 *variant_field
, *an_array_field
, *ret_field
;
571 uint64_t ret_unsigned_int
;
572 int64_t ret_signed_int
;
573 const char *ret_string
;
575 struct bt_ctf_stream_class
*ret_stream_class
;
576 struct bt_ctf_event_class
*ret_event_class
;
578 bt_ctf_field_type_set_alignment(int_16_type
, 32);
579 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
580 bt_ctf_field_type_integer_set_base(uint_35_type
,
581 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
583 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
584 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
587 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
588 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
589 ret_field_type
= bt_ctf_field_type_array_get_element_type(
591 ok(ret_field_type
== int_16_type
,
592 "bt_ctf_field_type_array_get_element_type returns the correct type");
593 bt_ctf_field_type_put(ret_field_type
);
595 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
596 "bt_ctf_field_type_array_get_length handles NULL correctly");
597 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
598 "bt_ctf_field_type_array_get_length returns the correct length");
600 bt_ctf_field_type_structure_add_field(inner_structure_type
,
601 uint_35_type
, "seq_len");
602 bt_ctf_field_type_structure_add_field(inner_structure_type
,
603 sequence_type
, "a_sequence");
604 bt_ctf_field_type_structure_add_field(inner_structure_type
,
605 array_type
, "an_array");
607 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
609 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
611 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
612 "UINT35_TYPE", 2, 7);
614 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
, "INT16_TYPE",
616 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
617 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type
, NULL
,
619 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
620 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type
, "INT16_TYPE",
622 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL index correctly");
623 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type
, "INT16_TYPE",
625 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns a value");
627 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
629 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1, &ret_size_t
) < 0,
630 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
631 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 1, NULL
) < 0,
632 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL index correctly");
633 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42, &ret_size_t
) < 0,
634 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
635 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5, &ret_size_t
) == 0,
636 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
638 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
640 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
641 "An unknown entry"), "Reject a variant field based on an unknown tag value");
642 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
643 "UINT3_TYPE") == 0, "Add a field to a variant");
644 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
646 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
649 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
650 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
651 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
652 ok(ret_field_type
== enum_variant_type
,
653 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
654 bt_ctf_field_type_put(ret_field_type
);
656 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
657 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
658 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
659 ok(!strcmp(ret_string
, "variant_selector"),
660 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
661 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
662 "INT16_TYPE") == NULL
,
663 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
664 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
666 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
667 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
668 variant_type
, "INT16_TYPE");
669 ok(ret_field_type
== int_16_type
,
670 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
671 bt_ctf_field_type_put(ret_field_type
);
673 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
674 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
675 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
676 "bt_ctf_field_type_variant_get_field_count returns the correct count");
678 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
679 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
680 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) < 0,
681 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
682 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) < 0,
683 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
684 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
685 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
686 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
687 "bt_ctf_field_type_variant_get_field returns a field");
688 ok(!strcmp("INT16_TYPE", ret_string
),
689 "bt_ctf_field_type_variant_get_field returns a correct field name");
690 ok(ret_field_type
== int_16_type
,
691 "bt_ctf_field_type_variant_get_field returns a correct field type");
692 bt_ctf_field_type_put(ret_field_type
);
694 bt_ctf_field_type_structure_add_field(complex_structure_type
,
695 enum_variant_type
, "variant_selector");
696 bt_ctf_field_type_structure_add_field(complex_structure_type
,
697 string_type
, "a_string");
698 bt_ctf_field_type_structure_add_field(complex_structure_type
,
699 variant_type
, "variant_value");
700 bt_ctf_field_type_structure_add_field(complex_structure_type
,
701 inner_structure_type
, "inner_structure");
703 ok(bt_ctf_event_class_create("clock") == NULL
,
704 "Reject creation of an event class with an illegal name");
705 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
706 ok(event_class
, "Create an event class");
707 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
708 "Reject addition of a field with an empty name to an event");
709 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
710 "Reject addition of a field with a NULL type to an event");
711 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
713 "Reject addition of a type with an illegal name to an event");
714 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
716 "Add field of type unsigned integer to an event");
717 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
718 "int_16") == 0, "Add field of type signed integer to an event");
719 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
720 "complex_structure") == 0,
721 "Add composite structure to an event");
723 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
724 "bt_ctf_event_class_get_name handles NULL correctly");
725 ret_string
= bt_ctf_event_class_get_name(event_class
);
726 ok(!strcmp(ret_string
, complex_test_event_string
),
727 "bt_ctf_event_class_get_name returns a correct name");
728 ok(bt_ctf_event_class_get_id(event_class
) < 0,
729 "bt_ctf_event_class_get_id returns a negative value when not set");
730 ok(bt_ctf_event_class_get_id(NULL
) < 0,
731 "bt_ctf_event_class_get_id handles NULL correctly");
732 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
733 "bt_ctf_event_class_set_id handles NULL correctly");
734 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
735 "Set an event class' id");
736 ok(bt_ctf_event_class_get_id(event_class
) == 42,
737 "bt_ctf_event_class_get_id returns the correct value");
739 /* Add event class to the stream class */
740 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
741 "Reject addition of NULL event class to a stream class");
742 ok(bt_ctf_stream_class_add_event_class(stream_class
,
743 event_class
) == 0, "Add an event class to stream class");
745 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
746 "bt_ctf_event_class_get_stream_class handles NULL correctly");
747 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
748 ok(ret_stream_class
== stream_class
,
749 "bt_ctf_event_class_get_stream_class returns the correct stream class");
750 bt_ctf_stream_class_put(ret_stream_class
);
752 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
753 "bt_ctf_event_class_get_field_count handles NULL correctly");
754 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
755 "bt_ctf_event_class_get_field_count returns a correct value");
757 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
758 &ret_field_type
, 0) < 0,
759 "bt_ctf_event_class_get_field handles a NULL event class correctly");
760 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
761 &ret_field_type
, 0) < 0,
762 "bt_ctf_event_class_get_field handles a NULL field name correctly");
763 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
765 "bt_ctf_event_class_get_field handles a NULL field type correctly");
766 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
767 &ret_field_type
, 42) < 0,
768 "bt_ctf_event_class_get_field handles an invalid index correctly");
769 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
770 &ret_field_type
, 0) == 0,
771 "bt_ctf_event_class_get_field returns a field");
772 ok(ret_field_type
== uint_35_type
,
773 "bt_ctf_event_class_get_field returns a correct field type");
774 bt_ctf_field_type_put(ret_field_type
);
775 ok(!strcmp(ret_string
, "uint_35"),
776 "bt_ctf_event_class_get_field returns a correct field name");
777 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
778 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
779 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
780 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
781 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
782 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
783 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
784 "complex_structure");
785 ok(ret_field_type
== complex_structure_type
,
786 "bt_ctf_event_class_get_field_by_name returns a correct field type");
787 bt_ctf_field_type_put(ret_field_type
);
789 event
= bt_ctf_event_create(event_class
);
790 ok(event
, "Instanciate a complex event");
792 ok(bt_ctf_event_get_class(NULL
) == NULL
,
793 "bt_ctf_event_get_class handles NULL correctly");
794 ret_event_class
= bt_ctf_event_get_class(event
);
795 ok(ret_event_class
== event_class
,
796 "bt_ctf_event_get_class returns the correct event class");
797 bt_ctf_event_class_put(ret_event_class
);
799 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
800 if (!uint_35_field
) {
801 printf("uint_35_field is NULL\n");
804 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
805 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
806 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) == -1,
807 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
808 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) == -1,
809 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
810 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
811 &ret_unsigned_int
) == 0,
812 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
813 ok(ret_unsigned_int
== 0x0DDF00D,
814 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
815 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
816 &ret_signed_int
) == -1,
817 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
818 bt_ctf_field_put(uint_35_field
);
820 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
821 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
822 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) == -1,
823 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
824 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) == -1,
825 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
826 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
827 &ret_signed_int
) == 0,
828 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
829 ok(ret_signed_int
== -12345,
830 "bt_ctf_field_signed_integer_get_value returns the correct value");
831 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
832 &ret_unsigned_int
) == -1,
833 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
834 bt_ctf_field_put(int_16_field
);
836 complex_structure_field
= bt_ctf_event_get_payload(event
,
837 "complex_structure");
839 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
840 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
841 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
842 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
843 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
844 complex_structure_field
, 3);
845 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
846 bt_ctf_field_put(inner_structure_field
);
847 ok(ret_field_type
== inner_structure_type
,
848 "bt_ctf_field_structure_get_field_by_index returns a correct field");
849 bt_ctf_field_type_put(ret_field_type
);
851 inner_structure_field
= bt_ctf_field_structure_get_field(
852 complex_structure_field
, "inner_structure");
853 a_string_field
= bt_ctf_field_structure_get_field(
854 complex_structure_field
, "a_string");
855 enum_variant_field
= bt_ctf_field_structure_get_field(
856 complex_structure_field
, "variant_selector");
857 variant_field
= bt_ctf_field_structure_get_field(
858 complex_structure_field
, "variant_value");
859 uint_35_field
= bt_ctf_field_structure_get_field(
860 inner_structure_field
, "seq_len");
861 a_sequence_field
= bt_ctf_field_structure_get_field(
862 inner_structure_field
, "a_sequence");
863 an_array_field
= bt_ctf_field_structure_get_field(
864 inner_structure_field
, "an_array");
866 enum_container_field
= bt_ctf_field_enumeration_get_container(
868 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
869 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
871 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
872 bt_ctf_field_put(int_16_field
);
873 ok(!bt_ctf_field_string_get_value(a_string_field
),
874 "bt_ctf_field_string_get_value returns NULL on an unset field");
875 bt_ctf_field_string_set_value(a_string_field
,
877 ok(!bt_ctf_field_string_get_value(NULL
),
878 "bt_ctf_field_string_get_value correctly handles NULL");
879 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
880 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
881 ok(!strcmp(ret_string
, test_string
),
882 "bt_ctf_field_string_get_value returns a correct value");
883 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
884 SEQUENCE_TEST_LENGTH
);
886 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
887 enum_container_field
) == NULL
,
888 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
889 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
891 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
892 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
893 variant_type
, enum_variant_field
);
894 ok(ret_field_type
== int_16_type
,
895 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
897 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
898 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
899 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
900 uint_35_field
) == 0, "Set a sequence field's length");
901 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
902 ok(ret_field
== uint_35_field
,
903 "bt_ctf_field_sequence_get_length returns the correct length field");
904 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
905 "bt_ctf_field_sequence_get_length properly handles NULL");
907 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
908 int_16_field
= bt_ctf_field_sequence_get_field(
909 a_sequence_field
, i
);
910 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
911 bt_ctf_field_put(int_16_field
);
914 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
915 int_16_field
= bt_ctf_field_array_get_field(
917 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
918 bt_ctf_field_put(int_16_field
);
921 bt_ctf_clock_set_time(clock
, ++current_time
);
922 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
923 "Append a complex event to a stream");
924 ok(bt_ctf_stream_flush(stream
) == 0,
925 "Flush a stream containing a complex event");
927 bt_ctf_field_put(uint_35_field
);
928 bt_ctf_field_put(a_string_field
);
929 bt_ctf_field_put(inner_structure_field
);
930 bt_ctf_field_put(complex_structure_field
);
931 bt_ctf_field_put(a_sequence_field
);
932 bt_ctf_field_put(an_array_field
);
933 bt_ctf_field_put(enum_variant_field
);
934 bt_ctf_field_put(enum_container_field
);
935 bt_ctf_field_put(variant_field
);
936 bt_ctf_field_put(ret_field
);
937 bt_ctf_field_type_put(uint_35_type
);
938 bt_ctf_field_type_put(int_16_type
);
939 bt_ctf_field_type_put(string_type
);
940 bt_ctf_field_type_put(sequence_type
);
941 bt_ctf_field_type_put(array_type
);
942 bt_ctf_field_type_put(inner_structure_type
);
943 bt_ctf_field_type_put(complex_structure_type
);
944 bt_ctf_field_type_put(uint_3_type
);
945 bt_ctf_field_type_put(enum_variant_type
);
946 bt_ctf_field_type_put(variant_type
);
947 bt_ctf_field_type_put(ret_field_type
);
948 bt_ctf_event_class_put(event_class
);
949 bt_ctf_event_put(event
);
952 void type_field_tests()
954 struct bt_ctf_field
*uint_12
;
955 struct bt_ctf_field
*int_16
;
956 struct bt_ctf_field
*string
;
957 struct bt_ctf_field
*enumeration
;
958 struct bt_ctf_field_type
*composite_structure_type
;
959 struct bt_ctf_field_type
*structure_seq_type
;
960 struct bt_ctf_field_type
*string_type
;
961 struct bt_ctf_field_type
*sequence_type
;
962 struct bt_ctf_field_type
*uint_8_type
;
963 struct bt_ctf_field_type
*int_16_type
;
964 struct bt_ctf_field_type
*uint_12_type
=
965 bt_ctf_field_type_integer_create(12);
966 struct bt_ctf_field_type
*enumeration_type
;
967 struct bt_ctf_field_type
*enumeration_sequence_type
;
968 struct bt_ctf_field_type
*enumeration_array_type
;
969 struct bt_ctf_field_type
*returned_type
;
970 const char *ret_string
;
972 returned_type
= bt_ctf_field_get_type(NULL
);
973 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
975 ok(uint_12_type
, "Create an unsigned integer type");
976 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
977 BT_CTF_INTEGER_BASE_BINARY
) == 0,
978 "Set integer type's base as binary");
979 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
980 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
981 "Set integer type's base as decimal");
982 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
983 BT_CTF_INTEGER_BASE_UNKNOWN
),
984 "Reject integer type's base set as unknown");
985 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
986 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
987 "Set integer type's base as octal");
988 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
989 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
990 "Set integer type's base as hexadecimal");
991 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
992 "Reject unknown integer base value");
993 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
994 "Set integer type signedness to signed");
995 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
996 "Set integer type signedness to unsigned");
997 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
998 "bt_ctf_field_type_integer_get_size handles NULL correctly");
999 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1000 "bt_ctf_field_type_integer_get_size returns a correct value");
1001 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1002 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1003 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1004 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1006 ok(bt_ctf_field_type_set_byte_order(NULL
,
1007 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1008 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1009 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1010 (enum bt_ctf_byte_order
) 42) < 0,
1011 "bt_ctf_field_type_set_byte_order rejects invalid values");
1012 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1013 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1014 "Set an integer's byte order to little endian");
1015 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1016 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1017 "Set an integer's byte order to big endian");
1018 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1019 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1020 "bt_ctf_field_type_get_byte_order returns a correct value");
1021 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1022 BT_CTF_BYTE_ORDER_UNKNOWN
,
1023 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1025 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1027 "bt_ctf_field_type_get_type_id handles NULL correctly");
1028 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1030 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1032 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1033 BT_CTF_INTEGER_BASE_UNKNOWN
,
1034 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1035 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1036 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1037 "bt_ctf_field_type_integer_get_base returns a correct value");
1039 ok(bt_ctf_field_type_integer_set_encoding(NULL
, CTF_STRING_ASCII
) < 0,
1040 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1041 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1042 (enum ctf_string_encoding
) 123) < 0,
1043 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1044 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1045 CTF_STRING_UTF8
) == 0,
1046 "Set integer type encoding to UTF8");
1047 ok(bt_ctf_field_type_integer_get_encoding(NULL
) == CTF_STRING_UNKNOWN
,
1048 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1049 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) == CTF_STRING_UTF8
,
1050 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1052 int_16_type
= bt_ctf_field_type_integer_create(16);
1053 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1054 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1055 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1056 uint_8_type
= bt_ctf_field_type_integer_create(8);
1058 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1059 ok(sequence_type
, "Create a sequence of int16_t type");
1060 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1062 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1064 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1065 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1066 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1068 ok(!strcmp(ret_string
, "seq_len"),
1069 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1070 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1071 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1072 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1074 ok(returned_type
== int_16_type
,
1075 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1076 bt_ctf_field_type_put(returned_type
);
1078 string_type
= bt_ctf_field_type_string_create();
1079 ok(string_type
, "Create a string type");
1080 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1082 "Reject invalid \"None\" string encoding");
1083 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1085 "Reject invalid string encoding");
1086 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1087 CTF_STRING_ASCII
) == 0,
1088 "Set string encoding to ASCII");
1090 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1092 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1093 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1095 "bt_ctf_field_type_string_get_encoding returns the correct value");
1097 structure_seq_type
= bt_ctf_field_type_structure_create();
1098 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1100 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1101 ok(structure_seq_type
, "Create a structure type");
1102 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1103 uint_8_type
, "seq_len") == 0,
1104 "Add a uint8_t type to a structure");
1105 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1106 sequence_type
, "a_sequence") == 0,
1107 "Add a sequence type to a structure");
1109 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1110 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1111 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1112 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1114 ok(bt_ctf_field_type_structure_get_field(NULL
,
1115 &ret_string
, &returned_type
, 1) < 0,
1116 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1117 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1118 NULL
, &returned_type
, 1) < 0,
1119 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1120 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1121 &ret_string
, NULL
, 1) < 0,
1122 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1123 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1124 &ret_string
, &returned_type
, 10) < 0,
1125 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1126 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1127 &ret_string
, &returned_type
, 1) == 0,
1128 "bt_ctf_field_type_structure_get_field returns a field");
1129 ok(!strcmp(ret_string
, "a_sequence"),
1130 "bt_ctf_field_type_structure_get_field returns a correct field name");
1131 ok(returned_type
== sequence_type
,
1132 "bt_ctf_field_type_structure_get_field returns a correct field type");
1133 bt_ctf_field_type_put(returned_type
);
1135 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1136 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1137 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1138 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1139 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1140 structure_seq_type
, "a_sequence");
1141 ok(returned_type
== sequence_type
,
1142 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1143 bt_ctf_field_type_put(returned_type
);
1145 composite_structure_type
= bt_ctf_field_type_structure_create();
1146 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1147 string_type
, "a_string") == 0,
1148 "Add a string type to a structure");
1149 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1150 structure_seq_type
, "inner_structure") == 0,
1151 "Add a structure type to a structure");
1153 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1154 NULL
, "a_sequence") == NULL
,
1155 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1156 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1157 structure_seq_type
, NULL
) == NULL
,
1158 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1159 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1160 structure_seq_type
, "a_sequence");
1161 ok(returned_type
== sequence_type
,
1162 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1163 bt_ctf_field_type_put(returned_type
);
1165 int_16
= bt_ctf_field_create(int_16_type
);
1166 ok(int_16
, "Instanciate a signed 16-bit integer");
1167 uint_12
= bt_ctf_field_create(uint_12_type
);
1168 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1169 returned_type
= bt_ctf_field_get_type(int_16
);
1170 ok(returned_type
== int_16_type
,
1171 "bt_ctf_field_get_type returns the correct type");
1173 /* Can't modify types after instanciating them */
1174 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1175 BT_CTF_INTEGER_BASE_DECIMAL
),
1176 "Check an integer type' base can't be modified after instanciation");
1177 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1178 "Check an integer type's signedness can't be modified after instanciation");
1180 /* Check signed property is checked */
1181 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1182 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1183 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1184 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1186 /* Check overflows are properly tested for */
1187 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1188 "Check -32768 is allowed for a signed 16-bit integer");
1189 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1190 "Check 32767 is allowed for a signed 16-bit integer");
1191 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1192 "Check 32768 is not allowed for a signed 16-bit integer");
1193 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1194 "Check -32769 is not allowed for a signed 16-bit integer");
1195 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1196 "Check -42 is allowed for a signed 16-bit integer");
1198 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1199 "Check 4095 is allowed for an unsigned 12-bit integer");
1200 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1201 "Check 4096 is not allowed for a unsigned 12-bit integer");
1202 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1203 "Check 0 is allowed for an unsigned 12-bit integer");
1205 string
= bt_ctf_field_create(string_type
);
1206 ok(string
, "Instanciate a string field");
1207 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1208 "Set a string's value");
1210 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1211 ok(enumeration_type
,
1212 "Create an enumeration type with an unsigned 12-bit integer as container");
1213 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
1214 enumeration_type
, "count");
1215 ok(!enumeration_sequence_type
,
1216 "Check enumeration types are validated when creating a sequence");
1217 enumeration_array_type
= bt_ctf_field_type_array_create(
1218 enumeration_type
, 10);
1219 ok(!enumeration_array_type
,
1220 "Check enumeration types are validated when creating an array");
1221 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1222 enumeration_type
, "enumeration") == 0,
1223 "Check enumeration types are validated when adding them as structure members");
1224 enumeration
= bt_ctf_field_create(enumeration_type
);
1226 "Check enumeration types are validated before instantiation");
1228 bt_ctf_field_put(string
);
1229 bt_ctf_field_put(uint_12
);
1230 bt_ctf_field_put(int_16
);
1231 bt_ctf_field_put(enumeration
);
1232 bt_ctf_field_type_put(composite_structure_type
);
1233 bt_ctf_field_type_put(structure_seq_type
);
1234 bt_ctf_field_type_put(string_type
);
1235 bt_ctf_field_type_put(sequence_type
);
1236 bt_ctf_field_type_put(uint_8_type
);
1237 bt_ctf_field_type_put(int_16_type
);
1238 bt_ctf_field_type_put(uint_12_type
);
1239 bt_ctf_field_type_put(enumeration_type
);
1240 bt_ctf_field_type_put(enumeration_sequence_type
);
1241 bt_ctf_field_type_put(enumeration_array_type
);
1242 bt_ctf_field_type_put(returned_type
);
1245 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1246 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1249 * Append enough events to force the underlying packet to be resized.
1250 * Also tests that a new event can be declared after a stream has been
1251 * instantiated and used/flushed.
1255 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1257 struct bt_ctf_field_type
*integer_type
=
1258 bt_ctf_field_type_integer_create(17);
1259 struct bt_ctf_field_type
*string_type
=
1260 bt_ctf_field_type_string_create();
1261 struct bt_ctf_event
*event
;
1262 struct bt_ctf_field
*ret_field
;
1263 struct bt_ctf_field_type
*ret_field_type
;
1265 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1267 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1269 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1270 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1275 event
= bt_ctf_event_create(event_class
);
1276 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
1277 ret_field_type
= bt_ctf_field_get_type(ret_field
);
1278 ok(ret_field_type
== integer_type
,
1279 "bt_ctf_event_get_payload_by_index returns a correct field");
1280 bt_ctf_field_type_put(ret_field_type
);
1281 bt_ctf_field_put(ret_field
);
1283 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
1284 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1285 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
1286 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1287 bt_ctf_event_put(event
);
1289 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
1290 event
= bt_ctf_event_create(event_class
);
1291 struct bt_ctf_field
*integer
=
1292 bt_ctf_field_create(integer_type
);
1293 struct bt_ctf_field
*string
=
1294 bt_ctf_field_create(string_type
);
1296 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1297 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1298 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1300 bt_ctf_field_put(integer
);
1301 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1302 ret
|= bt_ctf_event_set_payload(event
, "a_string",
1304 bt_ctf_field_put(string
);
1305 ret
|= bt_ctf_stream_append_event(stream
, event
);
1306 bt_ctf_event_put(event
);
1313 ok(ret
== 0, "Append 100 000 events to a stream");
1314 ok(bt_ctf_stream_flush(stream
) == 0,
1315 "Flush a stream that forces a packet resize");
1316 bt_ctf_field_type_put(integer_type
);
1317 bt_ctf_field_type_put(string_type
);
1318 bt_ctf_event_class_put(event_class
);
1321 int main(int argc
, char **argv
)
1323 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
1324 char metadata_path
[sizeof(trace_path
) + 9];
1325 const char *clock_name
= "test_clock";
1326 const char *clock_description
= "This is a test clock";
1327 const char *returned_clock_name
;
1328 const char *returned_clock_description
;
1329 const uint64_t frequency
= 1123456789;
1330 const uint64_t offset_s
= 1351530929945824323;
1331 const uint64_t offset
= 1234567;
1332 const uint64_t precision
= 10;
1333 const int is_absolute
= 0xFF;
1334 char *metadata_string
;
1335 struct bt_ctf_writer
*writer
;
1336 struct utsname name
;
1337 char hostname
[HOST_NAME_MAX
];
1338 struct bt_ctf_clock
*clock
, *ret_clock
;
1339 struct bt_ctf_stream_class
*stream_class
;
1340 struct bt_ctf_stream
*stream1
;
1341 const char *ret_string
;
1344 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
1350 if (!mkdtemp(trace_path
)) {
1354 strcpy(metadata_path
, trace_path
);
1355 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
1357 writer
= bt_ctf_writer_create(trace_path
);
1358 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
1360 /* Add environment context to the trace */
1361 gethostname(hostname
, HOST_NAME_MAX
);
1362 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
1363 "Add host (%s) environment field to writer instance",
1365 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
1367 "bt_ctf_writer_add_environment_field error with NULL writer");
1368 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
1370 "bt_ctf_writer_add_environment_field error with NULL field name");
1371 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
1373 "bt_ctf_writer_add_environment_field error with NULL field value");
1380 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
1381 == 0, "Add sysname (%s) environment field to writer instance",
1383 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
1384 name
.nodename
) == 0,
1385 "Add nodename (%s) environment field to writer instance",
1387 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
1388 == 0, "Add release (%s) environment field to writer instance",
1390 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
1391 == 0, "Add version (%s) environment field to writer instance",
1393 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
1394 == 0, "Add machine (%s) environment field to writer istance",
1397 /* Define a clock and add it to the trace */
1398 ok(bt_ctf_clock_create("signed") == NULL
,
1399 "Illegal clock name rejected");
1400 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
1401 clock
= bt_ctf_clock_create(clock_name
);
1402 ok(clock
, "Clock created sucessfully");
1403 returned_clock_name
= bt_ctf_clock_get_name(clock
);
1404 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
1405 ok(strcmp(returned_clock_name
, clock_name
) == 0,
1406 "Returned clock name is valid");
1408 returned_clock_description
= bt_ctf_clock_get_description(clock
);
1409 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
1410 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
1411 "Clock description set successfully");
1413 returned_clock_description
= bt_ctf_clock_get_description(clock
);
1414 ok(returned_clock_description
,
1415 "bt_ctf_clock_get_description returns a description.");
1416 ok(strcmp(returned_clock_description
, clock_description
) == 0,
1417 "Returned clock description is valid");
1419 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
1420 "bt_ctf_clock_get_frequency returns the correct default frequency");
1421 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
1422 "Set clock frequency");
1423 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
1424 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
1426 ok(bt_ctf_clock_get_offset_s(clock
) == DEFAULT_CLOCK_OFFSET_S
,
1427 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
1428 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
1429 "Set clock offset (seconds)");
1430 ok(bt_ctf_clock_get_offset_s(clock
) == offset_s
,
1431 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
1433 ok(bt_ctf_clock_get_offset(clock
) == DEFAULT_CLOCK_OFFSET
,
1434 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
1435 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
1436 ok(bt_ctf_clock_get_offset(clock
) == offset
,
1437 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
1439 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
1440 "bt_ctf_clock_get_precision returns the correct default precision");
1441 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
1442 "Set clock precision");
1443 ok(bt_ctf_clock_get_precision(clock
) == precision
,
1444 "bt_ctf_clock_get_precision returns the correct precision once it is set");
1446 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
1447 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
1448 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
1449 "Set clock absolute property");
1450 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
1451 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
1453 ok(bt_ctf_clock_get_time(clock
) == DEFAULT_CLOCK_TIME
,
1454 "bt_ctf_clock_get_time returns the correct default time");
1455 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
1457 ok(bt_ctf_clock_get_time(clock
) == current_time
,
1458 "bt_ctf_clock_get_time returns the correct time once it is set");
1460 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
1461 "Add clock to writer instance");
1462 ok(bt_ctf_writer_add_clock(writer
, clock
),
1463 "Verify a clock can't be added twice to a writer instance");
1465 ok(!bt_ctf_clock_get_name(NULL
),
1466 "bt_ctf_clock_get_name correctly handles NULL");
1467 ok(!bt_ctf_clock_get_description(NULL
),
1468 "bt_ctf_clock_get_description correctly handles NULL");
1469 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
1470 "bt_ctf_clock_get_frequency correctly handles NULL");
1471 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
1472 "bt_ctf_clock_get_precision correctly handles NULL");
1473 ok(bt_ctf_clock_get_offset_s(NULL
) == -1ULL,
1474 "bt_ctf_clock_get_offset_s correctly handles NULL");
1475 ok(bt_ctf_clock_get_offset(NULL
) == -1ULL,
1476 "bt_ctf_clock_get_offset correctly handles NULL");
1477 ok(bt_ctf_clock_get_is_absolute(NULL
) == -1,
1478 "bt_ctf_clock_get_is_absolute correctly handles NULL");
1479 ok(bt_ctf_clock_get_time(NULL
) == -1ULL,
1480 "bt_ctf_clock_get_time correctly handles NULL");
1482 ok(bt_ctf_clock_set_description(NULL
, NULL
) == -1,
1483 "bt_ctf_clock_set_description correctly handles NULL clock");
1484 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) == -1,
1485 "bt_ctf_clock_set_frequency correctly handles NULL clock");
1486 ok(bt_ctf_clock_set_precision(NULL
, precision
) == -1,
1487 "bt_ctf_clock_get_precision correctly handles NULL clock");
1488 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) == -1,
1489 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
1490 ok(bt_ctf_clock_set_offset(NULL
, offset
) == -1,
1491 "bt_ctf_clock_set_offset correctly handles NULL clock");
1492 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) == -1,
1493 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
1494 ok(bt_ctf_clock_set_time(NULL
, current_time
) == -1,
1495 "bt_ctf_clock_set_time correctly handles NULL clock");
1497 /* Define a stream class */
1498 stream_class
= bt_ctf_stream_class_create("test_stream");
1500 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
1501 "bt_ctf_stream_class_get_name handles NULL correctly");
1502 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
1503 ok(!strcmp(ret_string
, "test_stream"),
1504 "bt_ctf_stream_class_get_name returns a correct stream class name");
1506 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
1507 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
1508 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
1509 "bt_ctf_stream_class_get_clock handles NULL correctly");
1511 ok(stream_class
, "Create stream class");
1512 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
1513 "Set a stream class' clock");
1514 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
1515 ok(ret_clock
== clock
,
1516 "bt_ctf_stream_class_get_clock returns a correct clock");
1517 bt_ctf_clock_put(ret_clock
);
1519 /* Test the event fields and event types APIs */
1522 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
1523 "bt_ctf_stream_class_get_id returns an error when no id is set");
1524 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
1525 "bt_ctf_stream_class_get_id handles NULL correctly");
1526 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
1527 "bt_ctf_stream_class_set_id handles NULL correctly");
1528 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
1529 "Set an stream class' id");
1530 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
1531 "bt_ctf_stream_class_get_id returns the correct value");
1533 /* Instantiate a stream and append events */
1534 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
1535 ok(stream1
, "Instanciate a stream class from writer");
1537 /* Should fail after instanciating a stream (locked)*/
1538 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
1539 "Changes to a stream class that was already instantiated fail");
1541 append_simple_event(stream_class
, stream1
, clock
);
1543 packet_resize_test(stream_class
, stream1
, clock
);
1545 append_complex_event(stream_class
, stream1
, clock
);
1547 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
1548 ok(metadata_string
, "Get metadata string");
1550 bt_ctf_writer_flush_metadata(writer
);
1551 validate_metadata(argv
[1], metadata_path
);
1552 validate_trace(argv
[2], trace_path
);
1554 bt_ctf_clock_put(clock
);
1555 bt_ctf_stream_class_put(stream_class
);
1556 bt_ctf_writer_put(writer
);
1557 bt_ctf_stream_put(stream1
);
1558 free(metadata_string
);
1560 /* Remove all trace files and delete temporary trace directory */
1561 DIR *trace_dir
= opendir(trace_path
);
1563 perror("# opendir");
1567 struct dirent
*entry
;
1568 while ((entry
= readdir(trace_dir
))) {
1569 if (entry
->d_type
== DT_REG
) {
1570 unlinkat(dirfd(trace_dir
), entry
->d_name
, 0);
1575 closedir(trace_dir
);