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