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>
45 #include <babeltrace/compat/dirent-internal.h>
51 #define METADATA_LINE_SIZE 512
52 #define SEQUENCE_TEST_LENGTH 10
53 #define ARRAY_TEST_LENGTH 5
54 #define PACKET_RESIZE_TEST_LENGTH 5
56 #define DEFAULT_CLOCK_FREQ 1000000000
57 #define DEFAULT_CLOCK_PRECISION 1
58 #define DEFAULT_CLOCK_OFFSET 0
59 #define DEFAULT_CLOCK_OFFSET_S 0
60 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
61 #define DEFAULT_CLOCK_TIME 0
62 #define DEFAULT_CLOCK_VALUE 0
66 static int64_t current_time
= 42;
68 /* Return 1 if uuids match, zero if different. */
70 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
75 if (!uuid_a
|| !uuid_b
) {
79 for (i
= 0; i
< 16; i
++) {
80 if (uuid_a
[i
] != uuid_b
[i
]) {
91 void validate_trace(char *parser_path
, char *trace_path
)
94 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
95 int babeltrace_output_fd
= -1;
102 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
103 unlink(babeltrace_output_path
);
105 if (babeltrace_output_fd
== -1) {
106 diag("Failed to create a temporary file for trace parsing.");
114 waitpid(pid
, &status
, 0);
115 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
117 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
119 perror("# dup2 babeltrace_output_fd to STDOUT");
123 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
125 perror("# dup2 babeltrace_output_fd to STDERR");
129 execl(parser_path
, "babeltrace", trace_path
, NULL
);
130 perror("# Could not launch the babeltrace process");
134 ok(ret
== 0, "Babeltrace could read the resulting trace");
136 if (ret
&& babeltrace_output_fd
>= 0) {
138 size_t len
= METADATA_LINE_SIZE
;
139 FILE *babeltrace_output_fp
= NULL
;
141 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
142 if (!babeltrace_output_fp
) {
143 perror("fdopen on babeltrace_output_fd");
146 babeltrace_output_fd
= -1;
150 diag("malloc error");
152 rewind(babeltrace_output_fp
);
153 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
159 if (babeltrace_output_fp
) {
160 if (fclose(babeltrace_output_fp
)) {
161 diag("fclose error");
166 if (babeltrace_output_fd
>= 0) {
167 if (close(babeltrace_output_fd
)) {
174 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
175 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
177 /* Create and add a simple event class */
178 struct bt_ctf_event_class
*simple_event_class
=
179 bt_ctf_event_class_create("Simple Event");
180 struct bt_ctf_field_type
*uint_12_type
=
181 bt_ctf_field_type_integer_create(12);
182 struct bt_ctf_field_type
*int_64_type
=
183 bt_ctf_field_type_integer_create(64);
184 struct bt_ctf_field_type
*float_type
=
185 bt_ctf_field_type_floating_point_create();
186 struct bt_ctf_field_type
*enum_type
;
187 struct bt_ctf_field_type
*enum_type_unsigned
=
188 bt_ctf_field_type_enumeration_create(uint_12_type
);
189 struct bt_ctf_field_type
*event_context_type
=
190 bt_ctf_field_type_structure_create();
191 struct bt_ctf_field_type
*event_payload_type
= NULL
;
192 struct bt_ctf_field_type
*returned_type
;
193 struct bt_ctf_event
*simple_event
;
194 struct bt_ctf_field
*integer_field
;
195 struct bt_ctf_field
*float_field
;
196 struct bt_ctf_field
*enum_field
;
197 struct bt_ctf_field
*enum_field_unsigned
;
198 struct bt_ctf_field
*enum_container_field
;
199 const char *mapping_name_test
= "truie";
200 const double double_test_value
= 3.1415;
201 struct bt_ctf_field
*enum_container_field_unsigned
;
202 const char *mapping_name_negative_test
= "negative_value";
203 const char *ret_char
;
205 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
206 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
207 struct bt_ctf_event_class
*ret_event_class
;
208 struct bt_ctf_field
*packet_context
;
209 struct bt_ctf_field
*packet_context_field
;
210 struct bt_ctf_field
*stream_event_context
;
211 struct bt_ctf_field
*stream_event_context_field
;
212 struct bt_ctf_field
*event_context
;
213 struct bt_ctf_field
*event_context_field
;
214 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
215 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
216 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
217 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
220 ok(uint_12_type
, "Create an unsigned integer type");
222 ok(!bt_ctf_field_type_integer_set_signed(int_64_type
, 1),
223 "Set signed 64 bit integer signedness to true");
224 ok(int_64_type
, "Create a signed integer type");
225 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
227 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
228 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
229 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
230 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
231 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
232 bt_put(returned_type
);
234 bt_ctf_field_type_set_alignment(float_type
, 32);
235 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
236 "bt_ctf_field_type_get_alignment handles NULL correctly");
237 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
238 "bt_ctf_field_type_get_alignment returns a correct value");
240 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
241 "Set a floating point type's exponent digit count");
242 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
243 "Set a floating point type's mantissa digit count");
245 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
246 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
247 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
248 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
249 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
250 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
251 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
252 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
254 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
255 mapping_name_negative_test
, -12345, 0) == 0,
256 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
257 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
258 "escaping; \"test\"", 1, 1) == 0,
259 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
260 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
261 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
262 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
263 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
264 "event clock int float", 5, 22) == 0,
265 "Accept enumeration mapping strings containing reserved keywords");
266 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
268 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
269 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
270 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
271 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
272 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
273 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
274 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
276 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(NULL
, -42);
277 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly");
279 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, -4200000);
280 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values");
282 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, 3);
283 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value");
284 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, NULL
, NULL
, NULL
) == 0,
285 "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly");
288 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
289 "enum_field") == 0, "Add signed enumeration field to event");
291 ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL
, 0, &ret_char
,
292 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
293 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly");
294 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, NULL
,
295 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
296 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly");
297 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
298 NULL
, &ret_range_end_int64_t
) == 0,
299 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly");
300 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
301 &ret_range_start_int64_t
, NULL
) == 0,
302 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly");
303 /* Assumes entries are sorted by range_start values. */
304 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 6, &ret_char
,
305 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
306 "bt_ctf_field_type_enumeration_get_mapping_signed returns a value");
307 ok(!strcmp(ret_char
, mapping_name_test
),
308 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name");
309 ok(ret_range_start_int64_t
== 42,
310 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start");
311 ok(ret_range_end_int64_t
== 42,
312 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end");
314 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
315 "escaping; \"test\"", 0, 0) == 0,
316 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
317 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
318 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
319 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
320 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
321 "event clock int float", 5, 22) == 0,
322 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
323 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
325 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
326 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names");
327 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
328 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries");
329 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
330 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
331 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
332 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
334 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
335 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
336 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 6,
337 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
339 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
340 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
341 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
342 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
343 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
344 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
345 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
346 NULL
, &ret_range_end_uint64_t
) == 0,
347 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
348 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
349 &ret_range_start_uint64_t
, NULL
) == 0,
350 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
351 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 4, &ret_char
,
352 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
353 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
354 ok(!strcmp(ret_char
, mapping_name_test
),
355 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
356 ok(ret_range_start_uint64_t
== 42,
357 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
358 ok(ret_range_end_uint64_t
== 42,
359 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
361 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
363 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
366 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
368 /* Set an event context type which will contain a single integer. */
369 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
370 "event_specific_context"),
371 "Add event specific context field");
372 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
373 "bt_ctf_event_class_get_context_type handles NULL correctly");
375 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
376 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
377 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
378 "Set an event class' context type successfully");
379 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
380 ok(returned_type
== event_context_type
,
381 "bt_ctf_event_class_get_context_type returns the appropriate type");
382 bt_put(returned_type
);
384 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
385 "Adding simple event class to stream class");
388 * bt_ctf_stream_class_add_event_class() copies the field types
389 * of simple_event_class, so we retrieve the new ones to create
390 * the appropriate fields.
392 BT_PUT(event_context_type
);
393 BT_PUT(event_payload_type
);
394 event_payload_type
= bt_ctf_event_class_get_payload_type(
396 assert(event_payload_type
);
397 event_context_type
= bt_ctf_event_class_get_context_type(
399 assert(event_context_type
);
400 ep_integer_field_type
=
401 bt_ctf_field_type_structure_get_field_type_by_name(
402 event_payload_type
, "integer_field");
403 assert(ep_integer_field_type
);
405 bt_ctf_field_type_structure_get_field_type_by_name(
406 event_payload_type
, "enum_field");
407 assert(ep_enum_field_type
);
408 ep_enum_field_unsigned_type
=
409 bt_ctf_field_type_structure_get_field_type_by_name(
410 event_payload_type
, "enum_field_unsigned");
411 assert(ep_enum_field_unsigned_type
);
413 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
414 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
415 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
416 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
417 ok(bt_ctf_stream_class_get_event_class_by_index(NULL
, 0) == NULL
,
418 "bt_ctf_stream_class_get_event_class handles NULL correctly");
419 ok(bt_ctf_stream_class_get_event_class_by_index(stream_class
, 8724) == NULL
,
420 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
421 ret_event_class
= bt_ctf_stream_class_get_event_class_by_index(stream_class
, 0);
422 ok(ret_event_class
== simple_event_class
,
423 "bt_ctf_stream_class_get_event_class returns the correct event class");
424 bt_put(ret_event_class
);
425 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
426 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
427 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
428 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
430 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
431 ok(ret_event_class
== simple_event_class
,
432 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
433 bt_put(ret_event_class
);
435 simple_event
= bt_ctf_event_create(simple_event_class
);
437 "Instantiate an event containing a single integer field");
439 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
440 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
441 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
442 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
444 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
445 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
446 "bt_ctf_field_floating_point_get_value fails on an unset float field");
447 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
448 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
449 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
450 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
451 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
452 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
453 "bt_ctf_field_floating_point_get_value returns a double value");
454 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
455 "bt_ctf_field_floating_point_get_value returns a correct value");
457 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
460 iter
= bt_ctf_field_enumeration_get_mappings(NULL
);
461 ok(!iter
, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
462 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
463 ok(!iter
, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
464 enum_container_field
= bt_ctf_field_enumeration_get_container(
466 ok(bt_ctf_field_signed_integer_set_value(
467 enum_container_field
, -42) == 0,
468 "Set signed enumeration container value");
469 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
470 ok(iter
, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
471 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, &ret_char
, NULL
, NULL
);
472 ok(!ret
&& ret_char
, "bt_ctf_field_type_enumeration_mapping_iterator_get_signed return a mapping name");
474 ok(!strcmp(ret_char
, mapping_name_negative_test
),
475 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
476 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
480 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
481 assert(enum_field_unsigned
);
482 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
483 enum_field_unsigned
);
484 ok(bt_ctf_field_unsigned_integer_set_value(
485 enum_container_field_unsigned
, 42) == 0,
486 "Set unsigned enumeration container value");
487 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
488 enum_field_unsigned
);
490 iter
= bt_ctf_field_enumeration_get_mappings(enum_field_unsigned
);
492 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, &ret_char
, NULL
, NULL
);
493 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
494 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container");
496 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
498 /* Populate stream event context */
499 stream_event_context
=
500 bt_ctf_event_get_stream_event_context(simple_event
);
501 assert(stream_event_context
);
502 stream_event_context_field
= bt_ctf_field_structure_get_field(
503 stream_event_context
, "common_event_context");
504 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
506 /* Populate the event's context */
507 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
508 "bt_ctf_event_get_event_context handles NULL correctly");
509 event_context
= bt_ctf_event_get_event_context(simple_event
);
511 "bt_ctf_event_get_event_context returns a field");
512 returned_type
= bt_ctf_field_get_type(event_context
);
513 ok(returned_type
== event_context_type
,
514 "bt_ctf_event_get_event_context returns a field of the appropriate type");
515 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
516 "event_specific_context");
517 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
518 "Successfully set an event context's value");
519 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
520 "bt_ctf_event_set_event_context handles a NULL event correctly");
521 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
522 "bt_ctf_event_set_event_context handles a NULL event context correctly");
523 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
524 "bt_ctf_event_set_event_context rejects a context of the wrong type");
525 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
526 "Set an event context successfully");
528 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
529 "Append simple event to trace stream");
531 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
532 "bt_ctf_stream_get_packet_context handles NULL correctly");
533 packet_context
= bt_ctf_stream_get_packet_context(stream
);
535 "bt_ctf_stream_get_packet_context returns a packet context");
537 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
539 ok(packet_context_field
,
540 "Packet context contains the default packet_size field.");
541 bt_put(packet_context_field
);
542 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
543 "custom_packet_context_field");
544 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
545 "Custom packet context field value successfully set.");
547 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
548 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
549 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
550 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
551 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
552 "Successfully set a stream's packet context");
554 ok(bt_ctf_stream_flush(stream
) == 0,
555 "Flush trace stream with one event");
557 bt_put(simple_event_class
);
558 bt_put(simple_event
);
559 bt_put(uint_12_type
);
563 bt_put(enum_type_unsigned
);
564 bt_put(returned_type
);
565 bt_put(event_context_type
);
566 bt_put(integer_field
);
569 bt_put(enum_field_unsigned
);
570 bt_put(enum_container_field
);
571 bt_put(enum_container_field_unsigned
);
572 bt_put(packet_context
);
573 bt_put(packet_context_field
);
574 bt_put(stream_event_context
);
575 bt_put(stream_event_context_field
);
576 bt_put(event_context
);
577 bt_put(event_context_field
);
578 bt_put(event_payload_type
);
579 bt_put(ep_integer_field_type
);
580 bt_put(ep_enum_field_type
);
581 bt_put(ep_enum_field_unsigned_type
);
586 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
587 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
590 struct event_class_attrs_counts
;
591 const char *complex_test_event_string
= "Complex Test Event";
592 const char *test_string_1
= "Test ";
593 const char *test_string_2
= "string ";
594 const char *test_string_3
= "abcdefghi";
595 const char *test_string_4
= "abcd\0efg\0hi";
596 const char *test_string_cat
= "Test string abcdeabcd";
597 struct bt_ctf_field_type
*uint_35_type
=
598 bt_ctf_field_type_integer_create(35);
599 struct bt_ctf_field_type
*int_16_type
=
600 bt_ctf_field_type_integer_create(16);
601 struct bt_ctf_field_type
*uint_3_type
=
602 bt_ctf_field_type_integer_create(3);
603 struct bt_ctf_field_type
*enum_variant_type
=
604 bt_ctf_field_type_enumeration_create(uint_3_type
);
605 struct bt_ctf_field_type
*variant_type
=
606 bt_ctf_field_type_variant_create(enum_variant_type
,
608 struct bt_ctf_field_type
*string_type
=
609 bt_ctf_field_type_string_create();
610 struct bt_ctf_field_type
*sequence_type
;
611 struct bt_ctf_field_type
*array_type
;
612 struct bt_ctf_field_type
*inner_structure_type
=
613 bt_ctf_field_type_structure_create();
614 struct bt_ctf_field_type
*complex_structure_type
=
615 bt_ctf_field_type_structure_create();
616 struct bt_ctf_field_type
*ret_field_type
;
617 struct bt_ctf_event_class
*event_class
;
618 struct bt_ctf_event
*event
;
619 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
620 *inner_structure_field
, *complex_structure_field
,
621 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
622 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
623 *stream_event_ctx_int_field
, *ret_field
;
624 uint64_t ret_unsigned_int
;
625 int64_t ret_signed_int
;
626 const char *ret_string
;
627 struct bt_ctf_stream_class
*ret_stream_class
;
628 struct bt_ctf_event_class
*ret_event_class
;
629 struct bt_ctf_field
*packet_context
, *packet_context_field
;
630 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
632 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
633 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
634 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
635 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
636 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
637 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
638 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
639 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
640 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
641 "Set alignment of signed 16 bit integer to 32");
642 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
643 "Set integer signedness to true");
644 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
645 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
646 "Set signed 16 bit integer base to hexadecimal");
648 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
649 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
652 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
653 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
654 ret_field_type
= bt_ctf_field_type_array_get_element_type(
656 ok(ret_field_type
== int_16_type
,
657 "bt_ctf_field_type_array_get_element_type returns the correct type");
658 bt_put(ret_field_type
);
660 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
661 "bt_ctf_field_type_array_get_length handles NULL correctly");
662 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
663 "bt_ctf_field_type_array_get_length returns the correct length");
665 ok(bt_ctf_field_type_structure_add_field(inner_structure_type
,
666 inner_structure_type
, "yes"), "Cannot add self to structure");
667 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
668 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
669 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
670 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
671 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
672 array_type
, "an_array"), "Add an_array field to inner structure");
674 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
676 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
678 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
679 "UINT35_TYPE", 2, 7);
681 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(NULL
, "INT16_TYPE");
682 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly");
684 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, "INT16_TYPE");
685 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly");
686 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
687 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly");
690 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, NULL
);
691 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly");
693 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL
, 1);
694 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly");
696 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, -42);
697 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly");
698 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) != 0,
699 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly");
702 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, 5);
703 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly");
704 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
705 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly");
708 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
709 "An unknown entry"), "Reject a variant field based on an unknown tag value");
710 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
711 "UINT3_TYPE") == 0, "Add a field to a variant");
712 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
713 "INT16_TYPE"), "Add INT16_TYPE field to variant");
714 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
715 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
717 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
718 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
719 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
720 ok(ret_field_type
== enum_variant_type
,
721 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
722 bt_put(ret_field_type
);
724 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
725 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
726 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
727 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
728 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
729 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
730 "INT16_TYPE") == NULL
,
731 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
732 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
734 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
735 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
736 variant_type
, "INT16_TYPE");
737 ok(ret_field_type
== int_16_type
,
738 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
739 bt_put(ret_field_type
);
741 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
742 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
743 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
744 "bt_ctf_field_type_variant_get_field_count returns the correct count");
746 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
747 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
748 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
749 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
750 bt_put(ret_field_type
);
751 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
752 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
753 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
754 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
755 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
756 "bt_ctf_field_type_variant_get_field returns a field");
757 ok(!strcmp("INT16_TYPE", ret_string
),
758 "bt_ctf_field_type_variant_get_field returns a correct field name");
759 ok(ret_field_type
== int_16_type
,
760 "bt_ctf_field_type_variant_get_field returns a correct field type");
761 bt_put(ret_field_type
);
763 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
764 enum_variant_type
, "variant_selector"),
765 "Add variant_selector field to complex structure");
766 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
767 string_type
, "a_string"), "Add a_string field to complex structure");
768 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
769 variant_type
, "variant_value"),
770 "Add variant_value field to complex structure");
771 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
772 inner_structure_type
, "inner_structure"),
773 "Add inner_structure field to complex structure");
775 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
776 ok(event_class
, "Create an event class");
777 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
778 "Reject addition of a field with an empty name to an event");
779 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
780 "Reject addition of a field with a NULL type to an event");
781 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
783 "Reject addition of a type with an illegal name to an event");
784 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
786 "Add field of type unsigned integer to an event");
787 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
788 "int_16") == 0, "Add field of type signed integer to an event");
789 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
790 "complex_structure") == 0,
791 "Add composite structure to an event");
793 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
794 "bt_ctf_event_class_get_name handles NULL correctly");
795 ret_string
= bt_ctf_event_class_get_name(event_class
);
796 ok(!strcmp(ret_string
, complex_test_event_string
),
797 "bt_ctf_event_class_get_name returns a correct name");
798 ok(bt_ctf_event_class_get_id(event_class
) < 0,
799 "bt_ctf_event_class_get_id returns a negative value when not set");
800 ok(bt_ctf_event_class_get_id(NULL
) < 0,
801 "bt_ctf_event_class_get_id handles NULL correctly");
802 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
803 "bt_ctf_event_class_set_id handles NULL correctly");
804 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
805 "Set an event class' id");
806 ok(bt_ctf_event_class_get_id(event_class
) == 42,
807 "bt_ctf_event_class_get_id returns the correct value");
809 /* Test event class attributes */
810 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
,
811 "event class has the expected initial log level");
812 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
813 "as expected, event class has no initial EMF URI");
814 ok(bt_ctf_event_class_set_log_level(NULL
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
815 "bt_ctf_event_class_set_log_level handles a NULL event class correctly");
816 ok(bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
),
817 "bt_ctf_event_class_set_log_level handles an unknown log level correctly");
818 ok(!bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
819 "bt_ctf_event_class_set_log_level succeeds with a valid log level");
820 ok(bt_ctf_event_class_get_log_level(NULL
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
,
821 "bt_ctf_event_class_get_log_level handles a NULL event class correctly");
822 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
,
823 "bt_ctf_event_class_get_log_level returns the expected log level");
824 ok(bt_ctf_event_class_set_emf_uri(NULL
, "http://diamon.org/babeltrace/"),
825 "bt_ctf_event_class_set_emf_uri handles a NULL event class correctly");
826 ok(!bt_ctf_event_class_set_emf_uri(event_class
, "http://diamon.org/babeltrace/"),
827 "bt_ctf_event_class_set_emf_uri succeeds with a valid EMF URI");
828 ok(!bt_ctf_event_class_get_emf_uri(NULL
),
829 "bt_ctf_event_class_get_emf_uri handles a NULL event class correctly");
830 ok(strcmp(bt_ctf_event_class_get_emf_uri(event_class
), "http://diamon.org/babeltrace/") == 0,
831 "bt_ctf_event_class_get_emf_uri returns the expected EMF URI");
832 ok(!bt_ctf_event_class_set_emf_uri(event_class
, NULL
),
833 "bt_ctf_event_class_set_emf_uri succeeds with NULL (to reset)");
834 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
835 "as expected, event class has no EMF URI after reset");
837 /* Add event class to the stream class */
838 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
839 "Reject addition of NULL event class to a stream class");
840 ok(bt_ctf_stream_class_add_event_class(stream_class
,
841 event_class
) == 0, "Add an event class to stream class");
843 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
844 "bt_ctf_event_class_get_stream_class handles NULL correctly");
845 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
846 ok(ret_stream_class
== stream_class
,
847 "bt_ctf_event_class_get_stream_class returns the correct stream class");
848 bt_put(ret_stream_class
);
850 ok(bt_ctf_event_class_get_payload_type_field_count(NULL
) < 0,
851 "bt_ctf_event_class_get_field_count handles NULL correctly");
852 ok(bt_ctf_event_class_get_payload_type_field_count(event_class
) == 3,
853 "bt_ctf_event_class_get_field_count returns a correct value");
855 ok(bt_ctf_event_class_get_payload_type_field_by_index(NULL
, &ret_string
,
856 &ret_field_type
, 0) < 0,
857 "bt_ctf_event_class_get_field handles a NULL event class correctly");
858 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, NULL
,
859 &ret_field_type
, 0) == 0,
860 "bt_ctf_event_class_get_field handles a NULL field name correctly");
861 bt_put(ret_field_type
);
862 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
864 "bt_ctf_event_class_get_field handles a NULL field type correctly");
865 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
866 &ret_field_type
, 42) < 0,
867 "bt_ctf_event_class_get_field handles an invalid index correctly");
868 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
869 &ret_field_type
, 0) == 0,
870 "bt_ctf_event_class_get_field returns a field");
871 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
872 "bt_ctf_event_class_get_field returns a correct field type");
873 bt_put(ret_field_type
);
874 ok(!strcmp(ret_string
, "uint_35"),
875 "bt_ctf_event_class_get_field returns a correct field name");
876 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
877 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
878 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
879 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
880 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
881 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
882 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
883 "complex_structure");
884 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
885 "bt_ctf_event_class_get_field_by_name returns a correct field type");
886 bt_put(ret_field_type
);
888 event
= bt_ctf_event_create(event_class
);
889 ok(event
, "Instanciate a complex event");
891 ok(bt_ctf_event_get_class(NULL
) == NULL
,
892 "bt_ctf_event_get_class handles NULL correctly");
893 ret_event_class
= bt_ctf_event_get_class(event
);
894 ok(ret_event_class
== event_class
,
895 "bt_ctf_event_get_class returns the correct event class");
896 bt_put(ret_event_class
);
898 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
899 if (!uint_35_field
) {
900 printf("uint_35_field is NULL\n");
903 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
904 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
905 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
906 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
907 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
908 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
909 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
910 &ret_unsigned_int
) == 0,
911 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
912 ok(ret_unsigned_int
== 0x0DDF00D,
913 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
914 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
915 &ret_signed_int
) < 0,
916 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
917 bt_put(uint_35_field
);
919 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
920 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
921 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
922 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
923 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
924 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
925 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
926 &ret_signed_int
) == 0,
927 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
928 ok(ret_signed_int
== -12345,
929 "bt_ctf_field_signed_integer_get_value returns the correct value");
930 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
931 &ret_unsigned_int
) < 0,
932 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
933 bt_put(int_16_field
);
935 complex_structure_field
= bt_ctf_event_get_payload(event
,
936 "complex_structure");
938 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
939 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
940 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
941 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
942 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
943 complex_structure_field
, 3);
944 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
945 bt_put(inner_structure_field
);
946 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
947 "bt_ctf_field_structure_get_field_by_index returns a correct field");
948 bt_put(ret_field_type
);
950 inner_structure_field
= bt_ctf_field_structure_get_field(
951 complex_structure_field
, "inner_structure");
952 a_string_field
= bt_ctf_field_structure_get_field(
953 complex_structure_field
, "a_string");
954 enum_variant_field
= bt_ctf_field_structure_get_field(
955 complex_structure_field
, "variant_selector");
956 variant_field
= bt_ctf_field_structure_get_field(
957 complex_structure_field
, "variant_value");
958 uint_35_field
= bt_ctf_field_structure_get_field(
959 inner_structure_field
, "seq_len");
960 a_sequence_field
= bt_ctf_field_structure_get_field(
961 inner_structure_field
, "a_sequence");
962 an_array_field
= bt_ctf_field_structure_get_field(
963 inner_structure_field
, "an_array");
965 enum_container_field
= bt_ctf_field_enumeration_get_container(
967 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
968 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
970 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
971 bt_put(int_16_field
);
972 ok(!bt_ctf_field_string_get_value(a_string_field
),
973 "bt_ctf_field_string_get_value returns NULL on an unset field");
974 bt_ctf_field_string_set_value(a_string_field
,
976 ok(!bt_ctf_field_string_get_value(NULL
),
977 "bt_ctf_field_string_get_value correctly handles NULL");
978 ok(bt_ctf_field_string_append(NULL
, "yeah"),
979 "bt_ctf_field_string_append correctly handles a NULL string field");
980 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
981 "bt_ctf_field_string_append correctly handles a NULL string value");
982 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
983 "bt_ctf_field_string_append succeeds");
984 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
985 "bt_ctf_field_string_append_len correctly handles a NULL string field");
986 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
987 "bt_ctf_field_string_append_len correctly handles a NULL string value");
988 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
989 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
990 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
991 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
992 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
993 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
994 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
995 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
997 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
998 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
999 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1000 "bt_ctf_field_string_get_value returns a correct value");
1001 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1002 SEQUENCE_TEST_LENGTH
);
1004 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1005 enum_container_field
) == NULL
,
1006 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1007 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1009 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1010 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1011 variant_type
, enum_variant_field
);
1012 ok(ret_field_type
== int_16_type
,
1013 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1015 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1016 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1017 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1018 uint_35_field
) == 0, "Set a sequence field's length");
1019 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1020 ok(ret_field
== uint_35_field
,
1021 "bt_ctf_field_sequence_get_length returns the correct length field");
1022 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1023 "bt_ctf_field_sequence_get_length properly handles NULL");
1025 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1026 int_16_field
= bt_ctf_field_sequence_get_field(
1027 a_sequence_field
, i
);
1028 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1029 bt_put(int_16_field
);
1032 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1033 int_16_field
= bt_ctf_field_array_get_field(
1035 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1036 bt_put(int_16_field
);
1039 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1040 assert(stream_event_ctx_field
);
1041 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1042 stream_event_ctx_field
, "common_event_context");
1043 BT_PUT(stream_event_ctx_field
);
1044 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1045 BT_PUT(stream_event_ctx_int_field
);
1047 bt_ctf_clock_set_time(clock
, ++current_time
);
1048 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1049 "Append a complex event to a stream");
1052 * Populate the custom packet context field with a dummy value
1053 * otherwise flush will fail.
1055 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1056 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1057 "custom_packet_context_field");
1058 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1060 ok(bt_ctf_stream_flush(stream
) == 0,
1061 "Flush a stream containing a complex event");
1063 bt_put(uint_35_field
);
1064 bt_put(a_string_field
);
1065 bt_put(inner_structure_field
);
1066 bt_put(complex_structure_field
);
1067 bt_put(a_sequence_field
);
1068 bt_put(an_array_field
);
1069 bt_put(enum_variant_field
);
1070 bt_put(enum_container_field
);
1071 bt_put(variant_field
);
1073 bt_put(packet_context_field
);
1074 bt_put(packet_context
);
1075 bt_put(uint_35_type
);
1076 bt_put(int_16_type
);
1077 bt_put(string_type
);
1078 bt_put(sequence_type
);
1080 bt_put(inner_structure_type
);
1081 bt_put(complex_structure_type
);
1082 bt_put(uint_3_type
);
1083 bt_put(enum_variant_type
);
1084 bt_put(variant_type
);
1085 bt_put(ret_field_type
);
1086 bt_put(event_class
);
1091 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1092 struct bt_ctf_field_type
*expected_type
, const char *name
)
1094 struct bt_ctf_field_type
*copy_type
;
1096 copy_type
= bt_ctf_field_get_type(field
);
1097 ok(copy_type
== expected_type
,
1098 "bt_ctf_field_copy does not copy the type (%s)", name
);
1103 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1104 struct bt_ctf_field
*field_b
, const char *name
)
1106 ok(field_a
!= field_b
,
1107 "bt_ctf_field_copy creates different pointers (%s)", name
);
1111 void field_copy_tests()
1113 struct bt_ctf_field_type
*len_type
= NULL
;
1114 struct bt_ctf_field_type
*fp_type
= NULL
;
1115 struct bt_ctf_field_type
*s_type
= NULL
;
1116 struct bt_ctf_field_type
*e_int_type
= NULL
;
1117 struct bt_ctf_field_type
*e_type
= NULL
;
1118 struct bt_ctf_field_type
*v_type
= NULL
;
1119 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1120 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1121 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1122 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1123 struct bt_ctf_field_type
*strct_type
= NULL
;
1124 struct bt_ctf_field
*len
= NULL
;
1125 struct bt_ctf_field
*fp
= NULL
;
1126 struct bt_ctf_field
*s
= NULL
;
1127 struct bt_ctf_field
*e_int
= NULL
;
1128 struct bt_ctf_field
*e
= NULL
;
1129 struct bt_ctf_field
*v
= NULL
;
1130 struct bt_ctf_field
*v_selected
= NULL
;
1131 struct bt_ctf_field
*v_selected_cur
= NULL
;
1132 struct bt_ctf_field
*v_selected_0
= NULL
;
1133 struct bt_ctf_field
*v_selected_1
= NULL
;
1134 struct bt_ctf_field
*v_selected_2
= NULL
;
1135 struct bt_ctf_field
*v_selected_3
= NULL
;
1136 struct bt_ctf_field
*v_selected_4
= NULL
;
1137 struct bt_ctf_field
*v_selected_5
= NULL
;
1138 struct bt_ctf_field
*v_selected_6
= NULL
;
1139 struct bt_ctf_field
*a
= NULL
;
1140 struct bt_ctf_field
*a_0
= NULL
;
1141 struct bt_ctf_field
*a_1
= NULL
;
1142 struct bt_ctf_field
*a_2
= NULL
;
1143 struct bt_ctf_field
*a_3
= NULL
;
1144 struct bt_ctf_field
*a_4
= NULL
;
1145 struct bt_ctf_field
*strct
= NULL
;
1146 struct bt_ctf_field
*len_copy
= NULL
;
1147 struct bt_ctf_field
*fp_copy
= NULL
;
1148 struct bt_ctf_field
*s_copy
= NULL
;
1149 struct bt_ctf_field
*e_int_copy
= NULL
;
1150 struct bt_ctf_field
*e_copy
= NULL
;
1151 struct bt_ctf_field
*v_copy
= NULL
;
1152 struct bt_ctf_field
*v_selected_copy
= NULL
;
1153 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1154 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1155 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1156 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1157 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1158 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1159 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1160 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1161 struct bt_ctf_field
*a_copy
= NULL
;
1162 struct bt_ctf_field
*a_0_copy
= NULL
;
1163 struct bt_ctf_field
*a_1_copy
= NULL
;
1164 struct bt_ctf_field
*a_2_copy
= NULL
;
1165 struct bt_ctf_field
*a_3_copy
= NULL
;
1166 struct bt_ctf_field
*a_4_copy
= NULL
;
1167 struct bt_ctf_field
*strct_copy
= NULL
;
1168 struct bt_ctf_field_type_enumeration_mapping_iterator
*e_iter
= NULL
;
1169 uint64_t uint64_t_val
;
1170 const char *str_val
;
1174 /* create len type */
1175 len_type
= bt_ctf_field_type_integer_create(32);
1178 /* create fp type */
1179 fp_type
= bt_ctf_field_type_floating_point_create();
1183 s_type
= bt_ctf_field_type_string_create();
1186 /* create e_int type */
1187 e_int_type
= bt_ctf_field_type_integer_create(8);
1191 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1193 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1196 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1200 /* create v_label1 type */
1201 v_label1_type
= bt_ctf_field_type_string_create();
1202 assert(v_label1_type
);
1204 /* create v_label1_array type */
1205 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1206 assert(v_label1_array_type
);
1208 /* create v_label2 type */
1209 v_label2_type
= bt_ctf_field_type_integer_create(16);
1210 assert(v_label2_type
);
1212 /* create v_label2_seq type */
1213 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1215 assert(v_label2_seq_type
);
1218 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1220 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1223 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1227 /* create strct type */
1228 strct_type
= bt_ctf_field_type_structure_create();
1230 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1233 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1235 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1237 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1239 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1241 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1242 v_label1_array_type
, "a");
1246 strct
= bt_ctf_field_create(strct_type
);
1250 len
= bt_ctf_field_structure_get_field(strct
, "len");
1254 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1258 s
= bt_ctf_field_structure_get_field(strct
, "s");
1262 e
= bt_ctf_field_structure_get_field(strct
, "e");
1265 /* get e_int (underlying integer) */
1266 e_int
= bt_ctf_field_enumeration_get_container(e
);
1270 v
= bt_ctf_field_structure_get_field(strct
, "v");
1274 a
= bt_ctf_field_structure_get_field(strct
, "a");
1278 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1282 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1286 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1289 /* set e field (LABEL2) */
1290 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1294 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1296 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1297 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1298 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1299 ok(v_selected_cur
== v_selected
,
1300 "bt_ctf_field_variant_get_current_field returns the current field");
1301 bt_put(v_selected_cur
);
1303 /* set selected v field */
1304 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1306 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1307 assert(v_selected_0
);
1308 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1310 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1311 assert(v_selected_1
);
1312 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1314 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1315 assert(v_selected_2
);
1316 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1318 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1319 assert(v_selected_3
);
1320 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1322 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1323 assert(v_selected_4
);
1324 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1326 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1327 assert(v_selected_5
);
1328 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1330 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1331 assert(v_selected_6
);
1332 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1336 a_0
= bt_ctf_field_array_get_field(a
, 0);
1338 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1340 a_1
= bt_ctf_field_array_get_field(a
, 1);
1342 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1344 a_2
= bt_ctf_field_array_get_field(a
, 2);
1346 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1348 a_3
= bt_ctf_field_array_get_field(a
, 3);
1350 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1352 a_4
= bt_ctf_field_array_get_field(a
, 4);
1354 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1357 /* create copy of strct */
1358 ok(!bt_ctf_field_copy(NULL
),
1359 "bt_ctf_field_copy handles NULL correctly");
1360 strct_copy
= bt_ctf_field_copy(strct
);
1362 "bt_ctf_field_copy returns a valid pointer");
1364 /* get all copied fields */
1365 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1367 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1369 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1371 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1373 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1375 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1377 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1378 assert(v_selected_copy
);
1379 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1380 assert(v_selected_0_copy
);
1381 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1382 assert(v_selected_1_copy
);
1383 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1384 assert(v_selected_2_copy
);
1385 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1386 assert(v_selected_3_copy
);
1387 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1388 assert(v_selected_4_copy
);
1389 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1390 assert(v_selected_5_copy
);
1391 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1392 assert(v_selected_6_copy
);
1393 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1394 "sequence field copy is not too large");
1395 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1397 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1399 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1401 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1403 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1405 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1407 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1408 "array field copy is not too large");
1410 /* make sure copied fields are different pointers */
1411 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1412 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1413 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1414 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1415 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1416 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1417 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1418 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1420 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1422 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1424 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1426 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1428 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1430 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1432 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1434 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1435 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1436 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1437 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1438 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1439 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1441 /* make sure copied fields share the same types */
1442 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1443 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1444 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1445 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1446 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1447 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1448 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1450 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1452 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1454 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1456 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1458 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1460 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1462 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1464 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1465 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1466 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1467 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1468 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1469 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1471 /* validate len copy */
1472 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1474 ok(uint64_t_val
== 7,
1475 "bt_ctf_field_copy creates a valid integer field copy");
1477 /* validate fp copy */
1478 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1480 ok(double_val
== 3.14,
1481 "bt_ctf_field_copy creates a valid floating point number field copy");
1483 /* validate s copy */
1484 str_val
= bt_ctf_field_string_get_value(s_copy
);
1485 ok(str_val
&& !strcmp(str_val
, "btbt"),
1486 "bt_ctf_field_copy creates a valid string field copy");
1488 /* validate e_int copy */
1489 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1492 ok(uint64_t_val
== 23,
1493 "bt_ctf_field_copy creates a valid enum's integer field copy");
1495 /* validate e copy */
1496 e_iter
= bt_ctf_field_enumeration_get_mappings(e_copy
);
1497 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter
,
1498 &str_val
, NULL
, NULL
);
1499 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1500 "bt_ctf_field_copy creates a valid enum field copy");
1502 /* validate v_selected copy */
1503 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1504 assert(v_selected_copy_len
);
1505 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1508 ok(uint64_t_val
== 7,
1509 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1510 bt_put(v_selected_copy_len
);
1511 v_selected_copy_len
= NULL
;
1513 /* validate v_selected copy fields */
1514 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1517 ok(uint64_t_val
== 7,
1518 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1519 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1522 ok(uint64_t_val
== 6,
1523 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1524 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1527 ok(uint64_t_val
== 5,
1528 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1529 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1532 ok(uint64_t_val
== 4,
1533 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1534 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1537 ok(uint64_t_val
== 3,
1538 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1539 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1542 ok(uint64_t_val
== 2,
1543 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1544 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1547 ok(uint64_t_val
== 1,
1548 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1550 /* validate a copy fields */
1551 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1552 ok(str_val
&& !strcmp(str_val
, "a_0"),
1553 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1554 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1555 ok(str_val
&& !strcmp(str_val
, "a_1"),
1556 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1557 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1558 ok(str_val
&& !strcmp(str_val
, "a_2"),
1559 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1560 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1561 ok(str_val
&& !strcmp(str_val
, "a_3"),
1562 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1563 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1564 ok(str_val
&& !strcmp(str_val
, "a_4"),
1565 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1567 /* put everything */
1574 bt_put(v_label1_type
);
1575 bt_put(v_label1_array_type
);
1576 bt_put(v_label2_type
);
1577 bt_put(v_label2_seq_type
);
1586 bt_put(v_selected_0
);
1587 bt_put(v_selected_1
);
1588 bt_put(v_selected_2
);
1589 bt_put(v_selected_3
);
1590 bt_put(v_selected_4
);
1591 bt_put(v_selected_5
);
1592 bt_put(v_selected_6
);
1606 bt_put(v_selected_copy
);
1607 bt_put(v_selected_0_copy
);
1608 bt_put(v_selected_1_copy
);
1609 bt_put(v_selected_2_copy
);
1610 bt_put(v_selected_3_copy
);
1611 bt_put(v_selected_4_copy
);
1612 bt_put(v_selected_5_copy
);
1613 bt_put(v_selected_6_copy
);
1625 void type_field_tests()
1627 struct bt_ctf_field
*uint_12
;
1628 struct bt_ctf_field
*int_16
;
1629 struct bt_ctf_field
*string
;
1630 struct bt_ctf_field
*enumeration
;
1631 struct bt_ctf_field_type
*composite_structure_type
;
1632 struct bt_ctf_field_type
*structure_seq_type
;
1633 struct bt_ctf_field_type
*string_type
;
1634 struct bt_ctf_field_type
*sequence_type
;
1635 struct bt_ctf_field_type
*uint_8_type
;
1636 struct bt_ctf_field_type
*int_16_type
;
1637 struct bt_ctf_field_type
*uint_12_type
=
1638 bt_ctf_field_type_integer_create(12);
1639 struct bt_ctf_field_type
*enumeration_type
;
1640 struct bt_ctf_field_type
*returned_type
;
1641 const char *ret_string
;
1643 returned_type
= bt_ctf_field_get_type(NULL
);
1644 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1646 ok(uint_12_type
, "Create an unsigned integer type");
1647 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1648 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1649 "Set integer type's base as binary");
1650 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1651 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1652 "Set integer type's base as decimal");
1653 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1654 BT_CTF_INTEGER_BASE_UNKNOWN
),
1655 "Reject integer type's base set as unknown");
1656 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1657 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1658 "Set integer type's base as octal");
1659 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1660 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1661 "Set integer type's base as hexadecimal");
1662 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1663 "Reject unknown integer base value");
1664 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1665 "Set integer type signedness to signed");
1666 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1667 "Set integer type signedness to unsigned");
1668 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1669 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1670 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1671 "bt_ctf_field_type_integer_get_size returns a correct value");
1672 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1673 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1674 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1675 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1677 ok(bt_ctf_field_type_set_byte_order(NULL
,
1678 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1679 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1680 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1681 (enum bt_ctf_byte_order
) 42) < 0,
1682 "bt_ctf_field_type_set_byte_order rejects invalid values");
1683 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1684 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1685 "Set an integer's byte order to little endian");
1686 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1687 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1688 "Set an integer's byte order to big endian");
1689 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1690 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1691 "bt_ctf_field_type_get_byte_order returns a correct value");
1692 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1693 BT_CTF_BYTE_ORDER_UNKNOWN
,
1694 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1696 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1697 BT_CTF_FIELD_TYPE_ID_UNKNOWN
,
1698 "bt_ctf_field_type_get_type_id handles NULL correctly");
1699 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1700 BT_CTF_FIELD_TYPE_ID_INTEGER
,
1701 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1703 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1704 BT_CTF_INTEGER_BASE_UNKNOWN
,
1705 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1706 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1707 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1708 "bt_ctf_field_type_integer_get_base returns a correct value");
1710 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1711 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1712 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1713 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1714 (enum bt_ctf_string_encoding
) 123) < 0,
1715 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1716 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1717 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1718 "Set integer type encoding to UTF8");
1719 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1720 BT_CTF_STRING_ENCODING_UNKNOWN
,
1721 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1722 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1723 BT_CTF_STRING_ENCODING_UTF8
,
1724 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1726 int_16_type
= bt_ctf_field_type_integer_create(16);
1727 assert(int_16_type
);
1728 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1729 "Set signedness of 16 bit integer to true");
1730 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1731 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1732 uint_8_type
= bt_ctf_field_type_integer_create(8);
1734 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1735 ok(sequence_type
, "Create a sequence of int16_t type");
1736 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1737 BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
1738 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1740 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1741 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1742 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1744 ok(!strcmp(ret_string
, "seq_len"),
1745 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1746 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1747 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1748 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1750 ok(returned_type
== int_16_type
,
1751 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1752 bt_put(returned_type
);
1754 string_type
= bt_ctf_field_type_string_create();
1755 ok(string_type
, "Create a string type");
1756 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1757 BT_CTF_STRING_ENCODING_NONE
),
1758 "Reject invalid \"None\" string encoding");
1759 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1761 "Reject invalid string encoding");
1762 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1763 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1764 "Set string encoding to ASCII");
1766 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1767 BT_CTF_STRING_ENCODING_UNKNOWN
,
1768 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1769 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1770 BT_CTF_STRING_ENCODING_ASCII
,
1771 "bt_ctf_field_type_string_get_encoding returns the correct value");
1773 structure_seq_type
= bt_ctf_field_type_structure_create();
1774 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1775 BT_CTF_FIELD_TYPE_ID_STRUCT
,
1776 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1777 ok(structure_seq_type
, "Create a structure type");
1778 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1779 uint_8_type
, "seq_len") == 0,
1780 "Add a uint8_t type to a structure");
1781 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1782 sequence_type
, "a_sequence") == 0,
1783 "Add a sequence type to a structure");
1785 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1786 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1787 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1788 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1790 ok(bt_ctf_field_type_structure_get_field(NULL
,
1791 &ret_string
, &returned_type
, 1) < 0,
1792 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1793 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1794 NULL
, &returned_type
, 1) == 0,
1795 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1796 bt_put(returned_type
);
1797 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1798 &ret_string
, NULL
, 1) == 0,
1799 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1800 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1801 &ret_string
, &returned_type
, 10) < 0,
1802 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1803 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1804 &ret_string
, &returned_type
, 1) == 0,
1805 "bt_ctf_field_type_structure_get_field returns a field");
1806 ok(!strcmp(ret_string
, "a_sequence"),
1807 "bt_ctf_field_type_structure_get_field returns a correct field name");
1808 ok(returned_type
== sequence_type
,
1809 "bt_ctf_field_type_structure_get_field returns a correct field type");
1810 bt_put(returned_type
);
1812 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1813 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1814 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1815 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1816 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1817 structure_seq_type
, "a_sequence");
1818 ok(returned_type
== sequence_type
,
1819 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1820 bt_put(returned_type
);
1822 composite_structure_type
= bt_ctf_field_type_structure_create();
1823 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1824 string_type
, "a_string") == 0,
1825 "Add a string type to a structure");
1826 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1827 structure_seq_type
, "inner_structure") == 0,
1828 "Add a structure type to a structure");
1830 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1831 NULL
, "a_sequence") == NULL
,
1832 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1833 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1834 structure_seq_type
, NULL
) == NULL
,
1835 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1836 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1837 structure_seq_type
, "a_sequence");
1838 ok(returned_type
== sequence_type
,
1839 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1840 bt_put(returned_type
);
1842 int_16
= bt_ctf_field_create(int_16_type
);
1843 ok(int_16
, "Instanciate a signed 16-bit integer");
1844 uint_12
= bt_ctf_field_create(uint_12_type
);
1845 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1846 returned_type
= bt_ctf_field_get_type(int_16
);
1847 ok(returned_type
== int_16_type
,
1848 "bt_ctf_field_get_type returns the correct type");
1850 /* Can't modify types after instanciating them */
1851 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1852 BT_CTF_INTEGER_BASE_DECIMAL
),
1853 "Check an integer type' base can't be modified after instanciation");
1854 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1855 "Check an integer type's signedness can't be modified after instanciation");
1857 /* Check signed property is checked */
1858 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1859 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1860 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1861 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1863 /* Check overflows are properly tested for */
1864 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1865 "Check -32768 is allowed for a signed 16-bit integer");
1866 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1867 "Check 32767 is allowed for a signed 16-bit integer");
1868 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1869 "Check 32768 is not allowed for a signed 16-bit integer");
1870 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1871 "Check -32769 is not allowed for a signed 16-bit integer");
1872 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1873 "Check -42 is allowed for a signed 16-bit integer");
1875 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1876 "Check 4095 is allowed for an unsigned 12-bit integer");
1877 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1878 "Check 4096 is not allowed for a unsigned 12-bit integer");
1879 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1880 "Check 0 is allowed for an unsigned 12-bit integer");
1882 string
= bt_ctf_field_create(string_type
);
1883 ok(string
, "Instanciate a string field");
1884 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1885 "Set a string's value");
1887 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1888 ok(enumeration_type
,
1889 "Create an enumeration type with an unsigned 12-bit integer as container");
1890 enumeration
= bt_ctf_field_create(enumeration_type
);
1892 "Check enumeration types are validated before instantiation");
1897 bt_put(enumeration
);
1898 bt_put(composite_structure_type
);
1899 bt_put(structure_seq_type
);
1900 bt_put(string_type
);
1901 bt_put(sequence_type
);
1902 bt_put(uint_8_type
);
1903 bt_put(int_16_type
);
1904 bt_put(uint_12_type
);
1905 bt_put(enumeration_type
);
1906 bt_put(returned_type
);
1910 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1911 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1914 * Append enough events to force the underlying packet to be resized.
1915 * Also tests that a new event can be declared after a stream has been
1916 * instantiated and used/flushed.
1920 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1922 struct bt_ctf_field_type
*integer_type
=
1923 bt_ctf_field_type_integer_create(17);
1924 struct bt_ctf_field_type
*string_type
=
1925 bt_ctf_field_type_string_create();
1926 struct bt_ctf_event
*event
= NULL
;
1927 struct bt_ctf_field
*ret_field
= NULL
;
1928 struct bt_ctf_field_type
*ret_field_type
= NULL
;
1929 uint64_t ret_uint64
;
1930 int events_appended
= 0;
1931 struct bt_ctf_field
*packet_context
= NULL
,
1932 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
1933 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
1934 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
1935 struct bt_ctf_field_type
*ep_type
= NULL
;
1937 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1939 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1941 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1942 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1948 * bt_ctf_stream_class_add_event_class() copies the field types
1949 * of event_class, so we retrieve the new ones to create the
1950 * appropriate fields.
1952 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
1954 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1955 ep_type
, "field_1");
1956 assert(ep_field_1_type
);
1957 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1958 ep_type
, "a_string");
1959 assert(ep_a_string_type
);
1961 event
= bt_ctf_event_create(event_class
);
1962 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
1963 ret_field_type
= bt_ctf_field_get_type(ret_field
);
1964 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
1965 "bt_ctf_event_get_payload_by_index returns a correct field");
1966 bt_put(ret_field_type
);
1969 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
1970 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1971 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
1972 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1975 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
1976 event
= bt_ctf_event_create(event_class
);
1977 struct bt_ctf_field
*integer
=
1978 bt_ctf_field_create(ep_field_1_type
);
1979 struct bt_ctf_field
*string
=
1980 bt_ctf_field_create(ep_a_string_type
);
1982 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1983 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1984 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1987 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1988 ret
|= bt_ctf_event_set_payload(event
, "a_string",
1992 /* Populate stream event context */
1993 stream_event_context
=
1994 bt_ctf_event_get_stream_event_context(event
);
1995 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
1996 "common_event_context");
1997 BT_PUT(stream_event_context
);
1998 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2002 ret
|= bt_ctf_stream_append_event(stream
, event
);
2010 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2011 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2012 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2013 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2014 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2015 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2016 ok(ret
== 0 && ret_uint64
== 0,
2017 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2018 bt_ctf_stream_append_discarded_events(stream
, 1000);
2019 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2020 ok(ret
== 0 && ret_uint64
== 1000,
2021 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2024 ok(events_appended
, "Append 100 000 events to a stream");
2027 * Populate the custom packet context field with a dummy value
2028 * otherwise flush will fail.
2030 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2031 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2032 "custom_packet_context_field");
2033 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2035 ok(bt_ctf_stream_flush(stream
) == 0,
2036 "Flush a stream that forces a packet resize");
2037 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2038 ok(ret
== 0 && ret_uint64
== 1000,
2039 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2040 bt_put(integer_type
);
2041 bt_put(string_type
);
2042 bt_put(packet_context
);
2043 bt_put(packet_context_field
);
2044 bt_put(stream_event_context
);
2045 bt_put(event_class
);
2046 bt_put(ep_field_1_type
);
2047 bt_put(ep_a_string_type
);
2052 void test_empty_stream(struct bt_ctf_writer
*writer
)
2055 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2056 struct bt_ctf_stream_class
*stream_class
= NULL
;
2057 struct bt_ctf_stream
*stream
= NULL
;
2059 trace
= bt_ctf_writer_get_trace(writer
);
2061 diag("Failed to get trace from writer");
2066 stream_class
= bt_ctf_stream_class_create("empty_stream");
2067 if (!stream_class
) {
2068 diag("Failed to create stream class");
2073 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2075 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
2078 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2079 "bt_ctf_stream_class_get_trace handles NULL correctly");
2080 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2081 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2083 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2085 diag("Failed to create writer stream");
2090 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2091 ok(ret_trace
== trace
,
2092 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2095 "Created a stream class with default attributes and an empty stream");
2099 bt_put(stream_class
);
2103 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
,
2104 struct bt_ctf_clock
*clock
)
2107 struct bt_ctf_stream_class
*stream_class
= NULL
;
2108 struct bt_ctf_stream
*stream
= NULL
;
2109 struct bt_ctf_field_type
*integer_type
= NULL
,
2110 *sequence_type
= NULL
, *event_header_type
= NULL
;
2111 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2112 *event_header
= NULL
, *packet_header
= NULL
;
2113 struct bt_ctf_event_class
*event_class
= NULL
;
2114 struct bt_ctf_event
*event
= NULL
;
2116 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2117 if (!stream_class
) {
2118 fail("Failed to create stream class");
2122 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2124 fail("Failed to set stream class clock");
2129 * Customize event header to add an "seq_len" integer member
2130 * which will be used as the length of a sequence in an event of this
2133 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2135 if (!event_header_type
) {
2136 fail("Failed to get event header type");
2140 integer_type
= bt_ctf_field_type_integer_create(13);
2141 if (!integer_type
) {
2142 fail("Failed to create length integer type");
2146 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2147 integer_type
, "seq_len");
2149 fail("Failed to add a new field to stream event header");
2153 event_class
= bt_ctf_event_class_create("sequence_event");
2155 fail("Failed to create event class");
2160 * This event's payload will contain a sequence which references
2161 * stream.event.header.seq_len as its length field.
2163 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2164 "stream.event.header.seq_len");
2165 if (!sequence_type
) {
2166 fail("Failed to create a sequence");
2170 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2173 fail("Failed to add a sequence to an event class");
2177 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2179 fail("Failed to add event class to stream class");
2183 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2185 fail("Failed to create stream")
2190 * We have defined a custom packet header field. We have to populate it
2193 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2194 if (!packet_header
) {
2195 fail("Failed to get stream packet header");
2199 integer
= bt_ctf_field_structure_get_field(packet_header
,
2200 "custom_trace_packet_header_field");
2202 fail("Failed to retrieve custom_trace_packet_header_field");
2206 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2208 fail("Failed to set custom_trace_packet_header_field value");
2213 event
= bt_ctf_event_create(event_class
);
2215 fail("Failed to create event");
2219 event_header
= bt_ctf_event_get_header(event
);
2220 if (!event_header
) {
2221 fail("Failed to get event header");
2225 integer
= bt_ctf_field_structure_get_field(event_header
,
2228 fail("Failed to get seq_len field from event header");
2232 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2234 fail("Failed to set seq_len value in event header");
2238 /* Populate both sequence integer fields */
2239 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2241 fail("Failed to retrieve sequence from event");
2245 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2247 fail("Failed to set sequence length");
2252 for (i
= 0; i
< 2; i
++) {
2253 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2255 fail("Failed to retrieve sequence element");
2259 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2261 fail("Failed to set sequence element value");
2269 ret
= bt_ctf_stream_append_event(stream
, event
);
2271 fail("Failed to append event to stream");
2275 ret
= bt_ctf_stream_flush(stream
);
2277 fail("Failed to flush custom_event_header stream");
2281 bt_put(stream_class
);
2282 bt_put(event_class
);
2286 bt_put(event_header
);
2287 bt_put(packet_header
);
2288 bt_put(sequence_type
);
2289 bt_put(integer_type
);
2290 bt_put(event_header_type
);
2294 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
,
2295 struct bt_ctf_clock
*clock
)
2298 struct bt_ctf_stream_class
*stream_class
= NULL
;
2299 struct bt_ctf_stream
*stream
= NULL
,
2301 struct bt_ctf_event_class
*event_class
= NULL
;
2302 struct bt_ctf_event
*event
= NULL
;
2303 struct bt_ctf_field_type
*integer_type
= NULL
;
2304 struct bt_ctf_field
*integer
= NULL
;
2306 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2307 if (!stream_class
) {
2308 diag("Failed to create stream class");
2313 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2315 diag("Failed to set stream class clock");
2319 event_class
= bt_ctf_event_class_create("some_event_class_name");
2320 integer_type
= bt_ctf_field_type_integer_create(32);
2321 if (!integer_type
) {
2322 diag("Failed to create integer field type");
2327 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2330 diag("Failed to add field to event class");
2334 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2337 diag("Failed to add event class to stream class");
2340 event
= bt_ctf_event_create(event_class
);
2342 diag("Failed to create event");
2347 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2349 diag("Failed to get integer field payload from event");
2354 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2356 diag("Failed to set integer field value");
2360 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2362 diag("Failed to create writer stream");
2367 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2368 "bt_ctf_event_get_stream handles NULL correctly");
2369 ok(bt_ctf_event_get_stream(event
) == NULL
,
2370 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2372 ret
= bt_ctf_stream_append_event(stream
, event
);
2374 diag("Failed to append event to stream");
2378 ret_stream
= bt_ctf_event_get_stream(event
);
2379 ok(ret_stream
== stream
,
2380 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2383 "Create an event before instanciating its associated stream");
2386 bt_put(stream_class
);
2387 bt_put(event_class
);
2389 bt_put(integer_type
);
2394 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2396 struct bt_ctf_event_class
*event_class
;
2398 event_class
= bt_ctf_event_class_create("Simple Event");
2399 assert(event_class
);
2400 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
) == 0,
2401 "two event classes with the same name may cohabit within the same stream class");
2402 bt_put(event_class
);
2404 event_class
= bt_ctf_event_class_create("different name, ok");
2405 assert(event_class
);
2406 assert(!bt_ctf_event_class_set_id(event_class
, 13));
2407 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2408 "two event classes with the same ID cannot cohabit within the same stream class");
2409 bt_put(event_class
);
2413 struct bt_ctf_event_class
*create_minimal_event_class(void)
2415 struct bt_ctf_event_class
*ec
= NULL
;
2416 struct bt_ctf_field_type
*int_ft
= NULL
;
2419 int_ft
= bt_ctf_field_type_integer_create(23);
2421 ec
= bt_ctf_event_class_create("minimal");
2423 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2431 void test_create_writer_vs_non_writer_mode(void)
2434 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2435 const char *writer_stream_name
= "writer stream instance";
2436 struct bt_ctf_writer
*writer
= NULL
;
2437 struct bt_ctf_trace
*writer_trace
= NULL
;
2438 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2439 struct bt_ctf_stream
*writer_stream
= NULL
;
2440 struct bt_ctf_stream
*writer_stream2
= NULL
;
2441 struct bt_ctf_stream
*packet_stream
= NULL
;
2442 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2443 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2444 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2445 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2446 struct bt_ctf_event_class
*writer_ec
= NULL
;
2447 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2448 struct bt_ctf_event
*event
= NULL
;
2449 struct bt_ctf_event
*event2
= NULL
;
2450 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2451 struct bt_ctf_field
*int_field
= NULL
;
2452 struct bt_ctf_clock
*writer_clock
= NULL
;
2453 struct bt_ctf_clock_class
*non_writer_clock_class
= NULL
;
2454 struct bt_ctf_packet
*packet
= NULL
;
2455 struct bt_ctf_packet
*packet2
= NULL
;
2457 if (!bt_mkdtemp(trace_path
)) {
2461 /* Create empty structure field type (event header) */
2462 empty_struct_ft
= bt_ctf_field_type_structure_create();
2463 assert(empty_struct_ft
);
2465 /* Create writer, writer stream class, stream, and clock */
2466 writer
= bt_ctf_writer_create(trace_path
);
2468 writer_clock
= bt_ctf_clock_create("writer_clock");
2469 assert(writer_clock
);
2470 ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
2472 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2474 writer_trace
= bt_ctf_writer_get_trace(writer
);
2475 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2476 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2478 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2481 ret
= bt_ctf_stream_class_set_clock(writer_sc
, writer_clock
);
2483 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2485 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2486 assert(writer_stream
);
2487 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2488 "bt_ctf_stream_get_name() returns the stream's name");
2490 /* Create non-writer trace, stream class, stream, and clock */
2491 non_writer_trace
= bt_ctf_trace_create();
2492 assert(non_writer_trace
);
2493 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2494 assert(non_writer_sc
);
2495 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2498 ret
= bt_ctf_stream_class_set_packet_context_type(non_writer_sc
, NULL
);
2500 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2502 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2503 assert(non_writer_stream
);
2504 non_writer_clock_class
=
2505 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");
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);