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