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