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