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