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