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