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