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 PACKET_RESIZE_TEST_LENGTH 100000
49 #define DEFAULT_CLOCK_FREQ 1000000000
50 #define DEFAULT_CLOCK_PRECISION 1
51 #define DEFAULT_CLOCK_OFFSET 0
52 #define DEFAULT_CLOCK_OFFSET_S 0
53 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
54 #define DEFAULT_CLOCK_TIME 0
56 static uint64_t current_time
= 42;
58 void validate_metadata(char *parser_path
, char *metadata_path
)
61 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
62 int parser_output_fd
= -1, metadata_fd
= -1;
69 parser_output_fd
= mkstemp(parser_output_path
);
70 metadata_fd
= open(metadata_path
, O_RDONLY
);
72 unlink(parser_output_path
);
74 if (parser_output_fd
== -1 || metadata_fd
== -1) {
75 diag("Failed create temporary files for metadata parsing.");
83 waitpid(pid
, &status
, 0);
84 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
86 /* ctf-parser-test expects a metadata string on stdin. */
87 ret
= dup2(metadata_fd
, STDIN_FILENO
);
89 perror("# dup2 metadata_fd to STDIN");
93 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
95 perror("# dup2 parser_output_fd to STDOUT");
99 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
101 perror("# dup2 parser_output_fd to STDERR");
105 execl(parser_path
, "ctf-parser-test", NULL
);
106 perror("# Could not launch the ctf metadata parser process");
110 ok(ret
== 0, "Metadata string is valid");
112 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
114 size_t len
= METADATA_LINE_SIZE
;
115 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
117 metadata_fp
= fdopen(metadata_fd
, "r");
119 perror("fdopen on metadata_fd");
124 parser_output_fp
= fdopen(parser_output_fd
, "r");
125 if (!parser_output_fp
) {
126 perror("fdopen on parser_output_fd");
129 parser_output_fd
= -1;
133 diag("malloc failure");
138 /* Output the metadata and parser output as diagnostic */
139 while (getline(&line
, &len
, metadata_fp
) > 0) {
143 rewind(parser_output_fp
);
144 while (getline(&line
, &len
, parser_output_fp
) > 0) {
151 if (fclose(metadata_fp
)) {
152 diag("fclose failure");
155 if (parser_output_fp
) {
156 if (fclose(parser_output_fp
)) {
157 diag("fclose failure");
162 if (parser_output_fd
>= 0) {
163 if (close(parser_output_fd
)) {
167 if (metadata_fd
>= 0) {
168 if (close(metadata_fd
)) {
174 void validate_trace(char *parser_path
, char *trace_path
)
177 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
178 int babeltrace_output_fd
= -1;
185 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
186 unlink(babeltrace_output_path
);
188 if (babeltrace_output_fd
== -1) {
189 diag("Failed to create a temporary file for trace parsing.");
197 waitpid(pid
, &status
, 0);
198 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
200 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
202 perror("# dup2 babeltrace_output_fd to STDOUT");
206 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
208 perror("# dup2 babeltrace_output_fd to STDERR");
212 execl(parser_path
, "babeltrace", trace_path
, NULL
);
213 perror("# Could not launch the babeltrace process");
217 ok(ret
== 0, "Babeltrace could read the resulting trace");
219 if (ret
&& babeltrace_output_fd
>= 0) {
221 size_t len
= METADATA_LINE_SIZE
;
222 FILE *babeltrace_output_fp
= NULL
;
224 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
225 if (!babeltrace_output_fp
) {
226 perror("fdopen on babeltrace_output_fd");
229 babeltrace_output_fd
= -1;
233 diag("malloc error");
235 rewind(babeltrace_output_fp
);
236 while (getline(&line
, &len
, babeltrace_output_fp
) > 0) {
242 if (babeltrace_output_fp
) {
243 if (fclose(babeltrace_output_fp
)) {
244 diag("fclose error");
249 if (babeltrace_output_fd
>= 0) {
250 if (close(babeltrace_output_fd
)) {
256 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
257 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
259 /* Create and add a simple event class */
260 struct bt_ctf_event_class
*simple_event_class
=
261 bt_ctf_event_class_create("Simple Event");
262 struct bt_ctf_field_type
*uint_12_type
=
263 bt_ctf_field_type_integer_create(12);
264 struct bt_ctf_field_type
*int_64_type
=
265 bt_ctf_field_type_integer_create(64);
266 struct bt_ctf_field_type
*float_type
=
267 bt_ctf_field_type_floating_point_create();
268 struct bt_ctf_field_type
*enum_type
;
269 struct bt_ctf_field_type
*enum_type_unsigned
=
270 bt_ctf_field_type_enumeration_create(uint_12_type
);
271 struct bt_ctf_field_type
*returned_type
;
272 struct bt_ctf_event
*simple_event
;
273 struct bt_ctf_field
*integer_field
;
274 struct bt_ctf_field
*float_field
;
275 struct bt_ctf_field
*enum_field
;
276 struct bt_ctf_field
*enum_field_unsigned
;
277 struct bt_ctf_field
*enum_container_field
;
278 const char *mapping_name_test
= "truie";
279 const double double_test_value
= 3.1415;
280 struct bt_ctf_field
*enum_container_field_unsigned
;
281 const char *mapping_name_negative_test
= "negative_value";
282 const char *ret_char
;
285 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
286 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
288 ok(uint_12_type
, "Create an unsigned integer type");
290 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
291 ok(int_64_type
, "Create a signed integer type");
292 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
294 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
295 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
296 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
297 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
298 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
300 bt_ctf_field_type_set_alignment(float_type
, 32);
301 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
302 "bt_ctf_field_type_get_alignment handles NULL correctly");
303 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
304 "bt_ctf_field_type_get_alignment returns a correct value");
306 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
307 "Set a floating point type's exponent digit count");
308 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
309 "Set a floating point type's mantissa digit count");
311 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
312 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
313 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
314 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
315 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
316 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
317 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
318 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
320 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
321 mapping_name_negative_test
, -12345, 0) == 0,
322 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
323 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
324 "escaping; \"test\"", 1, 1) == 0,
325 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
326 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
327 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
328 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
329 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
330 "event clock int float", 5, 22) == 0,
331 "Accept enumeration mapping strings containing reserved keywords");
332 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
334 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
335 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
336 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
337 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
338 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
339 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
340 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
342 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42, &ret_size_t
) < 0,
343 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
344 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -42, NULL
) < 0,
345 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL index correctly");
346 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000, &ret_size_t
) < 0,
347 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
348 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55, &ret_size_t
) == 0,
349 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
351 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
353 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
354 "enum_field") == 0, "Add signed enumeration field to event");
356 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
357 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
358 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
359 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
360 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
361 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
362 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
363 NULL
, &ret_range_end_int64_t
) < 0,
364 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
365 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
366 &ret_range_start_int64_t
, NULL
) < 0,
367 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
368 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
369 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
370 "bt_ctf_field_type_enumeration_get_mapping returns a value");
371 ok(!strcmp(ret_char
, mapping_name_test
),
372 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
373 ok(ret_range_start_int64_t
== 42,
374 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
375 ok(ret_range_end_int64_t
== 42,
376 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
378 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
379 "escaping; \"test\"", 0, 0) == 0,
380 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
381 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
382 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
383 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
384 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
385 "event clock int float", 5, 22) == 0,
386 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
387 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
389 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
390 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
391 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
392 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
393 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
394 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
395 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
396 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
398 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
399 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
400 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
401 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
403 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
404 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
405 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
406 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
407 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
408 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
409 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
410 NULL
, &ret_range_end_uint64_t
) < 0,
411 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
412 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
413 &ret_range_start_uint64_t
, NULL
) < 0,
414 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
415 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
416 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
417 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
418 ok(!strcmp(ret_char
, mapping_name_test
),
419 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
420 ok(ret_range_start_uint64_t
== 42,
421 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
422 ok(ret_range_end_uint64_t
== 42,
423 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
425 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
427 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
429 bt_ctf_stream_class_add_event_class(stream_class
,
432 simple_event
= bt_ctf_event_create(simple_event_class
);
435 "Instantiate an event containing a single integer field");
437 integer_field
= bt_ctf_field_create(uint_12_type
);
438 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
439 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
440 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
442 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
443 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
444 "bt_ctf_field_floating_point_get_value fails on an unset float field");
445 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
446 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
447 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
448 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
449 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
450 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
451 "bt_ctf_field_floating_point_get_value returns a double value");
452 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
453 "bt_ctf_field_floating_point_get_value returns a correct value");
455 enum_field
= bt_ctf_field_create(enum_type
);
456 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
457 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
458 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
459 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
460 enum_container_field
= bt_ctf_field_enumeration_get_container(
462 ok(bt_ctf_field_signed_integer_set_value(
463 enum_container_field
, -42) == 0,
464 "Set signed enumeration container value");
465 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
466 ok(!strcmp(ret_char
, mapping_name_negative_test
),
467 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
468 bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
470 enum_field_unsigned
= bt_ctf_field_create(enum_type_unsigned
);
471 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
472 enum_field_unsigned
);
473 ok(bt_ctf_field_unsigned_integer_set_value(
474 enum_container_field_unsigned
, 42) == 0,
475 "Set unsigned enumeration container value");
476 bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
477 enum_field_unsigned
);
478 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
479 ok(!strcmp(ret_char
, mapping_name_test
),
480 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
482 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
484 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
485 "Append simple event to trace stream");
487 ok(bt_ctf_stream_flush(stream
) == 0,
488 "Flush trace stream with one event");
490 bt_ctf_event_class_put(simple_event_class
);
491 bt_ctf_event_put(simple_event
);
492 bt_ctf_field_type_put(uint_12_type
);
493 bt_ctf_field_type_put(int_64_type
);
494 bt_ctf_field_type_put(float_type
);
495 bt_ctf_field_type_put(enum_type
);
496 bt_ctf_field_type_put(enum_type_unsigned
);
497 bt_ctf_field_type_put(returned_type
);
498 bt_ctf_field_put(integer_field
);
499 bt_ctf_field_put(float_field
);
500 bt_ctf_field_put(enum_field
);
501 bt_ctf_field_put(enum_field_unsigned
);
502 bt_ctf_field_put(enum_container_field
);
503 bt_ctf_field_put(enum_container_field_unsigned
);
506 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
507 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
510 const char *test_string
= "Test string";
511 struct bt_ctf_field_type
*uint_35_type
=
512 bt_ctf_field_type_integer_create(35);
513 struct bt_ctf_field_type
*int_16_type
=
514 bt_ctf_field_type_integer_create(16);
515 struct bt_ctf_field_type
*uint_3_type
=
516 bt_ctf_field_type_integer_create(3);
517 struct bt_ctf_field_type
*enum_variant_type
=
518 bt_ctf_field_type_enumeration_create(uint_3_type
);
519 struct bt_ctf_field_type
*variant_type
=
520 bt_ctf_field_type_variant_create(enum_variant_type
,
522 struct bt_ctf_field_type
*string_type
=
523 bt_ctf_field_type_string_create();
524 struct bt_ctf_field_type
*sequence_type
;
525 struct bt_ctf_field_type
*inner_structure_type
=
526 bt_ctf_field_type_structure_create();
527 struct bt_ctf_field_type
*complex_structure_type
=
528 bt_ctf_field_type_structure_create();
529 struct bt_ctf_field_type
*ret_field_type
;
530 struct bt_ctf_event_class
*event_class
;
531 struct bt_ctf_event
*event
;
532 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
533 *inner_structure_field
, *complex_structure_field
,
534 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
535 *variant_field
, *ret_field
;
536 int64_t ret_signed_int
;
537 uint64_t ret_unsigned_int
;
538 const char *ret_string
;
541 bt_ctf_field_type_set_alignment(int_16_type
, 32);
542 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
543 bt_ctf_field_type_integer_set_base(uint_35_type
,
544 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
546 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
549 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
550 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
551 ret_field_type
= bt_ctf_field_type_array_get_element_type(
553 ok(ret_field_type
== int_16_type
,
554 "bt_ctf_field_type_array_get_element_type returns the correct type");
555 bt_ctf_field_type_put(ret_field_type
);
557 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
558 "bt_ctf_field_type_array_get_length handles NULL correctly");
559 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
560 "bt_ctf_field_type_array_get_length returns the correct length");
562 bt_ctf_field_type_structure_add_field(inner_structure_type
,
563 uint_35_type
, "seq_len");
564 bt_ctf_field_type_structure_add_field(inner_structure_type
,
565 sequence_type
, "a_sequence");
567 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
569 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
571 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
572 "UINT35_TYPE", 2, 7);
574 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
, "INT16_TYPE",
576 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
577 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type
, NULL
,
579 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
580 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type
, "INT16_TYPE",
582 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL index correctly");
583 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type
, "INT16_TYPE",
585 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns a value");
587 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
589 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1, &ret_size_t
) < 0,
590 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
591 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 1, NULL
) < 0,
592 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL index correctly");
593 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42, &ret_size_t
) < 0,
594 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
595 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5, &ret_size_t
) == 0,
596 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
598 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
600 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
601 "An unknown entry"), "Reject a variant field based on an unknown tag value");
602 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
603 "UINT3_TYPE") == 0, "Add a field to a variant");
604 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
606 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
609 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
610 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
611 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
612 ok(ret_field_type
== enum_variant_type
,
613 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
614 bt_ctf_field_type_put(ret_field_type
);
616 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
617 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
618 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
619 ok(!strcmp(ret_string
, "variant_selector"),
620 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
621 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
622 "INT16_TYPE") == NULL
,
623 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
624 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
626 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
627 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
628 variant_type
, "INT16_TYPE");
629 ok(ret_field_type
== int_16_type
,
630 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
631 bt_ctf_field_type_put(ret_field_type
);
633 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
634 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
635 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
636 "bt_ctf_field_type_variant_get_field_count returns the correct count");
638 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
639 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
640 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) < 0,
641 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
642 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) < 0,
643 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
644 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
645 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
646 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
647 "bt_ctf_field_type_variant_get_field returns a field");
648 ok(!strcmp("INT16_TYPE", ret_string
),
649 "bt_ctf_field_type_variant_get_field returns a correct field name");
650 ok(ret_field_type
== int_16_type
,
651 "bt_ctf_field_type_variant_get_field returns a correct field type");
652 bt_ctf_field_type_put(ret_field_type
);
654 bt_ctf_field_type_structure_add_field(complex_structure_type
,
655 enum_variant_type
, "variant_selector");
656 bt_ctf_field_type_structure_add_field(complex_structure_type
,
657 string_type
, "a_string");
658 bt_ctf_field_type_structure_add_field(complex_structure_type
,
659 variant_type
, "variant_value");
660 bt_ctf_field_type_structure_add_field(complex_structure_type
,
661 inner_structure_type
, "inner_structure");
663 ok(bt_ctf_event_class_create("clock") == NULL
,
664 "Reject creation of an event class with an illegal name");
665 event_class
= bt_ctf_event_class_create("Complex Test Event");
666 ok(event_class
, "Create an event class");
667 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
668 "Reject addition of a field with an empty name to an event");
669 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
670 "Reject addition of a field with a NULL type to an event");
671 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
673 "Reject addition of a type with an illegal name to an event");
674 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
676 "Add field of type unsigned integer to an event");
677 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
678 "int_16") == 0, "Add field of type signed integer to an event");
679 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
680 "complex_structure") == 0,
681 "Add composite structure to an event");
683 /* Add event class to the stream class */
684 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
685 "Reject addition of NULL event class to a stream class");
686 ok(bt_ctf_stream_class_add_event_class(stream_class
,
687 event_class
) == 0, "Add an event class to stream class");
689 event
= bt_ctf_event_create(event_class
);
690 ok(event
, "Instanciate a complex event");
692 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
693 if (!uint_35_field
) {
694 printf("uint_35_field is NULL\n");
697 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
698 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
699 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) == -1,
700 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
701 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) == -1,
702 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
703 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
704 &ret_unsigned_int
) == 0,
705 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
706 ok(ret_unsigned_int
== 0x0DDF00D,
707 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
708 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
709 &ret_signed_int
) == -1,
710 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
711 bt_ctf_field_put(uint_35_field
);
713 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
714 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
715 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) == -1,
716 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
717 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) == -1,
718 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
719 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
720 &ret_signed_int
) == 0,
721 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
722 ok(ret_signed_int
== -12345,
723 "bt_ctf_field_signed_integer_get_value returns the correct value");
724 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
725 &ret_unsigned_int
) == -1,
726 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
727 bt_ctf_field_put(int_16_field
);
729 complex_structure_field
= bt_ctf_event_get_payload(event
,
730 "complex_structure");
732 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
733 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
734 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
735 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
736 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
737 complex_structure_field
, 3);
738 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
739 bt_ctf_field_put(inner_structure_field
);
740 ok(ret_field_type
== inner_structure_type
,
741 "bt_ctf_field_structure_get_field_by_index returns a correct field");
742 bt_ctf_field_type_put(ret_field_type
);
744 inner_structure_field
= bt_ctf_field_structure_get_field(
745 complex_structure_field
, "inner_structure");
746 a_string_field
= bt_ctf_field_structure_get_field(
747 complex_structure_field
, "a_string");
748 enum_variant_field
= bt_ctf_field_structure_get_field(
749 complex_structure_field
, "variant_selector");
750 variant_field
= bt_ctf_field_structure_get_field(
751 complex_structure_field
, "variant_value");
752 uint_35_field
= bt_ctf_field_structure_get_field(
753 inner_structure_field
, "seq_len");
754 a_sequence_field
= bt_ctf_field_structure_get_field(
755 inner_structure_field
, "a_sequence");
757 enum_container_field
= bt_ctf_field_enumeration_get_container(
759 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
760 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
762 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
763 bt_ctf_field_put(int_16_field
);
764 ok(!bt_ctf_field_string_get_value(a_string_field
),
765 "bt_ctf_field_string_get_value returns NULL on an unset field");
766 bt_ctf_field_string_set_value(a_string_field
,
768 ok(!bt_ctf_field_string_get_value(NULL
),
769 "bt_ctf_field_string_get_value correctly handles NULL");
770 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
771 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
772 ok(!strcmp(ret_string
, test_string
),
773 "bt_ctf_field_string_get_value returns a correct value");
774 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
775 SEQUENCE_TEST_LENGTH
);
777 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
778 enum_container_field
) == NULL
,
779 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
780 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
782 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
783 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
784 variant_type
, enum_variant_field
);
785 ok(ret_field_type
== int_16_type
,
786 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
788 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
789 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
790 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
791 uint_35_field
) == 0, "Set a sequence field's length");
792 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
793 ok(ret_field
== uint_35_field
,
794 "bt_ctf_field_sequence_get_length returns the correct length field");
795 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
796 "bt_ctf_field_sequence_get_length properly handles NULL");
798 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
799 int_16_field
= bt_ctf_field_sequence_get_field(
800 a_sequence_field
, i
);
801 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
802 bt_ctf_field_put(int_16_field
);
805 bt_ctf_clock_set_time(clock
, ++current_time
);
806 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
807 "Append a complex event to a stream");
808 ok(bt_ctf_stream_flush(stream
) == 0,
809 "Flush a stream containing a complex event");
811 bt_ctf_field_put(uint_35_field
);
812 bt_ctf_field_put(a_string_field
);
813 bt_ctf_field_put(inner_structure_field
);
814 bt_ctf_field_put(complex_structure_field
);
815 bt_ctf_field_put(a_sequence_field
);
816 bt_ctf_field_put(enum_variant_field
);
817 bt_ctf_field_put(enum_container_field
);
818 bt_ctf_field_put(variant_field
);
819 bt_ctf_field_put(ret_field
);
820 bt_ctf_field_type_put(uint_35_type
);
821 bt_ctf_field_type_put(int_16_type
);
822 bt_ctf_field_type_put(string_type
);
823 bt_ctf_field_type_put(sequence_type
);
824 bt_ctf_field_type_put(inner_structure_type
);
825 bt_ctf_field_type_put(complex_structure_type
);
826 bt_ctf_field_type_put(uint_3_type
);
827 bt_ctf_field_type_put(enum_variant_type
);
828 bt_ctf_field_type_put(variant_type
);
829 bt_ctf_field_type_put(ret_field_type
);
830 bt_ctf_event_class_put(event_class
);
831 bt_ctf_event_put(event
);
834 void type_field_tests()
836 struct bt_ctf_field
*uint_12
;
837 struct bt_ctf_field
*int_16
;
838 struct bt_ctf_field
*string
;
839 struct bt_ctf_field
*enumeration
;
840 struct bt_ctf_field_type
*composite_structure_type
;
841 struct bt_ctf_field_type
*structure_seq_type
;
842 struct bt_ctf_field_type
*string_type
;
843 struct bt_ctf_field_type
*sequence_type
;
844 struct bt_ctf_field_type
*uint_8_type
;
845 struct bt_ctf_field_type
*int_16_type
;
846 struct bt_ctf_field_type
*uint_12_type
=
847 bt_ctf_field_type_integer_create(12);
848 struct bt_ctf_field_type
*enumeration_type
;
849 struct bt_ctf_field_type
*enumeration_sequence_type
;
850 struct bt_ctf_field_type
*enumeration_array_type
;
851 struct bt_ctf_field_type
*returned_type
;
852 const char *ret_string
;
854 returned_type
= bt_ctf_field_get_type(NULL
);
855 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
857 ok(uint_12_type
, "Create an unsigned integer type");
858 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
859 BT_CTF_INTEGER_BASE_BINARY
) == 0,
860 "Set integer type's base as binary");
861 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
862 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
863 "Set integer type's base as decimal");
864 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
865 BT_CTF_INTEGER_BASE_UNKNOWN
),
866 "Reject integer type's base set as unknown");
867 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
868 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
869 "Set integer type's base as octal");
870 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
871 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
872 "Set integer type's base as hexadecimal");
873 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
874 "Reject unknown integer base value");
875 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
876 "Set integer type signedness to signed");
877 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
878 "Set integer type signedness to unsigned");
879 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
880 "bt_ctf_field_type_integer_get_size handles NULL correctly");
881 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
882 "bt_ctf_field_type_integer_get_size returns a correct value");
883 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
884 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
885 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
886 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
888 ok(bt_ctf_field_type_set_byte_order(NULL
,
889 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
890 "bt_ctf_field_type_set_byte_order handles NULL correctly");
891 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
892 (enum bt_ctf_byte_order
) 42) < 0,
893 "bt_ctf_field_type_set_byte_order rejects invalid values");
894 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
895 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
896 "Set an integer's byte order to little endian");
897 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
898 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
899 "Set an integer's byte order to big endian");
900 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
901 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
902 "bt_ctf_field_type_get_byte_order returns a correct value");
903 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
904 BT_CTF_BYTE_ORDER_UNKNOWN
,
905 "bt_ctf_field_type_get_byte_order handles NULL correctly");
907 ok(bt_ctf_field_type_get_type_id(NULL
) ==
909 "bt_ctf_field_type_get_type_id handles NULL correctly");
910 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
912 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
914 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
915 BT_CTF_INTEGER_BASE_UNKNOWN
,
916 "bt_ctf_field_type_integer_get_base handles NULL correctly");
917 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
918 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
919 "bt_ctf_field_type_integer_get_base returns a correct value");
921 ok(bt_ctf_field_type_integer_set_encoding(NULL
, CTF_STRING_ASCII
) < 0,
922 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
923 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
924 (enum ctf_string_encoding
) 123) < 0,
925 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
926 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
927 CTF_STRING_UTF8
) == 0,
928 "Set integer type encoding to UTF8");
929 ok(bt_ctf_field_type_integer_get_encoding(NULL
) == CTF_STRING_UNKNOWN
,
930 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
931 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) == CTF_STRING_UTF8
,
932 "bt_ctf_field_type_integer_get_encoding returns a correct value");
934 int_16_type
= bt_ctf_field_type_integer_create(16);
935 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
936 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
937 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
938 uint_8_type
= bt_ctf_field_type_integer_create(8);
940 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
941 ok(sequence_type
, "Create a sequence of int16_t type");
942 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
944 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
946 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
947 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
948 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
950 ok(!strcmp(ret_string
, "seq_len"),
951 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
952 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
953 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
954 returned_type
= bt_ctf_field_type_sequence_get_element_type(
956 ok(returned_type
== int_16_type
,
957 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
958 bt_ctf_field_type_put(returned_type
);
960 string_type
= bt_ctf_field_type_string_create();
961 ok(string_type
, "Create a string type");
962 ok(bt_ctf_field_type_string_set_encoding(string_type
,
964 "Reject invalid \"None\" string encoding");
965 ok(bt_ctf_field_type_string_set_encoding(string_type
,
967 "Reject invalid string encoding");
968 ok(bt_ctf_field_type_string_set_encoding(string_type
,
969 CTF_STRING_ASCII
) == 0,
970 "Set string encoding to ASCII");
972 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
974 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
975 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
977 "bt_ctf_field_type_string_get_encoding returns the correct value");
979 structure_seq_type
= bt_ctf_field_type_structure_create();
980 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
982 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
983 ok(structure_seq_type
, "Create a structure type");
984 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
985 uint_8_type
, "seq_len") == 0,
986 "Add a uint8_t type to a structure");
987 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
988 sequence_type
, "a_sequence") == 0,
989 "Add a sequence type to a structure");
991 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
992 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
993 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
994 "bt_ctf_field_type_structure_get_field_count returns a correct value");
996 ok(bt_ctf_field_type_structure_get_field(NULL
,
997 &ret_string
, &returned_type
, 1) < 0,
998 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
999 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1000 NULL
, &returned_type
, 1) < 0,
1001 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1002 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1003 &ret_string
, NULL
, 1) < 0,
1004 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1005 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1006 &ret_string
, &returned_type
, 10) < 0,
1007 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1008 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1009 &ret_string
, &returned_type
, 1) == 0,
1010 "bt_ctf_field_type_structure_get_field returns a field");
1011 ok(!strcmp(ret_string
, "a_sequence"),
1012 "bt_ctf_field_type_structure_get_field returns a correct field name");
1013 ok(returned_type
== sequence_type
,
1014 "bt_ctf_field_type_structure_get_field returns a correct field type");
1015 bt_ctf_field_type_put(returned_type
);
1017 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1018 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1019 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1020 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1021 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1022 structure_seq_type
, "a_sequence");
1023 ok(returned_type
== sequence_type
,
1024 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1025 bt_ctf_field_type_put(returned_type
);
1027 composite_structure_type
= bt_ctf_field_type_structure_create();
1028 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1029 string_type
, "a_string") == 0,
1030 "Add a string type to a structure");
1031 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1032 structure_seq_type
, "inner_structure") == 0,
1033 "Add a structure type to a structure");
1035 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1036 NULL
, "a_sequence") == NULL
,
1037 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1038 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1039 structure_seq_type
, NULL
) == NULL
,
1040 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1041 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1042 structure_seq_type
, "a_sequence");
1043 ok(returned_type
== sequence_type
,
1044 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1045 bt_ctf_field_type_put(returned_type
);
1047 int_16
= bt_ctf_field_create(int_16_type
);
1048 ok(int_16
, "Instanciate a signed 16-bit integer");
1049 uint_12
= bt_ctf_field_create(uint_12_type
);
1050 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1051 returned_type
= bt_ctf_field_get_type(int_16
);
1052 ok(returned_type
== int_16_type
,
1053 "bt_ctf_field_get_type returns the correct type");
1055 /* Can't modify types after instanciating them */
1056 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1057 BT_CTF_INTEGER_BASE_DECIMAL
),
1058 "Check an integer type' base can't be modified after instanciation");
1059 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1060 "Check an integer type's signedness can't be modified after instanciation");
1062 /* Check signed property is checked */
1063 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1064 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1065 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1066 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1068 /* Check overflows are properly tested for */
1069 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1070 "Check -32768 is allowed for a signed 16-bit integer");
1071 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1072 "Check 32767 is allowed for a signed 16-bit integer");
1073 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1074 "Check 32768 is not allowed for a signed 16-bit integer");
1075 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1076 "Check -32769 is not allowed for a signed 16-bit integer");
1077 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1078 "Check -42 is allowed for a signed 16-bit integer");
1080 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1081 "Check 4095 is allowed for an unsigned 12-bit integer");
1082 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1083 "Check 4096 is not allowed for a unsigned 12-bit integer");
1084 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1085 "Check 0 is allowed for an unsigned 12-bit integer");
1087 string
= bt_ctf_field_create(string_type
);
1088 ok(string
, "Instanciate a string field");
1089 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1090 "Set a string's value");
1092 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1093 ok(enumeration_type
,
1094 "Create an enumeration type with an unsigned 12-bit integer as container");
1095 enumeration_sequence_type
= bt_ctf_field_type_sequence_create(
1096 enumeration_type
, "count");
1097 ok(!enumeration_sequence_type
,
1098 "Check enumeration types are validated when creating a sequence");
1099 enumeration_array_type
= bt_ctf_field_type_array_create(
1100 enumeration_type
, 10);
1101 ok(!enumeration_array_type
,
1102 "Check enumeration types are validated when creating an array");
1103 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1104 enumeration_type
, "enumeration") == 0,
1105 "Check enumeration types are validated when adding them as structure members");
1106 enumeration
= bt_ctf_field_create(enumeration_type
);
1108 "Check enumeration types are validated before instantiation");
1110 bt_ctf_field_put(string
);
1111 bt_ctf_field_put(uint_12
);
1112 bt_ctf_field_put(int_16
);
1113 bt_ctf_field_put(enumeration
);
1114 bt_ctf_field_type_put(composite_structure_type
);
1115 bt_ctf_field_type_put(structure_seq_type
);
1116 bt_ctf_field_type_put(string_type
);
1117 bt_ctf_field_type_put(sequence_type
);
1118 bt_ctf_field_type_put(uint_8_type
);
1119 bt_ctf_field_type_put(int_16_type
);
1120 bt_ctf_field_type_put(uint_12_type
);
1121 bt_ctf_field_type_put(enumeration_type
);
1122 bt_ctf_field_type_put(enumeration_sequence_type
);
1123 bt_ctf_field_type_put(enumeration_array_type
);
1124 bt_ctf_field_type_put(returned_type
);
1127 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1128 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1131 * Append enough events to force the underlying packet to be resized.
1132 * Also tests that a new event can be declared after a stream has been
1133 * instantiated and used/flushed.
1137 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1139 struct bt_ctf_field_type
*integer_type
=
1140 bt_ctf_field_type_integer_create(17);
1141 struct bt_ctf_field_type
*string_type
=
1142 bt_ctf_field_type_string_create();
1144 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1146 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1148 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1149 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1154 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
1155 struct bt_ctf_event
*event
= bt_ctf_event_create(event_class
);
1156 struct bt_ctf_field
*integer
=
1157 bt_ctf_field_create(integer_type
);
1158 struct bt_ctf_field
*string
=
1159 bt_ctf_field_create(string_type
);
1161 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1162 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1163 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1165 bt_ctf_field_put(integer
);
1166 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1167 ret
|= bt_ctf_event_set_payload(event
, "a_string",
1169 bt_ctf_field_put(string
);
1170 ret
|= bt_ctf_stream_append_event(stream
, event
);
1171 bt_ctf_event_put(event
);
1178 ok(ret
== 0, "Append 100 000 events to a stream");
1179 ok(bt_ctf_stream_flush(stream
) == 0,
1180 "Flush a stream that forces a packet resize");
1181 bt_ctf_field_type_put(integer_type
);
1182 bt_ctf_field_type_put(string_type
);
1183 bt_ctf_event_class_put(event_class
);
1186 int main(int argc
, char **argv
)
1188 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
1189 char metadata_path
[sizeof(trace_path
) + 9];
1190 const char *clock_name
= "test_clock";
1191 const char *clock_description
= "This is a test clock";
1192 const char *returned_clock_name
;
1193 const char *returned_clock_description
;
1194 const uint64_t frequency
= 1123456789;
1195 const uint64_t offset_s
= 1351530929945824323;
1196 const uint64_t offset
= 1234567;
1197 const uint64_t precision
= 10;
1198 const int is_absolute
= 0xFF;
1199 char *metadata_string
;
1200 struct bt_ctf_writer
*writer
;
1201 struct utsname name
;
1202 char hostname
[HOST_NAME_MAX
];
1203 struct bt_ctf_clock
*clock
;
1204 struct bt_ctf_stream_class
*stream_class
;
1205 struct bt_ctf_stream
*stream1
;
1208 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
1214 if (!mkdtemp(trace_path
)) {
1218 strcpy(metadata_path
, trace_path
);
1219 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
1221 writer
= bt_ctf_writer_create(trace_path
);
1222 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
1224 /* Add environment context to the trace */
1225 gethostname(hostname
, HOST_NAME_MAX
);
1226 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
1227 "Add host (%s) environment field to writer instance",
1229 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
1231 "bt_ctf_writer_add_environment_field error with NULL writer");
1232 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
1234 "bt_ctf_writer_add_environment_field error with NULL field name");
1235 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
1237 "bt_ctf_writer_add_environment_field error with NULL field value");
1244 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
1245 == 0, "Add sysname (%s) environment field to writer instance",
1247 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
1248 name
.nodename
) == 0,
1249 "Add nodename (%s) environment field to writer instance",
1251 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
1252 == 0, "Add release (%s) environment field to writer instance",
1254 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
1255 == 0, "Add version (%s) environment field to writer instance",
1257 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
1258 == 0, "Add machine (%s) environment field to writer istance",
1261 /* Define a clock and add it to the trace */
1262 ok(bt_ctf_clock_create("signed") == NULL
,
1263 "Illegal clock name rejected");
1264 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
1265 clock
= bt_ctf_clock_create(clock_name
);
1266 ok(clock
, "Clock created sucessfully");
1267 returned_clock_name
= bt_ctf_clock_get_name(clock
);
1268 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
1269 ok(strcmp(returned_clock_name
, clock_name
) == 0,
1270 "Returned clock name is valid");
1272 returned_clock_description
= bt_ctf_clock_get_description(clock
);
1273 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
1274 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
1275 "Clock description set successfully");
1277 returned_clock_description
= bt_ctf_clock_get_description(clock
);
1278 ok(returned_clock_description
,
1279 "bt_ctf_clock_get_description returns a description.");
1280 ok(strcmp(returned_clock_description
, clock_description
) == 0,
1281 "Returned clock description is valid");
1283 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
1284 "bt_ctf_clock_get_frequency returns the correct default frequency");
1285 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
1286 "Set clock frequency");
1287 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
1288 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
1290 ok(bt_ctf_clock_get_offset_s(clock
) == DEFAULT_CLOCK_OFFSET_S
,
1291 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
1292 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
1293 "Set clock offset (seconds)");
1294 ok(bt_ctf_clock_get_offset_s(clock
) == offset_s
,
1295 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
1297 ok(bt_ctf_clock_get_offset(clock
) == DEFAULT_CLOCK_OFFSET
,
1298 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
1299 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
1300 ok(bt_ctf_clock_get_offset(clock
) == offset
,
1301 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
1303 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
1304 "bt_ctf_clock_get_precision returns the correct default precision");
1305 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
1306 "Set clock precision");
1307 ok(bt_ctf_clock_get_precision(clock
) == precision
,
1308 "bt_ctf_clock_get_precision returns the correct precision once it is set");
1310 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
1311 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
1312 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
1313 "Set clock absolute property");
1314 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
1315 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
1317 ok(bt_ctf_clock_get_time(clock
) == DEFAULT_CLOCK_TIME
,
1318 "bt_ctf_clock_get_time returns the correct default time");
1319 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
1321 ok(bt_ctf_clock_get_time(clock
) == current_time
,
1322 "bt_ctf_clock_get_time returns the correct time once it is set");
1324 ok(bt_ctf_writer_add_clock(writer
, clock
) == 0,
1325 "Add clock to writer instance");
1326 ok(bt_ctf_writer_add_clock(writer
, clock
),
1327 "Verify a clock can't be added twice to a writer instance");
1329 ok(!bt_ctf_clock_get_name(NULL
),
1330 "bt_ctf_clock_get_name correctly handles NULL");
1331 ok(!bt_ctf_clock_get_description(NULL
),
1332 "bt_ctf_clock_get_description correctly handles NULL");
1333 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
1334 "bt_ctf_clock_get_frequency correctly handles NULL");
1335 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
1336 "bt_ctf_clock_get_precision correctly handles NULL");
1337 ok(bt_ctf_clock_get_offset_s(NULL
) == -1ULL,
1338 "bt_ctf_clock_get_offset_s correctly handles NULL");
1339 ok(bt_ctf_clock_get_offset(NULL
) == -1ULL,
1340 "bt_ctf_clock_get_offset correctly handles NULL");
1341 ok(bt_ctf_clock_get_is_absolute(NULL
) == -1,
1342 "bt_ctf_clock_get_is_absolute correctly handles NULL");
1343 ok(bt_ctf_clock_get_time(NULL
) == -1ULL,
1344 "bt_ctf_clock_get_time correctly handles NULL");
1346 ok(bt_ctf_clock_set_description(NULL
, NULL
) == -1,
1347 "bt_ctf_clock_set_description correctly handles NULL clock");
1348 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) == -1,
1349 "bt_ctf_clock_set_frequency correctly handles NULL clock");
1350 ok(bt_ctf_clock_set_precision(NULL
, precision
) == -1,
1351 "bt_ctf_clock_get_precision correctly handles NULL clock");
1352 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) == -1,
1353 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
1354 ok(bt_ctf_clock_set_offset(NULL
, offset
) == -1,
1355 "bt_ctf_clock_set_offset correctly handles NULL clock");
1356 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) == -1,
1357 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
1358 ok(bt_ctf_clock_set_time(NULL
, current_time
) == -1,
1359 "bt_ctf_clock_set_time correctly handles NULL clock");
1361 /* Define a stream class */
1362 stream_class
= bt_ctf_stream_class_create("test_stream");
1363 ok(stream_class
, "Create stream class");
1364 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
1365 "Set a stream class' clock");
1367 /* Test the event fields and event types APIs */
1370 /* Instantiate a stream and append events */
1371 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
1372 ok(stream1
, "Instanciate a stream class from writer");
1374 /* Should fail after instanciating a stream (locked)*/
1375 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
1376 "Changes to a stream class that was already instantiated fail");
1378 append_simple_event(stream_class
, stream1
, clock
);
1380 packet_resize_test(stream_class
, stream1
, clock
);
1382 append_complex_event(stream_class
, stream1
, clock
);
1384 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
1385 ok(metadata_string
, "Get metadata string");
1387 bt_ctf_writer_flush_metadata(writer
);
1388 validate_metadata(argv
[1], metadata_path
);
1389 validate_trace(argv
[2], trace_path
);
1391 bt_ctf_clock_put(clock
);
1392 bt_ctf_stream_class_put(stream_class
);
1393 bt_ctf_writer_put(writer
);
1394 bt_ctf_stream_put(stream1
);
1395 free(metadata_string
);
1397 /* Remove all trace files and delete temporary trace directory */
1398 DIR *trace_dir
= opendir(trace_path
);
1400 perror("# opendir");
1404 struct dirent
*entry
;
1405 while ((entry
= readdir(trace_dir
))) {
1406 if (entry
->d_type
== DT_REG
) {
1407 unlinkat(dirfd(trace_dir
), entry
->d_name
, 0);
1412 closedir(trace_dir
);