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