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