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>
36 #include <babeltrace/compat/stdlib-internal.h>
38 #include <sys/utsname.h>
39 #include <babeltrace/compat/limits-internal.h>
40 #include <babeltrace/compat/stdio-internal.h>
45 #include <babeltrace/compat/dirent-internal.h>
51 #define METADATA_LINE_SIZE 512
52 #define SEQUENCE_TEST_LENGTH 10
53 #define ARRAY_TEST_LENGTH 5
54 #define PACKET_RESIZE_TEST_LENGTH 100000
56 #define DEFAULT_CLOCK_FREQ 1000000000
57 #define DEFAULT_CLOCK_PRECISION 1
58 #define DEFAULT_CLOCK_OFFSET 0
59 #define DEFAULT_CLOCK_OFFSET_S 0
60 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
61 #define DEFAULT_CLOCK_TIME 0
62 #define DEFAULT_CLOCK_VALUE 0
66 static int64_t current_time
= 42;
68 /* Return 1 if uuids match, zero if different. */
70 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
75 if (!uuid_a
|| !uuid_b
) {
79 for (i
= 0; i
< 16; i
++) {
80 if (uuid_a
[i
] != uuid_b
[i
]) {
91 void validate_trace(char *parser_path
, char *trace_path
)
94 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
95 int babeltrace_output_fd
= -1;
102 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
103 unlink(babeltrace_output_path
);
105 if (babeltrace_output_fd
== -1) {
106 diag("Failed to create a temporary file for trace parsing.");
114 waitpid(pid
, &status
, 0);
115 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
117 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
119 perror("# dup2 babeltrace_output_fd to STDOUT");
123 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
125 perror("# dup2 babeltrace_output_fd to STDERR");
129 execl(parser_path
, "babeltrace", trace_path
, NULL
);
130 perror("# Could not launch the babeltrace process");
134 ok(ret
== 0, "Babeltrace could read the resulting trace");
136 if (ret
&& babeltrace_output_fd
>= 0) {
138 size_t len
= METADATA_LINE_SIZE
;
139 FILE *babeltrace_output_fp
= NULL
;
141 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
142 if (!babeltrace_output_fp
) {
143 perror("fdopen on babeltrace_output_fd");
146 babeltrace_output_fd
= -1;
150 diag("malloc error");
152 rewind(babeltrace_output_fp
);
153 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
159 if (babeltrace_output_fp
) {
160 if (fclose(babeltrace_output_fp
)) {
161 diag("fclose error");
166 if (babeltrace_output_fd
>= 0) {
167 if (close(babeltrace_output_fd
)) {
174 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
175 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
177 /* Create and add a simple event class */
178 struct bt_ctf_event_class
*simple_event_class
=
179 bt_ctf_event_class_create("Simple Event");
180 struct bt_ctf_field_type
*uint_12_type
=
181 bt_ctf_field_type_integer_create(12);
182 struct bt_ctf_field_type
*int_64_type
=
183 bt_ctf_field_type_integer_create(64);
184 struct bt_ctf_field_type
*float_type
=
185 bt_ctf_field_type_floating_point_create();
186 struct bt_ctf_field_type
*enum_type
;
187 struct bt_ctf_field_type
*enum_type_unsigned
=
188 bt_ctf_field_type_enumeration_create(uint_12_type
);
189 struct bt_ctf_field_type
*event_context_type
=
190 bt_ctf_field_type_structure_create();
191 struct bt_ctf_field_type
*event_payload_type
= NULL
;
192 struct bt_ctf_field_type
*returned_type
;
193 struct bt_ctf_event
*simple_event
;
194 struct bt_ctf_field
*integer_field
;
195 struct bt_ctf_field
*float_field
;
196 struct bt_ctf_field
*enum_field
;
197 struct bt_ctf_field
*enum_field_unsigned
;
198 struct bt_ctf_field
*enum_container_field
;
199 const char *mapping_name_test
= "truie";
200 const double double_test_value
= 3.1415;
201 struct bt_ctf_field
*enum_container_field_unsigned
;
202 const char *mapping_name_negative_test
= "negative_value";
203 const char *ret_char
;
205 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
206 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
207 struct bt_ctf_event_class
*ret_event_class
;
208 struct bt_ctf_field
*packet_context
;
209 struct bt_ctf_field
*packet_context_field
;
210 struct bt_ctf_field
*stream_event_context
;
211 struct bt_ctf_field
*stream_event_context_field
;
212 struct bt_ctf_field
*event_context
;
213 struct bt_ctf_field
*event_context_field
;
214 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
215 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
216 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
217 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
220 ok(uint_12_type
, "Create an unsigned integer type");
222 ok(!bt_ctf_field_type_integer_set_signed(int_64_type
, 1),
223 "Set signed 64 bit integer signedness to true");
224 ok(int_64_type
, "Create a signed integer type");
225 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
227 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
228 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
229 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
230 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
231 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
232 bt_put(returned_type
);
234 bt_ctf_field_type_set_alignment(float_type
, 32);
235 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
236 "bt_ctf_field_type_get_alignment handles NULL correctly");
237 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
238 "bt_ctf_field_type_get_alignment returns a correct value");
240 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
241 "Set a floating point type's exponent digit count");
242 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
243 "Set a floating point type's mantissa digit count");
245 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
246 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
247 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
248 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
249 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
250 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
251 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
252 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
254 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
255 mapping_name_negative_test
, -12345, 0) == 0,
256 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
257 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
258 "escaping; \"test\"", 1, 1) == 0,
259 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
260 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
261 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
262 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
263 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
264 "event clock int float", 5, 22) == 0,
265 "Accept enumeration mapping strings containing reserved keywords");
266 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
268 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
269 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
270 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
271 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
272 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
273 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
274 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
276 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(NULL
, -42);
277 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly");
279 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, -4200000);
280 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values");
282 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, 3);
283 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value");
284 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, NULL
, NULL
, NULL
) == 0,
285 "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly");
288 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
289 "enum_field") == 0, "Add signed enumeration field to event");
291 ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL
, 0, &ret_char
,
292 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
293 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly");
294 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, NULL
,
295 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
296 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly");
297 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
298 NULL
, &ret_range_end_int64_t
) == 0,
299 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly");
300 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
301 &ret_range_start_int64_t
, NULL
) == 0,
302 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly");
303 /* Assumes entries are sorted by range_start values. */
304 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 6, &ret_char
,
305 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
306 "bt_ctf_field_type_enumeration_get_mapping_signed returns a value");
307 ok(!strcmp(ret_char
, mapping_name_test
),
308 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name");
309 ok(ret_range_start_int64_t
== 42,
310 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start");
311 ok(ret_range_end_int64_t
== 42,
312 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end");
314 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
315 "escaping; \"test\"", 0, 0) == 0,
316 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
317 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
318 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
319 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
320 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
321 "event clock int float", 5, 22) == 0,
322 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
323 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
325 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
326 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names");
327 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
328 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries");
329 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
330 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
331 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
332 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
334 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
335 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
336 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 6,
337 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
339 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
340 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
341 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
342 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
343 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
344 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
345 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
346 NULL
, &ret_range_end_uint64_t
) == 0,
347 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
348 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
349 &ret_range_start_uint64_t
, NULL
) == 0,
350 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
351 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 4, &ret_char
,
352 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
353 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
354 ok(!strcmp(ret_char
, mapping_name_test
),
355 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
356 ok(ret_range_start_uint64_t
== 42,
357 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
358 ok(ret_range_end_uint64_t
== 42,
359 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
361 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
363 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
366 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
368 /* Set an event context type which will contain a single integer. */
369 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
370 "event_specific_context"),
371 "Add event specific context field");
372 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
373 "bt_ctf_event_class_get_context_type handles NULL correctly");
374 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
375 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
377 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
378 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
379 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
380 "Set an event class' context type successfully");
381 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
382 ok(returned_type
== event_context_type
,
383 "bt_ctf_event_class_get_context_type returns the appropriate type");
384 bt_put(returned_type
);
386 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
387 "Adding simple event class to stream class");
390 * bt_ctf_stream_class_add_event_class() copies the field types
391 * of simple_event_class, so we retrieve the new ones to create
392 * the appropriate fields.
394 BT_PUT(event_context_type
);
395 BT_PUT(event_payload_type
);
396 event_payload_type
= bt_ctf_event_class_get_payload_type(
398 assert(event_payload_type
);
399 event_context_type
= bt_ctf_event_class_get_context_type(
401 assert(event_context_type
);
402 ep_integer_field_type
=
403 bt_ctf_field_type_structure_get_field_type_by_name(
404 event_payload_type
, "integer_field");
405 assert(ep_integer_field_type
);
407 bt_ctf_field_type_structure_get_field_type_by_name(
408 event_payload_type
, "enum_field");
409 assert(ep_enum_field_type
);
410 ep_enum_field_unsigned_type
=
411 bt_ctf_field_type_structure_get_field_type_by_name(
412 event_payload_type
, "enum_field_unsigned");
413 assert(ep_enum_field_unsigned_type
);
415 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
416 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
417 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
418 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
419 ok(bt_ctf_stream_class_get_event_class_by_index(NULL
, 0) == NULL
,
420 "bt_ctf_stream_class_get_event_class handles NULL correctly");
421 ok(bt_ctf_stream_class_get_event_class_by_index(stream_class
, 8724) == NULL
,
422 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
423 ret_event_class
= bt_ctf_stream_class_get_event_class_by_index(stream_class
, 0);
424 ok(ret_event_class
== simple_event_class
,
425 "bt_ctf_stream_class_get_event_class returns the correct event class");
426 bt_put(ret_event_class
);
427 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
428 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
429 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
430 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
432 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
433 ok(ret_event_class
== simple_event_class
,
434 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
435 bt_put(ret_event_class
);
437 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
438 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
439 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
440 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
441 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
442 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
443 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
444 ok(ret_event_class
== simple_event_class
,
445 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
446 bt_put(ret_event_class
);
448 simple_event
= bt_ctf_event_create(simple_event_class
);
450 "Instantiate an event containing a single integer field");
452 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
453 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
454 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
455 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
457 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
458 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
459 "bt_ctf_field_floating_point_get_value fails on an unset float field");
460 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
461 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
462 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
463 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
464 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
465 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
466 "bt_ctf_field_floating_point_get_value returns a double value");
467 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
468 "bt_ctf_field_floating_point_get_value returns a correct value");
470 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
473 iter
= bt_ctf_field_enumeration_get_mappings(NULL
);
474 ok(!iter
, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
475 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
476 ok(!iter
, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
477 enum_container_field
= bt_ctf_field_enumeration_get_container(
479 ok(bt_ctf_field_signed_integer_set_value(
480 enum_container_field
, -42) == 0,
481 "Set signed enumeration container value");
482 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
483 ok(iter
, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
484 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, &ret_char
, NULL
, NULL
);
485 ok(!ret
&& ret_char
, "bt_ctf_field_type_enumeration_mapping_iterator_get_signed return a mapping name");
486 ok(!strcmp(ret_char
, mapping_name_negative_test
),
487 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
488 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
492 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
493 assert(enum_field_unsigned
);
494 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
495 enum_field_unsigned
);
496 ok(bt_ctf_field_unsigned_integer_set_value(
497 enum_container_field_unsigned
, 42) == 0,
498 "Set unsigned enumeration container value");
499 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
500 enum_field_unsigned
);
502 iter
= bt_ctf_field_enumeration_get_mappings(enum_field_unsigned
);
504 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, &ret_char
, NULL
, NULL
);
505 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
506 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container");
508 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
510 /* Populate stream event context */
511 stream_event_context
=
512 bt_ctf_event_get_stream_event_context(simple_event
);
513 assert(stream_event_context
);
514 stream_event_context_field
= bt_ctf_field_structure_get_field(
515 stream_event_context
, "common_event_context");
516 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
518 /* Populate the event's context */
519 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
520 "bt_ctf_event_get_event_context handles NULL correctly");
521 event_context
= bt_ctf_event_get_event_context(simple_event
);
523 "bt_ctf_event_get_event_context returns a field");
524 returned_type
= bt_ctf_field_get_type(event_context
);
525 ok(returned_type
== event_context_type
,
526 "bt_ctf_event_get_event_context returns a field of the appropriate type");
527 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
528 "event_specific_context");
529 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
530 "Successfully set an event context's value");
531 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
532 "bt_ctf_event_set_event_context handles a NULL event correctly");
533 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
534 "bt_ctf_event_set_event_context handles a NULL event context correctly");
535 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
536 "bt_ctf_event_set_event_context rejects a context of the wrong type");
537 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
538 "Set an event context successfully");
540 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
541 "Append simple event to trace stream");
543 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
544 "bt_ctf_stream_get_packet_context handles NULL correctly");
545 packet_context
= bt_ctf_stream_get_packet_context(stream
);
547 "bt_ctf_stream_get_packet_context returns a packet context");
549 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
551 ok(packet_context_field
,
552 "Packet context contains the default packet_size field.");
553 bt_put(packet_context_field
);
554 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
555 "custom_packet_context_field");
556 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
557 "Custom packet context field value successfully set.");
559 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
560 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
561 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
562 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
563 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
564 "Successfully set a stream's packet context");
566 ok(bt_ctf_stream_flush(stream
) == 0,
567 "Flush trace stream with one event");
569 bt_put(simple_event_class
);
570 bt_put(simple_event
);
571 bt_put(uint_12_type
);
575 bt_put(enum_type_unsigned
);
576 bt_put(returned_type
);
577 bt_put(event_context_type
);
578 bt_put(integer_field
);
581 bt_put(enum_field_unsigned
);
582 bt_put(enum_container_field
);
583 bt_put(enum_container_field_unsigned
);
584 bt_put(packet_context
);
585 bt_put(packet_context_field
);
586 bt_put(stream_event_context
);
587 bt_put(stream_event_context_field
);
588 bt_put(event_context
);
589 bt_put(event_context_field
);
590 bt_put(event_payload_type
);
591 bt_put(ep_integer_field_type
);
592 bt_put(ep_enum_field_type
);
593 bt_put(ep_enum_field_unsigned_type
);
598 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
599 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
601 struct event_class_attrs_counts
{
612 struct event_class_attrs_counts
;
613 const char *complex_test_event_string
= "Complex Test Event";
614 const char *test_string_1
= "Test ";
615 const char *test_string_2
= "string ";
616 const char *test_string_3
= "abcdefghi";
617 const char *test_string_4
= "abcd\0efg\0hi";
618 const char *test_string_cat
= "Test string abcdeabcd";
619 struct bt_ctf_field_type
*uint_35_type
=
620 bt_ctf_field_type_integer_create(35);
621 struct bt_ctf_field_type
*int_16_type
=
622 bt_ctf_field_type_integer_create(16);
623 struct bt_ctf_field_type
*uint_3_type
=
624 bt_ctf_field_type_integer_create(3);
625 struct bt_ctf_field_type
*enum_variant_type
=
626 bt_ctf_field_type_enumeration_create(uint_3_type
);
627 struct bt_ctf_field_type
*variant_type
=
628 bt_ctf_field_type_variant_create(enum_variant_type
,
630 struct bt_ctf_field_type
*string_type
=
631 bt_ctf_field_type_string_create();
632 struct bt_ctf_field_type
*sequence_type
;
633 struct bt_ctf_field_type
*array_type
;
634 struct bt_ctf_field_type
*inner_structure_type
=
635 bt_ctf_field_type_structure_create();
636 struct bt_ctf_field_type
*complex_structure_type
=
637 bt_ctf_field_type_structure_create();
638 struct bt_ctf_field_type
*ret_field_type
;
639 struct bt_ctf_event_class
*event_class
;
640 struct bt_ctf_event
*event
;
641 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
642 *inner_structure_field
, *complex_structure_field
,
643 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
644 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
645 *stream_event_ctx_int_field
, *ret_field
;
646 uint64_t ret_unsigned_int
;
647 int64_t ret_signed_int
;
648 const char *ret_string
;
649 struct bt_ctf_stream_class
*ret_stream_class
;
650 struct bt_ctf_event_class
*ret_event_class
;
651 struct bt_ctf_field
*packet_context
, *packet_context_field
;
652 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
653 struct bt_value
*obj
;
655 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
656 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
657 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
658 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
659 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
660 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
661 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
662 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
663 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
664 "Set alignment of signed 16 bit integer to 32");
665 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
666 "Set integer signedness to true");
667 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
668 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
669 "Set signed 16 bit integer base to hexadecimal");
671 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
672 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
675 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
676 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
677 ret_field_type
= bt_ctf_field_type_array_get_element_type(
679 ok(ret_field_type
== int_16_type
,
680 "bt_ctf_field_type_array_get_element_type returns the correct type");
681 bt_put(ret_field_type
);
683 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
684 "bt_ctf_field_type_array_get_length handles NULL correctly");
685 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
686 "bt_ctf_field_type_array_get_length returns the correct length");
688 ok(bt_ctf_field_type_structure_add_field(inner_structure_type
,
689 inner_structure_type
, "yes"), "Cannot add self to structure");
690 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
691 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
692 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
693 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
694 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
695 array_type
, "an_array"), "Add an_array field to inner structure");
697 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
699 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
701 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
702 "UINT35_TYPE", 2, 7);
704 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(NULL
, "INT16_TYPE");
705 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly");
707 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, "INT16_TYPE");
708 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly");
709 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
710 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly");
713 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, NULL
);
714 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly");
716 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL
, 1);
717 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly");
719 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, -42);
720 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly");
721 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) != 0,
722 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly");
725 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, 5);
726 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly");
727 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
728 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly");
731 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
732 "An unknown entry"), "Reject a variant field based on an unknown tag value");
733 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
734 "UINT3_TYPE") == 0, "Add a field to a variant");
735 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
736 "INT16_TYPE"), "Add INT16_TYPE field to variant");
737 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
738 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
740 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
741 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
742 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
743 ok(ret_field_type
== enum_variant_type
,
744 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
745 bt_put(ret_field_type
);
747 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
748 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
749 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
750 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
751 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
752 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
753 "INT16_TYPE") == NULL
,
754 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
755 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
757 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
758 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
759 variant_type
, "INT16_TYPE");
760 ok(ret_field_type
== int_16_type
,
761 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
762 bt_put(ret_field_type
);
764 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
765 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
766 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
767 "bt_ctf_field_type_variant_get_field_count returns the correct count");
769 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
770 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
771 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
772 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
773 bt_put(ret_field_type
);
774 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
775 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
776 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
777 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
778 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
779 "bt_ctf_field_type_variant_get_field returns a field");
780 ok(!strcmp("INT16_TYPE", ret_string
),
781 "bt_ctf_field_type_variant_get_field returns a correct field name");
782 ok(ret_field_type
== int_16_type
,
783 "bt_ctf_field_type_variant_get_field returns a correct field type");
784 bt_put(ret_field_type
);
786 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
787 enum_variant_type
, "variant_selector"),
788 "Add variant_selector field to complex structure");
789 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
790 string_type
, "a_string"), "Add a_string field to complex structure");
791 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
792 variant_type
, "variant_value"),
793 "Add variant_value field to complex structure");
794 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
795 inner_structure_type
, "inner_structure"),
796 "Add inner_structure field to complex structure");
798 ok(bt_ctf_event_class_create("clock") == NULL
,
799 "Reject creation of an event class with an illegal name");
800 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
801 ok(event_class
, "Create an event class");
802 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
803 "Reject addition of a field with an empty name to an event");
804 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
805 "Reject addition of a field with a NULL type to an event");
806 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
808 "Reject addition of a type with an illegal name to an event");
809 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
811 "Add field of type unsigned integer to an event");
812 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
813 "int_16") == 0, "Add field of type signed integer to an event");
814 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
815 "complex_structure") == 0,
816 "Add composite structure to an event");
818 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
819 "bt_ctf_event_class_get_name handles NULL correctly");
820 ret_string
= bt_ctf_event_class_get_name(event_class
);
821 ok(!strcmp(ret_string
, complex_test_event_string
),
822 "bt_ctf_event_class_get_name returns a correct name");
823 ok(bt_ctf_event_class_get_id(event_class
) < 0,
824 "bt_ctf_event_class_get_id returns a negative value when not set");
825 ok(bt_ctf_event_class_get_id(NULL
) < 0,
826 "bt_ctf_event_class_get_id handles NULL correctly");
827 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
828 "bt_ctf_event_class_set_id handles NULL correctly");
829 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
830 "Set an event class' id");
831 ok(bt_ctf_event_class_get_id(event_class
) == 42,
832 "bt_ctf_event_class_get_id returns the correct value");
834 /* Test event class attributes */
835 obj
= bt_value_integer_create_init(15);
837 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
838 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
839 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
840 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
841 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
842 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
843 assert(!bt_value_integer_set(obj
, -3));
844 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
845 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
846 assert(!bt_value_integer_set(obj
, 11));
847 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
848 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
849 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
850 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
851 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
853 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
856 obj
= bt_value_integer_create_init(5);
858 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
859 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
861 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
862 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
863 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
864 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
865 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
866 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
867 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
870 ret
= bt_value_integer_get(obj
, &int64_value
);
871 ok(obj
&& !ret
&& int64_value
== 5,
872 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
875 obj
= bt_value_string_create_init("nu name");
877 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
878 ret_string
= bt_ctf_event_class_get_name(event_class
);
879 ok(!strcmp(ret_string
, "nu name"),
880 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
881 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
882 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
884 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
886 obj
= bt_value_string_create_init("http://kernel.org/");
888 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
891 ok(bt_ctf_event_class_get_attribute_count(NULL
),
892 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
893 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
894 "bt_ctf_event_class_get_attribute_count returns the correct count");
895 ok(!bt_ctf_event_class_get_attribute_name_by_index(NULL
, 0),
896 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
897 ok(!bt_ctf_event_class_get_attribute_name_by_index(event_class
, 4),
898 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
899 ok(!bt_ctf_event_class_get_attribute_value_by_index(NULL
, 0),
900 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
901 ok(!bt_ctf_event_class_get_attribute_value_by_index(event_class
, 4),
902 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
904 memset(&attrs_count
, 0, sizeof(attrs_count
));
906 for (i
= 0; i
< 4; ++i
) {
907 ret_string
= bt_ctf_event_class_get_attribute_name_by_index(
909 obj
= bt_ctf_event_class_get_attribute_value_by_index(
911 assert(ret_string
&& obj
);
913 if (!strcmp(ret_string
, "id")) {
915 ok(bt_value_is_integer(obj
),
916 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
918 } else if (!strcmp(ret_string
, "name")) {
920 ok(bt_value_is_string(obj
),
921 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
923 } else if (!strcmp(ret_string
, "loglevel")) {
924 attrs_count
.loglevel
++;
925 ok(bt_value_is_integer(obj
),
926 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
928 } else if (!strcmp(ret_string
, "model.emf.uri")) {
929 attrs_count
.modelemfuri
++;
930 ok(bt_value_is_string(obj
),
931 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
934 attrs_count
.unknown
++;
940 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
941 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
942 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
943 "event class has one instance of each known attribute");
945 /* Add event class to the stream class */
946 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
947 "Reject addition of NULL event class to a stream class");
948 ok(bt_ctf_stream_class_add_event_class(stream_class
,
949 event_class
) == 0, "Add an event class to stream class");
951 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
952 "bt_ctf_event_class_get_stream_class handles NULL correctly");
953 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
954 ok(ret_stream_class
== stream_class
,
955 "bt_ctf_event_class_get_stream_class returns the correct stream class");
956 bt_put(ret_stream_class
);
958 ok(bt_ctf_event_class_get_payload_type_field_count(NULL
) < 0,
959 "bt_ctf_event_class_get_field_count handles NULL correctly");
960 ok(bt_ctf_event_class_get_payload_type_field_count(event_class
) == 3,
961 "bt_ctf_event_class_get_field_count returns a correct value");
963 ok(bt_ctf_event_class_get_payload_type_field_by_index(NULL
, &ret_string
,
964 &ret_field_type
, 0) < 0,
965 "bt_ctf_event_class_get_field handles a NULL event class correctly");
966 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, NULL
,
967 &ret_field_type
, 0) == 0,
968 "bt_ctf_event_class_get_field handles a NULL field name correctly");
969 bt_put(ret_field_type
);
970 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
972 "bt_ctf_event_class_get_field handles a NULL field type correctly");
973 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
974 &ret_field_type
, 42) < 0,
975 "bt_ctf_event_class_get_field handles an invalid index correctly");
976 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
977 &ret_field_type
, 0) == 0,
978 "bt_ctf_event_class_get_field returns a field");
979 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
980 "bt_ctf_event_class_get_field returns a correct field type");
981 bt_put(ret_field_type
);
982 ok(!strcmp(ret_string
, "uint_35"),
983 "bt_ctf_event_class_get_field returns a correct field name");
984 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
985 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
986 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
987 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
988 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
989 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
990 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
991 "complex_structure");
992 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
993 "bt_ctf_event_class_get_field_by_name returns a correct field type");
994 bt_put(ret_field_type
);
996 event
= bt_ctf_event_create(event_class
);
997 ok(event
, "Instanciate a complex event");
999 ok(bt_ctf_event_get_class(NULL
) == NULL
,
1000 "bt_ctf_event_get_class handles NULL correctly");
1001 ret_event_class
= bt_ctf_event_get_class(event
);
1002 ok(ret_event_class
== event_class
,
1003 "bt_ctf_event_get_class returns the correct event class");
1004 bt_put(ret_event_class
);
1006 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1007 if (!uint_35_field
) {
1008 printf("uint_35_field is NULL\n");
1011 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1012 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1013 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1014 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1015 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1016 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1017 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1018 &ret_unsigned_int
) == 0,
1019 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1020 ok(ret_unsigned_int
== 0x0DDF00D,
1021 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1022 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1023 &ret_signed_int
) < 0,
1024 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1025 bt_put(uint_35_field
);
1027 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1028 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1029 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1030 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1031 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1032 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1033 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1034 &ret_signed_int
) == 0,
1035 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1036 ok(ret_signed_int
== -12345,
1037 "bt_ctf_field_signed_integer_get_value returns the correct value");
1038 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1039 &ret_unsigned_int
) < 0,
1040 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1041 bt_put(int_16_field
);
1043 complex_structure_field
= bt_ctf_event_get_payload(event
,
1044 "complex_structure");
1046 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1047 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1048 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1049 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1050 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1051 complex_structure_field
, 3);
1052 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1053 bt_put(inner_structure_field
);
1054 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1055 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1056 bt_put(ret_field_type
);
1058 inner_structure_field
= bt_ctf_field_structure_get_field(
1059 complex_structure_field
, "inner_structure");
1060 a_string_field
= bt_ctf_field_structure_get_field(
1061 complex_structure_field
, "a_string");
1062 enum_variant_field
= bt_ctf_field_structure_get_field(
1063 complex_structure_field
, "variant_selector");
1064 variant_field
= bt_ctf_field_structure_get_field(
1065 complex_structure_field
, "variant_value");
1066 uint_35_field
= bt_ctf_field_structure_get_field(
1067 inner_structure_field
, "seq_len");
1068 a_sequence_field
= bt_ctf_field_structure_get_field(
1069 inner_structure_field
, "a_sequence");
1070 an_array_field
= bt_ctf_field_structure_get_field(
1071 inner_structure_field
, "an_array");
1073 enum_container_field
= bt_ctf_field_enumeration_get_container(
1074 enum_variant_field
);
1075 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1076 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1077 enum_variant_field
);
1078 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1079 bt_put(int_16_field
);
1080 ok(!bt_ctf_field_string_get_value(a_string_field
),
1081 "bt_ctf_field_string_get_value returns NULL on an unset field");
1082 bt_ctf_field_string_set_value(a_string_field
,
1084 ok(!bt_ctf_field_string_get_value(NULL
),
1085 "bt_ctf_field_string_get_value correctly handles NULL");
1086 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1087 "bt_ctf_field_string_append correctly handles a NULL string field");
1088 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1089 "bt_ctf_field_string_append correctly handles a NULL string value");
1090 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1091 "bt_ctf_field_string_append succeeds");
1092 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1093 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1094 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1095 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1096 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1097 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1098 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1099 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1100 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1101 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1102 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1103 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1105 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1106 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1107 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1108 "bt_ctf_field_string_get_value returns a correct value");
1109 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1110 SEQUENCE_TEST_LENGTH
);
1112 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1113 enum_container_field
) == NULL
,
1114 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1115 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1117 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1118 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1119 variant_type
, enum_variant_field
);
1120 ok(ret_field_type
== int_16_type
,
1121 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1123 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1124 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1125 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1126 uint_35_field
) == 0, "Set a sequence field's length");
1127 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1128 ok(ret_field
== uint_35_field
,
1129 "bt_ctf_field_sequence_get_length returns the correct length field");
1130 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1131 "bt_ctf_field_sequence_get_length properly handles NULL");
1133 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1134 int_16_field
= bt_ctf_field_sequence_get_field(
1135 a_sequence_field
, i
);
1136 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1137 bt_put(int_16_field
);
1140 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1141 int_16_field
= bt_ctf_field_array_get_field(
1143 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1144 bt_put(int_16_field
);
1147 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1148 assert(stream_event_ctx_field
);
1149 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1150 stream_event_ctx_field
, "common_event_context");
1151 BT_PUT(stream_event_ctx_field
);
1152 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1153 BT_PUT(stream_event_ctx_int_field
);
1155 bt_ctf_clock_set_time(clock
, ++current_time
);
1156 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1157 "Append a complex event to a stream");
1160 * Populate the custom packet context field with a dummy value
1161 * otherwise flush will fail.
1163 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1164 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1165 "custom_packet_context_field");
1166 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1168 ok(bt_ctf_stream_flush(stream
) == 0,
1169 "Flush a stream containing a complex event");
1171 bt_put(uint_35_field
);
1172 bt_put(a_string_field
);
1173 bt_put(inner_structure_field
);
1174 bt_put(complex_structure_field
);
1175 bt_put(a_sequence_field
);
1176 bt_put(an_array_field
);
1177 bt_put(enum_variant_field
);
1178 bt_put(enum_container_field
);
1179 bt_put(variant_field
);
1181 bt_put(packet_context_field
);
1182 bt_put(packet_context
);
1183 bt_put(uint_35_type
);
1184 bt_put(int_16_type
);
1185 bt_put(string_type
);
1186 bt_put(sequence_type
);
1188 bt_put(inner_structure_type
);
1189 bt_put(complex_structure_type
);
1190 bt_put(uint_3_type
);
1191 bt_put(enum_variant_type
);
1192 bt_put(variant_type
);
1193 bt_put(ret_field_type
);
1194 bt_put(event_class
);
1199 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1200 struct bt_ctf_field_type
*expected_type
, const char *name
)
1202 struct bt_ctf_field_type
*copy_type
;
1204 copy_type
= bt_ctf_field_get_type(field
);
1205 ok(copy_type
== expected_type
,
1206 "bt_ctf_field_copy does not copy the type (%s)", name
);
1211 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1212 struct bt_ctf_field
*field_b
, const char *name
)
1214 ok(field_a
!= field_b
,
1215 "bt_ctf_field_copy creates different pointers (%s)", name
);
1219 void field_copy_tests()
1221 struct bt_ctf_field_type
*len_type
= NULL
;
1222 struct bt_ctf_field_type
*fp_type
= NULL
;
1223 struct bt_ctf_field_type
*s_type
= NULL
;
1224 struct bt_ctf_field_type
*e_int_type
= NULL
;
1225 struct bt_ctf_field_type
*e_type
= NULL
;
1226 struct bt_ctf_field_type
*v_type
= NULL
;
1227 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1228 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1229 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1230 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1231 struct bt_ctf_field_type
*strct_type
= NULL
;
1232 struct bt_ctf_field
*len
= NULL
;
1233 struct bt_ctf_field
*fp
= NULL
;
1234 struct bt_ctf_field
*s
= NULL
;
1235 struct bt_ctf_field
*e_int
= NULL
;
1236 struct bt_ctf_field
*e
= NULL
;
1237 struct bt_ctf_field
*v
= NULL
;
1238 struct bt_ctf_field
*v_selected
= NULL
;
1239 struct bt_ctf_field
*v_selected_cur
= NULL
;
1240 struct bt_ctf_field
*v_selected_0
= NULL
;
1241 struct bt_ctf_field
*v_selected_1
= NULL
;
1242 struct bt_ctf_field
*v_selected_2
= NULL
;
1243 struct bt_ctf_field
*v_selected_3
= NULL
;
1244 struct bt_ctf_field
*v_selected_4
= NULL
;
1245 struct bt_ctf_field
*v_selected_5
= NULL
;
1246 struct bt_ctf_field
*v_selected_6
= NULL
;
1247 struct bt_ctf_field
*a
= NULL
;
1248 struct bt_ctf_field
*a_0
= NULL
;
1249 struct bt_ctf_field
*a_1
= NULL
;
1250 struct bt_ctf_field
*a_2
= NULL
;
1251 struct bt_ctf_field
*a_3
= NULL
;
1252 struct bt_ctf_field
*a_4
= NULL
;
1253 struct bt_ctf_field
*strct
= NULL
;
1254 struct bt_ctf_field
*len_copy
= NULL
;
1255 struct bt_ctf_field
*fp_copy
= NULL
;
1256 struct bt_ctf_field
*s_copy
= NULL
;
1257 struct bt_ctf_field
*e_int_copy
= NULL
;
1258 struct bt_ctf_field
*e_copy
= NULL
;
1259 struct bt_ctf_field
*v_copy
= NULL
;
1260 struct bt_ctf_field
*v_selected_copy
= NULL
;
1261 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1262 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1263 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1264 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1265 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1266 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1267 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1268 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1269 struct bt_ctf_field
*a_copy
= NULL
;
1270 struct bt_ctf_field
*a_0_copy
= NULL
;
1271 struct bt_ctf_field
*a_1_copy
= NULL
;
1272 struct bt_ctf_field
*a_2_copy
= NULL
;
1273 struct bt_ctf_field
*a_3_copy
= NULL
;
1274 struct bt_ctf_field
*a_4_copy
= NULL
;
1275 struct bt_ctf_field
*strct_copy
= NULL
;
1276 struct bt_ctf_field_type_enumeration_mapping_iterator
*e_iter
= NULL
;
1277 uint64_t uint64_t_val
;
1278 const char *str_val
;
1282 /* create len type */
1283 len_type
= bt_ctf_field_type_integer_create(32);
1286 /* create fp type */
1287 fp_type
= bt_ctf_field_type_floating_point_create();
1291 s_type
= bt_ctf_field_type_string_create();
1294 /* create e_int type */
1295 e_int_type
= bt_ctf_field_type_integer_create(8);
1299 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1301 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1304 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1308 /* create v_label1 type */
1309 v_label1_type
= bt_ctf_field_type_string_create();
1310 assert(v_label1_type
);
1312 /* create v_label1_array type */
1313 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1314 assert(v_label1_array_type
);
1316 /* create v_label2 type */
1317 v_label2_type
= bt_ctf_field_type_integer_create(16);
1318 assert(v_label2_type
);
1320 /* create v_label2_seq type */
1321 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1323 assert(v_label2_seq_type
);
1326 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1328 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1331 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1335 /* create strct type */
1336 strct_type
= bt_ctf_field_type_structure_create();
1338 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1341 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1343 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1345 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1347 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1349 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1350 v_label1_array_type
, "a");
1354 strct
= bt_ctf_field_create(strct_type
);
1358 len
= bt_ctf_field_structure_get_field(strct
, "len");
1362 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1366 s
= bt_ctf_field_structure_get_field(strct
, "s");
1370 e
= bt_ctf_field_structure_get_field(strct
, "e");
1373 /* get e_int (underlying integer) */
1374 e_int
= bt_ctf_field_enumeration_get_container(e
);
1378 v
= bt_ctf_field_structure_get_field(strct
, "v");
1382 a
= bt_ctf_field_structure_get_field(strct
, "a");
1386 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1390 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1394 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1397 /* set e field (LABEL2) */
1398 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1402 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1404 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1405 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1406 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1407 ok(v_selected_cur
== v_selected
,
1408 "bt_ctf_field_variant_get_current_field returns the current field");
1409 bt_put(v_selected_cur
);
1411 /* set selected v field */
1412 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1414 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1415 assert(v_selected_0
);
1416 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1418 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1419 assert(v_selected_1
);
1420 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1422 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1423 assert(v_selected_2
);
1424 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1426 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1427 assert(v_selected_3
);
1428 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1430 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1431 assert(v_selected_4
);
1432 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1434 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1435 assert(v_selected_5
);
1436 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1438 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1439 assert(v_selected_6
);
1440 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1444 a_0
= bt_ctf_field_array_get_field(a
, 0);
1446 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1448 a_1
= bt_ctf_field_array_get_field(a
, 1);
1450 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1452 a_2
= bt_ctf_field_array_get_field(a
, 2);
1454 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1456 a_3
= bt_ctf_field_array_get_field(a
, 3);
1458 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1460 a_4
= bt_ctf_field_array_get_field(a
, 4);
1462 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1465 /* create copy of strct */
1466 ok(!bt_ctf_field_copy(NULL
),
1467 "bt_ctf_field_copy handles NULL correctly");
1468 strct_copy
= bt_ctf_field_copy(strct
);
1470 "bt_ctf_field_copy returns a valid pointer");
1472 /* get all copied fields */
1473 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1475 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1477 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1479 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1481 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1483 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1485 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1486 assert(v_selected_copy
);
1487 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1488 assert(v_selected_0_copy
);
1489 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1490 assert(v_selected_1_copy
);
1491 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1492 assert(v_selected_2_copy
);
1493 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1494 assert(v_selected_3_copy
);
1495 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1496 assert(v_selected_4_copy
);
1497 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1498 assert(v_selected_5_copy
);
1499 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1500 assert(v_selected_6_copy
);
1501 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1502 "sequence field copy is not too large");
1503 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1505 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1507 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1509 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1511 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1513 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1515 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1516 "array field copy is not too large");
1518 /* make sure copied fields are different pointers */
1519 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1520 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1521 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1522 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1523 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1524 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1525 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1526 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1528 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1530 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1532 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1534 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1536 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1538 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1540 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1542 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1543 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1544 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1545 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1546 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1547 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1549 /* make sure copied fields share the same types */
1550 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1551 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1552 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1553 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1554 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1555 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1556 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1558 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1560 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1562 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1564 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1566 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1568 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1570 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1572 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1573 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1574 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1575 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1576 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1577 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1579 /* validate len copy */
1580 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1582 ok(uint64_t_val
== 7,
1583 "bt_ctf_field_copy creates a valid integer field copy");
1585 /* validate fp copy */
1586 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1588 ok(double_val
== 3.14,
1589 "bt_ctf_field_copy creates a valid floating point number field copy");
1591 /* validate s copy */
1592 str_val
= bt_ctf_field_string_get_value(s_copy
);
1593 ok(str_val
&& !strcmp(str_val
, "btbt"),
1594 "bt_ctf_field_copy creates a valid string field copy");
1596 /* validate e_int copy */
1597 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1600 ok(uint64_t_val
== 23,
1601 "bt_ctf_field_copy creates a valid enum's integer field copy");
1603 /* validate e copy */
1604 e_iter
= bt_ctf_field_enumeration_get_mappings(e_copy
);
1605 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter
,
1606 &str_val
, NULL
, NULL
);
1607 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1608 "bt_ctf_field_copy creates a valid enum field copy");
1610 /* validate v_selected copy */
1611 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1612 assert(v_selected_copy_len
);
1613 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1616 ok(uint64_t_val
== 7,
1617 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1618 bt_put(v_selected_copy_len
);
1619 v_selected_copy_len
= NULL
;
1621 /* validate v_selected copy fields */
1622 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1625 ok(uint64_t_val
== 7,
1626 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1627 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1630 ok(uint64_t_val
== 6,
1631 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1632 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1635 ok(uint64_t_val
== 5,
1636 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1637 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1640 ok(uint64_t_val
== 4,
1641 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1642 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1645 ok(uint64_t_val
== 3,
1646 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1647 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1650 ok(uint64_t_val
== 2,
1651 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1652 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1655 ok(uint64_t_val
== 1,
1656 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1658 /* validate a copy fields */
1659 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1660 ok(str_val
&& !strcmp(str_val
, "a_0"),
1661 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1662 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1663 ok(str_val
&& !strcmp(str_val
, "a_1"),
1664 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1665 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1666 ok(str_val
&& !strcmp(str_val
, "a_2"),
1667 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1668 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1669 ok(str_val
&& !strcmp(str_val
, "a_3"),
1670 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1671 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1672 ok(str_val
&& !strcmp(str_val
, "a_4"),
1673 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1675 /* put everything */
1682 bt_put(v_label1_type
);
1683 bt_put(v_label1_array_type
);
1684 bt_put(v_label2_type
);
1685 bt_put(v_label2_seq_type
);
1694 bt_put(v_selected_0
);
1695 bt_put(v_selected_1
);
1696 bt_put(v_selected_2
);
1697 bt_put(v_selected_3
);
1698 bt_put(v_selected_4
);
1699 bt_put(v_selected_5
);
1700 bt_put(v_selected_6
);
1714 bt_put(v_selected_copy
);
1715 bt_put(v_selected_0_copy
);
1716 bt_put(v_selected_1_copy
);
1717 bt_put(v_selected_2_copy
);
1718 bt_put(v_selected_3_copy
);
1719 bt_put(v_selected_4_copy
);
1720 bt_put(v_selected_5_copy
);
1721 bt_put(v_selected_6_copy
);
1733 void type_field_tests()
1735 struct bt_ctf_field
*uint_12
;
1736 struct bt_ctf_field
*int_16
;
1737 struct bt_ctf_field
*string
;
1738 struct bt_ctf_field
*enumeration
;
1739 struct bt_ctf_field_type
*composite_structure_type
;
1740 struct bt_ctf_field_type
*structure_seq_type
;
1741 struct bt_ctf_field_type
*string_type
;
1742 struct bt_ctf_field_type
*sequence_type
;
1743 struct bt_ctf_field_type
*uint_8_type
;
1744 struct bt_ctf_field_type
*int_16_type
;
1745 struct bt_ctf_field_type
*uint_12_type
=
1746 bt_ctf_field_type_integer_create(12);
1747 struct bt_ctf_field_type
*enumeration_type
;
1748 struct bt_ctf_field_type
*returned_type
;
1749 const char *ret_string
;
1751 returned_type
= bt_ctf_field_get_type(NULL
);
1752 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1754 ok(uint_12_type
, "Create an unsigned integer type");
1755 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1756 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1757 "Set integer type's base as binary");
1758 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1759 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1760 "Set integer type's base as decimal");
1761 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1762 BT_CTF_INTEGER_BASE_UNKNOWN
),
1763 "Reject integer type's base set as unknown");
1764 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1765 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1766 "Set integer type's base as octal");
1767 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1768 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1769 "Set integer type's base as hexadecimal");
1770 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1771 "Reject unknown integer base value");
1772 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1773 "Set integer type signedness to signed");
1774 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1775 "Set integer type signedness to unsigned");
1776 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1777 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1778 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1779 "bt_ctf_field_type_integer_get_size returns a correct value");
1780 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1781 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1782 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1783 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1785 ok(bt_ctf_field_type_set_byte_order(NULL
,
1786 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1787 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1788 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1789 (enum bt_ctf_byte_order
) 42) < 0,
1790 "bt_ctf_field_type_set_byte_order rejects invalid values");
1791 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1792 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1793 "Set an integer's byte order to little endian");
1794 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1795 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1796 "Set an integer's byte order to big endian");
1797 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1798 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1799 "bt_ctf_field_type_get_byte_order returns a correct value");
1800 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1801 BT_CTF_BYTE_ORDER_UNKNOWN
,
1802 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1804 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1805 BT_CTF_FIELD_TYPE_ID_UNKNOWN
,
1806 "bt_ctf_field_type_get_type_id handles NULL correctly");
1807 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1808 BT_CTF_FIELD_TYPE_ID_INTEGER
,
1809 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1811 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1812 BT_CTF_INTEGER_BASE_UNKNOWN
,
1813 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1814 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1815 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1816 "bt_ctf_field_type_integer_get_base returns a correct value");
1818 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1819 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1820 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1821 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1822 (enum bt_ctf_string_encoding
) 123) < 0,
1823 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1824 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1825 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1826 "Set integer type encoding to UTF8");
1827 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1828 BT_CTF_STRING_ENCODING_UNKNOWN
,
1829 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1830 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1831 BT_CTF_STRING_ENCODING_UTF8
,
1832 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1834 int_16_type
= bt_ctf_field_type_integer_create(16);
1835 assert(int_16_type
);
1836 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1837 "Set signedness of 16 bit integer to true");
1838 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1839 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1840 uint_8_type
= bt_ctf_field_type_integer_create(8);
1842 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1843 ok(sequence_type
, "Create a sequence of int16_t type");
1844 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1845 BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
1846 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1848 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1849 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1850 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1852 ok(!strcmp(ret_string
, "seq_len"),
1853 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1854 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1855 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1856 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1858 ok(returned_type
== int_16_type
,
1859 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1860 bt_put(returned_type
);
1862 string_type
= bt_ctf_field_type_string_create();
1863 ok(string_type
, "Create a string type");
1864 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1865 BT_CTF_STRING_ENCODING_NONE
),
1866 "Reject invalid \"None\" string encoding");
1867 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1869 "Reject invalid string encoding");
1870 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1871 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1872 "Set string encoding to ASCII");
1874 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1875 BT_CTF_STRING_ENCODING_UNKNOWN
,
1876 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1877 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1878 BT_CTF_STRING_ENCODING_ASCII
,
1879 "bt_ctf_field_type_string_get_encoding returns the correct value");
1881 structure_seq_type
= bt_ctf_field_type_structure_create();
1882 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1883 BT_CTF_FIELD_TYPE_ID_STRUCT
,
1884 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1885 ok(structure_seq_type
, "Create a structure type");
1886 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1887 uint_8_type
, "seq_len") == 0,
1888 "Add a uint8_t type to a structure");
1889 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1890 sequence_type
, "a_sequence") == 0,
1891 "Add a sequence type to a structure");
1893 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1894 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1895 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1896 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1898 ok(bt_ctf_field_type_structure_get_field(NULL
,
1899 &ret_string
, &returned_type
, 1) < 0,
1900 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1901 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1902 NULL
, &returned_type
, 1) == 0,
1903 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1904 bt_put(returned_type
);
1905 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1906 &ret_string
, NULL
, 1) == 0,
1907 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1908 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1909 &ret_string
, &returned_type
, 10) < 0,
1910 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1911 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1912 &ret_string
, &returned_type
, 1) == 0,
1913 "bt_ctf_field_type_structure_get_field returns a field");
1914 ok(!strcmp(ret_string
, "a_sequence"),
1915 "bt_ctf_field_type_structure_get_field returns a correct field name");
1916 ok(returned_type
== sequence_type
,
1917 "bt_ctf_field_type_structure_get_field returns a correct field type");
1918 bt_put(returned_type
);
1920 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1921 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1922 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1923 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1924 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1925 structure_seq_type
, "a_sequence");
1926 ok(returned_type
== sequence_type
,
1927 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1928 bt_put(returned_type
);
1930 composite_structure_type
= bt_ctf_field_type_structure_create();
1931 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1932 string_type
, "a_string") == 0,
1933 "Add a string type to a structure");
1934 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1935 structure_seq_type
, "inner_structure") == 0,
1936 "Add a structure type to a structure");
1938 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1939 NULL
, "a_sequence") == NULL
,
1940 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1941 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1942 structure_seq_type
, NULL
) == NULL
,
1943 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1944 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1945 structure_seq_type
, "a_sequence");
1946 ok(returned_type
== sequence_type
,
1947 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1948 bt_put(returned_type
);
1950 int_16
= bt_ctf_field_create(int_16_type
);
1951 ok(int_16
, "Instanciate a signed 16-bit integer");
1952 uint_12
= bt_ctf_field_create(uint_12_type
);
1953 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1954 returned_type
= bt_ctf_field_get_type(int_16
);
1955 ok(returned_type
== int_16_type
,
1956 "bt_ctf_field_get_type returns the correct type");
1958 /* Can't modify types after instanciating them */
1959 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1960 BT_CTF_INTEGER_BASE_DECIMAL
),
1961 "Check an integer type' base can't be modified after instanciation");
1962 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1963 "Check an integer type's signedness can't be modified after instanciation");
1965 /* Check signed property is checked */
1966 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1967 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1968 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1969 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1971 /* Check overflows are properly tested for */
1972 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1973 "Check -32768 is allowed for a signed 16-bit integer");
1974 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1975 "Check 32767 is allowed for a signed 16-bit integer");
1976 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1977 "Check 32768 is not allowed for a signed 16-bit integer");
1978 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1979 "Check -32769 is not allowed for a signed 16-bit integer");
1980 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1981 "Check -42 is allowed for a signed 16-bit integer");
1983 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1984 "Check 4095 is allowed for an unsigned 12-bit integer");
1985 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1986 "Check 4096 is not allowed for a unsigned 12-bit integer");
1987 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1988 "Check 0 is allowed for an unsigned 12-bit integer");
1990 string
= bt_ctf_field_create(string_type
);
1991 ok(string
, "Instanciate a string field");
1992 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1993 "Set a string's value");
1995 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1996 ok(enumeration_type
,
1997 "Create an enumeration type with an unsigned 12-bit integer as container");
1998 enumeration
= bt_ctf_field_create(enumeration_type
);
2000 "Check enumeration types are validated before instantiation");
2005 bt_put(enumeration
);
2006 bt_put(composite_structure_type
);
2007 bt_put(structure_seq_type
);
2008 bt_put(string_type
);
2009 bt_put(sequence_type
);
2010 bt_put(uint_8_type
);
2011 bt_put(int_16_type
);
2012 bt_put(uint_12_type
);
2013 bt_put(enumeration_type
);
2014 bt_put(returned_type
);
2018 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2019 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2022 * Append enough events to force the underlying packet to be resized.
2023 * Also tests that a new event can be declared after a stream has been
2024 * instantiated and used/flushed.
2028 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2030 struct bt_ctf_field_type
*integer_type
=
2031 bt_ctf_field_type_integer_create(17);
2032 struct bt_ctf_field_type
*string_type
=
2033 bt_ctf_field_type_string_create();
2034 struct bt_ctf_event
*event
= NULL
;
2035 struct bt_ctf_field
*ret_field
= NULL
;
2036 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2037 uint64_t ret_uint64
;
2038 int events_appended
= 0;
2039 struct bt_ctf_field
*packet_context
= NULL
,
2040 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
2041 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2042 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2043 struct bt_ctf_field_type
*ep_type
= NULL
;
2045 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2047 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2049 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2050 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2056 * bt_ctf_stream_class_add_event_class() copies the field types
2057 * of event_class, so we retrieve the new ones to create the
2058 * appropriate fields.
2060 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2062 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2063 ep_type
, "field_1");
2064 assert(ep_field_1_type
);
2065 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2066 ep_type
, "a_string");
2067 assert(ep_a_string_type
);
2069 event
= bt_ctf_event_create(event_class
);
2070 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2071 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2072 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2073 "bt_ctf_event_get_payload_by_index returns a correct field");
2074 bt_put(ret_field_type
);
2077 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2078 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2079 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2080 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2083 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2084 event
= bt_ctf_event_create(event_class
);
2085 struct bt_ctf_field
*integer
=
2086 bt_ctf_field_create(ep_field_1_type
);
2087 struct bt_ctf_field
*string
=
2088 bt_ctf_field_create(ep_a_string_type
);
2090 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2091 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2092 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2095 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2096 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2100 /* Populate stream event context */
2101 stream_event_context
=
2102 bt_ctf_event_get_stream_event_context(event
);
2103 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2104 "common_event_context");
2105 BT_PUT(stream_event_context
);
2106 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2110 ret
|= bt_ctf_stream_append_event(stream
, event
);
2118 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2119 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2120 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2121 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2122 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2123 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2124 ok(ret
== 0 && ret_uint64
== 0,
2125 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2126 bt_ctf_stream_append_discarded_events(stream
, 1000);
2127 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2128 ok(ret
== 0 && ret_uint64
== 1000,
2129 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2132 ok(events_appended
, "Append 100 000 events to a stream");
2135 * Populate the custom packet context field with a dummy value
2136 * otherwise flush will fail.
2138 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2139 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2140 "custom_packet_context_field");
2141 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2143 ok(bt_ctf_stream_flush(stream
) == 0,
2144 "Flush a stream that forces a packet resize");
2145 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2146 ok(ret
== 0 && ret_uint64
== 1000,
2147 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2148 bt_put(integer_type
);
2149 bt_put(string_type
);
2150 bt_put(packet_context
);
2151 bt_put(packet_context_field
);
2152 bt_put(stream_event_context
);
2153 bt_put(event_class
);
2154 bt_put(ep_field_1_type
);
2155 bt_put(ep_a_string_type
);
2160 void test_empty_stream(struct bt_ctf_writer
*writer
)
2163 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2164 struct bt_ctf_stream_class
*stream_class
= NULL
;
2165 struct bt_ctf_stream
*stream
= NULL
;
2167 trace
= bt_ctf_writer_get_trace(writer
);
2169 diag("Failed to get trace from writer");
2174 stream_class
= bt_ctf_stream_class_create("empty_stream");
2175 if (!stream_class
) {
2176 diag("Failed to create stream class");
2181 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2182 "bt_ctf_stream_class_get_trace handles NULL correctly");
2183 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2184 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2186 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2188 diag("Failed to create writer stream");
2193 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2194 ok(ret_trace
== trace
,
2195 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2198 "Created a stream class with default attributes and an empty stream");
2202 bt_put(stream_class
);
2206 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
,
2207 struct bt_ctf_clock
*clock
)
2210 struct bt_ctf_stream_class
*stream_class
= NULL
;
2211 struct bt_ctf_stream
*stream
= NULL
;
2212 struct bt_ctf_field_type
*integer_type
= NULL
,
2213 *sequence_type
= NULL
, *event_header_type
= NULL
;
2214 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2215 *event_header
= NULL
, *packet_header
= NULL
;
2216 struct bt_ctf_event_class
*event_class
= NULL
;
2217 struct bt_ctf_event
*event
= NULL
;
2219 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2220 if (!stream_class
) {
2221 fail("Failed to create stream class");
2225 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2227 fail("Failed to set stream class clock");
2232 * Customize event header to add an "seq_len" integer member
2233 * which will be used as the length of a sequence in an event of this
2236 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2238 if (!event_header_type
) {
2239 fail("Failed to get event header type");
2243 integer_type
= bt_ctf_field_type_integer_create(13);
2244 if (!integer_type
) {
2245 fail("Failed to create length integer type");
2249 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2250 integer_type
, "seq_len");
2252 fail("Failed to add a new field to stream event header");
2256 event_class
= bt_ctf_event_class_create("sequence_event");
2258 fail("Failed to create event class");
2263 * This event's payload will contain a sequence which references
2264 * stream.event.header.seq_len as its length field.
2266 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2267 "stream.event.header.seq_len");
2268 if (!sequence_type
) {
2269 fail("Failed to create a sequence");
2273 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2276 fail("Failed to add a sequence to an event class");
2280 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2282 fail("Failed to add event class to stream class");
2286 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2288 fail("Failed to create stream")
2293 * We have defined a custom packet header field. We have to populate it
2296 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2297 if (!packet_header
) {
2298 fail("Failed to get stream packet header");
2302 integer
= bt_ctf_field_structure_get_field(packet_header
,
2303 "custom_trace_packet_header_field");
2305 fail("Failed to retrieve custom_trace_packet_header_field");
2309 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2311 fail("Failed to set custom_trace_packet_header_field value");
2316 event
= bt_ctf_event_create(event_class
);
2318 fail("Failed to create event");
2322 event_header
= bt_ctf_event_get_header(event
);
2323 if (!event_header
) {
2324 fail("Failed to get event header");
2328 integer
= bt_ctf_field_structure_get_field(event_header
,
2331 fail("Failed to get seq_len field from event header");
2335 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2337 fail("Failed to set seq_len value in event header");
2341 /* Populate both sequence integer fields */
2342 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2344 fail("Failed to retrieve sequence from event");
2348 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2350 fail("Failed to set sequence length");
2355 for (i
= 0; i
< 2; i
++) {
2356 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2358 fail("Failed to retrieve sequence element");
2362 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2364 fail("Failed to set sequence element value");
2372 ret
= bt_ctf_stream_append_event(stream
, event
);
2374 fail("Failed to append event to stream");
2378 ret
= bt_ctf_stream_flush(stream
);
2380 fail("Failed to flush custom_event_header stream");
2384 bt_put(stream_class
);
2385 bt_put(event_class
);
2389 bt_put(event_header
);
2390 bt_put(packet_header
);
2391 bt_put(sequence_type
);
2392 bt_put(integer_type
);
2393 bt_put(event_header_type
);
2397 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
,
2398 struct bt_ctf_clock
*clock
)
2401 struct bt_ctf_stream_class
*stream_class
= NULL
;
2402 struct bt_ctf_stream
*stream
= NULL
,
2404 struct bt_ctf_event_class
*event_class
= NULL
;
2405 struct bt_ctf_event
*event
= NULL
;
2406 struct bt_ctf_field_type
*integer_type
= NULL
;
2407 struct bt_ctf_field
*integer
= NULL
;
2409 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2410 if (!stream_class
) {
2411 diag("Failed to create stream class");
2416 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2418 diag("Failed to set stream class clock");
2422 event_class
= bt_ctf_event_class_create("some_event_class_name");
2423 integer_type
= bt_ctf_field_type_integer_create(32);
2424 if (!integer_type
) {
2425 diag("Failed to create integer field type");
2430 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2433 diag("Failed to add field to event class");
2437 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2440 diag("Failed to add event class to stream class");
2443 event
= bt_ctf_event_create(event_class
);
2445 diag("Failed to create event");
2450 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2452 diag("Failed to get integer field payload from event");
2457 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2459 diag("Failed to set integer field value");
2463 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2465 diag("Failed to create writer stream");
2470 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2471 "bt_ctf_event_get_stream handles NULL correctly");
2472 ok(bt_ctf_event_get_stream(event
) == NULL
,
2473 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2475 ret
= bt_ctf_stream_append_event(stream
, event
);
2477 diag("Failed to append event to stream");
2481 ret_stream
= bt_ctf_event_get_stream(event
);
2482 ok(ret_stream
== stream
,
2483 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2486 "Create an event before instanciating its associated stream");
2489 bt_put(stream_class
);
2490 bt_put(event_class
);
2492 bt_put(integer_type
);
2497 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2499 struct bt_ctf_event_class
*event_class
;
2501 event_class
= bt_ctf_event_class_create("Simple Event");
2502 assert(event_class
);
2503 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2504 "two event classes with the same name cannot cohabit within the same stream class");
2505 bt_put(event_class
);
2507 event_class
= bt_ctf_event_class_create("different name, ok");
2508 assert(event_class
);
2509 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2510 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2511 "two event classes with the same ID cannot cohabit within the same stream class");
2512 bt_put(event_class
);
2516 struct bt_ctf_event_class
*create_minimal_event_class(void)
2518 struct bt_ctf_event_class
*ec
= NULL
;
2519 struct bt_ctf_field_type
*int_ft
= NULL
;
2522 int_ft
= bt_ctf_field_type_integer_create(23);
2524 ec
= bt_ctf_event_class_create("minimal");
2526 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2534 void test_create_writer_vs_non_writer_mode(void)
2537 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2538 const char *writer_stream_name
= "writer stream instance";
2539 struct bt_ctf_writer
*writer
= NULL
;
2540 struct bt_ctf_trace
*writer_trace
= NULL
;
2541 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2542 struct bt_ctf_stream
*writer_stream
= NULL
;
2543 struct bt_ctf_stream
*writer_stream2
= NULL
;
2544 struct bt_ctf_stream
*packet_stream
= NULL
;
2545 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2546 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2547 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2548 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2549 struct bt_ctf_event_class
*writer_ec
= NULL
;
2550 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2551 struct bt_ctf_event
*event
= NULL
;
2552 struct bt_ctf_event
*event2
= NULL
;
2553 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2554 struct bt_ctf_field
*int_field
= NULL
;
2555 struct bt_ctf_clock
*writer_clock
= NULL
;
2556 struct bt_ctf_clock_class
*non_writer_clock_class
= NULL
;
2557 struct bt_ctf_packet
*packet
= NULL
;
2558 struct bt_ctf_packet
*packet2
= NULL
;
2560 if (!bt_mkdtemp(trace_path
)) {
2564 /* Create empty structure field type (event header) */
2565 empty_struct_ft
= bt_ctf_field_type_structure_create();
2566 assert(empty_struct_ft
);
2568 /* Create writer, writer stream class, stream, and clock */
2569 writer
= bt_ctf_writer_create(trace_path
);
2571 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2573 writer_trace
= bt_ctf_writer_get_trace(writer
);
2574 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2575 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2577 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2580 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2582 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2583 assert(writer_stream
);
2584 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2585 "bt_ctf_stream_get_name() returns the stream's name");
2586 writer_clock
= bt_ctf_clock_create("writer_clock");
2587 assert(writer_clock
);
2588 ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
2591 /* Create non-writer trace, stream class, stream, and clock */
2592 non_writer_trace
= bt_ctf_trace_create();
2593 assert(non_writer_trace
);
2594 ret
= bt_ctf_trace_set_native_byte_order(non_writer_trace
,
2595 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2597 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2598 assert(non_writer_sc
);
2599 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2602 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2604 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2605 assert(non_writer_stream
);
2606 non_writer_clock_class
=
2607 bt_ctf_clock_class_create("non_writer_clock_class");
2608 assert(non_writer_clock_class
);
2609 ret
= bt_ctf_trace_add_clock_class(non_writer_trace
,
2610 non_writer_clock_class
);
2613 /* Create event class and event */
2614 writer_ec
= create_minimal_event_class();
2616 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2618 event
= bt_ctf_event_create(writer_ec
);
2620 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2622 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2625 * Verify non-writer stream: it should be impossible to append
2628 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2629 "bt_ctf_stream_append_event() fails with a non-writer stream");
2632 * Verify writer stream: it should be possible to append an
2635 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2636 "bt_ctf_stream_append_event() succeeds with a writer stream");
2639 * It should be possible to create a packet from a non-writer
2640 * stream, but not from a writer stream.
2642 packet
= bt_ctf_packet_create(writer_stream
);
2643 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2644 packet
= bt_ctf_packet_create(non_writer_stream
);
2645 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2646 packet_stream
= bt_ctf_packet_get_stream(packet
);
2647 ok(packet_stream
== non_writer_stream
,
2648 "bt_ctf_packet_get_stream() returns the correct stream");
2651 * It should not be possible to append an event associated to
2652 * a stream to a different stream.
2654 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2655 assert(writer_stream2
);
2656 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2657 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2660 * It should not be possible to set the packet of an event
2661 * associated to a given stream to a packet associated with
2662 * a different stream.
2664 ok(bt_ctf_event_set_packet(event
, packet
),
2665 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2668 * It should be possible to set the packet of a fresh event, as
2669 * long as the originating stream classes are the same.
2671 event2
= bt_ctf_event_create(writer_ec
);
2673 ok(bt_ctf_event_set_packet(event2
, packet
),
2674 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2675 non_writer_ec
= create_minimal_event_class();
2676 assert(non_writer_ec
);
2677 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2680 event2
= bt_ctf_event_create(non_writer_ec
);
2682 ok(!bt_ctf_event_set_packet(event2
, packet
),
2683 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2686 * It should be possible to set a packet created from the same
2687 * stream to an event with an existing packet.
2689 packet2
= bt_ctf_packet_create(non_writer_stream
);
2691 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2692 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2696 * It should not be possible to set a packet created from a
2697 * different stream to an event with an existing packet.
2699 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2700 assert(non_writer_stream2
);
2701 packet2
= bt_ctf_packet_create(non_writer_stream
);
2703 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2704 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2707 bt_put(writer_trace
);
2709 bt_put(writer_stream
);
2710 bt_put(writer_stream2
);
2711 bt_put(non_writer_trace
);
2712 bt_put(non_writer_sc
);
2713 bt_put(non_writer_stream
);
2714 bt_put(non_writer_stream2
);
2715 bt_put(packet_stream
);
2717 bt_put(non_writer_ec
);
2721 bt_put(empty_struct_ft
);
2722 bt_put(writer_clock
);
2723 bt_put(non_writer_clock_class
);
2726 recursive_rmdir(trace_path
);
2730 void test_clock_utils(void)
2733 struct bt_ctf_clock
*clock
= NULL
;
2735 clock
= bt_ctf_clock_create("water");
2737 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2739 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2741 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2743 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2749 void test_set_clock_non_writer_stream_class(void)
2751 struct bt_ctf_clock
*clock
;
2752 struct bt_ctf_trace
*trace
;
2753 struct bt_ctf_stream_class
*sc
;
2756 clock
= bt_ctf_clock_create("the_clock");
2759 trace
= bt_ctf_trace_create();
2762 sc
= bt_ctf_stream_class_create(NULL
);
2765 ret
= bt_ctf_stream_class_set_clock(sc
, clock
);
2768 ret
= bt_ctf_trace_add_stream_class(trace
, sc
);
2770 "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock");
2778 void test_static_trace(void)
2780 struct bt_ctf_trace
*trace
;
2781 struct bt_ctf_stream_class
*stream_class
;
2782 struct bt_ctf_stream_class
*stream_class2
;
2783 struct bt_ctf_stream
*stream
;
2784 struct bt_ctf_clock_class
*clock_class
;
2787 trace
= bt_ctf_trace_create();
2789 ret
= bt_ctf_trace_set_native_byte_order(trace
,
2790 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2792 stream_class
= bt_ctf_stream_class_create(NULL
);
2793 assert(stream_class
);
2794 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
2796 stream
= bt_ctf_stream_create(stream_class
, "hello");
2797 ok(stream
, "bt_ctf_stream_create() succeeds with a non-static trace");
2799 ok(!bt_ctf_trace_is_static(trace
),
2800 "bt_ctf_trace_is_static() returns the expected value");
2801 ok(bt_ctf_trace_set_is_static(trace
) == 0,
2802 "bt_ctf_trace_set_is_static() succeeds");
2803 ok(bt_ctf_trace_is_static(trace
),
2804 "bt_ctf_trace_is_static() returns the expected value");
2805 clock_class
= bt_ctf_clock_class_create("yes");
2806 assert(clock_class
);
2807 stream_class2
= bt_ctf_stream_class_create(NULL
);
2808 assert(stream_class2
);
2809 ok(bt_ctf_trace_add_stream_class(trace
, stream_class2
),
2810 "bt_ctf_trace_add_stream_class() fails with a static trace");
2811 ok(bt_ctf_trace_add_clock_class(trace
, clock_class
),
2812 "bt_ctf_trace_add_clock_class() fails with a static trace");
2813 ok(!bt_ctf_stream_create(stream_class
, "hello2"),
2814 "bt_ctf_stream_create() fails with a static trace");
2817 bt_put(stream_class
);
2818 bt_put(stream_class2
);
2819 bt_put(clock_class
);
2822 int main(int argc
, char **argv
)
2824 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2825 char metadata_path
[sizeof(trace_path
) + 9];
2826 const char *clock_name
= "test_clock";
2827 const char *clock_description
= "This is a test clock";
2828 const char *returned_clock_name
;
2829 const char *returned_clock_description
;
2830 const uint64_t frequency
= 1123456789;
2831 const int64_t offset_s
= 1351530929945824323;
2832 const int64_t offset
= 1234567;
2833 int64_t get_offset_s
,
2835 const uint64_t precision
= 10;
2836 const int is_absolute
= 0xFF;
2837 char *metadata_string
;
2838 struct bt_ctf_writer
*writer
;
2839 struct utsname name
;
2840 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2841 struct bt_ctf_clock
*clock
, *ret_clock
;
2842 struct bt_ctf_clock_class
*ret_clock_class
;
2843 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2844 struct bt_ctf_stream
*stream1
;
2845 struct bt_ctf_stream
*stream
;
2846 const char *ret_string
;
2847 const unsigned char *ret_uuid
;
2848 unsigned char tmp_uuid
[16] = { 0 };
2849 struct bt_ctf_field_type
*packet_context_type
,
2850 *packet_context_field_type
,
2851 *packet_header_type
,
2852 *packet_header_field_type
,
2854 *stream_event_context_type
,
2856 *event_header_field_type
;
2857 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2858 struct bt_ctf_trace
*trace
;
2860 int64_t ret_int64_t
;
2861 struct bt_value
*obj
;
2864 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
2868 plan_tests(NR_TESTS
);
2870 if (!bt_mkdtemp(trace_path
)) {
2874 strcpy(metadata_path
, trace_path
);
2875 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2877 writer
= bt_ctf_writer_create(trace_path
);
2878 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2880 ok(!bt_ctf_writer_get_trace(NULL
),
2881 "bt_ctf_writer_get_trace correctly handles NULL");
2882 trace
= bt_ctf_writer_get_trace(writer
);
2884 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2885 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2886 "Set a trace's byte order to big endian");
2887 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2888 "bt_ctf_trace_get_byte_order returns a correct endianness");
2890 /* Add environment context to the trace */
2891 ret
= gethostname(hostname
, sizeof(hostname
));
2895 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2896 "Add host (%s) environment field to writer instance",
2898 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2900 "bt_ctf_writer_add_environment_field error with NULL writer");
2901 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2903 "bt_ctf_writer_add_environment_field error with NULL field name");
2904 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2906 "bt_ctf_writer_add_environment_field error with NULL field value");
2908 /* Test bt_ctf_trace_set_environment_field with an integer object */
2909 obj
= bt_value_integer_create_init(23);
2911 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2912 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2913 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2914 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2915 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2916 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2917 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2918 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2921 /* Test bt_ctf_trace_set_environment_field with a string object */
2922 obj
= bt_value_string_create_init("the value");
2924 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2925 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2928 /* Test bt_ctf_trace_set_environment_field_integer */
2929 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2931 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2932 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2933 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2934 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2936 "bt_ctf_trace_set_environment_field_integer succeeds");
2938 /* Test bt_ctf_trace_set_environment_field_string */
2939 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2941 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2942 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2943 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2944 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2946 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2947 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2949 "bt_ctf_trace_set_environment_field_string succeeds");
2951 /* Test bt_ctf_trace_get_environment_field_count */
2952 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2953 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2954 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2955 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2957 /* Test bt_ctf_trace_get_environment_field_name */
2958 ok(bt_ctf_trace_get_environment_field_name_by_index(NULL
, 0) == NULL
,
2959 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2960 ok(bt_ctf_trace_get_environment_field_name_by_index(trace
, -1) == NULL
,
2961 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2962 ok(bt_ctf_trace_get_environment_field_name_by_index(trace
, 5) == NULL
,
2963 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2964 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 0);
2965 ok(ret_string
&& !strcmp(ret_string
, "host"),
2966 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2967 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 1);
2968 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2969 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2970 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 2);
2971 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2972 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2973 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 3);
2974 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2975 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2976 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 4);
2977 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2978 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2980 /* Test bt_ctf_trace_get_environment_field_value */
2981 ok(bt_ctf_trace_get_environment_field_value_by_index(NULL
, 0) == NULL
,
2982 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2983 ok(bt_ctf_trace_get_environment_field_value_by_index(trace
, -1) == NULL
,
2984 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2985 ok(bt_ctf_trace_get_environment_field_value_by_index(trace
, 5) == NULL
,
2986 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2987 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 1);
2988 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2989 ok(!ret
&& ret_int64_t
== 23,
2990 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2992 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 2);
2993 ret
= bt_value_string_get(obj
, &ret_string
);
2994 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2995 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2998 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2999 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
3001 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3002 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3003 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3004 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3005 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3006 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3008 ret
= bt_value_string_get(obj
, &ret_string
);
3009 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3010 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3013 /* Test environment field replacement */
3014 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3016 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3017 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3018 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3019 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 3);
3020 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3021 ok(!ret
&& ret_int64_t
== 654321,
3022 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3025 /* On Solaris, uname() can return any positive value on success */
3026 if (uname(&name
) < 0) {
3031 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3032 == 0, "Add sysname (%s) environment field to writer instance",
3034 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3035 name
.nodename
) == 0,
3036 "Add nodename (%s) environment field to writer instance",
3038 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3039 == 0, "Add release (%s) environment field to writer instance",
3041 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3042 == 0, "Add version (%s) environment field to writer instance",
3044 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3045 == 0, "Add machine (%s) environment field to writer istance",
3048 /* Define a clock and add it to the trace */
3049 ok(bt_ctf_clock_create("signed") == NULL
,
3050 "Illegal clock name rejected");
3051 clock
= bt_ctf_clock_create(clock_name
);
3052 ok(clock
, "Clock created sucessfully");
3053 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3054 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3055 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3056 "Returned clock name is valid");
3058 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3059 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3060 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3061 "Clock description set successfully");
3063 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3064 ok(returned_clock_description
,
3065 "bt_ctf_clock_get_description returns a description.");
3066 ok(returned_clock_description
?
3067 !strcmp(returned_clock_description
, clock_description
) : 0,
3068 "Returned clock description is valid");
3070 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3071 "bt_ctf_clock_get_frequency returns the correct default frequency");
3072 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3073 "Set clock frequency");
3074 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3075 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3077 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3078 "bt_ctf_clock_get_offset_s succeeds");
3079 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3080 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3081 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3082 "Set clock offset (seconds)");
3083 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3084 "bt_ctf_clock_get_offset_s succeeds");
3085 ok(get_offset_s
== offset_s
,
3086 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3088 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3089 "bt_ctf_clock_get_offset succeeds");
3090 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3091 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3092 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3093 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3094 "bt_ctf_clock_get_offset succeeds");
3095 ok(get_offset
== offset
,
3096 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3098 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3099 "bt_ctf_clock_get_precision returns the correct default precision");
3100 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3101 "Set clock precision");
3102 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3103 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3105 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3106 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3107 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3108 "Set clock absolute property");
3109 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3110 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3112 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3115 ok(!bt_ctf_clock_get_name(NULL
),
3116 "bt_ctf_clock_get_name correctly handles NULL");
3117 ok(!bt_ctf_clock_get_description(NULL
),
3118 "bt_ctf_clock_get_description correctly handles NULL");
3119 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3120 "bt_ctf_clock_get_frequency correctly handles NULL");
3121 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3122 "bt_ctf_clock_get_precision correctly handles NULL");
3123 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3124 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3125 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3126 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3127 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3128 "bt_ctf_clock_get_offset correctly handles NULL clock");
3129 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3130 "bt_ctf_clock_get_offset correctly handles NULL output");
3131 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3132 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3134 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3135 "bt_ctf_clock_set_description correctly handles NULL clock");
3136 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3137 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3138 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3139 "bt_ctf_clock_get_precision correctly handles NULL clock");
3140 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3141 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3142 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3143 "bt_ctf_clock_set_offset correctly handles NULL clock");
3144 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3145 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3146 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3147 "bt_ctf_clock_set_time correctly handles NULL clock");
3148 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3149 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3150 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3152 "bt_ctf_clock_get_uuid returns a UUID");
3154 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3155 /* Slightly modify UUID */
3156 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3159 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3160 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3161 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3162 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3163 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3164 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3165 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3167 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3168 ok(uuid_match(ret_uuid
, tmp_uuid
),
3169 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3171 /* Define a stream class */
3172 stream_class
= bt_ctf_stream_class_create("test_stream");
3174 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3175 "bt_ctf_stream_class_get_name handles NULL correctly");
3176 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3177 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3178 "bt_ctf_stream_class_get_name returns a correct stream class name");
3180 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3181 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3182 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3183 "bt_ctf_stream_class_get_clock handles NULL correctly");
3185 ok(stream_class
, "Create stream class");
3186 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3187 "Set a stream class' clock");
3188 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3189 ok(ret_clock
== clock
,
3190 "bt_ctf_stream_class_get_clock returns a correct clock");
3193 /* Test the event fields and event types APIs */
3196 /* Test fields copying */
3199 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3200 "bt_ctf_stream_class_get_id returns an error when no id is set");
3201 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3202 "bt_ctf_stream_class_get_id handles NULL correctly");
3203 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3204 "bt_ctf_stream_class_set_id handles NULL correctly");
3205 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3206 "Set an stream class' id");
3207 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3208 "bt_ctf_stream_class_get_id returns the correct value");
3210 /* Validate default event header fields */
3211 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3212 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3213 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3216 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3217 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3218 "Default event header type is a structure");
3219 event_header_field_type
=
3220 bt_ctf_field_type_structure_get_field_type_by_name(
3221 ret_field_type
, "id");
3222 ok(event_header_field_type
,
3223 "Default event header type contains an \"id\" field");
3224 ok(bt_ctf_field_type_get_type_id(
3225 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3226 "Default event header \"id\" field is an integer");
3227 bt_put(event_header_field_type
);
3228 event_header_field_type
=
3229 bt_ctf_field_type_structure_get_field_type_by_name(
3230 ret_field_type
, "timestamp");
3231 ok(event_header_field_type
,
3232 "Default event header type contains a \"timestamp\" field");
3233 ok(bt_ctf_field_type_get_type_id(
3234 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3235 "Default event header \"timestamp\" field is an integer");
3236 bt_put(event_header_field_type
);
3237 bt_put(ret_field_type
);
3239 /* Add a custom trace packet header field */
3240 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3241 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3242 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3243 ok(packet_header_type
,
3244 "bt_ctf_trace_get_packet_header_type returns a packet header");
3245 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3246 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3247 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3248 packet_header_type
, "magic");
3249 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3250 bt_put(ret_field_type
);
3251 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3252 packet_header_type
, "uuid");
3253 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3254 bt_put(ret_field_type
);
3255 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3256 packet_header_type
, "stream_id");
3257 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3258 bt_put(ret_field_type
);
3260 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3261 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3262 packet_header_field_type
, "custom_trace_packet_header_field"),
3263 "Added a custom trace packet header field successfully");
3265 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3266 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3267 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3268 "Set a trace packet_header_type successfully");
3270 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3271 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3273 /* Add a custom field to the stream class' packet context */
3274 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3275 ok(packet_context_type
,
3276 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3277 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3278 "Packet context is a structure");
3280 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3281 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3283 integer_type
= bt_ctf_field_type_integer_create(32);
3284 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3286 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3287 /* Create a "uint5_t" equivalent custom packet context field */
3288 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3290 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3291 packet_context_field_type
, "custom_packet_context_field");
3292 ok(ret
== 0, "Packet context field added successfully");
3294 /* Define a stream event context containing a my_integer field. */
3295 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3296 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3297 ok(bt_ctf_stream_class_get_event_context_type(
3298 stream_class
) == NULL
,
3299 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3300 stream_event_context_type
= bt_ctf_field_type_structure_create();
3301 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3302 integer_type
, "common_event_context");
3304 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3305 stream_event_context_type
) < 0,
3306 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3307 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3309 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3311 ok(bt_ctf_stream_class_set_event_context_type(
3312 stream_class
, stream_event_context_type
) == 0,
3313 "Set a new stream event context type");
3314 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3316 ok(ret_field_type
== stream_event_context_type
,
3317 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3318 bt_put(ret_field_type
);
3320 /* Instantiate a stream and append events */
3321 ret
= bt_ctf_writer_add_clock(writer
, clock
);
3323 ok(bt_ctf_trace_get_stream_count(trace
) == 0,
3324 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)");
3325 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3326 ok(stream1
, "Instanciate a stream class from writer");
3327 ok(bt_ctf_trace_get_stream_count(trace
) == 1,
3328 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)");
3329 stream
= bt_ctf_trace_get_stream_by_index(trace
, 0);
3330 ok(stream
== stream1
,
3331 "bt_ctf_trace_get_stream_by_index() succeeds and returns the correct value");
3335 * Creating a stream through a writer adds the given stream
3336 * class to the writer's trace, thus registering the stream
3337 * class's clock to the trace.
3339 ok(bt_ctf_trace_get_clock_class_count(NULL
) < 0,
3340 "bt_ctf_trace_get_clock_class_count correctly handles NULL");
3341 ok(bt_ctf_trace_get_clock_class_count(trace
) == 1,
3342 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
3343 ok(!bt_ctf_trace_get_clock_class_by_index(NULL
, 0),
3344 "bt_ctf_trace_get_clock_class correctly handles NULL");
3345 ok(!bt_ctf_trace_get_clock_class_by_index(trace
, -1),
3346 "bt_ctf_trace_get_clock_class correctly handles negative indexes");
3347 ok(!bt_ctf_trace_get_clock_class_by_index(trace
, 1),
3348 "bt_ctf_trace_get_clock_class correctly handles out of bound accesses");
3349 ret_clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
3350 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3351 bt_ctf_clock_get_name(clock
)) == 0,
3352 "bt_ctf_trace_get_clock_class returns the right clock instance");
3353 bt_put(ret_clock_class
);
3354 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, NULL
),
3355 "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)");
3356 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, clock_name
),
3357 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3358 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, NULL
),
3359 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3360 ret_clock_class
= bt_ctf_trace_get_clock_class_by_name(trace
, clock_name
);
3361 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3362 bt_ctf_clock_get_name(clock
)) == 0,
3363 "bt_ctf_trace_get_clock_class returns the right clock instance");
3364 bt_put(ret_clock_class
);
3365 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, "random"),
3366 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3368 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3369 "bt_ctf_stream_get_class correctly handles NULL");
3370 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3371 ok(ret_stream_class
,
3372 "bt_ctf_stream_get_class returns a stream class");
3373 ok(ret_stream_class
== stream_class
,
3374 "Returned stream class is of the correct type");
3377 * Packet header, packet context, event header, and stream
3378 * event context types were copied for the resolving
3381 BT_PUT(packet_header_type
);
3382 BT_PUT(packet_context_type
);
3383 BT_PUT(stream_event_context_type
);
3384 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3385 assert(packet_header_type
);
3386 packet_context_type
=
3387 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3388 assert(packet_context_type
);
3389 stream_event_context_type
=
3390 bt_ctf_stream_class_get_event_context_type(stream_class
);
3391 assert(stream_event_context_type
);
3394 * Try to modify the packet context type after a stream has been
3397 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3398 packet_header_field_type
, "should_fail");
3400 "Trace packet header type can't be modified once a stream has been instanciated");
3403 * Try to modify the packet context type after a stream has been
3406 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3407 packet_context_field_type
, "should_fail");
3409 "Packet context type can't be modified once a stream has been instanciated");
3412 * Try to modify the stream event context type after a stream has been
3415 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3416 integer_type
, "should_fail");
3418 "Stream event context type can't be modified once a stream has been instanciated");
3420 /* Should fail after instanciating a stream (frozen) */
3421 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3422 "Changes to a stream class that was already instantiated fail");
3424 /* Populate the custom packet header field only once for all tests */
3425 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3426 "bt_ctf_stream_get_packet_header handles NULL correctly");
3427 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3429 "bt_ctf_stream_get_packet_header returns a packet header");
3430 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3431 ok(ret_field_type
== packet_header_type
,
3432 "Stream returns a packet header of the appropriate type");
3433 bt_put(ret_field_type
);
3434 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3435 "custom_trace_packet_header_field");
3436 ok(packet_header_field
,
3437 "Packet header structure contains a custom field with the appropriate name");
3438 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3439 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3440 "Custom packet header field is of the expected type");
3441 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3442 54321), "Set custom packet header value successfully");
3443 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3444 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3445 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3446 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3447 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3448 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3449 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3450 "Successfully set a stream's packet header");
3452 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3453 "Add environment field to writer after stream creation");
3457 test_create_writer_vs_non_writer_mode();
3459 test_set_clock_non_writer_stream_class();
3461 test_instanciate_event_before_stream(writer
, clock
);
3463 append_simple_event(stream_class
, stream1
, clock
);
3465 packet_resize_test(stream_class
, stream1
, clock
);
3467 append_complex_event(stream_class
, stream1
, clock
);
3469 append_existing_event_class(stream_class
);
3471 test_empty_stream(writer
);
3473 test_custom_event_header_stream(writer
, clock
);
3475 test_static_trace();
3477 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3478 ok(metadata_string
, "Get metadata string");
3480 bt_ctf_writer_flush_metadata(writer
);
3483 bt_put(ret_stream_class
);
3486 bt_put(packet_context_type
);
3487 bt_put(packet_context_field_type
);
3488 bt_put(integer_type
);
3489 bt_put(stream_event_context_type
);
3490 bt_put(ret_field_type
);
3491 bt_put(packet_header_type
);
3492 bt_put(packet_header_field_type
);
3493 bt_put(packet_header
);
3494 bt_put(packet_header_field
);
3496 free(metadata_string
);
3497 bt_put(stream_class
);
3499 validate_trace(argv
[1], trace_path
);
3501 //recursive_rmdir(trace_path);