Docs: event class must have a stream class before events may be created
[babeltrace.git] / tests / lib / test_ctf_writer.c
CommitLineData
39d74371
JG
1/*
2 * test-ctf-writer.c
3 *
4 * CTF Writer test
5 *
88d26616 6 * Copyright 2013 - 2015 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
39d74371
JG
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>
88d26616 29#include <babeltrace/ctf-ir/stream-class.h>
39d74371
JG
30#include <babeltrace/ctf/events.h>
31#include <unistd.h>
32#include <stdlib.h>
33#include <stdio.h>
34#include <sys/utsname.h>
22843b66 35#include <babeltrace/compat/limits.h>
39d74371
JG
36#include <string.h>
37#include <assert.h>
39d74371
JG
38#include <sys/wait.h>
39#include <fcntl.h>
40#include <dirent.h>
41#include "tap/tap.h"
10817e06
JG
42#include <math.h>
43#include <float.h>
39d74371
JG
44
45#define METADATA_LINE_SIZE 512
46#define SEQUENCE_TEST_LENGTH 10
1fac895e 47#define ARRAY_TEST_LENGTH 5
39d74371
JG
48#define PACKET_RESIZE_TEST_LENGTH 100000
49
5494ce8b
JG
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
57static uint64_t current_time = 42;
39d74371 58
e61caf8e
JG
59/* Return 1 if uuids match, zero if different. */
60int 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
23f1c913 69 for (i = 0; i < 16; i++) {
e61caf8e
JG
70 if (uuid_a[i] != uuid_b[i]) {
71 goto end;
72 }
73 }
74
75 ret = 1;
76end:
77 return ret;
78}
79
39d74371
JG
80void 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
543409b0 94 unlink(parser_output_path);
39d74371
JG
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 }
131result:
132 ok(ret == 0, "Metadata string is valid");
133
25afa9e5 134 if (ret && metadata_fd >= 0 && parser_output_fd >= 0) {
39d74371
JG
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 }
e1d776cd 144 metadata_fd = -1;
39d74371
JG
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 }
e1d776cd 151 parser_output_fd = -1;
39d74371
JG
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) {
406b1e2c 162 fprintf(stderr, "# %s", line);
39d74371
JG
163 }
164
165 rewind(parser_output_fp);
166 while (getline(&line, &len, parser_output_fp) > 0) {
406b1e2c 167 fprintf(stderr, "# %s", line);
39d74371
JG
168 }
169
170 free(line);
171close_fp:
e1d776cd
MD
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 }
39d74371
JG
182 }
183
25afa9e5 184 if (parser_output_fd >= 0) {
e1d776cd
MD
185 if (close(parser_output_fd)) {
186 diag("close error");
187 }
188 }
25afa9e5 189 if (metadata_fd >= 0) {
e1d776cd
MD
190 if (close(metadata_fd)) {
191 diag("close error");
192 }
193 }
39d74371
JG
194}
195
196void 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);
543409b0 208 unlink(babeltrace_output_path);
39d74371
JG
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 }
238result:
239 ok(ret == 0, "Babeltrace could read the resulting trace");
240
25afa9e5 241 if (ret && babeltrace_output_fd >= 0) {
39d74371
JG
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 }
e1d776cd 251 babeltrace_output_fd = -1;
39d74371
JG
252
253 line = malloc(len);
e1d776cd
MD
254 if (!line) {
255 diag("malloc error");
256 }
39d74371
JG
257 rewind(babeltrace_output_fp);
258 while (getline(&line, &len, babeltrace_output_fp) > 0) {
259 diag("%s", line);
260 }
261
262 free(line);
263close_fp:
e1d776cd
MD
264 if (babeltrace_output_fp) {
265 if (fclose(babeltrace_output_fp)) {
266 diag("fclose error");
267 }
268 }
39d74371
JG
269 }
270
25afa9e5 271 if (babeltrace_output_fd >= 0) {
e1d776cd
MD
272 if (close(babeltrace_output_fd)) {
273 diag("close error");
274 }
275 }
39d74371
JG
276}
277
278void 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);
7cfd41d6
JG
286 struct bt_ctf_field_type *int_64_type =
287 bt_ctf_field_type_integer_create(64);
39d74371
JG
288 struct bt_ctf_field_type *float_type =
289 bt_ctf_field_type_floating_point_create();
7cfd41d6
JG
290 struct bt_ctf_field_type *enum_type;
291 struct bt_ctf_field_type *enum_type_unsigned =
8382544f 292 bt_ctf_field_type_enumeration_create(uint_12_type);
5edae678
JG
293 struct bt_ctf_field_type *event_context_type =
294 bt_ctf_field_type_structure_create();
7cfd41d6 295 struct bt_ctf_field_type *returned_type;
39d74371
JG
296 struct bt_ctf_event *simple_event;
297 struct bt_ctf_field *integer_field;
298 struct bt_ctf_field *float_field;
8382544f 299 struct bt_ctf_field *enum_field;
7cfd41d6 300 struct bt_ctf_field *enum_field_unsigned;
8382544f 301 struct bt_ctf_field *enum_container_field;
10817e06 302 const char *mapping_name_test = "truie";
10817e06 303 const double double_test_value = 3.1415;
7cfd41d6
JG
304 struct bt_ctf_field *enum_container_field_unsigned;
305 const char *mapping_name_negative_test = "negative_value";
306 const char *ret_char;
10817e06 307 double ret_double;
7cfd41d6
JG
308 int64_t ret_range_start_int64_t, ret_range_end_int64_t;
309 uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t;
1ff9582c 310 struct bt_ctf_clock *ret_clock;
e3c971da 311 struct bt_ctf_event_class *ret_event_class;
12c8a1a3
JG
312 struct bt_ctf_field *packet_context;
313 struct bt_ctf_field *packet_context_field;
5edae678
JG
314 struct bt_ctf_field *stream_event_context;
315 struct bt_ctf_field *stream_event_context_field;
6e1f8ea1
JG
316 struct bt_ctf_field *event_context;
317 struct bt_ctf_field *event_context_field;
7cfd41d6
JG
318
319 ok(uint_12_type, "Create an unsigned integer type");
320
321 bt_ctf_field_type_integer_set_signed(int_64_type, 1);
322 ok(int_64_type, "Create a signed integer type");
323 enum_type = bt_ctf_field_type_enumeration_create(int_64_type);
324
325 returned_type = bt_ctf_field_type_enumeration_get_container_type(enum_type);
326 ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
327 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
328 ok(!bt_ctf_field_type_enumeration_create(enum_type),
329 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
5edae678 330 bt_ctf_field_type_put(returned_type);
39d74371
JG
331
332 bt_ctf_field_type_set_alignment(float_type, 32);
7cfd41d6
JG
333 ok(bt_ctf_field_type_get_alignment(NULL) < 0,
334 "bt_ctf_field_type_get_alignment handles NULL correctly");
335 ok(bt_ctf_field_type_get_alignment(float_type) == 32,
336 "bt_ctf_field_type_get_alignment returns a correct value");
337
338 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0,
339 "Set a floating point type's exponent digit count");
340 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0,
341 "Set a floating point type's mantissa digit count");
342
343 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL) < 0,
344 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
345 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL) < 0,
346 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
347 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11,
348 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
349 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53,
350 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
8382544f
JG
351
352 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
7cfd41d6
JG
353 mapping_name_negative_test, -12345, 0) == 0,
354 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
8382544f 355 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
7cfd41d6
JG
356 "escaping; \"test\"", 1, 1) == 0,
357 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
358 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
359 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
360 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
8382544f
JG
361 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
362 "event clock int float", 5, 22) == 0,
363 "Accept enumeration mapping strings containing reserved keywords");
7cfd41d6
JG
364 bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
365 42, 42);
366 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
367 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
368 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something",
369 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
370 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
371 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
372 bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000);
373
074ee56d 374 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL, -42) < 0,
7cfd41d6 375 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
074ee56d 376 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, 1000000) < 0,
7cfd41d6 377 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
074ee56d 378 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -55) == 1,
7cfd41d6
JG
379 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
380
8382544f 381 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
7cfd41d6
JG
382 "enum_field") == 0, "Add signed enumeration field to event");
383
384 ok(bt_ctf_field_type_enumeration_get_mapping(NULL, 0, &ret_char,
385 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
386 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
387 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, NULL,
388 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
389 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
390 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
391 NULL, &ret_range_end_int64_t) < 0,
392 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
393 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
394 &ret_range_start_int64_t, NULL) < 0,
395 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
396 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 5, &ret_char,
397 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
398 "bt_ctf_field_type_enumeration_get_mapping returns a value");
399 ok(!strcmp(ret_char, mapping_name_test),
400 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
401 ok(ret_range_start_int64_t == 42,
402 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
403 ok(ret_range_end_int64_t == 42,
404 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
405
406 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
407 "escaping; \"test\"", 0, 0) == 0,
408 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
409 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
410 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
411 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
412 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
413 "event clock int float", 5, 22) == 0,
414 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
415 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
416 42, 42);
417 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
418 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
419 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something",
420 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
421 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
422 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
423 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned,
424 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
425
426 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL) < 0,
427 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
428 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 4,
429 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
430
431 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char,
432 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
433 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
434 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL,
435 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
436 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
437 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
438 NULL, &ret_range_end_uint64_t) < 0,
439 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
440 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
441 &ret_range_start_uint64_t, NULL) < 0,
442 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
443 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 3, &ret_char,
444 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
445 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
446 ok(!strcmp(ret_char, mapping_name_test),
447 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
448 ok(ret_range_start_uint64_t == 42,
449 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
450 ok(ret_range_end_uint64_t == 42,
451 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
8382544f 452
39d74371
JG
453 bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
454 "integer_field");
455 bt_ctf_event_class_add_field(simple_event_class, float_type,
456 "float_field");
457 bt_ctf_stream_class_add_event_class(stream_class,
458 simple_event_class);
459
e3c971da
JG
460 ok(bt_ctf_stream_class_get_event_class_count(NULL) < 0,
461 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
462 ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1,
463 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
464 ok(bt_ctf_stream_class_get_event_class(NULL, 0) == NULL,
465 "bt_ctf_stream_class_get_event_class handles NULL correctly");
466 ok(bt_ctf_stream_class_get_event_class(stream_class, 8724) == NULL,
467 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
468 ret_event_class = bt_ctf_stream_class_get_event_class(stream_class, 0);
469 ok(ret_event_class == simple_event_class,
470 "bt_ctf_stream_class_get_event_class returns the correct event class");
471 bt_ctf_event_class_put(ret_event_class);
472
473 ok(bt_ctf_stream_class_get_event_class_by_name(NULL, "some event name") == NULL,
474 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
475 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, NULL) == NULL,
476 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
477 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, "some event name") == NULL,
478 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
5edae678 479 ret_event_class = bt_ctf_stream_class_get_event_class_by_name(stream_class, "Simple Event");
e3c971da
JG
480 ok(ret_event_class == simple_event_class,
481 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
482 bt_ctf_event_class_put(ret_event_class);
483
5edae678 484 /* Set an event context type which will contain a single integer*/
8c627304
JG
485 ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
486 "event_specific_context"),
487 "Add event specific context field");
5edae678
JG
488 ok(bt_ctf_event_class_get_context_type(NULL) == NULL,
489 "bt_ctf_event_class_get_context_type handles NULL correctly");
490 ok(bt_ctf_event_class_get_context_type(simple_event_class) == NULL,
491 "bt_ctf_event_class_get_context_type returns NULL when no event context type is set");
492 ok(bt_ctf_event_class_set_context_type(simple_event_class, NULL) < 0,
493 "bt_ctf_event_class_set_context_type handles a NULL context type correctly");
494 ok(bt_ctf_event_class_set_context_type(NULL, event_context_type) < 0,
495 "bt_ctf_event_class_set_context_type handles a NULL event class correctly");
496 ok(!bt_ctf_event_class_set_context_type(simple_event_class, event_context_type),
497 "Set an event class' context type successfully");
498 returned_type = bt_ctf_event_class_get_context_type(simple_event_class);
499 ok(returned_type == event_context_type,
500 "bt_ctf_event_class_get_context_type returns the appropriate type");
501 bt_ctf_field_type_put(returned_type);
502
39d74371 503 simple_event = bt_ctf_event_create(simple_event_class);
39d74371
JG
504 ok(simple_event,
505 "Instantiate an event containing a single integer field");
506
1ff9582c
JG
507 ok(bt_ctf_event_get_clock(NULL) == NULL,
508 "bt_ctf_event_get_clock handles NULL correctly");
509 ret_clock = bt_ctf_event_get_clock(simple_event);
510 ok(ret_clock == clock,
511 "bt_ctf_event_get_clock returns a correct clock");
512 bt_ctf_clock_put(clock);
513
39d74371
JG
514 integer_field = bt_ctf_field_create(uint_12_type);
515 bt_ctf_field_unsigned_integer_set_value(integer_field, 42);
516 ok(bt_ctf_event_set_payload(simple_event, "integer_field",
517 integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
518
519 float_field = bt_ctf_event_get_payload(simple_event, "float_field");
10817e06
JG
520 ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double),
521 "bt_ctf_field_floating_point_get_value fails on an unset float field");
522 bt_ctf_field_floating_point_set_value(float_field, double_test_value);
523 ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double),
524 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
525 ok(bt_ctf_field_floating_point_get_value(float_field, NULL),
526 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
527 ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double),
528 "bt_ctf_field_floating_point_get_value returns a double value");
529 ok(fabs(ret_double - double_test_value) <= DBL_EPSILON,
530 "bt_ctf_field_floating_point_get_value returns a correct value");
531
8382544f 532 enum_field = bt_ctf_field_create(enum_type);
7cfd41d6
JG
533 ret_char = bt_ctf_field_enumeration_get_mapping_name(NULL);
534 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
535 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
536 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
8382544f
JG
537 enum_container_field = bt_ctf_field_enumeration_get_container(
538 enum_field);
7cfd41d6
JG
539 ok(bt_ctf_field_signed_integer_set_value(
540 enum_container_field, -42) == 0,
541 "Set signed enumeration container value");
542 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
543 ok(!strcmp(ret_char, mapping_name_negative_test),
544 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
8382544f 545 bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
39d74371 546
7cfd41d6
JG
547 enum_field_unsigned = bt_ctf_field_create(enum_type_unsigned);
548 enum_container_field_unsigned = bt_ctf_field_enumeration_get_container(
549 enum_field_unsigned);
550 ok(bt_ctf_field_unsigned_integer_set_value(
551 enum_container_field_unsigned, 42) == 0,
552 "Set unsigned enumeration container value");
553 bt_ctf_event_set_payload(simple_event, "enum_field_unsigned",
554 enum_field_unsigned);
555 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned);
556 ok(!strcmp(ret_char, mapping_name_test),
557 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
558
39d74371
JG
559 ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
560
6e1f8ea1 561 /* Populate stream event context */
5edae678
JG
562 stream_event_context = bt_ctf_stream_get_event_context(stream);
563 stream_event_context_field = bt_ctf_field_structure_get_field(
564 stream_event_context, "common_event_context");
565 bt_ctf_field_unsigned_integer_set_value(stream_event_context_field, 42);
566
567 /* Populate the event's context */
568 ok(bt_ctf_event_get_event_context(NULL) == NULL,
569 "bt_ctf_event_get_event_context handles NULL correctly");
570 event_context = bt_ctf_event_get_event_context(simple_event);
571 ok(event_context,
572 "bt_ctf_event_get_event_context returns a field");
573 returned_type = bt_ctf_field_get_type(event_context);
574 ok(returned_type == event_context_type,
575 "bt_ctf_event_get_event_context returns a field of the appropriate type");
576 event_context_field = bt_ctf_field_structure_get_field(event_context,
577 "event_specific_context");
578 ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field, 1234),
579 "Successfully set an event context's value");
580 ok(bt_ctf_event_set_event_context(NULL, event_context) < 0,
581 "bt_ctf_event_set_event_context handles a NULL event correctly");
582 ok(bt_ctf_event_set_event_context(simple_event, NULL) < 0,
583 "bt_ctf_event_set_event_context handles a NULL event context correctly");
584 ok(bt_ctf_event_set_event_context(simple_event, event_context_field) < 0,
585 "bt_ctf_event_set_event_context rejects a context of the wrong type");
586 ok(!bt_ctf_event_set_event_context(simple_event, event_context),
587 "Set an event context successfully");
6e1f8ea1 588
39d74371
JG
589 ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
590 "Append simple event to trace stream");
591
12c8a1a3
JG
592 ok(bt_ctf_stream_get_packet_context(NULL) == NULL,
593 "bt_ctf_stream_get_packet_context handles NULL correctly");
594 packet_context = bt_ctf_stream_get_packet_context(stream);
595 ok(packet_context,
596 "bt_ctf_stream_get_packet_context returns a packet context");
597
598 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
599 "packet_size");
600 ok(packet_context_field,
601 "Packet context contains the default packet_size field.");
602 bt_ctf_field_put(packet_context_field);
603 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
35e8709f 604 "custom_packet_context_field");
12c8a1a3
JG
605 ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field, 8) == 0,
606 "Custom packet context field value successfully set.");
607
608 ok(bt_ctf_stream_set_packet_context(NULL, packet_context_field) < 0,
609 "bt_ctf_stream_set_packet_context handles a NULL stream correctly");
610 ok(bt_ctf_stream_set_packet_context(stream, NULL) < 0,
611 "bt_ctf_stream_set_packet_context handles a NULL packet context correctly");
612 ok(bt_ctf_stream_set_packet_context(stream, packet_context) == 0,
613 "Successfully set a stream's packet context");
614
39d74371
JG
615 ok(bt_ctf_stream_flush(stream) == 0,
616 "Flush trace stream with one event");
617
618 bt_ctf_event_class_put(simple_event_class);
619 bt_ctf_event_put(simple_event);
620 bt_ctf_field_type_put(uint_12_type);
7cfd41d6 621 bt_ctf_field_type_put(int_64_type);
39d74371 622 bt_ctf_field_type_put(float_type);
8382544f 623 bt_ctf_field_type_put(enum_type);
7cfd41d6
JG
624 bt_ctf_field_type_put(enum_type_unsigned);
625 bt_ctf_field_type_put(returned_type);
5edae678 626 bt_ctf_field_type_put(event_context_type);
39d74371
JG
627 bt_ctf_field_put(integer_field);
628 bt_ctf_field_put(float_field);
8382544f 629 bt_ctf_field_put(enum_field);
7cfd41d6 630 bt_ctf_field_put(enum_field_unsigned);
8382544f 631 bt_ctf_field_put(enum_container_field);
7cfd41d6 632 bt_ctf_field_put(enum_container_field_unsigned);
12c8a1a3
JG
633 bt_ctf_field_put(packet_context);
634 bt_ctf_field_put(packet_context_field);
5edae678
JG
635 bt_ctf_field_put(stream_event_context);
636 bt_ctf_field_put(stream_event_context_field);
6e1f8ea1
JG
637 bt_ctf_field_put(event_context);
638 bt_ctf_field_put(event_context_field);
39d74371
JG
639}
640
641void append_complex_event(struct bt_ctf_stream_class *stream_class,
642 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
643{
644 int i;
1ff9582c 645 const char *complex_test_event_string = "Complex Test Event";
10817e06 646 const char *test_string = "Test string";
39d74371
JG
647 struct bt_ctf_field_type *uint_35_type =
648 bt_ctf_field_type_integer_create(35);
649 struct bt_ctf_field_type *int_16_type =
650 bt_ctf_field_type_integer_create(16);
651 struct bt_ctf_field_type *uint_3_type =
652 bt_ctf_field_type_integer_create(3);
653 struct bt_ctf_field_type *enum_variant_type =
654 bt_ctf_field_type_enumeration_create(uint_3_type);
655 struct bt_ctf_field_type *variant_type =
656 bt_ctf_field_type_variant_create(enum_variant_type,
657 "variant_selector");
658 struct bt_ctf_field_type *string_type =
659 bt_ctf_field_type_string_create();
660 struct bt_ctf_field_type *sequence_type;
1fac895e 661 struct bt_ctf_field_type *array_type;
39d74371
JG
662 struct bt_ctf_field_type *inner_structure_type =
663 bt_ctf_field_type_structure_create();
664 struct bt_ctf_field_type *complex_structure_type =
665 bt_ctf_field_type_structure_create();
7cfd41d6 666 struct bt_ctf_field_type *ret_field_type;
39d74371
JG
667 struct bt_ctf_event_class *event_class;
668 struct bt_ctf_event *event;
669 struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
670 *inner_structure_field, *complex_structure_field,
671 *a_sequence_field, *enum_variant_field, *enum_container_field,
1fac895e 672 *variant_field, *an_array_field, *ret_field;
10817e06 673 uint64_t ret_unsigned_int;
1fac895e 674 int64_t ret_signed_int;
10817e06 675 const char *ret_string;
1ff9582c
JG
676 struct bt_ctf_stream_class *ret_stream_class;
677 struct bt_ctf_event_class *ret_event_class;
12c8a1a3 678 struct bt_ctf_field *packet_context, *packet_context_field;
39d74371
JG
679
680 bt_ctf_field_type_set_alignment(int_16_type, 32);
681 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
682 bt_ctf_field_type_integer_set_base(uint_35_type,
683 BT_CTF_INTEGER_BASE_HEXADECIMAL);
684
1fac895e 685 array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH);
39d74371
JG
686 sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
687 "seq_len");
7cfd41d6
JG
688
689 ok(bt_ctf_field_type_array_get_element_type(NULL) == NULL,
690 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
691 ret_field_type = bt_ctf_field_type_array_get_element_type(
692 array_type);
693 ok(ret_field_type == int_16_type,
694 "bt_ctf_field_type_array_get_element_type returns the correct type");
695 bt_ctf_field_type_put(ret_field_type);
696
697 ok(bt_ctf_field_type_array_get_length(NULL) < 0,
698 "bt_ctf_field_type_array_get_length handles NULL correctly");
699 ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH,
700 "bt_ctf_field_type_array_get_length returns the correct length");
701
39d74371
JG
702 bt_ctf_field_type_structure_add_field(inner_structure_type,
703 uint_35_type, "seq_len");
704 bt_ctf_field_type_structure_add_field(inner_structure_type,
705 sequence_type, "a_sequence");
1fac895e
JG
706 bt_ctf_field_type_structure_add_field(inner_structure_type,
707 array_type, "an_array");
39d74371
JG
708
709 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
710 "UINT3_TYPE", 0, 0);
711 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
712 "INT16_TYPE", 1, 1);
713 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
714 "UINT35_TYPE", 2, 7);
7cfd41d6 715
074ee56d
JG
716 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL,
717 "INT16_TYPE") < 0,
7cfd41d6 718 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
074ee56d
JG
719 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
720 enum_variant_type, NULL) < 0,
7cfd41d6 721 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
074ee56d
JG
722 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(
723 enum_variant_type, "INT16_TYPE") == 1,
7cfd41d6
JG
724 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
725
074ee56d 726 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL, 1) < 0,
7cfd41d6 727 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
074ee56d 728 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, -42) < 0,
7cfd41d6 729 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
074ee56d 730 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 5) == 2,
7cfd41d6
JG
731 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
732
39d74371
JG
733 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
734 "An unknown entry"), "Reject a variant field based on an unknown tag value");
735 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
736 "UINT3_TYPE") == 0, "Add a field to a variant");
737 bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
738 "INT16_TYPE");
739 bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
740 "UINT35_TYPE");
741
7cfd41d6
JG
742 ok(bt_ctf_field_type_variant_get_tag_type(NULL) == NULL,
743 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
744 ret_field_type = bt_ctf_field_type_variant_get_tag_type(variant_type);
745 ok(ret_field_type == enum_variant_type,
746 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
747 bt_ctf_field_type_put(ret_field_type);
748
749 ok(bt_ctf_field_type_variant_get_tag_name(NULL) == NULL,
750 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
751 ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type);
752 ok(!strcmp(ret_string, "variant_selector"),
753 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
754 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL,
755 "INT16_TYPE") == NULL,
756 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
757 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type,
758 NULL) == NULL,
759 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
760 ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name(
761 variant_type, "INT16_TYPE");
762 ok(ret_field_type == int_16_type,
763 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
764 bt_ctf_field_type_put(ret_field_type);
765
766 ok(bt_ctf_field_type_variant_get_field_count(NULL) < 0,
767 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
768 ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3,
769 "bt_ctf_field_type_variant_get_field_count returns the correct count");
770
771 ok(bt_ctf_field_type_variant_get_field(NULL, &ret_string, &ret_field_type, 0) < 0,
772 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
773 ok(bt_ctf_field_type_variant_get_field(variant_type, NULL, &ret_field_type, 0) < 0,
774 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
775 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, NULL, 0) < 0,
776 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
777 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 200) < 0,
778 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
779 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 1) == 0,
780 "bt_ctf_field_type_variant_get_field returns a field");
781 ok(!strcmp("INT16_TYPE", ret_string),
782 "bt_ctf_field_type_variant_get_field returns a correct field name");
783 ok(ret_field_type == int_16_type,
784 "bt_ctf_field_type_variant_get_field returns a correct field type");
785 bt_ctf_field_type_put(ret_field_type);
786
39d74371
JG
787 bt_ctf_field_type_structure_add_field(complex_structure_type,
788 enum_variant_type, "variant_selector");
789 bt_ctf_field_type_structure_add_field(complex_structure_type,
790 string_type, "a_string");
791 bt_ctf_field_type_structure_add_field(complex_structure_type,
792 variant_type, "variant_value");
793 bt_ctf_field_type_structure_add_field(complex_structure_type,
794 inner_structure_type, "inner_structure");
795
796 ok(bt_ctf_event_class_create("clock") == NULL,
797 "Reject creation of an event class with an illegal name");
1ff9582c 798 event_class = bt_ctf_event_class_create(complex_test_event_string);
39d74371
JG
799 ok(event_class, "Create an event class");
800 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
801 "Reject addition of a field with an empty name to an event");
802 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
803 "Reject addition of a field with a NULL type to an event");
804 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
805 "int"),
806 "Reject addition of a type with an illegal name to an event");
807 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
808 "uint_35") == 0,
809 "Add field of type unsigned integer to an event");
810 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
811 "int_16") == 0, "Add field of type signed integer to an event");
812 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
813 "complex_structure") == 0,
814 "Add composite structure to an event");
815
1ff9582c
JG
816 ok(bt_ctf_event_class_get_name(NULL) == NULL,
817 "bt_ctf_event_class_get_name handles NULL correctly");
818 ret_string = bt_ctf_event_class_get_name(event_class);
819 ok(!strcmp(ret_string, complex_test_event_string),
820 "bt_ctf_event_class_get_name returns a correct name");
821 ok(bt_ctf_event_class_get_id(event_class) < 0,
822 "bt_ctf_event_class_get_id returns a negative value when not set");
823 ok(bt_ctf_event_class_get_id(NULL) < 0,
824 "bt_ctf_event_class_get_id handles NULL correctly");
825 ok(bt_ctf_event_class_set_id(NULL, 42) < 0,
826 "bt_ctf_event_class_set_id handles NULL correctly");
827 ok(bt_ctf_event_class_set_id(event_class, 42) == 0,
828 "Set an event class' id");
829 ok(bt_ctf_event_class_get_id(event_class) == 42,
830 "bt_ctf_event_class_get_id returns the correct value");
831
39d74371
JG
832 /* Add event class to the stream class */
833 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
834 "Reject addition of NULL event class to a stream class");
835 ok(bt_ctf_stream_class_add_event_class(stream_class,
836 event_class) == 0, "Add an event class to stream class");
837
1ff9582c
JG
838 ok(bt_ctf_event_class_get_stream_class(NULL) == NULL,
839 "bt_ctf_event_class_get_stream_class handles NULL correctly");
840 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
841 ok(ret_stream_class == stream_class,
842 "bt_ctf_event_class_get_stream_class returns the correct stream class");
843 bt_ctf_stream_class_put(ret_stream_class);
844
845 ok(bt_ctf_event_class_get_field_count(NULL) < 0,
846 "bt_ctf_event_class_get_field_count handles NULL correctly");
847 ok(bt_ctf_event_class_get_field_count(event_class) == 3,
848 "bt_ctf_event_class_get_field_count returns a correct value");
849
850 ok(bt_ctf_event_class_get_field(NULL, &ret_string,
851 &ret_field_type, 0) < 0,
852 "bt_ctf_event_class_get_field handles a NULL event class correctly");
853 ok(bt_ctf_event_class_get_field(event_class, NULL,
854 &ret_field_type, 0) < 0,
855 "bt_ctf_event_class_get_field handles a NULL field name correctly");
856 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
857 NULL, 0) < 0,
858 "bt_ctf_event_class_get_field handles a NULL field type correctly");
859 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
860 &ret_field_type, 42) < 0,
861 "bt_ctf_event_class_get_field handles an invalid index correctly");
862 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
863 &ret_field_type, 0) == 0,
864 "bt_ctf_event_class_get_field returns a field");
865 ok(ret_field_type == uint_35_type,
866 "bt_ctf_event_class_get_field returns a correct field type");
867 bt_ctf_field_type_put(ret_field_type);
868 ok(!strcmp(ret_string, "uint_35"),
869 "bt_ctf_event_class_get_field returns a correct field name");
870 ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL,
871 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
872 ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL,
873 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
874 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
875 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
876 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
877 "complex_structure");
878 ok(ret_field_type == complex_structure_type,
879 "bt_ctf_event_class_get_field_by_name returns a correct field type");
880 bt_ctf_field_type_put(ret_field_type);
881
39d74371
JG
882 event = bt_ctf_event_create(event_class);
883 ok(event, "Instanciate a complex event");
884
1ff9582c
JG
885 ok(bt_ctf_event_get_class(NULL) == NULL,
886 "bt_ctf_event_get_class handles NULL correctly");
887 ret_event_class = bt_ctf_event_get_class(event);
888 ok(ret_event_class == event_class,
889 "bt_ctf_event_get_class returns the correct event class");
890 bt_ctf_event_class_put(ret_event_class);
891
39d74371 892 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
7cfd41d6 893 if (!uint_35_field) {
10817e06 894 printf("uint_35_field is NULL\n");
7cfd41d6
JG
895 }
896
39d74371
JG
897 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
898 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
3c1d148b 899 ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0,
10817e06 900 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
3c1d148b 901 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0,
10817e06
JG
902 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
903 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
904 &ret_unsigned_int) == 0,
905 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
906 ok(ret_unsigned_int == 0x0DDF00D,
907 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
908 ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
3c1d148b 909 &ret_signed_int) < 0,
10817e06 910 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
39d74371
JG
911 bt_ctf_field_put(uint_35_field);
912
913 int_16_field = bt_ctf_event_get_payload(event, "int_16");
914 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
3c1d148b 915 ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0,
10817e06 916 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
3c1d148b 917 ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0,
10817e06
JG
918 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
919 ok(bt_ctf_field_signed_integer_get_value(int_16_field,
920 &ret_signed_int) == 0,
921 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
922 ok(ret_signed_int == -12345,
923 "bt_ctf_field_signed_integer_get_value returns the correct value");
924 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
3c1d148b 925 &ret_unsigned_int) < 0,
10817e06 926 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
39d74371
JG
927 bt_ctf_field_put(int_16_field);
928
929 complex_structure_field = bt_ctf_event_get_payload(event,
930 "complex_structure");
10817e06
JG
931
932 ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
933 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
934 ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
935 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
936 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
937 complex_structure_field, 3);
938 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
939 bt_ctf_field_put(inner_structure_field);
940 ok(ret_field_type == inner_structure_type,
941 "bt_ctf_field_structure_get_field_by_index returns a correct field");
942 bt_ctf_field_type_put(ret_field_type);
943
39d74371
JG
944 inner_structure_field = bt_ctf_field_structure_get_field(
945 complex_structure_field, "inner_structure");
946 a_string_field = bt_ctf_field_structure_get_field(
947 complex_structure_field, "a_string");
948 enum_variant_field = bt_ctf_field_structure_get_field(
949 complex_structure_field, "variant_selector");
950 variant_field = bt_ctf_field_structure_get_field(
951 complex_structure_field, "variant_value");
952 uint_35_field = bt_ctf_field_structure_get_field(
953 inner_structure_field, "seq_len");
954 a_sequence_field = bt_ctf_field_structure_get_field(
955 inner_structure_field, "a_sequence");
1fac895e
JG
956 an_array_field = bt_ctf_field_structure_get_field(
957 inner_structure_field, "an_array");
39d74371
JG
958
959 enum_container_field = bt_ctf_field_enumeration_get_container(
960 enum_variant_field);
961 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
962 int_16_field = bt_ctf_field_variant_get_field(variant_field,
963 enum_variant_field);
964 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
965 bt_ctf_field_put(int_16_field);
10817e06
JG
966 ok(!bt_ctf_field_string_get_value(a_string_field),
967 "bt_ctf_field_string_get_value returns NULL on an unset field");
39d74371 968 bt_ctf_field_string_set_value(a_string_field,
10817e06
JG
969 test_string);
970 ok(!bt_ctf_field_string_get_value(NULL),
971 "bt_ctf_field_string_get_value correctly handles NULL");
972 ret_string = bt_ctf_field_string_get_value(a_string_field);
973 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
d50c7132 974 ok(ret_string ? !strcmp(ret_string, test_string) : 0,
10817e06 975 "bt_ctf_field_string_get_value returns a correct value");
39d74371
JG
976 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
977 SEQUENCE_TEST_LENGTH);
10817e06 978
7cfd41d6
JG
979 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL,
980 enum_container_field) == NULL,
981 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
982 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type,
983 NULL) == NULL,
984 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
985 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
986 variant_type, enum_variant_field);
987 ok(ret_field_type == int_16_type,
988 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
989
10817e06
JG
990 ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
991 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
39d74371
JG
992 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
993 uint_35_field) == 0, "Set a sequence field's length");
cd95e351
JG
994 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
995 ok(ret_field == uint_35_field,
996 "bt_ctf_field_sequence_get_length returns the correct length field");
997 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
998 "bt_ctf_field_sequence_get_length properly handles NULL");
39d74371
JG
999
1000 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
1001 int_16_field = bt_ctf_field_sequence_get_field(
1002 a_sequence_field, i);
1003 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
1004 bt_ctf_field_put(int_16_field);
1005 }
1006
1fac895e
JG
1007 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
1008 int_16_field = bt_ctf_field_array_get_field(
1009 an_array_field, i);
1010 bt_ctf_field_signed_integer_set_value(int_16_field, i);
1011 bt_ctf_field_put(int_16_field);
1012 }
1013
39d74371
JG
1014 bt_ctf_clock_set_time(clock, ++current_time);
1015 ok(bt_ctf_stream_append_event(stream, event) == 0,
1016 "Append a complex event to a stream");
12c8a1a3
JG
1017
1018 /*
1019 * Populate the custom packet context field with a dummy value
1020 * otherwise flush will fail.
1021 */
1022 packet_context = bt_ctf_stream_get_packet_context(stream);
1023 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
35e8709f 1024 "custom_packet_context_field");
12c8a1a3
JG
1025 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1);
1026
39d74371
JG
1027 ok(bt_ctf_stream_flush(stream) == 0,
1028 "Flush a stream containing a complex event");
1029
1030 bt_ctf_field_put(uint_35_field);
1031 bt_ctf_field_put(a_string_field);
1032 bt_ctf_field_put(inner_structure_field);
1033 bt_ctf_field_put(complex_structure_field);
1034 bt_ctf_field_put(a_sequence_field);
1fac895e 1035 bt_ctf_field_put(an_array_field);
39d74371
JG
1036 bt_ctf_field_put(enum_variant_field);
1037 bt_ctf_field_put(enum_container_field);
1038 bt_ctf_field_put(variant_field);
cd95e351 1039 bt_ctf_field_put(ret_field);
12c8a1a3
JG
1040 bt_ctf_field_put(packet_context_field);
1041 bt_ctf_field_put(packet_context);
39d74371
JG
1042 bt_ctf_field_type_put(uint_35_type);
1043 bt_ctf_field_type_put(int_16_type);
1044 bt_ctf_field_type_put(string_type);
1045 bt_ctf_field_type_put(sequence_type);
1fac895e 1046 bt_ctf_field_type_put(array_type);
39d74371
JG
1047 bt_ctf_field_type_put(inner_structure_type);
1048 bt_ctf_field_type_put(complex_structure_type);
1049 bt_ctf_field_type_put(uint_3_type);
1050 bt_ctf_field_type_put(enum_variant_type);
1051 bt_ctf_field_type_put(variant_type);
7cfd41d6 1052 bt_ctf_field_type_put(ret_field_type);
39d74371
JG
1053 bt_ctf_event_class_put(event_class);
1054 bt_ctf_event_put(event);
1055}
1056
1057void type_field_tests()
1058{
1059 struct bt_ctf_field *uint_12;
1060 struct bt_ctf_field *int_16;
1061 struct bt_ctf_field *string;
0abce37e 1062 struct bt_ctf_field *enumeration;
39d74371
JG
1063 struct bt_ctf_field_type *composite_structure_type;
1064 struct bt_ctf_field_type *structure_seq_type;
1065 struct bt_ctf_field_type *string_type;
1066 struct bt_ctf_field_type *sequence_type;
1067 struct bt_ctf_field_type *uint_8_type;
1068 struct bt_ctf_field_type *int_16_type;
1069 struct bt_ctf_field_type *uint_12_type =
1070 bt_ctf_field_type_integer_create(12);
0abce37e
JG
1071 struct bt_ctf_field_type *enumeration_type;
1072 struct bt_ctf_field_type *enumeration_sequence_type;
1073 struct bt_ctf_field_type *enumeration_array_type;
10817e06 1074 struct bt_ctf_field_type *returned_type;
7cfd41d6 1075 const char *ret_string;
10817e06
JG
1076
1077 returned_type = bt_ctf_field_get_type(NULL);
1078 ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
39d74371
JG
1079
1080 ok(uint_12_type, "Create an unsigned integer type");
1081 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1082 BT_CTF_INTEGER_BASE_BINARY) == 0,
1083 "Set integer type's base as binary");
1084 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1085 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
1086 "Set integer type's base as decimal");
1087 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1088 BT_CTF_INTEGER_BASE_UNKNOWN),
1089 "Reject integer type's base set as unknown");
1090 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1091 BT_CTF_INTEGER_BASE_OCTAL) == 0,
1092 "Set integer type's base as octal");
1093 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1094 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
1095 "Set integer type's base as hexadecimal");
1096 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
1097 "Reject unknown integer base value");
1098 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
1099 "Set integer type signedness to signed");
1100 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
1101 "Set integer type signedness to unsigned");
7cfd41d6
JG
1102 ok(bt_ctf_field_type_integer_get_size(NULL) < 0,
1103 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1104 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
1105 "bt_ctf_field_type_integer_get_size returns a correct value");
1106 ok(bt_ctf_field_type_integer_get_signed(NULL) < 0,
1107 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1108 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
1109 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1110
1111 ok(bt_ctf_field_type_set_byte_order(NULL,
1112 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
1113 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1114 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1115 (enum bt_ctf_byte_order) 42) < 0,
1116 "bt_ctf_field_type_set_byte_order rejects invalid values");
1117 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1118 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
1119 "Set an integer's byte order to little endian");
1120 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1121 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1122 "Set an integer's byte order to big endian");
1123 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
1124 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1125 "bt_ctf_field_type_get_byte_order returns a correct value");
1126 ok(bt_ctf_field_type_get_byte_order(NULL) ==
1127 BT_CTF_BYTE_ORDER_UNKNOWN,
1128 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1129
1130 ok(bt_ctf_field_type_get_type_id(NULL) ==
1131 CTF_TYPE_UNKNOWN,
1132 "bt_ctf_field_type_get_type_id handles NULL correctly");
1133 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
1134 CTF_TYPE_INTEGER,
1135 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1136
1137 ok(bt_ctf_field_type_integer_get_base(NULL) ==
1138 BT_CTF_INTEGER_BASE_UNKNOWN,
1139 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1140 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
1141 BT_CTF_INTEGER_BASE_HEXADECIMAL,
1142 "bt_ctf_field_type_integer_get_base returns a correct value");
1143
1144 ok(bt_ctf_field_type_integer_set_encoding(NULL, CTF_STRING_ASCII) < 0,
1145 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1146 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1147 (enum ctf_string_encoding) 123) < 0,
1148 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1149 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1150 CTF_STRING_UTF8) == 0,
1151 "Set integer type encoding to UTF8");
1152 ok(bt_ctf_field_type_integer_get_encoding(NULL) == CTF_STRING_UNKNOWN,
1153 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1154 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) == CTF_STRING_UTF8,
1155 "bt_ctf_field_type_integer_get_encoding returns a correct value");
39d74371
JG
1156
1157 int_16_type = bt_ctf_field_type_integer_create(16);
1158 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
7cfd41d6
JG
1159 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
1160 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
39d74371
JG
1161 uint_8_type = bt_ctf_field_type_integer_create(8);
1162 sequence_type =
1163 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
1164 ok(sequence_type, "Create a sequence of int16_t type");
7cfd41d6
JG
1165 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
1166 CTF_TYPE_SEQUENCE,
1167 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1168
1169 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL,
1170 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1171 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
1172 sequence_type);
1173 ok(!strcmp(ret_string, "seq_len"),
1174 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1175 ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL,
1176 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1177 returned_type = bt_ctf_field_type_sequence_get_element_type(
1178 sequence_type);
1179 ok(returned_type == int_16_type,
1180 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1181 bt_ctf_field_type_put(returned_type);
39d74371
JG
1182
1183 string_type = bt_ctf_field_type_string_create();
1184 ok(string_type, "Create a string type");
1185 ok(bt_ctf_field_type_string_set_encoding(string_type,
1186 CTF_STRING_NONE),
1187 "Reject invalid \"None\" string encoding");
1188 ok(bt_ctf_field_type_string_set_encoding(string_type,
1189 42),
1190 "Reject invalid string encoding");
1191 ok(bt_ctf_field_type_string_set_encoding(string_type,
1192 CTF_STRING_ASCII) == 0,
1193 "Set string encoding to ASCII");
1194
7cfd41d6
JG
1195 ok(bt_ctf_field_type_string_get_encoding(NULL) ==
1196 CTF_STRING_UNKNOWN,
1197 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1198 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
1199 CTF_STRING_ASCII,
1200 "bt_ctf_field_type_string_get_encoding returns the correct value");
1201
39d74371 1202 structure_seq_type = bt_ctf_field_type_structure_create();
7cfd41d6
JG
1203 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
1204 CTF_TYPE_STRUCT,
1205 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
39d74371
JG
1206 ok(structure_seq_type, "Create a structure type");
1207 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1208 uint_8_type, "seq_len") == 0,
1209 "Add a uint8_t type to a structure");
1210 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1211 sequence_type, "a_sequence") == 0,
1212 "Add a sequence type to a structure");
7cfd41d6
JG
1213
1214 ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0,
1215 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1216 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
1217 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1218
1219 ok(bt_ctf_field_type_structure_get_field(NULL,
1220 &ret_string, &returned_type, 1) < 0,
1221 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1222 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1223 NULL, &returned_type, 1) < 0,
1224 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1225 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1226 &ret_string, NULL, 1) < 0,
1227 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1228 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1229 &ret_string, &returned_type, 10) < 0,
1230 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1231 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1232 &ret_string, &returned_type, 1) == 0,
1233 "bt_ctf_field_type_structure_get_field returns a field");
1234 ok(!strcmp(ret_string, "a_sequence"),
1235 "bt_ctf_field_type_structure_get_field returns a correct field name");
1236 ok(returned_type == sequence_type,
1237 "bt_ctf_field_type_structure_get_field returns a correct field type");
1238 bt_ctf_field_type_put(returned_type);
1239
1240 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL,
1241 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1242 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL,
1243 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1244 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1245 structure_seq_type, "a_sequence");
1246 ok(returned_type == sequence_type,
1247 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1248 bt_ctf_field_type_put(returned_type);
1249
39d74371
JG
1250 composite_structure_type = bt_ctf_field_type_structure_create();
1251 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1252 string_type, "a_string") == 0,
1253 "Add a string type to a structure");
1254 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1255 structure_seq_type, "inner_structure") == 0,
1256 "Add a structure type to a structure");
1257
7cfd41d6
JG
1258 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1259 NULL, "a_sequence") == NULL,
1260 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1261 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1262 structure_seq_type, NULL) == NULL,
1263 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1264 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1265 structure_seq_type, "a_sequence");
1266 ok(returned_type == sequence_type,
1267 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1268 bt_ctf_field_type_put(returned_type);
1269
39d74371
JG
1270 int_16 = bt_ctf_field_create(int_16_type);
1271 ok(int_16, "Instanciate a signed 16-bit integer");
1272 uint_12 = bt_ctf_field_create(uint_12_type);
1273 ok(uint_12, "Instanciate an unsigned 12-bit integer");
10817e06
JG
1274 returned_type = bt_ctf_field_get_type(int_16);
1275 ok(returned_type == int_16_type,
1276 "bt_ctf_field_get_type returns the correct type");
39d74371
JG
1277
1278 /* Can't modify types after instanciating them */
1279 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1280 BT_CTF_INTEGER_BASE_DECIMAL),
1281 "Check an integer type' base can't be modified after instanciation");
1282 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
1283 "Check an integer type's signedness can't be modified after instanciation");
1284
1285 /* Check signed property is checked */
1286 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
1287 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1288 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
1289 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1290
1291 /* Check overflows are properly tested for */
1292 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
1293 "Check -32768 is allowed for a signed 16-bit integer");
1294 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
1295 "Check 32767 is allowed for a signed 16-bit integer");
1296 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
1297 "Check 32768 is not allowed for a signed 16-bit integer");
1298 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
1299 "Check -32769 is not allowed for a signed 16-bit integer");
1300 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
1301 "Check -42 is allowed for a signed 16-bit integer");
1302
1303 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
1304 "Check 4095 is allowed for an unsigned 12-bit integer");
1305 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
1306 "Check 4096 is not allowed for a unsigned 12-bit integer");
1307 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
1308 "Check 0 is allowed for an unsigned 12-bit integer");
1309
1310 string = bt_ctf_field_create(string_type);
1311 ok(string, "Instanciate a string field");
1312 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
1313 "Set a string's value");
1314
0abce37e
JG
1315 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
1316 ok(enumeration_type,
1317 "Create an enumeration type with an unsigned 12-bit integer as container");
1318 enumeration_sequence_type = bt_ctf_field_type_sequence_create(
1319 enumeration_type, "count");
1320 ok(!enumeration_sequence_type,
1321 "Check enumeration types are validated when creating a sequence");
1322 enumeration_array_type = bt_ctf_field_type_array_create(
1323 enumeration_type, 10);
1324 ok(!enumeration_array_type,
1325 "Check enumeration types are validated when creating an array");
1326 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
e6235f1f 1327 enumeration_type, "enumeration"),
0abce37e
JG
1328 "Check enumeration types are validated when adding them as structure members");
1329 enumeration = bt_ctf_field_create(enumeration_type);
1330 ok(!enumeration,
1331 "Check enumeration types are validated before instantiation");
1332
39d74371
JG
1333 bt_ctf_field_put(string);
1334 bt_ctf_field_put(uint_12);
1335 bt_ctf_field_put(int_16);
0abce37e 1336 bt_ctf_field_put(enumeration);
39d74371
JG
1337 bt_ctf_field_type_put(composite_structure_type);
1338 bt_ctf_field_type_put(structure_seq_type);
1339 bt_ctf_field_type_put(string_type);
1340 bt_ctf_field_type_put(sequence_type);
1341 bt_ctf_field_type_put(uint_8_type);
1342 bt_ctf_field_type_put(int_16_type);
1343 bt_ctf_field_type_put(uint_12_type);
0abce37e
JG
1344 bt_ctf_field_type_put(enumeration_type);
1345 bt_ctf_field_type_put(enumeration_sequence_type);
1346 bt_ctf_field_type_put(enumeration_array_type);
7cfd41d6 1347 bt_ctf_field_type_put(returned_type);
39d74371
JG
1348}
1349
1350void packet_resize_test(struct bt_ctf_stream_class *stream_class,
1351 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
1352{
1353 /*
1354 * Append enough events to force the underlying packet to be resized.
1355 * Also tests that a new event can be declared after a stream has been
1356 * instantiated and used/flushed.
1357 */
1358 int ret = 0;
1359 int i;
1360 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
1361 "Spammy_Event");
1362 struct bt_ctf_field_type *integer_type =
1363 bt_ctf_field_type_integer_create(17);
1364 struct bt_ctf_field_type *string_type =
1365 bt_ctf_field_type_string_create();
35e8709f
JG
1366 struct bt_ctf_event *event = NULL;
1367 struct bt_ctf_field *ret_field = NULL;
1368 struct bt_ctf_field_type *ret_field_type = NULL;
6809e227 1369 uint64_t ret_uint64;
12c8a1a3 1370 int events_appended = 0;
35e8709f
JG
1371 struct bt_ctf_field *packet_context = NULL,
1372 *packet_context_field = NULL, *event_context = NULL;
39d74371
JG
1373
1374 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
1375 "field_1");
1376 ret |= bt_ctf_event_class_add_field(event_class, string_type,
1377 "a_string");
1378 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
1379 ok(ret == 0, "Add a new event class to a stream class after writing an event");
1380 if (ret) {
1381 goto end;
1382 }
1383
1ff9582c
JG
1384 event = bt_ctf_event_create(event_class);
1385 ret_field = bt_ctf_event_get_payload_by_index(event, 0);
1386 ret_field_type = bt_ctf_field_get_type(ret_field);
1387 ok(ret_field_type == integer_type,
1388 "bt_ctf_event_get_payload_by_index returns a correct field");
1389 bt_ctf_field_type_put(ret_field_type);
1390 bt_ctf_field_put(ret_field);
1391
1392 ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL,
1393 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1394 ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL,
1395 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1396 bt_ctf_event_put(event);
1397
6e1f8ea1
JG
1398 ok(bt_ctf_stream_get_event_context(NULL) == NULL,
1399 "bt_ctf_stream_get_event_context handles NULL correctly");
1400 event_context = bt_ctf_stream_get_event_context(stream);
1401 ok(event_context,
1402 "bt_ctf_stream_get_event_context returns a stream event context");
1403 ok(bt_ctf_stream_set_event_context(NULL, event_context) < 0,
1404 "bt_ctf_stream_set_event_context handles a NULL stream correctly");
1405 ok(bt_ctf_stream_set_event_context(stream, NULL) < 0,
1406 "bt_ctf_stream_set_event_context handles a NULL stream event context correctly");
1407 ok(!bt_ctf_stream_set_event_context(stream, event_context),
1408 "bt_ctf_stream_set_event_context correctly set a stream event context");
1409 ret_field = bt_ctf_field_create(integer_type);
1410 ok(bt_ctf_stream_set_event_context(stream, ret_field) < 0,
1411 "bt_ctf_stream_set_event_context rejects an event context of incorrect type");
1412 bt_ctf_field_put(ret_field);
1413
39d74371 1414 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
1ff9582c 1415 event = bt_ctf_event_create(event_class);
39d74371
JG
1416 struct bt_ctf_field *integer =
1417 bt_ctf_field_create(integer_type);
1418 struct bt_ctf_field *string =
1419 bt_ctf_field_create(string_type);
1420
1421 ret |= bt_ctf_clock_set_time(clock, ++current_time);
1422 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
1423 ret |= bt_ctf_event_set_payload(event, "field_1",
1424 integer);
1425 bt_ctf_field_put(integer);
1426 ret |= bt_ctf_field_string_set_value(string, "This is a test");
1427 ret |= bt_ctf_event_set_payload(event, "a_string",
1428 string);
1429 bt_ctf_field_put(string);
6e1f8ea1
JG
1430
1431 /* Populate stream event context */
1432 integer = bt_ctf_field_structure_get_field(event_context,
1433 "common_event_context");
1434 ret |= bt_ctf_field_unsigned_integer_set_value(integer,
1435 i % 42);
1436 bt_ctf_field_put(integer);
1437
39d74371
JG
1438 ret |= bt_ctf_stream_append_event(stream, event);
1439 bt_ctf_event_put(event);
1440
1441 if (ret) {
1442 break;
1443 }
1444 }
12c8a1a3 1445
6e1f8ea1 1446 events_appended = !!(i == PACKET_RESIZE_TEST_LENGTH);
3c1d148b 1447 ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0,
6809e227 1448 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
3c1d148b 1449 ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0,
6809e227
JG
1450 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
1451 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1452 ok(ret == 0 && ret_uint64 == 0,
1453 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
1454 bt_ctf_stream_append_discarded_events(stream, 1000);
1455 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1456 ok(ret == 0 && ret_uint64 == 1000,
1457 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
1458
39d74371 1459end:
12c8a1a3
JG
1460 ok(events_appended, "Append 100 000 events to a stream");
1461
1462 /*
1463 * Populate the custom packet context field with a dummy value
1464 * otherwise flush will fail.
1465 */
1466 packet_context = bt_ctf_stream_get_packet_context(stream);
1467 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
35e8709f 1468 "custom_packet_context_field");
12c8a1a3
JG
1469 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2);
1470
39d74371
JG
1471 ok(bt_ctf_stream_flush(stream) == 0,
1472 "Flush a stream that forces a packet resize");
6809e227
JG
1473 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1474 ok(ret == 0 && ret_uint64 == 1000,
1475 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
39d74371
JG
1476 bt_ctf_field_type_put(integer_type);
1477 bt_ctf_field_type_put(string_type);
12c8a1a3
JG
1478 bt_ctf_field_put(packet_context);
1479 bt_ctf_field_put(packet_context_field);
6e1f8ea1 1480 bt_ctf_field_put(event_context);
39d74371
JG
1481 bt_ctf_event_class_put(event_class);
1482}
1483
fdf80f32
JG
1484void test_empty_stream(struct bt_ctf_writer *writer)
1485{
1486 int ret = 0;
1487 struct bt_ctf_trace *trace = NULL;
1488 struct bt_ctf_clock *clock = NULL;
1489 struct bt_ctf_stream_class *stream_class = NULL;
1490 struct bt_ctf_stream *stream = NULL;
1491
1492 trace = bt_ctf_writer_get_trace(writer);
1493 if (!trace) {
1494 diag("Failed to get trace from writer");
1495 ret = -1;
1496 goto end;
1497 }
1498
1499 stream_class = bt_ctf_stream_class_create("empty_stream");
1500 if (!stream_class) {
1501 diag("Failed to create stream class");
1502 ret = -1;
1503 goto end;
1504 }
1505
1506 stream = bt_ctf_writer_create_stream(writer, stream_class);
1507 if (!stream) {
1508 diag("Failed to create writer stream");
1509 ret = -1;
1510 goto end;
1511 }
1512end:
1513 ok(ret == 0,
1514 "Created a stream class with default attributes and an empty stream");
1515 bt_ctf_trace_put(trace);
1516 bt_ctf_clock_put(clock);
1517 bt_ctf_stream_put(stream);
1518 bt_ctf_stream_class_put(stream_class);
1519}
1520
39d74371
JG
1521int main(int argc, char **argv)
1522{
1523 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
1524 char metadata_path[sizeof(trace_path) + 9];
1525 const char *clock_name = "test_clock";
1526 const char *clock_description = "This is a test clock";
5494ce8b
JG
1527 const char *returned_clock_name;
1528 const char *returned_clock_description;
1529 const uint64_t frequency = 1123456789;
39d74371
JG
1530 const uint64_t offset_s = 1351530929945824323;
1531 const uint64_t offset = 1234567;
1532 const uint64_t precision = 10;
5494ce8b 1533 const int is_absolute = 0xFF;
39d74371
JG
1534 char *metadata_string;
1535 struct bt_ctf_writer *writer;
1536 struct utsname name;
22843b66 1537 char hostname[BABELTRACE_HOST_NAME_MAX];
1ff9582c 1538 struct bt_ctf_clock *clock, *ret_clock;
36336d93 1539 struct bt_ctf_stream_class *stream_class, *ret_stream_class;
39d74371 1540 struct bt_ctf_stream *stream1;
e3c971da 1541 const char *ret_string;
e61caf8e
JG
1542 const unsigned char *ret_uuid;
1543 unsigned char tmp_uuid[16] = { 0 };
b34f4d90
JG
1544 struct bt_ctf_field_type *packet_context_type,
1545 *packet_context_field_type,
751b05c7
JG
1546 *packet_header_type,
1547 *packet_header_field_type,
35e8709f
JG
1548 *integer_type,
1549 *stream_event_context_type,
88d26616
JG
1550 *ret_field_type,
1551 *event_header_field_type;
751b05c7 1552 struct bt_ctf_field *packet_header, *packet_header_field;
8cdae8c6 1553 struct bt_ctf_trace *trace;
12c8a1a3 1554 int ret;
4ae7c93b 1555 int64_t ret_int64_t;
39d74371
JG
1556
1557 if (argc < 3) {
1558 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
783c9151 1559 return -1;
39d74371
JG
1560 }
1561
1562 plan_no_plan();
1563
1564 if (!mkdtemp(trace_path)) {
1565 perror("# perror");
1566 }
1567
1568 strcpy(metadata_path, trace_path);
1569 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
1570
1571 writer = bt_ctf_writer_create(trace_path);
1572 ok(writer, "bt_ctf_create succeeds in creating trace with path");
1573
4ae7c93b
JG
1574 ok(!bt_ctf_writer_get_trace(NULL),
1575 "bt_ctf_writer_get_trace correctly handles NULL");
1576 trace = bt_ctf_writer_get_trace(writer);
1577 ok(trace,
1578 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
1579
39d74371 1580 /* Add environment context to the trace */
22843b66
JG
1581 ret = gethostname(hostname, sizeof(hostname));
1582 if (ret < 0) {
1583 return ret;
1584 }
39d74371
JG
1585 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
1586 "Add host (%s) environment field to writer instance",
1587 hostname);
1588 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
1589 "test_value"),
1590 "bt_ctf_writer_add_environment_field error with NULL writer");
1591 ok(bt_ctf_writer_add_environment_field(writer, NULL,
1592 "test_value"),
1593 "bt_ctf_writer_add_environment_field error with NULL field name");
1594 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
1595 NULL),
1596 "bt_ctf_writer_add_environment_field error with NULL field value");
4ae7c93b
JG
1597 ok(bt_ctf_trace_add_environment_field_integer(NULL, "test_env", 0),
1598 "bt_ctf_trace_add_environment_field_integer handles a NULL trace correctly");
1599 ok(bt_ctf_trace_add_environment_field_integer(trace, NULL, 0),
1600 "bt_ctf_trace_add_environment_field_integer handles a NULL environment field name");
1601 ok(!bt_ctf_trace_add_environment_field_integer(trace, "test_env",
1602 123456),
1603 "Add an integer environment field to a trace instance");
1604
1605 /* Test bt_ctf_trace_get_environment_field_count */
1606 ok(bt_ctf_trace_get_environment_field_count(NULL) < 0,
1607 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
1608 ok(bt_ctf_trace_get_environment_field_count(trace) == 2,
1609 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
1610
1611 /* Test bt_ctf_trace_get_environment_field_type */
1612 ok(bt_ctf_trace_get_environment_field_type(trace, 2) ==
1613 BT_ENVIRONMENT_FIELD_TYPE_UNKNOWN,
1614 "bt_ctf_trace_get_environment_field_type handles an invalid index correctly");
1615 ok(bt_ctf_trace_get_environment_field_type(NULL, 0) ==
1616 BT_ENVIRONMENT_FIELD_TYPE_UNKNOWN,
1617 "bt_ctf_trace_get_environment_field_type handles a NULL trace correctly");
1618 ok(bt_ctf_trace_get_environment_field_type(trace, 1) ==
1619 BT_ENVIRONMENT_FIELD_TYPE_INTEGER,
1620 "bt_ctf_trace_get_environment_field_type the correct type of environment field");
1621
1622 /* Test bt_ctf_trace_get_environment_field_name */
1623 ok(bt_ctf_trace_get_environment_field_name(NULL, 0) == NULL,
1624 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
1625 ok(bt_ctf_trace_get_environment_field_name(trace, -1) == NULL,
1626 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly");
1627 ret_string = bt_ctf_trace_get_environment_field_name(trace, 0);
1628 ok(ret_string && !strcmp(ret_string, "host"),
1629 "bt_ctf_trace_get_environment_field_name returns a correct field name");
1630
1631 /* Test bt_ctf_trace_get_environment_field_value_string */
1632 ok(bt_ctf_trace_get_environment_field_value_string(NULL, 0) == NULL,
1633 "bt_ctf_trace_get_environment_field_value_string handles a NULL trace correctly");
1634 ok(bt_ctf_trace_get_environment_field_value_string(trace, -1) == NULL,
1635 "bt_ctf_trace_get_environment_field_value_string handles an invalid index correctly");
1636 ok(bt_ctf_trace_get_environment_field_value_string(trace, 1) == NULL,
1637 "bt_ctf_trace_get_environment_field_value_string validates environment field type");
1638 ret_string = bt_ctf_trace_get_environment_field_value_string(trace, 0);
1639 ok(ret_string && !strcmp(ret_string, hostname),
1640 "bt_ctf_trace_get_environment_field_value_string returns a correct value");
1641
1642 /* Test bt_ctf_trace_get_environment_field_value_integer */
1643 ok(bt_ctf_trace_get_environment_field_value_integer(NULL, 0, &ret_int64_t) < 0,
1644 "bt_ctf_trace_get_environment_field_value_integer handles a NULL trace correctly");
1645 ok(bt_ctf_trace_get_environment_field_value_integer(trace, 42, &ret_int64_t) < 0,
1646 "bt_ctf_trace_get_environment_field_value_integer handles an invalid index correctly");
1647 ok(bt_ctf_trace_get_environment_field_value_integer(trace, 1, NULL) < 0,
1648 "bt_ctf_trace_get_environment_field_value_integer handles a NULL value argument correctly");
1649 ok(bt_ctf_trace_get_environment_field_value_integer(trace, 0, &ret_int64_t) < 0,
1650 "bt_ctf_trace_get_environment_field_value_integer validates environment field type");
1651 ok(!bt_ctf_trace_get_environment_field_value_integer(trace, 1, &ret_int64_t),
1652 "bt_ctf_trace_get_environment_field_value_integer returns a value");
1653 ok(ret_int64_t == 123456,
1654 "bt_ctf_trace_get_environment_field_value_integer returned a correct value");
39d74371
JG
1655
1656 if (uname(&name)) {
1657 perror("uname");
1658 return -1;
1659 }
1660
1661 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
1662 == 0, "Add sysname (%s) environment field to writer instance",
1663 name.sysname);
1664 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
1665 name.nodename) == 0,
1666 "Add nodename (%s) environment field to writer instance",
1667 name.nodename);
1668 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
1669 == 0, "Add release (%s) environment field to writer instance",
1670 name.release);
1671 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
1672 == 0, "Add version (%s) environment field to writer instance",
1673 name.version);
1674 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
1675 == 0, "Add machine (%s) environment field to writer istance",
1676 name.machine);
1677
1678 /* Define a clock and add it to the trace */
5494ce8b
JG
1679 ok(bt_ctf_clock_create("signed") == NULL,
1680 "Illegal clock name rejected");
39d74371
JG
1681 ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
1682 clock = bt_ctf_clock_create(clock_name);
1683 ok(clock, "Clock created sucessfully");
5494ce8b
JG
1684 returned_clock_name = bt_ctf_clock_get_name(clock);
1685 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
d50c7132 1686 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
5494ce8b
JG
1687 "Returned clock name is valid");
1688
1689 returned_clock_description = bt_ctf_clock_get_description(clock);
1690 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
1691 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
1692 "Clock description set successfully");
1693
1694 returned_clock_description = bt_ctf_clock_get_description(clock);
1695 ok(returned_clock_description,
1696 "bt_ctf_clock_get_description returns a description.");
d50c7132
JG
1697 ok(returned_clock_description ?
1698 !strcmp(returned_clock_description, clock_description) : 0,
5494ce8b
JG
1699 "Returned clock description is valid");
1700
1701 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
1702 "bt_ctf_clock_get_frequency returns the correct default frequency");
39d74371
JG
1703 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
1704 "Set clock frequency");
5494ce8b
JG
1705 ok(bt_ctf_clock_get_frequency(clock) == frequency,
1706 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
1707
1708 ok(bt_ctf_clock_get_offset_s(clock) == DEFAULT_CLOCK_OFFSET_S,
1709 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
39d74371
JG
1710 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
1711 "Set clock offset (seconds)");
5494ce8b
JG
1712 ok(bt_ctf_clock_get_offset_s(clock) == offset_s,
1713 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
1714
1715 ok(bt_ctf_clock_get_offset(clock) == DEFAULT_CLOCK_OFFSET,
1716 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
39d74371 1717 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
5494ce8b
JG
1718 ok(bt_ctf_clock_get_offset(clock) == offset,
1719 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
1720
1721 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
1722 "bt_ctf_clock_get_precision returns the correct default precision");
39d74371
JG
1723 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
1724 "Set clock precision");
5494ce8b
JG
1725 ok(bt_ctf_clock_get_precision(clock) == precision,
1726 "bt_ctf_clock_get_precision returns the correct precision once it is set");
1727
1728 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
1729 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
1730 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
39d74371 1731 "Set clock absolute property");
5494ce8b
JG
1732 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
1733 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
1734
1735 ok(bt_ctf_clock_get_time(clock) == DEFAULT_CLOCK_TIME,
1736 "bt_ctf_clock_get_time returns the correct default time");
1737 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
1738 "Set clock time");
1739 ok(bt_ctf_clock_get_time(clock) == current_time,
1740 "bt_ctf_clock_get_time returns the correct time once it is set");
39d74371
JG
1741
1742 ok(bt_ctf_writer_add_clock(writer, clock) == 0,
1743 "Add clock to writer instance");
1744 ok(bt_ctf_writer_add_clock(writer, clock),
1745 "Verify a clock can't be added twice to a writer instance");
1746
8cdae8c6
JG
1747 ok(bt_ctf_trace_get_clock_count(NULL) < 0,
1748 "bt_ctf_trace_get_clock_count correctly handles NULL");
1749 ok(bt_ctf_trace_get_clock_count(trace) == 1,
1750 "bt_ctf_trace_get_clock_count returns the correct number of clocks");
1751 ok(!bt_ctf_trace_get_clock(NULL, 0),
1752 "bt_ctf_trace_get_clock correctly handles NULL");
1753 ok(!bt_ctf_trace_get_clock(trace, -1),
1754 "bt_ctf_trace_get_clock correctly handles negative indexes");
1755 ok(!bt_ctf_trace_get_clock(trace, 1),
1756 "bt_ctf_trace_get_clock correctly handles out of bound accesses");
1757 ret_clock = bt_ctf_trace_get_clock(trace, 0);
1758 ok(ret_clock == clock,
1759 "bt_ctf_trace_get_clock returns the right clock instance");
1760 bt_ctf_clock_put(ret_clock);
1761
5494ce8b
JG
1762 ok(!bt_ctf_clock_get_name(NULL),
1763 "bt_ctf_clock_get_name correctly handles NULL");
1764 ok(!bt_ctf_clock_get_description(NULL),
1765 "bt_ctf_clock_get_description correctly handles NULL");
1766 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
1767 "bt_ctf_clock_get_frequency correctly handles NULL");
1768 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
1769 "bt_ctf_clock_get_precision correctly handles NULL");
1770 ok(bt_ctf_clock_get_offset_s(NULL) == -1ULL,
1771 "bt_ctf_clock_get_offset_s correctly handles NULL");
1772 ok(bt_ctf_clock_get_offset(NULL) == -1ULL,
1773 "bt_ctf_clock_get_offset correctly handles NULL");
3c1d148b 1774 ok(bt_ctf_clock_get_is_absolute(NULL) < 0,
5494ce8b
JG
1775 "bt_ctf_clock_get_is_absolute correctly handles NULL");
1776 ok(bt_ctf_clock_get_time(NULL) == -1ULL,
1777 "bt_ctf_clock_get_time correctly handles NULL");
1778
3c1d148b 1779 ok(bt_ctf_clock_set_description(NULL, NULL) < 0,
5494ce8b 1780 "bt_ctf_clock_set_description correctly handles NULL clock");
3c1d148b 1781 ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0,
5494ce8b 1782 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3c1d148b 1783 ok(bt_ctf_clock_set_precision(NULL, precision) < 0,
5494ce8b 1784 "bt_ctf_clock_get_precision correctly handles NULL clock");
3c1d148b 1785 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0,
5494ce8b 1786 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3c1d148b 1787 ok(bt_ctf_clock_set_offset(NULL, offset) < 0,
5494ce8b 1788 "bt_ctf_clock_set_offset correctly handles NULL clock");
3c1d148b 1789 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0,
5494ce8b 1790 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3c1d148b 1791 ok(bt_ctf_clock_set_time(NULL, current_time) < 0,
5494ce8b 1792 "bt_ctf_clock_set_time correctly handles NULL clock");
e61caf8e
JG
1793 ok(bt_ctf_clock_get_uuid(NULL) == NULL,
1794 "bt_ctf_clock_get_uuid correctly handles NULL clock");
1795 ret_uuid = bt_ctf_clock_get_uuid(clock);
1796 ok(ret_uuid,
1797 "bt_ctf_clock_get_uuid returns a UUID");
1798 if (ret_uuid) {
1799 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
1800 /* Slightly modify UUID */
1801 tmp_uuid[sizeof(tmp_uuid) - 1]++;
1802 }
1803
1804 ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0,
1805 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
1806 ok(bt_ctf_clock_set_uuid(clock, NULL) < 0,
1807 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
1808 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
1809 "bt_ctf_clock_set_uuid sets a new uuid succesfully");
1810 ret_uuid = bt_ctf_clock_get_uuid(clock);
1811 ok(ret_uuid,
1812 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
1813 ok(uuid_match(ret_uuid, tmp_uuid),
1814 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
5494ce8b 1815
39d74371
JG
1816 /* Define a stream class */
1817 stream_class = bt_ctf_stream_class_create("test_stream");
e3c971da
JG
1818
1819 ok(bt_ctf_stream_class_get_name(NULL) == NULL,
1820 "bt_ctf_stream_class_get_name handles NULL correctly");
1821 ret_string = bt_ctf_stream_class_get_name(stream_class);
88d26616 1822 ok(ret_string && !strcmp(ret_string, "test_stream"),
12c8a1a3 1823 "bt_ctf_stream_class_get_name returns a correct stream class name");
e3c971da 1824
1ff9582c
JG
1825 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
1826 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
1827 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
1828 "bt_ctf_stream_class_get_clock handles NULL correctly");
1829
39d74371
JG
1830 ok(stream_class, "Create stream class");
1831 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
1832 "Set a stream class' clock");
1ff9582c
JG
1833 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
1834 ok(ret_clock == clock,
1835 "bt_ctf_stream_class_get_clock returns a correct clock");
1836 bt_ctf_clock_put(ret_clock);
39d74371
JG
1837
1838 /* Test the event fields and event types APIs */
1839 type_field_tests();
1840
1ff9582c
JG
1841 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
1842 "bt_ctf_stream_class_get_id returns an error when no id is set");
1843 ok(bt_ctf_stream_class_get_id(NULL) < 0,
1844 "bt_ctf_stream_class_get_id handles NULL correctly");
1845 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
1846 "bt_ctf_stream_class_set_id handles NULL correctly");
1847 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
1848 "Set an stream class' id");
1849 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
1850 "bt_ctf_stream_class_get_id returns the correct value");
1851
d8469458 1852 /* Validate default event header fields */
88d26616
JG
1853 ok(bt_ctf_stream_class_get_event_header_type(NULL) == NULL,
1854 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
1855 ret_field_type = bt_ctf_stream_class_get_event_header_type(
1856 stream_class);
1857 ok(ret_field_type,
1858 "bt_ctf_stream_class_get_event_header_type returns an event header type");
1859 ok(bt_ctf_field_type_get_type_id(ret_field_type) == CTF_TYPE_STRUCT,
1860 "Default event header type is a structure");
1861 event_header_field_type =
1862 bt_ctf_field_type_structure_get_field_type_by_name(
1863 ret_field_type, "id");
1864 ok(event_header_field_type,
1865 "Default event header type contains an \"id\" field");
1866 ok(bt_ctf_field_type_get_type_id(
1867 event_header_field_type) == CTF_TYPE_INTEGER,
1868 "Default event header \"id\" field is an integer");
1869 bt_ctf_field_type_put(event_header_field_type);
1870 event_header_field_type =
1871 bt_ctf_field_type_structure_get_field_type_by_name(
1872 ret_field_type, "timestamp");
1873 ok(event_header_field_type,
1874 "Default event header type contains a \"timestamp\" field");
1875 ok(bt_ctf_field_type_get_type_id(
1876 event_header_field_type) == CTF_TYPE_INTEGER,
1877 "Default event header \"timestamp\" field is an integer");
1878 bt_ctf_field_type_put(event_header_field_type);
1879 bt_ctf_field_type_put(ret_field_type);
1880
751b05c7
JG
1881 /* Add a custom trace packet header field */
1882 ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL,
1883 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
1884 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
1885 ok(packet_header_type,
1886 "bt_ctf_trace_get_packet_header_type returns a packet header");
1887 ok(bt_ctf_field_type_get_type_id(packet_header_type) == CTF_TYPE_STRUCT,
1888 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
1889 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1890 packet_header_type, "magic");
1891 ok(ret_field_type, "Default packet header type contains a \"magic\" field");
1892 bt_ctf_field_type_put(ret_field_type);
1893 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1894 packet_header_type, "uuid");
1895 ok(ret_field_type, "Default packet header type contains a \"uuid\" field");
1896 bt_ctf_field_type_put(ret_field_type);
1897 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
1898 packet_header_type, "stream_id");
1899 ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
1900 bt_ctf_field_type_put(ret_field_type);
1901
1902 packet_header_field_type = bt_ctf_field_type_integer_create(22);
1903 ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
1904 packet_header_field_type, "custom_trace_packet_header_field"),
1905 "Added a custom trace packet header field successfully");
1906
1907 ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0,
1908 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
1909 ok(bt_ctf_trace_set_packet_header_type(trace, NULL) < 0,
1910 "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
1911 ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type),
1912 "Set a trace packet_header_type successfully");
1913
12c8a1a3
JG
1914 ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL,
1915 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
1916
1917 /* Add a custom field to the stream class' packet context */
1918 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
1919 ok(packet_context_type,
1920 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
1921 ok(bt_ctf_field_type_get_type_id(packet_context_type) == CTF_TYPE_STRUCT,
1922 "Packet context is a structure");
1923
1924 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
1925 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
1926 ok(bt_ctf_stream_class_set_packet_context_type(stream_class, NULL),
1927 "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly");
b34f4d90
JG
1928
1929 integer_type = bt_ctf_field_type_integer_create(32);
1930 ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
1931 integer_type) < 0,
1932 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
88d26616
JG
1933 /* Create a "uint5_t" equivalent custom packet context field */
1934 packet_context_field_type = bt_ctf_field_type_integer_create(5);
1935
12c8a1a3 1936 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
35e8709f 1937 packet_context_field_type, "custom_packet_context_field");
12c8a1a3
JG
1938 ok(ret == 0, "Packet context field added successfully");
1939
35e8709f
JG
1940 /* Define a stream event context containing a my_integer field. */
1941 ok(bt_ctf_stream_class_get_event_context_type(NULL) == NULL,
1942 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
1943 ok(bt_ctf_stream_class_get_event_context_type(
1944 stream_class) == NULL,
1945 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
1946 stream_event_context_type = bt_ctf_field_type_structure_create();
1947 bt_ctf_field_type_structure_add_field(stream_event_context_type,
1948 integer_type, "common_event_context");
1949
1950 ok(bt_ctf_stream_class_set_event_context_type(NULL,
1951 stream_event_context_type) < 0,
1952 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
1953 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
1954 NULL) < 0,
1955 "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly");
1956 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
1957 integer_type) < 0,
1958 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
1959
1960 ok(bt_ctf_stream_class_set_event_context_type(
1961 stream_class, stream_event_context_type) == 0,
1962 "Set a new stream event context type");
1963 ret_field_type = bt_ctf_stream_class_get_event_context_type(
1964 stream_class);
1965 ok(ret_field_type == stream_event_context_type,
1966 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
751b05c7 1967 bt_ctf_field_type_put(ret_field_type);
12c8a1a3 1968
39d74371
JG
1969 /* Instantiate a stream and append events */
1970 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
1971 ok(stream1, "Instanciate a stream class from writer");
1972
36336d93
JG
1973 ok(bt_ctf_stream_get_class(NULL) == NULL,
1974 "bt_ctf_stream_get_class correctly handles NULL");
1975 ret_stream_class = bt_ctf_stream_get_class(stream1);
1976 ok(ret_stream_class,
1977 "bt_ctf_stream_get_class returns a stream class");
1978 ok(ret_stream_class == stream_class,
1979 "Returned stream class is of the correct type");
1980
751b05c7
JG
1981 /*
1982 * Try to modify the packet context type after a stream has been
1983 * created.
1984 */
1985 ret = bt_ctf_field_type_structure_add_field(packet_header_type,
1986 packet_header_field_type, "should_fail");
1987 ok(ret < 0,
1988 "Trace packet header type can't be modified once a stream has been instanciated");
1989
12c8a1a3
JG
1990 /*
1991 * Try to modify the packet context type after a stream has been
1992 * created.
1993 */
1994 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
1995 packet_context_field_type, "should_fail");
1996 ok(ret < 0,
751b05c7 1997 "Packet context type can't be modified once a stream has been instanciated");
12c8a1a3 1998
35e8709f
JG
1999 /*
2000 * Try to modify the stream event context type after a stream has been
2001 * created.
2002 */
2003 ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
2004 integer_type, "should_fail");
2005 ok(ret < 0,
751b05c7 2006 "Stream event context type can't be modified once a stream has been instanciated");
35e8709f
JG
2007
2008 /* Should fail after instanciating a stream (frozen) */
39d74371
JG
2009 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
2010 "Changes to a stream class that was already instantiated fail");
2011
751b05c7
JG
2012 /* Populate the custom packet header field only once for all tests */
2013 ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
2014 "bt_ctf_stream_get_packet_header handles NULL correctly");
2015 packet_header = bt_ctf_stream_get_packet_header(stream1);
2016 ok(packet_header,
2017 "bt_ctf_stream_get_packet_header returns a packet header");
2018 ret_field_type = bt_ctf_field_get_type(packet_header);
2019 ok(ret_field_type == packet_header_type,
2020 "Stream returns a packet header of the appropriate type");
2021 bt_ctf_field_type_put(ret_field_type);
2022 packet_header_field = bt_ctf_field_structure_get_field(packet_header,
2023 "custom_trace_packet_header_field");
2024 ok(packet_header_field,
2025 "Packet header structure contains a custom field with the appropriate name");
2026 ret_field_type = bt_ctf_field_get_type(packet_header_field);
2027 ok(ret_field_type == packet_header_field_type,
2028 "Custom packet header field is of the expected type");
2029 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field,
2030 54321), "Set custom packet header value successfully");
2031 ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
2032 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
2033 ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
2034 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
2035 ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
2036 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
2037 ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
2038 "Successfully set a stream's packet header");
2039
39d74371
JG
2040 append_simple_event(stream_class, stream1, clock);
2041
2042 packet_resize_test(stream_class, stream1, clock);
2043
2044 append_complex_event(stream_class, stream1, clock);
2045
fdf80f32
JG
2046 test_empty_stream(writer);
2047
39d74371
JG
2048 metadata_string = bt_ctf_writer_get_metadata_string(writer);
2049 ok(metadata_string, "Get metadata string");
2050
2051 bt_ctf_writer_flush_metadata(writer);
2052 validate_metadata(argv[1], metadata_path);
2053 validate_trace(argv[2], trace_path);
2054
2055 bt_ctf_clock_put(clock);
2056 bt_ctf_stream_class_put(stream_class);
36336d93 2057 bt_ctf_stream_class_put(ret_stream_class);
39d74371
JG
2058 bt_ctf_writer_put(writer);
2059 bt_ctf_stream_put(stream1);
12c8a1a3
JG
2060 bt_ctf_field_type_put(packet_context_type);
2061 bt_ctf_field_type_put(packet_context_field_type);
b34f4d90 2062 bt_ctf_field_type_put(integer_type);
35e8709f
JG
2063 bt_ctf_field_type_put(stream_event_context_type);
2064 bt_ctf_field_type_put(ret_field_type);
751b05c7
JG
2065 bt_ctf_field_type_put(packet_header_type);
2066 bt_ctf_field_type_put(packet_header_field_type);
2067 bt_ctf_field_put(packet_header);
2068 bt_ctf_field_put(packet_header_field);
8cdae8c6 2069 bt_ctf_trace_put(trace);
39d74371
JG
2070 free(metadata_string);
2071
2072 /* Remove all trace files and delete temporary trace directory */
2073 DIR *trace_dir = opendir(trace_path);
2074 if (!trace_dir) {
2075 perror("# opendir");
2076 return -1;
2077 }
2078
2079 struct dirent *entry;
2080 while ((entry = readdir(trace_dir))) {
2081 if (entry->d_type == DT_REG) {
543409b0 2082 unlinkat(dirfd(trace_dir), entry->d_name, 0);
39d74371
JG
2083 }
2084 }
2085
2086 rmdir(trace_path);
2087 closedir(trace_dir);
39d74371
JG
2088 return 0;
2089}
This page took 0.120685 seconds and 4 git commands to generate.