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