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