Tests: unchecked return values 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 609
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 bt_ctf_field_type_structure_add_field(inner_structure_type,
797 uint_35_type, "seq_len");
798 bt_ctf_field_type_structure_add_field(inner_structure_type,
799 sequence_type, "a_sequence");
800 bt_ctf_field_type_structure_add_field(inner_structure_type,
801 array_type, "an_array");
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 bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
832 "INT16_TYPE");
833 bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
834 "UINT35_TYPE");
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 bt_ctf_field_type_structure_add_field(complex_structure_type,
883 enum_variant_type, "variant_selector");
884 bt_ctf_field_type_structure_add_field(complex_structure_type,
885 string_type, "a_string");
886 bt_ctf_field_type_structure_add_field(complex_structure_type,
887 variant_type, "variant_value");
888 bt_ctf_field_type_structure_add_field(complex_structure_type,
889 inner_structure_type, "inner_structure");
890
891 ok(bt_ctf_event_class_create("clock") == NULL,
892 "Reject creation of an event class with an illegal name");
893 event_class = bt_ctf_event_class_create(complex_test_event_string);
894 ok(event_class, "Create an event class");
895 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
896 "Reject addition of a field with an empty name to an event");
897 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
898 "Reject addition of a field with a NULL type to an event");
899 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
900 "int"),
901 "Reject addition of a type with an illegal name to an event");
902 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
903 "uint_35") == 0,
904 "Add field of type unsigned integer to an event");
905 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
906 "int_16") == 0, "Add field of type signed integer to an event");
907 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
908 "complex_structure") == 0,
909 "Add composite structure to an event");
910
911 ok(bt_ctf_event_class_get_name(NULL) == NULL,
912 "bt_ctf_event_class_get_name handles NULL correctly");
913 ret_string = bt_ctf_event_class_get_name(event_class);
914 ok(!strcmp(ret_string, complex_test_event_string),
915 "bt_ctf_event_class_get_name returns a correct name");
916 ok(bt_ctf_event_class_get_id(event_class) < 0,
917 "bt_ctf_event_class_get_id returns a negative value when not set");
918 ok(bt_ctf_event_class_get_id(NULL) < 0,
919 "bt_ctf_event_class_get_id handles NULL correctly");
920 ok(bt_ctf_event_class_set_id(NULL, 42) < 0,
921 "bt_ctf_event_class_set_id handles NULL correctly");
922 ok(bt_ctf_event_class_set_id(event_class, 42) == 0,
923 "Set an event class' id");
924 ok(bt_ctf_event_class_get_id(event_class) == 42,
925 "bt_ctf_event_class_get_id returns the correct value");
926
927 /* Test event class attributes */
928 obj = bt_value_integer_create_init(15);
929 assert(obj);
930 ok(bt_ctf_event_class_set_attribute(NULL, "id", obj),
931 "bt_ctf_event_class_set_attribute handles a NULL event class correctly");
932 ok(bt_ctf_event_class_set_attribute(event_class, NULL, obj),
933 "bt_ctf_event_class_set_attribute handles a NULL name correctly");
934 ok(bt_ctf_event_class_set_attribute(event_class, "id", NULL),
935 "bt_ctf_event_class_set_attribute handles a NULL value correctly");
936 assert(!bt_value_integer_set(obj, -3));
937 ok(bt_ctf_event_class_set_attribute(event_class, "id", obj),
938 "bt_ctf_event_class_set_attribute fails with a negative \"id\" attribute");
939 assert(!bt_value_integer_set(obj, 11));
940 ret = bt_ctf_event_class_set_attribute(event_class, "id", obj);
941 ok(!ret && bt_ctf_event_class_get_id(event_class) == 11,
942 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"id\" attribute");
943 ret = bt_ctf_event_class_set_attribute(event_class, "name", obj);
944 ret &= bt_ctf_event_class_set_attribute(event_class, "model.emf.uri", obj);
945 ok(ret,
946 "bt_ctf_event_class_set_attribute cannot set \"name\" or \"model.emf.uri\" to an integer value");
947 BT_PUT(obj);
948
949 obj = bt_value_integer_create_init(5);
950 assert(obj);
951 ok(!bt_ctf_event_class_set_attribute(event_class, "loglevel", obj),
952 "bt_ctf_event_class_set_attribute succeeds in setting the \"loglevel\" attribute");
953 BT_PUT(obj);
954 ok(!bt_ctf_event_class_get_attribute_value_by_name(NULL, "loglevel"),
955 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL event class correctly");
956 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class, NULL),
957 "bt_ctf_event_class_get_attribute_value_by_name handles a NULL name correctly");
958 ok(!bt_ctf_event_class_get_attribute_value_by_name(event_class, "meow"),
959 "bt_ctf_event_class_get_attribute_value_by_name fails with a non-existing attribute name");
960 obj = bt_ctf_event_class_get_attribute_value_by_name(event_class,
961 "loglevel");
962 int64_value = 0;
963 ret = bt_value_integer_get(obj, &int64_value);
964 ok(obj && !ret && int64_value == 5,
965 "bt_ctf_event_class_get_attribute_value_by_name returns the correct value");
966 BT_PUT(obj);
967
968 obj = bt_value_string_create_init("nu name");
969 assert(obj);
970 assert(!bt_ctf_event_class_set_attribute(event_class, "name", obj));
971 ret_string = bt_ctf_event_class_get_name(event_class);
972 ok(!strcmp(ret_string, "nu name"),
973 "bt_ctf_event_class_set_attribute succeeds in replacing the existing \"name\" attribute");
974 ret = bt_ctf_event_class_set_attribute(event_class, "id", obj);
975 ret &= bt_ctf_event_class_set_attribute(event_class, "loglevel", obj);
976 ok(ret,
977 "bt_ctf_event_class_set_attribute cannot set \"id\" or \"loglevel\" to a string value");
978 BT_PUT(obj);
979 obj = bt_value_string_create_init("http://kernel.org/");
980 assert(obj);
981 assert(!bt_ctf_event_class_set_attribute(event_class, "model.emf.uri", obj));
982 BT_PUT(obj);
983
984 ok(bt_ctf_event_class_get_attribute_count(NULL),
985 "bt_ctf_event_class_get_attribute_count handles a NULL event class");
986 ok(bt_ctf_event_class_get_attribute_count(event_class) == 4,
987 "bt_ctf_event_class_get_attribute_count returns the correct count");
988 ok(!bt_ctf_event_class_get_attribute_name(NULL, 0),
989 "bt_ctf_event_class_get_attribute_name handles a NULL event class correctly");
990 ok(!bt_ctf_event_class_get_attribute_name(event_class, 4),
991 "bt_ctf_event_class_get_attribute_name handles a too large index correctly");
992 ok(!bt_ctf_event_class_get_attribute_value(NULL, 0),
993 "bt_ctf_event_class_get_attribute_value handles a NULL event class correctly");
994 ok(!bt_ctf_event_class_get_attribute_value(event_class, 4),
995 "bt_ctf_event_class_get_attribute_value handles a too large index correctly");
996
997 memset(&attrs_count, 0, sizeof(attrs_count));
998
999 for (i = 0; i < 4; ++i) {
1000 ret_string = bt_ctf_event_class_get_attribute_name(event_class,
1001 i);
1002 obj = bt_ctf_event_class_get_attribute_value(event_class, i);
1003 assert(ret_string && obj);
1004
1005 if (!strcmp(ret_string, "id")) {
1006 attrs_count.id++;
1007 ok(bt_value_is_integer(obj),
1008 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1009 ret_string);
1010 } else if (!strcmp(ret_string, "name")) {
1011 attrs_count.name++;
1012 ok(bt_value_is_string(obj),
1013 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1014 ret_string);
1015 } else if (!strcmp(ret_string, "loglevel")) {
1016 attrs_count.loglevel++;
1017 ok(bt_value_is_integer(obj),
1018 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1019 ret_string);
1020 } else if (!strcmp(ret_string, "model.emf.uri")) {
1021 attrs_count.modelemfuri++;
1022 ok(bt_value_is_string(obj),
1023 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
1024 ret_string);
1025 } else {
1026 attrs_count.unknown++;
1027 }
1028
1029 BT_PUT(obj);
1030 }
1031
1032 ok(attrs_count.unknown == 0, "event class has no unknown attributes");
1033 ok(attrs_count.id == 1 && attrs_count.name == 1 &&
1034 attrs_count.loglevel == 1 && attrs_count.modelemfuri == 1,
1035 "event class has one instance of each known attribute");
1036
1037 /* Add event class to the stream class */
1038 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
1039 "Reject addition of NULL event class to a stream class");
1040 ok(bt_ctf_stream_class_add_event_class(stream_class,
1041 event_class) == 0, "Add an event class to stream class");
1042
1043 ok(bt_ctf_event_class_get_stream_class(NULL) == NULL,
1044 "bt_ctf_event_class_get_stream_class handles NULL correctly");
1045 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
1046 ok(ret_stream_class == stream_class,
1047 "bt_ctf_event_class_get_stream_class returns the correct stream class");
1048 bt_put(ret_stream_class);
1049
1050 ok(bt_ctf_event_class_get_field_count(NULL) < 0,
1051 "bt_ctf_event_class_get_field_count handles NULL correctly");
1052 ok(bt_ctf_event_class_get_field_count(event_class) == 3,
1053 "bt_ctf_event_class_get_field_count returns a correct value");
1054
1055 ok(bt_ctf_event_class_get_field(NULL, &ret_string,
1056 &ret_field_type, 0) < 0,
1057 "bt_ctf_event_class_get_field handles a NULL event class correctly");
1058 ok(bt_ctf_event_class_get_field(event_class, NULL,
1059 &ret_field_type, 0) == 0,
1060 "bt_ctf_event_class_get_field handles a NULL field name correctly");
1061 bt_put(ret_field_type);
1062 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1063 NULL, 0) == 0,
1064 "bt_ctf_event_class_get_field handles a NULL field type correctly");
1065 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1066 &ret_field_type, 42) < 0,
1067 "bt_ctf_event_class_get_field handles an invalid index correctly");
1068 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
1069 &ret_field_type, 0) == 0,
1070 "bt_ctf_event_class_get_field returns a field");
1071 ok(bt_ctf_field_type_compare(ret_field_type, uint_35_type) == 0,
1072 "bt_ctf_event_class_get_field returns a correct field type");
1073 bt_put(ret_field_type);
1074 ok(!strcmp(ret_string, "uint_35"),
1075 "bt_ctf_event_class_get_field returns a correct field name");
1076 ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL,
1077 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
1078 ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL,
1079 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
1080 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
1081 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1082 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
1083 "complex_structure");
1084 ok(bt_ctf_field_type_compare(ret_field_type, complex_structure_type) == 0,
1085 "bt_ctf_event_class_get_field_by_name returns a correct field type");
1086 bt_put(ret_field_type);
1087
1088 event = bt_ctf_event_create(event_class);
1089 ok(event, "Instanciate a complex event");
1090
1091 ok(bt_ctf_event_get_class(NULL) == NULL,
1092 "bt_ctf_event_get_class handles NULL correctly");
1093 ret_event_class = bt_ctf_event_get_class(event);
1094 ok(ret_event_class == event_class,
1095 "bt_ctf_event_get_class returns the correct event class");
1096 bt_put(ret_event_class);
1097
1098 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
1099 if (!uint_35_field) {
1100 printf("uint_35_field is NULL\n");
1101 }
1102
1103 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
1104 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
1105 ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0,
1106 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1107 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0,
1108 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1109 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
1110 &ret_unsigned_int) == 0,
1111 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1112 ok(ret_unsigned_int == 0x0DDF00D,
1113 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1114 ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
1115 &ret_signed_int) < 0,
1116 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1117 bt_put(uint_35_field);
1118
1119 int_16_field = bt_ctf_event_get_payload(event, "int_16");
1120 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
1121 ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0,
1122 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1123 ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0,
1124 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1125 ok(bt_ctf_field_signed_integer_get_value(int_16_field,
1126 &ret_signed_int) == 0,
1127 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1128 ok(ret_signed_int == -12345,
1129 "bt_ctf_field_signed_integer_get_value returns the correct value");
1130 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
1131 &ret_unsigned_int) < 0,
1132 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1133 bt_put(int_16_field);
1134
1135 complex_structure_field = bt_ctf_event_get_payload(event,
1136 "complex_structure");
1137
1138 ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
1139 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1140 ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
1141 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1142 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
1143 complex_structure_field, 3);
1144 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
1145 bt_put(inner_structure_field);
1146 ok(bt_ctf_field_type_compare(ret_field_type, inner_structure_type) == 0,
1147 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1148 bt_put(ret_field_type);
1149
1150 inner_structure_field = bt_ctf_field_structure_get_field(
1151 complex_structure_field, "inner_structure");
1152 a_string_field = bt_ctf_field_structure_get_field(
1153 complex_structure_field, "a_string");
1154 enum_variant_field = bt_ctf_field_structure_get_field(
1155 complex_structure_field, "variant_selector");
1156 variant_field = bt_ctf_field_structure_get_field(
1157 complex_structure_field, "variant_value");
1158 uint_35_field = bt_ctf_field_structure_get_field(
1159 inner_structure_field, "seq_len");
1160 a_sequence_field = bt_ctf_field_structure_get_field(
1161 inner_structure_field, "a_sequence");
1162 an_array_field = bt_ctf_field_structure_get_field(
1163 inner_structure_field, "an_array");
1164
1165 enum_container_field = bt_ctf_field_enumeration_get_container(
1166 enum_variant_field);
1167 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
1168 int_16_field = bt_ctf_field_variant_get_field(variant_field,
1169 enum_variant_field);
1170 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
1171 bt_put(int_16_field);
1172 ok(!bt_ctf_field_string_get_value(a_string_field),
1173 "bt_ctf_field_string_get_value returns NULL on an unset field");
1174 bt_ctf_field_string_set_value(a_string_field,
1175 test_string_1);
1176 ok(!bt_ctf_field_string_get_value(NULL),
1177 "bt_ctf_field_string_get_value correctly handles NULL");
1178 ok(bt_ctf_field_string_append(NULL, "yeah"),
1179 "bt_ctf_field_string_append correctly handles a NULL string field");
1180 ok(bt_ctf_field_string_append(a_string_field, NULL),
1181 "bt_ctf_field_string_append correctly handles a NULL string value");
1182 ok(!bt_ctf_field_string_append(a_string_field, test_string_2),
1183 "bt_ctf_field_string_append succeeds");
1184 ok(bt_ctf_field_string_append_len(NULL, "oh noes", 3),
1185 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1186 ok(bt_ctf_field_string_append_len(a_string_field, NULL, 3),
1187 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1188 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 5),
1189 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1190 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_4, 10),
1191 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1192 ok(!bt_ctf_field_string_append_len(a_string_field, &test_string_4[4], 3),
1193 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1194 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 0),
1195 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1196
1197 ret_string = bt_ctf_field_string_get_value(a_string_field);
1198 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
1199 ok(ret_string ? !strcmp(ret_string, test_string_cat) : 0,
1200 "bt_ctf_field_string_get_value returns a correct value");
1201 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
1202 SEQUENCE_TEST_LENGTH);
1203
1204 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL,
1205 enum_container_field) == NULL,
1206 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1207 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type,
1208 NULL) == NULL,
1209 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1210 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
1211 variant_type, enum_variant_field);
1212 ok(ret_field_type == int_16_type,
1213 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1214
1215 ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
1216 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1217 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
1218 uint_35_field) == 0, "Set a sequence field's length");
1219 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
1220 ok(ret_field == uint_35_field,
1221 "bt_ctf_field_sequence_get_length returns the correct length field");
1222 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
1223 "bt_ctf_field_sequence_get_length properly handles NULL");
1224
1225 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
1226 int_16_field = bt_ctf_field_sequence_get_field(
1227 a_sequence_field, i);
1228 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
1229 bt_put(int_16_field);
1230 }
1231
1232 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
1233 int_16_field = bt_ctf_field_array_get_field(
1234 an_array_field, i);
1235 bt_ctf_field_signed_integer_set_value(int_16_field, i);
1236 bt_put(int_16_field);
1237 }
1238
1239 stream_event_ctx_field = bt_ctf_event_get_stream_event_context(event);
1240 assert(stream_event_ctx_field);
1241 stream_event_ctx_int_field = bt_ctf_field_structure_get_field(
1242 stream_event_ctx_field, "common_event_context");
1243 BT_PUT(stream_event_ctx_field);
1244 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field, 17);
1245 BT_PUT(stream_event_ctx_int_field);
1246
1247 bt_ctf_clock_set_time(clock, ++current_time);
1248 ok(bt_ctf_stream_append_event(stream, event) == 0,
1249 "Append a complex event to a stream");
1250
1251 /*
1252 * Populate the custom packet context field with a dummy value
1253 * otherwise flush will fail.
1254 */
1255 packet_context = bt_ctf_stream_get_packet_context(stream);
1256 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
1257 "custom_packet_context_field");
1258 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1);
1259
1260 ok(bt_ctf_stream_flush(stream) == 0,
1261 "Flush a stream containing a complex event");
1262
1263 bt_put(uint_35_field);
1264 bt_put(a_string_field);
1265 bt_put(inner_structure_field);
1266 bt_put(complex_structure_field);
1267 bt_put(a_sequence_field);
1268 bt_put(an_array_field);
1269 bt_put(enum_variant_field);
1270 bt_put(enum_container_field);
1271 bt_put(variant_field);
1272 bt_put(ret_field);
1273 bt_put(packet_context_field);
1274 bt_put(packet_context);
1275 bt_put(uint_35_type);
1276 bt_put(int_16_type);
1277 bt_put(string_type);
1278 bt_put(sequence_type);
1279 bt_put(array_type);
1280 bt_put(inner_structure_type);
1281 bt_put(complex_structure_type);
1282 bt_put(uint_3_type);
1283 bt_put(enum_variant_type);
1284 bt_put(variant_type);
1285 bt_put(ret_field_type);
1286 bt_put(event_class);
1287 bt_put(event);
1288 }
1289
1290 static
1291 void field_copy_tests_validate_same_type(struct bt_ctf_field *field,
1292 struct bt_ctf_field_type *expected_type, const char *name)
1293 {
1294 struct bt_ctf_field_type *copy_type;
1295
1296 copy_type = bt_ctf_field_get_type(field);
1297 ok(copy_type == expected_type,
1298 "bt_ctf_field_copy does not copy the type (%s)", name);
1299 bt_put(copy_type);
1300 }
1301
1302 static
1303 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field *field_a,
1304 struct bt_ctf_field *field_b, const char *name)
1305 {
1306 ok(field_a != field_b,
1307 "bt_ctf_field_copy creates different pointers (%s)", name);
1308 }
1309
1310 static
1311 void field_copy_tests()
1312 {
1313 struct bt_ctf_field_type *len_type = NULL;
1314 struct bt_ctf_field_type *fp_type = NULL;
1315 struct bt_ctf_field_type *s_type = NULL;
1316 struct bt_ctf_field_type *e_int_type = NULL;
1317 struct bt_ctf_field_type *e_type = NULL;
1318 struct bt_ctf_field_type *v_type = NULL;
1319 struct bt_ctf_field_type *v_label1_type = NULL;
1320 struct bt_ctf_field_type *v_label1_array_type = NULL;
1321 struct bt_ctf_field_type *v_label2_type = NULL;
1322 struct bt_ctf_field_type *v_label2_seq_type = NULL;
1323 struct bt_ctf_field_type *strct_type = NULL;
1324 struct bt_ctf_field *len = NULL;
1325 struct bt_ctf_field *fp = NULL;
1326 struct bt_ctf_field *s = NULL;
1327 struct bt_ctf_field *e_int = NULL;
1328 struct bt_ctf_field *e = NULL;
1329 struct bt_ctf_field *v = NULL;
1330 struct bt_ctf_field *v_selected = NULL;
1331 struct bt_ctf_field *v_selected_cur = NULL;
1332 struct bt_ctf_field *v_selected_0 = NULL;
1333 struct bt_ctf_field *v_selected_1 = NULL;
1334 struct bt_ctf_field *v_selected_2 = NULL;
1335 struct bt_ctf_field *v_selected_3 = NULL;
1336 struct bt_ctf_field *v_selected_4 = NULL;
1337 struct bt_ctf_field *v_selected_5 = NULL;
1338 struct bt_ctf_field *v_selected_6 = NULL;
1339 struct bt_ctf_field *a = NULL;
1340 struct bt_ctf_field *a_0 = NULL;
1341 struct bt_ctf_field *a_1 = NULL;
1342 struct bt_ctf_field *a_2 = NULL;
1343 struct bt_ctf_field *a_3 = NULL;
1344 struct bt_ctf_field *a_4 = NULL;
1345 struct bt_ctf_field *strct = NULL;
1346 struct bt_ctf_field *len_copy = NULL;
1347 struct bt_ctf_field *fp_copy = NULL;
1348 struct bt_ctf_field *s_copy = NULL;
1349 struct bt_ctf_field *e_int_copy = NULL;
1350 struct bt_ctf_field *e_copy = NULL;
1351 struct bt_ctf_field *v_copy = NULL;
1352 struct bt_ctf_field *v_selected_copy = NULL;
1353 struct bt_ctf_field *v_selected_copy_len = NULL;
1354 struct bt_ctf_field *v_selected_0_copy = NULL;
1355 struct bt_ctf_field *v_selected_1_copy = NULL;
1356 struct bt_ctf_field *v_selected_2_copy = NULL;
1357 struct bt_ctf_field *v_selected_3_copy = NULL;
1358 struct bt_ctf_field *v_selected_4_copy = NULL;
1359 struct bt_ctf_field *v_selected_5_copy = NULL;
1360 struct bt_ctf_field *v_selected_6_copy = NULL;
1361 struct bt_ctf_field *a_copy = NULL;
1362 struct bt_ctf_field *a_0_copy = NULL;
1363 struct bt_ctf_field *a_1_copy = NULL;
1364 struct bt_ctf_field *a_2_copy = NULL;
1365 struct bt_ctf_field *a_3_copy = NULL;
1366 struct bt_ctf_field *a_4_copy = NULL;
1367 struct bt_ctf_field *strct_copy = NULL;
1368 uint64_t uint64_t_val;
1369 const char *str_val;
1370 double double_val;
1371 int ret;
1372
1373 /* create len type */
1374 len_type = bt_ctf_field_type_integer_create(32);
1375 assert(len_type);
1376
1377 /* create fp type */
1378 fp_type = bt_ctf_field_type_floating_point_create();
1379 assert(fp_type);
1380
1381 /* create s type */
1382 s_type = bt_ctf_field_type_string_create();
1383 assert(s_type);
1384
1385 /* create e_int type */
1386 e_int_type = bt_ctf_field_type_integer_create(8);
1387 assert(e_int_type);
1388
1389 /* create e type */
1390 e_type = bt_ctf_field_type_enumeration_create(e_int_type);
1391 assert(e_type);
1392 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL1",
1393 10, 15);
1394 assert(!ret);
1395 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL2",
1396 23, 23);
1397 assert(!ret);
1398
1399 /* create v_label1 type */
1400 v_label1_type = bt_ctf_field_type_string_create();
1401 assert(v_label1_type);
1402
1403 /* create v_label1_array type */
1404 v_label1_array_type = bt_ctf_field_type_array_create(v_label1_type, 5);
1405 assert(v_label1_array_type);
1406
1407 /* create v_label2 type */
1408 v_label2_type = bt_ctf_field_type_integer_create(16);
1409 assert(v_label2_type);
1410
1411 /* create v_label2_seq type */
1412 v_label2_seq_type = bt_ctf_field_type_sequence_create(v_label2_type,
1413 "len");
1414 assert(v_label2_seq_type);
1415
1416 /* create v type */
1417 v_type = bt_ctf_field_type_variant_create(e_type, "e");
1418 assert(v_type);
1419 ret = bt_ctf_field_type_variant_add_field(v_type, v_label1_array_type,
1420 "LABEL1");
1421 assert(!ret);
1422 ret = bt_ctf_field_type_variant_add_field(v_type, v_label2_seq_type,
1423 "LABEL2");
1424 assert(!ret);
1425
1426 /* create strct type */
1427 strct_type = bt_ctf_field_type_structure_create();
1428 assert(strct_type);
1429 ret = bt_ctf_field_type_structure_add_field(strct_type, len_type,
1430 "len");
1431 assert(!ret);
1432 ret = bt_ctf_field_type_structure_add_field(strct_type, fp_type, "fp");
1433 assert(!ret);
1434 ret = bt_ctf_field_type_structure_add_field(strct_type, s_type, "s");
1435 assert(!ret);
1436 ret = bt_ctf_field_type_structure_add_field(strct_type, e_type, "e");
1437 assert(!ret);
1438 ret = bt_ctf_field_type_structure_add_field(strct_type, v_type, "v");
1439 assert(!ret);
1440 ret = bt_ctf_field_type_structure_add_field(strct_type,
1441 v_label1_array_type, "a");
1442 assert(!ret);
1443
1444 /* create strct */
1445 strct = bt_ctf_field_create(strct_type);
1446 assert(strct);
1447
1448 /* get len field */
1449 len = bt_ctf_field_structure_get_field(strct, "len");
1450 assert(len);
1451
1452 /* get fp field */
1453 fp = bt_ctf_field_structure_get_field(strct, "fp");
1454 assert(fp);
1455
1456 /* get s field */
1457 s = bt_ctf_field_structure_get_field(strct, "s");
1458 assert(s);
1459
1460 /* get e field */
1461 e = bt_ctf_field_structure_get_field(strct, "e");
1462 assert(e);
1463
1464 /* get e_int (underlying integer) */
1465 e_int = bt_ctf_field_enumeration_get_container(e);
1466 assert(e_int);
1467
1468 /* get v field */
1469 v = bt_ctf_field_structure_get_field(strct, "v");
1470 assert(v);
1471
1472 /* get a field */
1473 a = bt_ctf_field_structure_get_field(strct, "a");
1474 assert(a);
1475
1476 /* set len field */
1477 ret = bt_ctf_field_unsigned_integer_set_value(len, 7);
1478 assert(!ret);
1479
1480 /* set fp field */
1481 ret = bt_ctf_field_floating_point_set_value(fp, 3.14);
1482 assert(!ret);
1483
1484 /* set s field */
1485 ret = bt_ctf_field_string_set_value(s, "btbt");
1486 assert(!ret);
1487
1488 /* set e field (LABEL2) */
1489 ret = bt_ctf_field_unsigned_integer_set_value(e_int, 23);
1490 assert(!ret);
1491
1492 /* set v field */
1493 v_selected = bt_ctf_field_variant_get_field(v, e);
1494 assert(v_selected);
1495 ok(!bt_ctf_field_variant_get_current_field(NULL),
1496 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1497 v_selected_cur = bt_ctf_field_variant_get_current_field(v);
1498 ok(v_selected_cur == v_selected,
1499 "bt_ctf_field_variant_get_current_field returns the current field");
1500 bt_put(v_selected_cur);
1501
1502 /* set selected v field */
1503 ret = bt_ctf_field_sequence_set_length(v_selected, len);
1504 assert(!ret);
1505 v_selected_0 = bt_ctf_field_sequence_get_field(v_selected, 0);
1506 assert(v_selected_0);
1507 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_0, 7);
1508 assert(!ret);
1509 v_selected_1 = bt_ctf_field_sequence_get_field(v_selected, 1);
1510 assert(v_selected_1);
1511 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_1, 6);
1512 assert(!ret);
1513 v_selected_2 = bt_ctf_field_sequence_get_field(v_selected, 2);
1514 assert(v_selected_2);
1515 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_2, 5);
1516 assert(!ret);
1517 v_selected_3 = bt_ctf_field_sequence_get_field(v_selected, 3);
1518 assert(v_selected_3);
1519 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_3, 4);
1520 assert(!ret);
1521 v_selected_4 = bt_ctf_field_sequence_get_field(v_selected, 4);
1522 assert(v_selected_4);
1523 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_4, 3);
1524 assert(!ret);
1525 v_selected_5 = bt_ctf_field_sequence_get_field(v_selected, 5);
1526 assert(v_selected_5);
1527 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_5, 2);
1528 assert(!ret);
1529 v_selected_6 = bt_ctf_field_sequence_get_field(v_selected, 6);
1530 assert(v_selected_6);
1531 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_6, 1);
1532 assert(!ret);
1533
1534 /* set a field */
1535 a_0 = bt_ctf_field_array_get_field(a, 0);
1536 assert(a_0);
1537 ret = bt_ctf_field_string_set_value(a_0, "a_0");
1538 assert(!ret);
1539 a_1 = bt_ctf_field_array_get_field(a, 1);
1540 assert(a_1);
1541 ret = bt_ctf_field_string_set_value(a_1, "a_1");
1542 assert(!ret);
1543 a_2 = bt_ctf_field_array_get_field(a, 2);
1544 assert(a_2);
1545 ret = bt_ctf_field_string_set_value(a_2, "a_2");
1546 assert(!ret);
1547 a_3 = bt_ctf_field_array_get_field(a, 3);
1548 assert(a_3);
1549 ret = bt_ctf_field_string_set_value(a_3, "a_3");
1550 assert(!ret);
1551 a_4 = bt_ctf_field_array_get_field(a, 4);
1552 assert(a_4);
1553 ret = bt_ctf_field_string_set_value(a_4, "a_4");
1554 assert(!ret);
1555
1556 /* create copy of strct */
1557 ok(!bt_ctf_field_copy(NULL),
1558 "bt_ctf_field_copy handles NULL correctly");
1559 strct_copy = bt_ctf_field_copy(strct);
1560 ok(strct_copy,
1561 "bt_ctf_field_copy returns a valid pointer");
1562
1563 /* get all copied fields */
1564 len_copy = bt_ctf_field_structure_get_field(strct_copy, "len");
1565 assert(len_copy);
1566 fp_copy = bt_ctf_field_structure_get_field(strct_copy, "fp");
1567 assert(fp_copy);
1568 s_copy = bt_ctf_field_structure_get_field(strct_copy, "s");
1569 assert(s_copy);
1570 e_copy = bt_ctf_field_structure_get_field(strct_copy, "e");
1571 assert(e_copy);
1572 e_int_copy = bt_ctf_field_enumeration_get_container(e_copy);
1573 assert(e_int_copy);
1574 v_copy = bt_ctf_field_structure_get_field(strct_copy, "v");
1575 assert(v_copy);
1576 v_selected_copy = bt_ctf_field_variant_get_field(v_copy, e_copy);
1577 assert(v_selected_copy);
1578 v_selected_0_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 0);
1579 assert(v_selected_0_copy);
1580 v_selected_1_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 1);
1581 assert(v_selected_1_copy);
1582 v_selected_2_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 2);
1583 assert(v_selected_2_copy);
1584 v_selected_3_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 3);
1585 assert(v_selected_3_copy);
1586 v_selected_4_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 4);
1587 assert(v_selected_4_copy);
1588 v_selected_5_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 5);
1589 assert(v_selected_5_copy);
1590 v_selected_6_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 6);
1591 assert(v_selected_6_copy);
1592 ok(!bt_ctf_field_sequence_get_field(v_selected_copy, 7),
1593 "sequence field copy is not too large");
1594 a_copy = bt_ctf_field_structure_get_field(strct_copy, "a");
1595 assert(a_copy);
1596 a_0_copy = bt_ctf_field_array_get_field(a_copy, 0);
1597 assert(a_0_copy);
1598 a_1_copy = bt_ctf_field_array_get_field(a_copy, 1);
1599 assert(a_1_copy);
1600 a_2_copy = bt_ctf_field_array_get_field(a_copy, 2);
1601 assert(a_2_copy);
1602 a_3_copy = bt_ctf_field_array_get_field(a_copy, 3);
1603 assert(a_3_copy);
1604 a_4_copy = bt_ctf_field_array_get_field(a_copy, 4);
1605 assert(a_4_copy);
1606 ok(!bt_ctf_field_array_get_field(v_selected_copy, 5),
1607 "array field copy is not too large");
1608
1609 /* make sure copied fields are different pointers */
1610 field_copy_tests_validate_diff_ptrs(strct_copy, strct, "strct");
1611 field_copy_tests_validate_diff_ptrs(len_copy, len, "len");
1612 field_copy_tests_validate_diff_ptrs(fp_copy, fp, "fp");
1613 field_copy_tests_validate_diff_ptrs(s_copy, s, "s");
1614 field_copy_tests_validate_diff_ptrs(e_int_copy, e_int, "e_int");
1615 field_copy_tests_validate_diff_ptrs(e_copy, e, "e");
1616 field_copy_tests_validate_diff_ptrs(v_copy, v, "v");
1617 field_copy_tests_validate_diff_ptrs(v_selected_copy, v_selected,
1618 "v_selected");
1619 field_copy_tests_validate_diff_ptrs(v_selected_0_copy, v_selected_0,
1620 "v_selected_0");
1621 field_copy_tests_validate_diff_ptrs(v_selected_1_copy, v_selected_1,
1622 "v_selected_1");
1623 field_copy_tests_validate_diff_ptrs(v_selected_2_copy, v_selected_2,
1624 "v_selected_2");
1625 field_copy_tests_validate_diff_ptrs(v_selected_3_copy, v_selected_3,
1626 "v_selected_3");
1627 field_copy_tests_validate_diff_ptrs(v_selected_4_copy, v_selected_4,
1628 "v_selected_4");
1629 field_copy_tests_validate_diff_ptrs(v_selected_5_copy, v_selected_5,
1630 "v_selected_5");
1631 field_copy_tests_validate_diff_ptrs(v_selected_6_copy, v_selected_6,
1632 "v_selected_6");
1633 field_copy_tests_validate_diff_ptrs(a_copy, a, "a");
1634 field_copy_tests_validate_diff_ptrs(a_0_copy, a_0, "a_0");
1635 field_copy_tests_validate_diff_ptrs(a_1_copy, a_1, "a_1");
1636 field_copy_tests_validate_diff_ptrs(a_2_copy, a_2, "a_2");
1637 field_copy_tests_validate_diff_ptrs(a_3_copy, a_3, "a_3");
1638 field_copy_tests_validate_diff_ptrs(a_4_copy, a_4, "a_4");
1639
1640 /* make sure copied fields share the same types */
1641 field_copy_tests_validate_same_type(strct_copy, strct_type, "strct");
1642 field_copy_tests_validate_same_type(len_copy, len_type, "len");
1643 field_copy_tests_validate_same_type(fp_copy, fp_type, "fp");
1644 field_copy_tests_validate_same_type(e_int_copy, e_int_type, "e_int");
1645 field_copy_tests_validate_same_type(e_copy, e_type, "e");
1646 field_copy_tests_validate_same_type(v_copy, v_type, "v");
1647 field_copy_tests_validate_same_type(v_selected_copy, v_label2_seq_type,
1648 "v_selected");
1649 field_copy_tests_validate_same_type(v_selected_0_copy, v_label2_type,
1650 "v_selected_0");
1651 field_copy_tests_validate_same_type(v_selected_1_copy, v_label2_type,
1652 "v_selected_1");
1653 field_copy_tests_validate_same_type(v_selected_2_copy, v_label2_type,
1654 "v_selected_2");
1655 field_copy_tests_validate_same_type(v_selected_3_copy, v_label2_type,
1656 "v_selected_3");
1657 field_copy_tests_validate_same_type(v_selected_4_copy, v_label2_type,
1658 "v_selected_4");
1659 field_copy_tests_validate_same_type(v_selected_5_copy, v_label2_type,
1660 "v_selected_5");
1661 field_copy_tests_validate_same_type(v_selected_6_copy, v_label2_type,
1662 "v_selected_6");
1663 field_copy_tests_validate_same_type(a_copy, v_label1_array_type, "a");
1664 field_copy_tests_validate_same_type(a_0_copy, v_label1_type, "a_0");
1665 field_copy_tests_validate_same_type(a_1_copy, v_label1_type, "a_1");
1666 field_copy_tests_validate_same_type(a_2_copy, v_label1_type, "a_2");
1667 field_copy_tests_validate_same_type(a_3_copy, v_label1_type, "a_3");
1668 field_copy_tests_validate_same_type(a_4_copy, v_label1_type, "a_4");
1669
1670 /* validate len copy */
1671 ret = bt_ctf_field_unsigned_integer_get_value(len_copy, &uint64_t_val);
1672 assert(!ret);
1673 ok(uint64_t_val == 7,
1674 "bt_ctf_field_copy creates a valid integer field copy");
1675
1676 /* validate fp copy */
1677 ret = bt_ctf_field_floating_point_get_value(fp_copy, &double_val);
1678 assert(!ret);
1679 ok(double_val == 3.14,
1680 "bt_ctf_field_copy creates a valid floating point number field copy");
1681
1682 /* validate s copy */
1683 str_val = bt_ctf_field_string_get_value(s_copy);
1684 ok(str_val && !strcmp(str_val, "btbt"),
1685 "bt_ctf_field_copy creates a valid string field copy");
1686
1687 /* validate e_int copy */
1688 ret = bt_ctf_field_unsigned_integer_get_value(e_int_copy,
1689 &uint64_t_val);
1690 assert(!ret);
1691 ok(uint64_t_val == 23,
1692 "bt_ctf_field_copy creates a valid enum's integer field copy");
1693
1694 /* validate e copy */
1695 str_val = bt_ctf_field_enumeration_get_mapping_name(e_copy);
1696 ok(str_val && !strcmp(str_val, "LABEL2"),
1697 "bt_ctf_field_copy creates a valid enum field copy");
1698
1699 /* validate v_selected copy */
1700 v_selected_copy_len = bt_ctf_field_sequence_get_length(v_selected);
1701 assert(v_selected_copy_len);
1702 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len,
1703 &uint64_t_val);
1704 assert(!ret);
1705 ok(uint64_t_val == 7,
1706 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1707 bt_put(v_selected_copy_len);
1708 v_selected_copy_len = NULL;
1709
1710 /* validate v_selected copy fields */
1711 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy,
1712 &uint64_t_val);
1713 assert(!ret);
1714 ok(uint64_t_val == 7,
1715 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1716 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy,
1717 &uint64_t_val);
1718 assert(!ret);
1719 ok(uint64_t_val == 6,
1720 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1721 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy,
1722 &uint64_t_val);
1723 assert(!ret);
1724 ok(uint64_t_val == 5,
1725 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1726 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy,
1727 &uint64_t_val);
1728 assert(!ret);
1729 ok(uint64_t_val == 4,
1730 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1731 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy,
1732 &uint64_t_val);
1733 assert(!ret);
1734 ok(uint64_t_val == 3,
1735 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1736 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy,
1737 &uint64_t_val);
1738 assert(!ret);
1739 ok(uint64_t_val == 2,
1740 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1741 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy,
1742 &uint64_t_val);
1743 assert(!ret);
1744 ok(uint64_t_val == 1,
1745 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1746
1747 /* validate a copy fields */
1748 str_val = bt_ctf_field_string_get_value(a_0_copy);
1749 ok(str_val && !strcmp(str_val, "a_0"),
1750 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1751 str_val = bt_ctf_field_string_get_value(a_1_copy);
1752 ok(str_val && !strcmp(str_val, "a_1"),
1753 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1754 str_val = bt_ctf_field_string_get_value(a_2_copy);
1755 ok(str_val && !strcmp(str_val, "a_2"),
1756 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1757 str_val = bt_ctf_field_string_get_value(a_3_copy);
1758 ok(str_val && !strcmp(str_val, "a_3"),
1759 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1760 str_val = bt_ctf_field_string_get_value(a_4_copy);
1761 ok(str_val && !strcmp(str_val, "a_4"),
1762 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1763
1764 /* put everything */
1765 bt_put(len_type);
1766 bt_put(fp_type);
1767 bt_put(s_type);
1768 bt_put(e_int_type);
1769 bt_put(e_type);
1770 bt_put(v_type);
1771 bt_put(v_label1_type);
1772 bt_put(v_label1_array_type);
1773 bt_put(v_label2_type);
1774 bt_put(v_label2_seq_type);
1775 bt_put(strct_type);
1776 bt_put(len);
1777 bt_put(fp);
1778 bt_put(s);
1779 bt_put(e_int);
1780 bt_put(e);
1781 bt_put(v);
1782 bt_put(v_selected);
1783 bt_put(v_selected_0);
1784 bt_put(v_selected_1);
1785 bt_put(v_selected_2);
1786 bt_put(v_selected_3);
1787 bt_put(v_selected_4);
1788 bt_put(v_selected_5);
1789 bt_put(v_selected_6);
1790 bt_put(a);
1791 bt_put(a_0);
1792 bt_put(a_1);
1793 bt_put(a_2);
1794 bt_put(a_3);
1795 bt_put(a_4);
1796 bt_put(strct);
1797 bt_put(len_copy);
1798 bt_put(fp_copy);
1799 bt_put(s_copy);
1800 bt_put(e_int_copy);
1801 bt_put(e_copy);
1802 bt_put(v_copy);
1803 bt_put(v_selected_copy);
1804 bt_put(v_selected_0_copy);
1805 bt_put(v_selected_1_copy);
1806 bt_put(v_selected_2_copy);
1807 bt_put(v_selected_3_copy);
1808 bt_put(v_selected_4_copy);
1809 bt_put(v_selected_5_copy);
1810 bt_put(v_selected_6_copy);
1811 bt_put(a_copy);
1812 bt_put(a_0_copy);
1813 bt_put(a_1_copy);
1814 bt_put(a_2_copy);
1815 bt_put(a_3_copy);
1816 bt_put(a_4_copy);
1817 bt_put(strct_copy);
1818 }
1819
1820 static
1821 void type_field_tests()
1822 {
1823 struct bt_ctf_field *uint_12;
1824 struct bt_ctf_field *int_16;
1825 struct bt_ctf_field *string;
1826 struct bt_ctf_field *enumeration;
1827 struct bt_ctf_field_type *composite_structure_type;
1828 struct bt_ctf_field_type *structure_seq_type;
1829 struct bt_ctf_field_type *string_type;
1830 struct bt_ctf_field_type *sequence_type;
1831 struct bt_ctf_field_type *uint_8_type;
1832 struct bt_ctf_field_type *int_16_type;
1833 struct bt_ctf_field_type *uint_12_type =
1834 bt_ctf_field_type_integer_create(12);
1835 struct bt_ctf_field_type *enumeration_type;
1836 struct bt_ctf_field_type *returned_type;
1837 const char *ret_string;
1838
1839 returned_type = bt_ctf_field_get_type(NULL);
1840 ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
1841
1842 ok(uint_12_type, "Create an unsigned integer type");
1843 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1844 BT_CTF_INTEGER_BASE_BINARY) == 0,
1845 "Set integer type's base as binary");
1846 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1847 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
1848 "Set integer type's base as decimal");
1849 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1850 BT_CTF_INTEGER_BASE_UNKNOWN),
1851 "Reject integer type's base set as unknown");
1852 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1853 BT_CTF_INTEGER_BASE_OCTAL) == 0,
1854 "Set integer type's base as octal");
1855 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1856 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
1857 "Set integer type's base as hexadecimal");
1858 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
1859 "Reject unknown integer base value");
1860 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
1861 "Set integer type signedness to signed");
1862 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
1863 "Set integer type signedness to unsigned");
1864 ok(bt_ctf_field_type_integer_get_size(NULL) < 0,
1865 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1866 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
1867 "bt_ctf_field_type_integer_get_size returns a correct value");
1868 ok(bt_ctf_field_type_integer_get_signed(NULL) < 0,
1869 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1870 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
1871 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1872
1873 ok(bt_ctf_field_type_set_byte_order(NULL,
1874 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
1875 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1876 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1877 (enum bt_ctf_byte_order) 42) < 0,
1878 "bt_ctf_field_type_set_byte_order rejects invalid values");
1879 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1880 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
1881 "Set an integer's byte order to little endian");
1882 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1883 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1884 "Set an integer's byte order to big endian");
1885 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
1886 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1887 "bt_ctf_field_type_get_byte_order returns a correct value");
1888 ok(bt_ctf_field_type_get_byte_order(NULL) ==
1889 BT_CTF_BYTE_ORDER_UNKNOWN,
1890 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1891
1892 ok(bt_ctf_field_type_get_type_id(NULL) ==
1893 BT_CTF_TYPE_ID_UNKNOWN,
1894 "bt_ctf_field_type_get_type_id handles NULL correctly");
1895 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
1896 BT_CTF_TYPE_ID_INTEGER,
1897 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1898
1899 ok(bt_ctf_field_type_integer_get_base(NULL) ==
1900 BT_CTF_INTEGER_BASE_UNKNOWN,
1901 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1902 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
1903 BT_CTF_INTEGER_BASE_HEXADECIMAL,
1904 "bt_ctf_field_type_integer_get_base returns a correct value");
1905
1906 ok(bt_ctf_field_type_integer_set_encoding(NULL,
1907 BT_CTF_STRING_ENCODING_ASCII) < 0,
1908 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1909 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1910 (enum bt_ctf_string_encoding) 123) < 0,
1911 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1912 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1913 BT_CTF_STRING_ENCODING_UTF8) == 0,
1914 "Set integer type encoding to UTF8");
1915 ok(bt_ctf_field_type_integer_get_encoding(NULL) ==
1916 BT_CTF_STRING_ENCODING_UNKNOWN,
1917 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1918 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) ==
1919 BT_CTF_STRING_ENCODING_UTF8,
1920 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1921
1922 int_16_type = bt_ctf_field_type_integer_create(16);
1923 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
1924 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
1925 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1926 uint_8_type = bt_ctf_field_type_integer_create(8);
1927 sequence_type =
1928 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
1929 ok(sequence_type, "Create a sequence of int16_t type");
1930 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
1931 BT_CTF_TYPE_ID_SEQUENCE,
1932 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1933
1934 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL,
1935 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1936 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
1937 sequence_type);
1938 ok(!strcmp(ret_string, "seq_len"),
1939 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1940 ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL,
1941 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1942 returned_type = bt_ctf_field_type_sequence_get_element_type(
1943 sequence_type);
1944 ok(returned_type == int_16_type,
1945 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1946 bt_put(returned_type);
1947
1948 string_type = bt_ctf_field_type_string_create();
1949 ok(string_type, "Create a string type");
1950 ok(bt_ctf_field_type_string_set_encoding(string_type,
1951 BT_CTF_STRING_ENCODING_NONE),
1952 "Reject invalid \"None\" string encoding");
1953 ok(bt_ctf_field_type_string_set_encoding(string_type,
1954 42),
1955 "Reject invalid string encoding");
1956 ok(bt_ctf_field_type_string_set_encoding(string_type,
1957 BT_CTF_STRING_ENCODING_ASCII) == 0,
1958 "Set string encoding to ASCII");
1959
1960 ok(bt_ctf_field_type_string_get_encoding(NULL) ==
1961 BT_CTF_STRING_ENCODING_UNKNOWN,
1962 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1963 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
1964 BT_CTF_STRING_ENCODING_ASCII,
1965 "bt_ctf_field_type_string_get_encoding returns the correct value");
1966
1967 structure_seq_type = bt_ctf_field_type_structure_create();
1968 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
1969 BT_CTF_TYPE_ID_STRUCT,
1970 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1971 ok(structure_seq_type, "Create a structure type");
1972 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1973 uint_8_type, "seq_len") == 0,
1974 "Add a uint8_t type to a structure");
1975 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1976 sequence_type, "a_sequence") == 0,
1977 "Add a sequence type to a structure");
1978
1979 ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0,
1980 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1981 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
1982 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1983
1984 ok(bt_ctf_field_type_structure_get_field(NULL,
1985 &ret_string, &returned_type, 1) < 0,
1986 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1987 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1988 NULL, &returned_type, 1) == 0,
1989 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1990 bt_put(returned_type);
1991 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1992 &ret_string, NULL, 1) == 0,
1993 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1994 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1995 &ret_string, &returned_type, 10) < 0,
1996 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1997 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1998 &ret_string, &returned_type, 1) == 0,
1999 "bt_ctf_field_type_structure_get_field returns a field");
2000 ok(!strcmp(ret_string, "a_sequence"),
2001 "bt_ctf_field_type_structure_get_field returns a correct field name");
2002 ok(returned_type == sequence_type,
2003 "bt_ctf_field_type_structure_get_field returns a correct field type");
2004 bt_put(returned_type);
2005
2006 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL,
2007 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
2008 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL,
2009 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2010 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
2011 structure_seq_type, "a_sequence");
2012 ok(returned_type == sequence_type,
2013 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
2014 bt_put(returned_type);
2015
2016 composite_structure_type = bt_ctf_field_type_structure_create();
2017 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2018 string_type, "a_string") == 0,
2019 "Add a string type to a structure");
2020 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
2021 structure_seq_type, "inner_structure") == 0,
2022 "Add a structure type to a structure");
2023
2024 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2025 NULL, "a_sequence") == NULL,
2026 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
2027 ok(bt_ctf_field_type_structure_get_field_type_by_name(
2028 structure_seq_type, NULL) == NULL,
2029 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
2030 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
2031 structure_seq_type, "a_sequence");
2032 ok(returned_type == sequence_type,
2033 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
2034 bt_put(returned_type);
2035
2036 int_16 = bt_ctf_field_create(int_16_type);
2037 ok(int_16, "Instanciate a signed 16-bit integer");
2038 uint_12 = bt_ctf_field_create(uint_12_type);
2039 ok(uint_12, "Instanciate an unsigned 12-bit integer");
2040 returned_type = bt_ctf_field_get_type(int_16);
2041 ok(returned_type == int_16_type,
2042 "bt_ctf_field_get_type returns the correct type");
2043
2044 /* Can't modify types after instanciating them */
2045 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
2046 BT_CTF_INTEGER_BASE_DECIMAL),
2047 "Check an integer type' base can't be modified after instanciation");
2048 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
2049 "Check an integer type's signedness can't be modified after instanciation");
2050
2051 /* Check signed property is checked */
2052 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
2053 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
2054 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
2055 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
2056
2057 /* Check overflows are properly tested for */
2058 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
2059 "Check -32768 is allowed for a signed 16-bit integer");
2060 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
2061 "Check 32767 is allowed for a signed 16-bit integer");
2062 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
2063 "Check 32768 is not allowed for a signed 16-bit integer");
2064 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
2065 "Check -32769 is not allowed for a signed 16-bit integer");
2066 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
2067 "Check -42 is allowed for a signed 16-bit integer");
2068
2069 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
2070 "Check 4095 is allowed for an unsigned 12-bit integer");
2071 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
2072 "Check 4096 is not allowed for a unsigned 12-bit integer");
2073 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
2074 "Check 0 is allowed for an unsigned 12-bit integer");
2075
2076 string = bt_ctf_field_create(string_type);
2077 ok(string, "Instanciate a string field");
2078 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
2079 "Set a string's value");
2080
2081 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
2082 ok(enumeration_type,
2083 "Create an enumeration type with an unsigned 12-bit integer as container");
2084 enumeration = bt_ctf_field_create(enumeration_type);
2085 ok(!enumeration,
2086 "Check enumeration types are validated before instantiation");
2087
2088 bt_put(string);
2089 bt_put(uint_12);
2090 bt_put(int_16);
2091 bt_put(enumeration);
2092 bt_put(composite_structure_type);
2093 bt_put(structure_seq_type);
2094 bt_put(string_type);
2095 bt_put(sequence_type);
2096 bt_put(uint_8_type);
2097 bt_put(int_16_type);
2098 bt_put(uint_12_type);
2099 bt_put(enumeration_type);
2100 bt_put(returned_type);
2101 }
2102
2103 static
2104 void packet_resize_test(struct bt_ctf_stream_class *stream_class,
2105 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
2106 {
2107 /*
2108 * Append enough events to force the underlying packet to be resized.
2109 * Also tests that a new event can be declared after a stream has been
2110 * instantiated and used/flushed.
2111 */
2112 int ret = 0;
2113 int i;
2114 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
2115 "Spammy_Event");
2116 struct bt_ctf_field_type *integer_type =
2117 bt_ctf_field_type_integer_create(17);
2118 struct bt_ctf_field_type *string_type =
2119 bt_ctf_field_type_string_create();
2120 struct bt_ctf_event *event = NULL;
2121 struct bt_ctf_field *ret_field = NULL;
2122 struct bt_ctf_field_type *ret_field_type = NULL;
2123 uint64_t ret_uint64;
2124 int events_appended = 0;
2125 struct bt_ctf_field *packet_context = NULL,
2126 *packet_context_field = NULL, *stream_event_context = NULL;
2127 struct bt_ctf_field_type *ep_field_1_type = NULL;
2128 struct bt_ctf_field_type *ep_a_string_type = NULL;
2129 struct bt_ctf_field_type *ep_type = NULL;
2130
2131 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
2132 "field_1");
2133 ret |= bt_ctf_event_class_add_field(event_class, string_type,
2134 "a_string");
2135 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
2136 ok(ret == 0, "Add a new event class to a stream class after writing an event");
2137 if (ret) {
2138 goto end;
2139 }
2140
2141 /*
2142 * bt_ctf_stream_class_add_event_class() copies the field types
2143 * of event_class, so we retrieve the new ones to create the
2144 * appropriate fields.
2145 */
2146 ep_type = bt_ctf_event_class_get_payload_type(event_class);
2147 assert(ep_type);
2148 ep_field_1_type = bt_ctf_field_type_structure_get_field_type_by_name(
2149 ep_type, "field_1");
2150 assert(ep_field_1_type);
2151 ep_a_string_type = bt_ctf_field_type_structure_get_field_type_by_name(
2152 ep_type, "a_string");
2153 assert(ep_a_string_type);
2154
2155 event = bt_ctf_event_create(event_class);
2156 ret_field = bt_ctf_event_get_payload_by_index(event, 0);
2157 ret_field_type = bt_ctf_field_get_type(ret_field);
2158 ok(bt_ctf_field_type_compare(ret_field_type, integer_type) == 0,
2159 "bt_ctf_event_get_payload_by_index returns a correct field");
2160 bt_put(ret_field_type);
2161 bt_put(ret_field);
2162
2163 ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL,
2164 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2165 ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL,
2166 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2167 bt_put(event);
2168
2169 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
2170 event = bt_ctf_event_create(event_class);
2171 struct bt_ctf_field *integer =
2172 bt_ctf_field_create(ep_field_1_type);
2173 struct bt_ctf_field *string =
2174 bt_ctf_field_create(ep_a_string_type);
2175
2176 ret |= bt_ctf_clock_set_time(clock, ++current_time);
2177 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
2178 ret |= bt_ctf_event_set_payload(event, "field_1",
2179 integer);
2180 bt_put(integer);
2181 ret |= bt_ctf_field_string_set_value(string, "This is a test");
2182 ret |= bt_ctf_event_set_payload(event, "a_string",
2183 string);
2184 bt_put(string);
2185
2186 /* Populate stream event context */
2187 stream_event_context =
2188 bt_ctf_event_get_stream_event_context(event);
2189 integer = bt_ctf_field_structure_get_field(stream_event_context,
2190 "common_event_context");
2191 BT_PUT(stream_event_context);
2192 ret |= bt_ctf_field_unsigned_integer_set_value(integer,
2193 i % 42);
2194 bt_put(integer);
2195
2196 ret |= bt_ctf_stream_append_event(stream, event);
2197 bt_put(event);
2198
2199 if (ret) {
2200 break;
2201 }
2202 }
2203
2204 events_appended = !!(i == PACKET_RESIZE_TEST_LENGTH);
2205 ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0,
2206 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2207 ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0,
2208 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2209 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2210 ok(ret == 0 && ret_uint64 == 0,
2211 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2212 bt_ctf_stream_append_discarded_events(stream, 1000);
2213 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2214 ok(ret == 0 && ret_uint64 == 1000,
2215 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2216
2217 end:
2218 ok(events_appended, "Append 100 000 events to a stream");
2219
2220 /*
2221 * Populate the custom packet context field with a dummy value
2222 * otherwise flush will fail.
2223 */
2224 packet_context = bt_ctf_stream_get_packet_context(stream);
2225 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
2226 "custom_packet_context_field");
2227 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2);
2228
2229 ok(bt_ctf_stream_flush(stream) == 0,
2230 "Flush a stream that forces a packet resize");
2231 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2232 ok(ret == 0 && ret_uint64 == 1000,
2233 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2234 bt_put(integer_type);
2235 bt_put(string_type);
2236 bt_put(packet_context);
2237 bt_put(packet_context_field);
2238 bt_put(stream_event_context);
2239 bt_put(event_class);
2240 bt_put(ep_field_1_type);
2241 bt_put(ep_a_string_type);
2242 bt_put(ep_type);
2243 }
2244
2245 static
2246 void test_empty_stream(struct bt_ctf_writer *writer)
2247 {
2248 int ret = 0;
2249 struct bt_ctf_trace *trace = NULL, *ret_trace = NULL;
2250 struct bt_ctf_stream_class *stream_class = NULL;
2251 struct bt_ctf_stream *stream = NULL;
2252
2253 trace = bt_ctf_writer_get_trace(writer);
2254 if (!trace) {
2255 diag("Failed to get trace from writer");
2256 ret = -1;
2257 goto end;
2258 }
2259
2260 stream_class = bt_ctf_stream_class_create("empty_stream");
2261 if (!stream_class) {
2262 diag("Failed to create stream class");
2263 ret = -1;
2264 goto end;
2265 }
2266
2267 ok(bt_ctf_stream_class_get_trace(NULL) == NULL,
2268 "bt_ctf_stream_class_get_trace handles NULL correctly");
2269 ok(bt_ctf_stream_class_get_trace(stream_class) == NULL,
2270 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2271
2272 stream = bt_ctf_writer_create_stream(writer, stream_class);
2273 if (!stream) {
2274 diag("Failed to create writer stream");
2275 ret = -1;
2276 goto end;
2277 }
2278
2279 ret_trace = bt_ctf_stream_class_get_trace(stream_class);
2280 ok(ret_trace == trace,
2281 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2282 end:
2283 ok(ret == 0,
2284 "Created a stream class with default attributes and an empty stream");
2285 bt_put(trace);
2286 bt_put(ret_trace);
2287 bt_put(stream);
2288 bt_put(stream_class);
2289 }
2290
2291 static
2292 void test_custom_event_header_stream(struct bt_ctf_writer *writer)
2293 {
2294 int i, ret;
2295 struct bt_ctf_trace *trace = NULL;
2296 struct bt_ctf_clock *clock = NULL;
2297 struct bt_ctf_stream_class *stream_class = NULL;
2298 struct bt_ctf_stream *stream = NULL;
2299 struct bt_ctf_field_type *integer_type = NULL,
2300 *sequence_type = NULL, *event_header_type = NULL;
2301 struct bt_ctf_field *integer = NULL, *sequence = NULL,
2302 *event_header = NULL, *packet_header = NULL;
2303 struct bt_ctf_event_class *event_class = NULL;
2304 struct bt_ctf_event *event = NULL;
2305
2306 trace = bt_ctf_writer_get_trace(writer);
2307 if (!trace) {
2308 fail("Failed to get trace from writer");
2309 goto end;
2310 }
2311
2312 clock = bt_ctf_trace_get_clock(trace, 0);
2313 if (!clock) {
2314 fail("Failed to get clock from trace");
2315 goto end;
2316 }
2317
2318 stream_class = bt_ctf_stream_class_create("custom_event_header_stream");
2319 if (!stream_class) {
2320 fail("Failed to create stream class");
2321 goto end;
2322 }
2323
2324 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2325 if (ret) {
2326 fail("Failed to set stream class clock");
2327 goto end;
2328 }
2329
2330 /*
2331 * Customize event header to add an "seq_len" integer member
2332 * which will be used as the length of a sequence in an event of this
2333 * stream.
2334 */
2335 event_header_type = bt_ctf_stream_class_get_event_header_type(
2336 stream_class);
2337 if (!event_header_type) {
2338 fail("Failed to get event header type");
2339 goto end;
2340 }
2341
2342 integer_type = bt_ctf_field_type_integer_create(13);
2343 if (!integer_type) {
2344 fail("Failed to create length integer type");
2345 goto end;
2346 }
2347
2348 ret = bt_ctf_field_type_structure_add_field(event_header_type,
2349 integer_type, "seq_len");
2350 if (ret) {
2351 fail("Failed to add a new field to stream event header");
2352 goto end;
2353 }
2354
2355 event_class = bt_ctf_event_class_create("sequence_event");
2356 if (!event_class) {
2357 fail("Failed to create event class");
2358 goto end;
2359 }
2360
2361 /*
2362 * This event's payload will contain a sequence which references
2363 * stream.event.header.seq_len as its length field.
2364 */
2365 sequence_type = bt_ctf_field_type_sequence_create(integer_type,
2366 "stream.event.header.seq_len");
2367 if (!sequence_type) {
2368 fail("Failed to create a sequence");
2369 goto end;
2370 }
2371
2372 ret = bt_ctf_event_class_add_field(event_class, sequence_type,
2373 "some_sequence");
2374 if (ret) {
2375 fail("Failed to add a sequence to an event class");
2376 goto end;
2377 }
2378
2379 ret = bt_ctf_stream_class_add_event_class(stream_class, event_class);
2380 if (ret) {
2381 fail("Failed to add event class to stream class");
2382 goto end;
2383 }
2384
2385 stream = bt_ctf_writer_create_stream(writer, stream_class);
2386 if (!stream) {
2387 fail("Failed to create stream")
2388 goto end;
2389 }
2390
2391 /*
2392 * We have defined a custom packet header field. We have to populate it
2393 * explicitly.
2394 */
2395 packet_header = bt_ctf_stream_get_packet_header(stream);
2396 if (!packet_header) {
2397 fail("Failed to get stream packet header");
2398 goto end;
2399 }
2400
2401 integer = bt_ctf_field_structure_get_field(packet_header,
2402 "custom_trace_packet_header_field");
2403 if (!integer) {
2404 fail("Failed to retrieve custom_trace_packet_header_field");
2405 goto end;
2406 }
2407
2408 ret = bt_ctf_field_unsigned_integer_set_value(integer, 3487);
2409 if (ret) {
2410 fail("Failed to set custom_trace_packet_header_field value");
2411 goto end;
2412 }
2413 bt_put(integer);
2414
2415 event = bt_ctf_event_create(event_class);
2416 if (!event) {
2417 fail("Failed to create event");
2418 goto end;
2419 }
2420
2421 event_header = bt_ctf_event_get_header(event);
2422 if (!event_header) {
2423 fail("Failed to get event header");
2424 goto end;
2425 }
2426
2427 integer = bt_ctf_field_structure_get_field(event_header,
2428 "seq_len");
2429 if (!integer) {
2430 fail("Failed to get seq_len field from event header");
2431 goto end;
2432 }
2433
2434 ret = bt_ctf_field_unsigned_integer_set_value(integer, 2);
2435 if (ret) {
2436 fail("Failed to set seq_len value in event header");
2437 goto end;
2438 }
2439
2440 /* Populate both sequence integer fields */
2441 sequence = bt_ctf_event_get_payload(event, "some_sequence");
2442 if (!sequence) {
2443 fail("Failed to retrieve sequence from event");
2444 goto end;
2445 }
2446
2447 ret = bt_ctf_field_sequence_set_length(sequence, integer);
2448 if (ret) {
2449 fail("Failed to set sequence length");
2450 goto end;
2451 }
2452 bt_put(integer);
2453
2454 for (i = 0; i < 2; i++) {
2455 integer = bt_ctf_field_sequence_get_field(sequence, i);
2456 if (ret) {
2457 fail("Failed to retrieve sequence element");
2458 goto end;
2459 }
2460
2461 ret = bt_ctf_field_unsigned_integer_set_value(integer, i);
2462 if (ret) {
2463 fail("Failed to set sequence element value");
2464 goto end;
2465 }
2466
2467 bt_put(integer);
2468 integer = NULL;
2469 }
2470
2471 ret = bt_ctf_stream_append_event(stream, event);
2472 if (ret) {
2473 fail("Failed to append event to stream");
2474 goto end;
2475 }
2476
2477 ret = bt_ctf_stream_flush(stream);
2478 if (ret) {
2479 fail("Failed to flush custom_event_header stream");
2480 }
2481 end:
2482 bt_put(clock);
2483 bt_put(trace);
2484 bt_put(stream);
2485 bt_put(stream_class);
2486 bt_put(event_class);
2487 bt_put(event);
2488 bt_put(integer);
2489 bt_put(sequence);
2490 bt_put(event_header);
2491 bt_put(packet_header);
2492 bt_put(sequence_type);
2493 bt_put(integer_type);
2494 bt_put(event_header_type);
2495 }
2496
2497 static
2498 void test_instanciate_event_before_stream(struct bt_ctf_writer *writer)
2499 {
2500 int ret = 0;
2501 struct bt_ctf_trace *trace = NULL;
2502 struct bt_ctf_clock *clock = NULL;
2503 struct bt_ctf_stream_class *stream_class = NULL;
2504 struct bt_ctf_stream *stream = NULL,
2505 *ret_stream = NULL;
2506 struct bt_ctf_event_class *event_class = NULL;
2507 struct bt_ctf_event *event = NULL;
2508 struct bt_ctf_field_type *integer_type = NULL;
2509 struct bt_ctf_field *integer = NULL;
2510
2511 trace = bt_ctf_writer_get_trace(writer);
2512 if (!trace) {
2513 diag("Failed to get trace from writer");
2514 ret = -1;
2515 goto end;
2516 }
2517
2518 clock = bt_ctf_trace_get_clock(trace, 0);
2519 if (!clock) {
2520 diag("Failed to get clock from trace");
2521 ret = -1;
2522 goto end;
2523 }
2524
2525 stream_class = bt_ctf_stream_class_create("event_before_stream_test");
2526 if (!stream_class) {
2527 diag("Failed to create stream class");
2528 ret = -1;
2529 goto end;
2530 }
2531
2532 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2533 if (ret) {
2534 diag("Failed to set stream class clock");
2535 goto end;
2536 }
2537
2538 event_class = bt_ctf_event_class_create("some_event_class_name");
2539 integer_type = bt_ctf_field_type_integer_create(32);
2540 if (!integer_type) {
2541 diag("Failed to create integer field type");
2542 ret = -1;
2543 goto end;
2544 }
2545
2546 ret = bt_ctf_event_class_add_field(event_class, integer_type,
2547 "integer_field");
2548 if (ret) {
2549 diag("Failed to add field to event class");
2550 goto end;
2551 }
2552
2553 ret = bt_ctf_stream_class_add_event_class(stream_class,
2554 event_class);
2555 if (ret) {
2556 diag("Failed to add event class to stream class");
2557 }
2558
2559 event = bt_ctf_event_create(event_class);
2560 if (!event) {
2561 diag("Failed to create event");
2562 ret = -1;
2563 goto end;
2564 }
2565
2566 integer = bt_ctf_event_get_payload_by_index(event, 0);
2567 if (!integer) {
2568 diag("Failed to get integer field payload from event");
2569 ret = -1;
2570 goto end;
2571 }
2572
2573 ret = bt_ctf_field_unsigned_integer_set_value(integer, 1234);
2574 if (ret) {
2575 diag("Failed to set integer field value");
2576 goto end;
2577 }
2578
2579 stream = bt_ctf_writer_create_stream(writer, stream_class);
2580 if (!stream) {
2581 diag("Failed to create writer stream");
2582 ret = -1;
2583 goto end;
2584 }
2585
2586 ok(bt_ctf_event_get_stream(NULL) == NULL,
2587 "bt_ctf_event_get_stream handles NULL correctly");
2588 ok(bt_ctf_event_get_stream(event) == NULL,
2589 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2590
2591 ret = bt_ctf_stream_append_event(stream, event);
2592 if (ret) {
2593 diag("Failed to append event to stream");
2594 goto end;
2595 }
2596
2597 ret_stream = bt_ctf_event_get_stream(event);
2598 ok(ret_stream == stream,
2599 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2600 end:
2601 ok(ret == 0,
2602 "Create an event before instanciating its associated stream");
2603 bt_put(trace);
2604 bt_put(stream);
2605 bt_put(ret_stream);
2606 bt_put(stream_class);
2607 bt_put(event_class);
2608 bt_put(event);
2609 bt_put(integer_type);
2610 bt_put(integer);
2611 bt_put(clock);
2612 }
2613
2614 static
2615 void append_existing_event_class(struct bt_ctf_stream_class *stream_class)
2616 {
2617 struct bt_ctf_event_class *event_class;
2618
2619 event_class = bt_ctf_event_class_create("Simple Event");
2620 assert(event_class);
2621 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2622 "two event classes with the same name cannot cohabit within the same stream class");
2623 bt_put(event_class);
2624
2625 event_class = bt_ctf_event_class_create("different name, ok");
2626 assert(event_class);
2627 assert(!bt_ctf_event_class_set_id(event_class, 11));
2628 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2629 "two event classes with the same ID cannot cohabit within the same stream class");
2630 bt_put(event_class);
2631 }
2632
2633 static
2634 void test_trace_stream_class_clock(void)
2635 {
2636 struct bt_ctf_trace *trace = NULL;
2637 struct bt_ctf_stream_class *sc1 = NULL;
2638 struct bt_ctf_stream_class *sc2 = NULL;
2639 struct bt_ctf_clock *sc1_clock = NULL;
2640 struct bt_ctf_clock *sc2_clock = NULL;
2641 const char *clock_name = "hello";
2642
2643 trace = bt_ctf_trace_create();
2644 assert(trace);
2645 sc1 = bt_ctf_stream_class_create(NULL);
2646 assert(sc1);
2647 sc2 = bt_ctf_stream_class_create(NULL);
2648 assert(sc2);
2649 sc1_clock = bt_ctf_clock_create(clock_name);
2650 assert(sc1_clock);
2651 sc2_clock = bt_ctf_clock_create(clock_name);
2652 assert(sc2_clock);
2653
2654 ok(!bt_ctf_stream_class_set_clock(sc1, sc1_clock),
2655 "bt_ctf_stream_class_set_clock() succeeds for sc1");
2656 ok(!bt_ctf_stream_class_set_clock(sc2, sc2_clock),
2657 "bt_ctf_stream_class_set_clock() succeeds for sc2");
2658 ok(!bt_ctf_trace_add_stream_class(trace, sc1),
2659 "bt_ctf_trace_add_stream_class() succeeds with sc1");
2660 ok(bt_ctf_trace_add_stream_class(trace, sc2),
2661 "bt_ctf_trace_add_stream_class() fails with sc2 (different clock, same name)");
2662
2663 BT_PUT(trace);
2664 BT_PUT(sc1);
2665 BT_PUT(sc2);
2666 BT_PUT(sc1_clock);
2667 BT_PUT(sc2_clock);
2668 }
2669
2670 static
2671 struct bt_ctf_event_class *create_minimal_event_class(void)
2672 {
2673 struct bt_ctf_event_class *ec = NULL;
2674 struct bt_ctf_field_type *int_ft = NULL;
2675 int ret;
2676
2677 int_ft = bt_ctf_field_type_integer_create(23);
2678 assert(int_ft);
2679 ec = bt_ctf_event_class_create("minimal");
2680 assert(ec);
2681 ret = bt_ctf_event_class_add_field(ec, int_ft, "field");
2682 assert(!ret);
2683 BT_PUT(int_ft);
2684
2685 return ec;
2686 }
2687
2688 static
2689 void test_create_writer_vs_non_writer_mode(void)
2690 {
2691 int ret;
2692 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
2693 const char *writer_stream_name = "writer stream instance";
2694 struct bt_ctf_writer *writer = NULL;
2695 struct bt_ctf_trace *writer_trace = NULL;
2696 struct bt_ctf_stream_class *writer_sc = NULL;
2697 struct bt_ctf_stream *writer_stream = NULL;
2698 struct bt_ctf_stream *writer_stream2 = NULL;
2699 struct bt_ctf_stream *packet_stream = NULL;
2700 struct bt_ctf_trace *non_writer_trace = NULL;
2701 struct bt_ctf_stream_class *non_writer_sc = NULL;
2702 struct bt_ctf_stream *non_writer_stream = NULL;
2703 struct bt_ctf_stream *non_writer_stream2 = NULL;
2704 struct bt_ctf_event_class *writer_ec = NULL;
2705 struct bt_ctf_event_class *non_writer_ec = NULL;
2706 struct bt_ctf_event *event = NULL;
2707 struct bt_ctf_event *event2 = NULL;
2708 struct bt_ctf_field_type *empty_struct_ft = NULL;
2709 struct bt_ctf_field *int_field = NULL;
2710 struct bt_ctf_clock *writer_clock = NULL;
2711 struct bt_ctf_clock *non_writer_clock = NULL;
2712 struct bt_ctf_packet *packet = NULL;
2713 struct bt_ctf_packet *packet2 = NULL;
2714
2715 if (!bt_mkdtemp(trace_path)) {
2716 perror("# perror");
2717 }
2718
2719 /* Create empty structure field type (event header) */
2720 empty_struct_ft = bt_ctf_field_type_structure_create();
2721 assert(empty_struct_ft);
2722
2723 /* Create writer, writer stream class, stream, and clock */
2724 writer = bt_ctf_writer_create(trace_path);
2725 assert(writer);
2726 writer_trace = bt_ctf_writer_get_trace(writer);
2727 ok(writer_trace, "bt_ctf_writer_get_trace() returns a trace");
2728 writer_sc = bt_ctf_stream_class_create("writer_sc");
2729 assert(writer_sc);
2730 ret = bt_ctf_stream_class_set_event_header_type(writer_sc,
2731 empty_struct_ft);
2732 assert(!ret);
2733 ret = bt_ctf_trace_add_stream_class(writer_trace, writer_sc);
2734 assert(!ret);
2735 writer_stream = bt_ctf_stream_create(writer_sc, writer_stream_name);
2736 assert(writer_stream);
2737 ok(!strcmp(bt_ctf_stream_get_name(writer_stream), writer_stream_name),
2738 "bt_ctf_stream_get_name() returns the stream's name");
2739 writer_clock = bt_ctf_clock_create("writer_clock");
2740 assert(writer_clock);
2741 ret = bt_ctf_trace_add_clock(writer_trace, writer_clock);
2742 assert(!ret);
2743
2744 /* Create non-writer trace, stream class, stream, and clock */
2745 non_writer_trace = bt_ctf_trace_create();
2746 assert(non_writer_trace);
2747 non_writer_sc = bt_ctf_stream_class_create("nonwriter_sc");
2748 assert(non_writer_sc);
2749 ret = bt_ctf_stream_class_set_event_header_type(non_writer_sc,
2750 empty_struct_ft);
2751 assert(!ret);
2752 ret = bt_ctf_trace_add_stream_class(non_writer_trace, non_writer_sc);
2753 assert(!ret);
2754 non_writer_stream = bt_ctf_stream_create(non_writer_sc, NULL);
2755 assert(non_writer_stream);
2756 non_writer_clock = bt_ctf_clock_create("non_writer_clock");
2757 assert(non_writer_clock);
2758 ret = bt_ctf_trace_add_clock(non_writer_trace, non_writer_clock);
2759 assert(!ret);
2760
2761 /* Create event class and event */
2762 writer_ec = create_minimal_event_class();
2763 assert(writer_ec);
2764 ret = bt_ctf_stream_class_add_event_class(writer_sc, writer_ec);
2765 assert(!ret);
2766 event = bt_ctf_event_create(writer_ec);
2767 assert(event);
2768 int_field = bt_ctf_event_get_payload_by_index(event, 0);
2769 assert(int_field);
2770 bt_ctf_field_unsigned_integer_set_value(int_field, 17);
2771
2772 /*
2773 * Verify non-writer stream: it should be impossible to append
2774 * an event to it.
2775 */
2776 ok(bt_ctf_stream_append_event(non_writer_stream, event),
2777 "bt_ctf_stream_append_event() fails with a non-writer stream");
2778
2779 /*
2780 * Verify writer stream: it should be possible to append an
2781 * event to it.
2782 */
2783 ok(!bt_ctf_stream_append_event(writer_stream, event),
2784 "bt_ctf_stream_append_event() succeeds with a writer stream");
2785
2786 /*
2787 * Verify that it's possible to get and set the value of a
2788 * writer mode clock.
2789 */
2790 ok (!bt_ctf_clock_set_value(writer_clock, 1000),
2791 "bt_ctf_clock_set_value() succeeds with a writer mode clock");
2792 ok (bt_ctf_clock_get_value(writer_clock) == 1000,
2793 "bt_ctf_clock_get_value() succeeds with a writer mode clock");
2794
2795 /*
2796 * Verify that it's impossible to get and set the value of a
2797 * non-writer mode clock.
2798 */
2799 ok (bt_ctf_clock_set_value(non_writer_clock, 1000),
2800 "bt_ctf_clock_set_value() fails with a non-writer mode clock");
2801 ok (bt_ctf_clock_get_value(non_writer_clock) == -1ULL,
2802 "bt_ctf_clock_get_value() fails with a non-writer mode clock");
2803
2804 /*
2805 * It should be possible to create a packet from a non-writer
2806 * stream, but not from a writer stream.
2807 */
2808 packet = bt_ctf_packet_create(writer_stream);
2809 ok(!packet, "bt_ctf_packet_create() fails with a writer stream");
2810 packet = bt_ctf_packet_create(non_writer_stream);
2811 ok(packet, "bt_ctf_packet_create() succeeds with a non-writer stream");
2812 packet_stream = bt_ctf_packet_get_stream(packet);
2813 ok(packet_stream == non_writer_stream,
2814 "bt_ctf_packet_get_stream() returns the correct stream");
2815
2816 /*
2817 * It should not be possible to append an event associated to
2818 * a stream to a different stream.
2819 */
2820 writer_stream2 = bt_ctf_stream_create(writer_sc, "zoo");
2821 assert(writer_stream2);
2822 ok(bt_ctf_stream_append_event(writer_stream2, event),
2823 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2824
2825 /*
2826 * It should not be possible to set the packet of an event
2827 * associated to a given stream to a packet associated with
2828 * a different stream.
2829 */
2830 ok(bt_ctf_event_set_packet(event, packet),
2831 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2832
2833 /*
2834 * It should be possible to set the packet of a fresh event, as
2835 * long as the originating stream classes are the same.
2836 */
2837 event2 = bt_ctf_event_create(writer_ec);
2838 assert(event2);
2839 ok(bt_ctf_event_set_packet(event2, packet),
2840 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2841 non_writer_ec = create_minimal_event_class();
2842 assert(non_writer_ec);
2843 ret = bt_ctf_stream_class_add_event_class(non_writer_sc, non_writer_ec);
2844 assert(!ret);
2845 BT_PUT(event2);
2846 event2 = bt_ctf_event_create(non_writer_ec);
2847 assert(event2);
2848 ok(!bt_ctf_event_set_packet(event2, packet),
2849 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2850
2851 /*
2852 * It should be possible to set a packet created from the same
2853 * stream to an event with an existing packet.
2854 */
2855 packet2 = bt_ctf_packet_create(non_writer_stream);
2856 assert(packet2);
2857 ok(!bt_ctf_event_set_packet(event2, packet2),
2858 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2859 BT_PUT(packet2);
2860
2861 /*
2862 * It should not be possible to set a packet created from a
2863 * different stream to an event with an existing packet.
2864 */
2865 non_writer_stream2 = bt_ctf_stream_create(non_writer_sc, "rj45");
2866 assert(non_writer_stream2);
2867 packet2 = bt_ctf_packet_create(non_writer_stream);
2868 assert(packet2);
2869 ok(!bt_ctf_event_set_packet(event2, packet2),
2870 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2871
2872 bt_put(writer);
2873 bt_put(writer_trace);
2874 bt_put(writer_sc);
2875 bt_put(writer_stream);
2876 bt_put(writer_stream2);
2877 bt_put(non_writer_trace);
2878 bt_put(non_writer_sc);
2879 bt_put(non_writer_stream);
2880 bt_put(non_writer_stream2);
2881 bt_put(packet_stream);
2882 bt_put(writer_ec);
2883 bt_put(non_writer_ec);
2884 bt_put(event);
2885 bt_put(event2);
2886 bt_put(int_field);
2887 bt_put(empty_struct_ft);
2888 bt_put(writer_clock);
2889 bt_put(non_writer_clock);
2890 bt_put(packet);
2891 bt_put(packet2);
2892 recursive_rmdir(trace_path);
2893 }
2894
2895 static
2896 void test_clock_utils(void)
2897 {
2898 int ret;
2899 struct bt_ctf_clock *clock = NULL;
2900
2901 clock = bt_ctf_clock_create("water");
2902 assert(clock);
2903 ret = bt_ctf_clock_set_offset_s(clock, 1234);
2904 assert(!ret);
2905 ret = bt_ctf_clock_set_offset(clock, 1000);
2906 assert(!ret);
2907 ret = bt_ctf_clock_set_frequency(clock, 1000000000);
2908 assert(!ret);
2909 ok(bt_ctf_clock_ns_from_value(clock, 4321) == 1234000005321ULL,
2910 "bt_ctf_clock_ns_from_value() returns the correct value with a 1 GHz frequency");
2911 ret = bt_ctf_clock_set_frequency(clock, 1534);
2912 assert(!ret);
2913 ok(bt_ctf_clock_ns_from_value(clock, 4321) ==
2914 (uint64_t) 1237468709256.845,
2915 "bt_ctf_clock_ns_from_value() returns the correct value with a non-1 GHz frequency");
2916
2917 BT_PUT(clock);
2918 }
2919
2920 int main(int argc, char **argv)
2921 {
2922 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
2923 char metadata_path[sizeof(trace_path) + 9];
2924 const char *clock_name = "test_clock";
2925 const char *clock_description = "This is a test clock";
2926 const char *returned_clock_name;
2927 const char *returned_clock_description;
2928 const uint64_t frequency = 1123456789;
2929 const int64_t offset_s = 1351530929945824323;
2930 const int64_t offset = 1234567;
2931 int64_t get_offset_s,
2932 get_offset,
2933 get_time;
2934 const uint64_t precision = 10;
2935 const int is_absolute = 0xFF;
2936 char *metadata_string;
2937 struct bt_ctf_writer *writer;
2938 struct utsname name;
2939 char hostname[BABELTRACE_HOST_NAME_MAX];
2940 struct bt_ctf_clock *clock, *ret_clock;
2941 struct bt_ctf_stream_class *stream_class, *ret_stream_class;
2942 struct bt_ctf_stream *stream1;
2943 const char *ret_string;
2944 const unsigned char *ret_uuid;
2945 unsigned char tmp_uuid[16] = { 0 };
2946 struct bt_ctf_field_type *packet_context_type,
2947 *packet_context_field_type,
2948 *packet_header_type,
2949 *packet_header_field_type,
2950 *integer_type,
2951 *stream_event_context_type,
2952 *ret_field_type,
2953 *event_header_field_type;
2954 struct bt_ctf_field *packet_header, *packet_header_field;
2955 struct bt_ctf_trace *trace;
2956 int ret;
2957 int64_t ret_int64_t;
2958 struct bt_value *obj;
2959
2960 if (argc < 3) {
2961 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
2962 return -1;
2963 }
2964
2965 plan_tests(NR_TESTS);
2966
2967 if (!bt_mkdtemp(trace_path)) {
2968 perror("# perror");
2969 }
2970
2971 strcpy(metadata_path, trace_path);
2972 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
2973
2974 writer = bt_ctf_writer_create(trace_path);
2975 ok(writer, "bt_ctf_create succeeds in creating trace with path");
2976
2977 ok(!bt_ctf_writer_get_trace(NULL),
2978 "bt_ctf_writer_get_trace correctly handles NULL");
2979 trace = bt_ctf_writer_get_trace(writer);
2980 ok(trace,
2981 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2982 ok(bt_ctf_trace_set_byte_order(trace, BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
2983 "Set a trace's byte order to big endian");
2984 ok(bt_ctf_trace_get_byte_order(trace) == BT_CTF_BYTE_ORDER_BIG_ENDIAN,
2985 "bt_ctf_trace_get_byte_order returns a correct endianness");
2986
2987 /* Add environment context to the trace */
2988 ret = gethostname(hostname, sizeof(hostname));
2989 if (ret < 0) {
2990 return ret;
2991 }
2992 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
2993 "Add host (%s) environment field to writer instance",
2994 hostname);
2995 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
2996 "test_value"),
2997 "bt_ctf_writer_add_environment_field error with NULL writer");
2998 ok(bt_ctf_writer_add_environment_field(writer, NULL,
2999 "test_value"),
3000 "bt_ctf_writer_add_environment_field error with NULL field name");
3001 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
3002 NULL),
3003 "bt_ctf_writer_add_environment_field error with NULL field value");
3004
3005 /* Test bt_ctf_trace_set_environment_field with an integer object */
3006 obj = bt_value_integer_create_init(23);
3007 assert(obj);
3008 ok(bt_ctf_trace_set_environment_field(NULL, "test_env_int_obj", obj),
3009 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
3010 ok(bt_ctf_trace_set_environment_field(trace, NULL, obj),
3011 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
3012 ok(bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", NULL),
3013 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
3014 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", obj),
3015 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
3016 BT_PUT(obj);
3017
3018 /* Test bt_ctf_trace_set_environment_field with a string object */
3019 obj = bt_value_string_create_init("the value");
3020 assert(obj);
3021 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_str_obj", obj),
3022 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
3023 BT_PUT(obj);
3024
3025 /* Test bt_ctf_trace_set_environment_field_integer */
3026 ok(bt_ctf_trace_set_environment_field_integer(NULL, "test_env_int",
3027 -194875),
3028 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
3029 ok(bt_ctf_trace_set_environment_field_integer(trace, NULL, -194875),
3030 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
3031 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
3032 -164973),
3033 "bt_ctf_trace_set_environment_field_integer succeeds");
3034
3035 /* Test bt_ctf_trace_set_environment_field_string */
3036 ok(bt_ctf_trace_set_environment_field_string(NULL, "test_env_str",
3037 "yeah"),
3038 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
3039 ok(bt_ctf_trace_set_environment_field_string(trace, NULL, "yeah"),
3040 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
3041 ok(bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
3042 NULL),
3043 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
3044 ok(!bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
3045 "oh yeah"),
3046 "bt_ctf_trace_set_environment_field_string succeeds");
3047
3048 /* Test bt_ctf_trace_get_environment_field_count */
3049 ok(bt_ctf_trace_get_environment_field_count(NULL) < 0,
3050 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
3051 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
3052 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
3053
3054 /* Test bt_ctf_trace_get_environment_field_name */
3055 ok(bt_ctf_trace_get_environment_field_name(NULL, 0) == NULL,
3056 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
3057 ok(bt_ctf_trace_get_environment_field_name(trace, -1) == NULL,
3058 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
3059 ok(bt_ctf_trace_get_environment_field_name(trace, 5) == NULL,
3060 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
3061 ret_string = bt_ctf_trace_get_environment_field_name(trace, 0);
3062 ok(ret_string && !strcmp(ret_string, "host"),
3063 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3064 ret_string = bt_ctf_trace_get_environment_field_name(trace, 1);
3065 ok(ret_string && !strcmp(ret_string, "test_env_int_obj"),
3066 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3067 ret_string = bt_ctf_trace_get_environment_field_name(trace, 2);
3068 ok(ret_string && !strcmp(ret_string, "test_env_str_obj"),
3069 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3070 ret_string = bt_ctf_trace_get_environment_field_name(trace, 3);
3071 ok(ret_string && !strcmp(ret_string, "test_env_int"),
3072 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3073 ret_string = bt_ctf_trace_get_environment_field_name(trace, 4);
3074 ok(ret_string && !strcmp(ret_string, "test_env_str"),
3075 "bt_ctf_trace_get_environment_field_name returns a correct field name");
3076
3077 /* Test bt_ctf_trace_get_environment_field_value */
3078 ok(bt_ctf_trace_get_environment_field_value(NULL, 0) == NULL,
3079 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
3080 ok(bt_ctf_trace_get_environment_field_value(trace, -1) == NULL,
3081 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
3082 ok(bt_ctf_trace_get_environment_field_value(trace, 5) == NULL,
3083 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
3084 obj = bt_ctf_trace_get_environment_field_value(trace, 1);
3085 ret = bt_value_integer_get(obj, &ret_int64_t);
3086 ok(!ret && ret_int64_t == 23,
3087 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
3088 BT_PUT(obj);
3089 obj = bt_ctf_trace_get_environment_field_value(trace, 2);
3090 ret = bt_value_string_get(obj, &ret_string);
3091 ok(!ret && ret_string && !strcmp(ret_string, "the value"),
3092 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
3093 BT_PUT(obj);
3094
3095 /* Test bt_ctf_trace_get_environment_field_value_by_name */
3096 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL,
3097 "test_env_str"),
3098 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3099 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, NULL),
3100 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3101 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, "oh oh"),
3102 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3103 obj = bt_ctf_trace_get_environment_field_value_by_name(trace,
3104 "test_env_str");
3105 ret = bt_value_string_get(obj, &ret_string);
3106 ok(!ret && ret_string && !strcmp(ret_string, "oh yeah"),
3107 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3108 BT_PUT(obj);
3109
3110 /* Test environment field replacement */
3111 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
3112 654321),
3113 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3114 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
3115 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3116 obj = bt_ctf_trace_get_environment_field_value(trace, 3);
3117 ret = bt_value_integer_get(obj, &ret_int64_t);
3118 ok(!ret && ret_int64_t == 654321,
3119 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3120 BT_PUT(obj);
3121
3122 /* On Solaris, uname() can return any positive value on success */
3123 if (uname(&name) < 0) {
3124 perror("uname");
3125 return -1;
3126 }
3127
3128 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
3129 == 0, "Add sysname (%s) environment field to writer instance",
3130 name.sysname);
3131 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
3132 name.nodename) == 0,
3133 "Add nodename (%s) environment field to writer instance",
3134 name.nodename);
3135 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
3136 == 0, "Add release (%s) environment field to writer instance",
3137 name.release);
3138 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
3139 == 0, "Add version (%s) environment field to writer instance",
3140 name.version);
3141 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
3142 == 0, "Add machine (%s) environment field to writer istance",
3143 name.machine);
3144
3145 /* Define a clock and add it to the trace */
3146 ok(bt_ctf_clock_create("signed") == NULL,
3147 "Illegal clock name rejected");
3148 ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
3149 clock = bt_ctf_clock_create(clock_name);
3150 ok(clock, "Clock created sucessfully");
3151 returned_clock_name = bt_ctf_clock_get_name(clock);
3152 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
3153 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
3154 "Returned clock name is valid");
3155
3156 returned_clock_description = bt_ctf_clock_get_description(clock);
3157 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
3158 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
3159 "Clock description set successfully");
3160
3161 returned_clock_description = bt_ctf_clock_get_description(clock);
3162 ok(returned_clock_description,
3163 "bt_ctf_clock_get_description returns a description.");
3164 ok(returned_clock_description ?
3165 !strcmp(returned_clock_description, clock_description) : 0,
3166 "Returned clock description is valid");
3167
3168 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
3169 "bt_ctf_clock_get_frequency returns the correct default frequency");
3170 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
3171 "Set clock frequency");
3172 ok(bt_ctf_clock_get_frequency(clock) == frequency,
3173 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3174
3175 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
3176 "bt_ctf_clock_get_offset_s succeeds");
3177 ok(get_offset_s == DEFAULT_CLOCK_OFFSET_S,
3178 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3179 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
3180 "Set clock offset (seconds)");
3181 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
3182 "bt_ctf_clock_get_offset_s succeeds");
3183 ok(get_offset_s == offset_s,
3184 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3185
3186 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
3187 "bt_ctf_clock_get_offset succeeds");
3188 ok(get_offset == DEFAULT_CLOCK_OFFSET,
3189 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3190 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
3191 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
3192 "bt_ctf_clock_get_offset succeeds");
3193 ok(get_offset == offset,
3194 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3195
3196 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
3197 "bt_ctf_clock_get_precision returns the correct default precision");
3198 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
3199 "Set clock precision");
3200 ok(bt_ctf_clock_get_precision(clock) == precision,
3201 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3202
3203 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
3204 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3205 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
3206 "Set clock absolute property");
3207 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
3208 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3209
3210 ok(bt_ctf_clock_get_time(clock, &get_time) == 0,
3211 "bt_ctf_clock_get_time succeeds");
3212 ok(get_time == DEFAULT_CLOCK_TIME,
3213 "bt_ctf_clock_get_time returns the correct default time");
3214 ok(bt_ctf_clock_get_value(clock) == DEFAULT_CLOCK_VALUE,
3215 "bt_ctf_clock_get_value returns the correct default value");
3216 ok(bt_ctf_clock_set_value(clock, current_time) == 0,
3217 "Set clock value");
3218 ok(bt_ctf_clock_get_value(clock) == current_time,
3219 "bt_ctf_clock_get_value returns the correct value once it is set");
3220 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
3221 "Set clock time");
3222 ok(bt_ctf_clock_get_time(clock, &get_time) == 0,
3223 "bt_ctf_clock_get_time succeeds");
3224 ok(get_time >= current_time - 1 && get_time <= current_time + 1,
3225 "bt_ctf_clock_get_time returns the correct time once it is set");
3226
3227 ok(!bt_ctf_clock_get_name(NULL),
3228 "bt_ctf_clock_get_name correctly handles NULL");
3229 ok(!bt_ctf_clock_get_description(NULL),
3230 "bt_ctf_clock_get_description correctly handles NULL");
3231 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
3232 "bt_ctf_clock_get_frequency correctly handles NULL");
3233 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
3234 "bt_ctf_clock_get_precision correctly handles NULL");
3235 ok(bt_ctf_clock_get_offset_s(NULL, &get_offset_s) < 0,
3236 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3237 ok(bt_ctf_clock_get_offset_s(clock, NULL) < 0,
3238 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3239 ok(bt_ctf_clock_get_offset(NULL, &get_offset) < 0,
3240 "bt_ctf_clock_get_offset correctly handles NULL clock");
3241 ok(bt_ctf_clock_get_offset(clock, NULL) < 0,
3242 "bt_ctf_clock_get_offset correctly handles NULL output");
3243 ok(bt_ctf_clock_get_is_absolute(NULL) < 0,
3244 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3245 ok(bt_ctf_clock_get_time(NULL, &get_time) < 0,
3246 "bt_ctf_clock_get_time correctly handles NULL clock");
3247 ok(bt_ctf_clock_get_time(clock, NULL) < 0,
3248 "bt_ctf_clock_get_time correctly handles NULL output");
3249
3250 ok(bt_ctf_clock_set_description(NULL, NULL) < 0,
3251 "bt_ctf_clock_set_description correctly handles NULL clock");
3252 ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0,
3253 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3254 ok(bt_ctf_clock_set_precision(NULL, precision) < 0,
3255 "bt_ctf_clock_get_precision correctly handles NULL clock");
3256 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0,
3257 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3258 ok(bt_ctf_clock_set_offset(NULL, offset) < 0,
3259 "bt_ctf_clock_set_offset correctly handles NULL clock");
3260 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0,
3261 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3262 ok(bt_ctf_clock_set_time(NULL, current_time) < 0,
3263 "bt_ctf_clock_set_time correctly handles NULL clock");
3264 ok(bt_ctf_clock_get_uuid(NULL) == NULL,
3265 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3266 ret_uuid = bt_ctf_clock_get_uuid(clock);
3267 ok(ret_uuid,
3268 "bt_ctf_clock_get_uuid returns a UUID");
3269 if (ret_uuid) {
3270 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
3271 /* Slightly modify UUID */
3272 tmp_uuid[sizeof(tmp_uuid) - 1]++;
3273 }
3274
3275 ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0,
3276 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3277 ok(bt_ctf_clock_set_uuid(clock, NULL) < 0,
3278 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3279 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
3280 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3281 ret_uuid = bt_ctf_clock_get_uuid(clock);
3282 ok(ret_uuid,
3283 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3284 ok(uuid_match(ret_uuid, tmp_uuid),
3285 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3286
3287 /* Define a stream class */
3288 stream_class = bt_ctf_stream_class_create("test_stream");
3289
3290 ok(bt_ctf_stream_class_get_name(NULL) == NULL,
3291 "bt_ctf_stream_class_get_name handles NULL correctly");
3292 ret_string = bt_ctf_stream_class_get_name(stream_class);
3293 ok(ret_string && !strcmp(ret_string, "test_stream"),
3294 "bt_ctf_stream_class_get_name returns a correct stream class name");
3295
3296 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
3297 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3298 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
3299 "bt_ctf_stream_class_get_clock handles NULL correctly");
3300
3301 ok(stream_class, "Create stream class");
3302 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
3303 "Set a stream class' clock");
3304 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
3305 ok(ret_clock == clock,
3306 "bt_ctf_stream_class_get_clock returns a correct clock");
3307 bt_put(ret_clock);
3308
3309 /* Test the event fields and event types APIs */
3310 type_field_tests();
3311
3312 /* Test fields copying */
3313 field_copy_tests();
3314
3315 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
3316 "bt_ctf_stream_class_get_id returns an error when no id is set");
3317 ok(bt_ctf_stream_class_get_id(NULL) < 0,
3318 "bt_ctf_stream_class_get_id handles NULL correctly");
3319 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
3320 "bt_ctf_stream_class_set_id handles NULL correctly");
3321 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
3322 "Set an stream class' id");
3323 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
3324 "bt_ctf_stream_class_get_id returns the correct value");
3325
3326 /* Validate default event header fields */
3327 ok(bt_ctf_stream_class_get_event_header_type(NULL) == NULL,
3328 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3329 ret_field_type = bt_ctf_stream_class_get_event_header_type(
3330 stream_class);
3331 ok(ret_field_type,
3332 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3333 ok(bt_ctf_field_type_get_type_id(ret_field_type) == BT_CTF_TYPE_ID_STRUCT,
3334 "Default event header type is a structure");
3335 event_header_field_type =
3336 bt_ctf_field_type_structure_get_field_type_by_name(
3337 ret_field_type, "id");
3338 ok(event_header_field_type,
3339 "Default event header type contains an \"id\" field");
3340 ok(bt_ctf_field_type_get_type_id(
3341 event_header_field_type) == BT_CTF_TYPE_ID_INTEGER,
3342 "Default event header \"id\" field is an integer");
3343 bt_put(event_header_field_type);
3344 event_header_field_type =
3345 bt_ctf_field_type_structure_get_field_type_by_name(
3346 ret_field_type, "timestamp");
3347 ok(event_header_field_type,
3348 "Default event header type contains a \"timestamp\" field");
3349 ok(bt_ctf_field_type_get_type_id(
3350 event_header_field_type) == BT_CTF_TYPE_ID_INTEGER,
3351 "Default event header \"timestamp\" field is an integer");
3352 bt_put(event_header_field_type);
3353 bt_put(ret_field_type);
3354
3355 /* Add a custom trace packet header field */
3356 ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL,
3357 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3358 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
3359 ok(packet_header_type,
3360 "bt_ctf_trace_get_packet_header_type returns a packet header");
3361 ok(bt_ctf_field_type_get_type_id(packet_header_type) == BT_CTF_TYPE_ID_STRUCT,
3362 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3363 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3364 packet_header_type, "magic");
3365 ok(ret_field_type, "Default packet header type contains a \"magic\" field");
3366 bt_put(ret_field_type);
3367 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3368 packet_header_type, "uuid");
3369 ok(ret_field_type, "Default packet header type contains a \"uuid\" 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, "stream_id");
3373 ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
3374 bt_put(ret_field_type);
3375
3376 packet_header_field_type = bt_ctf_field_type_integer_create(22);
3377 ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
3378 packet_header_field_type, "custom_trace_packet_header_field"),
3379 "Added a custom trace packet header field successfully");
3380
3381 ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0,
3382 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3383 ok(bt_ctf_trace_set_packet_header_type(trace, NULL) < 0,
3384 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
3385 ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type),
3386 "Set a trace packet_header_type successfully");
3387
3388 ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL,
3389 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3390
3391 /* Add a custom field to the stream class' packet context */
3392 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
3393 ok(packet_context_type,
3394 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3395 ok(bt_ctf_field_type_get_type_id(packet_context_type) == BT_CTF_TYPE_ID_STRUCT,
3396 "Packet context is a structure");
3397
3398 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
3399 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3400 ok(bt_ctf_stream_class_set_packet_context_type(stream_class, NULL),
3401 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
3402
3403 integer_type = bt_ctf_field_type_integer_create(32);
3404 ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
3405 integer_type) < 0,
3406 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3407 /* Create a "uint5_t" equivalent custom packet context field */
3408 packet_context_field_type = bt_ctf_field_type_integer_create(5);
3409
3410 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3411 packet_context_field_type, "custom_packet_context_field");
3412 ok(ret == 0, "Packet context field added successfully");
3413
3414 /* Define a stream event context containing a my_integer field. */
3415 ok(bt_ctf_stream_class_get_event_context_type(NULL) == NULL,
3416 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3417 ok(bt_ctf_stream_class_get_event_context_type(
3418 stream_class) == NULL,
3419 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3420 stream_event_context_type = bt_ctf_field_type_structure_create();
3421 bt_ctf_field_type_structure_add_field(stream_event_context_type,
3422 integer_type, "common_event_context");
3423
3424 ok(bt_ctf_stream_class_set_event_context_type(NULL,
3425 stream_event_context_type) < 0,
3426 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3427 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3428 NULL) < 0,
3429 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
3430 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3431 integer_type) < 0,
3432 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3433
3434 ok(bt_ctf_stream_class_set_event_context_type(
3435 stream_class, stream_event_context_type) == 0,
3436 "Set a new stream event context type");
3437 ret_field_type = bt_ctf_stream_class_get_event_context_type(
3438 stream_class);
3439 ok(ret_field_type == stream_event_context_type,
3440 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3441 bt_put(ret_field_type);
3442
3443 /* Instantiate a stream and append events */
3444 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
3445 ok(stream1, "Instanciate a stream class from writer");
3446
3447 /*
3448 * Creating a stream through a writer adds the given stream
3449 * class to the writer's trace, thus registering the stream
3450 * class's clock to the trace.
3451 */
3452 ok(bt_ctf_trace_get_clock_count(NULL) < 0,
3453 "bt_ctf_trace_get_clock_count correctly handles NULL");
3454 ok(bt_ctf_trace_get_clock_count(trace) == 1,
3455 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
3456 ok(!bt_ctf_trace_get_clock(NULL, 0),
3457 "bt_ctf_trace_get_clock correctly handles NULL");
3458 ok(!bt_ctf_trace_get_clock(trace, -1),
3459 "bt_ctf_trace_get_clock correctly handles negative indexes");
3460 ok(!bt_ctf_trace_get_clock(trace, 1),
3461 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
3462 ret_clock = bt_ctf_trace_get_clock(trace, 0);
3463 ok(ret_clock == clock,
3464 "bt_ctf_trace_get_clock returns the right clock instance");
3465 bt_put(ret_clock);
3466 ok(!bt_ctf_trace_get_clock_by_name(trace, NULL),
3467 "bt_ctf_trace_get_clock_by_name correctly handles NULL (trace)");
3468 ok(!bt_ctf_trace_get_clock_by_name(NULL, clock_name),
3469 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3470 ok(!bt_ctf_trace_get_clock_by_name(NULL, NULL),
3471 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3472 ret_clock = bt_ctf_trace_get_clock_by_name(trace, clock_name);
3473 ok(ret_clock == clock,
3474 "bt_ctf_trace_get_clock_by_name returns the right clock instance");
3475 bt_put(ret_clock);
3476 ok(!bt_ctf_trace_get_clock_by_name(trace, "random"),
3477 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3478
3479 ok(bt_ctf_stream_get_class(NULL) == NULL,
3480 "bt_ctf_stream_get_class correctly handles NULL");
3481 ret_stream_class = bt_ctf_stream_get_class(stream1);
3482 ok(ret_stream_class,
3483 "bt_ctf_stream_get_class returns a stream class");
3484 ok(ret_stream_class == stream_class,
3485 "Returned stream class is of the correct type");
3486
3487 /*
3488 * Packet header, packet context, event header, and stream
3489 * event context types were copied for the resolving
3490 * process
3491 */
3492 BT_PUT(packet_header_type);
3493 BT_PUT(packet_context_type);
3494 BT_PUT(stream_event_context_type);
3495 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
3496 assert(packet_header_type);
3497 packet_context_type =
3498 bt_ctf_stream_class_get_packet_context_type(stream_class);
3499 assert(packet_context_type);
3500 stream_event_context_type =
3501 bt_ctf_stream_class_get_event_context_type(stream_class);
3502 assert(stream_event_context_type);
3503
3504 /*
3505 * Try to modify the packet context type after a stream has been
3506 * created.
3507 */
3508 ret = bt_ctf_field_type_structure_add_field(packet_header_type,
3509 packet_header_field_type, "should_fail");
3510 ok(ret < 0,
3511 "Trace packet header type can't be modified once a stream has been instanciated");
3512
3513 /*
3514 * Try to modify the packet context type after a stream has been
3515 * created.
3516 */
3517 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3518 packet_context_field_type, "should_fail");
3519 ok(ret < 0,
3520 "Packet context type can't be modified once a stream has been instanciated");
3521
3522 /*
3523 * Try to modify the stream event context type after a stream has been
3524 * created.
3525 */
3526 ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
3527 integer_type, "should_fail");
3528 ok(ret < 0,
3529 "Stream event context type can't be modified once a stream has been instanciated");
3530
3531 /* Should fail after instanciating a stream (frozen) */
3532 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
3533 "Changes to a stream class that was already instantiated fail");
3534
3535 /* Populate the custom packet header field only once for all tests */
3536 ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
3537 "bt_ctf_stream_get_packet_header handles NULL correctly");
3538 packet_header = bt_ctf_stream_get_packet_header(stream1);
3539 ok(packet_header,
3540 "bt_ctf_stream_get_packet_header returns a packet header");
3541 ret_field_type = bt_ctf_field_get_type(packet_header);
3542 ok(ret_field_type == packet_header_type,
3543 "Stream returns a packet header of the appropriate type");
3544 bt_put(ret_field_type);
3545 packet_header_field = bt_ctf_field_structure_get_field(packet_header,
3546 "custom_trace_packet_header_field");
3547 ok(packet_header_field,
3548 "Packet header structure contains a custom field with the appropriate name");
3549 ret_field_type = bt_ctf_field_get_type(packet_header_field);
3550 ok(bt_ctf_field_type_compare(ret_field_type, packet_header_field_type) == 0,
3551 "Custom packet header field is of the expected type");
3552 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field,
3553 54321), "Set custom packet header value successfully");
3554 ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
3555 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3556 ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
3557 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3558 ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
3559 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3560 ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
3561 "Successfully set a stream's packet header");
3562
3563 ok(bt_ctf_writer_add_environment_field(writer, "new_field", "test") == 0,
3564 "Add environment field to writer after stream creation");
3565
3566 test_clock_utils();
3567
3568 test_trace_stream_class_clock();
3569
3570 test_create_writer_vs_non_writer_mode();
3571
3572 test_instanciate_event_before_stream(writer);
3573
3574 append_simple_event(stream_class, stream1, clock);
3575
3576 packet_resize_test(stream_class, stream1, clock);
3577
3578 append_complex_event(stream_class, stream1, clock);
3579
3580 append_existing_event_class(stream_class);
3581
3582 test_empty_stream(writer);
3583
3584 test_custom_event_header_stream(writer);
3585
3586 metadata_string = bt_ctf_writer_get_metadata_string(writer);
3587 ok(metadata_string, "Get metadata string");
3588
3589 bt_ctf_writer_flush_metadata(writer);
3590 validate_metadata(argv[1], metadata_path);
3591 validate_trace(argv[2], trace_path);
3592
3593 bt_put(clock);
3594 bt_put(ret_stream_class);
3595 bt_put(writer);
3596 bt_put(stream1);
3597 bt_put(packet_context_type);
3598 bt_put(packet_context_field_type);
3599 bt_put(integer_type);
3600 bt_put(stream_event_context_type);
3601 bt_put(ret_field_type);
3602 bt_put(packet_header_type);
3603 bt_put(packet_header_field_type);
3604 bt_put(packet_header);
3605 bt_put(packet_header_field);
3606 bt_put(trace);
3607 free(metadata_string);
3608 bt_put(stream_class);
3609
3610 recursive_rmdir(trace_path);
3611 return 0;
3612 }
This page took 0.10494 seconds and 4 git commands to generate.