Split CTF IR and CTF writer APIs and implementations
[babeltrace.git] / tests / lib / test_ctf_writer.c
CommitLineData
39d74371 1/*
5c424b9c 2 * test_ctf_writer.c
39d74371
JG
3 *
4 * CTF Writer test
5 *
5c424b9c 6 * Copyright 2013 - 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
39d74371
JG
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
39d74371
JG
22#include <babeltrace/ctf-writer/writer.h>
23#include <babeltrace/ctf-writer/clock.h>
24#include <babeltrace/ctf-writer/stream.h>
25#include <babeltrace/ctf-writer/event.h>
26#include <babeltrace/ctf-writer/event-types.h>
27#include <babeltrace/ctf-writer/event-fields.h>
72bd645e 28#include <babeltrace/ctf-writer/stream-class.h>
3dca2276 29#include <babeltrace/ctf-writer/trace.h>
83509119 30#include <babeltrace/ref.h>
39d74371 31#include <babeltrace/ctf/events.h>
dac5c838 32#include <babeltrace/values.h>
32bd47d1 33#include <glib.h>
39d74371 34#include <unistd.h>
3d9990ac 35#include <babeltrace/compat/stdlib-internal.h>
39d74371 36#include <stdio.h>
3d9990ac
PP
37#include <babeltrace/compat/limits-internal.h>
38#include <babeltrace/compat/stdio-internal.h>
39d74371
JG
39#include <string.h>
40#include <assert.h>
39d74371 41#include <fcntl.h>
39d74371 42#include "tap/tap.h"
10817e06
JG
43#include <math.h>
44#include <float.h>
851299b9 45#include "common.h"
39d74371
JG
46
47#define METADATA_LINE_SIZE 512
48#define SEQUENCE_TEST_LENGTH 10
1fac895e 49#define ARRAY_TEST_LENGTH 5
c2237f17 50#define PACKET_RESIZE_TEST_DEF_LENGTH 100000
39d74371 51
5494ce8b
JG
52#define DEFAULT_CLOCK_FREQ 1000000000
53#define DEFAULT_CLOCK_PRECISION 1
54#define DEFAULT_CLOCK_OFFSET 0
55#define DEFAULT_CLOCK_OFFSET_S 0
56#define DEFAULT_CLOCK_IS_ABSOLUTE 0
57#define DEFAULT_CLOCK_TIME 0
e1e30a8c 58#define DEFAULT_CLOCK_VALUE 0
5494ce8b 59
3dca2276 60#define NR_TESTS 347
8bbe269d 61
e2e51e32
MJ
62struct bt_utsname {
63 char sysname[BABELTRACE_HOST_NAME_MAX];
64 char nodename[BABELTRACE_HOST_NAME_MAX];
65 char release[BABELTRACE_HOST_NAME_MAX];
66 char version[BABELTRACE_HOST_NAME_MAX];
67 char machine[BABELTRACE_HOST_NAME_MAX];
68};
69
61cf588b 70static int64_t current_time = 42;
c2237f17 71static unsigned int packet_resize_test_length = PACKET_RESIZE_TEST_DEF_LENGTH;
39d74371 72
e61caf8e 73/* Return 1 if uuids match, zero if different. */
a3d8579b 74static
e61caf8e
JG
75int uuid_match(const unsigned char *uuid_a, const unsigned char *uuid_b)
76{
77 int ret = 0;
78 int i;
79
80 if (!uuid_a || !uuid_b) {
81 goto end;
82 }
83
23f1c913 84 for (i = 0; i < 16; i++) {
e61caf8e
JG
85 if (uuid_a[i] != uuid_b[i]) {
86 goto end;
87 }
88 }
89
90 ret = 1;
91end:
92 return ret;
93}
94
a3d8579b 95static
39d74371
JG
96void validate_trace(char *parser_path, char *trace_path)
97{
98 int ret = 0;
f5d4e7db 99 gint exit_status;
5de3705f 100 char *argv[] = {parser_path, trace_path, "-o", "dummy", NULL};
39d74371 101
f5d4e7db 102 if (!parser_path || !trace_path) {
39d74371
JG
103 ret = -1;
104 goto result;
105 }
106
f5d4e7db
MJ
107 if (!g_spawn_sync(NULL,
108 argv,
109 NULL,
110 G_SPAWN_STDOUT_TO_DEV_NULL,
111 NULL,
112 NULL,
113 NULL,
3dca2276 114 NULL,
f5d4e7db
MJ
115 &exit_status,
116 NULL)) {
117 diag("Failed to spawn babeltrace.");
39d74371
JG
118 ret = -1;
119 goto result;
120 }
121
2a116459 122 /* Replace by g_spawn_check_exit_status when we require glib >= 2.34 */
e373846d 123#ifdef G_OS_UNIX
2a116459
MJ
124 ret = WIFEXITED(exit_status) ? WEXITSTATUS(exit_status) : -1;
125#else
126 ret = exit_status;
127#endif
128
129 if (ret != 0) {
f5d4e7db 130 diag("Babeltrace returned an error.");
f5d4e7db 131 goto result;
39d74371 132 }
f5d4e7db 133
39d74371
JG
134result:
135 ok(ret == 0, "Babeltrace could read the resulting trace");
39d74371
JG
136}
137
a3d8579b 138static
3dca2276
PP
139void append_simple_event(struct bt_ctf_stream_class *stream_class,
140 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
39d74371
JG
141{
142 /* Create and add a simple event class */
3dca2276
PP
143 struct bt_ctf_event_class *simple_event_class =
144 bt_ctf_event_class_create("Simple Event");
145 struct bt_ctf_field_type *uint_12_type =
146 bt_ctf_field_type_integer_create(12);
147 struct bt_ctf_field_type *int_64_type =
148 bt_ctf_field_type_integer_create(64);
149 struct bt_ctf_field_type *float_type =
150 bt_ctf_field_type_floating_point_create();
151 struct bt_ctf_field_type *enum_type;
152 struct bt_ctf_field_type *enum_type_unsigned =
153 bt_ctf_field_type_enumeration_create(uint_12_type);
154 struct bt_ctf_field_type *event_context_type =
155 bt_ctf_field_type_structure_create();
156 struct bt_ctf_field_type *event_payload_type = NULL;
157 struct bt_ctf_field_type *returned_type;
158 struct bt_ctf_event *simple_event;
159 struct bt_ctf_field *integer_field;
160 struct bt_ctf_field *float_field;
161 struct bt_ctf_field *enum_field;
162 struct bt_ctf_field *enum_field_unsigned;
163 struct bt_ctf_field *enum_container_field;
10817e06 164 const char *mapping_name_test = "truie";
10817e06 165 const double double_test_value = 3.1415;
3dca2276 166 struct bt_ctf_field *enum_container_field_unsigned;
7cfd41d6
JG
167 const char *mapping_name_negative_test = "negative_value";
168 const char *ret_char;
10817e06 169 double ret_double;
7cfd41d6
JG
170 int64_t ret_range_start_int64_t, ret_range_end_int64_t;
171 uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t;
3dca2276
PP
172 struct bt_ctf_event_class *ret_event_class;
173 struct bt_ctf_field *packet_context;
174 struct bt_ctf_field *packet_context_field;
175 struct bt_ctf_field *stream_event_context;
176 struct bt_ctf_field *stream_event_context_field;
177 struct bt_ctf_field *event_context;
178 struct bt_ctf_field *event_context_field;
179 struct bt_ctf_field_type *ep_integer_field_type = NULL;
180 struct bt_ctf_field_type *ep_enum_field_type = NULL;
181 struct bt_ctf_field_type *ep_enum_field_unsigned_type = NULL;
182 struct bt_ctf_field_type_enumeration_mapping_iterator *iter = NULL;
bb34b5a7 183 int ret;
7cfd41d6
JG
184
185 ok(uint_12_type, "Create an unsigned integer type");
186
687ae062
JG
187 ok(!bt_ctf_field_type_integer_set_signed(int_64_type, 1),
188 "Set signed 64 bit integer signedness to true");
7cfd41d6 189 ok(int_64_type, "Create a signed integer type");
3dca2276 190 enum_type = bt_ctf_field_type_enumeration_create(int_64_type);
7cfd41d6 191
3dca2276
PP
192 returned_type = bt_ctf_field_type_enumeration_get_container_field_type(enum_type);
193 ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_field_type returns the right type");
194 ok(!bt_ctf_field_type_enumeration_create(enum_type),
195 "bt_ctf_field_enumeration_type_create rejects non-integer container field types");
83509119 196 bt_put(returned_type);
39d74371 197
3dca2276
PP
198 bt_ctf_field_type_set_alignment(float_type, 32);
199 ok(bt_ctf_field_type_get_alignment(float_type) == 32,
200 "bt_ctf_field_type_get_alignment returns a correct value");
7cfd41d6 201
3dca2276 202 ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0,
7cfd41d6 203 "Set a floating point type's exponent digit count");
3dca2276 204 ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0,
7cfd41d6
JG
205 "Set a floating point type's mantissa digit count");
206
3dca2276
PP
207 ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11,
208 "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value");
209 ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53,
210 "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value");
8382544f
JG
211
212 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
7cfd41d6 213 mapping_name_negative_test, -12345, 0) == 0,
3dca2276 214 "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings");
8382544f 215 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
7cfd41d6 216 "escaping; \"test\"", 1, 1) == 0,
3dca2276 217 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes");
7cfd41d6
JG
218 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
219 "\tanother \'escaping\'\n test\"", 2, 4) == 0,
3dca2276 220 "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters");
8382544f
JG
221 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type,
222 "event clock int float", 5, 22) == 0,
223 "Accept enumeration mapping strings containing reserved keywords");
7cfd41d6
JG
224 bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
225 42, 42);
226 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
3dca2276 227 43, 51) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts duplicate mapping names");
7cfd41d6 228 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something",
3dca2276 229 -500, -400) == 0, "bt_ctf_field_type_enumeration_add_mapping accepts overlapping enum entries");
7cfd41d6 230 ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test,
3dca2276 231 -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start");
7cfd41d6
JG
232 bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000);
233
3dca2276 234 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type,
7cfd41d6
JG
235 "enum_field") == 0, "Add signed enumeration field to event");
236
3dca2276 237 ok(bt_ctf_field_type_enumeration_signed_get_mapping_by_index(enum_type, 0, NULL,
7cfd41d6 238 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
3dca2276
PP
239 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index handles a NULL string correctly");
240 ok(bt_ctf_field_type_enumeration_signed_get_mapping_by_index(enum_type, 0, &ret_char,
96e8f959 241 NULL, &ret_range_end_int64_t) == 0,
3dca2276
PP
242 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index handles a NULL start correctly");
243 ok(bt_ctf_field_type_enumeration_signed_get_mapping_by_index(enum_type, 0, &ret_char,
96e8f959 244 &ret_range_start_int64_t, NULL) == 0,
3dca2276 245 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index handles a NULL end correctly");
96e8f959 246 /* Assumes entries are sorted by range_start values. */
3dca2276 247 ok(bt_ctf_field_type_enumeration_signed_get_mapping_by_index(enum_type, 6, &ret_char,
96e8f959 248 &ret_range_start_int64_t, &ret_range_end_int64_t) == 0,
3dca2276 249 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index returns a value");
7cfd41d6 250 ok(!strcmp(ret_char, mapping_name_test),
3dca2276 251 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index returns a correct mapping name");
7cfd41d6 252 ok(ret_range_start_int64_t == 42,
3dca2276 253 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index returns a correct mapping start");
7cfd41d6 254 ok(ret_range_end_int64_t == 42,
3dca2276 255 "bt_ctf_field_type_enumeration_signed_get_mapping_by_index returns a correct mapping end");
7cfd41d6 256
3dca2276 257 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned,
7cfd41d6 258 "escaping; \"test\"", 0, 0) == 0,
3dca2276
PP
259 "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts enumeration mapping strings containing quotes");
260 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned,
7cfd41d6 261 "\tanother \'escaping\'\n test\"", 1, 4) == 0,
3dca2276
PP
262 "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts enumeration mapping strings containing special characters");
263 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned,
7cfd41d6 264 "event clock int float", 5, 22) == 0,
3dca2276
PP
265 "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts enumeration mapping strings containing reserved keywords");
266 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned, mapping_name_test,
267 42, 42) == 0, "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts single-value ranges");
268 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned, mapping_name_test,
269 43, 51) == 0, "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts duplicate mapping names");
270 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned, "something",
271 7, 8) == 0, "bt_ctf_field_type_enumeration_unsigned_add_mapping accepts overlapping enum entries");
272 ok(bt_ctf_field_type_enumeration_unsigned_add_mapping(enum_type_unsigned, mapping_name_test,
273 55, 54), "bt_ctf_field_type_enumeration_unsigned_add_mapping rejects mapping where end < start");
274 ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned,
7cfd41d6
JG
275 "enum_field_unsigned") == 0, "Add unsigned enumeration field to event");
276
3dca2276
PP
277 ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 6,
278 "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value");
7cfd41d6 279
3dca2276 280 ok(bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index(enum_type_unsigned, 0, NULL,
96e8f959 281 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
3dca2276
PP
282 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index handles a NULL string correctly");
283 ok(bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index(enum_type_unsigned, 0, &ret_char,
96e8f959 284 NULL, &ret_range_end_uint64_t) == 0,
3dca2276
PP
285 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index handles a NULL start correctly");
286 ok(bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index(enum_type_unsigned, 0, &ret_char,
96e8f959 287 &ret_range_start_uint64_t, NULL) == 0,
3dca2276
PP
288 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index handles a NULL end correctly");
289 ok(bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index(enum_type_unsigned, 4, &ret_char,
7cfd41d6 290 &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0,
3dca2276 291 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index returns a value");
7cfd41d6 292 ok(!strcmp(ret_char, mapping_name_test),
3dca2276 293 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index returns a correct mapping name");
7cfd41d6 294 ok(ret_range_start_uint64_t == 42,
3dca2276 295 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index returns a correct mapping start");
7cfd41d6 296 ok(ret_range_end_uint64_t == 42,
3dca2276 297 "bt_ctf_field_type_enumeration_unsigned_get_mapping_by_index returns a correct mapping end");
8382544f 298
3dca2276 299 bt_ctf_event_class_add_field(simple_event_class, uint_12_type,
39d74371 300 "integer_field");
3dca2276 301 bt_ctf_event_class_add_field(simple_event_class, float_type,
39d74371 302 "float_field");
58203827 303
3dca2276 304 assert(!bt_ctf_event_class_set_id(simple_event_class, 13));
3ed04f17 305
a001a2ac 306 /* Set an event context type which will contain a single integer. */
3dca2276 307 ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
58203827
JG
308 "event_specific_context"),
309 "Add event specific context field");
58203827 310
3dca2276
PP
311 ok(bt_ctf_event_class_set_context_field_type(NULL, event_context_type) < 0,
312 "bt_ctf_event_class_set_context_field_type handles a NULL event class correctly");
313 ok(!bt_ctf_event_class_set_context_field_type(simple_event_class, event_context_type),
58203827 314 "Set an event class' context type successfully");
3dca2276 315 returned_type = bt_ctf_event_class_get_context_field_type(simple_event_class);
58203827 316 ok(returned_type == event_context_type,
3dca2276 317 "bt_ctf_event_class_get_context_field_type returns the appropriate type");
83509119 318 bt_put(returned_type);
58203827 319
3dca2276 320 ok(!bt_ctf_stream_class_add_event_class(stream_class, simple_event_class),
a046cf3c 321 "Adding simple event class to stream class");
39d74371 322
09840de5 323 /*
3dca2276 324 * bt_ctf_stream_class_add_event_class() copies the field types
09840de5
PP
325 * of simple_event_class, so we retrieve the new ones to create
326 * the appropriate fields.
327 */
328 BT_PUT(event_context_type);
329 BT_PUT(event_payload_type);
3dca2276 330 event_payload_type = bt_ctf_event_class_get_payload_field_type(
09840de5
PP
331 simple_event_class);
332 assert(event_payload_type);
3dca2276 333 event_context_type = bt_ctf_event_class_get_context_field_type(
09840de5
PP
334 simple_event_class);
335 assert(event_context_type);
336 ep_integer_field_type =
3dca2276 337 bt_ctf_field_type_structure_get_field_type_by_name(
09840de5
PP
338 event_payload_type, "integer_field");
339 assert(ep_integer_field_type);
340 ep_enum_field_type =
3dca2276 341 bt_ctf_field_type_structure_get_field_type_by_name(
09840de5
PP
342 event_payload_type, "enum_field");
343 assert(ep_enum_field_type);
344 ep_enum_field_unsigned_type =
3dca2276 345 bt_ctf_field_type_structure_get_field_type_by_name(
09840de5
PP
346 event_payload_type, "enum_field_unsigned");
347 assert(ep_enum_field_unsigned_type);
348
3dca2276
PP
349 ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1,
350 "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes");
351 ret_event_class = bt_ctf_stream_class_get_event_class_by_index(stream_class, 0);
e3c971da 352 ok(ret_event_class == simple_event_class,
3dca2276 353 "bt_ctf_stream_class_get_event_class returns the correct event class");
83509119 354 bt_put(ret_event_class);
3dca2276
PP
355 ok(!bt_ctf_stream_class_get_event_class_by_id(stream_class, 2),
356 "bt_ctf_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist");
3ed04f17 357 ret_event_class =
3dca2276 358 bt_ctf_stream_class_get_event_class_by_id(stream_class, 13);
3ed04f17 359 ok(ret_event_class == simple_event_class,
3dca2276 360 "bt_ctf_stream_class_get_event_class_by_id returns a correct event class");
83509119 361 bt_put(ret_event_class);
e3c971da 362
3dca2276 363 simple_event = bt_ctf_event_create(simple_event_class);
39d74371
JG
364 ok(simple_event,
365 "Instantiate an event containing a single integer field");
366
3dca2276
PP
367 integer_field = bt_ctf_field_create(ep_integer_field_type);
368 bt_ctf_field_integer_unsigned_set_value(integer_field, 42);
369 ok(bt_ctf_event_set_payload(simple_event, "integer_field",
370 integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field");
50842bdc 371
3dca2276
PP
372 float_field = bt_ctf_event_get_payload(simple_event, "float_field");
373 bt_ctf_field_floating_point_set_value(float_field, double_test_value);
374 ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double),
375 "bt_ctf_field_floating_point_get_value returns a double value");
10817e06 376 ok(fabs(ret_double - double_test_value) <= DBL_EPSILON,
3dca2276 377 "bt_ctf_field_floating_point_get_value returns a correct value");
10817e06 378
3dca2276 379 enum_field = bt_ctf_field_create(ep_enum_field_type);
09840de5 380 assert(enum_field);
96e8f959 381
3dca2276
PP
382 enum_container_field = bt_ctf_field_enumeration_get_container(enum_field);
383 ok(bt_ctf_field_integer_signed_set_value(
7cfd41d6
JG
384 enum_container_field, -42) == 0,
385 "Set signed enumeration container value");
3dca2276 386 ret = bt_ctf_event_set_payload(simple_event, "enum_field", enum_field);
bb34b5a7 387 assert(!ret);
e0f15669 388 BT_PUT(iter);
39d74371 389
3dca2276 390 enum_field_unsigned = bt_ctf_field_create(ep_enum_field_unsigned_type);
09840de5 391 assert(enum_field_unsigned);
3dca2276 392 enum_container_field_unsigned = bt_ctf_field_enumeration_get_container(
7cfd41d6 393 enum_field_unsigned);
3dca2276 394 ok(bt_ctf_field_integer_unsigned_set_value(
7cfd41d6
JG
395 enum_container_field_unsigned, 42) == 0,
396 "Set unsigned enumeration container value");
3dca2276 397 ret = bt_ctf_event_set_payload(simple_event, "enum_field_unsigned",
7cfd41d6 398 enum_field_unsigned);
bb34b5a7 399 assert(!ret);
7cfd41d6 400
39d74371
JG
401 ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time");
402
6e1f8ea1 403 /* Populate stream event context */
5fd2e9fd 404 stream_event_context =
3dca2276 405 bt_ctf_event_get_stream_event_context(simple_event);
5fd2e9fd 406 assert(stream_event_context);
3dca2276 407 stream_event_context_field = bt_ctf_field_structure_get_field_by_name(
5edae678 408 stream_event_context, "common_event_context");
3dca2276 409 bt_ctf_field_integer_unsigned_set_value(stream_event_context_field, 42);
5edae678
JG
410
411 /* Populate the event's context */
3dca2276 412 event_context = bt_ctf_event_get_context(simple_event);
5edae678 413 ok(event_context,
3dca2276
PP
414 "bt_ctf_event_get_context returns a field");
415 returned_type = bt_ctf_field_get_type(event_context);
5edae678 416 ok(returned_type == event_context_type,
3dca2276
PP
417 "bt_ctf_event_get_context returns a field of the appropriate type");
418 event_context_field = bt_ctf_field_structure_get_field_by_name(event_context,
5edae678 419 "event_specific_context");
3dca2276 420 ok(!bt_ctf_field_integer_unsigned_set_value(event_context_field, 1234),
5edae678 421 "Successfully set an event context's value");
3dca2276 422 ok(!bt_ctf_event_set_context(simple_event, event_context),
5edae678 423 "Set an event context successfully");
6e1f8ea1 424
3dca2276 425 ok(bt_ctf_stream_append_event(stream, simple_event) == 0,
39d74371
JG
426 "Append simple event to trace stream");
427
3dca2276 428 packet_context = bt_ctf_stream_get_packet_context(stream);
12c8a1a3 429 ok(packet_context,
3dca2276 430 "bt_ctf_stream_get_packet_context returns a packet context");
12c8a1a3 431
3dca2276 432 packet_context_field = bt_ctf_field_structure_get_field_by_name(packet_context,
12c8a1a3
JG
433 "packet_size");
434 ok(packet_context_field,
435 "Packet context contains the default packet_size field.");
83509119 436 bt_put(packet_context_field);
3dca2276 437 packet_context_field = bt_ctf_field_structure_get_field_by_name(packet_context,
35e8709f 438 "custom_packet_context_field");
3dca2276 439 ok(bt_ctf_field_integer_unsigned_set_value(packet_context_field, 8) == 0,
12c8a1a3
JG
440 "Custom packet context field value successfully set.");
441
3dca2276 442 ok(bt_ctf_stream_set_packet_context(stream, packet_context) == 0,
12c8a1a3
JG
443 "Successfully set a stream's packet context");
444
3dca2276 445 ok(bt_ctf_stream_flush(stream) == 0,
39d74371
JG
446 "Flush trace stream with one event");
447
83509119
JG
448 bt_put(simple_event_class);
449 bt_put(simple_event);
450 bt_put(uint_12_type);
451 bt_put(int_64_type);
452 bt_put(float_type);
453 bt_put(enum_type);
454 bt_put(enum_type_unsigned);
455 bt_put(returned_type);
456 bt_put(event_context_type);
457 bt_put(integer_field);
458 bt_put(float_field);
459 bt_put(enum_field);
460 bt_put(enum_field_unsigned);
461 bt_put(enum_container_field);
462 bt_put(enum_container_field_unsigned);
463 bt_put(packet_context);
464 bt_put(packet_context_field);
465 bt_put(stream_event_context);
466 bt_put(stream_event_context_field);
467 bt_put(event_context);
468 bt_put(event_context_field);
09840de5
PP
469 bt_put(event_payload_type);
470 bt_put(ep_integer_field_type);
471 bt_put(ep_enum_field_type);
472 bt_put(ep_enum_field_unsigned_type);
e0f15669 473 bt_put(iter);
39d74371
JG
474}
475
a3d8579b 476static
3dca2276
PP
477void append_complex_event(struct bt_ctf_stream_class *stream_class,
478 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
39d74371 479{
3dca2276 480 int i;
3b3b162e 481 struct event_class_attrs_counts ;
1ff9582c 482 const char *complex_test_event_string = "Complex Test Event";
a31f4869 483 const char *test_string_1 = "Test ";
d8f190b2
PP
484 const char *test_string_2 = "string ";
485 const char *test_string_3 = "abcdefghi";
486 const char *test_string_4 = "abcd\0efg\0hi";
f6ccaed9 487 const char *test_string_cat = "Test string abcdeefg";
3dca2276
PP
488 struct bt_ctf_field_type *uint_35_type =
489 bt_ctf_field_type_integer_create(35);
490 struct bt_ctf_field_type *int_16_type =
491 bt_ctf_field_type_integer_create(16);
492 struct bt_ctf_field_type *uint_3_type =
493 bt_ctf_field_type_integer_create(3);
494 struct bt_ctf_field_type *enum_variant_type =
495 bt_ctf_field_type_enumeration_create(uint_3_type);
496 struct bt_ctf_field_type *variant_type =
497 bt_ctf_field_type_variant_create(enum_variant_type,
39d74371 498 "variant_selector");
3dca2276
PP
499 struct bt_ctf_field_type *string_type =
500 bt_ctf_field_type_string_create();
501 struct bt_ctf_field_type *sequence_type;
502 struct bt_ctf_field_type *array_type;
503 struct bt_ctf_field_type *inner_structure_type =
504 bt_ctf_field_type_structure_create();
505 struct bt_ctf_field_type *complex_structure_type =
506 bt_ctf_field_type_structure_create();
507 struct bt_ctf_field_type *ret_field_type;
508 struct bt_ctf_event_class *event_class;
509 struct bt_ctf_event *event;
510 struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field,
39d74371
JG
511 *inner_structure_field, *complex_structure_field,
512 *a_sequence_field, *enum_variant_field, *enum_container_field,
5fd2e9fd
PP
513 *variant_field, *an_array_field, *stream_event_ctx_field,
514 *stream_event_ctx_int_field, *ret_field;
10817e06 515 uint64_t ret_unsigned_int;
1fac895e 516 int64_t ret_signed_int;
10817e06 517 const char *ret_string;
3dca2276
PP
518 struct bt_ctf_stream_class *ret_stream_class;
519 struct bt_ctf_event_class *ret_event_class;
520 struct bt_ctf_field *packet_context, *packet_context_field;
521
522 ok(bt_ctf_field_type_set_alignment(int_16_type, 0),
523 "bt_ctf_field_type_set_alignment handles 0-alignment correctly");
524 ok(bt_ctf_field_type_set_alignment(int_16_type, 3),
525 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (3)");
526 ok(bt_ctf_field_type_set_alignment(int_16_type, 24),
527 "bt_ctf_field_type_set_alignment handles wrong alignment correctly (24)");
528 ok(!bt_ctf_field_type_set_alignment(int_16_type, 4),
529 "bt_ctf_field_type_set_alignment handles correct alignment correctly (4)");
530 ok(!bt_ctf_field_type_set_alignment(int_16_type, 32),
6f010556
JG
531 "Set alignment of signed 16 bit integer to 32");
532 ok(!bt_ctf_field_type_integer_set_signed(int_16_type, 1),
533 "Set integer signedness to true");
3dca2276
PP
534 ok(!bt_ctf_field_type_integer_set_base(uint_35_type,
535 BT_CTF_INTEGER_BASE_HEXADECIMAL),
6f010556 536 "Set signed 16 bit integer base to hexadecimal");
39d74371 537
3dca2276
PP
538 array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH);
539 sequence_type = bt_ctf_field_type_sequence_create(int_16_type,
39d74371 540 "seq_len");
7cfd41d6 541
3dca2276 542 ret_field_type = bt_ctf_field_type_array_get_element_field_type(
7cfd41d6
JG
543 array_type);
544 ok(ret_field_type == int_16_type,
3dca2276 545 "bt_ctf_field_type_array_get_element_field_type returns the correct type");
83509119 546 bt_put(ret_field_type);
7cfd41d6 547
3dca2276
PP
548 ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH,
549 "bt_ctf_field_type_array_get_length returns the correct length");
7cfd41d6 550
3dca2276 551 ok(bt_ctf_field_type_structure_add_field(inner_structure_type,
73892edc 552 inner_structure_type, "yes"), "Cannot add self to structure");
3dca2276 553 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type,
2c53bf6d 554 uint_35_type, "seq_len"), "Add seq_len field to inner structure");
3dca2276 555 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type,
2c53bf6d 556 sequence_type, "a_sequence"), "Add a_sequence field to inner structure");
3dca2276 557 ok(!bt_ctf_field_type_structure_add_field(inner_structure_type,
2c53bf6d 558 array_type, "an_array"), "Add an_array field to inner structure");
39d74371
JG
559
560 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
561 "UINT3_TYPE", 0, 0);
562 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
563 "INT16_TYPE", 1, 1);
564 bt_ctf_field_type_enumeration_add_mapping(enum_variant_type,
565 "UINT35_TYPE", 2, 7);
7cfd41d6 566
3dca2276 567 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
39d74371 568 "An unknown entry"), "Reject a variant field based on an unknown tag value");
3dca2276 569 ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type,
39d74371 570 "UINT3_TYPE") == 0, "Add a field to a variant");
3dca2276 571 ok(!bt_ctf_field_type_variant_add_field(variant_type, int_16_type,
822b92d5 572 "INT16_TYPE"), "Add INT16_TYPE field to variant");
3dca2276 573 ok(!bt_ctf_field_type_variant_add_field(variant_type, uint_35_type,
822b92d5 574 "UINT35_TYPE"), "Add UINT35_TYPE field to variant");
39d74371 575
3dca2276 576 ret_field_type = bt_ctf_field_type_variant_get_tag_field_type(variant_type);
7cfd41d6 577 ok(ret_field_type == enum_variant_type,
3dca2276 578 "bt_ctf_field_type_variant_get_tag_field_type returns a correct tag type");
83509119 579 bt_put(ret_field_type);
7cfd41d6 580
3dca2276 581 ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type);
0fae838f 582 ok(ret_string ? !strcmp(ret_string, "variant_selector") : 0,
3dca2276
PP
583 "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name");
584 ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name(
7cfd41d6
JG
585 variant_type, "INT16_TYPE");
586 ok(ret_field_type == int_16_type,
3dca2276 587 "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type");
83509119 588 bt_put(ret_field_type);
7cfd41d6 589
3dca2276
PP
590 ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3,
591 "bt_ctf_field_type_variant_get_field_count returns the correct count");
7cfd41d6 592
3dca2276
PP
593 ok(bt_ctf_field_type_variant_get_field_by_index(variant_type, NULL, &ret_field_type, 0) == 0,
594 "bt_ctf_field_type_variant_get_field handles a NULL field name correctly");
83509119 595 bt_put(ret_field_type);
3dca2276
PP
596 ok(bt_ctf_field_type_variant_get_field_by_index(variant_type, &ret_string, NULL, 0) == 0,
597 "bt_ctf_field_type_variant_get_field handles a NULL field type correctly");
598 ok(bt_ctf_field_type_variant_get_field_by_index(variant_type, &ret_string, &ret_field_type, 1) == 0,
599 "bt_ctf_field_type_variant_get_field returns a field");
7cfd41d6 600 ok(!strcmp("INT16_TYPE", ret_string),
3dca2276 601 "bt_ctf_field_type_variant_get_field returns a correct field name");
7cfd41d6 602 ok(ret_field_type == int_16_type,
3dca2276 603 "bt_ctf_field_type_variant_get_field returns a correct field type");
83509119 604 bt_put(ret_field_type);
7cfd41d6 605
3dca2276 606 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type,
2c53bf6d
JG
607 enum_variant_type, "variant_selector"),
608 "Add variant_selector field to complex structure");
3dca2276 609 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type,
beb0f041 610 string_type, "string"), "Add `string` field to complex structure");
3dca2276 611 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type,
2c53bf6d
JG
612 variant_type, "variant_value"),
613 "Add variant_value field to complex structure");
3dca2276 614 ok(!bt_ctf_field_type_structure_add_field(complex_structure_type,
2c53bf6d
JG
615 inner_structure_type, "inner_structure"),
616 "Add inner_structure field to complex structure");
39d74371 617
3dca2276 618 event_class = bt_ctf_event_class_create(complex_test_event_string);
39d74371 619 ok(event_class, "Create an event class");
3dca2276 620 ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""),
39d74371 621 "Reject addition of a field with an empty name to an event");
3dca2276 622 ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"),
39d74371 623 "Reject addition of a field with a NULL type to an event");
3dca2276 624 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
39d74371
JG
625 "int"),
626 "Reject addition of a type with an illegal name to an event");
3dca2276 627 ok(bt_ctf_event_class_add_field(event_class, uint_35_type,
39d74371
JG
628 "uint_35") == 0,
629 "Add field of type unsigned integer to an event");
3dca2276 630 ok(bt_ctf_event_class_add_field(event_class, int_16_type,
39d74371 631 "int_16") == 0, "Add field of type signed integer to an event");
3dca2276 632 ok(bt_ctf_event_class_add_field(event_class, complex_structure_type,
39d74371
JG
633 "complex_structure") == 0,
634 "Add composite structure to an event");
635
3dca2276 636 ret_string = bt_ctf_event_class_get_name(event_class);
1ff9582c 637 ok(!strcmp(ret_string, complex_test_event_string),
3dca2276
PP
638 "bt_ctf_event_class_get_name returns a correct name");
639 ok(bt_ctf_event_class_get_id(event_class) < 0,
640 "bt_ctf_event_class_get_id returns a negative value when not set");
641 ok(bt_ctf_event_class_set_id(NULL, 42) < 0,
642 "bt_ctf_event_class_set_id handles NULL correctly");
643 ok(bt_ctf_event_class_set_id(event_class, 42) == 0,
1ff9582c 644 "Set an event class' id");
3dca2276
PP
645 ok(bt_ctf_event_class_get_id(event_class) == 42,
646 "bt_ctf_event_class_get_id returns the correct value");
1ff9582c 647
3b3b162e 648 /* Test event class attributes */
3dca2276 649 ok(bt_ctf_event_class_get_log_level(event_class) == BT_CTF_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED,
cf76ce92 650 "event class has the expected initial log level");
3dca2276 651 ok(!bt_ctf_event_class_get_emf_uri(event_class),
cf76ce92 652 "as expected, event class has no initial EMF URI");
3dca2276
PP
653 ok(bt_ctf_event_class_set_log_level(NULL, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO),
654 "bt_ctf_event_class_set_log_level handles a NULL event class correctly");
655 ok(bt_ctf_event_class_set_log_level(event_class, BT_CTF_EVENT_CLASS_LOG_LEVEL_UNKNOWN),
656 "bt_ctf_event_class_set_log_level handles an unknown log level correctly");
657 ok(!bt_ctf_event_class_set_log_level(event_class, BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO),
658 "bt_ctf_event_class_set_log_level succeeds with a valid log level");
659 ok(bt_ctf_event_class_get_log_level(event_class) == BT_CTF_EVENT_CLASS_LOG_LEVEL_INFO,
660 "bt_ctf_event_class_get_log_level returns the expected log level");
661 ok(bt_ctf_event_class_set_emf_uri(NULL, "http://diamon.org/babeltrace/"),
662 "bt_ctf_event_class_set_emf_uri handles a NULL event class correctly");
663 ok(!bt_ctf_event_class_set_emf_uri(event_class, "http://diamon.org/babeltrace/"),
664 "bt_ctf_event_class_set_emf_uri succeeds with a valid EMF URI");
665 ok(strcmp(bt_ctf_event_class_get_emf_uri(event_class), "http://diamon.org/babeltrace/") == 0,
666 "bt_ctf_event_class_get_emf_uri returns the expected EMF URI");
667 ok(!bt_ctf_event_class_set_emf_uri(event_class, NULL),
668 "bt_ctf_event_class_set_emf_uri succeeds with NULL (to reset)");
669 ok(!bt_ctf_event_class_get_emf_uri(event_class),
cf76ce92 670 "as expected, event class has no EMF URI after reset");
3b3b162e 671
39d74371 672 /* Add event class to the stream class */
3dca2276 673 ok(bt_ctf_stream_class_add_event_class(stream_class, NULL),
39d74371 674 "Reject addition of NULL event class to a stream class");
3dca2276 675 ok(bt_ctf_stream_class_add_event_class(stream_class,
39d74371
JG
676 event_class) == 0, "Add an event class to stream class");
677
3dca2276 678 ret_stream_class = bt_ctf_event_class_get_stream_class(event_class);
1ff9582c 679 ok(ret_stream_class == stream_class,
3dca2276 680 "bt_ctf_event_class_get_stream_class returns the correct stream class");
83509119 681 bt_put(ret_stream_class);
1ff9582c 682
1ff9582c 683 ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL,
3dca2276 684 "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly");
1ff9582c
JG
685 ret_field_type = bt_ctf_event_class_get_field_by_name(event_class,
686 "complex_structure");
83509119 687 bt_put(ret_field_type);
1ff9582c 688
3dca2276 689 event = bt_ctf_event_create(event_class);
39d74371
JG
690 ok(event, "Instanciate a complex event");
691
3dca2276 692 ret_event_class = bt_ctf_event_get_class(event);
1ff9582c 693 ok(ret_event_class == event_class,
3dca2276 694 "bt_ctf_event_get_class returns the correct event class");
83509119 695 bt_put(ret_event_class);
1ff9582c 696
3dca2276
PP
697 uint_35_field = bt_ctf_event_get_payload(event, "uint_35");
698 ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance ");
699 bt_ctf_field_integer_unsigned_set_value(uint_35_field, 0x0DDF00D);
700 ok(bt_ctf_field_integer_unsigned_get_value(uint_35_field,
10817e06 701 &ret_unsigned_int) == 0,
3dca2276 702 "bt_ctf_field_integer_unsigned_get_value succeeds after setting a value");
10817e06 703 ok(ret_unsigned_int == 0x0DDF00D,
3dca2276 704 "bt_ctf_field_integer_unsigned_get_value returns the correct value");
83509119 705 bt_put(uint_35_field);
39d74371 706
3dca2276
PP
707 int_16_field = bt_ctf_event_get_payload(event, "int_16");
708 bt_ctf_field_integer_signed_set_value(int_16_field, -12345);
709 ok(bt_ctf_field_integer_signed_get_value(int_16_field,
10817e06 710 &ret_signed_int) == 0,
3dca2276 711 "bt_ctf_field_integer_signed_get_value succeeds after setting a value");
10817e06 712 ok(ret_signed_int == -12345,
3dca2276 713 "bt_ctf_field_integer_signed_get_value returns the correct value");
83509119 714 bt_put(int_16_field);
39d74371 715
3dca2276 716 complex_structure_field = bt_ctf_event_get_payload(event,
39d74371 717 "complex_structure");
10817e06 718
3dca2276 719 inner_structure_field = bt_ctf_field_structure_get_field_by_index(
10817e06 720 complex_structure_field, 3);
3dca2276 721 ret_field_type = bt_ctf_field_get_type(inner_structure_field);
83509119 722 bt_put(inner_structure_field);
83509119 723 bt_put(ret_field_type);
10817e06 724
3dca2276 725 inner_structure_field = bt_ctf_field_structure_get_field_by_name(
39d74371 726 complex_structure_field, "inner_structure");
3dca2276 727 a_string_field = bt_ctf_field_structure_get_field_by_name(
beb0f041 728 complex_structure_field, "string");
3dca2276 729 enum_variant_field = bt_ctf_field_structure_get_field_by_name(
39d74371 730 complex_structure_field, "variant_selector");
3dca2276 731 variant_field = bt_ctf_field_structure_get_field_by_name(
39d74371 732 complex_structure_field, "variant_value");
3dca2276 733 uint_35_field = bt_ctf_field_structure_get_field_by_name(
39d74371 734 inner_structure_field, "seq_len");
3dca2276 735 a_sequence_field = bt_ctf_field_structure_get_field_by_name(
39d74371 736 inner_structure_field, "a_sequence");
3dca2276 737 an_array_field = bt_ctf_field_structure_get_field_by_name(
1fac895e 738 inner_structure_field, "an_array");
39d74371 739
3dca2276 740 enum_container_field = bt_ctf_field_enumeration_get_container(
39d74371 741 enum_variant_field);
3dca2276
PP
742 bt_ctf_field_integer_unsigned_set_value(enum_container_field, 1);
743 int_16_field = bt_ctf_field_variant_get_field(variant_field,
39d74371 744 enum_variant_field);
3dca2276 745 bt_ctf_field_integer_signed_set_value(int_16_field, -200);
83509119 746 bt_put(int_16_field);
3dca2276 747 bt_ctf_field_string_set_value(a_string_field,
a31f4869 748 test_string_1);
3dca2276
PP
749 ok(!bt_ctf_field_string_append(a_string_field, test_string_2),
750 "bt_ctf_field_string_append succeeds");
751 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 5),
752 "bt_ctf_field_string_append_len succeeds (append 5 characters)");
753 ok(!bt_ctf_field_string_append_len(a_string_field, &test_string_4[5], 3),
754 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
755 ok(!bt_ctf_field_string_append_len(a_string_field, test_string_3, 0),
756 "bt_ctf_field_string_append_len succeeds (append 0 characters)");
757
758 ret_string = bt_ctf_field_string_get_value(a_string_field);
759 ok(ret_string, "bt_ctf_field_string_get_value returns a string");
a31f4869 760 ok(ret_string ? !strcmp(ret_string, test_string_cat) : 0,
3dca2276
PP
761 "bt_ctf_field_string_get_value returns a correct value");
762 bt_ctf_field_integer_unsigned_set_value(uint_35_field,
39d74371 763 SEQUENCE_TEST_LENGTH);
10817e06 764
3dca2276 765 ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag(
7cfd41d6
JG
766 variant_type, enum_variant_field);
767 ok(ret_field_type == int_16_type,
3dca2276 768 "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type");
7cfd41d6 769
3dca2276 770 ok(bt_ctf_field_sequence_set_length(a_sequence_field,
39d74371 771 uint_35_field) == 0, "Set a sequence field's length");
3dca2276 772 ret_field = bt_ctf_field_sequence_get_length(a_sequence_field);
cd95e351 773 ok(ret_field == uint_35_field,
3dca2276 774 "bt_ctf_field_sequence_get_length returns the correct length field");
39d74371
JG
775
776 for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) {
3dca2276 777 int_16_field = bt_ctf_field_sequence_get_field(
39d74371 778 a_sequence_field, i);
3dca2276 779 bt_ctf_field_integer_signed_set_value(int_16_field, 4 - i);
83509119 780 bt_put(int_16_field);
39d74371
JG
781 }
782
1fac895e 783 for (i = 0; i < ARRAY_TEST_LENGTH; i++) {
3dca2276 784 int_16_field = bt_ctf_field_array_get_field(
1fac895e 785 an_array_field, i);
3dca2276 786 bt_ctf_field_integer_signed_set_value(int_16_field, i);
83509119 787 bt_put(int_16_field);
1fac895e
JG
788 }
789
3dca2276 790 stream_event_ctx_field = bt_ctf_event_get_stream_event_context(event);
5fd2e9fd 791 assert(stream_event_ctx_field);
3dca2276 792 stream_event_ctx_int_field = bt_ctf_field_structure_get_field_by_name(
5fd2e9fd
PP
793 stream_event_ctx_field, "common_event_context");
794 BT_PUT(stream_event_ctx_field);
3dca2276 795 bt_ctf_field_integer_unsigned_set_value(stream_event_ctx_int_field, 17);
5fd2e9fd
PP
796 BT_PUT(stream_event_ctx_int_field);
797
39d74371 798 bt_ctf_clock_set_time(clock, ++current_time);
3dca2276 799 ok(bt_ctf_stream_append_event(stream, event) == 0,
39d74371 800 "Append a complex event to a stream");
12c8a1a3
JG
801
802 /*
803 * Populate the custom packet context field with a dummy value
804 * otherwise flush will fail.
805 */
3dca2276
PP
806 packet_context = bt_ctf_stream_get_packet_context(stream);
807 packet_context_field = bt_ctf_field_structure_get_field_by_name(packet_context,
35e8709f 808 "custom_packet_context_field");
3dca2276 809 bt_ctf_field_integer_unsigned_set_value(packet_context_field, 1);
12c8a1a3 810
3dca2276 811 ok(bt_ctf_stream_flush(stream) == 0,
39d74371
JG
812 "Flush a stream containing a complex event");
813
83509119
JG
814 bt_put(uint_35_field);
815 bt_put(a_string_field);
816 bt_put(inner_structure_field);
817 bt_put(complex_structure_field);
818 bt_put(a_sequence_field);
819 bt_put(an_array_field);
820 bt_put(enum_variant_field);
821 bt_put(enum_container_field);
822 bt_put(variant_field);
823 bt_put(ret_field);
824 bt_put(packet_context_field);
825 bt_put(packet_context);
826 bt_put(uint_35_type);
827 bt_put(int_16_type);
828 bt_put(string_type);
829 bt_put(sequence_type);
830 bt_put(array_type);
831 bt_put(inner_structure_type);
832 bt_put(complex_structure_type);
833 bt_put(uint_3_type);
834 bt_put(enum_variant_type);
835 bt_put(variant_type);
836 bt_put(ret_field_type);
837 bt_put(event_class);
838 bt_put(event);
39d74371
JG
839}
840
a3d8579b 841static
39d74371
JG
842void type_field_tests()
843{
3dca2276
PP
844 struct bt_ctf_field *uint_12;
845 struct bt_ctf_field *int_16;
846 struct bt_ctf_field *string;
847 struct bt_ctf_field_type *composite_structure_type;
848 struct bt_ctf_field_type *structure_seq_type;
849 struct bt_ctf_field_type *string_type;
850 struct bt_ctf_field_type *sequence_type;
851 struct bt_ctf_field_type *uint_8_type;
852 struct bt_ctf_field_type *int_16_type;
853 struct bt_ctf_field_type *uint_12_type =
854 bt_ctf_field_type_integer_create(12);
855 struct bt_ctf_field_type *enumeration_type;
856 struct bt_ctf_field_type *returned_type;
7cfd41d6 857 const char *ret_string;
10817e06 858
39d74371 859 ok(uint_12_type, "Create an unsigned integer type");
3dca2276
PP
860 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
861 BT_CTF_INTEGER_BASE_BINARY) == 0,
39d74371 862 "Set integer type's base as binary");
3dca2276
PP
863 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
864 BT_CTF_INTEGER_BASE_DECIMAL) == 0,
39d74371 865 "Set integer type's base as decimal");
3dca2276
PP
866 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
867 BT_CTF_INTEGER_BASE_UNKNOWN),
39d74371 868 "Reject integer type's base set as unknown");
3dca2276
PP
869 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
870 BT_CTF_INTEGER_BASE_OCTAL) == 0,
39d74371 871 "Set integer type's base as octal");
3dca2276
PP
872 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
873 BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0,
39d74371 874 "Set integer type's base as hexadecimal");
3dca2276 875 ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417),
39d74371
JG
876 "Reject unknown integer base value");
877 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0,
878 "Set integer type signedness to signed");
879 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0,
880 "Set integer type signedness to unsigned");
3dca2276
PP
881 ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12,
882 "bt_ctf_field_type_integer_get_size returns a correct value");
7cfd41d6 883 ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0,
3dca2276
PP
884 "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types");
885
886 ok(bt_ctf_field_type_set_byte_order(NULL,
887 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0,
888 "bt_ctf_field_type_set_byte_order handles NULL correctly");
889 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
890 (enum bt_ctf_byte_order) 42) < 0,
891 "bt_ctf_field_type_set_byte_order rejects invalid values");
892 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
893 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0,
7cfd41d6 894 "Set an integer's byte order to little endian");
3dca2276
PP
895 ok(bt_ctf_field_type_set_byte_order(uint_12_type,
896 BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
7cfd41d6 897 "Set an integer's byte order to big endian");
3dca2276
PP
898 ok(bt_ctf_field_type_get_byte_order(uint_12_type) ==
899 BT_CTF_BYTE_ORDER_BIG_ENDIAN,
900 "bt_ctf_field_type_get_byte_order returns a correct value");
901
902 ok(bt_ctf_field_type_get_type_id(uint_12_type) ==
903 BT_CTF_FIELD_TYPE_ID_INTEGER,
904 "bt_ctf_field_type_get_type_id returns a correct value with an integer type");
905
906 ok(bt_ctf_field_type_integer_get_base(uint_12_type) ==
907 BT_CTF_INTEGER_BASE_HEXADECIMAL,
908 "bt_ctf_field_type_integer_get_base returns a correct value");
909
910 ok(bt_ctf_field_type_integer_set_encoding(NULL,
911 BT_CTF_STRING_ENCODING_ASCII) < 0,
912 "bt_ctf_field_type_integer_set_encoding handles NULL correctly");
913 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
914 (enum bt_ctf_string_encoding) 123) < 0,
915 "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly");
916 ok(bt_ctf_field_type_integer_set_encoding(uint_12_type,
917 BT_CTF_STRING_ENCODING_UTF8) == 0,
7cfd41d6 918 "Set integer type encoding to UTF8");
3dca2276
PP
919 ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) ==
920 BT_CTF_STRING_ENCODING_UTF8,
921 "bt_ctf_field_type_integer_get_encoding returns a correct value");
50842bdc 922
3dca2276 923 int_16_type = bt_ctf_field_type_integer_create(16);
e13d28e1 924 assert(int_16_type);
687ae062
JG
925 ok(!bt_ctf_field_type_integer_set_signed(int_16_type, 1),
926 "Set signedness of 16 bit integer to true");
7cfd41d6 927 ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1,
3dca2276
PP
928 "bt_ctf_field_type_integer_get_signed returns a correct value for signed types");
929 uint_8_type = bt_ctf_field_type_integer_create(8);
39d74371 930 sequence_type =
3dca2276 931 bt_ctf_field_type_sequence_create(int_16_type, "seq_len");
39d74371 932 ok(sequence_type, "Create a sequence of int16_t type");
3dca2276
PP
933 ok(bt_ctf_field_type_get_type_id(sequence_type) ==
934 BT_CTF_FIELD_TYPE_ID_SEQUENCE,
935 "bt_ctf_field_type_get_type_id returns a correct value with a sequence type");
7cfd41d6 936
3dca2276 937 ret_string = bt_ctf_field_type_sequence_get_length_field_name(
7cfd41d6
JG
938 sequence_type);
939 ok(!strcmp(ret_string, "seq_len"),
3dca2276
PP
940 "bt_ctf_field_type_sequence_get_length_field_name returns the correct value");
941 returned_type = bt_ctf_field_type_sequence_get_element_field_type(
7cfd41d6
JG
942 sequence_type);
943 ok(returned_type == int_16_type,
3dca2276 944 "bt_ctf_field_type_sequence_get_element_field_type returns the correct type");
83509119 945 bt_put(returned_type);
39d74371 946
3dca2276 947 string_type = bt_ctf_field_type_string_create();
39d74371 948 ok(string_type, "Create a string type");
3dca2276
PP
949 ok(bt_ctf_field_type_string_set_encoding(string_type,
950 BT_CTF_STRING_ENCODING_NONE),
39d74371 951 "Reject invalid \"None\" string encoding");
3dca2276 952 ok(bt_ctf_field_type_string_set_encoding(string_type,
39d74371
JG
953 42),
954 "Reject invalid string encoding");
3dca2276
PP
955 ok(bt_ctf_field_type_string_set_encoding(string_type,
956 BT_CTF_STRING_ENCODING_ASCII) == 0,
39d74371
JG
957 "Set string encoding to ASCII");
958
3dca2276
PP
959 ok(bt_ctf_field_type_string_get_encoding(string_type) ==
960 BT_CTF_STRING_ENCODING_ASCII,
961 "bt_ctf_field_type_string_get_encoding returns the correct value");
50842bdc 962
3dca2276
PP
963 structure_seq_type = bt_ctf_field_type_structure_create();
964 ok(bt_ctf_field_type_get_type_id(structure_seq_type) ==
965 BT_CTF_FIELD_TYPE_ID_STRUCT,
966 "bt_ctf_field_type_get_type_id returns a correct value with a structure type");
39d74371 967 ok(structure_seq_type, "Create a structure type");
3dca2276 968 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
39d74371
JG
969 uint_8_type, "seq_len") == 0,
970 "Add a uint8_t type to a structure");
3dca2276 971 ok(bt_ctf_field_type_structure_add_field(structure_seq_type,
39d74371
JG
972 sequence_type, "a_sequence") == 0,
973 "Add a sequence type to a structure");
7cfd41d6 974
3dca2276
PP
975 ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2,
976 "bt_ctf_field_type_structure_get_field_count returns a correct value");
7cfd41d6 977
7cfd41d6 978 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
f9b799fc 979 NULL, &returned_type, 1) == 0,
3dca2276 980 "bt_ctf_field_type_structure_get_field handles a NULL name correctly");
83509119 981 bt_put(returned_type);
7cfd41d6 982 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
f9b799fc 983 &ret_string, NULL, 1) == 0,
3dca2276 984 "bt_ctf_field_type_structure_get_field handles a NULL return type correctly");
7cfd41d6
JG
985 ok(bt_ctf_field_type_structure_get_field(structure_seq_type,
986 &ret_string, &returned_type, 1) == 0,
3dca2276 987 "bt_ctf_field_type_structure_get_field returns a field");
7cfd41d6 988 ok(!strcmp(ret_string, "a_sequence"),
3dca2276 989 "bt_ctf_field_type_structure_get_field returns a correct field name");
7cfd41d6 990 ok(returned_type == sequence_type,
3dca2276 991 "bt_ctf_field_type_structure_get_field returns a correct field type");
83509119 992 bt_put(returned_type);
7cfd41d6 993
3dca2276 994 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
7cfd41d6
JG
995 structure_seq_type, "a_sequence");
996 ok(returned_type == sequence_type,
3dca2276 997 "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type");
83509119 998 bt_put(returned_type);
7cfd41d6 999
3dca2276
PP
1000 composite_structure_type = bt_ctf_field_type_structure_create();
1001 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
39d74371
JG
1002 string_type, "a_string") == 0,
1003 "Add a string type to a structure");
3dca2276 1004 ok(bt_ctf_field_type_structure_add_field(composite_structure_type,
39d74371
JG
1005 structure_seq_type, "inner_structure") == 0,
1006 "Add a structure type to a structure");
1007
3dca2276 1008 returned_type = bt_ctf_field_type_structure_get_field_type_by_name(
7cfd41d6
JG
1009 structure_seq_type, "a_sequence");
1010 ok(returned_type == sequence_type,
3dca2276 1011 "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type");
83509119 1012 bt_put(returned_type);
7cfd41d6 1013
3dca2276 1014 int_16 = bt_ctf_field_create(int_16_type);
39d74371 1015 ok(int_16, "Instanciate a signed 16-bit integer");
3dca2276 1016 uint_12 = bt_ctf_field_create(uint_12_type);
39d74371 1017 ok(uint_12, "Instanciate an unsigned 12-bit integer");
3dca2276 1018 returned_type = bt_ctf_field_get_type(int_16);
10817e06 1019 ok(returned_type == int_16_type,
3dca2276 1020 "bt_ctf_field_get_type returns the correct type");
39d74371
JG
1021
1022 /* Can't modify types after instanciating them */
3dca2276
PP
1023 ok(bt_ctf_field_type_integer_set_base(uint_12_type,
1024 BT_CTF_INTEGER_BASE_DECIMAL),
39d74371
JG
1025 "Check an integer type' base can't be modified after instanciation");
1026 ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0),
1027 "Check an integer type's signedness can't be modified after instanciation");
1028
39d74371 1029 /* Check overflows are properly tested for */
3dca2276 1030 ok(bt_ctf_field_integer_signed_set_value(int_16, -32768) == 0,
39d74371 1031 "Check -32768 is allowed for a signed 16-bit integer");
3dca2276 1032 ok(bt_ctf_field_integer_signed_set_value(int_16, 32767) == 0,
39d74371 1033 "Check 32767 is allowed for a signed 16-bit integer");
3dca2276 1034 ok(bt_ctf_field_integer_signed_set_value(int_16, -42) == 0,
39d74371
JG
1035 "Check -42 is allowed for a signed 16-bit integer");
1036
3dca2276 1037 ok(bt_ctf_field_integer_unsigned_set_value(uint_12, 4095) == 0,
39d74371 1038 "Check 4095 is allowed for an unsigned 12-bit integer");
3dca2276 1039 ok(bt_ctf_field_integer_unsigned_set_value(uint_12, 0) == 0,
39d74371
JG
1040 "Check 0 is allowed for an unsigned 12-bit integer");
1041
3dca2276 1042 string = bt_ctf_field_create(string_type);
39d74371 1043 ok(string, "Instanciate a string field");
3dca2276 1044 ok(bt_ctf_field_string_set_value(string, "A value") == 0,
39d74371
JG
1045 "Set a string's value");
1046
3dca2276 1047 enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type);
0abce37e
JG
1048 ok(enumeration_type,
1049 "Create an enumeration type with an unsigned 12-bit integer as container");
0abce37e 1050
83509119
JG
1051 bt_put(string);
1052 bt_put(uint_12);
1053 bt_put(int_16);
83509119
JG
1054 bt_put(composite_structure_type);
1055 bt_put(structure_seq_type);
1056 bt_put(string_type);
1057 bt_put(sequence_type);
1058 bt_put(uint_8_type);
1059 bt_put(int_16_type);
1060 bt_put(uint_12_type);
1061 bt_put(enumeration_type);
83509119 1062 bt_put(returned_type);
39d74371
JG
1063}
1064
a3d8579b 1065static
3dca2276
PP
1066void packet_resize_test(struct bt_ctf_stream_class *stream_class,
1067 struct bt_ctf_stream *stream, struct bt_ctf_clock *clock)
39d74371
JG
1068{
1069 /*
1070 * Append enough events to force the underlying packet to be resized.
1071 * Also tests that a new event can be declared after a stream has been
1072 * instantiated and used/flushed.
1073 */
1074 int ret = 0;
1075 int i;
3dca2276 1076 struct bt_ctf_event_class *event_class = bt_ctf_event_class_create(
39d74371 1077 "Spammy_Event");
3dca2276
PP
1078 struct bt_ctf_field_type *integer_type =
1079 bt_ctf_field_type_integer_create(17);
1080 struct bt_ctf_field_type *string_type =
1081 bt_ctf_field_type_string_create();
1082 struct bt_ctf_event *event = NULL;
1083 struct bt_ctf_field *ret_field = NULL;
1084 struct bt_ctf_field_type *ret_field_type = NULL;
6809e227 1085 uint64_t ret_uint64;
12c8a1a3 1086 int events_appended = 0;
3dca2276 1087 struct bt_ctf_field *packet_context = NULL,
5fd2e9fd 1088 *packet_context_field = NULL, *stream_event_context = NULL;
3dca2276
PP
1089 struct bt_ctf_field_type *ep_field_1_type = NULL;
1090 struct bt_ctf_field_type *ep_a_string_type = NULL;
1091 struct bt_ctf_field_type *ep_type = NULL;
39d74371 1092
3dca2276 1093 ret |= bt_ctf_event_class_add_field(event_class, integer_type,
39d74371 1094 "field_1");
3dca2276 1095 ret |= bt_ctf_event_class_add_field(event_class, string_type,
39d74371 1096 "a_string");
3dca2276 1097 ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class);
39d74371
JG
1098 ok(ret == 0, "Add a new event class to a stream class after writing an event");
1099 if (ret) {
1100 goto end;
1101 }
1102
09840de5 1103 /*
3dca2276 1104 * bt_ctf_stream_class_add_event_class() copies the field types
09840de5
PP
1105 * of event_class, so we retrieve the new ones to create the
1106 * appropriate fields.
1107 */
3dca2276 1108 ep_type = bt_ctf_event_class_get_payload_field_type(event_class);
09840de5 1109 assert(ep_type);
3dca2276 1110 ep_field_1_type = bt_ctf_field_type_structure_get_field_type_by_name(
09840de5
PP
1111 ep_type, "field_1");
1112 assert(ep_field_1_type);
3dca2276 1113 ep_a_string_type = bt_ctf_field_type_structure_get_field_type_by_name(
09840de5
PP
1114 ep_type, "a_string");
1115 assert(ep_a_string_type);
1116
3dca2276
PP
1117 event = bt_ctf_event_create(event_class);
1118 ret_field = bt_ctf_event_get_payload(event, 0);
1119 ret_field_type = bt_ctf_field_get_type(ret_field);
83509119
JG
1120 bt_put(ret_field_type);
1121 bt_put(ret_field);
83509119 1122 bt_put(event);
1ff9582c 1123
c2237f17 1124 for (i = 0; i < packet_resize_test_length; i++) {
3dca2276
PP
1125 event = bt_ctf_event_create(event_class);
1126 struct bt_ctf_field *integer =
1127 bt_ctf_field_create(ep_field_1_type);
1128 struct bt_ctf_field *string =
1129 bt_ctf_field_create(ep_a_string_type);
39d74371
JG
1130
1131 ret |= bt_ctf_clock_set_time(clock, ++current_time);
3dca2276
PP
1132 ret |= bt_ctf_field_integer_unsigned_set_value(integer, i);
1133 ret |= bt_ctf_event_set_payload(event, "field_1",
39d74371 1134 integer);
83509119 1135 bt_put(integer);
3dca2276
PP
1136 ret |= bt_ctf_field_string_set_value(string, "This is a test");
1137 ret |= bt_ctf_event_set_payload(event, "a_string",
39d74371 1138 string);
83509119 1139 bt_put(string);
6e1f8ea1
JG
1140
1141 /* Populate stream event context */
5fd2e9fd 1142 stream_event_context =
3dca2276
PP
1143 bt_ctf_event_get_stream_event_context(event);
1144 integer = bt_ctf_field_structure_get_field_by_name(stream_event_context,
6e1f8ea1 1145 "common_event_context");
5fd2e9fd 1146 BT_PUT(stream_event_context);
3dca2276 1147 ret |= bt_ctf_field_integer_unsigned_set_value(integer,
6e1f8ea1 1148 i % 42);
83509119 1149 bt_put(integer);
6e1f8ea1 1150
3dca2276 1151 ret |= bt_ctf_stream_append_event(stream, event);
83509119 1152 bt_put(event);
39d74371
JG
1153
1154 if (ret) {
1155 break;
1156 }
1157 }
12c8a1a3 1158
c2237f17 1159 events_appended = !!(i == packet_resize_test_length);
3dca2276 1160 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
6809e227 1161 ok(ret == 0 && ret_uint64 == 0,
3dca2276
PP
1162 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded");
1163 bt_ctf_stream_append_discarded_events(stream, 1000);
1164 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
6809e227 1165 ok(ret == 0 && ret_uint64 == 1000,
3dca2276 1166 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded");
6809e227 1167
39d74371 1168end:
12c8a1a3
JG
1169 ok(events_appended, "Append 100 000 events to a stream");
1170
1171 /*
1172 * Populate the custom packet context field with a dummy value
1173 * otherwise flush will fail.
1174 */
3dca2276
PP
1175 packet_context = bt_ctf_stream_get_packet_context(stream);
1176 packet_context_field = bt_ctf_field_structure_get_field_by_name(packet_context,
35e8709f 1177 "custom_packet_context_field");
3dca2276 1178 bt_ctf_field_integer_unsigned_set_value(packet_context_field, 2);
12c8a1a3 1179
3dca2276 1180 ok(bt_ctf_stream_flush(stream) == 0,
39d74371 1181 "Flush a stream that forces a packet resize");
3dca2276 1182 ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64);
6809e227 1183 ok(ret == 0 && ret_uint64 == 1000,
3dca2276 1184 "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush");
83509119
JG
1185 bt_put(integer_type);
1186 bt_put(string_type);
1187 bt_put(packet_context);
1188 bt_put(packet_context_field);
5fd2e9fd 1189 bt_put(stream_event_context);
83509119 1190 bt_put(event_class);
09840de5
PP
1191 bt_put(ep_field_1_type);
1192 bt_put(ep_a_string_type);
1193 bt_put(ep_type);
39d74371
JG
1194}
1195
a3d8579b 1196static
fdf80f32
JG
1197void test_empty_stream(struct bt_ctf_writer *writer)
1198{
1199 int ret = 0;
3dca2276
PP
1200 struct bt_ctf_trace *trace = NULL, *ret_trace = NULL;
1201 struct bt_ctf_stream_class *stream_class = NULL;
1202 struct bt_ctf_stream *stream = NULL;
fdf80f32
JG
1203
1204 trace = bt_ctf_writer_get_trace(writer);
1205 if (!trace) {
1206 diag("Failed to get trace from writer");
1207 ret = -1;
1208 goto end;
1209 }
1210
3dca2276 1211 stream_class = bt_ctf_stream_class_create("empty_stream");
fdf80f32
JG
1212 if (!stream_class) {
1213 diag("Failed to create stream class");
1214 ret = -1;
1215 goto end;
1216 }
1217
3dca2276 1218 ret = bt_ctf_stream_class_set_packet_context_type(stream_class, NULL);
e011d2c1 1219 assert(ret == 0);
3dca2276 1220 ret = bt_ctf_stream_class_set_event_header_type(stream_class, NULL);
e011d2c1
PP
1221 assert(ret == 0);
1222
3dca2276
PP
1223 ok(bt_ctf_stream_class_get_trace(stream_class) == NULL,
1224 "bt_ctf_stream_class_get_trace returns NULL when stream class is orphaned");
9b068522 1225
fdf80f32
JG
1226 stream = bt_ctf_writer_create_stream(writer, stream_class);
1227 if (!stream) {
1228 diag("Failed to create writer stream");
1229 ret = -1;
1230 goto end;
1231 }
9b068522 1232
3dca2276 1233 ret_trace = bt_ctf_stream_class_get_trace(stream_class);
9b068522 1234 ok(ret_trace == trace,
3dca2276 1235 "bt_ctf_stream_class_get_trace returns the correct trace after a stream has been created");
fdf80f32
JG
1236end:
1237 ok(ret == 0,
1238 "Created a stream class with default attributes and an empty stream");
83509119
JG
1239 bt_put(trace);
1240 bt_put(ret_trace);
1241 bt_put(stream);
1242 bt_put(stream_class);
fdf80f32
JG
1243}
1244
a3d8579b 1245static
ac0c6bdd
PP
1246void test_custom_event_header_stream(struct bt_ctf_writer *writer,
1247 struct bt_ctf_clock *clock)
29be776a
JG
1248{
1249 int i, ret;
3dca2276
PP
1250 struct bt_ctf_stream_class *stream_class = NULL;
1251 struct bt_ctf_stream *stream = NULL;
1252 struct bt_ctf_field_type *integer_type = NULL,
29be776a 1253 *sequence_type = NULL, *event_header_type = NULL;
3dca2276 1254 struct bt_ctf_field *integer = NULL, *sequence = NULL,
29be776a 1255 *event_header = NULL, *packet_header = NULL;
3dca2276
PP
1256 struct bt_ctf_event_class *event_class = NULL;
1257 struct bt_ctf_event *event = NULL;
29be776a 1258
3dca2276 1259 stream_class = bt_ctf_stream_class_create("custom_event_header_stream");
29be776a
JG
1260 if (!stream_class) {
1261 fail("Failed to create stream class");
1262 goto end;
1263 }
1264
3dca2276 1265 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
29be776a
JG
1266 if (ret) {
1267 fail("Failed to set stream class clock");
1268 goto end;
1269 }
1270
1271 /*
1272 * Customize event header to add an "seq_len" integer member
1273 * which will be used as the length of a sequence in an event of this
1274 * stream.
1275 */
3dca2276 1276 event_header_type = bt_ctf_stream_class_get_event_header_type(
29be776a
JG
1277 stream_class);
1278 if (!event_header_type) {
1279 fail("Failed to get event header type");
1280 goto end;
1281 }
1282
3dca2276 1283 integer_type = bt_ctf_field_type_integer_create(13);
29be776a
JG
1284 if (!integer_type) {
1285 fail("Failed to create length integer type");
1286 goto end;
1287 }
1288
3dca2276 1289 ret = bt_ctf_field_type_structure_add_field(event_header_type,
29be776a
JG
1290 integer_type, "seq_len");
1291 if (ret) {
1292 fail("Failed to add a new field to stream event header");
1293 goto end;
1294 }
1295
3dca2276 1296 event_class = bt_ctf_event_class_create("sequence_event");
29be776a
JG
1297 if (!event_class) {
1298 fail("Failed to create event class");
1299 goto end;
1300 }
1301
1302 /*
1303 * This event's payload will contain a sequence which references
1304 * stream.event.header.seq_len as its length field.
1305 */
3dca2276 1306 sequence_type = bt_ctf_field_type_sequence_create(integer_type,
29be776a
JG
1307 "stream.event.header.seq_len");
1308 if (!sequence_type) {
1309 fail("Failed to create a sequence");
1310 goto end;
1311 }
1312
3dca2276 1313 ret = bt_ctf_event_class_add_field(event_class, sequence_type,
29be776a
JG
1314 "some_sequence");
1315 if (ret) {
1316 fail("Failed to add a sequence to an event class");
1317 goto end;
1318 }
1319
3dca2276 1320 ret = bt_ctf_stream_class_add_event_class(stream_class, event_class);
29be776a
JG
1321 if (ret) {
1322 fail("Failed to add event class to stream class");
1323 goto end;
1324 }
1325
1326 stream = bt_ctf_writer_create_stream(writer, stream_class);
1327 if (!stream) {
1328 fail("Failed to create stream")
1329 goto end;
1330 }
1331
1332 /*
1333 * We have defined a custom packet header field. We have to populate it
1334 * explicitly.
1335 */
3dca2276 1336 packet_header = bt_ctf_stream_get_packet_header(stream);
29be776a
JG
1337 if (!packet_header) {
1338 fail("Failed to get stream packet header");
1339 goto end;
1340 }
1341
3dca2276 1342 integer = bt_ctf_field_structure_get_field_by_name(packet_header,
29be776a
JG
1343 "custom_trace_packet_header_field");
1344 if (!integer) {
1345 fail("Failed to retrieve custom_trace_packet_header_field");
1346 goto end;
1347 }
1348
3dca2276 1349 ret = bt_ctf_field_integer_unsigned_set_value(integer, 3487);
29be776a
JG
1350 if (ret) {
1351 fail("Failed to set custom_trace_packet_header_field value");
1352 goto end;
1353 }
83509119 1354 bt_put(integer);
29be776a 1355
3dca2276 1356 event = bt_ctf_event_create(event_class);
29be776a
JG
1357 if (!event) {
1358 fail("Failed to create event");
1359 goto end;
1360 }
1361
3dca2276 1362 event_header = bt_ctf_event_get_header(event);
29be776a
JG
1363 if (!event_header) {
1364 fail("Failed to get event header");
1365 goto end;
1366 }
1367
3dca2276 1368 integer = bt_ctf_field_structure_get_field_by_name(event_header,
29be776a
JG
1369 "seq_len");
1370 if (!integer) {
1371 fail("Failed to get seq_len field from event header");
1372 goto end;
1373 }
1374
3dca2276 1375 ret = bt_ctf_field_integer_unsigned_set_value(integer, 2);
29be776a
JG
1376 if (ret) {
1377 fail("Failed to set seq_len value in event header");
1378 goto end;
1379 }
1380
1381 /* Populate both sequence integer fields */
3dca2276 1382 sequence = bt_ctf_event_get_payload(event, "some_sequence");
29be776a
JG
1383 if (!sequence) {
1384 fail("Failed to retrieve sequence from event");
1385 goto end;
1386 }
1387
3dca2276 1388 ret = bt_ctf_field_sequence_set_length(sequence, integer);
29be776a
JG
1389 if (ret) {
1390 fail("Failed to set sequence length");
1391 goto end;
1392 }
83509119 1393 bt_put(integer);
29be776a
JG
1394
1395 for (i = 0; i < 2; i++) {
3dca2276 1396 integer = bt_ctf_field_sequence_get_field(sequence, i);
29be776a
JG
1397 if (ret) {
1398 fail("Failed to retrieve sequence element");
1399 goto end;
1400 }
1401
3dca2276 1402 ret = bt_ctf_field_integer_unsigned_set_value(integer, i);
29be776a
JG
1403 if (ret) {
1404 fail("Failed to set sequence element value");
1405 goto end;
1406 }
1407
83509119 1408 bt_put(integer);
29be776a
JG
1409 integer = NULL;
1410 }
1411
3dca2276 1412 ret = bt_ctf_stream_append_event(stream, event);
29be776a
JG
1413 if (ret) {
1414 fail("Failed to append event to stream");
1415 goto end;
1416 }
1417
3dca2276 1418 ret = bt_ctf_stream_flush(stream);
29be776a
JG
1419 if (ret) {
1420 fail("Failed to flush custom_event_header stream");
1421 }
1422end:
83509119
JG
1423 bt_put(stream);
1424 bt_put(stream_class);
1425 bt_put(event_class);
1426 bt_put(event);
1427 bt_put(integer);
1428 bt_put(sequence);
1429 bt_put(event_header);
1430 bt_put(packet_header);
1431 bt_put(sequence_type);
1432 bt_put(integer_type);
1433 bt_put(event_header_type);
29be776a
JG
1434}
1435
a3d8579b 1436static
ac0c6bdd
PP
1437void test_instanciate_event_before_stream(struct bt_ctf_writer *writer,
1438 struct bt_ctf_clock *clock)
42f45a8d
JG
1439{
1440 int ret = 0;
3dca2276
PP
1441 struct bt_ctf_stream_class *stream_class = NULL;
1442 struct bt_ctf_stream *stream = NULL,
2fb29fdc 1443 *ret_stream = NULL;
3dca2276
PP
1444 struct bt_ctf_event_class *event_class = NULL;
1445 struct bt_ctf_event *event = NULL;
1446 struct bt_ctf_field_type *integer_type = NULL;
1447 struct bt_ctf_field *payload_field = NULL;
1448 struct bt_ctf_field *integer = NULL;
42f45a8d 1449
3dca2276 1450 stream_class = bt_ctf_stream_class_create("event_before_stream_test");
42f45a8d
JG
1451 if (!stream_class) {
1452 diag("Failed to create stream class");
1453 ret = -1;
1454 goto end;
1455 }
1456
3dca2276 1457 ret = bt_ctf_stream_class_set_clock(stream_class, clock);
42f45a8d
JG
1458 if (ret) {
1459 diag("Failed to set stream class clock");
1460 goto end;
1461 }
1462
3dca2276
PP
1463 event_class = bt_ctf_event_class_create("some_event_class_name");
1464 integer_type = bt_ctf_field_type_integer_create(32);
42f45a8d
JG
1465 if (!integer_type) {
1466 diag("Failed to create integer field type");
1467 ret = -1;
1468 goto end;
1469 }
1470
3dca2276 1471 ret = bt_ctf_event_class_add_field(event_class, integer_type,
42f45a8d
JG
1472 "integer_field");
1473 if (ret) {
1474 diag("Failed to add field to event class");
1475 goto end;
1476 }
1477
3dca2276 1478 ret = bt_ctf_stream_class_add_event_class(stream_class,
42f45a8d
JG
1479 event_class);
1480 if (ret) {
1481 diag("Failed to add event class to stream class");
1482 }
1483
3dca2276 1484 event = bt_ctf_event_create(event_class);
42f45a8d
JG
1485 if (!event) {
1486 diag("Failed to create event");
1487 ret = -1;
1488 goto end;
1489 }
1490
3dca2276
PP
1491 payload_field = bt_ctf_event_get_payload_field(event);
1492 if (!payload_field) {
1493 diag("Failed to get event's payload field");
1494 ret = -1;
1495 goto end;
1496 }
1497
1498 integer = bt_ctf_field_structure_get_field_by_index(payload_field, 0);
42f45a8d
JG
1499 if (!integer) {
1500 diag("Failed to get integer field payload from event");
1501 ret = -1;
1502 goto end;
1503 }
1504
3dca2276 1505 ret = bt_ctf_field_integer_unsigned_set_value(integer, 1234);
42f45a8d
JG
1506 if (ret) {
1507 diag("Failed to set integer field value");
1508 goto end;
1509 }
1510
1511 stream = bt_ctf_writer_create_stream(writer, stream_class);
1512 if (!stream) {
1513 diag("Failed to create writer stream");
1514 ret = -1;
1515 goto end;
1516 }
1517
3dca2276 1518 ret = bt_ctf_stream_append_event(stream, event);
42f45a8d
JG
1519 if (ret) {
1520 diag("Failed to append event to stream");
1521 goto end;
1522 }
2fb29fdc 1523
3dca2276 1524 ret_stream = bt_ctf_event_get_stream(event);
2fb29fdc 1525 ok(ret_stream == stream,
3dca2276 1526 "bt_ctf_event_get_stream returns an event's stream after it has been appended");
42f45a8d
JG
1527end:
1528 ok(ret == 0,
1529 "Create an event before instanciating its associated stream");
83509119
JG
1530 bt_put(stream);
1531 bt_put(ret_stream);
1532 bt_put(stream_class);
1533 bt_put(event_class);
1534 bt_put(event);
1535 bt_put(integer_type);
1536 bt_put(integer);
3dca2276 1537 bt_put(payload_field);
42f45a8d
JG
1538}
1539
a3d8579b 1540static
3dca2276 1541void append_existing_event_class(struct bt_ctf_stream_class *stream_class)
f60fde63 1542{
3dca2276 1543 struct bt_ctf_event_class *event_class;
f60fde63 1544
3dca2276 1545 event_class = bt_ctf_event_class_create("Simple Event");
82faa54a 1546 assert(event_class);
3dca2276 1547 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class) == 0,
a9f0d01b 1548 "two event classes with the same name may cohabit within the same stream class");
83509119 1549 bt_put(event_class);
f60fde63 1550
3dca2276 1551 event_class = bt_ctf_event_class_create("different name, ok");
06a0c632 1552 assert(event_class);
3dca2276
PP
1553 assert(!bt_ctf_event_class_set_id(event_class, 13));
1554 ok(bt_ctf_stream_class_add_event_class(stream_class, event_class),
f60fde63 1555 "two event classes with the same ID cannot cohabit within the same stream class");
83509119 1556 bt_put(event_class);
f60fde63
PP
1557}
1558
a3d8579b 1559static
44e0165b
PP
1560void test_clock_utils(void)
1561{
1562 int ret;
1563 struct bt_ctf_clock *clock = NULL;
1564
1565 clock = bt_ctf_clock_create("water");
1566 assert(clock);
1567 ret = bt_ctf_clock_set_offset_s(clock, 1234);
72995028 1568 assert(!ret);
44e0165b
PP
1569 ret = bt_ctf_clock_set_offset(clock, 1000);
1570 assert(!ret);
1571 ret = bt_ctf_clock_set_frequency(clock, 1000000000);
1572 assert(!ret);
44e0165b
PP
1573 ret = bt_ctf_clock_set_frequency(clock, 1534);
1574 assert(!ret);
44e0165b
PP
1575
1576 BT_PUT(clock);
1577}
1578
39d74371
JG
1579int main(int argc, char **argv)
1580{
c2237f17 1581 const char *env_resize_length;
32bd47d1
MJ
1582 gchar *trace_path;
1583 gchar *metadata_path;
39d74371
JG
1584 const char *clock_name = "test_clock";
1585 const char *clock_description = "This is a test clock";
5494ce8b
JG
1586 const char *returned_clock_name;
1587 const char *returned_clock_description;
1588 const uint64_t frequency = 1123456789;
23396598 1589 const int64_t offset_s = 13515309;
61cf588b
MD
1590 const int64_t offset = 1234567;
1591 int64_t get_offset_s,
61ec14e6 1592 get_offset;
39d74371 1593 const uint64_t precision = 10;
5494ce8b 1594 const int is_absolute = 0xFF;
39d74371
JG
1595 char *metadata_string;
1596 struct bt_ctf_writer *writer;
e2e51e32
MJ
1597 struct bt_utsname name = {"GNU/Linux", "testhost", "4.4.0-87-generic",
1598 "#110-Ubuntu SMP Tue Jul 18 12:55:35 UTC 2017", "x86_64"};
1ff9582c 1599 struct bt_ctf_clock *clock, *ret_clock;
3dca2276
PP
1600 struct bt_ctf_clock_class *ret_clock_class;
1601 struct bt_ctf_stream_class *stream_class, *ret_stream_class;
1602 struct bt_ctf_stream *stream1;
1603 struct bt_ctf_stream *stream;
e3c971da 1604 const char *ret_string;
e61caf8e
JG
1605 const unsigned char *ret_uuid;
1606 unsigned char tmp_uuid[16] = { 0 };
3dca2276 1607 struct bt_ctf_field_type *packet_context_type,
b34f4d90 1608 *packet_context_field_type,
751b05c7
JG
1609 *packet_header_type,
1610 *packet_header_field_type,
35e8709f
JG
1611 *integer_type,
1612 *stream_event_context_type,
88d26616
JG
1613 *ret_field_type,
1614 *event_header_field_type;
3dca2276
PP
1615 struct bt_ctf_field *packet_header, *packet_header_field;
1616 struct bt_ctf_trace *trace;
12c8a1a3 1617 int ret;
4ae7c93b 1618 int64_t ret_int64_t;
dac5c838 1619 struct bt_value *obj;
39d74371 1620
dc3fffef
PP
1621 if (argc < 2) {
1622 printf("Usage: tests-ctf-writer path_to_babeltrace\n");
783c9151 1623 return -1;
39d74371
JG
1624 }
1625
c2237f17
PP
1626 env_resize_length = getenv("PACKET_RESIZE_TEST_LENGTH");
1627 if (env_resize_length) {
1628 packet_resize_test_length =
1629 (unsigned int) atoi(env_resize_length);
1630 }
1631
8bbe269d 1632 plan_tests(NR_TESTS);
39d74371 1633
32bd47d1 1634 trace_path = g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL);
2bb37f06 1635 if (!bt_mkdtemp(trace_path)) {
39d74371
JG
1636 perror("# perror");
1637 }
1638
32bd47d1 1639 metadata_path = g_build_filename(trace_path, "metadata", NULL);
39d74371
JG
1640
1641 writer = bt_ctf_writer_create(trace_path);
3dca2276 1642 ok(writer, "bt_ctf_create succeeds in creating trace with path");
39d74371 1643
4ae7c93b
JG
1644 ok(!bt_ctf_writer_get_trace(NULL),
1645 "bt_ctf_writer_get_trace correctly handles NULL");
1646 trace = bt_ctf_writer_get_trace(writer);
3dca2276
PP
1647 ok(bt_ctf_trace_set_native_byte_order(trace, BT_CTF_BYTE_ORDER_NATIVE),
1648 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_NATIVE");
1649 ok(bt_ctf_trace_set_native_byte_order(trace, BT_CTF_BYTE_ORDER_UNSPECIFIED),
1650 "Cannot set a trace's byte order to BT_CTF_BYTE_ORDER_UNSPECIFIED");
4ae7c93b 1651 ok(trace,
3dca2276
PP
1652 "bt_ctf_writer_get_trace returns a bt_ctf_trace object");
1653 ok(bt_ctf_trace_set_native_byte_order(trace, BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0,
35731220 1654 "Set a trace's byte order to big endian");
3dca2276
PP
1655 ok(bt_ctf_trace_get_native_byte_order(trace) == BT_CTF_BYTE_ORDER_BIG_ENDIAN,
1656 "bt_ctf_trace_get_native_byte_order returns a correct endianness");
4ae7c93b 1657
39d74371 1658 /* Add environment context to the trace */
e2e51e32 1659 ok(bt_ctf_writer_add_environment_field(writer, "host", name.nodename) == 0,
39d74371 1660 "Add host (%s) environment field to writer instance",
e2e51e32 1661 name.nodename);
39d74371
JG
1662 ok(bt_ctf_writer_add_environment_field(NULL, "test_field",
1663 "test_value"),
1664 "bt_ctf_writer_add_environment_field error with NULL writer");
1665 ok(bt_ctf_writer_add_environment_field(writer, NULL,
1666 "test_value"),
1667 "bt_ctf_writer_add_environment_field error with NULL field name");
1668 ok(bt_ctf_writer_add_environment_field(writer, "test_field",
1669 NULL),
1670 "bt_ctf_writer_add_environment_field error with NULL field value");
7f800dc7 1671
3dca2276 1672 /* Test bt_ctf_trace_set_environment_field with an integer object */
dac5c838 1673 obj = bt_value_integer_create_init(23);
7f800dc7 1674 assert(obj);
3dca2276
PP
1675 ok(bt_ctf_trace_set_environment_field(NULL, "test_env_int_obj", obj),
1676 "bt_ctf_trace_set_environment_field handles a NULL trace correctly");
1677 ok(bt_ctf_trace_set_environment_field(trace, NULL, obj),
1678 "bt_ctf_trace_set_environment_field handles a NULL name correctly");
1679 ok(bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", NULL),
1680 "bt_ctf_trace_set_environment_field handles a NULL value correctly");
1681 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_int_obj", obj),
1682 "bt_ctf_trace_set_environment_field succeeds in adding an integer object");
83509119 1683 BT_PUT(obj);
7f800dc7 1684
3dca2276 1685 /* Test bt_ctf_trace_set_environment_field with a string object */
dac5c838 1686 obj = bt_value_string_create_init("the value");
7f800dc7 1687 assert(obj);
3dca2276
PP
1688 ok(!bt_ctf_trace_set_environment_field(trace, "test_env_str_obj", obj),
1689 "bt_ctf_trace_set_environment_field succeeds in adding a string object");
83509119 1690 BT_PUT(obj);
7f800dc7 1691
3dca2276
PP
1692 /* Test bt_ctf_trace_set_environment_field_integer */
1693 ok(bt_ctf_trace_set_environment_field_integer(NULL, "test_env_int",
7f800dc7 1694 -194875),
3dca2276
PP
1695 "bt_ctf_trace_set_environment_field_integer handles a NULL trace correctly");
1696 ok(bt_ctf_trace_set_environment_field_integer(trace, NULL, -194875),
1697 "bt_ctf_trace_set_environment_field_integer handles a NULL name correctly");
1698 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
7f800dc7 1699 -164973),
3dca2276 1700 "bt_ctf_trace_set_environment_field_integer succeeds");
7f800dc7 1701
3dca2276
PP
1702 /* Test bt_ctf_trace_set_environment_field_string */
1703 ok(bt_ctf_trace_set_environment_field_string(NULL, "test_env_str",
7f800dc7 1704 "yeah"),
3dca2276
PP
1705 "bt_ctf_trace_set_environment_field_string handles a NULL trace correctly");
1706 ok(bt_ctf_trace_set_environment_field_string(trace, NULL, "yeah"),
1707 "bt_ctf_trace_set_environment_field_string handles a NULL name correctly");
1708 ok(bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
7f800dc7 1709 NULL),
3dca2276
PP
1710 "bt_ctf_trace_set_environment_field_string handles a NULL value correctly");
1711 ok(!bt_ctf_trace_set_environment_field_string(trace, "test_env_str",
7f800dc7 1712 "oh yeah"),
3dca2276 1713 "bt_ctf_trace_set_environment_field_string succeeds");
50842bdc 1714
3dca2276
PP
1715 /* Test bt_ctf_trace_get_environment_field_count */
1716 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
1717 "bt_ctf_trace_get_environment_field_count returns a correct number of environment fields");
50842bdc 1718
3dca2276
PP
1719 /* Test bt_ctf_trace_get_environment_field_name */
1720 ret_string = bt_ctf_trace_get_environment_field_name_by_index(trace, 0);
4ae7c93b 1721 ok(ret_string && !strcmp(ret_string, "host"),
3dca2276
PP
1722 "bt_ctf_trace_get_environment_field_name returns a correct field name");
1723 ret_string = bt_ctf_trace_get_environment_field_name_by_index(trace, 1);
7f800dc7 1724 ok(ret_string && !strcmp(ret_string, "test_env_int_obj"),
3dca2276
PP
1725 "bt_ctf_trace_get_environment_field_name returns a correct field name");
1726 ret_string = bt_ctf_trace_get_environment_field_name_by_index(trace, 2);
7f800dc7 1727 ok(ret_string && !strcmp(ret_string, "test_env_str_obj"),
3dca2276
PP
1728 "bt_ctf_trace_get_environment_field_name returns a correct field name");
1729 ret_string = bt_ctf_trace_get_environment_field_name_by_index(trace, 3);
7f800dc7 1730 ok(ret_string && !strcmp(ret_string, "test_env_int"),
3dca2276
PP
1731 "bt_ctf_trace_get_environment_field_name returns a correct field name");
1732 ret_string = bt_ctf_trace_get_environment_field_name_by_index(trace, 4);
7f800dc7 1733 ok(ret_string && !strcmp(ret_string, "test_env_str"),
3dca2276 1734 "bt_ctf_trace_get_environment_field_name returns a correct field name");
50842bdc 1735
3dca2276
PP
1736 /* Test bt_ctf_trace_get_environment_field_value */
1737 obj = bt_ctf_trace_get_environment_field_value_by_index(trace, 1);
dac5c838 1738 ret = bt_value_integer_get(obj, &ret_int64_t);
7f800dc7 1739 ok(!ret && ret_int64_t == 23,
3dca2276 1740 "bt_ctf_trace_get_environment_field_value succeeds in getting an integer value");
83509119 1741 BT_PUT(obj);
3dca2276 1742 obj = bt_ctf_trace_get_environment_field_value_by_index(trace, 2);
dac5c838 1743 ret = bt_value_string_get(obj, &ret_string);
7f800dc7 1744 ok(!ret && ret_string && !strcmp(ret_string, "the value"),
3dca2276 1745 "bt_ctf_trace_get_environment_field_value succeeds in getting a string value");
83509119 1746 BT_PUT(obj);
7f800dc7 1747
3dca2276
PP
1748 /* Test bt_ctf_trace_get_environment_field_value_by_name */
1749 ok(!bt_ctf_trace_get_environment_field_value_by_name(trace, "oh oh"),
1750 "bt_ctf_trace_get_environment_field_value_by_name returns NULL or an unknown field name");
1751 obj = bt_ctf_trace_get_environment_field_value_by_name(trace,
7f800dc7 1752 "test_env_str");
dac5c838 1753 ret = bt_value_string_get(obj, &ret_string);
7f800dc7 1754 ok(!ret && ret_string && !strcmp(ret_string, "oh yeah"),
3dca2276 1755 "bt_ctf_trace_get_environment_field_value_by_name succeeds in getting an existing field");
83509119 1756 BT_PUT(obj);
7f800dc7
PP
1757
1758 /* Test environment field replacement */
3dca2276 1759 ok(!bt_ctf_trace_set_environment_field_integer(trace, "test_env_int",
7f800dc7 1760 654321),
3dca2276
PP
1761 "bt_ctf_trace_set_environment_field_integer succeeds with an existing name");
1762 ok(bt_ctf_trace_get_environment_field_count(trace) == 5,
1763 "bt_ctf_trace_set_environment_field_integer with an existing key does not increase the environment size");
1764 obj = bt_ctf_trace_get_environment_field_value_by_index(trace, 3);
dac5c838 1765 ret = bt_value_integer_get(obj, &ret_int64_t);
7f800dc7 1766 ok(!ret && ret_int64_t == 654321,
3dca2276 1767 "bt_ctf_trace_get_environment_field_value successfully replaces an existing field");
83509119 1768 BT_PUT(obj);
39d74371 1769
39d74371
JG
1770 ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname)
1771 == 0, "Add sysname (%s) environment field to writer instance",
1772 name.sysname);
1773 ok(bt_ctf_writer_add_environment_field(writer, "nodename",
1774 name.nodename) == 0,
1775 "Add nodename (%s) environment field to writer instance",
1776 name.nodename);
1777 ok(bt_ctf_writer_add_environment_field(writer, "release", name.release)
1778 == 0, "Add release (%s) environment field to writer instance",
1779 name.release);
1780 ok(bt_ctf_writer_add_environment_field(writer, "version", name.version)
1781 == 0, "Add version (%s) environment field to writer instance",
1782 name.version);
1783 ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine)
1784 == 0, "Add machine (%s) environment field to writer istance",
1785 name.machine);
1786
1787 /* Define a clock and add it to the trace */
5494ce8b
JG
1788 ok(bt_ctf_clock_create("signed") == NULL,
1789 "Illegal clock name rejected");
39d74371
JG
1790 clock = bt_ctf_clock_create(clock_name);
1791 ok(clock, "Clock created sucessfully");
5494ce8b
JG
1792 returned_clock_name = bt_ctf_clock_get_name(clock);
1793 ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name");
d50c7132 1794 ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0,
5494ce8b
JG
1795 "Returned clock name is valid");
1796
1797 returned_clock_description = bt_ctf_clock_get_description(clock);
1798 ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description");
1799 ok(bt_ctf_clock_set_description(clock, clock_description) == 0,
1800 "Clock description set successfully");
1801
1802 returned_clock_description = bt_ctf_clock_get_description(clock);
1803 ok(returned_clock_description,
1804 "bt_ctf_clock_get_description returns a description.");
d50c7132
JG
1805 ok(returned_clock_description ?
1806 !strcmp(returned_clock_description, clock_description) : 0,
5494ce8b
JG
1807 "Returned clock description is valid");
1808
1809 ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ,
1810 "bt_ctf_clock_get_frequency returns the correct default frequency");
39d74371
JG
1811 ok(bt_ctf_clock_set_frequency(clock, frequency) == 0,
1812 "Set clock frequency");
5494ce8b
JG
1813 ok(bt_ctf_clock_get_frequency(clock) == frequency,
1814 "bt_ctf_clock_get_frequency returns the correct frequency once it is set");
1815
61cf588b
MD
1816 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
1817 "bt_ctf_clock_get_offset_s succeeds");
1818 ok(get_offset_s == DEFAULT_CLOCK_OFFSET_S,
5494ce8b 1819 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)");
39d74371
JG
1820 ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0,
1821 "Set clock offset (seconds)");
61cf588b
MD
1822 ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0,
1823 "bt_ctf_clock_get_offset_s succeeds");
1824 ok(get_offset_s == offset_s,
5494ce8b
JG
1825 "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set");
1826
61cf588b
MD
1827 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
1828 "bt_ctf_clock_get_offset succeeds");
1829 ok(get_offset == DEFAULT_CLOCK_OFFSET,
1830 "bt_ctf_clock_get_offset returns the correct default offset (in ticks)");
39d74371 1831 ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset");
61cf588b
MD
1832 ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0,
1833 "bt_ctf_clock_get_offset succeeds");
1834 ok(get_offset == offset,
1835 "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set");
5494ce8b
JG
1836
1837 ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION,
1838 "bt_ctf_clock_get_precision returns the correct default precision");
39d74371
JG
1839 ok(bt_ctf_clock_set_precision(clock, precision) == 0,
1840 "Set clock precision");
5494ce8b
JG
1841 ok(bt_ctf_clock_get_precision(clock) == precision,
1842 "bt_ctf_clock_get_precision returns the correct precision once it is set");
1843
1844 ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE,
1845 "bt_ctf_clock_get_precision returns the correct default is_absolute attribute");
1846 ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0,
39d74371 1847 "Set clock absolute property");
5494ce8b
JG
1848 ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute,
1849 "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set");
5494ce8b
JG
1850 ok(bt_ctf_clock_set_time(clock, current_time) == 0,
1851 "Set clock time");
e61caf8e
JG
1852 ret_uuid = bt_ctf_clock_get_uuid(clock);
1853 ok(ret_uuid,
1854 "bt_ctf_clock_get_uuid returns a UUID");
1855 if (ret_uuid) {
1856 memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid));
1857 /* Slightly modify UUID */
1858 tmp_uuid[sizeof(tmp_uuid) - 1]++;
1859 }
1860
e61caf8e 1861 ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0,
4caab45b 1862 "bt_ctf_clock_set_uuid sets a new uuid successfully");
e61caf8e
JG
1863 ret_uuid = bt_ctf_clock_get_uuid(clock);
1864 ok(ret_uuid,
1865 "bt_ctf_clock_get_uuid returns a UUID after setting a new one");
1866 ok(uuid_match(ret_uuid, tmp_uuid),
1867 "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one");
5494ce8b 1868
39d74371 1869 /* Define a stream class */
3dca2276
PP
1870 stream_class = bt_ctf_stream_class_create("test_stream");
1871 ret_string = bt_ctf_stream_class_get_name(stream_class);
88d26616 1872 ok(ret_string && !strcmp(ret_string, "test_stream"),
3dca2276 1873 "bt_ctf_stream_class_get_name returns a correct stream class name");
e3c971da 1874
3dca2276
PP
1875 ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
1876 "bt_ctf_stream_class_get_clock returns NULL when a clock was not set");
1877 ok(bt_ctf_stream_class_get_clock(NULL) == NULL,
1878 "bt_ctf_stream_class_get_clock handles NULL correctly");
1ff9582c 1879
39d74371 1880 ok(stream_class, "Create stream class");
3dca2276 1881 ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0,
39d74371 1882 "Set a stream class' clock");
3dca2276 1883 ret_clock = bt_ctf_stream_class_get_clock(stream_class);
1ff9582c 1884 ok(ret_clock == clock,
3dca2276 1885 "bt_ctf_stream_class_get_clock returns a correct clock");
83509119 1886 bt_put(ret_clock);
39d74371
JG
1887
1888 /* Test the event fields and event types APIs */
1889 type_field_tests();
1890
3dca2276
PP
1891 ok(bt_ctf_stream_class_get_id(stream_class) < 0,
1892 "bt_ctf_stream_class_get_id returns an error when no id is set");
1893 ok(bt_ctf_stream_class_set_id(NULL, 123) < 0,
1894 "bt_ctf_stream_class_set_id handles NULL correctly");
1895 ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0,
1ff9582c 1896 "Set an stream class' id");
3dca2276
PP
1897 ok(bt_ctf_stream_class_get_id(stream_class) == 123,
1898 "bt_ctf_stream_class_get_id returns the correct value");
1ff9582c 1899
d8469458 1900 /* Validate default event header fields */
3dca2276 1901 ret_field_type = bt_ctf_stream_class_get_event_header_type(
88d26616
JG
1902 stream_class);
1903 ok(ret_field_type,
3dca2276
PP
1904 "bt_ctf_stream_class_get_event_header_type returns an event header type");
1905 ok(bt_ctf_field_type_get_type_id(ret_field_type) == BT_CTF_FIELD_TYPE_ID_STRUCT,
88d26616
JG
1906 "Default event header type is a structure");
1907 event_header_field_type =
3dca2276 1908 bt_ctf_field_type_structure_get_field_type_by_name(
88d26616
JG
1909 ret_field_type, "id");
1910 ok(event_header_field_type,
1911 "Default event header type contains an \"id\" field");
3dca2276
PP
1912 ok(bt_ctf_field_type_get_type_id(
1913 event_header_field_type) == BT_CTF_FIELD_TYPE_ID_INTEGER,
88d26616 1914 "Default event header \"id\" field is an integer");
83509119 1915 bt_put(event_header_field_type);
88d26616 1916 event_header_field_type =
3dca2276 1917 bt_ctf_field_type_structure_get_field_type_by_name(
88d26616
JG
1918 ret_field_type, "timestamp");
1919 ok(event_header_field_type,
1920 "Default event header type contains a \"timestamp\" field");
3dca2276
PP
1921 ok(bt_ctf_field_type_get_type_id(
1922 event_header_field_type) == BT_CTF_FIELD_TYPE_ID_INTEGER,
88d26616 1923 "Default event header \"timestamp\" field is an integer");
83509119
JG
1924 bt_put(event_header_field_type);
1925 bt_put(ret_field_type);
88d26616 1926
751b05c7 1927 /* Add a custom trace packet header field */
3dca2276 1928 packet_header_type = bt_ctf_trace_get_packet_header_field_type(trace);
751b05c7 1929 ok(packet_header_type,
3dca2276
PP
1930 "bt_ctf_trace_get_packet_header_field_type returns a packet header");
1931 ok(bt_ctf_field_type_get_type_id(packet_header_type) == BT_CTF_FIELD_TYPE_ID_STRUCT,
1932 "bt_ctf_trace_get_packet_header_field_type returns a packet header of type struct");
1933 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
751b05c7
JG
1934 packet_header_type, "magic");
1935 ok(ret_field_type, "Default packet header type contains a \"magic\" field");
83509119 1936 bt_put(ret_field_type);
3dca2276 1937 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
751b05c7
JG
1938 packet_header_type, "uuid");
1939 ok(ret_field_type, "Default packet header type contains a \"uuid\" field");
83509119 1940 bt_put(ret_field_type);
3dca2276 1941 ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
751b05c7
JG
1942 packet_header_type, "stream_id");
1943 ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
83509119 1944 bt_put(ret_field_type);
751b05c7 1945
3dca2276
PP
1946 packet_header_field_type = bt_ctf_field_type_integer_create(22);
1947 ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
751b05c7
JG
1948 packet_header_field_type, "custom_trace_packet_header_field"),
1949 "Added a custom trace packet header field successfully");
1950
3dca2276
PP
1951 ok(bt_ctf_trace_set_packet_header_field_type(NULL, packet_header_type) < 0,
1952 "bt_ctf_trace_set_packet_header_field_type handles a NULL trace correctly");
1953 ok(!bt_ctf_trace_set_packet_header_field_type(trace, packet_header_type),
751b05c7
JG
1954 "Set a trace packet_header_type successfully");
1955
12c8a1a3 1956 /* Add a custom field to the stream class' packet context */
3dca2276 1957 packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class);
12c8a1a3 1958 ok(packet_context_type,
3dca2276
PP
1959 "bt_ctf_stream_class_get_packet_context_type returns a packet context type.");
1960 ok(bt_ctf_field_type_get_type_id(packet_context_type) == BT_CTF_FIELD_TYPE_ID_STRUCT,
12c8a1a3
JG
1961 "Packet context is a structure");
1962
3dca2276
PP
1963 ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type),
1964 "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly");
1965
1966 integer_type = bt_ctf_field_type_integer_create(32);
b34f4d90 1967
3dca2276 1968 ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
b34f4d90 1969 integer_type) < 0,
3dca2276
PP
1970 "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
1971
88d26616 1972 /* Create a "uint5_t" equivalent custom packet context field */
3dca2276 1973 packet_context_field_type = bt_ctf_field_type_integer_create(5);
88d26616 1974
3dca2276 1975 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
35e8709f 1976 packet_context_field_type, "custom_packet_context_field");
12c8a1a3
JG
1977 ok(ret == 0, "Packet context field added successfully");
1978
35e8709f 1979 /* Define a stream event context containing a my_integer field. */
3dca2276
PP
1980 stream_event_context_type = bt_ctf_field_type_structure_create();
1981 bt_ctf_field_type_structure_add_field(stream_event_context_type,
35e8709f
JG
1982 integer_type, "common_event_context");
1983
3dca2276 1984 ok(bt_ctf_stream_class_set_event_context_type(NULL,
35e8709f 1985 stream_event_context_type) < 0,
3dca2276
PP
1986 "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly");
1987 ok(bt_ctf_stream_class_set_event_context_type(stream_class,
35e8709f 1988 integer_type) < 0,
3dca2276 1989 "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure");
35e8709f 1990
3dca2276 1991 ok(bt_ctf_stream_class_set_event_context_type(
35e8709f
JG
1992 stream_class, stream_event_context_type) == 0,
1993 "Set a new stream event context type");
3dca2276
PP
1994
1995 ret_field_type = bt_ctf_stream_class_get_event_context_type(
35e8709f
JG
1996 stream_class);
1997 ok(ret_field_type == stream_event_context_type,
3dca2276 1998 "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
83509119 1999 bt_put(ret_field_type);
12c8a1a3 2000
3dca2276 2001
39d74371 2002 /* Instantiate a stream and append events */
ac0c6bdd
PP
2003 ret = bt_ctf_writer_add_clock(writer, clock);
2004 assert(ret == 0);
3dca2276
PP
2005
2006 ok(bt_ctf_trace_get_stream_count(trace) == 0,
2007 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)");
39d74371
JG
2008 stream1 = bt_ctf_writer_create_stream(writer, stream_class);
2009 ok(stream1, "Instanciate a stream class from writer");
3dca2276
PP
2010 ok(bt_ctf_trace_get_stream_count(trace) == 1,
2011 "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)");
2012 stream = bt_ctf_trace_get_stream_by_index(trace, 0);
c1e730fe 2013 ok(stream == stream1,
3dca2276 2014 "bt_ctf_trace_get_stream_by_index() succeeds and returns the correct value");
c1e730fe 2015 BT_PUT(stream);
39d74371 2016
b25d20ad
PP
2017 /*
2018 * Creating a stream through a writer adds the given stream
2019 * class to the writer's trace, thus registering the stream
2020 * class's clock to the trace.
2021 */
3dca2276
PP
2022 ok(bt_ctf_trace_get_clock_class_count(trace) == 1,
2023 "bt_ctf_trace_get_clock_class_count returns the correct number of clocks");
2024 ret_clock_class = bt_ctf_trace_get_clock_class_by_index(trace, 0);
2025 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class),
ac0c6bdd 2026 bt_ctf_clock_get_name(clock)) == 0,
3dca2276 2027 "bt_ctf_trace_get_clock_class returns the right clock instance");
ac0c6bdd 2028 bt_put(ret_clock_class);
3dca2276
PP
2029 ret_clock_class = bt_ctf_trace_get_clock_class_by_name(trace, clock_name);
2030 ok(strcmp(bt_ctf_clock_class_get_name(ret_clock_class),
ac0c6bdd 2031 bt_ctf_clock_get_name(clock)) == 0,
3dca2276 2032 "bt_ctf_trace_get_clock_class returns the right clock instance");
ac0c6bdd 2033 bt_put(ret_clock_class);
3dca2276
PP
2034 ok(!bt_ctf_trace_get_clock_class_by_name(trace, "random"),
2035 "bt_ctf_trace_get_clock_by_name fails when the requested clock doesn't exist");
b25d20ad 2036
3dca2276 2037 ret_stream_class = bt_ctf_stream_get_class(stream1);
36336d93 2038 ok(ret_stream_class,
3dca2276 2039 "bt_ctf_stream_get_class returns a stream class");
36336d93
JG
2040 ok(ret_stream_class == stream_class,
2041 "Returned stream class is of the correct type");
2042
09840de5
PP
2043 /*
2044 * Packet header, packet context, event header, and stream
2045 * event context types were copied for the resolving
2046 * process
2047 */
2048 BT_PUT(packet_header_type);
2049 BT_PUT(packet_context_type);
2050 BT_PUT(stream_event_context_type);
3dca2276 2051 packet_header_type = bt_ctf_trace_get_packet_header_field_type(trace);
09840de5
PP
2052 assert(packet_header_type);
2053 packet_context_type =
3dca2276 2054 bt_ctf_stream_class_get_packet_context_type(stream_class);
09840de5
PP
2055 assert(packet_context_type);
2056 stream_event_context_type =
3dca2276 2057 bt_ctf_stream_class_get_event_context_type(stream_class);
09840de5
PP
2058 assert(stream_event_context_type);
2059
751b05c7
JG
2060 /*
2061 * Try to modify the packet context type after a stream has been
2062 * created.
2063 */
3dca2276 2064 ret = bt_ctf_field_type_structure_add_field(packet_header_type,
751b05c7
JG
2065 packet_header_field_type, "should_fail");
2066 ok(ret < 0,
2067 "Trace packet header type can't be modified once a stream has been instanciated");
2068
12c8a1a3
JG
2069 /*
2070 * Try to modify the packet context type after a stream has been
2071 * created.
2072 */
3dca2276 2073 ret = bt_ctf_field_type_structure_add_field(packet_context_type,
12c8a1a3
JG
2074 packet_context_field_type, "should_fail");
2075 ok(ret < 0,
751b05c7 2076 "Packet context type can't be modified once a stream has been instanciated");
12c8a1a3 2077
35e8709f
JG
2078 /*
2079 * Try to modify the stream event context type after a stream has been
2080 * created.
2081 */
3dca2276 2082 ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
35e8709f
JG
2083 integer_type, "should_fail");
2084 ok(ret < 0,
751b05c7 2085 "Stream event context type can't be modified once a stream has been instanciated");
35e8709f
JG
2086
2087 /* Should fail after instanciating a stream (frozen) */
3dca2276 2088 ok(bt_ctf_stream_class_set_clock(stream_class, clock),
39d74371
JG
2089 "Changes to a stream class that was already instantiated fail");
2090
751b05c7 2091 /* Populate the custom packet header field only once for all tests */
3dca2276
PP
2092 ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
2093 "bt_ctf_stream_get_packet_header handles NULL correctly");
2094 packet_header = bt_ctf_stream_get_packet_header(stream1);
751b05c7 2095 ok(packet_header,
3dca2276
PP
2096 "bt_ctf_stream_get_packet_header returns a packet header");
2097 ret_field_type = bt_ctf_field_get_type(packet_header);
751b05c7
JG
2098 ok(ret_field_type == packet_header_type,
2099 "Stream returns a packet header of the appropriate type");
83509119 2100 bt_put(ret_field_type);
3dca2276 2101 packet_header_field = bt_ctf_field_structure_get_field_by_name(packet_header,
751b05c7
JG
2102 "custom_trace_packet_header_field");
2103 ok(packet_header_field,
2104 "Packet header structure contains a custom field with the appropriate name");
3dca2276
PP
2105 ret_field_type = bt_ctf_field_get_type(packet_header_field);
2106 ok(!bt_ctf_field_integer_unsigned_set_value(packet_header_field,
751b05c7 2107 54321), "Set custom packet header value successfully");
3dca2276
PP
2108 ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
2109 "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
2110 ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
2111 "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
2112 ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
2113 "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
2114 ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
751b05c7
JG
2115 "Successfully set a stream's packet header");
2116
3975bd7e
JG
2117 ok(bt_ctf_writer_add_environment_field(writer, "new_field", "test") == 0,
2118 "Add environment field to writer after stream creation");
2119
44e0165b
PP
2120 test_clock_utils();
2121
ac0c6bdd 2122 test_instanciate_event_before_stream(writer, clock);
42f45a8d 2123
39d74371
JG
2124 append_simple_event(stream_class, stream1, clock);
2125
2126 packet_resize_test(stream_class, stream1, clock);
2127
2128 append_complex_event(stream_class, stream1, clock);
2129
f60fde63
PP
2130 append_existing_event_class(stream_class);
2131
fdf80f32
JG
2132 test_empty_stream(writer);
2133
ac0c6bdd 2134 test_custom_event_header_stream(writer, clock);
29be776a 2135
39d74371
JG
2136 metadata_string = bt_ctf_writer_get_metadata_string(writer);
2137 ok(metadata_string, "Get metadata string");
2138
2139 bt_ctf_writer_flush_metadata(writer);
39d74371 2140
83509119
JG
2141 bt_put(clock);
2142 bt_put(ret_stream_class);
2143 bt_put(writer);
2144 bt_put(stream1);
2145 bt_put(packet_context_type);
2146 bt_put(packet_context_field_type);
2147 bt_put(integer_type);
2148 bt_put(stream_event_context_type);
2149 bt_put(ret_field_type);
2150 bt_put(packet_header_type);
2151 bt_put(packet_header_field_type);
2152 bt_put(packet_header);
2153 bt_put(packet_header_field);
2154 bt_put(trace);
39d74371 2155 free(metadata_string);
83509119 2156 bt_put(stream_class);
9b068522 2157
dc3fffef 2158 validate_trace(argv[1], trace_path);
245bd444 2159
3dca2276 2160 recursive_rmdir(trace_path);
32bd47d1
MJ
2161 g_free(trace_path);
2162 g_free(metadata_path);
2163
39d74371
JG
2164 return 0;
2165}
This page took 0.195918 seconds and 4 git commands to generate.