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