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