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