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/ref.h>
32 #include <babeltrace/ctf/events.h>
33 #include <babeltrace/values.h>
35 #include <babeltrace/compat/stdlib-internal.h>
37 #include <sys/utsname.h>
38 #include <babeltrace/compat/limits-internal.h>
39 #include <babeltrace/compat/stdio-internal.h>
44 #include <babeltrace/compat/dirent-internal.h>
50 #define METADATA_LINE_SIZE 512
51 #define SEQUENCE_TEST_LENGTH 10
52 #define ARRAY_TEST_LENGTH 5
53 #define PACKET_RESIZE_TEST_LENGTH 100000
55 #define DEFAULT_CLOCK_FREQ 1000000000
56 #define DEFAULT_CLOCK_PRECISION 1
57 #define DEFAULT_CLOCK_OFFSET 0
58 #define DEFAULT_CLOCK_OFFSET_S 0
59 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
60 #define DEFAULT_CLOCK_TIME 0
61 #define DEFAULT_CLOCK_VALUE 0
65 static int64_t current_time
= 42;
67 /* Return 1 if uuids match, zero if different. */
69 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
74 if (!uuid_a
|| !uuid_b
) {
78 for (i
= 0; i
< 16; i
++) {
79 if (uuid_a
[i
] != uuid_b
[i
]) {
90 void validate_trace(char *parser_path
, char *trace_path
)
93 char babeltrace_output_path
[] = "/tmp/babeltrace_output_XXXXXX";
94 int babeltrace_output_fd
= -1;
101 babeltrace_output_fd
= mkstemp(babeltrace_output_path
);
102 unlink(babeltrace_output_path
);
104 if (babeltrace_output_fd
== -1) {
105 diag("Failed to create a temporary file for trace parsing.");
113 waitpid(pid
, &status
, 0);
114 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
116 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
118 perror("# dup2 babeltrace_output_fd to STDOUT");
122 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
124 perror("# dup2 babeltrace_output_fd to STDERR");
128 execl(parser_path
, "babeltrace", trace_path
, NULL
);
129 perror("# Could not launch the babeltrace process");
133 ok(ret
== 0, "Babeltrace could read the resulting trace");
135 if (ret
&& babeltrace_output_fd
>= 0) {
137 size_t len
= METADATA_LINE_SIZE
;
138 FILE *babeltrace_output_fp
= NULL
;
140 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
141 if (!babeltrace_output_fp
) {
142 perror("fdopen on babeltrace_output_fd");
145 babeltrace_output_fd
= -1;
149 diag("malloc error");
151 rewind(babeltrace_output_fp
);
152 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
158 if (babeltrace_output_fp
) {
159 if (fclose(babeltrace_output_fp
)) {
160 diag("fclose error");
165 if (babeltrace_output_fd
>= 0) {
166 if (close(babeltrace_output_fd
)) {
173 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
174 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
176 /* Create and add a simple event class */
177 struct bt_ctf_event_class
*simple_event_class
=
178 bt_ctf_event_class_create("Simple Event");
179 struct bt_ctf_field_type
*uint_12_type
=
180 bt_ctf_field_type_integer_create(12);
181 struct bt_ctf_field_type
*int_64_type
=
182 bt_ctf_field_type_integer_create(64);
183 struct bt_ctf_field_type
*float_type
=
184 bt_ctf_field_type_floating_point_create();
185 struct bt_ctf_field_type
*enum_type
;
186 struct bt_ctf_field_type
*enum_type_unsigned
=
187 bt_ctf_field_type_enumeration_create(uint_12_type
);
188 struct bt_ctf_field_type
*event_context_type
=
189 bt_ctf_field_type_structure_create();
190 struct bt_ctf_field_type
*event_payload_type
= NULL
;
191 struct bt_ctf_field_type
*returned_type
;
192 struct bt_ctf_event
*simple_event
;
193 struct bt_ctf_field
*integer_field
;
194 struct bt_ctf_field
*float_field
;
195 struct bt_ctf_field
*enum_field
;
196 struct bt_ctf_field
*enum_field_unsigned
;
197 struct bt_ctf_field
*enum_container_field
;
198 const char *mapping_name_test
= "truie";
199 const double double_test_value
= 3.1415;
200 struct bt_ctf_field
*enum_container_field_unsigned
;
201 const char *mapping_name_negative_test
= "negative_value";
202 const char *ret_char
;
204 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
205 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
206 struct bt_ctf_event_class
*ret_event_class
;
207 struct bt_ctf_field
*packet_context
;
208 struct bt_ctf_field
*packet_context_field
;
209 struct bt_ctf_field
*stream_event_context
;
210 struct bt_ctf_field
*stream_event_context_field
;
211 struct bt_ctf_field
*event_context
;
212 struct bt_ctf_field
*event_context_field
;
213 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
214 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
215 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
216 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
219 ok(uint_12_type
, "Create an unsigned integer type");
221 ok(!bt_ctf_field_type_integer_set_signed(int_64_type
, 1),
222 "Set signed 64 bit integer signedness to true");
223 ok(int_64_type
, "Create a signed integer type");
224 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
226 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
227 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
228 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
229 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
230 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
231 bt_put(returned_type
);
233 bt_ctf_field_type_set_alignment(float_type
, 32);
234 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
235 "bt_ctf_field_type_get_alignment handles NULL correctly");
236 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
237 "bt_ctf_field_type_get_alignment returns a correct value");
239 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
240 "Set a floating point type's exponent digit count");
241 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
242 "Set a floating point type's mantissa digit count");
244 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
245 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
246 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
247 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
248 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
249 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
250 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
251 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
253 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
254 mapping_name_negative_test
, -12345, 0) == 0,
255 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
256 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
257 "escaping; \"test\"", 1, 1) == 0,
258 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
259 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
260 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
261 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
262 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
263 "event clock int float", 5, 22) == 0,
264 "Accept enumeration mapping strings containing reserved keywords");
265 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
267 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
268 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
269 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
270 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
271 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
272 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
273 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
275 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(NULL
, -42);
276 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly");
278 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, -4200000);
279 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values");
281 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, 3);
282 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value");
283 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, NULL
, NULL
, NULL
) == 0,
284 "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly");
287 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
288 "enum_field") == 0, "Add signed enumeration field to event");
290 ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL
, 0, &ret_char
,
291 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
292 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly");
293 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, NULL
,
294 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
295 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly");
296 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
297 NULL
, &ret_range_end_int64_t
) == 0,
298 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly");
299 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
300 &ret_range_start_int64_t
, NULL
) == 0,
301 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly");
302 /* Assumes entries are sorted by range_start values. */
303 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 6, &ret_char
,
304 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
305 "bt_ctf_field_type_enumeration_get_mapping_signed returns a value");
306 ok(!strcmp(ret_char
, mapping_name_test
),
307 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name");
308 ok(ret_range_start_int64_t
== 42,
309 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start");
310 ok(ret_range_end_int64_t
== 42,
311 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end");
313 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
314 "escaping; \"test\"", 0, 0) == 0,
315 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
316 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
317 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
318 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
319 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
320 "event clock int float", 5, 22) == 0,
321 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
322 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
324 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
325 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names");
326 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
327 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries");
328 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
329 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
330 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
331 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
333 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
334 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
335 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 6,
336 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
338 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
339 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
340 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
341 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
342 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
343 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
344 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
345 NULL
, &ret_range_end_uint64_t
) == 0,
346 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
347 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
348 &ret_range_start_uint64_t
, NULL
) == 0,
349 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
350 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 4, &ret_char
,
351 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
352 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
353 ok(!strcmp(ret_char
, mapping_name_test
),
354 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
355 ok(ret_range_start_uint64_t
== 42,
356 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
357 ok(ret_range_end_uint64_t
== 42,
358 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
360 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
362 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
365 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
367 /* Set an event context type which will contain a single integer. */
368 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
369 "event_specific_context"),
370 "Add event specific context field");
371 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
372 "bt_ctf_event_class_get_context_type handles NULL correctly");
373 ok(bt_ctf_event_class_get_context_type(simple_event_class
) == NULL
,
374 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
376 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
377 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
378 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
379 "Set an event class' context type successfully");
380 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
381 ok(returned_type
== event_context_type
,
382 "bt_ctf_event_class_get_context_type returns the appropriate type");
383 bt_put(returned_type
);
385 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
386 "Adding simple event class to stream class");
389 * bt_ctf_stream_class_add_event_class() copies the field types
390 * of simple_event_class, so we retrieve the new ones to create
391 * the appropriate fields.
393 BT_PUT(event_context_type
);
394 BT_PUT(event_payload_type
);
395 event_payload_type
= bt_ctf_event_class_get_payload_type(
397 assert(event_payload_type
);
398 event_context_type
= bt_ctf_event_class_get_context_type(
400 assert(event_context_type
);
401 ep_integer_field_type
=
402 bt_ctf_field_type_structure_get_field_type_by_name(
403 event_payload_type
, "integer_field");
404 assert(ep_integer_field_type
);
406 bt_ctf_field_type_structure_get_field_type_by_name(
407 event_payload_type
, "enum_field");
408 assert(ep_enum_field_type
);
409 ep_enum_field_unsigned_type
=
410 bt_ctf_field_type_structure_get_field_type_by_name(
411 event_payload_type
, "enum_field_unsigned");
412 assert(ep_enum_field_unsigned_type
);
414 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
415 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
416 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
417 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
418 ok(bt_ctf_stream_class_get_event_class(NULL
, 0) == NULL
,
419 "bt_ctf_stream_class_get_event_class handles NULL correctly");
420 ok(bt_ctf_stream_class_get_event_class(stream_class
, 8724) == NULL
,
421 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
422 ret_event_class
= bt_ctf_stream_class_get_event_class(stream_class
, 0);
423 ok(ret_event_class
== simple_event_class
,
424 "bt_ctf_stream_class_get_event_class returns the correct event class");
425 bt_put(ret_event_class
);
426 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
427 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
428 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
429 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
431 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
432 ok(ret_event_class
== simple_event_class
,
433 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
434 bt_put(ret_event_class
);
436 ok(bt_ctf_stream_class_get_event_class_by_name(NULL
, "some event name") == NULL
,
437 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
438 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, NULL
) == NULL
,
439 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
440 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class
, "some event name") == NULL
,
441 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
442 ret_event_class
= bt_ctf_stream_class_get_event_class_by_name(stream_class
, "Simple Event");
443 ok(ret_event_class
== simple_event_class
,
444 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
445 bt_put(ret_event_class
);
447 simple_event
= bt_ctf_event_create(simple_event_class
);
449 "Instantiate an event containing a single integer field");
451 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
452 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
453 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
454 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
456 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
457 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
458 "bt_ctf_field_floating_point_get_value fails on an unset float field");
459 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
460 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
461 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
462 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
463 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
464 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
465 "bt_ctf_field_floating_point_get_value returns a double value");
466 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
467 "bt_ctf_field_floating_point_get_value returns a correct value");
469 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
472 iter
= bt_ctf_field_enumeration_get_mappings(NULL
);
473 ok(!iter
, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
474 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
475 ok(!iter
, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
476 enum_container_field
= bt_ctf_field_enumeration_get_container(
478 ok(bt_ctf_field_signed_integer_set_value(
479 enum_container_field
, -42) == 0,
480 "Set signed enumeration container value");
481 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
482 ok(iter
, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
483 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, &ret_char
, NULL
, NULL
);
484 ok(!ret
&& ret_char
, "bt_ctf_field_type_enumeration_mapping_iterator_get_signed return a mapping name");
485 ok(!strcmp(ret_char
, mapping_name_negative_test
),
486 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
487 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
491 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
492 assert(enum_field_unsigned
);
493 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
494 enum_field_unsigned
);
495 ok(bt_ctf_field_unsigned_integer_set_value(
496 enum_container_field_unsigned
, 42) == 0,
497 "Set unsigned enumeration container value");
498 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
499 enum_field_unsigned
);
501 iter
= bt_ctf_field_enumeration_get_mappings(enum_field_unsigned
);
503 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, &ret_char
, NULL
, NULL
);
504 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
505 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container");
507 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
509 /* Populate stream event context */
510 stream_event_context
=
511 bt_ctf_event_get_stream_event_context(simple_event
);
512 assert(stream_event_context
);
513 stream_event_context_field
= bt_ctf_field_structure_get_field(
514 stream_event_context
, "common_event_context");
515 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
517 /* Populate the event's context */
518 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
519 "bt_ctf_event_get_event_context handles NULL correctly");
520 event_context
= bt_ctf_event_get_event_context(simple_event
);
522 "bt_ctf_event_get_event_context returns a field");
523 returned_type
= bt_ctf_field_get_type(event_context
);
524 ok(returned_type
== event_context_type
,
525 "bt_ctf_event_get_event_context returns a field of the appropriate type");
526 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
527 "event_specific_context");
528 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
529 "Successfully set an event context's value");
530 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
531 "bt_ctf_event_set_event_context handles a NULL event correctly");
532 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
533 "bt_ctf_event_set_event_context handles a NULL event context correctly");
534 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
535 "bt_ctf_event_set_event_context rejects a context of the wrong type");
536 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
537 "Set an event context successfully");
539 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
540 "Append simple event to trace stream");
542 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
543 "bt_ctf_stream_get_packet_context handles NULL correctly");
544 packet_context
= bt_ctf_stream_get_packet_context(stream
);
546 "bt_ctf_stream_get_packet_context returns a packet context");
548 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
550 ok(packet_context_field
,
551 "Packet context contains the default packet_size field.");
552 bt_put(packet_context_field
);
553 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
554 "custom_packet_context_field");
555 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
556 "Custom packet context field value successfully set.");
558 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
559 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
560 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
561 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
562 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
563 "Successfully set a stream's packet context");
565 ok(bt_ctf_stream_flush(stream
) == 0,
566 "Flush trace stream with one event");
568 bt_put(simple_event_class
);
569 bt_put(simple_event
);
570 bt_put(uint_12_type
);
574 bt_put(enum_type_unsigned
);
575 bt_put(returned_type
);
576 bt_put(event_context_type
);
577 bt_put(integer_field
);
580 bt_put(enum_field_unsigned
);
581 bt_put(enum_container_field
);
582 bt_put(enum_container_field_unsigned
);
583 bt_put(packet_context
);
584 bt_put(packet_context_field
);
585 bt_put(stream_event_context
);
586 bt_put(stream_event_context_field
);
587 bt_put(event_context
);
588 bt_put(event_context_field
);
589 bt_put(event_payload_type
);
590 bt_put(ep_integer_field_type
);
591 bt_put(ep_enum_field_type
);
592 bt_put(ep_enum_field_unsigned_type
);
597 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
598 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
600 struct event_class_attrs_counts
{
611 struct event_class_attrs_counts
;
612 const char *complex_test_event_string
= "Complex Test Event";
613 const char *test_string_1
= "Test ";
614 const char *test_string_2
= "string ";
615 const char *test_string_3
= "abcdefghi";
616 const char *test_string_4
= "abcd\0efg\0hi";
617 const char *test_string_cat
= "Test string abcdeabcd";
618 struct bt_ctf_field_type
*uint_35_type
=
619 bt_ctf_field_type_integer_create(35);
620 struct bt_ctf_field_type
*int_16_type
=
621 bt_ctf_field_type_integer_create(16);
622 struct bt_ctf_field_type
*uint_3_type
=
623 bt_ctf_field_type_integer_create(3);
624 struct bt_ctf_field_type
*enum_variant_type
=
625 bt_ctf_field_type_enumeration_create(uint_3_type
);
626 struct bt_ctf_field_type
*variant_type
=
627 bt_ctf_field_type_variant_create(enum_variant_type
,
629 struct bt_ctf_field_type
*string_type
=
630 bt_ctf_field_type_string_create();
631 struct bt_ctf_field_type
*sequence_type
;
632 struct bt_ctf_field_type
*array_type
;
633 struct bt_ctf_field_type
*inner_structure_type
=
634 bt_ctf_field_type_structure_create();
635 struct bt_ctf_field_type
*complex_structure_type
=
636 bt_ctf_field_type_structure_create();
637 struct bt_ctf_field_type
*ret_field_type
;
638 struct bt_ctf_event_class
*event_class
;
639 struct bt_ctf_event
*event
;
640 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
641 *inner_structure_field
, *complex_structure_field
,
642 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
643 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
644 *stream_event_ctx_int_field
, *ret_field
;
645 uint64_t ret_unsigned_int
;
646 int64_t ret_signed_int
;
647 const char *ret_string
;
648 struct bt_ctf_stream_class
*ret_stream_class
;
649 struct bt_ctf_event_class
*ret_event_class
;
650 struct bt_ctf_field
*packet_context
, *packet_context_field
;
651 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
652 struct bt_value
*obj
;
654 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
655 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
656 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
657 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
658 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
659 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
660 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
661 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
662 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
663 "Set alignment of signed 16 bit integer to 32");
664 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
665 "Set integer signedness to true");
666 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
667 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
668 "Set signed 16 bit integer base to hexadecimal");
670 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
671 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
674 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
675 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
676 ret_field_type
= bt_ctf_field_type_array_get_element_type(
678 ok(ret_field_type
== int_16_type
,
679 "bt_ctf_field_type_array_get_element_type returns the correct type");
680 bt_put(ret_field_type
);
682 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
683 "bt_ctf_field_type_array_get_length handles NULL correctly");
684 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
685 "bt_ctf_field_type_array_get_length returns the correct length");
687 ok(bt_ctf_field_type_structure_add_field(inner_structure_type
,
688 inner_structure_type
, "yes"), "Cannot add self to structure");
689 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
690 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
691 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
692 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
693 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
694 array_type
, "an_array"), "Add an_array field to inner structure");
696 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
698 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
700 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
701 "UINT35_TYPE", 2, 7);
703 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(NULL
, "INT16_TYPE");
704 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly");
706 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, "INT16_TYPE");
707 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly");
708 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
709 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly");
712 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, NULL
);
713 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly");
715 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL
, 1);
716 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly");
718 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, -42);
719 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly");
720 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) != 0,
721 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly");
724 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, 5);
725 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly");
726 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
727 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly");
730 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
731 "An unknown entry"), "Reject a variant field based on an unknown tag value");
732 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
733 "UINT3_TYPE") == 0, "Add a field to a variant");
734 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
735 "INT16_TYPE"), "Add INT16_TYPE field to variant");
736 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
737 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
739 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
740 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
741 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
742 ok(ret_field_type
== enum_variant_type
,
743 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
744 bt_put(ret_field_type
);
746 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
747 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
748 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
749 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
750 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
751 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
752 "INT16_TYPE") == NULL
,
753 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
754 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
756 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
757 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
758 variant_type
, "INT16_TYPE");
759 ok(ret_field_type
== int_16_type
,
760 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
761 bt_put(ret_field_type
);
763 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
764 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
765 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
766 "bt_ctf_field_type_variant_get_field_count returns the correct count");
768 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
769 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
770 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
771 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
772 bt_put(ret_field_type
);
773 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
774 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
775 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
776 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
777 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
778 "bt_ctf_field_type_variant_get_field returns a field");
779 ok(!strcmp("INT16_TYPE", ret_string
),
780 "bt_ctf_field_type_variant_get_field returns a correct field name");
781 ok(ret_field_type
== int_16_type
,
782 "bt_ctf_field_type_variant_get_field returns a correct field type");
783 bt_put(ret_field_type
);
785 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
786 enum_variant_type
, "variant_selector"),
787 "Add variant_selector field to complex structure");
788 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
789 string_type
, "a_string"), "Add a_string field to complex structure");
790 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
791 variant_type
, "variant_value"),
792 "Add variant_value field to complex structure");
793 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
794 inner_structure_type
, "inner_structure"),
795 "Add inner_structure field to complex structure");
797 ok(bt_ctf_event_class_create("clock") == NULL
,
798 "Reject creation of an event class with an illegal name");
799 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
800 ok(event_class
, "Create an event class");
801 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
802 "Reject addition of a field with an empty name to an event");
803 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
804 "Reject addition of a field with a NULL type to an event");
805 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
807 "Reject addition of a type with an illegal name to an event");
808 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
810 "Add field of type unsigned integer to an event");
811 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
812 "int_16") == 0, "Add field of type signed integer to an event");
813 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
814 "complex_structure") == 0,
815 "Add composite structure to an event");
817 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
818 "bt_ctf_event_class_get_name handles NULL correctly");
819 ret_string
= bt_ctf_event_class_get_name(event_class
);
820 ok(!strcmp(ret_string
, complex_test_event_string
),
821 "bt_ctf_event_class_get_name returns a correct name");
822 ok(bt_ctf_event_class_get_id(event_class
) < 0,
823 "bt_ctf_event_class_get_id returns a negative value when not set");
824 ok(bt_ctf_event_class_get_id(NULL
) < 0,
825 "bt_ctf_event_class_get_id handles NULL correctly");
826 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
827 "bt_ctf_event_class_set_id handles NULL correctly");
828 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
829 "Set an event class' id");
830 ok(bt_ctf_event_class_get_id(event_class
) == 42,
831 "bt_ctf_event_class_get_id returns the correct value");
833 /* Test event class attributes */
834 obj
= bt_value_integer_create_init(15);
836 ok(bt_ctf_event_class_set_attribute(NULL
, "id", obj
),
837 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
838 ok(bt_ctf_event_class_set_attribute(event_class
, NULL
, obj
),
839 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
840 ok(bt_ctf_event_class_set_attribute(event_class
, "id", NULL
),
841 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
842 assert(!bt_value_integer_set(obj
, -3));
843 ok(bt_ctf_event_class_set_attribute(event_class
, "id", obj
),
844 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
845 assert(!bt_value_integer_set(obj
, 11));
846 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
847 ok(!ret
&& bt_ctf_event_class_get_id(event_class
) == 11,
848 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
849 ret
= bt_ctf_event_class_set_attribute(event_class
, "name", obj
);
850 ret
&= bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
);
852 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
855 obj
= bt_value_integer_create_init(5);
857 ok(!bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
),
858 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
860 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL
, "loglevel"),
861 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
862 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, NULL
),
863 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
864 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class
, "meow"),
865 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
866 obj
= bt_ctf_event_class_get_attribute_value_by_name(event_class
,
869 ret
= bt_value_integer_get(obj
, &int64_value
);
870 ok(obj
&& !ret
&& int64_value
== 5,
871 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
874 obj
= bt_value_string_create_init("nu name");
876 assert(!bt_ctf_event_class_set_attribute(event_class
, "name", obj
));
877 ret_string
= bt_ctf_event_class_get_name(event_class
);
878 ok(!strcmp(ret_string
, "nu name"),
879 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
880 ret
= bt_ctf_event_class_set_attribute(event_class
, "id", obj
);
881 ret
&= bt_ctf_event_class_set_attribute(event_class
, "loglevel", obj
);
883 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
885 obj
= bt_value_string_create_init("http://kernel.org/");
887 assert(!bt_ctf_event_class_set_attribute(event_class
, "model.emf.uri", obj
));
890 ok(bt_ctf_event_class_get_attribute_count(NULL
),
891 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
892 ok(bt_ctf_event_class_get_attribute_count(event_class
) == 4,
893 "bt_ctf_event_class_get_attribute_count returns the correct count");
894 ok(!bt_ctf_event_class_get_attribute_name(NULL
, 0),
895 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
896 ok(!bt_ctf_event_class_get_attribute_name(event_class
, 4),
897 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
898 ok(!bt_ctf_event_class_get_attribute_value(NULL
, 0),
899 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
900 ok(!bt_ctf_event_class_get_attribute_value(event_class
, 4),
901 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
903 memset(&attrs_count
, 0, sizeof(attrs_count
));
905 for (i
= 0; i
< 4; ++i
) {
906 ret_string
= bt_ctf_event_class_get_attribute_name(event_class
,
908 obj
= bt_ctf_event_class_get_attribute_value(event_class
, i
);
909 assert(ret_string
&& obj
);
911 if (!strcmp(ret_string
, "id")) {
913 ok(bt_value_is_integer(obj
),
914 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
916 } else if (!strcmp(ret_string
, "name")) {
918 ok(bt_value_is_string(obj
),
919 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
921 } else if (!strcmp(ret_string
, "loglevel")) {
922 attrs_count
.loglevel
++;
923 ok(bt_value_is_integer(obj
),
924 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
926 } else if (!strcmp(ret_string
, "model.emf.uri")) {
927 attrs_count
.modelemfuri
++;
928 ok(bt_value_is_string(obj
),
929 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
932 attrs_count
.unknown
++;
938 ok(attrs_count
.unknown
== 0, "event class has no unknown attributes");
939 ok(attrs_count
.id
== 1 && attrs_count
.name
== 1 &&
940 attrs_count
.loglevel
== 1 && attrs_count
.modelemfuri
== 1,
941 "event class has one instance of each known attribute");
943 /* Add event class to the stream class */
944 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
945 "Reject addition of NULL event class to a stream class");
946 ok(bt_ctf_stream_class_add_event_class(stream_class
,
947 event_class
) == 0, "Add an event class to stream class");
949 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
950 "bt_ctf_event_class_get_stream_class handles NULL correctly");
951 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
952 ok(ret_stream_class
== stream_class
,
953 "bt_ctf_event_class_get_stream_class returns the correct stream class");
954 bt_put(ret_stream_class
);
956 ok(bt_ctf_event_class_get_field_count(NULL
) < 0,
957 "bt_ctf_event_class_get_field_count handles NULL correctly");
958 ok(bt_ctf_event_class_get_field_count(event_class
) == 3,
959 "bt_ctf_event_class_get_field_count returns a correct value");
961 ok(bt_ctf_event_class_get_field(NULL
, &ret_string
,
962 &ret_field_type
, 0) < 0,
963 "bt_ctf_event_class_get_field handles a NULL event class correctly");
964 ok(bt_ctf_event_class_get_field(event_class
, NULL
,
965 &ret_field_type
, 0) == 0,
966 "bt_ctf_event_class_get_field handles a NULL field name correctly");
967 bt_put(ret_field_type
);
968 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
970 "bt_ctf_event_class_get_field handles a NULL field type correctly");
971 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
972 &ret_field_type
, 42) < 0,
973 "bt_ctf_event_class_get_field handles an invalid index correctly");
974 ok(bt_ctf_event_class_get_field(event_class
, &ret_string
,
975 &ret_field_type
, 0) == 0,
976 "bt_ctf_event_class_get_field returns a field");
977 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
978 "bt_ctf_event_class_get_field returns a correct field type");
979 bt_put(ret_field_type
);
980 ok(!strcmp(ret_string
, "uint_35"),
981 "bt_ctf_event_class_get_field returns a correct field name");
982 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
983 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
984 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
985 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
986 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
987 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
988 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
989 "complex_structure");
990 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
991 "bt_ctf_event_class_get_field_by_name returns a correct field type");
992 bt_put(ret_field_type
);
994 event
= bt_ctf_event_create(event_class
);
995 ok(event
, "Instanciate a complex event");
997 ok(bt_ctf_event_get_class(NULL
) == NULL
,
998 "bt_ctf_event_get_class handles NULL correctly");
999 ret_event_class
= bt_ctf_event_get_class(event
);
1000 ok(ret_event_class
== event_class
,
1001 "bt_ctf_event_get_class returns the correct event class");
1002 bt_put(ret_event_class
);
1004 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
1005 if (!uint_35_field
) {
1006 printf("uint_35_field is NULL\n");
1009 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
1010 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
1011 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
1012 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1013 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
1014 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1015 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
1016 &ret_unsigned_int
) == 0,
1017 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1018 ok(ret_unsigned_int
== 0x0DDF00D,
1019 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1020 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
1021 &ret_signed_int
) < 0,
1022 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1023 bt_put(uint_35_field
);
1025 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
1026 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
1027 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
1028 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1029 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
1030 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1031 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
1032 &ret_signed_int
) == 0,
1033 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1034 ok(ret_signed_int
== -12345,
1035 "bt_ctf_field_signed_integer_get_value returns the correct value");
1036 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
1037 &ret_unsigned_int
) < 0,
1038 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1039 bt_put(int_16_field
);
1041 complex_structure_field
= bt_ctf_event_get_payload(event
,
1042 "complex_structure");
1044 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
1045 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1046 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
1047 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1048 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
1049 complex_structure_field
, 3);
1050 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
1051 bt_put(inner_structure_field
);
1052 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
1053 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1054 bt_put(ret_field_type
);
1056 inner_structure_field
= bt_ctf_field_structure_get_field(
1057 complex_structure_field
, "inner_structure");
1058 a_string_field
= bt_ctf_field_structure_get_field(
1059 complex_structure_field
, "a_string");
1060 enum_variant_field
= bt_ctf_field_structure_get_field(
1061 complex_structure_field
, "variant_selector");
1062 variant_field
= bt_ctf_field_structure_get_field(
1063 complex_structure_field
, "variant_value");
1064 uint_35_field
= bt_ctf_field_structure_get_field(
1065 inner_structure_field
, "seq_len");
1066 a_sequence_field
= bt_ctf_field_structure_get_field(
1067 inner_structure_field
, "a_sequence");
1068 an_array_field
= bt_ctf_field_structure_get_field(
1069 inner_structure_field
, "an_array");
1071 enum_container_field
= bt_ctf_field_enumeration_get_container(
1072 enum_variant_field
);
1073 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
1074 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
1075 enum_variant_field
);
1076 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
1077 bt_put(int_16_field
);
1078 ok(!bt_ctf_field_string_get_value(a_string_field
),
1079 "bt_ctf_field_string_get_value returns NULL on an unset field");
1080 bt_ctf_field_string_set_value(a_string_field
,
1082 ok(!bt_ctf_field_string_get_value(NULL
),
1083 "bt_ctf_field_string_get_value correctly handles NULL");
1084 ok(bt_ctf_field_string_append(NULL
, "yeah"),
1085 "bt_ctf_field_string_append correctly handles a NULL string field");
1086 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
1087 "bt_ctf_field_string_append correctly handles a NULL string value");
1088 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
1089 "bt_ctf_field_string_append succeeds");
1090 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
1091 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1092 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
1093 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1094 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
1095 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1096 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
1097 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1098 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
1099 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1100 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
1101 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1103 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1104 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1105 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1106 "bt_ctf_field_string_get_value returns a correct value");
1107 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1108 SEQUENCE_TEST_LENGTH
);
1110 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1111 enum_container_field
) == NULL
,
1112 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1113 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1115 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1116 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1117 variant_type
, enum_variant_field
);
1118 ok(ret_field_type
== int_16_type
,
1119 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1121 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1122 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1123 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1124 uint_35_field
) == 0, "Set a sequence field's length");
1125 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1126 ok(ret_field
== uint_35_field
,
1127 "bt_ctf_field_sequence_get_length returns the correct length field");
1128 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1129 "bt_ctf_field_sequence_get_length properly handles NULL");
1131 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1132 int_16_field
= bt_ctf_field_sequence_get_field(
1133 a_sequence_field
, i
);
1134 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1135 bt_put(int_16_field
);
1138 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1139 int_16_field
= bt_ctf_field_array_get_field(
1141 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1142 bt_put(int_16_field
);
1145 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1146 assert(stream_event_ctx_field
);
1147 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1148 stream_event_ctx_field
, "common_event_context");
1149 BT_PUT(stream_event_ctx_field
);
1150 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1151 BT_PUT(stream_event_ctx_int_field
);
1153 bt_ctf_clock_set_time(clock
, ++current_time
);
1154 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1155 "Append a complex event to a stream");
1158 * Populate the custom packet context field with a dummy value
1159 * otherwise flush will fail.
1161 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1162 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1163 "custom_packet_context_field");
1164 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1166 ok(bt_ctf_stream_flush(stream
) == 0,
1167 "Flush a stream containing a complex event");
1169 bt_put(uint_35_field
);
1170 bt_put(a_string_field
);
1171 bt_put(inner_structure_field
);
1172 bt_put(complex_structure_field
);
1173 bt_put(a_sequence_field
);
1174 bt_put(an_array_field
);
1175 bt_put(enum_variant_field
);
1176 bt_put(enum_container_field
);
1177 bt_put(variant_field
);
1179 bt_put(packet_context_field
);
1180 bt_put(packet_context
);
1181 bt_put(uint_35_type
);
1182 bt_put(int_16_type
);
1183 bt_put(string_type
);
1184 bt_put(sequence_type
);
1186 bt_put(inner_structure_type
);
1187 bt_put(complex_structure_type
);
1188 bt_put(uint_3_type
);
1189 bt_put(enum_variant_type
);
1190 bt_put(variant_type
);
1191 bt_put(ret_field_type
);
1192 bt_put(event_class
);
1197 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1198 struct bt_ctf_field_type
*expected_type
, const char *name
)
1200 struct bt_ctf_field_type
*copy_type
;
1202 copy_type
= bt_ctf_field_get_type(field
);
1203 ok(copy_type
== expected_type
,
1204 "bt_ctf_field_copy does not copy the type (%s)", name
);
1209 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1210 struct bt_ctf_field
*field_b
, const char *name
)
1212 ok(field_a
!= field_b
,
1213 "bt_ctf_field_copy creates different pointers (%s)", name
);
1217 void field_copy_tests()
1219 struct bt_ctf_field_type
*len_type
= NULL
;
1220 struct bt_ctf_field_type
*fp_type
= NULL
;
1221 struct bt_ctf_field_type
*s_type
= NULL
;
1222 struct bt_ctf_field_type
*e_int_type
= NULL
;
1223 struct bt_ctf_field_type
*e_type
= NULL
;
1224 struct bt_ctf_field_type
*v_type
= NULL
;
1225 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1226 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1227 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1228 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1229 struct bt_ctf_field_type
*strct_type
= NULL
;
1230 struct bt_ctf_field
*len
= NULL
;
1231 struct bt_ctf_field
*fp
= NULL
;
1232 struct bt_ctf_field
*s
= NULL
;
1233 struct bt_ctf_field
*e_int
= NULL
;
1234 struct bt_ctf_field
*e
= NULL
;
1235 struct bt_ctf_field
*v
= NULL
;
1236 struct bt_ctf_field
*v_selected
= NULL
;
1237 struct bt_ctf_field
*v_selected_cur
= NULL
;
1238 struct bt_ctf_field
*v_selected_0
= NULL
;
1239 struct bt_ctf_field
*v_selected_1
= NULL
;
1240 struct bt_ctf_field
*v_selected_2
= NULL
;
1241 struct bt_ctf_field
*v_selected_3
= NULL
;
1242 struct bt_ctf_field
*v_selected_4
= NULL
;
1243 struct bt_ctf_field
*v_selected_5
= NULL
;
1244 struct bt_ctf_field
*v_selected_6
= NULL
;
1245 struct bt_ctf_field
*a
= NULL
;
1246 struct bt_ctf_field
*a_0
= NULL
;
1247 struct bt_ctf_field
*a_1
= NULL
;
1248 struct bt_ctf_field
*a_2
= NULL
;
1249 struct bt_ctf_field
*a_3
= NULL
;
1250 struct bt_ctf_field
*a_4
= NULL
;
1251 struct bt_ctf_field
*strct
= NULL
;
1252 struct bt_ctf_field
*len_copy
= NULL
;
1253 struct bt_ctf_field
*fp_copy
= NULL
;
1254 struct bt_ctf_field
*s_copy
= NULL
;
1255 struct bt_ctf_field
*e_int_copy
= NULL
;
1256 struct bt_ctf_field
*e_copy
= NULL
;
1257 struct bt_ctf_field
*v_copy
= NULL
;
1258 struct bt_ctf_field
*v_selected_copy
= NULL
;
1259 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1260 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1261 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1262 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1263 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1264 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1265 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1266 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1267 struct bt_ctf_field
*a_copy
= NULL
;
1268 struct bt_ctf_field
*a_0_copy
= NULL
;
1269 struct bt_ctf_field
*a_1_copy
= NULL
;
1270 struct bt_ctf_field
*a_2_copy
= NULL
;
1271 struct bt_ctf_field
*a_3_copy
= NULL
;
1272 struct bt_ctf_field
*a_4_copy
= NULL
;
1273 struct bt_ctf_field
*strct_copy
= NULL
;
1274 struct bt_ctf_field_type_enumeration_mapping_iterator
*e_iter
= NULL
;
1275 uint64_t uint64_t_val
;
1276 const char *str_val
;
1280 /* create len type */
1281 len_type
= bt_ctf_field_type_integer_create(32);
1284 /* create fp type */
1285 fp_type
= bt_ctf_field_type_floating_point_create();
1289 s_type
= bt_ctf_field_type_string_create();
1292 /* create e_int type */
1293 e_int_type
= bt_ctf_field_type_integer_create(8);
1297 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1299 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1302 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1306 /* create v_label1 type */
1307 v_label1_type
= bt_ctf_field_type_string_create();
1308 assert(v_label1_type
);
1310 /* create v_label1_array type */
1311 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1312 assert(v_label1_array_type
);
1314 /* create v_label2 type */
1315 v_label2_type
= bt_ctf_field_type_integer_create(16);
1316 assert(v_label2_type
);
1318 /* create v_label2_seq type */
1319 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1321 assert(v_label2_seq_type
);
1324 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1326 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1329 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1333 /* create strct type */
1334 strct_type
= bt_ctf_field_type_structure_create();
1336 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1339 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1341 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1343 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1345 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1347 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1348 v_label1_array_type
, "a");
1352 strct
= bt_ctf_field_create(strct_type
);
1356 len
= bt_ctf_field_structure_get_field(strct
, "len");
1360 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1364 s
= bt_ctf_field_structure_get_field(strct
, "s");
1368 e
= bt_ctf_field_structure_get_field(strct
, "e");
1371 /* get e_int (underlying integer) */
1372 e_int
= bt_ctf_field_enumeration_get_container(e
);
1376 v
= bt_ctf_field_structure_get_field(strct
, "v");
1380 a
= bt_ctf_field_structure_get_field(strct
, "a");
1384 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1388 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1392 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1395 /* set e field (LABEL2) */
1396 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1400 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1402 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1403 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1404 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1405 ok(v_selected_cur
== v_selected
,
1406 "bt_ctf_field_variant_get_current_field returns the current field");
1407 bt_put(v_selected_cur
);
1409 /* set selected v field */
1410 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1412 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1413 assert(v_selected_0
);
1414 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1416 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1417 assert(v_selected_1
);
1418 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1420 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1421 assert(v_selected_2
);
1422 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1424 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1425 assert(v_selected_3
);
1426 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1428 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1429 assert(v_selected_4
);
1430 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1432 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1433 assert(v_selected_5
);
1434 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1436 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1437 assert(v_selected_6
);
1438 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1442 a_0
= bt_ctf_field_array_get_field(a
, 0);
1444 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1446 a_1
= bt_ctf_field_array_get_field(a
, 1);
1448 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1450 a_2
= bt_ctf_field_array_get_field(a
, 2);
1452 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1454 a_3
= bt_ctf_field_array_get_field(a
, 3);
1456 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1458 a_4
= bt_ctf_field_array_get_field(a
, 4);
1460 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1463 /* create copy of strct */
1464 ok(!bt_ctf_field_copy(NULL
),
1465 "bt_ctf_field_copy handles NULL correctly");
1466 strct_copy
= bt_ctf_field_copy(strct
);
1468 "bt_ctf_field_copy returns a valid pointer");
1470 /* get all copied fields */
1471 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1473 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1475 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1477 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1479 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1481 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1483 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1484 assert(v_selected_copy
);
1485 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1486 assert(v_selected_0_copy
);
1487 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1488 assert(v_selected_1_copy
);
1489 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1490 assert(v_selected_2_copy
);
1491 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1492 assert(v_selected_3_copy
);
1493 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1494 assert(v_selected_4_copy
);
1495 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1496 assert(v_selected_5_copy
);
1497 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1498 assert(v_selected_6_copy
);
1499 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1500 "sequence field copy is not too large");
1501 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1503 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1505 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1507 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1509 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1511 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1513 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1514 "array field copy is not too large");
1516 /* make sure copied fields are different pointers */
1517 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1518 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1519 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1520 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1521 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1522 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1523 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1524 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1526 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1528 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1530 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1532 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1534 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1536 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1538 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1540 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1541 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1542 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1543 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1544 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1545 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1547 /* make sure copied fields share the same types */
1548 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1549 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1550 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1551 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1552 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1553 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1554 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1556 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1558 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1560 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1562 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1564 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1566 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1568 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1570 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1571 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1572 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1573 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1574 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1575 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1577 /* validate len copy */
1578 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1580 ok(uint64_t_val
== 7,
1581 "bt_ctf_field_copy creates a valid integer field copy");
1583 /* validate fp copy */
1584 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1586 ok(double_val
== 3.14,
1587 "bt_ctf_field_copy creates a valid floating point number field copy");
1589 /* validate s copy */
1590 str_val
= bt_ctf_field_string_get_value(s_copy
);
1591 ok(str_val
&& !strcmp(str_val
, "btbt"),
1592 "bt_ctf_field_copy creates a valid string field copy");
1594 /* validate e_int copy */
1595 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1598 ok(uint64_t_val
== 23,
1599 "bt_ctf_field_copy creates a valid enum's integer field copy");
1601 /* validate e copy */
1602 e_iter
= bt_ctf_field_enumeration_get_mappings(e_copy
);
1603 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter
,
1604 &str_val
, NULL
, NULL
);
1605 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1606 "bt_ctf_field_copy creates a valid enum field copy");
1608 /* validate v_selected copy */
1609 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1610 assert(v_selected_copy_len
);
1611 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1614 ok(uint64_t_val
== 7,
1615 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1616 bt_put(v_selected_copy_len
);
1617 v_selected_copy_len
= NULL
;
1619 /* validate v_selected copy fields */
1620 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1623 ok(uint64_t_val
== 7,
1624 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1625 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1628 ok(uint64_t_val
== 6,
1629 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1630 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1633 ok(uint64_t_val
== 5,
1634 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1635 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1638 ok(uint64_t_val
== 4,
1639 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1640 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1643 ok(uint64_t_val
== 3,
1644 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1645 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1648 ok(uint64_t_val
== 2,
1649 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1650 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1653 ok(uint64_t_val
== 1,
1654 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1656 /* validate a copy fields */
1657 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1658 ok(str_val
&& !strcmp(str_val
, "a_0"),
1659 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1660 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1661 ok(str_val
&& !strcmp(str_val
, "a_1"),
1662 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1663 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1664 ok(str_val
&& !strcmp(str_val
, "a_2"),
1665 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1666 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1667 ok(str_val
&& !strcmp(str_val
, "a_3"),
1668 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1669 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1670 ok(str_val
&& !strcmp(str_val
, "a_4"),
1671 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1673 /* put everything */
1680 bt_put(v_label1_type
);
1681 bt_put(v_label1_array_type
);
1682 bt_put(v_label2_type
);
1683 bt_put(v_label2_seq_type
);
1692 bt_put(v_selected_0
);
1693 bt_put(v_selected_1
);
1694 bt_put(v_selected_2
);
1695 bt_put(v_selected_3
);
1696 bt_put(v_selected_4
);
1697 bt_put(v_selected_5
);
1698 bt_put(v_selected_6
);
1712 bt_put(v_selected_copy
);
1713 bt_put(v_selected_0_copy
);
1714 bt_put(v_selected_1_copy
);
1715 bt_put(v_selected_2_copy
);
1716 bt_put(v_selected_3_copy
);
1717 bt_put(v_selected_4_copy
);
1718 bt_put(v_selected_5_copy
);
1719 bt_put(v_selected_6_copy
);
1731 void type_field_tests()
1733 struct bt_ctf_field
*uint_12
;
1734 struct bt_ctf_field
*int_16
;
1735 struct bt_ctf_field
*string
;
1736 struct bt_ctf_field
*enumeration
;
1737 struct bt_ctf_field_type
*composite_structure_type
;
1738 struct bt_ctf_field_type
*structure_seq_type
;
1739 struct bt_ctf_field_type
*string_type
;
1740 struct bt_ctf_field_type
*sequence_type
;
1741 struct bt_ctf_field_type
*uint_8_type
;
1742 struct bt_ctf_field_type
*int_16_type
;
1743 struct bt_ctf_field_type
*uint_12_type
=
1744 bt_ctf_field_type_integer_create(12);
1745 struct bt_ctf_field_type
*enumeration_type
;
1746 struct bt_ctf_field_type
*returned_type
;
1747 const char *ret_string
;
1749 returned_type
= bt_ctf_field_get_type(NULL
);
1750 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1752 ok(uint_12_type
, "Create an unsigned integer type");
1753 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1754 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1755 "Set integer type's base as binary");
1756 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1757 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1758 "Set integer type's base as decimal");
1759 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1760 BT_CTF_INTEGER_BASE_UNKNOWN
),
1761 "Reject integer type's base set as unknown");
1762 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1763 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1764 "Set integer type's base as octal");
1765 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1766 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1767 "Set integer type's base as hexadecimal");
1768 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1769 "Reject unknown integer base value");
1770 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1771 "Set integer type signedness to signed");
1772 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1773 "Set integer type signedness to unsigned");
1774 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1775 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1776 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1777 "bt_ctf_field_type_integer_get_size returns a correct value");
1778 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1779 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1780 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1781 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1783 ok(bt_ctf_field_type_set_byte_order(NULL
,
1784 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1785 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1786 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1787 (enum bt_ctf_byte_order
) 42) < 0,
1788 "bt_ctf_field_type_set_byte_order rejects invalid values");
1789 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1790 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1791 "Set an integer's byte order to little endian");
1792 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1793 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1794 "Set an integer's byte order to big endian");
1795 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1796 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1797 "bt_ctf_field_type_get_byte_order returns a correct value");
1798 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1799 BT_CTF_BYTE_ORDER_UNKNOWN
,
1800 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1802 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1803 BT_CTF_FIELD_TYPE_ID_UNKNOWN
,
1804 "bt_ctf_field_type_get_type_id handles NULL correctly");
1805 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1806 BT_CTF_FIELD_TYPE_ID_INTEGER
,
1807 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1809 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1810 BT_CTF_INTEGER_BASE_UNKNOWN
,
1811 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1812 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1813 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1814 "bt_ctf_field_type_integer_get_base returns a correct value");
1816 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1817 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1818 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1819 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1820 (enum bt_ctf_string_encoding
) 123) < 0,
1821 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1822 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1823 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1824 "Set integer type encoding to UTF8");
1825 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1826 BT_CTF_STRING_ENCODING_UNKNOWN
,
1827 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1828 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1829 BT_CTF_STRING_ENCODING_UTF8
,
1830 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1832 int_16_type
= bt_ctf_field_type_integer_create(16);
1833 assert(int_16_type
);
1834 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1835 "Set signedness of 16 bit integer to true");
1836 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1837 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1838 uint_8_type
= bt_ctf_field_type_integer_create(8);
1840 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1841 ok(sequence_type
, "Create a sequence of int16_t type");
1842 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1843 BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
1844 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1846 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1847 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1848 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1850 ok(!strcmp(ret_string
, "seq_len"),
1851 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1852 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1853 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1854 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1856 ok(returned_type
== int_16_type
,
1857 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1858 bt_put(returned_type
);
1860 string_type
= bt_ctf_field_type_string_create();
1861 ok(string_type
, "Create a string type");
1862 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1863 BT_CTF_STRING_ENCODING_NONE
),
1864 "Reject invalid \"None\" string encoding");
1865 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1867 "Reject invalid string encoding");
1868 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1869 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1870 "Set string encoding to ASCII");
1872 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1873 BT_CTF_STRING_ENCODING_UNKNOWN
,
1874 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1875 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1876 BT_CTF_STRING_ENCODING_ASCII
,
1877 "bt_ctf_field_type_string_get_encoding returns the correct value");
1879 structure_seq_type
= bt_ctf_field_type_structure_create();
1880 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1881 BT_CTF_FIELD_TYPE_ID_STRUCT
,
1882 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1883 ok(structure_seq_type
, "Create a structure type");
1884 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1885 uint_8_type
, "seq_len") == 0,
1886 "Add a uint8_t type to a structure");
1887 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1888 sequence_type
, "a_sequence") == 0,
1889 "Add a sequence type to a structure");
1891 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1892 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1893 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1894 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1896 ok(bt_ctf_field_type_structure_get_field(NULL
,
1897 &ret_string
, &returned_type
, 1) < 0,
1898 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1899 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1900 NULL
, &returned_type
, 1) == 0,
1901 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1902 bt_put(returned_type
);
1903 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1904 &ret_string
, NULL
, 1) == 0,
1905 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1906 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1907 &ret_string
, &returned_type
, 10) < 0,
1908 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1909 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1910 &ret_string
, &returned_type
, 1) == 0,
1911 "bt_ctf_field_type_structure_get_field returns a field");
1912 ok(!strcmp(ret_string
, "a_sequence"),
1913 "bt_ctf_field_type_structure_get_field returns a correct field name");
1914 ok(returned_type
== sequence_type
,
1915 "bt_ctf_field_type_structure_get_field returns a correct field type");
1916 bt_put(returned_type
);
1918 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1919 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1920 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1921 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1922 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1923 structure_seq_type
, "a_sequence");
1924 ok(returned_type
== sequence_type
,
1925 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1926 bt_put(returned_type
);
1928 composite_structure_type
= bt_ctf_field_type_structure_create();
1929 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1930 string_type
, "a_string") == 0,
1931 "Add a string type to a structure");
1932 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1933 structure_seq_type
, "inner_structure") == 0,
1934 "Add a structure type to a structure");
1936 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1937 NULL
, "a_sequence") == NULL
,
1938 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1939 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1940 structure_seq_type
, NULL
) == NULL
,
1941 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1942 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1943 structure_seq_type
, "a_sequence");
1944 ok(returned_type
== sequence_type
,
1945 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1946 bt_put(returned_type
);
1948 int_16
= bt_ctf_field_create(int_16_type
);
1949 ok(int_16
, "Instanciate a signed 16-bit integer");
1950 uint_12
= bt_ctf_field_create(uint_12_type
);
1951 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1952 returned_type
= bt_ctf_field_get_type(int_16
);
1953 ok(returned_type
== int_16_type
,
1954 "bt_ctf_field_get_type returns the correct type");
1956 /* Can't modify types after instanciating them */
1957 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1958 BT_CTF_INTEGER_BASE_DECIMAL
),
1959 "Check an integer type' base can't be modified after instanciation");
1960 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1961 "Check an integer type's signedness can't be modified after instanciation");
1963 /* Check signed property is checked */
1964 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1965 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1966 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1967 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1969 /* Check overflows are properly tested for */
1970 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1971 "Check -32768 is allowed for a signed 16-bit integer");
1972 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1973 "Check 32767 is allowed for a signed 16-bit integer");
1974 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1975 "Check 32768 is not allowed for a signed 16-bit integer");
1976 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1977 "Check -32769 is not allowed for a signed 16-bit integer");
1978 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1979 "Check -42 is allowed for a signed 16-bit integer");
1981 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1982 "Check 4095 is allowed for an unsigned 12-bit integer");
1983 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1984 "Check 4096 is not allowed for a unsigned 12-bit integer");
1985 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1986 "Check 0 is allowed for an unsigned 12-bit integer");
1988 string
= bt_ctf_field_create(string_type
);
1989 ok(string
, "Instanciate a string field");
1990 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1991 "Set a string's value");
1993 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1994 ok(enumeration_type
,
1995 "Create an enumeration type with an unsigned 12-bit integer as container");
1996 enumeration
= bt_ctf_field_create(enumeration_type
);
1998 "Check enumeration types are validated before instantiation");
2003 bt_put(enumeration
);
2004 bt_put(composite_structure_type
);
2005 bt_put(structure_seq_type
);
2006 bt_put(string_type
);
2007 bt_put(sequence_type
);
2008 bt_put(uint_8_type
);
2009 bt_put(int_16_type
);
2010 bt_put(uint_12_type
);
2011 bt_put(enumeration_type
);
2012 bt_put(returned_type
);
2016 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
2017 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
2020 * Append enough events to force the underlying packet to be resized.
2021 * Also tests that a new event can be declared after a stream has been
2022 * instantiated and used/flushed.
2026 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
2028 struct bt_ctf_field_type
*integer_type
=
2029 bt_ctf_field_type_integer_create(17);
2030 struct bt_ctf_field_type
*string_type
=
2031 bt_ctf_field_type_string_create();
2032 struct bt_ctf_event
*event
= NULL
;
2033 struct bt_ctf_field
*ret_field
= NULL
;
2034 struct bt_ctf_field_type
*ret_field_type
= NULL
;
2035 uint64_t ret_uint64
;
2036 int events_appended
= 0;
2037 struct bt_ctf_field
*packet_context
= NULL
,
2038 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
2039 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
2040 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
2041 struct bt_ctf_field_type
*ep_type
= NULL
;
2043 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
2045 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
2047 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2048 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
2054 * bt_ctf_stream_class_add_event_class() copies the field types
2055 * of event_class, so we retrieve the new ones to create the
2056 * appropriate fields.
2058 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
2060 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2061 ep_type
, "field_1");
2062 assert(ep_field_1_type
);
2063 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
2064 ep_type
, "a_string");
2065 assert(ep_a_string_type
);
2067 event
= bt_ctf_event_create(event_class
);
2068 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2069 ret_field_type
= bt_ctf_field_get_type(ret_field
);
2070 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
2071 "bt_ctf_event_get_payload_by_index returns a correct field");
2072 bt_put(ret_field_type
);
2075 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
2076 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2077 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
2078 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2081 for (i
= 0; i
< PACKET_RESIZE_TEST_LENGTH
; i
++) {
2082 event
= bt_ctf_event_create(event_class
);
2083 struct bt_ctf_field
*integer
=
2084 bt_ctf_field_create(ep_field_1_type
);
2085 struct bt_ctf_field
*string
=
2086 bt_ctf_field_create(ep_a_string_type
);
2088 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
2089 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2090 ret
|= bt_ctf_event_set_payload(event
, "field_1",
2093 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
2094 ret
|= bt_ctf_event_set_payload(event
, "a_string",
2098 /* Populate stream event context */
2099 stream_event_context
=
2100 bt_ctf_event_get_stream_event_context(event
);
2101 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
2102 "common_event_context");
2103 BT_PUT(stream_event_context
);
2104 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2108 ret
|= bt_ctf_stream_append_event(stream
, event
);
2116 events_appended
= !!(i
== PACKET_RESIZE_TEST_LENGTH
);
2117 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2118 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2119 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2120 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2121 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2122 ok(ret
== 0 && ret_uint64
== 0,
2123 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2124 bt_ctf_stream_append_discarded_events(stream
, 1000);
2125 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2126 ok(ret
== 0 && ret_uint64
== 1000,
2127 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2130 ok(events_appended
, "Append 100 000 events to a stream");
2133 * Populate the custom packet context field with a dummy value
2134 * otherwise flush will fail.
2136 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2137 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2138 "custom_packet_context_field");
2139 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2141 ok(bt_ctf_stream_flush(stream
) == 0,
2142 "Flush a stream that forces a packet resize");
2143 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2144 ok(ret
== 0 && ret_uint64
== 1000,
2145 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2146 bt_put(integer_type
);
2147 bt_put(string_type
);
2148 bt_put(packet_context
);
2149 bt_put(packet_context_field
);
2150 bt_put(stream_event_context
);
2151 bt_put(event_class
);
2152 bt_put(ep_field_1_type
);
2153 bt_put(ep_a_string_type
);
2158 void test_empty_stream(struct bt_ctf_writer
*writer
)
2161 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2162 struct bt_ctf_stream_class
*stream_class
= NULL
;
2163 struct bt_ctf_stream
*stream
= NULL
;
2165 trace
= bt_ctf_writer_get_trace(writer
);
2167 diag("Failed to get trace from writer");
2172 stream_class
= bt_ctf_stream_class_create("empty_stream");
2173 if (!stream_class
) {
2174 diag("Failed to create stream class");
2179 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2180 "bt_ctf_stream_class_get_trace handles NULL correctly");
2181 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2182 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2184 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2186 diag("Failed to create writer stream");
2191 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2192 ok(ret_trace
== trace
,
2193 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2196 "Created a stream class with default attributes and an empty stream");
2200 bt_put(stream_class
);
2204 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
,
2205 struct bt_ctf_clock
*clock
)
2208 struct bt_ctf_stream_class
*stream_class
= NULL
;
2209 struct bt_ctf_stream
*stream
= NULL
;
2210 struct bt_ctf_field_type
*integer_type
= NULL
,
2211 *sequence_type
= NULL
, *event_header_type
= NULL
;
2212 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2213 *event_header
= NULL
, *packet_header
= NULL
;
2214 struct bt_ctf_event_class
*event_class
= NULL
;
2215 struct bt_ctf_event
*event
= NULL
;
2217 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2218 if (!stream_class
) {
2219 fail("Failed to create stream class");
2223 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2225 fail("Failed to set stream class clock");
2230 * Customize event header to add an "seq_len" integer member
2231 * which will be used as the length of a sequence in an event of this
2234 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2236 if (!event_header_type
) {
2237 fail("Failed to get event header type");
2241 integer_type
= bt_ctf_field_type_integer_create(13);
2242 if (!integer_type
) {
2243 fail("Failed to create length integer type");
2247 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2248 integer_type
, "seq_len");
2250 fail("Failed to add a new field to stream event header");
2254 event_class
= bt_ctf_event_class_create("sequence_event");
2256 fail("Failed to create event class");
2261 * This event's payload will contain a sequence which references
2262 * stream.event.header.seq_len as its length field.
2264 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2265 "stream.event.header.seq_len");
2266 if (!sequence_type
) {
2267 fail("Failed to create a sequence");
2271 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2274 fail("Failed to add a sequence to an event class");
2278 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2280 fail("Failed to add event class to stream class");
2284 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2286 fail("Failed to create stream")
2291 * We have defined a custom packet header field. We have to populate it
2294 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2295 if (!packet_header
) {
2296 fail("Failed to get stream packet header");
2300 integer
= bt_ctf_field_structure_get_field(packet_header
,
2301 "custom_trace_packet_header_field");
2303 fail("Failed to retrieve custom_trace_packet_header_field");
2307 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2309 fail("Failed to set custom_trace_packet_header_field value");
2314 event
= bt_ctf_event_create(event_class
);
2316 fail("Failed to create event");
2320 event_header
= bt_ctf_event_get_header(event
);
2321 if (!event_header
) {
2322 fail("Failed to get event header");
2326 integer
= bt_ctf_field_structure_get_field(event_header
,
2329 fail("Failed to get seq_len field from event header");
2333 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2335 fail("Failed to set seq_len value in event header");
2339 /* Populate both sequence integer fields */
2340 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2342 fail("Failed to retrieve sequence from event");
2346 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2348 fail("Failed to set sequence length");
2353 for (i
= 0; i
< 2; i
++) {
2354 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2356 fail("Failed to retrieve sequence element");
2360 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2362 fail("Failed to set sequence element value");
2370 ret
= bt_ctf_stream_append_event(stream
, event
);
2372 fail("Failed to append event to stream");
2376 ret
= bt_ctf_stream_flush(stream
);
2378 fail("Failed to flush custom_event_header stream");
2382 bt_put(stream_class
);
2383 bt_put(event_class
);
2387 bt_put(event_header
);
2388 bt_put(packet_header
);
2389 bt_put(sequence_type
);
2390 bt_put(integer_type
);
2391 bt_put(event_header_type
);
2395 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
,
2396 struct bt_ctf_clock
*clock
)
2399 struct bt_ctf_stream_class
*stream_class
= NULL
;
2400 struct bt_ctf_stream
*stream
= NULL
,
2402 struct bt_ctf_event_class
*event_class
= NULL
;
2403 struct bt_ctf_event
*event
= NULL
;
2404 struct bt_ctf_field_type
*integer_type
= NULL
;
2405 struct bt_ctf_field
*integer
= NULL
;
2407 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2408 if (!stream_class
) {
2409 diag("Failed to create stream class");
2414 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2416 diag("Failed to set stream class clock");
2420 event_class
= bt_ctf_event_class_create("some_event_class_name");
2421 integer_type
= bt_ctf_field_type_integer_create(32);
2422 if (!integer_type
) {
2423 diag("Failed to create integer field type");
2428 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2431 diag("Failed to add field to event class");
2435 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2438 diag("Failed to add event class to stream class");
2441 event
= bt_ctf_event_create(event_class
);
2443 diag("Failed to create event");
2448 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2450 diag("Failed to get integer field payload from event");
2455 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2457 diag("Failed to set integer field value");
2461 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2463 diag("Failed to create writer stream");
2468 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2469 "bt_ctf_event_get_stream handles NULL correctly");
2470 ok(bt_ctf_event_get_stream(event
) == NULL
,
2471 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2473 ret
= bt_ctf_stream_append_event(stream
, event
);
2475 diag("Failed to append event to stream");
2479 ret_stream
= bt_ctf_event_get_stream(event
);
2480 ok(ret_stream
== stream
,
2481 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2484 "Create an event before instanciating its associated stream");
2487 bt_put(stream_class
);
2488 bt_put(event_class
);
2490 bt_put(integer_type
);
2495 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2497 struct bt_ctf_event_class
*event_class
;
2499 event_class
= bt_ctf_event_class_create("Simple Event");
2500 assert(event_class
);
2501 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2502 "two event classes with the same name cannot cohabit within the same stream class");
2503 bt_put(event_class
);
2505 event_class
= bt_ctf_event_class_create("different name, ok");
2506 assert(event_class
);
2507 assert(!bt_ctf_event_class_set_id(event_class
, 11));
2508 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2509 "two event classes with the same ID cannot cohabit within the same stream class");
2510 bt_put(event_class
);
2514 struct bt_ctf_event_class
*create_minimal_event_class(void)
2516 struct bt_ctf_event_class
*ec
= NULL
;
2517 struct bt_ctf_field_type
*int_ft
= NULL
;
2520 int_ft
= bt_ctf_field_type_integer_create(23);
2522 ec
= bt_ctf_event_class_create("minimal");
2524 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2532 void test_create_writer_vs_non_writer_mode(void)
2535 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2536 const char *writer_stream_name
= "writer stream instance";
2537 struct bt_ctf_writer
*writer
= NULL
;
2538 struct bt_ctf_trace
*writer_trace
= NULL
;
2539 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2540 struct bt_ctf_stream
*writer_stream
= NULL
;
2541 struct bt_ctf_stream
*writer_stream2
= NULL
;
2542 struct bt_ctf_stream
*packet_stream
= NULL
;
2543 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2544 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2545 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2546 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2547 struct bt_ctf_event_class
*writer_ec
= NULL
;
2548 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2549 struct bt_ctf_event
*event
= NULL
;
2550 struct bt_ctf_event
*event2
= NULL
;
2551 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2552 struct bt_ctf_field
*int_field
= NULL
;
2553 struct bt_ctf_clock
*writer_clock
= NULL
;
2554 struct bt_ctf_clock_class
*non_writer_clock_class
= NULL
;
2555 struct bt_ctf_packet
*packet
= NULL
;
2556 struct bt_ctf_packet
*packet2
= NULL
;
2558 if (!bt_mkdtemp(trace_path
)) {
2562 /* Create empty structure field type (event header) */
2563 empty_struct_ft
= bt_ctf_field_type_structure_create();
2564 assert(empty_struct_ft
);
2566 /* Create writer, writer stream class, stream, and clock */
2567 writer
= bt_ctf_writer_create(trace_path
);
2569 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2571 writer_trace
= bt_ctf_writer_get_trace(writer
);
2572 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2573 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2575 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2578 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2580 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2581 assert(writer_stream
);
2582 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2583 "bt_ctf_stream_get_name() returns the stream's name");
2584 writer_clock
= bt_ctf_clock_create("writer_clock");
2585 assert(writer_clock
);
2586 ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
2589 /* Create non-writer trace, stream class, stream, and clock */
2590 non_writer_trace
= bt_ctf_trace_create();
2591 assert(non_writer_trace
);
2592 ret
= bt_ctf_trace_set_native_byte_order(non_writer_trace
,
2593 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2595 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2596 assert(non_writer_sc
);
2597 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2600 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2602 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2603 assert(non_writer_stream
);
2604 non_writer_clock_class
=
2605 bt_ctf_clock_class_create("non_writer_clock_class");
2606 assert(non_writer_clock_class
);
2607 ret
= bt_ctf_trace_add_clock_class(non_writer_trace
,
2608 non_writer_clock_class
);
2611 /* Create event class and event */
2612 writer_ec
= create_minimal_event_class();
2614 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2616 event
= bt_ctf_event_create(writer_ec
);
2618 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2620 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2623 * Verify non-writer stream: it should be impossible to append
2626 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2627 "bt_ctf_stream_append_event() fails with a non-writer stream");
2630 * Verify writer stream: it should be possible to append an
2633 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2634 "bt_ctf_stream_append_event() succeeds with a writer stream");
2637 * It should be possible to create a packet from a non-writer
2638 * stream, but not from a writer stream.
2640 packet
= bt_ctf_packet_create(writer_stream
);
2641 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2642 packet
= bt_ctf_packet_create(non_writer_stream
);
2643 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2644 packet_stream
= bt_ctf_packet_get_stream(packet
);
2645 ok(packet_stream
== non_writer_stream
,
2646 "bt_ctf_packet_get_stream() returns the correct stream");
2649 * It should not be possible to append an event associated to
2650 * a stream to a different stream.
2652 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2653 assert(writer_stream2
);
2654 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2655 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2658 * It should not be possible to set the packet of an event
2659 * associated to a given stream to a packet associated with
2660 * a different stream.
2662 ok(bt_ctf_event_set_packet(event
, packet
),
2663 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2666 * It should be possible to set the packet of a fresh event, as
2667 * long as the originating stream classes are the same.
2669 event2
= bt_ctf_event_create(writer_ec
);
2671 ok(bt_ctf_event_set_packet(event2
, packet
),
2672 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2673 non_writer_ec
= create_minimal_event_class();
2674 assert(non_writer_ec
);
2675 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2678 event2
= bt_ctf_event_create(non_writer_ec
);
2680 ok(!bt_ctf_event_set_packet(event2
, packet
),
2681 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2684 * It should be possible to set a packet created from the same
2685 * stream to an event with an existing packet.
2687 packet2
= bt_ctf_packet_create(non_writer_stream
);
2689 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2690 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2694 * It should not be possible to set a packet created from a
2695 * different stream to an event with an existing packet.
2697 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2698 assert(non_writer_stream2
);
2699 packet2
= bt_ctf_packet_create(non_writer_stream
);
2701 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2702 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2705 bt_put(writer_trace
);
2707 bt_put(writer_stream
);
2708 bt_put(writer_stream2
);
2709 bt_put(non_writer_trace
);
2710 bt_put(non_writer_sc
);
2711 bt_put(non_writer_stream
);
2712 bt_put(non_writer_stream2
);
2713 bt_put(packet_stream
);
2715 bt_put(non_writer_ec
);
2719 bt_put(empty_struct_ft
);
2720 bt_put(writer_clock
);
2721 bt_put(non_writer_clock_class
);
2724 recursive_rmdir(trace_path
);
2728 void test_clock_utils(void)
2731 struct bt_ctf_clock
*clock
= NULL
;
2733 clock
= bt_ctf_clock_create("water");
2735 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2737 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2739 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2741 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2747 void test_set_clock_non_writer_stream_class(void)
2749 struct bt_ctf_clock
*clock
;
2750 struct bt_ctf_trace
*trace
;
2751 struct bt_ctf_stream_class
*sc
;
2754 clock
= bt_ctf_clock_create("the_clock");
2757 trace
= bt_ctf_trace_create();
2760 sc
= bt_ctf_stream_class_create(NULL
);
2763 ret
= bt_ctf_stream_class_set_clock(sc
, clock
);
2766 ret
= bt_ctf_trace_add_stream_class(trace
, sc
);
2768 "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock");
2775 int main(int argc
, char **argv
)
2777 char trace_path
[] = "/tmp/ctfwriter_XXXXXX";
2778 char metadata_path
[sizeof(trace_path
) + 9];
2779 const char *clock_name
= "test_clock";
2780 const char *clock_description
= "This is a test clock";
2781 const char *returned_clock_name
;
2782 const char *returned_clock_description
;
2783 const uint64_t frequency
= 1123456789;
2784 const int64_t offset_s
= 1351530929945824323;
2785 const int64_t offset
= 1234567;
2786 int64_t get_offset_s
,
2788 const uint64_t precision
= 10;
2789 const int is_absolute
= 0xFF;
2790 char *metadata_string
;
2791 struct bt_ctf_writer
*writer
;
2792 struct utsname name
;
2793 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2794 struct bt_ctf_clock
*clock
, *ret_clock
;
2795 struct bt_ctf_clock_class
*ret_clock_class
;
2796 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2797 struct bt_ctf_stream
*stream1
;
2798 const char *ret_string
;
2799 const unsigned char *ret_uuid
;
2800 unsigned char tmp_uuid
[16] = { 0 };
2801 struct bt_ctf_field_type
*packet_context_type
,
2802 *packet_context_field_type
,
2803 *packet_header_type
,
2804 *packet_header_field_type
,
2806 *stream_event_context_type
,
2808 *event_header_field_type
;
2809 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2810 struct bt_ctf_trace
*trace
;
2812 int64_t ret_int64_t
;
2813 struct bt_value
*obj
;
2816 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
2820 plan_tests(NR_TESTS
);
2822 if (!bt_mkdtemp(trace_path
)) {
2826 strcpy(metadata_path
, trace_path
);
2827 strcat(metadata_path
+ sizeof(trace_path
) - 1, "/metadata");
2829 writer
= bt_ctf_writer_create(trace_path
);
2830 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2832 ok(!bt_ctf_writer_get_trace(NULL
),
2833 "bt_ctf_writer_get_trace correctly handles NULL");
2834 trace
= bt_ctf_writer_get_trace(writer
);
2836 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2837 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2838 "Set a trace's byte order to big endian");
2839 ok(bt_ctf_trace_get_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2840 "bt_ctf_trace_get_byte_order returns a correct endianness");
2842 /* Add environment context to the trace */
2843 ret
= gethostname(hostname
, sizeof(hostname
));
2847 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2848 "Add host (%s) environment field to writer instance",
2850 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2852 "bt_ctf_writer_add_environment_field error with NULL writer");
2853 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2855 "bt_ctf_writer_add_environment_field error with NULL field name");
2856 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2858 "bt_ctf_writer_add_environment_field error with NULL field value");
2860 /* Test bt_ctf_trace_set_environment_field with an integer object */
2861 obj
= bt_value_integer_create_init(23);
2863 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2864 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2865 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2866 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2867 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2868 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2869 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2870 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2873 /* Test bt_ctf_trace_set_environment_field with a string object */
2874 obj
= bt_value_string_create_init("the value");
2876 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2877 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2880 /* Test bt_ctf_trace_set_environment_field_integer */
2881 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2883 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2884 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2885 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2886 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2888 "bt_ctf_trace_set_environment_field_integer succeeds");
2890 /* Test bt_ctf_trace_set_environment_field_string */
2891 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2893 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2894 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2895 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2896 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2898 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2899 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2901 "bt_ctf_trace_set_environment_field_string succeeds");
2903 /* Test bt_ctf_trace_get_environment_field_count */
2904 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2905 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2906 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2907 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2909 /* Test bt_ctf_trace_get_environment_field_name */
2910 ok(bt_ctf_trace_get_environment_field_name(NULL
, 0) == NULL
,
2911 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2912 ok(bt_ctf_trace_get_environment_field_name(trace
, -1) == NULL
,
2913 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2914 ok(bt_ctf_trace_get_environment_field_name(trace
, 5) == NULL
,
2915 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2916 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 0);
2917 ok(ret_string
&& !strcmp(ret_string
, "host"),
2918 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2919 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 1);
2920 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2921 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2922 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 2);
2923 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2924 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2925 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 3);
2926 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2927 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2928 ret_string
= bt_ctf_trace_get_environment_field_name(trace
, 4);
2929 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
2930 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2932 /* Test bt_ctf_trace_get_environment_field_value */
2933 ok(bt_ctf_trace_get_environment_field_value(NULL
, 0) == NULL
,
2934 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2935 ok(bt_ctf_trace_get_environment_field_value(trace
, -1) == NULL
,
2936 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2937 ok(bt_ctf_trace_get_environment_field_value(trace
, 5) == NULL
,
2938 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2939 obj
= bt_ctf_trace_get_environment_field_value(trace
, 1);
2940 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2941 ok(!ret
&& ret_int64_t
== 23,
2942 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2944 obj
= bt_ctf_trace_get_environment_field_value(trace
, 2);
2945 ret
= bt_value_string_get(obj
, &ret_string
);
2946 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
2947 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2950 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2951 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
2953 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2954 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
2955 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2956 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
2957 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2958 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
2960 ret
= bt_value_string_get(obj
, &ret_string
);
2961 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
2962 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2965 /* Test environment field replacement */
2966 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2968 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2969 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2970 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2971 obj
= bt_ctf_trace_get_environment_field_value(trace
, 3);
2972 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
2973 ok(!ret
&& ret_int64_t
== 654321,
2974 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2977 /* On Solaris, uname() can return any positive value on success */
2978 if (uname(&name
) < 0) {
2983 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
2984 == 0, "Add sysname (%s) environment field to writer instance",
2986 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
2987 name
.nodename
) == 0,
2988 "Add nodename (%s) environment field to writer instance",
2990 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
2991 == 0, "Add release (%s) environment field to writer instance",
2993 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
2994 == 0, "Add version (%s) environment field to writer instance",
2996 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
2997 == 0, "Add machine (%s) environment field to writer istance",
3000 /* Define a clock and add it to the trace */
3001 ok(bt_ctf_clock_create("signed") == NULL
,
3002 "Illegal clock name rejected");
3003 clock
= bt_ctf_clock_create(clock_name
);
3004 ok(clock
, "Clock created sucessfully");
3005 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3006 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3007 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3008 "Returned clock name is valid");
3010 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3011 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3012 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3013 "Clock description set successfully");
3015 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3016 ok(returned_clock_description
,
3017 "bt_ctf_clock_get_description returns a description.");
3018 ok(returned_clock_description
?
3019 !strcmp(returned_clock_description
, clock_description
) : 0,
3020 "Returned clock description is valid");
3022 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3023 "bt_ctf_clock_get_frequency returns the correct default frequency");
3024 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3025 "Set clock frequency");
3026 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3027 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3029 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3030 "bt_ctf_clock_get_offset_s succeeds");
3031 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3032 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3033 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3034 "Set clock offset (seconds)");
3035 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3036 "bt_ctf_clock_get_offset_s succeeds");
3037 ok(get_offset_s
== offset_s
,
3038 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3040 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3041 "bt_ctf_clock_get_offset succeeds");
3042 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3043 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3044 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3045 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3046 "bt_ctf_clock_get_offset succeeds");
3047 ok(get_offset
== offset
,
3048 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3050 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3051 "bt_ctf_clock_get_precision returns the correct default precision");
3052 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3053 "Set clock precision");
3054 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3055 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3057 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3058 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3059 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3060 "Set clock absolute property");
3061 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3062 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3064 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3067 ok(!bt_ctf_clock_get_name(NULL
),
3068 "bt_ctf_clock_get_name correctly handles NULL");
3069 ok(!bt_ctf_clock_get_description(NULL
),
3070 "bt_ctf_clock_get_description correctly handles NULL");
3071 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3072 "bt_ctf_clock_get_frequency correctly handles NULL");
3073 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3074 "bt_ctf_clock_get_precision correctly handles NULL");
3075 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3076 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3077 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3078 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3079 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3080 "bt_ctf_clock_get_offset correctly handles NULL clock");
3081 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3082 "bt_ctf_clock_get_offset correctly handles NULL output");
3083 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3084 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3086 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3087 "bt_ctf_clock_set_description correctly handles NULL clock");
3088 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3089 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3090 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3091 "bt_ctf_clock_get_precision correctly handles NULL clock");
3092 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3093 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3094 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3095 "bt_ctf_clock_set_offset correctly handles NULL clock");
3096 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3097 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3098 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3099 "bt_ctf_clock_set_time correctly handles NULL clock");
3100 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3101 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3102 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3104 "bt_ctf_clock_get_uuid returns a UUID");
3106 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3107 /* Slightly modify UUID */
3108 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3111 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3112 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3113 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3114 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3115 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3116 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3117 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3119 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3120 ok(uuid_match(ret_uuid
, tmp_uuid
),
3121 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3123 /* Define a stream class */
3124 stream_class
= bt_ctf_stream_class_create("test_stream");
3126 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3127 "bt_ctf_stream_class_get_name handles NULL correctly");
3128 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3129 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3130 "bt_ctf_stream_class_get_name returns a correct stream class name");
3132 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3133 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3134 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3135 "bt_ctf_stream_class_get_clock handles NULL correctly");
3137 ok(stream_class
, "Create stream class");
3138 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3139 "Set a stream class' clock");
3140 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3141 ok(ret_clock
== clock
,
3142 "bt_ctf_stream_class_get_clock returns a correct clock");
3145 /* Test the event fields and event types APIs */
3148 /* Test fields copying */
3151 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3152 "bt_ctf_stream_class_get_id returns an error when no id is set");
3153 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3154 "bt_ctf_stream_class_get_id handles NULL correctly");
3155 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3156 "bt_ctf_stream_class_set_id handles NULL correctly");
3157 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3158 "Set an stream class' id");
3159 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3160 "bt_ctf_stream_class_get_id returns the correct value");
3162 /* Validate default event header fields */
3163 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3164 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3165 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3168 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3169 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3170 "Default event header type is a structure");
3171 event_header_field_type
=
3172 bt_ctf_field_type_structure_get_field_type_by_name(
3173 ret_field_type
, "id");
3174 ok(event_header_field_type
,
3175 "Default event header type contains an \"id\" field");
3176 ok(bt_ctf_field_type_get_type_id(
3177 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3178 "Default event header \"id\" field is an integer");
3179 bt_put(event_header_field_type
);
3180 event_header_field_type
=
3181 bt_ctf_field_type_structure_get_field_type_by_name(
3182 ret_field_type
, "timestamp");
3183 ok(event_header_field_type
,
3184 "Default event header type contains a \"timestamp\" field");
3185 ok(bt_ctf_field_type_get_type_id(
3186 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3187 "Default event header \"timestamp\" field is an integer");
3188 bt_put(event_header_field_type
);
3189 bt_put(ret_field_type
);
3191 /* Add a custom trace packet header field */
3192 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3193 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3194 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3195 ok(packet_header_type
,
3196 "bt_ctf_trace_get_packet_header_type returns a packet header");
3197 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3198 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3199 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3200 packet_header_type
, "magic");
3201 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3202 bt_put(ret_field_type
);
3203 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3204 packet_header_type
, "uuid");
3205 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3206 bt_put(ret_field_type
);
3207 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3208 packet_header_type
, "stream_id");
3209 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3210 bt_put(ret_field_type
);
3212 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3213 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3214 packet_header_field_type
, "custom_trace_packet_header_field"),
3215 "Added a custom trace packet header field successfully");
3217 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3218 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3219 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3220 "Set a trace packet_header_type successfully");
3222 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3223 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3225 /* Add a custom field to the stream class' packet context */
3226 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3227 ok(packet_context_type
,
3228 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3229 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3230 "Packet context is a structure");
3232 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3233 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3235 integer_type
= bt_ctf_field_type_integer_create(32);
3236 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3238 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3239 /* Create a "uint5_t" equivalent custom packet context field */
3240 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3242 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3243 packet_context_field_type
, "custom_packet_context_field");
3244 ok(ret
== 0, "Packet context field added successfully");
3246 /* Define a stream event context containing a my_integer field. */
3247 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3248 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3249 ok(bt_ctf_stream_class_get_event_context_type(
3250 stream_class
) == NULL
,
3251 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3252 stream_event_context_type
= bt_ctf_field_type_structure_create();
3253 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3254 integer_type
, "common_event_context");
3256 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3257 stream_event_context_type
) < 0,
3258 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3259 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3261 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3263 ok(bt_ctf_stream_class_set_event_context_type(
3264 stream_class
, stream_event_context_type
) == 0,
3265 "Set a new stream event context type");
3266 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3268 ok(ret_field_type
== stream_event_context_type
,
3269 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3270 bt_put(ret_field_type
);
3272 /* Instantiate a stream and append events */
3273 ret
= bt_ctf_writer_add_clock(writer
, clock
);
3275 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3276 ok(stream1
, "Instanciate a stream class from writer");
3279 * Creating a stream through a writer adds the given stream
3280 * class to the writer's trace, thus registering the stream
3281 * class's clock to the trace.
3283 ok(bt_ctf_trace_get_clock_class_count(NULL
) < 0,
3284 "bt_ctf_trace_get_clock_class_count correctly handles NULL");
3285 ok(bt_ctf_trace_get_clock_class_count(trace
) == 1,
3286 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
3287 ok(!bt_ctf_trace_get_clock_class(NULL
, 0),
3288 "bt_ctf_trace_get_clock_class correctly handles NULL");
3289 ok(!bt_ctf_trace_get_clock_class(trace
, -1),
3290 "bt_ctf_trace_get_clock_class correctly handles negative indexes");
3291 ok(!bt_ctf_trace_get_clock_class(trace
, 1),
3292 "bt_ctf_trace_get_clock_class correctly handles out of bound accesses");
3293 ret_clock_class
= bt_ctf_trace_get_clock_class(trace
, 0);
3294 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3295 bt_ctf_clock_get_name(clock
)) == 0,
3296 "bt_ctf_trace_get_clock_class returns the right clock instance");
3297 bt_put(ret_clock_class
);
3298 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, NULL
),
3299 "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)");
3300 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, clock_name
),
3301 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3302 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, NULL
),
3303 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3304 ret_clock_class
= bt_ctf_trace_get_clock_class_by_name(trace
, clock_name
);
3305 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3306 bt_ctf_clock_get_name(clock
)) == 0,
3307 "bt_ctf_trace_get_clock_class returns the right clock instance");
3308 bt_put(ret_clock_class
);
3309 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, "random"),
3310 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3312 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3313 "bt_ctf_stream_get_class correctly handles NULL");
3314 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3315 ok(ret_stream_class
,
3316 "bt_ctf_stream_get_class returns a stream class");
3317 ok(ret_stream_class
== stream_class
,
3318 "Returned stream class is of the correct type");
3321 * Packet header, packet context, event header, and stream
3322 * event context types were copied for the resolving
3325 BT_PUT(packet_header_type
);
3326 BT_PUT(packet_context_type
);
3327 BT_PUT(stream_event_context_type
);
3328 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3329 assert(packet_header_type
);
3330 packet_context_type
=
3331 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3332 assert(packet_context_type
);
3333 stream_event_context_type
=
3334 bt_ctf_stream_class_get_event_context_type(stream_class
);
3335 assert(stream_event_context_type
);
3338 * Try to modify the packet context type after a stream has been
3341 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3342 packet_header_field_type
, "should_fail");
3344 "Trace packet header type can't be modified once a stream has been instanciated");
3347 * Try to modify the packet context type after a stream has been
3350 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3351 packet_context_field_type
, "should_fail");
3353 "Packet context type can't be modified once a stream has been instanciated");
3356 * Try to modify the stream event context type after a stream has been
3359 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3360 integer_type
, "should_fail");
3362 "Stream event context type can't be modified once a stream has been instanciated");
3364 /* Should fail after instanciating a stream (frozen) */
3365 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3366 "Changes to a stream class that was already instantiated fail");
3368 /* Populate the custom packet header field only once for all tests */
3369 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3370 "bt_ctf_stream_get_packet_header handles NULL correctly");
3371 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3373 "bt_ctf_stream_get_packet_header returns a packet header");
3374 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3375 ok(ret_field_type
== packet_header_type
,
3376 "Stream returns a packet header of the appropriate type");
3377 bt_put(ret_field_type
);
3378 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3379 "custom_trace_packet_header_field");
3380 ok(packet_header_field
,
3381 "Packet header structure contains a custom field with the appropriate name");
3382 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3383 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3384 "Custom packet header field is of the expected type");
3385 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3386 54321), "Set custom packet header value successfully");
3387 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3388 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3389 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3390 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3391 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3392 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3393 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3394 "Successfully set a stream's packet header");
3396 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3397 "Add environment field to writer after stream creation");
3401 test_create_writer_vs_non_writer_mode();
3403 test_set_clock_non_writer_stream_class();
3405 test_instanciate_event_before_stream(writer
, clock
);
3407 append_simple_event(stream_class
, stream1
, clock
);
3409 packet_resize_test(stream_class
, stream1
, clock
);
3411 append_complex_event(stream_class
, stream1
, clock
);
3413 append_existing_event_class(stream_class
);
3415 test_empty_stream(writer
);
3417 test_custom_event_header_stream(writer
, clock
);
3419 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3420 ok(metadata_string
, "Get metadata string");
3422 bt_ctf_writer_flush_metadata(writer
);
3425 bt_put(ret_stream_class
);
3428 bt_put(packet_context_type
);
3429 bt_put(packet_context_field_type
);
3430 bt_put(integer_type
);
3431 bt_put(stream_event_context_type
);
3432 bt_put(ret_field_type
);
3433 bt_put(packet_header_type
);
3434 bt_put(packet_header_field_type
);
3435 bt_put(packet_header
);
3436 bt_put(packet_header_field
);
3438 free(metadata_string
);
3439 bt_put(stream_class
);
3441 validate_trace(argv
[1], trace_path
);
3443 //recursive_rmdir(trace_path);