6 * Copyright 2013 - 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <babeltrace/ctf-writer/writer.h>
23 #include <babeltrace/ctf-writer/clock.h>
24 #include <babeltrace/ctf-writer/stream.h>
25 #include <babeltrace/ctf-writer/event.h>
26 #include <babeltrace/ctf-writer/event-types.h>
27 #include <babeltrace/ctf-writer/event-fields.h>
28 #include <babeltrace/ctf-writer/stream-class.h>
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ctf-ir/clock-class.h>
31 #include <babeltrace/ctf-ir/trace.h>
32 #include <babeltrace/ref.h>
33 #include <babeltrace/ctf/events.h>
34 #include <babeltrace/values.h>
37 #include <babeltrace/compat/stdlib-internal.h>
39 #include <babeltrace/compat/utsname-internal.h>
40 #include <babeltrace/compat/limits-internal.h>
41 #include <babeltrace/compat/stdio-internal.h>
51 #define METADATA_LINE_SIZE 512
52 #define SEQUENCE_TEST_LENGTH 10
53 #define ARRAY_TEST_LENGTH 5
54 #define PACKET_RESIZE_TEST_DEF_LENGTH 100000
56 #define DEFAULT_CLOCK_FREQ 1000000000
57 #define DEFAULT_CLOCK_PRECISION 1
58 #define DEFAULT_CLOCK_OFFSET 0
59 #define DEFAULT_CLOCK_OFFSET_S 0
60 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
61 #define DEFAULT_CLOCK_TIME 0
62 #define DEFAULT_CLOCK_VALUE 0
66 static int64_t current_time
= 42;
67 static unsigned int packet_resize_test_length
= PACKET_RESIZE_TEST_DEF_LENGTH
;
69 /* Return 1 if uuids match, zero if different. */
71 int uuid_match(const unsigned char *uuid_a
, const unsigned char *uuid_b
)
76 if (!uuid_a
|| !uuid_b
) {
80 for (i
= 0; i
< 16; i
++) {
81 if (uuid_a
[i
] != uuid_b
[i
]) {
92 void validate_trace(char *parser_path
, char *trace_path
)
95 gchar
*babeltrace_output_path
;
96 int babeltrace_output_fd
= -1;
103 babeltrace_output_fd
= g_file_open_tmp("babeltrace_output_XXXXXX",
104 &babeltrace_output_path
, NULL
);
105 unlink(babeltrace_output_path
);
106 g_free(babeltrace_output_path
);
108 if (babeltrace_output_fd
== -1) {
109 diag("Failed to create a temporary file for trace parsing.");
117 waitpid(pid
, &status
, 0);
118 ret
= WIFEXITED(status
) ? WEXITSTATUS(status
) : -1;
120 ret
= dup2(babeltrace_output_fd
, STDOUT_FILENO
);
122 perror("# dup2 babeltrace_output_fd to STDOUT");
126 ret
= dup2(babeltrace_output_fd
, STDERR_FILENO
);
128 perror("# dup2 babeltrace_output_fd to STDERR");
132 execl(parser_path
, parser_path
, trace_path
, NULL
);
133 perror("# Could not launch the babeltrace process");
137 ok(ret
== 0, "Babeltrace could read the resulting trace");
139 if (ret
&& babeltrace_output_fd
>= 0) {
141 size_t len
= METADATA_LINE_SIZE
;
142 FILE *babeltrace_output_fp
= NULL
;
144 babeltrace_output_fp
= fdopen(babeltrace_output_fd
, "r");
145 if (!babeltrace_output_fp
) {
146 perror("fdopen on babeltrace_output_fd");
149 babeltrace_output_fd
= -1;
153 diag("malloc error");
155 rewind(babeltrace_output_fp
);
156 while (bt_getline(&line
, &len
, babeltrace_output_fp
) > 0) {
162 if (babeltrace_output_fp
) {
163 if (fclose(babeltrace_output_fp
)) {
164 diag("fclose error");
169 if (babeltrace_output_fd
>= 0) {
170 if (close(babeltrace_output_fd
)) {
177 void append_simple_event(struct bt_ctf_stream_class
*stream_class
,
178 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
180 /* Create and add a simple event class */
181 struct bt_ctf_event_class
*simple_event_class
=
182 bt_ctf_event_class_create("Simple Event");
183 struct bt_ctf_field_type
*uint_12_type
=
184 bt_ctf_field_type_integer_create(12);
185 struct bt_ctf_field_type
*int_64_type
=
186 bt_ctf_field_type_integer_create(64);
187 struct bt_ctf_field_type
*float_type
=
188 bt_ctf_field_type_floating_point_create();
189 struct bt_ctf_field_type
*enum_type
;
190 struct bt_ctf_field_type
*enum_type_unsigned
=
191 bt_ctf_field_type_enumeration_create(uint_12_type
);
192 struct bt_ctf_field_type
*event_context_type
=
193 bt_ctf_field_type_structure_create();
194 struct bt_ctf_field_type
*event_payload_type
= NULL
;
195 struct bt_ctf_field_type
*returned_type
;
196 struct bt_ctf_event
*simple_event
;
197 struct bt_ctf_field
*integer_field
;
198 struct bt_ctf_field
*float_field
;
199 struct bt_ctf_field
*enum_field
;
200 struct bt_ctf_field
*enum_field_unsigned
;
201 struct bt_ctf_field
*enum_container_field
;
202 const char *mapping_name_test
= "truie";
203 const double double_test_value
= 3.1415;
204 struct bt_ctf_field
*enum_container_field_unsigned
;
205 const char *mapping_name_negative_test
= "negative_value";
206 const char *ret_char
;
208 int64_t ret_range_start_int64_t
, ret_range_end_int64_t
;
209 uint64_t ret_range_start_uint64_t
, ret_range_end_uint64_t
;
210 struct bt_ctf_event_class
*ret_event_class
;
211 struct bt_ctf_field
*packet_context
;
212 struct bt_ctf_field
*packet_context_field
;
213 struct bt_ctf_field
*stream_event_context
;
214 struct bt_ctf_field
*stream_event_context_field
;
215 struct bt_ctf_field
*event_context
;
216 struct bt_ctf_field
*event_context_field
;
217 struct bt_ctf_field_type
*ep_integer_field_type
= NULL
;
218 struct bt_ctf_field_type
*ep_enum_field_type
= NULL
;
219 struct bt_ctf_field_type
*ep_enum_field_unsigned_type
= NULL
;
220 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
223 ok(uint_12_type
, "Create an unsigned integer type");
225 ok(!bt_ctf_field_type_integer_set_signed(int_64_type
, 1),
226 "Set signed 64 bit integer signedness to true");
227 ok(int_64_type
, "Create a signed integer type");
228 enum_type
= bt_ctf_field_type_enumeration_create(int_64_type
);
230 returned_type
= bt_ctf_field_type_enumeration_get_container_type(enum_type
);
231 ok(returned_type
== int_64_type
, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
232 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL
), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
233 ok(!bt_ctf_field_type_enumeration_create(enum_type
),
234 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
235 bt_put(returned_type
);
237 bt_ctf_field_type_set_alignment(float_type
, 32);
238 ok(bt_ctf_field_type_get_alignment(NULL
) < 0,
239 "bt_ctf_field_type_get_alignment handles NULL correctly");
240 ok(bt_ctf_field_type_get_alignment(float_type
) == 32,
241 "bt_ctf_field_type_get_alignment returns a correct value");
243 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type
, 11) == 0,
244 "Set a floating point type's exponent digit count");
245 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type
, 53) == 0,
246 "Set a floating point type's mantissa digit count");
248 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL
) < 0,
249 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
250 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL
) < 0,
251 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
252 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type
) == 11,
253 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
254 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type
) == 53,
255 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
257 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
258 mapping_name_negative_test
, -12345, 0) == 0,
259 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
260 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
261 "escaping; \"test\"", 1, 1) == 0,
262 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
263 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
264 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
265 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
266 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
,
267 "event clock int float", 5, 22) == 0,
268 "Accept enumeration mapping strings containing reserved keywords");
269 bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
271 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
272 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
273 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, "something",
274 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
275 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type
, mapping_name_test
,
276 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
277 bt_ctf_field_type_enumeration_add_mapping(enum_type
, "another entry", -42000, -13000);
279 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(NULL
, -42);
280 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly");
282 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, -4200000);
283 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values");
285 iter
= bt_ctf_field_type_enumeration_find_mappings_by_signed_value(enum_type
, 3);
286 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value");
287 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, NULL
, NULL
, NULL
) == 0,
288 "bt_ctf_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly");
291 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type
,
292 "enum_field") == 0, "Add signed enumeration field to event");
294 ok(bt_ctf_field_type_enumeration_get_mapping_signed(NULL
, 0, &ret_char
,
295 &ret_range_start_int64_t
, &ret_range_end_int64_t
) < 0,
296 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly");
297 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, NULL
,
298 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
299 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL string correctly");
300 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
301 NULL
, &ret_range_end_int64_t
) == 0,
302 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL start correctly");
303 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 0, &ret_char
,
304 &ret_range_start_int64_t
, NULL
) == 0,
305 "bt_ctf_field_type_enumeration_get_mapping_signed handles a NULL end correctly");
306 /* Assumes entries are sorted by range_start values. */
307 ok(bt_ctf_field_type_enumeration_get_mapping_signed(enum_type
, 6, &ret_char
,
308 &ret_range_start_int64_t
, &ret_range_end_int64_t
) == 0,
309 "bt_ctf_field_type_enumeration_get_mapping_signed returns a value");
310 ok(!strcmp(ret_char
, mapping_name_test
),
311 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping name");
312 ok(ret_range_start_int64_t
== 42,
313 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping start");
314 ok(ret_range_end_int64_t
== 42,
315 "bt_ctf_field_type_enumeration_get_mapping_signed returns a correct mapping end");
317 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
318 "escaping; \"test\"", 0, 0) == 0,
319 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
320 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
321 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
322 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
323 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
,
324 "event clock int float", 5, 22) == 0,
325 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
326 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
327 42, 42) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts single-value ranges");
328 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
329 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names");
330 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, "something",
331 7, 8) == 0, "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries");
332 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned
, mapping_name_test
,
333 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
334 ok(bt_ctf_event_class_add_field(simple_event_class
, enum_type_unsigned
,
335 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
337 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL
) < 0,
338 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
339 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned
) == 6,
340 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
342 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL
, 0, &ret_char
,
343 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) < 0,
344 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
345 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, NULL
,
346 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
347 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
348 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
349 NULL
, &ret_range_end_uint64_t
) == 0,
350 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
351 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 0, &ret_char
,
352 &ret_range_start_uint64_t
, NULL
) == 0,
353 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
354 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned
, 4, &ret_char
,
355 &ret_range_start_uint64_t
, &ret_range_end_uint64_t
) == 0,
356 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
357 ok(!strcmp(ret_char
, mapping_name_test
),
358 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
359 ok(ret_range_start_uint64_t
== 42,
360 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
361 ok(ret_range_end_uint64_t
== 42,
362 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
364 bt_ctf_event_class_add_field(simple_event_class
, uint_12_type
,
366 bt_ctf_event_class_add_field(simple_event_class
, float_type
,
369 assert(!bt_ctf_event_class_set_id(simple_event_class
, 13));
371 /* Set an event context type which will contain a single integer. */
372 ok(!bt_ctf_field_type_structure_add_field(event_context_type
, uint_12_type
,
373 "event_specific_context"),
374 "Add event specific context field");
375 ok(bt_ctf_event_class_get_context_type(NULL
) == NULL
,
376 "bt_ctf_event_class_get_context_type handles NULL correctly");
378 ok(bt_ctf_event_class_set_context_type(NULL
, event_context_type
) < 0,
379 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
380 ok(!bt_ctf_event_class_set_context_type(simple_event_class
, event_context_type
),
381 "Set an event class' context type successfully");
382 returned_type
= bt_ctf_event_class_get_context_type(simple_event_class
);
383 ok(returned_type
== event_context_type
,
384 "bt_ctf_event_class_get_context_type returns the appropriate type");
385 bt_put(returned_type
);
387 ok(!bt_ctf_stream_class_add_event_class(stream_class
, simple_event_class
),
388 "Adding simple event class to stream class");
391 * bt_ctf_stream_class_add_event_class() copies the field types
392 * of simple_event_class, so we retrieve the new ones to create
393 * the appropriate fields.
395 BT_PUT(event_context_type
);
396 BT_PUT(event_payload_type
);
397 event_payload_type
= bt_ctf_event_class_get_payload_type(
399 assert(event_payload_type
);
400 event_context_type
= bt_ctf_event_class_get_context_type(
402 assert(event_context_type
);
403 ep_integer_field_type
=
404 bt_ctf_field_type_structure_get_field_type_by_name(
405 event_payload_type
, "integer_field");
406 assert(ep_integer_field_type
);
408 bt_ctf_field_type_structure_get_field_type_by_name(
409 event_payload_type
, "enum_field");
410 assert(ep_enum_field_type
);
411 ep_enum_field_unsigned_type
=
412 bt_ctf_field_type_structure_get_field_type_by_name(
413 event_payload_type
, "enum_field_unsigned");
414 assert(ep_enum_field_unsigned_type
);
416 ok(bt_ctf_stream_class_get_event_class_count(NULL
) < 0,
417 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
418 ok(bt_ctf_stream_class_get_event_class_count(stream_class
) == 1,
419 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
420 ok(bt_ctf_stream_class_get_event_class_by_index(NULL
, 0) == NULL
,
421 "bt_ctf_stream_class_get_event_class handles NULL correctly");
422 ok(bt_ctf_stream_class_get_event_class_by_index(stream_class
, 8724) == NULL
,
423 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
424 ret_event_class
= bt_ctf_stream_class_get_event_class_by_index(stream_class
, 0);
425 ok(ret_event_class
== simple_event_class
,
426 "bt_ctf_stream_class_get_event_class returns the correct event class");
427 bt_put(ret_event_class
);
428 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL
, 0),
429 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
430 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class
, 2),
431 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
433 bt_ctf_stream_class_get_event_class_by_id(stream_class
, 13);
434 ok(ret_event_class
== simple_event_class
,
435 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
436 bt_put(ret_event_class
);
438 simple_event
= bt_ctf_event_create(simple_event_class
);
440 "Instantiate an event containing a single integer field");
442 integer_field
= bt_ctf_field_create(ep_integer_field_type
);
443 bt_ctf_field_unsigned_integer_set_value(integer_field
, 42);
444 ok(bt_ctf_event_set_payload(simple_event
, "integer_field",
445 integer_field
) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
447 float_field
= bt_ctf_event_get_payload(simple_event
, "float_field");
448 ok(bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
449 "bt_ctf_field_floating_point_get_value fails on an unset float field");
450 bt_ctf_field_floating_point_set_value(float_field
, double_test_value
);
451 ok(bt_ctf_field_floating_point_get_value(NULL
, &ret_double
),
452 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
453 ok(bt_ctf_field_floating_point_get_value(float_field
, NULL
),
454 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
455 ok(!bt_ctf_field_floating_point_get_value(float_field
, &ret_double
),
456 "bt_ctf_field_floating_point_get_value returns a double value");
457 ok(fabs(ret_double
- double_test_value
) <= DBL_EPSILON
,
458 "bt_ctf_field_floating_point_get_value returns a correct value");
460 enum_field
= bt_ctf_field_create(ep_enum_field_type
);
463 iter
= bt_ctf_field_enumeration_get_mappings(NULL
);
464 ok(!iter
, "bt_ctf_field_enumeration_get_mappings handles NULL correctly");
465 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
466 ok(!iter
, "bt_ctf_field_enumeration_get_mappings returns NULL if the enumeration's container field is unset");
467 enum_container_field
= bt_ctf_field_enumeration_get_container(
469 ok(bt_ctf_field_signed_integer_set_value(
470 enum_container_field
, -42) == 0,
471 "Set signed enumeration container value");
472 iter
= bt_ctf_field_enumeration_get_mappings(enum_field
);
473 ok(iter
, "bt_ctf_field_enumeration_get_mappings returns an iterator to matching mappings");
474 ret
= bt_ctf_field_type_enumeration_mapping_iterator_get_signed(iter
, &ret_char
, NULL
, NULL
);
475 ok(!ret
&& ret_char
, "bt_ctf_field_type_enumeration_mapping_iterator_get_signed return a mapping name");
477 ok(!strcmp(ret_char
, mapping_name_negative_test
),
478 "bt_ctf_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container");
479 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field", enum_field
);
483 enum_field_unsigned
= bt_ctf_field_create(ep_enum_field_unsigned_type
);
484 assert(enum_field_unsigned
);
485 enum_container_field_unsigned
= bt_ctf_field_enumeration_get_container(
486 enum_field_unsigned
);
487 ok(bt_ctf_field_unsigned_integer_set_value(
488 enum_container_field_unsigned
, 42) == 0,
489 "Set unsigned enumeration container value");
490 ret
= bt_ctf_event_set_payload(simple_event
, "enum_field_unsigned",
491 enum_field_unsigned
);
493 iter
= bt_ctf_field_enumeration_get_mappings(enum_field_unsigned
);
495 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, &ret_char
, NULL
, NULL
);
496 ok(ret_char
&& !strcmp(ret_char
, mapping_name_test
),
497 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container");
499 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0, "Set clock time");
501 /* Populate stream event context */
502 stream_event_context
=
503 bt_ctf_event_get_stream_event_context(simple_event
);
504 assert(stream_event_context
);
505 stream_event_context_field
= bt_ctf_field_structure_get_field(
506 stream_event_context
, "common_event_context");
507 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field
, 42);
509 /* Populate the event's context */
510 ok(bt_ctf_event_get_event_context(NULL
) == NULL
,
511 "bt_ctf_event_get_event_context handles NULL correctly");
512 event_context
= bt_ctf_event_get_event_context(simple_event
);
514 "bt_ctf_event_get_event_context returns a field");
515 returned_type
= bt_ctf_field_get_type(event_context
);
516 ok(returned_type
== event_context_type
,
517 "bt_ctf_event_get_event_context returns a field of the appropriate type");
518 event_context_field
= bt_ctf_field_structure_get_field(event_context
,
519 "event_specific_context");
520 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field
, 1234),
521 "Successfully set an event context's value");
522 ok(bt_ctf_event_set_event_context(NULL
, event_context
) < 0,
523 "bt_ctf_event_set_event_context handles a NULL event correctly");
524 ok(bt_ctf_event_set_event_context(simple_event
, NULL
) < 0,
525 "bt_ctf_event_set_event_context handles a NULL event context correctly");
526 ok(bt_ctf_event_set_event_context(simple_event
, event_context_field
) < 0,
527 "bt_ctf_event_set_event_context rejects a context of the wrong type");
528 ok(!bt_ctf_event_set_event_context(simple_event
, event_context
),
529 "Set an event context successfully");
531 ok(bt_ctf_stream_append_event(stream
, simple_event
) == 0,
532 "Append simple event to trace stream");
534 ok(bt_ctf_stream_get_packet_context(NULL
) == NULL
,
535 "bt_ctf_stream_get_packet_context handles NULL correctly");
536 packet_context
= bt_ctf_stream_get_packet_context(stream
);
538 "bt_ctf_stream_get_packet_context returns a packet context");
540 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
542 ok(packet_context_field
,
543 "Packet context contains the default packet_size field.");
544 bt_put(packet_context_field
);
545 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
546 "custom_packet_context_field");
547 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 8) == 0,
548 "Custom packet context field value successfully set.");
550 ok(bt_ctf_stream_set_packet_context(NULL
, packet_context_field
) < 0,
551 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
552 ok(bt_ctf_stream_set_packet_context(stream
, NULL
) < 0,
553 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
554 ok(bt_ctf_stream_set_packet_context(stream
, packet_context
) == 0,
555 "Successfully set a stream's packet context");
557 ok(bt_ctf_stream_flush(stream
) == 0,
558 "Flush trace stream with one event");
560 bt_put(simple_event_class
);
561 bt_put(simple_event
);
562 bt_put(uint_12_type
);
566 bt_put(enum_type_unsigned
);
567 bt_put(returned_type
);
568 bt_put(event_context_type
);
569 bt_put(integer_field
);
572 bt_put(enum_field_unsigned
);
573 bt_put(enum_container_field
);
574 bt_put(enum_container_field_unsigned
);
575 bt_put(packet_context
);
576 bt_put(packet_context_field
);
577 bt_put(stream_event_context
);
578 bt_put(stream_event_context_field
);
579 bt_put(event_context
);
580 bt_put(event_context_field
);
581 bt_put(event_payload_type
);
582 bt_put(ep_integer_field_type
);
583 bt_put(ep_enum_field_type
);
584 bt_put(ep_enum_field_unsigned_type
);
589 void append_complex_event(struct bt_ctf_stream_class
*stream_class
,
590 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
593 struct event_class_attrs_counts
;
594 const char *complex_test_event_string
= "Complex Test Event";
595 const char *test_string_1
= "Test ";
596 const char *test_string_2
= "string ";
597 const char *test_string_3
= "abcdefghi";
598 const char *test_string_4
= "abcd\0efg\0hi";
599 const char *test_string_cat
= "Test string abcdeabcd";
600 struct bt_ctf_field_type
*uint_35_type
=
601 bt_ctf_field_type_integer_create(35);
602 struct bt_ctf_field_type
*int_16_type
=
603 bt_ctf_field_type_integer_create(16);
604 struct bt_ctf_field_type
*uint_3_type
=
605 bt_ctf_field_type_integer_create(3);
606 struct bt_ctf_field_type
*enum_variant_type
=
607 bt_ctf_field_type_enumeration_create(uint_3_type
);
608 struct bt_ctf_field_type
*variant_type
=
609 bt_ctf_field_type_variant_create(enum_variant_type
,
611 struct bt_ctf_field_type
*string_type
=
612 bt_ctf_field_type_string_create();
613 struct bt_ctf_field_type
*sequence_type
;
614 struct bt_ctf_field_type
*array_type
;
615 struct bt_ctf_field_type
*inner_structure_type
=
616 bt_ctf_field_type_structure_create();
617 struct bt_ctf_field_type
*complex_structure_type
=
618 bt_ctf_field_type_structure_create();
619 struct bt_ctf_field_type
*ret_field_type
;
620 struct bt_ctf_event_class
*event_class
;
621 struct bt_ctf_event
*event
;
622 struct bt_ctf_field
*uint_35_field
, *int_16_field
, *a_string_field
,
623 *inner_structure_field
, *complex_structure_field
,
624 *a_sequence_field
, *enum_variant_field
, *enum_container_field
,
625 *variant_field
, *an_array_field
, *stream_event_ctx_field
,
626 *stream_event_ctx_int_field
, *ret_field
;
627 uint64_t ret_unsigned_int
;
628 int64_t ret_signed_int
;
629 const char *ret_string
;
630 struct bt_ctf_stream_class
*ret_stream_class
;
631 struct bt_ctf_event_class
*ret_event_class
;
632 struct bt_ctf_field
*packet_context
, *packet_context_field
;
633 struct bt_ctf_field_type_enumeration_mapping_iterator
*iter
= NULL
;
635 ok(bt_ctf_field_type_set_alignment(int_16_type
, 0),
636 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
637 ok(bt_ctf_field_type_set_alignment(int_16_type
, 3),
638 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
639 ok(bt_ctf_field_type_set_alignment(int_16_type
, 24),
640 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
641 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 4),
642 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
643 ok(!bt_ctf_field_type_set_alignment(int_16_type
, 32),
644 "Set alignment of signed 16 bit integer to 32");
645 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
646 "Set integer signedness to true");
647 ok(!bt_ctf_field_type_integer_set_base(uint_35_type
,
648 BT_CTF_INTEGER_BASE_HEXADECIMAL
),
649 "Set signed 16 bit integer base to hexadecimal");
651 array_type
= bt_ctf_field_type_array_create(int_16_type
, ARRAY_TEST_LENGTH
);
652 sequence_type
= bt_ctf_field_type_sequence_create(int_16_type
,
655 ok(bt_ctf_field_type_array_get_element_type(NULL
) == NULL
,
656 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
657 ret_field_type
= bt_ctf_field_type_array_get_element_type(
659 ok(ret_field_type
== int_16_type
,
660 "bt_ctf_field_type_array_get_element_type returns the correct type");
661 bt_put(ret_field_type
);
663 ok(bt_ctf_field_type_array_get_length(NULL
) < 0,
664 "bt_ctf_field_type_array_get_length handles NULL correctly");
665 ok(bt_ctf_field_type_array_get_length(array_type
) == ARRAY_TEST_LENGTH
,
666 "bt_ctf_field_type_array_get_length returns the correct length");
668 ok(bt_ctf_field_type_structure_add_field(inner_structure_type
,
669 inner_structure_type
, "yes"), "Cannot add self to structure");
670 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
671 uint_35_type
, "seq_len"), "Add seq_len field to inner structure");
672 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
673 sequence_type
, "a_sequence"), "Add a_sequence field to inner structure");
674 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type
,
675 array_type
, "an_array"), "Add an_array field to inner structure");
677 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
679 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
681 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type
,
682 "UINT35_TYPE", 2, 7);
684 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(NULL
, "INT16_TYPE");
685 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly");
687 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, "INT16_TYPE");
688 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly");
689 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
690 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly");
693 iter
= bt_ctf_field_type_enumeration_find_mappings_by_name(enum_variant_type
, NULL
);
694 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_name handles a NULL name correctly");
696 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(NULL
, 1);
697 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly");
699 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, -42);
700 ok(iter
== NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly");
701 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) != 0,
702 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly");
705 iter
= bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type
, 5);
706 ok(iter
!= NULL
, "bt_ctf_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly");
707 ok(bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned(iter
, NULL
, NULL
, NULL
) == 0,
708 "bt_ctf_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly");
711 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
712 "An unknown entry"), "Reject a variant field based on an unknown tag value");
713 ok(bt_ctf_field_type_variant_add_field(variant_type
, uint_3_type
,
714 "UINT3_TYPE") == 0, "Add a field to a variant");
715 ok(!bt_ctf_field_type_variant_add_field(variant_type
, int_16_type
,
716 "INT16_TYPE"), "Add INT16_TYPE field to variant");
717 ok(!bt_ctf_field_type_variant_add_field(variant_type
, uint_35_type
,
718 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
720 ok(bt_ctf_field_type_variant_get_tag_type(NULL
) == NULL
,
721 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
722 ret_field_type
= bt_ctf_field_type_variant_get_tag_type(variant_type
);
723 ok(ret_field_type
== enum_variant_type
,
724 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
725 bt_put(ret_field_type
);
727 ok(bt_ctf_field_type_variant_get_tag_name(NULL
) == NULL
,
728 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
729 ret_string
= bt_ctf_field_type_variant_get_tag_name(variant_type
);
730 ok(ret_string
? !strcmp(ret_string
, "variant_selector") : 0,
731 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
732 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL
,
733 "INT16_TYPE") == NULL
,
734 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
735 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type
,
737 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
738 ret_field_type
= bt_ctf_field_type_variant_get_field_type_by_name(
739 variant_type
, "INT16_TYPE");
740 ok(ret_field_type
== int_16_type
,
741 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
742 bt_put(ret_field_type
);
744 ok(bt_ctf_field_type_variant_get_field_count(NULL
) < 0,
745 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
746 ok(bt_ctf_field_type_variant_get_field_count(variant_type
) == 3,
747 "bt_ctf_field_type_variant_get_field_count returns the correct count");
749 ok(bt_ctf_field_type_variant_get_field(NULL
, &ret_string
, &ret_field_type
, 0) < 0,
750 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
751 ok(bt_ctf_field_type_variant_get_field(variant_type
, NULL
, &ret_field_type
, 0) == 0,
752 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
753 bt_put(ret_field_type
);
754 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, NULL
, 0) == 0,
755 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
756 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 200) < 0,
757 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
758 ok(bt_ctf_field_type_variant_get_field(variant_type
, &ret_string
, &ret_field_type
, 1) == 0,
759 "bt_ctf_field_type_variant_get_field returns a field");
760 ok(!strcmp("INT16_TYPE", ret_string
),
761 "bt_ctf_field_type_variant_get_field returns a correct field name");
762 ok(ret_field_type
== int_16_type
,
763 "bt_ctf_field_type_variant_get_field returns a correct field type");
764 bt_put(ret_field_type
);
766 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
767 enum_variant_type
, "variant_selector"),
768 "Add variant_selector field to complex structure");
769 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
770 string_type
, "a_string"), "Add a_string field to complex structure");
771 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
772 variant_type
, "variant_value"),
773 "Add variant_value field to complex structure");
774 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type
,
775 inner_structure_type
, "inner_structure"),
776 "Add inner_structure field to complex structure");
778 event_class
= bt_ctf_event_class_create(complex_test_event_string
);
779 ok(event_class
, "Create an event class");
780 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
, ""),
781 "Reject addition of a field with an empty name to an event");
782 ok(bt_ctf_event_class_add_field(event_class
, NULL
, "an_integer"),
783 "Reject addition of a field with a NULL type to an event");
784 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
786 "Reject addition of a type with an illegal name to an event");
787 ok(bt_ctf_event_class_add_field(event_class
, uint_35_type
,
789 "Add field of type unsigned integer to an event");
790 ok(bt_ctf_event_class_add_field(event_class
, int_16_type
,
791 "int_16") == 0, "Add field of type signed integer to an event");
792 ok(bt_ctf_event_class_add_field(event_class
, complex_structure_type
,
793 "complex_structure") == 0,
794 "Add composite structure to an event");
796 ok(bt_ctf_event_class_get_name(NULL
) == NULL
,
797 "bt_ctf_event_class_get_name handles NULL correctly");
798 ret_string
= bt_ctf_event_class_get_name(event_class
);
799 ok(!strcmp(ret_string
, complex_test_event_string
),
800 "bt_ctf_event_class_get_name returns a correct name");
801 ok(bt_ctf_event_class_get_id(event_class
) < 0,
802 "bt_ctf_event_class_get_id returns a negative value when not set");
803 ok(bt_ctf_event_class_get_id(NULL
) < 0,
804 "bt_ctf_event_class_get_id handles NULL correctly");
805 ok(bt_ctf_event_class_set_id(NULL
, 42) < 0,
806 "bt_ctf_event_class_set_id handles NULL correctly");
807 ok(bt_ctf_event_class_set_id(event_class
, 42) == 0,
808 "Set an event class' id");
809 ok(bt_ctf_event_class_get_id(event_class
) == 42,
810 "bt_ctf_event_class_get_id returns the correct value");
812 /* Test event class attributes */
813 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED
,
814 "event class has the expected initial log level");
815 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
816 "as expected, event class has no initial EMF URI");
817 ok(bt_ctf_event_class_set_log_level(NULL
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
818 "bt_ctf_event_class_set_log_level handles a NULL event class correctly");
819 ok(bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
),
820 "bt_ctf_event_class_set_log_level handles an unknown log level correctly");
821 ok(!bt_ctf_event_class_set_log_level(event_class
, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
),
822 "bt_ctf_event_class_set_log_level succeeds with a valid log level");
823 ok(bt_ctf_event_class_get_log_level(NULL
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN
,
824 "bt_ctf_event_class_get_log_level handles a NULL event class correctly");
825 ok(bt_ctf_event_class_get_log_level(event_class
) == BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO
,
826 "bt_ctf_event_class_get_log_level returns the expected log level");
827 ok(bt_ctf_event_class_set_emf_uri(NULL
, "http://diamon.org/babeltrace/"),
828 "bt_ctf_event_class_set_emf_uri handles a NULL event class correctly");
829 ok(!bt_ctf_event_class_set_emf_uri(event_class
, "http://diamon.org/babeltrace/"),
830 "bt_ctf_event_class_set_emf_uri succeeds with a valid EMF URI");
831 ok(!bt_ctf_event_class_get_emf_uri(NULL
),
832 "bt_ctf_event_class_get_emf_uri handles a NULL event class correctly");
833 ok(strcmp(bt_ctf_event_class_get_emf_uri(event_class
), "http://diamon.org/babeltrace/") == 0,
834 "bt_ctf_event_class_get_emf_uri returns the expected EMF URI");
835 ok(!bt_ctf_event_class_set_emf_uri(event_class
, NULL
),
836 "bt_ctf_event_class_set_emf_uri succeeds with NULL (to reset)");
837 ok(!bt_ctf_event_class_get_emf_uri(event_class
),
838 "as expected, event class has no EMF URI after reset");
840 /* Add event class to the stream class */
841 ok(bt_ctf_stream_class_add_event_class(stream_class
, NULL
),
842 "Reject addition of NULL event class to a stream class");
843 ok(bt_ctf_stream_class_add_event_class(stream_class
,
844 event_class
) == 0, "Add an event class to stream class");
846 ok(bt_ctf_event_class_get_stream_class(NULL
) == NULL
,
847 "bt_ctf_event_class_get_stream_class handles NULL correctly");
848 ret_stream_class
= bt_ctf_event_class_get_stream_class(event_class
);
849 ok(ret_stream_class
== stream_class
,
850 "bt_ctf_event_class_get_stream_class returns the correct stream class");
851 bt_put(ret_stream_class
);
853 ok(bt_ctf_event_class_get_payload_type_field_count(NULL
) < 0,
854 "bt_ctf_event_class_get_field_count handles NULL correctly");
855 ok(bt_ctf_event_class_get_payload_type_field_count(event_class
) == 3,
856 "bt_ctf_event_class_get_field_count returns a correct value");
858 ok(bt_ctf_event_class_get_payload_type_field_by_index(NULL
, &ret_string
,
859 &ret_field_type
, 0) < 0,
860 "bt_ctf_event_class_get_field handles a NULL event class correctly");
861 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, NULL
,
862 &ret_field_type
, 0) == 0,
863 "bt_ctf_event_class_get_field handles a NULL field name correctly");
864 bt_put(ret_field_type
);
865 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
867 "bt_ctf_event_class_get_field handles a NULL field type correctly");
868 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
869 &ret_field_type
, 42) < 0,
870 "bt_ctf_event_class_get_field handles an invalid index correctly");
871 ok(bt_ctf_event_class_get_payload_type_field_by_index(event_class
, &ret_string
,
872 &ret_field_type
, 0) == 0,
873 "bt_ctf_event_class_get_field returns a field");
874 ok(bt_ctf_field_type_compare(ret_field_type
, uint_35_type
) == 0,
875 "bt_ctf_event_class_get_field returns a correct field type");
876 bt_put(ret_field_type
);
877 ok(!strcmp(ret_string
, "uint_35"),
878 "bt_ctf_event_class_get_field returns a correct field name");
879 ok(bt_ctf_event_class_get_field_by_name(NULL
, "") == NULL
,
880 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
881 ok(bt_ctf_event_class_get_field_by_name(event_class
, NULL
) == NULL
,
882 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
883 ok(bt_ctf_event_class_get_field_by_name(event_class
, "truie") == NULL
,
884 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
885 ret_field_type
= bt_ctf_event_class_get_field_by_name(event_class
,
886 "complex_structure");
887 ok(bt_ctf_field_type_compare(ret_field_type
, complex_structure_type
) == 0,
888 "bt_ctf_event_class_get_field_by_name returns a correct field type");
889 bt_put(ret_field_type
);
891 event
= bt_ctf_event_create(event_class
);
892 ok(event
, "Instanciate a complex event");
894 ok(bt_ctf_event_get_class(NULL
) == NULL
,
895 "bt_ctf_event_get_class handles NULL correctly");
896 ret_event_class
= bt_ctf_event_get_class(event
);
897 ok(ret_event_class
== event_class
,
898 "bt_ctf_event_get_class returns the correct event class");
899 bt_put(ret_event_class
);
901 uint_35_field
= bt_ctf_event_get_payload(event
, "uint_35");
902 if (!uint_35_field
) {
903 printf("uint_35_field is NULL\n");
906 ok(uint_35_field
, "Use bt_ctf_event_get_payload to get a field instance ");
907 bt_ctf_field_unsigned_integer_set_value(uint_35_field
, 0x0DDF00D);
908 ok(bt_ctf_field_unsigned_integer_get_value(NULL
, &ret_unsigned_int
) < 0,
909 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
910 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
, NULL
) < 0,
911 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
912 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field
,
913 &ret_unsigned_int
) == 0,
914 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
915 ok(ret_unsigned_int
== 0x0DDF00D,
916 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
917 ok(bt_ctf_field_signed_integer_get_value(uint_35_field
,
918 &ret_signed_int
) < 0,
919 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
920 bt_put(uint_35_field
);
922 int_16_field
= bt_ctf_event_get_payload(event
, "int_16");
923 bt_ctf_field_signed_integer_set_value(int_16_field
, -12345);
924 ok(bt_ctf_field_signed_integer_get_value(NULL
, &ret_signed_int
) < 0,
925 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
926 ok(bt_ctf_field_signed_integer_get_value(int_16_field
, NULL
) < 0,
927 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
928 ok(bt_ctf_field_signed_integer_get_value(int_16_field
,
929 &ret_signed_int
) == 0,
930 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
931 ok(ret_signed_int
== -12345,
932 "bt_ctf_field_signed_integer_get_value returns the correct value");
933 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field
,
934 &ret_unsigned_int
) < 0,
935 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
936 bt_put(int_16_field
);
938 complex_structure_field
= bt_ctf_event_get_payload(event
,
939 "complex_structure");
941 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 0) == NULL
,
942 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
943 ok(bt_ctf_field_structure_get_field_by_index(NULL
, 9) == NULL
,
944 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
945 inner_structure_field
= bt_ctf_field_structure_get_field_by_index(
946 complex_structure_field
, 3);
947 ret_field_type
= bt_ctf_field_get_type(inner_structure_field
);
948 bt_put(inner_structure_field
);
949 ok(bt_ctf_field_type_compare(ret_field_type
, inner_structure_type
) == 0,
950 "bt_ctf_field_structure_get_field_by_index returns a correct field");
951 bt_put(ret_field_type
);
953 inner_structure_field
= bt_ctf_field_structure_get_field(
954 complex_structure_field
, "inner_structure");
955 a_string_field
= bt_ctf_field_structure_get_field(
956 complex_structure_field
, "a_string");
957 enum_variant_field
= bt_ctf_field_structure_get_field(
958 complex_structure_field
, "variant_selector");
959 variant_field
= bt_ctf_field_structure_get_field(
960 complex_structure_field
, "variant_value");
961 uint_35_field
= bt_ctf_field_structure_get_field(
962 inner_structure_field
, "seq_len");
963 a_sequence_field
= bt_ctf_field_structure_get_field(
964 inner_structure_field
, "a_sequence");
965 an_array_field
= bt_ctf_field_structure_get_field(
966 inner_structure_field
, "an_array");
968 enum_container_field
= bt_ctf_field_enumeration_get_container(
970 bt_ctf_field_unsigned_integer_set_value(enum_container_field
, 1);
971 int_16_field
= bt_ctf_field_variant_get_field(variant_field
,
973 bt_ctf_field_signed_integer_set_value(int_16_field
, -200);
974 bt_put(int_16_field
);
975 ok(!bt_ctf_field_string_get_value(a_string_field
),
976 "bt_ctf_field_string_get_value returns NULL on an unset field");
977 bt_ctf_field_string_set_value(a_string_field
,
979 ok(!bt_ctf_field_string_get_value(NULL
),
980 "bt_ctf_field_string_get_value correctly handles NULL");
981 ok(bt_ctf_field_string_append(NULL
, "yeah"),
982 "bt_ctf_field_string_append correctly handles a NULL string field");
983 ok(bt_ctf_field_string_append(a_string_field
, NULL
),
984 "bt_ctf_field_string_append correctly handles a NULL string value");
985 ok(!bt_ctf_field_string_append(a_string_field
, test_string_2
),
986 "bt_ctf_field_string_append succeeds");
987 ok(bt_ctf_field_string_append_len(NULL
, "oh noes", 3),
988 "bt_ctf_field_string_append_len correctly handles a NULL string field");
989 ok(bt_ctf_field_string_append_len(a_string_field
, NULL
, 3),
990 "bt_ctf_field_string_append_len correctly handles a NULL string value");
991 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 5),
992 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
993 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_4
, 10),
994 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
995 ok(!bt_ctf_field_string_append_len(a_string_field
, &test_string_4
[4], 3),
996 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
997 ok(!bt_ctf_field_string_append_len(a_string_field
, test_string_3
, 0),
998 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1000 ret_string
= bt_ctf_field_string_get_value(a_string_field
);
1001 ok(ret_string
, "bt_ctf_field_string_get_value returns a string");
1002 ok(ret_string
? !strcmp(ret_string
, test_string_cat
) : 0,
1003 "bt_ctf_field_string_get_value returns a correct value");
1004 bt_ctf_field_unsigned_integer_set_value(uint_35_field
,
1005 SEQUENCE_TEST_LENGTH
);
1007 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL
,
1008 enum_container_field
) == NULL
,
1009 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1010 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type
,
1012 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1013 ret_field_type
= bt_ctf_field_type_variant_get_field_type_from_tag(
1014 variant_type
, enum_variant_field
);
1015 ok(ret_field_type
== int_16_type
,
1016 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1018 ok(bt_ctf_field_sequence_get_length(a_sequence_field
) == NULL
,
1019 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1020 ok(bt_ctf_field_sequence_set_length(a_sequence_field
,
1021 uint_35_field
) == 0, "Set a sequence field's length");
1022 ret_field
= bt_ctf_field_sequence_get_length(a_sequence_field
);
1023 ok(ret_field
== uint_35_field
,
1024 "bt_ctf_field_sequence_get_length returns the correct length field");
1025 ok(bt_ctf_field_sequence_get_length(NULL
) == NULL
,
1026 "bt_ctf_field_sequence_get_length properly handles NULL");
1028 for (i
= 0; i
< SEQUENCE_TEST_LENGTH
; i
++) {
1029 int_16_field
= bt_ctf_field_sequence_get_field(
1030 a_sequence_field
, i
);
1031 bt_ctf_field_signed_integer_set_value(int_16_field
, 4 - i
);
1032 bt_put(int_16_field
);
1035 for (i
= 0; i
< ARRAY_TEST_LENGTH
; i
++) {
1036 int_16_field
= bt_ctf_field_array_get_field(
1038 bt_ctf_field_signed_integer_set_value(int_16_field
, i
);
1039 bt_put(int_16_field
);
1042 stream_event_ctx_field
= bt_ctf_event_get_stream_event_context(event
);
1043 assert(stream_event_ctx_field
);
1044 stream_event_ctx_int_field
= bt_ctf_field_structure_get_field(
1045 stream_event_ctx_field
, "common_event_context");
1046 BT_PUT(stream_event_ctx_field
);
1047 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field
, 17);
1048 BT_PUT(stream_event_ctx_int_field
);
1050 bt_ctf_clock_set_time(clock
, ++current_time
);
1051 ok(bt_ctf_stream_append_event(stream
, event
) == 0,
1052 "Append a complex event to a stream");
1055 * Populate the custom packet context field with a dummy value
1056 * otherwise flush will fail.
1058 packet_context
= bt_ctf_stream_get_packet_context(stream
);
1059 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
1060 "custom_packet_context_field");
1061 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 1);
1063 ok(bt_ctf_stream_flush(stream
) == 0,
1064 "Flush a stream containing a complex event");
1066 bt_put(uint_35_field
);
1067 bt_put(a_string_field
);
1068 bt_put(inner_structure_field
);
1069 bt_put(complex_structure_field
);
1070 bt_put(a_sequence_field
);
1071 bt_put(an_array_field
);
1072 bt_put(enum_variant_field
);
1073 bt_put(enum_container_field
);
1074 bt_put(variant_field
);
1076 bt_put(packet_context_field
);
1077 bt_put(packet_context
);
1078 bt_put(uint_35_type
);
1079 bt_put(int_16_type
);
1080 bt_put(string_type
);
1081 bt_put(sequence_type
);
1083 bt_put(inner_structure_type
);
1084 bt_put(complex_structure_type
);
1085 bt_put(uint_3_type
);
1086 bt_put(enum_variant_type
);
1087 bt_put(variant_type
);
1088 bt_put(ret_field_type
);
1089 bt_put(event_class
);
1094 void field_copy_tests_validate_same_type(struct bt_ctf_field
*field
,
1095 struct bt_ctf_field_type
*expected_type
, const char *name
)
1097 struct bt_ctf_field_type
*copy_type
;
1099 copy_type
= bt_ctf_field_get_type(field
);
1100 ok(copy_type
== expected_type
,
1101 "bt_ctf_field_copy does not copy the type (%s)", name
);
1106 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field
*field_a
,
1107 struct bt_ctf_field
*field_b
, const char *name
)
1109 ok(field_a
!= field_b
,
1110 "bt_ctf_field_copy creates different pointers (%s)", name
);
1114 void field_copy_tests()
1116 struct bt_ctf_field_type
*len_type
= NULL
;
1117 struct bt_ctf_field_type
*fp_type
= NULL
;
1118 struct bt_ctf_field_type
*s_type
= NULL
;
1119 struct bt_ctf_field_type
*e_int_type
= NULL
;
1120 struct bt_ctf_field_type
*e_type
= NULL
;
1121 struct bt_ctf_field_type
*v_type
= NULL
;
1122 struct bt_ctf_field_type
*v_label1_type
= NULL
;
1123 struct bt_ctf_field_type
*v_label1_array_type
= NULL
;
1124 struct bt_ctf_field_type
*v_label2_type
= NULL
;
1125 struct bt_ctf_field_type
*v_label2_seq_type
= NULL
;
1126 struct bt_ctf_field_type
*strct_type
= NULL
;
1127 struct bt_ctf_field
*len
= NULL
;
1128 struct bt_ctf_field
*fp
= NULL
;
1129 struct bt_ctf_field
*s
= NULL
;
1130 struct bt_ctf_field
*e_int
= NULL
;
1131 struct bt_ctf_field
*e
= NULL
;
1132 struct bt_ctf_field
*v
= NULL
;
1133 struct bt_ctf_field
*v_selected
= NULL
;
1134 struct bt_ctf_field
*v_selected_cur
= NULL
;
1135 struct bt_ctf_field
*v_selected_0
= NULL
;
1136 struct bt_ctf_field
*v_selected_1
= NULL
;
1137 struct bt_ctf_field
*v_selected_2
= NULL
;
1138 struct bt_ctf_field
*v_selected_3
= NULL
;
1139 struct bt_ctf_field
*v_selected_4
= NULL
;
1140 struct bt_ctf_field
*v_selected_5
= NULL
;
1141 struct bt_ctf_field
*v_selected_6
= NULL
;
1142 struct bt_ctf_field
*a
= NULL
;
1143 struct bt_ctf_field
*a_0
= NULL
;
1144 struct bt_ctf_field
*a_1
= NULL
;
1145 struct bt_ctf_field
*a_2
= NULL
;
1146 struct bt_ctf_field
*a_3
= NULL
;
1147 struct bt_ctf_field
*a_4
= NULL
;
1148 struct bt_ctf_field
*strct
= NULL
;
1149 struct bt_ctf_field
*len_copy
= NULL
;
1150 struct bt_ctf_field
*fp_copy
= NULL
;
1151 struct bt_ctf_field
*s_copy
= NULL
;
1152 struct bt_ctf_field
*e_int_copy
= NULL
;
1153 struct bt_ctf_field
*e_copy
= NULL
;
1154 struct bt_ctf_field
*v_copy
= NULL
;
1155 struct bt_ctf_field
*v_selected_copy
= NULL
;
1156 struct bt_ctf_field
*v_selected_copy_len
= NULL
;
1157 struct bt_ctf_field
*v_selected_0_copy
= NULL
;
1158 struct bt_ctf_field
*v_selected_1_copy
= NULL
;
1159 struct bt_ctf_field
*v_selected_2_copy
= NULL
;
1160 struct bt_ctf_field
*v_selected_3_copy
= NULL
;
1161 struct bt_ctf_field
*v_selected_4_copy
= NULL
;
1162 struct bt_ctf_field
*v_selected_5_copy
= NULL
;
1163 struct bt_ctf_field
*v_selected_6_copy
= NULL
;
1164 struct bt_ctf_field
*a_copy
= NULL
;
1165 struct bt_ctf_field
*a_0_copy
= NULL
;
1166 struct bt_ctf_field
*a_1_copy
= NULL
;
1167 struct bt_ctf_field
*a_2_copy
= NULL
;
1168 struct bt_ctf_field
*a_3_copy
= NULL
;
1169 struct bt_ctf_field
*a_4_copy
= NULL
;
1170 struct bt_ctf_field
*strct_copy
= NULL
;
1171 struct bt_ctf_field_type_enumeration_mapping_iterator
*e_iter
= NULL
;
1172 uint64_t uint64_t_val
;
1173 const char *str_val
;
1177 /* create len type */
1178 len_type
= bt_ctf_field_type_integer_create(32);
1181 /* create fp type */
1182 fp_type
= bt_ctf_field_type_floating_point_create();
1186 s_type
= bt_ctf_field_type_string_create();
1189 /* create e_int type */
1190 e_int_type
= bt_ctf_field_type_integer_create(8);
1194 e_type
= bt_ctf_field_type_enumeration_create(e_int_type
);
1196 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL1",
1199 ret
= bt_ctf_field_type_enumeration_add_mapping(e_type
, "LABEL2",
1203 /* create v_label1 type */
1204 v_label1_type
= bt_ctf_field_type_string_create();
1205 assert(v_label1_type
);
1207 /* create v_label1_array type */
1208 v_label1_array_type
= bt_ctf_field_type_array_create(v_label1_type
, 5);
1209 assert(v_label1_array_type
);
1211 /* create v_label2 type */
1212 v_label2_type
= bt_ctf_field_type_integer_create(16);
1213 assert(v_label2_type
);
1215 /* create v_label2_seq type */
1216 v_label2_seq_type
= bt_ctf_field_type_sequence_create(v_label2_type
,
1218 assert(v_label2_seq_type
);
1221 v_type
= bt_ctf_field_type_variant_create(e_type
, "e");
1223 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label1_array_type
,
1226 ret
= bt_ctf_field_type_variant_add_field(v_type
, v_label2_seq_type
,
1230 /* create strct type */
1231 strct_type
= bt_ctf_field_type_structure_create();
1233 ret
= bt_ctf_field_type_structure_add_field(strct_type
, len_type
,
1236 ret
= bt_ctf_field_type_structure_add_field(strct_type
, fp_type
, "fp");
1238 ret
= bt_ctf_field_type_structure_add_field(strct_type
, s_type
, "s");
1240 ret
= bt_ctf_field_type_structure_add_field(strct_type
, e_type
, "e");
1242 ret
= bt_ctf_field_type_structure_add_field(strct_type
, v_type
, "v");
1244 ret
= bt_ctf_field_type_structure_add_field(strct_type
,
1245 v_label1_array_type
, "a");
1249 strct
= bt_ctf_field_create(strct_type
);
1253 len
= bt_ctf_field_structure_get_field(strct
, "len");
1257 fp
= bt_ctf_field_structure_get_field(strct
, "fp");
1261 s
= bt_ctf_field_structure_get_field(strct
, "s");
1265 e
= bt_ctf_field_structure_get_field(strct
, "e");
1268 /* get e_int (underlying integer) */
1269 e_int
= bt_ctf_field_enumeration_get_container(e
);
1273 v
= bt_ctf_field_structure_get_field(strct
, "v");
1277 a
= bt_ctf_field_structure_get_field(strct
, "a");
1281 ret
= bt_ctf_field_unsigned_integer_set_value(len
, 7);
1285 ret
= bt_ctf_field_floating_point_set_value(fp
, 3.14);
1289 ret
= bt_ctf_field_string_set_value(s
, "btbt");
1292 /* set e field (LABEL2) */
1293 ret
= bt_ctf_field_unsigned_integer_set_value(e_int
, 23);
1297 v_selected
= bt_ctf_field_variant_get_field(v
, e
);
1299 ok(!bt_ctf_field_variant_get_current_field(NULL
),
1300 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1301 v_selected_cur
= bt_ctf_field_variant_get_current_field(v
);
1302 ok(v_selected_cur
== v_selected
,
1303 "bt_ctf_field_variant_get_current_field returns the current field");
1304 bt_put(v_selected_cur
);
1306 /* set selected v field */
1307 ret
= bt_ctf_field_sequence_set_length(v_selected
, len
);
1309 v_selected_0
= bt_ctf_field_sequence_get_field(v_selected
, 0);
1310 assert(v_selected_0
);
1311 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_0
, 7);
1313 v_selected_1
= bt_ctf_field_sequence_get_field(v_selected
, 1);
1314 assert(v_selected_1
);
1315 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_1
, 6);
1317 v_selected_2
= bt_ctf_field_sequence_get_field(v_selected
, 2);
1318 assert(v_selected_2
);
1319 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_2
, 5);
1321 v_selected_3
= bt_ctf_field_sequence_get_field(v_selected
, 3);
1322 assert(v_selected_3
);
1323 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_3
, 4);
1325 v_selected_4
= bt_ctf_field_sequence_get_field(v_selected
, 4);
1326 assert(v_selected_4
);
1327 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_4
, 3);
1329 v_selected_5
= bt_ctf_field_sequence_get_field(v_selected
, 5);
1330 assert(v_selected_5
);
1331 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_5
, 2);
1333 v_selected_6
= bt_ctf_field_sequence_get_field(v_selected
, 6);
1334 assert(v_selected_6
);
1335 ret
= bt_ctf_field_unsigned_integer_set_value(v_selected_6
, 1);
1339 a_0
= bt_ctf_field_array_get_field(a
, 0);
1341 ret
= bt_ctf_field_string_set_value(a_0
, "a_0");
1343 a_1
= bt_ctf_field_array_get_field(a
, 1);
1345 ret
= bt_ctf_field_string_set_value(a_1
, "a_1");
1347 a_2
= bt_ctf_field_array_get_field(a
, 2);
1349 ret
= bt_ctf_field_string_set_value(a_2
, "a_2");
1351 a_3
= bt_ctf_field_array_get_field(a
, 3);
1353 ret
= bt_ctf_field_string_set_value(a_3
, "a_3");
1355 a_4
= bt_ctf_field_array_get_field(a
, 4);
1357 ret
= bt_ctf_field_string_set_value(a_4
, "a_4");
1360 /* create copy of strct */
1361 ok(!bt_ctf_field_copy(NULL
),
1362 "bt_ctf_field_copy handles NULL correctly");
1363 strct_copy
= bt_ctf_field_copy(strct
);
1365 "bt_ctf_field_copy returns a valid pointer");
1367 /* get all copied fields */
1368 len_copy
= bt_ctf_field_structure_get_field(strct_copy
, "len");
1370 fp_copy
= bt_ctf_field_structure_get_field(strct_copy
, "fp");
1372 s_copy
= bt_ctf_field_structure_get_field(strct_copy
, "s");
1374 e_copy
= bt_ctf_field_structure_get_field(strct_copy
, "e");
1376 e_int_copy
= bt_ctf_field_enumeration_get_container(e_copy
);
1378 v_copy
= bt_ctf_field_structure_get_field(strct_copy
, "v");
1380 v_selected_copy
= bt_ctf_field_variant_get_field(v_copy
, e_copy
);
1381 assert(v_selected_copy
);
1382 v_selected_0_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 0);
1383 assert(v_selected_0_copy
);
1384 v_selected_1_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 1);
1385 assert(v_selected_1_copy
);
1386 v_selected_2_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 2);
1387 assert(v_selected_2_copy
);
1388 v_selected_3_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 3);
1389 assert(v_selected_3_copy
);
1390 v_selected_4_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 4);
1391 assert(v_selected_4_copy
);
1392 v_selected_5_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 5);
1393 assert(v_selected_5_copy
);
1394 v_selected_6_copy
= bt_ctf_field_sequence_get_field(v_selected_copy
, 6);
1395 assert(v_selected_6_copy
);
1396 ok(!bt_ctf_field_sequence_get_field(v_selected_copy
, 7),
1397 "sequence field copy is not too large");
1398 a_copy
= bt_ctf_field_structure_get_field(strct_copy
, "a");
1400 a_0_copy
= bt_ctf_field_array_get_field(a_copy
, 0);
1402 a_1_copy
= bt_ctf_field_array_get_field(a_copy
, 1);
1404 a_2_copy
= bt_ctf_field_array_get_field(a_copy
, 2);
1406 a_3_copy
= bt_ctf_field_array_get_field(a_copy
, 3);
1408 a_4_copy
= bt_ctf_field_array_get_field(a_copy
, 4);
1410 ok(!bt_ctf_field_array_get_field(v_selected_copy
, 5),
1411 "array field copy is not too large");
1413 /* make sure copied fields are different pointers */
1414 field_copy_tests_validate_diff_ptrs(strct_copy
, strct
, "strct");
1415 field_copy_tests_validate_diff_ptrs(len_copy
, len
, "len");
1416 field_copy_tests_validate_diff_ptrs(fp_copy
, fp
, "fp");
1417 field_copy_tests_validate_diff_ptrs(s_copy
, s
, "s");
1418 field_copy_tests_validate_diff_ptrs(e_int_copy
, e_int
, "e_int");
1419 field_copy_tests_validate_diff_ptrs(e_copy
, e
, "e");
1420 field_copy_tests_validate_diff_ptrs(v_copy
, v
, "v");
1421 field_copy_tests_validate_diff_ptrs(v_selected_copy
, v_selected
,
1423 field_copy_tests_validate_diff_ptrs(v_selected_0_copy
, v_selected_0
,
1425 field_copy_tests_validate_diff_ptrs(v_selected_1_copy
, v_selected_1
,
1427 field_copy_tests_validate_diff_ptrs(v_selected_2_copy
, v_selected_2
,
1429 field_copy_tests_validate_diff_ptrs(v_selected_3_copy
, v_selected_3
,
1431 field_copy_tests_validate_diff_ptrs(v_selected_4_copy
, v_selected_4
,
1433 field_copy_tests_validate_diff_ptrs(v_selected_5_copy
, v_selected_5
,
1435 field_copy_tests_validate_diff_ptrs(v_selected_6_copy
, v_selected_6
,
1437 field_copy_tests_validate_diff_ptrs(a_copy
, a
, "a");
1438 field_copy_tests_validate_diff_ptrs(a_0_copy
, a_0
, "a_0");
1439 field_copy_tests_validate_diff_ptrs(a_1_copy
, a_1
, "a_1");
1440 field_copy_tests_validate_diff_ptrs(a_2_copy
, a_2
, "a_2");
1441 field_copy_tests_validate_diff_ptrs(a_3_copy
, a_3
, "a_3");
1442 field_copy_tests_validate_diff_ptrs(a_4_copy
, a_4
, "a_4");
1444 /* make sure copied fields share the same types */
1445 field_copy_tests_validate_same_type(strct_copy
, strct_type
, "strct");
1446 field_copy_tests_validate_same_type(len_copy
, len_type
, "len");
1447 field_copy_tests_validate_same_type(fp_copy
, fp_type
, "fp");
1448 field_copy_tests_validate_same_type(e_int_copy
, e_int_type
, "e_int");
1449 field_copy_tests_validate_same_type(e_copy
, e_type
, "e");
1450 field_copy_tests_validate_same_type(v_copy
, v_type
, "v");
1451 field_copy_tests_validate_same_type(v_selected_copy
, v_label2_seq_type
,
1453 field_copy_tests_validate_same_type(v_selected_0_copy
, v_label2_type
,
1455 field_copy_tests_validate_same_type(v_selected_1_copy
, v_label2_type
,
1457 field_copy_tests_validate_same_type(v_selected_2_copy
, v_label2_type
,
1459 field_copy_tests_validate_same_type(v_selected_3_copy
, v_label2_type
,
1461 field_copy_tests_validate_same_type(v_selected_4_copy
, v_label2_type
,
1463 field_copy_tests_validate_same_type(v_selected_5_copy
, v_label2_type
,
1465 field_copy_tests_validate_same_type(v_selected_6_copy
, v_label2_type
,
1467 field_copy_tests_validate_same_type(a_copy
, v_label1_array_type
, "a");
1468 field_copy_tests_validate_same_type(a_0_copy
, v_label1_type
, "a_0");
1469 field_copy_tests_validate_same_type(a_1_copy
, v_label1_type
, "a_1");
1470 field_copy_tests_validate_same_type(a_2_copy
, v_label1_type
, "a_2");
1471 field_copy_tests_validate_same_type(a_3_copy
, v_label1_type
, "a_3");
1472 field_copy_tests_validate_same_type(a_4_copy
, v_label1_type
, "a_4");
1474 /* validate len copy */
1475 ret
= bt_ctf_field_unsigned_integer_get_value(len_copy
, &uint64_t_val
);
1477 ok(uint64_t_val
== 7,
1478 "bt_ctf_field_copy creates a valid integer field copy");
1480 /* validate fp copy */
1481 ret
= bt_ctf_field_floating_point_get_value(fp_copy
, &double_val
);
1483 ok(double_val
== 3.14,
1484 "bt_ctf_field_copy creates a valid floating point number field copy");
1486 /* validate s copy */
1487 str_val
= bt_ctf_field_string_get_value(s_copy
);
1488 ok(str_val
&& !strcmp(str_val
, "btbt"),
1489 "bt_ctf_field_copy creates a valid string field copy");
1491 /* validate e_int copy */
1492 ret
= bt_ctf_field_unsigned_integer_get_value(e_int_copy
,
1495 ok(uint64_t_val
== 23,
1496 "bt_ctf_field_copy creates a valid enum's integer field copy");
1498 /* validate e copy */
1499 e_iter
= bt_ctf_field_enumeration_get_mappings(e_copy
);
1500 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter
,
1501 &str_val
, NULL
, NULL
);
1502 ok(str_val
&& !strcmp(str_val
, "LABEL2"),
1503 "bt_ctf_field_copy creates a valid enum field copy");
1505 /* validate v_selected copy */
1506 v_selected_copy_len
= bt_ctf_field_sequence_get_length(v_selected
);
1507 assert(v_selected_copy_len
);
1508 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len
,
1511 ok(uint64_t_val
== 7,
1512 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1513 bt_put(v_selected_copy_len
);
1514 v_selected_copy_len
= NULL
;
1516 /* validate v_selected copy fields */
1517 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy
,
1520 ok(uint64_t_val
== 7,
1521 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1522 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy
,
1525 ok(uint64_t_val
== 6,
1526 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1527 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy
,
1530 ok(uint64_t_val
== 5,
1531 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1532 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy
,
1535 ok(uint64_t_val
== 4,
1536 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1537 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy
,
1540 ok(uint64_t_val
== 3,
1541 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1542 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy
,
1545 ok(uint64_t_val
== 2,
1546 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1547 ret
= bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy
,
1550 ok(uint64_t_val
== 1,
1551 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1553 /* validate a copy fields */
1554 str_val
= bt_ctf_field_string_get_value(a_0_copy
);
1555 ok(str_val
&& !strcmp(str_val
, "a_0"),
1556 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1557 str_val
= bt_ctf_field_string_get_value(a_1_copy
);
1558 ok(str_val
&& !strcmp(str_val
, "a_1"),
1559 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1560 str_val
= bt_ctf_field_string_get_value(a_2_copy
);
1561 ok(str_val
&& !strcmp(str_val
, "a_2"),
1562 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1563 str_val
= bt_ctf_field_string_get_value(a_3_copy
);
1564 ok(str_val
&& !strcmp(str_val
, "a_3"),
1565 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1566 str_val
= bt_ctf_field_string_get_value(a_4_copy
);
1567 ok(str_val
&& !strcmp(str_val
, "a_4"),
1568 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1570 /* put everything */
1577 bt_put(v_label1_type
);
1578 bt_put(v_label1_array_type
);
1579 bt_put(v_label2_type
);
1580 bt_put(v_label2_seq_type
);
1589 bt_put(v_selected_0
);
1590 bt_put(v_selected_1
);
1591 bt_put(v_selected_2
);
1592 bt_put(v_selected_3
);
1593 bt_put(v_selected_4
);
1594 bt_put(v_selected_5
);
1595 bt_put(v_selected_6
);
1609 bt_put(v_selected_copy
);
1610 bt_put(v_selected_0_copy
);
1611 bt_put(v_selected_1_copy
);
1612 bt_put(v_selected_2_copy
);
1613 bt_put(v_selected_3_copy
);
1614 bt_put(v_selected_4_copy
);
1615 bt_put(v_selected_5_copy
);
1616 bt_put(v_selected_6_copy
);
1628 void type_field_tests()
1630 struct bt_ctf_field
*uint_12
;
1631 struct bt_ctf_field
*int_16
;
1632 struct bt_ctf_field
*string
;
1633 struct bt_ctf_field
*enumeration
;
1634 struct bt_ctf_field_type
*composite_structure_type
;
1635 struct bt_ctf_field_type
*structure_seq_type
;
1636 struct bt_ctf_field_type
*string_type
;
1637 struct bt_ctf_field_type
*sequence_type
;
1638 struct bt_ctf_field_type
*uint_8_type
;
1639 struct bt_ctf_field_type
*int_16_type
;
1640 struct bt_ctf_field_type
*uint_12_type
=
1641 bt_ctf_field_type_integer_create(12);
1642 struct bt_ctf_field_type
*enumeration_type
;
1643 struct bt_ctf_field_type
*returned_type
;
1644 const char *ret_string
;
1646 returned_type
= bt_ctf_field_get_type(NULL
);
1647 ok(!returned_type
, "bt_ctf_field_get_type handles NULL correctly");
1649 ok(uint_12_type
, "Create an unsigned integer type");
1650 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1651 BT_CTF_INTEGER_BASE_BINARY
) == 0,
1652 "Set integer type's base as binary");
1653 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1654 BT_CTF_INTEGER_BASE_DECIMAL
) == 0,
1655 "Set integer type's base as decimal");
1656 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1657 BT_CTF_INTEGER_BASE_UNKNOWN
),
1658 "Reject integer type's base set as unknown");
1659 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1660 BT_CTF_INTEGER_BASE_OCTAL
) == 0,
1661 "Set integer type's base as octal");
1662 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1663 BT_CTF_INTEGER_BASE_HEXADECIMAL
) == 0,
1664 "Set integer type's base as hexadecimal");
1665 ok(bt_ctf_field_type_integer_set_base(uint_12_type
, 457417),
1666 "Reject unknown integer base value");
1667 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 952835) == 0,
1668 "Set integer type signedness to signed");
1669 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0) == 0,
1670 "Set integer type signedness to unsigned");
1671 ok(bt_ctf_field_type_integer_get_size(NULL
) < 0,
1672 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1673 ok(bt_ctf_field_type_integer_get_size(uint_12_type
) == 12,
1674 "bt_ctf_field_type_integer_get_size returns a correct value");
1675 ok(bt_ctf_field_type_integer_get_signed(NULL
) < 0,
1676 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1677 ok(bt_ctf_field_type_integer_get_signed(uint_12_type
) == 0,
1678 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1680 ok(bt_ctf_field_type_set_byte_order(NULL
,
1681 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) < 0,
1682 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1683 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1684 (enum bt_ctf_byte_order
) 42) < 0,
1685 "bt_ctf_field_type_set_byte_order rejects invalid values");
1686 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1687 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
) == 0,
1688 "Set an integer's byte order to little endian");
1689 ok(bt_ctf_field_type_set_byte_order(uint_12_type
,
1690 BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
1691 "Set an integer's byte order to big endian");
1692 ok(bt_ctf_field_type_get_byte_order(uint_12_type
) ==
1693 BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
1694 "bt_ctf_field_type_get_byte_order returns a correct value");
1695 ok(bt_ctf_field_type_get_byte_order(NULL
) ==
1696 BT_CTF_BYTE_ORDER_UNKNOWN
,
1697 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1699 ok(bt_ctf_field_type_get_type_id(NULL
) ==
1700 BT_CTF_FIELD_TYPE_ID_UNKNOWN
,
1701 "bt_ctf_field_type_get_type_id handles NULL correctly");
1702 ok(bt_ctf_field_type_get_type_id(uint_12_type
) ==
1703 BT_CTF_FIELD_TYPE_ID_INTEGER
,
1704 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1706 ok(bt_ctf_field_type_integer_get_base(NULL
) ==
1707 BT_CTF_INTEGER_BASE_UNKNOWN
,
1708 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1709 ok(bt_ctf_field_type_integer_get_base(uint_12_type
) ==
1710 BT_CTF_INTEGER_BASE_HEXADECIMAL
,
1711 "bt_ctf_field_type_integer_get_base returns a correct value");
1713 ok(bt_ctf_field_type_integer_set_encoding(NULL
,
1714 BT_CTF_STRING_ENCODING_ASCII
) < 0,
1715 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1716 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1717 (enum bt_ctf_string_encoding
) 123) < 0,
1718 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1719 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type
,
1720 BT_CTF_STRING_ENCODING_UTF8
) == 0,
1721 "Set integer type encoding to UTF8");
1722 ok(bt_ctf_field_type_integer_get_encoding(NULL
) ==
1723 BT_CTF_STRING_ENCODING_UNKNOWN
,
1724 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1725 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type
) ==
1726 BT_CTF_STRING_ENCODING_UTF8
,
1727 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1729 int_16_type
= bt_ctf_field_type_integer_create(16);
1730 assert(int_16_type
);
1731 ok(!bt_ctf_field_type_integer_set_signed(int_16_type
, 1),
1732 "Set signedness of 16 bit integer to true");
1733 ok(bt_ctf_field_type_integer_get_signed(int_16_type
) == 1,
1734 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1735 uint_8_type
= bt_ctf_field_type_integer_create(8);
1737 bt_ctf_field_type_sequence_create(int_16_type
, "seq_len");
1738 ok(sequence_type
, "Create a sequence of int16_t type");
1739 ok(bt_ctf_field_type_get_type_id(sequence_type
) ==
1740 BT_CTF_FIELD_TYPE_ID_SEQUENCE
,
1741 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1743 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL
) == NULL
,
1744 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1745 ret_string
= bt_ctf_field_type_sequence_get_length_field_name(
1747 ok(!strcmp(ret_string
, "seq_len"),
1748 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1749 ok(bt_ctf_field_type_sequence_get_element_type(NULL
) == NULL
,
1750 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1751 returned_type
= bt_ctf_field_type_sequence_get_element_type(
1753 ok(returned_type
== int_16_type
,
1754 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1755 bt_put(returned_type
);
1757 string_type
= bt_ctf_field_type_string_create();
1758 ok(string_type
, "Create a string type");
1759 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1760 BT_CTF_STRING_ENCODING_NONE
),
1761 "Reject invalid \"None\" string encoding");
1762 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1764 "Reject invalid string encoding");
1765 ok(bt_ctf_field_type_string_set_encoding(string_type
,
1766 BT_CTF_STRING_ENCODING_ASCII
) == 0,
1767 "Set string encoding to ASCII");
1769 ok(bt_ctf_field_type_string_get_encoding(NULL
) ==
1770 BT_CTF_STRING_ENCODING_UNKNOWN
,
1771 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1772 ok(bt_ctf_field_type_string_get_encoding(string_type
) ==
1773 BT_CTF_STRING_ENCODING_ASCII
,
1774 "bt_ctf_field_type_string_get_encoding returns the correct value");
1776 structure_seq_type
= bt_ctf_field_type_structure_create();
1777 ok(bt_ctf_field_type_get_type_id(structure_seq_type
) ==
1778 BT_CTF_FIELD_TYPE_ID_STRUCT
,
1779 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1780 ok(structure_seq_type
, "Create a structure type");
1781 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1782 uint_8_type
, "seq_len") == 0,
1783 "Add a uint8_t type to a structure");
1784 ok(bt_ctf_field_type_structure_add_field(structure_seq_type
,
1785 sequence_type
, "a_sequence") == 0,
1786 "Add a sequence type to a structure");
1788 ok(bt_ctf_field_type_structure_get_field_count(NULL
) < 0,
1789 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1790 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type
) == 2,
1791 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1793 ok(bt_ctf_field_type_structure_get_field(NULL
,
1794 &ret_string
, &returned_type
, 1) < 0,
1795 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1796 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1797 NULL
, &returned_type
, 1) == 0,
1798 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1799 bt_put(returned_type
);
1800 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1801 &ret_string
, NULL
, 1) == 0,
1802 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1803 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1804 &ret_string
, &returned_type
, 10) < 0,
1805 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1806 ok(bt_ctf_field_type_structure_get_field(structure_seq_type
,
1807 &ret_string
, &returned_type
, 1) == 0,
1808 "bt_ctf_field_type_structure_get_field returns a field");
1809 ok(!strcmp(ret_string
, "a_sequence"),
1810 "bt_ctf_field_type_structure_get_field returns a correct field name");
1811 ok(returned_type
== sequence_type
,
1812 "bt_ctf_field_type_structure_get_field returns a correct field type");
1813 bt_put(returned_type
);
1815 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL
, "a_sequence") == NULL
,
1816 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1817 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type
, NULL
) == NULL
,
1818 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1819 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1820 structure_seq_type
, "a_sequence");
1821 ok(returned_type
== sequence_type
,
1822 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1823 bt_put(returned_type
);
1825 composite_structure_type
= bt_ctf_field_type_structure_create();
1826 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1827 string_type
, "a_string") == 0,
1828 "Add a string type to a structure");
1829 ok(bt_ctf_field_type_structure_add_field(composite_structure_type
,
1830 structure_seq_type
, "inner_structure") == 0,
1831 "Add a structure type to a structure");
1833 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1834 NULL
, "a_sequence") == NULL
,
1835 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1836 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1837 structure_seq_type
, NULL
) == NULL
,
1838 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1839 returned_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1840 structure_seq_type
, "a_sequence");
1841 ok(returned_type
== sequence_type
,
1842 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1843 bt_put(returned_type
);
1845 int_16
= bt_ctf_field_create(int_16_type
);
1846 ok(int_16
, "Instanciate a signed 16-bit integer");
1847 uint_12
= bt_ctf_field_create(uint_12_type
);
1848 ok(uint_12
, "Instanciate an unsigned 12-bit integer");
1849 returned_type
= bt_ctf_field_get_type(int_16
);
1850 ok(returned_type
== int_16_type
,
1851 "bt_ctf_field_get_type returns the correct type");
1853 /* Can't modify types after instanciating them */
1854 ok(bt_ctf_field_type_integer_set_base(uint_12_type
,
1855 BT_CTF_INTEGER_BASE_DECIMAL
),
1856 "Check an integer type' base can't be modified after instanciation");
1857 ok(bt_ctf_field_type_integer_set_signed(uint_12_type
, 0),
1858 "Check an integer type's signedness can't be modified after instanciation");
1860 /* Check signed property is checked */
1861 ok(bt_ctf_field_signed_integer_set_value(uint_12
, -52),
1862 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1863 ok(bt_ctf_field_unsigned_integer_set_value(int_16
, 42),
1864 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1866 /* Check overflows are properly tested for */
1867 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32768) == 0,
1868 "Check -32768 is allowed for a signed 16-bit integer");
1869 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32767) == 0,
1870 "Check 32767 is allowed for a signed 16-bit integer");
1871 ok(bt_ctf_field_signed_integer_set_value(int_16
, 32768),
1872 "Check 32768 is not allowed for a signed 16-bit integer");
1873 ok(bt_ctf_field_signed_integer_set_value(int_16
, -32769),
1874 "Check -32769 is not allowed for a signed 16-bit integer");
1875 ok(bt_ctf_field_signed_integer_set_value(int_16
, -42) == 0,
1876 "Check -42 is allowed for a signed 16-bit integer");
1878 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4095) == 0,
1879 "Check 4095 is allowed for an unsigned 12-bit integer");
1880 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 4096),
1881 "Check 4096 is not allowed for a unsigned 12-bit integer");
1882 ok(bt_ctf_field_unsigned_integer_set_value(uint_12
, 0) == 0,
1883 "Check 0 is allowed for an unsigned 12-bit integer");
1885 string
= bt_ctf_field_create(string_type
);
1886 ok(string
, "Instanciate a string field");
1887 ok(bt_ctf_field_string_set_value(string
, "A value") == 0,
1888 "Set a string's value");
1890 enumeration_type
= bt_ctf_field_type_enumeration_create(uint_12_type
);
1891 ok(enumeration_type
,
1892 "Create an enumeration type with an unsigned 12-bit integer as container");
1893 enumeration
= bt_ctf_field_create(enumeration_type
);
1895 "Check enumeration types are validated before instantiation");
1900 bt_put(enumeration
);
1901 bt_put(composite_structure_type
);
1902 bt_put(structure_seq_type
);
1903 bt_put(string_type
);
1904 bt_put(sequence_type
);
1905 bt_put(uint_8_type
);
1906 bt_put(int_16_type
);
1907 bt_put(uint_12_type
);
1908 bt_put(enumeration_type
);
1909 bt_put(returned_type
);
1913 void packet_resize_test(struct bt_ctf_stream_class
*stream_class
,
1914 struct bt_ctf_stream
*stream
, struct bt_ctf_clock
*clock
)
1917 * Append enough events to force the underlying packet to be resized.
1918 * Also tests that a new event can be declared after a stream has been
1919 * instantiated and used/flushed.
1923 struct bt_ctf_event_class
*event_class
= bt_ctf_event_class_create(
1925 struct bt_ctf_field_type
*integer_type
=
1926 bt_ctf_field_type_integer_create(17);
1927 struct bt_ctf_field_type
*string_type
=
1928 bt_ctf_field_type_string_create();
1929 struct bt_ctf_event
*event
= NULL
;
1930 struct bt_ctf_field
*ret_field
= NULL
;
1931 struct bt_ctf_field_type
*ret_field_type
= NULL
;
1932 uint64_t ret_uint64
;
1933 int events_appended
= 0;
1934 struct bt_ctf_field
*packet_context
= NULL
,
1935 *packet_context_field
= NULL
, *stream_event_context
= NULL
;
1936 struct bt_ctf_field_type
*ep_field_1_type
= NULL
;
1937 struct bt_ctf_field_type
*ep_a_string_type
= NULL
;
1938 struct bt_ctf_field_type
*ep_type
= NULL
;
1940 ret
|= bt_ctf_event_class_add_field(event_class
, integer_type
,
1942 ret
|= bt_ctf_event_class_add_field(event_class
, string_type
,
1944 ret
|= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
1945 ok(ret
== 0, "Add a new event class to a stream class after writing an event");
1951 * bt_ctf_stream_class_add_event_class() copies the field types
1952 * of event_class, so we retrieve the new ones to create the
1953 * appropriate fields.
1955 ep_type
= bt_ctf_event_class_get_payload_type(event_class
);
1957 ep_field_1_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1958 ep_type
, "field_1");
1959 assert(ep_field_1_type
);
1960 ep_a_string_type
= bt_ctf_field_type_structure_get_field_type_by_name(
1961 ep_type
, "a_string");
1962 assert(ep_a_string_type
);
1964 event
= bt_ctf_event_create(event_class
);
1965 ret_field
= bt_ctf_event_get_payload_by_index(event
, 0);
1966 ret_field_type
= bt_ctf_field_get_type(ret_field
);
1967 ok(bt_ctf_field_type_compare(ret_field_type
, integer_type
) == 0,
1968 "bt_ctf_event_get_payload_by_index returns a correct field");
1969 bt_put(ret_field_type
);
1972 ok(bt_ctf_event_get_payload_by_index(NULL
, 0) == NULL
,
1973 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1974 ok(bt_ctf_event_get_payload_by_index(event
, 4) == NULL
,
1975 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1978 for (i
= 0; i
< packet_resize_test_length
; i
++) {
1979 event
= bt_ctf_event_create(event_class
);
1980 struct bt_ctf_field
*integer
=
1981 bt_ctf_field_create(ep_field_1_type
);
1982 struct bt_ctf_field
*string
=
1983 bt_ctf_field_create(ep_a_string_type
);
1985 ret
|= bt_ctf_clock_set_time(clock
, ++current_time
);
1986 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
1987 ret
|= bt_ctf_event_set_payload(event
, "field_1",
1990 ret
|= bt_ctf_field_string_set_value(string
, "This is a test");
1991 ret
|= bt_ctf_event_set_payload(event
, "a_string",
1995 /* Populate stream event context */
1996 stream_event_context
=
1997 bt_ctf_event_get_stream_event_context(event
);
1998 integer
= bt_ctf_field_structure_get_field(stream_event_context
,
1999 "common_event_context");
2000 BT_PUT(stream_event_context
);
2001 ret
|= bt_ctf_field_unsigned_integer_set_value(integer
,
2005 ret
|= bt_ctf_stream_append_event(stream
, event
);
2013 events_appended
= !!(i
== packet_resize_test_length
);
2014 ok(bt_ctf_stream_get_discarded_events_count(NULL
, &ret_uint64
) < 0,
2015 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2016 ok(bt_ctf_stream_get_discarded_events_count(stream
, NULL
) < 0,
2017 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2018 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2019 ok(ret
== 0 && ret_uint64
== 0,
2020 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2021 bt_ctf_stream_append_discarded_events(stream
, 1000);
2022 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2023 ok(ret
== 0 && ret_uint64
== 1000,
2024 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2027 ok(events_appended
, "Append 100 000 events to a stream");
2030 * Populate the custom packet context field with a dummy value
2031 * otherwise flush will fail.
2033 packet_context
= bt_ctf_stream_get_packet_context(stream
);
2034 packet_context_field
= bt_ctf_field_structure_get_field(packet_context
,
2035 "custom_packet_context_field");
2036 bt_ctf_field_unsigned_integer_set_value(packet_context_field
, 2);
2038 ok(bt_ctf_stream_flush(stream
) == 0,
2039 "Flush a stream that forces a packet resize");
2040 ret
= bt_ctf_stream_get_discarded_events_count(stream
, &ret_uint64
);
2041 ok(ret
== 0 && ret_uint64
== 1000,
2042 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2043 bt_put(integer_type
);
2044 bt_put(string_type
);
2045 bt_put(packet_context
);
2046 bt_put(packet_context_field
);
2047 bt_put(stream_event_context
);
2048 bt_put(event_class
);
2049 bt_put(ep_field_1_type
);
2050 bt_put(ep_a_string_type
);
2055 void test_empty_stream(struct bt_ctf_writer
*writer
)
2058 struct bt_ctf_trace
*trace
= NULL
, *ret_trace
= NULL
;
2059 struct bt_ctf_stream_class
*stream_class
= NULL
;
2060 struct bt_ctf_stream
*stream
= NULL
;
2062 trace
= bt_ctf_writer_get_trace(writer
);
2064 diag("Failed to get trace from writer");
2069 stream_class
= bt_ctf_stream_class_create("empty_stream");
2070 if (!stream_class
) {
2071 diag("Failed to create stream class");
2076 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2078 ret
= bt_ctf_stream_class_set_event_header_type(stream_class
, NULL
);
2081 ok(bt_ctf_stream_class_get_trace(NULL
) == NULL
,
2082 "bt_ctf_stream_class_get_trace handles NULL correctly");
2083 ok(bt_ctf_stream_class_get_trace(stream_class
) == NULL
,
2084 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2086 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2088 diag("Failed to create writer stream");
2093 ret_trace
= bt_ctf_stream_class_get_trace(stream_class
);
2094 ok(ret_trace
== trace
,
2095 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2098 "Created a stream class with default attributes and an empty stream");
2102 bt_put(stream_class
);
2106 void test_custom_event_header_stream(struct bt_ctf_writer
*writer
,
2107 struct bt_ctf_clock
*clock
)
2110 struct bt_ctf_stream_class
*stream_class
= NULL
;
2111 struct bt_ctf_stream
*stream
= NULL
;
2112 struct bt_ctf_field_type
*integer_type
= NULL
,
2113 *sequence_type
= NULL
, *event_header_type
= NULL
;
2114 struct bt_ctf_field
*integer
= NULL
, *sequence
= NULL
,
2115 *event_header
= NULL
, *packet_header
= NULL
;
2116 struct bt_ctf_event_class
*event_class
= NULL
;
2117 struct bt_ctf_event
*event
= NULL
;
2119 stream_class
= bt_ctf_stream_class_create("custom_event_header_stream");
2120 if (!stream_class
) {
2121 fail("Failed to create stream class");
2125 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2127 fail("Failed to set stream class clock");
2132 * Customize event header to add an "seq_len" integer member
2133 * which will be used as the length of a sequence in an event of this
2136 event_header_type
= bt_ctf_stream_class_get_event_header_type(
2138 if (!event_header_type
) {
2139 fail("Failed to get event header type");
2143 integer_type
= bt_ctf_field_type_integer_create(13);
2144 if (!integer_type
) {
2145 fail("Failed to create length integer type");
2149 ret
= bt_ctf_field_type_structure_add_field(event_header_type
,
2150 integer_type
, "seq_len");
2152 fail("Failed to add a new field to stream event header");
2156 event_class
= bt_ctf_event_class_create("sequence_event");
2158 fail("Failed to create event class");
2163 * This event's payload will contain a sequence which references
2164 * stream.event.header.seq_len as its length field.
2166 sequence_type
= bt_ctf_field_type_sequence_create(integer_type
,
2167 "stream.event.header.seq_len");
2168 if (!sequence_type
) {
2169 fail("Failed to create a sequence");
2173 ret
= bt_ctf_event_class_add_field(event_class
, sequence_type
,
2176 fail("Failed to add a sequence to an event class");
2180 ret
= bt_ctf_stream_class_add_event_class(stream_class
, event_class
);
2182 fail("Failed to add event class to stream class");
2186 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2188 fail("Failed to create stream")
2193 * We have defined a custom packet header field. We have to populate it
2196 packet_header
= bt_ctf_stream_get_packet_header(stream
);
2197 if (!packet_header
) {
2198 fail("Failed to get stream packet header");
2202 integer
= bt_ctf_field_structure_get_field(packet_header
,
2203 "custom_trace_packet_header_field");
2205 fail("Failed to retrieve custom_trace_packet_header_field");
2209 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 3487);
2211 fail("Failed to set custom_trace_packet_header_field value");
2216 event
= bt_ctf_event_create(event_class
);
2218 fail("Failed to create event");
2222 event_header
= bt_ctf_event_get_header(event
);
2223 if (!event_header
) {
2224 fail("Failed to get event header");
2228 integer
= bt_ctf_field_structure_get_field(event_header
,
2231 fail("Failed to get seq_len field from event header");
2235 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 2);
2237 fail("Failed to set seq_len value in event header");
2241 /* Populate both sequence integer fields */
2242 sequence
= bt_ctf_event_get_payload(event
, "some_sequence");
2244 fail("Failed to retrieve sequence from event");
2248 ret
= bt_ctf_field_sequence_set_length(sequence
, integer
);
2250 fail("Failed to set sequence length");
2255 for (i
= 0; i
< 2; i
++) {
2256 integer
= bt_ctf_field_sequence_get_field(sequence
, i
);
2258 fail("Failed to retrieve sequence element");
2262 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, i
);
2264 fail("Failed to set sequence element value");
2272 ret
= bt_ctf_stream_append_event(stream
, event
);
2274 fail("Failed to append event to stream");
2278 ret
= bt_ctf_stream_flush(stream
);
2280 fail("Failed to flush custom_event_header stream");
2284 bt_put(stream_class
);
2285 bt_put(event_class
);
2289 bt_put(event_header
);
2290 bt_put(packet_header
);
2291 bt_put(sequence_type
);
2292 bt_put(integer_type
);
2293 bt_put(event_header_type
);
2297 void test_instanciate_event_before_stream(struct bt_ctf_writer
*writer
,
2298 struct bt_ctf_clock
*clock
)
2301 struct bt_ctf_stream_class
*stream_class
= NULL
;
2302 struct bt_ctf_stream
*stream
= NULL
,
2304 struct bt_ctf_event_class
*event_class
= NULL
;
2305 struct bt_ctf_event
*event
= NULL
;
2306 struct bt_ctf_field_type
*integer_type
= NULL
;
2307 struct bt_ctf_field
*integer
= NULL
;
2309 stream_class
= bt_ctf_stream_class_create("event_before_stream_test");
2310 if (!stream_class
) {
2311 diag("Failed to create stream class");
2316 ret
= bt_ctf_stream_class_set_clock(stream_class
, clock
);
2318 diag("Failed to set stream class clock");
2322 event_class
= bt_ctf_event_class_create("some_event_class_name");
2323 integer_type
= bt_ctf_field_type_integer_create(32);
2324 if (!integer_type
) {
2325 diag("Failed to create integer field type");
2330 ret
= bt_ctf_event_class_add_field(event_class
, integer_type
,
2333 diag("Failed to add field to event class");
2337 ret
= bt_ctf_stream_class_add_event_class(stream_class
,
2340 diag("Failed to add event class to stream class");
2343 event
= bt_ctf_event_create(event_class
);
2345 diag("Failed to create event");
2350 integer
= bt_ctf_event_get_payload_by_index(event
, 0);
2352 diag("Failed to get integer field payload from event");
2357 ret
= bt_ctf_field_unsigned_integer_set_value(integer
, 1234);
2359 diag("Failed to set integer field value");
2363 stream
= bt_ctf_writer_create_stream(writer
, stream_class
);
2365 diag("Failed to create writer stream");
2370 ok(bt_ctf_event_get_stream(NULL
) == NULL
,
2371 "bt_ctf_event_get_stream handles NULL correctly");
2372 ok(bt_ctf_event_get_stream(event
) == NULL
,
2373 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2375 ret
= bt_ctf_stream_append_event(stream
, event
);
2377 diag("Failed to append event to stream");
2381 ret_stream
= bt_ctf_event_get_stream(event
);
2382 ok(ret_stream
== stream
,
2383 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2386 "Create an event before instanciating its associated stream");
2389 bt_put(stream_class
);
2390 bt_put(event_class
);
2392 bt_put(integer_type
);
2397 void append_existing_event_class(struct bt_ctf_stream_class
*stream_class
)
2399 struct bt_ctf_event_class
*event_class
;
2401 event_class
= bt_ctf_event_class_create("Simple Event");
2402 assert(event_class
);
2403 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
) == 0,
2404 "two event classes with the same name may cohabit within the same stream class");
2405 bt_put(event_class
);
2407 event_class
= bt_ctf_event_class_create("different name, ok");
2408 assert(event_class
);
2409 assert(!bt_ctf_event_class_set_id(event_class
, 13));
2410 ok(bt_ctf_stream_class_add_event_class(stream_class
, event_class
),
2411 "two event classes with the same ID cannot cohabit within the same stream class");
2412 bt_put(event_class
);
2416 struct bt_ctf_event_class
*create_minimal_event_class(void)
2418 struct bt_ctf_event_class
*ec
= NULL
;
2419 struct bt_ctf_field_type
*int_ft
= NULL
;
2422 int_ft
= bt_ctf_field_type_integer_create(23);
2424 ec
= bt_ctf_event_class_create("minimal");
2426 ret
= bt_ctf_event_class_add_field(ec
, int_ft
, "field");
2434 void test_create_writer_vs_non_writer_mode(void)
2438 const char *writer_stream_name
= "writer stream instance";
2439 struct bt_ctf_writer
*writer
= NULL
;
2440 struct bt_ctf_trace
*writer_trace
= NULL
;
2441 struct bt_ctf_stream_class
*writer_sc
= NULL
;
2442 struct bt_ctf_stream
*writer_stream
= NULL
;
2443 struct bt_ctf_stream
*writer_stream2
= NULL
;
2444 struct bt_ctf_stream
*packet_stream
= NULL
;
2445 struct bt_ctf_trace
*non_writer_trace
= NULL
;
2446 struct bt_ctf_stream_class
*non_writer_sc
= NULL
;
2447 struct bt_ctf_stream
*non_writer_stream
= NULL
;
2448 struct bt_ctf_stream
*non_writer_stream2
= NULL
;
2449 struct bt_ctf_event_class
*writer_ec
= NULL
;
2450 struct bt_ctf_event_class
*non_writer_ec
= NULL
;
2451 struct bt_ctf_event
*event
= NULL
;
2452 struct bt_ctf_event
*event2
= NULL
;
2453 struct bt_ctf_field_type
*empty_struct_ft
= NULL
;
2454 struct bt_ctf_field
*int_field
= NULL
;
2455 struct bt_ctf_clock
*writer_clock
= NULL
;
2456 struct bt_ctf_clock_class
*non_writer_clock_class
= NULL
;
2457 struct bt_ctf_packet
*packet
= NULL
;
2458 struct bt_ctf_packet
*packet2
= NULL
;
2460 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
2461 if (!bt_mkdtemp(trace_path
)) {
2465 /* Create empty structure field type (event header) */
2466 empty_struct_ft
= bt_ctf_field_type_structure_create();
2467 assert(empty_struct_ft
);
2469 /* Create writer, writer stream class, stream, and clock */
2470 writer
= bt_ctf_writer_create(trace_path
);
2472 writer_clock
= bt_ctf_clock_create("writer_clock");
2473 assert(writer_clock
);
2474 ret
= bt_ctf_writer_add_clock(writer
, writer_clock
);
2476 ret
= bt_ctf_writer_set_byte_order(writer
, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN
);
2478 writer_trace
= bt_ctf_writer_get_trace(writer
);
2479 ok(writer_trace
, "bt_ctf_writer_get_trace() returns a trace");
2480 writer_sc
= bt_ctf_stream_class_create("writer_sc");
2482 ret
= bt_ctf_stream_class_set_event_header_type(writer_sc
,
2485 ret
= bt_ctf_stream_class_set_clock(writer_sc
, writer_clock
);
2487 ret
= bt_ctf_trace_add_stream_class(writer_trace
, writer_sc
);
2489 writer_stream
= bt_ctf_stream_create(writer_sc
, writer_stream_name
);
2490 assert(writer_stream
);
2491 ok(!strcmp(bt_ctf_stream_get_name(writer_stream
), writer_stream_name
),
2492 "bt_ctf_stream_get_name() returns the stream's name");
2494 /* Create non-writer trace, stream class, stream, and clock */
2495 non_writer_trace
= bt_ctf_trace_create();
2496 assert(non_writer_trace
);
2497 non_writer_sc
= bt_ctf_stream_class_create("nonwriter_sc");
2498 assert(non_writer_sc
);
2499 ret
= bt_ctf_stream_class_set_event_header_type(non_writer_sc
,
2502 ret
= bt_ctf_stream_class_set_packet_context_type(non_writer_sc
, NULL
);
2504 ret
= bt_ctf_trace_add_stream_class(non_writer_trace
, non_writer_sc
);
2506 non_writer_stream
= bt_ctf_stream_create(non_writer_sc
, NULL
);
2507 assert(non_writer_stream
);
2508 non_writer_clock_class
=
2509 bt_ctf_clock_class_create("non_writer_clock_class",
2511 assert(non_writer_clock_class
);
2512 ret
= bt_ctf_trace_add_clock_class(non_writer_trace
,
2513 non_writer_clock_class
);
2516 /* Create event class and event */
2517 writer_ec
= create_minimal_event_class();
2519 ret
= bt_ctf_stream_class_add_event_class(writer_sc
, writer_ec
);
2521 event
= bt_ctf_event_create(writer_ec
);
2523 int_field
= bt_ctf_event_get_payload_by_index(event
, 0);
2525 bt_ctf_field_unsigned_integer_set_value(int_field
, 17);
2528 * Verify non-writer stream: it should be impossible to append
2531 ok(bt_ctf_stream_append_event(non_writer_stream
, event
),
2532 "bt_ctf_stream_append_event() fails with a non-writer stream");
2535 * Verify writer stream: it should be possible to append an
2538 ok(!bt_ctf_stream_append_event(writer_stream
, event
),
2539 "bt_ctf_stream_append_event() succeeds with a writer stream");
2542 * It should be possible to create a packet from a non-writer
2543 * stream, but not from a writer stream.
2545 packet
= bt_ctf_packet_create(writer_stream
);
2546 ok(!packet
, "bt_ctf_packet_create() fails with a writer stream");
2547 packet
= bt_ctf_packet_create(non_writer_stream
);
2548 ok(packet
, "bt_ctf_packet_create() succeeds with a non-writer stream");
2549 packet_stream
= bt_ctf_packet_get_stream(packet
);
2550 ok(packet_stream
== non_writer_stream
,
2551 "bt_ctf_packet_get_stream() returns the correct stream");
2554 * It should not be possible to append an event associated to
2555 * a stream to a different stream.
2557 writer_stream2
= bt_ctf_stream_create(writer_sc
, "zoo");
2558 assert(writer_stream2
);
2559 ok(bt_ctf_stream_append_event(writer_stream2
, event
),
2560 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2563 * It should not be possible to set the packet of an event
2564 * associated to a given stream to a packet associated with
2565 * a different stream.
2567 ok(bt_ctf_event_set_packet(event
, packet
),
2568 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2571 * It should be possible to set the packet of a fresh event, as
2572 * long as the originating stream classes are the same.
2574 event2
= bt_ctf_event_create(writer_ec
);
2576 ok(bt_ctf_event_set_packet(event2
, packet
),
2577 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2578 non_writer_ec
= create_minimal_event_class();
2579 assert(non_writer_ec
);
2580 ret
= bt_ctf_stream_class_add_event_class(non_writer_sc
, non_writer_ec
);
2583 event2
= bt_ctf_event_create(non_writer_ec
);
2585 ok(!bt_ctf_event_set_packet(event2
, packet
),
2586 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2589 * It should be possible to set a packet created from the same
2590 * stream to an event with an existing packet.
2592 packet2
= bt_ctf_packet_create(non_writer_stream
);
2594 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2595 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2599 * It should not be possible to set a packet created from a
2600 * different stream to an event with an existing packet.
2602 non_writer_stream2
= bt_ctf_stream_create(non_writer_sc
, "rj45");
2603 assert(non_writer_stream2
);
2604 packet2
= bt_ctf_packet_create(non_writer_stream
);
2606 ok(!bt_ctf_event_set_packet(event2
, packet2
),
2607 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2610 bt_put(writer_trace
);
2612 bt_put(writer_stream
);
2613 bt_put(writer_stream2
);
2614 bt_put(non_writer_trace
);
2615 bt_put(non_writer_sc
);
2616 bt_put(non_writer_stream
);
2617 bt_put(non_writer_stream2
);
2618 bt_put(packet_stream
);
2620 bt_put(non_writer_ec
);
2624 bt_put(empty_struct_ft
);
2625 bt_put(writer_clock
);
2626 bt_put(non_writer_clock_class
);
2629 recursive_rmdir(trace_path
);
2634 void test_clock_utils(void)
2637 struct bt_ctf_clock
*clock
= NULL
;
2639 clock
= bt_ctf_clock_create("water");
2641 ret
= bt_ctf_clock_set_offset_s(clock
, 1234);
2643 ret
= bt_ctf_clock_set_offset(clock
, 1000);
2645 ret
= bt_ctf_clock_set_frequency(clock
, 1000000000);
2647 ret
= bt_ctf_clock_set_frequency(clock
, 1534);
2653 void test_set_clock_non_writer_stream_class(void)
2655 struct bt_ctf_clock
*clock
;
2656 struct bt_ctf_trace
*trace
;
2657 struct bt_ctf_stream_class
*sc
;
2660 clock
= bt_ctf_clock_create("the_clock");
2663 trace
= bt_ctf_trace_create();
2666 sc
= bt_ctf_stream_class_create(NULL
);
2669 ret
= bt_ctf_stream_class_set_clock(sc
, clock
);
2672 ret
= bt_ctf_trace_add_stream_class(trace
, sc
);
2674 "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock");
2682 void test_static_trace(void)
2684 struct bt_ctf_trace
*trace
;
2685 struct bt_ctf_stream_class
*stream_class
;
2686 struct bt_ctf_stream_class
*stream_class2
;
2687 struct bt_ctf_stream
*stream
;
2688 struct bt_ctf_clock_class
*clock_class
;
2691 trace
= bt_ctf_trace_create();
2693 stream_class
= bt_ctf_stream_class_create(NULL
);
2694 assert(stream_class
);
2695 ret
= bt_ctf_stream_class_set_packet_context_type(stream_class
, NULL
);
2697 ret
= bt_ctf_trace_add_stream_class(trace
, stream_class
);
2699 stream
= bt_ctf_stream_create(stream_class
, "hello");
2700 ok(stream
, "bt_ctf_stream_create() succeeds with a non-static trace");
2702 ok(!bt_ctf_trace_is_static(trace
),
2703 "bt_ctf_trace_is_static() returns the expected value");
2704 ok(bt_ctf_trace_set_is_static(trace
) == 0,
2705 "bt_ctf_trace_set_is_static() succeeds");
2706 ok(bt_ctf_trace_is_static(trace
),
2707 "bt_ctf_trace_is_static() returns the expected value");
2708 clock_class
= bt_ctf_clock_class_create("yes", 1000000000);
2709 assert(clock_class
);
2710 stream_class2
= bt_ctf_stream_class_create(NULL
);
2711 assert(stream_class2
);
2712 ok(bt_ctf_trace_add_stream_class(trace
, stream_class2
),
2713 "bt_ctf_trace_add_stream_class() fails with a static trace");
2714 ok(bt_ctf_trace_add_clock_class(trace
, clock_class
),
2715 "bt_ctf_trace_add_clock_class() fails with a static trace");
2716 ok(!bt_ctf_stream_create(stream_class
, "hello2"),
2717 "bt_ctf_stream_create() fails with a static trace");
2720 bt_put(stream_class
);
2721 bt_put(stream_class2
);
2722 bt_put(clock_class
);
2726 void trace_is_static_listener(struct bt_ctf_trace
*trace
, void *data
)
2728 *((int *) data
) |= 1;
2732 void trace_listener_removed(struct bt_ctf_trace
*trace
, void *data
)
2734 *((int *) data
) |= 2;
2738 void test_trace_is_static_listener(void)
2740 struct bt_ctf_trace
*trace
;
2751 trace
= bt_ctf_trace_create();
2753 ret
= bt_ctf_trace_add_is_static_listener(NULL
,
2754 trace_is_static_listener
, trace_listener_removed
, &called1
);
2755 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (trace)");
2756 ret
= bt_ctf_trace_add_is_static_listener(trace
, NULL
,
2757 trace_listener_removed
, &called1
);
2758 ok(ret
< 0, "bt_ctf_trace_add_is_static_listener() handles NULL (listener)");
2759 listener1_id
= bt_ctf_trace_add_is_static_listener(trace
,
2760 trace_is_static_listener
, trace_listener_removed
, &called1
);
2761 ok(listener1_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (1)");
2762 listener2_id
= bt_ctf_trace_add_is_static_listener(trace
,
2763 trace_is_static_listener
, trace_listener_removed
, &called2
);
2764 ok(listener2_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (2)");
2765 listener3_id
= bt_ctf_trace_add_is_static_listener(trace
,
2766 trace_is_static_listener
, trace_listener_removed
, &called3
);
2767 ok(listener3_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (3)");
2768 ret
= bt_ctf_trace_remove_is_static_listener(NULL
, 0);
2769 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles NULL (trace)");
2770 ret
= bt_ctf_trace_remove_is_static_listener(trace
, -2);
2771 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (negative)");
2772 ret
= bt_ctf_trace_remove_is_static_listener(trace
, 77);
2773 ok(ret
< 0, "bt_ctf_trace_remove_is_static_listener() handles invalid ID (non existing)");
2774 ret
= bt_ctf_trace_remove_is_static_listener(trace
, listener2_id
);
2775 ok(ret
== 0, "bt_ctf_trace_remove_is_static_listener() succeeds");
2776 ok(called2
== 2, "bt_ctf_trace_remove_is_static_listener() calls the remove listener");
2777 listener4_id
= bt_ctf_trace_add_is_static_listener(trace
,
2778 trace_is_static_listener
, NULL
, &called4
);
2779 ok(listener4_id
>= 0, "bt_ctf_trace_add_is_static_listener() succeeds (4)");
2780 ok(called1
== 0, "\"trace is static\" listener not called before the trace is made static (1)");
2781 ok(called2
== 2, "\"trace is static\" listener not called before the trace is made static (2)");
2782 ok(called3
== 0, "\"trace is static\" listener not called before the trace is made static (3)");
2783 ok(called4
== 0, "\"trace is static\" listener not called before the trace is made static (4)");
2784 ret
= bt_ctf_trace_set_is_static(trace
);
2786 ret
= bt_ctf_trace_add_is_static_listener(trace
,
2787 trace_is_static_listener
, trace_listener_removed
, &called1
);
2789 "bt_ctf_trace_add_is_static_listener() fails when the trace is static");
2790 ok(called1
== 1, "\"trace is static\" listener called when the trace is made static (1)");
2791 ok(called2
== 2, "\"trace is static\" listener not called when the trace is made static (2)");
2792 ok(called3
== 1, "\"trace is static\" listener called when the trace is made static (3)");
2793 ok(called4
== 1, "\"trace is static\" listener called when the trace is made static (4)");
2799 ok(called1
== 2, "\"trace is static\" listener not called after the trace is put (1)");
2800 ok(called2
== 0, "\"trace is static\" listener not called after the trace is put (2)");
2801 ok(called3
== 2, "\"trace is static\" listener not called after the trace is put (3)");
2802 ok(called4
== 0, "\"trace is static\" listener not called after the trace is put (4)");
2806 void test_trace_uuid(void)
2808 struct bt_ctf_trace
*trace
;
2809 const unsigned char uuid
[] = {
2810 0x35, 0x92, 0x63, 0xab, 0xb4, 0xbe, 0x40, 0xb4,
2811 0xb2, 0x60, 0xd3, 0xf1, 0x3b, 0xb0, 0xd8, 0x59,
2813 const unsigned char *ret_uuid
;
2815 trace
= bt_ctf_trace_create();
2817 ok(!bt_ctf_trace_get_uuid(NULL
),
2818 "bt_ctf_trace_get_uuid() handles NULL");
2819 ok(!bt_ctf_trace_get_uuid(trace
),
2820 "bt_ctf_trace_get_uuid() returns NULL initially");
2821 ok(bt_ctf_trace_set_uuid(NULL
, uuid
),
2822 "bt_ctf_trace_set_uuid() handles NULL (trace)");
2823 ok(bt_ctf_trace_set_uuid(trace
, NULL
),
2824 "bt_ctf_trace_set_uuid() handles NULL (UUID)");
2825 ok(bt_ctf_trace_set_uuid(trace
, uuid
) == 0,
2826 "bt_ctf_trace_set_uuid() succeeds with a valid UUID");
2827 ret_uuid
= bt_ctf_trace_get_uuid(trace
);
2828 ok(ret_uuid
, "bt_ctf_trace_get_uuid() returns a UUID");
2830 ok(memcmp(uuid
, ret_uuid
, 16) == 0,
2831 "bt_ctf_trace_get_uuid() returns the expected UUID");
2836 int main(int argc
, char **argv
)
2838 const char *env_resize_length
;
2840 gchar
*metadata_path
;
2841 const char *clock_name
= "test_clock";
2842 const char *clock_description
= "This is a test clock";
2843 const char *returned_clock_name
;
2844 const char *returned_clock_description
;
2845 const uint64_t frequency
= 1123456789;
2846 const int64_t offset_s
= 13515309;
2847 const int64_t offset
= 1234567;
2848 int64_t get_offset_s
,
2850 const uint64_t precision
= 10;
2851 const int is_absolute
= 0xFF;
2852 char *metadata_string
;
2853 struct bt_ctf_writer
*writer
;
2854 struct utsname name
;
2855 char hostname
[BABELTRACE_HOST_NAME_MAX
];
2856 struct bt_ctf_clock
*clock
, *ret_clock
;
2857 struct bt_ctf_clock_class
*ret_clock_class
;
2858 struct bt_ctf_stream_class
*stream_class
, *ret_stream_class
;
2859 struct bt_ctf_stream
*stream1
;
2860 struct bt_ctf_stream
*stream
;
2861 const char *ret_string
;
2862 const unsigned char *ret_uuid
;
2863 unsigned char tmp_uuid
[16] = { 0 };
2864 struct bt_ctf_field_type
*packet_context_type
,
2865 *packet_context_field_type
,
2866 *packet_header_type
,
2867 *packet_header_field_type
,
2869 *stream_event_context_type
,
2871 *event_header_field_type
;
2872 struct bt_ctf_field
*packet_header
, *packet_header_field
;
2873 struct bt_ctf_trace
*trace
;
2875 int64_t ret_int64_t
;
2876 struct bt_value
*obj
;
2879 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
2883 env_resize_length
= getenv("PACKET_RESIZE_TEST_LENGTH");
2884 if (env_resize_length
) {
2885 packet_resize_test_length
=
2886 (unsigned int) atoi(env_resize_length
);
2889 plan_tests(NR_TESTS
);
2891 trace_path
= g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL
);
2892 if (!bt_mkdtemp(trace_path
)) {
2896 metadata_path
= g_build_filename(trace_path
, "metadata", NULL
);
2898 writer
= bt_ctf_writer_create(trace_path
);
2899 ok(writer
, "bt_ctf_create succeeds in creating trace with path");
2901 ok(!bt_ctf_writer_get_trace(NULL
),
2902 "bt_ctf_writer_get_trace correctly handles NULL");
2903 trace
= bt_ctf_writer_get_trace(writer
);
2904 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_NATIVE
),
2905 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_NATIVE");
2906 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_UNSPECIFIED
),
2907 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_UNSPECIFIED");
2909 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2910 ok(bt_ctf_trace_set_native_byte_order(trace
, BT_CTF_BYTE_ORDER_BIG_ENDIAN
) == 0,
2911 "Set a trace's byte order to big endian");
2912 ok(bt_ctf_trace_get_native_byte_order(trace
) == BT_CTF_BYTE_ORDER_BIG_ENDIAN
,
2913 "bt_ctf_trace_get_native_byte_order returns a correct endianness");
2915 /* Add environment context to the trace */
2916 ret
= gethostname(hostname
, sizeof(hostname
));
2920 ok(bt_ctf_writer_add_environment_field(writer
, "host", hostname
) == 0,
2921 "Add host (%s) environment field to writer instance",
2923 ok(bt_ctf_writer_add_environment_field(NULL
, "test_field",
2925 "bt_ctf_writer_add_environment_field error with NULL writer");
2926 ok(bt_ctf_writer_add_environment_field(writer
, NULL
,
2928 "bt_ctf_writer_add_environment_field error with NULL field name");
2929 ok(bt_ctf_writer_add_environment_field(writer
, "test_field",
2931 "bt_ctf_writer_add_environment_field error with NULL field value");
2933 /* Test bt_ctf_trace_set_environment_field with an integer object */
2934 obj
= bt_value_integer_create_init(23);
2936 ok(bt_ctf_trace_set_environment_field(NULL
, "test_env_int_obj", obj
),
2937 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2938 ok(bt_ctf_trace_set_environment_field(trace
, NULL
, obj
),
2939 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2940 ok(bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", NULL
),
2941 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2942 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_int_obj", obj
),
2943 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2946 /* Test bt_ctf_trace_set_environment_field with a string object */
2947 obj
= bt_value_string_create_init("the value");
2949 ok(!bt_ctf_trace_set_environment_field(trace
, "test_env_str_obj", obj
),
2950 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2953 /* Test bt_ctf_trace_set_environment_field_integer */
2954 ok(bt_ctf_trace_set_environment_field_integer(NULL
, "test_env_int",
2956 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2957 ok(bt_ctf_trace_set_environment_field_integer(trace
, NULL
, -194875),
2958 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2959 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
2961 "bt_ctf_trace_set_environment_field_integer succeeds");
2963 /* Test bt_ctf_trace_set_environment_field_string */
2964 ok(bt_ctf_trace_set_environment_field_string(NULL
, "test_env_str",
2966 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2967 ok(bt_ctf_trace_set_environment_field_string(trace
, NULL
, "yeah"),
2968 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2969 ok(bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2971 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2972 ok(!bt_ctf_trace_set_environment_field_string(trace
, "test_env_str",
2974 "bt_ctf_trace_set_environment_field_string succeeds");
2976 /* Test bt_ctf_trace_get_environment_field_count */
2977 ok(bt_ctf_trace_get_environment_field_count(NULL
) < 0,
2978 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2979 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
2980 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2982 /* Test bt_ctf_trace_get_environment_field_name */
2983 ok(bt_ctf_trace_get_environment_field_name_by_index(NULL
, 0) == NULL
,
2984 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2985 ok(bt_ctf_trace_get_environment_field_name_by_index(trace
, 5) == NULL
,
2986 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2987 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 0);
2988 ok(ret_string
&& !strcmp(ret_string
, "host"),
2989 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2990 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 1);
2991 ok(ret_string
&& !strcmp(ret_string
, "test_env_int_obj"),
2992 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2993 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 2);
2994 ok(ret_string
&& !strcmp(ret_string
, "test_env_str_obj"),
2995 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2996 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 3);
2997 ok(ret_string
&& !strcmp(ret_string
, "test_env_int"),
2998 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2999 ret_string
= bt_ctf_trace_get_environment_field_name_by_index(trace
, 4);
3000 ok(ret_string
&& !strcmp(ret_string
, "test_env_str"),
3001 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3003 /* Test bt_ctf_trace_get_environment_field_value */
3004 ok(bt_ctf_trace_get_environment_field_value_by_index(NULL
, 0) == NULL
,
3005 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
3006 ok(bt_ctf_trace_get_environment_field_value_by_index(trace
, 5) == NULL
,
3007 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
3008 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 1);
3009 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3010 ok(!ret
&& ret_int64_t
== 23,
3011 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
3013 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 2);
3014 ret
= bt_value_string_get(obj
, &ret_string
);
3015 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "the value"),
3016 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
3019 /* Test bt_ctf_trace_get_environment_field_value_by_name */
3020 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL
,
3022 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3023 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, NULL
),
3024 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3025 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace
, "oh oh"),
3026 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3027 obj
= bt_ctf_trace_get_environment_field_value_by_name(trace
,
3029 ret
= bt_value_string_get(obj
, &ret_string
);
3030 ok(!ret
&& ret_string
&& !strcmp(ret_string
, "oh yeah"),
3031 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3034 /* Test environment field replacement */
3035 ok(!bt_ctf_trace_set_environment_field_integer(trace
, "test_env_int",
3037 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3038 ok(bt_ctf_trace_get_environment_field_count(trace
) == 5,
3039 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3040 obj
= bt_ctf_trace_get_environment_field_value_by_index(trace
, 3);
3041 ret
= bt_value_integer_get(obj
, &ret_int64_t
);
3042 ok(!ret
&& ret_int64_t
== 654321,
3043 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3046 /* On Solaris, uname() can return any positive value on success */
3047 if (uname(&name
) < 0) {
3052 ok(bt_ctf_writer_add_environment_field(writer
, "sysname", name
.sysname
)
3053 == 0, "Add sysname (%s) environment field to writer instance",
3055 ok(bt_ctf_writer_add_environment_field(writer
, "nodename",
3056 name
.nodename
) == 0,
3057 "Add nodename (%s) environment field to writer instance",
3059 ok(bt_ctf_writer_add_environment_field(writer
, "release", name
.release
)
3060 == 0, "Add release (%s) environment field to writer instance",
3062 ok(bt_ctf_writer_add_environment_field(writer
, "version", name
.version
)
3063 == 0, "Add version (%s) environment field to writer instance",
3065 ok(bt_ctf_writer_add_environment_field(writer
, "machine", name
.machine
)
3066 == 0, "Add machine (%s) environment field to writer istance",
3069 /* Define a clock and add it to the trace */
3070 ok(bt_ctf_clock_create("signed") == NULL
,
3071 "Illegal clock name rejected");
3072 clock
= bt_ctf_clock_create(clock_name
);
3073 ok(clock
, "Clock created sucessfully");
3074 returned_clock_name
= bt_ctf_clock_get_name(clock
);
3075 ok(returned_clock_name
, "bt_ctf_clock_get_name returns a clock name");
3076 ok(returned_clock_name
? !strcmp(returned_clock_name
, clock_name
) : 0,
3077 "Returned clock name is valid");
3079 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3080 ok(!returned_clock_description
, "bt_ctf_clock_get_description returns NULL on an unset description");
3081 ok(bt_ctf_clock_set_description(clock
, clock_description
) == 0,
3082 "Clock description set successfully");
3084 returned_clock_description
= bt_ctf_clock_get_description(clock
);
3085 ok(returned_clock_description
,
3086 "bt_ctf_clock_get_description returns a description.");
3087 ok(returned_clock_description
?
3088 !strcmp(returned_clock_description
, clock_description
) : 0,
3089 "Returned clock description is valid");
3091 ok(bt_ctf_clock_get_frequency(clock
) == DEFAULT_CLOCK_FREQ
,
3092 "bt_ctf_clock_get_frequency returns the correct default frequency");
3093 ok(bt_ctf_clock_set_frequency(clock
, frequency
) == 0,
3094 "Set clock frequency");
3095 ok(bt_ctf_clock_get_frequency(clock
) == frequency
,
3096 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3098 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3099 "bt_ctf_clock_get_offset_s succeeds");
3100 ok(get_offset_s
== DEFAULT_CLOCK_OFFSET_S
,
3101 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3102 ok(bt_ctf_clock_set_offset_s(clock
, offset_s
) == 0,
3103 "Set clock offset (seconds)");
3104 ok(bt_ctf_clock_get_offset_s(clock
, &get_offset_s
) == 0,
3105 "bt_ctf_clock_get_offset_s succeeds");
3106 ok(get_offset_s
== offset_s
,
3107 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3109 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3110 "bt_ctf_clock_get_offset succeeds");
3111 ok(get_offset
== DEFAULT_CLOCK_OFFSET
,
3112 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3113 ok(bt_ctf_clock_set_offset(clock
, offset
) == 0, "Set clock offset");
3114 ok(bt_ctf_clock_get_offset(clock
, &get_offset
) == 0,
3115 "bt_ctf_clock_get_offset succeeds");
3116 ok(get_offset
== offset
,
3117 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3119 ok(bt_ctf_clock_get_precision(clock
) == DEFAULT_CLOCK_PRECISION
,
3120 "bt_ctf_clock_get_precision returns the correct default precision");
3121 ok(bt_ctf_clock_set_precision(clock
, precision
) == 0,
3122 "Set clock precision");
3123 ok(bt_ctf_clock_get_precision(clock
) == precision
,
3124 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3126 ok(bt_ctf_clock_get_is_absolute(clock
) == DEFAULT_CLOCK_IS_ABSOLUTE
,
3127 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3128 ok(bt_ctf_clock_set_is_absolute(clock
, is_absolute
) == 0,
3129 "Set clock absolute property");
3130 ok(bt_ctf_clock_get_is_absolute(clock
) == !!is_absolute
,
3131 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3133 ok(bt_ctf_clock_set_time(clock
, current_time
) == 0,
3136 ok(!bt_ctf_clock_get_name(NULL
),
3137 "bt_ctf_clock_get_name correctly handles NULL");
3138 ok(!bt_ctf_clock_get_description(NULL
),
3139 "bt_ctf_clock_get_description correctly handles NULL");
3140 ok(bt_ctf_clock_get_frequency(NULL
) == -1ULL,
3141 "bt_ctf_clock_get_frequency correctly handles NULL");
3142 ok(bt_ctf_clock_get_precision(NULL
) == -1ULL,
3143 "bt_ctf_clock_get_precision correctly handles NULL");
3144 ok(bt_ctf_clock_get_offset_s(NULL
, &get_offset_s
) < 0,
3145 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3146 ok(bt_ctf_clock_get_offset_s(clock
, NULL
) < 0,
3147 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3148 ok(bt_ctf_clock_get_offset(NULL
, &get_offset
) < 0,
3149 "bt_ctf_clock_get_offset correctly handles NULL clock");
3150 ok(bt_ctf_clock_get_offset(clock
, NULL
) < 0,
3151 "bt_ctf_clock_get_offset correctly handles NULL output");
3152 ok(bt_ctf_clock_get_is_absolute(NULL
) < 0,
3153 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3155 ok(bt_ctf_clock_set_description(NULL
, NULL
) < 0,
3156 "bt_ctf_clock_set_description correctly handles NULL clock");
3157 ok(bt_ctf_clock_set_frequency(NULL
, frequency
) < 0,
3158 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3159 ok(bt_ctf_clock_set_precision(NULL
, precision
) < 0,
3160 "bt_ctf_clock_get_precision correctly handles NULL clock");
3161 ok(bt_ctf_clock_set_offset_s(NULL
, offset_s
) < 0,
3162 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3163 ok(bt_ctf_clock_set_offset(NULL
, offset
) < 0,
3164 "bt_ctf_clock_set_offset correctly handles NULL clock");
3165 ok(bt_ctf_clock_set_is_absolute(NULL
, is_absolute
) < 0,
3166 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3167 ok(bt_ctf_clock_set_time(NULL
, current_time
) < 0,
3168 "bt_ctf_clock_set_time correctly handles NULL clock");
3169 ok(bt_ctf_clock_get_uuid(NULL
) == NULL
,
3170 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3171 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3173 "bt_ctf_clock_get_uuid returns a UUID");
3175 memcpy(tmp_uuid
, ret_uuid
, sizeof(tmp_uuid
));
3176 /* Slightly modify UUID */
3177 tmp_uuid
[sizeof(tmp_uuid
) - 1]++;
3180 ok(bt_ctf_clock_set_uuid(NULL
, tmp_uuid
) < 0,
3181 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3182 ok(bt_ctf_clock_set_uuid(clock
, NULL
) < 0,
3183 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3184 ok(bt_ctf_clock_set_uuid(clock
, tmp_uuid
) == 0,
3185 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3186 ret_uuid
= bt_ctf_clock_get_uuid(clock
);
3188 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3189 ok(uuid_match(ret_uuid
, tmp_uuid
),
3190 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3192 /* Define a stream class */
3193 stream_class
= bt_ctf_stream_class_create("test_stream");
3195 ok(bt_ctf_stream_class_get_name(NULL
) == NULL
,
3196 "bt_ctf_stream_class_get_name handles NULL correctly");
3197 ret_string
= bt_ctf_stream_class_get_name(stream_class
);
3198 ok(ret_string
&& !strcmp(ret_string
, "test_stream"),
3199 "bt_ctf_stream_class_get_name returns a correct stream class name");
3201 ok(bt_ctf_stream_class_get_clock(stream_class
) == NULL
,
3202 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3203 ok(bt_ctf_stream_class_get_clock(NULL
) == NULL
,
3204 "bt_ctf_stream_class_get_clock handles NULL correctly");
3206 ok(stream_class
, "Create stream class");
3207 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
) == 0,
3208 "Set a stream class' clock");
3209 ret_clock
= bt_ctf_stream_class_get_clock(stream_class
);
3210 ok(ret_clock
== clock
,
3211 "bt_ctf_stream_class_get_clock returns a correct clock");
3214 /* Test the event fields and event types APIs */
3217 /* Test fields copying */
3220 ok(bt_ctf_stream_class_get_id(stream_class
) < 0,
3221 "bt_ctf_stream_class_get_id returns an error when no id is set");
3222 ok(bt_ctf_stream_class_get_id(NULL
) < 0,
3223 "bt_ctf_stream_class_get_id handles NULL correctly");
3224 ok(bt_ctf_stream_class_set_id(NULL
, 123) < 0,
3225 "bt_ctf_stream_class_set_id handles NULL correctly");
3226 ok(bt_ctf_stream_class_set_id(stream_class
, 123) == 0,
3227 "Set an stream class' id");
3228 ok(bt_ctf_stream_class_get_id(stream_class
) == 123,
3229 "bt_ctf_stream_class_get_id returns the correct value");
3231 /* Validate default event header fields */
3232 ok(bt_ctf_stream_class_get_event_header_type(NULL
) == NULL
,
3233 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3234 ret_field_type
= bt_ctf_stream_class_get_event_header_type(
3237 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3238 ok(bt_ctf_field_type_get_type_id(ret_field_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3239 "Default event header type is a structure");
3240 event_header_field_type
=
3241 bt_ctf_field_type_structure_get_field_type_by_name(
3242 ret_field_type
, "id");
3243 ok(event_header_field_type
,
3244 "Default event header type contains an \"id\" field");
3245 ok(bt_ctf_field_type_get_type_id(
3246 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3247 "Default event header \"id\" field is an integer");
3248 bt_put(event_header_field_type
);
3249 event_header_field_type
=
3250 bt_ctf_field_type_structure_get_field_type_by_name(
3251 ret_field_type
, "timestamp");
3252 ok(event_header_field_type
,
3253 "Default event header type contains a \"timestamp\" field");
3254 ok(bt_ctf_field_type_get_type_id(
3255 event_header_field_type
) == BT_CTF_FIELD_TYPE_ID_INTEGER
,
3256 "Default event header \"timestamp\" field is an integer");
3257 bt_put(event_header_field_type
);
3258 bt_put(ret_field_type
);
3260 /* Add a custom trace packet header field */
3261 ok(bt_ctf_trace_get_packet_header_type(NULL
) == NULL
,
3262 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3263 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3264 ok(packet_header_type
,
3265 "bt_ctf_trace_get_packet_header_type returns a packet header");
3266 ok(bt_ctf_field_type_get_type_id(packet_header_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3267 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3268 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3269 packet_header_type
, "magic");
3270 ok(ret_field_type
, "Default packet header type contains a \"magic\" field");
3271 bt_put(ret_field_type
);
3272 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3273 packet_header_type
, "uuid");
3274 ok(ret_field_type
, "Default packet header type contains a \"uuid\" field");
3275 bt_put(ret_field_type
);
3276 ret_field_type
= bt_ctf_field_type_structure_get_field_type_by_name(
3277 packet_header_type
, "stream_id");
3278 ok(ret_field_type
, "Default packet header type contains a \"stream_id\" field");
3279 bt_put(ret_field_type
);
3281 packet_header_field_type
= bt_ctf_field_type_integer_create(22);
3282 ok(!bt_ctf_field_type_structure_add_field(packet_header_type
,
3283 packet_header_field_type
, "custom_trace_packet_header_field"),
3284 "Added a custom trace packet header field successfully");
3286 ok(bt_ctf_trace_set_packet_header_type(NULL
, packet_header_type
) < 0,
3287 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3288 ok(!bt_ctf_trace_set_packet_header_type(trace
, packet_header_type
),
3289 "Set a trace packet_header_type successfully");
3291 ok(bt_ctf_stream_class_get_packet_context_type(NULL
) == NULL
,
3292 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3294 /* Add a custom field to the stream class' packet context */
3295 packet_context_type
= bt_ctf_stream_class_get_packet_context_type(stream_class
);
3296 ok(packet_context_type
,
3297 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3298 ok(bt_ctf_field_type_get_type_id(packet_context_type
) == BT_CTF_FIELD_TYPE_ID_STRUCT
,
3299 "Packet context is a structure");
3301 ok(bt_ctf_stream_class_set_packet_context_type(NULL
, packet_context_type
),
3302 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3304 integer_type
= bt_ctf_field_type_integer_create(32);
3305 ok(bt_ctf_stream_class_set_packet_context_type(stream_class
,
3307 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3308 /* Create a "uint5_t" equivalent custom packet context field */
3309 packet_context_field_type
= bt_ctf_field_type_integer_create(5);
3311 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3312 packet_context_field_type
, "custom_packet_context_field");
3313 ok(ret
== 0, "Packet context field added successfully");
3315 /* Define a stream event context containing a my_integer field. */
3316 ok(bt_ctf_stream_class_get_event_context_type(NULL
) == NULL
,
3317 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3318 stream_event_context_type
= bt_ctf_field_type_structure_create();
3319 bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3320 integer_type
, "common_event_context");
3322 ok(bt_ctf_stream_class_set_event_context_type(NULL
,
3323 stream_event_context_type
) < 0,
3324 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3325 ok(bt_ctf_stream_class_set_event_context_type(stream_class
,
3327 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3329 ok(bt_ctf_stream_class_set_event_context_type(
3330 stream_class
, stream_event_context_type
) == 0,
3331 "Set a new stream event context type");
3332 ret_field_type
= bt_ctf_stream_class_get_event_context_type(
3334 ok(ret_field_type
== stream_event_context_type
,
3335 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3336 bt_put(ret_field_type
);
3338 /* Instantiate a stream and append events */
3339 ret
= bt_ctf_writer_add_clock(writer
, clock
);
3341 ok(bt_ctf_trace_get_stream_count(trace
) == 0,
3342 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)");
3343 stream1
= bt_ctf_writer_create_stream(writer
, stream_class
);
3344 ok(stream1
, "Instanciate a stream class from writer");
3345 ok(bt_ctf_trace_get_stream_count(trace
) == 1,
3346 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)");
3347 stream
= bt_ctf_trace_get_stream_by_index(trace
, 0);
3348 ok(stream
== stream1
,
3349 "bt_ctf_trace_get_stream_by_index() succeeds and returns the correct value");
3353 * Creating a stream through a writer adds the given stream
3354 * class to the writer's trace, thus registering the stream
3355 * class's clock to the trace.
3357 ok(bt_ctf_trace_get_clock_class_count(NULL
) < 0,
3358 "bt_ctf_trace_get_clock_class_count correctly handles NULL");
3359 ok(bt_ctf_trace_get_clock_class_count(trace
) == 1,
3360 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
3361 ok(!bt_ctf_trace_get_clock_class_by_index(NULL
, 0),
3362 "bt_ctf_trace_get_clock_class correctly handles NULL");
3363 ok(!bt_ctf_trace_get_clock_class_by_index(trace
, 1),
3364 "bt_ctf_trace_get_clock_class correctly handles out of bound accesses");
3365 ret_clock_class
= bt_ctf_trace_get_clock_class_by_index(trace
, 0);
3366 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3367 bt_ctf_clock_get_name(clock
)) == 0,
3368 "bt_ctf_trace_get_clock_class returns the right clock instance");
3369 bt_put(ret_clock_class
);
3370 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, NULL
),
3371 "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)");
3372 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, clock_name
),
3373 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3374 ok(!bt_ctf_trace_get_clock_class_by_name(NULL
, NULL
),
3375 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3376 ret_clock_class
= bt_ctf_trace_get_clock_class_by_name(trace
, clock_name
);
3377 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class
),
3378 bt_ctf_clock_get_name(clock
)) == 0,
3379 "bt_ctf_trace_get_clock_class returns the right clock instance");
3380 bt_put(ret_clock_class
);
3381 ok(!bt_ctf_trace_get_clock_class_by_name(trace
, "random"),
3382 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3384 ok(bt_ctf_stream_get_class(NULL
) == NULL
,
3385 "bt_ctf_stream_get_class correctly handles NULL");
3386 ret_stream_class
= bt_ctf_stream_get_class(stream1
);
3387 ok(ret_stream_class
,
3388 "bt_ctf_stream_get_class returns a stream class");
3389 ok(ret_stream_class
== stream_class
,
3390 "Returned stream class is of the correct type");
3393 * Packet header, packet context, event header, and stream
3394 * event context types were copied for the resolving
3397 BT_PUT(packet_header_type
);
3398 BT_PUT(packet_context_type
);
3399 BT_PUT(stream_event_context_type
);
3400 packet_header_type
= bt_ctf_trace_get_packet_header_type(trace
);
3401 assert(packet_header_type
);
3402 packet_context_type
=
3403 bt_ctf_stream_class_get_packet_context_type(stream_class
);
3404 assert(packet_context_type
);
3405 stream_event_context_type
=
3406 bt_ctf_stream_class_get_event_context_type(stream_class
);
3407 assert(stream_event_context_type
);
3410 * Try to modify the packet context type after a stream has been
3413 ret
= bt_ctf_field_type_structure_add_field(packet_header_type
,
3414 packet_header_field_type
, "should_fail");
3416 "Trace packet header type can't be modified once a stream has been instanciated");
3419 * Try to modify the packet context type after a stream has been
3422 ret
= bt_ctf_field_type_structure_add_field(packet_context_type
,
3423 packet_context_field_type
, "should_fail");
3425 "Packet context type can't be modified once a stream has been instanciated");
3428 * Try to modify the stream event context type after a stream has been
3431 ret
= bt_ctf_field_type_structure_add_field(stream_event_context_type
,
3432 integer_type
, "should_fail");
3434 "Stream event context type can't be modified once a stream has been instanciated");
3436 /* Should fail after instanciating a stream (frozen) */
3437 ok(bt_ctf_stream_class_set_clock(stream_class
, clock
),
3438 "Changes to a stream class that was already instantiated fail");
3440 /* Populate the custom packet header field only once for all tests */
3441 ok(bt_ctf_stream_get_packet_header(NULL
) == NULL
,
3442 "bt_ctf_stream_get_packet_header handles NULL correctly");
3443 packet_header
= bt_ctf_stream_get_packet_header(stream1
);
3445 "bt_ctf_stream_get_packet_header returns a packet header");
3446 ret_field_type
= bt_ctf_field_get_type(packet_header
);
3447 ok(ret_field_type
== packet_header_type
,
3448 "Stream returns a packet header of the appropriate type");
3449 bt_put(ret_field_type
);
3450 packet_header_field
= bt_ctf_field_structure_get_field(packet_header
,
3451 "custom_trace_packet_header_field");
3452 ok(packet_header_field
,
3453 "Packet header structure contains a custom field with the appropriate name");
3454 ret_field_type
= bt_ctf_field_get_type(packet_header_field
);
3455 ok(bt_ctf_field_type_compare(ret_field_type
, packet_header_field_type
) == 0,
3456 "Custom packet header field is of the expected type");
3457 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field
,
3458 54321), "Set custom packet header value successfully");
3459 ok(bt_ctf_stream_set_packet_header(stream1
, NULL
) < 0,
3460 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3461 ok(bt_ctf_stream_set_packet_header(NULL
, packet_header
) < 0,
3462 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3463 ok(bt_ctf_stream_set_packet_header(stream1
, packet_header_field
) < 0,
3464 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3465 ok(!bt_ctf_stream_set_packet_header(stream1
, packet_header
),
3466 "Successfully set a stream's packet header");
3468 ok(bt_ctf_writer_add_environment_field(writer
, "new_field", "test") == 0,
3469 "Add environment field to writer after stream creation");
3473 test_create_writer_vs_non_writer_mode();
3475 test_set_clock_non_writer_stream_class();
3477 test_instanciate_event_before_stream(writer
, clock
);
3479 append_simple_event(stream_class
, stream1
, clock
);
3481 packet_resize_test(stream_class
, stream1
, clock
);
3483 append_complex_event(stream_class
, stream1
, clock
);
3485 append_existing_event_class(stream_class
);
3487 test_empty_stream(writer
);
3489 test_custom_event_header_stream(writer
, clock
);
3491 test_static_trace();
3493 test_trace_is_static_listener();
3497 metadata_string
= bt_ctf_writer_get_metadata_string(writer
);
3498 ok(metadata_string
, "Get metadata string");
3500 bt_ctf_writer_flush_metadata(writer
);
3503 bt_put(ret_stream_class
);
3506 bt_put(packet_context_type
);
3507 bt_put(packet_context_field_type
);
3508 bt_put(integer_type
);
3509 bt_put(stream_event_context_type
);
3510 bt_put(ret_field_type
);
3511 bt_put(packet_header_type
);
3512 bt_put(packet_header_field_type
);
3513 bt_put(packet_header
);
3514 bt_put(packet_header_field
);
3516 free(metadata_string
);
3517 bt_put(stream_class
);
3519 validate_trace(argv
[1], trace_path
);
3521 recursive_rmdir(trace_path
);
3523 g_free(metadata_path
);