6 * Copyright 2013 - 2015 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf-writer/stream-class.h>
29 #include <babeltrace/ref.h>
30 #include <babeltrace/ctf/events.h>
31 #include <babeltrace/values.h>
33 #include <babeltrace/compat/stdlib.h>
35 #include <sys/utsname.h>
36 #include <babeltrace/compat/limits.h>
37 #include <babeltrace/compat/stdio.h>
42 #include <babeltrace/compat/dirent.h>
48 #define METADATA_LINE_SIZE 512
49 #define SEQUENCE_TEST_LENGTH 10
50 #define ARRAY_TEST_LENGTH 5
51 #define PACKET_RESIZE_TEST_LENGTH 100000
53 #define DEFAULT_CLOCK_FREQ 1000000000
54 #define DEFAULT_CLOCK_PRECISION 1
55 #define DEFAULT_CLOCK_OFFSET 0
56 #define DEFAULT_CLOCK_OFFSET_S 0
57 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
58 #define DEFAULT_CLOCK_TIME 0
59 #define DEFAULT_CLOCK_VALUE 0
63 static int64_t current_time
= 42;
65 /* Return 1 if uuids match, zero if different. */
66 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
71 if (!uuid_a
|| !uuid_b
) {
75 for (i
= 0; i
< 16; i
++) {
76 if (uuid_a
[i
] != uuid_b
[i
]) {
86 void validate_metadata(char *parser_path
, char *metadata_path
)
89 char parser_output_path
[] = "/tmp/parser_output_XXXXXX";
90 int parser_output_fd
= -1, metadata_fd
= -1;
97 parser_output_fd
= mkstemp(parser_output_path
);
98 metadata_fd
= open(metadata_path
, O_RDONLY
);
100 unlink(parser_output_path
);
102 if (parser_output_fd
== -1 || metadata_fd
== -1) {
103 diag("Failed create temporary files for metadata parsing.");
111 waitpid(pid
, &status
, 0);
112 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
114 /* ctf-parser-test expects a metadata string on stdin. */
115 ret
= dup2(metadata_fd
, STDIN_FILENO
);
117 perror("# dup2 metadata_fd to STDIN");
121 ret
= dup2(parser_output_fd
, STDOUT_FILENO
);
123 perror("# dup2 parser_output_fd to STDOUT");
127 ret
= dup2(parser_output_fd
, STDERR_FILENO
);
129 perror("# dup2 parser_output_fd to STDERR");
133 execl(parser_path
, "ctf-parser-test", NULL
);
134 perror("# Could not launch the ctf metadata parser process");
138 ok(ret
== 0, "Metadata string is valid");
140 if (ret
&& metadata_fd
>= 0 && parser_output_fd
>= 0) {
142 size_t len
= METADATA_LINE_SIZE
;
143 FILE *metadata_fp
= NULL
, *parser_output_fp
= NULL
;
145 metadata_fp
= fdopen(metadata_fd
, "r");
147 perror("fdopen on metadata_fd");
152 parser_output_fp
= fdopen(parser_output_fd
, "r");
153 if (!parser_output_fp
) {
154 perror("fdopen on parser_output_fd");
157 parser_output_fd
= -1;
161 diag("malloc failure");
166 /* Output the metadata and parser output as diagnostic */
167 while (bt_getline(&line
, &len
, metadata_fp
) > 0) {
168 fprintf(stderr
, "# %s", line
);
171 rewind(parser_output_fp
);
172 while (bt_getline(&line
, &len
, parser_output_fp
) > 0) {
173 fprintf(stderr
, "# %s", line
);
179 if (fclose(metadata_fp
)) {
180 diag("fclose failure");
183 if (parser_output_fp
) {
184 if (fclose(parser_output_fp
)) {
185 diag("fclose failure");
190 if (parser_output_fd
>= 0) {
191 if (close(parser_output_fd
)) {
195 if (metadata_fd
>= 0) {
196 if (close(metadata_fd
)) {
202 void validate_trace(char *parser_path
, char *trace_path
)
205 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
206 int babeltrace_output_fd
= -1;
213 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
214 unlink(babeltrace_output_path
);
216 if (babeltrace_output_fd
== -1) {
217 diag("Failed to create a temporary file for trace parsing.");
225 waitpid(pid
, &status
, 0);
226 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
228 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
230 perror("# dup2 babeltrace_output_fd to STDOUT");
234 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
236 perror("# dup2 babeltrace_output_fd to STDERR");
240 execl(parser_path
, "babeltrace", trace_path
, NULL
);
241 perror("# Could not launch the babeltrace process");
245 ok(ret
== 0, "Babeltrace could read the resulting trace");
247 if (ret
&& babeltrace_output_fd
>= 0) {
249 size_t len
= METADATA_LINE_SIZE
;
250 FILE *babeltrace_output_fp
= NULL
;
252 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
253 if (!babeltrace_output_fp
) {
254 perror("fdopen on babeltrace_output_fd");
257 babeltrace_output_fd
= -1;
261 diag("malloc error");
263 rewind(babeltrace_output_fp
);
264 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
270 if (babeltrace_output_fp
) {
271 if (fclose(babeltrace_output_fp
)) {
272 diag("fclose error");
277 if (babeltrace_output_fd
>= 0) {
278 if (close(babeltrace_output_fd
)) {
284 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
285 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
287 /* Create and add a simple event class */
288 struct bt_ctf_event_class
*simple_event_class
=
289 bt_ctf_event_class_create("Simple Event");
290 struct bt_ctf_field_type
*uint_12_type
=
291 bt_ctf_field_type_integer_create(12);
292 struct bt_ctf_field_type
*int_64_type
=
293 bt_ctf_field_type_integer_create(64);
294 struct bt_ctf_field_type
*float_type
=
295 bt_ctf_field_type_floating_point_create();
296 struct bt_ctf_field_type
*enum_type
;
297 struct bt_ctf_field_type
*enum_type_unsigned
=
298 bt_ctf_field_type_enumeration_create(uint_12_type
);
299 struct bt_ctf_field_type
*event_context_type
=
300 bt_ctf_field_type_structure_create();
301 struct bt_ctf_field_type
*event_payload_type
= NULL
;
302 struct bt_ctf_field_type
*returned_type
;
303 struct bt_ctf_event
*simple_event
;
304 struct bt_ctf_field
*integer_field
;
305 struct bt_ctf_field
*float_field
;
306 struct bt_ctf_field
*enum_field
;
307 struct bt_ctf_field
*enum_field_unsigned
;
308 struct bt_ctf_field
*enum_container_field
;
309 const char *mapping_name_test
= "truie";
310 const double double_test_value
= 3.1415;
311 struct bt_ctf_field
*enum_container_field_unsigned
;
312 const char *mapping_name_negative_test
= "negative_value";
313 const char *ret_char
;
315 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
316 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
317 struct bt_ctf_clock
*ret_clock
;
318 struct bt_ctf_event_class
*ret_event_class
;
319 struct bt_ctf_field
*packet_context
;
320 struct bt_ctf_field
*packet_context_field
;
321 struct bt_ctf_field
*stream_event_context
;
322 struct bt_ctf_field
*stream_event_context_field
;
323 struct bt_ctf_field
*event_context
;
324 struct bt_ctf_field
*event_context_field
;
325 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
326 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
327 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
330 ok(uint_12_type
, "Create an unsigned integer type");
332 bt_ctf_field_type_integer_set_signed(int_64_type
, 1);
333 ok(int_64_type
, "Create a signed integer type");
334 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
336 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
337 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
338 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
339 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
340 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
341 bt_put(returned_type
);
343 bt_ctf_field_type_set_alignment(float_type
, 32);
344 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
345 "bt_ctf_field_type_get_alignment handles NULL correctly");
346 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
347 "bt_ctf_field_type_get_alignment returns a correct value");
349 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
350 "Set a floating point type's exponent digit count");
351 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
352 "Set a floating point type's mantissa digit count");
354 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
355 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
356 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
357 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
358 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
359 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
360 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
361 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
363 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
364 mapping_name_negative_test
, -12345, 0) == 0,
365 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
366 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
367 "escaping; \"test\"", 1, 1) == 0,
368 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
369 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
370 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
371 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
372 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
373 "event clock int float", 5, 22) == 0,
374 "Accept enumeration mapping strings containing reserved keywords");
375 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
377 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
378 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
379 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
380 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
381 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
382 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
383 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
385 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL
, -42) < 0,
386 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
387 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, 1000000) < 0,
388 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
389 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type
, -55) == 1,
390 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
392 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
393 "enum_field") == 0, "Add signed enumeration field to event");
395 ok(bt_ctf_field_type_enumeration_get_mapping(NULL
, 0, &ret_char
,
396 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
397 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
398 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, NULL
,
399 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
400 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
401 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
402 NULL
, &ret_range_end_int64_t
) < 0,
403 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
404 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 0, &ret_char
,
405 &ret_range_start_int64_t
, NULL
) < 0,
406 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
407 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type
, 5, &ret_char
,
408 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
409 "bt_ctf_field_type_enumeration_get_mapping returns a value");
410 ok(!strcmp(ret_char
, mapping_name_test
),
411 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
412 ok(ret_range_start_int64_t
== 42,
413 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
414 ok(ret_range_end_int64_t
== 42,
415 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
417 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
418 "escaping; \"test\"", 0, 0) == 0,
419 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
420 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
421 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
422 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
423 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
424 "event clock int float", 5, 22) == 0,
425 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
426 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
428 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
429 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
430 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
431 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
432 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
433 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
434 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
435 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
437 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
438 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
439 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 4,
440 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
442 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
443 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
444 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
445 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
446 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
447 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
448 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
449 NULL
, &ret_range_end_uint64_t
) < 0,
450 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
451 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
452 &ret_range_start_uint64_t
, NULL
) < 0,
453 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
454 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 3, &ret_char
,
455 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
456 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
457 ok(!strcmp(ret_char
, mapping_name_test
),
458 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
459 ok(ret_range_start_uint64_t
== 42,
460 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
461 ok(ret_range_end_uint64_t
== 42,
462 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
464 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
466 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
469 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
471 /* Set an event context type which will contain a single integer*/
472 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
473 "event_specific_context"),
474 "Add event specific context field");
475 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
476 "bt_ctf_event_class_get_context_type handles NULL correctly");
477 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
478 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
480 ok(bt_ctf_event_class_set_context_type(simple_event_class
, NULL
) < 0,
481 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
482 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
483 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
484 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
485 "Set an event class' context type successfully");
486 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
487 ok(returned_type
== event_context_type
,
488 "bt_ctf_event_class_get_context_type returns the appropriate type");
489 bt_put(returned_type
);
491 bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
);
494 * bt_ctf_stream_class_add_event_class() copies the field types
495 * of simple_event_class, so we retrieve the new ones to create
496 * the appropriate fields.
498 BT_PUT(event_context_type
);
499 BT_PUT(event_payload_type
);
500 event_payload_type
= bt_ctf_event_class_get_payload_type(
502 assert(event_payload_type
);
503 event_context_type
= bt_ctf_event_class_get_context_type(
505 assert(event_context_type
);
506 ep_integer_field_type
=
507 bt_ctf_field_type_structure_get_field_type_by_name(
508 event_payload_type
, "integer_field");
509 assert(ep_integer_field_type
);
511 bt_ctf_field_type_structure_get_field_type_by_name(
512 event_payload_type
, "enum_field");
513 assert(ep_enum_field_type
);
514 ep_enum_field_unsigned_type
=
515 bt_ctf_field_type_structure_get_field_type_by_name(
516 event_payload_type
, "enum_field_unsigned");
517 assert(ep_enum_field_unsigned_type
);
519 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
520 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
521 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
522 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
523 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
524 "bt_ctf_stream_class_get_event_class handles NULL correctly");
525 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
526 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
527 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
528 ok(ret_event_class
== simple_event_class
,
529 "bt_ctf_stream_class_get_event_class returns the correct event class");
530 bt_put(ret_event_class
);
531 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
532 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
533 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
534 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
536 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
537 ok(ret_event_class
== simple_event_class
,
538 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
539 bt_put(ret_event_class
);
541 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
542 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
543 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
544 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
545 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
546 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
547 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
548 ok(ret_event_class
== simple_event_class
,
549 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
550 bt_put(ret_event_class
);
552 simple_event
= bt_ctf_event_create(simple_event_class
);
554 "Instantiate an event containing a single integer field");
556 ok(bt_ctf_event_get_clock(NULL
) == NULL
,
557 "bt_ctf_event_get_clock handles NULL correctly");
558 ret_clock
= bt_ctf_event_get_clock(simple_event
);
559 ok(ret_clock
== clock
,
560 "bt_ctf_event_get_clock returns a correct clock");
563 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
564 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
565 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
566 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
568 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
569 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
570 "bt_ctf_field_floating_point_get_value fails on an unset float field");
571 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
572 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
573 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
574 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
575 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
576 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
577 "bt_ctf_field_floating_point_get_value returns a double value");
578 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
579 "bt_ctf_field_floating_point_get_value returns a correct value");
581 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
583 ret_char
= bt_ctf_field_enumeration_get_mapping_name(NULL
);
584 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
585 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
586 ok(!ret_char
, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
587 enum_container_field
= bt_ctf_field_enumeration_get_container(
589 ok(bt_ctf_field_signed_integer_set_value(
590 enum_container_field
, -42) == 0,
591 "Set signed enumeration container value");
592 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field
);
593 ok(!strcmp(ret_char
, mapping_name_negative_test
),
594 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
595 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
598 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
599 assert(enum_field_unsigned
);
600 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
601 enum_field_unsigned
);
602 ok(bt_ctf_field_unsigned_integer_set_value(
603 enum_container_field_unsigned
, 42) == 0,
604 "Set unsigned enumeration container value");
605 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
606 enum_field_unsigned
);
608 ret_char
= bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned
);
609 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
610 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
612 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
614 /* Populate stream event context */
615 stream_event_context
=
616 bt_ctf_event_get_stream_event_context(simple_event
);
617 assert(stream_event_context
);
618 stream_event_context_field
= bt_ctf_field_structure_get_field(
619 stream_event_context
, "common_event_context");
620 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
622 /* Populate the event's context */
623 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
624 "bt_ctf_event_get_event_context handles NULL correctly");
625 event_context
= bt_ctf_event_get_event_context(simple_event
);
627 "bt_ctf_event_get_event_context returns a field");
628 returned_type
= bt_ctf_field_get_type(event_context
);
629 ok(returned_type
== event_context_type
,
630 "bt_ctf_event_get_event_context returns a field of the appropriate type");
631 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
632 "event_specific_context");
633 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
634 "Successfully set an event context's value");
635 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
636 "bt_ctf_event_set_event_context handles a NULL event correctly");
637 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
638 "bt_ctf_event_set_event_context handles a NULL event context correctly");
639 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
640 "bt_ctf_event_set_event_context rejects a context of the wrong type");
641 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
642 "Set an event context successfully");
644 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
645 "Append simple event to trace stream");
647 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
648 "bt_ctf_stream_get_packet_context handles NULL correctly");
649 packet_context
= bt_ctf_stream_get_packet_context(stream
);
651 "bt_ctf_stream_get_packet_context returns a packet context");
653 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
655 ok(packet_context_field
,
656 "Packet context contains the default packet_size field.");
657 bt_put(packet_context_field
);
658 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
659 "custom_packet_context_field");
660 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
661 "Custom packet context field value successfully set.");
663 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
664 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
665 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
666 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
667 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
668 "Successfully set a stream's packet context");
670 ok(bt_ctf_stream_flush(stream
) == 0,
671 "Flush trace stream with one event");
673 bt_put(simple_event_class
);
674 bt_put(simple_event
);
675 bt_put(uint_12_type
);
679 bt_put(enum_type_unsigned
);
680 bt_put(returned_type
);
681 bt_put(event_context_type
);
682 bt_put(integer_field
);
685 bt_put(enum_field_unsigned
);
686 bt_put(enum_container_field
);
687 bt_put(enum_container_field_unsigned
);
688 bt_put(packet_context
);
689 bt_put(packet_context_field
);
690 bt_put(stream_event_context
);
691 bt_put(stream_event_context_field
);
692 bt_put(event_context
);
693 bt_put(event_context_field
);
694 bt_put(event_payload_type
);
695 bt_put(ep_integer_field_type
);
696 bt_put(ep_enum_field_type
);
697 bt_put(ep_enum_field_unsigned_type
);
700 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
701 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
703 struct event_class_attrs_counts
{
714 struct event_class_attrs_counts
;
715 const char *complex_test_event_string
= "Complex Test Event";
716 const char *test_string_1
= "Test ";
717 const char *test_string_2
= "string ";
718 const char *test_string_3
= "abcdefghi";
719 const char *test_string_4
= "abcd\0efg\0hi";
720 const char *test_string_cat
= "Test string abcdeabcd";
721 struct bt_ctf_field_type
*uint_35_type
=
722 bt_ctf_field_type_integer_create(35);
723 struct bt_ctf_field_type
*int_16_type
=
724 bt_ctf_field_type_integer_create(16);
725 struct bt_ctf_field_type
*uint_3_type
=
726 bt_ctf_field_type_integer_create(3);
727 struct bt_ctf_field_type
*enum_variant_type
=
728 bt_ctf_field_type_enumeration_create(uint_3_type
);
729 struct bt_ctf_field_type
*variant_type
=
730 bt_ctf_field_type_variant_create(enum_variant_type
,
732 struct bt_ctf_field_type
*string_type
=
733 bt_ctf_field_type_string_create();
734 struct bt_ctf_field_type
*sequence_type
;
735 struct bt_ctf_field_type
*array_type
;
736 struct bt_ctf_field_type
*inner_structure_type
=
737 bt_ctf_field_type_structure_create();
738 struct bt_ctf_field_type
*complex_structure_type
=
739 bt_ctf_field_type_structure_create();
740 struct bt_ctf_field_type
*ret_field_type
;
741 struct bt_ctf_event_class
*event_class
;
742 struct bt_ctf_event
*event
;
743 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
744 *inner_structure_field
, *complex_structure_field
,
745 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
746 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
747 *stream_event_ctx_int_field
, *ret_field
;
748 uint64_t ret_unsigned_int
;
749 int64_t ret_signed_int
;
750 const char *ret_string
;
751 struct bt_ctf_stream_class
*ret_stream_class
;
752 struct bt_ctf_event_class
*ret_event_class
;
753 struct bt_ctf_field
*packet_context
, *packet_context_field
;
754 struct bt_value
*obj
;
756 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
757 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
758 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
759 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
760 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
761 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
762 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
763 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
764 bt_ctf_field_type_set_alignment(int_16_type
, 32);
765 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
766 bt_ctf_field_type_integer_set_base(uint_35_type
,
767 BT_CTF_INTEGER_BASE_HEXADECIMAL
);
769 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
770 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
773 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
774 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
775 ret_field_type
= bt_ctf_field_type_array_get_element_type(
777 ok(ret_field_type
== int_16_type
,
778 "bt_ctf_field_type_array_get_element_type returns the correct type");
779 bt_put(ret_field_type
);
781 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
782 "bt_ctf_field_type_array_get_length handles NULL correctly");
783 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
784 "bt_ctf_field_type_array_get_length returns the correct length");
786 bt_ctf_field_type_structure_add_field(inner_structure_type
,
787 uint_35_type
, "seq_len");
788 bt_ctf_field_type_structure_add_field(inner_structure_type
,
789 sequence_type
, "a_sequence");
790 bt_ctf_field_type_structure_add_field(inner_structure_type
,
791 array_type
, "an_array");
793 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
795 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
797 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
798 "UINT35_TYPE", 2, 7);
800 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL
,
802 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
803 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
804 enum_variant_type
, NULL
) < 0,
805 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
806 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
807 enum_variant_type
, "INT16_TYPE") == 1,
808 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
810 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL
, 1) < 0,
811 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
812 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, -42) < 0,
813 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
814 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type
, 5) == 2,
815 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
817 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
818 "An unknown entry"), "Reject a variant field based on an unknown tag value");
819 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
820 "UINT3_TYPE") == 0, "Add a field to a variant");
821 bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
823 bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
826 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
827 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
828 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
829 ok(ret_field_type
== enum_variant_type
,
830 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
831 bt_put(ret_field_type
);
833 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
834 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
835 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
836 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
837 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
838 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
839 "INT16_TYPE") == NULL
,
840 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
841 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
843 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
844 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
845 variant_type
, "INT16_TYPE");
846 ok(ret_field_type
== int_16_type
,
847 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
848 bt_put(ret_field_type
);
850 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
851 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
852 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
853 "bt_ctf_field_type_variant_get_field_count returns the correct count");
855 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
856 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
857 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
858 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
859 bt_put(ret_field_type
);
860 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
861 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
862 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
863 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
864 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
865 "bt_ctf_field_type_variant_get_field returns a field");
866 ok(!strcmp("INT16_TYPE", ret_string
),
867 "bt_ctf_field_type_variant_get_field returns a correct field name");
868 ok(ret_field_type
== int_16_type
,
869 "bt_ctf_field_type_variant_get_field returns a correct field type");
870 bt_put(ret_field_type
);
872 bt_ctf_field_type_structure_add_field(complex_structure_type
,
873 enum_variant_type
, "variant_selector");
874 bt_ctf_field_type_structure_add_field(complex_structure_type
,
875 string_type
, "a_string");
876 bt_ctf_field_type_structure_add_field(complex_structure_type
,
877 variant_type
, "variant_value");
878 bt_ctf_field_type_structure_add_field(complex_structure_type
,
879 inner_structure_type
, "inner_structure");
881 ok(bt_ctf_event_class_create("clock") == NULL
,
882 "Reject creation of an event class with an illegal name");
883 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
884 ok(event_class
, "Create an event class");
885 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
886 "Reject addition of a field with an empty name to an event");
887 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
888 "Reject addition of a field with a NULL type to an event");
889 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
891 "Reject addition of a type with an illegal name to an event");
892 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
894 "Add field of type unsigned integer to an event");
895 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
896 "int_16") == 0, "Add field of type signed integer to an event");
897 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
898 "complex_structure") == 0,
899 "Add composite structure to an event");
901 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
902 "bt_ctf_event_class_get_name handles NULL correctly");
903 ret_string
= bt_ctf_event_class_get_name(event_class
);
904 ok(!strcmp(ret_string
, complex_test_event_string
),
905 "bt_ctf_event_class_get_name returns a correct name");
906 ok(bt_ctf_event_class_get_id(event_class
) < 0,
907 "bt_ctf_event_class_get_id returns a negative value when not set");
908 ok(bt_ctf_event_class_get_id(NULL
) < 0,
909 "bt_ctf_event_class_get_id handles NULL correctly");
910 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
911 "bt_ctf_event_class_set_id handles NULL correctly");
912 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
913 "Set an event class' id");
914 ok(bt_ctf_event_class_get_id(event_class
) == 42,
915 "bt_ctf_event_class_get_id returns the correct value");
917 /* Test event class attributes */
918 obj
= bt_value_integer_create_init(15);
920 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
921 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
922 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
923 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
924 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
925 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
926 assert(!bt_value_integer_set(obj
, -3));
927 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
928 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
929 assert(!bt_value_integer_set(obj
, 11));
930 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
931 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
932 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
933 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
934 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
936 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
939 obj
= bt_value_integer_create_init(5);
941 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
942 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
944 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
945 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
946 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
947 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
948 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
949 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
950 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
953 ret
= bt_value_integer_get(obj
, &int64_value
);
954 ok(obj
&& !ret
&& int64_value
== 5,
955 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
958 obj
= bt_value_string_create_init("nu name");
960 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
961 ret_string
= bt_ctf_event_class_get_name(event_class
);
962 ok(!strcmp(ret_string
, "nu name"),
963 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
964 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
965 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
967 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
969 obj
= bt_value_string_create_init("http://kernel.org/");
971 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
974 ok(bt_ctf_event_class_get_attribute_count(NULL
),
975 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
976 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
977 "bt_ctf_event_class_get_attribute_count returns the correct count");
978 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
979 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
980 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
981 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
982 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
983 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
984 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
985 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
987 memset(&attrs_count
, 0, sizeof(attrs_count
));
989 for (i
= 0; i
< 4; ++i
) {
990 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
992 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
993 assert(ret_string
&& obj
);
995 if (!strcmp(ret_string
, "id")) {
997 ok(bt_value_is_integer(obj
),
998 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1000 } else if (!strcmp(ret_string
, "name")) {
1002 ok(bt_value_is_string(obj
),
1003 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1005 } else if (!strcmp(ret_string
, "loglevel")) {
1006 attrs_count
.loglevel
++;
1007 ok(bt_value_is_integer(obj
),
1008 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1010 } else if (!strcmp(ret_string
, "model.emf.uri")) {
1011 attrs_count
.modelemfuri
++;
1012 ok(bt_value_is_string(obj
),
1013 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1016 attrs_count
.unknown
++;
1022 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
1023 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
1024 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
1025 "event class has one instance of each known attribute");
1027 /* Add event class to the stream class */
1028 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
1029 "Reject addition of NULL event class to a stream class");
1030 ok(bt_ctf_stream_class_add_event_class(stream_class
,
1031 event_class
) == 0, "Add an event class to stream class");
1033 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
1034 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1035 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
1036 ok(ret_stream_class
== stream_class
,
1037 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1038 bt_put(ret_stream_class
);
1040 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
1041 "bt_ctf_event_class_get_field_count handles NULL correctly");
1042 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
1043 "bt_ctf_event_class_get_field_count returns a correct value");
1045 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
1046 &ret_field_type
, 0) < 0,
1047 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1048 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
1049 &ret_field_type
, 0) == 0,
1050 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1051 bt_put(ret_field_type
);
1052 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1054 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1055 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1056 &ret_field_type
, 42) < 0,
1057 "bt_ctf_event_class_get_field handles an invalid index correctly");
1058 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
1059 &ret_field_type
, 0) == 0,
1060 "bt_ctf_event_class_get_field returns a field");
1061 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
1062 "bt_ctf_event_class_get_field returns a correct field type");
1063 bt_put(ret_field_type
);
1064 ok(!strcmp(ret_string
, "uint_35"),
1065 "bt_ctf_event_class_get_field returns a correct field name");
1066 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
1067 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1068 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
1069 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1070 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
1071 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1072 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
1073 "complex_structure");
1074 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
1075 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1076 bt_put(ret_field_type
);
1078 event
= bt_ctf_event_create(event_class
);
1079 ok(event
, "Instanciate a complex event");
1081 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1082 "bt_ctf_event_get_class handles NULL correctly");
1083 ret_event_class
= bt_ctf_event_get_class(event
);
1084 ok(ret_event_class
== event_class
,
1085 "bt_ctf_event_get_class returns the correct event class");
1086 bt_put(ret_event_class
);
1088 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1089 if (!uint_35_field
) {
1090 printf("uint_35_field is NULL\n");
1093 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1094 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1095 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1096 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1097 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1098 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1099 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1100 &ret_unsigned_int
) == 0,
1101 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1102 ok(ret_unsigned_int
== 0x0DDF00D,
1103 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1104 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1105 &ret_signed_int
) < 0,
1106 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1107 bt_put(uint_35_field
);
1109 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1110 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1111 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1112 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1113 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1114 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1115 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1116 &ret_signed_int
) == 0,
1117 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1118 ok(ret_signed_int
== -12345,
1119 "bt_ctf_field_signed_integer_get_value returns the correct value");
1120 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1121 &ret_unsigned_int
) < 0,
1122 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1123 bt_put(int_16_field
);
1125 complex_structure_field
= bt_ctf_event_get_payload(event
,
1126 "complex_structure");
1128 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1129 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1130 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1131 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1132 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1133 complex_structure_field
, 3);
1134 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1135 bt_put(inner_structure_field
);
1136 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1137 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1138 bt_put(ret_field_type
);
1140 inner_structure_field
= bt_ctf_field_structure_get_field(
1141 complex_structure_field
, "inner_structure");
1142 a_string_field
= bt_ctf_field_structure_get_field(
1143 complex_structure_field
, "a_string");
1144 enum_variant_field
= bt_ctf_field_structure_get_field(
1145 complex_structure_field
, "variant_selector");
1146 variant_field
= bt_ctf_field_structure_get_field(
1147 complex_structure_field
, "variant_value");
1148 uint_35_field
= bt_ctf_field_structure_get_field(
1149 inner_structure_field
, "seq_len");
1150 a_sequence_field
= bt_ctf_field_structure_get_field(
1151 inner_structure_field
, "a_sequence");
1152 an_array_field
= bt_ctf_field_structure_get_field(
1153 inner_structure_field
, "an_array");
1155 enum_container_field
= bt_ctf_field_enumeration_get_container(
1156 enum_variant_field
);
1157 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1158 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1159 enum_variant_field
);
1160 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1161 bt_put(int_16_field
);
1162 ok(!bt_ctf_field_string_get_value(a_string_field
),
1163 "bt_ctf_field_string_get_value returns NULL on an unset field");
1164 bt_ctf_field_string_set_value(a_string_field
,
1166 ok(!bt_ctf_field_string_get_value(NULL
),
1167 "bt_ctf_field_string_get_value correctly handles NULL");
1168 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1169 "bt_ctf_field_string_append correctly handles a NULL string field");
1170 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1171 "bt_ctf_field_string_append correctly handles a NULL string value");
1172 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1173 "bt_ctf_field_string_append succeeds");
1174 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1175 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1176 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1177 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1178 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1179 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1180 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1181 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1182 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1183 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1184 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1185 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1187 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1188 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1189 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1190 "bt_ctf_field_string_get_value returns a correct value");
1191 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1192 SEQUENCE_TEST_LENGTH
);
1194 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1195 enum_container_field
) == NULL
,
1196 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1197 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1199 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1200 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1201 variant_type
, enum_variant_field
);
1202 ok(ret_field_type
== int_16_type
,
1203 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1205 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1206 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1207 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1208 uint_35_field
) == 0, "Set a sequence field's length");
1209 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1210 ok(ret_field
== uint_35_field
,
1211 "bt_ctf_field_sequence_get_length returns the correct length field");
1212 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1213 "bt_ctf_field_sequence_get_length properly handles NULL");
1215 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1216 int_16_field
= bt_ctf_field_sequence_get_field(
1217 a_sequence_field
, i
);
1218 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1219 bt_put(int_16_field
);
1222 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1223 int_16_field
= bt_ctf_field_array_get_field(
1225 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1226 bt_put(int_16_field
);
1229 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1230 assert(stream_event_ctx_field
);
1231 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1232 stream_event_ctx_field
, "common_event_context");
1233 BT_PUT(stream_event_ctx_field
);
1234 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1235 BT_PUT(stream_event_ctx_int_field
);
1237 bt_ctf_clock_set_time(clock
, ++current_time
);
1238 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1239 "Append a complex event to a stream");
1242 * Populate the custom packet context field with a dummy value
1243 * otherwise flush will fail.
1245 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1246 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1247 "custom_packet_context_field");
1248 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1250 ok(bt_ctf_stream_flush(stream
) == 0,
1251 "Flush a stream containing a complex event");
1253 bt_put(uint_35_field
);
1254 bt_put(a_string_field
);
1255 bt_put(inner_structure_field
);
1256 bt_put(complex_structure_field
);
1257 bt_put(a_sequence_field
);
1258 bt_put(an_array_field
);
1259 bt_put(enum_variant_field
);
1260 bt_put(enum_container_field
);
1261 bt_put(variant_field
);
1263 bt_put(packet_context_field
);
1264 bt_put(packet_context
);
1265 bt_put(uint_35_type
);
1266 bt_put(int_16_type
);
1267 bt_put(string_type
);
1268 bt_put(sequence_type
);
1270 bt_put(inner_structure_type
);
1271 bt_put(complex_structure_type
);
1272 bt_put(uint_3_type
);
1273 bt_put(enum_variant_type
);
1274 bt_put(variant_type
);
1275 bt_put(ret_field_type
);
1276 bt_put(event_class
);
1280 static void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1281 struct bt_ctf_field_type
*expected_type
, const char *name
)
1283 struct bt_ctf_field_type
*copy_type
;
1285 copy_type
= bt_ctf_field_get_type(field
);
1286 ok(copy_type
== expected_type
,
1287 "bt_ctf_field_copy does not copy the type (%s)", name
);
1291 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1292 struct bt_ctf_field
*field_b
, const char *name
)
1294 ok(field_a
!= field_b
,
1295 "bt_ctf_field_copy creates different pointers (%s)", name
);
1298 void field_copy_tests()
1300 struct bt_ctf_field_type
*len_type
= NULL
;
1301 struct bt_ctf_field_type
*fp_type
= NULL
;
1302 struct bt_ctf_field_type
*s_type
= NULL
;
1303 struct bt_ctf_field_type
*e_int_type
= NULL
;
1304 struct bt_ctf_field_type
*e_type
= NULL
;
1305 struct bt_ctf_field_type
*v_type
= NULL
;
1306 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1307 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1308 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1309 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1310 struct bt_ctf_field_type
*strct_type
= NULL
;
1311 struct bt_ctf_field
*len
= NULL
;
1312 struct bt_ctf_field
*fp
= NULL
;
1313 struct bt_ctf_field
*s
= NULL
;
1314 struct bt_ctf_field
*e_int
= NULL
;
1315 struct bt_ctf_field
*e
= NULL
;
1316 struct bt_ctf_field
*v
= NULL
;
1317 struct bt_ctf_field
*v_selected
= NULL
;
1318 struct bt_ctf_field
*v_selected_cur
= NULL
;
1319 struct bt_ctf_field
*v_selected_0
= NULL
;
1320 struct bt_ctf_field
*v_selected_1
= NULL
;
1321 struct bt_ctf_field
*v_selected_2
= NULL
;
1322 struct bt_ctf_field
*v_selected_3
= NULL
;
1323 struct bt_ctf_field
*v_selected_4
= NULL
;
1324 struct bt_ctf_field
*v_selected_5
= NULL
;
1325 struct bt_ctf_field
*v_selected_6
= NULL
;
1326 struct bt_ctf_field
*a
= NULL
;
1327 struct bt_ctf_field
*a_0
= NULL
;
1328 struct bt_ctf_field
*a_1
= NULL
;
1329 struct bt_ctf_field
*a_2
= NULL
;
1330 struct bt_ctf_field
*a_3
= NULL
;
1331 struct bt_ctf_field
*a_4
= NULL
;
1332 struct bt_ctf_field
*strct
= NULL
;
1333 struct bt_ctf_field
*len_copy
= NULL
;
1334 struct bt_ctf_field
*fp_copy
= NULL
;
1335 struct bt_ctf_field
*s_copy
= NULL
;
1336 struct bt_ctf_field
*e_int_copy
= NULL
;
1337 struct bt_ctf_field
*e_copy
= NULL
;
1338 struct bt_ctf_field
*v_copy
= NULL
;
1339 struct bt_ctf_field
*v_selected_copy
= NULL
;
1340 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1341 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1342 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1343 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1344 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1345 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1346 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1347 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1348 struct bt_ctf_field
*a_copy
= NULL
;
1349 struct bt_ctf_field
*a_0_copy
= NULL
;
1350 struct bt_ctf_field
*a_1_copy
= NULL
;
1351 struct bt_ctf_field
*a_2_copy
= NULL
;
1352 struct bt_ctf_field
*a_3_copy
= NULL
;
1353 struct bt_ctf_field
*a_4_copy
= NULL
;
1354 struct bt_ctf_field
*strct_copy
= NULL
;
1355 uint64_t uint64_t_val
;
1356 const char *str_val
;
1360 /* create len type */
1361 len_type
= bt_ctf_field_type_integer_create(32);
1364 /* create fp type */
1365 fp_type
= bt_ctf_field_type_floating_point_create();
1369 s_type
= bt_ctf_field_type_string_create();
1372 /* create e_int type */
1373 e_int_type
= bt_ctf_field_type_integer_create(8);
1377 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1379 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1382 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1386 /* create v_label1 type */
1387 v_label1_type
= bt_ctf_field_type_string_create();
1388 assert(v_label1_type
);
1390 /* create v_label1_array type */
1391 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1392 assert(v_label1_array_type
);
1394 /* create v_label2 type */
1395 v_label2_type
= bt_ctf_field_type_integer_create(16);
1396 assert(v_label2_type
);
1398 /* create v_label2_seq type */
1399 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1401 assert(v_label2_seq_type
);
1404 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1406 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1409 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1413 /* create strct type */
1414 strct_type
= bt_ctf_field_type_structure_create();
1416 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1419 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1421 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1423 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1425 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1427 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1428 v_label1_array_type
, "a");
1432 strct
= bt_ctf_field_create(strct_type
);
1436 len
= bt_ctf_field_structure_get_field(strct
, "len");
1440 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1444 s
= bt_ctf_field_structure_get_field(strct
, "s");
1448 e
= bt_ctf_field_structure_get_field(strct
, "e");
1451 /* get e_int (underlying integer) */
1452 e_int
= bt_ctf_field_enumeration_get_container(e
);
1456 v
= bt_ctf_field_structure_get_field(strct
, "v");
1460 a
= bt_ctf_field_structure_get_field(strct
, "a");
1464 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1468 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1472 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1475 /* set e field (LABEL2) */
1476 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1480 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1482 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1483 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1484 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1485 ok(v_selected_cur
== v_selected
,
1486 "bt_ctf_field_variant_get_current_field returns the current field");
1487 bt_put(v_selected_cur
);
1489 /* set selected v field */
1490 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1492 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1493 assert(v_selected_0
);
1494 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1496 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1497 assert(v_selected_1
);
1498 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1500 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1501 assert(v_selected_2
);
1502 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1504 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1505 assert(v_selected_3
);
1506 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1508 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1509 assert(v_selected_4
);
1510 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1512 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1513 assert(v_selected_5
);
1514 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1516 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1517 assert(v_selected_6
);
1518 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1522 a_0
= bt_ctf_field_array_get_field(a
, 0);
1524 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1526 a_1
= bt_ctf_field_array_get_field(a
, 1);
1528 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1530 a_2
= bt_ctf_field_array_get_field(a
, 2);
1532 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1534 a_3
= bt_ctf_field_array_get_field(a
, 3);
1536 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1538 a_4
= bt_ctf_field_array_get_field(a
, 4);
1540 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1543 /* create copy of strct */
1544 ok(!bt_ctf_field_copy(NULL
),
1545 "bt_ctf_field_copy handles NULL correctly");
1546 strct_copy
= bt_ctf_field_copy(strct
);
1548 "bt_ctf_field_copy returns a valid pointer");
1550 /* get all copied fields */
1551 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1553 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1555 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1557 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1559 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1561 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1563 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1564 assert(v_selected_copy
);
1565 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1566 assert(v_selected_0_copy
);
1567 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1568 assert(v_selected_1_copy
);
1569 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1570 assert(v_selected_2_copy
);
1571 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1572 assert(v_selected_3_copy
);
1573 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1574 assert(v_selected_4_copy
);
1575 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1576 assert(v_selected_5_copy
);
1577 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1578 assert(v_selected_6_copy
);
1579 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1580 "sequence field copy is not too large");
1581 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1583 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1585 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1587 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1589 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1591 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1593 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1594 "array field copy is not too large");
1596 /* make sure copied fields are different pointers */
1597 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1598 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1599 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1600 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1601 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1602 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1603 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1604 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1606 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1608 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1610 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1612 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1614 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1616 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1618 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1620 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1621 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1622 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1623 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1624 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1625 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1627 /* make sure copied fields share the same types */
1628 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1629 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1630 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1631 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1632 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1633 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1634 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1636 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1638 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1640 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1642 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1644 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1646 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1648 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1650 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1651 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1652 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1653 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1654 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1655 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1657 /* validate len copy */
1658 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1660 ok(uint64_t_val
== 7,
1661 "bt_ctf_field_copy creates a valid integer field copy");
1663 /* validate fp copy */
1664 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1666 ok(double_val
== 3.14,
1667 "bt_ctf_field_copy creates a valid floating point number field copy");
1669 /* validate s copy */
1670 str_val
= bt_ctf_field_string_get_value(s_copy
);
1671 ok(str_val
&& !strcmp(str_val
, "btbt"),
1672 "bt_ctf_field_copy creates a valid string field copy");
1674 /* validate e_int copy */
1675 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1678 ok(uint64_t_val
== 23,
1679 "bt_ctf_field_copy creates a valid enum's integer field copy");
1681 /* validate e copy */
1682 str_val
= bt_ctf_field_enumeration_get_mapping_name(e_copy
);
1683 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1684 "bt_ctf_field_copy creates a valid enum field copy");
1686 /* validate v_selected copy */
1687 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1688 assert(v_selected_copy_len
);
1689 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1692 ok(uint64_t_val
== 7,
1693 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1694 bt_put(v_selected_copy_len
);
1695 v_selected_copy_len
= NULL
;
1697 /* validate v_selected copy fields */
1698 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1701 ok(uint64_t_val
== 7,
1702 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1703 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1706 ok(uint64_t_val
== 6,
1707 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1708 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1711 ok(uint64_t_val
== 5,
1712 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1713 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1716 ok(uint64_t_val
== 4,
1717 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1718 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1721 ok(uint64_t_val
== 3,
1722 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1723 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1726 ok(uint64_t_val
== 2,
1727 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1728 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1731 ok(uint64_t_val
== 1,
1732 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1734 /* validate a copy fields */
1735 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1736 ok(str_val
&& !strcmp(str_val
, "a_0"),
1737 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1738 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1739 ok(str_val
&& !strcmp(str_val
, "a_1"),
1740 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1741 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1742 ok(str_val
&& !strcmp(str_val
, "a_2"),
1743 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1744 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1745 ok(str_val
&& !strcmp(str_val
, "a_3"),
1746 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1747 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1748 ok(str_val
&& !strcmp(str_val
, "a_4"),
1749 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1751 /* put everything */
1758 bt_put(v_label1_type
);
1759 bt_put(v_label1_array_type
);
1760 bt_put(v_label2_type
);
1761 bt_put(v_label2_seq_type
);
1770 bt_put(v_selected_0
);
1771 bt_put(v_selected_1
);
1772 bt_put(v_selected_2
);
1773 bt_put(v_selected_3
);
1774 bt_put(v_selected_4
);
1775 bt_put(v_selected_5
);
1776 bt_put(v_selected_6
);
1790 bt_put(v_selected_copy
);
1791 bt_put(v_selected_0_copy
);
1792 bt_put(v_selected_1_copy
);
1793 bt_put(v_selected_2_copy
);
1794 bt_put(v_selected_3_copy
);
1795 bt_put(v_selected_4_copy
);
1796 bt_put(v_selected_5_copy
);
1797 bt_put(v_selected_6_copy
);
1807 void type_field_tests()
1809 struct bt_ctf_field
*uint_12
;
1810 struct bt_ctf_field
*int_16
;
1811 struct bt_ctf_field
*string
;
1812 struct bt_ctf_field
*enumeration
;
1813 struct bt_ctf_field_type
*composite_structure_type
;
1814 struct bt_ctf_field_type
*structure_seq_type
;
1815 struct bt_ctf_field_type
*string_type
;
1816 struct bt_ctf_field_type
*sequence_type
;
1817 struct bt_ctf_field_type
*uint_8_type
;
1818 struct bt_ctf_field_type
*int_16_type
;
1819 struct bt_ctf_field_type
*uint_12_type
=
1820 bt_ctf_field_type_integer_create(12);
1821 struct bt_ctf_field_type
*enumeration_type
;
1822 struct bt_ctf_field_type
*returned_type
;
1823 const char *ret_string
;
1825 returned_type
= bt_ctf_field_get_type(NULL
);
1826 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1828 ok(uint_12_type
, "Create an unsigned integer type");
1829 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1830 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1831 "Set integer type's base as binary");
1832 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1833 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1834 "Set integer type's base as decimal");
1835 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1836 BT_CTF_INTEGER_BASE_UNKNOWN
),
1837 "Reject integer type's base set as unknown");
1838 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1839 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1840 "Set integer type's base as octal");
1841 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1842 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1843 "Set integer type's base as hexadecimal");
1844 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1845 "Reject unknown integer base value");
1846 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1847 "Set integer type signedness to signed");
1848 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1849 "Set integer type signedness to unsigned");
1850 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1851 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1852 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1853 "bt_ctf_field_type_integer_get_size returns a correct value");
1854 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1855 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1856 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1857 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1859 ok(bt_ctf_field_type_set_byte_order(NULL
,
1860 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1861 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1862 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1863 (enum bt_ctf_byte_order
) 42) < 0,
1864 "bt_ctf_field_type_set_byte_order rejects invalid values");
1865 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1866 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1867 "Set an integer's byte order to little endian");
1868 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1869 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1870 "Set an integer's byte order to big endian");
1871 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1872 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1873 "bt_ctf_field_type_get_byte_order returns a correct value");
1874 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1875 BT_CTF_BYTE_ORDER_UNKNOWN
,
1876 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1878 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1879 BT_CTF_TYPE_ID_UNKNOWN
,
1880 "bt_ctf_field_type_get_type_id handles NULL correctly");
1881 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1882 BT_CTF_TYPE_ID_INTEGER
,
1883 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1885 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1886 BT_CTF_INTEGER_BASE_UNKNOWN
,
1887 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1888 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1889 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1890 "bt_ctf_field_type_integer_get_base returns a correct value");
1892 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1893 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1894 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1895 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1896 (enum bt_ctf_string_encoding
) 123) < 0,
1897 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1898 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1899 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1900 "Set integer type encoding to UTF8");
1901 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1902 BT_CTF_STRING_ENCODING_UNKNOWN
,
1903 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1904 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1905 BT_CTF_STRING_ENCODING_UTF8
,
1906 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1908 int_16_type
= bt_ctf_field_type_integer_create(16);
1909 bt_ctf_field_type_integer_set_signed(int_16_type
, 1);
1910 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1911 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1912 uint_8_type
= bt_ctf_field_type_integer_create(8);
1914 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1915 ok(sequence_type
, "Create a sequence of int16_t type");
1916 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1917 BT_CTF_TYPE_ID_SEQUENCE
,
1918 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1920 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1921 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1922 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1924 ok(!strcmp(ret_string
, "seq_len"),
1925 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1926 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1927 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1928 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1930 ok(returned_type
== int_16_type
,
1931 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1932 bt_put(returned_type
);
1934 string_type
= bt_ctf_field_type_string_create();
1935 ok(string_type
, "Create a string type");
1936 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1937 BT_CTF_STRING_ENCODING_NONE
),
1938 "Reject invalid \"None\" string encoding");
1939 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1941 "Reject invalid string encoding");
1942 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1943 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1944 "Set string encoding to ASCII");
1946 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1947 BT_CTF_STRING_ENCODING_UNKNOWN
,
1948 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1949 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1950 BT_CTF_STRING_ENCODING_ASCII
,
1951 "bt_ctf_field_type_string_get_encoding returns the correct value");
1953 structure_seq_type
= bt_ctf_field_type_structure_create();
1954 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1955 BT_CTF_TYPE_ID_STRUCT
,
1956 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1957 ok(structure_seq_type
, "Create a structure type");
1958 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1959 uint_8_type
, "seq_len") == 0,
1960 "Add a uint8_t type to a structure");
1961 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1962 sequence_type
, "a_sequence") == 0,
1963 "Add a sequence type to a structure");
1965 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1966 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1967 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1968 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1970 ok(bt_ctf_field_type_structure_get_field(NULL
,
1971 &ret_string
, &returned_type
, 1) < 0,
1972 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1973 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1974 NULL
, &returned_type
, 1) == 0,
1975 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1976 bt_put(returned_type
);
1977 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1978 &ret_string
, NULL
, 1) == 0,
1979 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1980 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1981 &ret_string
, &returned_type
, 10) < 0,
1982 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1983 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1984 &ret_string
, &returned_type
, 1) == 0,
1985 "bt_ctf_field_type_structure_get_field returns a field");
1986 ok(!strcmp(ret_string
, "a_sequence"),
1987 "bt_ctf_field_type_structure_get_field returns a correct field name");
1988 ok(returned_type
== sequence_type
,
1989 "bt_ctf_field_type_structure_get_field returns a correct field type");
1990 bt_put(returned_type
);
1992 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1993 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1994 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1995 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1996 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1997 structure_seq_type
, "a_sequence");
1998 ok(returned_type
== sequence_type
,
1999 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2000 bt_put(returned_type
);
2002 composite_structure_type
= bt_ctf_field_type_structure_create();
2003 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2004 string_type
, "a_string") == 0,
2005 "Add a string type to a structure");
2006 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
2007 structure_seq_type
, "inner_structure") == 0,
2008 "Add a structure type to a structure");
2010 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2011 NULL
, "a_sequence") == NULL
,
2012 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2013 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2014 structure_seq_type
, NULL
) == NULL
,
2015 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2016 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2017 structure_seq_type
, "a_sequence");
2018 ok(returned_type
== sequence_type
,
2019 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2020 bt_put(returned_type
);
2022 int_16
= bt_ctf_field_create(int_16_type
);
2023 ok(int_16
, "Instanciate a signed 16-bit integer");
2024 uint_12
= bt_ctf_field_create(uint_12_type
);
2025 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
2026 returned_type
= bt_ctf_field_get_type(int_16
);
2027 ok(returned_type
== int_16_type
,
2028 "bt_ctf_field_get_type returns the correct type");
2030 /* Can't modify types after instanciating them */
2031 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
2032 BT_CTF_INTEGER_BASE_DECIMAL
),
2033 "Check an integer type' base can't be modified after instanciation");
2034 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
2035 "Check an integer type's signedness can't be modified after instanciation");
2037 /* Check signed property is checked */
2038 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
2039 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2040 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
2041 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2043 /* Check overflows are properly tested for */
2044 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
2045 "Check -32768 is allowed for a signed 16-bit integer");
2046 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
2047 "Check 32767 is allowed for a signed 16-bit integer");
2048 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
2049 "Check 32768 is not allowed for a signed 16-bit integer");
2050 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
2051 "Check -32769 is not allowed for a signed 16-bit integer");
2052 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
2053 "Check -42 is allowed for a signed 16-bit integer");
2055 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
2056 "Check 4095 is allowed for an unsigned 12-bit integer");
2057 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
2058 "Check 4096 is not allowed for a unsigned 12-bit integer");
2059 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
2060 "Check 0 is allowed for an unsigned 12-bit integer");
2062 string
= bt_ctf_field_create(string_type
);
2063 ok(string
, "Instanciate a string field");
2064 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
2065 "Set a string's value");
2067 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
2068 ok(enumeration_type
,
2069 "Create an enumeration type with an unsigned 12-bit integer as container");
2070 enumeration
= bt_ctf_field_create(enumeration_type
);
2072 "Check enumeration types are validated before instantiation");
2077 bt_put(enumeration
);
2078 bt_put(composite_structure_type
);
2079 bt_put(structure_seq_type
);
2080 bt_put(string_type
);
2081 bt_put(sequence_type
);
2082 bt_put(uint_8_type
);
2083 bt_put(int_16_type
);
2084 bt_put(uint_12_type
);
2085 bt_put(enumeration_type
);
2086 bt_put(returned_type
);
2089 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2090 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2093 * Append enough events to force the underlying packet to be resized.
2094 * Also tests that a new event can be declared after a stream has been
2095 * instantiated and used/flushed.
2099 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2101 struct bt_ctf_field_type
*integer_type
=
2102 bt_ctf_field_type_integer_create(17);
2103 struct bt_ctf_field_type
*string_type
=
2104 bt_ctf_field_type_string_create();
2105 struct bt_ctf_event
*event
= NULL
;
2106 struct bt_ctf_field
*ret_field
= NULL
;
2107 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2108 uint64_t ret_uint64
;
2109 int events_appended
= 0;
2110 struct bt_ctf_field
*packet_context
= NULL
,
2111 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
2112 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2113 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2114 struct bt_ctf_field_type
*ep_type
= NULL
;
2116 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2118 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2120 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2121 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2127 * bt_ctf_stream_class_add_event_class() copies the field types
2128 * of event_class, so we retrieve the new ones to create the
2129 * appropriate fields.
2131 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2133 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2134 ep_type
, "field_1");
2135 assert(ep_field_1_type
);
2136 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2137 ep_type
, "a_string");
2138 assert(ep_a_string_type
);
2140 event
= bt_ctf_event_create(event_class
);
2141 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2142 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2143 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2144 "bt_ctf_event_get_payload_by_index returns a correct field");
2145 bt_put(ret_field_type
);
2148 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2149 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2150 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2151 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2154 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2155 event
= bt_ctf_event_create(event_class
);
2156 struct bt_ctf_field
*integer
=
2157 bt_ctf_field_create(ep_field_1_type
);
2158 struct bt_ctf_field
*string
=
2159 bt_ctf_field_create(ep_a_string_type
);
2161 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2162 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2163 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2166 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2167 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2171 /* Populate stream event context */
2172 stream_event_context
=
2173 bt_ctf_event_get_stream_event_context(event
);
2174 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2175 "common_event_context");
2176 BT_PUT(stream_event_context
);
2177 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2181 ret
|= bt_ctf_stream_append_event(stream
, event
);
2189 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2190 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2191 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2192 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2193 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2194 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2195 ok(ret
== 0 && ret_uint64
== 0,
2196 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2197 bt_ctf_stream_append_discarded_events(stream
, 1000);
2198 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2199 ok(ret
== 0 && ret_uint64
== 1000,
2200 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2203 ok(events_appended
, "Append 100 000 events to a stream");
2206 * Populate the custom packet context field with a dummy value
2207 * otherwise flush will fail.
2209 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2210 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2211 "custom_packet_context_field");
2212 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2214 ok(bt_ctf_stream_flush(stream
) == 0,
2215 "Flush a stream that forces a packet resize");
2216 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2217 ok(ret
== 0 && ret_uint64
== 1000,
2218 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2219 bt_put(integer_type
);
2220 bt_put(string_type
);
2221 bt_put(packet_context
);
2222 bt_put(packet_context_field
);
2223 bt_put(stream_event_context
);
2224 bt_put(event_class
);
2225 bt_put(ep_field_1_type
);
2226 bt_put(ep_a_string_type
);
2230 void test_empty_stream(struct bt_ctf_writer
*writer
)
2233 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2234 struct bt_ctf_stream_class
*stream_class
= NULL
;
2235 struct bt_ctf_stream
*stream
= NULL
;
2237 trace
= bt_ctf_writer_get_trace(writer
);
2239 diag("Failed to get trace from writer");
2244 stream_class
= bt_ctf_stream_class_create("empty_stream");
2245 if (!stream_class
) {
2246 diag("Failed to create stream class");
2251 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2252 "bt_ctf_stream_class_get_trace handles NULL correctly");
2253 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2254 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2256 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2258 diag("Failed to create writer stream");
2263 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2264 ok(ret_trace
== trace
,
2265 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2268 "Created a stream class with default attributes and an empty stream");
2272 bt_put(stream_class
);
2275 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
)
2278 struct bt_ctf_trace
*trace
= NULL
;
2279 struct bt_ctf_clock
*clock
= NULL
;
2280 struct bt_ctf_stream_class
*stream_class
= NULL
;
2281 struct bt_ctf_stream
*stream
= NULL
;
2282 struct bt_ctf_field_type
*integer_type
= NULL
,
2283 *sequence_type
= NULL
, *event_header_type
= NULL
;
2284 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2285 *event_header
= NULL
, *packet_header
= NULL
;
2286 struct bt_ctf_event_class
*event_class
= NULL
;
2287 struct bt_ctf_event
*event
= NULL
;
2289 trace
= bt_ctf_writer_get_trace(writer
);
2291 fail("Failed to get trace from writer");
2295 clock
= bt_ctf_trace_get_clock(trace
, 0);
2297 fail("Failed to get clock from trace");
2301 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2302 if (!stream_class
) {
2303 fail("Failed to create stream class");
2307 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2309 fail("Failed to set stream class clock");
2314 * Customize event header to add an "seq_len" integer member
2315 * which will be used as the length of a sequence in an event of this
2318 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2320 if (!event_header_type
) {
2321 fail("Failed to get event header type");
2325 integer_type
= bt_ctf_field_type_integer_create(13);
2326 if (!integer_type
) {
2327 fail("Failed to create length integer type");
2331 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2332 integer_type
, "seq_len");
2334 fail("Failed to add a new field to stream event header");
2338 event_class
= bt_ctf_event_class_create("sequence_event");
2340 fail("Failed to create event class");
2345 * This event's payload will contain a sequence which references
2346 * stream.event.header.seq_len as its length field.
2348 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2349 "stream.event.header.seq_len");
2350 if (!sequence_type
) {
2351 fail("Failed to create a sequence");
2355 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2358 fail("Failed to add a sequence to an event class");
2362 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2364 fail("Failed to add event class to stream class");
2368 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2370 fail("Failed to create stream")
2375 * We have defined a custom packet header field. We have to populate it
2378 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2379 if (!packet_header
) {
2380 fail("Failed to get stream packet header");
2384 integer
= bt_ctf_field_structure_get_field(packet_header
,
2385 "custom_trace_packet_header_field");
2387 fail("Failed to retrieve custom_trace_packet_header_field");
2391 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2393 fail("Failed to set custom_trace_packet_header_field value");
2398 event
= bt_ctf_event_create(event_class
);
2400 fail("Failed to create event");
2404 event_header
= bt_ctf_event_get_header(event
);
2405 if (!event_header
) {
2406 fail("Failed to get event header");
2410 integer
= bt_ctf_field_structure_get_field(event_header
,
2413 fail("Failed to get seq_len field from event header");
2417 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2419 fail("Failed to set seq_len value in event header");
2423 /* Populate both sequence integer fields */
2424 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2426 fail("Failed to retrieve sequence from event");
2430 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2432 fail("Failed to set sequence length");
2437 for (i
= 0; i
< 2; i
++) {
2438 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2440 fail("Failed to retrieve sequence element");
2444 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2446 fail("Failed to set sequence element value");
2454 ret
= bt_ctf_stream_append_event(stream
, event
);
2456 fail("Failed to append event to stream");
2460 ret
= bt_ctf_stream_flush(stream
);
2462 fail("Failed to flush custom_event_header stream");
2468 bt_put(stream_class
);
2469 bt_put(event_class
);
2473 bt_put(event_header
);
2474 bt_put(packet_header
);
2475 bt_put(sequence_type
);
2476 bt_put(integer_type
);
2477 bt_put(event_header_type
);
2480 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
)
2483 struct bt_ctf_trace
*trace
= NULL
;
2484 struct bt_ctf_clock
*clock
= NULL
;
2485 struct bt_ctf_stream_class
*stream_class
= NULL
;
2486 struct bt_ctf_stream
*stream
= NULL
,
2488 struct bt_ctf_event_class
*event_class
= NULL
;
2489 struct bt_ctf_event
*event
= NULL
;
2490 struct bt_ctf_field_type
*integer_type
= NULL
;
2491 struct bt_ctf_field
*integer
= NULL
;
2493 trace
= bt_ctf_writer_get_trace(writer
);
2495 diag("Failed to get trace from writer");
2500 clock
= bt_ctf_trace_get_clock(trace
, 0);
2502 diag("Failed to get clock from trace");
2507 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2508 if (!stream_class
) {
2509 diag("Failed to create stream class");
2514 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2516 diag("Failed to set stream class clock");
2520 event_class
= bt_ctf_event_class_create("some_event_class_name");
2521 integer_type
= bt_ctf_field_type_integer_create(32);
2522 if (!integer_type
) {
2523 diag("Failed to create integer field type");
2528 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2531 diag("Failed to add field to event class");
2535 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2538 diag("Failed to add event class to stream class");
2541 event
= bt_ctf_event_create(event_class
);
2543 diag("Failed to create event");
2548 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2550 diag("Failed to get integer field payload from event");
2555 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2557 diag("Failed to set integer field value");
2561 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2563 diag("Failed to create writer stream");
2568 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2569 "bt_ctf_event_get_stream handles NULL correctly");
2570 ok(bt_ctf_event_get_stream(event
) == NULL
,
2571 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2573 ret
= bt_ctf_stream_append_event(stream
, event
);
2575 diag("Failed to append event to stream");
2579 ret_stream
= bt_ctf_event_get_stream(event
);
2580 ok(ret_stream
== stream
,
2581 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2584 "Create an event before instanciating its associated stream");
2588 bt_put(stream_class
);
2589 bt_put(event_class
);
2591 bt_put(integer_type
);
2596 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2598 struct bt_ctf_event_class
*event_class
;
2600 event_class
= bt_ctf_event_class_create("Simple Event");
2601 assert(event_class
);
2602 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2603 "two event classes with the same name cannot cohabit within the same stream class");
2604 bt_put(event_class
);
2606 event_class
= bt_ctf_event_class_create("different name, ok");
2607 assert(event_class
);
2608 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2609 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2610 "two event classes with the same ID cannot cohabit within the same stream class");
2611 bt_put(event_class
);
2614 void test_trace_stream_class_clock(void)
2616 struct bt_ctf_trace
*trace
= NULL
;
2617 struct bt_ctf_stream_class
*sc1
= NULL
;
2618 struct bt_ctf_stream_class
*sc2
= NULL
;
2619 struct bt_ctf_clock
*sc1_clock
= NULL
;
2620 struct bt_ctf_clock
*sc2_clock
= NULL
;
2621 const char *clock_name
= "hello";
2623 trace
= bt_ctf_trace_create();
2625 sc1
= bt_ctf_stream_class_create(NULL
);
2627 sc2
= bt_ctf_stream_class_create(NULL
);
2629 sc1_clock
= bt_ctf_clock_create(clock_name
);
2631 sc2_clock
= bt_ctf_clock_create(clock_name
);
2634 ok(!bt_ctf_stream_class_set_clock(sc1
, sc1_clock
),
2635 "bt_ctf_stream_class_set_clock() succeeds for sc1");
2636 ok(!bt_ctf_stream_class_set_clock(sc2
, sc2_clock
),
2637 "bt_ctf_stream_class_set_clock() succeeds for sc2");
2638 ok(!bt_ctf_trace_add_stream_class(trace
, sc1
),
2639 "bt_ctf_trace_add_stream_class() succeeds with sc1");
2640 ok(bt_ctf_trace_add_stream_class(trace
, sc2
),
2641 "bt_ctf_trace_add_stream_class() fails with sc2 (different clock, same name)");
2651 struct bt_ctf_event_class
*create_minimal_event_class(void)
2653 struct bt_ctf_event_class
*ec
= NULL
;
2654 struct bt_ctf_field_type
*int_ft
= NULL
;
2657 int_ft
= bt_ctf_field_type_integer_create(23);
2659 ec
= bt_ctf_event_class_create("minimal");
2661 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2669 void test_create_writer_stream_from_stream_class(void)
2672 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2673 const char *writer_stream_name
= "writer stream instance";
2674 struct bt_ctf_writer
*writer
= NULL
;
2675 struct bt_ctf_trace
*writer_trace
= NULL
;
2676 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2677 struct bt_ctf_stream
*writer_stream
= NULL
;
2678 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2679 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2680 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2681 struct bt_ctf_event_class
*ec
= NULL
;
2682 struct bt_ctf_event
*event
= NULL
;
2683 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2684 struct bt_ctf_field
*int_field
= NULL
;
2685 struct bt_ctf_clock
*writer_clock
= NULL
;
2686 struct bt_ctf_clock
*non_writer_clock
= NULL
;
2688 if (!bt_mkdtemp(trace_path
)) {
2692 /* Create empty structure field type (event header) */
2693 empty_struct_ft
= bt_ctf_field_type_structure_create();
2694 assert(empty_struct_ft
);
2696 /* Create writer, writer stream class, stream, and clock */
2697 writer
= bt_ctf_writer_create(trace_path
);
2699 writer_trace
= bt_ctf_writer_get_trace(writer
);
2700 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2701 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2703 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2706 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2708 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2709 assert(writer_stream
);
2710 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2711 "bt_ctf_stream_get_name() returns the stream's name");
2712 writer_clock
= bt_ctf_clock_create("writer_clock");
2713 assert(writer_clock
);
2714 ret
= bt_ctf_trace_add_clock(writer_trace
, writer_clock
);
2717 /* Create non-writer trace, stream class, stream, and clock */
2718 non_writer_trace
= bt_ctf_trace_create();
2719 assert(non_writer_trace
);
2720 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2721 assert(non_writer_sc
);
2722 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2725 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2727 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2728 assert(non_writer_stream
);
2729 non_writer_clock
= bt_ctf_clock_create("non_writer_clock");
2730 assert(non_writer_clock
);
2731 ret
= bt_ctf_trace_add_clock(non_writer_trace
, non_writer_clock
);
2734 /* Create event class and event */
2735 ec
= create_minimal_event_class();
2737 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, ec
);
2739 event
= bt_ctf_event_create(ec
);
2741 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2743 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2746 * Verify non-writer stream: it should be impossible to append
2749 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2750 "bt_ctf_stream_append_event() fails with a non-writer stream");
2753 * Verify writer stream: it should be possible to append an
2756 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2757 "bt_ctf_stream_append_event() succeeds with a writer stream");
2760 * Verify that it's possible to get and set the value of a
2761 * writer mode clock.
2763 ok (!bt_ctf_clock_set_value(writer_clock
, 1000),
2764 "bt_ctf_clock_set_value() succeeds with a writer mode clock");
2765 ok (bt_ctf_clock_get_value(writer_clock
) == 1000,
2766 "bt_ctf_clock_get_value() succeeds with a writer mode clock");
2769 * Verify that it's impossible to get and set the value of a
2770 * non-writer mode clock.
2772 ok (bt_ctf_clock_set_value(non_writer_clock
, 1000),
2773 "bt_ctf_clock_set_value() fails with a non-writer mode clock");
2774 ok (bt_ctf_clock_get_value(non_writer_clock
) == -1ULL,
2775 "bt_ctf_clock_get_value() fails with a non-writer mode clock");
2778 BT_PUT(writer_trace
);
2780 BT_PUT(writer_stream
);
2781 BT_PUT(non_writer_trace
);
2782 BT_PUT(non_writer_sc
);
2783 BT_PUT(non_writer_stream
);
2787 BT_PUT(empty_struct_ft
);
2788 BT_PUT(writer_clock
);
2789 BT_PUT(non_writer_clock
);
2792 int main(int argc
, char **argv
)
2794 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2795 char metadata_path
[sizeof(trace_path
) + 9];
2796 const char *clock_name
= "test_clock";
2797 const char *clock_description
= "This is a test clock";
2798 const char *returned_clock_name
;
2799 const char *returned_clock_description
;
2800 const uint64_t frequency
= 1123456789;
2801 const int64_t offset_s
= 1351530929945824323;
2802 const int64_t offset
= 1234567;
2803 int64_t get_offset_s
,
2806 const uint64_t precision
= 10;
2807 const int is_absolute
= 0xFF;
2808 char *metadata_string
;
2809 struct bt_ctf_writer
*writer
;
2810 struct utsname name
;
2811 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2812 struct bt_ctf_clock
*clock
, *ret_clock
;
2813 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2814 struct bt_ctf_stream
*stream1
;
2815 const char *ret_string
;
2816 const unsigned char *ret_uuid
;
2817 unsigned char tmp_uuid
[16] = { 0 };
2818 struct bt_ctf_field_type
*packet_context_type
,
2819 *packet_context_field_type
,
2820 *packet_header_type
,
2821 *packet_header_field_type
,
2823 *stream_event_context_type
,
2825 *event_header_field_type
;
2826 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2827 struct bt_ctf_trace
*trace
;
2829 int64_t ret_int64_t
;
2830 struct bt_value
*obj
;
2833 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2837 plan_tests(NR_TESTS
);
2839 if (!bt_mkdtemp(trace_path
)) {
2843 strcpy(metadata_path
, trace_path
);
2844 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2846 writer
= bt_ctf_writer_create(trace_path
);
2847 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2849 ok(!bt_ctf_writer_get_trace(NULL
),
2850 "bt_ctf_writer_get_trace correctly handles NULL");
2851 trace
= bt_ctf_writer_get_trace(writer
);
2853 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2854 ok(bt_ctf_trace_set_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2855 "Set a trace's byte order to big endian");
2856 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2857 "bt_ctf_trace_get_byte_order returns a correct endianness");
2859 /* Add environment context to the trace */
2860 ret
= gethostname(hostname
, sizeof(hostname
));
2864 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2865 "Add host (%s) environment field to writer instance",
2867 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2869 "bt_ctf_writer_add_environment_field error with NULL writer");
2870 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2872 "bt_ctf_writer_add_environment_field error with NULL field name");
2873 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2875 "bt_ctf_writer_add_environment_field error with NULL field value");
2877 /* Test bt_ctf_trace_set_environment_field with an integer object */
2878 obj
= bt_value_integer_create_init(23);
2880 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2881 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2882 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2883 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2884 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2885 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2886 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2887 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2890 /* Test bt_ctf_trace_set_environment_field with a string object */
2891 obj
= bt_value_string_create_init("the value");
2893 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2894 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2897 /* Test bt_ctf_trace_set_environment_field_integer */
2898 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2900 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2901 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2902 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2903 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2905 "bt_ctf_trace_set_environment_field_integer succeeds");
2907 /* Test bt_ctf_trace_set_environment_field_string */
2908 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2910 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2911 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2912 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2913 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2915 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2916 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2918 "bt_ctf_trace_set_environment_field_string succeeds");
2920 /* Test bt_ctf_trace_get_environment_field_count */
2921 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2922 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2923 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2924 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2926 /* Test bt_ctf_trace_get_environment_field_name */
2927 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
2928 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2929 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
2930 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2931 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
2932 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2933 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
2934 ok(ret_string
&& !strcmp(ret_string
, "host"),
2935 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2936 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
2937 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2938 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2939 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
2940 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2941 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2942 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
2943 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2944 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2945 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
2946 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2947 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2949 /* Test bt_ctf_trace_get_environment_field_value */
2950 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
2951 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2952 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
2953 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2954 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
2955 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2956 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
2957 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2958 ok(!ret
&& ret_int64_t
== 23,
2959 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2961 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
2962 ret
= bt_value_string_get(obj
, &ret_string
);
2963 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2964 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2967 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2968 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2970 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2971 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
2972 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2973 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
2974 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2975 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
2977 ret
= bt_value_string_get(obj
, &ret_string
);
2978 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
2979 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2982 /* Test environment field replacement */
2983 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2985 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2986 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2987 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2988 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
2989 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2990 ok(!ret
&& ret_int64_t
== 654321,
2991 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2994 /* On Solaris, uname() can return any positive value on success */
2995 if (uname(&name
) < 0) {
3000 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3001 == 0, "Add sysname (%s) environment field to writer instance",
3003 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3004 name
.nodename
) == 0,
3005 "Add nodename (%s) environment field to writer instance",
3007 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3008 == 0, "Add release (%s) environment field to writer instance",
3010 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3011 == 0, "Add version (%s) environment field to writer instance",
3013 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3014 == 0, "Add machine (%s) environment field to writer istance",
3017 /* Define a clock and add it to the trace */
3018 ok(bt_ctf_clock_create("signed") == NULL
,
3019 "Illegal clock name rejected");
3020 ok(bt_ctf_clock_create(NULL
) == NULL
, "NULL clock name rejected");
3021 clock
= bt_ctf_clock_create(clock_name
);
3022 ok(clock
, "Clock created sucessfully");
3023 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3024 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3025 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3026 "Returned clock name is valid");
3028 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3029 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3030 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3031 "Clock description set successfully");
3033 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3034 ok(returned_clock_description
,
3035 "bt_ctf_clock_get_description returns a description.");
3036 ok(returned_clock_description
?
3037 !strcmp(returned_clock_description
, clock_description
) : 0,
3038 "Returned clock description is valid");
3040 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3041 "bt_ctf_clock_get_frequency returns the correct default frequency");
3042 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3043 "Set clock frequency");
3044 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3045 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3047 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3048 "bt_ctf_clock_get_offset_s succeeds");
3049 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3050 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3051 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3052 "Set clock offset (seconds)");
3053 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3054 "bt_ctf_clock_get_offset_s succeeds");
3055 ok(get_offset_s
== offset_s
,
3056 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3058 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3059 "bt_ctf_clock_get_offset succeeds");
3060 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3061 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3062 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3063 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3064 "bt_ctf_clock_get_offset succeeds");
3065 ok(get_offset
== offset
,
3066 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3068 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3069 "bt_ctf_clock_get_precision returns the correct default precision");
3070 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3071 "Set clock precision");
3072 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3073 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3075 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3076 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3077 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3078 "Set clock absolute property");
3079 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3080 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3082 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3083 "bt_ctf_clock_get_time succeeds");
3084 ok(get_time
== DEFAULT_CLOCK_TIME
,
3085 "bt_ctf_clock_get_time returns the correct default time");
3086 ok(bt_ctf_clock_get_value(clock
) == DEFAULT_CLOCK_VALUE
,
3087 "bt_ctf_clock_get_value returns the correct default value");
3088 ok(bt_ctf_clock_set_value(clock
, current_time
) == 0,
3090 ok(bt_ctf_clock_get_value(clock
) == current_time
,
3091 "bt_ctf_clock_get_value returns the correct value once it is set");
3092 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3094 ok(bt_ctf_clock_get_time(clock
, &get_time
) == 0,
3095 "bt_ctf_clock_get_time succeeds");
3096 ok(get_time
>= current_time
- 1 && get_time
<= current_time
+ 1,
3097 "bt_ctf_clock_get_time returns the correct time once it is set");
3099 ok(!bt_ctf_clock_get_name(NULL
),
3100 "bt_ctf_clock_get_name correctly handles NULL");
3101 ok(!bt_ctf_clock_get_description(NULL
),
3102 "bt_ctf_clock_get_description correctly handles NULL");
3103 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3104 "bt_ctf_clock_get_frequency correctly handles NULL");
3105 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3106 "bt_ctf_clock_get_precision correctly handles NULL");
3107 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3108 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3109 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3110 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3111 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3112 "bt_ctf_clock_get_offset correctly handles NULL clock");
3113 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3114 "bt_ctf_clock_get_offset correctly handles NULL output");
3115 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3116 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3117 ok(bt_ctf_clock_get_time(NULL
, &get_time
) < 0,
3118 "bt_ctf_clock_get_time correctly handles NULL clock");
3119 ok(bt_ctf_clock_get_time(clock
, NULL
) < 0,
3120 "bt_ctf_clock_get_time correctly handles NULL output");
3122 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3123 "bt_ctf_clock_set_description correctly handles NULL clock");
3124 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3125 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3126 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3127 "bt_ctf_clock_get_precision correctly handles NULL clock");
3128 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3129 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3130 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3131 "bt_ctf_clock_set_offset correctly handles NULL clock");
3132 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3133 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3134 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3135 "bt_ctf_clock_set_time correctly handles NULL clock");
3136 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3137 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3138 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3140 "bt_ctf_clock_get_uuid returns a UUID");
3142 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3143 /* Slightly modify UUID */
3144 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3147 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3148 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3149 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3150 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3151 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3152 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3153 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3155 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3156 ok(uuid_match(ret_uuid
, tmp_uuid
),
3157 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3159 /* Define a stream class */
3160 stream_class
= bt_ctf_stream_class_create("test_stream");
3162 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3163 "bt_ctf_stream_class_get_name handles NULL correctly");
3164 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3165 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3166 "bt_ctf_stream_class_get_name returns a correct stream class name");
3168 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3169 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3170 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3171 "bt_ctf_stream_class_get_clock handles NULL correctly");
3173 ok(stream_class
, "Create stream class");
3174 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3175 "Set a stream class' clock");
3176 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3177 ok(ret_clock
== clock
,
3178 "bt_ctf_stream_class_get_clock returns a correct clock");
3181 /* Test the event fields and event types APIs */
3184 /* Test fields copying */
3187 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3188 "bt_ctf_stream_class_get_id returns an error when no id is set");
3189 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3190 "bt_ctf_stream_class_get_id handles NULL correctly");
3191 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3192 "bt_ctf_stream_class_set_id handles NULL correctly");
3193 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3194 "Set an stream class' id");
3195 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3196 "bt_ctf_stream_class_get_id returns the correct value");
3198 /* Validate default event header fields */
3199 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3200 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3201 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3204 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3205 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_TYPE_ID_STRUCT
,
3206 "Default event header type is a structure");
3207 event_header_field_type
=
3208 bt_ctf_field_type_structure_get_field_type_by_name(
3209 ret_field_type
, "id");
3210 ok(event_header_field_type
,
3211 "Default event header type contains an \"id\" field");
3212 ok(bt_ctf_field_type_get_type_id(
3213 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3214 "Default event header \"id\" field is an integer");
3215 bt_put(event_header_field_type
);
3216 event_header_field_type
=
3217 bt_ctf_field_type_structure_get_field_type_by_name(
3218 ret_field_type
, "timestamp");
3219 ok(event_header_field_type
,
3220 "Default event header type contains a \"timestamp\" field");
3221 ok(bt_ctf_field_type_get_type_id(
3222 event_header_field_type
) == BT_CTF_TYPE_ID_INTEGER
,
3223 "Default event header \"timestamp\" field is an integer");
3224 bt_put(event_header_field_type
);
3225 bt_put(ret_field_type
);
3227 /* Add a custom trace packet header field */
3228 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3229 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3230 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3231 ok(packet_header_type
,
3232 "bt_ctf_trace_get_packet_header_type returns a packet header");
3233 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_TYPE_ID_STRUCT
,
3234 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3235 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3236 packet_header_type
, "magic");
3237 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3238 bt_put(ret_field_type
);
3239 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3240 packet_header_type
, "uuid");
3241 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3242 bt_put(ret_field_type
);
3243 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3244 packet_header_type
, "stream_id");
3245 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3246 bt_put(ret_field_type
);
3248 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3249 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3250 packet_header_field_type
, "custom_trace_packet_header_field"),
3251 "Added a custom trace packet header field successfully");
3253 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3254 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3255 ok(bt_ctf_trace_set_packet_header_type(trace
, NULL
) < 0,
3256 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3257 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3258 "Set a trace packet_header_type successfully");
3260 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3261 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3263 /* Add a custom field to the stream class' packet context */
3264 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3265 ok(packet_context_type
,
3266 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3267 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_TYPE_ID_STRUCT
,
3268 "Packet context is a structure");
3270 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3271 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3272 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
),
3273 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3275 integer_type
= bt_ctf_field_type_integer_create(32);
3276 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3278 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3279 /* Create a "uint5_t" equivalent custom packet context field */
3280 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3282 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3283 packet_context_field_type
, "custom_packet_context_field");
3284 ok(ret
== 0, "Packet context field added successfully");
3286 /* Define a stream event context containing a my_integer field. */
3287 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3288 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3289 ok(bt_ctf_stream_class_get_event_context_type(
3290 stream_class
) == NULL
,
3291 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3292 stream_event_context_type
= bt_ctf_field_type_structure_create();
3293 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3294 integer_type
, "common_event_context");
3296 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3297 stream_event_context_type
) < 0,
3298 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3299 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3301 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3302 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3304 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3306 ok(bt_ctf_stream_class_set_event_context_type(
3307 stream_class
, stream_event_context_type
) == 0,
3308 "Set a new stream event context type");
3309 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3311 ok(ret_field_type
== stream_event_context_type
,
3312 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3313 bt_put(ret_field_type
);
3315 /* Instantiate a stream and append events */
3316 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3317 ok(stream1
, "Instanciate a stream class from writer");
3320 * Creating a stream through a writer adds the given stream
3321 * class to the writer's trace, thus registering the stream
3322 * class's clock to the trace.
3324 ok(bt_ctf_trace_get_clock_count(NULL
) < 0,
3325 "bt_ctf_trace_get_clock_count correctly handles NULL");
3326 ok(bt_ctf_trace_get_clock_count(trace
) == 1,
3327 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3328 ok(!bt_ctf_trace_get_clock(NULL
, 0),
3329 "bt_ctf_trace_get_clock correctly handles NULL");
3330 ok(!bt_ctf_trace_get_clock(trace
, -1),
3331 "bt_ctf_trace_get_clock correctly handles negative indexes");
3332 ok(!bt_ctf_trace_get_clock(trace
, 1),
3333 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3334 ret_clock
= bt_ctf_trace_get_clock(trace
, 0);
3335 ok(ret_clock
== clock
,
3336 "bt_ctf_trace_get_clock returns the right clock instance");
3338 ok(!bt_ctf_trace_get_clock_by_name(trace
, NULL
),
3339 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3340 ok(!bt_ctf_trace_get_clock_by_name(NULL
, clock_name
),
3341 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3342 ok(!bt_ctf_trace_get_clock_by_name(NULL
, NULL
),
3343 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3344 ret_clock
= bt_ctf_trace_get_clock_by_name(trace
, clock_name
);
3345 ok(ret_clock
== clock
,
3346 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3348 ok(!bt_ctf_trace_get_clock_by_name(trace
, "random"),
3349 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3351 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3352 "bt_ctf_stream_get_class correctly handles NULL");
3353 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3354 ok(ret_stream_class
,
3355 "bt_ctf_stream_get_class returns a stream class");
3356 ok(ret_stream_class
== stream_class
,
3357 "Returned stream class is of the correct type");
3360 * Packet header, packet context, event header, and stream
3361 * event context types were copied for the resolving
3364 BT_PUT(packet_header_type
);
3365 BT_PUT(packet_context_type
);
3366 BT_PUT(stream_event_context_type
);
3367 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3368 assert(packet_header_type
);
3369 packet_context_type
=
3370 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3371 assert(packet_context_type
);
3372 stream_event_context_type
=
3373 bt_ctf_stream_class_get_event_context_type(stream_class
);
3374 assert(stream_event_context_type
);
3377 * Try to modify the packet context type after a stream has been
3380 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3381 packet_header_field_type
, "should_fail");
3383 "Trace packet header type can't be modified once a stream has been instanciated");
3386 * Try to modify the packet context type after a stream has been
3389 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3390 packet_context_field_type
, "should_fail");
3392 "Packet context type can't be modified once a stream has been instanciated");
3395 * Try to modify the stream event context type after a stream has been
3398 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3399 integer_type
, "should_fail");
3401 "Stream event context type can't be modified once a stream has been instanciated");
3403 /* Should fail after instanciating a stream (frozen) */
3404 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3405 "Changes to a stream class that was already instantiated fail");
3407 /* Populate the custom packet header field only once for all tests */
3408 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3409 "bt_ctf_stream_get_packet_header handles NULL correctly");
3410 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3412 "bt_ctf_stream_get_packet_header returns a packet header");
3413 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3414 ok(ret_field_type
== packet_header_type
,
3415 "Stream returns a packet header of the appropriate type");
3416 bt_put(ret_field_type
);
3417 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3418 "custom_trace_packet_header_field");
3419 ok(packet_header_field
,
3420 "Packet header structure contains a custom field with the appropriate name");
3421 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3422 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3423 "Custom packet header field is of the expected type");
3424 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3425 54321), "Set custom packet header value successfully");
3426 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3427 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3428 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3429 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3430 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3431 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3432 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3433 "Successfully set a stream's packet header");
3435 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3436 "Add environment field to writer after stream creation");
3438 test_trace_stream_class_clock();
3440 test_create_writer_stream_from_stream_class();
3442 test_instanciate_event_before_stream(writer
);
3444 append_simple_event(stream_class
, stream1
, clock
);
3446 packet_resize_test(stream_class
, stream1
, clock
);
3448 append_complex_event(stream_class
, stream1
, clock
);
3450 append_existing_event_class(stream_class
);
3452 test_empty_stream(writer
);
3454 test_custom_event_header_stream(writer
);
3456 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3457 ok(metadata_string
, "Get metadata string");
3459 bt_ctf_writer_flush_metadata(writer
);
3460 validate_metadata(argv
[1], metadata_path
);
3461 validate_trace(argv
[2], trace_path
);
3464 bt_put(ret_stream_class
);
3467 bt_put(packet_context_type
);
3468 bt_put(packet_context_field_type
);
3469 bt_put(integer_type
);
3470 bt_put(stream_event_context_type
);
3471 bt_put(ret_field_type
);
3472 bt_put(packet_header_type
);
3473 bt_put(packet_header_field_type
);
3474 bt_put(packet_header
);
3475 bt_put(packet_header_field
);
3477 free(metadata_string
);
3478 bt_put(stream_class
);
3480 /* Remove all trace files and delete temporary trace directory */
3481 DIR *trace_dir
= opendir(trace_path
);
3483 perror("# opendir");
3487 struct dirent
*entry
;
3488 while ((entry
= readdir(trace_dir
))) {
3490 char filename
[PATH_MAX
];
3492 if (snprintf(filename
, sizeof(filename
), "%s/%s",
3493 trace_path
, entry
->d_name
) <= 0) {
3497 if (stat(entry
->d_name
, &st
)) {
3501 if (S_ISREG(st
.st_mode
)) {
3502 unlinkat(bt_dirfd(trace_dir
), entry
->d_name
, 0);
3507 closedir(trace_dir
);