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 ret
= bt_ctf_field_type_enumeration_mapping_iterator_next(iter
);
251 ok(iter
&& ret
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values");
254 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, 3);
255 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value");
256 ret
= bt_ctf_field_type_enumeration_mapping_iterator_next(iter
);
257 ok(!ret
&& bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, NULL
, NULL
, NULL
) == 0,
258 "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly");
261 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
262 "enum_field") == 0, "Add signed enumeration field to event");
264 ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL
, 0, &ret_char
,
265 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
266 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly");
267 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, NULL
,
268 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
269 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly");
270 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
271 NULL
, &ret_range_end_int64_t
) == 0,
272 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly");
273 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
274 &ret_range_start_int64_t
, NULL
) == 0,
275 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly");
276 /* Assumes entries are sorted by range_start values. */
277 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 6, &ret_char
,
278 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
279 "bt_ctf_field_type_enumeration_get_mapping_signed returns a value");
280 ok(!strcmp(ret_char
, mapping_name_test
),
281 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name");
282 ok(ret_range_start_int64_t
== 42,
283 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start");
284 ok(ret_range_end_int64_t
== 42,
285 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end");
287 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
288 "escaping; \"test\"", 0, 0) == 0,
289 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
290 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
291 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
292 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
293 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
294 "event clock int float", 5, 22) == 0,
295 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
296 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
297 42, 42) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts single-value ranges");
298 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
299 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names");
300 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
301 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries");
302 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
303 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
304 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
305 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
307 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
308 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
309 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 6,
310 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
312 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
313 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
314 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
315 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
316 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
317 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
318 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
319 NULL
, &ret_range_end_uint64_t
) == 0,
320 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
321 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
322 &ret_range_start_uint64_t
, NULL
) == 0,
323 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
324 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 4, &ret_char
,
325 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
326 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
327 ok(!strcmp(ret_char
, mapping_name_test
),
328 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
329 ok(ret_range_start_uint64_t
== 42,
330 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
331 ok(ret_range_end_uint64_t
== 42,
332 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
334 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
336 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
339 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
341 /* Set an event context type which will contain a single integer. */
342 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
343 "event_specific_context"),
344 "Add event specific context field");
345 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
346 "bt_ctf_event_class_get_context_type handles NULL correctly");
348 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
349 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
350 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
351 "Set an event class' context type successfully");
352 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
353 ok(returned_type
== event_context_type
,
354 "bt_ctf_event_class_get_context_type returns the appropriate type");
355 bt_put(returned_type
);
357 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
358 "Adding simple event class to stream class");
361 * bt_ctf_stream_class_add_event_class() copies the field types
362 * of simple_event_class, so we retrieve the new ones to create
363 * the appropriate fields.
365 BT_PUT(event_context_type
);
366 BT_PUT(event_payload_type
);
367 event_payload_type
= bt_ctf_event_class_get_payload_type(
369 assert(event_payload_type
);
370 event_context_type
= bt_ctf_event_class_get_context_type(
372 assert(event_context_type
);
373 ep_integer_field_type
=
374 bt_ctf_field_type_structure_get_field_type_by_name(
375 event_payload_type
, "integer_field");
376 assert(ep_integer_field_type
);
378 bt_ctf_field_type_structure_get_field_type_by_name(
379 event_payload_type
, "enum_field");
380 assert(ep_enum_field_type
);
381 ep_enum_field_unsigned_type
=
382 bt_ctf_field_type_structure_get_field_type_by_name(
383 event_payload_type
, "enum_field_unsigned");
384 assert(ep_enum_field_unsigned_type
);
386 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
387 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
388 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
389 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
390 ok(bt_ctf_stream_class_get_event_class_by_index(NULL
, 0) == NULL
,
391 "bt_ctf_stream_class_get_event_class handles NULL correctly");
392 ok(bt_ctf_stream_class_get_event_class_by_index(stream_class
, 8724) == NULL
,
393 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
394 ret_event_class
= bt_ctf_stream_class_get_event_class_by_index(stream_class
, 0);
395 ok(ret_event_class
== simple_event_class
,
396 "bt_ctf_stream_class_get_event_class returns the correct event class");
397 bt_put(ret_event_class
);
398 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
399 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
400 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
401 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
403 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
404 ok(ret_event_class
== simple_event_class
,
405 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
406 bt_put(ret_event_class
);
408 simple_event
= bt_ctf_event_create(simple_event_class
);
410 "Instantiate an event containing a single integer field");
412 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
413 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
414 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
415 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
417 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
418 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
419 "bt_ctf_field_floating_point_get_value fails on an unset float field");
420 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
421 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
422 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
423 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
424 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
425 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
426 "bt_ctf_field_floating_point_get_value returns a double value");
427 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
428 "bt_ctf_field_floating_point_get_value returns a correct value");
430 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
433 iter
= bt_ctf_field_enumeration_get_mappings(NULL
);
434 ok(!iter
, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
435 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
436 ok(!iter
, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
437 enum_container_field
= bt_ctf_field_enumeration_get_container(
439 ok(bt_ctf_field_signed_integer_set_value(
440 enum_container_field
, -42) == 0,
441 "Set signed enumeration container value");
442 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
443 ok(iter
, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
444 ret
= bt_ctf_field_type_enumeration_mapping_iterator_next(iter
);
445 ok(!ret
, "bt_ctf_field_enumeration_get_mappings returned a non-empty match");
446 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, &ret_char
, NULL
, NULL
);
447 ok(!ret
&& ret_char
, "bt_ctf_field_type_enumeration_mapping_iterator_get_signed return a mapping name");
449 ok(!strcmp(ret_char
, mapping_name_negative_test
),
450 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
451 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
455 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
456 assert(enum_field_unsigned
);
457 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
458 enum_field_unsigned
);
459 ok(bt_ctf_field_unsigned_integer_set_value(
460 enum_container_field_unsigned
, 42) == 0,
461 "Set unsigned enumeration container value");
462 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
463 enum_field_unsigned
);
465 iter
= bt_ctf_field_enumeration_get_mappings(enum_field_unsigned
);
467 ret
= bt_ctf_field_type_enumeration_mapping_iterator_next(iter
);
469 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, &ret_char
, NULL
, NULL
);
470 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
471 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container");
473 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
475 /* Populate stream event context */
476 stream_event_context
=
477 bt_ctf_event_get_stream_event_context(simple_event
);
478 assert(stream_event_context
);
479 stream_event_context_field
= bt_ctf_field_structure_get_field(
480 stream_event_context
, "common_event_context");
481 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
483 /* Populate the event's context */
484 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
485 "bt_ctf_event_get_event_context handles NULL correctly");
486 event_context
= bt_ctf_event_get_event_context(simple_event
);
488 "bt_ctf_event_get_event_context returns a field");
489 returned_type
= bt_ctf_field_get_type(event_context
);
490 ok(returned_type
== event_context_type
,
491 "bt_ctf_event_get_event_context returns a field of the appropriate type");
492 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
493 "event_specific_context");
494 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
495 "Successfully set an event context's value");
496 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
497 "bt_ctf_event_set_event_context handles a NULL event correctly");
498 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
499 "bt_ctf_event_set_event_context handles a NULL event context correctly");
500 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
501 "bt_ctf_event_set_event_context rejects a context of the wrong type");
502 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
503 "Set an event context successfully");
505 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
506 "Append simple event to trace stream");
508 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
509 "bt_ctf_stream_get_packet_context handles NULL correctly");
510 packet_context
= bt_ctf_stream_get_packet_context(stream
);
512 "bt_ctf_stream_get_packet_context returns a packet context");
514 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
516 ok(packet_context_field
,
517 "Packet context contains the default packet_size field.");
518 bt_put(packet_context_field
);
519 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
520 "custom_packet_context_field");
521 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
522 "Custom packet context field value successfully set.");
524 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
525 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
526 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
527 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
528 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
529 "Successfully set a stream's packet context");
531 ok(bt_ctf_stream_flush(stream
) == 0,
532 "Flush trace stream with one event");
534 bt_put(simple_event_class
);
535 bt_put(simple_event
);
536 bt_put(uint_12_type
);
540 bt_put(enum_type_unsigned
);
541 bt_put(returned_type
);
542 bt_put(event_context_type
);
543 bt_put(integer_field
);
546 bt_put(enum_field_unsigned
);
547 bt_put(enum_container_field
);
548 bt_put(enum_container_field_unsigned
);
549 bt_put(packet_context
);
550 bt_put(packet_context_field
);
551 bt_put(stream_event_context
);
552 bt_put(stream_event_context_field
);
553 bt_put(event_context
);
554 bt_put(event_context_field
);
555 bt_put(event_payload_type
);
556 bt_put(ep_integer_field_type
);
557 bt_put(ep_enum_field_type
);
558 bt_put(ep_enum_field_unsigned_type
);
563 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
564 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
567 struct event_class_attrs_counts
;
568 const char *complex_test_event_string
= "Complex Test Event";
569 const char *test_string_1
= "Test ";
570 const char *test_string_2
= "string ";
571 const char *test_string_3
= "abcdefghi";
572 const char *test_string_4
= "abcd\0efg\0hi";
573 const char *test_string_cat
= "Test string abcdeabcd";
574 struct bt_ctf_field_type
*uint_35_type
=
575 bt_ctf_field_type_integer_create(35);
576 struct bt_ctf_field_type
*int_16_type
=
577 bt_ctf_field_type_integer_create(16);
578 struct bt_ctf_field_type
*uint_3_type
=
579 bt_ctf_field_type_integer_create(3);
580 struct bt_ctf_field_type
*enum_variant_type
=
581 bt_ctf_field_type_enumeration_create(uint_3_type
);
582 struct bt_ctf_field_type
*variant_type
=
583 bt_ctf_field_type_variant_create(enum_variant_type
,
585 struct bt_ctf_field_type
*string_type
=
586 bt_ctf_field_type_string_create();
587 struct bt_ctf_field_type
*sequence_type
;
588 struct bt_ctf_field_type
*array_type
;
589 struct bt_ctf_field_type
*inner_structure_type
=
590 bt_ctf_field_type_structure_create();
591 struct bt_ctf_field_type
*complex_structure_type
=
592 bt_ctf_field_type_structure_create();
593 struct bt_ctf_field_type
*ret_field_type
;
594 struct bt_ctf_event_class
*event_class
;
595 struct bt_ctf_event
*event
;
596 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
597 *inner_structure_field
, *complex_structure_field
,
598 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
599 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
600 *stream_event_ctx_int_field
, *ret_field
;
601 uint64_t ret_unsigned_int
;
602 int64_t ret_signed_int
;
603 const char *ret_string
;
604 struct bt_ctf_stream_class
*ret_stream_class
;
605 struct bt_ctf_event_class
*ret_event_class
;
606 struct bt_ctf_field
*packet_context
, *packet_context_field
;
607 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
609 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
610 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
611 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
612 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
613 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
614 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
615 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
616 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
617 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
618 "Set alignment of signed 16 bit integer to 32");
619 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
620 "Set integer signedness to true");
621 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
622 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
623 "Set signed 16 bit integer base to hexadecimal");
625 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
626 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
629 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
630 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
631 ret_field_type
= bt_ctf_field_type_array_get_element_type(
633 ok(ret_field_type
== int_16_type
,
634 "bt_ctf_field_type_array_get_element_type returns the correct type");
635 bt_put(ret_field_type
);
637 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
638 "bt_ctf_field_type_array_get_length handles NULL correctly");
639 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
640 "bt_ctf_field_type_array_get_length returns the correct length");
642 ok(bt_ctf_field_type_structure_add_field(inner_structure_type
,
643 inner_structure_type
, "yes"), "Cannot add self to structure");
644 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
645 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
646 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
647 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
648 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
649 array_type
, "an_array"), "Add an_array field to inner structure");
651 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
653 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
655 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
656 "UINT35_TYPE", 2, 7);
658 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(NULL
, "INT16_TYPE");
659 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly");
661 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, "INT16_TYPE");
662 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name returns a non-NULL iterator");
663 ret
= bt_ctf_field_type_enumeration_mapping_iterator_next(iter
);
664 ok(!ret
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly");
665 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
666 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly");
669 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, NULL
);
670 ret
= bt_ctf_field_type_enumeration_mapping_iterator_next(iter
);
671 ok(iter
&& ret
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly");
674 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL
, 1);
675 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly");
677 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, -42);
678 ret
= bt_ctf_field_type_enumeration_mapping_iterator_next(iter
);
679 ok(iter
&& ret
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly");
680 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) != 0,
681 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly");
684 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, 5);
685 ret
= bt_ctf_field_type_enumeration_mapping_iterator_next(iter
);
686 ok(iter
!= NULL
&& !ret
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly");
687 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
688 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly");
691 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
692 "An unknown entry"), "Reject a variant field based on an unknown tag value");
693 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
694 "UINT3_TYPE") == 0, "Add a field to a variant");
695 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
696 "INT16_TYPE"), "Add INT16_TYPE field to variant");
697 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
698 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
700 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
701 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
702 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
703 ok(ret_field_type
== enum_variant_type
,
704 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
705 bt_put(ret_field_type
);
707 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
708 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
709 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
710 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
711 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
712 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
713 "INT16_TYPE") == NULL
,
714 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
715 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
717 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
718 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
719 variant_type
, "INT16_TYPE");
720 ok(ret_field_type
== int_16_type
,
721 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
722 bt_put(ret_field_type
);
724 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
725 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
726 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
727 "bt_ctf_field_type_variant_get_field_count returns the correct count");
729 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
730 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
731 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
732 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
733 bt_put(ret_field_type
);
734 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
735 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
736 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
737 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
738 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
739 "bt_ctf_field_type_variant_get_field returns a field");
740 ok(!strcmp("INT16_TYPE", ret_string
),
741 "bt_ctf_field_type_variant_get_field returns a correct field name");
742 ok(ret_field_type
== int_16_type
,
743 "bt_ctf_field_type_variant_get_field returns a correct field type");
744 bt_put(ret_field_type
);
746 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
747 enum_variant_type
, "variant_selector"),
748 "Add variant_selector field to complex structure");
749 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
750 string_type
, "a_string"), "Add a_string field to complex structure");
751 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
752 variant_type
, "variant_value"),
753 "Add variant_value field to complex structure");
754 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
755 inner_structure_type
, "inner_structure"),
756 "Add inner_structure field to complex structure");
758 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
759 ok(event_class
, "Create an event class");
760 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
761 "Reject addition of a field with an empty name to an event");
762 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
763 "Reject addition of a field with a NULL type to an event");
764 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
766 "Reject addition of a type with an illegal name to an event");
767 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
769 "Add field of type unsigned integer to an event");
770 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
771 "int_16") == 0, "Add field of type signed integer to an event");
772 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
773 "complex_structure") == 0,
774 "Add composite structure to an event");
776 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
777 "bt_ctf_event_class_get_name handles NULL correctly");
778 ret_string
= bt_ctf_event_class_get_name(event_class
);
779 ok(!strcmp(ret_string
, complex_test_event_string
),
780 "bt_ctf_event_class_get_name returns a correct name");
781 ok(bt_ctf_event_class_get_id(event_class
) < 0,
782 "bt_ctf_event_class_get_id returns a negative value when not set");
783 ok(bt_ctf_event_class_get_id(NULL
) < 0,
784 "bt_ctf_event_class_get_id handles NULL correctly");
785 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
786 "bt_ctf_event_class_set_id handles NULL correctly");
787 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
788 "Set an event class' id");
789 ok(bt_ctf_event_class_get_id(event_class
) == 42,
790 "bt_ctf_event_class_get_id returns the correct value");
792 /* Test event class attributes */
793 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
,
794 "event class has the expected initial log level");
795 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
796 "as expected, event class has no initial EMF URI");
797 ok(bt_ctf_event_class_set_log_level(NULL
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
798 "bt_ctf_event_class_set_log_level handles a NULL event class correctly");
799 ok(bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
),
800 "bt_ctf_event_class_set_log_level handles an unknown log level correctly");
801 ok(!bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
802 "bt_ctf_event_class_set_log_level succeeds with a valid log level");
803 ok(bt_ctf_event_class_get_log_level(NULL
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
,
804 "bt_ctf_event_class_get_log_level handles a NULL event class correctly");
805 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
,
806 "bt_ctf_event_class_get_log_level returns the expected log level");
807 ok(bt_ctf_event_class_set_emf_uri(NULL
, "http://diamon.org/babeltrace/"),
808 "bt_ctf_event_class_set_emf_uri handles a NULL event class correctly");
809 ok(!bt_ctf_event_class_set_emf_uri(event_class
, "http://diamon.org/babeltrace/"),
810 "bt_ctf_event_class_set_emf_uri succeeds with a valid EMF URI");
811 ok(!bt_ctf_event_class_get_emf_uri(NULL
),
812 "bt_ctf_event_class_get_emf_uri handles a NULL event class correctly");
813 ok(strcmp(bt_ctf_event_class_get_emf_uri(event_class
), "http://diamon.org/babeltrace/") == 0,
814 "bt_ctf_event_class_get_emf_uri returns the expected EMF URI");
815 ok(!bt_ctf_event_class_set_emf_uri(event_class
, NULL
),
816 "bt_ctf_event_class_set_emf_uri succeeds with NULL (to reset)");
817 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
818 "as expected, event class has no EMF URI after reset");
820 /* Add event class to the stream class */
821 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
822 "Reject addition of NULL event class to a stream class");
823 ok(bt_ctf_stream_class_add_event_class(stream_class
,
824 event_class
) == 0, "Add an event class to stream class");
826 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
827 "bt_ctf_event_class_get_stream_class handles NULL correctly");
828 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
829 ok(ret_stream_class
== stream_class
,
830 "bt_ctf_event_class_get_stream_class returns the correct stream class");
831 bt_put(ret_stream_class
);
833 ok(bt_ctf_event_class_get_payload_type_field_count(NULL
) < 0,
834 "bt_ctf_event_class_get_field_count handles NULL correctly");
835 ok(bt_ctf_event_class_get_payload_type_field_count(event_class
) == 3,
836 "bt_ctf_event_class_get_field_count returns a correct value");
838 ok(bt_ctf_event_class_get_payload_type_field_by_index(NULL
, &ret_string
,
839 &ret_field_type
, 0) < 0,
840 "bt_ctf_event_class_get_field handles a NULL event class correctly");
841 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, NULL
,
842 &ret_field_type
, 0) == 0,
843 "bt_ctf_event_class_get_field handles a NULL field name correctly");
844 bt_put(ret_field_type
);
845 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
847 "bt_ctf_event_class_get_field handles a NULL field type correctly");
848 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
849 &ret_field_type
, 42) < 0,
850 "bt_ctf_event_class_get_field handles an invalid index correctly");
851 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
852 &ret_field_type
, 0) == 0,
853 "bt_ctf_event_class_get_field returns a field");
854 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
855 "bt_ctf_event_class_get_field returns a correct field type");
856 bt_put(ret_field_type
);
857 ok(!strcmp(ret_string
, "uint_35"),
858 "bt_ctf_event_class_get_field returns a correct field name");
859 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
860 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
861 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
862 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
863 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
864 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
865 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
866 "complex_structure");
867 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
868 "bt_ctf_event_class_get_field_by_name returns a correct field type");
869 bt_put(ret_field_type
);
871 event
= bt_ctf_event_create(event_class
);
872 ok(event
, "Instanciate a complex event");
874 ok(bt_ctf_event_get_class(NULL
) == NULL
,
875 "bt_ctf_event_get_class handles NULL correctly");
876 ret_event_class
= bt_ctf_event_get_class(event
);
877 ok(ret_event_class
== event_class
,
878 "bt_ctf_event_get_class returns the correct event class");
879 bt_put(ret_event_class
);
881 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
882 if (!uint_35_field
) {
883 printf("uint_35_field is NULL\n");
886 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
887 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
888 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
889 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
890 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
891 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
892 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
893 &ret_unsigned_int
) == 0,
894 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
895 ok(ret_unsigned_int
== 0x0DDF00D,
896 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
897 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
898 &ret_signed_int
) < 0,
899 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
900 bt_put(uint_35_field
);
902 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
903 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
904 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
905 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
906 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
907 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
908 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
909 &ret_signed_int
) == 0,
910 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
911 ok(ret_signed_int
== -12345,
912 "bt_ctf_field_signed_integer_get_value returns the correct value");
913 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
914 &ret_unsigned_int
) < 0,
915 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
916 bt_put(int_16_field
);
918 complex_structure_field
= bt_ctf_event_get_payload(event
,
919 "complex_structure");
921 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
922 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
923 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
924 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
925 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
926 complex_structure_field
, 3);
927 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
928 bt_put(inner_structure_field
);
929 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
930 "bt_ctf_field_structure_get_field_by_index returns a correct field");
931 bt_put(ret_field_type
);
933 inner_structure_field
= bt_ctf_field_structure_get_field(
934 complex_structure_field
, "inner_structure");
935 a_string_field
= bt_ctf_field_structure_get_field(
936 complex_structure_field
, "a_string");
937 enum_variant_field
= bt_ctf_field_structure_get_field(
938 complex_structure_field
, "variant_selector");
939 variant_field
= bt_ctf_field_structure_get_field(
940 complex_structure_field
, "variant_value");
941 uint_35_field
= bt_ctf_field_structure_get_field(
942 inner_structure_field
, "seq_len");
943 a_sequence_field
= bt_ctf_field_structure_get_field(
944 inner_structure_field
, "a_sequence");
945 an_array_field
= bt_ctf_field_structure_get_field(
946 inner_structure_field
, "an_array");
948 enum_container_field
= bt_ctf_field_enumeration_get_container(
950 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
951 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
953 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
954 bt_put(int_16_field
);
955 ok(!bt_ctf_field_string_get_value(a_string_field
),
956 "bt_ctf_field_string_get_value returns NULL on an unset field");
957 bt_ctf_field_string_set_value(a_string_field
,
959 ok(!bt_ctf_field_string_get_value(NULL
),
960 "bt_ctf_field_string_get_value correctly handles NULL");
961 ok(bt_ctf_field_string_append(NULL
, "yeah"),
962 "bt_ctf_field_string_append correctly handles a NULL string field");
963 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
964 "bt_ctf_field_string_append correctly handles a NULL string value");
965 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
966 "bt_ctf_field_string_append succeeds");
967 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
968 "bt_ctf_field_string_append_len correctly handles a NULL string field");
969 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
970 "bt_ctf_field_string_append_len correctly handles a NULL string value");
971 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
972 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
973 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
974 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
975 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
976 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
977 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
978 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
980 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
981 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
982 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
983 "bt_ctf_field_string_get_value returns a correct value");
984 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
985 SEQUENCE_TEST_LENGTH
);
987 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
988 enum_container_field
) == NULL
,
989 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
990 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
992 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
993 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
994 variant_type
, enum_variant_field
);
995 ok(ret_field_type
== int_16_type
,
996 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
998 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
999 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1000 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1001 uint_35_field
) == 0, "Set a sequence field's length");
1002 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1003 ok(ret_field
== uint_35_field
,
1004 "bt_ctf_field_sequence_get_length returns the correct length field");
1005 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1006 "bt_ctf_field_sequence_get_length properly handles NULL");
1008 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1009 int_16_field
= bt_ctf_field_sequence_get_field(
1010 a_sequence_field
, i
);
1011 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1012 bt_put(int_16_field
);
1015 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1016 int_16_field
= bt_ctf_field_array_get_field(
1018 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1019 bt_put(int_16_field
);
1022 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1023 assert(stream_event_ctx_field
);
1024 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1025 stream_event_ctx_field
, "common_event_context");
1026 BT_PUT(stream_event_ctx_field
);
1027 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1028 BT_PUT(stream_event_ctx_int_field
);
1030 bt_ctf_clock_set_time(clock
, ++current_time
);
1031 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1032 "Append a complex event to a stream");
1035 * Populate the custom packet context field with a dummy value
1036 * otherwise flush will fail.
1038 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1039 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1040 "custom_packet_context_field");
1041 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1043 ok(bt_ctf_stream_flush(stream
) == 0,
1044 "Flush a stream containing a complex event");
1046 bt_put(uint_35_field
);
1047 bt_put(a_string_field
);
1048 bt_put(inner_structure_field
);
1049 bt_put(complex_structure_field
);
1050 bt_put(a_sequence_field
);
1051 bt_put(an_array_field
);
1052 bt_put(enum_variant_field
);
1053 bt_put(enum_container_field
);
1054 bt_put(variant_field
);
1056 bt_put(packet_context_field
);
1057 bt_put(packet_context
);
1058 bt_put(uint_35_type
);
1059 bt_put(int_16_type
);
1060 bt_put(string_type
);
1061 bt_put(sequence_type
);
1063 bt_put(inner_structure_type
);
1064 bt_put(complex_structure_type
);
1065 bt_put(uint_3_type
);
1066 bt_put(enum_variant_type
);
1067 bt_put(variant_type
);
1068 bt_put(ret_field_type
);
1069 bt_put(event_class
);
1074 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1075 struct bt_ctf_field_type
*expected_type
, const char *name
)
1077 struct bt_ctf_field_type
*copy_type
;
1079 copy_type
= bt_ctf_field_get_type(field
);
1080 ok(copy_type
== expected_type
,
1081 "bt_ctf_field_copy does not copy the type (%s)", name
);
1086 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1087 struct bt_ctf_field
*field_b
, const char *name
)
1089 ok(field_a
!= field_b
,
1090 "bt_ctf_field_copy creates different pointers (%s)", name
);
1094 void field_copy_tests()
1096 struct bt_ctf_field_type
*len_type
= NULL
;
1097 struct bt_ctf_field_type
*fp_type
= NULL
;
1098 struct bt_ctf_field_type
*s_type
= NULL
;
1099 struct bt_ctf_field_type
*e_int_type
= NULL
;
1100 struct bt_ctf_field_type
*e_type
= NULL
;
1101 struct bt_ctf_field_type
*v_type
= NULL
;
1102 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1103 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1104 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1105 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1106 struct bt_ctf_field_type
*strct_type
= NULL
;
1107 struct bt_ctf_field
*len
= NULL
;
1108 struct bt_ctf_field
*fp
= NULL
;
1109 struct bt_ctf_field
*s
= NULL
;
1110 struct bt_ctf_field
*e_int
= NULL
;
1111 struct bt_ctf_field
*e
= NULL
;
1112 struct bt_ctf_field
*v
= NULL
;
1113 struct bt_ctf_field
*v_selected
= NULL
;
1114 struct bt_ctf_field
*v_selected_cur
= NULL
;
1115 struct bt_ctf_field
*v_selected_0
= NULL
;
1116 struct bt_ctf_field
*v_selected_1
= NULL
;
1117 struct bt_ctf_field
*v_selected_2
= NULL
;
1118 struct bt_ctf_field
*v_selected_3
= NULL
;
1119 struct bt_ctf_field
*v_selected_4
= NULL
;
1120 struct bt_ctf_field
*v_selected_5
= NULL
;
1121 struct bt_ctf_field
*v_selected_6
= NULL
;
1122 struct bt_ctf_field
*a
= NULL
;
1123 struct bt_ctf_field
*a_0
= NULL
;
1124 struct bt_ctf_field
*a_1
= NULL
;
1125 struct bt_ctf_field
*a_2
= NULL
;
1126 struct bt_ctf_field
*a_3
= NULL
;
1127 struct bt_ctf_field
*a_4
= NULL
;
1128 struct bt_ctf_field
*strct
= NULL
;
1129 struct bt_ctf_field
*len_copy
= NULL
;
1130 struct bt_ctf_field
*fp_copy
= NULL
;
1131 struct bt_ctf_field
*s_copy
= NULL
;
1132 struct bt_ctf_field
*e_int_copy
= NULL
;
1133 struct bt_ctf_field
*e_copy
= NULL
;
1134 struct bt_ctf_field
*v_copy
= NULL
;
1135 struct bt_ctf_field
*v_selected_copy
= NULL
;
1136 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1137 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1138 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1139 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1140 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1141 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1142 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1143 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1144 struct bt_ctf_field
*a_copy
= NULL
;
1145 struct bt_ctf_field
*a_0_copy
= NULL
;
1146 struct bt_ctf_field
*a_1_copy
= NULL
;
1147 struct bt_ctf_field
*a_2_copy
= NULL
;
1148 struct bt_ctf_field
*a_3_copy
= NULL
;
1149 struct bt_ctf_field
*a_4_copy
= NULL
;
1150 struct bt_ctf_field
*strct_copy
= NULL
;
1151 struct bt_ctf_field_type_enumeration_mapping_iterator
*e_iter
= NULL
;
1152 uint64_t uint64_t_val
;
1153 const char *str_val
;
1157 /* create len type */
1158 len_type
= bt_ctf_field_type_integer_create(32);
1161 /* create fp type */
1162 fp_type
= bt_ctf_field_type_floating_point_create();
1166 s_type
= bt_ctf_field_type_string_create();
1169 /* create e_int type */
1170 e_int_type
= bt_ctf_field_type_integer_create(8);
1174 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1176 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1179 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1183 /* create v_label1 type */
1184 v_label1_type
= bt_ctf_field_type_string_create();
1185 assert(v_label1_type
);
1187 /* create v_label1_array type */
1188 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1189 assert(v_label1_array_type
);
1191 /* create v_label2 type */
1192 v_label2_type
= bt_ctf_field_type_integer_create(16);
1193 assert(v_label2_type
);
1195 /* create v_label2_seq type */
1196 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1198 assert(v_label2_seq_type
);
1201 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1203 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1206 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1210 /* create strct type */
1211 strct_type
= bt_ctf_field_type_structure_create();
1213 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1216 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1218 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1220 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1222 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1224 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1225 v_label1_array_type
, "a");
1229 strct
= bt_ctf_field_create(strct_type
);
1233 len
= bt_ctf_field_structure_get_field(strct
, "len");
1237 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1241 s
= bt_ctf_field_structure_get_field(strct
, "s");
1245 e
= bt_ctf_field_structure_get_field(strct
, "e");
1248 /* get e_int (underlying integer) */
1249 e_int
= bt_ctf_field_enumeration_get_container(e
);
1253 v
= bt_ctf_field_structure_get_field(strct
, "v");
1257 a
= bt_ctf_field_structure_get_field(strct
, "a");
1261 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1265 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1269 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1272 /* set e field (LABEL2) */
1273 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1277 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1279 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1280 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1281 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1282 ok(v_selected_cur
== v_selected
,
1283 "bt_ctf_field_variant_get_current_field returns the current field");
1284 bt_put(v_selected_cur
);
1286 /* set selected v field */
1287 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1289 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1290 assert(v_selected_0
);
1291 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1293 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1294 assert(v_selected_1
);
1295 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1297 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1298 assert(v_selected_2
);
1299 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1301 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1302 assert(v_selected_3
);
1303 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1305 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1306 assert(v_selected_4
);
1307 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1309 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1310 assert(v_selected_5
);
1311 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1313 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1314 assert(v_selected_6
);
1315 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1319 a_0
= bt_ctf_field_array_get_field(a
, 0);
1321 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1323 a_1
= bt_ctf_field_array_get_field(a
, 1);
1325 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1327 a_2
= bt_ctf_field_array_get_field(a
, 2);
1329 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1331 a_3
= bt_ctf_field_array_get_field(a
, 3);
1333 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1335 a_4
= bt_ctf_field_array_get_field(a
, 4);
1337 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1340 /* create copy of strct */
1341 ok(!bt_ctf_field_copy(NULL
),
1342 "bt_ctf_field_copy handles NULL correctly");
1343 strct_copy
= bt_ctf_field_copy(strct
);
1345 "bt_ctf_field_copy returns a valid pointer");
1347 /* get all copied fields */
1348 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1350 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1352 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1354 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1356 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1358 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1360 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1361 assert(v_selected_copy
);
1362 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1363 assert(v_selected_0_copy
);
1364 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1365 assert(v_selected_1_copy
);
1366 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1367 assert(v_selected_2_copy
);
1368 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1369 assert(v_selected_3_copy
);
1370 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1371 assert(v_selected_4_copy
);
1372 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1373 assert(v_selected_5_copy
);
1374 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1375 assert(v_selected_6_copy
);
1376 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1377 "sequence field copy is not too large");
1378 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1380 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1382 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1384 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1386 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1388 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1390 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1391 "array field copy is not too large");
1393 /* make sure copied fields are different pointers */
1394 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1395 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1396 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1397 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1398 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1399 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1400 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1401 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1403 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1405 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1407 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1409 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1411 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1413 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1415 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1417 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1418 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1419 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1420 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1421 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1422 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1424 /* make sure copied fields share the same types */
1425 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1426 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1427 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1428 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1429 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1430 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1431 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1433 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1435 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1437 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1439 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1441 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1443 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1445 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1447 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1448 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1449 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1450 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1451 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1452 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1454 /* validate len copy */
1455 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1457 ok(uint64_t_val
== 7,
1458 "bt_ctf_field_copy creates a valid integer field copy");
1460 /* validate fp copy */
1461 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1463 ok(double_val
== 3.14,
1464 "bt_ctf_field_copy creates a valid floating point number field copy");
1466 /* validate s copy */
1467 str_val
= bt_ctf_field_string_get_value(s_copy
);
1468 ok(str_val
&& !strcmp(str_val
, "btbt"),
1469 "bt_ctf_field_copy creates a valid string field copy");
1471 /* validate e_int copy */
1472 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1475 ok(uint64_t_val
== 23,
1476 "bt_ctf_field_copy creates a valid enum's integer field copy");
1478 /* validate e copy */
1479 e_iter
= bt_ctf_field_enumeration_get_mappings(e_copy
);
1480 (void) bt_ctf_field_type_enumeration_mapping_iterator_next(e_iter
);
1481 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter
,
1482 &str_val
, NULL
, NULL
);
1483 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1484 "bt_ctf_field_copy creates a valid enum field copy");
1486 /* validate v_selected copy */
1487 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1488 assert(v_selected_copy_len
);
1489 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1492 ok(uint64_t_val
== 7,
1493 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1494 bt_put(v_selected_copy_len
);
1495 v_selected_copy_len
= NULL
;
1497 /* validate v_selected copy fields */
1498 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1501 ok(uint64_t_val
== 7,
1502 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1503 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1506 ok(uint64_t_val
== 6,
1507 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1508 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1511 ok(uint64_t_val
== 5,
1512 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1513 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1516 ok(uint64_t_val
== 4,
1517 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1518 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1521 ok(uint64_t_val
== 3,
1522 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1523 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1526 ok(uint64_t_val
== 2,
1527 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1528 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1531 ok(uint64_t_val
== 1,
1532 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1534 /* validate a copy fields */
1535 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1536 ok(str_val
&& !strcmp(str_val
, "a_0"),
1537 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1538 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1539 ok(str_val
&& !strcmp(str_val
, "a_1"),
1540 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1541 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1542 ok(str_val
&& !strcmp(str_val
, "a_2"),
1543 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1544 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1545 ok(str_val
&& !strcmp(str_val
, "a_3"),
1546 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1547 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1548 ok(str_val
&& !strcmp(str_val
, "a_4"),
1549 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1551 /* put everything */
1558 bt_put(v_label1_type
);
1559 bt_put(v_label1_array_type
);
1560 bt_put(v_label2_type
);
1561 bt_put(v_label2_seq_type
);
1570 bt_put(v_selected_0
);
1571 bt_put(v_selected_1
);
1572 bt_put(v_selected_2
);
1573 bt_put(v_selected_3
);
1574 bt_put(v_selected_4
);
1575 bt_put(v_selected_5
);
1576 bt_put(v_selected_6
);
1590 bt_put(v_selected_copy
);
1591 bt_put(v_selected_0_copy
);
1592 bt_put(v_selected_1_copy
);
1593 bt_put(v_selected_2_copy
);
1594 bt_put(v_selected_3_copy
);
1595 bt_put(v_selected_4_copy
);
1596 bt_put(v_selected_5_copy
);
1597 bt_put(v_selected_6_copy
);
1609 void type_field_tests()
1611 struct bt_ctf_field
*uint_12
;
1612 struct bt_ctf_field
*int_16
;
1613 struct bt_ctf_field
*string
;
1614 struct bt_ctf_field
*enumeration
;
1615 struct bt_ctf_field_type
*composite_structure_type
;
1616 struct bt_ctf_field_type
*structure_seq_type
;
1617 struct bt_ctf_field_type
*string_type
;
1618 struct bt_ctf_field_type
*sequence_type
;
1619 struct bt_ctf_field_type
*uint_8_type
;
1620 struct bt_ctf_field_type
*int_16_type
;
1621 struct bt_ctf_field_type
*uint_12_type
=
1622 bt_ctf_field_type_integer_create(12);
1623 struct bt_ctf_field_type
*enumeration_type
;
1624 struct bt_ctf_field_type
*returned_type
;
1625 const char *ret_string
;
1627 returned_type
= bt_ctf_field_get_type(NULL
);
1628 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1630 ok(uint_12_type
, "Create an unsigned integer type");
1631 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1632 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1633 "Set integer type's base as binary");
1634 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1635 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1636 "Set integer type's base as decimal");
1637 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1638 BT_CTF_INTEGER_BASE_UNKNOWN
),
1639 "Reject integer type's base set as unknown");
1640 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1641 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1642 "Set integer type's base as octal");
1643 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1644 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1645 "Set integer type's base as hexadecimal");
1646 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1647 "Reject unknown integer base value");
1648 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1649 "Set integer type signedness to signed");
1650 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1651 "Set integer type signedness to unsigned");
1652 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1653 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1654 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1655 "bt_ctf_field_type_integer_get_size returns a correct value");
1656 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1657 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1658 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1659 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1661 ok(bt_ctf_field_type_set_byte_order(NULL
,
1662 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1663 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1664 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1665 (enum bt_ctf_byte_order
) 42) < 0,
1666 "bt_ctf_field_type_set_byte_order rejects invalid values");
1667 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1668 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1669 "Set an integer's byte order to little endian");
1670 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1671 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1672 "Set an integer's byte order to big endian");
1673 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1674 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1675 "bt_ctf_field_type_get_byte_order returns a correct value");
1676 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1677 BT_CTF_BYTE_ORDER_UNKNOWN
,
1678 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1680 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1681 BT_CTF_FIELD_TYPE_ID_UNKNOWN
,
1682 "bt_ctf_field_type_get_type_id handles NULL correctly");
1683 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1684 BT_CTF_FIELD_TYPE_ID_INTEGER
,
1685 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1687 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1688 BT_CTF_INTEGER_BASE_UNKNOWN
,
1689 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1690 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1691 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1692 "bt_ctf_field_type_integer_get_base returns a correct value");
1694 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1695 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1696 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1697 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1698 (enum bt_ctf_string_encoding
) 123) < 0,
1699 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1700 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1701 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1702 "Set integer type encoding to UTF8");
1703 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1704 BT_CTF_STRING_ENCODING_UNKNOWN
,
1705 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1706 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1707 BT_CTF_STRING_ENCODING_UTF8
,
1708 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1710 int_16_type
= bt_ctf_field_type_integer_create(16);
1711 assert(int_16_type
);
1712 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1713 "Set signedness of 16 bit integer to true");
1714 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1715 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1716 uint_8_type
= bt_ctf_field_type_integer_create(8);
1718 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1719 ok(sequence_type
, "Create a sequence of int16_t type");
1720 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1721 BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
1722 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1724 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1725 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1726 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1728 ok(!strcmp(ret_string
, "seq_len"),
1729 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1730 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1731 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1732 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1734 ok(returned_type
== int_16_type
,
1735 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1736 bt_put(returned_type
);
1738 string_type
= bt_ctf_field_type_string_create();
1739 ok(string_type
, "Create a string type");
1740 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1741 BT_CTF_STRING_ENCODING_NONE
),
1742 "Reject invalid \"None\" string encoding");
1743 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1745 "Reject invalid string encoding");
1746 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1747 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1748 "Set string encoding to ASCII");
1750 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1751 BT_CTF_STRING_ENCODING_UNKNOWN
,
1752 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1753 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1754 BT_CTF_STRING_ENCODING_ASCII
,
1755 "bt_ctf_field_type_string_get_encoding returns the correct value");
1757 structure_seq_type
= bt_ctf_field_type_structure_create();
1758 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1759 BT_CTF_FIELD_TYPE_ID_STRUCT
,
1760 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1761 ok(structure_seq_type
, "Create a structure type");
1762 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1763 uint_8_type
, "seq_len") == 0,
1764 "Add a uint8_t type to a structure");
1765 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1766 sequence_type
, "a_sequence") == 0,
1767 "Add a sequence type to a structure");
1769 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1770 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1771 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1772 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1774 ok(bt_ctf_field_type_structure_get_field(NULL
,
1775 &ret_string
, &returned_type
, 1) < 0,
1776 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1777 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1778 NULL
, &returned_type
, 1) == 0,
1779 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1780 bt_put(returned_type
);
1781 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1782 &ret_string
, NULL
, 1) == 0,
1783 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1784 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1785 &ret_string
, &returned_type
, 10) < 0,
1786 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1787 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1788 &ret_string
, &returned_type
, 1) == 0,
1789 "bt_ctf_field_type_structure_get_field returns a field");
1790 ok(!strcmp(ret_string
, "a_sequence"),
1791 "bt_ctf_field_type_structure_get_field returns a correct field name");
1792 ok(returned_type
== sequence_type
,
1793 "bt_ctf_field_type_structure_get_field returns a correct field type");
1794 bt_put(returned_type
);
1796 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1797 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1798 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1799 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1800 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1801 structure_seq_type
, "a_sequence");
1802 ok(returned_type
== sequence_type
,
1803 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1804 bt_put(returned_type
);
1806 composite_structure_type
= bt_ctf_field_type_structure_create();
1807 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1808 string_type
, "a_string") == 0,
1809 "Add a string type to a structure");
1810 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1811 structure_seq_type
, "inner_structure") == 0,
1812 "Add a structure type to a structure");
1814 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1815 NULL
, "a_sequence") == NULL
,
1816 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1817 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1818 structure_seq_type
, NULL
) == NULL
,
1819 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1820 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1821 structure_seq_type
, "a_sequence");
1822 ok(returned_type
== sequence_type
,
1823 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1824 bt_put(returned_type
);
1826 int_16
= bt_ctf_field_create(int_16_type
);
1827 ok(int_16
, "Instanciate a signed 16-bit integer");
1828 uint_12
= bt_ctf_field_create(uint_12_type
);
1829 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1830 returned_type
= bt_ctf_field_get_type(int_16
);
1831 ok(returned_type
== int_16_type
,
1832 "bt_ctf_field_get_type returns the correct type");
1834 /* Can't modify types after instanciating them */
1835 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1836 BT_CTF_INTEGER_BASE_DECIMAL
),
1837 "Check an integer type' base can't be modified after instanciation");
1838 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1839 "Check an integer type's signedness can't be modified after instanciation");
1841 /* Check signed property is checked */
1842 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1843 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1844 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1845 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1847 /* Check overflows are properly tested for */
1848 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1849 "Check -32768 is allowed for a signed 16-bit integer");
1850 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1851 "Check 32767 is allowed for a signed 16-bit integer");
1852 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1853 "Check 32768 is not allowed for a signed 16-bit integer");
1854 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1855 "Check -32769 is not allowed for a signed 16-bit integer");
1856 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1857 "Check -42 is allowed for a signed 16-bit integer");
1859 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1860 "Check 4095 is allowed for an unsigned 12-bit integer");
1861 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1862 "Check 4096 is not allowed for a unsigned 12-bit integer");
1863 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1864 "Check 0 is allowed for an unsigned 12-bit integer");
1866 string
= bt_ctf_field_create(string_type
);
1867 ok(string
, "Instanciate a string field");
1868 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1869 "Set a string's value");
1871 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1872 ok(enumeration_type
,
1873 "Create an enumeration type with an unsigned 12-bit integer as container");
1874 enumeration
= bt_ctf_field_create(enumeration_type
);
1876 "Check enumeration types are validated before instantiation");
1881 bt_put(enumeration
);
1882 bt_put(composite_structure_type
);
1883 bt_put(structure_seq_type
);
1884 bt_put(string_type
);
1885 bt_put(sequence_type
);
1886 bt_put(uint_8_type
);
1887 bt_put(int_16_type
);
1888 bt_put(uint_12_type
);
1889 bt_put(enumeration_type
);
1890 bt_put(returned_type
);
1894 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1895 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1898 * Append enough events to force the underlying packet to be resized.
1899 * Also tests that a new event can be declared after a stream has been
1900 * instantiated and used/flushed.
1904 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1906 struct bt_ctf_field_type
*integer_type
=
1907 bt_ctf_field_type_integer_create(17);
1908 struct bt_ctf_field_type
*string_type
=
1909 bt_ctf_field_type_string_create();
1910 struct bt_ctf_event
*event
= NULL
;
1911 struct bt_ctf_field
*ret_field
= NULL
;
1912 struct bt_ctf_field_type
*ret_field_type
= NULL
;
1913 uint64_t ret_uint64
;
1914 int events_appended
= 0;
1915 struct bt_ctf_field
*packet_context
= NULL
,
1916 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
1917 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
1918 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
1919 struct bt_ctf_field_type
*ep_type
= NULL
;
1921 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1923 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1925 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1926 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1932 * bt_ctf_stream_class_add_event_class() copies the field types
1933 * of event_class, so we retrieve the new ones to create the
1934 * appropriate fields.
1936 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
1938 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1939 ep_type
, "field_1");
1940 assert(ep_field_1_type
);
1941 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1942 ep_type
, "a_string");
1943 assert(ep_a_string_type
);
1945 event
= bt_ctf_event_create(event_class
);
1946 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
1947 ret_field_type
= bt_ctf_field_get_type(ret_field
);
1948 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
1949 "bt_ctf_event_get_payload_by_index returns a correct field");
1950 bt_put(ret_field_type
);
1953 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
1954 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1955 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
1956 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1959 for (i
= 0; i
< packet_resize_test_length
; i
++) {
1960 event
= bt_ctf_event_create(event_class
);
1961 struct bt_ctf_field
*integer
=
1962 bt_ctf_field_create(ep_field_1_type
);
1963 struct bt_ctf_field
*string
=
1964 bt_ctf_field_create(ep_a_string_type
);
1966 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1967 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1968 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1971 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1972 ret
|= bt_ctf_event_set_payload(event
, "a_string",
1976 /* Populate stream event context */
1977 stream_event_context
=
1978 bt_ctf_event_get_stream_event_context(event
);
1979 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
1980 "common_event_context");
1981 BT_PUT(stream_event_context
);
1982 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
1986 ret
|= bt_ctf_stream_append_event(stream
, event
);
1994 events_appended
= !!(i
== packet_resize_test_length
);
1995 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
1996 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
1997 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
1998 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
1999 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2000 ok(ret
== 0 && ret_uint64
== 0,
2001 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2002 bt_ctf_stream_append_discarded_events(stream
, 1000);
2003 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2004 ok(ret
== 0 && ret_uint64
== 1000,
2005 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2008 ok(events_appended
, "Append 100 000 events to a stream");
2011 * Populate the custom packet context field with a dummy value
2012 * otherwise flush will fail.
2014 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2015 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2016 "custom_packet_context_field");
2017 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2019 ok(bt_ctf_stream_flush(stream
) == 0,
2020 "Flush a stream that forces a packet resize");
2021 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2022 ok(ret
== 0 && ret_uint64
== 1000,
2023 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2024 bt_put(integer_type
);
2025 bt_put(string_type
);
2026 bt_put(packet_context
);
2027 bt_put(packet_context_field
);
2028 bt_put(stream_event_context
);
2029 bt_put(event_class
);
2030 bt_put(ep_field_1_type
);
2031 bt_put(ep_a_string_type
);
2036 void test_empty_stream(struct bt_ctf_writer
*writer
)
2039 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2040 struct bt_ctf_stream_class
*stream_class
= NULL
;
2041 struct bt_ctf_stream
*stream
= NULL
;
2043 trace
= bt_ctf_writer_get_trace(writer
);
2045 diag("Failed to get trace from writer");
2050 stream_class
= bt_ctf_stream_class_create("empty_stream");
2051 if (!stream_class
) {
2052 diag("Failed to create stream class");
2057 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2059 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
2062 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2063 "bt_ctf_stream_class_get_trace handles NULL correctly");
2064 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2065 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2067 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2069 diag("Failed to create writer stream");
2074 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2075 ok(ret_trace
== trace
,
2076 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2079 "Created a stream class with default attributes and an empty stream");
2083 bt_put(stream_class
);
2087 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
,
2088 struct bt_ctf_clock
*clock
)
2091 struct bt_ctf_stream_class
*stream_class
= NULL
;
2092 struct bt_ctf_stream
*stream
= NULL
;
2093 struct bt_ctf_field_type
*integer_type
= NULL
,
2094 *sequence_type
= NULL
, *event_header_type
= NULL
;
2095 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2096 *event_header
= NULL
, *packet_header
= NULL
;
2097 struct bt_ctf_event_class
*event_class
= NULL
;
2098 struct bt_ctf_event
*event
= NULL
;
2100 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2101 if (!stream_class
) {
2102 fail("Failed to create stream class");
2106 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2108 fail("Failed to set stream class clock");
2113 * Customize event header to add an "seq_len" integer member
2114 * which will be used as the length of a sequence in an event of this
2117 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2119 if (!event_header_type
) {
2120 fail("Failed to get event header type");
2124 integer_type
= bt_ctf_field_type_integer_create(13);
2125 if (!integer_type
) {
2126 fail("Failed to create length integer type");
2130 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2131 integer_type
, "seq_len");
2133 fail("Failed to add a new field to stream event header");
2137 event_class
= bt_ctf_event_class_create("sequence_event");
2139 fail("Failed to create event class");
2144 * This event's payload will contain a sequence which references
2145 * stream.event.header.seq_len as its length field.
2147 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2148 "stream.event.header.seq_len");
2149 if (!sequence_type
) {
2150 fail("Failed to create a sequence");
2154 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2157 fail("Failed to add a sequence to an event class");
2161 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2163 fail("Failed to add event class to stream class");
2167 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2169 fail("Failed to create stream")
2174 * We have defined a custom packet header field. We have to populate it
2177 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2178 if (!packet_header
) {
2179 fail("Failed to get stream packet header");
2183 integer
= bt_ctf_field_structure_get_field(packet_header
,
2184 "custom_trace_packet_header_field");
2186 fail("Failed to retrieve custom_trace_packet_header_field");
2190 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2192 fail("Failed to set custom_trace_packet_header_field value");
2197 event
= bt_ctf_event_create(event_class
);
2199 fail("Failed to create event");
2203 event_header
= bt_ctf_event_get_header(event
);
2204 if (!event_header
) {
2205 fail("Failed to get event header");
2209 integer
= bt_ctf_field_structure_get_field(event_header
,
2212 fail("Failed to get seq_len field from event header");
2216 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2218 fail("Failed to set seq_len value in event header");
2222 /* Populate both sequence integer fields */
2223 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2225 fail("Failed to retrieve sequence from event");
2229 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2231 fail("Failed to set sequence length");
2236 for (i
= 0; i
< 2; i
++) {
2237 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2239 fail("Failed to retrieve sequence element");
2243 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2245 fail("Failed to set sequence element value");
2253 ret
= bt_ctf_stream_append_event(stream
, event
);
2255 fail("Failed to append event to stream");
2259 ret
= bt_ctf_stream_flush(stream
);
2261 fail("Failed to flush custom_event_header stream");
2265 bt_put(stream_class
);
2266 bt_put(event_class
);
2270 bt_put(event_header
);
2271 bt_put(packet_header
);
2272 bt_put(sequence_type
);
2273 bt_put(integer_type
);
2274 bt_put(event_header_type
);
2278 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
,
2279 struct bt_ctf_clock
*clock
)
2282 struct bt_ctf_stream_class
*stream_class
= NULL
;
2283 struct bt_ctf_stream
*stream
= NULL
,
2285 struct bt_ctf_event_class
*event_class
= NULL
;
2286 struct bt_ctf_event
*event
= NULL
;
2287 struct bt_ctf_field_type
*integer_type
= NULL
;
2288 struct bt_ctf_field
*integer
= NULL
;
2290 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2291 if (!stream_class
) {
2292 diag("Failed to create stream class");
2297 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2299 diag("Failed to set stream class clock");
2303 event_class
= bt_ctf_event_class_create("some_event_class_name");
2304 integer_type
= bt_ctf_field_type_integer_create(32);
2305 if (!integer_type
) {
2306 diag("Failed to create integer field type");
2311 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2314 diag("Failed to add field to event class");
2318 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2321 diag("Failed to add event class to stream class");
2324 event
= bt_ctf_event_create(event_class
);
2326 diag("Failed to create event");
2331 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2333 diag("Failed to get integer field payload from event");
2338 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2340 diag("Failed to set integer field value");
2344 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2346 diag("Failed to create writer stream");
2351 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2352 "bt_ctf_event_get_stream handles NULL correctly");
2353 ok(bt_ctf_event_get_stream(event
) == NULL
,
2354 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2356 ret
= bt_ctf_stream_append_event(stream
, event
);
2358 diag("Failed to append event to stream");
2362 ret_stream
= bt_ctf_event_get_stream(event
);
2363 ok(ret_stream
== stream
,
2364 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2367 "Create an event before instanciating its associated stream");
2370 bt_put(stream_class
);
2371 bt_put(event_class
);
2373 bt_put(integer_type
);
2378 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2380 struct bt_ctf_event_class
*event_class
;
2382 event_class
= bt_ctf_event_class_create("Simple Event");
2383 assert(event_class
);
2384 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
) == 0,
2385 "two event classes with the same name may cohabit within the same stream class");
2386 bt_put(event_class
);
2388 event_class
= bt_ctf_event_class_create("different name, ok");
2389 assert(event_class
);
2390 assert(!bt_ctf_event_class_set_id(event_class
, 13));
2391 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2392 "two event classes with the same ID cannot cohabit within the same stream class");
2393 bt_put(event_class
);
2397 struct bt_ctf_event_class
*create_minimal_event_class(void)
2399 struct bt_ctf_event_class
*ec
= NULL
;
2400 struct bt_ctf_field_type
*int_ft
= NULL
;
2403 int_ft
= bt_ctf_field_type_integer_create(23);
2405 ec
= bt_ctf_event_class_create("minimal");
2407 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2415 void test_create_writer_vs_non_writer_mode(void)
2419 const char *writer_stream_name
= "writer stream instance";
2420 struct bt_ctf_writer
*writer
= NULL
;
2421 struct bt_ctf_trace
*writer_trace
= NULL
;
2422 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2423 struct bt_ctf_stream
*writer_stream
= NULL
;
2424 struct bt_ctf_stream
*writer_stream2
= NULL
;
2425 struct bt_ctf_stream
*packet_stream
= NULL
;
2426 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2427 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2428 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2429 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2430 struct bt_ctf_event_class
*writer_ec
= NULL
;
2431 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2432 struct bt_ctf_event
*event
= NULL
;
2433 struct bt_ctf_event
*event2
= NULL
;
2434 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2435 struct bt_ctf_field
*int_field
= NULL
;
2436 struct bt_ctf_clock
*writer_clock
= NULL
;
2437 struct bt_ctf_clock_class
*non_writer_clock_class
= NULL
;
2438 struct bt_ctf_packet
*packet
= NULL
;
2439 struct bt_ctf_packet
*packet2
= NULL
;
2441 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
2442 if (!bt_mkdtemp(trace_path
)) {
2446 /* Create empty structure field type (event header) */
2447 empty_struct_ft
= bt_ctf_field_type_structure_create();
2448 assert(empty_struct_ft
);
2450 /* Create writer, writer stream class, stream, and clock */
2451 writer
= bt_ctf_writer_create(trace_path
);
2453 writer_clock
= bt_ctf_clock_create("writer_clock");
2454 assert(writer_clock
);
2455 ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
2457 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2459 writer_trace
= bt_ctf_writer_get_trace(writer
);
2460 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2461 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2463 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2466 ret
= bt_ctf_stream_class_set_clock(writer_sc
, writer_clock
);
2468 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2470 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2471 assert(writer_stream
);
2472 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2473 "bt_ctf_stream_get_name() returns the stream's name");
2475 /* Create non-writer trace, stream class, stream, and clock */
2476 non_writer_trace
= bt_ctf_trace_create();
2477 assert(non_writer_trace
);
2478 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2479 assert(non_writer_sc
);
2480 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2483 ret
= bt_ctf_stream_class_set_packet_context_type(non_writer_sc
, NULL
);
2485 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2487 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2488 assert(non_writer_stream
);
2489 non_writer_clock_class
=
2490 bt_ctf_clock_class_create("non_writer_clock_class",
2492 assert(non_writer_clock_class
);
2493 ret
= bt_ctf_trace_add_clock_class(non_writer_trace
,
2494 non_writer_clock_class
);
2497 /* Create event class and event */
2498 writer_ec
= create_minimal_event_class();
2500 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2502 event
= bt_ctf_event_create(writer_ec
);
2504 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2506 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2509 * Verify non-writer stream: it should be impossible to append
2512 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2513 "bt_ctf_stream_append_event() fails with a non-writer stream");
2516 * Verify writer stream: it should be possible to append an
2519 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2520 "bt_ctf_stream_append_event() succeeds with a writer stream");
2523 * It should be possible to create a packet from a non-writer
2524 * stream, but not from a writer stream.
2526 packet
= bt_ctf_packet_create(writer_stream
);
2527 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2528 packet
= bt_ctf_packet_create(non_writer_stream
);
2529 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2530 packet_stream
= bt_ctf_packet_get_stream(packet
);
2531 ok(packet_stream
== non_writer_stream
,
2532 "bt_ctf_packet_get_stream() returns the correct stream");
2535 * It should not be possible to append an event associated to
2536 * a stream to a different stream.
2538 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2539 assert(writer_stream2
);
2540 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2541 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2544 * It should not be possible to set the packet of an event
2545 * associated to a given stream to a packet associated with
2546 * a different stream.
2548 ok(bt_ctf_event_set_packet(event
, packet
),
2549 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2552 * It should be possible to set the packet of a fresh event, as
2553 * long as the originating stream classes are the same.
2555 event2
= bt_ctf_event_create(writer_ec
);
2557 ok(bt_ctf_event_set_packet(event2
, packet
),
2558 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2559 non_writer_ec
= create_minimal_event_class();
2560 assert(non_writer_ec
);
2561 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2564 event2
= bt_ctf_event_create(non_writer_ec
);
2566 ok(!bt_ctf_event_set_packet(event2
, packet
),
2567 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2570 * It should be possible to set a packet created from the same
2571 * stream to an event with an existing packet.
2573 packet2
= bt_ctf_packet_create(non_writer_stream
);
2575 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2576 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2580 * It should not be possible to set a packet created from a
2581 * different stream to an event with an existing packet.
2583 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2584 assert(non_writer_stream2
);
2585 packet2
= bt_ctf_packet_create(non_writer_stream
);
2587 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2588 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2591 bt_put(writer_trace
);
2593 bt_put(writer_stream
);
2594 bt_put(writer_stream2
);
2595 bt_put(non_writer_trace
);
2596 bt_put(non_writer_sc
);
2597 bt_put(non_writer_stream
);
2598 bt_put(non_writer_stream2
);
2599 bt_put(packet_stream
);
2601 bt_put(non_writer_ec
);
2605 bt_put(empty_struct_ft
);
2606 bt_put(writer_clock
);
2607 bt_put(non_writer_clock_class
);
2610 recursive_rmdir(trace_path
);
2615 void test_clock_utils(void)
2618 struct bt_ctf_clock
*clock
= NULL
;
2620 clock
= bt_ctf_clock_create("water");
2622 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2624 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2626 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2628 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2634 void test_set_clock_non_writer_stream_class(void)
2636 struct bt_ctf_clock
*clock
;
2637 struct bt_ctf_trace
*trace
;
2638 struct bt_ctf_stream_class
*sc
;
2641 clock
= bt_ctf_clock_create("the_clock");
2644 trace
= bt_ctf_trace_create();
2647 sc
= bt_ctf_stream_class_create(NULL
);
2650 ret
= bt_ctf_stream_class_set_clock(sc
, clock
);
2653 ret
= bt_ctf_trace_add_stream_class(trace
, sc
);
2655 "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock");
2663 void test_static_trace(void)
2665 struct bt_ctf_trace
*trace
;
2666 struct bt_ctf_stream_class
*stream_class
;
2667 struct bt_ctf_stream_class
*stream_class2
;
2668 struct bt_ctf_stream
*stream
;
2669 struct bt_ctf_clock_class
*clock_class
;
2672 trace
= bt_ctf_trace_create();
2674 stream_class
= bt_ctf_stream_class_create(NULL
);
2675 assert(stream_class
);
2676 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2678 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
2680 stream
= bt_ctf_stream_create(stream_class
, "hello");
2681 ok(stream
, "bt_ctf_stream_create() succeeds with a non-static trace");
2683 ok(!bt_ctf_trace_is_static(trace
),
2684 "bt_ctf_trace_is_static() returns the expected value");
2685 ok(bt_ctf_trace_set_is_static(trace
) == 0,
2686 "bt_ctf_trace_set_is_static() succeeds");
2687 ok(bt_ctf_trace_is_static(trace
),
2688 "bt_ctf_trace_is_static() returns the expected value");
2689 clock_class
= bt_ctf_clock_class_create("yes", 1000000000);
2690 assert(clock_class
);
2691 stream_class2
= bt_ctf_stream_class_create(NULL
);
2692 assert(stream_class2
);
2693 ok(bt_ctf_trace_add_stream_class(trace
, stream_class2
),
2694 "bt_ctf_trace_add_stream_class() fails with a static trace");
2695 ok(bt_ctf_trace_add_clock_class(trace
, clock_class
),
2696 "bt_ctf_trace_add_clock_class() fails with a static trace");
2697 ok(!bt_ctf_stream_create(stream_class
, "hello2"),
2698 "bt_ctf_stream_create() fails with a static trace");
2701 bt_put(stream_class
);
2702 bt_put(stream_class2
);
2703 bt_put(clock_class
);
2707 void trace_is_static_listener(struct bt_ctf_trace
*trace
, void *data
)
2709 *((int *) data
) |= 1;
2713 void trace_listener_removed(struct bt_ctf_trace
*trace
, void *data
)
2715 *((int *) data
) |= 2;
2719 void test_trace_is_static_listener(void)
2721 struct bt_ctf_trace
*trace
;
2732 trace
= bt_ctf_trace_create();
2734 ret
= bt_ctf_trace_add_is_static_listener(NULL
,
2735 trace_is_static_listener
, trace_listener_removed
, &called1
);
2736 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (trace)");
2737 ret
= bt_ctf_trace_add_is_static_listener(trace
, NULL
,
2738 trace_listener_removed
, &called1
);
2739 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (listener)");
2740 listener1_id
= bt_ctf_trace_add_is_static_listener(trace
,
2741 trace_is_static_listener
, trace_listener_removed
, &called1
);
2742 ok(listener1_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (1)");
2743 listener2_id
= bt_ctf_trace_add_is_static_listener(trace
,
2744 trace_is_static_listener
, trace_listener_removed
, &called2
);
2745 ok(listener2_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (2)");
2746 listener3_id
= bt_ctf_trace_add_is_static_listener(trace
,
2747 trace_is_static_listener
, trace_listener_removed
, &called3
);
2748 ok(listener3_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (3)");
2749 ret
= bt_ctf_trace_remove_is_static_listener(NULL
, 0);
2750 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles NULL (trace)");
2751 ret
= bt_ctf_trace_remove_is_static_listener(trace
, -2);
2752 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (negative)");
2753 ret
= bt_ctf_trace_remove_is_static_listener(trace
, 77);
2754 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (non existing)");
2755 ret
= bt_ctf_trace_remove_is_static_listener(trace
, listener2_id
);
2756 ok(ret
== 0, "bt_ctf_trace_remove_is_static_listener() succeeds");
2757 ok(called2
== 2, "bt_ctf_trace_remove_is_static_listener() calls the remove listener");
2758 listener4_id
= bt_ctf_trace_add_is_static_listener(trace
,
2759 trace_is_static_listener
, NULL
, &called4
);
2760 ok(listener4_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (4)");
2761 ok(called1
== 0, "\"trace is static\" listener not called before the trace is made static (1)");
2762 ok(called2
== 2, "\"trace is static\" listener not called before the trace is made static (2)");
2763 ok(called3
== 0, "\"trace is static\" listener not called before the trace is made static (3)");
2764 ok(called4
== 0, "\"trace is static\" listener not called before the trace is made static (4)");
2765 ret
= bt_ctf_trace_set_is_static(trace
);
2767 ret
= bt_ctf_trace_add_is_static_listener(trace
,
2768 trace_is_static_listener
, trace_listener_removed
, &called1
);
2770 "bt_ctf_trace_add_is_static_listener() fails when the trace is static");
2771 ok(called1
== 1, "\"trace is static\" listener called when the trace is made static (1)");
2772 ok(called2
== 2, "\"trace is static\" listener not called when the trace is made static (2)");
2773 ok(called3
== 1, "\"trace is static\" listener called when the trace is made static (3)");
2774 ok(called4
== 1, "\"trace is static\" listener called when the trace is made static (4)");
2780 ok(called1
== 2, "\"trace is static\" listener not called after the trace is put (1)");
2781 ok(called2
== 0, "\"trace is static\" listener not called after the trace is put (2)");
2782 ok(called3
== 2, "\"trace is static\" listener not called after the trace is put (3)");
2783 ok(called4
== 0, "\"trace is static\" listener not called after the trace is put (4)");
2787 void test_trace_uuid(void)
2789 struct bt_ctf_trace
*trace
;
2790 const unsigned char uuid
[] = {
2791 0x35, 0x92, 0x63, 0xab, 0xb4, 0xbe, 0x40, 0xb4,
2792 0xb2, 0x60, 0xd3, 0xf1, 0x3b, 0xb0, 0xd8, 0x59,
2794 const unsigned char *ret_uuid
;
2796 trace
= bt_ctf_trace_create();
2798 ok(!bt_ctf_trace_get_uuid(NULL
),
2799 "bt_ctf_trace_get_uuid() handles NULL");
2800 ok(!bt_ctf_trace_get_uuid(trace
),
2801 "bt_ctf_trace_get_uuid() returns NULL initially");
2802 ok(bt_ctf_trace_set_uuid(NULL
, uuid
),
2803 "bt_ctf_trace_set_uuid() handles NULL (trace)");
2804 ok(bt_ctf_trace_set_uuid(trace
, NULL
),
2805 "bt_ctf_trace_set_uuid() handles NULL (UUID)");
2806 ok(bt_ctf_trace_set_uuid(trace
, uuid
) == 0,
2807 "bt_ctf_trace_set_uuid() succeeds with a valid UUID");
2808 ret_uuid
= bt_ctf_trace_get_uuid(trace
);
2809 ok(ret_uuid
, "bt_ctf_trace_get_uuid() returns a UUID");
2811 ok(memcmp(uuid
, ret_uuid
, 16) == 0,
2812 "bt_ctf_trace_get_uuid() returns the expected UUID");
2817 int main(int argc
, char **argv
)
2819 const char *env_resize_length
;
2821 gchar
*metadata_path
;
2822 const char *clock_name
= "test_clock";
2823 const char *clock_description
= "This is a test clock";
2824 const char *returned_clock_name
;
2825 const char *returned_clock_description
;
2826 const uint64_t frequency
= 1123456789;
2827 const int64_t offset_s
= 13515309;
2828 const int64_t offset
= 1234567;
2829 int64_t get_offset_s
,
2831 const uint64_t precision
= 10;
2832 const int is_absolute
= 0xFF;
2833 char *metadata_string
;
2834 struct bt_ctf_writer
*writer
;
2835 struct bt_utsname name
= {"GNU/Linux", "testhost", "4.4.0-87-generic",
2836 "#110-Ubuntu SMP Tue Jul 18 12:55:35 UTC 2017", "x86_64"};
2837 struct bt_ctf_clock
*clock
, *ret_clock
;
2838 struct bt_ctf_clock_class
*ret_clock_class
;
2839 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2840 struct bt_ctf_stream
*stream1
;
2841 struct bt_ctf_stream
*stream
;
2842 const char *ret_string
;
2843 const unsigned char *ret_uuid
;
2844 unsigned char tmp_uuid
[16] = { 0 };
2845 struct bt_ctf_field_type
*packet_context_type
,
2846 *packet_context_field_type
,
2847 *packet_header_type
,
2848 *packet_header_field_type
,
2850 *stream_event_context_type
,
2852 *event_header_field_type
;
2853 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2854 struct bt_ctf_trace
*trace
;
2856 int64_t ret_int64_t
;
2857 struct bt_value
*obj
;
2860 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
2864 env_resize_length
= getenv("PACKET_RESIZE_TEST_LENGTH");
2865 if (env_resize_length
) {
2866 packet_resize_test_length
=
2867 (unsigned int) atoi(env_resize_length
);
2870 plan_tests(NR_TESTS
);
2872 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
2873 if (!bt_mkdtemp(trace_path
)) {
2877 metadata_path
= g_build_filename(trace_path
, "metadata", NULL
);
2879 writer
= bt_ctf_writer_create(trace_path
);
2880 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2882 ok(!bt_ctf_writer_get_trace(NULL
),
2883 "bt_ctf_writer_get_trace correctly handles NULL");
2884 trace
= bt_ctf_writer_get_trace(writer
);
2885 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
),
2886 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_NATIVE");
2887 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_UNSPECIFIED
),
2888 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_UNSPECIFIED");
2890 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2891 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2892 "Set a trace's byte order to big endian");
2893 ok(bt_ctf_trace_get_native_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2894 "bt_ctf_trace_get_native_byte_order returns a correct endianness");
2896 /* Add environment context to the trace */
2897 ok(bt_ctf_writer_add_environment_field(writer
, "host", name
.nodename
) == 0,
2898 "Add host (%s) environment field to writer instance",
2900 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2902 "bt_ctf_writer_add_environment_field error with NULL writer");
2903 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2905 "bt_ctf_writer_add_environment_field error with NULL field name");
2906 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2908 "bt_ctf_writer_add_environment_field error with NULL field value");
2910 /* Test bt_ctf_trace_set_environment_field with an integer object */
2911 obj
= bt_value_integer_create_init(23);
2913 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2914 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2915 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2916 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2917 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2918 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2919 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2920 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2923 /* Test bt_ctf_trace_set_environment_field with a string object */
2924 obj
= bt_value_string_create_init("the value");
2926 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2927 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2930 /* Test bt_ctf_trace_set_environment_field_integer */
2931 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2933 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2934 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2935 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2936 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2938 "bt_ctf_trace_set_environment_field_integer succeeds");
2940 /* Test bt_ctf_trace_set_environment_field_string */
2941 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2943 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2944 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2945 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2946 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2948 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2949 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2951 "bt_ctf_trace_set_environment_field_string succeeds");
2953 /* Test bt_ctf_trace_get_environment_field_count */
2954 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2955 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2956 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2957 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2959 /* Test bt_ctf_trace_get_environment_field_name */
2960 ok(bt_ctf_trace_get_environment_field_name_by_index(NULL
, 0) == NULL
,
2961 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2962 ok(bt_ctf_trace_get_environment_field_name_by_index(trace
, 5) == NULL
,
2963 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2964 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 0);
2965 ok(ret_string
&& !strcmp(ret_string
, "host"),
2966 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2967 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 1);
2968 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2969 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2970 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 2);
2971 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2972 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2973 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 3);
2974 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2975 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2976 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 4);
2977 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2978 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2980 /* Test bt_ctf_trace_get_environment_field_value */
2981 ok(bt_ctf_trace_get_environment_field_value_by_index(NULL
, 0) == NULL
,
2982 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2983 ok(bt_ctf_trace_get_environment_field_value_by_index(trace
, 5) == NULL
,
2984 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2985 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 1);
2986 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2987 ok(!ret
&& ret_int64_t
== 23,
2988 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2990 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 2);
2991 ret
= bt_value_string_get(obj
, &ret_string
);
2992 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2993 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2996 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2997 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2999 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3000 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3001 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3002 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3003 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3004 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3006 ret
= bt_value_string_get(obj
, &ret_string
);
3007 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3008 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3011 /* Test environment field replacement */
3012 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3014 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3015 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3016 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3017 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 3);
3018 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3019 ok(!ret
&& ret_int64_t
== 654321,
3020 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3023 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3024 == 0, "Add sysname (%s) environment field to writer instance",
3026 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3027 name
.nodename
) == 0,
3028 "Add nodename (%s) environment field to writer instance",
3030 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3031 == 0, "Add release (%s) environment field to writer instance",
3033 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3034 == 0, "Add version (%s) environment field to writer instance",
3036 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3037 == 0, "Add machine (%s) environment field to writer istance",
3040 /* Define a clock and add it to the trace */
3041 ok(bt_ctf_clock_create("signed") == NULL
,
3042 "Illegal clock name rejected");
3043 clock
= bt_ctf_clock_create(clock_name
);
3044 ok(clock
, "Clock created sucessfully");
3045 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3046 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3047 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3048 "Returned clock name is valid");
3050 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3051 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3052 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3053 "Clock description set successfully");
3055 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3056 ok(returned_clock_description
,
3057 "bt_ctf_clock_get_description returns a description.");
3058 ok(returned_clock_description
?
3059 !strcmp(returned_clock_description
, clock_description
) : 0,
3060 "Returned clock description is valid");
3062 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3063 "bt_ctf_clock_get_frequency returns the correct default frequency");
3064 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3065 "Set clock frequency");
3066 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3067 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3069 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3070 "bt_ctf_clock_get_offset_s succeeds");
3071 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3072 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3073 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3074 "Set clock offset (seconds)");
3075 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3076 "bt_ctf_clock_get_offset_s succeeds");
3077 ok(get_offset_s
== offset_s
,
3078 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3080 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3081 "bt_ctf_clock_get_offset succeeds");
3082 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3083 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3084 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3085 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3086 "bt_ctf_clock_get_offset succeeds");
3087 ok(get_offset
== offset
,
3088 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3090 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3091 "bt_ctf_clock_get_precision returns the correct default precision");
3092 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3093 "Set clock precision");
3094 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3095 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3097 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3098 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3099 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3100 "Set clock absolute property");
3101 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3102 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3104 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3107 ok(!bt_ctf_clock_get_name(NULL
),
3108 "bt_ctf_clock_get_name correctly handles NULL");
3109 ok(!bt_ctf_clock_get_description(NULL
),
3110 "bt_ctf_clock_get_description correctly handles NULL");
3111 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3112 "bt_ctf_clock_get_frequency correctly handles NULL");
3113 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3114 "bt_ctf_clock_get_precision correctly handles NULL");
3115 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3116 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3117 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3118 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3119 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3120 "bt_ctf_clock_get_offset correctly handles NULL clock");
3121 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3122 "bt_ctf_clock_get_offset correctly handles NULL output");
3123 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3124 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3126 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3127 "bt_ctf_clock_set_description correctly handles NULL clock");
3128 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3129 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3130 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3131 "bt_ctf_clock_get_precision correctly handles NULL clock");
3132 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3133 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3134 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3135 "bt_ctf_clock_set_offset correctly handles NULL clock");
3136 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3137 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3138 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3139 "bt_ctf_clock_set_time correctly handles NULL clock");
3140 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3141 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3142 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3144 "bt_ctf_clock_get_uuid returns a UUID");
3146 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3147 /* Slightly modify UUID */
3148 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3151 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3152 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3153 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3154 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3155 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3156 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3157 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3159 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3160 ok(uuid_match(ret_uuid
, tmp_uuid
),
3161 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3163 /* Define a stream class */
3164 stream_class
= bt_ctf_stream_class_create("test_stream");
3166 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3167 "bt_ctf_stream_class_get_name handles NULL correctly");
3168 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3169 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3170 "bt_ctf_stream_class_get_name returns a correct stream class name");
3172 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3173 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3174 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3175 "bt_ctf_stream_class_get_clock handles NULL correctly");
3177 ok(stream_class
, "Create stream class");
3178 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3179 "Set a stream class' clock");
3180 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3181 ok(ret_clock
== clock
,
3182 "bt_ctf_stream_class_get_clock returns a correct clock");
3185 /* Test the event fields and event types APIs */
3188 /* Test fields copying */
3191 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3192 "bt_ctf_stream_class_get_id returns an error when no id is set");
3193 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3194 "bt_ctf_stream_class_get_id handles NULL correctly");
3195 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3196 "bt_ctf_stream_class_set_id handles NULL correctly");
3197 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3198 "Set an stream class' id");
3199 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3200 "bt_ctf_stream_class_get_id returns the correct value");
3202 /* Validate default event header fields */
3203 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3204 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3205 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3208 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3209 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3210 "Default event header type is a structure");
3211 event_header_field_type
=
3212 bt_ctf_field_type_structure_get_field_type_by_name(
3213 ret_field_type
, "id");
3214 ok(event_header_field_type
,
3215 "Default event header type contains an \"id\" field");
3216 ok(bt_ctf_field_type_get_type_id(
3217 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3218 "Default event header \"id\" field is an integer");
3219 bt_put(event_header_field_type
);
3220 event_header_field_type
=
3221 bt_ctf_field_type_structure_get_field_type_by_name(
3222 ret_field_type
, "timestamp");
3223 ok(event_header_field_type
,
3224 "Default event header type contains a \"timestamp\" field");
3225 ok(bt_ctf_field_type_get_type_id(
3226 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3227 "Default event header \"timestamp\" field is an integer");
3228 bt_put(event_header_field_type
);
3229 bt_put(ret_field_type
);
3231 /* Add a custom trace packet header field */
3232 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3233 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3234 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3235 ok(packet_header_type
,
3236 "bt_ctf_trace_get_packet_header_type returns a packet header");
3237 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3238 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3239 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3240 packet_header_type
, "magic");
3241 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3242 bt_put(ret_field_type
);
3243 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3244 packet_header_type
, "uuid");
3245 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3246 bt_put(ret_field_type
);
3247 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3248 packet_header_type
, "stream_id");
3249 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3250 bt_put(ret_field_type
);
3252 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3253 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3254 packet_header_field_type
, "custom_trace_packet_header_field"),
3255 "Added a custom trace packet header field successfully");
3257 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3258 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3259 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3260 "Set a trace packet_header_type successfully");
3262 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3263 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3265 /* Add a custom field to the stream class' packet context */
3266 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3267 ok(packet_context_type
,
3268 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3269 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3270 "Packet context is a structure");
3272 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3273 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3275 integer_type
= bt_ctf_field_type_integer_create(32);
3276 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3278 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3279 /* Create a "uint5_t" equivalent custom packet context field */
3280 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3282 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3283 packet_context_field_type
, "custom_packet_context_field");
3284 ok(ret
== 0, "Packet context field added successfully");
3286 /* Define a stream event context containing a my_integer field. */
3287 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3288 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3289 stream_event_context_type
= bt_ctf_field_type_structure_create();
3290 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3291 integer_type
, "common_event_context");
3293 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3294 stream_event_context_type
) < 0,
3295 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3296 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3298 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3300 ok(bt_ctf_stream_class_set_event_context_type(
3301 stream_class
, stream_event_context_type
) == 0,
3302 "Set a new stream event context type");
3303 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3305 ok(ret_field_type
== stream_event_context_type
,
3306 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3307 bt_put(ret_field_type
);
3309 /* Instantiate a stream and append events */
3310 ret
= bt_ctf_writer_add_clock(writer
, clock
);
3312 ok(bt_ctf_trace_get_stream_count(trace
) == 0,
3313 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)");
3314 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3315 ok(stream1
, "Instanciate a stream class from writer");
3316 ok(bt_ctf_trace_get_stream_count(trace
) == 1,
3317 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)");
3318 stream
= bt_ctf_trace_get_stream_by_index(trace
, 0);
3319 ok(stream
== stream1
,
3320 "bt_ctf_trace_get_stream_by_index() succeeds and returns the correct value");
3324 * Creating a stream through a writer adds the given stream
3325 * class to the writer's trace, thus registering the stream
3326 * class's clock to the trace.
3328 ok(bt_ctf_trace_get_clock_class_count(NULL
) < 0,
3329 "bt_ctf_trace_get_clock_class_count correctly handles NULL");
3330 ok(bt_ctf_trace_get_clock_class_count(trace
) == 1,
3331 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
3332 ok(!bt_ctf_trace_get_clock_class_by_index(NULL
, 0),
3333 "bt_ctf_trace_get_clock_class correctly handles NULL");
3334 ok(!bt_ctf_trace_get_clock_class_by_index(trace
, 1),
3335 "bt_ctf_trace_get_clock_class correctly handles out of bound accesses");
3336 ret_clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
3337 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3338 bt_ctf_clock_get_name(clock
)) == 0,
3339 "bt_ctf_trace_get_clock_class returns the right clock instance");
3340 bt_put(ret_clock_class
);
3341 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, NULL
),
3342 "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)");
3343 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, clock_name
),
3344 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3345 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, NULL
),
3346 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3347 ret_clock_class
= bt_ctf_trace_get_clock_class_by_name(trace
, clock_name
);
3348 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3349 bt_ctf_clock_get_name(clock
)) == 0,
3350 "bt_ctf_trace_get_clock_class returns the right clock instance");
3351 bt_put(ret_clock_class
);
3352 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, "random"),
3353 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3355 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3356 "bt_ctf_stream_get_class correctly handles NULL");
3357 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3358 ok(ret_stream_class
,
3359 "bt_ctf_stream_get_class returns a stream class");
3360 ok(ret_stream_class
== stream_class
,
3361 "Returned stream class is of the correct type");
3364 * Packet header, packet context, event header, and stream
3365 * event context types were copied for the resolving
3368 BT_PUT(packet_header_type
);
3369 BT_PUT(packet_context_type
);
3370 BT_PUT(stream_event_context_type
);
3371 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3372 assert(packet_header_type
);
3373 packet_context_type
=
3374 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3375 assert(packet_context_type
);
3376 stream_event_context_type
=
3377 bt_ctf_stream_class_get_event_context_type(stream_class
);
3378 assert(stream_event_context_type
);
3381 * Try to modify the packet context type after a stream has been
3384 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3385 packet_header_field_type
, "should_fail");
3387 "Trace packet header type can't be modified once a stream has been instanciated");
3390 * Try to modify the packet context type after a stream has been
3393 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3394 packet_context_field_type
, "should_fail");
3396 "Packet context type can't be modified once a stream has been instanciated");
3399 * Try to modify the stream event context type after a stream has been
3402 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3403 integer_type
, "should_fail");
3405 "Stream event context type can't be modified once a stream has been instanciated");
3407 /* Should fail after instanciating a stream (frozen) */
3408 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3409 "Changes to a stream class that was already instantiated fail");
3411 /* Populate the custom packet header field only once for all tests */
3412 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3413 "bt_ctf_stream_get_packet_header handles NULL correctly");
3414 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3416 "bt_ctf_stream_get_packet_header returns a packet header");
3417 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3418 ok(ret_field_type
== packet_header_type
,
3419 "Stream returns a packet header of the appropriate type");
3420 bt_put(ret_field_type
);
3421 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3422 "custom_trace_packet_header_field");
3423 ok(packet_header_field
,
3424 "Packet header structure contains a custom field with the appropriate name");
3425 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3426 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3427 "Custom packet header field is of the expected type");
3428 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3429 54321), "Set custom packet header value successfully");
3430 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3431 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3432 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3433 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3434 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3435 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3436 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3437 "Successfully set a stream's packet header");
3439 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3440 "Add environment field to writer after stream creation");
3444 test_create_writer_vs_non_writer_mode();
3446 test_set_clock_non_writer_stream_class();
3448 test_instanciate_event_before_stream(writer
, clock
);
3450 append_simple_event(stream_class
, stream1
, clock
);
3452 packet_resize_test(stream_class
, stream1
, clock
);
3454 append_complex_event(stream_class
, stream1
, clock
);
3456 append_existing_event_class(stream_class
);
3458 test_empty_stream(writer
);
3460 test_custom_event_header_stream(writer
, clock
);
3462 test_static_trace();
3464 test_trace_is_static_listener();
3468 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3469 ok(metadata_string
, "Get metadata string");
3471 bt_ctf_writer_flush_metadata(writer
);
3474 bt_put(ret_stream_class
);
3477 bt_put(packet_context_type
);
3478 bt_put(packet_context_field_type
);
3479 bt_put(integer_type
);
3480 bt_put(stream_event_context_type
);
3481 bt_put(ret_field_type
);
3482 bt_put(packet_header_type
);
3483 bt_put(packet_header_field_type
);
3484 bt_put(packet_header
);
3485 bt_put(packet_header_field
);
3487 free(metadata_string
);
3488 bt_put(stream_class
);
3490 validate_trace(argv
[1], trace_path
);
3492 recursive_rmdir(trace_path
);
3494 g_free(metadata_path
);