6 * Copyright 2013 - 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf-writer/stream-class.h>
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ctf-ir/clock-class.h>
31 #include <babeltrace/ctf-ir/trace.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf/events.h>
34 #include <babeltrace/values.h>
36 #include <babeltrace/compat/stdlib-internal.h>
38 #include <babeltrace/compat/utsname-internal.h>
39 #include <babeltrace/compat/limits-internal.h>
40 #include <babeltrace/compat/stdio-internal.h>
50 #define METADATA_LINE_SIZE 512
51 #define SEQUENCE_TEST_LENGTH 10
52 #define ARRAY_TEST_LENGTH 5
53 #define PACKET_RESIZE_TEST_LENGTH 100000
55 #define DEFAULT_CLOCK_FREQ 1000000000
56 #define DEFAULT_CLOCK_PRECISION 1
57 #define DEFAULT_CLOCK_OFFSET 0
58 #define DEFAULT_CLOCK_OFFSET_S 0
59 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
60 #define DEFAULT_CLOCK_TIME 0
61 #define DEFAULT_CLOCK_VALUE 0
65 static int64_t current_time
= 42;
67 /* Return 1 if uuids match, zero if different. */
69 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
74 if (!uuid_a
|| !uuid_b
) {
78 for (i
= 0; i
< 16; i
++) {
79 if (uuid_a
[i
] != uuid_b
[i
]) {
90 void validate_trace(char *parser_path
, char *trace_path
)
93 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
94 int babeltrace_output_fd
= -1;
101 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
102 unlink(babeltrace_output_path
);
104 if (babeltrace_output_fd
== -1) {
105 diag("Failed to create a temporary file for trace parsing.");
113 waitpid(pid
, &status
, 0);
114 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
116 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
118 perror("# dup2 babeltrace_output_fd to STDOUT");
122 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
124 perror("# dup2 babeltrace_output_fd to STDERR");
128 execl(parser_path
, parser_path
, trace_path
, NULL
);
129 perror("# Could not launch the babeltrace process");
133 ok(ret
== 0, "Babeltrace could read the resulting trace");
135 if (ret
&& babeltrace_output_fd
>= 0) {
137 size_t len
= METADATA_LINE_SIZE
;
138 FILE *babeltrace_output_fp
= NULL
;
140 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
141 if (!babeltrace_output_fp
) {
142 perror("fdopen on babeltrace_output_fd");
145 babeltrace_output_fd
= -1;
149 diag("malloc error");
151 rewind(babeltrace_output_fp
);
152 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
158 if (babeltrace_output_fp
) {
159 if (fclose(babeltrace_output_fp
)) {
160 diag("fclose error");
165 if (babeltrace_output_fd
>= 0) {
166 if (close(babeltrace_output_fd
)) {
173 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
174 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
176 /* Create and add a simple event class */
177 struct bt_ctf_event_class
*simple_event_class
=
178 bt_ctf_event_class_create("Simple Event");
179 struct bt_ctf_field_type
*uint_12_type
=
180 bt_ctf_field_type_integer_create(12);
181 struct bt_ctf_field_type
*int_64_type
=
182 bt_ctf_field_type_integer_create(64);
183 struct bt_ctf_field_type
*float_type
=
184 bt_ctf_field_type_floating_point_create();
185 struct bt_ctf_field_type
*enum_type
;
186 struct bt_ctf_field_type
*enum_type_unsigned
=
187 bt_ctf_field_type_enumeration_create(uint_12_type
);
188 struct bt_ctf_field_type
*event_context_type
=
189 bt_ctf_field_type_structure_create();
190 struct bt_ctf_field_type
*event_payload_type
= NULL
;
191 struct bt_ctf_field_type
*returned_type
;
192 struct bt_ctf_event
*simple_event
;
193 struct bt_ctf_field
*integer_field
;
194 struct bt_ctf_field
*float_field
;
195 struct bt_ctf_field
*enum_field
;
196 struct bt_ctf_field
*enum_field_unsigned
;
197 struct bt_ctf_field
*enum_container_field
;
198 const char *mapping_name_test
= "truie";
199 const double double_test_value
= 3.1415;
200 struct bt_ctf_field
*enum_container_field_unsigned
;
201 const char *mapping_name_negative_test
= "negative_value";
202 const char *ret_char
;
204 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
205 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
206 struct bt_ctf_event_class
*ret_event_class
;
207 struct bt_ctf_field
*packet_context
;
208 struct bt_ctf_field
*packet_context_field
;
209 struct bt_ctf_field
*stream_event_context
;
210 struct bt_ctf_field
*stream_event_context_field
;
211 struct bt_ctf_field
*event_context
;
212 struct bt_ctf_field
*event_context_field
;
213 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
214 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
215 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
216 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
219 ok(uint_12_type
, "Create an unsigned integer type");
221 ok(!bt_ctf_field_type_integer_set_signed(int_64_type
, 1),
222 "Set signed 64 bit integer signedness to true");
223 ok(int_64_type
, "Create a signed integer type");
224 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
226 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
227 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
228 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
229 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
230 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
231 bt_put(returned_type
);
233 bt_ctf_field_type_set_alignment(float_type
, 32);
234 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
235 "bt_ctf_field_type_get_alignment handles NULL correctly");
236 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
237 "bt_ctf_field_type_get_alignment returns a correct value");
239 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
240 "Set a floating point type's exponent digit count");
241 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
242 "Set a floating point type's mantissa digit count");
244 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
245 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
246 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
247 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
248 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
249 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
250 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
251 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
253 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
254 mapping_name_negative_test
, -12345, 0) == 0,
255 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
256 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
257 "escaping; \"test\"", 1, 1) == 0,
258 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
259 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
260 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
261 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
262 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
263 "event clock int float", 5, 22) == 0,
264 "Accept enumeration mapping strings containing reserved keywords");
265 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
267 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
268 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
269 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
270 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
271 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
272 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
273 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
275 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(NULL
, -42);
276 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly");
278 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, -4200000);
279 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values");
281 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, 3);
282 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value");
283 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, NULL
, NULL
, NULL
) == 0,
284 "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly");
287 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
288 "enum_field") == 0, "Add signed enumeration field to event");
290 ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL
, 0, &ret_char
,
291 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
292 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly");
293 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, NULL
,
294 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
295 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly");
296 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
297 NULL
, &ret_range_end_int64_t
) == 0,
298 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly");
299 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
300 &ret_range_start_int64_t
, NULL
) == 0,
301 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly");
302 /* Assumes entries are sorted by range_start values. */
303 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 6, &ret_char
,
304 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
305 "bt_ctf_field_type_enumeration_get_mapping_signed returns a value");
306 ok(!strcmp(ret_char
, mapping_name_test
),
307 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name");
308 ok(ret_range_start_int64_t
== 42,
309 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start");
310 ok(ret_range_end_int64_t
== 42,
311 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end");
313 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
314 "escaping; \"test\"", 0, 0) == 0,
315 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
316 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
317 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
318 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
319 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
320 "event clock int float", 5, 22) == 0,
321 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
322 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
324 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
325 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names");
326 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
327 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries");
328 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
329 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
330 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
331 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
333 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
334 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
335 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 6,
336 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
338 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
339 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
340 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
341 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
342 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
343 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
344 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
345 NULL
, &ret_range_end_uint64_t
) == 0,
346 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
347 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
348 &ret_range_start_uint64_t
, NULL
) == 0,
349 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
350 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 4, &ret_char
,
351 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
352 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
353 ok(!strcmp(ret_char
, mapping_name_test
),
354 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
355 ok(ret_range_start_uint64_t
== 42,
356 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
357 ok(ret_range_end_uint64_t
== 42,
358 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
360 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
362 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
365 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
367 /* Set an event context type which will contain a single integer. */
368 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
369 "event_specific_context"),
370 "Add event specific context field");
371 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
372 "bt_ctf_event_class_get_context_type handles NULL correctly");
374 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
375 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
376 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
377 "Set an event class' context type successfully");
378 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
379 ok(returned_type
== event_context_type
,
380 "bt_ctf_event_class_get_context_type returns the appropriate type");
381 bt_put(returned_type
);
383 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
384 "Adding simple event class to stream class");
387 * bt_ctf_stream_class_add_event_class() copies the field types
388 * of simple_event_class, so we retrieve the new ones to create
389 * the appropriate fields.
391 BT_PUT(event_context_type
);
392 BT_PUT(event_payload_type
);
393 event_payload_type
= bt_ctf_event_class_get_payload_type(
395 assert(event_payload_type
);
396 event_context_type
= bt_ctf_event_class_get_context_type(
398 assert(event_context_type
);
399 ep_integer_field_type
=
400 bt_ctf_field_type_structure_get_field_type_by_name(
401 event_payload_type
, "integer_field");
402 assert(ep_integer_field_type
);
404 bt_ctf_field_type_structure_get_field_type_by_name(
405 event_payload_type
, "enum_field");
406 assert(ep_enum_field_type
);
407 ep_enum_field_unsigned_type
=
408 bt_ctf_field_type_structure_get_field_type_by_name(
409 event_payload_type
, "enum_field_unsigned");
410 assert(ep_enum_field_unsigned_type
);
412 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
413 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
414 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
415 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
416 ok(bt_ctf_stream_class_get_event_class_by_index(NULL
, 0) == NULL
,
417 "bt_ctf_stream_class_get_event_class handles NULL correctly");
418 ok(bt_ctf_stream_class_get_event_class_by_index(stream_class
, 8724) == NULL
,
419 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
420 ret_event_class
= bt_ctf_stream_class_get_event_class_by_index(stream_class
, 0);
421 ok(ret_event_class
== simple_event_class
,
422 "bt_ctf_stream_class_get_event_class returns the correct event class");
423 bt_put(ret_event_class
);
424 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
425 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
426 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
427 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
429 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
430 ok(ret_event_class
== simple_event_class
,
431 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
432 bt_put(ret_event_class
);
434 simple_event
= bt_ctf_event_create(simple_event_class
);
436 "Instantiate an event containing a single integer field");
438 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
439 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
440 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
441 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
443 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
444 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
445 "bt_ctf_field_floating_point_get_value fails on an unset float field");
446 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
447 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
448 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
449 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
450 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
451 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
452 "bt_ctf_field_floating_point_get_value returns a double value");
453 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
454 "bt_ctf_field_floating_point_get_value returns a correct value");
456 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
459 iter
= bt_ctf_field_enumeration_get_mappings(NULL
);
460 ok(!iter
, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
461 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
462 ok(!iter
, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
463 enum_container_field
= bt_ctf_field_enumeration_get_container(
465 ok(bt_ctf_field_signed_integer_set_value(
466 enum_container_field
, -42) == 0,
467 "Set signed enumeration container value");
468 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
469 ok(iter
, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
470 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, &ret_char
, NULL
, NULL
);
471 ok(!ret
&& ret_char
, "bt_ctf_field_type_enumeration_mapping_iterator_get_signed return a mapping name");
473 ok(!strcmp(ret_char
, mapping_name_negative_test
),
474 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
475 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
479 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
480 assert(enum_field_unsigned
);
481 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
482 enum_field_unsigned
);
483 ok(bt_ctf_field_unsigned_integer_set_value(
484 enum_container_field_unsigned
, 42) == 0,
485 "Set unsigned enumeration container value");
486 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
487 enum_field_unsigned
);
489 iter
= bt_ctf_field_enumeration_get_mappings(enum_field_unsigned
);
491 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, &ret_char
, NULL
, NULL
);
492 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
493 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container");
495 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
497 /* Populate stream event context */
498 stream_event_context
=
499 bt_ctf_event_get_stream_event_context(simple_event
);
500 assert(stream_event_context
);
501 stream_event_context_field
= bt_ctf_field_structure_get_field(
502 stream_event_context
, "common_event_context");
503 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
505 /* Populate the event's context */
506 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
507 "bt_ctf_event_get_event_context handles NULL correctly");
508 event_context
= bt_ctf_event_get_event_context(simple_event
);
510 "bt_ctf_event_get_event_context returns a field");
511 returned_type
= bt_ctf_field_get_type(event_context
);
512 ok(returned_type
== event_context_type
,
513 "bt_ctf_event_get_event_context returns a field of the appropriate type");
514 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
515 "event_specific_context");
516 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
517 "Successfully set an event context's value");
518 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
519 "bt_ctf_event_set_event_context handles a NULL event correctly");
520 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
521 "bt_ctf_event_set_event_context handles a NULL event context correctly");
522 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
523 "bt_ctf_event_set_event_context rejects a context of the wrong type");
524 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
525 "Set an event context successfully");
527 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
528 "Append simple event to trace stream");
530 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
531 "bt_ctf_stream_get_packet_context handles NULL correctly");
532 packet_context
= bt_ctf_stream_get_packet_context(stream
);
534 "bt_ctf_stream_get_packet_context returns a packet context");
536 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
538 ok(packet_context_field
,
539 "Packet context contains the default packet_size field.");
540 bt_put(packet_context_field
);
541 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
542 "custom_packet_context_field");
543 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
544 "Custom packet context field value successfully set.");
546 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
547 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
548 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
549 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
550 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
551 "Successfully set a stream's packet context");
553 ok(bt_ctf_stream_flush(stream
) == 0,
554 "Flush trace stream with one event");
556 bt_put(simple_event_class
);
557 bt_put(simple_event
);
558 bt_put(uint_12_type
);
562 bt_put(enum_type_unsigned
);
563 bt_put(returned_type
);
564 bt_put(event_context_type
);
565 bt_put(integer_field
);
568 bt_put(enum_field_unsigned
);
569 bt_put(enum_container_field
);
570 bt_put(enum_container_field_unsigned
);
571 bt_put(packet_context
);
572 bt_put(packet_context_field
);
573 bt_put(stream_event_context
);
574 bt_put(stream_event_context_field
);
575 bt_put(event_context
);
576 bt_put(event_context_field
);
577 bt_put(event_payload_type
);
578 bt_put(ep_integer_field_type
);
579 bt_put(ep_enum_field_type
);
580 bt_put(ep_enum_field_unsigned_type
);
585 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
586 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
589 struct event_class_attrs_counts
;
590 const char *complex_test_event_string
= "Complex Test Event";
591 const char *test_string_1
= "Test ";
592 const char *test_string_2
= "string ";
593 const char *test_string_3
= "abcdefghi";
594 const char *test_string_4
= "abcd\0efg\0hi";
595 const char *test_string_cat
= "Test string abcdeabcd";
596 struct bt_ctf_field_type
*uint_35_type
=
597 bt_ctf_field_type_integer_create(35);
598 struct bt_ctf_field_type
*int_16_type
=
599 bt_ctf_field_type_integer_create(16);
600 struct bt_ctf_field_type
*uint_3_type
=
601 bt_ctf_field_type_integer_create(3);
602 struct bt_ctf_field_type
*enum_variant_type
=
603 bt_ctf_field_type_enumeration_create(uint_3_type
);
604 struct bt_ctf_field_type
*variant_type
=
605 bt_ctf_field_type_variant_create(enum_variant_type
,
607 struct bt_ctf_field_type
*string_type
=
608 bt_ctf_field_type_string_create();
609 struct bt_ctf_field_type
*sequence_type
;
610 struct bt_ctf_field_type
*array_type
;
611 struct bt_ctf_field_type
*inner_structure_type
=
612 bt_ctf_field_type_structure_create();
613 struct bt_ctf_field_type
*complex_structure_type
=
614 bt_ctf_field_type_structure_create();
615 struct bt_ctf_field_type
*ret_field_type
;
616 struct bt_ctf_event_class
*event_class
;
617 struct bt_ctf_event
*event
;
618 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
619 *inner_structure_field
, *complex_structure_field
,
620 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
621 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
622 *stream_event_ctx_int_field
, *ret_field
;
623 uint64_t ret_unsigned_int
;
624 int64_t ret_signed_int
;
625 const char *ret_string
;
626 struct bt_ctf_stream_class
*ret_stream_class
;
627 struct bt_ctf_event_class
*ret_event_class
;
628 struct bt_ctf_field
*packet_context
, *packet_context_field
;
629 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
631 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
632 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
633 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
634 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
635 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
636 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
637 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
638 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
639 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
640 "Set alignment of signed 16 bit integer to 32");
641 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
642 "Set integer signedness to true");
643 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
644 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
645 "Set signed 16 bit integer base to hexadecimal");
647 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
648 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
651 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
652 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
653 ret_field_type
= bt_ctf_field_type_array_get_element_type(
655 ok(ret_field_type
== int_16_type
,
656 "bt_ctf_field_type_array_get_element_type returns the correct type");
657 bt_put(ret_field_type
);
659 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
660 "bt_ctf_field_type_array_get_length handles NULL correctly");
661 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
662 "bt_ctf_field_type_array_get_length returns the correct length");
664 ok(bt_ctf_field_type_structure_add_field(inner_structure_type
,
665 inner_structure_type
, "yes"), "Cannot add self to structure");
666 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
667 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
668 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
669 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
670 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
671 array_type
, "an_array"), "Add an_array field to inner structure");
673 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
675 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
677 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
678 "UINT35_TYPE", 2, 7);
680 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(NULL
, "INT16_TYPE");
681 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly");
683 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, "INT16_TYPE");
684 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly");
685 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
686 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly");
689 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, NULL
);
690 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly");
692 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL
, 1);
693 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly");
695 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, -42);
696 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly");
697 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) != 0,
698 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly");
701 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, 5);
702 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly");
703 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
704 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly");
707 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
708 "An unknown entry"), "Reject a variant field based on an unknown tag value");
709 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
710 "UINT3_TYPE") == 0, "Add a field to a variant");
711 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
712 "INT16_TYPE"), "Add INT16_TYPE field to variant");
713 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
714 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
716 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
717 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
718 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
719 ok(ret_field_type
== enum_variant_type
,
720 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
721 bt_put(ret_field_type
);
723 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
724 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
725 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
726 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
727 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
728 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
729 "INT16_TYPE") == NULL
,
730 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
731 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
733 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
734 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
735 variant_type
, "INT16_TYPE");
736 ok(ret_field_type
== int_16_type
,
737 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
738 bt_put(ret_field_type
);
740 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
741 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
742 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
743 "bt_ctf_field_type_variant_get_field_count returns the correct count");
745 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
746 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
747 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
748 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
749 bt_put(ret_field_type
);
750 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
751 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
752 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
753 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
754 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
755 "bt_ctf_field_type_variant_get_field returns a field");
756 ok(!strcmp("INT16_TYPE", ret_string
),
757 "bt_ctf_field_type_variant_get_field returns a correct field name");
758 ok(ret_field_type
== int_16_type
,
759 "bt_ctf_field_type_variant_get_field returns a correct field type");
760 bt_put(ret_field_type
);
762 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
763 enum_variant_type
, "variant_selector"),
764 "Add variant_selector field to complex structure");
765 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
766 string_type
, "a_string"), "Add a_string field to complex structure");
767 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
768 variant_type
, "variant_value"),
769 "Add variant_value field to complex structure");
770 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
771 inner_structure_type
, "inner_structure"),
772 "Add inner_structure field to complex structure");
774 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
775 ok(event_class
, "Create an event class");
776 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
777 "Reject addition of a field with an empty name to an event");
778 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
779 "Reject addition of a field with a NULL type to an event");
780 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
782 "Reject addition of a type with an illegal name to an event");
783 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
785 "Add field of type unsigned integer to an event");
786 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
787 "int_16") == 0, "Add field of type signed integer to an event");
788 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
789 "complex_structure") == 0,
790 "Add composite structure to an event");
792 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
793 "bt_ctf_event_class_get_name handles NULL correctly");
794 ret_string
= bt_ctf_event_class_get_name(event_class
);
795 ok(!strcmp(ret_string
, complex_test_event_string
),
796 "bt_ctf_event_class_get_name returns a correct name");
797 ok(bt_ctf_event_class_get_id(event_class
) < 0,
798 "bt_ctf_event_class_get_id returns a negative value when not set");
799 ok(bt_ctf_event_class_get_id(NULL
) < 0,
800 "bt_ctf_event_class_get_id handles NULL correctly");
801 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
802 "bt_ctf_event_class_set_id handles NULL correctly");
803 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
804 "Set an event class' id");
805 ok(bt_ctf_event_class_get_id(event_class
) == 42,
806 "bt_ctf_event_class_get_id returns the correct value");
808 /* Test event class attributes */
809 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
,
810 "event class has the expected initial log level");
811 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
812 "as expected, event class has no initial EMF URI");
813 ok(bt_ctf_event_class_set_log_level(NULL
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
814 "bt_ctf_event_class_set_log_level handles a NULL event class correctly");
815 ok(bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
),
816 "bt_ctf_event_class_set_log_level handles an unknown log level correctly");
817 ok(!bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
818 "bt_ctf_event_class_set_log_level succeeds with a valid log level");
819 ok(bt_ctf_event_class_get_log_level(NULL
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
,
820 "bt_ctf_event_class_get_log_level handles a NULL event class correctly");
821 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
,
822 "bt_ctf_event_class_get_log_level returns the expected log level");
823 ok(bt_ctf_event_class_set_emf_uri(NULL
, "http://diamon.org/babeltrace/"),
824 "bt_ctf_event_class_set_emf_uri handles a NULL event class correctly");
825 ok(!bt_ctf_event_class_set_emf_uri(event_class
, "http://diamon.org/babeltrace/"),
826 "bt_ctf_event_class_set_emf_uri succeeds with a valid EMF URI");
827 ok(!bt_ctf_event_class_get_emf_uri(NULL
),
828 "bt_ctf_event_class_get_emf_uri handles a NULL event class correctly");
829 ok(strcmp(bt_ctf_event_class_get_emf_uri(event_class
), "http://diamon.org/babeltrace/") == 0,
830 "bt_ctf_event_class_get_emf_uri returns the expected EMF URI");
831 ok(!bt_ctf_event_class_set_emf_uri(event_class
, NULL
),
832 "bt_ctf_event_class_set_emf_uri succeeds with NULL (to reset)");
833 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
834 "as expected, event class has no EMF URI after reset");
836 /* Add event class to the stream class */
837 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
838 "Reject addition of NULL event class to a stream class");
839 ok(bt_ctf_stream_class_add_event_class(stream_class
,
840 event_class
) == 0, "Add an event class to stream class");
842 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
843 "bt_ctf_event_class_get_stream_class handles NULL correctly");
844 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
845 ok(ret_stream_class
== stream_class
,
846 "bt_ctf_event_class_get_stream_class returns the correct stream class");
847 bt_put(ret_stream_class
);
849 ok(bt_ctf_event_class_get_payload_type_field_count(NULL
) < 0,
850 "bt_ctf_event_class_get_field_count handles NULL correctly");
851 ok(bt_ctf_event_class_get_payload_type_field_count(event_class
) == 3,
852 "bt_ctf_event_class_get_field_count returns a correct value");
854 ok(bt_ctf_event_class_get_payload_type_field_by_index(NULL
, &ret_string
,
855 &ret_field_type
, 0) < 0,
856 "bt_ctf_event_class_get_field handles a NULL event class correctly");
857 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, NULL
,
858 &ret_field_type
, 0) == 0,
859 "bt_ctf_event_class_get_field handles a NULL field name correctly");
860 bt_put(ret_field_type
);
861 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
863 "bt_ctf_event_class_get_field handles a NULL field type correctly");
864 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
865 &ret_field_type
, 42) < 0,
866 "bt_ctf_event_class_get_field handles an invalid index correctly");
867 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
868 &ret_field_type
, 0) == 0,
869 "bt_ctf_event_class_get_field returns a field");
870 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
871 "bt_ctf_event_class_get_field returns a correct field type");
872 bt_put(ret_field_type
);
873 ok(!strcmp(ret_string
, "uint_35"),
874 "bt_ctf_event_class_get_field returns a correct field name");
875 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
876 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
877 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
878 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
879 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
880 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
881 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
882 "complex_structure");
883 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
884 "bt_ctf_event_class_get_field_by_name returns a correct field type");
885 bt_put(ret_field_type
);
887 event
= bt_ctf_event_create(event_class
);
888 ok(event
, "Instanciate a complex event");
890 ok(bt_ctf_event_get_class(NULL
) == NULL
,
891 "bt_ctf_event_get_class handles NULL correctly");
892 ret_event_class
= bt_ctf_event_get_class(event
);
893 ok(ret_event_class
== event_class
,
894 "bt_ctf_event_get_class returns the correct event class");
895 bt_put(ret_event_class
);
897 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
898 if (!uint_35_field
) {
899 printf("uint_35_field is NULL\n");
902 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
903 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
904 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
905 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
906 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
907 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
908 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
909 &ret_unsigned_int
) == 0,
910 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
911 ok(ret_unsigned_int
== 0x0DDF00D,
912 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
913 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
914 &ret_signed_int
) < 0,
915 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
916 bt_put(uint_35_field
);
918 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
919 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
920 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
921 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
922 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
923 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
924 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
925 &ret_signed_int
) == 0,
926 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
927 ok(ret_signed_int
== -12345,
928 "bt_ctf_field_signed_integer_get_value returns the correct value");
929 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
930 &ret_unsigned_int
) < 0,
931 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
932 bt_put(int_16_field
);
934 complex_structure_field
= bt_ctf_event_get_payload(event
,
935 "complex_structure");
937 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
938 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
939 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
940 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
941 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
942 complex_structure_field
, 3);
943 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
944 bt_put(inner_structure_field
);
945 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
946 "bt_ctf_field_structure_get_field_by_index returns a correct field");
947 bt_put(ret_field_type
);
949 inner_structure_field
= bt_ctf_field_structure_get_field(
950 complex_structure_field
, "inner_structure");
951 a_string_field
= bt_ctf_field_structure_get_field(
952 complex_structure_field
, "a_string");
953 enum_variant_field
= bt_ctf_field_structure_get_field(
954 complex_structure_field
, "variant_selector");
955 variant_field
= bt_ctf_field_structure_get_field(
956 complex_structure_field
, "variant_value");
957 uint_35_field
= bt_ctf_field_structure_get_field(
958 inner_structure_field
, "seq_len");
959 a_sequence_field
= bt_ctf_field_structure_get_field(
960 inner_structure_field
, "a_sequence");
961 an_array_field
= bt_ctf_field_structure_get_field(
962 inner_structure_field
, "an_array");
964 enum_container_field
= bt_ctf_field_enumeration_get_container(
966 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
967 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
969 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
970 bt_put(int_16_field
);
971 ok(!bt_ctf_field_string_get_value(a_string_field
),
972 "bt_ctf_field_string_get_value returns NULL on an unset field");
973 bt_ctf_field_string_set_value(a_string_field
,
975 ok(!bt_ctf_field_string_get_value(NULL
),
976 "bt_ctf_field_string_get_value correctly handles NULL");
977 ok(bt_ctf_field_string_append(NULL
, "yeah"),
978 "bt_ctf_field_string_append correctly handles a NULL string field");
979 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
980 "bt_ctf_field_string_append correctly handles a NULL string value");
981 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
982 "bt_ctf_field_string_append succeeds");
983 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
984 "bt_ctf_field_string_append_len correctly handles a NULL string field");
985 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
986 "bt_ctf_field_string_append_len correctly handles a NULL string value");
987 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
988 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
989 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
990 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
991 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
992 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
993 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
994 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
996 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
997 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
998 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
999 "bt_ctf_field_string_get_value returns a correct value");
1000 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1001 SEQUENCE_TEST_LENGTH
);
1003 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1004 enum_container_field
) == NULL
,
1005 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1006 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1008 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1009 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1010 variant_type
, enum_variant_field
);
1011 ok(ret_field_type
== int_16_type
,
1012 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1014 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1015 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1016 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1017 uint_35_field
) == 0, "Set a sequence field's length");
1018 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1019 ok(ret_field
== uint_35_field
,
1020 "bt_ctf_field_sequence_get_length returns the correct length field");
1021 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1022 "bt_ctf_field_sequence_get_length properly handles NULL");
1024 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1025 int_16_field
= bt_ctf_field_sequence_get_field(
1026 a_sequence_field
, i
);
1027 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1028 bt_put(int_16_field
);
1031 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1032 int_16_field
= bt_ctf_field_array_get_field(
1034 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1035 bt_put(int_16_field
);
1038 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1039 assert(stream_event_ctx_field
);
1040 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1041 stream_event_ctx_field
, "common_event_context");
1042 BT_PUT(stream_event_ctx_field
);
1043 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1044 BT_PUT(stream_event_ctx_int_field
);
1046 bt_ctf_clock_set_time(clock
, ++current_time
);
1047 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1048 "Append a complex event to a stream");
1051 * Populate the custom packet context field with a dummy value
1052 * otherwise flush will fail.
1054 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1055 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1056 "custom_packet_context_field");
1057 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1059 ok(bt_ctf_stream_flush(stream
) == 0,
1060 "Flush a stream containing a complex event");
1062 bt_put(uint_35_field
);
1063 bt_put(a_string_field
);
1064 bt_put(inner_structure_field
);
1065 bt_put(complex_structure_field
);
1066 bt_put(a_sequence_field
);
1067 bt_put(an_array_field
);
1068 bt_put(enum_variant_field
);
1069 bt_put(enum_container_field
);
1070 bt_put(variant_field
);
1072 bt_put(packet_context_field
);
1073 bt_put(packet_context
);
1074 bt_put(uint_35_type
);
1075 bt_put(int_16_type
);
1076 bt_put(string_type
);
1077 bt_put(sequence_type
);
1079 bt_put(inner_structure_type
);
1080 bt_put(complex_structure_type
);
1081 bt_put(uint_3_type
);
1082 bt_put(enum_variant_type
);
1083 bt_put(variant_type
);
1084 bt_put(ret_field_type
);
1085 bt_put(event_class
);
1090 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1091 struct bt_ctf_field_type
*expected_type
, const char *name
)
1093 struct bt_ctf_field_type
*copy_type
;
1095 copy_type
= bt_ctf_field_get_type(field
);
1096 ok(copy_type
== expected_type
,
1097 "bt_ctf_field_copy does not copy the type (%s)", name
);
1102 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1103 struct bt_ctf_field
*field_b
, const char *name
)
1105 ok(field_a
!= field_b
,
1106 "bt_ctf_field_copy creates different pointers (%s)", name
);
1110 void field_copy_tests()
1112 struct bt_ctf_field_type
*len_type
= NULL
;
1113 struct bt_ctf_field_type
*fp_type
= NULL
;
1114 struct bt_ctf_field_type
*s_type
= NULL
;
1115 struct bt_ctf_field_type
*e_int_type
= NULL
;
1116 struct bt_ctf_field_type
*e_type
= NULL
;
1117 struct bt_ctf_field_type
*v_type
= NULL
;
1118 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1119 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1120 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1121 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1122 struct bt_ctf_field_type
*strct_type
= NULL
;
1123 struct bt_ctf_field
*len
= NULL
;
1124 struct bt_ctf_field
*fp
= NULL
;
1125 struct bt_ctf_field
*s
= NULL
;
1126 struct bt_ctf_field
*e_int
= NULL
;
1127 struct bt_ctf_field
*e
= NULL
;
1128 struct bt_ctf_field
*v
= NULL
;
1129 struct bt_ctf_field
*v_selected
= NULL
;
1130 struct bt_ctf_field
*v_selected_cur
= NULL
;
1131 struct bt_ctf_field
*v_selected_0
= NULL
;
1132 struct bt_ctf_field
*v_selected_1
= NULL
;
1133 struct bt_ctf_field
*v_selected_2
= NULL
;
1134 struct bt_ctf_field
*v_selected_3
= NULL
;
1135 struct bt_ctf_field
*v_selected_4
= NULL
;
1136 struct bt_ctf_field
*v_selected_5
= NULL
;
1137 struct bt_ctf_field
*v_selected_6
= NULL
;
1138 struct bt_ctf_field
*a
= NULL
;
1139 struct bt_ctf_field
*a_0
= NULL
;
1140 struct bt_ctf_field
*a_1
= NULL
;
1141 struct bt_ctf_field
*a_2
= NULL
;
1142 struct bt_ctf_field
*a_3
= NULL
;
1143 struct bt_ctf_field
*a_4
= NULL
;
1144 struct bt_ctf_field
*strct
= NULL
;
1145 struct bt_ctf_field
*len_copy
= NULL
;
1146 struct bt_ctf_field
*fp_copy
= NULL
;
1147 struct bt_ctf_field
*s_copy
= NULL
;
1148 struct bt_ctf_field
*e_int_copy
= NULL
;
1149 struct bt_ctf_field
*e_copy
= NULL
;
1150 struct bt_ctf_field
*v_copy
= NULL
;
1151 struct bt_ctf_field
*v_selected_copy
= NULL
;
1152 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1153 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1154 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1155 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1156 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1157 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1158 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1159 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1160 struct bt_ctf_field
*a_copy
= NULL
;
1161 struct bt_ctf_field
*a_0_copy
= NULL
;
1162 struct bt_ctf_field
*a_1_copy
= NULL
;
1163 struct bt_ctf_field
*a_2_copy
= NULL
;
1164 struct bt_ctf_field
*a_3_copy
= NULL
;
1165 struct bt_ctf_field
*a_4_copy
= NULL
;
1166 struct bt_ctf_field
*strct_copy
= NULL
;
1167 struct bt_ctf_field_type_enumeration_mapping_iterator
*e_iter
= NULL
;
1168 uint64_t uint64_t_val
;
1169 const char *str_val
;
1173 /* create len type */
1174 len_type
= bt_ctf_field_type_integer_create(32);
1177 /* create fp type */
1178 fp_type
= bt_ctf_field_type_floating_point_create();
1182 s_type
= bt_ctf_field_type_string_create();
1185 /* create e_int type */
1186 e_int_type
= bt_ctf_field_type_integer_create(8);
1190 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1192 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1195 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1199 /* create v_label1 type */
1200 v_label1_type
= bt_ctf_field_type_string_create();
1201 assert(v_label1_type
);
1203 /* create v_label1_array type */
1204 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1205 assert(v_label1_array_type
);
1207 /* create v_label2 type */
1208 v_label2_type
= bt_ctf_field_type_integer_create(16);
1209 assert(v_label2_type
);
1211 /* create v_label2_seq type */
1212 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1214 assert(v_label2_seq_type
);
1217 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1219 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1222 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1226 /* create strct type */
1227 strct_type
= bt_ctf_field_type_structure_create();
1229 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1232 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1234 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1236 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1238 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1240 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1241 v_label1_array_type
, "a");
1245 strct
= bt_ctf_field_create(strct_type
);
1249 len
= bt_ctf_field_structure_get_field(strct
, "len");
1253 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1257 s
= bt_ctf_field_structure_get_field(strct
, "s");
1261 e
= bt_ctf_field_structure_get_field(strct
, "e");
1264 /* get e_int (underlying integer) */
1265 e_int
= bt_ctf_field_enumeration_get_container(e
);
1269 v
= bt_ctf_field_structure_get_field(strct
, "v");
1273 a
= bt_ctf_field_structure_get_field(strct
, "a");
1277 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1281 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1285 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1288 /* set e field (LABEL2) */
1289 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1293 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1295 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1296 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1297 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1298 ok(v_selected_cur
== v_selected
,
1299 "bt_ctf_field_variant_get_current_field returns the current field");
1300 bt_put(v_selected_cur
);
1302 /* set selected v field */
1303 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1305 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1306 assert(v_selected_0
);
1307 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1309 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1310 assert(v_selected_1
);
1311 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1313 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1314 assert(v_selected_2
);
1315 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1317 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1318 assert(v_selected_3
);
1319 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1321 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1322 assert(v_selected_4
);
1323 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1325 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1326 assert(v_selected_5
);
1327 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1329 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1330 assert(v_selected_6
);
1331 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1335 a_0
= bt_ctf_field_array_get_field(a
, 0);
1337 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1339 a_1
= bt_ctf_field_array_get_field(a
, 1);
1341 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1343 a_2
= bt_ctf_field_array_get_field(a
, 2);
1345 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1347 a_3
= bt_ctf_field_array_get_field(a
, 3);
1349 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1351 a_4
= bt_ctf_field_array_get_field(a
, 4);
1353 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1356 /* create copy of strct */
1357 ok(!bt_ctf_field_copy(NULL
),
1358 "bt_ctf_field_copy handles NULL correctly");
1359 strct_copy
= bt_ctf_field_copy(strct
);
1361 "bt_ctf_field_copy returns a valid pointer");
1363 /* get all copied fields */
1364 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1366 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1368 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1370 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1372 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1374 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1376 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1377 assert(v_selected_copy
);
1378 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1379 assert(v_selected_0_copy
);
1380 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1381 assert(v_selected_1_copy
);
1382 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1383 assert(v_selected_2_copy
);
1384 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1385 assert(v_selected_3_copy
);
1386 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1387 assert(v_selected_4_copy
);
1388 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1389 assert(v_selected_5_copy
);
1390 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1391 assert(v_selected_6_copy
);
1392 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1393 "sequence field copy is not too large");
1394 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1396 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1398 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1400 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1402 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1404 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1406 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1407 "array field copy is not too large");
1409 /* make sure copied fields are different pointers */
1410 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1411 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1412 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1413 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1414 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1415 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1416 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1417 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1419 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1421 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1423 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1425 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1427 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1429 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1431 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1433 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1434 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1435 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1436 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1437 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1438 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1440 /* make sure copied fields share the same types */
1441 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1442 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1443 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1444 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1445 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1446 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1447 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1449 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1451 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1453 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1455 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1457 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1459 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1461 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1463 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1464 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1465 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1466 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1467 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1468 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1470 /* validate len copy */
1471 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1473 ok(uint64_t_val
== 7,
1474 "bt_ctf_field_copy creates a valid integer field copy");
1476 /* validate fp copy */
1477 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1479 ok(double_val
== 3.14,
1480 "bt_ctf_field_copy creates a valid floating point number field copy");
1482 /* validate s copy */
1483 str_val
= bt_ctf_field_string_get_value(s_copy
);
1484 ok(str_val
&& !strcmp(str_val
, "btbt"),
1485 "bt_ctf_field_copy creates a valid string field copy");
1487 /* validate e_int copy */
1488 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1491 ok(uint64_t_val
== 23,
1492 "bt_ctf_field_copy creates a valid enum's integer field copy");
1494 /* validate e copy */
1495 e_iter
= bt_ctf_field_enumeration_get_mappings(e_copy
);
1496 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter
,
1497 &str_val
, NULL
, NULL
);
1498 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1499 "bt_ctf_field_copy creates a valid enum field copy");
1501 /* validate v_selected copy */
1502 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1503 assert(v_selected_copy_len
);
1504 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1507 ok(uint64_t_val
== 7,
1508 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1509 bt_put(v_selected_copy_len
);
1510 v_selected_copy_len
= NULL
;
1512 /* validate v_selected copy fields */
1513 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1516 ok(uint64_t_val
== 7,
1517 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1518 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1521 ok(uint64_t_val
== 6,
1522 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1523 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1526 ok(uint64_t_val
== 5,
1527 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1528 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1531 ok(uint64_t_val
== 4,
1532 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1533 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1536 ok(uint64_t_val
== 3,
1537 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1538 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1541 ok(uint64_t_val
== 2,
1542 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1543 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1546 ok(uint64_t_val
== 1,
1547 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1549 /* validate a copy fields */
1550 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1551 ok(str_val
&& !strcmp(str_val
, "a_0"),
1552 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1553 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1554 ok(str_val
&& !strcmp(str_val
, "a_1"),
1555 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1556 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1557 ok(str_val
&& !strcmp(str_val
, "a_2"),
1558 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1559 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1560 ok(str_val
&& !strcmp(str_val
, "a_3"),
1561 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1562 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1563 ok(str_val
&& !strcmp(str_val
, "a_4"),
1564 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1566 /* put everything */
1573 bt_put(v_label1_type
);
1574 bt_put(v_label1_array_type
);
1575 bt_put(v_label2_type
);
1576 bt_put(v_label2_seq_type
);
1585 bt_put(v_selected_0
);
1586 bt_put(v_selected_1
);
1587 bt_put(v_selected_2
);
1588 bt_put(v_selected_3
);
1589 bt_put(v_selected_4
);
1590 bt_put(v_selected_5
);
1591 bt_put(v_selected_6
);
1605 bt_put(v_selected_copy
);
1606 bt_put(v_selected_0_copy
);
1607 bt_put(v_selected_1_copy
);
1608 bt_put(v_selected_2_copy
);
1609 bt_put(v_selected_3_copy
);
1610 bt_put(v_selected_4_copy
);
1611 bt_put(v_selected_5_copy
);
1612 bt_put(v_selected_6_copy
);
1624 void type_field_tests()
1626 struct bt_ctf_field
*uint_12
;
1627 struct bt_ctf_field
*int_16
;
1628 struct bt_ctf_field
*string
;
1629 struct bt_ctf_field
*enumeration
;
1630 struct bt_ctf_field_type
*composite_structure_type
;
1631 struct bt_ctf_field_type
*structure_seq_type
;
1632 struct bt_ctf_field_type
*string_type
;
1633 struct bt_ctf_field_type
*sequence_type
;
1634 struct bt_ctf_field_type
*uint_8_type
;
1635 struct bt_ctf_field_type
*int_16_type
;
1636 struct bt_ctf_field_type
*uint_12_type
=
1637 bt_ctf_field_type_integer_create(12);
1638 struct bt_ctf_field_type
*enumeration_type
;
1639 struct bt_ctf_field_type
*returned_type
;
1640 const char *ret_string
;
1642 returned_type
= bt_ctf_field_get_type(NULL
);
1643 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1645 ok(uint_12_type
, "Create an unsigned integer type");
1646 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1647 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1648 "Set integer type's base as binary");
1649 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1650 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1651 "Set integer type's base as decimal");
1652 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1653 BT_CTF_INTEGER_BASE_UNKNOWN
),
1654 "Reject integer type's base set as unknown");
1655 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1656 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1657 "Set integer type's base as octal");
1658 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1659 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1660 "Set integer type's base as hexadecimal");
1661 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1662 "Reject unknown integer base value");
1663 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1664 "Set integer type signedness to signed");
1665 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1666 "Set integer type signedness to unsigned");
1667 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1668 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1669 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1670 "bt_ctf_field_type_integer_get_size returns a correct value");
1671 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1672 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1673 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1674 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1676 ok(bt_ctf_field_type_set_byte_order(NULL
,
1677 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1678 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1679 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1680 (enum bt_ctf_byte_order
) 42) < 0,
1681 "bt_ctf_field_type_set_byte_order rejects invalid values");
1682 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1683 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1684 "Set an integer's byte order to little endian");
1685 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1686 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1687 "Set an integer's byte order to big endian");
1688 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1689 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1690 "bt_ctf_field_type_get_byte_order returns a correct value");
1691 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1692 BT_CTF_BYTE_ORDER_UNKNOWN
,
1693 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1695 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1696 BT_CTF_FIELD_TYPE_ID_UNKNOWN
,
1697 "bt_ctf_field_type_get_type_id handles NULL correctly");
1698 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1699 BT_CTF_FIELD_TYPE_ID_INTEGER
,
1700 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1702 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1703 BT_CTF_INTEGER_BASE_UNKNOWN
,
1704 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1705 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1706 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1707 "bt_ctf_field_type_integer_get_base returns a correct value");
1709 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1710 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1711 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1712 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1713 (enum bt_ctf_string_encoding
) 123) < 0,
1714 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1715 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1716 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1717 "Set integer type encoding to UTF8");
1718 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1719 BT_CTF_STRING_ENCODING_UNKNOWN
,
1720 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1721 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1722 BT_CTF_STRING_ENCODING_UTF8
,
1723 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1725 int_16_type
= bt_ctf_field_type_integer_create(16);
1726 assert(int_16_type
);
1727 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1728 "Set signedness of 16 bit integer to true");
1729 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1730 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1731 uint_8_type
= bt_ctf_field_type_integer_create(8);
1733 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1734 ok(sequence_type
, "Create a sequence of int16_t type");
1735 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1736 BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
1737 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1739 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1740 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1741 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1743 ok(!strcmp(ret_string
, "seq_len"),
1744 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1745 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1746 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1747 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1749 ok(returned_type
== int_16_type
,
1750 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1751 bt_put(returned_type
);
1753 string_type
= bt_ctf_field_type_string_create();
1754 ok(string_type
, "Create a string type");
1755 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1756 BT_CTF_STRING_ENCODING_NONE
),
1757 "Reject invalid \"None\" string encoding");
1758 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1760 "Reject invalid string encoding");
1761 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1762 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1763 "Set string encoding to ASCII");
1765 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1766 BT_CTF_STRING_ENCODING_UNKNOWN
,
1767 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1768 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1769 BT_CTF_STRING_ENCODING_ASCII
,
1770 "bt_ctf_field_type_string_get_encoding returns the correct value");
1772 structure_seq_type
= bt_ctf_field_type_structure_create();
1773 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1774 BT_CTF_FIELD_TYPE_ID_STRUCT
,
1775 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1776 ok(structure_seq_type
, "Create a structure type");
1777 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1778 uint_8_type
, "seq_len") == 0,
1779 "Add a uint8_t type to a structure");
1780 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1781 sequence_type
, "a_sequence") == 0,
1782 "Add a sequence type to a structure");
1784 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1785 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1786 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1787 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1789 ok(bt_ctf_field_type_structure_get_field(NULL
,
1790 &ret_string
, &returned_type
, 1) < 0,
1791 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1792 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1793 NULL
, &returned_type
, 1) == 0,
1794 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1795 bt_put(returned_type
);
1796 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1797 &ret_string
, NULL
, 1) == 0,
1798 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1799 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1800 &ret_string
, &returned_type
, 10) < 0,
1801 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1802 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1803 &ret_string
, &returned_type
, 1) == 0,
1804 "bt_ctf_field_type_structure_get_field returns a field");
1805 ok(!strcmp(ret_string
, "a_sequence"),
1806 "bt_ctf_field_type_structure_get_field returns a correct field name");
1807 ok(returned_type
== sequence_type
,
1808 "bt_ctf_field_type_structure_get_field returns a correct field type");
1809 bt_put(returned_type
);
1811 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1812 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1813 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1814 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1815 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1816 structure_seq_type
, "a_sequence");
1817 ok(returned_type
== sequence_type
,
1818 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1819 bt_put(returned_type
);
1821 composite_structure_type
= bt_ctf_field_type_structure_create();
1822 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1823 string_type
, "a_string") == 0,
1824 "Add a string type to a structure");
1825 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1826 structure_seq_type
, "inner_structure") == 0,
1827 "Add a structure type to a structure");
1829 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1830 NULL
, "a_sequence") == NULL
,
1831 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1832 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1833 structure_seq_type
, NULL
) == NULL
,
1834 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1835 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1836 structure_seq_type
, "a_sequence");
1837 ok(returned_type
== sequence_type
,
1838 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1839 bt_put(returned_type
);
1841 int_16
= bt_ctf_field_create(int_16_type
);
1842 ok(int_16
, "Instanciate a signed 16-bit integer");
1843 uint_12
= bt_ctf_field_create(uint_12_type
);
1844 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1845 returned_type
= bt_ctf_field_get_type(int_16
);
1846 ok(returned_type
== int_16_type
,
1847 "bt_ctf_field_get_type returns the correct type");
1849 /* Can't modify types after instanciating them */
1850 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1851 BT_CTF_INTEGER_BASE_DECIMAL
),
1852 "Check an integer type' base can't be modified after instanciation");
1853 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1854 "Check an integer type's signedness can't be modified after instanciation");
1856 /* Check signed property is checked */
1857 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1858 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1859 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1860 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1862 /* Check overflows are properly tested for */
1863 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1864 "Check -32768 is allowed for a signed 16-bit integer");
1865 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1866 "Check 32767 is allowed for a signed 16-bit integer");
1867 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1868 "Check 32768 is not allowed for a signed 16-bit integer");
1869 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1870 "Check -32769 is not allowed for a signed 16-bit integer");
1871 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1872 "Check -42 is allowed for a signed 16-bit integer");
1874 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1875 "Check 4095 is allowed for an unsigned 12-bit integer");
1876 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1877 "Check 4096 is not allowed for a unsigned 12-bit integer");
1878 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1879 "Check 0 is allowed for an unsigned 12-bit integer");
1881 string
= bt_ctf_field_create(string_type
);
1882 ok(string
, "Instanciate a string field");
1883 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1884 "Set a string's value");
1886 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1887 ok(enumeration_type
,
1888 "Create an enumeration type with an unsigned 12-bit integer as container");
1889 enumeration
= bt_ctf_field_create(enumeration_type
);
1891 "Check enumeration types are validated before instantiation");
1896 bt_put(enumeration
);
1897 bt_put(composite_structure_type
);
1898 bt_put(structure_seq_type
);
1899 bt_put(string_type
);
1900 bt_put(sequence_type
);
1901 bt_put(uint_8_type
);
1902 bt_put(int_16_type
);
1903 bt_put(uint_12_type
);
1904 bt_put(enumeration_type
);
1905 bt_put(returned_type
);
1909 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1910 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1913 * Append enough events to force the underlying packet to be resized.
1914 * Also tests that a new event can be declared after a stream has been
1915 * instantiated and used/flushed.
1919 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1921 struct bt_ctf_field_type
*integer_type
=
1922 bt_ctf_field_type_integer_create(17);
1923 struct bt_ctf_field_type
*string_type
=
1924 bt_ctf_field_type_string_create();
1925 struct bt_ctf_event
*event
= NULL
;
1926 struct bt_ctf_field
*ret_field
= NULL
;
1927 struct bt_ctf_field_type
*ret_field_type
= NULL
;
1928 uint64_t ret_uint64
;
1929 int events_appended
= 0;
1930 struct bt_ctf_field
*packet_context
= NULL
,
1931 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
1932 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
1933 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
1934 struct bt_ctf_field_type
*ep_type
= NULL
;
1936 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1938 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1940 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1941 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1947 * bt_ctf_stream_class_add_event_class() copies the field types
1948 * of event_class, so we retrieve the new ones to create the
1949 * appropriate fields.
1951 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
1953 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1954 ep_type
, "field_1");
1955 assert(ep_field_1_type
);
1956 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1957 ep_type
, "a_string");
1958 assert(ep_a_string_type
);
1960 event
= bt_ctf_event_create(event_class
);
1961 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
1962 ret_field_type
= bt_ctf_field_get_type(ret_field
);
1963 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
1964 "bt_ctf_event_get_payload_by_index returns a correct field");
1965 bt_put(ret_field_type
);
1968 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
1969 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1970 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
1971 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1974 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
1975 event
= bt_ctf_event_create(event_class
);
1976 struct bt_ctf_field
*integer
=
1977 bt_ctf_field_create(ep_field_1_type
);
1978 struct bt_ctf_field
*string
=
1979 bt_ctf_field_create(ep_a_string_type
);
1981 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1982 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1983 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1986 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1987 ret
|= bt_ctf_event_set_payload(event
, "a_string",
1991 /* Populate stream event context */
1992 stream_event_context
=
1993 bt_ctf_event_get_stream_event_context(event
);
1994 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
1995 "common_event_context");
1996 BT_PUT(stream_event_context
);
1997 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2001 ret
|= bt_ctf_stream_append_event(stream
, event
);
2009 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2010 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2011 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2012 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2013 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2014 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2015 ok(ret
== 0 && ret_uint64
== 0,
2016 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2017 bt_ctf_stream_append_discarded_events(stream
, 1000);
2018 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2019 ok(ret
== 0 && ret_uint64
== 1000,
2020 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2023 ok(events_appended
, "Append 100 000 events to a stream");
2026 * Populate the custom packet context field with a dummy value
2027 * otherwise flush will fail.
2029 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2030 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2031 "custom_packet_context_field");
2032 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2034 ok(bt_ctf_stream_flush(stream
) == 0,
2035 "Flush a stream that forces a packet resize");
2036 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2037 ok(ret
== 0 && ret_uint64
== 1000,
2038 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2039 bt_put(integer_type
);
2040 bt_put(string_type
);
2041 bt_put(packet_context
);
2042 bt_put(packet_context_field
);
2043 bt_put(stream_event_context
);
2044 bt_put(event_class
);
2045 bt_put(ep_field_1_type
);
2046 bt_put(ep_a_string_type
);
2051 void test_empty_stream(struct bt_ctf_writer
*writer
)
2054 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2055 struct bt_ctf_stream_class
*stream_class
= NULL
;
2056 struct bt_ctf_stream
*stream
= NULL
;
2058 trace
= bt_ctf_writer_get_trace(writer
);
2060 diag("Failed to get trace from writer");
2065 stream_class
= bt_ctf_stream_class_create("empty_stream");
2066 if (!stream_class
) {
2067 diag("Failed to create stream class");
2072 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2074 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
2077 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2078 "bt_ctf_stream_class_get_trace handles NULL correctly");
2079 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2080 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2082 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2084 diag("Failed to create writer stream");
2089 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2090 ok(ret_trace
== trace
,
2091 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2094 "Created a stream class with default attributes and an empty stream");
2098 bt_put(stream_class
);
2102 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
,
2103 struct bt_ctf_clock
*clock
)
2106 struct bt_ctf_stream_class
*stream_class
= NULL
;
2107 struct bt_ctf_stream
*stream
= NULL
;
2108 struct bt_ctf_field_type
*integer_type
= NULL
,
2109 *sequence_type
= NULL
, *event_header_type
= NULL
;
2110 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2111 *event_header
= NULL
, *packet_header
= NULL
;
2112 struct bt_ctf_event_class
*event_class
= NULL
;
2113 struct bt_ctf_event
*event
= NULL
;
2115 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2116 if (!stream_class
) {
2117 fail("Failed to create stream class");
2121 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2123 fail("Failed to set stream class clock");
2128 * Customize event header to add an "seq_len" integer member
2129 * which will be used as the length of a sequence in an event of this
2132 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2134 if (!event_header_type
) {
2135 fail("Failed to get event header type");
2139 integer_type
= bt_ctf_field_type_integer_create(13);
2140 if (!integer_type
) {
2141 fail("Failed to create length integer type");
2145 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2146 integer_type
, "seq_len");
2148 fail("Failed to add a new field to stream event header");
2152 event_class
= bt_ctf_event_class_create("sequence_event");
2154 fail("Failed to create event class");
2159 * This event's payload will contain a sequence which references
2160 * stream.event.header.seq_len as its length field.
2162 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2163 "stream.event.header.seq_len");
2164 if (!sequence_type
) {
2165 fail("Failed to create a sequence");
2169 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2172 fail("Failed to add a sequence to an event class");
2176 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2178 fail("Failed to add event class to stream class");
2182 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2184 fail("Failed to create stream")
2189 * We have defined a custom packet header field. We have to populate it
2192 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2193 if (!packet_header
) {
2194 fail("Failed to get stream packet header");
2198 integer
= bt_ctf_field_structure_get_field(packet_header
,
2199 "custom_trace_packet_header_field");
2201 fail("Failed to retrieve custom_trace_packet_header_field");
2205 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2207 fail("Failed to set custom_trace_packet_header_field value");
2212 event
= bt_ctf_event_create(event_class
);
2214 fail("Failed to create event");
2218 event_header
= bt_ctf_event_get_header(event
);
2219 if (!event_header
) {
2220 fail("Failed to get event header");
2224 integer
= bt_ctf_field_structure_get_field(event_header
,
2227 fail("Failed to get seq_len field from event header");
2231 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2233 fail("Failed to set seq_len value in event header");
2237 /* Populate both sequence integer fields */
2238 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2240 fail("Failed to retrieve sequence from event");
2244 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2246 fail("Failed to set sequence length");
2251 for (i
= 0; i
< 2; i
++) {
2252 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2254 fail("Failed to retrieve sequence element");
2258 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2260 fail("Failed to set sequence element value");
2268 ret
= bt_ctf_stream_append_event(stream
, event
);
2270 fail("Failed to append event to stream");
2274 ret
= bt_ctf_stream_flush(stream
);
2276 fail("Failed to flush custom_event_header stream");
2280 bt_put(stream_class
);
2281 bt_put(event_class
);
2285 bt_put(event_header
);
2286 bt_put(packet_header
);
2287 bt_put(sequence_type
);
2288 bt_put(integer_type
);
2289 bt_put(event_header_type
);
2293 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
,
2294 struct bt_ctf_clock
*clock
)
2297 struct bt_ctf_stream_class
*stream_class
= NULL
;
2298 struct bt_ctf_stream
*stream
= NULL
,
2300 struct bt_ctf_event_class
*event_class
= NULL
;
2301 struct bt_ctf_event
*event
= NULL
;
2302 struct bt_ctf_field_type
*integer_type
= NULL
;
2303 struct bt_ctf_field
*integer
= NULL
;
2305 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2306 if (!stream_class
) {
2307 diag("Failed to create stream class");
2312 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2314 diag("Failed to set stream class clock");
2318 event_class
= bt_ctf_event_class_create("some_event_class_name");
2319 integer_type
= bt_ctf_field_type_integer_create(32);
2320 if (!integer_type
) {
2321 diag("Failed to create integer field type");
2326 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2329 diag("Failed to add field to event class");
2333 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2336 diag("Failed to add event class to stream class");
2339 event
= bt_ctf_event_create(event_class
);
2341 diag("Failed to create event");
2346 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2348 diag("Failed to get integer field payload from event");
2353 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2355 diag("Failed to set integer field value");
2359 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2361 diag("Failed to create writer stream");
2366 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2367 "bt_ctf_event_get_stream handles NULL correctly");
2368 ok(bt_ctf_event_get_stream(event
) == NULL
,
2369 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2371 ret
= bt_ctf_stream_append_event(stream
, event
);
2373 diag("Failed to append event to stream");
2377 ret_stream
= bt_ctf_event_get_stream(event
);
2378 ok(ret_stream
== stream
,
2379 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2382 "Create an event before instanciating its associated stream");
2385 bt_put(stream_class
);
2386 bt_put(event_class
);
2388 bt_put(integer_type
);
2393 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2395 struct bt_ctf_event_class
*event_class
;
2397 event_class
= bt_ctf_event_class_create("Simple Event");
2398 assert(event_class
);
2399 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
) == 0,
2400 "two event classes with the same name may cohabit within the same stream class");
2401 bt_put(event_class
);
2403 event_class
= bt_ctf_event_class_create("different name, ok");
2404 assert(event_class
);
2405 assert(!bt_ctf_event_class_set_id(event_class
, 13));
2406 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2407 "two event classes with the same ID cannot cohabit within the same stream class");
2408 bt_put(event_class
);
2412 struct bt_ctf_event_class
*create_minimal_event_class(void)
2414 struct bt_ctf_event_class
*ec
= NULL
;
2415 struct bt_ctf_field_type
*int_ft
= NULL
;
2418 int_ft
= bt_ctf_field_type_integer_create(23);
2420 ec
= bt_ctf_event_class_create("minimal");
2422 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2430 void test_create_writer_vs_non_writer_mode(void)
2433 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2434 const char *writer_stream_name
= "writer stream instance";
2435 struct bt_ctf_writer
*writer
= NULL
;
2436 struct bt_ctf_trace
*writer_trace
= NULL
;
2437 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2438 struct bt_ctf_stream
*writer_stream
= NULL
;
2439 struct bt_ctf_stream
*writer_stream2
= NULL
;
2440 struct bt_ctf_stream
*packet_stream
= NULL
;
2441 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2442 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2443 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2444 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2445 struct bt_ctf_event_class
*writer_ec
= NULL
;
2446 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2447 struct bt_ctf_event
*event
= NULL
;
2448 struct bt_ctf_event
*event2
= NULL
;
2449 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2450 struct bt_ctf_field
*int_field
= NULL
;
2451 struct bt_ctf_clock
*writer_clock
= NULL
;
2452 struct bt_ctf_clock_class
*non_writer_clock_class
= NULL
;
2453 struct bt_ctf_packet
*packet
= NULL
;
2454 struct bt_ctf_packet
*packet2
= NULL
;
2456 if (!bt_mkdtemp(trace_path
)) {
2460 /* Create empty structure field type (event header) */
2461 empty_struct_ft
= bt_ctf_field_type_structure_create();
2462 assert(empty_struct_ft
);
2464 /* Create writer, writer stream class, stream, and clock */
2465 writer
= bt_ctf_writer_create(trace_path
);
2467 writer_clock
= bt_ctf_clock_create("writer_clock");
2468 assert(writer_clock
);
2469 ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
2471 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2473 writer_trace
= bt_ctf_writer_get_trace(writer
);
2474 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2475 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2477 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2480 ret
= bt_ctf_stream_class_set_clock(writer_sc
, writer_clock
);
2482 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2484 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2485 assert(writer_stream
);
2486 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2487 "bt_ctf_stream_get_name() returns the stream's name");
2489 /* Create non-writer trace, stream class, stream, and clock */
2490 non_writer_trace
= bt_ctf_trace_create();
2491 assert(non_writer_trace
);
2492 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2493 assert(non_writer_sc
);
2494 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2497 ret
= bt_ctf_stream_class_set_packet_context_type(non_writer_sc
, NULL
);
2499 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2501 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2502 assert(non_writer_stream
);
2503 non_writer_clock_class
=
2504 bt_ctf_clock_class_create("non_writer_clock_class",
2506 assert(non_writer_clock_class
);
2507 ret
= bt_ctf_trace_add_clock_class(non_writer_trace
,
2508 non_writer_clock_class
);
2511 /* Create event class and event */
2512 writer_ec
= create_minimal_event_class();
2514 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2516 event
= bt_ctf_event_create(writer_ec
);
2518 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2520 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2523 * Verify non-writer stream: it should be impossible to append
2526 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2527 "bt_ctf_stream_append_event() fails with a non-writer stream");
2530 * Verify writer stream: it should be possible to append an
2533 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2534 "bt_ctf_stream_append_event() succeeds with a writer stream");
2537 * It should be possible to create a packet from a non-writer
2538 * stream, but not from a writer stream.
2540 packet
= bt_ctf_packet_create(writer_stream
);
2541 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2542 packet
= bt_ctf_packet_create(non_writer_stream
);
2543 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2544 packet_stream
= bt_ctf_packet_get_stream(packet
);
2545 ok(packet_stream
== non_writer_stream
,
2546 "bt_ctf_packet_get_stream() returns the correct stream");
2549 * It should not be possible to append an event associated to
2550 * a stream to a different stream.
2552 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2553 assert(writer_stream2
);
2554 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2555 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2558 * It should not be possible to set the packet of an event
2559 * associated to a given stream to a packet associated with
2560 * a different stream.
2562 ok(bt_ctf_event_set_packet(event
, packet
),
2563 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2566 * It should be possible to set the packet of a fresh event, as
2567 * long as the originating stream classes are the same.
2569 event2
= bt_ctf_event_create(writer_ec
);
2571 ok(bt_ctf_event_set_packet(event2
, packet
),
2572 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2573 non_writer_ec
= create_minimal_event_class();
2574 assert(non_writer_ec
);
2575 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2578 event2
= bt_ctf_event_create(non_writer_ec
);
2580 ok(!bt_ctf_event_set_packet(event2
, packet
),
2581 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2584 * It should be possible to set a packet created from the same
2585 * stream to an event with an existing packet.
2587 packet2
= bt_ctf_packet_create(non_writer_stream
);
2589 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2590 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2594 * It should not be possible to set a packet created from a
2595 * different stream to an event with an existing packet.
2597 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2598 assert(non_writer_stream2
);
2599 packet2
= bt_ctf_packet_create(non_writer_stream
);
2601 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2602 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2605 bt_put(writer_trace
);
2607 bt_put(writer_stream
);
2608 bt_put(writer_stream2
);
2609 bt_put(non_writer_trace
);
2610 bt_put(non_writer_sc
);
2611 bt_put(non_writer_stream
);
2612 bt_put(non_writer_stream2
);
2613 bt_put(packet_stream
);
2615 bt_put(non_writer_ec
);
2619 bt_put(empty_struct_ft
);
2620 bt_put(writer_clock
);
2621 bt_put(non_writer_clock_class
);
2624 recursive_rmdir(trace_path
);
2628 void test_clock_utils(void)
2631 struct bt_ctf_clock
*clock
= NULL
;
2633 clock
= bt_ctf_clock_create("water");
2635 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2637 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2639 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2641 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2647 void test_set_clock_non_writer_stream_class(void)
2649 struct bt_ctf_clock
*clock
;
2650 struct bt_ctf_trace
*trace
;
2651 struct bt_ctf_stream_class
*sc
;
2654 clock
= bt_ctf_clock_create("the_clock");
2657 trace
= bt_ctf_trace_create();
2660 sc
= bt_ctf_stream_class_create(NULL
);
2663 ret
= bt_ctf_stream_class_set_clock(sc
, clock
);
2666 ret
= bt_ctf_trace_add_stream_class(trace
, sc
);
2668 "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock");
2676 void test_static_trace(void)
2678 struct bt_ctf_trace
*trace
;
2679 struct bt_ctf_stream_class
*stream_class
;
2680 struct bt_ctf_stream_class
*stream_class2
;
2681 struct bt_ctf_stream
*stream
;
2682 struct bt_ctf_clock_class
*clock_class
;
2685 trace
= bt_ctf_trace_create();
2687 stream_class
= bt_ctf_stream_class_create(NULL
);
2688 assert(stream_class
);
2689 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2691 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
2693 stream
= bt_ctf_stream_create(stream_class
, "hello");
2694 ok(stream
, "bt_ctf_stream_create() succeeds with a non-static trace");
2696 ok(!bt_ctf_trace_is_static(trace
),
2697 "bt_ctf_trace_is_static() returns the expected value");
2698 ok(bt_ctf_trace_set_is_static(trace
) == 0,
2699 "bt_ctf_trace_set_is_static() succeeds");
2700 ok(bt_ctf_trace_is_static(trace
),
2701 "bt_ctf_trace_is_static() returns the expected value");
2702 clock_class
= bt_ctf_clock_class_create("yes", 1000000000);
2703 assert(clock_class
);
2704 stream_class2
= bt_ctf_stream_class_create(NULL
);
2705 assert(stream_class2
);
2706 ok(bt_ctf_trace_add_stream_class(trace
, stream_class2
),
2707 "bt_ctf_trace_add_stream_class() fails with a static trace");
2708 ok(bt_ctf_trace_add_clock_class(trace
, clock_class
),
2709 "bt_ctf_trace_add_clock_class() fails with a static trace");
2710 ok(!bt_ctf_stream_create(stream_class
, "hello2"),
2711 "bt_ctf_stream_create() fails with a static trace");
2714 bt_put(stream_class
);
2715 bt_put(stream_class2
);
2716 bt_put(clock_class
);
2720 void trace_is_static_listener(struct bt_ctf_trace
*trace
, void *data
)
2722 *((int *) data
) = 1;
2726 void test_trace_is_static_listener(void)
2728 struct bt_ctf_trace
*trace
;
2739 trace
= bt_ctf_trace_create();
2741 ret
= bt_ctf_trace_add_is_static_listener(NULL
,
2742 trace_is_static_listener
, &called1
);
2743 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (trace)");
2744 ret
= bt_ctf_trace_add_is_static_listener(trace
, NULL
, &called1
);
2745 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (listener)");
2746 listener1_id
= bt_ctf_trace_add_is_static_listener(trace
,
2747 trace_is_static_listener
, &called1
);
2748 ok(listener1_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (1)");
2749 listener2_id
= bt_ctf_trace_add_is_static_listener(trace
,
2750 trace_is_static_listener
, &called2
);
2751 ok(listener2_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (2)");
2752 listener3_id
= bt_ctf_trace_add_is_static_listener(trace
,
2753 trace_is_static_listener
, &called3
);
2754 ok(listener3_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (3)");
2755 ret
= bt_ctf_trace_remove_is_static_listener(NULL
, 0);
2756 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles NULL (trace)");
2757 ret
= bt_ctf_trace_remove_is_static_listener(trace
, -2);
2758 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (negative)");
2759 ret
= bt_ctf_trace_remove_is_static_listener(trace
, 77);
2760 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (non existing)");
2761 ret
= bt_ctf_trace_remove_is_static_listener(trace
, listener2_id
);
2762 ok(ret
== 0, "bt_ctf_trace_remove_is_static_listener() succeeds");
2763 listener4_id
= bt_ctf_trace_add_is_static_listener(trace
,
2764 trace_is_static_listener
, &called4
);
2765 ok(listener4_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (4)");
2766 ok(called1
== 0, "\"trace is static\" listener not called before the trace is made static (1)");
2767 ok(called2
== 0, "\"trace is static\" listener not called before the trace is made static (2)");
2768 ok(called3
== 0, "\"trace is static\" listener not called before the trace is made static (3)");
2769 ok(called4
== 0, "\"trace is static\" listener not called before the trace is made static (4)");
2770 ret
= bt_ctf_trace_set_is_static(trace
);
2772 ret
= bt_ctf_trace_add_is_static_listener(trace
,
2773 trace_is_static_listener
, &called1
);
2775 "bt_ctf_trace_add_is_static_listener() fails when the trace is static");
2776 ok(called1
== 1, "\"trace is static\" listener called when the trace is made static (1)");
2777 ok(called2
== 0, "\"trace is static\" listener not called when the trace is made static (2)");
2778 ok(called3
== 1, "\"trace is static\" listener called when the trace is made static (3)");
2779 ok(called4
== 1, "\"trace is static\" listener called when the trace is made static (4)");
2785 ok(called1
== 0, "\"trace is static\" listener not called after the trace is put (1)");
2786 ok(called2
== 0, "\"trace is static\" listener not called after the trace is put (2)");
2787 ok(called3
== 0, "\"trace is static\" listener not called after the trace is put (3)");
2788 ok(called4
== 0, "\"trace is static\" listener not called after the trace is put (4)");
2792 void test_trace_uuid(void)
2794 struct bt_ctf_trace
*trace
;
2795 const unsigned char uuid
[] = {
2796 0x35, 0x92, 0x63, 0xab, 0xb4, 0xbe, 0x40, 0xb4,
2797 0xb2, 0x60, 0xd3, 0xf1, 0x3b, 0xb0, 0xd8, 0x59,
2799 const unsigned char *ret_uuid
;
2801 trace
= bt_ctf_trace_create();
2803 ok(!bt_ctf_trace_get_uuid(NULL
),
2804 "bt_ctf_trace_get_uuid() handles NULL");
2805 ok(!bt_ctf_trace_get_uuid(trace
),
2806 "bt_ctf_trace_get_uuid() returns NULL initially");
2807 ok(bt_ctf_trace_set_uuid(NULL
, uuid
),
2808 "bt_ctf_trace_set_uuid() handles NULL (trace)");
2809 ok(bt_ctf_trace_set_uuid(trace
, NULL
),
2810 "bt_ctf_trace_set_uuid() handles NULL (UUID)");
2811 ok(bt_ctf_trace_set_uuid(trace
, uuid
) == 0,
2812 "bt_ctf_trace_set_uuid() succeeds with a valid UUID");
2813 ret_uuid
= bt_ctf_trace_get_uuid(trace
);
2814 ok(ret_uuid
, "bt_ctf_trace_get_uuid() returns a UUID");
2816 ok(memcmp(uuid
, ret_uuid
, 16) == 0,
2817 "bt_ctf_trace_get_uuid() returns the expected UUID");
2822 int main(int argc
, char **argv
)
2824 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2825 char metadata_path
[sizeof(trace_path
) + 9];
2826 const char *clock_name
= "test_clock";
2827 const char *clock_description
= "This is a test clock";
2828 const char *returned_clock_name
;
2829 const char *returned_clock_description
;
2830 const uint64_t frequency
= 1123456789;
2831 const int64_t offset_s
= 1351530929945824323;
2832 const int64_t offset
= 1234567;
2833 int64_t get_offset_s
,
2835 const uint64_t precision
= 10;
2836 const int is_absolute
= 0xFF;
2837 char *metadata_string
;
2838 struct bt_ctf_writer
*writer
;
2839 struct utsname name
;
2840 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2841 struct bt_ctf_clock
*clock
, *ret_clock
;
2842 struct bt_ctf_clock_class
*ret_clock_class
;
2843 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2844 struct bt_ctf_stream
*stream1
;
2845 struct bt_ctf_stream
*stream
;
2846 const char *ret_string
;
2847 const unsigned char *ret_uuid
;
2848 unsigned char tmp_uuid
[16] = { 0 };
2849 struct bt_ctf_field_type
*packet_context_type
,
2850 *packet_context_field_type
,
2851 *packet_header_type
,
2852 *packet_header_field_type
,
2854 *stream_event_context_type
,
2856 *event_header_field_type
;
2857 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2858 struct bt_ctf_trace
*trace
;
2860 int64_t ret_int64_t
;
2861 struct bt_value
*obj
;
2864 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
2868 plan_tests(NR_TESTS
);
2870 if (!bt_mkdtemp(trace_path
)) {
2874 strcpy(metadata_path
, trace_path
);
2875 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2877 writer
= bt_ctf_writer_create(trace_path
);
2878 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2880 ok(!bt_ctf_writer_get_trace(NULL
),
2881 "bt_ctf_writer_get_trace correctly handles NULL");
2882 trace
= bt_ctf_writer_get_trace(writer
);
2883 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
),
2884 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_NATIVE");
2885 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_UNSPECIFIED
),
2886 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_UNSPECIFIED");
2888 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2889 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2890 "Set a trace's byte order to big endian");
2891 ok(bt_ctf_trace_get_native_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2892 "bt_ctf_trace_get_native_byte_order returns a correct endianness");
2894 /* Add environment context to the trace */
2895 ret
= gethostname(hostname
, sizeof(hostname
));
2899 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2900 "Add host (%s) environment field to writer instance",
2902 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2904 "bt_ctf_writer_add_environment_field error with NULL writer");
2905 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2907 "bt_ctf_writer_add_environment_field error with NULL field name");
2908 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2910 "bt_ctf_writer_add_environment_field error with NULL field value");
2912 /* Test bt_ctf_trace_set_environment_field with an integer object */
2913 obj
= bt_value_integer_create_init(23);
2915 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2916 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2917 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2918 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2919 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2920 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2921 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2922 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2925 /* Test bt_ctf_trace_set_environment_field with a string object */
2926 obj
= bt_value_string_create_init("the value");
2928 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2929 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2932 /* Test bt_ctf_trace_set_environment_field_integer */
2933 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2935 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2936 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2937 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2938 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2940 "bt_ctf_trace_set_environment_field_integer succeeds");
2942 /* Test bt_ctf_trace_set_environment_field_string */
2943 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2945 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2946 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2947 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2948 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2950 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2951 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2953 "bt_ctf_trace_set_environment_field_string succeeds");
2955 /* Test bt_ctf_trace_get_environment_field_count */
2956 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2957 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2958 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2959 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2961 /* Test bt_ctf_trace_get_environment_field_name */
2962 ok(bt_ctf_trace_get_environment_field_name_by_index(NULL
, 0) == NULL
,
2963 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2964 ok(bt_ctf_trace_get_environment_field_name_by_index(trace
, 5) == NULL
,
2965 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2966 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 0);
2967 ok(ret_string
&& !strcmp(ret_string
, "host"),
2968 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2969 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 1);
2970 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2971 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2972 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 2);
2973 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2974 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2975 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 3);
2976 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2977 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2978 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 4);
2979 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2980 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2982 /* Test bt_ctf_trace_get_environment_field_value */
2983 ok(bt_ctf_trace_get_environment_field_value_by_index(NULL
, 0) == NULL
,
2984 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2985 ok(bt_ctf_trace_get_environment_field_value_by_index(trace
, 5) == NULL
,
2986 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2987 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 1);
2988 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2989 ok(!ret
&& ret_int64_t
== 23,
2990 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2992 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 2);
2993 ret
= bt_value_string_get(obj
, &ret_string
);
2994 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2995 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2998 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2999 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
3001 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3002 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3003 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3004 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3005 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3006 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3008 ret
= bt_value_string_get(obj
, &ret_string
);
3009 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3010 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3013 /* Test environment field replacement */
3014 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3016 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3017 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3018 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3019 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 3);
3020 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3021 ok(!ret
&& ret_int64_t
== 654321,
3022 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3025 /* On Solaris, uname() can return any positive value on success */
3026 if (uname(&name
) < 0) {
3031 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3032 == 0, "Add sysname (%s) environment field to writer instance",
3034 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3035 name
.nodename
) == 0,
3036 "Add nodename (%s) environment field to writer instance",
3038 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3039 == 0, "Add release (%s) environment field to writer instance",
3041 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3042 == 0, "Add version (%s) environment field to writer instance",
3044 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3045 == 0, "Add machine (%s) environment field to writer istance",
3048 /* Define a clock and add it to the trace */
3049 ok(bt_ctf_clock_create("signed") == NULL
,
3050 "Illegal clock name rejected");
3051 clock
= bt_ctf_clock_create(clock_name
);
3052 ok(clock
, "Clock created sucessfully");
3053 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3054 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3055 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3056 "Returned clock name is valid");
3058 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3059 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3060 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3061 "Clock description set successfully");
3063 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3064 ok(returned_clock_description
,
3065 "bt_ctf_clock_get_description returns a description.");
3066 ok(returned_clock_description
?
3067 !strcmp(returned_clock_description
, clock_description
) : 0,
3068 "Returned clock description is valid");
3070 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3071 "bt_ctf_clock_get_frequency returns the correct default frequency");
3072 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3073 "Set clock frequency");
3074 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3075 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3077 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3078 "bt_ctf_clock_get_offset_s succeeds");
3079 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3080 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3081 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3082 "Set clock offset (seconds)");
3083 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3084 "bt_ctf_clock_get_offset_s succeeds");
3085 ok(get_offset_s
== offset_s
,
3086 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3088 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3089 "bt_ctf_clock_get_offset succeeds");
3090 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3091 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3092 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3093 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3094 "bt_ctf_clock_get_offset succeeds");
3095 ok(get_offset
== offset
,
3096 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3098 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3099 "bt_ctf_clock_get_precision returns the correct default precision");
3100 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3101 "Set clock precision");
3102 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3103 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3105 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3106 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3107 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3108 "Set clock absolute property");
3109 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3110 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3112 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3115 ok(!bt_ctf_clock_get_name(NULL
),
3116 "bt_ctf_clock_get_name correctly handles NULL");
3117 ok(!bt_ctf_clock_get_description(NULL
),
3118 "bt_ctf_clock_get_description correctly handles NULL");
3119 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3120 "bt_ctf_clock_get_frequency correctly handles NULL");
3121 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3122 "bt_ctf_clock_get_precision correctly handles NULL");
3123 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3124 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3125 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3126 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3127 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3128 "bt_ctf_clock_get_offset correctly handles NULL clock");
3129 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3130 "bt_ctf_clock_get_offset correctly handles NULL output");
3131 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3132 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3134 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3135 "bt_ctf_clock_set_description correctly handles NULL clock");
3136 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3137 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3138 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3139 "bt_ctf_clock_get_precision correctly handles NULL clock");
3140 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3141 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3142 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3143 "bt_ctf_clock_set_offset correctly handles NULL clock");
3144 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3145 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3146 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3147 "bt_ctf_clock_set_time correctly handles NULL clock");
3148 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3149 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3150 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3152 "bt_ctf_clock_get_uuid returns a UUID");
3154 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3155 /* Slightly modify UUID */
3156 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3159 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3160 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3161 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3162 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3163 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3164 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3165 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3167 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3168 ok(uuid_match(ret_uuid
, tmp_uuid
),
3169 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3171 /* Define a stream class */
3172 stream_class
= bt_ctf_stream_class_create("test_stream");
3174 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3175 "bt_ctf_stream_class_get_name handles NULL correctly");
3176 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3177 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3178 "bt_ctf_stream_class_get_name returns a correct stream class name");
3180 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3181 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3182 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3183 "bt_ctf_stream_class_get_clock handles NULL correctly");
3185 ok(stream_class
, "Create stream class");
3186 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3187 "Set a stream class' clock");
3188 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3189 ok(ret_clock
== clock
,
3190 "bt_ctf_stream_class_get_clock returns a correct clock");
3193 /* Test the event fields and event types APIs */
3196 /* Test fields copying */
3199 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3200 "bt_ctf_stream_class_get_id returns an error when no id is set");
3201 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3202 "bt_ctf_stream_class_get_id handles NULL correctly");
3203 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3204 "bt_ctf_stream_class_set_id handles NULL correctly");
3205 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3206 "Set an stream class' id");
3207 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3208 "bt_ctf_stream_class_get_id returns the correct value");
3210 /* Validate default event header fields */
3211 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3212 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3213 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3216 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3217 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3218 "Default event header type is a structure");
3219 event_header_field_type
=
3220 bt_ctf_field_type_structure_get_field_type_by_name(
3221 ret_field_type
, "id");
3222 ok(event_header_field_type
,
3223 "Default event header type contains an \"id\" field");
3224 ok(bt_ctf_field_type_get_type_id(
3225 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3226 "Default event header \"id\" field is an integer");
3227 bt_put(event_header_field_type
);
3228 event_header_field_type
=
3229 bt_ctf_field_type_structure_get_field_type_by_name(
3230 ret_field_type
, "timestamp");
3231 ok(event_header_field_type
,
3232 "Default event header type contains a \"timestamp\" field");
3233 ok(bt_ctf_field_type_get_type_id(
3234 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3235 "Default event header \"timestamp\" field is an integer");
3236 bt_put(event_header_field_type
);
3237 bt_put(ret_field_type
);
3239 /* Add a custom trace packet header field */
3240 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3241 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3242 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3243 ok(packet_header_type
,
3244 "bt_ctf_trace_get_packet_header_type returns a packet header");
3245 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3246 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3247 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3248 packet_header_type
, "magic");
3249 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3250 bt_put(ret_field_type
);
3251 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3252 packet_header_type
, "uuid");
3253 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3254 bt_put(ret_field_type
);
3255 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3256 packet_header_type
, "stream_id");
3257 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3258 bt_put(ret_field_type
);
3260 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3261 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3262 packet_header_field_type
, "custom_trace_packet_header_field"),
3263 "Added a custom trace packet header field successfully");
3265 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3266 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3267 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3268 "Set a trace packet_header_type successfully");
3270 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3271 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3273 /* Add a custom field to the stream class' packet context */
3274 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3275 ok(packet_context_type
,
3276 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3277 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3278 "Packet context is a structure");
3280 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3281 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3283 integer_type
= bt_ctf_field_type_integer_create(32);
3284 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3286 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3287 /* Create a "uint5_t" equivalent custom packet context field */
3288 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3290 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3291 packet_context_field_type
, "custom_packet_context_field");
3292 ok(ret
== 0, "Packet context field added successfully");
3294 /* Define a stream event context containing a my_integer field. */
3295 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3296 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3297 stream_event_context_type
= bt_ctf_field_type_structure_create();
3298 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3299 integer_type
, "common_event_context");
3301 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3302 stream_event_context_type
) < 0,
3303 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3304 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3306 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3308 ok(bt_ctf_stream_class_set_event_context_type(
3309 stream_class
, stream_event_context_type
) == 0,
3310 "Set a new stream event context type");
3311 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3313 ok(ret_field_type
== stream_event_context_type
,
3314 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3315 bt_put(ret_field_type
);
3317 /* Instantiate a stream and append events */
3318 ret
= bt_ctf_writer_add_clock(writer
, clock
);
3320 ok(bt_ctf_trace_get_stream_count(trace
) == 0,
3321 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)");
3322 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3323 ok(stream1
, "Instanciate a stream class from writer");
3324 ok(bt_ctf_trace_get_stream_count(trace
) == 1,
3325 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)");
3326 stream
= bt_ctf_trace_get_stream_by_index(trace
, 0);
3327 ok(stream
== stream1
,
3328 "bt_ctf_trace_get_stream_by_index() succeeds and returns the correct value");
3332 * Creating a stream through a writer adds the given stream
3333 * class to the writer's trace, thus registering the stream
3334 * class's clock to the trace.
3336 ok(bt_ctf_trace_get_clock_class_count(NULL
) < 0,
3337 "bt_ctf_trace_get_clock_class_count correctly handles NULL");
3338 ok(bt_ctf_trace_get_clock_class_count(trace
) == 1,
3339 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
3340 ok(!bt_ctf_trace_get_clock_class_by_index(NULL
, 0),
3341 "bt_ctf_trace_get_clock_class correctly handles NULL");
3342 ok(!bt_ctf_trace_get_clock_class_by_index(trace
, 1),
3343 "bt_ctf_trace_get_clock_class correctly handles out of bound accesses");
3344 ret_clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
3345 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3346 bt_ctf_clock_get_name(clock
)) == 0,
3347 "bt_ctf_trace_get_clock_class returns the right clock instance");
3348 bt_put(ret_clock_class
);
3349 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, NULL
),
3350 "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)");
3351 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, clock_name
),
3352 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3353 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, NULL
),
3354 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3355 ret_clock_class
= bt_ctf_trace_get_clock_class_by_name(trace
, clock_name
);
3356 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3357 bt_ctf_clock_get_name(clock
)) == 0,
3358 "bt_ctf_trace_get_clock_class returns the right clock instance");
3359 bt_put(ret_clock_class
);
3360 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, "random"),
3361 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3363 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3364 "bt_ctf_stream_get_class correctly handles NULL");
3365 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3366 ok(ret_stream_class
,
3367 "bt_ctf_stream_get_class returns a stream class");
3368 ok(ret_stream_class
== stream_class
,
3369 "Returned stream class is of the correct type");
3372 * Packet header, packet context, event header, and stream
3373 * event context types were copied for the resolving
3376 BT_PUT(packet_header_type
);
3377 BT_PUT(packet_context_type
);
3378 BT_PUT(stream_event_context_type
);
3379 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3380 assert(packet_header_type
);
3381 packet_context_type
=
3382 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3383 assert(packet_context_type
);
3384 stream_event_context_type
=
3385 bt_ctf_stream_class_get_event_context_type(stream_class
);
3386 assert(stream_event_context_type
);
3389 * Try to modify the packet context type after a stream has been
3392 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3393 packet_header_field_type
, "should_fail");
3395 "Trace packet header type can't be modified once a stream has been instanciated");
3398 * Try to modify the packet context type after a stream has been
3401 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3402 packet_context_field_type
, "should_fail");
3404 "Packet context type can't be modified once a stream has been instanciated");
3407 * Try to modify the stream event context type after a stream has been
3410 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3411 integer_type
, "should_fail");
3413 "Stream event context type can't be modified once a stream has been instanciated");
3415 /* Should fail after instanciating a stream (frozen) */
3416 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3417 "Changes to a stream class that was already instantiated fail");
3419 /* Populate the custom packet header field only once for all tests */
3420 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3421 "bt_ctf_stream_get_packet_header handles NULL correctly");
3422 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3424 "bt_ctf_stream_get_packet_header returns a packet header");
3425 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3426 ok(ret_field_type
== packet_header_type
,
3427 "Stream returns a packet header of the appropriate type");
3428 bt_put(ret_field_type
);
3429 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3430 "custom_trace_packet_header_field");
3431 ok(packet_header_field
,
3432 "Packet header structure contains a custom field with the appropriate name");
3433 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3434 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3435 "Custom packet header field is of the expected type");
3436 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3437 54321), "Set custom packet header value successfully");
3438 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3439 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3440 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3441 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3442 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3443 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3444 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3445 "Successfully set a stream's packet header");
3447 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3448 "Add environment field to writer after stream creation");
3452 test_create_writer_vs_non_writer_mode();
3454 test_set_clock_non_writer_stream_class();
3456 test_instanciate_event_before_stream(writer
, clock
);
3458 append_simple_event(stream_class
, stream1
, clock
);
3460 packet_resize_test(stream_class
, stream1
, clock
);
3462 append_complex_event(stream_class
, stream1
, clock
);
3464 append_existing_event_class(stream_class
);
3466 test_empty_stream(writer
);
3468 test_custom_event_header_stream(writer
, clock
);
3470 test_static_trace();
3472 test_trace_is_static_listener();
3476 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3477 ok(metadata_string
, "Get metadata string");
3479 bt_ctf_writer_flush_metadata(writer
);
3482 bt_put(ret_stream_class
);
3485 bt_put(packet_context_type
);
3486 bt_put(packet_context_field_type
);
3487 bt_put(integer_type
);
3488 bt_put(stream_event_context_type
);
3489 bt_put(ret_field_type
);
3490 bt_put(packet_header_type
);
3491 bt_put(packet_header_field_type
);
3492 bt_put(packet_header
);
3493 bt_put(packet_header_field
);
3495 free(metadata_string
);
3496 bt_put(stream_class
);
3498 validate_trace(argv
[1], trace_path
);
3500 //recursive_rmdir(trace_path);