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