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