ir: clock: use value in cycles instead of ns
[babeltrace.git] / tests / lib / test_ctf_writer.c
1 /*
2 * test-ctf-writer.c
3 *
4 * CTF Writer test
5 *
6 * Copyright 2013 - 2015 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
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.
11 *
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.
16 *
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.
20 */
21
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-ir/stream-class.h>
29 #include <babeltrace/ref.h>
30 #include <babeltrace/ctf/events.h>
31 #include <babeltrace/values.h>
32 #include <unistd.h>
33 #include <babeltrace/compat/stdlib.h>
34 #include <stdio.h>
35 #include <sys/utsname.h>
36 #include <babeltrace/compat/limits.h>
37 #include <babeltrace/compat/stdio.h>
38 #include <string.h>
39 #include <assert.h>
40 #include <sys/wait.h>
41 #include <fcntl.h>
42 #include <babeltrace/compat/dirent.h>
43 #include "tap/tap.h"
44 #include <math.h>
45 #include <float.h>
46 #include <sys/stat.h>
47
48 #define METADATA_LINE_SIZE 512
49 #define SEQUENCE_TEST_LENGTH 10
50 #define ARRAY_TEST_LENGTH 5
51 #define PACKET_RESIZE_TEST_LENGTH 100000
52
53 #define DEFAULT_CLOCK_FREQ 1000000000
54 #define DEFAULT_CLOCK_PRECISION 1
55 #define DEFAULT_CLOCK_OFFSET 0
56 #define DEFAULT_CLOCK_OFFSET_S 0
57 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
58 #define DEFAULT_CLOCK_TIME 0
59 #define DEFAULT_CLOCK_VALUE 0
60
61 #define NR_TESTS 597
62
63 static int64_t current_time = 42;
64
65 /* Return 1 if uuids match, zero if different. */
66 int uuid_match(const unsigned char *uuid_a, const unsigned char *uuid_b)
67 {
68 int ret = 0;
69 int i;
70
71 if (!uuid_a || !uuid_b) {
72 goto end;
73 }
74
75 for (i = 0; i < 16; i++) {
76 if (uuid_a[i] != uuid_b[i]) {
77 goto end;
78 }
79 }
80
81 ret = 1;
82 end:
83 return ret;
84 }
85
86 void validate_metadata(char *parser_path, char *metadata_path)
87 {
88 int ret = 0;
89 char parser_output_path[] = "/tmp/parser_output_XXXXXX";
90 int parser_output_fd = -1, metadata_fd = -1;
91
92 if (!metadata_path) {
93 ret = -1;
94 goto result;
95 }
96
97 parser_output_fd = mkstemp(parser_output_path);
98 metadata_fd = open(metadata_path, O_RDONLY);
99
100 unlink(parser_output_path);
101
102 if (parser_output_fd == -1 || metadata_fd == -1) {
103 diag("Failed create temporary files for metadata parsing.");
104 ret = -1;
105 goto result;
106 }
107
108 pid_t pid = fork();
109 if (pid) {
110 int status = 0;
111 waitpid(pid, &status, 0);
112 ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
113 } else {
114 /* ctf-parser-test expects a metadata string on stdin. */
115 ret = dup2(metadata_fd, STDIN_FILENO);
116 if (ret < 0) {
117 perror("# dup2 metadata_fd to STDIN");
118 goto result;
119 }
120
121 ret = dup2(parser_output_fd, STDOUT_FILENO);
122 if (ret < 0) {
123 perror("# dup2 parser_output_fd to STDOUT");
124 goto result;
125 }
126
127 ret = dup2(parser_output_fd, STDERR_FILENO);
128 if (ret < 0) {
129 perror("# dup2 parser_output_fd to STDERR");
130 goto result;
131 }
132
133 execl(parser_path, "ctf-parser-test", NULL);
134 perror("# Could not launch the ctf metadata parser process");
135 exit(-1);
136 }
137 result:
138 ok(ret == 0, "Metadata string is valid");
139
140 if (ret && metadata_fd >= 0 && parser_output_fd >= 0) {
141 char *line;
142 size_t len = METADATA_LINE_SIZE;
143 FILE *metadata_fp = NULL, *parser_output_fp = NULL;
144
145 metadata_fp = fdopen(metadata_fd, "r");
146 if (!metadata_fp) {
147 perror("fdopen on metadata_fd");
148 goto close_fp;
149 }
150 metadata_fd = -1;
151
152 parser_output_fp = fdopen(parser_output_fd, "r");
153 if (!parser_output_fp) {
154 perror("fdopen on parser_output_fd");
155 goto close_fp;
156 }
157 parser_output_fd = -1;
158
159 line = malloc(len);
160 if (!line) {
161 diag("malloc failure");
162 }
163
164 rewind(metadata_fp);
165
166 /* Output the metadata and parser output as diagnostic */
167 while (bt_getline(&line, &len, metadata_fp) > 0) {
168 fprintf(stderr, "# %s", line);
169 }
170
171 rewind(parser_output_fp);
172 while (bt_getline(&line, &len, parser_output_fp) > 0) {
173 fprintf(stderr, "# %s", line);
174 }
175
176 free(line);
177 close_fp:
178 if (metadata_fp) {
179 if (fclose(metadata_fp)) {
180 diag("fclose failure");
181 }
182 }
183 if (parser_output_fp) {
184 if (fclose(parser_output_fp)) {
185 diag("fclose failure");
186 }
187 }
188 }
189
190 if (parser_output_fd >= 0) {
191 if (close(parser_output_fd)) {
192 diag("close error");
193 }
194 }
195 if (metadata_fd >= 0) {
196 if (close(metadata_fd)) {
197 diag("close error");
198 }
199 }
200 }
201
202 void validate_trace(char *parser_path, char *trace_path)
203 {
204 int ret = 0;
205 char babeltrace_output_path[] = "/tmp/babeltrace_output_XXXXXX";
206 int babeltrace_output_fd = -1;
207
208 if (!trace_path) {
209 ret = -1;
210 goto result;
211 }
212
213 babeltrace_output_fd = mkstemp(babeltrace_output_path);
214 unlink(babeltrace_output_path);
215
216 if (babeltrace_output_fd == -1) {
217 diag("Failed to create a temporary file for trace parsing.");
218 ret = -1;
219 goto result;
220 }
221
222 pid_t pid = fork();
223 if (pid) {
224 int status = 0;
225 waitpid(pid, &status, 0);
226 ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
227 } else {
228 ret = dup2(babeltrace_output_fd, STDOUT_FILENO);
229 if (ret < 0) {
230 perror("# dup2 babeltrace_output_fd to STDOUT");
231 goto result;
232 }
233
234 ret = dup2(babeltrace_output_fd, STDERR_FILENO);
235 if (ret < 0) {
236 perror("# dup2 babeltrace_output_fd to STDERR");
237 goto result;
238 }
239
240 execl(parser_path, "babeltrace", trace_path, NULL);
241 perror("# Could not launch the babeltrace process");
242 exit(-1);
243 }
244 result:
245 ok(ret == 0, "Babeltrace could read the resulting trace");
246
247 if (ret && babeltrace_output_fd >= 0) {
248 char *line;
249 size_t len = METADATA_LINE_SIZE;
250 FILE *babeltrace_output_fp = NULL;
251
252 babeltrace_output_fp = fdopen(babeltrace_output_fd, "r");
253 if (!babeltrace_output_fp) {
254 perror("fdopen on babeltrace_output_fd");
255 goto close_fp;
256 }
257 babeltrace_output_fd = -1;
258
259 line = malloc(len);
260 if (!line) {
261 diag("malloc error");
262 }
263 rewind(babeltrace_output_fp);
264 while (bt_getline(&line, &len, babeltrace_output_fp) > 0) {
265 diag("%s", line);
266 }
267
268 free(line);
269 close_fp:
270 if (babeltrace_output_fp) {
271 if (fclose(babeltrace_output_fp)) {
272 diag("fclose error");
273 }
274 }
275 }
276
277 if (babeltrace_output_fd >= 0) {
278 if (close(babeltrace_output_fd)) {
279 diag("close error");
280 }
281 }
282 }
283
284 void event_copy_tests(struct bt_ctf_event *event)
285 {
286 struct bt_ctf_event *copy;
287 struct bt_ctf_event_class *orig_event_class;
288 struct bt_ctf_event_class *copy_event_class;
289 struct bt_ctf_stream *orig_stream;
290 struct bt_ctf_stream *copy_stream;
291 struct bt_ctf_field *orig_field;
292 struct bt_ctf_field *copy_field;
293
294 /* copy */
295 ok(!bt_ctf_event_copy(NULL),
296 "bt_ctf_event_copy handles NULL correctly");
297 copy = bt_ctf_event_copy(event);
298 ok(copy, "bt_ctf_event_copy returns a valid pointer");
299
300 /* validate event class */
301 orig_event_class = bt_ctf_event_get_class(event);
302 assert(orig_event_class);
303 copy_event_class = bt_ctf_event_get_class(copy);
304 ok(orig_event_class == copy_event_class,
305 "original and copied events share the same event class pointer");
306 bt_put(orig_event_class);
307 bt_put(copy_event_class);
308
309 /* validate stream */
310 orig_stream = bt_ctf_event_get_stream(event);
311 copy_stream = bt_ctf_event_get_stream(copy);
312
313 if (!orig_stream) {
314 ok(!copy_stream, "original and copied events have no stream");
315 } else {
316 ok(orig_stream == copy_stream,
317 "original and copied events share the same stream pointer");
318 }
319 bt_put(orig_stream);
320 bt_put(copy_stream);
321
322 /* header */
323 orig_field = bt_ctf_event_get_header(event);
324 copy_field = bt_ctf_event_get_header(copy);
325
326 if (!orig_field) {
327 ok(!copy_field, "original and copied events have no header");
328 } else {
329 ok(orig_field != copy_field,
330 "original and copied events headers are different pointers");
331 }
332
333 bt_put(orig_field);
334 bt_put(copy_field);
335
336 /* context */
337 orig_field = bt_ctf_event_get_event_context(event);
338 copy_field = bt_ctf_event_get_event_context(copy);
339
340 if (!orig_field) {
341 ok(!copy_field, "original and copied events have no context");
342 } else {
343 ok(orig_field != copy_field,
344 "original and copied events contexts are different pointers");
345 }
346
347 bt_put(orig_field);
348 bt_put(copy_field);
349
350 /* payload */
351 orig_field = bt_ctf_event_get_payload_field(event);
352 copy_field = bt_ctf_event_get_payload_field(copy);
353
354 if (!orig_field) {
355 ok(!copy_field, "original and copied events have no payload");
356 } else {
357 ok(orig_field != copy_field,
358 "original and copied events payloads are different pointers");
359 }
360
361 bt_put(orig_field);
362 bt_put(copy_field);
363
364 bt_put(copy);
365 }
366
367 void append_simple_event(struct bt_ctf_stream_class *stream_class,
368 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
369 {
370 /* Create and add a simple event class */
371 struct bt_ctf_event_class *simple_event_class =
372 bt_ctf_event_class_create("Simple Event");
373 struct bt_ctf_field_type *uint_12_type =
374 bt_ctf_field_type_integer_create(12);
375 struct bt_ctf_field_type *int_64_type =
376 bt_ctf_field_type_integer_create(64);
377 struct bt_ctf_field_type *float_type =
378 bt_ctf_field_type_floating_point_create();
379 struct bt_ctf_field_type *enum_type;
380 struct bt_ctf_field_type *enum_type_unsigned =
381 bt_ctf_field_type_enumeration_create(uint_12_type);
382 struct bt_ctf_field_type *event_context_type =
383 bt_ctf_field_type_structure_create();
384 struct bt_ctf_field_type *event_payload_type = NULL;
385 struct bt_ctf_field_type *returned_type;
386 struct bt_ctf_event *simple_event;
387 struct bt_ctf_field *integer_field;
388 struct bt_ctf_field *float_field;
389 struct bt_ctf_field *enum_field;
390 struct bt_ctf_field *enum_field_unsigned;
391 struct bt_ctf_field *enum_container_field;
392 const char *mapping_name_test = "truie";
393 const double double_test_value = 3.1415;
394 struct bt_ctf_field *enum_container_field_unsigned;
395 const char *mapping_name_negative_test = "negative_value";
396 const char *ret_char;
397 double ret_double;
398 int64_t ret_range_start_int64_t, ret_range_end_int64_t;
399 uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t;
400 struct bt_ctf_clock *ret_clock;
401 struct bt_ctf_event_class *ret_event_class;
402 struct bt_ctf_field *packet_context;
403 struct bt_ctf_field *packet_context_field;
404 struct bt_ctf_field *stream_event_context;
405 struct bt_ctf_field *stream_event_context_field;
406 struct bt_ctf_field *event_context;
407 struct bt_ctf_field *event_context_field;
408 struct bt_ctf_field_type *ep_integer_field_type = NULL;
409 struct bt_ctf_field_type *ep_enum_field_type = NULL;
410 struct bt_ctf_field_type *ep_enum_field_unsigned_type = NULL;
411 int ret;
412
413 ok(uint_12_type, "Create an unsigned integer type");
414
415 bt_ctf_field_type_integer_set_signed(int_64_type, 1);
416 ok(int_64_type, "Create a signed integer type");
417 enum_type = bt_ctf_field_type_enumeration_create(int_64_type);
418
419 returned_type = bt_ctf_field_type_enumeration_get_container_type(enum_type);
420 ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
421 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
422 ok(!bt_ctf_field_type_enumeration_create(enum_type),
423 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
424 bt_put(returned_type);
425
426 bt_ctf_field_type_set_alignment(float_type, 32);
427 ok(bt_ctf_field_type_get_alignment(NULL) < 0,
428 "bt_ctf_field_type_get_alignment handles NULL correctly");
429 ok(bt_ctf_field_type_get_alignment(float_type) == 32,
430 "bt_ctf_field_type_get_alignment returns a correct value");
431
432 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0,
433 "Set a floating point type's exponent digit count");
434 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0,
435 "Set a floating point type's mantissa digit count");
436
437 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL) < 0,
438 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
439 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL) < 0,
440 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
441 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11,
442 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
443 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53,
444 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
445
446 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
447 mapping_name_negative_test, -12345, 0) == 0,
448 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
449 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
450 "escaping; \"test\"", 1, 1) == 0,
451 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
452 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
453 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
454 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
455 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
456 "event clock int float", 5, 22) == 0,
457 "Accept enumeration mapping strings containing reserved keywords");
458 bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
459 42, 42);
460 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
461 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
462 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something",
463 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
464 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
465 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
466 bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000);
467
468 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL, -42) < 0,
469 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
470 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, 1000000) < 0,
471 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
472 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -55) == 1,
473 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
474
475 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
476 "enum_field") == 0, "Add signed enumeration field to event");
477
478 ok(bt_ctf_field_type_enumeration_get_mapping(NULL, 0, &ret_char,
479 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
480 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
481 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, NULL,
482 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
483 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
484 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
485 NULL, &ret_range_end_int64_t) < 0,
486 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
487 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
488 &ret_range_start_int64_t, NULL) < 0,
489 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
490 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 5, &ret_char,
491 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
492 "bt_ctf_field_type_enumeration_get_mapping returns a value");
493 ok(!strcmp(ret_char, mapping_name_test),
494 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
495 ok(ret_range_start_int64_t == 42,
496 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
497 ok(ret_range_end_int64_t == 42,
498 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
499
500 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
501 "escaping; \"test\"", 0, 0) == 0,
502 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
503 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
504 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
505 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
506 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
507 "event clock int float", 5, 22) == 0,
508 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
509 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
510 42, 42);
511 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
512 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
513 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something",
514 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
515 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
516 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
517 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned,
518 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
519
520 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL) < 0,
521 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
522 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 4,
523 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
524
525 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char,
526 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
527 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
528 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL,
529 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
530 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
531 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
532 NULL, &ret_range_end_uint64_t) < 0,
533 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
534 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
535 &ret_range_start_uint64_t, NULL) < 0,
536 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
537 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 3, &ret_char,
538 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
539 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
540 ok(!strcmp(ret_char, mapping_name_test),
541 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
542 ok(ret_range_start_uint64_t == 42,
543 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
544 ok(ret_range_end_uint64_t == 42,
545 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
546
547 bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
548 "integer_field");
549 bt_ctf_event_class_add_field(simple_event_class, float_type,
550 "float_field");
551
552 assert(!bt_ctf_event_class_set_id(simple_event_class, 13));
553
554 /* Set an event context type which will contain a single integer*/
555 ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
556 "event_specific_context"),
557 "Add event specific context field");
558 ok(bt_ctf_event_class_get_context_type(NULL) == NULL,
559 "bt_ctf_event_class_get_context_type handles NULL correctly");
560 ok(bt_ctf_event_class_get_context_type(simple_event_class) == NULL,
561 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
562
563 ok(bt_ctf_event_class_set_context_type(simple_event_class, NULL) < 0,
564 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
565 ok(bt_ctf_event_class_set_context_type(NULL, event_context_type) < 0,
566 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
567 ok(!bt_ctf_event_class_set_context_type(simple_event_class, event_context_type),
568 "Set an event class' context type successfully");
569 returned_type = bt_ctf_event_class_get_context_type(simple_event_class);
570 ok(returned_type == event_context_type,
571 "bt_ctf_event_class_get_context_type returns the appropriate type");
572 bt_put(returned_type);
573
574 bt_ctf_stream_class_add_event_class(stream_class, simple_event_class);
575
576 /*
577 * bt_ctf_stream_class_add_event_class() copies the field types
578 * of simple_event_class, so we retrieve the new ones to create
579 * the appropriate fields.
580 */
581 BT_PUT(event_context_type);
582 BT_PUT(event_payload_type);
583 event_payload_type = bt_ctf_event_class_get_payload_type(
584 simple_event_class);
585 assert(event_payload_type);
586 event_context_type = bt_ctf_event_class_get_context_type(
587 simple_event_class);
588 assert(event_context_type);
589 ep_integer_field_type =
590 bt_ctf_field_type_structure_get_field_type_by_name(
591 event_payload_type, "integer_field");
592 assert(ep_integer_field_type);
593 ep_enum_field_type =
594 bt_ctf_field_type_structure_get_field_type_by_name(
595 event_payload_type, "enum_field");
596 assert(ep_enum_field_type);
597 ep_enum_field_unsigned_type =
598 bt_ctf_field_type_structure_get_field_type_by_name(
599 event_payload_type, "enum_field_unsigned");
600 assert(ep_enum_field_unsigned_type);
601
602 ok(bt_ctf_stream_class_get_event_class_count(NULL) < 0,
603 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
604 ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1,
605 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
606 ok(bt_ctf_stream_class_get_event_class(NULL, 0) == NULL,
607 "bt_ctf_stream_class_get_event_class handles NULL correctly");
608 ok(bt_ctf_stream_class_get_event_class(stream_class, 8724) == NULL,
609 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
610 ret_event_class = bt_ctf_stream_class_get_event_class(stream_class, 0);
611 ok(ret_event_class == simple_event_class,
612 "bt_ctf_stream_class_get_event_class returns the correct event class");
613 bt_put(ret_event_class);
614 ok(!bt_ctf_stream_class_get_event_class_by_id(NULL, 0),
615 "bt_ctf_stream_class_get_event_class_by_id handles NULL correctly");
616 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class, 2),
617 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
618 ret_event_class =
619 bt_ctf_stream_class_get_event_class_by_id(stream_class, 13);
620 ok(ret_event_class == simple_event_class,
621 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
622 bt_put(ret_event_class);
623
624 ok(bt_ctf_stream_class_get_event_class_by_name(NULL, "some event name") == NULL,
625 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
626 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, NULL) == NULL,
627 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
628 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, "some event name") == NULL,
629 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
630 ret_event_class = bt_ctf_stream_class_get_event_class_by_name(stream_class, "Simple Event");
631 ok(ret_event_class == simple_event_class,
632 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
633 bt_put(ret_event_class);
634
635 simple_event = bt_ctf_event_create(simple_event_class);
636 ok(simple_event,
637 "Instantiate an event containing a single integer field");
638
639 ok(bt_ctf_event_get_clock(NULL) == NULL,
640 "bt_ctf_event_get_clock handles NULL correctly");
641 ret_clock = bt_ctf_event_get_clock(simple_event);
642 ok(ret_clock == clock,
643 "bt_ctf_event_get_clock returns a correct clock");
644 bt_put(clock);
645
646 integer_field = bt_ctf_field_create(ep_integer_field_type);
647 bt_ctf_field_unsigned_integer_set_value(integer_field, 42);
648 ok(bt_ctf_event_set_payload(simple_event, "integer_field",
649 integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
650
651 float_field = bt_ctf_event_get_payload(simple_event, "float_field");
652 ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double),
653 "bt_ctf_field_floating_point_get_value fails on an unset float field");
654 bt_ctf_field_floating_point_set_value(float_field, double_test_value);
655 ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double),
656 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
657 ok(bt_ctf_field_floating_point_get_value(float_field, NULL),
658 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
659 ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double),
660 "bt_ctf_field_floating_point_get_value returns a double value");
661 ok(fabs(ret_double - double_test_value) <= DBL_EPSILON,
662 "bt_ctf_field_floating_point_get_value returns a correct value");
663
664 enum_field = bt_ctf_field_create(ep_enum_field_type);
665 assert(enum_field);
666 ret_char = bt_ctf_field_enumeration_get_mapping_name(NULL);
667 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
668 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
669 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
670 enum_container_field = bt_ctf_field_enumeration_get_container(
671 enum_field);
672 ok(bt_ctf_field_signed_integer_set_value(
673 enum_container_field, -42) == 0,
674 "Set signed enumeration container value");
675 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
676 ok(!strcmp(ret_char, mapping_name_negative_test),
677 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
678 ret = bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
679 assert(!ret);
680
681 enum_field_unsigned = bt_ctf_field_create(ep_enum_field_unsigned_type);
682 assert(enum_field_unsigned);
683 enum_container_field_unsigned = bt_ctf_field_enumeration_get_container(
684 enum_field_unsigned);
685 ok(bt_ctf_field_unsigned_integer_set_value(
686 enum_container_field_unsigned, 42) == 0,
687 "Set unsigned enumeration container value");
688 ret = bt_ctf_event_set_payload(simple_event, "enum_field_unsigned",
689 enum_field_unsigned);
690 assert(!ret);
691 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned);
692 ok(ret_char && !strcmp(ret_char, mapping_name_test),
693 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
694
695 ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
696
697 /* Populate stream event context */
698 stream_event_context = bt_ctf_stream_get_event_context(stream);
699 stream_event_context_field = bt_ctf_field_structure_get_field(
700 stream_event_context, "common_event_context");
701 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field, 42);
702
703 /* Populate the event's context */
704 ok(bt_ctf_event_get_event_context(NULL) == NULL,
705 "bt_ctf_event_get_event_context handles NULL correctly");
706 event_context = bt_ctf_event_get_event_context(simple_event);
707 ok(event_context,
708 "bt_ctf_event_get_event_context returns a field");
709 returned_type = bt_ctf_field_get_type(event_context);
710 ok(returned_type == event_context_type,
711 "bt_ctf_event_get_event_context returns a field of the appropriate type");
712 event_context_field = bt_ctf_field_structure_get_field(event_context,
713 "event_specific_context");
714 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field, 1234),
715 "Successfully set an event context's value");
716 ok(bt_ctf_event_set_event_context(NULL, event_context) < 0,
717 "bt_ctf_event_set_event_context handles a NULL event correctly");
718 ok(bt_ctf_event_set_event_context(simple_event, NULL) < 0,
719 "bt_ctf_event_set_event_context handles a NULL event context correctly");
720 ok(bt_ctf_event_set_event_context(simple_event, event_context_field) < 0,
721 "bt_ctf_event_set_event_context rejects a context of the wrong type");
722 ok(!bt_ctf_event_set_event_context(simple_event, event_context),
723 "Set an event context successfully");
724
725 event_copy_tests(simple_event);
726 ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
727 "Append simple event to trace stream");
728
729 ok(bt_ctf_stream_get_packet_context(NULL) == NULL,
730 "bt_ctf_stream_get_packet_context handles NULL correctly");
731 packet_context = bt_ctf_stream_get_packet_context(stream);
732 ok(packet_context,
733 "bt_ctf_stream_get_packet_context returns a packet context");
734
735 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
736 "packet_size");
737 ok(packet_context_field,
738 "Packet context contains the default packet_size field.");
739 bt_put(packet_context_field);
740 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
741 "custom_packet_context_field");
742 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field, 8) == 0,
743 "Custom packet context field value successfully set.");
744
745 ok(bt_ctf_stream_set_packet_context(NULL, packet_context_field) < 0,
746 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
747 ok(bt_ctf_stream_set_packet_context(stream, NULL) < 0,
748 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
749 ok(bt_ctf_stream_set_packet_context(stream, packet_context) == 0,
750 "Successfully set a stream's packet context");
751
752 ok(bt_ctf_stream_flush(stream) == 0,
753 "Flush trace stream with one event");
754
755 bt_put(simple_event_class);
756 bt_put(simple_event);
757 bt_put(uint_12_type);
758 bt_put(int_64_type);
759 bt_put(float_type);
760 bt_put(enum_type);
761 bt_put(enum_type_unsigned);
762 bt_put(returned_type);
763 bt_put(event_context_type);
764 bt_put(integer_field);
765 bt_put(float_field);
766 bt_put(enum_field);
767 bt_put(enum_field_unsigned);
768 bt_put(enum_container_field);
769 bt_put(enum_container_field_unsigned);
770 bt_put(packet_context);
771 bt_put(packet_context_field);
772 bt_put(stream_event_context);
773 bt_put(stream_event_context_field);
774 bt_put(event_context);
775 bt_put(event_context_field);
776 bt_put(event_payload_type);
777 bt_put(ep_integer_field_type);
778 bt_put(ep_enum_field_type);
779 bt_put(ep_enum_field_unsigned_type);
780 }
781
782 void append_complex_event(struct bt_ctf_stream_class *stream_class,
783 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
784 {
785 struct event_class_attrs_counts {
786 int id;
787 int name;
788 int loglevel;
789 int modelemfuri;
790 int unknown;
791 } attrs_count;
792
793 int i;
794 int ret;
795 int64_t int64_value;
796 struct event_class_attrs_counts ;
797 const char *complex_test_event_string = "Complex Test Event";
798 const char *test_string_1 = "Test ";
799 const char *test_string_2 = "string ";
800 const char *test_string_3 = "abcdefghi";
801 const char *test_string_4 = "abcd\0efg\0hi";
802 const char *test_string_cat = "Test string abcdeabcd";
803 struct bt_ctf_field_type *uint_35_type =
804 bt_ctf_field_type_integer_create(35);
805 struct bt_ctf_field_type *int_16_type =
806 bt_ctf_field_type_integer_create(16);
807 struct bt_ctf_field_type *uint_3_type =
808 bt_ctf_field_type_integer_create(3);
809 struct bt_ctf_field_type *enum_variant_type =
810 bt_ctf_field_type_enumeration_create(uint_3_type);
811 struct bt_ctf_field_type *variant_type =
812 bt_ctf_field_type_variant_create(enum_variant_type,
813 "variant_selector");
814 struct bt_ctf_field_type *string_type =
815 bt_ctf_field_type_string_create();
816 struct bt_ctf_field_type *sequence_type;
817 struct bt_ctf_field_type *array_type;
818 struct bt_ctf_field_type *inner_structure_type =
819 bt_ctf_field_type_structure_create();
820 struct bt_ctf_field_type *complex_structure_type =
821 bt_ctf_field_type_structure_create();
822 struct bt_ctf_field_type *ret_field_type;
823 struct bt_ctf_event_class *event_class;
824 struct bt_ctf_event *event;
825 struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
826 *inner_structure_field, *complex_structure_field,
827 *a_sequence_field, *enum_variant_field, *enum_container_field,
828 *variant_field, *an_array_field, *ret_field;
829 uint64_t ret_unsigned_int;
830 int64_t ret_signed_int;
831 const char *ret_string;
832 struct bt_ctf_stream_class *ret_stream_class;
833 struct bt_ctf_event_class *ret_event_class;
834 struct bt_ctf_field *packet_context, *packet_context_field;
835 struct bt_value *obj;
836
837 ok(bt_ctf_field_type_set_alignment(int_16_type, 0),
838 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
839 ok(bt_ctf_field_type_set_alignment(int_16_type, 3),
840 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
841 ok(bt_ctf_field_type_set_alignment(int_16_type, 24),
842 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
843 ok(!bt_ctf_field_type_set_alignment(int_16_type, 4),
844 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
845 bt_ctf_field_type_set_alignment(int_16_type, 32);
846 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
847 bt_ctf_field_type_integer_set_base(uint_35_type,
848 BT_CTF_INTEGER_BASE_HEXADECIMAL);
849
850 array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH);
851 sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
852 "seq_len");
853
854 ok(bt_ctf_field_type_array_get_element_type(NULL) == NULL,
855 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
856 ret_field_type = bt_ctf_field_type_array_get_element_type(
857 array_type);
858 ok(ret_field_type == int_16_type,
859 "bt_ctf_field_type_array_get_element_type returns the correct type");
860 bt_put(ret_field_type);
861
862 ok(bt_ctf_field_type_array_get_length(NULL) < 0,
863 "bt_ctf_field_type_array_get_length handles NULL correctly");
864 ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH,
865 "bt_ctf_field_type_array_get_length returns the correct length");
866
867 bt_ctf_field_type_structure_add_field(inner_structure_type,
868 uint_35_type, "seq_len");
869 bt_ctf_field_type_structure_add_field(inner_structure_type,
870 sequence_type, "a_sequence");
871 bt_ctf_field_type_structure_add_field(inner_structure_type,
872 array_type, "an_array");
873
874 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
875 "UINT3_TYPE", 0, 0);
876 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
877 "INT16_TYPE", 1, 1);
878 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
879 "UINT35_TYPE", 2, 7);
880
881 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL,
882 "INT16_TYPE") < 0,
883 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
884 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
885 enum_variant_type, NULL) < 0,
886 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
887 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
888 enum_variant_type, "INT16_TYPE") == 1,
889 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
890
891 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL, 1) < 0,
892 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
893 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, -42) < 0,
894 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
895 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 5) == 2,
896 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
897
898 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
899 "An unknown entry"), "Reject a variant field based on an unknown tag value");
900 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
901 "UINT3_TYPE") == 0, "Add a field to a variant");
902 bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
903 "INT16_TYPE");
904 bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
905 "UINT35_TYPE");
906
907 ok(bt_ctf_field_type_variant_get_tag_type(NULL) == NULL,
908 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
909 ret_field_type = bt_ctf_field_type_variant_get_tag_type(variant_type);
910 ok(ret_field_type == enum_variant_type,
911 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
912 bt_put(ret_field_type);
913
914 ok(bt_ctf_field_type_variant_get_tag_name(NULL) == NULL,
915 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
916 ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type);
917 ok(ret_string ? !strcmp(ret_string, "variant_selector") : 0,
918 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
919 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL,
920 "INT16_TYPE") == NULL,
921 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
922 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type,
923 NULL) == NULL,
924 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
925 ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name(
926 variant_type, "INT16_TYPE");
927 ok(ret_field_type == int_16_type,
928 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
929 bt_put(ret_field_type);
930
931 ok(bt_ctf_field_type_variant_get_field_count(NULL) < 0,
932 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
933 ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3,
934 "bt_ctf_field_type_variant_get_field_count returns the correct count");
935
936 ok(bt_ctf_field_type_variant_get_field(NULL, &ret_string, &ret_field_type, 0) < 0,
937 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
938 ok(bt_ctf_field_type_variant_get_field(variant_type, NULL, &ret_field_type, 0) == 0,
939 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
940 bt_put(ret_field_type);
941 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, NULL, 0) == 0,
942 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
943 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 200) < 0,
944 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
945 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 1) == 0,
946 "bt_ctf_field_type_variant_get_field returns a field");
947 ok(!strcmp("INT16_TYPE", ret_string),
948 "bt_ctf_field_type_variant_get_field returns a correct field name");
949 ok(ret_field_type == int_16_type,
950 "bt_ctf_field_type_variant_get_field returns a correct field type");
951 bt_put(ret_field_type);
952
953 bt_ctf_field_type_structure_add_field(complex_structure_type,
954 enum_variant_type, "variant_selector");
955 bt_ctf_field_type_structure_add_field(complex_structure_type,
956 string_type, "a_string");
957 bt_ctf_field_type_structure_add_field(complex_structure_type,
958 variant_type, "variant_value");
959 bt_ctf_field_type_structure_add_field(complex_structure_type,
960 inner_structure_type, "inner_structure");
961
962 ok(bt_ctf_event_class_create("clock") == NULL,
963 "Reject creation of an event class with an illegal name");
964 event_class = bt_ctf_event_class_create(complex_test_event_string);
965 ok(event_class, "Create an event class");
966 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
967 "Reject addition of a field with an empty name to an event");
968 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
969 "Reject addition of a field with a NULL type to an event");
970 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
971 "int"),
972 "Reject addition of a type with an illegal name to an event");
973 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
974 "uint_35") == 0,
975 "Add field of type unsigned integer to an event");
976 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
977 "int_16") == 0, "Add field of type signed integer to an event");
978 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
979 "complex_structure") == 0,
980 "Add composite structure to an event");
981
982 ok(bt_ctf_event_class_get_name(NULL) == NULL,
983 "bt_ctf_event_class_get_name handles NULL correctly");
984 ret_string = bt_ctf_event_class_get_name(event_class);
985 ok(!strcmp(ret_string, complex_test_event_string),
986 "bt_ctf_event_class_get_name returns a correct name");
987 ok(bt_ctf_event_class_get_id(event_class) < 0,
988 "bt_ctf_event_class_get_id returns a negative value when not set");
989 ok(bt_ctf_event_class_get_id(NULL) < 0,
990 "bt_ctf_event_class_get_id handles NULL correctly");
991 ok(bt_ctf_event_class_set_id(NULL, 42) < 0,
992 "bt_ctf_event_class_set_id handles NULL correctly");
993 ok(bt_ctf_event_class_set_id(event_class, 42) == 0,
994 "Set an event class' id");
995 ok(bt_ctf_event_class_get_id(event_class) == 42,
996 "bt_ctf_event_class_get_id returns the correct value");
997
998 /* Test event class attributes */
999 obj = bt_value_integer_create_init(15);
1000 assert(obj);
1001 ok(bt_ctf_event_class_set_attribute(NULL, "id", obj),
1002 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
1003 ok(bt_ctf_event_class_set_attribute(event_class, NULL, obj),
1004 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
1005 ok(bt_ctf_event_class_set_attribute(event_class, "id", NULL),
1006 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
1007 assert(!bt_value_integer_set(obj, -3));
1008 ok(bt_ctf_event_class_set_attribute(event_class, "id", obj),
1009 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
1010 assert(!bt_value_integer_set(obj, 11));
1011 ret = bt_ctf_event_class_set_attribute(event_class, "id", obj);
1012 ok(!ret && bt_ctf_event_class_get_id(event_class) == 11,
1013 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
1014 ret = bt_ctf_event_class_set_attribute(event_class, "name", obj);
1015 ret &= bt_ctf_event_class_set_attribute(event_class, "model.emf.uri", obj);
1016 ok(ret,
1017 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
1018 BT_PUT(obj);
1019
1020 obj = bt_value_integer_create_init(5);
1021 assert(obj);
1022 ok(!bt_ctf_event_class_set_attribute(event_class, "loglevel", obj),
1023 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
1024 BT_PUT(obj);
1025 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL, "loglevel"),
1026 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
1027 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class, NULL),
1028 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
1029 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class, "meow"),
1030 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
1031 obj = bt_ctf_event_class_get_attribute_value_by_name(event_class,
1032 "loglevel");
1033 int64_value = 0;
1034 ret = bt_value_integer_get(obj, &int64_value);
1035 ok(obj && !ret && int64_value == 5,
1036 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
1037 BT_PUT(obj);
1038
1039 obj = bt_value_string_create_init("nu name");
1040 assert(obj);
1041 assert(!bt_ctf_event_class_set_attribute(event_class, "name", obj));
1042 ret_string = bt_ctf_event_class_get_name(event_class);
1043 ok(!strcmp(ret_string, "nu name"),
1044 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
1045 ret = bt_ctf_event_class_set_attribute(event_class, "id", obj);
1046 ret &= bt_ctf_event_class_set_attribute(event_class, "loglevel", obj);
1047 ok(ret,
1048 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
1049 BT_PUT(obj);
1050 obj = bt_value_string_create_init("http://kernel.org/");
1051 assert(obj);
1052 assert(!bt_ctf_event_class_set_attribute(event_class, "model.emf.uri", obj));
1053 BT_PUT(obj);
1054
1055 ok(bt_ctf_event_class_get_attribute_count(NULL),
1056 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
1057 ok(bt_ctf_event_class_get_attribute_count(event_class) == 4,
1058 "bt_ctf_event_class_get_attribute_count returns the correct count");
1059 ok(!bt_ctf_event_class_get_attribute_name(NULL, 0),
1060 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
1061 ok(!bt_ctf_event_class_get_attribute_name(event_class, 4),
1062 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
1063 ok(!bt_ctf_event_class_get_attribute_value(NULL, 0),
1064 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
1065 ok(!bt_ctf_event_class_get_attribute_value(event_class, 4),
1066 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
1067
1068 memset(&attrs_count, 0, sizeof(attrs_count));
1069
1070 for (i = 0; i < 4; ++i) {
1071 ret_string = bt_ctf_event_class_get_attribute_name(event_class,
1072 i);
1073 obj = bt_ctf_event_class_get_attribute_value(event_class, i);
1074 assert(ret_string && obj);
1075
1076 if (!strcmp(ret_string, "id")) {
1077 attrs_count.id++;
1078 ok(bt_value_is_integer(obj),
1079 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1080 ret_string);
1081 } else if (!strcmp(ret_string, "name")) {
1082 attrs_count.name++;
1083 ok(bt_value_is_string(obj),
1084 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1085 ret_string);
1086 } else if (!strcmp(ret_string, "loglevel")) {
1087 attrs_count.loglevel++;
1088 ok(bt_value_is_integer(obj),
1089 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1090 ret_string);
1091 } else if (!strcmp(ret_string, "model.emf.uri")) {
1092 attrs_count.modelemfuri++;
1093 ok(bt_value_is_string(obj),
1094 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1095 ret_string);
1096 } else {
1097 attrs_count.unknown++;
1098 }
1099
1100 BT_PUT(obj);
1101 }
1102
1103 ok(attrs_count.unknown == 0, "event class has no unknown attributes");
1104 ok(attrs_count.id == 1 && attrs_count.name == 1 &&
1105 attrs_count.loglevel == 1 && attrs_count.modelemfuri == 1,
1106 "event class has one instance of each known attribute");
1107
1108 /* Add event class to the stream class */
1109 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
1110 "Reject addition of NULL event class to a stream class");
1111 ok(bt_ctf_stream_class_add_event_class(stream_class,
1112 event_class) == 0, "Add an event class to stream class");
1113
1114 ok(bt_ctf_event_class_get_stream_class(NULL) == NULL,
1115 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1116 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
1117 ok(ret_stream_class == stream_class,
1118 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1119 bt_put(ret_stream_class);
1120
1121 ok(bt_ctf_event_class_get_field_count(NULL) < 0,
1122 "bt_ctf_event_class_get_field_count handles NULL correctly");
1123 ok(bt_ctf_event_class_get_field_count(event_class) == 3,
1124 "bt_ctf_event_class_get_field_count returns a correct value");
1125
1126 ok(bt_ctf_event_class_get_field(NULL, &ret_string,
1127 &ret_field_type, 0) < 0,
1128 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1129 ok(bt_ctf_event_class_get_field(event_class, NULL,
1130 &ret_field_type, 0) == 0,
1131 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1132 bt_put(ret_field_type);
1133 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1134 NULL, 0) == 0,
1135 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1136 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1137 &ret_field_type, 42) < 0,
1138 "bt_ctf_event_class_get_field handles an invalid index correctly");
1139 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1140 &ret_field_type, 0) == 0,
1141 "bt_ctf_event_class_get_field returns a field");
1142 ok(bt_ctf_field_type_compare(ret_field_type, uint_35_type) == 0,
1143 "bt_ctf_event_class_get_field returns a correct field type");
1144 bt_put(ret_field_type);
1145 ok(!strcmp(ret_string, "uint_35"),
1146 "bt_ctf_event_class_get_field returns a correct field name");
1147 ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL,
1148 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1149 ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL,
1150 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1151 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
1152 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1153 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
1154 "complex_structure");
1155 ok(bt_ctf_field_type_compare(ret_field_type, complex_structure_type) == 0,
1156 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1157 bt_put(ret_field_type);
1158
1159 event = bt_ctf_event_create(event_class);
1160 ok(event, "Instanciate a complex event");
1161
1162 ok(bt_ctf_event_get_class(NULL) == NULL,
1163 "bt_ctf_event_get_class handles NULL correctly");
1164 ret_event_class = bt_ctf_event_get_class(event);
1165 ok(ret_event_class == event_class,
1166 "bt_ctf_event_get_class returns the correct event class");
1167 bt_put(ret_event_class);
1168
1169 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
1170 if (!uint_35_field) {
1171 printf("uint_35_field is NULL\n");
1172 }
1173
1174 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
1175 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
1176 ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0,
1177 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1178 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0,
1179 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1180 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
1181 &ret_unsigned_int) == 0,
1182 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1183 ok(ret_unsigned_int == 0x0DDF00D,
1184 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1185 ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
1186 &ret_signed_int) < 0,
1187 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1188 bt_put(uint_35_field);
1189
1190 int_16_field = bt_ctf_event_get_payload(event, "int_16");
1191 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
1192 ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0,
1193 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1194 ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0,
1195 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1196 ok(bt_ctf_field_signed_integer_get_value(int_16_field,
1197 &ret_signed_int) == 0,
1198 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1199 ok(ret_signed_int == -12345,
1200 "bt_ctf_field_signed_integer_get_value returns the correct value");
1201 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
1202 &ret_unsigned_int) < 0,
1203 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1204 bt_put(int_16_field);
1205
1206 complex_structure_field = bt_ctf_event_get_payload(event,
1207 "complex_structure");
1208
1209 ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
1210 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1211 ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
1212 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1213 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
1214 complex_structure_field, 3);
1215 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
1216 bt_put(inner_structure_field);
1217 ok(bt_ctf_field_type_compare(ret_field_type, inner_structure_type) == 0,
1218 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1219 bt_put(ret_field_type);
1220
1221 inner_structure_field = bt_ctf_field_structure_get_field(
1222 complex_structure_field, "inner_structure");
1223 a_string_field = bt_ctf_field_structure_get_field(
1224 complex_structure_field, "a_string");
1225 enum_variant_field = bt_ctf_field_structure_get_field(
1226 complex_structure_field, "variant_selector");
1227 variant_field = bt_ctf_field_structure_get_field(
1228 complex_structure_field, "variant_value");
1229 uint_35_field = bt_ctf_field_structure_get_field(
1230 inner_structure_field, "seq_len");
1231 a_sequence_field = bt_ctf_field_structure_get_field(
1232 inner_structure_field, "a_sequence");
1233 an_array_field = bt_ctf_field_structure_get_field(
1234 inner_structure_field, "an_array");
1235
1236 enum_container_field = bt_ctf_field_enumeration_get_container(
1237 enum_variant_field);
1238 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
1239 int_16_field = bt_ctf_field_variant_get_field(variant_field,
1240 enum_variant_field);
1241 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
1242 bt_put(int_16_field);
1243 ok(!bt_ctf_field_string_get_value(a_string_field),
1244 "bt_ctf_field_string_get_value returns NULL on an unset field");
1245 bt_ctf_field_string_set_value(a_string_field,
1246 test_string_1);
1247 ok(!bt_ctf_field_string_get_value(NULL),
1248 "bt_ctf_field_string_get_value correctly handles NULL");
1249 ok(bt_ctf_field_string_append(NULL, "yeah"),
1250 "bt_ctf_field_string_append correctly handles a NULL string field");
1251 ok(bt_ctf_field_string_append(a_string_field, NULL),
1252 "bt_ctf_field_string_append correctly handles a NULL string value");
1253 ok(!bt_ctf_field_string_append(a_string_field, test_string_2),
1254 "bt_ctf_field_string_append succeeds");
1255 ok(bt_ctf_field_string_append_len(NULL, "oh noes", 3),
1256 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1257 ok(bt_ctf_field_string_append_len(a_string_field, NULL, 3),
1258 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1259 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 5),
1260 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1261 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_4, 10),
1262 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1263 ok(!bt_ctf_field_string_append_len(a_string_field, &test_string_4[4], 3),
1264 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1265 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 0),
1266 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1267
1268 ret_string = bt_ctf_field_string_get_value(a_string_field);
1269 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
1270 ok(ret_string ? !strcmp(ret_string, test_string_cat) : 0,
1271 "bt_ctf_field_string_get_value returns a correct value");
1272 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
1273 SEQUENCE_TEST_LENGTH);
1274
1275 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL,
1276 enum_container_field) == NULL,
1277 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1278 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type,
1279 NULL) == NULL,
1280 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1281 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
1282 variant_type, enum_variant_field);
1283 ok(ret_field_type == int_16_type,
1284 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1285
1286 ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
1287 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1288 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
1289 uint_35_field) == 0, "Set a sequence field's length");
1290 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
1291 ok(ret_field == uint_35_field,
1292 "bt_ctf_field_sequence_get_length returns the correct length field");
1293 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
1294 "bt_ctf_field_sequence_get_length properly handles NULL");
1295
1296 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
1297 int_16_field = bt_ctf_field_sequence_get_field(
1298 a_sequence_field, i);
1299 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
1300 bt_put(int_16_field);
1301 }
1302
1303 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
1304 int_16_field = bt_ctf_field_array_get_field(
1305 an_array_field, i);
1306 bt_ctf_field_signed_integer_set_value(int_16_field, i);
1307 bt_put(int_16_field);
1308 }
1309
1310 bt_ctf_clock_set_time(clock, ++current_time);
1311 ok(bt_ctf_stream_append_event(stream, event) == 0,
1312 "Append a complex event to a stream");
1313
1314 /*
1315 * Populate the custom packet context field with a dummy value
1316 * otherwise flush will fail.
1317 */
1318 packet_context = bt_ctf_stream_get_packet_context(stream);
1319 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
1320 "custom_packet_context_field");
1321 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1);
1322
1323 ok(bt_ctf_stream_flush(stream) == 0,
1324 "Flush a stream containing a complex event");
1325
1326 bt_put(uint_35_field);
1327 bt_put(a_string_field);
1328 bt_put(inner_structure_field);
1329 bt_put(complex_structure_field);
1330 bt_put(a_sequence_field);
1331 bt_put(an_array_field);
1332 bt_put(enum_variant_field);
1333 bt_put(enum_container_field);
1334 bt_put(variant_field);
1335 bt_put(ret_field);
1336 bt_put(packet_context_field);
1337 bt_put(packet_context);
1338 bt_put(uint_35_type);
1339 bt_put(int_16_type);
1340 bt_put(string_type);
1341 bt_put(sequence_type);
1342 bt_put(array_type);
1343 bt_put(inner_structure_type);
1344 bt_put(complex_structure_type);
1345 bt_put(uint_3_type);
1346 bt_put(enum_variant_type);
1347 bt_put(variant_type);
1348 bt_put(ret_field_type);
1349 bt_put(event_class);
1350 bt_put(event);
1351 }
1352
1353 static void field_copy_tests_validate_same_type(struct bt_ctf_field *field,
1354 struct bt_ctf_field_type *expected_type, const char *name)
1355 {
1356 struct bt_ctf_field_type *copy_type;
1357
1358 copy_type = bt_ctf_field_get_type(field);
1359 ok(copy_type == expected_type,
1360 "bt_ctf_field_copy does not copy the type (%s)", name);
1361 bt_put(copy_type);
1362 }
1363
1364 static void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field *field_a,
1365 struct bt_ctf_field *field_b, const char *name)
1366 {
1367 ok(field_a != field_b,
1368 "bt_ctf_field_copy creates different pointers (%s)", name);
1369 }
1370
1371 void field_copy_tests()
1372 {
1373 struct bt_ctf_field_type *len_type = NULL;
1374 struct bt_ctf_field_type *fp_type = NULL;
1375 struct bt_ctf_field_type *s_type = NULL;
1376 struct bt_ctf_field_type *e_int_type = NULL;
1377 struct bt_ctf_field_type *e_type = NULL;
1378 struct bt_ctf_field_type *v_type = NULL;
1379 struct bt_ctf_field_type *v_label1_type = NULL;
1380 struct bt_ctf_field_type *v_label1_array_type = NULL;
1381 struct bt_ctf_field_type *v_label2_type = NULL;
1382 struct bt_ctf_field_type *v_label2_seq_type = NULL;
1383 struct bt_ctf_field_type *strct_type = NULL;
1384 struct bt_ctf_field *len = NULL;
1385 struct bt_ctf_field *fp = NULL;
1386 struct bt_ctf_field *s = NULL;
1387 struct bt_ctf_field *e_int = NULL;
1388 struct bt_ctf_field *e = NULL;
1389 struct bt_ctf_field *v = NULL;
1390 struct bt_ctf_field *v_selected = NULL;
1391 struct bt_ctf_field *v_selected_cur = NULL;
1392 struct bt_ctf_field *v_selected_0 = NULL;
1393 struct bt_ctf_field *v_selected_1 = NULL;
1394 struct bt_ctf_field *v_selected_2 = NULL;
1395 struct bt_ctf_field *v_selected_3 = NULL;
1396 struct bt_ctf_field *v_selected_4 = NULL;
1397 struct bt_ctf_field *v_selected_5 = NULL;
1398 struct bt_ctf_field *v_selected_6 = NULL;
1399 struct bt_ctf_field *a = NULL;
1400 struct bt_ctf_field *a_0 = NULL;
1401 struct bt_ctf_field *a_1 = NULL;
1402 struct bt_ctf_field *a_2 = NULL;
1403 struct bt_ctf_field *a_3 = NULL;
1404 struct bt_ctf_field *a_4 = NULL;
1405 struct bt_ctf_field *strct = NULL;
1406 struct bt_ctf_field *len_copy = NULL;
1407 struct bt_ctf_field *fp_copy = NULL;
1408 struct bt_ctf_field *s_copy = NULL;
1409 struct bt_ctf_field *e_int_copy = NULL;
1410 struct bt_ctf_field *e_copy = NULL;
1411 struct bt_ctf_field *v_copy = NULL;
1412 struct bt_ctf_field *v_selected_copy = NULL;
1413 struct bt_ctf_field *v_selected_copy_len = NULL;
1414 struct bt_ctf_field *v_selected_0_copy = NULL;
1415 struct bt_ctf_field *v_selected_1_copy = NULL;
1416 struct bt_ctf_field *v_selected_2_copy = NULL;
1417 struct bt_ctf_field *v_selected_3_copy = NULL;
1418 struct bt_ctf_field *v_selected_4_copy = NULL;
1419 struct bt_ctf_field *v_selected_5_copy = NULL;
1420 struct bt_ctf_field *v_selected_6_copy = NULL;
1421 struct bt_ctf_field *a_copy = NULL;
1422 struct bt_ctf_field *a_0_copy = NULL;
1423 struct bt_ctf_field *a_1_copy = NULL;
1424 struct bt_ctf_field *a_2_copy = NULL;
1425 struct bt_ctf_field *a_3_copy = NULL;
1426 struct bt_ctf_field *a_4_copy = NULL;
1427 struct bt_ctf_field *strct_copy = NULL;
1428 uint64_t uint64_t_val;
1429 const char *str_val;
1430 double double_val;
1431 int ret;
1432
1433 /* create len type */
1434 len_type = bt_ctf_field_type_integer_create(32);
1435 assert(len_type);
1436
1437 /* create fp type */
1438 fp_type = bt_ctf_field_type_floating_point_create();
1439 assert(fp_type);
1440
1441 /* create s type */
1442 s_type = bt_ctf_field_type_string_create();
1443 assert(s_type);
1444
1445 /* create e_int type */
1446 e_int_type = bt_ctf_field_type_integer_create(8);
1447 assert(e_int_type);
1448
1449 /* create e type */
1450 e_type = bt_ctf_field_type_enumeration_create(e_int_type);
1451 assert(e_type);
1452 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL1",
1453 10, 15);
1454 assert(!ret);
1455 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL2",
1456 23, 23);
1457 assert(!ret);
1458
1459 /* create v_label1 type */
1460 v_label1_type = bt_ctf_field_type_string_create();
1461 assert(v_label1_type);
1462
1463 /* create v_label1_array type */
1464 v_label1_array_type = bt_ctf_field_type_array_create(v_label1_type, 5);
1465 assert(v_label1_array_type);
1466
1467 /* create v_label2 type */
1468 v_label2_type = bt_ctf_field_type_integer_create(16);
1469 assert(v_label2_type);
1470
1471 /* create v_label2_seq type */
1472 v_label2_seq_type = bt_ctf_field_type_sequence_create(v_label2_type,
1473 "len");
1474 assert(v_label2_seq_type);
1475
1476 /* create v type */
1477 v_type = bt_ctf_field_type_variant_create(e_type, "e");
1478 assert(v_type);
1479 ret = bt_ctf_field_type_variant_add_field(v_type, v_label1_array_type,
1480 "LABEL1");
1481 assert(!ret);
1482 ret = bt_ctf_field_type_variant_add_field(v_type, v_label2_seq_type,
1483 "LABEL2");
1484 assert(!ret);
1485
1486 /* create strct type */
1487 strct_type = bt_ctf_field_type_structure_create();
1488 assert(strct_type);
1489 ret = bt_ctf_field_type_structure_add_field(strct_type, len_type,
1490 "len");
1491 assert(!ret);
1492 ret = bt_ctf_field_type_structure_add_field(strct_type, fp_type, "fp");
1493 assert(!ret);
1494 ret = bt_ctf_field_type_structure_add_field(strct_type, s_type, "s");
1495 assert(!ret);
1496 ret = bt_ctf_field_type_structure_add_field(strct_type, e_type, "e");
1497 assert(!ret);
1498 ret = bt_ctf_field_type_structure_add_field(strct_type, v_type, "v");
1499 assert(!ret);
1500 ret = bt_ctf_field_type_structure_add_field(strct_type,
1501 v_label1_array_type, "a");
1502 assert(!ret);
1503
1504 /* create strct */
1505 strct = bt_ctf_field_create(strct_type);
1506 assert(strct);
1507
1508 /* get len field */
1509 len = bt_ctf_field_structure_get_field(strct, "len");
1510 assert(len);
1511
1512 /* get fp field */
1513 fp = bt_ctf_field_structure_get_field(strct, "fp");
1514 assert(fp);
1515
1516 /* get s field */
1517 s = bt_ctf_field_structure_get_field(strct, "s");
1518 assert(s);
1519
1520 /* get e field */
1521 e = bt_ctf_field_structure_get_field(strct, "e");
1522 assert(e);
1523
1524 /* get e_int (underlying integer) */
1525 e_int = bt_ctf_field_enumeration_get_container(e);
1526 assert(e_int);
1527
1528 /* get v field */
1529 v = bt_ctf_field_structure_get_field(strct, "v");
1530 assert(v);
1531
1532 /* get a field */
1533 a = bt_ctf_field_structure_get_field(strct, "a");
1534 assert(a);
1535
1536 /* set len field */
1537 ret = bt_ctf_field_unsigned_integer_set_value(len, 7);
1538 assert(!ret);
1539
1540 /* set fp field */
1541 ret = bt_ctf_field_floating_point_set_value(fp, 3.14);
1542 assert(!ret);
1543
1544 /* set s field */
1545 ret = bt_ctf_field_string_set_value(s, "btbt");
1546 assert(!ret);
1547
1548 /* set e field (LABEL2) */
1549 ret = bt_ctf_field_unsigned_integer_set_value(e_int, 23);
1550 assert(!ret);
1551
1552 /* set v field */
1553 v_selected = bt_ctf_field_variant_get_field(v, e);
1554 assert(v_selected);
1555 ok(!bt_ctf_field_variant_get_current_field(NULL),
1556 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1557 v_selected_cur = bt_ctf_field_variant_get_current_field(v);
1558 ok(v_selected_cur == v_selected,
1559 "bt_ctf_field_variant_get_current_field returns the current field");
1560 bt_put(v_selected_cur);
1561
1562 /* set selected v field */
1563 ret = bt_ctf_field_sequence_set_length(v_selected, len);
1564 assert(!ret);
1565 v_selected_0 = bt_ctf_field_sequence_get_field(v_selected, 0);
1566 assert(v_selected_0);
1567 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_0, 7);
1568 assert(!ret);
1569 v_selected_1 = bt_ctf_field_sequence_get_field(v_selected, 1);
1570 assert(v_selected_1);
1571 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_1, 6);
1572 assert(!ret);
1573 v_selected_2 = bt_ctf_field_sequence_get_field(v_selected, 2);
1574 assert(v_selected_2);
1575 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_2, 5);
1576 assert(!ret);
1577 v_selected_3 = bt_ctf_field_sequence_get_field(v_selected, 3);
1578 assert(v_selected_3);
1579 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_3, 4);
1580 assert(!ret);
1581 v_selected_4 = bt_ctf_field_sequence_get_field(v_selected, 4);
1582 assert(v_selected_4);
1583 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_4, 3);
1584 assert(!ret);
1585 v_selected_5 = bt_ctf_field_sequence_get_field(v_selected, 5);
1586 assert(v_selected_5);
1587 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_5, 2);
1588 assert(!ret);
1589 v_selected_6 = bt_ctf_field_sequence_get_field(v_selected, 6);
1590 assert(v_selected_6);
1591 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_6, 1);
1592 assert(!ret);
1593
1594 /* set a field */
1595 a_0 = bt_ctf_field_array_get_field(a, 0);
1596 assert(a_0);
1597 ret = bt_ctf_field_string_set_value(a_0, "a_0");
1598 assert(!ret);
1599 a_1 = bt_ctf_field_array_get_field(a, 1);
1600 assert(a_1);
1601 ret = bt_ctf_field_string_set_value(a_1, "a_1");
1602 assert(!ret);
1603 a_2 = bt_ctf_field_array_get_field(a, 2);
1604 assert(a_2);
1605 ret = bt_ctf_field_string_set_value(a_2, "a_2");
1606 assert(!ret);
1607 a_3 = bt_ctf_field_array_get_field(a, 3);
1608 assert(a_3);
1609 ret = bt_ctf_field_string_set_value(a_3, "a_3");
1610 assert(!ret);
1611 a_4 = bt_ctf_field_array_get_field(a, 4);
1612 assert(a_4);
1613 ret = bt_ctf_field_string_set_value(a_4, "a_4");
1614 assert(!ret);
1615
1616 /* create copy of strct */
1617 ok(!bt_ctf_field_copy(NULL),
1618 "bt_ctf_field_copy handles NULL correctly");
1619 strct_copy = bt_ctf_field_copy(strct);
1620 ok(strct_copy,
1621 "bt_ctf_field_copy returns a valid pointer");
1622
1623 /* get all copied fields */
1624 len_copy = bt_ctf_field_structure_get_field(strct_copy, "len");
1625 assert(len_copy);
1626 fp_copy = bt_ctf_field_structure_get_field(strct_copy, "fp");
1627 assert(fp_copy);
1628 s_copy = bt_ctf_field_structure_get_field(strct_copy, "s");
1629 assert(s_copy);
1630 e_copy = bt_ctf_field_structure_get_field(strct_copy, "e");
1631 assert(e_copy);
1632 e_int_copy = bt_ctf_field_enumeration_get_container(e_copy);
1633 assert(e_int_copy);
1634 v_copy = bt_ctf_field_structure_get_field(strct_copy, "v");
1635 assert(v_copy);
1636 v_selected_copy = bt_ctf_field_variant_get_field(v_copy, e_copy);
1637 assert(v_selected_copy);
1638 v_selected_0_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 0);
1639 assert(v_selected_0_copy);
1640 v_selected_1_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 1);
1641 assert(v_selected_1_copy);
1642 v_selected_2_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 2);
1643 assert(v_selected_2_copy);
1644 v_selected_3_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 3);
1645 assert(v_selected_3_copy);
1646 v_selected_4_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 4);
1647 assert(v_selected_4_copy);
1648 v_selected_5_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 5);
1649 assert(v_selected_5_copy);
1650 v_selected_6_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 6);
1651 assert(v_selected_6_copy);
1652 ok(!bt_ctf_field_sequence_get_field(v_selected_copy, 7),
1653 "sequence field copy is not too large");
1654 a_copy = bt_ctf_field_structure_get_field(strct_copy, "a");
1655 assert(a_copy);
1656 a_0_copy = bt_ctf_field_array_get_field(a_copy, 0);
1657 assert(a_0_copy);
1658 a_1_copy = bt_ctf_field_array_get_field(a_copy, 1);
1659 assert(a_1_copy);
1660 a_2_copy = bt_ctf_field_array_get_field(a_copy, 2);
1661 assert(a_2_copy);
1662 a_3_copy = bt_ctf_field_array_get_field(a_copy, 3);
1663 assert(a_3_copy);
1664 a_4_copy = bt_ctf_field_array_get_field(a_copy, 4);
1665 assert(a_4_copy);
1666 ok(!bt_ctf_field_array_get_field(v_selected_copy, 5),
1667 "array field copy is not too large");
1668
1669 /* make sure copied fields are different pointers */
1670 field_copy_tests_validate_diff_ptrs(strct_copy, strct, "strct");
1671 field_copy_tests_validate_diff_ptrs(len_copy, len, "len");
1672 field_copy_tests_validate_diff_ptrs(fp_copy, fp, "fp");
1673 field_copy_tests_validate_diff_ptrs(s_copy, s, "s");
1674 field_copy_tests_validate_diff_ptrs(e_int_copy, e_int, "e_int");
1675 field_copy_tests_validate_diff_ptrs(e_copy, e, "e");
1676 field_copy_tests_validate_diff_ptrs(v_copy, v, "v");
1677 field_copy_tests_validate_diff_ptrs(v_selected_copy, v_selected,
1678 "v_selected");
1679 field_copy_tests_validate_diff_ptrs(v_selected_0_copy, v_selected_0,
1680 "v_selected_0");
1681 field_copy_tests_validate_diff_ptrs(v_selected_1_copy, v_selected_1,
1682 "v_selected_1");
1683 field_copy_tests_validate_diff_ptrs(v_selected_2_copy, v_selected_2,
1684 "v_selected_2");
1685 field_copy_tests_validate_diff_ptrs(v_selected_3_copy, v_selected_3,
1686 "v_selected_3");
1687 field_copy_tests_validate_diff_ptrs(v_selected_4_copy, v_selected_4,
1688 "v_selected_4");
1689 field_copy_tests_validate_diff_ptrs(v_selected_5_copy, v_selected_5,
1690 "v_selected_5");
1691 field_copy_tests_validate_diff_ptrs(v_selected_6_copy, v_selected_6,
1692 "v_selected_6");
1693 field_copy_tests_validate_diff_ptrs(a_copy, a, "a");
1694 field_copy_tests_validate_diff_ptrs(a_0_copy, a_0, "a_0");
1695 field_copy_tests_validate_diff_ptrs(a_1_copy, a_1, "a_1");
1696 field_copy_tests_validate_diff_ptrs(a_2_copy, a_2, "a_2");
1697 field_copy_tests_validate_diff_ptrs(a_3_copy, a_3, "a_3");
1698 field_copy_tests_validate_diff_ptrs(a_4_copy, a_4, "a_4");
1699
1700 /* make sure copied fields share the same types */
1701 field_copy_tests_validate_same_type(strct_copy, strct_type, "strct");
1702 field_copy_tests_validate_same_type(len_copy, len_type, "len");
1703 field_copy_tests_validate_same_type(fp_copy, fp_type, "fp");
1704 field_copy_tests_validate_same_type(e_int_copy, e_int_type, "e_int");
1705 field_copy_tests_validate_same_type(e_copy, e_type, "e");
1706 field_copy_tests_validate_same_type(v_copy, v_type, "v");
1707 field_copy_tests_validate_same_type(v_selected_copy, v_label2_seq_type,
1708 "v_selected");
1709 field_copy_tests_validate_same_type(v_selected_0_copy, v_label2_type,
1710 "v_selected_0");
1711 field_copy_tests_validate_same_type(v_selected_1_copy, v_label2_type,
1712 "v_selected_1");
1713 field_copy_tests_validate_same_type(v_selected_2_copy, v_label2_type,
1714 "v_selected_2");
1715 field_copy_tests_validate_same_type(v_selected_3_copy, v_label2_type,
1716 "v_selected_3");
1717 field_copy_tests_validate_same_type(v_selected_4_copy, v_label2_type,
1718 "v_selected_4");
1719 field_copy_tests_validate_same_type(v_selected_5_copy, v_label2_type,
1720 "v_selected_5");
1721 field_copy_tests_validate_same_type(v_selected_6_copy, v_label2_type,
1722 "v_selected_6");
1723 field_copy_tests_validate_same_type(a_copy, v_label1_array_type, "a");
1724 field_copy_tests_validate_same_type(a_0_copy, v_label1_type, "a_0");
1725 field_copy_tests_validate_same_type(a_1_copy, v_label1_type, "a_1");
1726 field_copy_tests_validate_same_type(a_2_copy, v_label1_type, "a_2");
1727 field_copy_tests_validate_same_type(a_3_copy, v_label1_type, "a_3");
1728 field_copy_tests_validate_same_type(a_4_copy, v_label1_type, "a_4");
1729
1730 /* validate len copy */
1731 ret = bt_ctf_field_unsigned_integer_get_value(len_copy, &uint64_t_val);
1732 assert(!ret);
1733 ok(uint64_t_val == 7,
1734 "bt_ctf_field_copy creates a valid integer field copy");
1735
1736 /* validate fp copy */
1737 ret = bt_ctf_field_floating_point_get_value(fp_copy, &double_val);
1738 assert(!ret);
1739 ok(double_val == 3.14,
1740 "bt_ctf_field_copy creates a valid floating point number field copy");
1741
1742 /* validate s copy */
1743 str_val = bt_ctf_field_string_get_value(s_copy);
1744 ok(str_val && !strcmp(str_val, "btbt"),
1745 "bt_ctf_field_copy creates a valid string field copy");
1746
1747 /* validate e_int copy */
1748 ret = bt_ctf_field_unsigned_integer_get_value(e_int_copy,
1749 &uint64_t_val);
1750 assert(!ret);
1751 ok(uint64_t_val == 23,
1752 "bt_ctf_field_copy creates a valid enum's integer field copy");
1753
1754 /* validate e copy */
1755 str_val = bt_ctf_field_enumeration_get_mapping_name(e_copy);
1756 ok(str_val && !strcmp(str_val, "LABEL2"),
1757 "bt_ctf_field_copy creates a valid enum field copy");
1758
1759 /* validate v_selected copy */
1760 v_selected_copy_len = bt_ctf_field_sequence_get_length(v_selected);
1761 assert(v_selected_copy_len);
1762 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len,
1763 &uint64_t_val);
1764 assert(!ret);
1765 ok(uint64_t_val == 7,
1766 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1767 bt_put(v_selected_copy_len);
1768 v_selected_copy_len = NULL;
1769
1770 /* validate v_selected copy fields */
1771 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy,
1772 &uint64_t_val);
1773 assert(!ret);
1774 ok(uint64_t_val == 7,
1775 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1776 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy,
1777 &uint64_t_val);
1778 assert(!ret);
1779 ok(uint64_t_val == 6,
1780 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1781 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy,
1782 &uint64_t_val);
1783 assert(!ret);
1784 ok(uint64_t_val == 5,
1785 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1786 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy,
1787 &uint64_t_val);
1788 assert(!ret);
1789 ok(uint64_t_val == 4,
1790 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1791 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy,
1792 &uint64_t_val);
1793 assert(!ret);
1794 ok(uint64_t_val == 3,
1795 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1796 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy,
1797 &uint64_t_val);
1798 assert(!ret);
1799 ok(uint64_t_val == 2,
1800 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1801 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy,
1802 &uint64_t_val);
1803 assert(!ret);
1804 ok(uint64_t_val == 1,
1805 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1806
1807 /* validate a copy fields */
1808 str_val = bt_ctf_field_string_get_value(a_0_copy);
1809 ok(str_val && !strcmp(str_val, "a_0"),
1810 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1811 str_val = bt_ctf_field_string_get_value(a_1_copy);
1812 ok(str_val && !strcmp(str_val, "a_1"),
1813 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1814 str_val = bt_ctf_field_string_get_value(a_2_copy);
1815 ok(str_val && !strcmp(str_val, "a_2"),
1816 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1817 str_val = bt_ctf_field_string_get_value(a_3_copy);
1818 ok(str_val && !strcmp(str_val, "a_3"),
1819 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1820 str_val = bt_ctf_field_string_get_value(a_4_copy);
1821 ok(str_val && !strcmp(str_val, "a_4"),
1822 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1823
1824 /* put everything */
1825 bt_put(len_type);
1826 bt_put(fp_type);
1827 bt_put(s_type);
1828 bt_put(e_int_type);
1829 bt_put(e_type);
1830 bt_put(v_type);
1831 bt_put(v_label1_type);
1832 bt_put(v_label1_array_type);
1833 bt_put(v_label2_type);
1834 bt_put(v_label2_seq_type);
1835 bt_put(strct_type);
1836 bt_put(len);
1837 bt_put(fp);
1838 bt_put(s);
1839 bt_put(e_int);
1840 bt_put(e);
1841 bt_put(v);
1842 bt_put(v_selected);
1843 bt_put(v_selected_0);
1844 bt_put(v_selected_1);
1845 bt_put(v_selected_2);
1846 bt_put(v_selected_3);
1847 bt_put(v_selected_4);
1848 bt_put(v_selected_5);
1849 bt_put(v_selected_6);
1850 bt_put(a);
1851 bt_put(a_0);
1852 bt_put(a_1);
1853 bt_put(a_2);
1854 bt_put(a_3);
1855 bt_put(a_4);
1856 bt_put(strct);
1857 bt_put(len_copy);
1858 bt_put(fp_copy);
1859 bt_put(s_copy);
1860 bt_put(e_int_copy);
1861 bt_put(e_copy);
1862 bt_put(v_copy);
1863 bt_put(v_selected_copy);
1864 bt_put(v_selected_0_copy);
1865 bt_put(v_selected_1_copy);
1866 bt_put(v_selected_2_copy);
1867 bt_put(v_selected_3_copy);
1868 bt_put(v_selected_4_copy);
1869 bt_put(v_selected_5_copy);
1870 bt_put(v_selected_6_copy);
1871 bt_put(a_copy);
1872 bt_put(a_0_copy);
1873 bt_put(a_1_copy);
1874 bt_put(a_2_copy);
1875 bt_put(a_3_copy);
1876 bt_put(a_4_copy);
1877 bt_put(strct_copy);
1878 }
1879
1880 void type_field_tests()
1881 {
1882 struct bt_ctf_field *uint_12;
1883 struct bt_ctf_field *int_16;
1884 struct bt_ctf_field *string;
1885 struct bt_ctf_field *enumeration;
1886 struct bt_ctf_field_type *composite_structure_type;
1887 struct bt_ctf_field_type *structure_seq_type;
1888 struct bt_ctf_field_type *string_type;
1889 struct bt_ctf_field_type *sequence_type;
1890 struct bt_ctf_field_type *uint_8_type;
1891 struct bt_ctf_field_type *int_16_type;
1892 struct bt_ctf_field_type *uint_12_type =
1893 bt_ctf_field_type_integer_create(12);
1894 struct bt_ctf_field_type *enumeration_type;
1895 struct bt_ctf_field_type *returned_type;
1896 const char *ret_string;
1897
1898 returned_type = bt_ctf_field_get_type(NULL);
1899 ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
1900
1901 ok(uint_12_type, "Create an unsigned integer type");
1902 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1903 BT_CTF_INTEGER_BASE_BINARY) == 0,
1904 "Set integer type's base as binary");
1905 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1906 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
1907 "Set integer type's base as decimal");
1908 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1909 BT_CTF_INTEGER_BASE_UNKNOWN),
1910 "Reject integer type's base set as unknown");
1911 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1912 BT_CTF_INTEGER_BASE_OCTAL) == 0,
1913 "Set integer type's base as octal");
1914 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1915 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
1916 "Set integer type's base as hexadecimal");
1917 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
1918 "Reject unknown integer base value");
1919 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
1920 "Set integer type signedness to signed");
1921 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
1922 "Set integer type signedness to unsigned");
1923 ok(bt_ctf_field_type_integer_get_size(NULL) < 0,
1924 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1925 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
1926 "bt_ctf_field_type_integer_get_size returns a correct value");
1927 ok(bt_ctf_field_type_integer_get_signed(NULL) < 0,
1928 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1929 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
1930 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1931
1932 ok(bt_ctf_field_type_set_byte_order(NULL,
1933 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
1934 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1935 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1936 (enum bt_ctf_byte_order) 42) < 0,
1937 "bt_ctf_field_type_set_byte_order rejects invalid values");
1938 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1939 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
1940 "Set an integer's byte order to little endian");
1941 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1942 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1943 "Set an integer's byte order to big endian");
1944 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
1945 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1946 "bt_ctf_field_type_get_byte_order returns a correct value");
1947 ok(bt_ctf_field_type_get_byte_order(NULL) ==
1948 BT_CTF_BYTE_ORDER_UNKNOWN,
1949 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1950
1951 ok(bt_ctf_field_type_get_type_id(NULL) ==
1952 BT_CTF_TYPE_ID_UNKNOWN,
1953 "bt_ctf_field_type_get_type_id handles NULL correctly");
1954 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
1955 BT_CTF_TYPE_ID_INTEGER,
1956 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1957
1958 ok(bt_ctf_field_type_integer_get_base(NULL) ==
1959 BT_CTF_INTEGER_BASE_UNKNOWN,
1960 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1961 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
1962 BT_CTF_INTEGER_BASE_HEXADECIMAL,
1963 "bt_ctf_field_type_integer_get_base returns a correct value");
1964
1965 ok(bt_ctf_field_type_integer_set_encoding(NULL,
1966 BT_CTF_STRING_ENCODING_ASCII) < 0,
1967 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1968 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1969 (enum bt_ctf_string_encoding) 123) < 0,
1970 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1971 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1972 BT_CTF_STRING_ENCODING_UTF8) == 0,
1973 "Set integer type encoding to UTF8");
1974 ok(bt_ctf_field_type_integer_get_encoding(NULL) ==
1975 BT_CTF_STRING_ENCODING_UNKNOWN,
1976 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1977 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) ==
1978 BT_CTF_STRING_ENCODING_UTF8,
1979 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1980
1981 int_16_type = bt_ctf_field_type_integer_create(16);
1982 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
1983 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
1984 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1985 uint_8_type = bt_ctf_field_type_integer_create(8);
1986 sequence_type =
1987 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
1988 ok(sequence_type, "Create a sequence of int16_t type");
1989 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
1990 BT_CTF_TYPE_ID_SEQUENCE,
1991 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1992
1993 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL,
1994 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1995 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
1996 sequence_type);
1997 ok(!strcmp(ret_string, "seq_len"),
1998 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1999 ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL,
2000 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
2001 returned_type = bt_ctf_field_type_sequence_get_element_type(
2002 sequence_type);
2003 ok(returned_type == int_16_type,
2004 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
2005 bt_put(returned_type);
2006
2007 string_type = bt_ctf_field_type_string_create();
2008 ok(string_type, "Create a string type");
2009 ok(bt_ctf_field_type_string_set_encoding(string_type,
2010 BT_CTF_STRING_ENCODING_NONE),
2011 "Reject invalid \"None\" string encoding");
2012 ok(bt_ctf_field_type_string_set_encoding(string_type,
2013 42),
2014 "Reject invalid string encoding");
2015 ok(bt_ctf_field_type_string_set_encoding(string_type,
2016 BT_CTF_STRING_ENCODING_ASCII) == 0,
2017 "Set string encoding to ASCII");
2018
2019 ok(bt_ctf_field_type_string_get_encoding(NULL) ==
2020 BT_CTF_STRING_ENCODING_UNKNOWN,
2021 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
2022 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
2023 BT_CTF_STRING_ENCODING_ASCII,
2024 "bt_ctf_field_type_string_get_encoding returns the correct value");
2025
2026 structure_seq_type = bt_ctf_field_type_structure_create();
2027 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
2028 BT_CTF_TYPE_ID_STRUCT,
2029 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
2030 ok(structure_seq_type, "Create a structure type");
2031 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
2032 uint_8_type, "seq_len") == 0,
2033 "Add a uint8_t type to a structure");
2034 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
2035 sequence_type, "a_sequence") == 0,
2036 "Add a sequence type to a structure");
2037
2038 ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0,
2039 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
2040 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
2041 "bt_ctf_field_type_structure_get_field_count returns a correct value");
2042
2043 ok(bt_ctf_field_type_structure_get_field(NULL,
2044 &ret_string, &returned_type, 1) < 0,
2045 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
2046 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2047 NULL, &returned_type, 1) == 0,
2048 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
2049 bt_put(returned_type);
2050 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2051 &ret_string, NULL, 1) == 0,
2052 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
2053 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2054 &ret_string, &returned_type, 10) < 0,
2055 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
2056 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
2057 &ret_string, &returned_type, 1) == 0,
2058 "bt_ctf_field_type_structure_get_field returns a field");
2059 ok(!strcmp(ret_string, "a_sequence"),
2060 "bt_ctf_field_type_structure_get_field returns a correct field name");
2061 ok(returned_type == sequence_type,
2062 "bt_ctf_field_type_structure_get_field returns a correct field type");
2063 bt_put(returned_type);
2064
2065 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL,
2066 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2067 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL,
2068 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2069 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
2070 structure_seq_type, "a_sequence");
2071 ok(returned_type == sequence_type,
2072 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2073 bt_put(returned_type);
2074
2075 composite_structure_type = bt_ctf_field_type_structure_create();
2076 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2077 string_type, "a_string") == 0,
2078 "Add a string type to a structure");
2079 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2080 structure_seq_type, "inner_structure") == 0,
2081 "Add a structure type to a structure");
2082
2083 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2084 NULL, "a_sequence") == NULL,
2085 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2086 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2087 structure_seq_type, NULL) == NULL,
2088 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2089 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
2090 structure_seq_type, "a_sequence");
2091 ok(returned_type == sequence_type,
2092 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2093 bt_put(returned_type);
2094
2095 int_16 = bt_ctf_field_create(int_16_type);
2096 ok(int_16, "Instanciate a signed 16-bit integer");
2097 uint_12 = bt_ctf_field_create(uint_12_type);
2098 ok(uint_12, "Instanciate an unsigned 12-bit integer");
2099 returned_type = bt_ctf_field_get_type(int_16);
2100 ok(returned_type == int_16_type,
2101 "bt_ctf_field_get_type returns the correct type");
2102
2103 /* Can't modify types after instanciating them */
2104 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
2105 BT_CTF_INTEGER_BASE_DECIMAL),
2106 "Check an integer type' base can't be modified after instanciation");
2107 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
2108 "Check an integer type's signedness can't be modified after instanciation");
2109
2110 /* Check signed property is checked */
2111 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
2112 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2113 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
2114 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2115
2116 /* Check overflows are properly tested for */
2117 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
2118 "Check -32768 is allowed for a signed 16-bit integer");
2119 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
2120 "Check 32767 is allowed for a signed 16-bit integer");
2121 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
2122 "Check 32768 is not allowed for a signed 16-bit integer");
2123 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
2124 "Check -32769 is not allowed for a signed 16-bit integer");
2125 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
2126 "Check -42 is allowed for a signed 16-bit integer");
2127
2128 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
2129 "Check 4095 is allowed for an unsigned 12-bit integer");
2130 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
2131 "Check 4096 is not allowed for a unsigned 12-bit integer");
2132 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
2133 "Check 0 is allowed for an unsigned 12-bit integer");
2134
2135 string = bt_ctf_field_create(string_type);
2136 ok(string, "Instanciate a string field");
2137 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
2138 "Set a string's value");
2139
2140 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
2141 ok(enumeration_type,
2142 "Create an enumeration type with an unsigned 12-bit integer as container");
2143 enumeration = bt_ctf_field_create(enumeration_type);
2144 ok(!enumeration,
2145 "Check enumeration types are validated before instantiation");
2146
2147 bt_put(string);
2148 bt_put(uint_12);
2149 bt_put(int_16);
2150 bt_put(enumeration);
2151 bt_put(composite_structure_type);
2152 bt_put(structure_seq_type);
2153 bt_put(string_type);
2154 bt_put(sequence_type);
2155 bt_put(uint_8_type);
2156 bt_put(int_16_type);
2157 bt_put(uint_12_type);
2158 bt_put(enumeration_type);
2159 bt_put(returned_type);
2160 }
2161
2162 void packet_resize_test(struct bt_ctf_stream_class *stream_class,
2163 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
2164 {
2165 /*
2166 * Append enough events to force the underlying packet to be resized.
2167 * Also tests that a new event can be declared after a stream has been
2168 * instantiated and used/flushed.
2169 */
2170 int ret = 0;
2171 int i;
2172 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
2173 "Spammy_Event");
2174 struct bt_ctf_field_type *integer_type =
2175 bt_ctf_field_type_integer_create(17);
2176 struct bt_ctf_field_type *string_type =
2177 bt_ctf_field_type_string_create();
2178 struct bt_ctf_event *event = NULL;
2179 struct bt_ctf_field *ret_field = NULL;
2180 struct bt_ctf_field_type *ret_field_type = NULL;
2181 uint64_t ret_uint64;
2182 int events_appended = 0;
2183 struct bt_ctf_field *packet_context = NULL,
2184 *packet_context_field = NULL, *event_context = NULL;
2185 struct bt_ctf_field_type *ep_field_1_type = NULL;
2186 struct bt_ctf_field_type *ep_a_string_type = NULL;
2187 struct bt_ctf_field_type *ep_type = NULL;
2188
2189 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
2190 "field_1");
2191 ret |= bt_ctf_event_class_add_field(event_class, string_type,
2192 "a_string");
2193 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
2194 ok(ret == 0, "Add a new event class to a stream class after writing an event");
2195 if (ret) {
2196 goto end;
2197 }
2198
2199 /*
2200 * bt_ctf_stream_class_add_event_class() copies the field types
2201 * of event_class, so we retrieve the new ones to create the
2202 * appropriate fields.
2203 */
2204 ep_type = bt_ctf_event_class_get_payload_type(event_class);
2205 assert(ep_type);
2206 ep_field_1_type = bt_ctf_field_type_structure_get_field_type_by_name(
2207 ep_type, "field_1");
2208 assert(ep_field_1_type);
2209 ep_a_string_type = bt_ctf_field_type_structure_get_field_type_by_name(
2210 ep_type, "a_string");
2211 assert(ep_a_string_type);
2212
2213 event = bt_ctf_event_create(event_class);
2214 ret_field = bt_ctf_event_get_payload_by_index(event, 0);
2215 ret_field_type = bt_ctf_field_get_type(ret_field);
2216 ok(bt_ctf_field_type_compare(ret_field_type, integer_type) == 0,
2217 "bt_ctf_event_get_payload_by_index returns a correct field");
2218 bt_put(ret_field_type);
2219 bt_put(ret_field);
2220
2221 ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL,
2222 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2223 ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL,
2224 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2225 bt_put(event);
2226
2227 ok(bt_ctf_stream_get_event_context(NULL) == NULL,
2228 "bt_ctf_stream_get_event_context handles NULL correctly");
2229 event_context = bt_ctf_stream_get_event_context(stream);
2230 ok(event_context,
2231 "bt_ctf_stream_get_event_context returns a stream event context");
2232 ok(bt_ctf_stream_set_event_context(NULL, event_context) < 0,
2233 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
2234 ok(bt_ctf_stream_set_event_context(stream, NULL) < 0,
2235 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
2236 ok(!bt_ctf_stream_set_event_context(stream, event_context),
2237 "bt_ctf_stream_set_event_context correctly set a stream event context");
2238 ret_field = bt_ctf_field_create(ep_field_1_type);
2239 ok(bt_ctf_stream_set_event_context(stream, ret_field) < 0,
2240 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
2241 bt_put(ret_field);
2242
2243 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
2244 event = bt_ctf_event_create(event_class);
2245 struct bt_ctf_field *integer =
2246 bt_ctf_field_create(ep_field_1_type);
2247 struct bt_ctf_field *string =
2248 bt_ctf_field_create(ep_a_string_type);
2249
2250 ret |= bt_ctf_clock_set_time(clock, ++current_time);
2251 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
2252 ret |= bt_ctf_event_set_payload(event, "field_1",
2253 integer);
2254 bt_put(integer);
2255 ret |= bt_ctf_field_string_set_value(string, "This is a test");
2256 ret |= bt_ctf_event_set_payload(event, "a_string",
2257 string);
2258 bt_put(string);
2259
2260 /* Populate stream event context */
2261 integer = bt_ctf_field_structure_get_field(event_context,
2262 "common_event_context");
2263 ret |= bt_ctf_field_unsigned_integer_set_value(integer,
2264 i % 42);
2265 bt_put(integer);
2266
2267 ret |= bt_ctf_stream_append_event(stream, event);
2268 bt_put(event);
2269
2270 if (ret) {
2271 break;
2272 }
2273 }
2274
2275 events_appended = !!(i == PACKET_RESIZE_TEST_LENGTH);
2276 ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0,
2277 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2278 ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0,
2279 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2280 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2281 ok(ret == 0 && ret_uint64 == 0,
2282 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2283 bt_ctf_stream_append_discarded_events(stream, 1000);
2284 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2285 ok(ret == 0 && ret_uint64 == 1000,
2286 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2287
2288 end:
2289 ok(events_appended, "Append 100 000 events to a stream");
2290
2291 /*
2292 * Populate the custom packet context field with a dummy value
2293 * otherwise flush will fail.
2294 */
2295 packet_context = bt_ctf_stream_get_packet_context(stream);
2296 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
2297 "custom_packet_context_field");
2298 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2);
2299
2300 ok(bt_ctf_stream_flush(stream) == 0,
2301 "Flush a stream that forces a packet resize");
2302 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2303 ok(ret == 0 && ret_uint64 == 1000,
2304 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2305 bt_put(integer_type);
2306 bt_put(string_type);
2307 bt_put(packet_context);
2308 bt_put(packet_context_field);
2309 bt_put(event_context);
2310 bt_put(event_class);
2311 bt_put(ep_field_1_type);
2312 bt_put(ep_a_string_type);
2313 bt_put(ep_type);
2314 }
2315
2316 void test_empty_stream(struct bt_ctf_writer *writer)
2317 {
2318 int ret = 0;
2319 struct bt_ctf_trace *trace = NULL, *ret_trace = NULL;
2320 struct bt_ctf_stream_class *stream_class = NULL;
2321 struct bt_ctf_stream *stream = NULL;
2322
2323 trace = bt_ctf_writer_get_trace(writer);
2324 if (!trace) {
2325 diag("Failed to get trace from writer");
2326 ret = -1;
2327 goto end;
2328 }
2329
2330 stream_class = bt_ctf_stream_class_create("empty_stream");
2331 if (!stream_class) {
2332 diag("Failed to create stream class");
2333 ret = -1;
2334 goto end;
2335 }
2336
2337 ok(bt_ctf_stream_class_get_trace(NULL) == NULL,
2338 "bt_ctf_stream_class_get_trace handles NULL correctly");
2339 ok(bt_ctf_stream_class_get_trace(stream_class) == NULL,
2340 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2341
2342 stream = bt_ctf_writer_create_stream(writer, stream_class);
2343 if (!stream) {
2344 diag("Failed to create writer stream");
2345 ret = -1;
2346 goto end;
2347 }
2348
2349 ret_trace = bt_ctf_stream_class_get_trace(stream_class);
2350 ok(ret_trace == trace,
2351 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2352 end:
2353 ok(ret == 0,
2354 "Created a stream class with default attributes and an empty stream");
2355 bt_put(trace);
2356 bt_put(ret_trace);
2357 bt_put(stream);
2358 bt_put(stream_class);
2359 }
2360
2361 void test_custom_event_header_stream(struct bt_ctf_writer *writer)
2362 {
2363 int i, ret;
2364 struct bt_ctf_trace *trace = NULL;
2365 struct bt_ctf_clock *clock = NULL;
2366 struct bt_ctf_stream_class *stream_class = NULL;
2367 struct bt_ctf_stream *stream = NULL;
2368 struct bt_ctf_field_type *integer_type = NULL,
2369 *sequence_type = NULL, *event_header_type = NULL;
2370 struct bt_ctf_field *integer = NULL, *sequence = NULL,
2371 *event_header = NULL, *packet_header = NULL;
2372 struct bt_ctf_event_class *event_class = NULL;
2373 struct bt_ctf_event *event = NULL;
2374
2375 trace = bt_ctf_writer_get_trace(writer);
2376 if (!trace) {
2377 fail("Failed to get trace from writer");
2378 goto end;
2379 }
2380
2381 clock = bt_ctf_trace_get_clock(trace, 0);
2382 if (!clock) {
2383 fail("Failed to get clock from trace");
2384 goto end;
2385 }
2386
2387 stream_class = bt_ctf_stream_class_create("custom_event_header_stream");
2388 if (!stream_class) {
2389 fail("Failed to create stream class");
2390 goto end;
2391 }
2392
2393 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2394 if (ret) {
2395 fail("Failed to set stream class clock");
2396 goto end;
2397 }
2398
2399 /*
2400 * Customize event header to add an "seq_len" integer member
2401 * which will be used as the length of a sequence in an event of this
2402 * stream.
2403 */
2404 event_header_type = bt_ctf_stream_class_get_event_header_type(
2405 stream_class);
2406 if (!event_header_type) {
2407 fail("Failed to get event header type");
2408 goto end;
2409 }
2410
2411 integer_type = bt_ctf_field_type_integer_create(13);
2412 if (!integer_type) {
2413 fail("Failed to create length integer type");
2414 goto end;
2415 }
2416
2417 ret = bt_ctf_field_type_structure_add_field(event_header_type,
2418 integer_type, "seq_len");
2419 if (ret) {
2420 fail("Failed to add a new field to stream event header");
2421 goto end;
2422 }
2423
2424 event_class = bt_ctf_event_class_create("sequence_event");
2425 if (!event_class) {
2426 fail("Failed to create event class");
2427 goto end;
2428 }
2429
2430 /*
2431 * This event's payload will contain a sequence which references
2432 * stream.event.header.seq_len as its length field.
2433 */
2434 sequence_type = bt_ctf_field_type_sequence_create(integer_type,
2435 "stream.event.header.seq_len");
2436 if (!sequence_type) {
2437 fail("Failed to create a sequence");
2438 goto end;
2439 }
2440
2441 ret = bt_ctf_event_class_add_field(event_class, sequence_type,
2442 "some_sequence");
2443 if (ret) {
2444 fail("Failed to add a sequence to an event class");
2445 goto end;
2446 }
2447
2448 ret = bt_ctf_stream_class_add_event_class(stream_class, event_class);
2449 if (ret) {
2450 fail("Failed to add event class to stream class");
2451 goto end;
2452 }
2453
2454 stream = bt_ctf_writer_create_stream(writer, stream_class);
2455 if (!stream) {
2456 fail("Failed to create stream")
2457 goto end;
2458 }
2459
2460 /*
2461 * We have defined a custom packet header field. We have to populate it
2462 * explicitly.
2463 */
2464 packet_header = bt_ctf_stream_get_packet_header(stream);
2465 if (!packet_header) {
2466 fail("Failed to get stream packet header");
2467 goto end;
2468 }
2469
2470 integer = bt_ctf_field_structure_get_field(packet_header,
2471 "custom_trace_packet_header_field");
2472 if (!integer) {
2473 fail("Failed to retrieve custom_trace_packet_header_field");
2474 goto end;
2475 }
2476
2477 ret = bt_ctf_field_unsigned_integer_set_value(integer, 3487);
2478 if (ret) {
2479 fail("Failed to set custom_trace_packet_header_field value");
2480 goto end;
2481 }
2482 bt_put(integer);
2483
2484 event = bt_ctf_event_create(event_class);
2485 if (!event) {
2486 fail("Failed to create event");
2487 goto end;
2488 }
2489
2490 event_header = bt_ctf_event_get_header(event);
2491 if (!event_header) {
2492 fail("Failed to get event header");
2493 goto end;
2494 }
2495
2496 integer = bt_ctf_field_structure_get_field(event_header,
2497 "seq_len");
2498 if (!integer) {
2499 fail("Failed to get seq_len field from event header");
2500 goto end;
2501 }
2502
2503 ret = bt_ctf_field_unsigned_integer_set_value(integer, 2);
2504 if (ret) {
2505 fail("Failed to set seq_len value in event header");
2506 goto end;
2507 }
2508
2509 /* Populate both sequence integer fields */
2510 sequence = bt_ctf_event_get_payload(event, "some_sequence");
2511 if (!sequence) {
2512 fail("Failed to retrieve sequence from event");
2513 goto end;
2514 }
2515
2516 ret = bt_ctf_field_sequence_set_length(sequence, integer);
2517 if (ret) {
2518 fail("Failed to set sequence length");
2519 goto end;
2520 }
2521 bt_put(integer);
2522
2523 for (i = 0; i < 2; i++) {
2524 integer = bt_ctf_field_sequence_get_field(sequence, i);
2525 if (ret) {
2526 fail("Failed to retrieve sequence element");
2527 goto end;
2528 }
2529
2530 ret = bt_ctf_field_unsigned_integer_set_value(integer, i);
2531 if (ret) {
2532 fail("Failed to set sequence element value");
2533 goto end;
2534 }
2535
2536 bt_put(integer);
2537 integer = NULL;
2538 }
2539
2540 ret = bt_ctf_stream_append_event(stream, event);
2541 if (ret) {
2542 fail("Failed to append event to stream");
2543 goto end;
2544 }
2545
2546 ret = bt_ctf_stream_flush(stream);
2547 if (ret) {
2548 fail("Failed to flush custom_event_header stream");
2549 }
2550 end:
2551 bt_put(clock);
2552 bt_put(trace);
2553 bt_put(stream);
2554 bt_put(stream_class);
2555 bt_put(event_class);
2556 bt_put(event);
2557 bt_put(integer);
2558 bt_put(sequence);
2559 bt_put(event_header);
2560 bt_put(packet_header);
2561 bt_put(sequence_type);
2562 bt_put(integer_type);
2563 bt_put(event_header_type);
2564 }
2565
2566 void test_instanciate_event_before_stream(struct bt_ctf_writer *writer)
2567 {
2568 int ret = 0;
2569 struct bt_ctf_trace *trace = NULL;
2570 struct bt_ctf_clock *clock = NULL;
2571 struct bt_ctf_stream_class *stream_class = NULL;
2572 struct bt_ctf_stream *stream = NULL,
2573 *ret_stream = NULL;
2574 struct bt_ctf_event_class *event_class = NULL;
2575 struct bt_ctf_event *event = NULL;
2576 struct bt_ctf_field_type *integer_type = NULL;
2577 struct bt_ctf_field *integer = NULL;
2578
2579 trace = bt_ctf_writer_get_trace(writer);
2580 if (!trace) {
2581 diag("Failed to get trace from writer");
2582 ret = -1;
2583 goto end;
2584 }
2585
2586 clock = bt_ctf_trace_get_clock(trace, 0);
2587 if (!clock) {
2588 diag("Failed to get clock from trace");
2589 ret = -1;
2590 goto end;
2591 }
2592
2593 stream_class = bt_ctf_stream_class_create("event_before_stream_test");
2594 if (!stream_class) {
2595 diag("Failed to create stream class");
2596 ret = -1;
2597 goto end;
2598 }
2599
2600 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2601 if (ret) {
2602 diag("Failed to set stream class clock");
2603 goto end;
2604 }
2605
2606 event_class = bt_ctf_event_class_create("some_event_class_name");
2607 integer_type = bt_ctf_field_type_integer_create(32);
2608 if (!integer_type) {
2609 diag("Failed to create integer field type");
2610 ret = -1;
2611 goto end;
2612 }
2613
2614 ret = bt_ctf_event_class_add_field(event_class, integer_type,
2615 "integer_field");
2616 if (ret) {
2617 diag("Failed to add field to event class");
2618 goto end;
2619 }
2620
2621 ret = bt_ctf_stream_class_add_event_class(stream_class,
2622 event_class);
2623 if (ret) {
2624 diag("Failed to add event class to stream class");
2625 }
2626
2627 event = bt_ctf_event_create(event_class);
2628 if (!event) {
2629 diag("Failed to create event");
2630 ret = -1;
2631 goto end;
2632 }
2633
2634 integer = bt_ctf_event_get_payload_by_index(event, 0);
2635 if (!integer) {
2636 diag("Failed to get integer field payload from event");
2637 ret = -1;
2638 goto end;
2639 }
2640
2641 ret = bt_ctf_field_unsigned_integer_set_value(integer, 1234);
2642 if (ret) {
2643 diag("Failed to set integer field value");
2644 goto end;
2645 }
2646
2647 stream = bt_ctf_writer_create_stream(writer, stream_class);
2648 if (!stream) {
2649 diag("Failed to create writer stream");
2650 ret = -1;
2651 goto end;
2652 }
2653
2654 ok(bt_ctf_event_get_stream(NULL) == NULL,
2655 "bt_ctf_event_get_stream handles NULL correctly");
2656 ok(bt_ctf_event_get_stream(event) == NULL,
2657 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2658
2659 ret = bt_ctf_stream_append_event(stream, event);
2660 if (ret) {
2661 diag("Failed to append event to stream");
2662 goto end;
2663 }
2664
2665 ret_stream = bt_ctf_event_get_stream(event);
2666 ok(ret_stream == stream,
2667 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2668 end:
2669 ok(ret == 0,
2670 "Create an event before instanciating its associated stream");
2671 bt_put(trace);
2672 bt_put(stream);
2673 bt_put(ret_stream);
2674 bt_put(stream_class);
2675 bt_put(event_class);
2676 bt_put(event);
2677 bt_put(integer_type);
2678 bt_put(integer);
2679 bt_put(clock);
2680 }
2681
2682 void append_existing_event_class(struct bt_ctf_stream_class *stream_class)
2683 {
2684 struct bt_ctf_event_class *event_class;
2685
2686 event_class = bt_ctf_event_class_create("Simple Event");
2687 assert(event_class);
2688 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2689 "two event classes with the same name cannot cohabit within the same stream class");
2690 bt_put(event_class);
2691
2692 event_class = bt_ctf_event_class_create("different name, ok");
2693 assert(event_class);
2694 assert(!bt_ctf_event_class_set_id(event_class, 11));
2695 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2696 "two event classes with the same ID cannot cohabit within the same stream class");
2697 bt_put(event_class);
2698 }
2699
2700 int main(int argc, char **argv)
2701 {
2702 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
2703 char metadata_path[sizeof(trace_path) + 9];
2704 const char *clock_name = "test_clock";
2705 const char *clock_description = "This is a test clock";
2706 const char *returned_clock_name;
2707 const char *returned_clock_description;
2708 const uint64_t frequency = 1123456789;
2709 const int64_t offset_s = 1351530929945824323;
2710 const int64_t offset = 1234567;
2711 int64_t get_offset_s,
2712 get_offset,
2713 get_time;
2714 const uint64_t precision = 10;
2715 const int is_absolute = 0xFF;
2716 char *metadata_string;
2717 struct bt_ctf_writer *writer;
2718 struct utsname name;
2719 char hostname[BABELTRACE_HOST_NAME_MAX];
2720 struct bt_ctf_clock *clock, *ret_clock;
2721 struct bt_ctf_stream_class *stream_class, *ret_stream_class;
2722 struct bt_ctf_stream *stream1;
2723 const char *ret_string;
2724 const unsigned char *ret_uuid;
2725 unsigned char tmp_uuid[16] = { 0 };
2726 struct bt_ctf_field_type *packet_context_type,
2727 *packet_context_field_type,
2728 *packet_header_type,
2729 *packet_header_field_type,
2730 *integer_type,
2731 *stream_event_context_type,
2732 *ret_field_type,
2733 *event_header_field_type;
2734 struct bt_ctf_field *packet_header, *packet_header_field;
2735 struct bt_ctf_trace *trace;
2736 int ret;
2737 int64_t ret_int64_t;
2738 struct bt_value *obj;
2739
2740 if (argc < 3) {
2741 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2742 return -1;
2743 }
2744
2745 plan_tests(NR_TESTS);
2746
2747 if (!bt_mkdtemp(trace_path)) {
2748 perror("# perror");
2749 }
2750
2751 strcpy(metadata_path, trace_path);
2752 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
2753
2754 writer = bt_ctf_writer_create(trace_path);
2755 ok(writer, "bt_ctf_create succeeds in creating trace with path");
2756
2757 ok(!bt_ctf_writer_get_trace(NULL),
2758 "bt_ctf_writer_get_trace correctly handles NULL");
2759 trace = bt_ctf_writer_get_trace(writer);
2760 ok(trace,
2761 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2762 ok(bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
2763 "Set a trace's byte order to big endian");
2764 ok(bt_ctf_trace_get_byte_order(trace) == BT_CTF_BYTE_ORDER_BIG_ENDIAN,
2765 "bt_ctf_trace_get_byte_order returns a correct endianness");
2766
2767 /* Add environment context to the trace */
2768 ret = gethostname(hostname, sizeof(hostname));
2769 if (ret < 0) {
2770 return ret;
2771 }
2772 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
2773 "Add host (%s) environment field to writer instance",
2774 hostname);
2775 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
2776 "test_value"),
2777 "bt_ctf_writer_add_environment_field error with NULL writer");
2778 ok(bt_ctf_writer_add_environment_field(writer, NULL,
2779 "test_value"),
2780 "bt_ctf_writer_add_environment_field error with NULL field name");
2781 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
2782 NULL),
2783 "bt_ctf_writer_add_environment_field error with NULL field value");
2784
2785 /* Test bt_ctf_trace_set_environment_field with an integer object */
2786 obj = bt_value_integer_create_init(23);
2787 assert(obj);
2788 ok(bt_ctf_trace_set_environment_field(NULL, "test_env_int_obj", obj),
2789 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2790 ok(bt_ctf_trace_set_environment_field(trace, NULL, obj),
2791 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2792 ok(bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", NULL),
2793 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2794 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", obj),
2795 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2796 BT_PUT(obj);
2797
2798 /* Test bt_ctf_trace_set_environment_field with a string object */
2799 obj = bt_value_string_create_init("the value");
2800 assert(obj);
2801 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_str_obj", obj),
2802 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2803 BT_PUT(obj);
2804
2805 /* Test bt_ctf_trace_set_environment_field_integer */
2806 ok(bt_ctf_trace_set_environment_field_integer(NULL, "test_env_int",
2807 -194875),
2808 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2809 ok(bt_ctf_trace_set_environment_field_integer(trace, NULL, -194875),
2810 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2811 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
2812 -164973),
2813 "bt_ctf_trace_set_environment_field_integer succeeds");
2814
2815 /* Test bt_ctf_trace_set_environment_field_string */
2816 ok(bt_ctf_trace_set_environment_field_string(NULL, "test_env_str",
2817 "yeah"),
2818 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2819 ok(bt_ctf_trace_set_environment_field_string(trace, NULL, "yeah"),
2820 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2821 ok(bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
2822 NULL),
2823 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2824 ok(!bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
2825 "oh yeah"),
2826 "bt_ctf_trace_set_environment_field_string succeeds");
2827
2828 /* Test bt_ctf_trace_get_environment_field_count */
2829 ok(bt_ctf_trace_get_environment_field_count(NULL) < 0,
2830 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2831 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
2832 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2833
2834 /* Test bt_ctf_trace_get_environment_field_name */
2835 ok(bt_ctf_trace_get_environment_field_name(NULL, 0) == NULL,
2836 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2837 ok(bt_ctf_trace_get_environment_field_name(trace, -1) == NULL,
2838 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2839 ok(bt_ctf_trace_get_environment_field_name(trace, 5) == NULL,
2840 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2841 ret_string = bt_ctf_trace_get_environment_field_name(trace, 0);
2842 ok(ret_string && !strcmp(ret_string, "host"),
2843 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2844 ret_string = bt_ctf_trace_get_environment_field_name(trace, 1);
2845 ok(ret_string && !strcmp(ret_string, "test_env_int_obj"),
2846 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2847 ret_string = bt_ctf_trace_get_environment_field_name(trace, 2);
2848 ok(ret_string && !strcmp(ret_string, "test_env_str_obj"),
2849 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2850 ret_string = bt_ctf_trace_get_environment_field_name(trace, 3);
2851 ok(ret_string && !strcmp(ret_string, "test_env_int"),
2852 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2853 ret_string = bt_ctf_trace_get_environment_field_name(trace, 4);
2854 ok(ret_string && !strcmp(ret_string, "test_env_str"),
2855 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2856
2857 /* Test bt_ctf_trace_get_environment_field_value */
2858 ok(bt_ctf_trace_get_environment_field_value(NULL, 0) == NULL,
2859 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2860 ok(bt_ctf_trace_get_environment_field_value(trace, -1) == NULL,
2861 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2862 ok(bt_ctf_trace_get_environment_field_value(trace, 5) == NULL,
2863 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2864 obj = bt_ctf_trace_get_environment_field_value(trace, 1);
2865 ret = bt_value_integer_get(obj, &ret_int64_t);
2866 ok(!ret && ret_int64_t == 23,
2867 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2868 BT_PUT(obj);
2869 obj = bt_ctf_trace_get_environment_field_value(trace, 2);
2870 ret = bt_value_string_get(obj, &ret_string);
2871 ok(!ret && ret_string && !strcmp(ret_string, "the value"),
2872 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2873 BT_PUT(obj);
2874
2875 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2876 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL,
2877 "test_env_str"),
2878 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
2879 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, NULL),
2880 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
2881 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, "oh oh"),
2882 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
2883 obj = bt_ctf_trace_get_environment_field_value_by_name(trace,
2884 "test_env_str");
2885 ret = bt_value_string_get(obj, &ret_string);
2886 ok(!ret && ret_string && !strcmp(ret_string, "oh yeah"),
2887 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
2888 BT_PUT(obj);
2889
2890 /* Test environment field replacement */
2891 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
2892 654321),
2893 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
2894 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
2895 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
2896 obj = bt_ctf_trace_get_environment_field_value(trace, 3);
2897 ret = bt_value_integer_get(obj, &ret_int64_t);
2898 ok(!ret && ret_int64_t == 654321,
2899 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
2900 BT_PUT(obj);
2901
2902 /* On Solaris, uname() can return any positive value on success */
2903 if (uname(&name) < 0) {
2904 perror("uname");
2905 return -1;
2906 }
2907
2908 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
2909 == 0, "Add sysname (%s) environment field to writer instance",
2910 name.sysname);
2911 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
2912 name.nodename) == 0,
2913 "Add nodename (%s) environment field to writer instance",
2914 name.nodename);
2915 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
2916 == 0, "Add release (%s) environment field to writer instance",
2917 name.release);
2918 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
2919 == 0, "Add version (%s) environment field to writer instance",
2920 name.version);
2921 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
2922 == 0, "Add machine (%s) environment field to writer istance",
2923 name.machine);
2924
2925 /* Define a clock and add it to the trace */
2926 ok(bt_ctf_clock_create("signed") == NULL,
2927 "Illegal clock name rejected");
2928 ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
2929 clock = bt_ctf_clock_create(clock_name);
2930 ok(clock, "Clock created sucessfully");
2931 returned_clock_name = bt_ctf_clock_get_name(clock);
2932 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
2933 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
2934 "Returned clock name is valid");
2935
2936 returned_clock_description = bt_ctf_clock_get_description(clock);
2937 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
2938 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
2939 "Clock description set successfully");
2940
2941 returned_clock_description = bt_ctf_clock_get_description(clock);
2942 ok(returned_clock_description,
2943 "bt_ctf_clock_get_description returns a description.");
2944 ok(returned_clock_description ?
2945 !strcmp(returned_clock_description, clock_description) : 0,
2946 "Returned clock description is valid");
2947
2948 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
2949 "bt_ctf_clock_get_frequency returns the correct default frequency");
2950 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
2951 "Set clock frequency");
2952 ok(bt_ctf_clock_get_frequency(clock) == frequency,
2953 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
2954
2955 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
2956 "bt_ctf_clock_get_offset_s succeeds");
2957 ok(get_offset_s == DEFAULT_CLOCK_OFFSET_S,
2958 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
2959 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
2960 "Set clock offset (seconds)");
2961 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
2962 "bt_ctf_clock_get_offset_s succeeds");
2963 ok(get_offset_s == offset_s,
2964 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
2965
2966 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
2967 "bt_ctf_clock_get_offset succeeds");
2968 ok(get_offset == DEFAULT_CLOCK_OFFSET,
2969 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
2970 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
2971 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
2972 "bt_ctf_clock_get_offset succeeds");
2973 ok(get_offset == offset,
2974 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
2975
2976 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
2977 "bt_ctf_clock_get_precision returns the correct default precision");
2978 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
2979 "Set clock precision");
2980 ok(bt_ctf_clock_get_precision(clock) == precision,
2981 "bt_ctf_clock_get_precision returns the correct precision once it is set");
2982
2983 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
2984 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
2985 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
2986 "Set clock absolute property");
2987 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
2988 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
2989
2990 ok(bt_ctf_clock_get_time(clock, &get_time) == 0,
2991 "bt_ctf_clock_get_time succeeds");
2992 ok(get_time == DEFAULT_CLOCK_TIME,
2993 "bt_ctf_clock_get_time returns the correct default time");
2994 ok(bt_ctf_clock_get_value(clock) == DEFAULT_CLOCK_VALUE,
2995 "bt_ctf_clock_get_value returns the correct default value");
2996 ok(bt_ctf_clock_set_value(clock, current_time) == 0,
2997 "Set clock value");
2998 ok(bt_ctf_clock_get_value(clock) == current_time,
2999 "bt_ctf_clock_get_value returns the correct value once it is set");
3000 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
3001 "Set clock time");
3002 ok(bt_ctf_clock_get_time(clock, &get_time) == 0,
3003 "bt_ctf_clock_get_time succeeds");
3004 ok(get_time >= current_time - 1 && get_time <= current_time + 1,
3005 "bt_ctf_clock_get_time returns the correct time once it is set");
3006
3007 ok(bt_ctf_writer_add_clock(writer, clock) == 0,
3008 "Add clock to writer instance");
3009 ok(bt_ctf_writer_add_clock(writer, clock),
3010 "Verify a clock can't be added twice to a writer instance");
3011
3012 ok(bt_ctf_trace_get_clock_count(NULL) < 0,
3013 "bt_ctf_trace_get_clock_count correctly handles NULL");
3014 ok(bt_ctf_trace_get_clock_count(trace) == 1,
3015 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3016 ok(!bt_ctf_trace_get_clock(NULL, 0),
3017 "bt_ctf_trace_get_clock correctly handles NULL");
3018 ok(!bt_ctf_trace_get_clock(trace, -1),
3019 "bt_ctf_trace_get_clock correctly handles negative indexes");
3020 ok(!bt_ctf_trace_get_clock(trace, 1),
3021 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3022 ret_clock = bt_ctf_trace_get_clock(trace, 0);
3023 ok(ret_clock == clock,
3024 "bt_ctf_trace_get_clock returns the right clock instance");
3025 bt_put(ret_clock);
3026 ok(!bt_ctf_trace_get_clock_by_name(trace, NULL),
3027 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3028 ok(!bt_ctf_trace_get_clock_by_name(NULL, clock_name),
3029 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3030 ok(!bt_ctf_trace_get_clock_by_name(NULL, NULL),
3031 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3032 ret_clock = bt_ctf_trace_get_clock_by_name(trace, clock_name);
3033 ok(ret_clock == clock,
3034 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3035 bt_put(ret_clock);
3036 ok(!bt_ctf_trace_get_clock_by_name(trace, "random"),
3037 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3038
3039 ok(!bt_ctf_clock_get_name(NULL),
3040 "bt_ctf_clock_get_name correctly handles NULL");
3041 ok(!bt_ctf_clock_get_description(NULL),
3042 "bt_ctf_clock_get_description correctly handles NULL");
3043 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
3044 "bt_ctf_clock_get_frequency correctly handles NULL");
3045 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
3046 "bt_ctf_clock_get_precision correctly handles NULL");
3047 ok(bt_ctf_clock_get_offset_s(NULL, &get_offset_s) < 0,
3048 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3049 ok(bt_ctf_clock_get_offset_s(clock, NULL) < 0,
3050 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3051 ok(bt_ctf_clock_get_offset(NULL, &get_offset) < 0,
3052 "bt_ctf_clock_get_offset correctly handles NULL clock");
3053 ok(bt_ctf_clock_get_offset(clock, NULL) < 0,
3054 "bt_ctf_clock_get_offset correctly handles NULL output");
3055 ok(bt_ctf_clock_get_is_absolute(NULL) < 0,
3056 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3057 ok(bt_ctf_clock_get_time(NULL, &get_time) < 0,
3058 "bt_ctf_clock_get_time correctly handles NULL clock");
3059 ok(bt_ctf_clock_get_time(clock, NULL) < 0,
3060 "bt_ctf_clock_get_time correctly handles NULL output");
3061
3062 ok(bt_ctf_clock_set_description(NULL, NULL) < 0,
3063 "bt_ctf_clock_set_description correctly handles NULL clock");
3064 ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0,
3065 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3066 ok(bt_ctf_clock_set_precision(NULL, precision) < 0,
3067 "bt_ctf_clock_get_precision correctly handles NULL clock");
3068 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0,
3069 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3070 ok(bt_ctf_clock_set_offset(NULL, offset) < 0,
3071 "bt_ctf_clock_set_offset correctly handles NULL clock");
3072 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0,
3073 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3074 ok(bt_ctf_clock_set_time(NULL, current_time) < 0,
3075 "bt_ctf_clock_set_time correctly handles NULL clock");
3076 ok(bt_ctf_clock_get_uuid(NULL) == NULL,
3077 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3078 ret_uuid = bt_ctf_clock_get_uuid(clock);
3079 ok(ret_uuid,
3080 "bt_ctf_clock_get_uuid returns a UUID");
3081 if (ret_uuid) {
3082 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
3083 /* Slightly modify UUID */
3084 tmp_uuid[sizeof(tmp_uuid) - 1]++;
3085 }
3086
3087 ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0,
3088 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3089 ok(bt_ctf_clock_set_uuid(clock, NULL) < 0,
3090 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3091 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
3092 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
3093 ret_uuid = bt_ctf_clock_get_uuid(clock);
3094 ok(ret_uuid,
3095 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3096 ok(uuid_match(ret_uuid, tmp_uuid),
3097 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3098
3099 /* Define a stream class */
3100 stream_class = bt_ctf_stream_class_create("test_stream");
3101
3102 ok(bt_ctf_stream_class_get_name(NULL) == NULL,
3103 "bt_ctf_stream_class_get_name handles NULL correctly");
3104 ret_string = bt_ctf_stream_class_get_name(stream_class);
3105 ok(ret_string && !strcmp(ret_string, "test_stream"),
3106 "bt_ctf_stream_class_get_name returns a correct stream class name");
3107
3108 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
3109 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3110 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
3111 "bt_ctf_stream_class_get_clock handles NULL correctly");
3112
3113 ok(stream_class, "Create stream class");
3114 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
3115 "Set a stream class' clock");
3116 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
3117 ok(ret_clock == clock,
3118 "bt_ctf_stream_class_get_clock returns a correct clock");
3119 bt_put(ret_clock);
3120
3121 /* Test the event fields and event types APIs */
3122 type_field_tests();
3123
3124 /* Test fields copying */
3125 field_copy_tests();
3126
3127 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
3128 "bt_ctf_stream_class_get_id returns an error when no id is set");
3129 ok(bt_ctf_stream_class_get_id(NULL) < 0,
3130 "bt_ctf_stream_class_get_id handles NULL correctly");
3131 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
3132 "bt_ctf_stream_class_set_id handles NULL correctly");
3133 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
3134 "Set an stream class' id");
3135 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
3136 "bt_ctf_stream_class_get_id returns the correct value");
3137
3138 /* Validate default event header fields */
3139 ok(bt_ctf_stream_class_get_event_header_type(NULL) == NULL,
3140 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3141 ret_field_type = bt_ctf_stream_class_get_event_header_type(
3142 stream_class);
3143 ok(ret_field_type,
3144 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3145 ok(bt_ctf_field_type_get_type_id(ret_field_type) == BT_CTF_TYPE_ID_STRUCT,
3146 "Default event header type is a structure");
3147 event_header_field_type =
3148 bt_ctf_field_type_structure_get_field_type_by_name(
3149 ret_field_type, "id");
3150 ok(event_header_field_type,
3151 "Default event header type contains an \"id\" field");
3152 ok(bt_ctf_field_type_get_type_id(
3153 event_header_field_type) == BT_CTF_TYPE_ID_INTEGER,
3154 "Default event header \"id\" field is an integer");
3155 bt_put(event_header_field_type);
3156 event_header_field_type =
3157 bt_ctf_field_type_structure_get_field_type_by_name(
3158 ret_field_type, "timestamp");
3159 ok(event_header_field_type,
3160 "Default event header type contains a \"timestamp\" field");
3161 ok(bt_ctf_field_type_get_type_id(
3162 event_header_field_type) == BT_CTF_TYPE_ID_INTEGER,
3163 "Default event header \"timestamp\" field is an integer");
3164 bt_put(event_header_field_type);
3165 bt_put(ret_field_type);
3166
3167 /* Add a custom trace packet header field */
3168 ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL,
3169 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3170 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
3171 ok(packet_header_type,
3172 "bt_ctf_trace_get_packet_header_type returns a packet header");
3173 ok(bt_ctf_field_type_get_type_id(packet_header_type) == BT_CTF_TYPE_ID_STRUCT,
3174 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3175 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3176 packet_header_type, "magic");
3177 ok(ret_field_type, "Default packet header type contains a \"magic\" field");
3178 bt_put(ret_field_type);
3179 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3180 packet_header_type, "uuid");
3181 ok(ret_field_type, "Default packet header type contains a \"uuid\" field");
3182 bt_put(ret_field_type);
3183 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3184 packet_header_type, "stream_id");
3185 ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
3186 bt_put(ret_field_type);
3187
3188 packet_header_field_type = bt_ctf_field_type_integer_create(22);
3189 ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
3190 packet_header_field_type, "custom_trace_packet_header_field"),
3191 "Added a custom trace packet header field successfully");
3192
3193 ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0,
3194 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3195 ok(bt_ctf_trace_set_packet_header_type(trace, NULL) < 0,
3196 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3197 ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type),
3198 "Set a trace packet_header_type successfully");
3199
3200 ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL,
3201 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3202
3203 /* Add a custom field to the stream class' packet context */
3204 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
3205 ok(packet_context_type,
3206 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3207 ok(bt_ctf_field_type_get_type_id(packet_context_type) == BT_CTF_TYPE_ID_STRUCT,
3208 "Packet context is a structure");
3209
3210 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
3211 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3212 ok(bt_ctf_stream_class_set_packet_context_type(stream_class, NULL),
3213 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3214
3215 integer_type = bt_ctf_field_type_integer_create(32);
3216 ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
3217 integer_type) < 0,
3218 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3219 /* Create a "uint5_t" equivalent custom packet context field */
3220 packet_context_field_type = bt_ctf_field_type_integer_create(5);
3221
3222 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3223 packet_context_field_type, "custom_packet_context_field");
3224 ok(ret == 0, "Packet context field added successfully");
3225
3226 /* Define a stream event context containing a my_integer field. */
3227 ok(bt_ctf_stream_class_get_event_context_type(NULL) == NULL,
3228 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3229 ok(bt_ctf_stream_class_get_event_context_type(
3230 stream_class) == NULL,
3231 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3232 stream_event_context_type = bt_ctf_field_type_structure_create();
3233 bt_ctf_field_type_structure_add_field(stream_event_context_type,
3234 integer_type, "common_event_context");
3235
3236 ok(bt_ctf_stream_class_set_event_context_type(NULL,
3237 stream_event_context_type) < 0,
3238 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3239 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3240 NULL) < 0,
3241 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3242 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3243 integer_type) < 0,
3244 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3245
3246 ok(bt_ctf_stream_class_set_event_context_type(
3247 stream_class, stream_event_context_type) == 0,
3248 "Set a new stream event context type");
3249 ret_field_type = bt_ctf_stream_class_get_event_context_type(
3250 stream_class);
3251 ok(ret_field_type == stream_event_context_type,
3252 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3253 bt_put(ret_field_type);
3254
3255 /* Instantiate a stream and append events */
3256 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
3257 ok(stream1, "Instanciate a stream class from writer");
3258
3259 ok(bt_ctf_stream_get_class(NULL) == NULL,
3260 "bt_ctf_stream_get_class correctly handles NULL");
3261 ret_stream_class = bt_ctf_stream_get_class(stream1);
3262 ok(ret_stream_class,
3263 "bt_ctf_stream_get_class returns a stream class");
3264 ok(ret_stream_class == stream_class,
3265 "Returned stream class is of the correct type");
3266
3267 /*
3268 * Packet header, packet context, event header, and stream
3269 * event context types were copied for the resolving
3270 * process
3271 */
3272 BT_PUT(packet_header_type);
3273 BT_PUT(packet_context_type);
3274 BT_PUT(stream_event_context_type);
3275 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
3276 assert(packet_header_type);
3277 packet_context_type =
3278 bt_ctf_stream_class_get_packet_context_type(stream_class);
3279 assert(packet_context_type);
3280 stream_event_context_type =
3281 bt_ctf_stream_class_get_event_context_type(stream_class);
3282 assert(stream_event_context_type);
3283
3284 /*
3285 * Try to modify the packet context type after a stream has been
3286 * created.
3287 */
3288 ret = bt_ctf_field_type_structure_add_field(packet_header_type,
3289 packet_header_field_type, "should_fail");
3290 ok(ret < 0,
3291 "Trace packet header type can't be modified once a stream has been instanciated");
3292
3293 /*
3294 * Try to modify the packet context type after a stream has been
3295 * created.
3296 */
3297 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3298 packet_context_field_type, "should_fail");
3299 ok(ret < 0,
3300 "Packet context type can't be modified once a stream has been instanciated");
3301
3302 /*
3303 * Try to modify the stream event context type after a stream has been
3304 * created.
3305 */
3306 ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
3307 integer_type, "should_fail");
3308 ok(ret < 0,
3309 "Stream event context type can't be modified once a stream has been instanciated");
3310
3311 /* Should fail after instanciating a stream (frozen) */
3312 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
3313 "Changes to a stream class that was already instantiated fail");
3314
3315 /* Populate the custom packet header field only once for all tests */
3316 ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
3317 "bt_ctf_stream_get_packet_header handles NULL correctly");
3318 packet_header = bt_ctf_stream_get_packet_header(stream1);
3319 ok(packet_header,
3320 "bt_ctf_stream_get_packet_header returns a packet header");
3321 ret_field_type = bt_ctf_field_get_type(packet_header);
3322 ok(ret_field_type == packet_header_type,
3323 "Stream returns a packet header of the appropriate type");
3324 bt_put(ret_field_type);
3325 packet_header_field = bt_ctf_field_structure_get_field(packet_header,
3326 "custom_trace_packet_header_field");
3327 ok(packet_header_field,
3328 "Packet header structure contains a custom field with the appropriate name");
3329 ret_field_type = bt_ctf_field_get_type(packet_header_field);
3330 ok(bt_ctf_field_type_compare(ret_field_type, packet_header_field_type) == 0,
3331 "Custom packet header field is of the expected type");
3332 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field,
3333 54321), "Set custom packet header value successfully");
3334 ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
3335 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3336 ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
3337 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3338 ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
3339 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3340 ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
3341 "Successfully set a stream's packet header");
3342
3343 ok(bt_ctf_writer_add_environment_field(writer, "new_field", "test") == 0,
3344 "Add environment field to writer after stream creation");
3345
3346 test_instanciate_event_before_stream(writer);
3347
3348 append_simple_event(stream_class, stream1, clock);
3349
3350 packet_resize_test(stream_class, stream1, clock);
3351
3352 append_complex_event(stream_class, stream1, clock);
3353
3354 append_existing_event_class(stream_class);
3355
3356 test_empty_stream(writer);
3357
3358 test_custom_event_header_stream(writer);
3359
3360 metadata_string = bt_ctf_writer_get_metadata_string(writer);
3361 ok(metadata_string, "Get metadata string");
3362
3363 bt_ctf_writer_flush_metadata(writer);
3364 validate_metadata(argv[1], metadata_path);
3365 validate_trace(argv[2], trace_path);
3366
3367 bt_put(clock);
3368 bt_put(ret_stream_class);
3369 bt_put(writer);
3370 bt_put(stream1);
3371 bt_put(packet_context_type);
3372 bt_put(packet_context_field_type);
3373 bt_put(integer_type);
3374 bt_put(stream_event_context_type);
3375 bt_put(ret_field_type);
3376 bt_put(packet_header_type);
3377 bt_put(packet_header_field_type);
3378 bt_put(packet_header);
3379 bt_put(packet_header_field);
3380 bt_put(trace);
3381 free(metadata_string);
3382 bt_put(stream_class);
3383
3384 /* Remove all trace files and delete temporary trace directory */
3385 DIR *trace_dir = opendir(trace_path);
3386 if (!trace_dir) {
3387 perror("# opendir");
3388 return -1;
3389 }
3390
3391 struct dirent *entry;
3392 while ((entry = readdir(trace_dir))) {
3393 struct stat st;
3394 char filename[PATH_MAX];
3395
3396 if (snprintf(filename, sizeof(filename), "%s/%s",
3397 trace_path, entry->d_name) <= 0) {
3398 continue;
3399 }
3400
3401 if (stat(entry->d_name, &st)) {
3402 continue;
3403 }
3404
3405 if (S_ISREG(st.st_mode)) {
3406 unlinkat(bt_dirfd(trace_dir), entry->d_name, 0);
3407 }
3408 }
3409
3410 rmdir(trace_path);
3411 closedir(trace_dir);
3412 return 0;
3413 }
This page took 0.238855 seconds and 5 git commands to generate.