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