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