Ensure types used for index and count in CTF IR API match
[babeltrace.git] / tests / lib / test_ctf_writer.c
1 /*
2 * test-ctf-writer.c
3 *
4 * CTF Writer test
5 *
6 * Copyright 2013 - 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/events.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <sys/utsname.h>
34 #include <babeltrace/compat/limits.h>
35 #include <string.h>
36 #include <assert.h>
37 #include <unistd.h>
38 #include <sys/wait.h>
39 #include <fcntl.h>
40 #include <dirent.h>
41 #include "tap/tap.h"
42 #include <math.h>
43 #include <float.h>
44
45 #define METADATA_LINE_SIZE 512
46 #define SEQUENCE_TEST_LENGTH 10
47 #define ARRAY_TEST_LENGTH 5
48 #define PACKET_RESIZE_TEST_LENGTH 100000
49
50 #define DEFAULT_CLOCK_FREQ 1000000000
51 #define DEFAULT_CLOCK_PRECISION 1
52 #define DEFAULT_CLOCK_OFFSET 0
53 #define DEFAULT_CLOCK_OFFSET_S 0
54 #define DEFAULT_CLOCK_IS_ABSOLUTE 0
55 #define DEFAULT_CLOCK_TIME 0
56
57 static uint64_t current_time = 42;
58
59 /* Return 1 if uuids match, zero if different. */
60 int uuid_match(const unsigned char *uuid_a, const unsigned char *uuid_b)
61 {
62 int ret = 0;
63 int i;
64
65 if (!uuid_a || !uuid_b) {
66 goto end;
67 }
68
69 for (i = 0; i < 16; i++) {
70 if (uuid_a[i] != uuid_b[i]) {
71 goto end;
72 }
73 }
74
75 ret = 1;
76 end:
77 return ret;
78 }
79
80 void validate_metadata(char *parser_path, char *metadata_path)
81 {
82 int ret = 0;
83 char parser_output_path[] = "/tmp/parser_output_XXXXXX";
84 int parser_output_fd = -1, metadata_fd = -1;
85
86 if (!metadata_path) {
87 ret = -1;
88 goto result;
89 }
90
91 parser_output_fd = mkstemp(parser_output_path);
92 metadata_fd = open(metadata_path, O_RDONLY);
93
94 unlink(parser_output_path);
95
96 if (parser_output_fd == -1 || metadata_fd == -1) {
97 diag("Failed create temporary files for metadata parsing.");
98 ret = -1;
99 goto result;
100 }
101
102 pid_t pid = fork();
103 if (pid) {
104 int status = 0;
105 waitpid(pid, &status, 0);
106 ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
107 } else {
108 /* ctf-parser-test expects a metadata string on stdin. */
109 ret = dup2(metadata_fd, STDIN_FILENO);
110 if (ret < 0) {
111 perror("# dup2 metadata_fd to STDIN");
112 goto result;
113 }
114
115 ret = dup2(parser_output_fd, STDOUT_FILENO);
116 if (ret < 0) {
117 perror("# dup2 parser_output_fd to STDOUT");
118 goto result;
119 }
120
121 ret = dup2(parser_output_fd, STDERR_FILENO);
122 if (ret < 0) {
123 perror("# dup2 parser_output_fd to STDERR");
124 goto result;
125 }
126
127 execl(parser_path, "ctf-parser-test", NULL);
128 perror("# Could not launch the ctf metadata parser process");
129 exit(-1);
130 }
131 result:
132 ok(ret == 0, "Metadata string is valid");
133
134 if (ret && metadata_fd >= 0 && parser_output_fd >= 0) {
135 char *line;
136 size_t len = METADATA_LINE_SIZE;
137 FILE *metadata_fp = NULL, *parser_output_fp = NULL;
138
139 metadata_fp = fdopen(metadata_fd, "r");
140 if (!metadata_fp) {
141 perror("fdopen on metadata_fd");
142 goto close_fp;
143 }
144 metadata_fd = -1;
145
146 parser_output_fp = fdopen(parser_output_fd, "r");
147 if (!parser_output_fp) {
148 perror("fdopen on parser_output_fd");
149 goto close_fp;
150 }
151 parser_output_fd = -1;
152
153 line = malloc(len);
154 if (!line) {
155 diag("malloc failure");
156 }
157
158 rewind(metadata_fp);
159
160 /* Output the metadata and parser output as diagnostic */
161 while (getline(&line, &len, metadata_fp) > 0) {
162 diag("%s", line);
163 }
164
165 rewind(parser_output_fp);
166 while (getline(&line, &len, parser_output_fp) > 0) {
167 diag("%s", line);
168 }
169
170 free(line);
171 close_fp:
172 if (metadata_fp) {
173 if (fclose(metadata_fp)) {
174 diag("fclose failure");
175 }
176 }
177 if (parser_output_fp) {
178 if (fclose(parser_output_fp)) {
179 diag("fclose failure");
180 }
181 }
182 }
183
184 if (parser_output_fd >= 0) {
185 if (close(parser_output_fd)) {
186 diag("close error");
187 }
188 }
189 if (metadata_fd >= 0) {
190 if (close(metadata_fd)) {
191 diag("close error");
192 }
193 }
194 }
195
196 void validate_trace(char *parser_path, char *trace_path)
197 {
198 int ret = 0;
199 char babeltrace_output_path[] = "/tmp/babeltrace_output_XXXXXX";
200 int babeltrace_output_fd = -1;
201
202 if (!trace_path) {
203 ret = -1;
204 goto result;
205 }
206
207 babeltrace_output_fd = mkstemp(babeltrace_output_path);
208 unlink(babeltrace_output_path);
209
210 if (babeltrace_output_fd == -1) {
211 diag("Failed to create a temporary file for trace parsing.");
212 ret = -1;
213 goto result;
214 }
215
216 pid_t pid = fork();
217 if (pid) {
218 int status = 0;
219 waitpid(pid, &status, 0);
220 ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
221 } else {
222 ret = dup2(babeltrace_output_fd, STDOUT_FILENO);
223 if (ret < 0) {
224 perror("# dup2 babeltrace_output_fd to STDOUT");
225 goto result;
226 }
227
228 ret = dup2(babeltrace_output_fd, STDERR_FILENO);
229 if (ret < 0) {
230 perror("# dup2 babeltrace_output_fd to STDERR");
231 goto result;
232 }
233
234 execl(parser_path, "babeltrace", trace_path, NULL);
235 perror("# Could not launch the babeltrace process");
236 exit(-1);
237 }
238 result:
239 ok(ret == 0, "Babeltrace could read the resulting trace");
240
241 if (ret && babeltrace_output_fd >= 0) {
242 char *line;
243 size_t len = METADATA_LINE_SIZE;
244 FILE *babeltrace_output_fp = NULL;
245
246 babeltrace_output_fp = fdopen(babeltrace_output_fd, "r");
247 if (!babeltrace_output_fp) {
248 perror("fdopen on babeltrace_output_fd");
249 goto close_fp;
250 }
251 babeltrace_output_fd = -1;
252
253 line = malloc(len);
254 if (!line) {
255 diag("malloc error");
256 }
257 rewind(babeltrace_output_fp);
258 while (getline(&line, &len, babeltrace_output_fp) > 0) {
259 diag("%s", line);
260 }
261
262 free(line);
263 close_fp:
264 if (babeltrace_output_fp) {
265 if (fclose(babeltrace_output_fp)) {
266 diag("fclose error");
267 }
268 }
269 }
270
271 if (babeltrace_output_fd >= 0) {
272 if (close(babeltrace_output_fd)) {
273 diag("close error");
274 }
275 }
276 }
277
278 void append_simple_event(struct bt_ctf_stream_class *stream_class,
279 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
280 {
281 /* Create and add a simple event class */
282 struct bt_ctf_event_class *simple_event_class =
283 bt_ctf_event_class_create("Simple Event");
284 struct bt_ctf_field_type *uint_12_type =
285 bt_ctf_field_type_integer_create(12);
286 struct bt_ctf_field_type *int_64_type =
287 bt_ctf_field_type_integer_create(64);
288 struct bt_ctf_field_type *float_type =
289 bt_ctf_field_type_floating_point_create();
290 struct bt_ctf_field_type *enum_type;
291 struct bt_ctf_field_type *enum_type_unsigned =
292 bt_ctf_field_type_enumeration_create(uint_12_type);
293 struct bt_ctf_field_type *returned_type;
294 struct bt_ctf_event *simple_event;
295 struct bt_ctf_field *integer_field;
296 struct bt_ctf_field *float_field;
297 struct bt_ctf_field *enum_field;
298 struct bt_ctf_field *enum_field_unsigned;
299 struct bt_ctf_field *enum_container_field;
300 const char *mapping_name_test = "truie";
301 const double double_test_value = 3.1415;
302 struct bt_ctf_field *enum_container_field_unsigned;
303 const char *mapping_name_negative_test = "negative_value";
304 const char *ret_char;
305 double ret_double;
306 int64_t ret_range_start_int64_t, ret_range_end_int64_t;
307 uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t;
308 struct bt_ctf_clock *ret_clock;
309 struct bt_ctf_event_class *ret_event_class;
310 struct bt_ctf_field *packet_context;
311 struct bt_ctf_field *packet_context_field;
312
313 ok(uint_12_type, "Create an unsigned integer type");
314
315 bt_ctf_field_type_integer_set_signed(int_64_type, 1);
316 ok(int_64_type, "Create a signed integer type");
317 enum_type = bt_ctf_field_type_enumeration_create(int_64_type);
318
319 returned_type = bt_ctf_field_type_enumeration_get_container_type(enum_type);
320 ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
321 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
322 ok(!bt_ctf_field_type_enumeration_create(enum_type),
323 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
324
325 bt_ctf_field_type_set_alignment(float_type, 32);
326 ok(bt_ctf_field_type_get_alignment(NULL) < 0,
327 "bt_ctf_field_type_get_alignment handles NULL correctly");
328 ok(bt_ctf_field_type_get_alignment(float_type) == 32,
329 "bt_ctf_field_type_get_alignment returns a correct value");
330
331 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0,
332 "Set a floating point type's exponent digit count");
333 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0,
334 "Set a floating point type's mantissa digit count");
335
336 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL) < 0,
337 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
338 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL) < 0,
339 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
340 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11,
341 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
342 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53,
343 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
344
345 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
346 mapping_name_negative_test, -12345, 0) == 0,
347 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
348 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
349 "escaping; \"test\"", 1, 1) == 0,
350 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
351 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
352 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
353 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
354 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
355 "event clock int float", 5, 22) == 0,
356 "Accept enumeration mapping strings containing reserved keywords");
357 bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
358 42, 42);
359 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
360 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
361 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something",
362 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
363 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
364 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
365 bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000);
366
367 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL, -42) < 0,
368 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
369 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, 1000000) < 0,
370 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
371 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -55) == 1,
372 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
373
374 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
375 "enum_field") == 0, "Add signed enumeration field to event");
376
377 ok(bt_ctf_field_type_enumeration_get_mapping(NULL, 0, &ret_char,
378 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
379 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
380 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, NULL,
381 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
382 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
383 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
384 NULL, &ret_range_end_int64_t) < 0,
385 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
386 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
387 &ret_range_start_int64_t, NULL) < 0,
388 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
389 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 5, &ret_char,
390 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
391 "bt_ctf_field_type_enumeration_get_mapping returns a value");
392 ok(!strcmp(ret_char, mapping_name_test),
393 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
394 ok(ret_range_start_int64_t == 42,
395 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
396 ok(ret_range_end_int64_t == 42,
397 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
398
399 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
400 "escaping; \"test\"", 0, 0) == 0,
401 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
402 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
403 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
404 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
405 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
406 "event clock int float", 5, 22) == 0,
407 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
408 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
409 42, 42);
410 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
411 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
412 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something",
413 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
414 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
415 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
416 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned,
417 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
418
419 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL) < 0,
420 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
421 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 4,
422 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
423
424 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char,
425 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
426 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
427 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL,
428 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
429 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
430 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
431 NULL, &ret_range_end_uint64_t) < 0,
432 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
433 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
434 &ret_range_start_uint64_t, NULL) < 0,
435 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
436 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 3, &ret_char,
437 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
438 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
439 ok(!strcmp(ret_char, mapping_name_test),
440 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
441 ok(ret_range_start_uint64_t == 42,
442 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
443 ok(ret_range_end_uint64_t == 42,
444 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
445
446 bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
447 "integer_field");
448 bt_ctf_event_class_add_field(simple_event_class, float_type,
449 "float_field");
450 bt_ctf_stream_class_add_event_class(stream_class,
451 simple_event_class);
452
453 ok(bt_ctf_stream_class_get_event_class_count(NULL) < 0,
454 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
455 ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1,
456 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
457 ok(bt_ctf_stream_class_get_event_class(NULL, 0) == NULL,
458 "bt_ctf_stream_class_get_event_class handles NULL correctly");
459 ok(bt_ctf_stream_class_get_event_class(stream_class, 8724) == NULL,
460 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
461 ret_event_class = bt_ctf_stream_class_get_event_class(stream_class, 0);
462 ok(ret_event_class == simple_event_class,
463 "bt_ctf_stream_class_get_event_class returns the correct event class");
464 bt_ctf_event_class_put(ret_event_class);
465
466 ok(bt_ctf_stream_class_get_event_class_by_name(NULL, "some event name") == NULL,
467 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
468 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, NULL) == NULL,
469 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
470 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, "some event name") == NULL,
471 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
472 ret_event_class = bt_ctf_stream_class_get_event_class_by_name(stream_class, "Simple Event");
473 ok(ret_event_class == simple_event_class,
474 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
475 bt_ctf_event_class_put(ret_event_class);
476
477 simple_event = bt_ctf_event_create(simple_event_class);
478 ok(simple_event,
479 "Instantiate an event containing a single integer field");
480
481 ok(bt_ctf_event_get_clock(NULL) == NULL,
482 "bt_ctf_event_get_clock handles NULL correctly");
483 ret_clock = bt_ctf_event_get_clock(simple_event);
484 ok(ret_clock == clock,
485 "bt_ctf_event_get_clock returns a correct clock");
486 bt_ctf_clock_put(clock);
487
488 integer_field = bt_ctf_field_create(uint_12_type);
489 bt_ctf_field_unsigned_integer_set_value(integer_field, 42);
490 ok(bt_ctf_event_set_payload(simple_event, "integer_field",
491 integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
492
493 float_field = bt_ctf_event_get_payload(simple_event, "float_field");
494 ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double),
495 "bt_ctf_field_floating_point_get_value fails on an unset float field");
496 bt_ctf_field_floating_point_set_value(float_field, double_test_value);
497 ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double),
498 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
499 ok(bt_ctf_field_floating_point_get_value(float_field, NULL),
500 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
501 ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double),
502 "bt_ctf_field_floating_point_get_value returns a double value");
503 ok(fabs(ret_double - double_test_value) <= DBL_EPSILON,
504 "bt_ctf_field_floating_point_get_value returns a correct value");
505
506 enum_field = bt_ctf_field_create(enum_type);
507 ret_char = bt_ctf_field_enumeration_get_mapping_name(NULL);
508 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
509 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
510 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
511 enum_container_field = bt_ctf_field_enumeration_get_container(
512 enum_field);
513 ok(bt_ctf_field_signed_integer_set_value(
514 enum_container_field, -42) == 0,
515 "Set signed enumeration container value");
516 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
517 ok(!strcmp(ret_char, mapping_name_negative_test),
518 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
519 bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
520
521 enum_field_unsigned = bt_ctf_field_create(enum_type_unsigned);
522 enum_container_field_unsigned = bt_ctf_field_enumeration_get_container(
523 enum_field_unsigned);
524 ok(bt_ctf_field_unsigned_integer_set_value(
525 enum_container_field_unsigned, 42) == 0,
526 "Set unsigned enumeration container value");
527 bt_ctf_event_set_payload(simple_event, "enum_field_unsigned",
528 enum_field_unsigned);
529 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned);
530 ok(!strcmp(ret_char, mapping_name_test),
531 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
532
533 ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
534
535 ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
536 "Append simple event to trace stream");
537
538 ok(bt_ctf_stream_get_packet_context(NULL) == NULL,
539 "bt_ctf_stream_get_packet_context handles NULL correctly");
540 packet_context = bt_ctf_stream_get_packet_context(stream);
541 ok(packet_context,
542 "bt_ctf_stream_get_packet_context returns a packet context");
543
544 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
545 "packet_size");
546 ok(packet_context_field,
547 "Packet context contains the default packet_size field.");
548 bt_ctf_field_put(packet_context_field);
549 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
550 "custom_field");
551 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field, 8) == 0,
552 "Custom packet context field value successfully set.");
553
554 ok(bt_ctf_stream_set_packet_context(NULL, packet_context_field) < 0,
555 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
556 ok(bt_ctf_stream_set_packet_context(stream, NULL) < 0,
557 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
558 ok(bt_ctf_stream_set_packet_context(stream, packet_context) == 0,
559 "Successfully set a stream's packet context");
560
561 ok(bt_ctf_stream_flush(stream) == 0,
562 "Flush trace stream with one event");
563
564 bt_ctf_event_class_put(simple_event_class);
565 bt_ctf_event_put(simple_event);
566 bt_ctf_field_type_put(uint_12_type);
567 bt_ctf_field_type_put(int_64_type);
568 bt_ctf_field_type_put(float_type);
569 bt_ctf_field_type_put(enum_type);
570 bt_ctf_field_type_put(enum_type_unsigned);
571 bt_ctf_field_type_put(returned_type);
572 bt_ctf_field_put(integer_field);
573 bt_ctf_field_put(float_field);
574 bt_ctf_field_put(enum_field);
575 bt_ctf_field_put(enum_field_unsigned);
576 bt_ctf_field_put(enum_container_field);
577 bt_ctf_field_put(enum_container_field_unsigned);
578 bt_ctf_field_put(packet_context);
579 bt_ctf_field_put(packet_context_field);
580 }
581
582 void append_complex_event(struct bt_ctf_stream_class *stream_class,
583 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
584 {
585 int i;
586 const char *complex_test_event_string = "Complex Test Event";
587 const char *test_string = "Test string";
588 struct bt_ctf_field_type *uint_35_type =
589 bt_ctf_field_type_integer_create(35);
590 struct bt_ctf_field_type *int_16_type =
591 bt_ctf_field_type_integer_create(16);
592 struct bt_ctf_field_type *uint_3_type =
593 bt_ctf_field_type_integer_create(3);
594 struct bt_ctf_field_type *enum_variant_type =
595 bt_ctf_field_type_enumeration_create(uint_3_type);
596 struct bt_ctf_field_type *variant_type =
597 bt_ctf_field_type_variant_create(enum_variant_type,
598 "variant_selector");
599 struct bt_ctf_field_type *string_type =
600 bt_ctf_field_type_string_create();
601 struct bt_ctf_field_type *sequence_type;
602 struct bt_ctf_field_type *array_type;
603 struct bt_ctf_field_type *inner_structure_type =
604 bt_ctf_field_type_structure_create();
605 struct bt_ctf_field_type *complex_structure_type =
606 bt_ctf_field_type_structure_create();
607 struct bt_ctf_field_type *ret_field_type;
608 struct bt_ctf_event_class *event_class;
609 struct bt_ctf_event *event;
610 struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
611 *inner_structure_field, *complex_structure_field,
612 *a_sequence_field, *enum_variant_field, *enum_container_field,
613 *variant_field, *an_array_field, *ret_field;
614 uint64_t ret_unsigned_int;
615 int64_t ret_signed_int;
616 const char *ret_string;
617 struct bt_ctf_stream_class *ret_stream_class;
618 struct bt_ctf_event_class *ret_event_class;
619 struct bt_ctf_field *packet_context, *packet_context_field;
620
621 bt_ctf_field_type_set_alignment(int_16_type, 32);
622 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
623 bt_ctf_field_type_integer_set_base(uint_35_type,
624 BT_CTF_INTEGER_BASE_HEXADECIMAL);
625
626 array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH);
627 sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
628 "seq_len");
629
630 ok(bt_ctf_field_type_array_get_element_type(NULL) == NULL,
631 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
632 ret_field_type = bt_ctf_field_type_array_get_element_type(
633 array_type);
634 ok(ret_field_type == int_16_type,
635 "bt_ctf_field_type_array_get_element_type returns the correct type");
636 bt_ctf_field_type_put(ret_field_type);
637
638 ok(bt_ctf_field_type_array_get_length(NULL) < 0,
639 "bt_ctf_field_type_array_get_length handles NULL correctly");
640 ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH,
641 "bt_ctf_field_type_array_get_length returns the correct length");
642
643 bt_ctf_field_type_structure_add_field(inner_structure_type,
644 uint_35_type, "seq_len");
645 bt_ctf_field_type_structure_add_field(inner_structure_type,
646 sequence_type, "a_sequence");
647 bt_ctf_field_type_structure_add_field(inner_structure_type,
648 array_type, "an_array");
649
650 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
651 "UINT3_TYPE", 0, 0);
652 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
653 "INT16_TYPE", 1, 1);
654 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
655 "UINT35_TYPE", 2, 7);
656
657 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL,
658 "INT16_TYPE") < 0,
659 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
660 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
661 enum_variant_type, NULL) < 0,
662 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
663 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
664 enum_variant_type, "INT16_TYPE") == 1,
665 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
666
667 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL, 1) < 0,
668 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
669 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, -42) < 0,
670 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
671 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 5) == 2,
672 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
673
674 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
675 "An unknown entry"), "Reject a variant field based on an unknown tag value");
676 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
677 "UINT3_TYPE") == 0, "Add a field to a variant");
678 bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
679 "INT16_TYPE");
680 bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
681 "UINT35_TYPE");
682
683 ok(bt_ctf_field_type_variant_get_tag_type(NULL) == NULL,
684 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
685 ret_field_type = bt_ctf_field_type_variant_get_tag_type(variant_type);
686 ok(ret_field_type == enum_variant_type,
687 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
688 bt_ctf_field_type_put(ret_field_type);
689
690 ok(bt_ctf_field_type_variant_get_tag_name(NULL) == NULL,
691 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
692 ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type);
693 ok(!strcmp(ret_string, "variant_selector"),
694 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
695 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL,
696 "INT16_TYPE") == NULL,
697 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
698 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type,
699 NULL) == NULL,
700 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
701 ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name(
702 variant_type, "INT16_TYPE");
703 ok(ret_field_type == int_16_type,
704 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
705 bt_ctf_field_type_put(ret_field_type);
706
707 ok(bt_ctf_field_type_variant_get_field_count(NULL) < 0,
708 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
709 ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3,
710 "bt_ctf_field_type_variant_get_field_count returns the correct count");
711
712 ok(bt_ctf_field_type_variant_get_field(NULL, &ret_string, &ret_field_type, 0) < 0,
713 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
714 ok(bt_ctf_field_type_variant_get_field(variant_type, NULL, &ret_field_type, 0) < 0,
715 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
716 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, NULL, 0) < 0,
717 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
718 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 200) < 0,
719 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
720 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 1) == 0,
721 "bt_ctf_field_type_variant_get_field returns a field");
722 ok(!strcmp("INT16_TYPE", ret_string),
723 "bt_ctf_field_type_variant_get_field returns a correct field name");
724 ok(ret_field_type == int_16_type,
725 "bt_ctf_field_type_variant_get_field returns a correct field type");
726 bt_ctf_field_type_put(ret_field_type);
727
728 bt_ctf_field_type_structure_add_field(complex_structure_type,
729 enum_variant_type, "variant_selector");
730 bt_ctf_field_type_structure_add_field(complex_structure_type,
731 string_type, "a_string");
732 bt_ctf_field_type_structure_add_field(complex_structure_type,
733 variant_type, "variant_value");
734 bt_ctf_field_type_structure_add_field(complex_structure_type,
735 inner_structure_type, "inner_structure");
736
737 ok(bt_ctf_event_class_create("clock") == NULL,
738 "Reject creation of an event class with an illegal name");
739 event_class = bt_ctf_event_class_create(complex_test_event_string);
740 ok(event_class, "Create an event class");
741 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
742 "Reject addition of a field with an empty name to an event");
743 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
744 "Reject addition of a field with a NULL type to an event");
745 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
746 "int"),
747 "Reject addition of a type with an illegal name to an event");
748 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
749 "uint_35") == 0,
750 "Add field of type unsigned integer to an event");
751 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
752 "int_16") == 0, "Add field of type signed integer to an event");
753 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
754 "complex_structure") == 0,
755 "Add composite structure to an event");
756
757 ok(bt_ctf_event_class_get_name(NULL) == NULL,
758 "bt_ctf_event_class_get_name handles NULL correctly");
759 ret_string = bt_ctf_event_class_get_name(event_class);
760 ok(!strcmp(ret_string, complex_test_event_string),
761 "bt_ctf_event_class_get_name returns a correct name");
762 ok(bt_ctf_event_class_get_id(event_class) < 0,
763 "bt_ctf_event_class_get_id returns a negative value when not set");
764 ok(bt_ctf_event_class_get_id(NULL) < 0,
765 "bt_ctf_event_class_get_id handles NULL correctly");
766 ok(bt_ctf_event_class_set_id(NULL, 42) < 0,
767 "bt_ctf_event_class_set_id handles NULL correctly");
768 ok(bt_ctf_event_class_set_id(event_class, 42) == 0,
769 "Set an event class' id");
770 ok(bt_ctf_event_class_get_id(event_class) == 42,
771 "bt_ctf_event_class_get_id returns the correct value");
772
773 /* Add event class to the stream class */
774 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
775 "Reject addition of NULL event class to a stream class");
776 ok(bt_ctf_stream_class_add_event_class(stream_class,
777 event_class) == 0, "Add an event class to stream class");
778
779 ok(bt_ctf_event_class_get_stream_class(NULL) == NULL,
780 "bt_ctf_event_class_get_stream_class handles NULL correctly");
781 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
782 ok(ret_stream_class == stream_class,
783 "bt_ctf_event_class_get_stream_class returns the correct stream class");
784 bt_ctf_stream_class_put(ret_stream_class);
785
786 ok(bt_ctf_event_class_get_field_count(NULL) < 0,
787 "bt_ctf_event_class_get_field_count handles NULL correctly");
788 ok(bt_ctf_event_class_get_field_count(event_class) == 3,
789 "bt_ctf_event_class_get_field_count returns a correct value");
790
791 ok(bt_ctf_event_class_get_field(NULL, &ret_string,
792 &ret_field_type, 0) < 0,
793 "bt_ctf_event_class_get_field handles a NULL event class correctly");
794 ok(bt_ctf_event_class_get_field(event_class, NULL,
795 &ret_field_type, 0) < 0,
796 "bt_ctf_event_class_get_field handles a NULL field name correctly");
797 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
798 NULL, 0) < 0,
799 "bt_ctf_event_class_get_field handles a NULL field type correctly");
800 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
801 &ret_field_type, 42) < 0,
802 "bt_ctf_event_class_get_field handles an invalid index correctly");
803 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
804 &ret_field_type, 0) == 0,
805 "bt_ctf_event_class_get_field returns a field");
806 ok(ret_field_type == uint_35_type,
807 "bt_ctf_event_class_get_field returns a correct field type");
808 bt_ctf_field_type_put(ret_field_type);
809 ok(!strcmp(ret_string, "uint_35"),
810 "bt_ctf_event_class_get_field returns a correct field name");
811 ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL,
812 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
813 ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL,
814 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
815 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
816 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
817 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
818 "complex_structure");
819 ok(ret_field_type == complex_structure_type,
820 "bt_ctf_event_class_get_field_by_name returns a correct field type");
821 bt_ctf_field_type_put(ret_field_type);
822
823 event = bt_ctf_event_create(event_class);
824 ok(event, "Instanciate a complex event");
825
826 ok(bt_ctf_event_get_class(NULL) == NULL,
827 "bt_ctf_event_get_class handles NULL correctly");
828 ret_event_class = bt_ctf_event_get_class(event);
829 ok(ret_event_class == event_class,
830 "bt_ctf_event_get_class returns the correct event class");
831 bt_ctf_event_class_put(ret_event_class);
832
833 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
834 if (!uint_35_field) {
835 printf("uint_35_field is NULL\n");
836 }
837
838 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
839 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
840 ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0,
841 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
842 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0,
843 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
844 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
845 &ret_unsigned_int) == 0,
846 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
847 ok(ret_unsigned_int == 0x0DDF00D,
848 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
849 ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
850 &ret_signed_int) < 0,
851 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
852 bt_ctf_field_put(uint_35_field);
853
854 int_16_field = bt_ctf_event_get_payload(event, "int_16");
855 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
856 ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0,
857 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
858 ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0,
859 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
860 ok(bt_ctf_field_signed_integer_get_value(int_16_field,
861 &ret_signed_int) == 0,
862 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
863 ok(ret_signed_int == -12345,
864 "bt_ctf_field_signed_integer_get_value returns the correct value");
865 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
866 &ret_unsigned_int) < 0,
867 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
868 bt_ctf_field_put(int_16_field);
869
870 complex_structure_field = bt_ctf_event_get_payload(event,
871 "complex_structure");
872
873 ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
874 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
875 ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
876 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
877 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
878 complex_structure_field, 3);
879 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
880 bt_ctf_field_put(inner_structure_field);
881 ok(ret_field_type == inner_structure_type,
882 "bt_ctf_field_structure_get_field_by_index returns a correct field");
883 bt_ctf_field_type_put(ret_field_type);
884
885 inner_structure_field = bt_ctf_field_structure_get_field(
886 complex_structure_field, "inner_structure");
887 a_string_field = bt_ctf_field_structure_get_field(
888 complex_structure_field, "a_string");
889 enum_variant_field = bt_ctf_field_structure_get_field(
890 complex_structure_field, "variant_selector");
891 variant_field = bt_ctf_field_structure_get_field(
892 complex_structure_field, "variant_value");
893 uint_35_field = bt_ctf_field_structure_get_field(
894 inner_structure_field, "seq_len");
895 a_sequence_field = bt_ctf_field_structure_get_field(
896 inner_structure_field, "a_sequence");
897 an_array_field = bt_ctf_field_structure_get_field(
898 inner_structure_field, "an_array");
899
900 enum_container_field = bt_ctf_field_enumeration_get_container(
901 enum_variant_field);
902 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
903 int_16_field = bt_ctf_field_variant_get_field(variant_field,
904 enum_variant_field);
905 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
906 bt_ctf_field_put(int_16_field);
907 ok(!bt_ctf_field_string_get_value(a_string_field),
908 "bt_ctf_field_string_get_value returns NULL on an unset field");
909 bt_ctf_field_string_set_value(a_string_field,
910 test_string);
911 ok(!bt_ctf_field_string_get_value(NULL),
912 "bt_ctf_field_string_get_value correctly handles NULL");
913 ret_string = bt_ctf_field_string_get_value(a_string_field);
914 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
915 ok(ret_string ? !strcmp(ret_string, test_string) : 0,
916 "bt_ctf_field_string_get_value returns a correct value");
917 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
918 SEQUENCE_TEST_LENGTH);
919
920 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL,
921 enum_container_field) == NULL,
922 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
923 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type,
924 NULL) == NULL,
925 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
926 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
927 variant_type, enum_variant_field);
928 ok(ret_field_type == int_16_type,
929 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
930
931 ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
932 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
933 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
934 uint_35_field) == 0, "Set a sequence field's length");
935 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
936 ok(ret_field == uint_35_field,
937 "bt_ctf_field_sequence_get_length returns the correct length field");
938 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
939 "bt_ctf_field_sequence_get_length properly handles NULL");
940
941 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
942 int_16_field = bt_ctf_field_sequence_get_field(
943 a_sequence_field, i);
944 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
945 bt_ctf_field_put(int_16_field);
946 }
947
948 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
949 int_16_field = bt_ctf_field_array_get_field(
950 an_array_field, i);
951 bt_ctf_field_signed_integer_set_value(int_16_field, i);
952 bt_ctf_field_put(int_16_field);
953 }
954
955 bt_ctf_clock_set_time(clock, ++current_time);
956 ok(bt_ctf_stream_append_event(stream, event) == 0,
957 "Append a complex event to a stream");
958
959 /*
960 * Populate the custom packet context field with a dummy value
961 * otherwise flush will fail.
962 */
963 packet_context = bt_ctf_stream_get_packet_context(stream);
964 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
965 "custom_field");
966 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1);
967
968 ok(bt_ctf_stream_flush(stream) == 0,
969 "Flush a stream containing a complex event");
970
971 bt_ctf_field_put(uint_35_field);
972 bt_ctf_field_put(a_string_field);
973 bt_ctf_field_put(inner_structure_field);
974 bt_ctf_field_put(complex_structure_field);
975 bt_ctf_field_put(a_sequence_field);
976 bt_ctf_field_put(an_array_field);
977 bt_ctf_field_put(enum_variant_field);
978 bt_ctf_field_put(enum_container_field);
979 bt_ctf_field_put(variant_field);
980 bt_ctf_field_put(ret_field);
981 bt_ctf_field_put(packet_context_field);
982 bt_ctf_field_put(packet_context);
983 bt_ctf_field_type_put(uint_35_type);
984 bt_ctf_field_type_put(int_16_type);
985 bt_ctf_field_type_put(string_type);
986 bt_ctf_field_type_put(sequence_type);
987 bt_ctf_field_type_put(array_type);
988 bt_ctf_field_type_put(inner_structure_type);
989 bt_ctf_field_type_put(complex_structure_type);
990 bt_ctf_field_type_put(uint_3_type);
991 bt_ctf_field_type_put(enum_variant_type);
992 bt_ctf_field_type_put(variant_type);
993 bt_ctf_field_type_put(ret_field_type);
994 bt_ctf_event_class_put(event_class);
995 bt_ctf_event_put(event);
996 }
997
998 void type_field_tests()
999 {
1000 struct bt_ctf_field *uint_12;
1001 struct bt_ctf_field *int_16;
1002 struct bt_ctf_field *string;
1003 struct bt_ctf_field *enumeration;
1004 struct bt_ctf_field_type *composite_structure_type;
1005 struct bt_ctf_field_type *structure_seq_type;
1006 struct bt_ctf_field_type *string_type;
1007 struct bt_ctf_field_type *sequence_type;
1008 struct bt_ctf_field_type *uint_8_type;
1009 struct bt_ctf_field_type *int_16_type;
1010 struct bt_ctf_field_type *uint_12_type =
1011 bt_ctf_field_type_integer_create(12);
1012 struct bt_ctf_field_type *enumeration_type;
1013 struct bt_ctf_field_type *enumeration_sequence_type;
1014 struct bt_ctf_field_type *enumeration_array_type;
1015 struct bt_ctf_field_type *returned_type;
1016 const char *ret_string;
1017
1018 returned_type = bt_ctf_field_get_type(NULL);
1019 ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
1020
1021 ok(uint_12_type, "Create an unsigned integer type");
1022 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1023 BT_CTF_INTEGER_BASE_BINARY) == 0,
1024 "Set integer type's base as binary");
1025 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1026 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
1027 "Set integer type's base as decimal");
1028 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1029 BT_CTF_INTEGER_BASE_UNKNOWN),
1030 "Reject integer type's base set as unknown");
1031 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1032 BT_CTF_INTEGER_BASE_OCTAL) == 0,
1033 "Set integer type's base as octal");
1034 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1035 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
1036 "Set integer type's base as hexadecimal");
1037 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
1038 "Reject unknown integer base value");
1039 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
1040 "Set integer type signedness to signed");
1041 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
1042 "Set integer type signedness to unsigned");
1043 ok(bt_ctf_field_type_integer_get_size(NULL) < 0,
1044 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1045 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
1046 "bt_ctf_field_type_integer_get_size returns a correct value");
1047 ok(bt_ctf_field_type_integer_get_signed(NULL) < 0,
1048 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1049 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
1050 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1051
1052 ok(bt_ctf_field_type_set_byte_order(NULL,
1053 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
1054 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1055 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1056 (enum bt_ctf_byte_order) 42) < 0,
1057 "bt_ctf_field_type_set_byte_order rejects invalid values");
1058 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1059 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
1060 "Set an integer's byte order to little endian");
1061 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1062 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1063 "Set an integer's byte order to big endian");
1064 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
1065 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1066 "bt_ctf_field_type_get_byte_order returns a correct value");
1067 ok(bt_ctf_field_type_get_byte_order(NULL) ==
1068 BT_CTF_BYTE_ORDER_UNKNOWN,
1069 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1070
1071 ok(bt_ctf_field_type_get_type_id(NULL) ==
1072 CTF_TYPE_UNKNOWN,
1073 "bt_ctf_field_type_get_type_id handles NULL correctly");
1074 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
1075 CTF_TYPE_INTEGER,
1076 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1077
1078 ok(bt_ctf_field_type_integer_get_base(NULL) ==
1079 BT_CTF_INTEGER_BASE_UNKNOWN,
1080 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1081 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
1082 BT_CTF_INTEGER_BASE_HEXADECIMAL,
1083 "bt_ctf_field_type_integer_get_base returns a correct value");
1084
1085 ok(bt_ctf_field_type_integer_set_encoding(NULL, CTF_STRING_ASCII) < 0,
1086 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1087 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1088 (enum ctf_string_encoding) 123) < 0,
1089 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1090 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1091 CTF_STRING_UTF8) == 0,
1092 "Set integer type encoding to UTF8");
1093 ok(bt_ctf_field_type_integer_get_encoding(NULL) == CTF_STRING_UNKNOWN,
1094 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1095 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) == CTF_STRING_UTF8,
1096 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1097
1098 int_16_type = bt_ctf_field_type_integer_create(16);
1099 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
1100 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
1101 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1102 uint_8_type = bt_ctf_field_type_integer_create(8);
1103 sequence_type =
1104 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
1105 ok(sequence_type, "Create a sequence of int16_t type");
1106 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
1107 CTF_TYPE_SEQUENCE,
1108 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1109
1110 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL,
1111 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1112 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
1113 sequence_type);
1114 ok(!strcmp(ret_string, "seq_len"),
1115 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1116 ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL,
1117 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1118 returned_type = bt_ctf_field_type_sequence_get_element_type(
1119 sequence_type);
1120 ok(returned_type == int_16_type,
1121 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1122 bt_ctf_field_type_put(returned_type);
1123
1124 string_type = bt_ctf_field_type_string_create();
1125 ok(string_type, "Create a string type");
1126 ok(bt_ctf_field_type_string_set_encoding(string_type,
1127 CTF_STRING_NONE),
1128 "Reject invalid \"None\" string encoding");
1129 ok(bt_ctf_field_type_string_set_encoding(string_type,
1130 42),
1131 "Reject invalid string encoding");
1132 ok(bt_ctf_field_type_string_set_encoding(string_type,
1133 CTF_STRING_ASCII) == 0,
1134 "Set string encoding to ASCII");
1135
1136 ok(bt_ctf_field_type_string_get_encoding(NULL) ==
1137 CTF_STRING_UNKNOWN,
1138 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1139 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
1140 CTF_STRING_ASCII,
1141 "bt_ctf_field_type_string_get_encoding returns the correct value");
1142
1143 structure_seq_type = bt_ctf_field_type_structure_create();
1144 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
1145 CTF_TYPE_STRUCT,
1146 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1147 ok(structure_seq_type, "Create a structure type");
1148 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1149 uint_8_type, "seq_len") == 0,
1150 "Add a uint8_t type to a structure");
1151 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1152 sequence_type, "a_sequence") == 0,
1153 "Add a sequence type to a structure");
1154
1155 ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0,
1156 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1157 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
1158 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1159
1160 ok(bt_ctf_field_type_structure_get_field(NULL,
1161 &ret_string, &returned_type, 1) < 0,
1162 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1163 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1164 NULL, &returned_type, 1) < 0,
1165 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1166 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1167 &ret_string, NULL, 1) < 0,
1168 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1169 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1170 &ret_string, &returned_type, 10) < 0,
1171 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1172 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1173 &ret_string, &returned_type, 1) == 0,
1174 "bt_ctf_field_type_structure_get_field returns a field");
1175 ok(!strcmp(ret_string, "a_sequence"),
1176 "bt_ctf_field_type_structure_get_field returns a correct field name");
1177 ok(returned_type == sequence_type,
1178 "bt_ctf_field_type_structure_get_field returns a correct field type");
1179 bt_ctf_field_type_put(returned_type);
1180
1181 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL,
1182 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1183 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL,
1184 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1185 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1186 structure_seq_type, "a_sequence");
1187 ok(returned_type == sequence_type,
1188 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1189 bt_ctf_field_type_put(returned_type);
1190
1191 composite_structure_type = bt_ctf_field_type_structure_create();
1192 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1193 string_type, "a_string") == 0,
1194 "Add a string type to a structure");
1195 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1196 structure_seq_type, "inner_structure") == 0,
1197 "Add a structure type to a structure");
1198
1199 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1200 NULL, "a_sequence") == NULL,
1201 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1202 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1203 structure_seq_type, NULL) == NULL,
1204 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1205 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1206 structure_seq_type, "a_sequence");
1207 ok(returned_type == sequence_type,
1208 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1209 bt_ctf_field_type_put(returned_type);
1210
1211 int_16 = bt_ctf_field_create(int_16_type);
1212 ok(int_16, "Instanciate a signed 16-bit integer");
1213 uint_12 = bt_ctf_field_create(uint_12_type);
1214 ok(uint_12, "Instanciate an unsigned 12-bit integer");
1215 returned_type = bt_ctf_field_get_type(int_16);
1216 ok(returned_type == int_16_type,
1217 "bt_ctf_field_get_type returns the correct type");
1218
1219 /* Can't modify types after instanciating them */
1220 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1221 BT_CTF_INTEGER_BASE_DECIMAL),
1222 "Check an integer type' base can't be modified after instanciation");
1223 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
1224 "Check an integer type's signedness can't be modified after instanciation");
1225
1226 /* Check signed property is checked */
1227 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
1228 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1229 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
1230 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1231
1232 /* Check overflows are properly tested for */
1233 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
1234 "Check -32768 is allowed for a signed 16-bit integer");
1235 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
1236 "Check 32767 is allowed for a signed 16-bit integer");
1237 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
1238 "Check 32768 is not allowed for a signed 16-bit integer");
1239 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
1240 "Check -32769 is not allowed for a signed 16-bit integer");
1241 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
1242 "Check -42 is allowed for a signed 16-bit integer");
1243
1244 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
1245 "Check 4095 is allowed for an unsigned 12-bit integer");
1246 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
1247 "Check 4096 is not allowed for a unsigned 12-bit integer");
1248 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
1249 "Check 0 is allowed for an unsigned 12-bit integer");
1250
1251 string = bt_ctf_field_create(string_type);
1252 ok(string, "Instanciate a string field");
1253 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
1254 "Set a string's value");
1255
1256 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
1257 ok(enumeration_type,
1258 "Create an enumeration type with an unsigned 12-bit integer as container");
1259 enumeration_sequence_type = bt_ctf_field_type_sequence_create(
1260 enumeration_type, "count");
1261 ok(!enumeration_sequence_type,
1262 "Check enumeration types are validated when creating a sequence");
1263 enumeration_array_type = bt_ctf_field_type_array_create(
1264 enumeration_type, 10);
1265 ok(!enumeration_array_type,
1266 "Check enumeration types are validated when creating an array");
1267 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1268 enumeration_type, "enumeration"),
1269 "Check enumeration types are validated when adding them as structure members");
1270 enumeration = bt_ctf_field_create(enumeration_type);
1271 ok(!enumeration,
1272 "Check enumeration types are validated before instantiation");
1273
1274 bt_ctf_field_put(string);
1275 bt_ctf_field_put(uint_12);
1276 bt_ctf_field_put(int_16);
1277 bt_ctf_field_put(enumeration);
1278 bt_ctf_field_type_put(composite_structure_type);
1279 bt_ctf_field_type_put(structure_seq_type);
1280 bt_ctf_field_type_put(string_type);
1281 bt_ctf_field_type_put(sequence_type);
1282 bt_ctf_field_type_put(uint_8_type);
1283 bt_ctf_field_type_put(int_16_type);
1284 bt_ctf_field_type_put(uint_12_type);
1285 bt_ctf_field_type_put(enumeration_type);
1286 bt_ctf_field_type_put(enumeration_sequence_type);
1287 bt_ctf_field_type_put(enumeration_array_type);
1288 bt_ctf_field_type_put(returned_type);
1289 }
1290
1291 void packet_resize_test(struct bt_ctf_stream_class *stream_class,
1292 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
1293 {
1294 /*
1295 * Append enough events to force the underlying packet to be resized.
1296 * Also tests that a new event can be declared after a stream has been
1297 * instantiated and used/flushed.
1298 */
1299 int ret = 0;
1300 int i;
1301 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
1302 "Spammy_Event");
1303 struct bt_ctf_field_type *integer_type =
1304 bt_ctf_field_type_integer_create(17);
1305 struct bt_ctf_field_type *string_type =
1306 bt_ctf_field_type_string_create();
1307 struct bt_ctf_event *event;
1308 struct bt_ctf_field *ret_field;
1309 struct bt_ctf_field_type *ret_field_type;
1310 uint64_t ret_uint64;
1311 int events_appended = 0;
1312 struct bt_ctf_field *packet_context, *packet_context_field;
1313
1314 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
1315 "field_1");
1316 ret |= bt_ctf_event_class_add_field(event_class, string_type,
1317 "a_string");
1318 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
1319 ok(ret == 0, "Add a new event class to a stream class after writing an event");
1320 if (ret) {
1321 goto end;
1322 }
1323
1324 event = bt_ctf_event_create(event_class);
1325 ret_field = bt_ctf_event_get_payload_by_index(event, 0);
1326 ret_field_type = bt_ctf_field_get_type(ret_field);
1327 ok(ret_field_type == integer_type,
1328 "bt_ctf_event_get_payload_by_index returns a correct field");
1329 bt_ctf_field_type_put(ret_field_type);
1330 bt_ctf_field_put(ret_field);
1331
1332 ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL,
1333 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1334 ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL,
1335 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1336 bt_ctf_event_put(event);
1337
1338 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
1339 event = bt_ctf_event_create(event_class);
1340 struct bt_ctf_field *integer =
1341 bt_ctf_field_create(integer_type);
1342 struct bt_ctf_field *string =
1343 bt_ctf_field_create(string_type);
1344
1345 ret |= bt_ctf_clock_set_time(clock, ++current_time);
1346 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
1347 ret |= bt_ctf_event_set_payload(event, "field_1",
1348 integer);
1349 bt_ctf_field_put(integer);
1350 ret |= bt_ctf_field_string_set_value(string, "This is a test");
1351 ret |= bt_ctf_event_set_payload(event, "a_string",
1352 string);
1353 bt_ctf_field_put(string);
1354 ret |= bt_ctf_stream_append_event(stream, event);
1355 bt_ctf_event_put(event);
1356
1357 if (ret) {
1358 break;
1359 }
1360 }
1361
1362 events_appended = 1;
1363 ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0,
1364 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
1365 ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0,
1366 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
1367 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1368 ok(ret == 0 && ret_uint64 == 0,
1369 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
1370 bt_ctf_stream_append_discarded_events(stream, 1000);
1371 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1372 ok(ret == 0 && ret_uint64 == 1000,
1373 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
1374
1375 end:
1376 ok(events_appended, "Append 100 000 events to a stream");
1377
1378 /*
1379 * Populate the custom packet context field with a dummy value
1380 * otherwise flush will fail.
1381 */
1382 packet_context = bt_ctf_stream_get_packet_context(stream);
1383 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
1384 "custom_field");
1385 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2);
1386
1387 ok(bt_ctf_stream_flush(stream) == 0,
1388 "Flush a stream that forces a packet resize");
1389 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1390 ok(ret == 0 && ret_uint64 == 1000,
1391 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
1392 bt_ctf_field_type_put(integer_type);
1393 bt_ctf_field_type_put(string_type);
1394 bt_ctf_field_put(packet_context);
1395 bt_ctf_field_put(packet_context_field);
1396 bt_ctf_event_class_put(event_class);
1397 }
1398
1399 int main(int argc, char **argv)
1400 {
1401 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
1402 char metadata_path[sizeof(trace_path) + 9];
1403 const char *clock_name = "test_clock";
1404 const char *clock_description = "This is a test clock";
1405 const char *returned_clock_name;
1406 const char *returned_clock_description;
1407 const uint64_t frequency = 1123456789;
1408 const uint64_t offset_s = 1351530929945824323;
1409 const uint64_t offset = 1234567;
1410 const uint64_t precision = 10;
1411 const int is_absolute = 0xFF;
1412 char *metadata_string;
1413 struct bt_ctf_writer *writer;
1414 struct utsname name;
1415 char hostname[BABELTRACE_HOST_NAME_MAX];
1416 struct bt_ctf_clock *clock, *ret_clock;
1417 struct bt_ctf_stream_class *stream_class;
1418 struct bt_ctf_stream *stream1;
1419 const char *ret_string;
1420 const unsigned char *ret_uuid;
1421 unsigned char tmp_uuid[16] = { 0 };
1422 struct bt_ctf_field_type *packet_context_type, *packet_context_field_type;
1423 struct bt_ctf_trace *trace;
1424 int ret;
1425
1426 if (argc < 3) {
1427 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
1428 return -1;
1429 }
1430
1431 plan_no_plan();
1432
1433 if (!mkdtemp(trace_path)) {
1434 perror("# perror");
1435 }
1436
1437 strcpy(metadata_path, trace_path);
1438 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
1439
1440 writer = bt_ctf_writer_create(trace_path);
1441 ok(writer, "bt_ctf_create succeeds in creating trace with path");
1442
1443 /* Add environment context to the trace */
1444 ret = gethostname(hostname, sizeof(hostname));
1445 if (ret < 0) {
1446 return ret;
1447 }
1448 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
1449 "Add host (%s) environment field to writer instance",
1450 hostname);
1451 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
1452 "test_value"),
1453 "bt_ctf_writer_add_environment_field error with NULL writer");
1454 ok(bt_ctf_writer_add_environment_field(writer, NULL,
1455 "test_value"),
1456 "bt_ctf_writer_add_environment_field error with NULL field name");
1457 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
1458 NULL),
1459 "bt_ctf_writer_add_environment_field error with NULL field value");
1460
1461 if (uname(&name)) {
1462 perror("uname");
1463 return -1;
1464 }
1465
1466 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
1467 == 0, "Add sysname (%s) environment field to writer instance",
1468 name.sysname);
1469 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
1470 name.nodename) == 0,
1471 "Add nodename (%s) environment field to writer instance",
1472 name.nodename);
1473 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
1474 == 0, "Add release (%s) environment field to writer instance",
1475 name.release);
1476 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
1477 == 0, "Add version (%s) environment field to writer instance",
1478 name.version);
1479 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
1480 == 0, "Add machine (%s) environment field to writer istance",
1481 name.machine);
1482
1483 /* Define a clock and add it to the trace */
1484 ok(bt_ctf_clock_create("signed") == NULL,
1485 "Illegal clock name rejected");
1486 ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
1487 clock = bt_ctf_clock_create(clock_name);
1488 ok(clock, "Clock created sucessfully");
1489 returned_clock_name = bt_ctf_clock_get_name(clock);
1490 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
1491 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
1492 "Returned clock name is valid");
1493
1494 returned_clock_description = bt_ctf_clock_get_description(clock);
1495 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
1496 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
1497 "Clock description set successfully");
1498
1499 returned_clock_description = bt_ctf_clock_get_description(clock);
1500 ok(returned_clock_description,
1501 "bt_ctf_clock_get_description returns a description.");
1502 ok(returned_clock_description ?
1503 !strcmp(returned_clock_description, clock_description) : 0,
1504 "Returned clock description is valid");
1505
1506 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
1507 "bt_ctf_clock_get_frequency returns the correct default frequency");
1508 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
1509 "Set clock frequency");
1510 ok(bt_ctf_clock_get_frequency(clock) == frequency,
1511 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
1512
1513 ok(bt_ctf_clock_get_offset_s(clock) == DEFAULT_CLOCK_OFFSET_S,
1514 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
1515 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
1516 "Set clock offset (seconds)");
1517 ok(bt_ctf_clock_get_offset_s(clock) == offset_s,
1518 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
1519
1520 ok(bt_ctf_clock_get_offset(clock) == DEFAULT_CLOCK_OFFSET,
1521 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
1522 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
1523 ok(bt_ctf_clock_get_offset(clock) == offset,
1524 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
1525
1526 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
1527 "bt_ctf_clock_get_precision returns the correct default precision");
1528 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
1529 "Set clock precision");
1530 ok(bt_ctf_clock_get_precision(clock) == precision,
1531 "bt_ctf_clock_get_precision returns the correct precision once it is set");
1532
1533 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
1534 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
1535 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
1536 "Set clock absolute property");
1537 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
1538 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
1539
1540 ok(bt_ctf_clock_get_time(clock) == DEFAULT_CLOCK_TIME,
1541 "bt_ctf_clock_get_time returns the correct default time");
1542 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
1543 "Set clock time");
1544 ok(bt_ctf_clock_get_time(clock) == current_time,
1545 "bt_ctf_clock_get_time returns the correct time once it is set");
1546
1547 ok(bt_ctf_writer_add_clock(writer, clock) == 0,
1548 "Add clock to writer instance");
1549 ok(bt_ctf_writer_add_clock(writer, clock),
1550 "Verify a clock can't be added twice to a writer instance");
1551
1552 ok(!bt_ctf_writer_get_trace(NULL),
1553 "bt_ctf_writer_get_trace correctly handles NULL");
1554 trace = bt_ctf_writer_get_trace(writer);
1555 ok(trace,
1556 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
1557 ok(bt_ctf_trace_get_clock_count(NULL) < 0,
1558 "bt_ctf_trace_get_clock_count correctly handles NULL");
1559 ok(bt_ctf_trace_get_clock_count(trace) == 1,
1560 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
1561 ok(!bt_ctf_trace_get_clock(NULL, 0),
1562 "bt_ctf_trace_get_clock correctly handles NULL");
1563 ok(!bt_ctf_trace_get_clock(trace, -1),
1564 "bt_ctf_trace_get_clock correctly handles negative indexes");
1565 ok(!bt_ctf_trace_get_clock(trace, 1),
1566 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
1567 ret_clock = bt_ctf_trace_get_clock(trace, 0);
1568 ok(ret_clock == clock,
1569 "bt_ctf_trace_get_clock returns the right clock instance");
1570 bt_ctf_clock_put(ret_clock);
1571
1572 ok(!bt_ctf_clock_get_name(NULL),
1573 "bt_ctf_clock_get_name correctly handles NULL");
1574 ok(!bt_ctf_clock_get_description(NULL),
1575 "bt_ctf_clock_get_description correctly handles NULL");
1576 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
1577 "bt_ctf_clock_get_frequency correctly handles NULL");
1578 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
1579 "bt_ctf_clock_get_precision correctly handles NULL");
1580 ok(bt_ctf_clock_get_offset_s(NULL) == -1ULL,
1581 "bt_ctf_clock_get_offset_s correctly handles NULL");
1582 ok(bt_ctf_clock_get_offset(NULL) == -1ULL,
1583 "bt_ctf_clock_get_offset correctly handles NULL");
1584 ok(bt_ctf_clock_get_is_absolute(NULL) < 0,
1585 "bt_ctf_clock_get_is_absolute correctly handles NULL");
1586 ok(bt_ctf_clock_get_time(NULL) == -1ULL,
1587 "bt_ctf_clock_get_time correctly handles NULL");
1588
1589 ok(bt_ctf_clock_set_description(NULL, NULL) < 0,
1590 "bt_ctf_clock_set_description correctly handles NULL clock");
1591 ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0,
1592 "bt_ctf_clock_set_frequency correctly handles NULL clock");
1593 ok(bt_ctf_clock_set_precision(NULL, precision) < 0,
1594 "bt_ctf_clock_get_precision correctly handles NULL clock");
1595 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0,
1596 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
1597 ok(bt_ctf_clock_set_offset(NULL, offset) < 0,
1598 "bt_ctf_clock_set_offset correctly handles NULL clock");
1599 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0,
1600 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
1601 ok(bt_ctf_clock_set_time(NULL, current_time) < 0,
1602 "bt_ctf_clock_set_time correctly handles NULL clock");
1603 ok(bt_ctf_clock_get_uuid(NULL) == NULL,
1604 "bt_ctf_clock_get_uuid correctly handles NULL clock");
1605 ret_uuid = bt_ctf_clock_get_uuid(clock);
1606 ok(ret_uuid,
1607 "bt_ctf_clock_get_uuid returns a UUID");
1608 if (ret_uuid) {
1609 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
1610 /* Slightly modify UUID */
1611 tmp_uuid[sizeof(tmp_uuid) - 1]++;
1612 }
1613
1614 ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0,
1615 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
1616 ok(bt_ctf_clock_set_uuid(clock, NULL) < 0,
1617 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
1618 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
1619 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
1620 ret_uuid = bt_ctf_clock_get_uuid(clock);
1621 ok(ret_uuid,
1622 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
1623 ok(uuid_match(ret_uuid, tmp_uuid),
1624 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
1625
1626 /* Define a stream class */
1627 stream_class = bt_ctf_stream_class_create("test_stream");
1628
1629 ok(bt_ctf_stream_class_get_name(NULL) == NULL,
1630 "bt_ctf_stream_class_get_name handles NULL correctly");
1631 ret_string = bt_ctf_stream_class_get_name(stream_class);
1632 ok(!strcmp(ret_string, "test_stream"),
1633 "bt_ctf_stream_class_get_name returns a correct stream class name");
1634
1635 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
1636 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
1637 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
1638 "bt_ctf_stream_class_get_clock handles NULL correctly");
1639
1640 ok(stream_class, "Create stream class");
1641 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
1642 "Set a stream class' clock");
1643 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
1644 ok(ret_clock == clock,
1645 "bt_ctf_stream_class_get_clock returns a correct clock");
1646 bt_ctf_clock_put(ret_clock);
1647
1648 /* Test the event fields and event types APIs */
1649 type_field_tests();
1650
1651 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
1652 "bt_ctf_stream_class_get_id returns an error when no id is set");
1653 ok(bt_ctf_stream_class_get_id(NULL) < 0,
1654 "bt_ctf_stream_class_get_id handles NULL correctly");
1655 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
1656 "bt_ctf_stream_class_set_id handles NULL correctly");
1657 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
1658 "Set an stream class' id");
1659 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
1660 "bt_ctf_stream_class_get_id returns the correct value");
1661
1662 /* Create a "uint5_t" equivalent custom packet context field */
1663 packet_context_field_type = bt_ctf_field_type_integer_create(5);
1664
1665 ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL,
1666 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
1667
1668 /* Add a custom field to the stream class' packet context */
1669 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1670 ok(packet_context_type,
1671 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
1672 ok(bt_ctf_field_type_get_type_id(packet_context_type) == CTF_TYPE_STRUCT,
1673 "Packet context is a structure");
1674
1675 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
1676 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
1677 ok(bt_ctf_stream_class_set_packet_context_type(stream_class, NULL),
1678 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
1679 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1680 packet_context_field_type, "custom_field");
1681 ok(ret == 0, "Packet context field added successfully");
1682
1683
1684 /* Instantiate a stream and append events */
1685 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
1686 ok(stream1, "Instanciate a stream class from writer");
1687
1688 /*
1689 * Try to modify the packet context type after a stream has been
1690 * created.
1691 */
1692 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1693 packet_context_field_type, "should_fail");
1694 ok(ret < 0,
1695 "Packet context type can't be modified once a stream class has been instanciated");
1696
1697 /* Should fail after instanciating a stream (locked)*/
1698 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
1699 "Changes to a stream class that was already instantiated fail");
1700
1701 append_simple_event(stream_class, stream1, clock);
1702
1703 packet_resize_test(stream_class, stream1, clock);
1704
1705 append_complex_event(stream_class, stream1, clock);
1706
1707 metadata_string = bt_ctf_writer_get_metadata_string(writer);
1708 ok(metadata_string, "Get metadata string");
1709
1710 bt_ctf_writer_flush_metadata(writer);
1711 validate_metadata(argv[1], metadata_path);
1712 validate_trace(argv[2], trace_path);
1713
1714 bt_ctf_clock_put(clock);
1715 bt_ctf_stream_class_put(stream_class);
1716 bt_ctf_writer_put(writer);
1717 bt_ctf_stream_put(stream1);
1718 bt_ctf_field_type_put(packet_context_type);
1719 bt_ctf_field_type_put(packet_context_field_type);
1720 bt_ctf_trace_put(trace);
1721 free(metadata_string);
1722
1723 /* Remove all trace files and delete temporary trace directory */
1724 DIR *trace_dir = opendir(trace_path);
1725 if (!trace_dir) {
1726 perror("# opendir");
1727 return -1;
1728 }
1729
1730 struct dirent *entry;
1731 while ((entry = readdir(trace_dir))) {
1732 if (entry->d_type == DT_REG) {
1733 unlinkat(dirfd(trace_dir), entry->d_name, 0);
1734 }
1735 }
1736
1737 rmdir(trace_path);
1738 closedir(trace_dir);
1739
1740 return 0;
1741 }
This page took 0.140968 seconds and 5 git commands to generate.