6 * Copyright 2013 - 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf-writer/stream-class.h>
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ctf-ir/clock-class.h>
31 #include <babeltrace/ctf-ir/trace.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf/events.h>
34 #include <babeltrace/values.h>
37 #include <babeltrace/compat/stdlib-internal.h>
39 #include <babeltrace/compat/limits-internal.h>
40 #include <babeltrace/compat/stdio-internal.h>
49 #define METADATA_LINE_SIZE 512
50 #define SEQUENCE_TEST_LENGTH 10
51 #define ARRAY_TEST_LENGTH 5
52 #define PACKET_RESIZE_TEST_DEF_LENGTH 100000
54 #define DEFAULT_CLOCK_FREQ 1000000000
55 #define DEFAULT_CLOCK_PRECISION 1
56 #define DEFAULT_CLOCK_OFFSET 0
57 #define DEFAULT_CLOCK_OFFSET_S 0
58 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
59 #define DEFAULT_CLOCK_TIME 0
60 #define DEFAULT_CLOCK_VALUE 0
65 char sysname
[BABELTRACE_HOST_NAME_MAX
];
66 char nodename
[BABELTRACE_HOST_NAME_MAX
];
67 char release
[BABELTRACE_HOST_NAME_MAX
];
68 char version
[BABELTRACE_HOST_NAME_MAX
];
69 char machine
[BABELTRACE_HOST_NAME_MAX
];
72 static int64_t current_time
= 42;
73 static unsigned int packet_resize_test_length
= PACKET_RESIZE_TEST_DEF_LENGTH
;
75 /* Return 1 if uuids match, zero if different. */
77 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
82 if (!uuid_a
|| !uuid_b
) {
86 for (i
= 0; i
< 16; i
++) {
87 if (uuid_a
[i
] != uuid_b
[i
]) {
98 void validate_trace(char *parser_path
, char *trace_path
)
101 gchar
*standard_error
= NULL
;
103 char *argv
[] = {parser_path
, trace_path
, NULL
};
105 if (!parser_path
|| !trace_path
) {
110 if (!g_spawn_sync(NULL
,
113 G_SPAWN_STDOUT_TO_DEV_NULL
,
120 diag("Failed to spawn babeltrace.");
125 /* Replace by g_spawn_check_exit_status when we require glib >= 2.34 */
127 ret
= WIFEXITED(exit_status
) ? WEXITSTATUS(exit_status
) : -1;
133 diag("Babeltrace returned an error.");
134 diag_multiline(standard_error
);
139 ok(ret
== 0, "Babeltrace could read the resulting trace");
140 g_free(standard_error
);
144 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
145 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
147 /* Create and add a simple event class */
148 struct bt_ctf_event_class
*simple_event_class
=
149 bt_ctf_event_class_create("Simple Event");
150 struct bt_ctf_field_type
*uint_12_type
=
151 bt_ctf_field_type_integer_create(12);
152 struct bt_ctf_field_type
*int_64_type
=
153 bt_ctf_field_type_integer_create(64);
154 struct bt_ctf_field_type
*float_type
=
155 bt_ctf_field_type_floating_point_create();
156 struct bt_ctf_field_type
*enum_type
;
157 struct bt_ctf_field_type
*enum_type_unsigned
=
158 bt_ctf_field_type_enumeration_create(uint_12_type
);
159 struct bt_ctf_field_type
*event_context_type
=
160 bt_ctf_field_type_structure_create();
161 struct bt_ctf_field_type
*event_payload_type
= NULL
;
162 struct bt_ctf_field_type
*returned_type
;
163 struct bt_ctf_event
*simple_event
;
164 struct bt_ctf_field
*integer_field
;
165 struct bt_ctf_field
*float_field
;
166 struct bt_ctf_field
*enum_field
;
167 struct bt_ctf_field
*enum_field_unsigned
;
168 struct bt_ctf_field
*enum_container_field
;
169 const char *mapping_name_test
= "truie";
170 const double double_test_value
= 3.1415;
171 struct bt_ctf_field
*enum_container_field_unsigned
;
172 const char *mapping_name_negative_test
= "negative_value";
173 const char *ret_char
;
175 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
176 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
177 struct bt_ctf_event_class
*ret_event_class
;
178 struct bt_ctf_field
*packet_context
;
179 struct bt_ctf_field
*packet_context_field
;
180 struct bt_ctf_field
*stream_event_context
;
181 struct bt_ctf_field
*stream_event_context_field
;
182 struct bt_ctf_field
*event_context
;
183 struct bt_ctf_field
*event_context_field
;
184 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
185 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
186 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
187 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
190 ok(uint_12_type
, "Create an unsigned integer type");
192 ok(!bt_ctf_field_type_integer_set_signed(int_64_type
, 1),
193 "Set signed 64 bit integer signedness to true");
194 ok(int_64_type
, "Create a signed integer type");
195 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
197 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
198 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
199 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
200 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
201 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
202 bt_put(returned_type
);
204 bt_ctf_field_type_set_alignment(float_type
, 32);
205 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
206 "bt_ctf_field_type_get_alignment handles NULL correctly");
207 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
208 "bt_ctf_field_type_get_alignment returns a correct value");
210 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
211 "Set a floating point type's exponent digit count");
212 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
213 "Set a floating point type's mantissa digit count");
215 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
216 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
217 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
218 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
219 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
220 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
221 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
222 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
224 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
225 mapping_name_negative_test
, -12345, 0) == 0,
226 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
227 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
228 "escaping; \"test\"", 1, 1) == 0,
229 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
230 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
231 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
232 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
233 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
234 "event clock int float", 5, 22) == 0,
235 "Accept enumeration mapping strings containing reserved keywords");
236 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
238 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
239 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
240 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
241 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
242 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
243 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
244 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
246 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(NULL
, -42);
247 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly");
249 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, -4200000);
250 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values");
252 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, 3);
253 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value");
254 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, NULL
, NULL
, NULL
) == 0,
255 "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly");
258 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
259 "enum_field") == 0, "Add signed enumeration field to event");
261 ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL
, 0, &ret_char
,
262 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
263 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly");
264 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, NULL
,
265 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
266 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly");
267 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
268 NULL
, &ret_range_end_int64_t
) == 0,
269 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly");
270 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
271 &ret_range_start_int64_t
, NULL
) == 0,
272 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly");
273 /* Assumes entries are sorted by range_start values. */
274 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 6, &ret_char
,
275 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
276 "bt_ctf_field_type_enumeration_get_mapping_signed returns a value");
277 ok(!strcmp(ret_char
, mapping_name_test
),
278 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name");
279 ok(ret_range_start_int64_t
== 42,
280 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start");
281 ok(ret_range_end_int64_t
== 42,
282 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end");
284 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
285 "escaping; \"test\"", 0, 0) == 0,
286 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
287 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
288 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
289 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
290 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
291 "event clock int float", 5, 22) == 0,
292 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
293 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
294 42, 42) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts single-value ranges");
295 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
296 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names");
297 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
298 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries");
299 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
300 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
301 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
302 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
304 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
305 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
306 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 6,
307 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
309 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
310 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
311 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
312 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
313 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
314 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
315 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
316 NULL
, &ret_range_end_uint64_t
) == 0,
317 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
318 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
319 &ret_range_start_uint64_t
, NULL
) == 0,
320 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
321 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 4, &ret_char
,
322 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
323 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
324 ok(!strcmp(ret_char
, mapping_name_test
),
325 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
326 ok(ret_range_start_uint64_t
== 42,
327 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
328 ok(ret_range_end_uint64_t
== 42,
329 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
331 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
333 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
336 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
338 /* Set an event context type which will contain a single integer. */
339 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
340 "event_specific_context"),
341 "Add event specific context field");
342 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
343 "bt_ctf_event_class_get_context_type handles NULL correctly");
345 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
346 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
347 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
348 "Set an event class' context type successfully");
349 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
350 ok(returned_type
== event_context_type
,
351 "bt_ctf_event_class_get_context_type returns the appropriate type");
352 bt_put(returned_type
);
354 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
355 "Adding simple event class to stream class");
358 * bt_ctf_stream_class_add_event_class() copies the field types
359 * of simple_event_class, so we retrieve the new ones to create
360 * the appropriate fields.
362 BT_PUT(event_context_type
);
363 BT_PUT(event_payload_type
);
364 event_payload_type
= bt_ctf_event_class_get_payload_type(
366 assert(event_payload_type
);
367 event_context_type
= bt_ctf_event_class_get_context_type(
369 assert(event_context_type
);
370 ep_integer_field_type
=
371 bt_ctf_field_type_structure_get_field_type_by_name(
372 event_payload_type
, "integer_field");
373 assert(ep_integer_field_type
);
375 bt_ctf_field_type_structure_get_field_type_by_name(
376 event_payload_type
, "enum_field");
377 assert(ep_enum_field_type
);
378 ep_enum_field_unsigned_type
=
379 bt_ctf_field_type_structure_get_field_type_by_name(
380 event_payload_type
, "enum_field_unsigned");
381 assert(ep_enum_field_unsigned_type
);
383 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
384 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
385 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
386 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
387 ok(bt_ctf_stream_class_get_event_class_by_index(NULL
, 0) == NULL
,
388 "bt_ctf_stream_class_get_event_class handles NULL correctly");
389 ok(bt_ctf_stream_class_get_event_class_by_index(stream_class
, 8724) == NULL
,
390 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
391 ret_event_class
= bt_ctf_stream_class_get_event_class_by_index(stream_class
, 0);
392 ok(ret_event_class
== simple_event_class
,
393 "bt_ctf_stream_class_get_event_class returns the correct event class");
394 bt_put(ret_event_class
);
395 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
396 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
397 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
398 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
400 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
401 ok(ret_event_class
== simple_event_class
,
402 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
403 bt_put(ret_event_class
);
405 simple_event
= bt_ctf_event_create(simple_event_class
);
407 "Instantiate an event containing a single integer field");
409 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
410 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
411 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
412 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
414 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
415 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
416 "bt_ctf_field_floating_point_get_value fails on an unset float field");
417 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
418 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
419 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
420 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
421 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
422 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
423 "bt_ctf_field_floating_point_get_value returns a double value");
424 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
425 "bt_ctf_field_floating_point_get_value returns a correct value");
427 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
430 iter
= bt_ctf_field_enumeration_get_mappings(NULL
);
431 ok(!iter
, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
432 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
433 ok(!iter
, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
434 enum_container_field
= bt_ctf_field_enumeration_get_container(
436 ok(bt_ctf_field_signed_integer_set_value(
437 enum_container_field
, -42) == 0,
438 "Set signed enumeration container value");
439 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
440 ok(iter
, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
441 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, &ret_char
, NULL
, NULL
);
442 ok(!ret
&& ret_char
, "bt_ctf_field_type_enumeration_mapping_iterator_get_signed return a mapping name");
444 ok(!strcmp(ret_char
, mapping_name_negative_test
),
445 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
446 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
450 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
451 assert(enum_field_unsigned
);
452 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
453 enum_field_unsigned
);
454 ok(bt_ctf_field_unsigned_integer_set_value(
455 enum_container_field_unsigned
, 42) == 0,
456 "Set unsigned enumeration container value");
457 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
458 enum_field_unsigned
);
460 iter
= bt_ctf_field_enumeration_get_mappings(enum_field_unsigned
);
462 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, &ret_char
, NULL
, NULL
);
463 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
464 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container");
466 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
468 /* Populate stream event context */
469 stream_event_context
=
470 bt_ctf_event_get_stream_event_context(simple_event
);
471 assert(stream_event_context
);
472 stream_event_context_field
= bt_ctf_field_structure_get_field(
473 stream_event_context
, "common_event_context");
474 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
476 /* Populate the event's context */
477 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
478 "bt_ctf_event_get_event_context handles NULL correctly");
479 event_context
= bt_ctf_event_get_event_context(simple_event
);
481 "bt_ctf_event_get_event_context returns a field");
482 returned_type
= bt_ctf_field_get_type(event_context
);
483 ok(returned_type
== event_context_type
,
484 "bt_ctf_event_get_event_context returns a field of the appropriate type");
485 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
486 "event_specific_context");
487 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
488 "Successfully set an event context's value");
489 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
490 "bt_ctf_event_set_event_context handles a NULL event correctly");
491 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
492 "bt_ctf_event_set_event_context handles a NULL event context correctly");
493 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
494 "bt_ctf_event_set_event_context rejects a context of the wrong type");
495 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
496 "Set an event context successfully");
498 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
499 "Append simple event to trace stream");
501 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
502 "bt_ctf_stream_get_packet_context handles NULL correctly");
503 packet_context
= bt_ctf_stream_get_packet_context(stream
);
505 "bt_ctf_stream_get_packet_context returns a packet context");
507 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
509 ok(packet_context_field
,
510 "Packet context contains the default packet_size field.");
511 bt_put(packet_context_field
);
512 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
513 "custom_packet_context_field");
514 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
515 "Custom packet context field value successfully set.");
517 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
518 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
519 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
520 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
521 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
522 "Successfully set a stream's packet context");
524 ok(bt_ctf_stream_flush(stream
) == 0,
525 "Flush trace stream with one event");
527 bt_put(simple_event_class
);
528 bt_put(simple_event
);
529 bt_put(uint_12_type
);
533 bt_put(enum_type_unsigned
);
534 bt_put(returned_type
);
535 bt_put(event_context_type
);
536 bt_put(integer_field
);
539 bt_put(enum_field_unsigned
);
540 bt_put(enum_container_field
);
541 bt_put(enum_container_field_unsigned
);
542 bt_put(packet_context
);
543 bt_put(packet_context_field
);
544 bt_put(stream_event_context
);
545 bt_put(stream_event_context_field
);
546 bt_put(event_context
);
547 bt_put(event_context_field
);
548 bt_put(event_payload_type
);
549 bt_put(ep_integer_field_type
);
550 bt_put(ep_enum_field_type
);
551 bt_put(ep_enum_field_unsigned_type
);
556 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
557 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
560 struct event_class_attrs_counts
;
561 const char *complex_test_event_string
= "Complex Test Event";
562 const char *test_string_1
= "Test ";
563 const char *test_string_2
= "string ";
564 const char *test_string_3
= "abcdefghi";
565 const char *test_string_4
= "abcd\0efg\0hi";
566 const char *test_string_cat
= "Test string abcdeabcd";
567 struct bt_ctf_field_type
*uint_35_type
=
568 bt_ctf_field_type_integer_create(35);
569 struct bt_ctf_field_type
*int_16_type
=
570 bt_ctf_field_type_integer_create(16);
571 struct bt_ctf_field_type
*uint_3_type
=
572 bt_ctf_field_type_integer_create(3);
573 struct bt_ctf_field_type
*enum_variant_type
=
574 bt_ctf_field_type_enumeration_create(uint_3_type
);
575 struct bt_ctf_field_type
*variant_type
=
576 bt_ctf_field_type_variant_create(enum_variant_type
,
578 struct bt_ctf_field_type
*string_type
=
579 bt_ctf_field_type_string_create();
580 struct bt_ctf_field_type
*sequence_type
;
581 struct bt_ctf_field_type
*array_type
;
582 struct bt_ctf_field_type
*inner_structure_type
=
583 bt_ctf_field_type_structure_create();
584 struct bt_ctf_field_type
*complex_structure_type
=
585 bt_ctf_field_type_structure_create();
586 struct bt_ctf_field_type
*ret_field_type
;
587 struct bt_ctf_event_class
*event_class
;
588 struct bt_ctf_event
*event
;
589 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
590 *inner_structure_field
, *complex_structure_field
,
591 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
592 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
593 *stream_event_ctx_int_field
, *ret_field
;
594 uint64_t ret_unsigned_int
;
595 int64_t ret_signed_int
;
596 const char *ret_string
;
597 struct bt_ctf_stream_class
*ret_stream_class
;
598 struct bt_ctf_event_class
*ret_event_class
;
599 struct bt_ctf_field
*packet_context
, *packet_context_field
;
600 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
602 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
603 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
604 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
605 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
606 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
607 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
608 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
609 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
610 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
611 "Set alignment of signed 16 bit integer to 32");
612 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
613 "Set integer signedness to true");
614 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
615 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
616 "Set signed 16 bit integer base to hexadecimal");
618 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
619 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
622 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
623 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
624 ret_field_type
= bt_ctf_field_type_array_get_element_type(
626 ok(ret_field_type
== int_16_type
,
627 "bt_ctf_field_type_array_get_element_type returns the correct type");
628 bt_put(ret_field_type
);
630 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
631 "bt_ctf_field_type_array_get_length handles NULL correctly");
632 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
633 "bt_ctf_field_type_array_get_length returns the correct length");
635 ok(bt_ctf_field_type_structure_add_field(inner_structure_type
,
636 inner_structure_type
, "yes"), "Cannot add self to structure");
637 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
638 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
639 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
640 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
641 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
642 array_type
, "an_array"), "Add an_array field to inner structure");
644 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
646 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
648 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
649 "UINT35_TYPE", 2, 7);
651 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(NULL
, "INT16_TYPE");
652 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly");
654 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, "INT16_TYPE");
655 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly");
656 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
657 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly");
660 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, NULL
);
661 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly");
663 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL
, 1);
664 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly");
666 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, -42);
667 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly");
668 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) != 0,
669 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly");
672 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, 5);
673 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly");
674 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
675 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly");
678 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
679 "An unknown entry"), "Reject a variant field based on an unknown tag value");
680 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
681 "UINT3_TYPE") == 0, "Add a field to a variant");
682 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
683 "INT16_TYPE"), "Add INT16_TYPE field to variant");
684 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
685 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
687 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
688 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
689 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
690 ok(ret_field_type
== enum_variant_type
,
691 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
692 bt_put(ret_field_type
);
694 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
695 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
696 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
697 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
698 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
699 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
700 "INT16_TYPE") == NULL
,
701 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
702 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
704 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
705 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
706 variant_type
, "INT16_TYPE");
707 ok(ret_field_type
== int_16_type
,
708 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
709 bt_put(ret_field_type
);
711 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
712 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
713 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
714 "bt_ctf_field_type_variant_get_field_count returns the correct count");
716 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
717 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
718 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
719 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
720 bt_put(ret_field_type
);
721 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
722 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
723 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
724 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
725 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
726 "bt_ctf_field_type_variant_get_field returns a field");
727 ok(!strcmp("INT16_TYPE", ret_string
),
728 "bt_ctf_field_type_variant_get_field returns a correct field name");
729 ok(ret_field_type
== int_16_type
,
730 "bt_ctf_field_type_variant_get_field returns a correct field type");
731 bt_put(ret_field_type
);
733 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
734 enum_variant_type
, "variant_selector"),
735 "Add variant_selector field to complex structure");
736 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
737 string_type
, "a_string"), "Add a_string field to complex structure");
738 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
739 variant_type
, "variant_value"),
740 "Add variant_value field to complex structure");
741 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
742 inner_structure_type
, "inner_structure"),
743 "Add inner_structure field to complex structure");
745 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
746 ok(event_class
, "Create an event class");
747 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
748 "Reject addition of a field with an empty name to an event");
749 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
750 "Reject addition of a field with a NULL type to an event");
751 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
753 "Reject addition of a type with an illegal name to an event");
754 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
756 "Add field of type unsigned integer to an event");
757 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
758 "int_16") == 0, "Add field of type signed integer to an event");
759 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
760 "complex_structure") == 0,
761 "Add composite structure to an event");
763 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
764 "bt_ctf_event_class_get_name handles NULL correctly");
765 ret_string
= bt_ctf_event_class_get_name(event_class
);
766 ok(!strcmp(ret_string
, complex_test_event_string
),
767 "bt_ctf_event_class_get_name returns a correct name");
768 ok(bt_ctf_event_class_get_id(event_class
) < 0,
769 "bt_ctf_event_class_get_id returns a negative value when not set");
770 ok(bt_ctf_event_class_get_id(NULL
) < 0,
771 "bt_ctf_event_class_get_id handles NULL correctly");
772 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
773 "bt_ctf_event_class_set_id handles NULL correctly");
774 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
775 "Set an event class' id");
776 ok(bt_ctf_event_class_get_id(event_class
) == 42,
777 "bt_ctf_event_class_get_id returns the correct value");
779 /* Test event class attributes */
780 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
,
781 "event class has the expected initial log level");
782 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
783 "as expected, event class has no initial EMF URI");
784 ok(bt_ctf_event_class_set_log_level(NULL
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
785 "bt_ctf_event_class_set_log_level handles a NULL event class correctly");
786 ok(bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
),
787 "bt_ctf_event_class_set_log_level handles an unknown log level correctly");
788 ok(!bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
789 "bt_ctf_event_class_set_log_level succeeds with a valid log level");
790 ok(bt_ctf_event_class_get_log_level(NULL
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
,
791 "bt_ctf_event_class_get_log_level handles a NULL event class correctly");
792 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
,
793 "bt_ctf_event_class_get_log_level returns the expected log level");
794 ok(bt_ctf_event_class_set_emf_uri(NULL
, "http://diamon.org/babeltrace/"),
795 "bt_ctf_event_class_set_emf_uri handles a NULL event class correctly");
796 ok(!bt_ctf_event_class_set_emf_uri(event_class
, "http://diamon.org/babeltrace/"),
797 "bt_ctf_event_class_set_emf_uri succeeds with a valid EMF URI");
798 ok(!bt_ctf_event_class_get_emf_uri(NULL
),
799 "bt_ctf_event_class_get_emf_uri handles a NULL event class correctly");
800 ok(strcmp(bt_ctf_event_class_get_emf_uri(event_class
), "http://diamon.org/babeltrace/") == 0,
801 "bt_ctf_event_class_get_emf_uri returns the expected EMF URI");
802 ok(!bt_ctf_event_class_set_emf_uri(event_class
, NULL
),
803 "bt_ctf_event_class_set_emf_uri succeeds with NULL (to reset)");
804 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
805 "as expected, event class has no EMF URI after reset");
807 /* Add event class to the stream class */
808 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
809 "Reject addition of NULL event class to a stream class");
810 ok(bt_ctf_stream_class_add_event_class(stream_class
,
811 event_class
) == 0, "Add an event class to stream class");
813 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
814 "bt_ctf_event_class_get_stream_class handles NULL correctly");
815 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
816 ok(ret_stream_class
== stream_class
,
817 "bt_ctf_event_class_get_stream_class returns the correct stream class");
818 bt_put(ret_stream_class
);
820 ok(bt_ctf_event_class_get_payload_type_field_count(NULL
) < 0,
821 "bt_ctf_event_class_get_field_count handles NULL correctly");
822 ok(bt_ctf_event_class_get_payload_type_field_count(event_class
) == 3,
823 "bt_ctf_event_class_get_field_count returns a correct value");
825 ok(bt_ctf_event_class_get_payload_type_field_by_index(NULL
, &ret_string
,
826 &ret_field_type
, 0) < 0,
827 "bt_ctf_event_class_get_field handles a NULL event class correctly");
828 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, NULL
,
829 &ret_field_type
, 0) == 0,
830 "bt_ctf_event_class_get_field handles a NULL field name correctly");
831 bt_put(ret_field_type
);
832 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
834 "bt_ctf_event_class_get_field handles a NULL field type correctly");
835 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
836 &ret_field_type
, 42) < 0,
837 "bt_ctf_event_class_get_field handles an invalid index correctly");
838 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
839 &ret_field_type
, 0) == 0,
840 "bt_ctf_event_class_get_field returns a field");
841 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
842 "bt_ctf_event_class_get_field returns a correct field type");
843 bt_put(ret_field_type
);
844 ok(!strcmp(ret_string
, "uint_35"),
845 "bt_ctf_event_class_get_field returns a correct field name");
846 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
847 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
848 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
849 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
850 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
851 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
852 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
853 "complex_structure");
854 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
855 "bt_ctf_event_class_get_field_by_name returns a correct field type");
856 bt_put(ret_field_type
);
858 event
= bt_ctf_event_create(event_class
);
859 ok(event
, "Instanciate a complex event");
861 ok(bt_ctf_event_get_class(NULL
) == NULL
,
862 "bt_ctf_event_get_class handles NULL correctly");
863 ret_event_class
= bt_ctf_event_get_class(event
);
864 ok(ret_event_class
== event_class
,
865 "bt_ctf_event_get_class returns the correct event class");
866 bt_put(ret_event_class
);
868 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
869 if (!uint_35_field
) {
870 printf("uint_35_field is NULL\n");
873 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
874 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
875 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
876 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
877 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
878 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
879 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
880 &ret_unsigned_int
) == 0,
881 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
882 ok(ret_unsigned_int
== 0x0DDF00D,
883 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
884 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
885 &ret_signed_int
) < 0,
886 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
887 bt_put(uint_35_field
);
889 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
890 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
891 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
892 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
893 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
894 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
895 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
896 &ret_signed_int
) == 0,
897 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
898 ok(ret_signed_int
== -12345,
899 "bt_ctf_field_signed_integer_get_value returns the correct value");
900 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
901 &ret_unsigned_int
) < 0,
902 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
903 bt_put(int_16_field
);
905 complex_structure_field
= bt_ctf_event_get_payload(event
,
906 "complex_structure");
908 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
909 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
910 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
911 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
912 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
913 complex_structure_field
, 3);
914 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
915 bt_put(inner_structure_field
);
916 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
917 "bt_ctf_field_structure_get_field_by_index returns a correct field");
918 bt_put(ret_field_type
);
920 inner_structure_field
= bt_ctf_field_structure_get_field(
921 complex_structure_field
, "inner_structure");
922 a_string_field
= bt_ctf_field_structure_get_field(
923 complex_structure_field
, "a_string");
924 enum_variant_field
= bt_ctf_field_structure_get_field(
925 complex_structure_field
, "variant_selector");
926 variant_field
= bt_ctf_field_structure_get_field(
927 complex_structure_field
, "variant_value");
928 uint_35_field
= bt_ctf_field_structure_get_field(
929 inner_structure_field
, "seq_len");
930 a_sequence_field
= bt_ctf_field_structure_get_field(
931 inner_structure_field
, "a_sequence");
932 an_array_field
= bt_ctf_field_structure_get_field(
933 inner_structure_field
, "an_array");
935 enum_container_field
= bt_ctf_field_enumeration_get_container(
937 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
938 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
940 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
941 bt_put(int_16_field
);
942 ok(!bt_ctf_field_string_get_value(a_string_field
),
943 "bt_ctf_field_string_get_value returns NULL on an unset field");
944 bt_ctf_field_string_set_value(a_string_field
,
946 ok(!bt_ctf_field_string_get_value(NULL
),
947 "bt_ctf_field_string_get_value correctly handles NULL");
948 ok(bt_ctf_field_string_append(NULL
, "yeah"),
949 "bt_ctf_field_string_append correctly handles a NULL string field");
950 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
951 "bt_ctf_field_string_append correctly handles a NULL string value");
952 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
953 "bt_ctf_field_string_append succeeds");
954 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
955 "bt_ctf_field_string_append_len correctly handles a NULL string field");
956 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
957 "bt_ctf_field_string_append_len correctly handles a NULL string value");
958 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
959 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
960 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
961 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
962 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
963 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
964 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
965 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
967 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
968 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
969 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
970 "bt_ctf_field_string_get_value returns a correct value");
971 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
972 SEQUENCE_TEST_LENGTH
);
974 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
975 enum_container_field
) == NULL
,
976 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
977 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
979 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
980 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
981 variant_type
, enum_variant_field
);
982 ok(ret_field_type
== int_16_type
,
983 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
985 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
986 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
987 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
988 uint_35_field
) == 0, "Set a sequence field's length");
989 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
990 ok(ret_field
== uint_35_field
,
991 "bt_ctf_field_sequence_get_length returns the correct length field");
992 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
993 "bt_ctf_field_sequence_get_length properly handles NULL");
995 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
996 int_16_field
= bt_ctf_field_sequence_get_field(
997 a_sequence_field
, i
);
998 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
999 bt_put(int_16_field
);
1002 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1003 int_16_field
= bt_ctf_field_array_get_field(
1005 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1006 bt_put(int_16_field
);
1009 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1010 assert(stream_event_ctx_field
);
1011 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1012 stream_event_ctx_field
, "common_event_context");
1013 BT_PUT(stream_event_ctx_field
);
1014 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1015 BT_PUT(stream_event_ctx_int_field
);
1017 bt_ctf_clock_set_time(clock
, ++current_time
);
1018 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1019 "Append a complex event to a stream");
1022 * Populate the custom packet context field with a dummy value
1023 * otherwise flush will fail.
1025 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1026 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1027 "custom_packet_context_field");
1028 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1030 ok(bt_ctf_stream_flush(stream
) == 0,
1031 "Flush a stream containing a complex event");
1033 bt_put(uint_35_field
);
1034 bt_put(a_string_field
);
1035 bt_put(inner_structure_field
);
1036 bt_put(complex_structure_field
);
1037 bt_put(a_sequence_field
);
1038 bt_put(an_array_field
);
1039 bt_put(enum_variant_field
);
1040 bt_put(enum_container_field
);
1041 bt_put(variant_field
);
1043 bt_put(packet_context_field
);
1044 bt_put(packet_context
);
1045 bt_put(uint_35_type
);
1046 bt_put(int_16_type
);
1047 bt_put(string_type
);
1048 bt_put(sequence_type
);
1050 bt_put(inner_structure_type
);
1051 bt_put(complex_structure_type
);
1052 bt_put(uint_3_type
);
1053 bt_put(enum_variant_type
);
1054 bt_put(variant_type
);
1055 bt_put(ret_field_type
);
1056 bt_put(event_class
);
1061 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1062 struct bt_ctf_field_type
*expected_type
, const char *name
)
1064 struct bt_ctf_field_type
*copy_type
;
1066 copy_type
= bt_ctf_field_get_type(field
);
1067 ok(copy_type
== expected_type
,
1068 "bt_ctf_field_copy does not copy the type (%s)", name
);
1073 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1074 struct bt_ctf_field
*field_b
, const char *name
)
1076 ok(field_a
!= field_b
,
1077 "bt_ctf_field_copy creates different pointers (%s)", name
);
1081 void field_copy_tests()
1083 struct bt_ctf_field_type
*len_type
= NULL
;
1084 struct bt_ctf_field_type
*fp_type
= NULL
;
1085 struct bt_ctf_field_type
*s_type
= NULL
;
1086 struct bt_ctf_field_type
*e_int_type
= NULL
;
1087 struct bt_ctf_field_type
*e_type
= NULL
;
1088 struct bt_ctf_field_type
*v_type
= NULL
;
1089 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1090 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1091 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1092 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1093 struct bt_ctf_field_type
*strct_type
= NULL
;
1094 struct bt_ctf_field
*len
= NULL
;
1095 struct bt_ctf_field
*fp
= NULL
;
1096 struct bt_ctf_field
*s
= NULL
;
1097 struct bt_ctf_field
*e_int
= NULL
;
1098 struct bt_ctf_field
*e
= NULL
;
1099 struct bt_ctf_field
*v
= NULL
;
1100 struct bt_ctf_field
*v_selected
= NULL
;
1101 struct bt_ctf_field
*v_selected_cur
= NULL
;
1102 struct bt_ctf_field
*v_selected_0
= NULL
;
1103 struct bt_ctf_field
*v_selected_1
= NULL
;
1104 struct bt_ctf_field
*v_selected_2
= NULL
;
1105 struct bt_ctf_field
*v_selected_3
= NULL
;
1106 struct bt_ctf_field
*v_selected_4
= NULL
;
1107 struct bt_ctf_field
*v_selected_5
= NULL
;
1108 struct bt_ctf_field
*v_selected_6
= NULL
;
1109 struct bt_ctf_field
*a
= NULL
;
1110 struct bt_ctf_field
*a_0
= NULL
;
1111 struct bt_ctf_field
*a_1
= NULL
;
1112 struct bt_ctf_field
*a_2
= NULL
;
1113 struct bt_ctf_field
*a_3
= NULL
;
1114 struct bt_ctf_field
*a_4
= NULL
;
1115 struct bt_ctf_field
*strct
= NULL
;
1116 struct bt_ctf_field
*len_copy
= NULL
;
1117 struct bt_ctf_field
*fp_copy
= NULL
;
1118 struct bt_ctf_field
*s_copy
= NULL
;
1119 struct bt_ctf_field
*e_int_copy
= NULL
;
1120 struct bt_ctf_field
*e_copy
= NULL
;
1121 struct bt_ctf_field
*v_copy
= NULL
;
1122 struct bt_ctf_field
*v_selected_copy
= NULL
;
1123 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1124 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1125 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1126 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1127 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1128 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1129 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1130 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1131 struct bt_ctf_field
*a_copy
= NULL
;
1132 struct bt_ctf_field
*a_0_copy
= NULL
;
1133 struct bt_ctf_field
*a_1_copy
= NULL
;
1134 struct bt_ctf_field
*a_2_copy
= NULL
;
1135 struct bt_ctf_field
*a_3_copy
= NULL
;
1136 struct bt_ctf_field
*a_4_copy
= NULL
;
1137 struct bt_ctf_field
*strct_copy
= NULL
;
1138 struct bt_ctf_field_type_enumeration_mapping_iterator
*e_iter
= NULL
;
1139 uint64_t uint64_t_val
;
1140 const char *str_val
;
1144 /* create len type */
1145 len_type
= bt_ctf_field_type_integer_create(32);
1148 /* create fp type */
1149 fp_type
= bt_ctf_field_type_floating_point_create();
1153 s_type
= bt_ctf_field_type_string_create();
1156 /* create e_int type */
1157 e_int_type
= bt_ctf_field_type_integer_create(8);
1161 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1163 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1166 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1170 /* create v_label1 type */
1171 v_label1_type
= bt_ctf_field_type_string_create();
1172 assert(v_label1_type
);
1174 /* create v_label1_array type */
1175 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1176 assert(v_label1_array_type
);
1178 /* create v_label2 type */
1179 v_label2_type
= bt_ctf_field_type_integer_create(16);
1180 assert(v_label2_type
);
1182 /* create v_label2_seq type */
1183 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1185 assert(v_label2_seq_type
);
1188 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1190 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1193 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1197 /* create strct type */
1198 strct_type
= bt_ctf_field_type_structure_create();
1200 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1203 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1205 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1207 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1209 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1211 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1212 v_label1_array_type
, "a");
1216 strct
= bt_ctf_field_create(strct_type
);
1220 len
= bt_ctf_field_structure_get_field(strct
, "len");
1224 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1228 s
= bt_ctf_field_structure_get_field(strct
, "s");
1232 e
= bt_ctf_field_structure_get_field(strct
, "e");
1235 /* get e_int (underlying integer) */
1236 e_int
= bt_ctf_field_enumeration_get_container(e
);
1240 v
= bt_ctf_field_structure_get_field(strct
, "v");
1244 a
= bt_ctf_field_structure_get_field(strct
, "a");
1248 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1252 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1256 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1259 /* set e field (LABEL2) */
1260 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1264 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1266 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1267 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1268 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1269 ok(v_selected_cur
== v_selected
,
1270 "bt_ctf_field_variant_get_current_field returns the current field");
1271 bt_put(v_selected_cur
);
1273 /* set selected v field */
1274 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1276 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1277 assert(v_selected_0
);
1278 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1280 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1281 assert(v_selected_1
);
1282 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1284 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1285 assert(v_selected_2
);
1286 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1288 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1289 assert(v_selected_3
);
1290 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1292 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1293 assert(v_selected_4
);
1294 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1296 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1297 assert(v_selected_5
);
1298 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1300 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1301 assert(v_selected_6
);
1302 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1306 a_0
= bt_ctf_field_array_get_field(a
, 0);
1308 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1310 a_1
= bt_ctf_field_array_get_field(a
, 1);
1312 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1314 a_2
= bt_ctf_field_array_get_field(a
, 2);
1316 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1318 a_3
= bt_ctf_field_array_get_field(a
, 3);
1320 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1322 a_4
= bt_ctf_field_array_get_field(a
, 4);
1324 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1327 /* create copy of strct */
1328 ok(!bt_ctf_field_copy(NULL
),
1329 "bt_ctf_field_copy handles NULL correctly");
1330 strct_copy
= bt_ctf_field_copy(strct
);
1332 "bt_ctf_field_copy returns a valid pointer");
1334 /* get all copied fields */
1335 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1337 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1339 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1341 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1343 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1345 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1347 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1348 assert(v_selected_copy
);
1349 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1350 assert(v_selected_0_copy
);
1351 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1352 assert(v_selected_1_copy
);
1353 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1354 assert(v_selected_2_copy
);
1355 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1356 assert(v_selected_3_copy
);
1357 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1358 assert(v_selected_4_copy
);
1359 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1360 assert(v_selected_5_copy
);
1361 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1362 assert(v_selected_6_copy
);
1363 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1364 "sequence field copy is not too large");
1365 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1367 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1369 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1371 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1373 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1375 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1377 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1378 "array field copy is not too large");
1380 /* make sure copied fields are different pointers */
1381 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1382 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1383 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1384 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1385 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1386 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1387 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1388 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1390 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1392 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1394 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1396 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1398 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1400 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1402 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1404 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1405 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1406 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1407 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1408 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1409 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1411 /* make sure copied fields share the same types */
1412 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1413 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1414 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1415 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1416 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1417 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1418 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1420 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1422 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1424 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1426 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1428 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1430 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1432 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1434 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1435 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1436 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1437 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1438 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1439 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1441 /* validate len copy */
1442 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1444 ok(uint64_t_val
== 7,
1445 "bt_ctf_field_copy creates a valid integer field copy");
1447 /* validate fp copy */
1448 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1450 ok(double_val
== 3.14,
1451 "bt_ctf_field_copy creates a valid floating point number field copy");
1453 /* validate s copy */
1454 str_val
= bt_ctf_field_string_get_value(s_copy
);
1455 ok(str_val
&& !strcmp(str_val
, "btbt"),
1456 "bt_ctf_field_copy creates a valid string field copy");
1458 /* validate e_int copy */
1459 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1462 ok(uint64_t_val
== 23,
1463 "bt_ctf_field_copy creates a valid enum's integer field copy");
1465 /* validate e copy */
1466 e_iter
= bt_ctf_field_enumeration_get_mappings(e_copy
);
1467 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter
,
1468 &str_val
, NULL
, NULL
);
1469 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1470 "bt_ctf_field_copy creates a valid enum field copy");
1472 /* validate v_selected copy */
1473 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1474 assert(v_selected_copy_len
);
1475 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1478 ok(uint64_t_val
== 7,
1479 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1480 bt_put(v_selected_copy_len
);
1481 v_selected_copy_len
= NULL
;
1483 /* validate v_selected copy fields */
1484 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1487 ok(uint64_t_val
== 7,
1488 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1489 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1492 ok(uint64_t_val
== 6,
1493 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1494 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1497 ok(uint64_t_val
== 5,
1498 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1499 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1502 ok(uint64_t_val
== 4,
1503 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1504 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1507 ok(uint64_t_val
== 3,
1508 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1509 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1512 ok(uint64_t_val
== 2,
1513 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1514 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1517 ok(uint64_t_val
== 1,
1518 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1520 /* validate a copy fields */
1521 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1522 ok(str_val
&& !strcmp(str_val
, "a_0"),
1523 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1524 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1525 ok(str_val
&& !strcmp(str_val
, "a_1"),
1526 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1527 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1528 ok(str_val
&& !strcmp(str_val
, "a_2"),
1529 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1530 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1531 ok(str_val
&& !strcmp(str_val
, "a_3"),
1532 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1533 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1534 ok(str_val
&& !strcmp(str_val
, "a_4"),
1535 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1537 /* put everything */
1544 bt_put(v_label1_type
);
1545 bt_put(v_label1_array_type
);
1546 bt_put(v_label2_type
);
1547 bt_put(v_label2_seq_type
);
1556 bt_put(v_selected_0
);
1557 bt_put(v_selected_1
);
1558 bt_put(v_selected_2
);
1559 bt_put(v_selected_3
);
1560 bt_put(v_selected_4
);
1561 bt_put(v_selected_5
);
1562 bt_put(v_selected_6
);
1576 bt_put(v_selected_copy
);
1577 bt_put(v_selected_0_copy
);
1578 bt_put(v_selected_1_copy
);
1579 bt_put(v_selected_2_copy
);
1580 bt_put(v_selected_3_copy
);
1581 bt_put(v_selected_4_copy
);
1582 bt_put(v_selected_5_copy
);
1583 bt_put(v_selected_6_copy
);
1595 void type_field_tests()
1597 struct bt_ctf_field
*uint_12
;
1598 struct bt_ctf_field
*int_16
;
1599 struct bt_ctf_field
*string
;
1600 struct bt_ctf_field
*enumeration
;
1601 struct bt_ctf_field_type
*composite_structure_type
;
1602 struct bt_ctf_field_type
*structure_seq_type
;
1603 struct bt_ctf_field_type
*string_type
;
1604 struct bt_ctf_field_type
*sequence_type
;
1605 struct bt_ctf_field_type
*uint_8_type
;
1606 struct bt_ctf_field_type
*int_16_type
;
1607 struct bt_ctf_field_type
*uint_12_type
=
1608 bt_ctf_field_type_integer_create(12);
1609 struct bt_ctf_field_type
*enumeration_type
;
1610 struct bt_ctf_field_type
*returned_type
;
1611 const char *ret_string
;
1613 returned_type
= bt_ctf_field_get_type(NULL
);
1614 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1616 ok(uint_12_type
, "Create an unsigned integer type");
1617 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1618 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1619 "Set integer type's base as binary");
1620 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1621 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1622 "Set integer type's base as decimal");
1623 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1624 BT_CTF_INTEGER_BASE_UNKNOWN
),
1625 "Reject integer type's base set as unknown");
1626 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1627 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1628 "Set integer type's base as octal");
1629 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1630 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1631 "Set integer type's base as hexadecimal");
1632 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1633 "Reject unknown integer base value");
1634 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1635 "Set integer type signedness to signed");
1636 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1637 "Set integer type signedness to unsigned");
1638 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1639 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1640 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1641 "bt_ctf_field_type_integer_get_size returns a correct value");
1642 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1643 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1644 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1645 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1647 ok(bt_ctf_field_type_set_byte_order(NULL
,
1648 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1649 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1650 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1651 (enum bt_ctf_byte_order
) 42) < 0,
1652 "bt_ctf_field_type_set_byte_order rejects invalid values");
1653 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1654 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1655 "Set an integer's byte order to little endian");
1656 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1657 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1658 "Set an integer's byte order to big endian");
1659 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1660 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1661 "bt_ctf_field_type_get_byte_order returns a correct value");
1662 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1663 BT_CTF_BYTE_ORDER_UNKNOWN
,
1664 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1666 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1667 BT_CTF_FIELD_TYPE_ID_UNKNOWN
,
1668 "bt_ctf_field_type_get_type_id handles NULL correctly");
1669 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1670 BT_CTF_FIELD_TYPE_ID_INTEGER
,
1671 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1673 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1674 BT_CTF_INTEGER_BASE_UNKNOWN
,
1675 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1676 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1677 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1678 "bt_ctf_field_type_integer_get_base returns a correct value");
1680 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1681 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1682 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1683 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1684 (enum bt_ctf_string_encoding
) 123) < 0,
1685 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1686 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1687 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1688 "Set integer type encoding to UTF8");
1689 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1690 BT_CTF_STRING_ENCODING_UNKNOWN
,
1691 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1692 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1693 BT_CTF_STRING_ENCODING_UTF8
,
1694 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1696 int_16_type
= bt_ctf_field_type_integer_create(16);
1697 assert(int_16_type
);
1698 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1699 "Set signedness of 16 bit integer to true");
1700 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1701 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1702 uint_8_type
= bt_ctf_field_type_integer_create(8);
1704 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1705 ok(sequence_type
, "Create a sequence of int16_t type");
1706 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1707 BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
1708 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1710 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1711 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1712 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1714 ok(!strcmp(ret_string
, "seq_len"),
1715 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1716 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1717 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1718 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1720 ok(returned_type
== int_16_type
,
1721 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1722 bt_put(returned_type
);
1724 string_type
= bt_ctf_field_type_string_create();
1725 ok(string_type
, "Create a string type");
1726 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1727 BT_CTF_STRING_ENCODING_NONE
),
1728 "Reject invalid \"None\" string encoding");
1729 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1731 "Reject invalid string encoding");
1732 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1733 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1734 "Set string encoding to ASCII");
1736 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1737 BT_CTF_STRING_ENCODING_UNKNOWN
,
1738 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1739 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1740 BT_CTF_STRING_ENCODING_ASCII
,
1741 "bt_ctf_field_type_string_get_encoding returns the correct value");
1743 structure_seq_type
= bt_ctf_field_type_structure_create();
1744 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1745 BT_CTF_FIELD_TYPE_ID_STRUCT
,
1746 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1747 ok(structure_seq_type
, "Create a structure type");
1748 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1749 uint_8_type
, "seq_len") == 0,
1750 "Add a uint8_t type to a structure");
1751 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1752 sequence_type
, "a_sequence") == 0,
1753 "Add a sequence type to a structure");
1755 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1756 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1757 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1758 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1760 ok(bt_ctf_field_type_structure_get_field(NULL
,
1761 &ret_string
, &returned_type
, 1) < 0,
1762 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1763 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1764 NULL
, &returned_type
, 1) == 0,
1765 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1766 bt_put(returned_type
);
1767 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1768 &ret_string
, NULL
, 1) == 0,
1769 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1770 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1771 &ret_string
, &returned_type
, 10) < 0,
1772 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1773 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1774 &ret_string
, &returned_type
, 1) == 0,
1775 "bt_ctf_field_type_structure_get_field returns a field");
1776 ok(!strcmp(ret_string
, "a_sequence"),
1777 "bt_ctf_field_type_structure_get_field returns a correct field name");
1778 ok(returned_type
== sequence_type
,
1779 "bt_ctf_field_type_structure_get_field returns a correct field type");
1780 bt_put(returned_type
);
1782 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1783 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1784 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1785 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1786 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1787 structure_seq_type
, "a_sequence");
1788 ok(returned_type
== sequence_type
,
1789 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1790 bt_put(returned_type
);
1792 composite_structure_type
= bt_ctf_field_type_structure_create();
1793 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1794 string_type
, "a_string") == 0,
1795 "Add a string type to a structure");
1796 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1797 structure_seq_type
, "inner_structure") == 0,
1798 "Add a structure type to a structure");
1800 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1801 NULL
, "a_sequence") == NULL
,
1802 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1803 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1804 structure_seq_type
, NULL
) == NULL
,
1805 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1806 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1807 structure_seq_type
, "a_sequence");
1808 ok(returned_type
== sequence_type
,
1809 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1810 bt_put(returned_type
);
1812 int_16
= bt_ctf_field_create(int_16_type
);
1813 ok(int_16
, "Instanciate a signed 16-bit integer");
1814 uint_12
= bt_ctf_field_create(uint_12_type
);
1815 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1816 returned_type
= bt_ctf_field_get_type(int_16
);
1817 ok(returned_type
== int_16_type
,
1818 "bt_ctf_field_get_type returns the correct type");
1820 /* Can't modify types after instanciating them */
1821 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1822 BT_CTF_INTEGER_BASE_DECIMAL
),
1823 "Check an integer type' base can't be modified after instanciation");
1824 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1825 "Check an integer type's signedness can't be modified after instanciation");
1827 /* Check signed property is checked */
1828 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1829 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1830 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1831 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1833 /* Check overflows are properly tested for */
1834 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1835 "Check -32768 is allowed for a signed 16-bit integer");
1836 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1837 "Check 32767 is allowed for a signed 16-bit integer");
1838 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1839 "Check 32768 is not allowed for a signed 16-bit integer");
1840 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1841 "Check -32769 is not allowed for a signed 16-bit integer");
1842 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1843 "Check -42 is allowed for a signed 16-bit integer");
1845 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1846 "Check 4095 is allowed for an unsigned 12-bit integer");
1847 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1848 "Check 4096 is not allowed for a unsigned 12-bit integer");
1849 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1850 "Check 0 is allowed for an unsigned 12-bit integer");
1852 string
= bt_ctf_field_create(string_type
);
1853 ok(string
, "Instanciate a string field");
1854 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1855 "Set a string's value");
1857 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1858 ok(enumeration_type
,
1859 "Create an enumeration type with an unsigned 12-bit integer as container");
1860 enumeration
= bt_ctf_field_create(enumeration_type
);
1862 "Check enumeration types are validated before instantiation");
1867 bt_put(enumeration
);
1868 bt_put(composite_structure_type
);
1869 bt_put(structure_seq_type
);
1870 bt_put(string_type
);
1871 bt_put(sequence_type
);
1872 bt_put(uint_8_type
);
1873 bt_put(int_16_type
);
1874 bt_put(uint_12_type
);
1875 bt_put(enumeration_type
);
1876 bt_put(returned_type
);
1880 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1881 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1884 * Append enough events to force the underlying packet to be resized.
1885 * Also tests that a new event can be declared after a stream has been
1886 * instantiated and used/flushed.
1890 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1892 struct bt_ctf_field_type
*integer_type
=
1893 bt_ctf_field_type_integer_create(17);
1894 struct bt_ctf_field_type
*string_type
=
1895 bt_ctf_field_type_string_create();
1896 struct bt_ctf_event
*event
= NULL
;
1897 struct bt_ctf_field
*ret_field
= NULL
;
1898 struct bt_ctf_field_type
*ret_field_type
= NULL
;
1899 uint64_t ret_uint64
;
1900 int events_appended
= 0;
1901 struct bt_ctf_field
*packet_context
= NULL
,
1902 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
1903 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
1904 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
1905 struct bt_ctf_field_type
*ep_type
= NULL
;
1907 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1909 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1911 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1912 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1918 * bt_ctf_stream_class_add_event_class() copies the field types
1919 * of event_class, so we retrieve the new ones to create the
1920 * appropriate fields.
1922 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
1924 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1925 ep_type
, "field_1");
1926 assert(ep_field_1_type
);
1927 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1928 ep_type
, "a_string");
1929 assert(ep_a_string_type
);
1931 event
= bt_ctf_event_create(event_class
);
1932 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
1933 ret_field_type
= bt_ctf_field_get_type(ret_field
);
1934 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
1935 "bt_ctf_event_get_payload_by_index returns a correct field");
1936 bt_put(ret_field_type
);
1939 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
1940 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1941 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
1942 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1945 for (i
= 0; i
< packet_resize_test_length
; i
++) {
1946 event
= bt_ctf_event_create(event_class
);
1947 struct bt_ctf_field
*integer
=
1948 bt_ctf_field_create(ep_field_1_type
);
1949 struct bt_ctf_field
*string
=
1950 bt_ctf_field_create(ep_a_string_type
);
1952 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1953 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1954 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1957 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1958 ret
|= bt_ctf_event_set_payload(event
, "a_string",
1962 /* Populate stream event context */
1963 stream_event_context
=
1964 bt_ctf_event_get_stream_event_context(event
);
1965 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
1966 "common_event_context");
1967 BT_PUT(stream_event_context
);
1968 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
1972 ret
|= bt_ctf_stream_append_event(stream
, event
);
1980 events_appended
= !!(i
== packet_resize_test_length
);
1981 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
1982 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
1983 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
1984 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
1985 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
1986 ok(ret
== 0 && ret_uint64
== 0,
1987 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
1988 bt_ctf_stream_append_discarded_events(stream
, 1000);
1989 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
1990 ok(ret
== 0 && ret_uint64
== 1000,
1991 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
1994 ok(events_appended
, "Append 100 000 events to a stream");
1997 * Populate the custom packet context field with a dummy value
1998 * otherwise flush will fail.
2000 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2001 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2002 "custom_packet_context_field");
2003 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2005 ok(bt_ctf_stream_flush(stream
) == 0,
2006 "Flush a stream that forces a packet resize");
2007 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2008 ok(ret
== 0 && ret_uint64
== 1000,
2009 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2010 bt_put(integer_type
);
2011 bt_put(string_type
);
2012 bt_put(packet_context
);
2013 bt_put(packet_context_field
);
2014 bt_put(stream_event_context
);
2015 bt_put(event_class
);
2016 bt_put(ep_field_1_type
);
2017 bt_put(ep_a_string_type
);
2022 void test_empty_stream(struct bt_ctf_writer
*writer
)
2025 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2026 struct bt_ctf_stream_class
*stream_class
= NULL
;
2027 struct bt_ctf_stream
*stream
= NULL
;
2029 trace
= bt_ctf_writer_get_trace(writer
);
2031 diag("Failed to get trace from writer");
2036 stream_class
= bt_ctf_stream_class_create("empty_stream");
2037 if (!stream_class
) {
2038 diag("Failed to create stream class");
2043 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2045 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
2048 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2049 "bt_ctf_stream_class_get_trace handles NULL correctly");
2050 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2051 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2053 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2055 diag("Failed to create writer stream");
2060 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2061 ok(ret_trace
== trace
,
2062 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2065 "Created a stream class with default attributes and an empty stream");
2069 bt_put(stream_class
);
2073 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
,
2074 struct bt_ctf_clock
*clock
)
2077 struct bt_ctf_stream_class
*stream_class
= NULL
;
2078 struct bt_ctf_stream
*stream
= NULL
;
2079 struct bt_ctf_field_type
*integer_type
= NULL
,
2080 *sequence_type
= NULL
, *event_header_type
= NULL
;
2081 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2082 *event_header
= NULL
, *packet_header
= NULL
;
2083 struct bt_ctf_event_class
*event_class
= NULL
;
2084 struct bt_ctf_event
*event
= NULL
;
2086 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2087 if (!stream_class
) {
2088 fail("Failed to create stream class");
2092 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2094 fail("Failed to set stream class clock");
2099 * Customize event header to add an "seq_len" integer member
2100 * which will be used as the length of a sequence in an event of this
2103 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2105 if (!event_header_type
) {
2106 fail("Failed to get event header type");
2110 integer_type
= bt_ctf_field_type_integer_create(13);
2111 if (!integer_type
) {
2112 fail("Failed to create length integer type");
2116 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2117 integer_type
, "seq_len");
2119 fail("Failed to add a new field to stream event header");
2123 event_class
= bt_ctf_event_class_create("sequence_event");
2125 fail("Failed to create event class");
2130 * This event's payload will contain a sequence which references
2131 * stream.event.header.seq_len as its length field.
2133 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2134 "stream.event.header.seq_len");
2135 if (!sequence_type
) {
2136 fail("Failed to create a sequence");
2140 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2143 fail("Failed to add a sequence to an event class");
2147 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2149 fail("Failed to add event class to stream class");
2153 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2155 fail("Failed to create stream")
2160 * We have defined a custom packet header field. We have to populate it
2163 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2164 if (!packet_header
) {
2165 fail("Failed to get stream packet header");
2169 integer
= bt_ctf_field_structure_get_field(packet_header
,
2170 "custom_trace_packet_header_field");
2172 fail("Failed to retrieve custom_trace_packet_header_field");
2176 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2178 fail("Failed to set custom_trace_packet_header_field value");
2183 event
= bt_ctf_event_create(event_class
);
2185 fail("Failed to create event");
2189 event_header
= bt_ctf_event_get_header(event
);
2190 if (!event_header
) {
2191 fail("Failed to get event header");
2195 integer
= bt_ctf_field_structure_get_field(event_header
,
2198 fail("Failed to get seq_len field from event header");
2202 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2204 fail("Failed to set seq_len value in event header");
2208 /* Populate both sequence integer fields */
2209 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2211 fail("Failed to retrieve sequence from event");
2215 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2217 fail("Failed to set sequence length");
2222 for (i
= 0; i
< 2; i
++) {
2223 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2225 fail("Failed to retrieve sequence element");
2229 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2231 fail("Failed to set sequence element value");
2239 ret
= bt_ctf_stream_append_event(stream
, event
);
2241 fail("Failed to append event to stream");
2245 ret
= bt_ctf_stream_flush(stream
);
2247 fail("Failed to flush custom_event_header stream");
2251 bt_put(stream_class
);
2252 bt_put(event_class
);
2256 bt_put(event_header
);
2257 bt_put(packet_header
);
2258 bt_put(sequence_type
);
2259 bt_put(integer_type
);
2260 bt_put(event_header_type
);
2264 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
,
2265 struct bt_ctf_clock
*clock
)
2268 struct bt_ctf_stream_class
*stream_class
= NULL
;
2269 struct bt_ctf_stream
*stream
= NULL
,
2271 struct bt_ctf_event_class
*event_class
= NULL
;
2272 struct bt_ctf_event
*event
= NULL
;
2273 struct bt_ctf_field_type
*integer_type
= NULL
;
2274 struct bt_ctf_field
*integer
= NULL
;
2276 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2277 if (!stream_class
) {
2278 diag("Failed to create stream class");
2283 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2285 diag("Failed to set stream class clock");
2289 event_class
= bt_ctf_event_class_create("some_event_class_name");
2290 integer_type
= bt_ctf_field_type_integer_create(32);
2291 if (!integer_type
) {
2292 diag("Failed to create integer field type");
2297 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2300 diag("Failed to add field to event class");
2304 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2307 diag("Failed to add event class to stream class");
2310 event
= bt_ctf_event_create(event_class
);
2312 diag("Failed to create event");
2317 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2319 diag("Failed to get integer field payload from event");
2324 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2326 diag("Failed to set integer field value");
2330 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2332 diag("Failed to create writer stream");
2337 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2338 "bt_ctf_event_get_stream handles NULL correctly");
2339 ok(bt_ctf_event_get_stream(event
) == NULL
,
2340 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2342 ret
= bt_ctf_stream_append_event(stream
, event
);
2344 diag("Failed to append event to stream");
2348 ret_stream
= bt_ctf_event_get_stream(event
);
2349 ok(ret_stream
== stream
,
2350 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2353 "Create an event before instanciating its associated stream");
2356 bt_put(stream_class
);
2357 bt_put(event_class
);
2359 bt_put(integer_type
);
2364 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2366 struct bt_ctf_event_class
*event_class
;
2368 event_class
= bt_ctf_event_class_create("Simple Event");
2369 assert(event_class
);
2370 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
) == 0,
2371 "two event classes with the same name may cohabit within the same stream class");
2372 bt_put(event_class
);
2374 event_class
= bt_ctf_event_class_create("different name, ok");
2375 assert(event_class
);
2376 assert(!bt_ctf_event_class_set_id(event_class
, 13));
2377 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2378 "two event classes with the same ID cannot cohabit within the same stream class");
2379 bt_put(event_class
);
2383 struct bt_ctf_event_class
*create_minimal_event_class(void)
2385 struct bt_ctf_event_class
*ec
= NULL
;
2386 struct bt_ctf_field_type
*int_ft
= NULL
;
2389 int_ft
= bt_ctf_field_type_integer_create(23);
2391 ec
= bt_ctf_event_class_create("minimal");
2393 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2401 void test_create_writer_vs_non_writer_mode(void)
2405 const char *writer_stream_name
= "writer stream instance";
2406 struct bt_ctf_writer
*writer
= NULL
;
2407 struct bt_ctf_trace
*writer_trace
= NULL
;
2408 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2409 struct bt_ctf_stream
*writer_stream
= NULL
;
2410 struct bt_ctf_stream
*writer_stream2
= NULL
;
2411 struct bt_ctf_stream
*packet_stream
= NULL
;
2412 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2413 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2414 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2415 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2416 struct bt_ctf_event_class
*writer_ec
= NULL
;
2417 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2418 struct bt_ctf_event
*event
= NULL
;
2419 struct bt_ctf_event
*event2
= NULL
;
2420 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2421 struct bt_ctf_field
*int_field
= NULL
;
2422 struct bt_ctf_clock
*writer_clock
= NULL
;
2423 struct bt_ctf_clock_class
*non_writer_clock_class
= NULL
;
2424 struct bt_ctf_packet
*packet
= NULL
;
2425 struct bt_ctf_packet
*packet2
= NULL
;
2427 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
2428 if (!bt_mkdtemp(trace_path
)) {
2432 /* Create empty structure field type (event header) */
2433 empty_struct_ft
= bt_ctf_field_type_structure_create();
2434 assert(empty_struct_ft
);
2436 /* Create writer, writer stream class, stream, and clock */
2437 writer
= bt_ctf_writer_create(trace_path
);
2439 writer_clock
= bt_ctf_clock_create("writer_clock");
2440 assert(writer_clock
);
2441 ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
2443 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2445 writer_trace
= bt_ctf_writer_get_trace(writer
);
2446 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2447 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2449 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2452 ret
= bt_ctf_stream_class_set_clock(writer_sc
, writer_clock
);
2454 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2456 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2457 assert(writer_stream
);
2458 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2459 "bt_ctf_stream_get_name() returns the stream's name");
2461 /* Create non-writer trace, stream class, stream, and clock */
2462 non_writer_trace
= bt_ctf_trace_create();
2463 assert(non_writer_trace
);
2464 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2465 assert(non_writer_sc
);
2466 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2469 ret
= bt_ctf_stream_class_set_packet_context_type(non_writer_sc
, NULL
);
2471 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2473 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2474 assert(non_writer_stream
);
2475 non_writer_clock_class
=
2476 bt_ctf_clock_class_create("non_writer_clock_class",
2478 assert(non_writer_clock_class
);
2479 ret
= bt_ctf_trace_add_clock_class(non_writer_trace
,
2480 non_writer_clock_class
);
2483 /* Create event class and event */
2484 writer_ec
= create_minimal_event_class();
2486 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2488 event
= bt_ctf_event_create(writer_ec
);
2490 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2492 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2495 * Verify non-writer stream: it should be impossible to append
2498 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2499 "bt_ctf_stream_append_event() fails with a non-writer stream");
2502 * Verify writer stream: it should be possible to append an
2505 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2506 "bt_ctf_stream_append_event() succeeds with a writer stream");
2509 * It should be possible to create a packet from a non-writer
2510 * stream, but not from a writer stream.
2512 packet
= bt_ctf_packet_create(writer_stream
);
2513 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2514 packet
= bt_ctf_packet_create(non_writer_stream
);
2515 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2516 packet_stream
= bt_ctf_packet_get_stream(packet
);
2517 ok(packet_stream
== non_writer_stream
,
2518 "bt_ctf_packet_get_stream() returns the correct stream");
2521 * It should not be possible to append an event associated to
2522 * a stream to a different stream.
2524 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2525 assert(writer_stream2
);
2526 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2527 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2530 * It should not be possible to set the packet of an event
2531 * associated to a given stream to a packet associated with
2532 * a different stream.
2534 ok(bt_ctf_event_set_packet(event
, packet
),
2535 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2538 * It should be possible to set the packet of a fresh event, as
2539 * long as the originating stream classes are the same.
2541 event2
= bt_ctf_event_create(writer_ec
);
2543 ok(bt_ctf_event_set_packet(event2
, packet
),
2544 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2545 non_writer_ec
= create_minimal_event_class();
2546 assert(non_writer_ec
);
2547 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2550 event2
= bt_ctf_event_create(non_writer_ec
);
2552 ok(!bt_ctf_event_set_packet(event2
, packet
),
2553 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2556 * It should be possible to set a packet created from the same
2557 * stream to an event with an existing packet.
2559 packet2
= bt_ctf_packet_create(non_writer_stream
);
2561 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2562 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2566 * It should not be possible to set a packet created from a
2567 * different stream to an event with an existing packet.
2569 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2570 assert(non_writer_stream2
);
2571 packet2
= bt_ctf_packet_create(non_writer_stream
);
2573 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2574 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2577 bt_put(writer_trace
);
2579 bt_put(writer_stream
);
2580 bt_put(writer_stream2
);
2581 bt_put(non_writer_trace
);
2582 bt_put(non_writer_sc
);
2583 bt_put(non_writer_stream
);
2584 bt_put(non_writer_stream2
);
2585 bt_put(packet_stream
);
2587 bt_put(non_writer_ec
);
2591 bt_put(empty_struct_ft
);
2592 bt_put(writer_clock
);
2593 bt_put(non_writer_clock_class
);
2596 recursive_rmdir(trace_path
);
2601 void test_clock_utils(void)
2604 struct bt_ctf_clock
*clock
= NULL
;
2606 clock
= bt_ctf_clock_create("water");
2608 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2610 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2612 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2614 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2620 void test_set_clock_non_writer_stream_class(void)
2622 struct bt_ctf_clock
*clock
;
2623 struct bt_ctf_trace
*trace
;
2624 struct bt_ctf_stream_class
*sc
;
2627 clock
= bt_ctf_clock_create("the_clock");
2630 trace
= bt_ctf_trace_create();
2633 sc
= bt_ctf_stream_class_create(NULL
);
2636 ret
= bt_ctf_stream_class_set_clock(sc
, clock
);
2639 ret
= bt_ctf_trace_add_stream_class(trace
, sc
);
2641 "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock");
2649 void test_static_trace(void)
2651 struct bt_ctf_trace
*trace
;
2652 struct bt_ctf_stream_class
*stream_class
;
2653 struct bt_ctf_stream_class
*stream_class2
;
2654 struct bt_ctf_stream
*stream
;
2655 struct bt_ctf_clock_class
*clock_class
;
2658 trace
= bt_ctf_trace_create();
2660 stream_class
= bt_ctf_stream_class_create(NULL
);
2661 assert(stream_class
);
2662 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2664 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
2666 stream
= bt_ctf_stream_create(stream_class
, "hello");
2667 ok(stream
, "bt_ctf_stream_create() succeeds with a non-static trace");
2669 ok(!bt_ctf_trace_is_static(trace
),
2670 "bt_ctf_trace_is_static() returns the expected value");
2671 ok(bt_ctf_trace_set_is_static(trace
) == 0,
2672 "bt_ctf_trace_set_is_static() succeeds");
2673 ok(bt_ctf_trace_is_static(trace
),
2674 "bt_ctf_trace_is_static() returns the expected value");
2675 clock_class
= bt_ctf_clock_class_create("yes", 1000000000);
2676 assert(clock_class
);
2677 stream_class2
= bt_ctf_stream_class_create(NULL
);
2678 assert(stream_class2
);
2679 ok(bt_ctf_trace_add_stream_class(trace
, stream_class2
),
2680 "bt_ctf_trace_add_stream_class() fails with a static trace");
2681 ok(bt_ctf_trace_add_clock_class(trace
, clock_class
),
2682 "bt_ctf_trace_add_clock_class() fails with a static trace");
2683 ok(!bt_ctf_stream_create(stream_class
, "hello2"),
2684 "bt_ctf_stream_create() fails with a static trace");
2687 bt_put(stream_class
);
2688 bt_put(stream_class2
);
2689 bt_put(clock_class
);
2693 void trace_is_static_listener(struct bt_ctf_trace
*trace
, void *data
)
2695 *((int *) data
) |= 1;
2699 void trace_listener_removed(struct bt_ctf_trace
*trace
, void *data
)
2701 *((int *) data
) |= 2;
2705 void test_trace_is_static_listener(void)
2707 struct bt_ctf_trace
*trace
;
2718 trace
= bt_ctf_trace_create();
2720 ret
= bt_ctf_trace_add_is_static_listener(NULL
,
2721 trace_is_static_listener
, trace_listener_removed
, &called1
);
2722 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (trace)");
2723 ret
= bt_ctf_trace_add_is_static_listener(trace
, NULL
,
2724 trace_listener_removed
, &called1
);
2725 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (listener)");
2726 listener1_id
= bt_ctf_trace_add_is_static_listener(trace
,
2727 trace_is_static_listener
, trace_listener_removed
, &called1
);
2728 ok(listener1_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (1)");
2729 listener2_id
= bt_ctf_trace_add_is_static_listener(trace
,
2730 trace_is_static_listener
, trace_listener_removed
, &called2
);
2731 ok(listener2_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (2)");
2732 listener3_id
= bt_ctf_trace_add_is_static_listener(trace
,
2733 trace_is_static_listener
, trace_listener_removed
, &called3
);
2734 ok(listener3_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (3)");
2735 ret
= bt_ctf_trace_remove_is_static_listener(NULL
, 0);
2736 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles NULL (trace)");
2737 ret
= bt_ctf_trace_remove_is_static_listener(trace
, -2);
2738 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (negative)");
2739 ret
= bt_ctf_trace_remove_is_static_listener(trace
, 77);
2740 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (non existing)");
2741 ret
= bt_ctf_trace_remove_is_static_listener(trace
, listener2_id
);
2742 ok(ret
== 0, "bt_ctf_trace_remove_is_static_listener() succeeds");
2743 ok(called2
== 2, "bt_ctf_trace_remove_is_static_listener() calls the remove listener");
2744 listener4_id
= bt_ctf_trace_add_is_static_listener(trace
,
2745 trace_is_static_listener
, NULL
, &called4
);
2746 ok(listener4_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (4)");
2747 ok(called1
== 0, "\"trace is static\" listener not called before the trace is made static (1)");
2748 ok(called2
== 2, "\"trace is static\" listener not called before the trace is made static (2)");
2749 ok(called3
== 0, "\"trace is static\" listener not called before the trace is made static (3)");
2750 ok(called4
== 0, "\"trace is static\" listener not called before the trace is made static (4)");
2751 ret
= bt_ctf_trace_set_is_static(trace
);
2753 ret
= bt_ctf_trace_add_is_static_listener(trace
,
2754 trace_is_static_listener
, trace_listener_removed
, &called1
);
2756 "bt_ctf_trace_add_is_static_listener() fails when the trace is static");
2757 ok(called1
== 1, "\"trace is static\" listener called when the trace is made static (1)");
2758 ok(called2
== 2, "\"trace is static\" listener not called when the trace is made static (2)");
2759 ok(called3
== 1, "\"trace is static\" listener called when the trace is made static (3)");
2760 ok(called4
== 1, "\"trace is static\" listener called when the trace is made static (4)");
2766 ok(called1
== 2, "\"trace is static\" listener not called after the trace is put (1)");
2767 ok(called2
== 0, "\"trace is static\" listener not called after the trace is put (2)");
2768 ok(called3
== 2, "\"trace is static\" listener not called after the trace is put (3)");
2769 ok(called4
== 0, "\"trace is static\" listener not called after the trace is put (4)");
2773 void test_trace_uuid(void)
2775 struct bt_ctf_trace
*trace
;
2776 const unsigned char uuid
[] = {
2777 0x35, 0x92, 0x63, 0xab, 0xb4, 0xbe, 0x40, 0xb4,
2778 0xb2, 0x60, 0xd3, 0xf1, 0x3b, 0xb0, 0xd8, 0x59,
2780 const unsigned char *ret_uuid
;
2782 trace
= bt_ctf_trace_create();
2784 ok(!bt_ctf_trace_get_uuid(NULL
),
2785 "bt_ctf_trace_get_uuid() handles NULL");
2786 ok(!bt_ctf_trace_get_uuid(trace
),
2787 "bt_ctf_trace_get_uuid() returns NULL initially");
2788 ok(bt_ctf_trace_set_uuid(NULL
, uuid
),
2789 "bt_ctf_trace_set_uuid() handles NULL (trace)");
2790 ok(bt_ctf_trace_set_uuid(trace
, NULL
),
2791 "bt_ctf_trace_set_uuid() handles NULL (UUID)");
2792 ok(bt_ctf_trace_set_uuid(trace
, uuid
) == 0,
2793 "bt_ctf_trace_set_uuid() succeeds with a valid UUID");
2794 ret_uuid
= bt_ctf_trace_get_uuid(trace
);
2795 ok(ret_uuid
, "bt_ctf_trace_get_uuid() returns a UUID");
2797 ok(memcmp(uuid
, ret_uuid
, 16) == 0,
2798 "bt_ctf_trace_get_uuid() returns the expected UUID");
2803 int main(int argc
, char **argv
)
2805 const char *env_resize_length
;
2807 gchar
*metadata_path
;
2808 const char *clock_name
= "test_clock";
2809 const char *clock_description
= "This is a test clock";
2810 const char *returned_clock_name
;
2811 const char *returned_clock_description
;
2812 const uint64_t frequency
= 1123456789;
2813 const int64_t offset_s
= 13515309;
2814 const int64_t offset
= 1234567;
2815 int64_t get_offset_s
,
2817 const uint64_t precision
= 10;
2818 const int is_absolute
= 0xFF;
2819 char *metadata_string
;
2820 struct bt_ctf_writer
*writer
;
2821 struct bt_utsname name
= {"GNU/Linux", "testhost", "4.4.0-87-generic",
2822 "#110-Ubuntu SMP Tue Jul 18 12:55:35 UTC 2017", "x86_64"};
2823 struct bt_ctf_clock
*clock
, *ret_clock
;
2824 struct bt_ctf_clock_class
*ret_clock_class
;
2825 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2826 struct bt_ctf_stream
*stream1
;
2827 struct bt_ctf_stream
*stream
;
2828 const char *ret_string
;
2829 const unsigned char *ret_uuid
;
2830 unsigned char tmp_uuid
[16] = { 0 };
2831 struct bt_ctf_field_type
*packet_context_type
,
2832 *packet_context_field_type
,
2833 *packet_header_type
,
2834 *packet_header_field_type
,
2836 *stream_event_context_type
,
2838 *event_header_field_type
;
2839 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2840 struct bt_ctf_trace
*trace
;
2842 int64_t ret_int64_t
;
2843 struct bt_value
*obj
;
2846 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
2850 env_resize_length
= getenv("PACKET_RESIZE_TEST_LENGTH");
2851 if (env_resize_length
) {
2852 packet_resize_test_length
=
2853 (unsigned int) atoi(env_resize_length
);
2856 plan_tests(NR_TESTS
);
2858 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
2859 if (!bt_mkdtemp(trace_path
)) {
2863 metadata_path
= g_build_filename(trace_path
, "metadata", NULL
);
2865 writer
= bt_ctf_writer_create(trace_path
);
2866 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2868 ok(!bt_ctf_writer_get_trace(NULL
),
2869 "bt_ctf_writer_get_trace correctly handles NULL");
2870 trace
= bt_ctf_writer_get_trace(writer
);
2871 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
),
2872 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_NATIVE");
2873 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_UNSPECIFIED
),
2874 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_UNSPECIFIED");
2876 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2877 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2878 "Set a trace's byte order to big endian");
2879 ok(bt_ctf_trace_get_native_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2880 "bt_ctf_trace_get_native_byte_order returns a correct endianness");
2882 /* Add environment context to the trace */
2883 ok(bt_ctf_writer_add_environment_field(writer
, "host", name
.nodename
) == 0,
2884 "Add host (%s) environment field to writer instance",
2886 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2888 "bt_ctf_writer_add_environment_field error with NULL writer");
2889 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2891 "bt_ctf_writer_add_environment_field error with NULL field name");
2892 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2894 "bt_ctf_writer_add_environment_field error with NULL field value");
2896 /* Test bt_ctf_trace_set_environment_field with an integer object */
2897 obj
= bt_value_integer_create_init(23);
2899 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2900 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2901 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2902 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2903 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2904 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2905 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2906 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2909 /* Test bt_ctf_trace_set_environment_field with a string object */
2910 obj
= bt_value_string_create_init("the value");
2912 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2913 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2916 /* Test bt_ctf_trace_set_environment_field_integer */
2917 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2919 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2920 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2921 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2922 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2924 "bt_ctf_trace_set_environment_field_integer succeeds");
2926 /* Test bt_ctf_trace_set_environment_field_string */
2927 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2929 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2930 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2931 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2932 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2934 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2935 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2937 "bt_ctf_trace_set_environment_field_string succeeds");
2939 /* Test bt_ctf_trace_get_environment_field_count */
2940 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2941 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2942 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2943 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2945 /* Test bt_ctf_trace_get_environment_field_name */
2946 ok(bt_ctf_trace_get_environment_field_name_by_index(NULL
, 0) == NULL
,
2947 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2948 ok(bt_ctf_trace_get_environment_field_name_by_index(trace
, 5) == NULL
,
2949 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2950 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 0);
2951 ok(ret_string
&& !strcmp(ret_string
, "host"),
2952 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2953 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 1);
2954 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2955 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2956 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 2);
2957 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2958 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2959 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 3);
2960 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2961 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2962 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 4);
2963 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2964 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2966 /* Test bt_ctf_trace_get_environment_field_value */
2967 ok(bt_ctf_trace_get_environment_field_value_by_index(NULL
, 0) == NULL
,
2968 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2969 ok(bt_ctf_trace_get_environment_field_value_by_index(trace
, 5) == NULL
,
2970 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2971 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 1);
2972 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2973 ok(!ret
&& ret_int64_t
== 23,
2974 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2976 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 2);
2977 ret
= bt_value_string_get(obj
, &ret_string
);
2978 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2979 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2982 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2983 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2985 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2986 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
2987 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2988 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
2989 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2990 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
2992 ret
= bt_value_string_get(obj
, &ret_string
);
2993 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
2994 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2997 /* Test environment field replacement */
2998 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3000 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3001 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3002 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3003 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 3);
3004 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3005 ok(!ret
&& ret_int64_t
== 654321,
3006 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3009 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3010 == 0, "Add sysname (%s) environment field to writer instance",
3012 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3013 name
.nodename
) == 0,
3014 "Add nodename (%s) environment field to writer instance",
3016 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3017 == 0, "Add release (%s) environment field to writer instance",
3019 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3020 == 0, "Add version (%s) environment field to writer instance",
3022 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3023 == 0, "Add machine (%s) environment field to writer istance",
3026 /* Define a clock and add it to the trace */
3027 ok(bt_ctf_clock_create("signed") == NULL
,
3028 "Illegal clock name rejected");
3029 clock
= bt_ctf_clock_create(clock_name
);
3030 ok(clock
, "Clock created sucessfully");
3031 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3032 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3033 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3034 "Returned clock name is valid");
3036 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3037 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3038 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3039 "Clock description set successfully");
3041 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3042 ok(returned_clock_description
,
3043 "bt_ctf_clock_get_description returns a description.");
3044 ok(returned_clock_description
?
3045 !strcmp(returned_clock_description
, clock_description
) : 0,
3046 "Returned clock description is valid");
3048 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3049 "bt_ctf_clock_get_frequency returns the correct default frequency");
3050 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3051 "Set clock frequency");
3052 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3053 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3055 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3056 "bt_ctf_clock_get_offset_s succeeds");
3057 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3058 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3059 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3060 "Set clock offset (seconds)");
3061 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3062 "bt_ctf_clock_get_offset_s succeeds");
3063 ok(get_offset_s
== offset_s
,
3064 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3066 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3067 "bt_ctf_clock_get_offset succeeds");
3068 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3069 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3070 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3071 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3072 "bt_ctf_clock_get_offset succeeds");
3073 ok(get_offset
== offset
,
3074 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3076 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3077 "bt_ctf_clock_get_precision returns the correct default precision");
3078 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3079 "Set clock precision");
3080 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3081 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3083 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3084 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3085 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3086 "Set clock absolute property");
3087 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3088 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3090 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3093 ok(!bt_ctf_clock_get_name(NULL
),
3094 "bt_ctf_clock_get_name correctly handles NULL");
3095 ok(!bt_ctf_clock_get_description(NULL
),
3096 "bt_ctf_clock_get_description correctly handles NULL");
3097 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3098 "bt_ctf_clock_get_frequency correctly handles NULL");
3099 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3100 "bt_ctf_clock_get_precision correctly handles NULL");
3101 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3102 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3103 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3104 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3105 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3106 "bt_ctf_clock_get_offset correctly handles NULL clock");
3107 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3108 "bt_ctf_clock_get_offset correctly handles NULL output");
3109 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3110 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3112 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3113 "bt_ctf_clock_set_description correctly handles NULL clock");
3114 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3115 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3116 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3117 "bt_ctf_clock_get_precision correctly handles NULL clock");
3118 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3119 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3120 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3121 "bt_ctf_clock_set_offset correctly handles NULL clock");
3122 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3123 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3124 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3125 "bt_ctf_clock_set_time correctly handles NULL clock");
3126 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3127 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3128 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3130 "bt_ctf_clock_get_uuid returns a UUID");
3132 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3133 /* Slightly modify UUID */
3134 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3137 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3138 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3139 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3140 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3141 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3142 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3143 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3145 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3146 ok(uuid_match(ret_uuid
, tmp_uuid
),
3147 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3149 /* Define a stream class */
3150 stream_class
= bt_ctf_stream_class_create("test_stream");
3152 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3153 "bt_ctf_stream_class_get_name handles NULL correctly");
3154 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3155 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3156 "bt_ctf_stream_class_get_name returns a correct stream class name");
3158 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3159 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3160 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3161 "bt_ctf_stream_class_get_clock handles NULL correctly");
3163 ok(stream_class
, "Create stream class");
3164 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3165 "Set a stream class' clock");
3166 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3167 ok(ret_clock
== clock
,
3168 "bt_ctf_stream_class_get_clock returns a correct clock");
3171 /* Test the event fields and event types APIs */
3174 /* Test fields copying */
3177 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3178 "bt_ctf_stream_class_get_id returns an error when no id is set");
3179 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3180 "bt_ctf_stream_class_get_id handles NULL correctly");
3181 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3182 "bt_ctf_stream_class_set_id handles NULL correctly");
3183 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3184 "Set an stream class' id");
3185 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3186 "bt_ctf_stream_class_get_id returns the correct value");
3188 /* Validate default event header fields */
3189 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3190 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3191 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3194 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3195 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3196 "Default event header type is a structure");
3197 event_header_field_type
=
3198 bt_ctf_field_type_structure_get_field_type_by_name(
3199 ret_field_type
, "id");
3200 ok(event_header_field_type
,
3201 "Default event header type contains an \"id\" field");
3202 ok(bt_ctf_field_type_get_type_id(
3203 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3204 "Default event header \"id\" field is an integer");
3205 bt_put(event_header_field_type
);
3206 event_header_field_type
=
3207 bt_ctf_field_type_structure_get_field_type_by_name(
3208 ret_field_type
, "timestamp");
3209 ok(event_header_field_type
,
3210 "Default event header type contains a \"timestamp\" field");
3211 ok(bt_ctf_field_type_get_type_id(
3212 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3213 "Default event header \"timestamp\" field is an integer");
3214 bt_put(event_header_field_type
);
3215 bt_put(ret_field_type
);
3217 /* Add a custom trace packet header field */
3218 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3219 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3220 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3221 ok(packet_header_type
,
3222 "bt_ctf_trace_get_packet_header_type returns a packet header");
3223 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3224 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3225 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3226 packet_header_type
, "magic");
3227 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3228 bt_put(ret_field_type
);
3229 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3230 packet_header_type
, "uuid");
3231 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3232 bt_put(ret_field_type
);
3233 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3234 packet_header_type
, "stream_id");
3235 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3236 bt_put(ret_field_type
);
3238 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3239 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3240 packet_header_field_type
, "custom_trace_packet_header_field"),
3241 "Added a custom trace packet header field successfully");
3243 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3244 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3245 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3246 "Set a trace packet_header_type successfully");
3248 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3249 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3251 /* Add a custom field to the stream class' packet context */
3252 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3253 ok(packet_context_type
,
3254 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3255 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3256 "Packet context is a structure");
3258 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3259 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3261 integer_type
= bt_ctf_field_type_integer_create(32);
3262 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3264 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3265 /* Create a "uint5_t" equivalent custom packet context field */
3266 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3268 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3269 packet_context_field_type
, "custom_packet_context_field");
3270 ok(ret
== 0, "Packet context field added successfully");
3272 /* Define a stream event context containing a my_integer field. */
3273 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3274 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3275 stream_event_context_type
= bt_ctf_field_type_structure_create();
3276 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3277 integer_type
, "common_event_context");
3279 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3280 stream_event_context_type
) < 0,
3281 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3282 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3284 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3286 ok(bt_ctf_stream_class_set_event_context_type(
3287 stream_class
, stream_event_context_type
) == 0,
3288 "Set a new stream event context type");
3289 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3291 ok(ret_field_type
== stream_event_context_type
,
3292 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3293 bt_put(ret_field_type
);
3295 /* Instantiate a stream and append events */
3296 ret
= bt_ctf_writer_add_clock(writer
, clock
);
3298 ok(bt_ctf_trace_get_stream_count(trace
) == 0,
3299 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)");
3300 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3301 ok(stream1
, "Instanciate a stream class from writer");
3302 ok(bt_ctf_trace_get_stream_count(trace
) == 1,
3303 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)");
3304 stream
= bt_ctf_trace_get_stream_by_index(trace
, 0);
3305 ok(stream
== stream1
,
3306 "bt_ctf_trace_get_stream_by_index() succeeds and returns the correct value");
3310 * Creating a stream through a writer adds the given stream
3311 * class to the writer's trace, thus registering the stream
3312 * class's clock to the trace.
3314 ok(bt_ctf_trace_get_clock_class_count(NULL
) < 0,
3315 "bt_ctf_trace_get_clock_class_count correctly handles NULL");
3316 ok(bt_ctf_trace_get_clock_class_count(trace
) == 1,
3317 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
3318 ok(!bt_ctf_trace_get_clock_class_by_index(NULL
, 0),
3319 "bt_ctf_trace_get_clock_class correctly handles NULL");
3320 ok(!bt_ctf_trace_get_clock_class_by_index(trace
, 1),
3321 "bt_ctf_trace_get_clock_class correctly handles out of bound accesses");
3322 ret_clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
3323 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3324 bt_ctf_clock_get_name(clock
)) == 0,
3325 "bt_ctf_trace_get_clock_class returns the right clock instance");
3326 bt_put(ret_clock_class
);
3327 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, NULL
),
3328 "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)");
3329 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, clock_name
),
3330 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3331 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, NULL
),
3332 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3333 ret_clock_class
= bt_ctf_trace_get_clock_class_by_name(trace
, clock_name
);
3334 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3335 bt_ctf_clock_get_name(clock
)) == 0,
3336 "bt_ctf_trace_get_clock_class returns the right clock instance");
3337 bt_put(ret_clock_class
);
3338 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, "random"),
3339 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3341 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3342 "bt_ctf_stream_get_class correctly handles NULL");
3343 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3344 ok(ret_stream_class
,
3345 "bt_ctf_stream_get_class returns a stream class");
3346 ok(ret_stream_class
== stream_class
,
3347 "Returned stream class is of the correct type");
3350 * Packet header, packet context, event header, and stream
3351 * event context types were copied for the resolving
3354 BT_PUT(packet_header_type
);
3355 BT_PUT(packet_context_type
);
3356 BT_PUT(stream_event_context_type
);
3357 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3358 assert(packet_header_type
);
3359 packet_context_type
=
3360 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3361 assert(packet_context_type
);
3362 stream_event_context_type
=
3363 bt_ctf_stream_class_get_event_context_type(stream_class
);
3364 assert(stream_event_context_type
);
3367 * Try to modify the packet context type after a stream has been
3370 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3371 packet_header_field_type
, "should_fail");
3373 "Trace packet header type can't be modified once a stream has been instanciated");
3376 * Try to modify the packet context type after a stream has been
3379 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3380 packet_context_field_type
, "should_fail");
3382 "Packet context type can't be modified once a stream has been instanciated");
3385 * Try to modify the stream event context type after a stream has been
3388 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3389 integer_type
, "should_fail");
3391 "Stream event context type can't be modified once a stream has been instanciated");
3393 /* Should fail after instanciating a stream (frozen) */
3394 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3395 "Changes to a stream class that was already instantiated fail");
3397 /* Populate the custom packet header field only once for all tests */
3398 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3399 "bt_ctf_stream_get_packet_header handles NULL correctly");
3400 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3402 "bt_ctf_stream_get_packet_header returns a packet header");
3403 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3404 ok(ret_field_type
== packet_header_type
,
3405 "Stream returns a packet header of the appropriate type");
3406 bt_put(ret_field_type
);
3407 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3408 "custom_trace_packet_header_field");
3409 ok(packet_header_field
,
3410 "Packet header structure contains a custom field with the appropriate name");
3411 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3412 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3413 "Custom packet header field is of the expected type");
3414 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3415 54321), "Set custom packet header value successfully");
3416 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3417 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3418 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3419 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3420 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3421 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3422 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3423 "Successfully set a stream's packet header");
3425 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3426 "Add environment field to writer after stream creation");
3430 test_create_writer_vs_non_writer_mode();
3432 test_set_clock_non_writer_stream_class();
3434 test_instanciate_event_before_stream(writer
, clock
);
3436 append_simple_event(stream_class
, stream1
, clock
);
3438 packet_resize_test(stream_class
, stream1
, clock
);
3440 append_complex_event(stream_class
, stream1
, clock
);
3442 append_existing_event_class(stream_class
);
3444 test_empty_stream(writer
);
3446 test_custom_event_header_stream(writer
, clock
);
3448 test_static_trace();
3450 test_trace_is_static_listener();
3454 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3455 ok(metadata_string
, "Get metadata string");
3457 bt_ctf_writer_flush_metadata(writer
);
3460 bt_put(ret_stream_class
);
3463 bt_put(packet_context_type
);
3464 bt_put(packet_context_field_type
);
3465 bt_put(integer_type
);
3466 bt_put(stream_event_context_type
);
3467 bt_put(ret_field_type
);
3468 bt_put(packet_header_type
);
3469 bt_put(packet_header_field_type
);
3470 bt_put(packet_header
);
3471 bt_put(packet_header_field
);
3473 free(metadata_string
);
3474 bt_put(stream_class
);
3476 validate_trace(argv
[1], trace_path
);
3478 recursive_rmdir(trace_path
);
3480 g_free(metadata_path
);