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