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
;
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 if(!g_spawn_check_exit_status(exit_status
, NULL
)) {
126 diag("Babeltrace returned an error.");
127 diag_multiline(standard_error
);
133 ok(ret
== 0, "Babeltrace could read the resulting trace");
134 g_free(standard_error
);
138 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
139 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
141 /* Create and add a simple event class */
142 struct bt_ctf_event_class
*simple_event_class
=
143 bt_ctf_event_class_create("Simple Event");
144 struct bt_ctf_field_type
*uint_12_type
=
145 bt_ctf_field_type_integer_create(12);
146 struct bt_ctf_field_type
*int_64_type
=
147 bt_ctf_field_type_integer_create(64);
148 struct bt_ctf_field_type
*float_type
=
149 bt_ctf_field_type_floating_point_create();
150 struct bt_ctf_field_type
*enum_type
;
151 struct bt_ctf_field_type
*enum_type_unsigned
=
152 bt_ctf_field_type_enumeration_create(uint_12_type
);
153 struct bt_ctf_field_type
*event_context_type
=
154 bt_ctf_field_type_structure_create();
155 struct bt_ctf_field_type
*event_payload_type
= NULL
;
156 struct bt_ctf_field_type
*returned_type
;
157 struct bt_ctf_event
*simple_event
;
158 struct bt_ctf_field
*integer_field
;
159 struct bt_ctf_field
*float_field
;
160 struct bt_ctf_field
*enum_field
;
161 struct bt_ctf_field
*enum_field_unsigned
;
162 struct bt_ctf_field
*enum_container_field
;
163 const char *mapping_name_test
= "truie";
164 const double double_test_value
= 3.1415;
165 struct bt_ctf_field
*enum_container_field_unsigned
;
166 const char *mapping_name_negative_test
= "negative_value";
167 const char *ret_char
;
169 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
170 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
171 struct bt_ctf_event_class
*ret_event_class
;
172 struct bt_ctf_field
*packet_context
;
173 struct bt_ctf_field
*packet_context_field
;
174 struct bt_ctf_field
*stream_event_context
;
175 struct bt_ctf_field
*stream_event_context_field
;
176 struct bt_ctf_field
*event_context
;
177 struct bt_ctf_field
*event_context_field
;
178 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
179 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
180 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
181 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
184 ok(uint_12_type
, "Create an unsigned integer type");
186 ok(!bt_ctf_field_type_integer_set_signed(int_64_type
, 1),
187 "Set signed 64 bit integer signedness to true");
188 ok(int_64_type
, "Create a signed integer type");
189 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
191 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
192 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
193 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
194 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
195 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
196 bt_put(returned_type
);
198 bt_ctf_field_type_set_alignment(float_type
, 32);
199 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
200 "bt_ctf_field_type_get_alignment handles NULL correctly");
201 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
202 "bt_ctf_field_type_get_alignment returns a correct value");
204 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
205 "Set a floating point type's exponent digit count");
206 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
207 "Set a floating point type's mantissa digit count");
209 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
210 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
211 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
212 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
213 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
214 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
215 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
216 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
218 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
219 mapping_name_negative_test
, -12345, 0) == 0,
220 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
221 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
222 "escaping; \"test\"", 1, 1) == 0,
223 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
224 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
225 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
226 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
227 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
228 "event clock int float", 5, 22) == 0,
229 "Accept enumeration mapping strings containing reserved keywords");
230 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
232 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
233 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
234 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
235 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
236 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
237 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
238 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
240 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(NULL
, -42);
241 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly");
243 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, -4200000);
244 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values");
246 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, 3);
247 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value");
248 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, NULL
, NULL
, NULL
) == 0,
249 "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly");
252 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
253 "enum_field") == 0, "Add signed enumeration field to event");
255 ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL
, 0, &ret_char
,
256 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
257 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly");
258 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, NULL
,
259 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
260 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly");
261 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
262 NULL
, &ret_range_end_int64_t
) == 0,
263 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly");
264 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
265 &ret_range_start_int64_t
, NULL
) == 0,
266 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly");
267 /* Assumes entries are sorted by range_start values. */
268 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 6, &ret_char
,
269 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
270 "bt_ctf_field_type_enumeration_get_mapping_signed returns a value");
271 ok(!strcmp(ret_char
, mapping_name_test
),
272 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name");
273 ok(ret_range_start_int64_t
== 42,
274 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start");
275 ok(ret_range_end_int64_t
== 42,
276 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end");
278 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
279 "escaping; \"test\"", 0, 0) == 0,
280 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
281 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
282 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
283 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
284 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
285 "event clock int float", 5, 22) == 0,
286 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
287 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
288 42, 42) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts single-value ranges");
289 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
290 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names");
291 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
292 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries");
293 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
294 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
295 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
296 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
298 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
299 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
300 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 6,
301 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
303 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
304 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
305 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
306 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
307 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
308 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
309 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
310 NULL
, &ret_range_end_uint64_t
) == 0,
311 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
312 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
313 &ret_range_start_uint64_t
, NULL
) == 0,
314 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
315 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 4, &ret_char
,
316 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
317 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
318 ok(!strcmp(ret_char
, mapping_name_test
),
319 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
320 ok(ret_range_start_uint64_t
== 42,
321 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
322 ok(ret_range_end_uint64_t
== 42,
323 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
325 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
327 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
330 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
332 /* Set an event context type which will contain a single integer. */
333 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
334 "event_specific_context"),
335 "Add event specific context field");
336 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
337 "bt_ctf_event_class_get_context_type handles NULL correctly");
339 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
340 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
341 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
342 "Set an event class' context type successfully");
343 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
344 ok(returned_type
== event_context_type
,
345 "bt_ctf_event_class_get_context_type returns the appropriate type");
346 bt_put(returned_type
);
348 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
349 "Adding simple event class to stream class");
352 * bt_ctf_stream_class_add_event_class() copies the field types
353 * of simple_event_class, so we retrieve the new ones to create
354 * the appropriate fields.
356 BT_PUT(event_context_type
);
357 BT_PUT(event_payload_type
);
358 event_payload_type
= bt_ctf_event_class_get_payload_type(
360 assert(event_payload_type
);
361 event_context_type
= bt_ctf_event_class_get_context_type(
363 assert(event_context_type
);
364 ep_integer_field_type
=
365 bt_ctf_field_type_structure_get_field_type_by_name(
366 event_payload_type
, "integer_field");
367 assert(ep_integer_field_type
);
369 bt_ctf_field_type_structure_get_field_type_by_name(
370 event_payload_type
, "enum_field");
371 assert(ep_enum_field_type
);
372 ep_enum_field_unsigned_type
=
373 bt_ctf_field_type_structure_get_field_type_by_name(
374 event_payload_type
, "enum_field_unsigned");
375 assert(ep_enum_field_unsigned_type
);
377 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
378 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
379 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
380 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
381 ok(bt_ctf_stream_class_get_event_class_by_index(NULL
, 0) == NULL
,
382 "bt_ctf_stream_class_get_event_class handles NULL correctly");
383 ok(bt_ctf_stream_class_get_event_class_by_index(stream_class
, 8724) == NULL
,
384 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
385 ret_event_class
= bt_ctf_stream_class_get_event_class_by_index(stream_class
, 0);
386 ok(ret_event_class
== simple_event_class
,
387 "bt_ctf_stream_class_get_event_class returns the correct event class");
388 bt_put(ret_event_class
);
389 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
390 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
391 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
392 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
394 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
395 ok(ret_event_class
== simple_event_class
,
396 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
397 bt_put(ret_event_class
);
399 simple_event
= bt_ctf_event_create(simple_event_class
);
401 "Instantiate an event containing a single integer field");
403 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
404 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
405 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
406 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
408 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
409 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
410 "bt_ctf_field_floating_point_get_value fails on an unset float field");
411 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
412 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
413 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
414 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
415 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
416 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
417 "bt_ctf_field_floating_point_get_value returns a double value");
418 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
419 "bt_ctf_field_floating_point_get_value returns a correct value");
421 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
424 iter
= bt_ctf_field_enumeration_get_mappings(NULL
);
425 ok(!iter
, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
426 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
427 ok(!iter
, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
428 enum_container_field
= bt_ctf_field_enumeration_get_container(
430 ok(bt_ctf_field_signed_integer_set_value(
431 enum_container_field
, -42) == 0,
432 "Set signed enumeration container value");
433 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
434 ok(iter
, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
435 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, &ret_char
, NULL
, NULL
);
436 ok(!ret
&& ret_char
, "bt_ctf_field_type_enumeration_mapping_iterator_get_signed return a mapping name");
438 ok(!strcmp(ret_char
, mapping_name_negative_test
),
439 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
440 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
444 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
445 assert(enum_field_unsigned
);
446 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
447 enum_field_unsigned
);
448 ok(bt_ctf_field_unsigned_integer_set_value(
449 enum_container_field_unsigned
, 42) == 0,
450 "Set unsigned enumeration container value");
451 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
452 enum_field_unsigned
);
454 iter
= bt_ctf_field_enumeration_get_mappings(enum_field_unsigned
);
456 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, &ret_char
, NULL
, NULL
);
457 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
458 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container");
460 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
462 /* Populate stream event context */
463 stream_event_context
=
464 bt_ctf_event_get_stream_event_context(simple_event
);
465 assert(stream_event_context
);
466 stream_event_context_field
= bt_ctf_field_structure_get_field(
467 stream_event_context
, "common_event_context");
468 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
470 /* Populate the event's context */
471 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
472 "bt_ctf_event_get_event_context handles NULL correctly");
473 event_context
= bt_ctf_event_get_event_context(simple_event
);
475 "bt_ctf_event_get_event_context returns a field");
476 returned_type
= bt_ctf_field_get_type(event_context
);
477 ok(returned_type
== event_context_type
,
478 "bt_ctf_event_get_event_context returns a field of the appropriate type");
479 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
480 "event_specific_context");
481 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
482 "Successfully set an event context's value");
483 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
484 "bt_ctf_event_set_event_context handles a NULL event correctly");
485 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
486 "bt_ctf_event_set_event_context handles a NULL event context correctly");
487 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
488 "bt_ctf_event_set_event_context rejects a context of the wrong type");
489 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
490 "Set an event context successfully");
492 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
493 "Append simple event to trace stream");
495 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
496 "bt_ctf_stream_get_packet_context handles NULL correctly");
497 packet_context
= bt_ctf_stream_get_packet_context(stream
);
499 "bt_ctf_stream_get_packet_context returns a packet context");
501 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
503 ok(packet_context_field
,
504 "Packet context contains the default packet_size field.");
505 bt_put(packet_context_field
);
506 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
507 "custom_packet_context_field");
508 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
509 "Custom packet context field value successfully set.");
511 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
512 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
513 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
514 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
515 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
516 "Successfully set a stream's packet context");
518 ok(bt_ctf_stream_flush(stream
) == 0,
519 "Flush trace stream with one event");
521 bt_put(simple_event_class
);
522 bt_put(simple_event
);
523 bt_put(uint_12_type
);
527 bt_put(enum_type_unsigned
);
528 bt_put(returned_type
);
529 bt_put(event_context_type
);
530 bt_put(integer_field
);
533 bt_put(enum_field_unsigned
);
534 bt_put(enum_container_field
);
535 bt_put(enum_container_field_unsigned
);
536 bt_put(packet_context
);
537 bt_put(packet_context_field
);
538 bt_put(stream_event_context
);
539 bt_put(stream_event_context_field
);
540 bt_put(event_context
);
541 bt_put(event_context_field
);
542 bt_put(event_payload_type
);
543 bt_put(ep_integer_field_type
);
544 bt_put(ep_enum_field_type
);
545 bt_put(ep_enum_field_unsigned_type
);
550 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
551 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
554 struct event_class_attrs_counts
;
555 const char *complex_test_event_string
= "Complex Test Event";
556 const char *test_string_1
= "Test ";
557 const char *test_string_2
= "string ";
558 const char *test_string_3
= "abcdefghi";
559 const char *test_string_4
= "abcd\0efg\0hi";
560 const char *test_string_cat
= "Test string abcdeabcd";
561 struct bt_ctf_field_type
*uint_35_type
=
562 bt_ctf_field_type_integer_create(35);
563 struct bt_ctf_field_type
*int_16_type
=
564 bt_ctf_field_type_integer_create(16);
565 struct bt_ctf_field_type
*uint_3_type
=
566 bt_ctf_field_type_integer_create(3);
567 struct bt_ctf_field_type
*enum_variant_type
=
568 bt_ctf_field_type_enumeration_create(uint_3_type
);
569 struct bt_ctf_field_type
*variant_type
=
570 bt_ctf_field_type_variant_create(enum_variant_type
,
572 struct bt_ctf_field_type
*string_type
=
573 bt_ctf_field_type_string_create();
574 struct bt_ctf_field_type
*sequence_type
;
575 struct bt_ctf_field_type
*array_type
;
576 struct bt_ctf_field_type
*inner_structure_type
=
577 bt_ctf_field_type_structure_create();
578 struct bt_ctf_field_type
*complex_structure_type
=
579 bt_ctf_field_type_structure_create();
580 struct bt_ctf_field_type
*ret_field_type
;
581 struct bt_ctf_event_class
*event_class
;
582 struct bt_ctf_event
*event
;
583 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
584 *inner_structure_field
, *complex_structure_field
,
585 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
586 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
587 *stream_event_ctx_int_field
, *ret_field
;
588 uint64_t ret_unsigned_int
;
589 int64_t ret_signed_int
;
590 const char *ret_string
;
591 struct bt_ctf_stream_class
*ret_stream_class
;
592 struct bt_ctf_event_class
*ret_event_class
;
593 struct bt_ctf_field
*packet_context
, *packet_context_field
;
594 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
596 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
597 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
598 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
599 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
600 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
601 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
602 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
603 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
604 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
605 "Set alignment of signed 16 bit integer to 32");
606 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
607 "Set integer signedness to true");
608 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
609 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
610 "Set signed 16 bit integer base to hexadecimal");
612 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
613 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
616 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
617 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
618 ret_field_type
= bt_ctf_field_type_array_get_element_type(
620 ok(ret_field_type
== int_16_type
,
621 "bt_ctf_field_type_array_get_element_type returns the correct type");
622 bt_put(ret_field_type
);
624 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
625 "bt_ctf_field_type_array_get_length handles NULL correctly");
626 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
627 "bt_ctf_field_type_array_get_length returns the correct length");
629 ok(bt_ctf_field_type_structure_add_field(inner_structure_type
,
630 inner_structure_type
, "yes"), "Cannot add self to structure");
631 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
632 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
633 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
634 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
635 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
636 array_type
, "an_array"), "Add an_array field to inner structure");
638 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
640 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
642 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
643 "UINT35_TYPE", 2, 7);
645 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(NULL
, "INT16_TYPE");
646 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly");
648 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, "INT16_TYPE");
649 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly");
650 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
651 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly");
654 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, NULL
);
655 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly");
657 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL
, 1);
658 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly");
660 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, -42);
661 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly");
662 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) != 0,
663 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly");
666 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, 5);
667 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly");
668 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
669 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly");
672 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
673 "An unknown entry"), "Reject a variant field based on an unknown tag value");
674 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
675 "UINT3_TYPE") == 0, "Add a field to a variant");
676 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
677 "INT16_TYPE"), "Add INT16_TYPE field to variant");
678 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
679 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
681 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
682 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
683 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
684 ok(ret_field_type
== enum_variant_type
,
685 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
686 bt_put(ret_field_type
);
688 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
689 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
690 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
691 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
692 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
693 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
694 "INT16_TYPE") == NULL
,
695 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
696 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
698 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
699 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
700 variant_type
, "INT16_TYPE");
701 ok(ret_field_type
== int_16_type
,
702 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
703 bt_put(ret_field_type
);
705 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
706 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
707 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
708 "bt_ctf_field_type_variant_get_field_count returns the correct count");
710 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
711 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
712 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
713 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
714 bt_put(ret_field_type
);
715 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
716 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
717 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
718 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
719 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
720 "bt_ctf_field_type_variant_get_field returns a field");
721 ok(!strcmp("INT16_TYPE", ret_string
),
722 "bt_ctf_field_type_variant_get_field returns a correct field name");
723 ok(ret_field_type
== int_16_type
,
724 "bt_ctf_field_type_variant_get_field returns a correct field type");
725 bt_put(ret_field_type
);
727 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
728 enum_variant_type
, "variant_selector"),
729 "Add variant_selector field to complex structure");
730 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
731 string_type
, "a_string"), "Add a_string field to complex structure");
732 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
733 variant_type
, "variant_value"),
734 "Add variant_value field to complex structure");
735 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
736 inner_structure_type
, "inner_structure"),
737 "Add inner_structure field to complex structure");
739 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
740 ok(event_class
, "Create an event class");
741 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
742 "Reject addition of a field with an empty name to an event");
743 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
744 "Reject addition of a field with a NULL type to an event");
745 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
747 "Reject addition of a type with an illegal name to an event");
748 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
750 "Add field of type unsigned integer to an event");
751 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
752 "int_16") == 0, "Add field of type signed integer to an event");
753 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
754 "complex_structure") == 0,
755 "Add composite structure to an event");
757 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
758 "bt_ctf_event_class_get_name handles NULL correctly");
759 ret_string
= bt_ctf_event_class_get_name(event_class
);
760 ok(!strcmp(ret_string
, complex_test_event_string
),
761 "bt_ctf_event_class_get_name returns a correct name");
762 ok(bt_ctf_event_class_get_id(event_class
) < 0,
763 "bt_ctf_event_class_get_id returns a negative value when not set");
764 ok(bt_ctf_event_class_get_id(NULL
) < 0,
765 "bt_ctf_event_class_get_id handles NULL correctly");
766 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
767 "bt_ctf_event_class_set_id handles NULL correctly");
768 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
769 "Set an event class' id");
770 ok(bt_ctf_event_class_get_id(event_class
) == 42,
771 "bt_ctf_event_class_get_id returns the correct value");
773 /* Test event class attributes */
774 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
,
775 "event class has the expected initial log level");
776 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
777 "as expected, event class has no initial EMF URI");
778 ok(bt_ctf_event_class_set_log_level(NULL
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
779 "bt_ctf_event_class_set_log_level handles a NULL event class correctly");
780 ok(bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
),
781 "bt_ctf_event_class_set_log_level handles an unknown log level correctly");
782 ok(!bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
783 "bt_ctf_event_class_set_log_level succeeds with a valid log level");
784 ok(bt_ctf_event_class_get_log_level(NULL
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
,
785 "bt_ctf_event_class_get_log_level handles a NULL event class correctly");
786 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
,
787 "bt_ctf_event_class_get_log_level returns the expected log level");
788 ok(bt_ctf_event_class_set_emf_uri(NULL
, "http://diamon.org/babeltrace/"),
789 "bt_ctf_event_class_set_emf_uri handles a NULL event class correctly");
790 ok(!bt_ctf_event_class_set_emf_uri(event_class
, "http://diamon.org/babeltrace/"),
791 "bt_ctf_event_class_set_emf_uri succeeds with a valid EMF URI");
792 ok(!bt_ctf_event_class_get_emf_uri(NULL
),
793 "bt_ctf_event_class_get_emf_uri handles a NULL event class correctly");
794 ok(strcmp(bt_ctf_event_class_get_emf_uri(event_class
), "http://diamon.org/babeltrace/") == 0,
795 "bt_ctf_event_class_get_emf_uri returns the expected EMF URI");
796 ok(!bt_ctf_event_class_set_emf_uri(event_class
, NULL
),
797 "bt_ctf_event_class_set_emf_uri succeeds with NULL (to reset)");
798 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
799 "as expected, event class has no EMF URI after reset");
801 /* Add event class to the stream class */
802 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
803 "Reject addition of NULL event class to a stream class");
804 ok(bt_ctf_stream_class_add_event_class(stream_class
,
805 event_class
) == 0, "Add an event class to stream class");
807 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
808 "bt_ctf_event_class_get_stream_class handles NULL correctly");
809 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
810 ok(ret_stream_class
== stream_class
,
811 "bt_ctf_event_class_get_stream_class returns the correct stream class");
812 bt_put(ret_stream_class
);
814 ok(bt_ctf_event_class_get_payload_type_field_count(NULL
) < 0,
815 "bt_ctf_event_class_get_field_count handles NULL correctly");
816 ok(bt_ctf_event_class_get_payload_type_field_count(event_class
) == 3,
817 "bt_ctf_event_class_get_field_count returns a correct value");
819 ok(bt_ctf_event_class_get_payload_type_field_by_index(NULL
, &ret_string
,
820 &ret_field_type
, 0) < 0,
821 "bt_ctf_event_class_get_field handles a NULL event class correctly");
822 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, NULL
,
823 &ret_field_type
, 0) == 0,
824 "bt_ctf_event_class_get_field handles a NULL field name correctly");
825 bt_put(ret_field_type
);
826 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
828 "bt_ctf_event_class_get_field handles a NULL field type correctly");
829 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
830 &ret_field_type
, 42) < 0,
831 "bt_ctf_event_class_get_field handles an invalid index correctly");
832 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
833 &ret_field_type
, 0) == 0,
834 "bt_ctf_event_class_get_field returns a field");
835 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
836 "bt_ctf_event_class_get_field returns a correct field type");
837 bt_put(ret_field_type
);
838 ok(!strcmp(ret_string
, "uint_35"),
839 "bt_ctf_event_class_get_field returns a correct field name");
840 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
841 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
842 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
843 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
844 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
845 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
846 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
847 "complex_structure");
848 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
849 "bt_ctf_event_class_get_field_by_name returns a correct field type");
850 bt_put(ret_field_type
);
852 event
= bt_ctf_event_create(event_class
);
853 ok(event
, "Instanciate a complex event");
855 ok(bt_ctf_event_get_class(NULL
) == NULL
,
856 "bt_ctf_event_get_class handles NULL correctly");
857 ret_event_class
= bt_ctf_event_get_class(event
);
858 ok(ret_event_class
== event_class
,
859 "bt_ctf_event_get_class returns the correct event class");
860 bt_put(ret_event_class
);
862 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
863 if (!uint_35_field
) {
864 printf("uint_35_field is NULL\n");
867 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
868 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
869 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
870 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
871 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
872 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
873 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
874 &ret_unsigned_int
) == 0,
875 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
876 ok(ret_unsigned_int
== 0x0DDF00D,
877 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
878 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
879 &ret_signed_int
) < 0,
880 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
881 bt_put(uint_35_field
);
883 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
884 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
885 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
886 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
887 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
888 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
889 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
890 &ret_signed_int
) == 0,
891 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
892 ok(ret_signed_int
== -12345,
893 "bt_ctf_field_signed_integer_get_value returns the correct value");
894 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
895 &ret_unsigned_int
) < 0,
896 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
897 bt_put(int_16_field
);
899 complex_structure_field
= bt_ctf_event_get_payload(event
,
900 "complex_structure");
902 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
903 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
904 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
905 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
906 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
907 complex_structure_field
, 3);
908 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
909 bt_put(inner_structure_field
);
910 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
911 "bt_ctf_field_structure_get_field_by_index returns a correct field");
912 bt_put(ret_field_type
);
914 inner_structure_field
= bt_ctf_field_structure_get_field(
915 complex_structure_field
, "inner_structure");
916 a_string_field
= bt_ctf_field_structure_get_field(
917 complex_structure_field
, "a_string");
918 enum_variant_field
= bt_ctf_field_structure_get_field(
919 complex_structure_field
, "variant_selector");
920 variant_field
= bt_ctf_field_structure_get_field(
921 complex_structure_field
, "variant_value");
922 uint_35_field
= bt_ctf_field_structure_get_field(
923 inner_structure_field
, "seq_len");
924 a_sequence_field
= bt_ctf_field_structure_get_field(
925 inner_structure_field
, "a_sequence");
926 an_array_field
= bt_ctf_field_structure_get_field(
927 inner_structure_field
, "an_array");
929 enum_container_field
= bt_ctf_field_enumeration_get_container(
931 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
932 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
934 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
935 bt_put(int_16_field
);
936 ok(!bt_ctf_field_string_get_value(a_string_field
),
937 "bt_ctf_field_string_get_value returns NULL on an unset field");
938 bt_ctf_field_string_set_value(a_string_field
,
940 ok(!bt_ctf_field_string_get_value(NULL
),
941 "bt_ctf_field_string_get_value correctly handles NULL");
942 ok(bt_ctf_field_string_append(NULL
, "yeah"),
943 "bt_ctf_field_string_append correctly handles a NULL string field");
944 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
945 "bt_ctf_field_string_append correctly handles a NULL string value");
946 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
947 "bt_ctf_field_string_append succeeds");
948 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
949 "bt_ctf_field_string_append_len correctly handles a NULL string field");
950 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
951 "bt_ctf_field_string_append_len correctly handles a NULL string value");
952 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
953 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
954 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
955 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
956 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
957 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
958 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
959 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
961 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
962 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
963 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
964 "bt_ctf_field_string_get_value returns a correct value");
965 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
966 SEQUENCE_TEST_LENGTH
);
968 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
969 enum_container_field
) == NULL
,
970 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
971 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
973 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
974 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
975 variant_type
, enum_variant_field
);
976 ok(ret_field_type
== int_16_type
,
977 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
979 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
980 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
981 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
982 uint_35_field
) == 0, "Set a sequence field's length");
983 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
984 ok(ret_field
== uint_35_field
,
985 "bt_ctf_field_sequence_get_length returns the correct length field");
986 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
987 "bt_ctf_field_sequence_get_length properly handles NULL");
989 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
990 int_16_field
= bt_ctf_field_sequence_get_field(
991 a_sequence_field
, i
);
992 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
993 bt_put(int_16_field
);
996 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
997 int_16_field
= bt_ctf_field_array_get_field(
999 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1000 bt_put(int_16_field
);
1003 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1004 assert(stream_event_ctx_field
);
1005 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1006 stream_event_ctx_field
, "common_event_context");
1007 BT_PUT(stream_event_ctx_field
);
1008 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1009 BT_PUT(stream_event_ctx_int_field
);
1011 bt_ctf_clock_set_time(clock
, ++current_time
);
1012 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1013 "Append a complex event to a stream");
1016 * Populate the custom packet context field with a dummy value
1017 * otherwise flush will fail.
1019 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1020 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1021 "custom_packet_context_field");
1022 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1024 ok(bt_ctf_stream_flush(stream
) == 0,
1025 "Flush a stream containing a complex event");
1027 bt_put(uint_35_field
);
1028 bt_put(a_string_field
);
1029 bt_put(inner_structure_field
);
1030 bt_put(complex_structure_field
);
1031 bt_put(a_sequence_field
);
1032 bt_put(an_array_field
);
1033 bt_put(enum_variant_field
);
1034 bt_put(enum_container_field
);
1035 bt_put(variant_field
);
1037 bt_put(packet_context_field
);
1038 bt_put(packet_context
);
1039 bt_put(uint_35_type
);
1040 bt_put(int_16_type
);
1041 bt_put(string_type
);
1042 bt_put(sequence_type
);
1044 bt_put(inner_structure_type
);
1045 bt_put(complex_structure_type
);
1046 bt_put(uint_3_type
);
1047 bt_put(enum_variant_type
);
1048 bt_put(variant_type
);
1049 bt_put(ret_field_type
);
1050 bt_put(event_class
);
1055 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1056 struct bt_ctf_field_type
*expected_type
, const char *name
)
1058 struct bt_ctf_field_type
*copy_type
;
1060 copy_type
= bt_ctf_field_get_type(field
);
1061 ok(copy_type
== expected_type
,
1062 "bt_ctf_field_copy does not copy the type (%s)", name
);
1067 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1068 struct bt_ctf_field
*field_b
, const char *name
)
1070 ok(field_a
!= field_b
,
1071 "bt_ctf_field_copy creates different pointers (%s)", name
);
1075 void field_copy_tests()
1077 struct bt_ctf_field_type
*len_type
= NULL
;
1078 struct bt_ctf_field_type
*fp_type
= NULL
;
1079 struct bt_ctf_field_type
*s_type
= NULL
;
1080 struct bt_ctf_field_type
*e_int_type
= NULL
;
1081 struct bt_ctf_field_type
*e_type
= NULL
;
1082 struct bt_ctf_field_type
*v_type
= NULL
;
1083 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1084 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1085 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1086 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1087 struct bt_ctf_field_type
*strct_type
= NULL
;
1088 struct bt_ctf_field
*len
= NULL
;
1089 struct bt_ctf_field
*fp
= NULL
;
1090 struct bt_ctf_field
*s
= NULL
;
1091 struct bt_ctf_field
*e_int
= NULL
;
1092 struct bt_ctf_field
*e
= NULL
;
1093 struct bt_ctf_field
*v
= NULL
;
1094 struct bt_ctf_field
*v_selected
= NULL
;
1095 struct bt_ctf_field
*v_selected_cur
= NULL
;
1096 struct bt_ctf_field
*v_selected_0
= NULL
;
1097 struct bt_ctf_field
*v_selected_1
= NULL
;
1098 struct bt_ctf_field
*v_selected_2
= NULL
;
1099 struct bt_ctf_field
*v_selected_3
= NULL
;
1100 struct bt_ctf_field
*v_selected_4
= NULL
;
1101 struct bt_ctf_field
*v_selected_5
= NULL
;
1102 struct bt_ctf_field
*v_selected_6
= NULL
;
1103 struct bt_ctf_field
*a
= NULL
;
1104 struct bt_ctf_field
*a_0
= NULL
;
1105 struct bt_ctf_field
*a_1
= NULL
;
1106 struct bt_ctf_field
*a_2
= NULL
;
1107 struct bt_ctf_field
*a_3
= NULL
;
1108 struct bt_ctf_field
*a_4
= NULL
;
1109 struct bt_ctf_field
*strct
= NULL
;
1110 struct bt_ctf_field
*len_copy
= NULL
;
1111 struct bt_ctf_field
*fp_copy
= NULL
;
1112 struct bt_ctf_field
*s_copy
= NULL
;
1113 struct bt_ctf_field
*e_int_copy
= NULL
;
1114 struct bt_ctf_field
*e_copy
= NULL
;
1115 struct bt_ctf_field
*v_copy
= NULL
;
1116 struct bt_ctf_field
*v_selected_copy
= NULL
;
1117 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1118 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1119 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1120 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1121 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1122 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1123 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1124 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1125 struct bt_ctf_field
*a_copy
= NULL
;
1126 struct bt_ctf_field
*a_0_copy
= NULL
;
1127 struct bt_ctf_field
*a_1_copy
= NULL
;
1128 struct bt_ctf_field
*a_2_copy
= NULL
;
1129 struct bt_ctf_field
*a_3_copy
= NULL
;
1130 struct bt_ctf_field
*a_4_copy
= NULL
;
1131 struct bt_ctf_field
*strct_copy
= NULL
;
1132 struct bt_ctf_field_type_enumeration_mapping_iterator
*e_iter
= NULL
;
1133 uint64_t uint64_t_val
;
1134 const char *str_val
;
1138 /* create len type */
1139 len_type
= bt_ctf_field_type_integer_create(32);
1142 /* create fp type */
1143 fp_type
= bt_ctf_field_type_floating_point_create();
1147 s_type
= bt_ctf_field_type_string_create();
1150 /* create e_int type */
1151 e_int_type
= bt_ctf_field_type_integer_create(8);
1155 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1157 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1160 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1164 /* create v_label1 type */
1165 v_label1_type
= bt_ctf_field_type_string_create();
1166 assert(v_label1_type
);
1168 /* create v_label1_array type */
1169 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1170 assert(v_label1_array_type
);
1172 /* create v_label2 type */
1173 v_label2_type
= bt_ctf_field_type_integer_create(16);
1174 assert(v_label2_type
);
1176 /* create v_label2_seq type */
1177 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1179 assert(v_label2_seq_type
);
1182 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1184 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1187 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1191 /* create strct type */
1192 strct_type
= bt_ctf_field_type_structure_create();
1194 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1197 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1199 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1201 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1203 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1205 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1206 v_label1_array_type
, "a");
1210 strct
= bt_ctf_field_create(strct_type
);
1214 len
= bt_ctf_field_structure_get_field(strct
, "len");
1218 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1222 s
= bt_ctf_field_structure_get_field(strct
, "s");
1226 e
= bt_ctf_field_structure_get_field(strct
, "e");
1229 /* get e_int (underlying integer) */
1230 e_int
= bt_ctf_field_enumeration_get_container(e
);
1234 v
= bt_ctf_field_structure_get_field(strct
, "v");
1238 a
= bt_ctf_field_structure_get_field(strct
, "a");
1242 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1246 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1250 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1253 /* set e field (LABEL2) */
1254 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1258 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1260 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1261 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1262 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1263 ok(v_selected_cur
== v_selected
,
1264 "bt_ctf_field_variant_get_current_field returns the current field");
1265 bt_put(v_selected_cur
);
1267 /* set selected v field */
1268 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1270 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1271 assert(v_selected_0
);
1272 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1274 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1275 assert(v_selected_1
);
1276 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1278 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1279 assert(v_selected_2
);
1280 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1282 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1283 assert(v_selected_3
);
1284 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1286 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1287 assert(v_selected_4
);
1288 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1290 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1291 assert(v_selected_5
);
1292 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1294 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1295 assert(v_selected_6
);
1296 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1300 a_0
= bt_ctf_field_array_get_field(a
, 0);
1302 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1304 a_1
= bt_ctf_field_array_get_field(a
, 1);
1306 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1308 a_2
= bt_ctf_field_array_get_field(a
, 2);
1310 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1312 a_3
= bt_ctf_field_array_get_field(a
, 3);
1314 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1316 a_4
= bt_ctf_field_array_get_field(a
, 4);
1318 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1321 /* create copy of strct */
1322 ok(!bt_ctf_field_copy(NULL
),
1323 "bt_ctf_field_copy handles NULL correctly");
1324 strct_copy
= bt_ctf_field_copy(strct
);
1326 "bt_ctf_field_copy returns a valid pointer");
1328 /* get all copied fields */
1329 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1331 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1333 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1335 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1337 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1339 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1341 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1342 assert(v_selected_copy
);
1343 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1344 assert(v_selected_0_copy
);
1345 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1346 assert(v_selected_1_copy
);
1347 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1348 assert(v_selected_2_copy
);
1349 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1350 assert(v_selected_3_copy
);
1351 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1352 assert(v_selected_4_copy
);
1353 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1354 assert(v_selected_5_copy
);
1355 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1356 assert(v_selected_6_copy
);
1357 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1358 "sequence field copy is not too large");
1359 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1361 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1363 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1365 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1367 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1369 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1371 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1372 "array field copy is not too large");
1374 /* make sure copied fields are different pointers */
1375 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1376 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1377 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1378 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1379 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1380 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1381 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1382 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1384 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1386 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1388 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1390 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1392 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1394 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1396 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1398 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1399 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1400 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1401 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1402 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1403 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1405 /* make sure copied fields share the same types */
1406 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1407 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1408 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1409 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1410 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1411 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1412 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1414 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1416 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1418 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1420 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1422 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1424 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1426 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1428 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1429 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1430 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1431 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1432 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1433 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1435 /* validate len copy */
1436 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1438 ok(uint64_t_val
== 7,
1439 "bt_ctf_field_copy creates a valid integer field copy");
1441 /* validate fp copy */
1442 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1444 ok(double_val
== 3.14,
1445 "bt_ctf_field_copy creates a valid floating point number field copy");
1447 /* validate s copy */
1448 str_val
= bt_ctf_field_string_get_value(s_copy
);
1449 ok(str_val
&& !strcmp(str_val
, "btbt"),
1450 "bt_ctf_field_copy creates a valid string field copy");
1452 /* validate e_int copy */
1453 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1456 ok(uint64_t_val
== 23,
1457 "bt_ctf_field_copy creates a valid enum's integer field copy");
1459 /* validate e copy */
1460 e_iter
= bt_ctf_field_enumeration_get_mappings(e_copy
);
1461 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter
,
1462 &str_val
, NULL
, NULL
);
1463 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1464 "bt_ctf_field_copy creates a valid enum field copy");
1466 /* validate v_selected copy */
1467 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1468 assert(v_selected_copy_len
);
1469 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1472 ok(uint64_t_val
== 7,
1473 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1474 bt_put(v_selected_copy_len
);
1475 v_selected_copy_len
= NULL
;
1477 /* validate v_selected copy fields */
1478 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1481 ok(uint64_t_val
== 7,
1482 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1483 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1486 ok(uint64_t_val
== 6,
1487 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1488 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1491 ok(uint64_t_val
== 5,
1492 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1493 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1496 ok(uint64_t_val
== 4,
1497 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1498 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1501 ok(uint64_t_val
== 3,
1502 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1503 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1506 ok(uint64_t_val
== 2,
1507 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1508 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1511 ok(uint64_t_val
== 1,
1512 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1514 /* validate a copy fields */
1515 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1516 ok(str_val
&& !strcmp(str_val
, "a_0"),
1517 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1518 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1519 ok(str_val
&& !strcmp(str_val
, "a_1"),
1520 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1521 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1522 ok(str_val
&& !strcmp(str_val
, "a_2"),
1523 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1524 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1525 ok(str_val
&& !strcmp(str_val
, "a_3"),
1526 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1527 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1528 ok(str_val
&& !strcmp(str_val
, "a_4"),
1529 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1531 /* put everything */
1538 bt_put(v_label1_type
);
1539 bt_put(v_label1_array_type
);
1540 bt_put(v_label2_type
);
1541 bt_put(v_label2_seq_type
);
1550 bt_put(v_selected_0
);
1551 bt_put(v_selected_1
);
1552 bt_put(v_selected_2
);
1553 bt_put(v_selected_3
);
1554 bt_put(v_selected_4
);
1555 bt_put(v_selected_5
);
1556 bt_put(v_selected_6
);
1570 bt_put(v_selected_copy
);
1571 bt_put(v_selected_0_copy
);
1572 bt_put(v_selected_1_copy
);
1573 bt_put(v_selected_2_copy
);
1574 bt_put(v_selected_3_copy
);
1575 bt_put(v_selected_4_copy
);
1576 bt_put(v_selected_5_copy
);
1577 bt_put(v_selected_6_copy
);
1589 void type_field_tests()
1591 struct bt_ctf_field
*uint_12
;
1592 struct bt_ctf_field
*int_16
;
1593 struct bt_ctf_field
*string
;
1594 struct bt_ctf_field
*enumeration
;
1595 struct bt_ctf_field_type
*composite_structure_type
;
1596 struct bt_ctf_field_type
*structure_seq_type
;
1597 struct bt_ctf_field_type
*string_type
;
1598 struct bt_ctf_field_type
*sequence_type
;
1599 struct bt_ctf_field_type
*uint_8_type
;
1600 struct bt_ctf_field_type
*int_16_type
;
1601 struct bt_ctf_field_type
*uint_12_type
=
1602 bt_ctf_field_type_integer_create(12);
1603 struct bt_ctf_field_type
*enumeration_type
;
1604 struct bt_ctf_field_type
*returned_type
;
1605 const char *ret_string
;
1607 returned_type
= bt_ctf_field_get_type(NULL
);
1608 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1610 ok(uint_12_type
, "Create an unsigned integer type");
1611 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1612 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1613 "Set integer type's base as binary");
1614 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1615 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1616 "Set integer type's base as decimal");
1617 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1618 BT_CTF_INTEGER_BASE_UNKNOWN
),
1619 "Reject integer type's base set as unknown");
1620 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1621 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1622 "Set integer type's base as octal");
1623 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1624 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1625 "Set integer type's base as hexadecimal");
1626 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1627 "Reject unknown integer base value");
1628 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1629 "Set integer type signedness to signed");
1630 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1631 "Set integer type signedness to unsigned");
1632 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1633 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1634 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1635 "bt_ctf_field_type_integer_get_size returns a correct value");
1636 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1637 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1638 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1639 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1641 ok(bt_ctf_field_type_set_byte_order(NULL
,
1642 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1643 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1644 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1645 (enum bt_ctf_byte_order
) 42) < 0,
1646 "bt_ctf_field_type_set_byte_order rejects invalid values");
1647 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1648 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1649 "Set an integer's byte order to little endian");
1650 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1651 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1652 "Set an integer's byte order to big endian");
1653 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1654 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1655 "bt_ctf_field_type_get_byte_order returns a correct value");
1656 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1657 BT_CTF_BYTE_ORDER_UNKNOWN
,
1658 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1660 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1661 BT_CTF_FIELD_TYPE_ID_UNKNOWN
,
1662 "bt_ctf_field_type_get_type_id handles NULL correctly");
1663 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1664 BT_CTF_FIELD_TYPE_ID_INTEGER
,
1665 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1667 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1668 BT_CTF_INTEGER_BASE_UNKNOWN
,
1669 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1670 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1671 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1672 "bt_ctf_field_type_integer_get_base returns a correct value");
1674 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1675 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1676 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1677 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1678 (enum bt_ctf_string_encoding
) 123) < 0,
1679 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1680 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1681 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1682 "Set integer type encoding to UTF8");
1683 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1684 BT_CTF_STRING_ENCODING_UNKNOWN
,
1685 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1686 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1687 BT_CTF_STRING_ENCODING_UTF8
,
1688 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1690 int_16_type
= bt_ctf_field_type_integer_create(16);
1691 assert(int_16_type
);
1692 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1693 "Set signedness of 16 bit integer to true");
1694 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1695 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1696 uint_8_type
= bt_ctf_field_type_integer_create(8);
1698 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1699 ok(sequence_type
, "Create a sequence of int16_t type");
1700 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1701 BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
1702 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1704 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1705 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1706 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1708 ok(!strcmp(ret_string
, "seq_len"),
1709 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1710 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1711 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1712 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1714 ok(returned_type
== int_16_type
,
1715 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1716 bt_put(returned_type
);
1718 string_type
= bt_ctf_field_type_string_create();
1719 ok(string_type
, "Create a string type");
1720 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1721 BT_CTF_STRING_ENCODING_NONE
),
1722 "Reject invalid \"None\" string encoding");
1723 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1725 "Reject invalid string encoding");
1726 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1727 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1728 "Set string encoding to ASCII");
1730 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1731 BT_CTF_STRING_ENCODING_UNKNOWN
,
1732 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1733 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1734 BT_CTF_STRING_ENCODING_ASCII
,
1735 "bt_ctf_field_type_string_get_encoding returns the correct value");
1737 structure_seq_type
= bt_ctf_field_type_structure_create();
1738 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1739 BT_CTF_FIELD_TYPE_ID_STRUCT
,
1740 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1741 ok(structure_seq_type
, "Create a structure type");
1742 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1743 uint_8_type
, "seq_len") == 0,
1744 "Add a uint8_t type to a structure");
1745 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1746 sequence_type
, "a_sequence") == 0,
1747 "Add a sequence type to a structure");
1749 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1750 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1751 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1752 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1754 ok(bt_ctf_field_type_structure_get_field(NULL
,
1755 &ret_string
, &returned_type
, 1) < 0,
1756 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1757 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1758 NULL
, &returned_type
, 1) == 0,
1759 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1760 bt_put(returned_type
);
1761 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1762 &ret_string
, NULL
, 1) == 0,
1763 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1764 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1765 &ret_string
, &returned_type
, 10) < 0,
1766 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1767 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1768 &ret_string
, &returned_type
, 1) == 0,
1769 "bt_ctf_field_type_structure_get_field returns a field");
1770 ok(!strcmp(ret_string
, "a_sequence"),
1771 "bt_ctf_field_type_structure_get_field returns a correct field name");
1772 ok(returned_type
== sequence_type
,
1773 "bt_ctf_field_type_structure_get_field returns a correct field type");
1774 bt_put(returned_type
);
1776 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1777 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1778 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1779 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1780 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1781 structure_seq_type
, "a_sequence");
1782 ok(returned_type
== sequence_type
,
1783 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1784 bt_put(returned_type
);
1786 composite_structure_type
= bt_ctf_field_type_structure_create();
1787 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1788 string_type
, "a_string") == 0,
1789 "Add a string type to a structure");
1790 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1791 structure_seq_type
, "inner_structure") == 0,
1792 "Add a structure type to a structure");
1794 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1795 NULL
, "a_sequence") == NULL
,
1796 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1797 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1798 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 a correct type");
1804 bt_put(returned_type
);
1806 int_16
= bt_ctf_field_create(int_16_type
);
1807 ok(int_16
, "Instanciate a signed 16-bit integer");
1808 uint_12
= bt_ctf_field_create(uint_12_type
);
1809 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1810 returned_type
= bt_ctf_field_get_type(int_16
);
1811 ok(returned_type
== int_16_type
,
1812 "bt_ctf_field_get_type returns the correct type");
1814 /* Can't modify types after instanciating them */
1815 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1816 BT_CTF_INTEGER_BASE_DECIMAL
),
1817 "Check an integer type' base can't be modified after instanciation");
1818 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1819 "Check an integer type's signedness can't be modified after instanciation");
1821 /* Check signed property is checked */
1822 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1823 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1824 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1825 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1827 /* Check overflows are properly tested for */
1828 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1829 "Check -32768 is allowed for a signed 16-bit integer");
1830 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1831 "Check 32767 is allowed for a signed 16-bit integer");
1832 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1833 "Check 32768 is not allowed for a signed 16-bit integer");
1834 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1835 "Check -32769 is not allowed for a signed 16-bit integer");
1836 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1837 "Check -42 is allowed for a signed 16-bit integer");
1839 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1840 "Check 4095 is allowed for an unsigned 12-bit integer");
1841 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1842 "Check 4096 is not allowed for a unsigned 12-bit integer");
1843 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1844 "Check 0 is allowed for an unsigned 12-bit integer");
1846 string
= bt_ctf_field_create(string_type
);
1847 ok(string
, "Instanciate a string field");
1848 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1849 "Set a string's value");
1851 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1852 ok(enumeration_type
,
1853 "Create an enumeration type with an unsigned 12-bit integer as container");
1854 enumeration
= bt_ctf_field_create(enumeration_type
);
1856 "Check enumeration types are validated before instantiation");
1861 bt_put(enumeration
);
1862 bt_put(composite_structure_type
);
1863 bt_put(structure_seq_type
);
1864 bt_put(string_type
);
1865 bt_put(sequence_type
);
1866 bt_put(uint_8_type
);
1867 bt_put(int_16_type
);
1868 bt_put(uint_12_type
);
1869 bt_put(enumeration_type
);
1870 bt_put(returned_type
);
1874 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1875 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1878 * Append enough events to force the underlying packet to be resized.
1879 * Also tests that a new event can be declared after a stream has been
1880 * instantiated and used/flushed.
1884 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1886 struct bt_ctf_field_type
*integer_type
=
1887 bt_ctf_field_type_integer_create(17);
1888 struct bt_ctf_field_type
*string_type
=
1889 bt_ctf_field_type_string_create();
1890 struct bt_ctf_event
*event
= NULL
;
1891 struct bt_ctf_field
*ret_field
= NULL
;
1892 struct bt_ctf_field_type
*ret_field_type
= NULL
;
1893 uint64_t ret_uint64
;
1894 int events_appended
= 0;
1895 struct bt_ctf_field
*packet_context
= NULL
,
1896 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
1897 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
1898 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
1899 struct bt_ctf_field_type
*ep_type
= NULL
;
1901 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1903 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1905 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1906 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1912 * bt_ctf_stream_class_add_event_class() copies the field types
1913 * of event_class, so we retrieve the new ones to create the
1914 * appropriate fields.
1916 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
1918 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1919 ep_type
, "field_1");
1920 assert(ep_field_1_type
);
1921 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1922 ep_type
, "a_string");
1923 assert(ep_a_string_type
);
1925 event
= bt_ctf_event_create(event_class
);
1926 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
1927 ret_field_type
= bt_ctf_field_get_type(ret_field
);
1928 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
1929 "bt_ctf_event_get_payload_by_index returns a correct field");
1930 bt_put(ret_field_type
);
1933 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
1934 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1935 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
1936 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1939 for (i
= 0; i
< packet_resize_test_length
; i
++) {
1940 event
= bt_ctf_event_create(event_class
);
1941 struct bt_ctf_field
*integer
=
1942 bt_ctf_field_create(ep_field_1_type
);
1943 struct bt_ctf_field
*string
=
1944 bt_ctf_field_create(ep_a_string_type
);
1946 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1947 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1948 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1951 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1952 ret
|= bt_ctf_event_set_payload(event
, "a_string",
1956 /* Populate stream event context */
1957 stream_event_context
=
1958 bt_ctf_event_get_stream_event_context(event
);
1959 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
1960 "common_event_context");
1961 BT_PUT(stream_event_context
);
1962 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
1966 ret
|= bt_ctf_stream_append_event(stream
, event
);
1974 events_appended
= !!(i
== packet_resize_test_length
);
1975 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
1976 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
1977 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
1978 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
1979 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
1980 ok(ret
== 0 && ret_uint64
== 0,
1981 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
1982 bt_ctf_stream_append_discarded_events(stream
, 1000);
1983 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
1984 ok(ret
== 0 && ret_uint64
== 1000,
1985 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
1988 ok(events_appended
, "Append 100 000 events to a stream");
1991 * Populate the custom packet context field with a dummy value
1992 * otherwise flush will fail.
1994 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1995 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1996 "custom_packet_context_field");
1997 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
1999 ok(bt_ctf_stream_flush(stream
) == 0,
2000 "Flush a stream that forces a packet resize");
2001 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2002 ok(ret
== 0 && ret_uint64
== 1000,
2003 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2004 bt_put(integer_type
);
2005 bt_put(string_type
);
2006 bt_put(packet_context
);
2007 bt_put(packet_context_field
);
2008 bt_put(stream_event_context
);
2009 bt_put(event_class
);
2010 bt_put(ep_field_1_type
);
2011 bt_put(ep_a_string_type
);
2016 void test_empty_stream(struct bt_ctf_writer
*writer
)
2019 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2020 struct bt_ctf_stream_class
*stream_class
= NULL
;
2021 struct bt_ctf_stream
*stream
= NULL
;
2023 trace
= bt_ctf_writer_get_trace(writer
);
2025 diag("Failed to get trace from writer");
2030 stream_class
= bt_ctf_stream_class_create("empty_stream");
2031 if (!stream_class
) {
2032 diag("Failed to create stream class");
2037 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2039 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
2042 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2043 "bt_ctf_stream_class_get_trace handles NULL correctly");
2044 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2045 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2047 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2049 diag("Failed to create writer stream");
2054 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2055 ok(ret_trace
== trace
,
2056 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2059 "Created a stream class with default attributes and an empty stream");
2063 bt_put(stream_class
);
2067 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
,
2068 struct bt_ctf_clock
*clock
)
2071 struct bt_ctf_stream_class
*stream_class
= NULL
;
2072 struct bt_ctf_stream
*stream
= NULL
;
2073 struct bt_ctf_field_type
*integer_type
= NULL
,
2074 *sequence_type
= NULL
, *event_header_type
= NULL
;
2075 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2076 *event_header
= NULL
, *packet_header
= NULL
;
2077 struct bt_ctf_event_class
*event_class
= NULL
;
2078 struct bt_ctf_event
*event
= NULL
;
2080 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2081 if (!stream_class
) {
2082 fail("Failed to create stream class");
2086 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2088 fail("Failed to set stream class clock");
2093 * Customize event header to add an "seq_len" integer member
2094 * which will be used as the length of a sequence in an event of this
2097 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2099 if (!event_header_type
) {
2100 fail("Failed to get event header type");
2104 integer_type
= bt_ctf_field_type_integer_create(13);
2105 if (!integer_type
) {
2106 fail("Failed to create length integer type");
2110 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2111 integer_type
, "seq_len");
2113 fail("Failed to add a new field to stream event header");
2117 event_class
= bt_ctf_event_class_create("sequence_event");
2119 fail("Failed to create event class");
2124 * This event's payload will contain a sequence which references
2125 * stream.event.header.seq_len as its length field.
2127 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2128 "stream.event.header.seq_len");
2129 if (!sequence_type
) {
2130 fail("Failed to create a sequence");
2134 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2137 fail("Failed to add a sequence to an event class");
2141 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2143 fail("Failed to add event class to stream class");
2147 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2149 fail("Failed to create stream")
2154 * We have defined a custom packet header field. We have to populate it
2157 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2158 if (!packet_header
) {
2159 fail("Failed to get stream packet header");
2163 integer
= bt_ctf_field_structure_get_field(packet_header
,
2164 "custom_trace_packet_header_field");
2166 fail("Failed to retrieve custom_trace_packet_header_field");
2170 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2172 fail("Failed to set custom_trace_packet_header_field value");
2177 event
= bt_ctf_event_create(event_class
);
2179 fail("Failed to create event");
2183 event_header
= bt_ctf_event_get_header(event
);
2184 if (!event_header
) {
2185 fail("Failed to get event header");
2189 integer
= bt_ctf_field_structure_get_field(event_header
,
2192 fail("Failed to get seq_len field from event header");
2196 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2198 fail("Failed to set seq_len value in event header");
2202 /* Populate both sequence integer fields */
2203 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2205 fail("Failed to retrieve sequence from event");
2209 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2211 fail("Failed to set sequence length");
2216 for (i
= 0; i
< 2; i
++) {
2217 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2219 fail("Failed to retrieve sequence element");
2223 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2225 fail("Failed to set sequence element value");
2233 ret
= bt_ctf_stream_append_event(stream
, event
);
2235 fail("Failed to append event to stream");
2239 ret
= bt_ctf_stream_flush(stream
);
2241 fail("Failed to flush custom_event_header stream");
2245 bt_put(stream_class
);
2246 bt_put(event_class
);
2250 bt_put(event_header
);
2251 bt_put(packet_header
);
2252 bt_put(sequence_type
);
2253 bt_put(integer_type
);
2254 bt_put(event_header_type
);
2258 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
,
2259 struct bt_ctf_clock
*clock
)
2262 struct bt_ctf_stream_class
*stream_class
= NULL
;
2263 struct bt_ctf_stream
*stream
= NULL
,
2265 struct bt_ctf_event_class
*event_class
= NULL
;
2266 struct bt_ctf_event
*event
= NULL
;
2267 struct bt_ctf_field_type
*integer_type
= NULL
;
2268 struct bt_ctf_field
*integer
= NULL
;
2270 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2271 if (!stream_class
) {
2272 diag("Failed to create stream class");
2277 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2279 diag("Failed to set stream class clock");
2283 event_class
= bt_ctf_event_class_create("some_event_class_name");
2284 integer_type
= bt_ctf_field_type_integer_create(32);
2285 if (!integer_type
) {
2286 diag("Failed to create integer field type");
2291 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2294 diag("Failed to add field to event class");
2298 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2301 diag("Failed to add event class to stream class");
2304 event
= bt_ctf_event_create(event_class
);
2306 diag("Failed to create event");
2311 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2313 diag("Failed to get integer field payload from event");
2318 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2320 diag("Failed to set integer field value");
2324 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2326 diag("Failed to create writer stream");
2331 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2332 "bt_ctf_event_get_stream handles NULL correctly");
2333 ok(bt_ctf_event_get_stream(event
) == NULL
,
2334 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2336 ret
= bt_ctf_stream_append_event(stream
, event
);
2338 diag("Failed to append event to stream");
2342 ret_stream
= bt_ctf_event_get_stream(event
);
2343 ok(ret_stream
== stream
,
2344 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2347 "Create an event before instanciating its associated stream");
2350 bt_put(stream_class
);
2351 bt_put(event_class
);
2353 bt_put(integer_type
);
2358 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2360 struct bt_ctf_event_class
*event_class
;
2362 event_class
= bt_ctf_event_class_create("Simple Event");
2363 assert(event_class
);
2364 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
) == 0,
2365 "two event classes with the same name may cohabit within the same stream class");
2366 bt_put(event_class
);
2368 event_class
= bt_ctf_event_class_create("different name, ok");
2369 assert(event_class
);
2370 assert(!bt_ctf_event_class_set_id(event_class
, 13));
2371 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2372 "two event classes with the same ID cannot cohabit within the same stream class");
2373 bt_put(event_class
);
2377 struct bt_ctf_event_class
*create_minimal_event_class(void)
2379 struct bt_ctf_event_class
*ec
= NULL
;
2380 struct bt_ctf_field_type
*int_ft
= NULL
;
2383 int_ft
= bt_ctf_field_type_integer_create(23);
2385 ec
= bt_ctf_event_class_create("minimal");
2387 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2395 void test_create_writer_vs_non_writer_mode(void)
2399 const char *writer_stream_name
= "writer stream instance";
2400 struct bt_ctf_writer
*writer
= NULL
;
2401 struct bt_ctf_trace
*writer_trace
= NULL
;
2402 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2403 struct bt_ctf_stream
*writer_stream
= NULL
;
2404 struct bt_ctf_stream
*writer_stream2
= NULL
;
2405 struct bt_ctf_stream
*packet_stream
= NULL
;
2406 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2407 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2408 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2409 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2410 struct bt_ctf_event_class
*writer_ec
= NULL
;
2411 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2412 struct bt_ctf_event
*event
= NULL
;
2413 struct bt_ctf_event
*event2
= NULL
;
2414 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2415 struct bt_ctf_field
*int_field
= NULL
;
2416 struct bt_ctf_clock
*writer_clock
= NULL
;
2417 struct bt_ctf_clock_class
*non_writer_clock_class
= NULL
;
2418 struct bt_ctf_packet
*packet
= NULL
;
2419 struct bt_ctf_packet
*packet2
= NULL
;
2421 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
2422 if (!bt_mkdtemp(trace_path
)) {
2426 /* Create empty structure field type (event header) */
2427 empty_struct_ft
= bt_ctf_field_type_structure_create();
2428 assert(empty_struct_ft
);
2430 /* Create writer, writer stream class, stream, and clock */
2431 writer
= bt_ctf_writer_create(trace_path
);
2433 writer_clock
= bt_ctf_clock_create("writer_clock");
2434 assert(writer_clock
);
2435 ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
2437 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2439 writer_trace
= bt_ctf_writer_get_trace(writer
);
2440 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2441 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2443 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2446 ret
= bt_ctf_stream_class_set_clock(writer_sc
, writer_clock
);
2448 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2450 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2451 assert(writer_stream
);
2452 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2453 "bt_ctf_stream_get_name() returns the stream's name");
2455 /* Create non-writer trace, stream class, stream, and clock */
2456 non_writer_trace
= bt_ctf_trace_create();
2457 assert(non_writer_trace
);
2458 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2459 assert(non_writer_sc
);
2460 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2463 ret
= bt_ctf_stream_class_set_packet_context_type(non_writer_sc
, NULL
);
2465 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2467 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2468 assert(non_writer_stream
);
2469 non_writer_clock_class
=
2470 bt_ctf_clock_class_create("non_writer_clock_class",
2472 assert(non_writer_clock_class
);
2473 ret
= bt_ctf_trace_add_clock_class(non_writer_trace
,
2474 non_writer_clock_class
);
2477 /* Create event class and event */
2478 writer_ec
= create_minimal_event_class();
2480 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2482 event
= bt_ctf_event_create(writer_ec
);
2484 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2486 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2489 * Verify non-writer stream: it should be impossible to append
2492 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2493 "bt_ctf_stream_append_event() fails with a non-writer stream");
2496 * Verify writer stream: it should be possible to append an
2499 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2500 "bt_ctf_stream_append_event() succeeds with a writer stream");
2503 * It should be possible to create a packet from a non-writer
2504 * stream, but not from a writer stream.
2506 packet
= bt_ctf_packet_create(writer_stream
);
2507 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2508 packet
= bt_ctf_packet_create(non_writer_stream
);
2509 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2510 packet_stream
= bt_ctf_packet_get_stream(packet
);
2511 ok(packet_stream
== non_writer_stream
,
2512 "bt_ctf_packet_get_stream() returns the correct stream");
2515 * It should not be possible to append an event associated to
2516 * a stream to a different stream.
2518 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2519 assert(writer_stream2
);
2520 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2521 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2524 * It should not be possible to set the packet of an event
2525 * associated to a given stream to a packet associated with
2526 * a different stream.
2528 ok(bt_ctf_event_set_packet(event
, packet
),
2529 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2532 * It should be possible to set the packet of a fresh event, as
2533 * long as the originating stream classes are the same.
2535 event2
= bt_ctf_event_create(writer_ec
);
2537 ok(bt_ctf_event_set_packet(event2
, packet
),
2538 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2539 non_writer_ec
= create_minimal_event_class();
2540 assert(non_writer_ec
);
2541 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2544 event2
= bt_ctf_event_create(non_writer_ec
);
2546 ok(!bt_ctf_event_set_packet(event2
, packet
),
2547 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2550 * It should be possible to set a packet created from the same
2551 * stream to an event with an existing packet.
2553 packet2
= bt_ctf_packet_create(non_writer_stream
);
2555 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2556 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2560 * It should not be possible to set a packet created from a
2561 * different stream to an event with an existing packet.
2563 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2564 assert(non_writer_stream2
);
2565 packet2
= bt_ctf_packet_create(non_writer_stream
);
2567 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2568 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2571 bt_put(writer_trace
);
2573 bt_put(writer_stream
);
2574 bt_put(writer_stream2
);
2575 bt_put(non_writer_trace
);
2576 bt_put(non_writer_sc
);
2577 bt_put(non_writer_stream
);
2578 bt_put(non_writer_stream2
);
2579 bt_put(packet_stream
);
2581 bt_put(non_writer_ec
);
2585 bt_put(empty_struct_ft
);
2586 bt_put(writer_clock
);
2587 bt_put(non_writer_clock_class
);
2590 recursive_rmdir(trace_path
);
2595 void test_clock_utils(void)
2598 struct bt_ctf_clock
*clock
= NULL
;
2600 clock
= bt_ctf_clock_create("water");
2602 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2604 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2606 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2608 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2614 void test_set_clock_non_writer_stream_class(void)
2616 struct bt_ctf_clock
*clock
;
2617 struct bt_ctf_trace
*trace
;
2618 struct bt_ctf_stream_class
*sc
;
2621 clock
= bt_ctf_clock_create("the_clock");
2624 trace
= bt_ctf_trace_create();
2627 sc
= bt_ctf_stream_class_create(NULL
);
2630 ret
= bt_ctf_stream_class_set_clock(sc
, clock
);
2633 ret
= bt_ctf_trace_add_stream_class(trace
, sc
);
2635 "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock");
2643 void test_static_trace(void)
2645 struct bt_ctf_trace
*trace
;
2646 struct bt_ctf_stream_class
*stream_class
;
2647 struct bt_ctf_stream_class
*stream_class2
;
2648 struct bt_ctf_stream
*stream
;
2649 struct bt_ctf_clock_class
*clock_class
;
2652 trace
= bt_ctf_trace_create();
2654 stream_class
= bt_ctf_stream_class_create(NULL
);
2655 assert(stream_class
);
2656 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2658 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
2660 stream
= bt_ctf_stream_create(stream_class
, "hello");
2661 ok(stream
, "bt_ctf_stream_create() succeeds with a non-static trace");
2663 ok(!bt_ctf_trace_is_static(trace
),
2664 "bt_ctf_trace_is_static() returns the expected value");
2665 ok(bt_ctf_trace_set_is_static(trace
) == 0,
2666 "bt_ctf_trace_set_is_static() succeeds");
2667 ok(bt_ctf_trace_is_static(trace
),
2668 "bt_ctf_trace_is_static() returns the expected value");
2669 clock_class
= bt_ctf_clock_class_create("yes", 1000000000);
2670 assert(clock_class
);
2671 stream_class2
= bt_ctf_stream_class_create(NULL
);
2672 assert(stream_class2
);
2673 ok(bt_ctf_trace_add_stream_class(trace
, stream_class2
),
2674 "bt_ctf_trace_add_stream_class() fails with a static trace");
2675 ok(bt_ctf_trace_add_clock_class(trace
, clock_class
),
2676 "bt_ctf_trace_add_clock_class() fails with a static trace");
2677 ok(!bt_ctf_stream_create(stream_class
, "hello2"),
2678 "bt_ctf_stream_create() fails with a static trace");
2681 bt_put(stream_class
);
2682 bt_put(stream_class2
);
2683 bt_put(clock_class
);
2687 void trace_is_static_listener(struct bt_ctf_trace
*trace
, void *data
)
2689 *((int *) data
) |= 1;
2693 void trace_listener_removed(struct bt_ctf_trace
*trace
, void *data
)
2695 *((int *) data
) |= 2;
2699 void test_trace_is_static_listener(void)
2701 struct bt_ctf_trace
*trace
;
2712 trace
= bt_ctf_trace_create();
2714 ret
= bt_ctf_trace_add_is_static_listener(NULL
,
2715 trace_is_static_listener
, trace_listener_removed
, &called1
);
2716 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (trace)");
2717 ret
= bt_ctf_trace_add_is_static_listener(trace
, NULL
,
2718 trace_listener_removed
, &called1
);
2719 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (listener)");
2720 listener1_id
= bt_ctf_trace_add_is_static_listener(trace
,
2721 trace_is_static_listener
, trace_listener_removed
, &called1
);
2722 ok(listener1_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (1)");
2723 listener2_id
= bt_ctf_trace_add_is_static_listener(trace
,
2724 trace_is_static_listener
, trace_listener_removed
, &called2
);
2725 ok(listener2_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (2)");
2726 listener3_id
= bt_ctf_trace_add_is_static_listener(trace
,
2727 trace_is_static_listener
, trace_listener_removed
, &called3
);
2728 ok(listener3_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (3)");
2729 ret
= bt_ctf_trace_remove_is_static_listener(NULL
, 0);
2730 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles NULL (trace)");
2731 ret
= bt_ctf_trace_remove_is_static_listener(trace
, -2);
2732 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (negative)");
2733 ret
= bt_ctf_trace_remove_is_static_listener(trace
, 77);
2734 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (non existing)");
2735 ret
= bt_ctf_trace_remove_is_static_listener(trace
, listener2_id
);
2736 ok(ret
== 0, "bt_ctf_trace_remove_is_static_listener() succeeds");
2737 ok(called2
== 2, "bt_ctf_trace_remove_is_static_listener() calls the remove listener");
2738 listener4_id
= bt_ctf_trace_add_is_static_listener(trace
,
2739 trace_is_static_listener
, NULL
, &called4
);
2740 ok(listener4_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (4)");
2741 ok(called1
== 0, "\"trace is static\" listener not called before the trace is made static (1)");
2742 ok(called2
== 2, "\"trace is static\" listener not called before the trace is made static (2)");
2743 ok(called3
== 0, "\"trace is static\" listener not called before the trace is made static (3)");
2744 ok(called4
== 0, "\"trace is static\" listener not called before the trace is made static (4)");
2745 ret
= bt_ctf_trace_set_is_static(trace
);
2747 ret
= bt_ctf_trace_add_is_static_listener(trace
,
2748 trace_is_static_listener
, trace_listener_removed
, &called1
);
2750 "bt_ctf_trace_add_is_static_listener() fails when the trace is static");
2751 ok(called1
== 1, "\"trace is static\" listener called when the trace is made static (1)");
2752 ok(called2
== 2, "\"trace is static\" listener not called when the trace is made static (2)");
2753 ok(called3
== 1, "\"trace is static\" listener called when the trace is made static (3)");
2754 ok(called4
== 1, "\"trace is static\" listener called when the trace is made static (4)");
2760 ok(called1
== 2, "\"trace is static\" listener not called after the trace is put (1)");
2761 ok(called2
== 0, "\"trace is static\" listener not called after the trace is put (2)");
2762 ok(called3
== 2, "\"trace is static\" listener not called after the trace is put (3)");
2763 ok(called4
== 0, "\"trace is static\" listener not called after the trace is put (4)");
2767 void test_trace_uuid(void)
2769 struct bt_ctf_trace
*trace
;
2770 const unsigned char uuid
[] = {
2771 0x35, 0x92, 0x63, 0xab, 0xb4, 0xbe, 0x40, 0xb4,
2772 0xb2, 0x60, 0xd3, 0xf1, 0x3b, 0xb0, 0xd8, 0x59,
2774 const unsigned char *ret_uuid
;
2776 trace
= bt_ctf_trace_create();
2778 ok(!bt_ctf_trace_get_uuid(NULL
),
2779 "bt_ctf_trace_get_uuid() handles NULL");
2780 ok(!bt_ctf_trace_get_uuid(trace
),
2781 "bt_ctf_trace_get_uuid() returns NULL initially");
2782 ok(bt_ctf_trace_set_uuid(NULL
, uuid
),
2783 "bt_ctf_trace_set_uuid() handles NULL (trace)");
2784 ok(bt_ctf_trace_set_uuid(trace
, NULL
),
2785 "bt_ctf_trace_set_uuid() handles NULL (UUID)");
2786 ok(bt_ctf_trace_set_uuid(trace
, uuid
) == 0,
2787 "bt_ctf_trace_set_uuid() succeeds with a valid UUID");
2788 ret_uuid
= bt_ctf_trace_get_uuid(trace
);
2789 ok(ret_uuid
, "bt_ctf_trace_get_uuid() returns a UUID");
2791 ok(memcmp(uuid
, ret_uuid
, 16) == 0,
2792 "bt_ctf_trace_get_uuid() returns the expected UUID");
2797 int main(int argc
, char **argv
)
2799 const char *env_resize_length
;
2801 gchar
*metadata_path
;
2802 const char *clock_name
= "test_clock";
2803 const char *clock_description
= "This is a test clock";
2804 const char *returned_clock_name
;
2805 const char *returned_clock_description
;
2806 const uint64_t frequency
= 1123456789;
2807 const int64_t offset_s
= 13515309;
2808 const int64_t offset
= 1234567;
2809 int64_t get_offset_s
,
2811 const uint64_t precision
= 10;
2812 const int is_absolute
= 0xFF;
2813 char *metadata_string
;
2814 struct bt_ctf_writer
*writer
;
2815 struct bt_utsname name
= {"GNU/Linux", "testhost", "4.4.0-87-generic",
2816 "#110-Ubuntu SMP Tue Jul 18 12:55:35 UTC 2017", "x86_64"};
2817 struct bt_ctf_clock
*clock
, *ret_clock
;
2818 struct bt_ctf_clock_class
*ret_clock_class
;
2819 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2820 struct bt_ctf_stream
*stream1
;
2821 struct bt_ctf_stream
*stream
;
2822 const char *ret_string
;
2823 const unsigned char *ret_uuid
;
2824 unsigned char tmp_uuid
[16] = { 0 };
2825 struct bt_ctf_field_type
*packet_context_type
,
2826 *packet_context_field_type
,
2827 *packet_header_type
,
2828 *packet_header_field_type
,
2830 *stream_event_context_type
,
2832 *event_header_field_type
;
2833 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2834 struct bt_ctf_trace
*trace
;
2836 int64_t ret_int64_t
;
2837 struct bt_value
*obj
;
2840 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
2844 env_resize_length
= getenv("PACKET_RESIZE_TEST_LENGTH");
2845 if (env_resize_length
) {
2846 packet_resize_test_length
=
2847 (unsigned int) atoi(env_resize_length
);
2850 plan_tests(NR_TESTS
);
2852 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
2853 if (!bt_mkdtemp(trace_path
)) {
2857 metadata_path
= g_build_filename(trace_path
, "metadata", NULL
);
2859 writer
= bt_ctf_writer_create(trace_path
);
2860 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2862 ok(!bt_ctf_writer_get_trace(NULL
),
2863 "bt_ctf_writer_get_trace correctly handles NULL");
2864 trace
= bt_ctf_writer_get_trace(writer
);
2865 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
),
2866 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_NATIVE");
2867 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_UNSPECIFIED
),
2868 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_UNSPECIFIED");
2870 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2871 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2872 "Set a trace's byte order to big endian");
2873 ok(bt_ctf_trace_get_native_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2874 "bt_ctf_trace_get_native_byte_order returns a correct endianness");
2876 /* Add environment context to the trace */
2877 ok(bt_ctf_writer_add_environment_field(writer
, "host", name
.nodename
) == 0,
2878 "Add host (%s) environment field to writer instance",
2880 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2882 "bt_ctf_writer_add_environment_field error with NULL writer");
2883 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2885 "bt_ctf_writer_add_environment_field error with NULL field name");
2886 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2888 "bt_ctf_writer_add_environment_field error with NULL field value");
2890 /* Test bt_ctf_trace_set_environment_field with an integer object */
2891 obj
= bt_value_integer_create_init(23);
2893 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2894 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2895 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2896 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2897 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2898 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2899 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2900 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2903 /* Test bt_ctf_trace_set_environment_field with a string object */
2904 obj
= bt_value_string_create_init("the value");
2906 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2907 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2910 /* Test bt_ctf_trace_set_environment_field_integer */
2911 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2913 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2914 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2915 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2916 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2918 "bt_ctf_trace_set_environment_field_integer succeeds");
2920 /* Test bt_ctf_trace_set_environment_field_string */
2921 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2923 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2924 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2925 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2926 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2928 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2929 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2931 "bt_ctf_trace_set_environment_field_string succeeds");
2933 /* Test bt_ctf_trace_get_environment_field_count */
2934 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2935 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2936 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2937 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2939 /* Test bt_ctf_trace_get_environment_field_name */
2940 ok(bt_ctf_trace_get_environment_field_name_by_index(NULL
, 0) == NULL
,
2941 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2942 ok(bt_ctf_trace_get_environment_field_name_by_index(trace
, 5) == NULL
,
2943 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2944 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 0);
2945 ok(ret_string
&& !strcmp(ret_string
, "host"),
2946 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2947 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 1);
2948 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2949 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2950 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 2);
2951 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2952 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2953 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 3);
2954 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2955 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2956 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 4);
2957 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2958 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2960 /* Test bt_ctf_trace_get_environment_field_value */
2961 ok(bt_ctf_trace_get_environment_field_value_by_index(NULL
, 0) == NULL
,
2962 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2963 ok(bt_ctf_trace_get_environment_field_value_by_index(trace
, 5) == NULL
,
2964 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2965 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 1);
2966 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2967 ok(!ret
&& ret_int64_t
== 23,
2968 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2970 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 2);
2971 ret
= bt_value_string_get(obj
, &ret_string
);
2972 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2973 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2976 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2977 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2979 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2980 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
2981 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2982 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
2983 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2984 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
2986 ret
= bt_value_string_get(obj
, &ret_string
);
2987 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
2988 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2991 /* Test environment field replacement */
2992 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2994 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2995 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2996 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2997 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 3);
2998 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2999 ok(!ret
&& ret_int64_t
== 654321,
3000 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3003 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3004 == 0, "Add sysname (%s) environment field to writer instance",
3006 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3007 name
.nodename
) == 0,
3008 "Add nodename (%s) environment field to writer instance",
3010 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3011 == 0, "Add release (%s) environment field to writer instance",
3013 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3014 == 0, "Add version (%s) environment field to writer instance",
3016 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3017 == 0, "Add machine (%s) environment field to writer istance",
3020 /* Define a clock and add it to the trace */
3021 ok(bt_ctf_clock_create("signed") == NULL
,
3022 "Illegal clock name rejected");
3023 clock
= bt_ctf_clock_create(clock_name
);
3024 ok(clock
, "Clock created sucessfully");
3025 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3026 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3027 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3028 "Returned clock name is valid");
3030 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3031 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3032 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3033 "Clock description set successfully");
3035 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3036 ok(returned_clock_description
,
3037 "bt_ctf_clock_get_description returns a description.");
3038 ok(returned_clock_description
?
3039 !strcmp(returned_clock_description
, clock_description
) : 0,
3040 "Returned clock description is valid");
3042 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3043 "bt_ctf_clock_get_frequency returns the correct default frequency");
3044 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3045 "Set clock frequency");
3046 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3047 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3049 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3050 "bt_ctf_clock_get_offset_s succeeds");
3051 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3052 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3053 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3054 "Set clock offset (seconds)");
3055 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3056 "bt_ctf_clock_get_offset_s succeeds");
3057 ok(get_offset_s
== offset_s
,
3058 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3060 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3061 "bt_ctf_clock_get_offset succeeds");
3062 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3063 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3064 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3065 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3066 "bt_ctf_clock_get_offset succeeds");
3067 ok(get_offset
== offset
,
3068 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3070 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3071 "bt_ctf_clock_get_precision returns the correct default precision");
3072 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3073 "Set clock precision");
3074 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3075 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3077 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3078 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3079 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3080 "Set clock absolute property");
3081 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3082 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3084 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3087 ok(!bt_ctf_clock_get_name(NULL
),
3088 "bt_ctf_clock_get_name correctly handles NULL");
3089 ok(!bt_ctf_clock_get_description(NULL
),
3090 "bt_ctf_clock_get_description correctly handles NULL");
3091 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3092 "bt_ctf_clock_get_frequency correctly handles NULL");
3093 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3094 "bt_ctf_clock_get_precision correctly handles NULL");
3095 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3096 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3097 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3098 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3099 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3100 "bt_ctf_clock_get_offset correctly handles NULL clock");
3101 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3102 "bt_ctf_clock_get_offset correctly handles NULL output");
3103 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3104 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3106 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3107 "bt_ctf_clock_set_description correctly handles NULL clock");
3108 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3109 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3110 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3111 "bt_ctf_clock_get_precision correctly handles NULL clock");
3112 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3113 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3114 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3115 "bt_ctf_clock_set_offset correctly handles NULL clock");
3116 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3117 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3118 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3119 "bt_ctf_clock_set_time correctly handles NULL clock");
3120 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3121 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3122 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3124 "bt_ctf_clock_get_uuid returns a UUID");
3126 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3127 /* Slightly modify UUID */
3128 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3131 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3132 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3133 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3134 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3135 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3136 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3137 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3139 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3140 ok(uuid_match(ret_uuid
, tmp_uuid
),
3141 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3143 /* Define a stream class */
3144 stream_class
= bt_ctf_stream_class_create("test_stream");
3146 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3147 "bt_ctf_stream_class_get_name handles NULL correctly");
3148 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3149 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3150 "bt_ctf_stream_class_get_name returns a correct stream class name");
3152 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3153 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3154 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3155 "bt_ctf_stream_class_get_clock handles NULL correctly");
3157 ok(stream_class
, "Create stream class");
3158 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3159 "Set a stream class' clock");
3160 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3161 ok(ret_clock
== clock
,
3162 "bt_ctf_stream_class_get_clock returns a correct clock");
3165 /* Test the event fields and event types APIs */
3168 /* Test fields copying */
3171 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3172 "bt_ctf_stream_class_get_id returns an error when no id is set");
3173 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3174 "bt_ctf_stream_class_get_id handles NULL correctly");
3175 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3176 "bt_ctf_stream_class_set_id handles NULL correctly");
3177 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3178 "Set an stream class' id");
3179 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3180 "bt_ctf_stream_class_get_id returns the correct value");
3182 /* Validate default event header fields */
3183 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3184 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3185 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3188 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3189 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3190 "Default event header type is a structure");
3191 event_header_field_type
=
3192 bt_ctf_field_type_structure_get_field_type_by_name(
3193 ret_field_type
, "id");
3194 ok(event_header_field_type
,
3195 "Default event header type contains an \"id\" field");
3196 ok(bt_ctf_field_type_get_type_id(
3197 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3198 "Default event header \"id\" field is an integer");
3199 bt_put(event_header_field_type
);
3200 event_header_field_type
=
3201 bt_ctf_field_type_structure_get_field_type_by_name(
3202 ret_field_type
, "timestamp");
3203 ok(event_header_field_type
,
3204 "Default event header type contains a \"timestamp\" field");
3205 ok(bt_ctf_field_type_get_type_id(
3206 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3207 "Default event header \"timestamp\" field is an integer");
3208 bt_put(event_header_field_type
);
3209 bt_put(ret_field_type
);
3211 /* Add a custom trace packet header field */
3212 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3213 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3214 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3215 ok(packet_header_type
,
3216 "bt_ctf_trace_get_packet_header_type returns a packet header");
3217 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3218 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3219 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3220 packet_header_type
, "magic");
3221 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3222 bt_put(ret_field_type
);
3223 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3224 packet_header_type
, "uuid");
3225 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3226 bt_put(ret_field_type
);
3227 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3228 packet_header_type
, "stream_id");
3229 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3230 bt_put(ret_field_type
);
3232 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3233 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3234 packet_header_field_type
, "custom_trace_packet_header_field"),
3235 "Added a custom trace packet header field successfully");
3237 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3238 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3239 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3240 "Set a trace packet_header_type successfully");
3242 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3243 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3245 /* Add a custom field to the stream class' packet context */
3246 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3247 ok(packet_context_type
,
3248 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3249 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3250 "Packet context is a structure");
3252 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3253 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3255 integer_type
= bt_ctf_field_type_integer_create(32);
3256 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3258 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3259 /* Create a "uint5_t" equivalent custom packet context field */
3260 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3262 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3263 packet_context_field_type
, "custom_packet_context_field");
3264 ok(ret
== 0, "Packet context field added successfully");
3266 /* Define a stream event context containing a my_integer field. */
3267 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3268 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3269 stream_event_context_type
= bt_ctf_field_type_structure_create();
3270 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3271 integer_type
, "common_event_context");
3273 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3274 stream_event_context_type
) < 0,
3275 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3276 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3278 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3280 ok(bt_ctf_stream_class_set_event_context_type(
3281 stream_class
, stream_event_context_type
) == 0,
3282 "Set a new stream event context type");
3283 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3285 ok(ret_field_type
== stream_event_context_type
,
3286 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3287 bt_put(ret_field_type
);
3289 /* Instantiate a stream and append events */
3290 ret
= bt_ctf_writer_add_clock(writer
, clock
);
3292 ok(bt_ctf_trace_get_stream_count(trace
) == 0,
3293 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)");
3294 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3295 ok(stream1
, "Instanciate a stream class from writer");
3296 ok(bt_ctf_trace_get_stream_count(trace
) == 1,
3297 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)");
3298 stream
= bt_ctf_trace_get_stream_by_index(trace
, 0);
3299 ok(stream
== stream1
,
3300 "bt_ctf_trace_get_stream_by_index() succeeds and returns the correct value");
3304 * Creating a stream through a writer adds the given stream
3305 * class to the writer's trace, thus registering the stream
3306 * class's clock to the trace.
3308 ok(bt_ctf_trace_get_clock_class_count(NULL
) < 0,
3309 "bt_ctf_trace_get_clock_class_count correctly handles NULL");
3310 ok(bt_ctf_trace_get_clock_class_count(trace
) == 1,
3311 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
3312 ok(!bt_ctf_trace_get_clock_class_by_index(NULL
, 0),
3313 "bt_ctf_trace_get_clock_class correctly handles NULL");
3314 ok(!bt_ctf_trace_get_clock_class_by_index(trace
, 1),
3315 "bt_ctf_trace_get_clock_class correctly handles out of bound accesses");
3316 ret_clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
3317 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3318 bt_ctf_clock_get_name(clock
)) == 0,
3319 "bt_ctf_trace_get_clock_class returns the right clock instance");
3320 bt_put(ret_clock_class
);
3321 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, NULL
),
3322 "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)");
3323 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, clock_name
),
3324 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3325 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, NULL
),
3326 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3327 ret_clock_class
= bt_ctf_trace_get_clock_class_by_name(trace
, clock_name
);
3328 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3329 bt_ctf_clock_get_name(clock
)) == 0,
3330 "bt_ctf_trace_get_clock_class returns the right clock instance");
3331 bt_put(ret_clock_class
);
3332 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, "random"),
3333 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3335 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3336 "bt_ctf_stream_get_class correctly handles NULL");
3337 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3338 ok(ret_stream_class
,
3339 "bt_ctf_stream_get_class returns a stream class");
3340 ok(ret_stream_class
== stream_class
,
3341 "Returned stream class is of the correct type");
3344 * Packet header, packet context, event header, and stream
3345 * event context types were copied for the resolving
3348 BT_PUT(packet_header_type
);
3349 BT_PUT(packet_context_type
);
3350 BT_PUT(stream_event_context_type
);
3351 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3352 assert(packet_header_type
);
3353 packet_context_type
=
3354 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3355 assert(packet_context_type
);
3356 stream_event_context_type
=
3357 bt_ctf_stream_class_get_event_context_type(stream_class
);
3358 assert(stream_event_context_type
);
3361 * Try to modify the packet context type after a stream has been
3364 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3365 packet_header_field_type
, "should_fail");
3367 "Trace packet header type can't be modified once a stream has been instanciated");
3370 * Try to modify the packet context type after a stream has been
3373 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3374 packet_context_field_type
, "should_fail");
3376 "Packet context type can't be modified once a stream has been instanciated");
3379 * Try to modify the stream event context type after a stream has been
3382 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3383 integer_type
, "should_fail");
3385 "Stream event context type can't be modified once a stream has been instanciated");
3387 /* Should fail after instanciating a stream (frozen) */
3388 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3389 "Changes to a stream class that was already instantiated fail");
3391 /* Populate the custom packet header field only once for all tests */
3392 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3393 "bt_ctf_stream_get_packet_header handles NULL correctly");
3394 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3396 "bt_ctf_stream_get_packet_header returns a packet header");
3397 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3398 ok(ret_field_type
== packet_header_type
,
3399 "Stream returns a packet header of the appropriate type");
3400 bt_put(ret_field_type
);
3401 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3402 "custom_trace_packet_header_field");
3403 ok(packet_header_field
,
3404 "Packet header structure contains a custom field with the appropriate name");
3405 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3406 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3407 "Custom packet header field is of the expected type");
3408 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3409 54321), "Set custom packet header value successfully");
3410 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3411 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3412 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3413 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3414 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3415 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3416 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3417 "Successfully set a stream's packet header");
3419 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3420 "Add environment field to writer after stream creation");
3424 test_create_writer_vs_non_writer_mode();
3426 test_set_clock_non_writer_stream_class();
3428 test_instanciate_event_before_stream(writer
, clock
);
3430 append_simple_event(stream_class
, stream1
, clock
);
3432 packet_resize_test(stream_class
, stream1
, clock
);
3434 append_complex_event(stream_class
, stream1
, clock
);
3436 append_existing_event_class(stream_class
);
3438 test_empty_stream(writer
);
3440 test_custom_event_header_stream(writer
, clock
);
3442 test_static_trace();
3444 test_trace_is_static_listener();
3448 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3449 ok(metadata_string
, "Get metadata string");
3451 bt_ctf_writer_flush_metadata(writer
);
3454 bt_put(ret_stream_class
);
3457 bt_put(packet_context_type
);
3458 bt_put(packet_context_field_type
);
3459 bt_put(integer_type
);
3460 bt_put(stream_event_context_type
);
3461 bt_put(ret_field_type
);
3462 bt_put(packet_header_type
);
3463 bt_put(packet_header_field_type
);
3464 bt_put(packet_header
);
3465 bt_put(packet_header_field
);
3467 free(metadata_string
);
3468 bt_put(stream_class
);
3470 validate_trace(argv
[1], trace_path
);
3472 recursive_rmdir(trace_path
);
3474 g_free(metadata_path
);