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