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