6 * Copyright 2013 - 2017 - 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/ctf-ir/packet.h>
30 #include <babeltrace/ctf-ir/clock-class.h>
31 #include <babeltrace/ctf-ir/trace.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf/events.h>
34 #include <babeltrace/values.h>
37 #include <babeltrace/compat/stdlib-internal.h>
39 #include <babeltrace/compat/limits-internal.h>
40 #include <babeltrace/compat/stdio-internal.h>
50 #define METADATA_LINE_SIZE 512
51 #define SEQUENCE_TEST_LENGTH 10
52 #define ARRAY_TEST_LENGTH 5
53 #define PACKET_RESIZE_TEST_DEF_LENGTH 100000
55 #define DEFAULT_CLOCK_FREQ 1000000000
56 #define DEFAULT_CLOCK_PRECISION 1
57 #define DEFAULT_CLOCK_OFFSET 0
58 #define DEFAULT_CLOCK_OFFSET_S 0
59 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
60 #define DEFAULT_CLOCK_TIME 0
61 #define DEFAULT_CLOCK_VALUE 0
66 char sysname
[BABELTRACE_HOST_NAME_MAX
];
67 char nodename
[BABELTRACE_HOST_NAME_MAX
];
68 char release
[BABELTRACE_HOST_NAME_MAX
];
69 char version
[BABELTRACE_HOST_NAME_MAX
];
70 char machine
[BABELTRACE_HOST_NAME_MAX
];
73 static int64_t current_time
= 42;
74 static unsigned int packet_resize_test_length
= PACKET_RESIZE_TEST_DEF_LENGTH
;
76 /* Return 1 if uuids match, zero if different. */
78 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
83 if (!uuid_a
|| !uuid_b
) {
87 for (i
= 0; i
< 16; i
++) {
88 if (uuid_a
[i
] != uuid_b
[i
]) {
99 void validate_trace(char *parser_path
, char *trace_path
)
102 gchar
*babeltrace_output_path
;
103 int babeltrace_output_fd
= -1;
110 babeltrace_output_fd
= g_file_open_tmp("babeltrace_output_XXXXXX",
111 &babeltrace_output_path
, NULL
);
112 unlink(babeltrace_output_path
);
113 g_free(babeltrace_output_path
);
115 if (babeltrace_output_fd
== -1) {
116 diag("Failed to create a temporary file for trace parsing.");
124 waitpid(pid
, &status
, 0);
125 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
127 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
129 perror("# dup2 babeltrace_output_fd to STDOUT");
133 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
135 perror("# dup2 babeltrace_output_fd to STDERR");
139 execl(parser_path
, parser_path
, trace_path
, NULL
);
140 perror("# Could not launch the babeltrace process");
144 ok(ret
== 0, "Babeltrace could read the resulting trace");
146 if (ret
&& babeltrace_output_fd
>= 0) {
148 size_t len
= METADATA_LINE_SIZE
;
149 FILE *babeltrace_output_fp
= NULL
;
151 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
152 if (!babeltrace_output_fp
) {
153 perror("fdopen on babeltrace_output_fd");
156 babeltrace_output_fd
= -1;
160 diag("malloc error");
162 rewind(babeltrace_output_fp
);
163 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
169 if (babeltrace_output_fp
) {
170 if (fclose(babeltrace_output_fp
)) {
171 diag("fclose error");
176 if (babeltrace_output_fd
>= 0) {
177 if (close(babeltrace_output_fd
)) {
184 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
185 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
187 /* Create and add a simple event class */
188 struct bt_ctf_event_class
*simple_event_class
=
189 bt_ctf_event_class_create("Simple Event");
190 struct bt_ctf_field_type
*uint_12_type
=
191 bt_ctf_field_type_integer_create(12);
192 struct bt_ctf_field_type
*int_64_type
=
193 bt_ctf_field_type_integer_create(64);
194 struct bt_ctf_field_type
*float_type
=
195 bt_ctf_field_type_floating_point_create();
196 struct bt_ctf_field_type
*enum_type
;
197 struct bt_ctf_field_type
*enum_type_unsigned
=
198 bt_ctf_field_type_enumeration_create(uint_12_type
);
199 struct bt_ctf_field_type
*event_context_type
=
200 bt_ctf_field_type_structure_create();
201 struct bt_ctf_field_type
*event_payload_type
= NULL
;
202 struct bt_ctf_field_type
*returned_type
;
203 struct bt_ctf_event
*simple_event
;
204 struct bt_ctf_field
*integer_field
;
205 struct bt_ctf_field
*float_field
;
206 struct bt_ctf_field
*enum_field
;
207 struct bt_ctf_field
*enum_field_unsigned
;
208 struct bt_ctf_field
*enum_container_field
;
209 const char *mapping_name_test
= "truie";
210 const double double_test_value
= 3.1415;
211 struct bt_ctf_field
*enum_container_field_unsigned
;
212 const char *mapping_name_negative_test
= "negative_value";
213 const char *ret_char
;
215 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
216 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
217 struct bt_ctf_event_class
*ret_event_class
;
218 struct bt_ctf_field
*packet_context
;
219 struct bt_ctf_field
*packet_context_field
;
220 struct bt_ctf_field
*stream_event_context
;
221 struct bt_ctf_field
*stream_event_context_field
;
222 struct bt_ctf_field
*event_context
;
223 struct bt_ctf_field
*event_context_field
;
224 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
225 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
226 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
227 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
230 ok(uint_12_type
, "Create an unsigned integer type");
232 ok(!bt_ctf_field_type_integer_set_signed(int_64_type
, 1),
233 "Set signed 64 bit integer signedness to true");
234 ok(int_64_type
, "Create a signed integer type");
235 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
237 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
238 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
239 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
240 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
241 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
242 bt_put(returned_type
);
244 bt_ctf_field_type_set_alignment(float_type
, 32);
245 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
246 "bt_ctf_field_type_get_alignment handles NULL correctly");
247 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
248 "bt_ctf_field_type_get_alignment returns a correct value");
250 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
251 "Set a floating point type's exponent digit count");
252 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
253 "Set a floating point type's mantissa digit count");
255 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
256 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
257 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
258 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
259 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
260 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
261 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
262 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
264 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
265 mapping_name_negative_test
, -12345, 0) == 0,
266 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
267 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
268 "escaping; \"test\"", 1, 1) == 0,
269 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
270 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
271 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
272 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
273 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
274 "event clock int float", 5, 22) == 0,
275 "Accept enumeration mapping strings containing reserved keywords");
276 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
278 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
279 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
280 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
281 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
282 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
283 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
284 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
286 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(NULL
, -42);
287 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly");
289 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, -4200000);
290 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values");
292 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, 3);
293 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value");
294 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, NULL
, NULL
, NULL
) == 0,
295 "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly");
298 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
299 "enum_field") == 0, "Add signed enumeration field to event");
301 ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL
, 0, &ret_char
,
302 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
303 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly");
304 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, NULL
,
305 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
306 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly");
307 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
308 NULL
, &ret_range_end_int64_t
) == 0,
309 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly");
310 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
311 &ret_range_start_int64_t
, NULL
) == 0,
312 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly");
313 /* Assumes entries are sorted by range_start values. */
314 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 6, &ret_char
,
315 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
316 "bt_ctf_field_type_enumeration_get_mapping_signed returns a value");
317 ok(!strcmp(ret_char
, mapping_name_test
),
318 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name");
319 ok(ret_range_start_int64_t
== 42,
320 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start");
321 ok(ret_range_end_int64_t
== 42,
322 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end");
324 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
325 "escaping; \"test\"", 0, 0) == 0,
326 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
327 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
328 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
329 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
330 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
331 "event clock int float", 5, 22) == 0,
332 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
333 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
334 42, 42) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts single-value ranges");
335 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
336 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names");
337 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
338 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries");
339 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
340 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
341 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
342 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
344 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
345 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
346 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 6,
347 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
349 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
350 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
351 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
352 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
353 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
354 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
355 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
356 NULL
, &ret_range_end_uint64_t
) == 0,
357 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
358 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
359 &ret_range_start_uint64_t
, NULL
) == 0,
360 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
361 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 4, &ret_char
,
362 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
363 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
364 ok(!strcmp(ret_char
, mapping_name_test
),
365 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
366 ok(ret_range_start_uint64_t
== 42,
367 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
368 ok(ret_range_end_uint64_t
== 42,
369 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
371 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
373 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
376 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
378 /* Set an event context type which will contain a single integer. */
379 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
380 "event_specific_context"),
381 "Add event specific context field");
382 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
383 "bt_ctf_event_class_get_context_type handles NULL correctly");
385 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
386 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
387 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
388 "Set an event class' context type successfully");
389 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
390 ok(returned_type
== event_context_type
,
391 "bt_ctf_event_class_get_context_type returns the appropriate type");
392 bt_put(returned_type
);
394 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
395 "Adding simple event class to stream class");
398 * bt_ctf_stream_class_add_event_class() copies the field types
399 * of simple_event_class, so we retrieve the new ones to create
400 * the appropriate fields.
402 BT_PUT(event_context_type
);
403 BT_PUT(event_payload_type
);
404 event_payload_type
= bt_ctf_event_class_get_payload_type(
406 assert(event_payload_type
);
407 event_context_type
= bt_ctf_event_class_get_context_type(
409 assert(event_context_type
);
410 ep_integer_field_type
=
411 bt_ctf_field_type_structure_get_field_type_by_name(
412 event_payload_type
, "integer_field");
413 assert(ep_integer_field_type
);
415 bt_ctf_field_type_structure_get_field_type_by_name(
416 event_payload_type
, "enum_field");
417 assert(ep_enum_field_type
);
418 ep_enum_field_unsigned_type
=
419 bt_ctf_field_type_structure_get_field_type_by_name(
420 event_payload_type
, "enum_field_unsigned");
421 assert(ep_enum_field_unsigned_type
);
423 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
424 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
425 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
426 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
427 ok(bt_ctf_stream_class_get_event_class_by_index(NULL
, 0) == NULL
,
428 "bt_ctf_stream_class_get_event_class handles NULL correctly");
429 ok(bt_ctf_stream_class_get_event_class_by_index(stream_class
, 8724) == NULL
,
430 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
431 ret_event_class
= bt_ctf_stream_class_get_event_class_by_index(stream_class
, 0);
432 ok(ret_event_class
== simple_event_class
,
433 "bt_ctf_stream_class_get_event_class returns the correct event class");
434 bt_put(ret_event_class
);
435 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
436 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
437 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
438 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
440 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
441 ok(ret_event_class
== simple_event_class
,
442 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
443 bt_put(ret_event_class
);
445 simple_event
= bt_ctf_event_create(simple_event_class
);
447 "Instantiate an event containing a single integer field");
449 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
450 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
451 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
452 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
454 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
455 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
456 "bt_ctf_field_floating_point_get_value fails on an unset float field");
457 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
458 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
459 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
460 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
461 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
462 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
463 "bt_ctf_field_floating_point_get_value returns a double value");
464 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
465 "bt_ctf_field_floating_point_get_value returns a correct value");
467 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
470 iter
= bt_ctf_field_enumeration_get_mappings(NULL
);
471 ok(!iter
, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
472 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
473 ok(!iter
, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
474 enum_container_field
= bt_ctf_field_enumeration_get_container(
476 ok(bt_ctf_field_signed_integer_set_value(
477 enum_container_field
, -42) == 0,
478 "Set signed enumeration container value");
479 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
480 ok(iter
, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
481 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, &ret_char
, NULL
, NULL
);
482 ok(!ret
&& ret_char
, "bt_ctf_field_type_enumeration_mapping_iterator_get_signed return a mapping name");
484 ok(!strcmp(ret_char
, mapping_name_negative_test
),
485 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
486 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
490 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
491 assert(enum_field_unsigned
);
492 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
493 enum_field_unsigned
);
494 ok(bt_ctf_field_unsigned_integer_set_value(
495 enum_container_field_unsigned
, 42) == 0,
496 "Set unsigned enumeration container value");
497 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
498 enum_field_unsigned
);
500 iter
= bt_ctf_field_enumeration_get_mappings(enum_field_unsigned
);
502 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, &ret_char
, NULL
, NULL
);
503 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
504 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container");
506 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
508 /* Populate stream event context */
509 stream_event_context
=
510 bt_ctf_event_get_stream_event_context(simple_event
);
511 assert(stream_event_context
);
512 stream_event_context_field
= bt_ctf_field_structure_get_field(
513 stream_event_context
, "common_event_context");
514 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
516 /* Populate the event's context */
517 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
518 "bt_ctf_event_get_event_context handles NULL correctly");
519 event_context
= bt_ctf_event_get_event_context(simple_event
);
521 "bt_ctf_event_get_event_context returns a field");
522 returned_type
= bt_ctf_field_get_type(event_context
);
523 ok(returned_type
== event_context_type
,
524 "bt_ctf_event_get_event_context returns a field of the appropriate type");
525 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
526 "event_specific_context");
527 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
528 "Successfully set an event context's value");
529 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
530 "bt_ctf_event_set_event_context handles a NULL event correctly");
531 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
532 "bt_ctf_event_set_event_context handles a NULL event context correctly");
533 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
534 "bt_ctf_event_set_event_context rejects a context of the wrong type");
535 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
536 "Set an event context successfully");
538 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
539 "Append simple event to trace stream");
541 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
542 "bt_ctf_stream_get_packet_context handles NULL correctly");
543 packet_context
= bt_ctf_stream_get_packet_context(stream
);
545 "bt_ctf_stream_get_packet_context returns a packet context");
547 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
549 ok(packet_context_field
,
550 "Packet context contains the default packet_size field.");
551 bt_put(packet_context_field
);
552 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
553 "custom_packet_context_field");
554 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
555 "Custom packet context field value successfully set.");
557 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
558 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
559 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
560 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
561 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
562 "Successfully set a stream's packet context");
564 ok(bt_ctf_stream_flush(stream
) == 0,
565 "Flush trace stream with one event");
567 bt_put(simple_event_class
);
568 bt_put(simple_event
);
569 bt_put(uint_12_type
);
573 bt_put(enum_type_unsigned
);
574 bt_put(returned_type
);
575 bt_put(event_context_type
);
576 bt_put(integer_field
);
579 bt_put(enum_field_unsigned
);
580 bt_put(enum_container_field
);
581 bt_put(enum_container_field_unsigned
);
582 bt_put(packet_context
);
583 bt_put(packet_context_field
);
584 bt_put(stream_event_context
);
585 bt_put(stream_event_context_field
);
586 bt_put(event_context
);
587 bt_put(event_context_field
);
588 bt_put(event_payload_type
);
589 bt_put(ep_integer_field_type
);
590 bt_put(ep_enum_field_type
);
591 bt_put(ep_enum_field_unsigned_type
);
596 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
597 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
600 struct event_class_attrs_counts
;
601 const char *complex_test_event_string
= "Complex Test Event";
602 const char *test_string_1
= "Test ";
603 const char *test_string_2
= "string ";
604 const char *test_string_3
= "abcdefghi";
605 const char *test_string_4
= "abcd\0efg\0hi";
606 const char *test_string_cat
= "Test string abcdeabcd";
607 struct bt_ctf_field_type
*uint_35_type
=
608 bt_ctf_field_type_integer_create(35);
609 struct bt_ctf_field_type
*int_16_type
=
610 bt_ctf_field_type_integer_create(16);
611 struct bt_ctf_field_type
*uint_3_type
=
612 bt_ctf_field_type_integer_create(3);
613 struct bt_ctf_field_type
*enum_variant_type
=
614 bt_ctf_field_type_enumeration_create(uint_3_type
);
615 struct bt_ctf_field_type
*variant_type
=
616 bt_ctf_field_type_variant_create(enum_variant_type
,
618 struct bt_ctf_field_type
*string_type
=
619 bt_ctf_field_type_string_create();
620 struct bt_ctf_field_type
*sequence_type
;
621 struct bt_ctf_field_type
*array_type
;
622 struct bt_ctf_field_type
*inner_structure_type
=
623 bt_ctf_field_type_structure_create();
624 struct bt_ctf_field_type
*complex_structure_type
=
625 bt_ctf_field_type_structure_create();
626 struct bt_ctf_field_type
*ret_field_type
;
627 struct bt_ctf_event_class
*event_class
;
628 struct bt_ctf_event
*event
;
629 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
630 *inner_structure_field
, *complex_structure_field
,
631 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
632 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
633 *stream_event_ctx_int_field
, *ret_field
;
634 uint64_t ret_unsigned_int
;
635 int64_t ret_signed_int
;
636 const char *ret_string
;
637 struct bt_ctf_stream_class
*ret_stream_class
;
638 struct bt_ctf_event_class
*ret_event_class
;
639 struct bt_ctf_field
*packet_context
, *packet_context_field
;
640 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
642 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
643 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
644 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
645 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
646 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
647 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
648 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
649 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
650 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
651 "Set alignment of signed 16 bit integer to 32");
652 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
653 "Set integer signedness to true");
654 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
655 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
656 "Set signed 16 bit integer base to hexadecimal");
658 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
659 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
662 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
663 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
664 ret_field_type
= bt_ctf_field_type_array_get_element_type(
666 ok(ret_field_type
== int_16_type
,
667 "bt_ctf_field_type_array_get_element_type returns the correct type");
668 bt_put(ret_field_type
);
670 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
671 "bt_ctf_field_type_array_get_length handles NULL correctly");
672 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
673 "bt_ctf_field_type_array_get_length returns the correct length");
675 ok(bt_ctf_field_type_structure_add_field(inner_structure_type
,
676 inner_structure_type
, "yes"), "Cannot add self to structure");
677 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
678 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
679 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
680 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
681 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
682 array_type
, "an_array"), "Add an_array field to inner structure");
684 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
686 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
688 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
689 "UINT35_TYPE", 2, 7);
691 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(NULL
, "INT16_TYPE");
692 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly");
694 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, "INT16_TYPE");
695 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly");
696 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
697 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly");
700 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, NULL
);
701 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly");
703 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL
, 1);
704 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly");
706 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, -42);
707 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly");
708 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) != 0,
709 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly");
712 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, 5);
713 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly");
714 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
715 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly");
718 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
719 "An unknown entry"), "Reject a variant field based on an unknown tag value");
720 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
721 "UINT3_TYPE") == 0, "Add a field to a variant");
722 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
723 "INT16_TYPE"), "Add INT16_TYPE field to variant");
724 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
725 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
727 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
728 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
729 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
730 ok(ret_field_type
== enum_variant_type
,
731 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
732 bt_put(ret_field_type
);
734 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
735 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
736 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
737 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
738 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
739 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
740 "INT16_TYPE") == NULL
,
741 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
742 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
744 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
745 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
746 variant_type
, "INT16_TYPE");
747 ok(ret_field_type
== int_16_type
,
748 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
749 bt_put(ret_field_type
);
751 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
752 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
753 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
754 "bt_ctf_field_type_variant_get_field_count returns the correct count");
756 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
757 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
758 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
759 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
760 bt_put(ret_field_type
);
761 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
762 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
763 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
764 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
765 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
766 "bt_ctf_field_type_variant_get_field returns a field");
767 ok(!strcmp("INT16_TYPE", ret_string
),
768 "bt_ctf_field_type_variant_get_field returns a correct field name");
769 ok(ret_field_type
== int_16_type
,
770 "bt_ctf_field_type_variant_get_field returns a correct field type");
771 bt_put(ret_field_type
);
773 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
774 enum_variant_type
, "variant_selector"),
775 "Add variant_selector field to complex structure");
776 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
777 string_type
, "a_string"), "Add a_string field to complex structure");
778 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
779 variant_type
, "variant_value"),
780 "Add variant_value field to complex structure");
781 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
782 inner_structure_type
, "inner_structure"),
783 "Add inner_structure field to complex structure");
785 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
786 ok(event_class
, "Create an event class");
787 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
788 "Reject addition of a field with an empty name to an event");
789 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
790 "Reject addition of a field with a NULL type to an event");
791 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
793 "Reject addition of a type with an illegal name to an event");
794 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
796 "Add field of type unsigned integer to an event");
797 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
798 "int_16") == 0, "Add field of type signed integer to an event");
799 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
800 "complex_structure") == 0,
801 "Add composite structure to an event");
803 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
804 "bt_ctf_event_class_get_name handles NULL correctly");
805 ret_string
= bt_ctf_event_class_get_name(event_class
);
806 ok(!strcmp(ret_string
, complex_test_event_string
),
807 "bt_ctf_event_class_get_name returns a correct name");
808 ok(bt_ctf_event_class_get_id(event_class
) < 0,
809 "bt_ctf_event_class_get_id returns a negative value when not set");
810 ok(bt_ctf_event_class_get_id(NULL
) < 0,
811 "bt_ctf_event_class_get_id handles NULL correctly");
812 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
813 "bt_ctf_event_class_set_id handles NULL correctly");
814 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
815 "Set an event class' id");
816 ok(bt_ctf_event_class_get_id(event_class
) == 42,
817 "bt_ctf_event_class_get_id returns the correct value");
819 /* Test event class attributes */
820 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
,
821 "event class has the expected initial log level");
822 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
823 "as expected, event class has no initial EMF URI");
824 ok(bt_ctf_event_class_set_log_level(NULL
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
825 "bt_ctf_event_class_set_log_level handles a NULL event class correctly");
826 ok(bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
),
827 "bt_ctf_event_class_set_log_level handles an unknown log level correctly");
828 ok(!bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
829 "bt_ctf_event_class_set_log_level succeeds with a valid log level");
830 ok(bt_ctf_event_class_get_log_level(NULL
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
,
831 "bt_ctf_event_class_get_log_level handles a NULL event class correctly");
832 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
,
833 "bt_ctf_event_class_get_log_level returns the expected log level");
834 ok(bt_ctf_event_class_set_emf_uri(NULL
, "http://diamon.org/babeltrace/"),
835 "bt_ctf_event_class_set_emf_uri handles a NULL event class correctly");
836 ok(!bt_ctf_event_class_set_emf_uri(event_class
, "http://diamon.org/babeltrace/"),
837 "bt_ctf_event_class_set_emf_uri succeeds with a valid EMF URI");
838 ok(!bt_ctf_event_class_get_emf_uri(NULL
),
839 "bt_ctf_event_class_get_emf_uri handles a NULL event class correctly");
840 ok(strcmp(bt_ctf_event_class_get_emf_uri(event_class
), "http://diamon.org/babeltrace/") == 0,
841 "bt_ctf_event_class_get_emf_uri returns the expected EMF URI");
842 ok(!bt_ctf_event_class_set_emf_uri(event_class
, NULL
),
843 "bt_ctf_event_class_set_emf_uri succeeds with NULL (to reset)");
844 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
845 "as expected, event class has no EMF URI after reset");
847 /* Add event class to the stream class */
848 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
849 "Reject addition of NULL event class to a stream class");
850 ok(bt_ctf_stream_class_add_event_class(stream_class
,
851 event_class
) == 0, "Add an event class to stream class");
853 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
854 "bt_ctf_event_class_get_stream_class handles NULL correctly");
855 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
856 ok(ret_stream_class
== stream_class
,
857 "bt_ctf_event_class_get_stream_class returns the correct stream class");
858 bt_put(ret_stream_class
);
860 ok(bt_ctf_event_class_get_payload_type_field_count(NULL
) < 0,
861 "bt_ctf_event_class_get_field_count handles NULL correctly");
862 ok(bt_ctf_event_class_get_payload_type_field_count(event_class
) == 3,
863 "bt_ctf_event_class_get_field_count returns a correct value");
865 ok(bt_ctf_event_class_get_payload_type_field_by_index(NULL
, &ret_string
,
866 &ret_field_type
, 0) < 0,
867 "bt_ctf_event_class_get_field handles a NULL event class correctly");
868 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, NULL
,
869 &ret_field_type
, 0) == 0,
870 "bt_ctf_event_class_get_field handles a NULL field name correctly");
871 bt_put(ret_field_type
);
872 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
874 "bt_ctf_event_class_get_field handles a NULL field type correctly");
875 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
876 &ret_field_type
, 42) < 0,
877 "bt_ctf_event_class_get_field handles an invalid index correctly");
878 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
879 &ret_field_type
, 0) == 0,
880 "bt_ctf_event_class_get_field returns a field");
881 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
882 "bt_ctf_event_class_get_field returns a correct field type");
883 bt_put(ret_field_type
);
884 ok(!strcmp(ret_string
, "uint_35"),
885 "bt_ctf_event_class_get_field returns a correct field name");
886 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
887 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
888 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
889 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
890 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
891 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
892 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
893 "complex_structure");
894 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
895 "bt_ctf_event_class_get_field_by_name returns a correct field type");
896 bt_put(ret_field_type
);
898 event
= bt_ctf_event_create(event_class
);
899 ok(event
, "Instanciate a complex event");
901 ok(bt_ctf_event_get_class(NULL
) == NULL
,
902 "bt_ctf_event_get_class handles NULL correctly");
903 ret_event_class
= bt_ctf_event_get_class(event
);
904 ok(ret_event_class
== event_class
,
905 "bt_ctf_event_get_class returns the correct event class");
906 bt_put(ret_event_class
);
908 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
909 if (!uint_35_field
) {
910 printf("uint_35_field is NULL\n");
913 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
914 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
915 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
916 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
917 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
918 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
919 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
920 &ret_unsigned_int
) == 0,
921 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
922 ok(ret_unsigned_int
== 0x0DDF00D,
923 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
924 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
925 &ret_signed_int
) < 0,
926 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
927 bt_put(uint_35_field
);
929 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
930 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
931 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
932 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
933 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
934 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
935 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
936 &ret_signed_int
) == 0,
937 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
938 ok(ret_signed_int
== -12345,
939 "bt_ctf_field_signed_integer_get_value returns the correct value");
940 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
941 &ret_unsigned_int
) < 0,
942 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
943 bt_put(int_16_field
);
945 complex_structure_field
= bt_ctf_event_get_payload(event
,
946 "complex_structure");
948 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
949 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
950 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
951 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
952 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
953 complex_structure_field
, 3);
954 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
955 bt_put(inner_structure_field
);
956 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
957 "bt_ctf_field_structure_get_field_by_index returns a correct field");
958 bt_put(ret_field_type
);
960 inner_structure_field
= bt_ctf_field_structure_get_field(
961 complex_structure_field
, "inner_structure");
962 a_string_field
= bt_ctf_field_structure_get_field(
963 complex_structure_field
, "a_string");
964 enum_variant_field
= bt_ctf_field_structure_get_field(
965 complex_structure_field
, "variant_selector");
966 variant_field
= bt_ctf_field_structure_get_field(
967 complex_structure_field
, "variant_value");
968 uint_35_field
= bt_ctf_field_structure_get_field(
969 inner_structure_field
, "seq_len");
970 a_sequence_field
= bt_ctf_field_structure_get_field(
971 inner_structure_field
, "a_sequence");
972 an_array_field
= bt_ctf_field_structure_get_field(
973 inner_structure_field
, "an_array");
975 enum_container_field
= bt_ctf_field_enumeration_get_container(
977 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
978 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
980 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
981 bt_put(int_16_field
);
982 ok(!bt_ctf_field_string_get_value(a_string_field
),
983 "bt_ctf_field_string_get_value returns NULL on an unset field");
984 bt_ctf_field_string_set_value(a_string_field
,
986 ok(!bt_ctf_field_string_get_value(NULL
),
987 "bt_ctf_field_string_get_value correctly handles NULL");
988 ok(bt_ctf_field_string_append(NULL
, "yeah"),
989 "bt_ctf_field_string_append correctly handles a NULL string field");
990 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
991 "bt_ctf_field_string_append correctly handles a NULL string value");
992 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
993 "bt_ctf_field_string_append succeeds");
994 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
995 "bt_ctf_field_string_append_len correctly handles a NULL string field");
996 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
997 "bt_ctf_field_string_append_len correctly handles a NULL string value");
998 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
999 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1000 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1001 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1002 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1003 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1004 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1005 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1007 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1008 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1009 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1010 "bt_ctf_field_string_get_value returns a correct value");
1011 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1012 SEQUENCE_TEST_LENGTH
);
1014 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1015 enum_container_field
) == NULL
,
1016 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1017 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1019 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1020 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1021 variant_type
, enum_variant_field
);
1022 ok(ret_field_type
== int_16_type
,
1023 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1025 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1026 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1027 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1028 uint_35_field
) == 0, "Set a sequence field's length");
1029 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1030 ok(ret_field
== uint_35_field
,
1031 "bt_ctf_field_sequence_get_length returns the correct length field");
1032 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1033 "bt_ctf_field_sequence_get_length properly handles NULL");
1035 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1036 int_16_field
= bt_ctf_field_sequence_get_field(
1037 a_sequence_field
, i
);
1038 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1039 bt_put(int_16_field
);
1042 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1043 int_16_field
= bt_ctf_field_array_get_field(
1045 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1046 bt_put(int_16_field
);
1049 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1050 assert(stream_event_ctx_field
);
1051 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1052 stream_event_ctx_field
, "common_event_context");
1053 BT_PUT(stream_event_ctx_field
);
1054 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1055 BT_PUT(stream_event_ctx_int_field
);
1057 bt_ctf_clock_set_time(clock
, ++current_time
);
1058 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1059 "Append a complex event to a stream");
1062 * Populate the custom packet context field with a dummy value
1063 * otherwise flush will fail.
1065 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1066 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1067 "custom_packet_context_field");
1068 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1070 ok(bt_ctf_stream_flush(stream
) == 0,
1071 "Flush a stream containing a complex event");
1073 bt_put(uint_35_field
);
1074 bt_put(a_string_field
);
1075 bt_put(inner_structure_field
);
1076 bt_put(complex_structure_field
);
1077 bt_put(a_sequence_field
);
1078 bt_put(an_array_field
);
1079 bt_put(enum_variant_field
);
1080 bt_put(enum_container_field
);
1081 bt_put(variant_field
);
1083 bt_put(packet_context_field
);
1084 bt_put(packet_context
);
1085 bt_put(uint_35_type
);
1086 bt_put(int_16_type
);
1087 bt_put(string_type
);
1088 bt_put(sequence_type
);
1090 bt_put(inner_structure_type
);
1091 bt_put(complex_structure_type
);
1092 bt_put(uint_3_type
);
1093 bt_put(enum_variant_type
);
1094 bt_put(variant_type
);
1095 bt_put(ret_field_type
);
1096 bt_put(event_class
);
1101 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1102 struct bt_ctf_field_type
*expected_type
, const char *name
)
1104 struct bt_ctf_field_type
*copy_type
;
1106 copy_type
= bt_ctf_field_get_type(field
);
1107 ok(copy_type
== expected_type
,
1108 "bt_ctf_field_copy does not copy the type (%s)", name
);
1113 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1114 struct bt_ctf_field
*field_b
, const char *name
)
1116 ok(field_a
!= field_b
,
1117 "bt_ctf_field_copy creates different pointers (%s)", name
);
1121 void field_copy_tests()
1123 struct bt_ctf_field_type
*len_type
= NULL
;
1124 struct bt_ctf_field_type
*fp_type
= NULL
;
1125 struct bt_ctf_field_type
*s_type
= NULL
;
1126 struct bt_ctf_field_type
*e_int_type
= NULL
;
1127 struct bt_ctf_field_type
*e_type
= NULL
;
1128 struct bt_ctf_field_type
*v_type
= NULL
;
1129 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1130 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1131 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1132 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1133 struct bt_ctf_field_type
*strct_type
= NULL
;
1134 struct bt_ctf_field
*len
= NULL
;
1135 struct bt_ctf_field
*fp
= NULL
;
1136 struct bt_ctf_field
*s
= NULL
;
1137 struct bt_ctf_field
*e_int
= NULL
;
1138 struct bt_ctf_field
*e
= NULL
;
1139 struct bt_ctf_field
*v
= NULL
;
1140 struct bt_ctf_field
*v_selected
= NULL
;
1141 struct bt_ctf_field
*v_selected_cur
= NULL
;
1142 struct bt_ctf_field
*v_selected_0
= NULL
;
1143 struct bt_ctf_field
*v_selected_1
= NULL
;
1144 struct bt_ctf_field
*v_selected_2
= NULL
;
1145 struct bt_ctf_field
*v_selected_3
= NULL
;
1146 struct bt_ctf_field
*v_selected_4
= NULL
;
1147 struct bt_ctf_field
*v_selected_5
= NULL
;
1148 struct bt_ctf_field
*v_selected_6
= NULL
;
1149 struct bt_ctf_field
*a
= NULL
;
1150 struct bt_ctf_field
*a_0
= NULL
;
1151 struct bt_ctf_field
*a_1
= NULL
;
1152 struct bt_ctf_field
*a_2
= NULL
;
1153 struct bt_ctf_field
*a_3
= NULL
;
1154 struct bt_ctf_field
*a_4
= NULL
;
1155 struct bt_ctf_field
*strct
= NULL
;
1156 struct bt_ctf_field
*len_copy
= NULL
;
1157 struct bt_ctf_field
*fp_copy
= NULL
;
1158 struct bt_ctf_field
*s_copy
= NULL
;
1159 struct bt_ctf_field
*e_int_copy
= NULL
;
1160 struct bt_ctf_field
*e_copy
= NULL
;
1161 struct bt_ctf_field
*v_copy
= NULL
;
1162 struct bt_ctf_field
*v_selected_copy
= NULL
;
1163 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1164 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1165 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1166 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1167 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1168 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1169 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1170 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1171 struct bt_ctf_field
*a_copy
= NULL
;
1172 struct bt_ctf_field
*a_0_copy
= NULL
;
1173 struct bt_ctf_field
*a_1_copy
= NULL
;
1174 struct bt_ctf_field
*a_2_copy
= NULL
;
1175 struct bt_ctf_field
*a_3_copy
= NULL
;
1176 struct bt_ctf_field
*a_4_copy
= NULL
;
1177 struct bt_ctf_field
*strct_copy
= NULL
;
1178 struct bt_ctf_field_type_enumeration_mapping_iterator
*e_iter
= NULL
;
1179 uint64_t uint64_t_val
;
1180 const char *str_val
;
1184 /* create len type */
1185 len_type
= bt_ctf_field_type_integer_create(32);
1188 /* create fp type */
1189 fp_type
= bt_ctf_field_type_floating_point_create();
1193 s_type
= bt_ctf_field_type_string_create();
1196 /* create e_int type */
1197 e_int_type
= bt_ctf_field_type_integer_create(8);
1201 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1203 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1206 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1210 /* create v_label1 type */
1211 v_label1_type
= bt_ctf_field_type_string_create();
1212 assert(v_label1_type
);
1214 /* create v_label1_array type */
1215 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1216 assert(v_label1_array_type
);
1218 /* create v_label2 type */
1219 v_label2_type
= bt_ctf_field_type_integer_create(16);
1220 assert(v_label2_type
);
1222 /* create v_label2_seq type */
1223 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1225 assert(v_label2_seq_type
);
1228 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1230 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1233 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1237 /* create strct type */
1238 strct_type
= bt_ctf_field_type_structure_create();
1240 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1243 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1245 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1247 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1249 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1251 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1252 v_label1_array_type
, "a");
1256 strct
= bt_ctf_field_create(strct_type
);
1260 len
= bt_ctf_field_structure_get_field(strct
, "len");
1264 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1268 s
= bt_ctf_field_structure_get_field(strct
, "s");
1272 e
= bt_ctf_field_structure_get_field(strct
, "e");
1275 /* get e_int (underlying integer) */
1276 e_int
= bt_ctf_field_enumeration_get_container(e
);
1280 v
= bt_ctf_field_structure_get_field(strct
, "v");
1284 a
= bt_ctf_field_structure_get_field(strct
, "a");
1288 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1292 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1296 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1299 /* set e field (LABEL2) */
1300 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1304 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1306 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1307 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1308 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1309 ok(v_selected_cur
== v_selected
,
1310 "bt_ctf_field_variant_get_current_field returns the current field");
1311 bt_put(v_selected_cur
);
1313 /* set selected v field */
1314 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1316 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1317 assert(v_selected_0
);
1318 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1320 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1321 assert(v_selected_1
);
1322 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1324 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1325 assert(v_selected_2
);
1326 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1328 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1329 assert(v_selected_3
);
1330 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1332 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1333 assert(v_selected_4
);
1334 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1336 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1337 assert(v_selected_5
);
1338 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1340 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1341 assert(v_selected_6
);
1342 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1346 a_0
= bt_ctf_field_array_get_field(a
, 0);
1348 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1350 a_1
= bt_ctf_field_array_get_field(a
, 1);
1352 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1354 a_2
= bt_ctf_field_array_get_field(a
, 2);
1356 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1358 a_3
= bt_ctf_field_array_get_field(a
, 3);
1360 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1362 a_4
= bt_ctf_field_array_get_field(a
, 4);
1364 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1367 /* create copy of strct */
1368 ok(!bt_ctf_field_copy(NULL
),
1369 "bt_ctf_field_copy handles NULL correctly");
1370 strct_copy
= bt_ctf_field_copy(strct
);
1372 "bt_ctf_field_copy returns a valid pointer");
1374 /* get all copied fields */
1375 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1377 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1379 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1381 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1383 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1385 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1387 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1388 assert(v_selected_copy
);
1389 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1390 assert(v_selected_0_copy
);
1391 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1392 assert(v_selected_1_copy
);
1393 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1394 assert(v_selected_2_copy
);
1395 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1396 assert(v_selected_3_copy
);
1397 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1398 assert(v_selected_4_copy
);
1399 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1400 assert(v_selected_5_copy
);
1401 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1402 assert(v_selected_6_copy
);
1403 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1404 "sequence field copy is not too large");
1405 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1407 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1409 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1411 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1413 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1415 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1417 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1418 "array field copy is not too large");
1420 /* make sure copied fields are different pointers */
1421 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1422 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1423 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1424 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1425 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1426 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1427 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1428 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1430 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1432 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1434 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1436 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1438 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1440 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1442 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1444 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1445 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1446 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1447 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1448 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1449 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1451 /* make sure copied fields share the same types */
1452 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1453 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1454 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1455 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1456 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1457 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1458 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1460 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1462 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1464 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1466 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1468 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1470 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1472 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1474 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1475 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1476 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1477 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1478 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1479 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1481 /* validate len copy */
1482 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1484 ok(uint64_t_val
== 7,
1485 "bt_ctf_field_copy creates a valid integer field copy");
1487 /* validate fp copy */
1488 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1490 ok(double_val
== 3.14,
1491 "bt_ctf_field_copy creates a valid floating point number field copy");
1493 /* validate s copy */
1494 str_val
= bt_ctf_field_string_get_value(s_copy
);
1495 ok(str_val
&& !strcmp(str_val
, "btbt"),
1496 "bt_ctf_field_copy creates a valid string field copy");
1498 /* validate e_int copy */
1499 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1502 ok(uint64_t_val
== 23,
1503 "bt_ctf_field_copy creates a valid enum's integer field copy");
1505 /* validate e copy */
1506 e_iter
= bt_ctf_field_enumeration_get_mappings(e_copy
);
1507 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter
,
1508 &str_val
, NULL
, NULL
);
1509 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1510 "bt_ctf_field_copy creates a valid enum field copy");
1512 /* validate v_selected copy */
1513 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1514 assert(v_selected_copy_len
);
1515 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1518 ok(uint64_t_val
== 7,
1519 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1520 bt_put(v_selected_copy_len
);
1521 v_selected_copy_len
= NULL
;
1523 /* validate v_selected copy fields */
1524 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1527 ok(uint64_t_val
== 7,
1528 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1529 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1532 ok(uint64_t_val
== 6,
1533 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1534 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1537 ok(uint64_t_val
== 5,
1538 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1539 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1542 ok(uint64_t_val
== 4,
1543 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1544 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1547 ok(uint64_t_val
== 3,
1548 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1549 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1552 ok(uint64_t_val
== 2,
1553 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1554 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1557 ok(uint64_t_val
== 1,
1558 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1560 /* validate a copy fields */
1561 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1562 ok(str_val
&& !strcmp(str_val
, "a_0"),
1563 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1564 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1565 ok(str_val
&& !strcmp(str_val
, "a_1"),
1566 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1567 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1568 ok(str_val
&& !strcmp(str_val
, "a_2"),
1569 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1570 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1571 ok(str_val
&& !strcmp(str_val
, "a_3"),
1572 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1573 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1574 ok(str_val
&& !strcmp(str_val
, "a_4"),
1575 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1577 /* put everything */
1584 bt_put(v_label1_type
);
1585 bt_put(v_label1_array_type
);
1586 bt_put(v_label2_type
);
1587 bt_put(v_label2_seq_type
);
1596 bt_put(v_selected_0
);
1597 bt_put(v_selected_1
);
1598 bt_put(v_selected_2
);
1599 bt_put(v_selected_3
);
1600 bt_put(v_selected_4
);
1601 bt_put(v_selected_5
);
1602 bt_put(v_selected_6
);
1616 bt_put(v_selected_copy
);
1617 bt_put(v_selected_0_copy
);
1618 bt_put(v_selected_1_copy
);
1619 bt_put(v_selected_2_copy
);
1620 bt_put(v_selected_3_copy
);
1621 bt_put(v_selected_4_copy
);
1622 bt_put(v_selected_5_copy
);
1623 bt_put(v_selected_6_copy
);
1635 void type_field_tests()
1637 struct bt_ctf_field
*uint_12
;
1638 struct bt_ctf_field
*int_16
;
1639 struct bt_ctf_field
*string
;
1640 struct bt_ctf_field
*enumeration
;
1641 struct bt_ctf_field_type
*composite_structure_type
;
1642 struct bt_ctf_field_type
*structure_seq_type
;
1643 struct bt_ctf_field_type
*string_type
;
1644 struct bt_ctf_field_type
*sequence_type
;
1645 struct bt_ctf_field_type
*uint_8_type
;
1646 struct bt_ctf_field_type
*int_16_type
;
1647 struct bt_ctf_field_type
*uint_12_type
=
1648 bt_ctf_field_type_integer_create(12);
1649 struct bt_ctf_field_type
*enumeration_type
;
1650 struct bt_ctf_field_type
*returned_type
;
1651 const char *ret_string
;
1653 returned_type
= bt_ctf_field_get_type(NULL
);
1654 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1656 ok(uint_12_type
, "Create an unsigned integer type");
1657 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1658 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1659 "Set integer type's base as binary");
1660 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1661 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1662 "Set integer type's base as decimal");
1663 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1664 BT_CTF_INTEGER_BASE_UNKNOWN
),
1665 "Reject integer type's base set as unknown");
1666 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1667 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1668 "Set integer type's base as octal");
1669 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1670 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1671 "Set integer type's base as hexadecimal");
1672 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1673 "Reject unknown integer base value");
1674 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1675 "Set integer type signedness to signed");
1676 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1677 "Set integer type signedness to unsigned");
1678 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1679 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1680 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1681 "bt_ctf_field_type_integer_get_size returns a correct value");
1682 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1683 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1684 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1685 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1687 ok(bt_ctf_field_type_set_byte_order(NULL
,
1688 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1689 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1690 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1691 (enum bt_ctf_byte_order
) 42) < 0,
1692 "bt_ctf_field_type_set_byte_order rejects invalid values");
1693 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1694 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1695 "Set an integer's byte order to little endian");
1696 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1697 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1698 "Set an integer's byte order to big endian");
1699 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1700 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1701 "bt_ctf_field_type_get_byte_order returns a correct value");
1702 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1703 BT_CTF_BYTE_ORDER_UNKNOWN
,
1704 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1706 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1707 BT_CTF_FIELD_TYPE_ID_UNKNOWN
,
1708 "bt_ctf_field_type_get_type_id handles NULL correctly");
1709 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1710 BT_CTF_FIELD_TYPE_ID_INTEGER
,
1711 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1713 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1714 BT_CTF_INTEGER_BASE_UNKNOWN
,
1715 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1716 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1717 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1718 "bt_ctf_field_type_integer_get_base returns a correct value");
1720 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1721 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1722 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1723 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1724 (enum bt_ctf_string_encoding
) 123) < 0,
1725 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1726 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1727 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1728 "Set integer type encoding to UTF8");
1729 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1730 BT_CTF_STRING_ENCODING_UNKNOWN
,
1731 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1732 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1733 BT_CTF_STRING_ENCODING_UTF8
,
1734 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1736 int_16_type
= bt_ctf_field_type_integer_create(16);
1737 assert(int_16_type
);
1738 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1739 "Set signedness of 16 bit integer to true");
1740 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1741 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1742 uint_8_type
= bt_ctf_field_type_integer_create(8);
1744 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1745 ok(sequence_type
, "Create a sequence of int16_t type");
1746 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1747 BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
1748 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1750 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1751 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1752 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1754 ok(!strcmp(ret_string
, "seq_len"),
1755 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1756 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1757 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1758 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1760 ok(returned_type
== int_16_type
,
1761 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1762 bt_put(returned_type
);
1764 string_type
= bt_ctf_field_type_string_create();
1765 ok(string_type
, "Create a string type");
1766 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1767 BT_CTF_STRING_ENCODING_NONE
),
1768 "Reject invalid \"None\" string encoding");
1769 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1771 "Reject invalid string encoding");
1772 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1773 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1774 "Set string encoding to ASCII");
1776 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1777 BT_CTF_STRING_ENCODING_UNKNOWN
,
1778 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1779 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1780 BT_CTF_STRING_ENCODING_ASCII
,
1781 "bt_ctf_field_type_string_get_encoding returns the correct value");
1783 structure_seq_type
= bt_ctf_field_type_structure_create();
1784 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1785 BT_CTF_FIELD_TYPE_ID_STRUCT
,
1786 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1787 ok(structure_seq_type
, "Create a structure type");
1788 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1789 uint_8_type
, "seq_len") == 0,
1790 "Add a uint8_t type to a structure");
1791 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1792 sequence_type
, "a_sequence") == 0,
1793 "Add a sequence type to a structure");
1795 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1796 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1797 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1798 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1800 ok(bt_ctf_field_type_structure_get_field(NULL
,
1801 &ret_string
, &returned_type
, 1) < 0,
1802 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1803 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1804 NULL
, &returned_type
, 1) == 0,
1805 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1806 bt_put(returned_type
);
1807 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1808 &ret_string
, NULL
, 1) == 0,
1809 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1810 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1811 &ret_string
, &returned_type
, 10) < 0,
1812 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1813 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1814 &ret_string
, &returned_type
, 1) == 0,
1815 "bt_ctf_field_type_structure_get_field returns a field");
1816 ok(!strcmp(ret_string
, "a_sequence"),
1817 "bt_ctf_field_type_structure_get_field returns a correct field name");
1818 ok(returned_type
== sequence_type
,
1819 "bt_ctf_field_type_structure_get_field returns a correct field type");
1820 bt_put(returned_type
);
1822 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1823 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1824 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1825 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1826 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1827 structure_seq_type
, "a_sequence");
1828 ok(returned_type
== sequence_type
,
1829 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1830 bt_put(returned_type
);
1832 composite_structure_type
= bt_ctf_field_type_structure_create();
1833 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1834 string_type
, "a_string") == 0,
1835 "Add a string type to a structure");
1836 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1837 structure_seq_type
, "inner_structure") == 0,
1838 "Add a structure type to a structure");
1840 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1841 NULL
, "a_sequence") == NULL
,
1842 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1843 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1844 structure_seq_type
, NULL
) == NULL
,
1845 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1846 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1847 structure_seq_type
, "a_sequence");
1848 ok(returned_type
== sequence_type
,
1849 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1850 bt_put(returned_type
);
1852 int_16
= bt_ctf_field_create(int_16_type
);
1853 ok(int_16
, "Instanciate a signed 16-bit integer");
1854 uint_12
= bt_ctf_field_create(uint_12_type
);
1855 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1856 returned_type
= bt_ctf_field_get_type(int_16
);
1857 ok(returned_type
== int_16_type
,
1858 "bt_ctf_field_get_type returns the correct type");
1860 /* Can't modify types after instanciating them */
1861 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1862 BT_CTF_INTEGER_BASE_DECIMAL
),
1863 "Check an integer type' base can't be modified after instanciation");
1864 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1865 "Check an integer type's signedness can't be modified after instanciation");
1867 /* Check signed property is checked */
1868 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1869 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1870 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1871 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1873 /* Check overflows are properly tested for */
1874 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1875 "Check -32768 is allowed for a signed 16-bit integer");
1876 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1877 "Check 32767 is allowed for a signed 16-bit integer");
1878 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1879 "Check 32768 is not allowed for a signed 16-bit integer");
1880 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1881 "Check -32769 is not allowed for a signed 16-bit integer");
1882 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1883 "Check -42 is allowed for a signed 16-bit integer");
1885 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1886 "Check 4095 is allowed for an unsigned 12-bit integer");
1887 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1888 "Check 4096 is not allowed for a unsigned 12-bit integer");
1889 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1890 "Check 0 is allowed for an unsigned 12-bit integer");
1892 string
= bt_ctf_field_create(string_type
);
1893 ok(string
, "Instanciate a string field");
1894 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1895 "Set a string's value");
1897 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1898 ok(enumeration_type
,
1899 "Create an enumeration type with an unsigned 12-bit integer as container");
1900 enumeration
= bt_ctf_field_create(enumeration_type
);
1902 "Check enumeration types are validated before instantiation");
1907 bt_put(enumeration
);
1908 bt_put(composite_structure_type
);
1909 bt_put(structure_seq_type
);
1910 bt_put(string_type
);
1911 bt_put(sequence_type
);
1912 bt_put(uint_8_type
);
1913 bt_put(int_16_type
);
1914 bt_put(uint_12_type
);
1915 bt_put(enumeration_type
);
1916 bt_put(returned_type
);
1920 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1921 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1924 * Append enough events to force the underlying packet to be resized.
1925 * Also tests that a new event can be declared after a stream has been
1926 * instantiated and used/flushed.
1930 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1932 struct bt_ctf_field_type
*integer_type
=
1933 bt_ctf_field_type_integer_create(17);
1934 struct bt_ctf_field_type
*string_type
=
1935 bt_ctf_field_type_string_create();
1936 struct bt_ctf_event
*event
= NULL
;
1937 struct bt_ctf_field
*ret_field
= NULL
;
1938 struct bt_ctf_field_type
*ret_field_type
= NULL
;
1939 uint64_t ret_uint64
;
1940 int events_appended
= 0;
1941 struct bt_ctf_field
*packet_context
= NULL
,
1942 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
1943 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
1944 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
1945 struct bt_ctf_field_type
*ep_type
= NULL
;
1947 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1949 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1951 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1952 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1958 * bt_ctf_stream_class_add_event_class() copies the field types
1959 * of event_class, so we retrieve the new ones to create the
1960 * appropriate fields.
1962 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
1964 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1965 ep_type
, "field_1");
1966 assert(ep_field_1_type
);
1967 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1968 ep_type
, "a_string");
1969 assert(ep_a_string_type
);
1971 event
= bt_ctf_event_create(event_class
);
1972 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
1973 ret_field_type
= bt_ctf_field_get_type(ret_field
);
1974 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
1975 "bt_ctf_event_get_payload_by_index returns a correct field");
1976 bt_put(ret_field_type
);
1979 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
1980 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1981 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
1982 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1985 for (i
= 0; i
< packet_resize_test_length
; i
++) {
1986 event
= bt_ctf_event_create(event_class
);
1987 struct bt_ctf_field
*integer
=
1988 bt_ctf_field_create(ep_field_1_type
);
1989 struct bt_ctf_field
*string
=
1990 bt_ctf_field_create(ep_a_string_type
);
1992 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1993 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1994 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1997 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1998 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2002 /* Populate stream event context */
2003 stream_event_context
=
2004 bt_ctf_event_get_stream_event_context(event
);
2005 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2006 "common_event_context");
2007 BT_PUT(stream_event_context
);
2008 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2012 ret
|= bt_ctf_stream_append_event(stream
, event
);
2020 events_appended
= !!(i
== packet_resize_test_length
);
2021 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2022 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2023 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2024 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2025 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2026 ok(ret
== 0 && ret_uint64
== 0,
2027 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2028 bt_ctf_stream_append_discarded_events(stream
, 1000);
2029 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2030 ok(ret
== 0 && ret_uint64
== 1000,
2031 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2034 ok(events_appended
, "Append 100 000 events to a stream");
2037 * Populate the custom packet context field with a dummy value
2038 * otherwise flush will fail.
2040 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2041 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2042 "custom_packet_context_field");
2043 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2045 ok(bt_ctf_stream_flush(stream
) == 0,
2046 "Flush a stream that forces a packet resize");
2047 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2048 ok(ret
== 0 && ret_uint64
== 1000,
2049 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2050 bt_put(integer_type
);
2051 bt_put(string_type
);
2052 bt_put(packet_context
);
2053 bt_put(packet_context_field
);
2054 bt_put(stream_event_context
);
2055 bt_put(event_class
);
2056 bt_put(ep_field_1_type
);
2057 bt_put(ep_a_string_type
);
2062 void test_empty_stream(struct bt_ctf_writer
*writer
)
2065 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2066 struct bt_ctf_stream_class
*stream_class
= NULL
;
2067 struct bt_ctf_stream
*stream
= NULL
;
2069 trace
= bt_ctf_writer_get_trace(writer
);
2071 diag("Failed to get trace from writer");
2076 stream_class
= bt_ctf_stream_class_create("empty_stream");
2077 if (!stream_class
) {
2078 diag("Failed to create stream class");
2083 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2085 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
2088 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2089 "bt_ctf_stream_class_get_trace handles NULL correctly");
2090 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2091 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2093 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2095 diag("Failed to create writer stream");
2100 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2101 ok(ret_trace
== trace
,
2102 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2105 "Created a stream class with default attributes and an empty stream");
2109 bt_put(stream_class
);
2113 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
,
2114 struct bt_ctf_clock
*clock
)
2117 struct bt_ctf_stream_class
*stream_class
= NULL
;
2118 struct bt_ctf_stream
*stream
= NULL
;
2119 struct bt_ctf_field_type
*integer_type
= NULL
,
2120 *sequence_type
= NULL
, *event_header_type
= NULL
;
2121 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2122 *event_header
= NULL
, *packet_header
= NULL
;
2123 struct bt_ctf_event_class
*event_class
= NULL
;
2124 struct bt_ctf_event
*event
= NULL
;
2126 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2127 if (!stream_class
) {
2128 fail("Failed to create stream class");
2132 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2134 fail("Failed to set stream class clock");
2139 * Customize event header to add an "seq_len" integer member
2140 * which will be used as the length of a sequence in an event of this
2143 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2145 if (!event_header_type
) {
2146 fail("Failed to get event header type");
2150 integer_type
= bt_ctf_field_type_integer_create(13);
2151 if (!integer_type
) {
2152 fail("Failed to create length integer type");
2156 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2157 integer_type
, "seq_len");
2159 fail("Failed to add a new field to stream event header");
2163 event_class
= bt_ctf_event_class_create("sequence_event");
2165 fail("Failed to create event class");
2170 * This event's payload will contain a sequence which references
2171 * stream.event.header.seq_len as its length field.
2173 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2174 "stream.event.header.seq_len");
2175 if (!sequence_type
) {
2176 fail("Failed to create a sequence");
2180 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2183 fail("Failed to add a sequence to an event class");
2187 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2189 fail("Failed to add event class to stream class");
2193 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2195 fail("Failed to create stream")
2200 * We have defined a custom packet header field. We have to populate it
2203 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2204 if (!packet_header
) {
2205 fail("Failed to get stream packet header");
2209 integer
= bt_ctf_field_structure_get_field(packet_header
,
2210 "custom_trace_packet_header_field");
2212 fail("Failed to retrieve custom_trace_packet_header_field");
2216 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2218 fail("Failed to set custom_trace_packet_header_field value");
2223 event
= bt_ctf_event_create(event_class
);
2225 fail("Failed to create event");
2229 event_header
= bt_ctf_event_get_header(event
);
2230 if (!event_header
) {
2231 fail("Failed to get event header");
2235 integer
= bt_ctf_field_structure_get_field(event_header
,
2238 fail("Failed to get seq_len field from event header");
2242 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2244 fail("Failed to set seq_len value in event header");
2248 /* Populate both sequence integer fields */
2249 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2251 fail("Failed to retrieve sequence from event");
2255 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2257 fail("Failed to set sequence length");
2262 for (i
= 0; i
< 2; i
++) {
2263 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2265 fail("Failed to retrieve sequence element");
2269 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2271 fail("Failed to set sequence element value");
2279 ret
= bt_ctf_stream_append_event(stream
, event
);
2281 fail("Failed to append event to stream");
2285 ret
= bt_ctf_stream_flush(stream
);
2287 fail("Failed to flush custom_event_header stream");
2291 bt_put(stream_class
);
2292 bt_put(event_class
);
2296 bt_put(event_header
);
2297 bt_put(packet_header
);
2298 bt_put(sequence_type
);
2299 bt_put(integer_type
);
2300 bt_put(event_header_type
);
2304 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
,
2305 struct bt_ctf_clock
*clock
)
2308 struct bt_ctf_stream_class
*stream_class
= NULL
;
2309 struct bt_ctf_stream
*stream
= NULL
,
2311 struct bt_ctf_event_class
*event_class
= NULL
;
2312 struct bt_ctf_event
*event
= NULL
;
2313 struct bt_ctf_field_type
*integer_type
= NULL
;
2314 struct bt_ctf_field
*integer
= NULL
;
2316 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2317 if (!stream_class
) {
2318 diag("Failed to create stream class");
2323 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2325 diag("Failed to set stream class clock");
2329 event_class
= bt_ctf_event_class_create("some_event_class_name");
2330 integer_type
= bt_ctf_field_type_integer_create(32);
2331 if (!integer_type
) {
2332 diag("Failed to create integer field type");
2337 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2340 diag("Failed to add field to event class");
2344 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2347 diag("Failed to add event class to stream class");
2350 event
= bt_ctf_event_create(event_class
);
2352 diag("Failed to create event");
2357 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2359 diag("Failed to get integer field payload from event");
2364 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2366 diag("Failed to set integer field value");
2370 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2372 diag("Failed to create writer stream");
2377 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2378 "bt_ctf_event_get_stream handles NULL correctly");
2379 ok(bt_ctf_event_get_stream(event
) == NULL
,
2380 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2382 ret
= bt_ctf_stream_append_event(stream
, event
);
2384 diag("Failed to append event to stream");
2388 ret_stream
= bt_ctf_event_get_stream(event
);
2389 ok(ret_stream
== stream
,
2390 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2393 "Create an event before instanciating its associated stream");
2396 bt_put(stream_class
);
2397 bt_put(event_class
);
2399 bt_put(integer_type
);
2404 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2406 struct bt_ctf_event_class
*event_class
;
2408 event_class
= bt_ctf_event_class_create("Simple Event");
2409 assert(event_class
);
2410 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
) == 0,
2411 "two event classes with the same name may cohabit within the same stream class");
2412 bt_put(event_class
);
2414 event_class
= bt_ctf_event_class_create("different name, ok");
2415 assert(event_class
);
2416 assert(!bt_ctf_event_class_set_id(event_class
, 13));
2417 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2418 "two event classes with the same ID cannot cohabit within the same stream class");
2419 bt_put(event_class
);
2423 struct bt_ctf_event_class
*create_minimal_event_class(void)
2425 struct bt_ctf_event_class
*ec
= NULL
;
2426 struct bt_ctf_field_type
*int_ft
= NULL
;
2429 int_ft
= bt_ctf_field_type_integer_create(23);
2431 ec
= bt_ctf_event_class_create("minimal");
2433 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2441 void test_create_writer_vs_non_writer_mode(void)
2445 const char *writer_stream_name
= "writer stream instance";
2446 struct bt_ctf_writer
*writer
= NULL
;
2447 struct bt_ctf_trace
*writer_trace
= NULL
;
2448 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2449 struct bt_ctf_stream
*writer_stream
= NULL
;
2450 struct bt_ctf_stream
*writer_stream2
= NULL
;
2451 struct bt_ctf_stream
*packet_stream
= NULL
;
2452 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2453 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2454 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2455 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2456 struct bt_ctf_event_class
*writer_ec
= NULL
;
2457 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2458 struct bt_ctf_event
*event
= NULL
;
2459 struct bt_ctf_event
*event2
= NULL
;
2460 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2461 struct bt_ctf_field
*int_field
= NULL
;
2462 struct bt_ctf_clock
*writer_clock
= NULL
;
2463 struct bt_ctf_clock_class
*non_writer_clock_class
= NULL
;
2464 struct bt_ctf_packet
*packet
= NULL
;
2465 struct bt_ctf_packet
*packet2
= NULL
;
2467 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
2468 if (!bt_mkdtemp(trace_path
)) {
2472 /* Create empty structure field type (event header) */
2473 empty_struct_ft
= bt_ctf_field_type_structure_create();
2474 assert(empty_struct_ft
);
2476 /* Create writer, writer stream class, stream, and clock */
2477 writer
= bt_ctf_writer_create(trace_path
);
2479 writer_clock
= bt_ctf_clock_create("writer_clock");
2480 assert(writer_clock
);
2481 ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
2483 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2485 writer_trace
= bt_ctf_writer_get_trace(writer
);
2486 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2487 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2489 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2492 ret
= bt_ctf_stream_class_set_clock(writer_sc
, writer_clock
);
2494 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2496 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2497 assert(writer_stream
);
2498 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2499 "bt_ctf_stream_get_name() returns the stream's name");
2501 /* Create non-writer trace, stream class, stream, and clock */
2502 non_writer_trace
= bt_ctf_trace_create();
2503 assert(non_writer_trace
);
2504 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2505 assert(non_writer_sc
);
2506 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2509 ret
= bt_ctf_stream_class_set_packet_context_type(non_writer_sc
, NULL
);
2511 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2513 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2514 assert(non_writer_stream
);
2515 non_writer_clock_class
=
2516 bt_ctf_clock_class_create("non_writer_clock_class",
2518 assert(non_writer_clock_class
);
2519 ret
= bt_ctf_trace_add_clock_class(non_writer_trace
,
2520 non_writer_clock_class
);
2523 /* Create event class and event */
2524 writer_ec
= create_minimal_event_class();
2526 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2528 event
= bt_ctf_event_create(writer_ec
);
2530 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2532 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2535 * Verify non-writer stream: it should be impossible to append
2538 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2539 "bt_ctf_stream_append_event() fails with a non-writer stream");
2542 * Verify writer stream: it should be possible to append an
2545 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2546 "bt_ctf_stream_append_event() succeeds with a writer stream");
2549 * It should be possible to create a packet from a non-writer
2550 * stream, but not from a writer stream.
2552 packet
= bt_ctf_packet_create(writer_stream
);
2553 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2554 packet
= bt_ctf_packet_create(non_writer_stream
);
2555 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2556 packet_stream
= bt_ctf_packet_get_stream(packet
);
2557 ok(packet_stream
== non_writer_stream
,
2558 "bt_ctf_packet_get_stream() returns the correct stream");
2561 * It should not be possible to append an event associated to
2562 * a stream to a different stream.
2564 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2565 assert(writer_stream2
);
2566 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2567 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2570 * It should not be possible to set the packet of an event
2571 * associated to a given stream to a packet associated with
2572 * a different stream.
2574 ok(bt_ctf_event_set_packet(event
, packet
),
2575 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2578 * It should be possible to set the packet of a fresh event, as
2579 * long as the originating stream classes are the same.
2581 event2
= bt_ctf_event_create(writer_ec
);
2583 ok(bt_ctf_event_set_packet(event2
, packet
),
2584 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2585 non_writer_ec
= create_minimal_event_class();
2586 assert(non_writer_ec
);
2587 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2590 event2
= bt_ctf_event_create(non_writer_ec
);
2592 ok(!bt_ctf_event_set_packet(event2
, packet
),
2593 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2596 * It should be possible to set a packet created from the same
2597 * stream to an event with an existing packet.
2599 packet2
= bt_ctf_packet_create(non_writer_stream
);
2601 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2602 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2606 * It should not be possible to set a packet created from a
2607 * different stream to an event with an existing packet.
2609 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2610 assert(non_writer_stream2
);
2611 packet2
= bt_ctf_packet_create(non_writer_stream
);
2613 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2614 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2617 bt_put(writer_trace
);
2619 bt_put(writer_stream
);
2620 bt_put(writer_stream2
);
2621 bt_put(non_writer_trace
);
2622 bt_put(non_writer_sc
);
2623 bt_put(non_writer_stream
);
2624 bt_put(non_writer_stream2
);
2625 bt_put(packet_stream
);
2627 bt_put(non_writer_ec
);
2631 bt_put(empty_struct_ft
);
2632 bt_put(writer_clock
);
2633 bt_put(non_writer_clock_class
);
2636 recursive_rmdir(trace_path
);
2641 void test_clock_utils(void)
2644 struct bt_ctf_clock
*clock
= NULL
;
2646 clock
= bt_ctf_clock_create("water");
2648 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2650 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2652 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2654 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2660 void test_set_clock_non_writer_stream_class(void)
2662 struct bt_ctf_clock
*clock
;
2663 struct bt_ctf_trace
*trace
;
2664 struct bt_ctf_stream_class
*sc
;
2667 clock
= bt_ctf_clock_create("the_clock");
2670 trace
= bt_ctf_trace_create();
2673 sc
= bt_ctf_stream_class_create(NULL
);
2676 ret
= bt_ctf_stream_class_set_clock(sc
, clock
);
2679 ret
= bt_ctf_trace_add_stream_class(trace
, sc
);
2681 "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock");
2689 void test_static_trace(void)
2691 struct bt_ctf_trace
*trace
;
2692 struct bt_ctf_stream_class
*stream_class
;
2693 struct bt_ctf_stream_class
*stream_class2
;
2694 struct bt_ctf_stream
*stream
;
2695 struct bt_ctf_clock_class
*clock_class
;
2698 trace
= bt_ctf_trace_create();
2700 stream_class
= bt_ctf_stream_class_create(NULL
);
2701 assert(stream_class
);
2702 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2704 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
2706 stream
= bt_ctf_stream_create(stream_class
, "hello");
2707 ok(stream
, "bt_ctf_stream_create() succeeds with a non-static trace");
2709 ok(!bt_ctf_trace_is_static(trace
),
2710 "bt_ctf_trace_is_static() returns the expected value");
2711 ok(bt_ctf_trace_set_is_static(trace
) == 0,
2712 "bt_ctf_trace_set_is_static() succeeds");
2713 ok(bt_ctf_trace_is_static(trace
),
2714 "bt_ctf_trace_is_static() returns the expected value");
2715 clock_class
= bt_ctf_clock_class_create("yes", 1000000000);
2716 assert(clock_class
);
2717 stream_class2
= bt_ctf_stream_class_create(NULL
);
2718 assert(stream_class2
);
2719 ok(bt_ctf_trace_add_stream_class(trace
, stream_class2
),
2720 "bt_ctf_trace_add_stream_class() fails with a static trace");
2721 ok(bt_ctf_trace_add_clock_class(trace
, clock_class
),
2722 "bt_ctf_trace_add_clock_class() fails with a static trace");
2723 ok(!bt_ctf_stream_create(stream_class
, "hello2"),
2724 "bt_ctf_stream_create() fails with a static trace");
2727 bt_put(stream_class
);
2728 bt_put(stream_class2
);
2729 bt_put(clock_class
);
2733 void trace_is_static_listener(struct bt_ctf_trace
*trace
, void *data
)
2735 *((int *) data
) |= 1;
2739 void trace_listener_removed(struct bt_ctf_trace
*trace
, void *data
)
2741 *((int *) data
) |= 2;
2745 void test_trace_is_static_listener(void)
2747 struct bt_ctf_trace
*trace
;
2758 trace
= bt_ctf_trace_create();
2760 ret
= bt_ctf_trace_add_is_static_listener(NULL
,
2761 trace_is_static_listener
, trace_listener_removed
, &called1
);
2762 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (trace)");
2763 ret
= bt_ctf_trace_add_is_static_listener(trace
, NULL
,
2764 trace_listener_removed
, &called1
);
2765 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (listener)");
2766 listener1_id
= bt_ctf_trace_add_is_static_listener(trace
,
2767 trace_is_static_listener
, trace_listener_removed
, &called1
);
2768 ok(listener1_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (1)");
2769 listener2_id
= bt_ctf_trace_add_is_static_listener(trace
,
2770 trace_is_static_listener
, trace_listener_removed
, &called2
);
2771 ok(listener2_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (2)");
2772 listener3_id
= bt_ctf_trace_add_is_static_listener(trace
,
2773 trace_is_static_listener
, trace_listener_removed
, &called3
);
2774 ok(listener3_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (3)");
2775 ret
= bt_ctf_trace_remove_is_static_listener(NULL
, 0);
2776 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles NULL (trace)");
2777 ret
= bt_ctf_trace_remove_is_static_listener(trace
, -2);
2778 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (negative)");
2779 ret
= bt_ctf_trace_remove_is_static_listener(trace
, 77);
2780 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (non existing)");
2781 ret
= bt_ctf_trace_remove_is_static_listener(trace
, listener2_id
);
2782 ok(ret
== 0, "bt_ctf_trace_remove_is_static_listener() succeeds");
2783 ok(called2
== 2, "bt_ctf_trace_remove_is_static_listener() calls the remove listener");
2784 listener4_id
= bt_ctf_trace_add_is_static_listener(trace
,
2785 trace_is_static_listener
, NULL
, &called4
);
2786 ok(listener4_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (4)");
2787 ok(called1
== 0, "\"trace is static\" listener not called before the trace is made static (1)");
2788 ok(called2
== 2, "\"trace is static\" listener not called before the trace is made static (2)");
2789 ok(called3
== 0, "\"trace is static\" listener not called before the trace is made static (3)");
2790 ok(called4
== 0, "\"trace is static\" listener not called before the trace is made static (4)");
2791 ret
= bt_ctf_trace_set_is_static(trace
);
2793 ret
= bt_ctf_trace_add_is_static_listener(trace
,
2794 trace_is_static_listener
, trace_listener_removed
, &called1
);
2796 "bt_ctf_trace_add_is_static_listener() fails when the trace is static");
2797 ok(called1
== 1, "\"trace is static\" listener called when the trace is made static (1)");
2798 ok(called2
== 2, "\"trace is static\" listener not called when the trace is made static (2)");
2799 ok(called3
== 1, "\"trace is static\" listener called when the trace is made static (3)");
2800 ok(called4
== 1, "\"trace is static\" listener called when the trace is made static (4)");
2806 ok(called1
== 2, "\"trace is static\" listener not called after the trace is put (1)");
2807 ok(called2
== 0, "\"trace is static\" listener not called after the trace is put (2)");
2808 ok(called3
== 2, "\"trace is static\" listener not called after the trace is put (3)");
2809 ok(called4
== 0, "\"trace is static\" listener not called after the trace is put (4)");
2813 void test_trace_uuid(void)
2815 struct bt_ctf_trace
*trace
;
2816 const unsigned char uuid
[] = {
2817 0x35, 0x92, 0x63, 0xab, 0xb4, 0xbe, 0x40, 0xb4,
2818 0xb2, 0x60, 0xd3, 0xf1, 0x3b, 0xb0, 0xd8, 0x59,
2820 const unsigned char *ret_uuid
;
2822 trace
= bt_ctf_trace_create();
2824 ok(!bt_ctf_trace_get_uuid(NULL
),
2825 "bt_ctf_trace_get_uuid() handles NULL");
2826 ok(!bt_ctf_trace_get_uuid(trace
),
2827 "bt_ctf_trace_get_uuid() returns NULL initially");
2828 ok(bt_ctf_trace_set_uuid(NULL
, uuid
),
2829 "bt_ctf_trace_set_uuid() handles NULL (trace)");
2830 ok(bt_ctf_trace_set_uuid(trace
, NULL
),
2831 "bt_ctf_trace_set_uuid() handles NULL (UUID)");
2832 ok(bt_ctf_trace_set_uuid(trace
, uuid
) == 0,
2833 "bt_ctf_trace_set_uuid() succeeds with a valid UUID");
2834 ret_uuid
= bt_ctf_trace_get_uuid(trace
);
2835 ok(ret_uuid
, "bt_ctf_trace_get_uuid() returns a UUID");
2837 ok(memcmp(uuid
, ret_uuid
, 16) == 0,
2838 "bt_ctf_trace_get_uuid() returns the expected UUID");
2843 int main(int argc
, char **argv
)
2845 const char *env_resize_length
;
2847 gchar
*metadata_path
;
2848 const char *clock_name
= "test_clock";
2849 const char *clock_description
= "This is a test clock";
2850 const char *returned_clock_name
;
2851 const char *returned_clock_description
;
2852 const uint64_t frequency
= 1123456789;
2853 const int64_t offset_s
= 13515309;
2854 const int64_t offset
= 1234567;
2855 int64_t get_offset_s
,
2857 const uint64_t precision
= 10;
2858 const int is_absolute
= 0xFF;
2859 char *metadata_string
;
2860 struct bt_ctf_writer
*writer
;
2861 struct bt_utsname name
= {"GNU/Linux", "testhost", "4.4.0-87-generic",
2862 "#110-Ubuntu SMP Tue Jul 18 12:55:35 UTC 2017", "x86_64"};
2863 struct bt_ctf_clock
*clock
, *ret_clock
;
2864 struct bt_ctf_clock_class
*ret_clock_class
;
2865 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2866 struct bt_ctf_stream
*stream1
;
2867 struct bt_ctf_stream
*stream
;
2868 const char *ret_string
;
2869 const unsigned char *ret_uuid
;
2870 unsigned char tmp_uuid
[16] = { 0 };
2871 struct bt_ctf_field_type
*packet_context_type
,
2872 *packet_context_field_type
,
2873 *packet_header_type
,
2874 *packet_header_field_type
,
2876 *stream_event_context_type
,
2878 *event_header_field_type
;
2879 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2880 struct bt_ctf_trace
*trace
;
2882 int64_t ret_int64_t
;
2883 struct bt_value
*obj
;
2886 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
2890 env_resize_length
= getenv("PACKET_RESIZE_TEST_LENGTH");
2891 if (env_resize_length
) {
2892 packet_resize_test_length
=
2893 (unsigned int) atoi(env_resize_length
);
2896 plan_tests(NR_TESTS
);
2898 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
2899 if (!bt_mkdtemp(trace_path
)) {
2903 metadata_path
= g_build_filename(trace_path
, "metadata", NULL
);
2905 writer
= bt_ctf_writer_create(trace_path
);
2906 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2908 ok(!bt_ctf_writer_get_trace(NULL
),
2909 "bt_ctf_writer_get_trace correctly handles NULL");
2910 trace
= bt_ctf_writer_get_trace(writer
);
2911 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
),
2912 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_NATIVE");
2913 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_UNSPECIFIED
),
2914 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_UNSPECIFIED");
2916 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2917 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2918 "Set a trace's byte order to big endian");
2919 ok(bt_ctf_trace_get_native_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2920 "bt_ctf_trace_get_native_byte_order returns a correct endianness");
2922 /* Add environment context to the trace */
2923 ok(bt_ctf_writer_add_environment_field(writer
, "host", name
.nodename
) == 0,
2924 "Add host (%s) environment field to writer instance",
2926 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2928 "bt_ctf_writer_add_environment_field error with NULL writer");
2929 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2931 "bt_ctf_writer_add_environment_field error with NULL field name");
2932 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2934 "bt_ctf_writer_add_environment_field error with NULL field value");
2936 /* Test bt_ctf_trace_set_environment_field with an integer object */
2937 obj
= bt_value_integer_create_init(23);
2939 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2940 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2941 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2942 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2943 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2944 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2945 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2946 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2949 /* Test bt_ctf_trace_set_environment_field with a string object */
2950 obj
= bt_value_string_create_init("the value");
2952 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2953 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2956 /* Test bt_ctf_trace_set_environment_field_integer */
2957 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2959 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2960 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2961 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2962 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2964 "bt_ctf_trace_set_environment_field_integer succeeds");
2966 /* Test bt_ctf_trace_set_environment_field_string */
2967 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2969 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2970 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2971 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2972 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2974 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2975 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2977 "bt_ctf_trace_set_environment_field_string succeeds");
2979 /* Test bt_ctf_trace_get_environment_field_count */
2980 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2981 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2982 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2983 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2985 /* Test bt_ctf_trace_get_environment_field_name */
2986 ok(bt_ctf_trace_get_environment_field_name_by_index(NULL
, 0) == NULL
,
2987 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2988 ok(bt_ctf_trace_get_environment_field_name_by_index(trace
, 5) == NULL
,
2989 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2990 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 0);
2991 ok(ret_string
&& !strcmp(ret_string
, "host"),
2992 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2993 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 1);
2994 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2995 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2996 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 2);
2997 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2998 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2999 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 3);
3000 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
3001 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3002 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 4);
3003 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
3004 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3006 /* Test bt_ctf_trace_get_environment_field_value */
3007 ok(bt_ctf_trace_get_environment_field_value_by_index(NULL
, 0) == NULL
,
3008 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
3009 ok(bt_ctf_trace_get_environment_field_value_by_index(trace
, 5) == NULL
,
3010 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
3011 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 1);
3012 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3013 ok(!ret
&& ret_int64_t
== 23,
3014 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
3016 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 2);
3017 ret
= bt_value_string_get(obj
, &ret_string
);
3018 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
3019 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
3022 /* Test bt_ctf_trace_get_environment_field_value_by_name */
3023 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
3025 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3026 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3027 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3028 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3029 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3030 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3032 ret
= bt_value_string_get(obj
, &ret_string
);
3033 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3034 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3037 /* Test environment field replacement */
3038 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3040 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3041 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3042 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3043 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 3);
3044 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3045 ok(!ret
&& ret_int64_t
== 654321,
3046 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3049 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3050 == 0, "Add sysname (%s) environment field to writer instance",
3052 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3053 name
.nodename
) == 0,
3054 "Add nodename (%s) environment field to writer instance",
3056 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3057 == 0, "Add release (%s) environment field to writer instance",
3059 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3060 == 0, "Add version (%s) environment field to writer instance",
3062 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3063 == 0, "Add machine (%s) environment field to writer istance",
3066 /* Define a clock and add it to the trace */
3067 ok(bt_ctf_clock_create("signed") == NULL
,
3068 "Illegal clock name rejected");
3069 clock
= bt_ctf_clock_create(clock_name
);
3070 ok(clock
, "Clock created sucessfully");
3071 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3072 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3073 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3074 "Returned clock name is valid");
3076 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3077 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3078 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3079 "Clock description set successfully");
3081 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3082 ok(returned_clock_description
,
3083 "bt_ctf_clock_get_description returns a description.");
3084 ok(returned_clock_description
?
3085 !strcmp(returned_clock_description
, clock_description
) : 0,
3086 "Returned clock description is valid");
3088 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3089 "bt_ctf_clock_get_frequency returns the correct default frequency");
3090 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3091 "Set clock frequency");
3092 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3093 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3095 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3096 "bt_ctf_clock_get_offset_s succeeds");
3097 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3098 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3099 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3100 "Set clock offset (seconds)");
3101 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3102 "bt_ctf_clock_get_offset_s succeeds");
3103 ok(get_offset_s
== offset_s
,
3104 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3106 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3107 "bt_ctf_clock_get_offset succeeds");
3108 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3109 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3110 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3111 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3112 "bt_ctf_clock_get_offset succeeds");
3113 ok(get_offset
== offset
,
3114 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3116 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3117 "bt_ctf_clock_get_precision returns the correct default precision");
3118 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3119 "Set clock precision");
3120 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3121 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3123 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3124 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3125 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3126 "Set clock absolute property");
3127 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3128 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3130 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3133 ok(!bt_ctf_clock_get_name(NULL
),
3134 "bt_ctf_clock_get_name correctly handles NULL");
3135 ok(!bt_ctf_clock_get_description(NULL
),
3136 "bt_ctf_clock_get_description correctly handles NULL");
3137 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3138 "bt_ctf_clock_get_frequency correctly handles NULL");
3139 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3140 "bt_ctf_clock_get_precision correctly handles NULL");
3141 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3142 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3143 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3144 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3145 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3146 "bt_ctf_clock_get_offset correctly handles NULL clock");
3147 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3148 "bt_ctf_clock_get_offset correctly handles NULL output");
3149 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3150 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3152 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3153 "bt_ctf_clock_set_description correctly handles NULL clock");
3154 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3155 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3156 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3157 "bt_ctf_clock_get_precision correctly handles NULL clock");
3158 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3159 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3160 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3161 "bt_ctf_clock_set_offset correctly handles NULL clock");
3162 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3163 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3164 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3165 "bt_ctf_clock_set_time correctly handles NULL clock");
3166 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3167 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3168 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3170 "bt_ctf_clock_get_uuid returns a UUID");
3172 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3173 /* Slightly modify UUID */
3174 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3177 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3178 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3179 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3180 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3181 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3182 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3183 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3185 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3186 ok(uuid_match(ret_uuid
, tmp_uuid
),
3187 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3189 /* Define a stream class */
3190 stream_class
= bt_ctf_stream_class_create("test_stream");
3192 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3193 "bt_ctf_stream_class_get_name handles NULL correctly");
3194 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3195 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3196 "bt_ctf_stream_class_get_name returns a correct stream class name");
3198 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3199 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3200 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3201 "bt_ctf_stream_class_get_clock handles NULL correctly");
3203 ok(stream_class
, "Create stream class");
3204 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3205 "Set a stream class' clock");
3206 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3207 ok(ret_clock
== clock
,
3208 "bt_ctf_stream_class_get_clock returns a correct clock");
3211 /* Test the event fields and event types APIs */
3214 /* Test fields copying */
3217 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3218 "bt_ctf_stream_class_get_id returns an error when no id is set");
3219 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3220 "bt_ctf_stream_class_get_id handles NULL correctly");
3221 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3222 "bt_ctf_stream_class_set_id handles NULL correctly");
3223 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3224 "Set an stream class' id");
3225 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3226 "bt_ctf_stream_class_get_id returns the correct value");
3228 /* Validate default event header fields */
3229 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3230 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3231 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3234 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3235 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3236 "Default event header type is a structure");
3237 event_header_field_type
=
3238 bt_ctf_field_type_structure_get_field_type_by_name(
3239 ret_field_type
, "id");
3240 ok(event_header_field_type
,
3241 "Default event header type contains an \"id\" field");
3242 ok(bt_ctf_field_type_get_type_id(
3243 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3244 "Default event header \"id\" field is an integer");
3245 bt_put(event_header_field_type
);
3246 event_header_field_type
=
3247 bt_ctf_field_type_structure_get_field_type_by_name(
3248 ret_field_type
, "timestamp");
3249 ok(event_header_field_type
,
3250 "Default event header type contains a \"timestamp\" field");
3251 ok(bt_ctf_field_type_get_type_id(
3252 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3253 "Default event header \"timestamp\" field is an integer");
3254 bt_put(event_header_field_type
);
3255 bt_put(ret_field_type
);
3257 /* Add a custom trace packet header field */
3258 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3259 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3260 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3261 ok(packet_header_type
,
3262 "bt_ctf_trace_get_packet_header_type returns a packet header");
3263 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3264 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3265 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3266 packet_header_type
, "magic");
3267 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3268 bt_put(ret_field_type
);
3269 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3270 packet_header_type
, "uuid");
3271 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3272 bt_put(ret_field_type
);
3273 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3274 packet_header_type
, "stream_id");
3275 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3276 bt_put(ret_field_type
);
3278 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3279 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3280 packet_header_field_type
, "custom_trace_packet_header_field"),
3281 "Added a custom trace packet header field successfully");
3283 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3284 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3285 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3286 "Set a trace packet_header_type successfully");
3288 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3289 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3291 /* Add a custom field to the stream class' packet context */
3292 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3293 ok(packet_context_type
,
3294 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3295 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3296 "Packet context is a structure");
3298 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3299 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3301 integer_type
= bt_ctf_field_type_integer_create(32);
3302 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3304 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3305 /* Create a "uint5_t" equivalent custom packet context field */
3306 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3308 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3309 packet_context_field_type
, "custom_packet_context_field");
3310 ok(ret
== 0, "Packet context field added successfully");
3312 /* Define a stream event context containing a my_integer field. */
3313 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3314 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3315 stream_event_context_type
= bt_ctf_field_type_structure_create();
3316 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3317 integer_type
, "common_event_context");
3319 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3320 stream_event_context_type
) < 0,
3321 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3322 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3324 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3326 ok(bt_ctf_stream_class_set_event_context_type(
3327 stream_class
, stream_event_context_type
) == 0,
3328 "Set a new stream event context type");
3329 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3331 ok(ret_field_type
== stream_event_context_type
,
3332 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3333 bt_put(ret_field_type
);
3335 /* Instantiate a stream and append events */
3336 ret
= bt_ctf_writer_add_clock(writer
, clock
);
3338 ok(bt_ctf_trace_get_stream_count(trace
) == 0,
3339 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)");
3340 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3341 ok(stream1
, "Instanciate a stream class from writer");
3342 ok(bt_ctf_trace_get_stream_count(trace
) == 1,
3343 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)");
3344 stream
= bt_ctf_trace_get_stream_by_index(trace
, 0);
3345 ok(stream
== stream1
,
3346 "bt_ctf_trace_get_stream_by_index() succeeds and returns the correct value");
3350 * Creating a stream through a writer adds the given stream
3351 * class to the writer's trace, thus registering the stream
3352 * class's clock to the trace.
3354 ok(bt_ctf_trace_get_clock_class_count(NULL
) < 0,
3355 "bt_ctf_trace_get_clock_class_count correctly handles NULL");
3356 ok(bt_ctf_trace_get_clock_class_count(trace
) == 1,
3357 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
3358 ok(!bt_ctf_trace_get_clock_class_by_index(NULL
, 0),
3359 "bt_ctf_trace_get_clock_class correctly handles NULL");
3360 ok(!bt_ctf_trace_get_clock_class_by_index(trace
, 1),
3361 "bt_ctf_trace_get_clock_class correctly handles out of bound accesses");
3362 ret_clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
3363 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3364 bt_ctf_clock_get_name(clock
)) == 0,
3365 "bt_ctf_trace_get_clock_class returns the right clock instance");
3366 bt_put(ret_clock_class
);
3367 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, NULL
),
3368 "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)");
3369 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, clock_name
),
3370 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3371 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, NULL
),
3372 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3373 ret_clock_class
= bt_ctf_trace_get_clock_class_by_name(trace
, clock_name
);
3374 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3375 bt_ctf_clock_get_name(clock
)) == 0,
3376 "bt_ctf_trace_get_clock_class returns the right clock instance");
3377 bt_put(ret_clock_class
);
3378 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, "random"),
3379 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3381 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3382 "bt_ctf_stream_get_class correctly handles NULL");
3383 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3384 ok(ret_stream_class
,
3385 "bt_ctf_stream_get_class returns a stream class");
3386 ok(ret_stream_class
== stream_class
,
3387 "Returned stream class is of the correct type");
3390 * Packet header, packet context, event header, and stream
3391 * event context types were copied for the resolving
3394 BT_PUT(packet_header_type
);
3395 BT_PUT(packet_context_type
);
3396 BT_PUT(stream_event_context_type
);
3397 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3398 assert(packet_header_type
);
3399 packet_context_type
=
3400 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3401 assert(packet_context_type
);
3402 stream_event_context_type
=
3403 bt_ctf_stream_class_get_event_context_type(stream_class
);
3404 assert(stream_event_context_type
);
3407 * Try to modify the packet context type after a stream has been
3410 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3411 packet_header_field_type
, "should_fail");
3413 "Trace packet header type can't be modified once a stream has been instanciated");
3416 * Try to modify the packet context type after a stream has been
3419 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3420 packet_context_field_type
, "should_fail");
3422 "Packet context type can't be modified once a stream has been instanciated");
3425 * Try to modify the stream event context type after a stream has been
3428 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3429 integer_type
, "should_fail");
3431 "Stream event context type can't be modified once a stream has been instanciated");
3433 /* Should fail after instanciating a stream (frozen) */
3434 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3435 "Changes to a stream class that was already instantiated fail");
3437 /* Populate the custom packet header field only once for all tests */
3438 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3439 "bt_ctf_stream_get_packet_header handles NULL correctly");
3440 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3442 "bt_ctf_stream_get_packet_header returns a packet header");
3443 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3444 ok(ret_field_type
== packet_header_type
,
3445 "Stream returns a packet header of the appropriate type");
3446 bt_put(ret_field_type
);
3447 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3448 "custom_trace_packet_header_field");
3449 ok(packet_header_field
,
3450 "Packet header structure contains a custom field with the appropriate name");
3451 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3452 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3453 "Custom packet header field is of the expected type");
3454 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3455 54321), "Set custom packet header value successfully");
3456 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3457 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3458 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3459 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3460 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3461 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3462 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3463 "Successfully set a stream's packet header");
3465 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3466 "Add environment field to writer after stream creation");
3470 test_create_writer_vs_non_writer_mode();
3472 test_set_clock_non_writer_stream_class();
3474 test_instanciate_event_before_stream(writer
, clock
);
3476 append_simple_event(stream_class
, stream1
, clock
);
3478 packet_resize_test(stream_class
, stream1
, clock
);
3480 append_complex_event(stream_class
, stream1
, clock
);
3482 append_existing_event_class(stream_class
);
3484 test_empty_stream(writer
);
3486 test_custom_event_header_stream(writer
, clock
);
3488 test_static_trace();
3490 test_trace_is_static_listener();
3494 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3495 ok(metadata_string
, "Get metadata string");
3497 bt_ctf_writer_flush_metadata(writer
);
3500 bt_put(ret_stream_class
);
3503 bt_put(packet_context_type
);
3504 bt_put(packet_context_field_type
);
3505 bt_put(integer_type
);
3506 bt_put(stream_event_context_type
);
3507 bt_put(ret_field_type
);
3508 bt_put(packet_header_type
);
3509 bt_put(packet_header_field_type
);
3510 bt_put(packet_header
);
3511 bt_put(packet_header_field
);
3513 free(metadata_string
);
3514 bt_put(stream_class
);
3516 validate_trace(argv
[1], trace_path
);
3518 recursive_rmdir(trace_path
);
3520 g_free(metadata_path
);