Tests: Add stream getter tests
[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;
7cfd41d6
JG
290
291 ok(uint_12_type, "Create an unsigned integer type");
292
293 bt_ctf_field_type_integer_set_signed(int_64_type, 1);
294 ok(int_64_type, "Create a signed integer type");
295 enum_type = bt_ctf_field_type_enumeration_create(int_64_type);
296
297 returned_type = bt_ctf_field_type_enumeration_get_container_type(enum_type);
298 ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_type returns the right type");
299 ok(!bt_ctf_field_type_enumeration_get_container_type(NULL), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly");
300 ok(!bt_ctf_field_type_enumeration_create(enum_type),
301 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
39d74371
JG
302
303 bt_ctf_field_type_set_alignment(float_type, 32);
7cfd41d6
JG
304 ok(bt_ctf_field_type_get_alignment(NULL) < 0,
305 "bt_ctf_field_type_get_alignment handles NULL correctly");
306 ok(bt_ctf_field_type_get_alignment(float_type) == 32,
307 "bt_ctf_field_type_get_alignment returns a correct value");
308
309 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0,
310 "Set a floating point type's exponent digit count");
311 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0,
312 "Set a floating point type's mantissa digit count");
313
314 ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL) < 0,
315 "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly");
316 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL) < 0,
317 "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly");
318 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11,
319 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
320 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53,
321 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
8382544f
JG
322
323 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
7cfd41d6
JG
324 mapping_name_negative_test, -12345, 0) == 0,
325 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
8382544f 326 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
7cfd41d6
JG
327 "escaping; \"test\"", 1, 1) == 0,
328 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
329 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
330 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
331 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
8382544f
JG
332 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
333 "event clock int float", 5, 22) == 0,
334 "Accept enumeration mapping strings containing reserved keywords");
7cfd41d6
JG
335 bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
336 42, 42);
337 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
338 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names");
339 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something",
340 -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries");
341 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
342 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
343 bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000);
344
345 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL, -42, &ret_size_t) < 0,
346 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly");
347 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -42, NULL) < 0,
348 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL index correctly");
349 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, 1000000, &ret_size_t) < 0,
350 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
351 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -55, &ret_size_t) == 0,
352 "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly");
353 ok(ret_size_t == 1,
354 "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index");
355
8382544f 356 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
7cfd41d6
JG
357 "enum_field") == 0, "Add signed enumeration field to event");
358
359 ok(bt_ctf_field_type_enumeration_get_mapping(NULL, 0, &ret_char,
360 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
361 "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly");
362 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, NULL,
363 &ret_range_start_int64_t, &ret_range_end_int64_t) < 0,
364 "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly");
365 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
366 NULL, &ret_range_end_int64_t) < 0,
367 "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly");
368 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char,
369 &ret_range_start_int64_t, NULL) < 0,
370 "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly");
371 ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 5, &ret_char,
372 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
373 "bt_ctf_field_type_enumeration_get_mapping returns a value");
374 ok(!strcmp(ret_char, mapping_name_test),
375 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name");
376 ok(ret_range_start_int64_t == 42,
377 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start");
378 ok(ret_range_end_int64_t == 42,
379 "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end");
380
381 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
382 "escaping; \"test\"", 0, 0) == 0,
383 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes");
384 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
385 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
386 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters");
387 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned,
388 "event clock int float", 5, 22) == 0,
389 "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords");
390 bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
391 42, 42);
392 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
393 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names");
394 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something",
395 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries");
396 ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test,
397 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start");
398 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned,
399 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
400
401 ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL) < 0,
402 "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly");
403 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 4,
404 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
405
406 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char,
407 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
408 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly");
409 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL,
410 &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0,
411 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly");
412 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
413 NULL, &ret_range_end_uint64_t) < 0,
414 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly");
415 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char,
416 &ret_range_start_uint64_t, NULL) < 0,
417 "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly");
418 ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 3, &ret_char,
419 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
420 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value");
421 ok(!strcmp(ret_char, mapping_name_test),
422 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name");
423 ok(ret_range_start_uint64_t == 42,
424 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start");
425 ok(ret_range_end_uint64_t == 42,
426 "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end");
8382544f 427
39d74371
JG
428 bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
429 "integer_field");
430 bt_ctf_event_class_add_field(simple_event_class, float_type,
431 "float_field");
432 bt_ctf_stream_class_add_event_class(stream_class,
433 simple_event_class);
434
e3c971da
JG
435 ok(bt_ctf_stream_class_get_event_class_count(NULL) < 0,
436 "bt_ctf_stream_class_get_event_class_count handles NULL correctly");
437 ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1,
438 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
439 ok(bt_ctf_stream_class_get_event_class(NULL, 0) == NULL,
440 "bt_ctf_stream_class_get_event_class handles NULL correctly");
441 ok(bt_ctf_stream_class_get_event_class(stream_class, 8724) == NULL,
442 "bt_ctf_stream_class_get_event_class handles invalid indexes correctly");
443 ret_event_class = bt_ctf_stream_class_get_event_class(stream_class, 0);
444 ok(ret_event_class == simple_event_class,
445 "bt_ctf_stream_class_get_event_class returns the correct event class");
446 bt_ctf_event_class_put(ret_event_class);
447
448 ok(bt_ctf_stream_class_get_event_class_by_name(NULL, "some event name") == NULL,
449 "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly");
450 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, NULL) == NULL,
451 "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly");
452 ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, "some event name") == NULL,
453 "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly");
454 ret_event_class = bt_ctf_stream_class_get_event_class_by_name(stream_class, "Simple Event");
455 ok(ret_event_class == simple_event_class,
456 "bt_ctf_stream_class_get_event_class_by_name returns a correct event class");
457 bt_ctf_event_class_put(ret_event_class);
458
39d74371 459 simple_event = bt_ctf_event_create(simple_event_class);
39d74371
JG
460 ok(simple_event,
461 "Instantiate an event containing a single integer field");
462
1ff9582c
JG
463 ok(bt_ctf_event_get_clock(NULL) == NULL,
464 "bt_ctf_event_get_clock handles NULL correctly");
465 ret_clock = bt_ctf_event_get_clock(simple_event);
466 ok(ret_clock == clock,
467 "bt_ctf_event_get_clock returns a correct clock");
468 bt_ctf_clock_put(clock);
469
39d74371
JG
470 integer_field = bt_ctf_field_create(uint_12_type);
471 bt_ctf_field_unsigned_integer_set_value(integer_field, 42);
472 ok(bt_ctf_event_set_payload(simple_event, "integer_field",
473 integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
474
475 float_field = bt_ctf_event_get_payload(simple_event, "float_field");
10817e06
JG
476 ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double),
477 "bt_ctf_field_floating_point_get_value fails on an unset float field");
478 bt_ctf_field_floating_point_set_value(float_field, double_test_value);
479 ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double),
480 "bt_ctf_field_floating_point_get_value properly handles a NULL field");
481 ok(bt_ctf_field_floating_point_get_value(float_field, NULL),
482 "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer");
483 ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double),
484 "bt_ctf_field_floating_point_get_value returns a double value");
485 ok(fabs(ret_double - double_test_value) <= DBL_EPSILON,
486 "bt_ctf_field_floating_point_get_value returns a correct value");
487
8382544f 488 enum_field = bt_ctf_field_create(enum_type);
7cfd41d6
JG
489 ret_char = bt_ctf_field_enumeration_get_mapping_name(NULL);
490 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly");
491 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
492 ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset");
8382544f
JG
493 enum_container_field = bt_ctf_field_enumeration_get_container(
494 enum_field);
7cfd41d6
JG
495 ok(bt_ctf_field_signed_integer_set_value(
496 enum_container_field, -42) == 0,
497 "Set signed enumeration container value");
498 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field);
499 ok(!strcmp(ret_char, mapping_name_negative_test),
500 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container");
8382544f 501 bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
39d74371 502
7cfd41d6
JG
503 enum_field_unsigned = bt_ctf_field_create(enum_type_unsigned);
504 enum_container_field_unsigned = bt_ctf_field_enumeration_get_container(
505 enum_field_unsigned);
506 ok(bt_ctf_field_unsigned_integer_set_value(
507 enum_container_field_unsigned, 42) == 0,
508 "Set unsigned enumeration container value");
509 bt_ctf_event_set_payload(simple_event, "enum_field_unsigned",
510 enum_field_unsigned);
511 ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned);
512 ok(!strcmp(ret_char, mapping_name_test),
513 "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container");
514
39d74371
JG
515 ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
516
517 ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
518 "Append simple event to trace stream");
519
520 ok(bt_ctf_stream_flush(stream) == 0,
521 "Flush trace stream with one event");
522
523 bt_ctf_event_class_put(simple_event_class);
524 bt_ctf_event_put(simple_event);
525 bt_ctf_field_type_put(uint_12_type);
7cfd41d6 526 bt_ctf_field_type_put(int_64_type);
39d74371 527 bt_ctf_field_type_put(float_type);
8382544f 528 bt_ctf_field_type_put(enum_type);
7cfd41d6
JG
529 bt_ctf_field_type_put(enum_type_unsigned);
530 bt_ctf_field_type_put(returned_type);
39d74371
JG
531 bt_ctf_field_put(integer_field);
532 bt_ctf_field_put(float_field);
8382544f 533 bt_ctf_field_put(enum_field);
7cfd41d6 534 bt_ctf_field_put(enum_field_unsigned);
8382544f 535 bt_ctf_field_put(enum_container_field);
7cfd41d6 536 bt_ctf_field_put(enum_container_field_unsigned);
39d74371
JG
537}
538
539void append_complex_event(struct bt_ctf_stream_class *stream_class,
540 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
541{
542 int i;
1ff9582c 543 const char *complex_test_event_string = "Complex Test Event";
10817e06 544 const char *test_string = "Test string";
39d74371
JG
545 struct bt_ctf_field_type *uint_35_type =
546 bt_ctf_field_type_integer_create(35);
547 struct bt_ctf_field_type *int_16_type =
548 bt_ctf_field_type_integer_create(16);
549 struct bt_ctf_field_type *uint_3_type =
550 bt_ctf_field_type_integer_create(3);
551 struct bt_ctf_field_type *enum_variant_type =
552 bt_ctf_field_type_enumeration_create(uint_3_type);
553 struct bt_ctf_field_type *variant_type =
554 bt_ctf_field_type_variant_create(enum_variant_type,
555 "variant_selector");
556 struct bt_ctf_field_type *string_type =
557 bt_ctf_field_type_string_create();
558 struct bt_ctf_field_type *sequence_type;
1fac895e 559 struct bt_ctf_field_type *array_type;
39d74371
JG
560 struct bt_ctf_field_type *inner_structure_type =
561 bt_ctf_field_type_structure_create();
562 struct bt_ctf_field_type *complex_structure_type =
563 bt_ctf_field_type_structure_create();
7cfd41d6 564 struct bt_ctf_field_type *ret_field_type;
39d74371
JG
565 struct bt_ctf_event_class *event_class;
566 struct bt_ctf_event *event;
567 struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
568 *inner_structure_field, *complex_structure_field,
569 *a_sequence_field, *enum_variant_field, *enum_container_field,
1fac895e 570 *variant_field, *an_array_field, *ret_field;
10817e06 571 uint64_t ret_unsigned_int;
1fac895e 572 int64_t ret_signed_int;
10817e06 573 const char *ret_string;
7cfd41d6 574 size_t ret_size_t;
1ff9582c
JG
575 struct bt_ctf_stream_class *ret_stream_class;
576 struct bt_ctf_event_class *ret_event_class;
39d74371
JG
577
578 bt_ctf_field_type_set_alignment(int_16_type, 32);
579 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
580 bt_ctf_field_type_integer_set_base(uint_35_type,
581 BT_CTF_INTEGER_BASE_HEXADECIMAL);
582
1fac895e 583 array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH);
39d74371
JG
584 sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
585 "seq_len");
7cfd41d6
JG
586
587 ok(bt_ctf_field_type_array_get_element_type(NULL) == NULL,
588 "bt_ctf_field_type_array_get_element_type handles NULL correctly");
589 ret_field_type = bt_ctf_field_type_array_get_element_type(
590 array_type);
591 ok(ret_field_type == int_16_type,
592 "bt_ctf_field_type_array_get_element_type returns the correct type");
593 bt_ctf_field_type_put(ret_field_type);
594
595 ok(bt_ctf_field_type_array_get_length(NULL) < 0,
596 "bt_ctf_field_type_array_get_length handles NULL correctly");
597 ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH,
598 "bt_ctf_field_type_array_get_length returns the correct length");
599
39d74371
JG
600 bt_ctf_field_type_structure_add_field(inner_structure_type,
601 uint_35_type, "seq_len");
602 bt_ctf_field_type_structure_add_field(inner_structure_type,
603 sequence_type, "a_sequence");
1fac895e
JG
604 bt_ctf_field_type_structure_add_field(inner_structure_type,
605 array_type, "an_array");
39d74371
JG
606
607 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
608 "UINT3_TYPE", 0, 0);
609 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
610 "INT16_TYPE", 1, 1);
611 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
612 "UINT35_TYPE", 2, 7);
7cfd41d6
JG
613
614 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL, "INT16_TYPE",
615 &ret_size_t) < 0,
616 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly");
617 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type, NULL,
618 &ret_size_t) < 0,
619 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly");
620 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type, "INT16_TYPE",
621 NULL) < 0,
622 "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL index correctly");
623 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type, "INT16_TYPE",
624 &ret_size_t) == 0,
625 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns a value");
626 ok(ret_size_t == 1,
627 "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index");
628
629 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL, 1, &ret_size_t) < 0,
630 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly");
631 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 1, NULL) < 0,
632 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL index correctly");
633 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, -42, &ret_size_t) < 0,
634 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
635 ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 5, &ret_size_t) == 0,
636 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly");
637 ok(ret_size_t == 2,
638 "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index");
639
39d74371
JG
640 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
641 "An unknown entry"), "Reject a variant field based on an unknown tag value");
642 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
643 "UINT3_TYPE") == 0, "Add a field to a variant");
644 bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
645 "INT16_TYPE");
646 bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
647 "UINT35_TYPE");
648
7cfd41d6
JG
649 ok(bt_ctf_field_type_variant_get_tag_type(NULL) == NULL,
650 "bt_ctf_field_type_variant_get_tag_type handles NULL correctly");
651 ret_field_type = bt_ctf_field_type_variant_get_tag_type(variant_type);
652 ok(ret_field_type == enum_variant_type,
653 "bt_ctf_field_type_variant_get_tag_type returns a correct tag type");
654 bt_ctf_field_type_put(ret_field_type);
655
656 ok(bt_ctf_field_type_variant_get_tag_name(NULL) == NULL,
657 "bt_ctf_field_type_variant_get_tag_name handles NULL correctly");
658 ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type);
659 ok(!strcmp(ret_string, "variant_selector"),
660 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
661 ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL,
662 "INT16_TYPE") == NULL,
663 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly");
664 ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type,
665 NULL) == NULL,
666 "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly");
667 ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name(
668 variant_type, "INT16_TYPE");
669 ok(ret_field_type == int_16_type,
670 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
671 bt_ctf_field_type_put(ret_field_type);
672
673 ok(bt_ctf_field_type_variant_get_field_count(NULL) < 0,
674 "bt_ctf_field_type_variant_get_field_count handles NULL correctly");
675 ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3,
676 "bt_ctf_field_type_variant_get_field_count returns the correct count");
677
678 ok(bt_ctf_field_type_variant_get_field(NULL, &ret_string, &ret_field_type, 0) < 0,
679 "bt_ctf_field_type_variant_get_field handles a NULL type correctly");
680 ok(bt_ctf_field_type_variant_get_field(variant_type, NULL, &ret_field_type, 0) < 0,
681 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
682 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, NULL, 0) < 0,
683 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
684 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 200) < 0,
685 "bt_ctf_field_type_variant_get_field handles an invalid index correctly");
686 ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 1) == 0,
687 "bt_ctf_field_type_variant_get_field returns a field");
688 ok(!strcmp("INT16_TYPE", ret_string),
689 "bt_ctf_field_type_variant_get_field returns a correct field name");
690 ok(ret_field_type == int_16_type,
691 "bt_ctf_field_type_variant_get_field returns a correct field type");
692 bt_ctf_field_type_put(ret_field_type);
693
39d74371
JG
694 bt_ctf_field_type_structure_add_field(complex_structure_type,
695 enum_variant_type, "variant_selector");
696 bt_ctf_field_type_structure_add_field(complex_structure_type,
697 string_type, "a_string");
698 bt_ctf_field_type_structure_add_field(complex_structure_type,
699 variant_type, "variant_value");
700 bt_ctf_field_type_structure_add_field(complex_structure_type,
701 inner_structure_type, "inner_structure");
702
703 ok(bt_ctf_event_class_create("clock") == NULL,
704 "Reject creation of an event class with an illegal name");
1ff9582c 705 event_class = bt_ctf_event_class_create(complex_test_event_string);
39d74371
JG
706 ok(event_class, "Create an event class");
707 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
708 "Reject addition of a field with an empty name to an event");
709 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
710 "Reject addition of a field with a NULL type to an event");
711 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
712 "int"),
713 "Reject addition of a type with an illegal name to an event");
714 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
715 "uint_35") == 0,
716 "Add field of type unsigned integer to an event");
717 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
718 "int_16") == 0, "Add field of type signed integer to an event");
719 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
720 "complex_structure") == 0,
721 "Add composite structure to an event");
722
1ff9582c
JG
723 ok(bt_ctf_event_class_get_name(NULL) == NULL,
724 "bt_ctf_event_class_get_name handles NULL correctly");
725 ret_string = bt_ctf_event_class_get_name(event_class);
726 ok(!strcmp(ret_string, complex_test_event_string),
727 "bt_ctf_event_class_get_name returns a correct name");
728 ok(bt_ctf_event_class_get_id(event_class) < 0,
729 "bt_ctf_event_class_get_id returns a negative value when not set");
730 ok(bt_ctf_event_class_get_id(NULL) < 0,
731 "bt_ctf_event_class_get_id handles NULL correctly");
732 ok(bt_ctf_event_class_set_id(NULL, 42) < 0,
733 "bt_ctf_event_class_set_id handles NULL correctly");
734 ok(bt_ctf_event_class_set_id(event_class, 42) == 0,
735 "Set an event class' id");
736 ok(bt_ctf_event_class_get_id(event_class) == 42,
737 "bt_ctf_event_class_get_id returns the correct value");
738
39d74371
JG
739 /* Add event class to the stream class */
740 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
741 "Reject addition of NULL event class to a stream class");
742 ok(bt_ctf_stream_class_add_event_class(stream_class,
743 event_class) == 0, "Add an event class to stream class");
744
1ff9582c
JG
745 ok(bt_ctf_event_class_get_stream_class(NULL) == NULL,
746 "bt_ctf_event_class_get_stream_class handles NULL correctly");
747 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
748 ok(ret_stream_class == stream_class,
749 "bt_ctf_event_class_get_stream_class returns the correct stream class");
750 bt_ctf_stream_class_put(ret_stream_class);
751
752 ok(bt_ctf_event_class_get_field_count(NULL) < 0,
753 "bt_ctf_event_class_get_field_count handles NULL correctly");
754 ok(bt_ctf_event_class_get_field_count(event_class) == 3,
755 "bt_ctf_event_class_get_field_count returns a correct value");
756
757 ok(bt_ctf_event_class_get_field(NULL, &ret_string,
758 &ret_field_type, 0) < 0,
759 "bt_ctf_event_class_get_field handles a NULL event class correctly");
760 ok(bt_ctf_event_class_get_field(event_class, NULL,
761 &ret_field_type, 0) < 0,
762 "bt_ctf_event_class_get_field handles a NULL field name correctly");
763 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
764 NULL, 0) < 0,
765 "bt_ctf_event_class_get_field handles a NULL field type correctly");
766 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
767 &ret_field_type, 42) < 0,
768 "bt_ctf_event_class_get_field handles an invalid index correctly");
769 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
770 &ret_field_type, 0) == 0,
771 "bt_ctf_event_class_get_field returns a field");
772 ok(ret_field_type == uint_35_type,
773 "bt_ctf_event_class_get_field returns a correct field type");
774 bt_ctf_field_type_put(ret_field_type);
775 ok(!strcmp(ret_string, "uint_35"),
776 "bt_ctf_event_class_get_field returns a correct field name");
777 ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL,
778 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
779 ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL,
780 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
781 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
782 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
783 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
784 "complex_structure");
785 ok(ret_field_type == complex_structure_type,
786 "bt_ctf_event_class_get_field_by_name returns a correct field type");
787 bt_ctf_field_type_put(ret_field_type);
788
39d74371
JG
789 event = bt_ctf_event_create(event_class);
790 ok(event, "Instanciate a complex event");
791
1ff9582c
JG
792 ok(bt_ctf_event_get_class(NULL) == NULL,
793 "bt_ctf_event_get_class handles NULL correctly");
794 ret_event_class = bt_ctf_event_get_class(event);
795 ok(ret_event_class == event_class,
796 "bt_ctf_event_get_class returns the correct event class");
797 bt_ctf_event_class_put(ret_event_class);
798
39d74371 799 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
7cfd41d6 800 if (!uint_35_field) {
10817e06 801 printf("uint_35_field is NULL\n");
7cfd41d6
JG
802 }
803
39d74371
JG
804 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
805 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
10817e06
JG
806 ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) == -1,
807 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
808 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) == -1,
809 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
810 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
811 &ret_unsigned_int) == 0,
812 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
813 ok(ret_unsigned_int == 0x0DDF00D,
814 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
815 ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
816 &ret_signed_int) == -1,
817 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
39d74371
JG
818 bt_ctf_field_put(uint_35_field);
819
820 int_16_field = bt_ctf_event_get_payload(event, "int_16");
821 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
10817e06
JG
822 ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) == -1,
823 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
824 ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) == -1,
825 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
826 ok(bt_ctf_field_signed_integer_get_value(int_16_field,
827 &ret_signed_int) == 0,
828 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
829 ok(ret_signed_int == -12345,
830 "bt_ctf_field_signed_integer_get_value returns the correct value");
831 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
832 &ret_unsigned_int) == -1,
833 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
39d74371
JG
834 bt_ctf_field_put(int_16_field);
835
836 complex_structure_field = bt_ctf_event_get_payload(event,
837 "complex_structure");
10817e06
JG
838
839 ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
840 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
841 ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
842 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
843 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
844 complex_structure_field, 3);
845 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
846 bt_ctf_field_put(inner_structure_field);
847 ok(ret_field_type == inner_structure_type,
848 "bt_ctf_field_structure_get_field_by_index returns a correct field");
849 bt_ctf_field_type_put(ret_field_type);
850
39d74371
JG
851 inner_structure_field = bt_ctf_field_structure_get_field(
852 complex_structure_field, "inner_structure");
853 a_string_field = bt_ctf_field_structure_get_field(
854 complex_structure_field, "a_string");
855 enum_variant_field = bt_ctf_field_structure_get_field(
856 complex_structure_field, "variant_selector");
857 variant_field = bt_ctf_field_structure_get_field(
858 complex_structure_field, "variant_value");
859 uint_35_field = bt_ctf_field_structure_get_field(
860 inner_structure_field, "seq_len");
861 a_sequence_field = bt_ctf_field_structure_get_field(
862 inner_structure_field, "a_sequence");
1fac895e
JG
863 an_array_field = bt_ctf_field_structure_get_field(
864 inner_structure_field, "an_array");
39d74371
JG
865
866 enum_container_field = bt_ctf_field_enumeration_get_container(
867 enum_variant_field);
868 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
869 int_16_field = bt_ctf_field_variant_get_field(variant_field,
870 enum_variant_field);
871 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
872 bt_ctf_field_put(int_16_field);
10817e06
JG
873 ok(!bt_ctf_field_string_get_value(a_string_field),
874 "bt_ctf_field_string_get_value returns NULL on an unset field");
39d74371 875 bt_ctf_field_string_set_value(a_string_field,
10817e06
JG
876 test_string);
877 ok(!bt_ctf_field_string_get_value(NULL),
878 "bt_ctf_field_string_get_value correctly handles NULL");
879 ret_string = bt_ctf_field_string_get_value(a_string_field);
880 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
881 ok(!strcmp(ret_string, test_string),
882 "bt_ctf_field_string_get_value returns a correct value");
39d74371
JG
883 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
884 SEQUENCE_TEST_LENGTH);
10817e06 885
7cfd41d6
JG
886 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL,
887 enum_container_field) == NULL,
888 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
889 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type,
890 NULL) == NULL,
891 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
892 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
893 variant_type, enum_variant_field);
894 ok(ret_field_type == int_16_type,
895 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
896
10817e06
JG
897 ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
898 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
39d74371
JG
899 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
900 uint_35_field) == 0, "Set a sequence field's length");
cd95e351
JG
901 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
902 ok(ret_field == uint_35_field,
903 "bt_ctf_field_sequence_get_length returns the correct length field");
904 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
905 "bt_ctf_field_sequence_get_length properly handles NULL");
39d74371
JG
906
907 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
908 int_16_field = bt_ctf_field_sequence_get_field(
909 a_sequence_field, i);
910 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
911 bt_ctf_field_put(int_16_field);
912 }
913
1fac895e
JG
914 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
915 int_16_field = bt_ctf_field_array_get_field(
916 an_array_field, i);
917 bt_ctf_field_signed_integer_set_value(int_16_field, i);
918 bt_ctf_field_put(int_16_field);
919 }
920
39d74371
JG
921 bt_ctf_clock_set_time(clock, ++current_time);
922 ok(bt_ctf_stream_append_event(stream, event) == 0,
923 "Append a complex event to a stream");
924 ok(bt_ctf_stream_flush(stream) == 0,
925 "Flush a stream containing a complex event");
926
927 bt_ctf_field_put(uint_35_field);
928 bt_ctf_field_put(a_string_field);
929 bt_ctf_field_put(inner_structure_field);
930 bt_ctf_field_put(complex_structure_field);
931 bt_ctf_field_put(a_sequence_field);
1fac895e 932 bt_ctf_field_put(an_array_field);
39d74371
JG
933 bt_ctf_field_put(enum_variant_field);
934 bt_ctf_field_put(enum_container_field);
935 bt_ctf_field_put(variant_field);
cd95e351 936 bt_ctf_field_put(ret_field);
39d74371
JG
937 bt_ctf_field_type_put(uint_35_type);
938 bt_ctf_field_type_put(int_16_type);
939 bt_ctf_field_type_put(string_type);
940 bt_ctf_field_type_put(sequence_type);
1fac895e 941 bt_ctf_field_type_put(array_type);
39d74371
JG
942 bt_ctf_field_type_put(inner_structure_type);
943 bt_ctf_field_type_put(complex_structure_type);
944 bt_ctf_field_type_put(uint_3_type);
945 bt_ctf_field_type_put(enum_variant_type);
946 bt_ctf_field_type_put(variant_type);
7cfd41d6 947 bt_ctf_field_type_put(ret_field_type);
39d74371
JG
948 bt_ctf_event_class_put(event_class);
949 bt_ctf_event_put(event);
950}
951
952void type_field_tests()
953{
954 struct bt_ctf_field *uint_12;
955 struct bt_ctf_field *int_16;
956 struct bt_ctf_field *string;
0abce37e 957 struct bt_ctf_field *enumeration;
39d74371
JG
958 struct bt_ctf_field_type *composite_structure_type;
959 struct bt_ctf_field_type *structure_seq_type;
960 struct bt_ctf_field_type *string_type;
961 struct bt_ctf_field_type *sequence_type;
962 struct bt_ctf_field_type *uint_8_type;
963 struct bt_ctf_field_type *int_16_type;
964 struct bt_ctf_field_type *uint_12_type =
965 bt_ctf_field_type_integer_create(12);
0abce37e
JG
966 struct bt_ctf_field_type *enumeration_type;
967 struct bt_ctf_field_type *enumeration_sequence_type;
968 struct bt_ctf_field_type *enumeration_array_type;
10817e06 969 struct bt_ctf_field_type *returned_type;
7cfd41d6 970 const char *ret_string;
10817e06
JG
971
972 returned_type = bt_ctf_field_get_type(NULL);
973 ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
39d74371
JG
974
975 ok(uint_12_type, "Create an unsigned integer type");
976 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
977 BT_CTF_INTEGER_BASE_BINARY) == 0,
978 "Set integer type's base as binary");
979 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
980 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
981 "Set integer type's base as decimal");
982 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
983 BT_CTF_INTEGER_BASE_UNKNOWN),
984 "Reject integer type's base set as unknown");
985 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
986 BT_CTF_INTEGER_BASE_OCTAL) == 0,
987 "Set integer type's base as octal");
988 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
989 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
990 "Set integer type's base as hexadecimal");
991 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
992 "Reject unknown integer base value");
993 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
994 "Set integer type signedness to signed");
995 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
996 "Set integer type signedness to unsigned");
7cfd41d6
JG
997 ok(bt_ctf_field_type_integer_get_size(NULL) < 0,
998 "bt_ctf_field_type_integer_get_size handles NULL correctly");
999 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
1000 "bt_ctf_field_type_integer_get_size returns a correct value");
1001 ok(bt_ctf_field_type_integer_get_signed(NULL) < 0,
1002 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1003 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
1004 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1005
1006 ok(bt_ctf_field_type_set_byte_order(NULL,
1007 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
1008 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1009 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1010 (enum bt_ctf_byte_order) 42) < 0,
1011 "bt_ctf_field_type_set_byte_order rejects invalid values");
1012 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1013 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
1014 "Set an integer's byte order to little endian");
1015 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1016 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1017 "Set an integer's byte order to big endian");
1018 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
1019 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1020 "bt_ctf_field_type_get_byte_order returns a correct value");
1021 ok(bt_ctf_field_type_get_byte_order(NULL) ==
1022 BT_CTF_BYTE_ORDER_UNKNOWN,
1023 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1024
1025 ok(bt_ctf_field_type_get_type_id(NULL) ==
1026 CTF_TYPE_UNKNOWN,
1027 "bt_ctf_field_type_get_type_id handles NULL correctly");
1028 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
1029 CTF_TYPE_INTEGER,
1030 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1031
1032 ok(bt_ctf_field_type_integer_get_base(NULL) ==
1033 BT_CTF_INTEGER_BASE_UNKNOWN,
1034 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1035 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
1036 BT_CTF_INTEGER_BASE_HEXADECIMAL,
1037 "bt_ctf_field_type_integer_get_base returns a correct value");
1038
1039 ok(bt_ctf_field_type_integer_set_encoding(NULL, CTF_STRING_ASCII) < 0,
1040 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1041 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1042 (enum ctf_string_encoding) 123) < 0,
1043 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1044 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1045 CTF_STRING_UTF8) == 0,
1046 "Set integer type encoding to UTF8");
1047 ok(bt_ctf_field_type_integer_get_encoding(NULL) == CTF_STRING_UNKNOWN,
1048 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1049 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) == CTF_STRING_UTF8,
1050 "bt_ctf_field_type_integer_get_encoding returns a correct value");
39d74371
JG
1051
1052 int_16_type = bt_ctf_field_type_integer_create(16);
1053 bt_ctf_field_type_integer_set_signed(int_16_type, 1);
7cfd41d6
JG
1054 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
1055 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
39d74371
JG
1056 uint_8_type = bt_ctf_field_type_integer_create(8);
1057 sequence_type =
1058 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
1059 ok(sequence_type, "Create a sequence of int16_t type");
7cfd41d6
JG
1060 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
1061 CTF_TYPE_SEQUENCE,
1062 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1063
1064 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL,
1065 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1066 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
1067 sequence_type);
1068 ok(!strcmp(ret_string, "seq_len"),
1069 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1070 ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL,
1071 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1072 returned_type = bt_ctf_field_type_sequence_get_element_type(
1073 sequence_type);
1074 ok(returned_type == int_16_type,
1075 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1076 bt_ctf_field_type_put(returned_type);
39d74371
JG
1077
1078 string_type = bt_ctf_field_type_string_create();
1079 ok(string_type, "Create a string type");
1080 ok(bt_ctf_field_type_string_set_encoding(string_type,
1081 CTF_STRING_NONE),
1082 "Reject invalid \"None\" string encoding");
1083 ok(bt_ctf_field_type_string_set_encoding(string_type,
1084 42),
1085 "Reject invalid string encoding");
1086 ok(bt_ctf_field_type_string_set_encoding(string_type,
1087 CTF_STRING_ASCII) == 0,
1088 "Set string encoding to ASCII");
1089
7cfd41d6
JG
1090 ok(bt_ctf_field_type_string_get_encoding(NULL) ==
1091 CTF_STRING_UNKNOWN,
1092 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1093 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
1094 CTF_STRING_ASCII,
1095 "bt_ctf_field_type_string_get_encoding returns the correct value");
1096
39d74371 1097 structure_seq_type = bt_ctf_field_type_structure_create();
7cfd41d6
JG
1098 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
1099 CTF_TYPE_STRUCT,
1100 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
39d74371
JG
1101 ok(structure_seq_type, "Create a structure type");
1102 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1103 uint_8_type, "seq_len") == 0,
1104 "Add a uint8_t type to a structure");
1105 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1106 sequence_type, "a_sequence") == 0,
1107 "Add a sequence type to a structure");
7cfd41d6
JG
1108
1109 ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0,
1110 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1111 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
1112 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1113
1114 ok(bt_ctf_field_type_structure_get_field(NULL,
1115 &ret_string, &returned_type, 1) < 0,
1116 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1117 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1118 NULL, &returned_type, 1) < 0,
1119 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1120 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1121 &ret_string, NULL, 1) < 0,
1122 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1123 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1124 &ret_string, &returned_type, 10) < 0,
1125 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1126 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1127 &ret_string, &returned_type, 1) == 0,
1128 "bt_ctf_field_type_structure_get_field returns a field");
1129 ok(!strcmp(ret_string, "a_sequence"),
1130 "bt_ctf_field_type_structure_get_field returns a correct field name");
1131 ok(returned_type == sequence_type,
1132 "bt_ctf_field_type_structure_get_field returns a correct field type");
1133 bt_ctf_field_type_put(returned_type);
1134
1135 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL,
1136 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1137 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL,
1138 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1139 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1140 structure_seq_type, "a_sequence");
1141 ok(returned_type == sequence_type,
1142 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1143 bt_ctf_field_type_put(returned_type);
1144
39d74371
JG
1145 composite_structure_type = bt_ctf_field_type_structure_create();
1146 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1147 string_type, "a_string") == 0,
1148 "Add a string type to a structure");
1149 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1150 structure_seq_type, "inner_structure") == 0,
1151 "Add a structure type to a structure");
1152
7cfd41d6
JG
1153 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1154 NULL, "a_sequence") == NULL,
1155 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1156 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1157 structure_seq_type, NULL) == NULL,
1158 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1159 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1160 structure_seq_type, "a_sequence");
1161 ok(returned_type == sequence_type,
1162 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1163 bt_ctf_field_type_put(returned_type);
1164
39d74371
JG
1165 int_16 = bt_ctf_field_create(int_16_type);
1166 ok(int_16, "Instanciate a signed 16-bit integer");
1167 uint_12 = bt_ctf_field_create(uint_12_type);
1168 ok(uint_12, "Instanciate an unsigned 12-bit integer");
10817e06
JG
1169 returned_type = bt_ctf_field_get_type(int_16);
1170 ok(returned_type == int_16_type,
1171 "bt_ctf_field_get_type returns the correct type");
39d74371
JG
1172
1173 /* Can't modify types after instanciating them */
1174 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1175 BT_CTF_INTEGER_BASE_DECIMAL),
1176 "Check an integer type' base can't be modified after instanciation");
1177 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
1178 "Check an integer type's signedness can't be modified after instanciation");
1179
1180 /* Check signed property is checked */
1181 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
1182 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1183 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
1184 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1185
1186 /* Check overflows are properly tested for */
1187 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
1188 "Check -32768 is allowed for a signed 16-bit integer");
1189 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
1190 "Check 32767 is allowed for a signed 16-bit integer");
1191 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
1192 "Check 32768 is not allowed for a signed 16-bit integer");
1193 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
1194 "Check -32769 is not allowed for a signed 16-bit integer");
1195 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
1196 "Check -42 is allowed for a signed 16-bit integer");
1197
1198 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
1199 "Check 4095 is allowed for an unsigned 12-bit integer");
1200 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
1201 "Check 4096 is not allowed for a unsigned 12-bit integer");
1202 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
1203 "Check 0 is allowed for an unsigned 12-bit integer");
1204
1205 string = bt_ctf_field_create(string_type);
1206 ok(string, "Instanciate a string field");
1207 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
1208 "Set a string's value");
1209
0abce37e
JG
1210 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
1211 ok(enumeration_type,
1212 "Create an enumeration type with an unsigned 12-bit integer as container");
1213 enumeration_sequence_type = bt_ctf_field_type_sequence_create(
1214 enumeration_type, "count");
1215 ok(!enumeration_sequence_type,
1216 "Check enumeration types are validated when creating a sequence");
1217 enumeration_array_type = bt_ctf_field_type_array_create(
1218 enumeration_type, 10);
1219 ok(!enumeration_array_type,
1220 "Check enumeration types are validated when creating an array");
1221 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1222 enumeration_type, "enumeration") == 0,
1223 "Check enumeration types are validated when adding them as structure members");
1224 enumeration = bt_ctf_field_create(enumeration_type);
1225 ok(!enumeration,
1226 "Check enumeration types are validated before instantiation");
1227
39d74371
JG
1228 bt_ctf_field_put(string);
1229 bt_ctf_field_put(uint_12);
1230 bt_ctf_field_put(int_16);
0abce37e 1231 bt_ctf_field_put(enumeration);
39d74371
JG
1232 bt_ctf_field_type_put(composite_structure_type);
1233 bt_ctf_field_type_put(structure_seq_type);
1234 bt_ctf_field_type_put(string_type);
1235 bt_ctf_field_type_put(sequence_type);
1236 bt_ctf_field_type_put(uint_8_type);
1237 bt_ctf_field_type_put(int_16_type);
1238 bt_ctf_field_type_put(uint_12_type);
0abce37e
JG
1239 bt_ctf_field_type_put(enumeration_type);
1240 bt_ctf_field_type_put(enumeration_sequence_type);
1241 bt_ctf_field_type_put(enumeration_array_type);
7cfd41d6 1242 bt_ctf_field_type_put(returned_type);
39d74371
JG
1243}
1244
1245void packet_resize_test(struct bt_ctf_stream_class *stream_class,
1246 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
1247{
1248 /*
1249 * Append enough events to force the underlying packet to be resized.
1250 * Also tests that a new event can be declared after a stream has been
1251 * instantiated and used/flushed.
1252 */
1253 int ret = 0;
1254 int i;
1255 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
1256 "Spammy_Event");
1257 struct bt_ctf_field_type *integer_type =
1258 bt_ctf_field_type_integer_create(17);
1259 struct bt_ctf_field_type *string_type =
1260 bt_ctf_field_type_string_create();
1ff9582c
JG
1261 struct bt_ctf_event *event;
1262 struct bt_ctf_field *ret_field;
1263 struct bt_ctf_field_type *ret_field_type;
6809e227 1264 uint64_t ret_uint64;
39d74371
JG
1265
1266 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
1267 "field_1");
1268 ret |= bt_ctf_event_class_add_field(event_class, string_type,
1269 "a_string");
1270 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
1271 ok(ret == 0, "Add a new event class to a stream class after writing an event");
1272 if (ret) {
1273 goto end;
1274 }
1275
1ff9582c
JG
1276 event = bt_ctf_event_create(event_class);
1277 ret_field = bt_ctf_event_get_payload_by_index(event, 0);
1278 ret_field_type = bt_ctf_field_get_type(ret_field);
1279 ok(ret_field_type == integer_type,
1280 "bt_ctf_event_get_payload_by_index returns a correct field");
1281 bt_ctf_field_type_put(ret_field_type);
1282 bt_ctf_field_put(ret_field);
1283
1284 ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL,
1285 "bt_ctf_event_get_payload_by_index handles NULL correctly");
1286 ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL,
1287 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
1288 bt_ctf_event_put(event);
1289
39d74371 1290 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
1ff9582c 1291 event = bt_ctf_event_create(event_class);
39d74371
JG
1292 struct bt_ctf_field *integer =
1293 bt_ctf_field_create(integer_type);
1294 struct bt_ctf_field *string =
1295 bt_ctf_field_create(string_type);
1296
1297 ret |= bt_ctf_clock_set_time(clock, ++current_time);
1298 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
1299 ret |= bt_ctf_event_set_payload(event, "field_1",
1300 integer);
1301 bt_ctf_field_put(integer);
1302 ret |= bt_ctf_field_string_set_value(string, "This is a test");
1303 ret |= bt_ctf_event_set_payload(event, "a_string",
1304 string);
1305 bt_ctf_field_put(string);
1306 ret |= bt_ctf_stream_append_event(stream, event);
1307 bt_ctf_event_put(event);
1308
1309 if (ret) {
1310 break;
1311 }
1312 }
6809e227
JG
1313
1314 ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) == -1,
1315 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
1316 ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) == -1,
1317 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
1318 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1319 ok(ret == 0 && ret_uint64 == 0,
1320 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
1321 bt_ctf_stream_append_discarded_events(stream, 1000);
1322 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1323 ok(ret == 0 && ret_uint64 == 1000,
1324 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
1325
39d74371
JG
1326end:
1327 ok(ret == 0, "Append 100 000 events to a stream");
1328 ok(bt_ctf_stream_flush(stream) == 0,
1329 "Flush a stream that forces a packet resize");
6809e227
JG
1330 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
1331 ok(ret == 0 && ret_uint64 == 1000,
1332 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
39d74371
JG
1333 bt_ctf_field_type_put(integer_type);
1334 bt_ctf_field_type_put(string_type);
1335 bt_ctf_event_class_put(event_class);
1336}
1337
1338int main(int argc, char **argv)
1339{
1340 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
1341 char metadata_path[sizeof(trace_path) + 9];
1342 const char *clock_name = "test_clock";
1343 const char *clock_description = "This is a test clock";
5494ce8b
JG
1344 const char *returned_clock_name;
1345 const char *returned_clock_description;
1346 const uint64_t frequency = 1123456789;
39d74371
JG
1347 const uint64_t offset_s = 1351530929945824323;
1348 const uint64_t offset = 1234567;
1349 const uint64_t precision = 10;
5494ce8b 1350 const int is_absolute = 0xFF;
39d74371
JG
1351 char *metadata_string;
1352 struct bt_ctf_writer *writer;
1353 struct utsname name;
1354 char hostname[HOST_NAME_MAX];
1ff9582c 1355 struct bt_ctf_clock *clock, *ret_clock;
39d74371
JG
1356 struct bt_ctf_stream_class *stream_class;
1357 struct bt_ctf_stream *stream1;
e3c971da 1358 const char *ret_string;
39d74371
JG
1359
1360 if (argc < 3) {
1361 printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n");
1362 exit(-1);
1363 }
1364
1365 plan_no_plan();
1366
1367 if (!mkdtemp(trace_path)) {
1368 perror("# perror");
1369 }
1370
1371 strcpy(metadata_path, trace_path);
1372 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
1373
1374 writer = bt_ctf_writer_create(trace_path);
1375 ok(writer, "bt_ctf_create succeeds in creating trace with path");
1376
1377 /* Add environment context to the trace */
1378 gethostname(hostname, HOST_NAME_MAX);
1379 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
1380 "Add host (%s) environment field to writer instance",
1381 hostname);
1382 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
1383 "test_value"),
1384 "bt_ctf_writer_add_environment_field error with NULL writer");
1385 ok(bt_ctf_writer_add_environment_field(writer, NULL,
1386 "test_value"),
1387 "bt_ctf_writer_add_environment_field error with NULL field name");
1388 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
1389 NULL),
1390 "bt_ctf_writer_add_environment_field error with NULL field value");
1391
1392 if (uname(&name)) {
1393 perror("uname");
1394 return -1;
1395 }
1396
1397 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
1398 == 0, "Add sysname (%s) environment field to writer instance",
1399 name.sysname);
1400 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
1401 name.nodename) == 0,
1402 "Add nodename (%s) environment field to writer instance",
1403 name.nodename);
1404 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
1405 == 0, "Add release (%s) environment field to writer instance",
1406 name.release);
1407 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
1408 == 0, "Add version (%s) environment field to writer instance",
1409 name.version);
1410 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
1411 == 0, "Add machine (%s) environment field to writer istance",
1412 name.machine);
1413
1414 /* Define a clock and add it to the trace */
5494ce8b
JG
1415 ok(bt_ctf_clock_create("signed") == NULL,
1416 "Illegal clock name rejected");
39d74371
JG
1417 ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected");
1418 clock = bt_ctf_clock_create(clock_name);
1419 ok(clock, "Clock created sucessfully");
5494ce8b
JG
1420 returned_clock_name = bt_ctf_clock_get_name(clock);
1421 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
1422 ok(strcmp(returned_clock_name, clock_name) == 0,
1423 "Returned clock name is valid");
1424
1425 returned_clock_description = bt_ctf_clock_get_description(clock);
1426 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
1427 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
1428 "Clock description set successfully");
1429
1430 returned_clock_description = bt_ctf_clock_get_description(clock);
1431 ok(returned_clock_description,
1432 "bt_ctf_clock_get_description returns a description.");
1433 ok(strcmp(returned_clock_description, clock_description) == 0,
1434 "Returned clock description is valid");
1435
1436 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
1437 "bt_ctf_clock_get_frequency returns the correct default frequency");
39d74371
JG
1438 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
1439 "Set clock frequency");
5494ce8b
JG
1440 ok(bt_ctf_clock_get_frequency(clock) == frequency,
1441 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
1442
1443 ok(bt_ctf_clock_get_offset_s(clock) == DEFAULT_CLOCK_OFFSET_S,
1444 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
39d74371
JG
1445 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
1446 "Set clock offset (seconds)");
5494ce8b
JG
1447 ok(bt_ctf_clock_get_offset_s(clock) == offset_s,
1448 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
1449
1450 ok(bt_ctf_clock_get_offset(clock) == DEFAULT_CLOCK_OFFSET,
1451 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)");
39d74371 1452 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
5494ce8b
JG
1453 ok(bt_ctf_clock_get_offset(clock) == offset,
1454 "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set");
1455
1456 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
1457 "bt_ctf_clock_get_precision returns the correct default precision");
39d74371
JG
1458 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
1459 "Set clock precision");
5494ce8b
JG
1460 ok(bt_ctf_clock_get_precision(clock) == precision,
1461 "bt_ctf_clock_get_precision returns the correct precision once it is set");
1462
1463 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
1464 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
1465 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
39d74371 1466 "Set clock absolute property");
5494ce8b
JG
1467 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
1468 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
1469
1470 ok(bt_ctf_clock_get_time(clock) == DEFAULT_CLOCK_TIME,
1471 "bt_ctf_clock_get_time returns the correct default time");
1472 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
1473 "Set clock time");
1474 ok(bt_ctf_clock_get_time(clock) == current_time,
1475 "bt_ctf_clock_get_time returns the correct time once it is set");
39d74371
JG
1476
1477 ok(bt_ctf_writer_add_clock(writer, clock) == 0,
1478 "Add clock to writer instance");
1479 ok(bt_ctf_writer_add_clock(writer, clock),
1480 "Verify a clock can't be added twice to a writer instance");
1481
5494ce8b
JG
1482 ok(!bt_ctf_clock_get_name(NULL),
1483 "bt_ctf_clock_get_name correctly handles NULL");
1484 ok(!bt_ctf_clock_get_description(NULL),
1485 "bt_ctf_clock_get_description correctly handles NULL");
1486 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
1487 "bt_ctf_clock_get_frequency correctly handles NULL");
1488 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
1489 "bt_ctf_clock_get_precision correctly handles NULL");
1490 ok(bt_ctf_clock_get_offset_s(NULL) == -1ULL,
1491 "bt_ctf_clock_get_offset_s correctly handles NULL");
1492 ok(bt_ctf_clock_get_offset(NULL) == -1ULL,
1493 "bt_ctf_clock_get_offset correctly handles NULL");
1494 ok(bt_ctf_clock_get_is_absolute(NULL) == -1,
1495 "bt_ctf_clock_get_is_absolute correctly handles NULL");
1496 ok(bt_ctf_clock_get_time(NULL) == -1ULL,
1497 "bt_ctf_clock_get_time correctly handles NULL");
1498
1499 ok(bt_ctf_clock_set_description(NULL, NULL) == -1,
1500 "bt_ctf_clock_set_description correctly handles NULL clock");
1501 ok(bt_ctf_clock_set_frequency(NULL, frequency) == -1,
1502 "bt_ctf_clock_set_frequency correctly handles NULL clock");
1503 ok(bt_ctf_clock_set_precision(NULL, precision) == -1,
1504 "bt_ctf_clock_get_precision correctly handles NULL clock");
1505 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) == -1,
1506 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
1507 ok(bt_ctf_clock_set_offset(NULL, offset) == -1,
1508 "bt_ctf_clock_set_offset correctly handles NULL clock");
1509 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) == -1,
1510 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
1511 ok(bt_ctf_clock_set_time(NULL, current_time) == -1,
1512 "bt_ctf_clock_set_time correctly handles NULL clock");
1513
39d74371
JG
1514 /* Define a stream class */
1515 stream_class = bt_ctf_stream_class_create("test_stream");
e3c971da
JG
1516
1517 ok(bt_ctf_stream_class_get_name(NULL) == NULL,
1518 "bt_ctf_stream_class_get_name handles NULL correctly");
1519 ret_string = bt_ctf_stream_class_get_name(stream_class);
1520 ok(!strcmp(ret_string, "test_stream"),
1521 "bt_ctf_stream_class_get_name returns a correct stream class name");
1522
1ff9582c
JG
1523 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
1524 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
1525 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
1526 "bt_ctf_stream_class_get_clock handles NULL correctly");
1527
39d74371
JG
1528 ok(stream_class, "Create stream class");
1529 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
1530 "Set a stream class' clock");
1ff9582c
JG
1531 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
1532 ok(ret_clock == clock,
1533 "bt_ctf_stream_class_get_clock returns a correct clock");
1534 bt_ctf_clock_put(ret_clock);
39d74371
JG
1535
1536 /* Test the event fields and event types APIs */
1537 type_field_tests();
1538
1ff9582c
JG
1539 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
1540 "bt_ctf_stream_class_get_id returns an error when no id is set");
1541 ok(bt_ctf_stream_class_get_id(NULL) < 0,
1542 "bt_ctf_stream_class_get_id handles NULL correctly");
1543 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
1544 "bt_ctf_stream_class_set_id handles NULL correctly");
1545 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
1546 "Set an stream class' id");
1547 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
1548 "bt_ctf_stream_class_get_id returns the correct value");
1549
39d74371
JG
1550 /* Instantiate a stream and append events */
1551 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
1552 ok(stream1, "Instanciate a stream class from writer");
1553
1554 /* Should fail after instanciating a stream (locked)*/
1555 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
1556 "Changes to a stream class that was already instantiated fail");
1557
1558 append_simple_event(stream_class, stream1, clock);
1559
1560 packet_resize_test(stream_class, stream1, clock);
1561
1562 append_complex_event(stream_class, stream1, clock);
1563
1564 metadata_string = bt_ctf_writer_get_metadata_string(writer);
1565 ok(metadata_string, "Get metadata string");
1566
1567 bt_ctf_writer_flush_metadata(writer);
1568 validate_metadata(argv[1], metadata_path);
1569 validate_trace(argv[2], trace_path);
1570
1571 bt_ctf_clock_put(clock);
1572 bt_ctf_stream_class_put(stream_class);
1573 bt_ctf_writer_put(writer);
1574 bt_ctf_stream_put(stream1);
1575 free(metadata_string);
1576
1577 /* Remove all trace files and delete temporary trace directory */
1578 DIR *trace_dir = opendir(trace_path);
1579 if (!trace_dir) {
1580 perror("# opendir");
1581 return -1;
1582 }
1583
1584 struct dirent *entry;
1585 while ((entry = readdir(trace_dir))) {
1586 if (entry->d_type == DT_REG) {
1587 unlinkat(dirfd(trace_dir), entry->d_name, 0);
1588 }
1589 }
1590
1591 rmdir(trace_path);
1592 closedir(trace_dir);
1593
1594 return 0;
1595}
This page took 0.092526 seconds and 4 git commands to generate.