Add static trace tests
[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(NULL, 0) == NULL,
420 "bt_ctf_stream_class_get_event_class handles NULL correctly");
421 ok(bt_ctf_stream_class_get_event_class(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(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(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(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(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(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(event_class,
908 i);
909 obj = bt_ctf_event_class_get_attribute_value(event_class, i);
910 assert(ret_string && obj);
911
912 if (!strcmp(ret_string, "id")) {
913 attrs_count.id++;
914 ok(bt_value_is_integer(obj),
915 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
916 ret_string);
917 } else if (!strcmp(ret_string, "name")) {
918 attrs_count.name++;
919 ok(bt_value_is_string(obj),
920 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
921 ret_string);
922 } else if (!strcmp(ret_string, "loglevel")) {
923 attrs_count.loglevel++;
924 ok(bt_value_is_integer(obj),
925 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
926 ret_string);
927 } else if (!strcmp(ret_string, "model.emf.uri")) {
928 attrs_count.modelemfuri++;
929 ok(bt_value_is_string(obj),
930 "bt_ctf_event_class_get_attribute_value returns the correct type (\"%s\")",
931 ret_string);
932 } else {
933 attrs_count.unknown++;
934 }
935
936 BT_PUT(obj);
937 }
938
939 ok(attrs_count.unknown == 0, "event class has no unknown attributes");
940 ok(attrs_count.id == 1 && attrs_count.name == 1 &&
941 attrs_count.loglevel == 1 && attrs_count.modelemfuri == 1,
942 "event class has one instance of each known attribute");
943
944 /* Add event class to the stream class */
945 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
946 "Reject addition of NULL event class to a stream class");
947 ok(bt_ctf_stream_class_add_event_class(stream_class,
948 event_class) == 0, "Add an event class to stream class");
949
950 ok(bt_ctf_event_class_get_stream_class(NULL) == NULL,
951 "bt_ctf_event_class_get_stream_class handles NULL correctly");
952 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
953 ok(ret_stream_class == stream_class,
954 "bt_ctf_event_class_get_stream_class returns the correct stream class");
955 bt_put(ret_stream_class);
956
957 ok(bt_ctf_event_class_get_field_count(NULL) < 0,
958 "bt_ctf_event_class_get_field_count handles NULL correctly");
959 ok(bt_ctf_event_class_get_field_count(event_class) == 3,
960 "bt_ctf_event_class_get_field_count returns a correct value");
961
962 ok(bt_ctf_event_class_get_field(NULL, &ret_string,
963 &ret_field_type, 0) < 0,
964 "bt_ctf_event_class_get_field handles a NULL event class correctly");
965 ok(bt_ctf_event_class_get_field(event_class, NULL,
966 &ret_field_type, 0) == 0,
967 "bt_ctf_event_class_get_field handles a NULL field name correctly");
968 bt_put(ret_field_type);
969 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
970 NULL, 0) == 0,
971 "bt_ctf_event_class_get_field handles a NULL field type correctly");
972 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
973 &ret_field_type, 42) < 0,
974 "bt_ctf_event_class_get_field handles an invalid index correctly");
975 ok(bt_ctf_event_class_get_field(event_class, &ret_string,
976 &ret_field_type, 0) == 0,
977 "bt_ctf_event_class_get_field returns a field");
978 ok(bt_ctf_field_type_compare(ret_field_type, uint_35_type) == 0,
979 "bt_ctf_event_class_get_field returns a correct field type");
980 bt_put(ret_field_type);
981 ok(!strcmp(ret_string, "uint_35"),
982 "bt_ctf_event_class_get_field returns a correct field name");
983 ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL,
984 "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly");
985 ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL,
986 "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly");
987 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
988 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
989 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
990 "complex_structure");
991 ok(bt_ctf_field_type_compare(ret_field_type, complex_structure_type) == 0,
992 "bt_ctf_event_class_get_field_by_name returns a correct field type");
993 bt_put(ret_field_type);
994
995 event = bt_ctf_event_create(event_class);
996 ok(event, "Instanciate a complex event");
997
998 ok(bt_ctf_event_get_class(NULL) == NULL,
999 "bt_ctf_event_get_class handles NULL correctly");
1000 ret_event_class = bt_ctf_event_get_class(event);
1001 ok(ret_event_class == event_class,
1002 "bt_ctf_event_get_class returns the correct event class");
1003 bt_put(ret_event_class);
1004
1005 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
1006 if (!uint_35_field) {
1007 printf("uint_35_field is NULL\n");
1008 }
1009
1010 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
1011 bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D);
1012 ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0,
1013 "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field.");
1014 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0,
1015 "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value");
1016 ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field,
1017 &ret_unsigned_int) == 0,
1018 "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value");
1019 ok(ret_unsigned_int == 0x0DDF00D,
1020 "bt_ctf_field_unsigned_integer_get_value returns the correct value");
1021 ok(bt_ctf_field_signed_integer_get_value(uint_35_field,
1022 &ret_signed_int) < 0,
1023 "bt_ctf_field_signed_integer_get_value fails on an unsigned field");
1024 bt_put(uint_35_field);
1025
1026 int_16_field = bt_ctf_event_get_payload(event, "int_16");
1027 bt_ctf_field_signed_integer_set_value(int_16_field, -12345);
1028 ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0,
1029 "bt_ctf_field_signed_integer_get_value properly handles a NULL field");
1030 ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0,
1031 "bt_ctf_field_signed_integer_get_value properly handles a NULL return value");
1032 ok(bt_ctf_field_signed_integer_get_value(int_16_field,
1033 &ret_signed_int) == 0,
1034 "bt_ctf_field_signed_integer_get_value succeeds after setting a value");
1035 ok(ret_signed_int == -12345,
1036 "bt_ctf_field_signed_integer_get_value returns the correct value");
1037 ok(bt_ctf_field_unsigned_integer_get_value(int_16_field,
1038 &ret_unsigned_int) < 0,
1039 "bt_ctf_field_unsigned_integer_get_value fails on a signed field");
1040 bt_put(int_16_field);
1041
1042 complex_structure_field = bt_ctf_event_get_payload(event,
1043 "complex_structure");
1044
1045 ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL,
1046 "bt_ctf_field_structure_get_field_by_index handles NULL correctly");
1047 ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL,
1048 "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly");
1049 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
1050 complex_structure_field, 3);
1051 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
1052 bt_put(inner_structure_field);
1053 ok(bt_ctf_field_type_compare(ret_field_type, inner_structure_type) == 0,
1054 "bt_ctf_field_structure_get_field_by_index returns a correct field");
1055 bt_put(ret_field_type);
1056
1057 inner_structure_field = bt_ctf_field_structure_get_field(
1058 complex_structure_field, "inner_structure");
1059 a_string_field = bt_ctf_field_structure_get_field(
1060 complex_structure_field, "a_string");
1061 enum_variant_field = bt_ctf_field_structure_get_field(
1062 complex_structure_field, "variant_selector");
1063 variant_field = bt_ctf_field_structure_get_field(
1064 complex_structure_field, "variant_value");
1065 uint_35_field = bt_ctf_field_structure_get_field(
1066 inner_structure_field, "seq_len");
1067 a_sequence_field = bt_ctf_field_structure_get_field(
1068 inner_structure_field, "a_sequence");
1069 an_array_field = bt_ctf_field_structure_get_field(
1070 inner_structure_field, "an_array");
1071
1072 enum_container_field = bt_ctf_field_enumeration_get_container(
1073 enum_variant_field);
1074 bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1);
1075 int_16_field = bt_ctf_field_variant_get_field(variant_field,
1076 enum_variant_field);
1077 bt_ctf_field_signed_integer_set_value(int_16_field, -200);
1078 bt_put(int_16_field);
1079 ok(!bt_ctf_field_string_get_value(a_string_field),
1080 "bt_ctf_field_string_get_value returns NULL on an unset field");
1081 bt_ctf_field_string_set_value(a_string_field,
1082 test_string_1);
1083 ok(!bt_ctf_field_string_get_value(NULL),
1084 "bt_ctf_field_string_get_value correctly handles NULL");
1085 ok(bt_ctf_field_string_append(NULL, "yeah"),
1086 "bt_ctf_field_string_append correctly handles a NULL string field");
1087 ok(bt_ctf_field_string_append(a_string_field, NULL),
1088 "bt_ctf_field_string_append correctly handles a NULL string value");
1089 ok(!bt_ctf_field_string_append(a_string_field, test_string_2),
1090 "bt_ctf_field_string_append succeeds");
1091 ok(bt_ctf_field_string_append_len(NULL, "oh noes", 3),
1092 "bt_ctf_field_string_append_len correctly handles a NULL string field");
1093 ok(bt_ctf_field_string_append_len(a_string_field, NULL, 3),
1094 "bt_ctf_field_string_append_len correctly handles a NULL string value");
1095 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 5),
1096 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
1097 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_4, 10),
1098 "bt_ctf_field_string_append_len succeeds (append 4 characters)");
1099 ok(!bt_ctf_field_string_append_len(a_string_field, &test_string_4[4], 3),
1100 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1101 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 0),
1102 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
1103
1104 ret_string = bt_ctf_field_string_get_value(a_string_field);
1105 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
1106 ok(ret_string ? !strcmp(ret_string, test_string_cat) : 0,
1107 "bt_ctf_field_string_get_value returns a correct value");
1108 bt_ctf_field_unsigned_integer_set_value(uint_35_field,
1109 SEQUENCE_TEST_LENGTH);
1110
1111 ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL,
1112 enum_container_field) == NULL,
1113 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly");
1114 ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type,
1115 NULL) == NULL,
1116 "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly");
1117 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
1118 variant_type, enum_variant_field);
1119 ok(ret_field_type == int_16_type,
1120 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
1121
1122 ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL,
1123 "bt_ctf_field_sequence_get_length returns NULL when length is unset");
1124 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
1125 uint_35_field) == 0, "Set a sequence field's length");
1126 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
1127 ok(ret_field == uint_35_field,
1128 "bt_ctf_field_sequence_get_length returns the correct length field");
1129 ok(bt_ctf_field_sequence_get_length(NULL) == NULL,
1130 "bt_ctf_field_sequence_get_length properly handles NULL");
1131
1132 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
1133 int_16_field = bt_ctf_field_sequence_get_field(
1134 a_sequence_field, i);
1135 bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i);
1136 bt_put(int_16_field);
1137 }
1138
1139 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
1140 int_16_field = bt_ctf_field_array_get_field(
1141 an_array_field, i);
1142 bt_ctf_field_signed_integer_set_value(int_16_field, i);
1143 bt_put(int_16_field);
1144 }
1145
1146 stream_event_ctx_field = bt_ctf_event_get_stream_event_context(event);
1147 assert(stream_event_ctx_field);
1148 stream_event_ctx_int_field = bt_ctf_field_structure_get_field(
1149 stream_event_ctx_field, "common_event_context");
1150 BT_PUT(stream_event_ctx_field);
1151 bt_ctf_field_unsigned_integer_set_value(stream_event_ctx_int_field, 17);
1152 BT_PUT(stream_event_ctx_int_field);
1153
1154 bt_ctf_clock_set_time(clock, ++current_time);
1155 ok(bt_ctf_stream_append_event(stream, event) == 0,
1156 "Append a complex event to a stream");
1157
1158 /*
1159 * Populate the custom packet context field with a dummy value
1160 * otherwise flush will fail.
1161 */
1162 packet_context = bt_ctf_stream_get_packet_context(stream);
1163 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
1164 "custom_packet_context_field");
1165 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1);
1166
1167 ok(bt_ctf_stream_flush(stream) == 0,
1168 "Flush a stream containing a complex event");
1169
1170 bt_put(uint_35_field);
1171 bt_put(a_string_field);
1172 bt_put(inner_structure_field);
1173 bt_put(complex_structure_field);
1174 bt_put(a_sequence_field);
1175 bt_put(an_array_field);
1176 bt_put(enum_variant_field);
1177 bt_put(enum_container_field);
1178 bt_put(variant_field);
1179 bt_put(ret_field);
1180 bt_put(packet_context_field);
1181 bt_put(packet_context);
1182 bt_put(uint_35_type);
1183 bt_put(int_16_type);
1184 bt_put(string_type);
1185 bt_put(sequence_type);
1186 bt_put(array_type);
1187 bt_put(inner_structure_type);
1188 bt_put(complex_structure_type);
1189 bt_put(uint_3_type);
1190 bt_put(enum_variant_type);
1191 bt_put(variant_type);
1192 bt_put(ret_field_type);
1193 bt_put(event_class);
1194 bt_put(event);
1195 }
1196
1197 static
1198 void field_copy_tests_validate_same_type(struct bt_ctf_field *field,
1199 struct bt_ctf_field_type *expected_type, const char *name)
1200 {
1201 struct bt_ctf_field_type *copy_type;
1202
1203 copy_type = bt_ctf_field_get_type(field);
1204 ok(copy_type == expected_type,
1205 "bt_ctf_field_copy does not copy the type (%s)", name);
1206 bt_put(copy_type);
1207 }
1208
1209 static
1210 void field_copy_tests_validate_diff_ptrs(struct bt_ctf_field *field_a,
1211 struct bt_ctf_field *field_b, const char *name)
1212 {
1213 ok(field_a != field_b,
1214 "bt_ctf_field_copy creates different pointers (%s)", name);
1215 }
1216
1217 static
1218 void field_copy_tests()
1219 {
1220 struct bt_ctf_field_type *len_type = NULL;
1221 struct bt_ctf_field_type *fp_type = NULL;
1222 struct bt_ctf_field_type *s_type = NULL;
1223 struct bt_ctf_field_type *e_int_type = NULL;
1224 struct bt_ctf_field_type *e_type = NULL;
1225 struct bt_ctf_field_type *v_type = NULL;
1226 struct bt_ctf_field_type *v_label1_type = NULL;
1227 struct bt_ctf_field_type *v_label1_array_type = NULL;
1228 struct bt_ctf_field_type *v_label2_type = NULL;
1229 struct bt_ctf_field_type *v_label2_seq_type = NULL;
1230 struct bt_ctf_field_type *strct_type = NULL;
1231 struct bt_ctf_field *len = NULL;
1232 struct bt_ctf_field *fp = NULL;
1233 struct bt_ctf_field *s = NULL;
1234 struct bt_ctf_field *e_int = NULL;
1235 struct bt_ctf_field *e = NULL;
1236 struct bt_ctf_field *v = NULL;
1237 struct bt_ctf_field *v_selected = NULL;
1238 struct bt_ctf_field *v_selected_cur = NULL;
1239 struct bt_ctf_field *v_selected_0 = NULL;
1240 struct bt_ctf_field *v_selected_1 = NULL;
1241 struct bt_ctf_field *v_selected_2 = NULL;
1242 struct bt_ctf_field *v_selected_3 = NULL;
1243 struct bt_ctf_field *v_selected_4 = NULL;
1244 struct bt_ctf_field *v_selected_5 = NULL;
1245 struct bt_ctf_field *v_selected_6 = NULL;
1246 struct bt_ctf_field *a = NULL;
1247 struct bt_ctf_field *a_0 = NULL;
1248 struct bt_ctf_field *a_1 = NULL;
1249 struct bt_ctf_field *a_2 = NULL;
1250 struct bt_ctf_field *a_3 = NULL;
1251 struct bt_ctf_field *a_4 = NULL;
1252 struct bt_ctf_field *strct = NULL;
1253 struct bt_ctf_field *len_copy = NULL;
1254 struct bt_ctf_field *fp_copy = NULL;
1255 struct bt_ctf_field *s_copy = NULL;
1256 struct bt_ctf_field *e_int_copy = NULL;
1257 struct bt_ctf_field *e_copy = NULL;
1258 struct bt_ctf_field *v_copy = NULL;
1259 struct bt_ctf_field *v_selected_copy = NULL;
1260 struct bt_ctf_field *v_selected_copy_len = NULL;
1261 struct bt_ctf_field *v_selected_0_copy = NULL;
1262 struct bt_ctf_field *v_selected_1_copy = NULL;
1263 struct bt_ctf_field *v_selected_2_copy = NULL;
1264 struct bt_ctf_field *v_selected_3_copy = NULL;
1265 struct bt_ctf_field *v_selected_4_copy = NULL;
1266 struct bt_ctf_field *v_selected_5_copy = NULL;
1267 struct bt_ctf_field *v_selected_6_copy = NULL;
1268 struct bt_ctf_field *a_copy = NULL;
1269 struct bt_ctf_field *a_0_copy = NULL;
1270 struct bt_ctf_field *a_1_copy = NULL;
1271 struct bt_ctf_field *a_2_copy = NULL;
1272 struct bt_ctf_field *a_3_copy = NULL;
1273 struct bt_ctf_field *a_4_copy = NULL;
1274 struct bt_ctf_field *strct_copy = NULL;
1275 struct bt_ctf_field_type_enumeration_mapping_iterator *e_iter = NULL;
1276 uint64_t uint64_t_val;
1277 const char *str_val;
1278 double double_val;
1279 int ret;
1280
1281 /* create len type */
1282 len_type = bt_ctf_field_type_integer_create(32);
1283 assert(len_type);
1284
1285 /* create fp type */
1286 fp_type = bt_ctf_field_type_floating_point_create();
1287 assert(fp_type);
1288
1289 /* create s type */
1290 s_type = bt_ctf_field_type_string_create();
1291 assert(s_type);
1292
1293 /* create e_int type */
1294 e_int_type = bt_ctf_field_type_integer_create(8);
1295 assert(e_int_type);
1296
1297 /* create e type */
1298 e_type = bt_ctf_field_type_enumeration_create(e_int_type);
1299 assert(e_type);
1300 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL1",
1301 10, 15);
1302 assert(!ret);
1303 ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL2",
1304 23, 23);
1305 assert(!ret);
1306
1307 /* create v_label1 type */
1308 v_label1_type = bt_ctf_field_type_string_create();
1309 assert(v_label1_type);
1310
1311 /* create v_label1_array type */
1312 v_label1_array_type = bt_ctf_field_type_array_create(v_label1_type, 5);
1313 assert(v_label1_array_type);
1314
1315 /* create v_label2 type */
1316 v_label2_type = bt_ctf_field_type_integer_create(16);
1317 assert(v_label2_type);
1318
1319 /* create v_label2_seq type */
1320 v_label2_seq_type = bt_ctf_field_type_sequence_create(v_label2_type,
1321 "len");
1322 assert(v_label2_seq_type);
1323
1324 /* create v type */
1325 v_type = bt_ctf_field_type_variant_create(e_type, "e");
1326 assert(v_type);
1327 ret = bt_ctf_field_type_variant_add_field(v_type, v_label1_array_type,
1328 "LABEL1");
1329 assert(!ret);
1330 ret = bt_ctf_field_type_variant_add_field(v_type, v_label2_seq_type,
1331 "LABEL2");
1332 assert(!ret);
1333
1334 /* create strct type */
1335 strct_type = bt_ctf_field_type_structure_create();
1336 assert(strct_type);
1337 ret = bt_ctf_field_type_structure_add_field(strct_type, len_type,
1338 "len");
1339 assert(!ret);
1340 ret = bt_ctf_field_type_structure_add_field(strct_type, fp_type, "fp");
1341 assert(!ret);
1342 ret = bt_ctf_field_type_structure_add_field(strct_type, s_type, "s");
1343 assert(!ret);
1344 ret = bt_ctf_field_type_structure_add_field(strct_type, e_type, "e");
1345 assert(!ret);
1346 ret = bt_ctf_field_type_structure_add_field(strct_type, v_type, "v");
1347 assert(!ret);
1348 ret = bt_ctf_field_type_structure_add_field(strct_type,
1349 v_label1_array_type, "a");
1350 assert(!ret);
1351
1352 /* create strct */
1353 strct = bt_ctf_field_create(strct_type);
1354 assert(strct);
1355
1356 /* get len field */
1357 len = bt_ctf_field_structure_get_field(strct, "len");
1358 assert(len);
1359
1360 /* get fp field */
1361 fp = bt_ctf_field_structure_get_field(strct, "fp");
1362 assert(fp);
1363
1364 /* get s field */
1365 s = bt_ctf_field_structure_get_field(strct, "s");
1366 assert(s);
1367
1368 /* get e field */
1369 e = bt_ctf_field_structure_get_field(strct, "e");
1370 assert(e);
1371
1372 /* get e_int (underlying integer) */
1373 e_int = bt_ctf_field_enumeration_get_container(e);
1374 assert(e_int);
1375
1376 /* get v field */
1377 v = bt_ctf_field_structure_get_field(strct, "v");
1378 assert(v);
1379
1380 /* get a field */
1381 a = bt_ctf_field_structure_get_field(strct, "a");
1382 assert(a);
1383
1384 /* set len field */
1385 ret = bt_ctf_field_unsigned_integer_set_value(len, 7);
1386 assert(!ret);
1387
1388 /* set fp field */
1389 ret = bt_ctf_field_floating_point_set_value(fp, 3.14);
1390 assert(!ret);
1391
1392 /* set s field */
1393 ret = bt_ctf_field_string_set_value(s, "btbt");
1394 assert(!ret);
1395
1396 /* set e field (LABEL2) */
1397 ret = bt_ctf_field_unsigned_integer_set_value(e_int, 23);
1398 assert(!ret);
1399
1400 /* set v field */
1401 v_selected = bt_ctf_field_variant_get_field(v, e);
1402 assert(v_selected);
1403 ok(!bt_ctf_field_variant_get_current_field(NULL),
1404 "bt_ctf_field_variant_get_current_field handles NULL correctly");
1405 v_selected_cur = bt_ctf_field_variant_get_current_field(v);
1406 ok(v_selected_cur == v_selected,
1407 "bt_ctf_field_variant_get_current_field returns the current field");
1408 bt_put(v_selected_cur);
1409
1410 /* set selected v field */
1411 ret = bt_ctf_field_sequence_set_length(v_selected, len);
1412 assert(!ret);
1413 v_selected_0 = bt_ctf_field_sequence_get_field(v_selected, 0);
1414 assert(v_selected_0);
1415 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_0, 7);
1416 assert(!ret);
1417 v_selected_1 = bt_ctf_field_sequence_get_field(v_selected, 1);
1418 assert(v_selected_1);
1419 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_1, 6);
1420 assert(!ret);
1421 v_selected_2 = bt_ctf_field_sequence_get_field(v_selected, 2);
1422 assert(v_selected_2);
1423 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_2, 5);
1424 assert(!ret);
1425 v_selected_3 = bt_ctf_field_sequence_get_field(v_selected, 3);
1426 assert(v_selected_3);
1427 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_3, 4);
1428 assert(!ret);
1429 v_selected_4 = bt_ctf_field_sequence_get_field(v_selected, 4);
1430 assert(v_selected_4);
1431 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_4, 3);
1432 assert(!ret);
1433 v_selected_5 = bt_ctf_field_sequence_get_field(v_selected, 5);
1434 assert(v_selected_5);
1435 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_5, 2);
1436 assert(!ret);
1437 v_selected_6 = bt_ctf_field_sequence_get_field(v_selected, 6);
1438 assert(v_selected_6);
1439 ret = bt_ctf_field_unsigned_integer_set_value(v_selected_6, 1);
1440 assert(!ret);
1441
1442 /* set a field */
1443 a_0 = bt_ctf_field_array_get_field(a, 0);
1444 assert(a_0);
1445 ret = bt_ctf_field_string_set_value(a_0, "a_0");
1446 assert(!ret);
1447 a_1 = bt_ctf_field_array_get_field(a, 1);
1448 assert(a_1);
1449 ret = bt_ctf_field_string_set_value(a_1, "a_1");
1450 assert(!ret);
1451 a_2 = bt_ctf_field_array_get_field(a, 2);
1452 assert(a_2);
1453 ret = bt_ctf_field_string_set_value(a_2, "a_2");
1454 assert(!ret);
1455 a_3 = bt_ctf_field_array_get_field(a, 3);
1456 assert(a_3);
1457 ret = bt_ctf_field_string_set_value(a_3, "a_3");
1458 assert(!ret);
1459 a_4 = bt_ctf_field_array_get_field(a, 4);
1460 assert(a_4);
1461 ret = bt_ctf_field_string_set_value(a_4, "a_4");
1462 assert(!ret);
1463
1464 /* create copy of strct */
1465 ok(!bt_ctf_field_copy(NULL),
1466 "bt_ctf_field_copy handles NULL correctly");
1467 strct_copy = bt_ctf_field_copy(strct);
1468 ok(strct_copy,
1469 "bt_ctf_field_copy returns a valid pointer");
1470
1471 /* get all copied fields */
1472 len_copy = bt_ctf_field_structure_get_field(strct_copy, "len");
1473 assert(len_copy);
1474 fp_copy = bt_ctf_field_structure_get_field(strct_copy, "fp");
1475 assert(fp_copy);
1476 s_copy = bt_ctf_field_structure_get_field(strct_copy, "s");
1477 assert(s_copy);
1478 e_copy = bt_ctf_field_structure_get_field(strct_copy, "e");
1479 assert(e_copy);
1480 e_int_copy = bt_ctf_field_enumeration_get_container(e_copy);
1481 assert(e_int_copy);
1482 v_copy = bt_ctf_field_structure_get_field(strct_copy, "v");
1483 assert(v_copy);
1484 v_selected_copy = bt_ctf_field_variant_get_field(v_copy, e_copy);
1485 assert(v_selected_copy);
1486 v_selected_0_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 0);
1487 assert(v_selected_0_copy);
1488 v_selected_1_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 1);
1489 assert(v_selected_1_copy);
1490 v_selected_2_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 2);
1491 assert(v_selected_2_copy);
1492 v_selected_3_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 3);
1493 assert(v_selected_3_copy);
1494 v_selected_4_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 4);
1495 assert(v_selected_4_copy);
1496 v_selected_5_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 5);
1497 assert(v_selected_5_copy);
1498 v_selected_6_copy = bt_ctf_field_sequence_get_field(v_selected_copy, 6);
1499 assert(v_selected_6_copy);
1500 ok(!bt_ctf_field_sequence_get_field(v_selected_copy, 7),
1501 "sequence field copy is not too large");
1502 a_copy = bt_ctf_field_structure_get_field(strct_copy, "a");
1503 assert(a_copy);
1504 a_0_copy = bt_ctf_field_array_get_field(a_copy, 0);
1505 assert(a_0_copy);
1506 a_1_copy = bt_ctf_field_array_get_field(a_copy, 1);
1507 assert(a_1_copy);
1508 a_2_copy = bt_ctf_field_array_get_field(a_copy, 2);
1509 assert(a_2_copy);
1510 a_3_copy = bt_ctf_field_array_get_field(a_copy, 3);
1511 assert(a_3_copy);
1512 a_4_copy = bt_ctf_field_array_get_field(a_copy, 4);
1513 assert(a_4_copy);
1514 ok(!bt_ctf_field_array_get_field(v_selected_copy, 5),
1515 "array field copy is not too large");
1516
1517 /* make sure copied fields are different pointers */
1518 field_copy_tests_validate_diff_ptrs(strct_copy, strct, "strct");
1519 field_copy_tests_validate_diff_ptrs(len_copy, len, "len");
1520 field_copy_tests_validate_diff_ptrs(fp_copy, fp, "fp");
1521 field_copy_tests_validate_diff_ptrs(s_copy, s, "s");
1522 field_copy_tests_validate_diff_ptrs(e_int_copy, e_int, "e_int");
1523 field_copy_tests_validate_diff_ptrs(e_copy, e, "e");
1524 field_copy_tests_validate_diff_ptrs(v_copy, v, "v");
1525 field_copy_tests_validate_diff_ptrs(v_selected_copy, v_selected,
1526 "v_selected");
1527 field_copy_tests_validate_diff_ptrs(v_selected_0_copy, v_selected_0,
1528 "v_selected_0");
1529 field_copy_tests_validate_diff_ptrs(v_selected_1_copy, v_selected_1,
1530 "v_selected_1");
1531 field_copy_tests_validate_diff_ptrs(v_selected_2_copy, v_selected_2,
1532 "v_selected_2");
1533 field_copy_tests_validate_diff_ptrs(v_selected_3_copy, v_selected_3,
1534 "v_selected_3");
1535 field_copy_tests_validate_diff_ptrs(v_selected_4_copy, v_selected_4,
1536 "v_selected_4");
1537 field_copy_tests_validate_diff_ptrs(v_selected_5_copy, v_selected_5,
1538 "v_selected_5");
1539 field_copy_tests_validate_diff_ptrs(v_selected_6_copy, v_selected_6,
1540 "v_selected_6");
1541 field_copy_tests_validate_diff_ptrs(a_copy, a, "a");
1542 field_copy_tests_validate_diff_ptrs(a_0_copy, a_0, "a_0");
1543 field_copy_tests_validate_diff_ptrs(a_1_copy, a_1, "a_1");
1544 field_copy_tests_validate_diff_ptrs(a_2_copy, a_2, "a_2");
1545 field_copy_tests_validate_diff_ptrs(a_3_copy, a_3, "a_3");
1546 field_copy_tests_validate_diff_ptrs(a_4_copy, a_4, "a_4");
1547
1548 /* make sure copied fields share the same types */
1549 field_copy_tests_validate_same_type(strct_copy, strct_type, "strct");
1550 field_copy_tests_validate_same_type(len_copy, len_type, "len");
1551 field_copy_tests_validate_same_type(fp_copy, fp_type, "fp");
1552 field_copy_tests_validate_same_type(e_int_copy, e_int_type, "e_int");
1553 field_copy_tests_validate_same_type(e_copy, e_type, "e");
1554 field_copy_tests_validate_same_type(v_copy, v_type, "v");
1555 field_copy_tests_validate_same_type(v_selected_copy, v_label2_seq_type,
1556 "v_selected");
1557 field_copy_tests_validate_same_type(v_selected_0_copy, v_label2_type,
1558 "v_selected_0");
1559 field_copy_tests_validate_same_type(v_selected_1_copy, v_label2_type,
1560 "v_selected_1");
1561 field_copy_tests_validate_same_type(v_selected_2_copy, v_label2_type,
1562 "v_selected_2");
1563 field_copy_tests_validate_same_type(v_selected_3_copy, v_label2_type,
1564 "v_selected_3");
1565 field_copy_tests_validate_same_type(v_selected_4_copy, v_label2_type,
1566 "v_selected_4");
1567 field_copy_tests_validate_same_type(v_selected_5_copy, v_label2_type,
1568 "v_selected_5");
1569 field_copy_tests_validate_same_type(v_selected_6_copy, v_label2_type,
1570 "v_selected_6");
1571 field_copy_tests_validate_same_type(a_copy, v_label1_array_type, "a");
1572 field_copy_tests_validate_same_type(a_0_copy, v_label1_type, "a_0");
1573 field_copy_tests_validate_same_type(a_1_copy, v_label1_type, "a_1");
1574 field_copy_tests_validate_same_type(a_2_copy, v_label1_type, "a_2");
1575 field_copy_tests_validate_same_type(a_3_copy, v_label1_type, "a_3");
1576 field_copy_tests_validate_same_type(a_4_copy, v_label1_type, "a_4");
1577
1578 /* validate len copy */
1579 ret = bt_ctf_field_unsigned_integer_get_value(len_copy, &uint64_t_val);
1580 assert(!ret);
1581 ok(uint64_t_val == 7,
1582 "bt_ctf_field_copy creates a valid integer field copy");
1583
1584 /* validate fp copy */
1585 ret = bt_ctf_field_floating_point_get_value(fp_copy, &double_val);
1586 assert(!ret);
1587 ok(double_val == 3.14,
1588 "bt_ctf_field_copy creates a valid floating point number field copy");
1589
1590 /* validate s copy */
1591 str_val = bt_ctf_field_string_get_value(s_copy);
1592 ok(str_val && !strcmp(str_val, "btbt"),
1593 "bt_ctf_field_copy creates a valid string field copy");
1594
1595 /* validate e_int copy */
1596 ret = bt_ctf_field_unsigned_integer_get_value(e_int_copy,
1597 &uint64_t_val);
1598 assert(!ret);
1599 ok(uint64_t_val == 23,
1600 "bt_ctf_field_copy creates a valid enum's integer field copy");
1601
1602 /* validate e copy */
1603 e_iter = bt_ctf_field_enumeration_get_mappings(e_copy);
1604 (void) bt_ctf_field_type_enumeration_mapping_iterator_get_signed(e_iter,
1605 &str_val, NULL, NULL);
1606 ok(str_val && !strcmp(str_val, "LABEL2"),
1607 "bt_ctf_field_copy creates a valid enum field copy");
1608
1609 /* validate v_selected copy */
1610 v_selected_copy_len = bt_ctf_field_sequence_get_length(v_selected);
1611 assert(v_selected_copy_len);
1612 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_copy_len,
1613 &uint64_t_val);
1614 assert(!ret);
1615 ok(uint64_t_val == 7,
1616 "bt_ctf_field_copy creates a sequence field copy with the proper length");
1617 bt_put(v_selected_copy_len);
1618 v_selected_copy_len = NULL;
1619
1620 /* validate v_selected copy fields */
1621 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_0_copy,
1622 &uint64_t_val);
1623 assert(!ret);
1624 ok(uint64_t_val == 7,
1625 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_0)");
1626 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_1_copy,
1627 &uint64_t_val);
1628 assert(!ret);
1629 ok(uint64_t_val == 6,
1630 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_1)");
1631 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_2_copy,
1632 &uint64_t_val);
1633 assert(!ret);
1634 ok(uint64_t_val == 5,
1635 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_2)");
1636 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_3_copy,
1637 &uint64_t_val);
1638 assert(!ret);
1639 ok(uint64_t_val == 4,
1640 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_3)");
1641 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_4_copy,
1642 &uint64_t_val);
1643 assert(!ret);
1644 ok(uint64_t_val == 3,
1645 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_4)");
1646 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_5_copy,
1647 &uint64_t_val);
1648 assert(!ret);
1649 ok(uint64_t_val == 2,
1650 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_5)");
1651 ret = bt_ctf_field_unsigned_integer_get_value(v_selected_6_copy,
1652 &uint64_t_val);
1653 assert(!ret);
1654 ok(uint64_t_val == 1,
1655 "bt_ctf_field_copy creates a valid sequence field element copy (v_selected_6)");
1656
1657 /* validate a copy fields */
1658 str_val = bt_ctf_field_string_get_value(a_0_copy);
1659 ok(str_val && !strcmp(str_val, "a_0"),
1660 "bt_ctf_field_copy creates a valid array field element copy (a_0)");
1661 str_val = bt_ctf_field_string_get_value(a_1_copy);
1662 ok(str_val && !strcmp(str_val, "a_1"),
1663 "bt_ctf_field_copy creates a valid array field element copy (a_1)");
1664 str_val = bt_ctf_field_string_get_value(a_2_copy);
1665 ok(str_val && !strcmp(str_val, "a_2"),
1666 "bt_ctf_field_copy creates a valid array field element copy (a_2)");
1667 str_val = bt_ctf_field_string_get_value(a_3_copy);
1668 ok(str_val && !strcmp(str_val, "a_3"),
1669 "bt_ctf_field_copy creates a valid array field element copy (a_3)");
1670 str_val = bt_ctf_field_string_get_value(a_4_copy);
1671 ok(str_val && !strcmp(str_val, "a_4"),
1672 "bt_ctf_field_copy creates a valid array field element copy (a_4)");
1673
1674 /* put everything */
1675 bt_put(len_type);
1676 bt_put(fp_type);
1677 bt_put(s_type);
1678 bt_put(e_int_type);
1679 bt_put(e_type);
1680 bt_put(v_type);
1681 bt_put(v_label1_type);
1682 bt_put(v_label1_array_type);
1683 bt_put(v_label2_type);
1684 bt_put(v_label2_seq_type);
1685 bt_put(strct_type);
1686 bt_put(len);
1687 bt_put(fp);
1688 bt_put(s);
1689 bt_put(e_int);
1690 bt_put(e);
1691 bt_put(v);
1692 bt_put(v_selected);
1693 bt_put(v_selected_0);
1694 bt_put(v_selected_1);
1695 bt_put(v_selected_2);
1696 bt_put(v_selected_3);
1697 bt_put(v_selected_4);
1698 bt_put(v_selected_5);
1699 bt_put(v_selected_6);
1700 bt_put(a);
1701 bt_put(a_0);
1702 bt_put(a_1);
1703 bt_put(a_2);
1704 bt_put(a_3);
1705 bt_put(a_4);
1706 bt_put(strct);
1707 bt_put(len_copy);
1708 bt_put(fp_copy);
1709 bt_put(s_copy);
1710 bt_put(e_int_copy);
1711 bt_put(e_copy);
1712 bt_put(v_copy);
1713 bt_put(v_selected_copy);
1714 bt_put(v_selected_0_copy);
1715 bt_put(v_selected_1_copy);
1716 bt_put(v_selected_2_copy);
1717 bt_put(v_selected_3_copy);
1718 bt_put(v_selected_4_copy);
1719 bt_put(v_selected_5_copy);
1720 bt_put(v_selected_6_copy);
1721 bt_put(a_copy);
1722 bt_put(a_0_copy);
1723 bt_put(a_1_copy);
1724 bt_put(a_2_copy);
1725 bt_put(a_3_copy);
1726 bt_put(a_4_copy);
1727 bt_put(strct_copy);
1728 bt_put(e_iter);
1729 }
1730
1731 static
1732 void type_field_tests()
1733 {
1734 struct bt_ctf_field *uint_12;
1735 struct bt_ctf_field *int_16;
1736 struct bt_ctf_field *string;
1737 struct bt_ctf_field *enumeration;
1738 struct bt_ctf_field_type *composite_structure_type;
1739 struct bt_ctf_field_type *structure_seq_type;
1740 struct bt_ctf_field_type *string_type;
1741 struct bt_ctf_field_type *sequence_type;
1742 struct bt_ctf_field_type *uint_8_type;
1743 struct bt_ctf_field_type *int_16_type;
1744 struct bt_ctf_field_type *uint_12_type =
1745 bt_ctf_field_type_integer_create(12);
1746 struct bt_ctf_field_type *enumeration_type;
1747 struct bt_ctf_field_type *returned_type;
1748 const char *ret_string;
1749
1750 returned_type = bt_ctf_field_get_type(NULL);
1751 ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly");
1752
1753 ok(uint_12_type, "Create an unsigned integer type");
1754 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1755 BT_CTF_INTEGER_BASE_BINARY) == 0,
1756 "Set integer type's base as binary");
1757 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1758 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
1759 "Set integer type's base as decimal");
1760 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1761 BT_CTF_INTEGER_BASE_UNKNOWN),
1762 "Reject integer type's base set as unknown");
1763 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1764 BT_CTF_INTEGER_BASE_OCTAL) == 0,
1765 "Set integer type's base as octal");
1766 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1767 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
1768 "Set integer type's base as hexadecimal");
1769 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
1770 "Reject unknown integer base value");
1771 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
1772 "Set integer type signedness to signed");
1773 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
1774 "Set integer type signedness to unsigned");
1775 ok(bt_ctf_field_type_integer_get_size(NULL) < 0,
1776 "bt_ctf_field_type_integer_get_size handles NULL correctly");
1777 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
1778 "bt_ctf_field_type_integer_get_size returns a correct value");
1779 ok(bt_ctf_field_type_integer_get_signed(NULL) < 0,
1780 "bt_ctf_field_type_integer_get_signed handles NULL correctly");
1781 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
1782 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
1783
1784 ok(bt_ctf_field_type_set_byte_order(NULL,
1785 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
1786 "bt_ctf_field_type_set_byte_order handles NULL correctly");
1787 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1788 (enum bt_ctf_byte_order) 42) < 0,
1789 "bt_ctf_field_type_set_byte_order rejects invalid values");
1790 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1791 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
1792 "Set an integer's byte order to little endian");
1793 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
1794 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
1795 "Set an integer's byte order to big endian");
1796 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
1797 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1798 "bt_ctf_field_type_get_byte_order returns a correct value");
1799 ok(bt_ctf_field_type_get_byte_order(NULL) ==
1800 BT_CTF_BYTE_ORDER_UNKNOWN,
1801 "bt_ctf_field_type_get_byte_order handles NULL correctly");
1802
1803 ok(bt_ctf_field_type_get_type_id(NULL) ==
1804 BT_CTF_FIELD_TYPE_ID_UNKNOWN,
1805 "bt_ctf_field_type_get_type_id handles NULL correctly");
1806 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
1807 BT_CTF_FIELD_TYPE_ID_INTEGER,
1808 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
1809
1810 ok(bt_ctf_field_type_integer_get_base(NULL) ==
1811 BT_CTF_INTEGER_BASE_UNKNOWN,
1812 "bt_ctf_field_type_integer_get_base handles NULL correctly");
1813 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
1814 BT_CTF_INTEGER_BASE_HEXADECIMAL,
1815 "bt_ctf_field_type_integer_get_base returns a correct value");
1816
1817 ok(bt_ctf_field_type_integer_set_encoding(NULL,
1818 BT_CTF_STRING_ENCODING_ASCII) < 0,
1819 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
1820 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1821 (enum bt_ctf_string_encoding) 123) < 0,
1822 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
1823 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
1824 BT_CTF_STRING_ENCODING_UTF8) == 0,
1825 "Set integer type encoding to UTF8");
1826 ok(bt_ctf_field_type_integer_get_encoding(NULL) ==
1827 BT_CTF_STRING_ENCODING_UNKNOWN,
1828 "bt_ctf_field_type_integer_get_encoding handles NULL correctly");
1829 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) ==
1830 BT_CTF_STRING_ENCODING_UTF8,
1831 "bt_ctf_field_type_integer_get_encoding returns a correct value");
1832
1833 int_16_type = bt_ctf_field_type_integer_create(16);
1834 assert(int_16_type);
1835 ok(!bt_ctf_field_type_integer_set_signed(int_16_type, 1),
1836 "Set signedness of 16 bit integer to true");
1837 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
1838 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
1839 uint_8_type = bt_ctf_field_type_integer_create(8);
1840 sequence_type =
1841 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
1842 ok(sequence_type, "Create a sequence of int16_t type");
1843 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
1844 BT_CTF_FIELD_TYPE_ID_SEQUENCE,
1845 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
1846
1847 ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL,
1848 "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly");
1849 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
1850 sequence_type);
1851 ok(!strcmp(ret_string, "seq_len"),
1852 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
1853 ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL,
1854 "bt_ctf_field_type_sequence_get_element_type handles NULL correctly");
1855 returned_type = bt_ctf_field_type_sequence_get_element_type(
1856 sequence_type);
1857 ok(returned_type == int_16_type,
1858 "bt_ctf_field_type_sequence_get_element_type returns the correct type");
1859 bt_put(returned_type);
1860
1861 string_type = bt_ctf_field_type_string_create();
1862 ok(string_type, "Create a string type");
1863 ok(bt_ctf_field_type_string_set_encoding(string_type,
1864 BT_CTF_STRING_ENCODING_NONE),
1865 "Reject invalid \"None\" string encoding");
1866 ok(bt_ctf_field_type_string_set_encoding(string_type,
1867 42),
1868 "Reject invalid string encoding");
1869 ok(bt_ctf_field_type_string_set_encoding(string_type,
1870 BT_CTF_STRING_ENCODING_ASCII) == 0,
1871 "Set string encoding to ASCII");
1872
1873 ok(bt_ctf_field_type_string_get_encoding(NULL) ==
1874 BT_CTF_STRING_ENCODING_UNKNOWN,
1875 "bt_ctf_field_type_string_get_encoding handles NULL correctly");
1876 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
1877 BT_CTF_STRING_ENCODING_ASCII,
1878 "bt_ctf_field_type_string_get_encoding returns the correct value");
1879
1880 structure_seq_type = bt_ctf_field_type_structure_create();
1881 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
1882 BT_CTF_FIELD_TYPE_ID_STRUCT,
1883 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
1884 ok(structure_seq_type, "Create a structure type");
1885 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1886 uint_8_type, "seq_len") == 0,
1887 "Add a uint8_t type to a structure");
1888 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
1889 sequence_type, "a_sequence") == 0,
1890 "Add a sequence type to a structure");
1891
1892 ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0,
1893 "bt_ctf_field_type_structure_get_field_count handles NULL correctly");
1894 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
1895 "bt_ctf_field_type_structure_get_field_count returns a correct value");
1896
1897 ok(bt_ctf_field_type_structure_get_field(NULL,
1898 &ret_string, &returned_type, 1) < 0,
1899 "bt_ctf_field_type_structure_get_field handles a NULL type correctly");
1900 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1901 NULL, &returned_type, 1) == 0,
1902 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
1903 bt_put(returned_type);
1904 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1905 &ret_string, NULL, 1) == 0,
1906 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
1907 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1908 &ret_string, &returned_type, 10) < 0,
1909 "bt_ctf_field_type_structure_get_field handles an invalid index correctly");
1910 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
1911 &ret_string, &returned_type, 1) == 0,
1912 "bt_ctf_field_type_structure_get_field returns a field");
1913 ok(!strcmp(ret_string, "a_sequence"),
1914 "bt_ctf_field_type_structure_get_field returns a correct field name");
1915 ok(returned_type == sequence_type,
1916 "bt_ctf_field_type_structure_get_field returns a correct field type");
1917 bt_put(returned_type);
1918
1919 ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL,
1920 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly");
1921 ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL,
1922 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1923 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1924 structure_seq_type, "a_sequence");
1925 ok(returned_type == sequence_type,
1926 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
1927 bt_put(returned_type);
1928
1929 composite_structure_type = bt_ctf_field_type_structure_create();
1930 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1931 string_type, "a_string") == 0,
1932 "Add a string type to a structure");
1933 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
1934 structure_seq_type, "inner_structure") == 0,
1935 "Add a structure type to a structure");
1936
1937 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1938 NULL, "a_sequence") == NULL,
1939 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly");
1940 ok(bt_ctf_field_type_structure_get_field_type_by_name(
1941 structure_seq_type, NULL) == NULL,
1942 "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly");
1943 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
1944 structure_seq_type, "a_sequence");
1945 ok(returned_type == sequence_type,
1946 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
1947 bt_put(returned_type);
1948
1949 int_16 = bt_ctf_field_create(int_16_type);
1950 ok(int_16, "Instanciate a signed 16-bit integer");
1951 uint_12 = bt_ctf_field_create(uint_12_type);
1952 ok(uint_12, "Instanciate an unsigned 12-bit integer");
1953 returned_type = bt_ctf_field_get_type(int_16);
1954 ok(returned_type == int_16_type,
1955 "bt_ctf_field_get_type returns the correct type");
1956
1957 /* Can't modify types after instanciating them */
1958 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1959 BT_CTF_INTEGER_BASE_DECIMAL),
1960 "Check an integer type' base can't be modified after instanciation");
1961 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
1962 "Check an integer type's signedness can't be modified after instanciation");
1963
1964 /* Check signed property is checked */
1965 ok(bt_ctf_field_signed_integer_set_value(uint_12, -52),
1966 "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer");
1967 ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42),
1968 "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer");
1969
1970 /* Check overflows are properly tested for */
1971 ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0,
1972 "Check -32768 is allowed for a signed 16-bit integer");
1973 ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0,
1974 "Check 32767 is allowed for a signed 16-bit integer");
1975 ok(bt_ctf_field_signed_integer_set_value(int_16, 32768),
1976 "Check 32768 is not allowed for a signed 16-bit integer");
1977 ok(bt_ctf_field_signed_integer_set_value(int_16, -32769),
1978 "Check -32769 is not allowed for a signed 16-bit integer");
1979 ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0,
1980 "Check -42 is allowed for a signed 16-bit integer");
1981
1982 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0,
1983 "Check 4095 is allowed for an unsigned 12-bit integer");
1984 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096),
1985 "Check 4096 is not allowed for a unsigned 12-bit integer");
1986 ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0,
1987 "Check 0 is allowed for an unsigned 12-bit integer");
1988
1989 string = bt_ctf_field_create(string_type);
1990 ok(string, "Instanciate a string field");
1991 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
1992 "Set a string's value");
1993
1994 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
1995 ok(enumeration_type,
1996 "Create an enumeration type with an unsigned 12-bit integer as container");
1997 enumeration = bt_ctf_field_create(enumeration_type);
1998 ok(!enumeration,
1999 "Check enumeration types are validated before instantiation");
2000
2001 bt_put(string);
2002 bt_put(uint_12);
2003 bt_put(int_16);
2004 bt_put(enumeration);
2005 bt_put(composite_structure_type);
2006 bt_put(structure_seq_type);
2007 bt_put(string_type);
2008 bt_put(sequence_type);
2009 bt_put(uint_8_type);
2010 bt_put(int_16_type);
2011 bt_put(uint_12_type);
2012 bt_put(enumeration_type);
2013 bt_put(returned_type);
2014 }
2015
2016 static
2017 void packet_resize_test(struct bt_ctf_stream_class *stream_class,
2018 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
2019 {
2020 /*
2021 * Append enough events to force the underlying packet to be resized.
2022 * Also tests that a new event can be declared after a stream has been
2023 * instantiated and used/flushed.
2024 */
2025 int ret = 0;
2026 int i;
2027 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
2028 "Spammy_Event");
2029 struct bt_ctf_field_type *integer_type =
2030 bt_ctf_field_type_integer_create(17);
2031 struct bt_ctf_field_type *string_type =
2032 bt_ctf_field_type_string_create();
2033 struct bt_ctf_event *event = NULL;
2034 struct bt_ctf_field *ret_field = NULL;
2035 struct bt_ctf_field_type *ret_field_type = NULL;
2036 uint64_t ret_uint64;
2037 int events_appended = 0;
2038 struct bt_ctf_field *packet_context = NULL,
2039 *packet_context_field = NULL, *stream_event_context = NULL;
2040 struct bt_ctf_field_type *ep_field_1_type = NULL;
2041 struct bt_ctf_field_type *ep_a_string_type = NULL;
2042 struct bt_ctf_field_type *ep_type = NULL;
2043
2044 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
2045 "field_1");
2046 ret |= bt_ctf_event_class_add_field(event_class, string_type,
2047 "a_string");
2048 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
2049 ok(ret == 0, "Add a new event class to a stream class after writing an event");
2050 if (ret) {
2051 goto end;
2052 }
2053
2054 /*
2055 * bt_ctf_stream_class_add_event_class() copies the field types
2056 * of event_class, so we retrieve the new ones to create the
2057 * appropriate fields.
2058 */
2059 ep_type = bt_ctf_event_class_get_payload_type(event_class);
2060 assert(ep_type);
2061 ep_field_1_type = bt_ctf_field_type_structure_get_field_type_by_name(
2062 ep_type, "field_1");
2063 assert(ep_field_1_type);
2064 ep_a_string_type = bt_ctf_field_type_structure_get_field_type_by_name(
2065 ep_type, "a_string");
2066 assert(ep_a_string_type);
2067
2068 event = bt_ctf_event_create(event_class);
2069 ret_field = bt_ctf_event_get_payload_by_index(event, 0);
2070 ret_field_type = bt_ctf_field_get_type(ret_field);
2071 ok(bt_ctf_field_type_compare(ret_field_type, integer_type) == 0,
2072 "bt_ctf_event_get_payload_by_index returns a correct field");
2073 bt_put(ret_field_type);
2074 bt_put(ret_field);
2075
2076 ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL,
2077 "bt_ctf_event_get_payload_by_index handles NULL correctly");
2078 ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL,
2079 "bt_ctf_event_get_payload_by_index handles an invalid index correctly");
2080 bt_put(event);
2081
2082 for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) {
2083 event = bt_ctf_event_create(event_class);
2084 struct bt_ctf_field *integer =
2085 bt_ctf_field_create(ep_field_1_type);
2086 struct bt_ctf_field *string =
2087 bt_ctf_field_create(ep_a_string_type);
2088
2089 ret |= bt_ctf_clock_set_time(clock, ++current_time);
2090 ret |= bt_ctf_field_unsigned_integer_set_value(integer, i);
2091 ret |= bt_ctf_event_set_payload(event, "field_1",
2092 integer);
2093 bt_put(integer);
2094 ret |= bt_ctf_field_string_set_value(string, "This is a test");
2095 ret |= bt_ctf_event_set_payload(event, "a_string",
2096 string);
2097 bt_put(string);
2098
2099 /* Populate stream event context */
2100 stream_event_context =
2101 bt_ctf_event_get_stream_event_context(event);
2102 integer = bt_ctf_field_structure_get_field(stream_event_context,
2103 "common_event_context");
2104 BT_PUT(stream_event_context);
2105 ret |= bt_ctf_field_unsigned_integer_set_value(integer,
2106 i % 42);
2107 bt_put(integer);
2108
2109 ret |= bt_ctf_stream_append_event(stream, event);
2110 bt_put(event);
2111
2112 if (ret) {
2113 break;
2114 }
2115 }
2116
2117 events_appended = !!(i == PACKET_RESIZE_TEST_LENGTH);
2118 ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0,
2119 "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly");
2120 ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0,
2121 "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly");
2122 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2123 ok(ret == 0 && ret_uint64 == 0,
2124 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
2125 bt_ctf_stream_append_discarded_events(stream, 1000);
2126 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2127 ok(ret == 0 && ret_uint64 == 1000,
2128 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
2129
2130 end:
2131 ok(events_appended, "Append 100 000 events to a stream");
2132
2133 /*
2134 * Populate the custom packet context field with a dummy value
2135 * otherwise flush will fail.
2136 */
2137 packet_context = bt_ctf_stream_get_packet_context(stream);
2138 packet_context_field = bt_ctf_field_structure_get_field(packet_context,
2139 "custom_packet_context_field");
2140 bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2);
2141
2142 ok(bt_ctf_stream_flush(stream) == 0,
2143 "Flush a stream that forces a packet resize");
2144 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
2145 ok(ret == 0 && ret_uint64 == 1000,
2146 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
2147 bt_put(integer_type);
2148 bt_put(string_type);
2149 bt_put(packet_context);
2150 bt_put(packet_context_field);
2151 bt_put(stream_event_context);
2152 bt_put(event_class);
2153 bt_put(ep_field_1_type);
2154 bt_put(ep_a_string_type);
2155 bt_put(ep_type);
2156 }
2157
2158 static
2159 void test_empty_stream(struct bt_ctf_writer *writer)
2160 {
2161 int ret = 0;
2162 struct bt_ctf_trace *trace = NULL, *ret_trace = NULL;
2163 struct bt_ctf_stream_class *stream_class = NULL;
2164 struct bt_ctf_stream *stream = NULL;
2165
2166 trace = bt_ctf_writer_get_trace(writer);
2167 if (!trace) {
2168 diag("Failed to get trace from writer");
2169 ret = -1;
2170 goto end;
2171 }
2172
2173 stream_class = bt_ctf_stream_class_create("empty_stream");
2174 if (!stream_class) {
2175 diag("Failed to create stream class");
2176 ret = -1;
2177 goto end;
2178 }
2179
2180 ok(bt_ctf_stream_class_get_trace(NULL) == NULL,
2181 "bt_ctf_stream_class_get_trace handles NULL correctly");
2182 ok(bt_ctf_stream_class_get_trace(stream_class) == NULL,
2183 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
2184
2185 stream = bt_ctf_writer_create_stream(writer, stream_class);
2186 if (!stream) {
2187 diag("Failed to create writer stream");
2188 ret = -1;
2189 goto end;
2190 }
2191
2192 ret_trace = bt_ctf_stream_class_get_trace(stream_class);
2193 ok(ret_trace == trace,
2194 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
2195 end:
2196 ok(ret == 0,
2197 "Created a stream class with default attributes and an empty stream");
2198 bt_put(trace);
2199 bt_put(ret_trace);
2200 bt_put(stream);
2201 bt_put(stream_class);
2202 }
2203
2204 static
2205 void test_custom_event_header_stream(struct bt_ctf_writer *writer,
2206 struct bt_ctf_clock *clock)
2207 {
2208 int i, ret;
2209 struct bt_ctf_stream_class *stream_class = NULL;
2210 struct bt_ctf_stream *stream = NULL;
2211 struct bt_ctf_field_type *integer_type = NULL,
2212 *sequence_type = NULL, *event_header_type = NULL;
2213 struct bt_ctf_field *integer = NULL, *sequence = NULL,
2214 *event_header = NULL, *packet_header = NULL;
2215 struct bt_ctf_event_class *event_class = NULL;
2216 struct bt_ctf_event *event = NULL;
2217
2218 stream_class = bt_ctf_stream_class_create("custom_event_header_stream");
2219 if (!stream_class) {
2220 fail("Failed to create stream class");
2221 goto end;
2222 }
2223
2224 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2225 if (ret) {
2226 fail("Failed to set stream class clock");
2227 goto end;
2228 }
2229
2230 /*
2231 * Customize event header to add an "seq_len" integer member
2232 * which will be used as the length of a sequence in an event of this
2233 * stream.
2234 */
2235 event_header_type = bt_ctf_stream_class_get_event_header_type(
2236 stream_class);
2237 if (!event_header_type) {
2238 fail("Failed to get event header type");
2239 goto end;
2240 }
2241
2242 integer_type = bt_ctf_field_type_integer_create(13);
2243 if (!integer_type) {
2244 fail("Failed to create length integer type");
2245 goto end;
2246 }
2247
2248 ret = bt_ctf_field_type_structure_add_field(event_header_type,
2249 integer_type, "seq_len");
2250 if (ret) {
2251 fail("Failed to add a new field to stream event header");
2252 goto end;
2253 }
2254
2255 event_class = bt_ctf_event_class_create("sequence_event");
2256 if (!event_class) {
2257 fail("Failed to create event class");
2258 goto end;
2259 }
2260
2261 /*
2262 * This event's payload will contain a sequence which references
2263 * stream.event.header.seq_len as its length field.
2264 */
2265 sequence_type = bt_ctf_field_type_sequence_create(integer_type,
2266 "stream.event.header.seq_len");
2267 if (!sequence_type) {
2268 fail("Failed to create a sequence");
2269 goto end;
2270 }
2271
2272 ret = bt_ctf_event_class_add_field(event_class, sequence_type,
2273 "some_sequence");
2274 if (ret) {
2275 fail("Failed to add a sequence to an event class");
2276 goto end;
2277 }
2278
2279 ret = bt_ctf_stream_class_add_event_class(stream_class, event_class);
2280 if (ret) {
2281 fail("Failed to add event class to stream class");
2282 goto end;
2283 }
2284
2285 stream = bt_ctf_writer_create_stream(writer, stream_class);
2286 if (!stream) {
2287 fail("Failed to create stream")
2288 goto end;
2289 }
2290
2291 /*
2292 * We have defined a custom packet header field. We have to populate it
2293 * explicitly.
2294 */
2295 packet_header = bt_ctf_stream_get_packet_header(stream);
2296 if (!packet_header) {
2297 fail("Failed to get stream packet header");
2298 goto end;
2299 }
2300
2301 integer = bt_ctf_field_structure_get_field(packet_header,
2302 "custom_trace_packet_header_field");
2303 if (!integer) {
2304 fail("Failed to retrieve custom_trace_packet_header_field");
2305 goto end;
2306 }
2307
2308 ret = bt_ctf_field_unsigned_integer_set_value(integer, 3487);
2309 if (ret) {
2310 fail("Failed to set custom_trace_packet_header_field value");
2311 goto end;
2312 }
2313 bt_put(integer);
2314
2315 event = bt_ctf_event_create(event_class);
2316 if (!event) {
2317 fail("Failed to create event");
2318 goto end;
2319 }
2320
2321 event_header = bt_ctf_event_get_header(event);
2322 if (!event_header) {
2323 fail("Failed to get event header");
2324 goto end;
2325 }
2326
2327 integer = bt_ctf_field_structure_get_field(event_header,
2328 "seq_len");
2329 if (!integer) {
2330 fail("Failed to get seq_len field from event header");
2331 goto end;
2332 }
2333
2334 ret = bt_ctf_field_unsigned_integer_set_value(integer, 2);
2335 if (ret) {
2336 fail("Failed to set seq_len value in event header");
2337 goto end;
2338 }
2339
2340 /* Populate both sequence integer fields */
2341 sequence = bt_ctf_event_get_payload(event, "some_sequence");
2342 if (!sequence) {
2343 fail("Failed to retrieve sequence from event");
2344 goto end;
2345 }
2346
2347 ret = bt_ctf_field_sequence_set_length(sequence, integer);
2348 if (ret) {
2349 fail("Failed to set sequence length");
2350 goto end;
2351 }
2352 bt_put(integer);
2353
2354 for (i = 0; i < 2; i++) {
2355 integer = bt_ctf_field_sequence_get_field(sequence, i);
2356 if (ret) {
2357 fail("Failed to retrieve sequence element");
2358 goto end;
2359 }
2360
2361 ret = bt_ctf_field_unsigned_integer_set_value(integer, i);
2362 if (ret) {
2363 fail("Failed to set sequence element value");
2364 goto end;
2365 }
2366
2367 bt_put(integer);
2368 integer = NULL;
2369 }
2370
2371 ret = bt_ctf_stream_append_event(stream, event);
2372 if (ret) {
2373 fail("Failed to append event to stream");
2374 goto end;
2375 }
2376
2377 ret = bt_ctf_stream_flush(stream);
2378 if (ret) {
2379 fail("Failed to flush custom_event_header stream");
2380 }
2381 end:
2382 bt_put(stream);
2383 bt_put(stream_class);
2384 bt_put(event_class);
2385 bt_put(event);
2386 bt_put(integer);
2387 bt_put(sequence);
2388 bt_put(event_header);
2389 bt_put(packet_header);
2390 bt_put(sequence_type);
2391 bt_put(integer_type);
2392 bt_put(event_header_type);
2393 }
2394
2395 static
2396 void test_instanciate_event_before_stream(struct bt_ctf_writer *writer,
2397 struct bt_ctf_clock *clock)
2398 {
2399 int ret = 0;
2400 struct bt_ctf_stream_class *stream_class = NULL;
2401 struct bt_ctf_stream *stream = NULL,
2402 *ret_stream = NULL;
2403 struct bt_ctf_event_class *event_class = NULL;
2404 struct bt_ctf_event *event = NULL;
2405 struct bt_ctf_field_type *integer_type = NULL;
2406 struct bt_ctf_field *integer = NULL;
2407
2408 stream_class = bt_ctf_stream_class_create("event_before_stream_test");
2409 if (!stream_class) {
2410 diag("Failed to create stream class");
2411 ret = -1;
2412 goto end;
2413 }
2414
2415 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
2416 if (ret) {
2417 diag("Failed to set stream class clock");
2418 goto end;
2419 }
2420
2421 event_class = bt_ctf_event_class_create("some_event_class_name");
2422 integer_type = bt_ctf_field_type_integer_create(32);
2423 if (!integer_type) {
2424 diag("Failed to create integer field type");
2425 ret = -1;
2426 goto end;
2427 }
2428
2429 ret = bt_ctf_event_class_add_field(event_class, integer_type,
2430 "integer_field");
2431 if (ret) {
2432 diag("Failed to add field to event class");
2433 goto end;
2434 }
2435
2436 ret = bt_ctf_stream_class_add_event_class(stream_class,
2437 event_class);
2438 if (ret) {
2439 diag("Failed to add event class to stream class");
2440 }
2441
2442 event = bt_ctf_event_create(event_class);
2443 if (!event) {
2444 diag("Failed to create event");
2445 ret = -1;
2446 goto end;
2447 }
2448
2449 integer = bt_ctf_event_get_payload_by_index(event, 0);
2450 if (!integer) {
2451 diag("Failed to get integer field payload from event");
2452 ret = -1;
2453 goto end;
2454 }
2455
2456 ret = bt_ctf_field_unsigned_integer_set_value(integer, 1234);
2457 if (ret) {
2458 diag("Failed to set integer field value");
2459 goto end;
2460 }
2461
2462 stream = bt_ctf_writer_create_stream(writer, stream_class);
2463 if (!stream) {
2464 diag("Failed to create writer stream");
2465 ret = -1;
2466 goto end;
2467 }
2468
2469 ok(bt_ctf_event_get_stream(NULL) == NULL,
2470 "bt_ctf_event_get_stream handles NULL correctly");
2471 ok(bt_ctf_event_get_stream(event) == NULL,
2472 "bt_ctf_event_get_stream returns NULL on event which has not yet been appended to a stream");
2473
2474 ret = bt_ctf_stream_append_event(stream, event);
2475 if (ret) {
2476 diag("Failed to append event to stream");
2477 goto end;
2478 }
2479
2480 ret_stream = bt_ctf_event_get_stream(event);
2481 ok(ret_stream == stream,
2482 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
2483 end:
2484 ok(ret == 0,
2485 "Create an event before instanciating its associated stream");
2486 bt_put(stream);
2487 bt_put(ret_stream);
2488 bt_put(stream_class);
2489 bt_put(event_class);
2490 bt_put(event);
2491 bt_put(integer_type);
2492 bt_put(integer);
2493 }
2494
2495 static
2496 void append_existing_event_class(struct bt_ctf_stream_class *stream_class)
2497 {
2498 struct bt_ctf_event_class *event_class;
2499
2500 event_class = bt_ctf_event_class_create("Simple Event");
2501 assert(event_class);
2502 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2503 "two event classes with the same name cannot cohabit within the same stream class");
2504 bt_put(event_class);
2505
2506 event_class = bt_ctf_event_class_create("different name, ok");
2507 assert(event_class);
2508 assert(!bt_ctf_event_class_set_id(event_class, 11));
2509 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
2510 "two event classes with the same ID cannot cohabit within the same stream class");
2511 bt_put(event_class);
2512 }
2513
2514 static
2515 struct bt_ctf_event_class *create_minimal_event_class(void)
2516 {
2517 struct bt_ctf_event_class *ec = NULL;
2518 struct bt_ctf_field_type *int_ft = NULL;
2519 int ret;
2520
2521 int_ft = bt_ctf_field_type_integer_create(23);
2522 assert(int_ft);
2523 ec = bt_ctf_event_class_create("minimal");
2524 assert(ec);
2525 ret = bt_ctf_event_class_add_field(ec, int_ft, "field");
2526 assert(!ret);
2527 BT_PUT(int_ft);
2528
2529 return ec;
2530 }
2531
2532 static
2533 void test_create_writer_vs_non_writer_mode(void)
2534 {
2535 int ret;
2536 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
2537 const char *writer_stream_name = "writer stream instance";
2538 struct bt_ctf_writer *writer = NULL;
2539 struct bt_ctf_trace *writer_trace = NULL;
2540 struct bt_ctf_stream_class *writer_sc = NULL;
2541 struct bt_ctf_stream *writer_stream = NULL;
2542 struct bt_ctf_stream *writer_stream2 = NULL;
2543 struct bt_ctf_stream *packet_stream = NULL;
2544 struct bt_ctf_trace *non_writer_trace = NULL;
2545 struct bt_ctf_stream_class *non_writer_sc = NULL;
2546 struct bt_ctf_stream *non_writer_stream = NULL;
2547 struct bt_ctf_stream *non_writer_stream2 = NULL;
2548 struct bt_ctf_event_class *writer_ec = NULL;
2549 struct bt_ctf_event_class *non_writer_ec = NULL;
2550 struct bt_ctf_event *event = NULL;
2551 struct bt_ctf_event *event2 = NULL;
2552 struct bt_ctf_field_type *empty_struct_ft = NULL;
2553 struct bt_ctf_field *int_field = NULL;
2554 struct bt_ctf_clock *writer_clock = NULL;
2555 struct bt_ctf_clock_class *non_writer_clock_class = NULL;
2556 struct bt_ctf_packet *packet = NULL;
2557 struct bt_ctf_packet *packet2 = NULL;
2558
2559 if (!bt_mkdtemp(trace_path)) {
2560 perror("# perror");
2561 }
2562
2563 /* Create empty structure field type (event header) */
2564 empty_struct_ft = bt_ctf_field_type_structure_create();
2565 assert(empty_struct_ft);
2566
2567 /* Create writer, writer stream class, stream, and clock */
2568 writer = bt_ctf_writer_create(trace_path);
2569 assert(writer);
2570 ret = bt_ctf_writer_set_byte_order(writer, BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
2571 assert(!ret);
2572 writer_trace = bt_ctf_writer_get_trace(writer);
2573 ok(writer_trace, "bt_ctf_writer_get_trace() returns a trace");
2574 writer_sc = bt_ctf_stream_class_create("writer_sc");
2575 assert(writer_sc);
2576 ret = bt_ctf_stream_class_set_event_header_type(writer_sc,
2577 empty_struct_ft);
2578 assert(!ret);
2579 ret = bt_ctf_trace_add_stream_class(writer_trace, writer_sc);
2580 assert(!ret);
2581 writer_stream = bt_ctf_stream_create(writer_sc, writer_stream_name);
2582 assert(writer_stream);
2583 ok(!strcmp(bt_ctf_stream_get_name(writer_stream), writer_stream_name),
2584 "bt_ctf_stream_get_name() returns the stream's name");
2585 writer_clock = bt_ctf_clock_create("writer_clock");
2586 assert(writer_clock);
2587 ret = bt_ctf_writer_add_clock(writer, writer_clock);
2588 assert(!ret);
2589
2590 /* Create non-writer trace, stream class, stream, and clock */
2591 non_writer_trace = bt_ctf_trace_create();
2592 assert(non_writer_trace);
2593 ret = bt_ctf_trace_set_native_byte_order(non_writer_trace,
2594 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
2595 assert(!ret);
2596 non_writer_sc = bt_ctf_stream_class_create("nonwriter_sc");
2597 assert(non_writer_sc);
2598 ret = bt_ctf_stream_class_set_event_header_type(non_writer_sc,
2599 empty_struct_ft);
2600 assert(!ret);
2601 ret = bt_ctf_trace_add_stream_class(non_writer_trace, non_writer_sc);
2602 assert(!ret);
2603 non_writer_stream = bt_ctf_stream_create(non_writer_sc, NULL);
2604 assert(non_writer_stream);
2605 non_writer_clock_class =
2606 bt_ctf_clock_class_create("non_writer_clock_class");
2607 assert(non_writer_clock_class);
2608 ret = bt_ctf_trace_add_clock_class(non_writer_trace,
2609 non_writer_clock_class);
2610 assert(!ret);
2611
2612 /* Create event class and event */
2613 writer_ec = create_minimal_event_class();
2614 assert(writer_ec);
2615 ret = bt_ctf_stream_class_add_event_class(writer_sc, writer_ec);
2616 assert(!ret);
2617 event = bt_ctf_event_create(writer_ec);
2618 assert(event);
2619 int_field = bt_ctf_event_get_payload_by_index(event, 0);
2620 assert(int_field);
2621 bt_ctf_field_unsigned_integer_set_value(int_field, 17);
2622
2623 /*
2624 * Verify non-writer stream: it should be impossible to append
2625 * an event to it.
2626 */
2627 ok(bt_ctf_stream_append_event(non_writer_stream, event),
2628 "bt_ctf_stream_append_event() fails with a non-writer stream");
2629
2630 /*
2631 * Verify writer stream: it should be possible to append an
2632 * event to it.
2633 */
2634 ok(!bt_ctf_stream_append_event(writer_stream, event),
2635 "bt_ctf_stream_append_event() succeeds with a writer stream");
2636
2637 /*
2638 * It should be possible to create a packet from a non-writer
2639 * stream, but not from a writer stream.
2640 */
2641 packet = bt_ctf_packet_create(writer_stream);
2642 ok(!packet, "bt_ctf_packet_create() fails with a writer stream");
2643 packet = bt_ctf_packet_create(non_writer_stream);
2644 ok(packet, "bt_ctf_packet_create() succeeds with a non-writer stream");
2645 packet_stream = bt_ctf_packet_get_stream(packet);
2646 ok(packet_stream == non_writer_stream,
2647 "bt_ctf_packet_get_stream() returns the correct stream");
2648
2649 /*
2650 * It should not be possible to append an event associated to
2651 * a stream to a different stream.
2652 */
2653 writer_stream2 = bt_ctf_stream_create(writer_sc, "zoo");
2654 assert(writer_stream2);
2655 ok(bt_ctf_stream_append_event(writer_stream2, event),
2656 "bt_ctf_stream_append_event() fails with an event associated to another stream");
2657
2658 /*
2659 * It should not be possible to set the packet of an event
2660 * associated to a given stream to a packet associated with
2661 * a different stream.
2662 */
2663 ok(bt_ctf_event_set_packet(event, packet),
2664 "bt_ctf_event_set_packet() fails with a packet not sharing the event's stream");
2665
2666 /*
2667 * It should be possible to set the packet of a fresh event, as
2668 * long as the originating stream classes are the same.
2669 */
2670 event2 = bt_ctf_event_create(writer_ec);
2671 assert(event2);
2672 ok(bt_ctf_event_set_packet(event2, packet),
2673 "bt_ctf_event_set_packet() fails when the event's and the packet's stream class differ");
2674 non_writer_ec = create_minimal_event_class();
2675 assert(non_writer_ec);
2676 ret = bt_ctf_stream_class_add_event_class(non_writer_sc, non_writer_ec);
2677 assert(!ret);
2678 BT_PUT(event2);
2679 event2 = bt_ctf_event_create(non_writer_ec);
2680 assert(event2);
2681 ok(!bt_ctf_event_set_packet(event2, packet),
2682 "bt_ctf_event_set_packet() succeeds when the event's and the packet's stream class are the same");
2683
2684 /*
2685 * It should be possible to set a packet created from the same
2686 * stream to an event with an existing packet.
2687 */
2688 packet2 = bt_ctf_packet_create(non_writer_stream);
2689 assert(packet2);
2690 ok(!bt_ctf_event_set_packet(event2, packet2),
2691 "bt_ctf_event_set_packet() succeeds when the event's current packet has the same stream");
2692 BT_PUT(packet2);
2693
2694 /*
2695 * It should not be possible to set a packet created from a
2696 * different stream to an event with an existing packet.
2697 */
2698 non_writer_stream2 = bt_ctf_stream_create(non_writer_sc, "rj45");
2699 assert(non_writer_stream2);
2700 packet2 = bt_ctf_packet_create(non_writer_stream);
2701 assert(packet2);
2702 ok(!bt_ctf_event_set_packet(event2, packet2),
2703 "bt_ctf_event_set_packet() fails when the event's current packet does not have the same stream");
2704
2705 bt_put(writer);
2706 bt_put(writer_trace);
2707 bt_put(writer_sc);
2708 bt_put(writer_stream);
2709 bt_put(writer_stream2);
2710 bt_put(non_writer_trace);
2711 bt_put(non_writer_sc);
2712 bt_put(non_writer_stream);
2713 bt_put(non_writer_stream2);
2714 bt_put(packet_stream);
2715 bt_put(writer_ec);
2716 bt_put(non_writer_ec);
2717 bt_put(event);
2718 bt_put(event2);
2719 bt_put(int_field);
2720 bt_put(empty_struct_ft);
2721 bt_put(writer_clock);
2722 bt_put(non_writer_clock_class);
2723 bt_put(packet);
2724 bt_put(packet2);
2725 recursive_rmdir(trace_path);
2726 }
2727
2728 static
2729 void test_clock_utils(void)
2730 {
2731 int ret;
2732 struct bt_ctf_clock *clock = NULL;
2733
2734 clock = bt_ctf_clock_create("water");
2735 assert(clock);
2736 ret = bt_ctf_clock_set_offset_s(clock, 1234);
2737 assert(!ret);
2738 ret = bt_ctf_clock_set_offset(clock, 1000);
2739 assert(!ret);
2740 ret = bt_ctf_clock_set_frequency(clock, 1000000000);
2741 assert(!ret);
2742 ret = bt_ctf_clock_set_frequency(clock, 1534);
2743 assert(!ret);
2744
2745 BT_PUT(clock);
2746 }
2747
2748 void test_set_clock_non_writer_stream_class(void)
2749 {
2750 struct bt_ctf_clock *clock;
2751 struct bt_ctf_trace *trace;
2752 struct bt_ctf_stream_class *sc;
2753 int ret;
2754
2755 clock = bt_ctf_clock_create("the_clock");
2756 assert(clock);
2757
2758 trace = bt_ctf_trace_create();
2759 assert(trace);
2760
2761 sc = bt_ctf_stream_class_create(NULL);
2762 assert(sc);
2763
2764 ret = bt_ctf_stream_class_set_clock(sc, clock);
2765 assert(ret == 0);
2766
2767 ret = bt_ctf_trace_add_stream_class(trace, sc);
2768 ok(ret < 0,
2769 "bt_ctf_trace_add_stream_class() fails with a stream class with a registered clock");
2770
2771 bt_put(clock);
2772 bt_put(trace);
2773 bt_put(sc);
2774 }
2775
2776 static
2777 void test_static_trace(void)
2778 {
2779 struct bt_ctf_trace *trace;
2780 struct bt_ctf_stream_class *stream_class;
2781 struct bt_ctf_stream_class *stream_class2;
2782 struct bt_ctf_stream *stream;
2783 struct bt_ctf_clock_class *clock_class;
2784 int ret;
2785
2786 trace = bt_ctf_trace_create();
2787 assert(trace);
2788 ret = bt_ctf_trace_set_native_byte_order(trace,
2789 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
2790 assert(ret == 0);
2791 stream_class = bt_ctf_stream_class_create(NULL);
2792 assert(stream_class);
2793 ret = bt_ctf_trace_add_stream_class(trace, stream_class);
2794 assert(ret == 0);
2795 stream = bt_ctf_stream_create(stream_class, "hello");
2796 ok(stream, "bt_ctf_stream_create() succeeds with a non-static trace");
2797 bt_put(stream);
2798 ok(!bt_ctf_trace_is_static(trace),
2799 "bt_ctf_trace_is_static() returns the expected value");
2800 ok(bt_ctf_trace_set_is_static(trace) == 0,
2801 "bt_ctf_trace_set_is_static() succeeds");
2802 ok(bt_ctf_trace_is_static(trace),
2803 "bt_ctf_trace_is_static() returns the expected value");
2804 clock_class = bt_ctf_clock_class_create("yes");
2805 assert(clock_class);
2806 stream_class2 = bt_ctf_stream_class_create(NULL);
2807 assert(stream_class2);
2808 ok(bt_ctf_trace_add_stream_class(trace, stream_class2),
2809 "bt_ctf_trace_add_stream_class() fails with a static trace");
2810 ok(bt_ctf_trace_add_clock_class(trace, clock_class),
2811 "bt_ctf_trace_add_clock_class() fails with a static trace");
2812 ok(!bt_ctf_stream_create(stream_class, "hello2"),
2813 "bt_ctf_stream_create() fails with a static trace");
2814
2815 bt_put(trace);
2816 bt_put(stream_class);
2817 bt_put(stream_class2);
2818 bt_put(clock_class);
2819 }
2820
2821 int main(int argc, char **argv)
2822 {
2823 char trace_path[] = "/tmp/ctfwriter_XXXXXX";
2824 char metadata_path[sizeof(trace_path) + 9];
2825 const char *clock_name = "test_clock";
2826 const char *clock_description = "This is a test clock";
2827 const char *returned_clock_name;
2828 const char *returned_clock_description;
2829 const uint64_t frequency = 1123456789;
2830 const int64_t offset_s = 1351530929945824323;
2831 const int64_t offset = 1234567;
2832 int64_t get_offset_s,
2833 get_offset;
2834 const uint64_t precision = 10;
2835 const int is_absolute = 0xFF;
2836 char *metadata_string;
2837 struct bt_ctf_writer *writer;
2838 struct utsname name;
2839 char hostname[BABELTRACE_HOST_NAME_MAX];
2840 struct bt_ctf_clock *clock, *ret_clock;
2841 struct bt_ctf_clock_class *ret_clock_class;
2842 struct bt_ctf_stream_class *stream_class, *ret_stream_class;
2843 struct bt_ctf_stream *stream1;
2844 struct bt_ctf_stream *stream;
2845 const char *ret_string;
2846 const unsigned char *ret_uuid;
2847 unsigned char tmp_uuid[16] = { 0 };
2848 struct bt_ctf_field_type *packet_context_type,
2849 *packet_context_field_type,
2850 *packet_header_type,
2851 *packet_header_field_type,
2852 *integer_type,
2853 *stream_event_context_type,
2854 *ret_field_type,
2855 *event_header_field_type;
2856 struct bt_ctf_field *packet_header, *packet_header_field;
2857 struct bt_ctf_trace *trace;
2858 int ret;
2859 int64_t ret_int64_t;
2860 struct bt_value *obj;
2861
2862 if (argc < 2) {
2863 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
2864 return -1;
2865 }
2866
2867 plan_tests(NR_TESTS);
2868
2869 if (!bt_mkdtemp(trace_path)) {
2870 perror("# perror");
2871 }
2872
2873 strcpy(metadata_path, trace_path);
2874 strcat(metadata_path + sizeof(trace_path) - 1, "/metadata");
2875
2876 writer = bt_ctf_writer_create(trace_path);
2877 ok(writer, "bt_ctf_create succeeds in creating trace with path");
2878
2879 ok(!bt_ctf_writer_get_trace(NULL),
2880 "bt_ctf_writer_get_trace correctly handles NULL");
2881 trace = bt_ctf_writer_get_trace(writer);
2882 ok(trace,
2883 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
2884 ok(bt_ctf_trace_set_native_byte_order(trace, BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
2885 "Set a trace's byte order to big endian");
2886 ok(bt_ctf_trace_get_byte_order(trace) == BT_CTF_BYTE_ORDER_BIG_ENDIAN,
2887 "bt_ctf_trace_get_byte_order returns a correct endianness");
2888
2889 /* Add environment context to the trace */
2890 ret = gethostname(hostname, sizeof(hostname));
2891 if (ret < 0) {
2892 return ret;
2893 }
2894 ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0,
2895 "Add host (%s) environment field to writer instance",
2896 hostname);
2897 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
2898 "test_value"),
2899 "bt_ctf_writer_add_environment_field error with NULL writer");
2900 ok(bt_ctf_writer_add_environment_field(writer, NULL,
2901 "test_value"),
2902 "bt_ctf_writer_add_environment_field error with NULL field name");
2903 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
2904 NULL),
2905 "bt_ctf_writer_add_environment_field error with NULL field value");
2906
2907 /* Test bt_ctf_trace_set_environment_field with an integer object */
2908 obj = bt_value_integer_create_init(23);
2909 assert(obj);
2910 ok(bt_ctf_trace_set_environment_field(NULL, "test_env_int_obj", obj),
2911 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
2912 ok(bt_ctf_trace_set_environment_field(trace, NULL, obj),
2913 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
2914 ok(bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", NULL),
2915 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
2916 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", obj),
2917 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
2918 BT_PUT(obj);
2919
2920 /* Test bt_ctf_trace_set_environment_field with a string object */
2921 obj = bt_value_string_create_init("the value");
2922 assert(obj);
2923 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_str_obj", obj),
2924 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
2925 BT_PUT(obj);
2926
2927 /* Test bt_ctf_trace_set_environment_field_integer */
2928 ok(bt_ctf_trace_set_environment_field_integer(NULL, "test_env_int",
2929 -194875),
2930 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
2931 ok(bt_ctf_trace_set_environment_field_integer(trace, NULL, -194875),
2932 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
2933 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
2934 -164973),
2935 "bt_ctf_trace_set_environment_field_integer succeeds");
2936
2937 /* Test bt_ctf_trace_set_environment_field_string */
2938 ok(bt_ctf_trace_set_environment_field_string(NULL, "test_env_str",
2939 "yeah"),
2940 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
2941 ok(bt_ctf_trace_set_environment_field_string(trace, NULL, "yeah"),
2942 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
2943 ok(bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
2944 NULL),
2945 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
2946 ok(!bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
2947 "oh yeah"),
2948 "bt_ctf_trace_set_environment_field_string succeeds");
2949
2950 /* Test bt_ctf_trace_get_environment_field_count */
2951 ok(bt_ctf_trace_get_environment_field_count(NULL) < 0,
2952 "bt_ctf_trace_get_environment_field_count handles a NULL trace correctly");
2953 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
2954 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
2955
2956 /* Test bt_ctf_trace_get_environment_field_name */
2957 ok(bt_ctf_trace_get_environment_field_name(NULL, 0) == NULL,
2958 "bt_ctf_trace_get_environment_field_name handles a NULL trace correctly");
2959 ok(bt_ctf_trace_get_environment_field_name(trace, -1) == NULL,
2960 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (negative)");
2961 ok(bt_ctf_trace_get_environment_field_name(trace, 5) == NULL,
2962 "bt_ctf_trace_get_environment_field_name handles an invalid index correctly (too large)");
2963 ret_string = bt_ctf_trace_get_environment_field_name(trace, 0);
2964 ok(ret_string && !strcmp(ret_string, "host"),
2965 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2966 ret_string = bt_ctf_trace_get_environment_field_name(trace, 1);
2967 ok(ret_string && !strcmp(ret_string, "test_env_int_obj"),
2968 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2969 ret_string = bt_ctf_trace_get_environment_field_name(trace, 2);
2970 ok(ret_string && !strcmp(ret_string, "test_env_str_obj"),
2971 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2972 ret_string = bt_ctf_trace_get_environment_field_name(trace, 3);
2973 ok(ret_string && !strcmp(ret_string, "test_env_int"),
2974 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2975 ret_string = bt_ctf_trace_get_environment_field_name(trace, 4);
2976 ok(ret_string && !strcmp(ret_string, "test_env_str"),
2977 "bt_ctf_trace_get_environment_field_name returns a correct field name");
2978
2979 /* Test bt_ctf_trace_get_environment_field_value */
2980 ok(bt_ctf_trace_get_environment_field_value(NULL, 0) == NULL,
2981 "bt_ctf_trace_get_environment_field_value handles a NULL trace correctly");
2982 ok(bt_ctf_trace_get_environment_field_value(trace, -1) == NULL,
2983 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (negative)");
2984 ok(bt_ctf_trace_get_environment_field_value(trace, 5) == NULL,
2985 "bt_ctf_trace_get_environment_field_value handles an invalid index correctly (too large)");
2986 obj = bt_ctf_trace_get_environment_field_value(trace, 1);
2987 ret = bt_value_integer_get(obj, &ret_int64_t);
2988 ok(!ret && ret_int64_t == 23,
2989 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
2990 BT_PUT(obj);
2991 obj = bt_ctf_trace_get_environment_field_value(trace, 2);
2992 ret = bt_value_string_get(obj, &ret_string);
2993 ok(!ret && ret_string && !strcmp(ret_string, "the value"),
2994 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
2995 BT_PUT(obj);
2996
2997 /* Test bt_ctf_trace_get_environment_field_value_by_name */
2998 ok(!bt_ctf_trace_get_environment_field_value_by_name(NULL,
2999 "test_env_str"),
3000 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL trace correctly");
3001 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, NULL),
3002 "bt_ctf_trace_get_environment_field_value_by_name handles a NULL name correctly");
3003 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, "oh oh"),
3004 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
3005 obj = bt_ctf_trace_get_environment_field_value_by_name(trace,
3006 "test_env_str");
3007 ret = bt_value_string_get(obj, &ret_string);
3008 ok(!ret && ret_string && !strcmp(ret_string, "oh yeah"),
3009 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
3010 BT_PUT(obj);
3011
3012 /* Test environment field replacement */
3013 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
3014 654321),
3015 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
3016 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
3017 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
3018 obj = bt_ctf_trace_get_environment_field_value(trace, 3);
3019 ret = bt_value_integer_get(obj, &ret_int64_t);
3020 ok(!ret && ret_int64_t == 654321,
3021 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
3022 BT_PUT(obj);
3023
3024 /* On Solaris, uname() can return any positive value on success */
3025 if (uname(&name) < 0) {
3026 perror("uname");
3027 return -1;
3028 }
3029
3030 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
3031 == 0, "Add sysname (%s) environment field to writer instance",
3032 name.sysname);
3033 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
3034 name.nodename) == 0,
3035 "Add nodename (%s) environment field to writer instance",
3036 name.nodename);
3037 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
3038 == 0, "Add release (%s) environment field to writer instance",
3039 name.release);
3040 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
3041 == 0, "Add version (%s) environment field to writer instance",
3042 name.version);
3043 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
3044 == 0, "Add machine (%s) environment field to writer istance",
3045 name.machine);
3046
3047 /* Define a clock and add it to the trace */
3048 ok(bt_ctf_clock_create("signed") == NULL,
3049 "Illegal clock name rejected");
3050 clock = bt_ctf_clock_create(clock_name);
3051 ok(clock, "Clock created sucessfully");
3052 returned_clock_name = bt_ctf_clock_get_name(clock);
3053 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
3054 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
3055 "Returned clock name is valid");
3056
3057 returned_clock_description = bt_ctf_clock_get_description(clock);
3058 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
3059 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
3060 "Clock description set successfully");
3061
3062 returned_clock_description = bt_ctf_clock_get_description(clock);
3063 ok(returned_clock_description,
3064 "bt_ctf_clock_get_description returns a description.");
3065 ok(returned_clock_description ?
3066 !strcmp(returned_clock_description, clock_description) : 0,
3067 "Returned clock description is valid");
3068
3069 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
3070 "bt_ctf_clock_get_frequency returns the correct default frequency");
3071 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
3072 "Set clock frequency");
3073 ok(bt_ctf_clock_get_frequency(clock) == frequency,
3074 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
3075
3076 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
3077 "bt_ctf_clock_get_offset_s succeeds");
3078 ok(get_offset_s == DEFAULT_CLOCK_OFFSET_S,
3079 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
3080 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
3081 "Set clock offset (seconds)");
3082 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
3083 "bt_ctf_clock_get_offset_s succeeds");
3084 ok(get_offset_s == offset_s,
3085 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
3086
3087 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
3088 "bt_ctf_clock_get_offset succeeds");
3089 ok(get_offset == DEFAULT_CLOCK_OFFSET,
3090 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
3091 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
3092 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
3093 "bt_ctf_clock_get_offset succeeds");
3094 ok(get_offset == offset,
3095 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
3096
3097 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
3098 "bt_ctf_clock_get_precision returns the correct default precision");
3099 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
3100 "Set clock precision");
3101 ok(bt_ctf_clock_get_precision(clock) == precision,
3102 "bt_ctf_clock_get_precision returns the correct precision once it is set");
3103
3104 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
3105 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
3106 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
3107 "Set clock absolute property");
3108 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
3109 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
3110
3111 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
3112 "Set clock time");
3113
3114 ok(!bt_ctf_clock_get_name(NULL),
3115 "bt_ctf_clock_get_name correctly handles NULL");
3116 ok(!bt_ctf_clock_get_description(NULL),
3117 "bt_ctf_clock_get_description correctly handles NULL");
3118 ok(bt_ctf_clock_get_frequency(NULL) == -1ULL,
3119 "bt_ctf_clock_get_frequency correctly handles NULL");
3120 ok(bt_ctf_clock_get_precision(NULL) == -1ULL,
3121 "bt_ctf_clock_get_precision correctly handles NULL");
3122 ok(bt_ctf_clock_get_offset_s(NULL, &get_offset_s) < 0,
3123 "bt_ctf_clock_get_offset_s correctly handles NULL clock");
3124 ok(bt_ctf_clock_get_offset_s(clock, NULL) < 0,
3125 "bt_ctf_clock_get_offset_s correctly handles NULL output");
3126 ok(bt_ctf_clock_get_offset(NULL, &get_offset) < 0,
3127 "bt_ctf_clock_get_offset correctly handles NULL clock");
3128 ok(bt_ctf_clock_get_offset(clock, NULL) < 0,
3129 "bt_ctf_clock_get_offset correctly handles NULL output");
3130 ok(bt_ctf_clock_get_is_absolute(NULL) < 0,
3131 "bt_ctf_clock_get_is_absolute correctly handles NULL");
3132
3133 ok(bt_ctf_clock_set_description(NULL, NULL) < 0,
3134 "bt_ctf_clock_set_description correctly handles NULL clock");
3135 ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0,
3136 "bt_ctf_clock_set_frequency correctly handles NULL clock");
3137 ok(bt_ctf_clock_set_precision(NULL, precision) < 0,
3138 "bt_ctf_clock_get_precision correctly handles NULL clock");
3139 ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0,
3140 "bt_ctf_clock_set_offset_s correctly handles NULL clock");
3141 ok(bt_ctf_clock_set_offset(NULL, offset) < 0,
3142 "bt_ctf_clock_set_offset correctly handles NULL clock");
3143 ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0,
3144 "bt_ctf_clock_set_is_absolute correctly handles NULL clock");
3145 ok(bt_ctf_clock_set_time(NULL, current_time) < 0,
3146 "bt_ctf_clock_set_time correctly handles NULL clock");
3147 ok(bt_ctf_clock_get_uuid(NULL) == NULL,
3148 "bt_ctf_clock_get_uuid correctly handles NULL clock");
3149 ret_uuid = bt_ctf_clock_get_uuid(clock);
3150 ok(ret_uuid,
3151 "bt_ctf_clock_get_uuid returns a UUID");
3152 if (ret_uuid) {
3153 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
3154 /* Slightly modify UUID */
3155 tmp_uuid[sizeof(tmp_uuid) - 1]++;
3156 }
3157
3158 ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0,
3159 "bt_ctf_clock_set_uuid correctly handles a NULL clock");
3160 ok(bt_ctf_clock_set_uuid(clock, NULL) < 0,
3161 "bt_ctf_clock_set_uuid correctly handles a NULL UUID");
3162 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
3163 "bt_ctf_clock_set_uuid sets a new uuid successfully");
3164 ret_uuid = bt_ctf_clock_get_uuid(clock);
3165 ok(ret_uuid,
3166 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
3167 ok(uuid_match(ret_uuid, tmp_uuid),
3168 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
3169
3170 /* Define a stream class */
3171 stream_class = bt_ctf_stream_class_create("test_stream");
3172
3173 ok(bt_ctf_stream_class_get_name(NULL) == NULL,
3174 "bt_ctf_stream_class_get_name handles NULL correctly");
3175 ret_string = bt_ctf_stream_class_get_name(stream_class);
3176 ok(ret_string && !strcmp(ret_string, "test_stream"),
3177 "bt_ctf_stream_class_get_name returns a correct stream class name");
3178
3179 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
3180 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
3181 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
3182 "bt_ctf_stream_class_get_clock handles NULL correctly");
3183
3184 ok(stream_class, "Create stream class");
3185 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
3186 "Set a stream class' clock");
3187 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
3188 ok(ret_clock == clock,
3189 "bt_ctf_stream_class_get_clock returns a correct clock");
3190 bt_put(ret_clock);
3191
3192 /* Test the event fields and event types APIs */
3193 type_field_tests();
3194
3195 /* Test fields copying */
3196 field_copy_tests();
3197
3198 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
3199 "bt_ctf_stream_class_get_id returns an error when no id is set");
3200 ok(bt_ctf_stream_class_get_id(NULL) < 0,
3201 "bt_ctf_stream_class_get_id handles NULL correctly");
3202 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
3203 "bt_ctf_stream_class_set_id handles NULL correctly");
3204 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
3205 "Set an stream class' id");
3206 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
3207 "bt_ctf_stream_class_get_id returns the correct value");
3208
3209 /* Validate default event header fields */
3210 ok(bt_ctf_stream_class_get_event_header_type(NULL) == NULL,
3211 "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
3212 ret_field_type = bt_ctf_stream_class_get_event_header_type(
3213 stream_class);
3214 ok(ret_field_type,
3215 "bt_ctf_stream_class_get_event_header_type returns an event header type");
3216 ok(bt_ctf_field_type_get_type_id(ret_field_type) == BT_CTF_FIELD_TYPE_ID_STRUCT,
3217 "Default event header type is a structure");
3218 event_header_field_type =
3219 bt_ctf_field_type_structure_get_field_type_by_name(
3220 ret_field_type, "id");
3221 ok(event_header_field_type,
3222 "Default event header type contains an \"id\" field");
3223 ok(bt_ctf_field_type_get_type_id(
3224 event_header_field_type) == BT_CTF_FIELD_TYPE_ID_INTEGER,
3225 "Default event header \"id\" field is an integer");
3226 bt_put(event_header_field_type);
3227 event_header_field_type =
3228 bt_ctf_field_type_structure_get_field_type_by_name(
3229 ret_field_type, "timestamp");
3230 ok(event_header_field_type,
3231 "Default event header type contains a \"timestamp\" field");
3232 ok(bt_ctf_field_type_get_type_id(
3233 event_header_field_type) == BT_CTF_FIELD_TYPE_ID_INTEGER,
3234 "Default event header \"timestamp\" field is an integer");
3235 bt_put(event_header_field_type);
3236 bt_put(ret_field_type);
3237
3238 /* Add a custom trace packet header field */
3239 ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL,
3240 "bt_ctf_trace_get_packet_header_type handles NULL correctly");
3241 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
3242 ok(packet_header_type,
3243 "bt_ctf_trace_get_packet_header_type returns a packet header");
3244 ok(bt_ctf_field_type_get_type_id(packet_header_type) == BT_CTF_FIELD_TYPE_ID_STRUCT,
3245 "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
3246 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3247 packet_header_type, "magic");
3248 ok(ret_field_type, "Default packet header type contains a \"magic\" field");
3249 bt_put(ret_field_type);
3250 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3251 packet_header_type, "uuid");
3252 ok(ret_field_type, "Default packet header type contains a \"uuid\" field");
3253 bt_put(ret_field_type);
3254 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
3255 packet_header_type, "stream_id");
3256 ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
3257 bt_put(ret_field_type);
3258
3259 packet_header_field_type = bt_ctf_field_type_integer_create(22);
3260 ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
3261 packet_header_field_type, "custom_trace_packet_header_field"),
3262 "Added a custom trace packet header field successfully");
3263
3264 ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0,
3265 "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
3266 ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type),
3267 "Set a trace packet_header_type successfully");
3268
3269 ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL,
3270 "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
3271
3272 /* Add a custom field to the stream class' packet context */
3273 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
3274 ok(packet_context_type,
3275 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
3276 ok(bt_ctf_field_type_get_type_id(packet_context_type) == BT_CTF_FIELD_TYPE_ID_STRUCT,
3277 "Packet context is a structure");
3278
3279 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
3280 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
3281
3282 integer_type = bt_ctf_field_type_integer_create(32);
3283 ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
3284 integer_type) < 0,
3285 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
3286 /* Create a "uint5_t" equivalent custom packet context field */
3287 packet_context_field_type = bt_ctf_field_type_integer_create(5);
3288
3289 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3290 packet_context_field_type, "custom_packet_context_field");
3291 ok(ret == 0, "Packet context field added successfully");
3292
3293 /* Define a stream event context containing a my_integer field. */
3294 ok(bt_ctf_stream_class_get_event_context_type(NULL) == NULL,
3295 "bt_ctf_stream_class_get_event_context_type handles NULL correctly");
3296 ok(bt_ctf_stream_class_get_event_context_type(
3297 stream_class) == NULL,
3298 "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set.");
3299 stream_event_context_type = bt_ctf_field_type_structure_create();
3300 bt_ctf_field_type_structure_add_field(stream_event_context_type,
3301 integer_type, "common_event_context");
3302
3303 ok(bt_ctf_stream_class_set_event_context_type(NULL,
3304 stream_event_context_type) < 0,
3305 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
3306 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
3307 integer_type) < 0,
3308 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
3309
3310 ok(bt_ctf_stream_class_set_event_context_type(
3311 stream_class, stream_event_context_type) == 0,
3312 "Set a new stream event context type");
3313 ret_field_type = bt_ctf_stream_class_get_event_context_type(
3314 stream_class);
3315 ok(ret_field_type == stream_event_context_type,
3316 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
3317 bt_put(ret_field_type);
3318
3319 /* Instantiate a stream and append events */
3320 ret = bt_ctf_writer_add_clock(writer, clock);
3321 assert(ret == 0);
3322 ok(bt_ctf_trace_get_stream_count(trace) == 0,
3323 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)");
3324 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
3325 ok(stream1, "Instanciate a stream class from writer");
3326 ok(bt_ctf_trace_get_stream_count(trace) == 1,
3327 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)");
3328 stream = bt_ctf_trace_get_stream(trace, 0);
3329 ok(stream == stream1,
3330 "bt_ctf_trace_get_stream() succeeds and returns the correct value");
3331 BT_PUT(stream);
3332
3333 /*
3334 * Creating a stream through a writer adds the given stream
3335 * class to the writer's trace, thus registering the stream
3336 * class's clock to the trace.
3337 */
3338 ok(bt_ctf_trace_get_clock_class_count(NULL) < 0,
3339 "bt_ctf_trace_get_clock_class_count correctly handles NULL");
3340 ok(bt_ctf_trace_get_clock_class_count(trace) == 1,
3341 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
3342 ok(!bt_ctf_trace_get_clock_class(NULL, 0),
3343 "bt_ctf_trace_get_clock_class correctly handles NULL");
3344 ok(!bt_ctf_trace_get_clock_class(trace, -1),
3345 "bt_ctf_trace_get_clock_class correctly handles negative indexes");
3346 ok(!bt_ctf_trace_get_clock_class(trace, 1),
3347 "bt_ctf_trace_get_clock_class correctly handles out of bound accesses");
3348 ret_clock_class = bt_ctf_trace_get_clock_class(trace, 0);
3349 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class),
3350 bt_ctf_clock_get_name(clock)) == 0,
3351 "bt_ctf_trace_get_clock_class returns the right clock instance");
3352 bt_put(ret_clock_class);
3353 ok(!bt_ctf_trace_get_clock_class_by_name(trace, NULL),
3354 "bt_ctf_trace_get_clock_class_by_name correctly handles NULL (trace)");
3355 ok(!bt_ctf_trace_get_clock_class_by_name(NULL, clock_name),
3356 "bt_ctf_trace_get_clock_by_name correctly handles NULL (clock name)");
3357 ok(!bt_ctf_trace_get_clock_class_by_name(NULL, NULL),
3358 "bt_ctf_trace_get_clock_by_name correctly handles NULL (both)");
3359 ret_clock_class = bt_ctf_trace_get_clock_class_by_name(trace, clock_name);
3360 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class),
3361 bt_ctf_clock_get_name(clock)) == 0,
3362 "bt_ctf_trace_get_clock_class returns the right clock instance");
3363 bt_put(ret_clock_class);
3364 ok(!bt_ctf_trace_get_clock_class_by_name(trace, "random"),
3365 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
3366
3367 ok(bt_ctf_stream_get_class(NULL) == NULL,
3368 "bt_ctf_stream_get_class correctly handles NULL");
3369 ret_stream_class = bt_ctf_stream_get_class(stream1);
3370 ok(ret_stream_class,
3371 "bt_ctf_stream_get_class returns a stream class");
3372 ok(ret_stream_class == stream_class,
3373 "Returned stream class is of the correct type");
3374
3375 /*
3376 * Packet header, packet context, event header, and stream
3377 * event context types were copied for the resolving
3378 * process
3379 */
3380 BT_PUT(packet_header_type);
3381 BT_PUT(packet_context_type);
3382 BT_PUT(stream_event_context_type);
3383 packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
3384 assert(packet_header_type);
3385 packet_context_type =
3386 bt_ctf_stream_class_get_packet_context_type(stream_class);
3387 assert(packet_context_type);
3388 stream_event_context_type =
3389 bt_ctf_stream_class_get_event_context_type(stream_class);
3390 assert(stream_event_context_type);
3391
3392 /*
3393 * Try to modify the packet context type after a stream has been
3394 * created.
3395 */
3396 ret = bt_ctf_field_type_structure_add_field(packet_header_type,
3397 packet_header_field_type, "should_fail");
3398 ok(ret < 0,
3399 "Trace packet header type can't be modified once a stream has been instanciated");
3400
3401 /*
3402 * Try to modify the packet context type after a stream has been
3403 * created.
3404 */
3405 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
3406 packet_context_field_type, "should_fail");
3407 ok(ret < 0,
3408 "Packet context type can't be modified once a stream has been instanciated");
3409
3410 /*
3411 * Try to modify the stream event context type after a stream has been
3412 * created.
3413 */
3414 ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
3415 integer_type, "should_fail");
3416 ok(ret < 0,
3417 "Stream event context type can't be modified once a stream has been instanciated");
3418
3419 /* Should fail after instanciating a stream (frozen) */
3420 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
3421 "Changes to a stream class that was already instantiated fail");
3422
3423 /* Populate the custom packet header field only once for all tests */
3424 ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
3425 "bt_ctf_stream_get_packet_header handles NULL correctly");
3426 packet_header = bt_ctf_stream_get_packet_header(stream1);
3427 ok(packet_header,
3428 "bt_ctf_stream_get_packet_header returns a packet header");
3429 ret_field_type = bt_ctf_field_get_type(packet_header);
3430 ok(ret_field_type == packet_header_type,
3431 "Stream returns a packet header of the appropriate type");
3432 bt_put(ret_field_type);
3433 packet_header_field = bt_ctf_field_structure_get_field(packet_header,
3434 "custom_trace_packet_header_field");
3435 ok(packet_header_field,
3436 "Packet header structure contains a custom field with the appropriate name");
3437 ret_field_type = bt_ctf_field_get_type(packet_header_field);
3438 ok(bt_ctf_field_type_compare(ret_field_type, packet_header_field_type) == 0,
3439 "Custom packet header field is of the expected type");
3440 ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field,
3441 54321), "Set custom packet header value successfully");
3442 ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
3443 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
3444 ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
3445 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
3446 ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
3447 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
3448 ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
3449 "Successfully set a stream's packet header");
3450
3451 ok(bt_ctf_writer_add_environment_field(writer, "new_field", "test") == 0,
3452 "Add environment field to writer after stream creation");
3453
3454 test_clock_utils();
3455
3456 test_create_writer_vs_non_writer_mode();
3457
3458 test_set_clock_non_writer_stream_class();
3459
3460 test_instanciate_event_before_stream(writer, clock);
3461
3462 append_simple_event(stream_class, stream1, clock);
3463
3464 packet_resize_test(stream_class, stream1, clock);
3465
3466 append_complex_event(stream_class, stream1, clock);
3467
3468 append_existing_event_class(stream_class);
3469
3470 test_empty_stream(writer);
3471
3472 test_custom_event_header_stream(writer, clock);
3473
3474 test_static_trace();
3475
3476 metadata_string = bt_ctf_writer_get_metadata_string(writer);
3477 ok(metadata_string, "Get metadata string");
3478
3479 bt_ctf_writer_flush_metadata(writer);
3480
3481 bt_put(clock);
3482 bt_put(ret_stream_class);
3483 bt_put(writer);
3484 bt_put(stream1);
3485 bt_put(packet_context_type);
3486 bt_put(packet_context_field_type);
3487 bt_put(integer_type);
3488 bt_put(stream_event_context_type);
3489 bt_put(ret_field_type);
3490 bt_put(packet_header_type);
3491 bt_put(packet_header_field_type);
3492 bt_put(packet_header);
3493 bt_put(packet_header_field);
3494 bt_put(trace);
3495 free(metadata_string);
3496 bt_put(stream_class);
3497
3498 validate_trace(argv[1], trace_path);
3499
3500 //recursive_rmdir(trace_path);
3501 return 0;
3502 }
This page took 0.278959 seconds and 5 git commands to generate.