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> |
bcd3a967 | 29 | #include <babeltrace/ctf-ir/packet.h> |
ac0c6bdd | 30 | #include <babeltrace/ctf-ir/clock-class.h> |
28437b95 | 31 | #include <babeltrace/ctf-ir/trace.h> |
83509119 | 32 | #include <babeltrace/ref.h> |
39d74371 | 33 | #include <babeltrace/ctf/events.h> |
dac5c838 | 34 | #include <babeltrace/values.h> |
32bd47d1 | 35 | #include <glib.h> |
39d74371 | 36 | #include <unistd.h> |
3d9990ac | 37 | #include <babeltrace/compat/stdlib-internal.h> |
39d74371 | 38 | #include <stdio.h> |
3d9990ac PP |
39 | #include <babeltrace/compat/limits-internal.h> |
40 | #include <babeltrace/compat/stdio-internal.h> | |
39d74371 JG |
41 | #include <string.h> |
42 | #include <assert.h> | |
39d74371 | 43 | #include <fcntl.h> |
39d74371 | 44 | #include "tap/tap.h" |
10817e06 JG |
45 | #include <math.h> |
46 | #include <float.h> | |
851299b9 | 47 | #include "common.h" |
39d74371 JG |
48 | |
49 | #define METADATA_LINE_SIZE 512 | |
50 | #define SEQUENCE_TEST_LENGTH 10 | |
1fac895e | 51 | #define ARRAY_TEST_LENGTH 5 |
c2237f17 | 52 | #define PACKET_RESIZE_TEST_DEF_LENGTH 100000 |
39d74371 | 53 | |
5494ce8b JG |
54 | #define DEFAULT_CLOCK_FREQ 1000000000 |
55 | #define DEFAULT_CLOCK_PRECISION 1 | |
56 | #define DEFAULT_CLOCK_OFFSET 0 | |
57 | #define DEFAULT_CLOCK_OFFSET_S 0 | |
58 | #define DEFAULT_CLOCK_IS_ABSOLUTE 0 | |
59 | #define DEFAULT_CLOCK_TIME 0 | |
e1e30a8c | 60 | #define DEFAULT_CLOCK_VALUE 0 |
5494ce8b | 61 | |
f6ccaed9 | 62 | #define NR_TESTS 552 |
8bbe269d | 63 | |
e2e51e32 MJ |
64 | struct bt_utsname { |
65 | char sysname[BABELTRACE_HOST_NAME_MAX]; | |
66 | char nodename[BABELTRACE_HOST_NAME_MAX]; | |
67 | char release[BABELTRACE_HOST_NAME_MAX]; | |
68 | char version[BABELTRACE_HOST_NAME_MAX]; | |
69 | char machine[BABELTRACE_HOST_NAME_MAX]; | |
70 | }; | |
71 | ||
61cf588b | 72 | static int64_t current_time = 42; |
c2237f17 | 73 | static unsigned int packet_resize_test_length = PACKET_RESIZE_TEST_DEF_LENGTH; |
39d74371 | 74 | |
e61caf8e | 75 | /* Return 1 if uuids match, zero if different. */ |
a3d8579b | 76 | static |
e61caf8e JG |
77 | int uuid_match(const unsigned char *uuid_a, const unsigned char *uuid_b) |
78 | { | |
79 | int ret = 0; | |
80 | int i; | |
81 | ||
82 | if (!uuid_a || !uuid_b) { | |
83 | goto end; | |
84 | } | |
85 | ||
23f1c913 | 86 | for (i = 0; i < 16; i++) { |
e61caf8e JG |
87 | if (uuid_a[i] != uuid_b[i]) { |
88 | goto end; | |
89 | } | |
90 | } | |
91 | ||
92 | ret = 1; | |
93 | end: | |
94 | return ret; | |
95 | } | |
96 | ||
a3d8579b | 97 | static |
39d74371 JG |
98 | void validate_trace(char *parser_path, char *trace_path) |
99 | { | |
100 | int ret = 0; | |
62df685c | 101 | gchar *standard_error = NULL; |
f5d4e7db | 102 | gint exit_status; |
5de3705f | 103 | char *argv[] = {parser_path, trace_path, "-o", "dummy", NULL}; |
39d74371 | 104 | |
f5d4e7db | 105 | if (!parser_path || !trace_path) { |
39d74371 JG |
106 | ret = -1; |
107 | goto result; | |
108 | } | |
109 | ||
f5d4e7db MJ |
110 | if (!g_spawn_sync(NULL, |
111 | argv, | |
112 | NULL, | |
113 | G_SPAWN_STDOUT_TO_DEV_NULL, | |
114 | NULL, | |
115 | NULL, | |
116 | NULL, | |
117 | &standard_error, | |
118 | &exit_status, | |
119 | NULL)) { | |
120 | diag("Failed to spawn babeltrace."); | |
39d74371 JG |
121 | ret = -1; |
122 | goto result; | |
123 | } | |
124 | ||
2a116459 | 125 | /* Replace by g_spawn_check_exit_status when we require glib >= 2.34 */ |
e373846d | 126 | #ifdef G_OS_UNIX |
2a116459 MJ |
127 | ret = WIFEXITED(exit_status) ? WEXITSTATUS(exit_status) : -1; |
128 | #else | |
129 | ret = exit_status; | |
130 | #endif | |
131 | ||
132 | if (ret != 0) { | |
f5d4e7db MJ |
133 | diag("Babeltrace returned an error."); |
134 | diag_multiline(standard_error); | |
f5d4e7db | 135 | goto result; |
39d74371 | 136 | } |
f5d4e7db | 137 | |
39d74371 JG |
138 | result: |
139 | ok(ret == 0, "Babeltrace could read the resulting trace"); | |
f5d4e7db | 140 | g_free(standard_error); |
39d74371 JG |
141 | } |
142 | ||
a3d8579b | 143 | static |
50842bdc PP |
144 | void append_simple_event(struct bt_stream_class *stream_class, |
145 | struct bt_stream *stream, struct bt_ctf_clock *clock) | |
39d74371 JG |
146 | { |
147 | /* Create and add a simple event class */ | |
50842bdc PP |
148 | struct bt_event_class *simple_event_class = |
149 | bt_event_class_create("Simple Event"); | |
150 | struct bt_field_type *uint_12_type = | |
151 | bt_field_type_integer_create(12); | |
152 | struct bt_field_type *int_64_type = | |
153 | bt_field_type_integer_create(64); | |
154 | struct bt_field_type *float_type = | |
155 | bt_field_type_floating_point_create(); | |
156 | struct bt_field_type *enum_type; | |
157 | struct bt_field_type *enum_type_unsigned = | |
158 | bt_field_type_enumeration_create(uint_12_type); | |
159 | struct bt_field_type *event_context_type = | |
160 | bt_field_type_structure_create(); | |
161 | struct bt_field_type *event_payload_type = NULL; | |
162 | struct bt_field_type *returned_type; | |
163 | struct bt_event *simple_event; | |
164 | struct bt_field *integer_field; | |
165 | struct bt_field *float_field; | |
166 | struct bt_field *enum_field; | |
167 | struct bt_field *enum_field_unsigned; | |
168 | struct bt_field *enum_container_field; | |
10817e06 | 169 | const char *mapping_name_test = "truie"; |
10817e06 | 170 | const double double_test_value = 3.1415; |
50842bdc | 171 | struct bt_field *enum_container_field_unsigned; |
7cfd41d6 JG |
172 | const char *mapping_name_negative_test = "negative_value"; |
173 | const char *ret_char; | |
10817e06 | 174 | double ret_double; |
7cfd41d6 JG |
175 | int64_t ret_range_start_int64_t, ret_range_end_int64_t; |
176 | uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t; | |
50842bdc PP |
177 | struct bt_event_class *ret_event_class; |
178 | struct bt_field *packet_context; | |
179 | struct bt_field *packet_context_field; | |
180 | struct bt_field *stream_event_context; | |
181 | struct bt_field *stream_event_context_field; | |
182 | struct bt_field *event_context; | |
183 | struct bt_field *event_context_field; | |
184 | struct bt_field_type *ep_integer_field_type = NULL; | |
185 | struct bt_field_type *ep_enum_field_type = NULL; | |
186 | struct bt_field_type *ep_enum_field_unsigned_type = NULL; | |
187 | struct bt_field_type_enumeration_mapping_iterator *iter = NULL; | |
bb34b5a7 | 188 | int ret; |
7cfd41d6 JG |
189 | |
190 | ok(uint_12_type, "Create an unsigned integer type"); | |
191 | ||
687ae062 JG |
192 | ok(!bt_ctf_field_type_integer_set_signed(int_64_type, 1), |
193 | "Set signed 64 bit integer signedness to true"); | |
7cfd41d6 | 194 | ok(int_64_type, "Create a signed integer type"); |
50842bdc | 195 | enum_type = bt_field_type_enumeration_create(int_64_type); |
7cfd41d6 | 196 | |
50842bdc PP |
197 | returned_type = bt_field_type_enumeration_get_container_type(enum_type); |
198 | ok(returned_type == int_64_type, "bt_field_type_enumeration_get_container_type returns the right type"); | |
199 | ok(!bt_field_type_enumeration_get_container_type(NULL), "bt_field_type_enumeration_get_container_type handles NULL correctly"); | |
200 | ok(!bt_field_type_enumeration_create(enum_type), | |
201 | "bt_field_enumeration_type_create rejects non-integer container field types"); | |
83509119 | 202 | bt_put(returned_type); |
39d74371 | 203 | |
50842bdc PP |
204 | bt_field_type_set_alignment(float_type, 32); |
205 | ok(bt_field_type_get_alignment(NULL) < 0, | |
206 | "bt_field_type_get_alignment handles NULL correctly"); | |
207 | ok(bt_field_type_get_alignment(float_type) == 32, | |
208 | "bt_field_type_get_alignment returns a correct value"); | |
7cfd41d6 | 209 | |
50842bdc | 210 | ok(bt_field_type_floating_point_set_exponent_digits(float_type, 11) == 0, |
7cfd41d6 | 211 | "Set a floating point type's exponent digit count"); |
50842bdc | 212 | ok(bt_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0, |
7cfd41d6 JG |
213 | "Set a floating point type's mantissa digit count"); |
214 | ||
50842bdc PP |
215 | ok(bt_field_type_floating_point_get_exponent_digits(NULL) < 0, |
216 | "bt_field_type_floating_point_get_exponent_digits handles NULL properly"); | |
217 | ok(bt_field_type_floating_point_get_mantissa_digits(NULL) < 0, | |
218 | "bt_field_type_floating_point_get_mantissa_digits handles NULL properly"); | |
219 | ok(bt_field_type_floating_point_get_exponent_digits(float_type) == 11, | |
220 | "bt_field_type_floating_point_get_exponent_digits returns the correct value"); | |
221 | ok(bt_field_type_floating_point_get_mantissa_digits(float_type) == 53, | |
222 | "bt_field_type_floating_point_get_mantissa_digits returns the correct value"); | |
8382544f JG |
223 | |
224 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, | |
7cfd41d6 | 225 | mapping_name_negative_test, -12345, 0) == 0, |
50842bdc | 226 | "bt_field_type_enumeration_add_mapping accepts negative enumeration mappings"); |
8382544f | 227 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
7cfd41d6 | 228 | "escaping; \"test\"", 1, 1) == 0, |
50842bdc | 229 | "bt_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes"); |
7cfd41d6 JG |
230 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
231 | "\tanother \'escaping\'\n test\"", 2, 4) == 0, | |
50842bdc | 232 | "bt_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters"); |
8382544f JG |
233 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
234 | "event clock int float", 5, 22) == 0, | |
235 | "Accept enumeration mapping strings containing reserved keywords"); | |
7cfd41d6 JG |
236 | bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, |
237 | 42, 42); | |
238 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, | |
50842bdc | 239 | 43, 51) == 0, "bt_field_type_enumeration_add_mapping accepts duplicate mapping names"); |
7cfd41d6 | 240 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something", |
50842bdc | 241 | -500, -400) == 0, "bt_field_type_enumeration_add_mapping accepts overlapping enum entries"); |
7cfd41d6 | 242 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, |
50842bdc | 243 | -54, -55), "bt_field_type_enumeration_add_mapping rejects mapping where end < start"); |
7cfd41d6 JG |
244 | bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000); |
245 | ||
50842bdc PP |
246 | iter = bt_field_type_enumeration_find_mappings_by_signed_value(NULL, -42); |
247 | ok(iter == NULL, "bt_field_type_enumeration_find_mappings_by_signed_value handles a NULL field type correctly"); | |
96e8f959 | 248 | |
50842bdc PP |
249 | iter = bt_field_type_enumeration_find_mappings_by_signed_value(enum_type, -4200000); |
250 | ret = bt_field_type_enumeration_mapping_iterator_next(iter); | |
251 | ok(iter && ret, "bt_field_type_enumeration_find_mappings_by_signed_value rejects non-mapped values"); | |
74fb0452 | 252 | BT_PUT(iter); |
96e8f959 | 253 | |
50842bdc PP |
254 | iter = bt_field_type_enumeration_find_mappings_by_signed_value(enum_type, 3); |
255 | ok(iter != NULL, "bt_field_type_enumeration_find_mappings_by_signed_value succeeds with mapped value"); | |
256 | ret = bt_field_type_enumeration_mapping_iterator_next(iter); | |
257 | ok(!ret && bt_field_type_enumeration_mapping_iterator_get_signed(iter, NULL, NULL, NULL) == 0, | |
258 | "bt_field_type_enumeration_mapping_iterator_get_signed handles mapped values correctly"); | |
96e8f959 | 259 | BT_PUT(iter); |
7cfd41d6 | 260 | |
50842bdc | 261 | ok(bt_event_class_add_field(simple_event_class, enum_type, |
7cfd41d6 JG |
262 | "enum_field") == 0, "Add signed enumeration field to event"); |
263 | ||
50842bdc | 264 | ok(bt_field_type_enumeration_get_mapping_signed(NULL, 0, &ret_char, |
7cfd41d6 | 265 | &ret_range_start_int64_t, &ret_range_end_int64_t) < 0, |
50842bdc PP |
266 | "bt_field_type_enumeration_get_mapping_signed handles a NULL enumeration correctly"); |
267 | ok(bt_field_type_enumeration_get_mapping_signed(enum_type, 0, NULL, | |
7cfd41d6 | 268 | &ret_range_start_int64_t, &ret_range_end_int64_t) == 0, |
50842bdc PP |
269 | "bt_field_type_enumeration_get_mapping_signed handles a NULL string correctly"); |
270 | ok(bt_field_type_enumeration_get_mapping_signed(enum_type, 0, &ret_char, | |
96e8f959 | 271 | NULL, &ret_range_end_int64_t) == 0, |
50842bdc PP |
272 | "bt_field_type_enumeration_get_mapping_signed handles a NULL start correctly"); |
273 | ok(bt_field_type_enumeration_get_mapping_signed(enum_type, 0, &ret_char, | |
96e8f959 | 274 | &ret_range_start_int64_t, NULL) == 0, |
50842bdc | 275 | "bt_field_type_enumeration_get_mapping_signed handles a NULL end correctly"); |
96e8f959 | 276 | /* Assumes entries are sorted by range_start values. */ |
50842bdc | 277 | ok(bt_field_type_enumeration_get_mapping_signed(enum_type, 6, &ret_char, |
96e8f959 | 278 | &ret_range_start_int64_t, &ret_range_end_int64_t) == 0, |
50842bdc | 279 | "bt_field_type_enumeration_get_mapping_signed returns a value"); |
7cfd41d6 | 280 | ok(!strcmp(ret_char, mapping_name_test), |
50842bdc | 281 | "bt_field_type_enumeration_get_mapping_signed returns a correct mapping name"); |
7cfd41d6 | 282 | ok(ret_range_start_int64_t == 42, |
50842bdc | 283 | "bt_field_type_enumeration_get_mapping_signed returns a correct mapping start"); |
7cfd41d6 | 284 | ok(ret_range_end_int64_t == 42, |
50842bdc | 285 | "bt_field_type_enumeration_get_mapping_signed returns a correct mapping end"); |
7cfd41d6 | 286 | |
50842bdc | 287 | ok(bt_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, |
7cfd41d6 | 288 | "escaping; \"test\"", 0, 0) == 0, |
50842bdc PP |
289 | "bt_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes"); |
290 | ok(bt_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, | |
7cfd41d6 | 291 | "\tanother \'escaping\'\n test\"", 1, 4) == 0, |
50842bdc PP |
292 | "bt_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters"); |
293 | ok(bt_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, | |
7cfd41d6 | 294 | "event clock int float", 5, 22) == 0, |
50842bdc PP |
295 | "bt_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords"); |
296 | ok(bt_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, | |
297 | 42, 42) == 0, "bt_field_type_enumeration_add_mapping_unsigned accepts single-value ranges"); | |
298 | ok(bt_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, | |
299 | 43, 51) == 0, "bt_field_type_enumeration_add_mapping_unsigned accepts duplicate mapping names"); | |
300 | ok(bt_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something", | |
301 | 7, 8) == 0, "bt_field_type_enumeration_add_mapping_unsigned accepts overlapping enum entries"); | |
302 | ok(bt_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, | |
303 | 55, 54), "bt_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start"); | |
304 | ok(bt_event_class_add_field(simple_event_class, enum_type_unsigned, | |
7cfd41d6 JG |
305 | "enum_field_unsigned") == 0, "Add unsigned enumeration field to event"); |
306 | ||
50842bdc PP |
307 | ok(bt_field_type_enumeration_get_mapping_count(NULL) < 0, |
308 | "bt_field_type_enumeration_get_mapping_count handles NULL correctly"); | |
309 | ok(bt_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 6, | |
310 | "bt_field_type_enumeration_get_mapping_count returns the correct value"); | |
7cfd41d6 | 311 | |
50842bdc | 312 | ok(bt_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char, |
7cfd41d6 | 313 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0, |
50842bdc PP |
314 | "bt_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly"); |
315 | ok(bt_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL, | |
96e8f959 | 316 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0, |
50842bdc PP |
317 | "bt_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly"); |
318 | ok(bt_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char, | |
96e8f959 | 319 | NULL, &ret_range_end_uint64_t) == 0, |
50842bdc PP |
320 | "bt_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly"); |
321 | ok(bt_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char, | |
96e8f959 | 322 | &ret_range_start_uint64_t, NULL) == 0, |
50842bdc PP |
323 | "bt_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly"); |
324 | ok(bt_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 4, &ret_char, | |
7cfd41d6 | 325 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0, |
50842bdc | 326 | "bt_field_type_enumeration_get_mapping_unsigned returns a value"); |
7cfd41d6 | 327 | ok(!strcmp(ret_char, mapping_name_test), |
50842bdc | 328 | "bt_field_type_enumeration_get_mapping_unsigned returns a correct mapping name"); |
7cfd41d6 | 329 | ok(ret_range_start_uint64_t == 42, |
50842bdc | 330 | "bt_field_type_enumeration_get_mapping_unsigned returns a correct mapping start"); |
7cfd41d6 | 331 | ok(ret_range_end_uint64_t == 42, |
50842bdc | 332 | "bt_field_type_enumeration_get_mapping_unsigned returns a correct mapping end"); |
8382544f | 333 | |
50842bdc | 334 | bt_event_class_add_field(simple_event_class, uint_12_type, |
39d74371 | 335 | "integer_field"); |
50842bdc | 336 | bt_event_class_add_field(simple_event_class, float_type, |
39d74371 | 337 | "float_field"); |
58203827 | 338 | |
50842bdc | 339 | assert(!bt_event_class_set_id(simple_event_class, 13)); |
3ed04f17 | 340 | |
a001a2ac | 341 | /* Set an event context type which will contain a single integer. */ |
50842bdc | 342 | ok(!bt_field_type_structure_add_field(event_context_type, uint_12_type, |
58203827 JG |
343 | "event_specific_context"), |
344 | "Add event specific context field"); | |
50842bdc PP |
345 | ok(bt_event_class_get_context_type(NULL) == NULL, |
346 | "bt_event_class_get_context_type handles NULL correctly"); | |
58203827 | 347 | |
50842bdc PP |
348 | ok(bt_event_class_set_context_type(NULL, event_context_type) < 0, |
349 | "bt_event_class_set_context_type handles a NULL event class correctly"); | |
350 | ok(!bt_event_class_set_context_type(simple_event_class, event_context_type), | |
58203827 | 351 | "Set an event class' context type successfully"); |
50842bdc | 352 | returned_type = bt_event_class_get_context_type(simple_event_class); |
58203827 | 353 | ok(returned_type == event_context_type, |
50842bdc | 354 | "bt_event_class_get_context_type returns the appropriate type"); |
83509119 | 355 | bt_put(returned_type); |
58203827 | 356 | |
50842bdc | 357 | ok(!bt_stream_class_add_event_class(stream_class, simple_event_class), |
a046cf3c | 358 | "Adding simple event class to stream class"); |
39d74371 | 359 | |
09840de5 | 360 | /* |
50842bdc | 361 | * bt_stream_class_add_event_class() copies the field types |
09840de5 PP |
362 | * of simple_event_class, so we retrieve the new ones to create |
363 | * the appropriate fields. | |
364 | */ | |
365 | BT_PUT(event_context_type); | |
366 | BT_PUT(event_payload_type); | |
50842bdc | 367 | event_payload_type = bt_event_class_get_payload_type( |
09840de5 PP |
368 | simple_event_class); |
369 | assert(event_payload_type); | |
50842bdc | 370 | event_context_type = bt_event_class_get_context_type( |
09840de5 PP |
371 | simple_event_class); |
372 | assert(event_context_type); | |
373 | ep_integer_field_type = | |
50842bdc | 374 | bt_field_type_structure_get_field_type_by_name( |
09840de5 PP |
375 | event_payload_type, "integer_field"); |
376 | assert(ep_integer_field_type); | |
377 | ep_enum_field_type = | |
50842bdc | 378 | bt_field_type_structure_get_field_type_by_name( |
09840de5 PP |
379 | event_payload_type, "enum_field"); |
380 | assert(ep_enum_field_type); | |
381 | ep_enum_field_unsigned_type = | |
50842bdc | 382 | bt_field_type_structure_get_field_type_by_name( |
09840de5 PP |
383 | event_payload_type, "enum_field_unsigned"); |
384 | assert(ep_enum_field_unsigned_type); | |
385 | ||
50842bdc PP |
386 | ok(bt_stream_class_get_event_class_count(NULL) < 0, |
387 | "bt_stream_class_get_event_class_count handles NULL correctly"); | |
388 | ok(bt_stream_class_get_event_class_count(stream_class) == 1, | |
389 | "bt_stream_class_get_event_class_count returns a correct number of event classes"); | |
390 | ok(bt_stream_class_get_event_class_by_index(NULL, 0) == NULL, | |
391 | "bt_stream_class_get_event_class handles NULL correctly"); | |
392 | ok(bt_stream_class_get_event_class_by_index(stream_class, 8724) == NULL, | |
393 | "bt_stream_class_get_event_class handles invalid indexes correctly"); | |
394 | ret_event_class = bt_stream_class_get_event_class_by_index(stream_class, 0); | |
e3c971da | 395 | ok(ret_event_class == simple_event_class, |
50842bdc | 396 | "bt_stream_class_get_event_class returns the correct event class"); |
83509119 | 397 | bt_put(ret_event_class); |
50842bdc PP |
398 | ok(!bt_stream_class_get_event_class_by_id(NULL, 0), |
399 | "bt_stream_class_get_event_class_by_id handles NULL correctly"); | |
400 | ok(!bt_stream_class_get_event_class_by_id(stream_class, 2), | |
401 | "bt_stream_class_get_event_class_by_id returns NULL when the requested ID doesn't exist"); | |
3ed04f17 | 402 | ret_event_class = |
50842bdc | 403 | bt_stream_class_get_event_class_by_id(stream_class, 13); |
3ed04f17 | 404 | ok(ret_event_class == simple_event_class, |
50842bdc | 405 | "bt_stream_class_get_event_class_by_id returns a correct event class"); |
83509119 | 406 | bt_put(ret_event_class); |
e3c971da | 407 | |
50842bdc | 408 | simple_event = bt_event_create(simple_event_class); |
39d74371 JG |
409 | ok(simple_event, |
410 | "Instantiate an event containing a single integer field"); | |
411 | ||
50842bdc PP |
412 | integer_field = bt_field_create(ep_integer_field_type); |
413 | bt_field_unsigned_integer_set_value(integer_field, 42); | |
414 | ok(bt_event_set_payload(simple_event, "integer_field", | |
415 | integer_field) == 0, "Use bt_event_set_payload to set a manually allocated field"); | |
416 | ||
417 | float_field = bt_event_get_payload(simple_event, "float_field"); | |
50842bdc | 418 | bt_field_floating_point_set_value(float_field, double_test_value); |
50842bdc PP |
419 | ok(!bt_field_floating_point_get_value(float_field, &ret_double), |
420 | "bt_field_floating_point_get_value returns a double value"); | |
10817e06 | 421 | ok(fabs(ret_double - double_test_value) <= DBL_EPSILON, |
50842bdc | 422 | "bt_field_floating_point_get_value returns a correct value"); |
10817e06 | 423 | |
50842bdc | 424 | enum_field = bt_field_create(ep_enum_field_type); |
09840de5 | 425 | assert(enum_field); |
96e8f959 | 426 | |
f6ccaed9 | 427 | enum_container_field = bt_field_enumeration_get_container(enum_field); |
50842bdc | 428 | ok(bt_field_signed_integer_set_value( |
7cfd41d6 JG |
429 | enum_container_field, -42) == 0, |
430 | "Set signed enumeration container value"); | |
50842bdc PP |
431 | iter = bt_field_enumeration_get_mappings(enum_field); |
432 | ok(iter, "bt_field_enumeration_get_mappings returns an iterator to matching mappings"); | |
433 | ret = bt_field_type_enumeration_mapping_iterator_next(iter); | |
434 | ok(!ret, "bt_field_enumeration_get_mappings returned a non-empty match"); | |
435 | ret = bt_field_type_enumeration_mapping_iterator_get_signed(iter, &ret_char, NULL, NULL); | |
436 | ok(!ret && ret_char, "bt_field_type_enumeration_mapping_iterator_get_signed return a mapping name"); | |
fcc2519b | 437 | assert(ret_char); |
7cfd41d6 | 438 | ok(!strcmp(ret_char, mapping_name_negative_test), |
50842bdc PP |
439 | "bt_field_enumeration_get_single_mapping_name returns the correct mapping name with an signed container"); |
440 | ret = bt_event_set_payload(simple_event, "enum_field", enum_field); | |
bb34b5a7 | 441 | assert(!ret); |
e0f15669 | 442 | BT_PUT(iter); |
39d74371 | 443 | |
50842bdc | 444 | enum_field_unsigned = bt_field_create(ep_enum_field_unsigned_type); |
09840de5 | 445 | assert(enum_field_unsigned); |
50842bdc | 446 | enum_container_field_unsigned = bt_field_enumeration_get_container( |
7cfd41d6 | 447 | enum_field_unsigned); |
50842bdc | 448 | ok(bt_field_unsigned_integer_set_value( |
7cfd41d6 JG |
449 | enum_container_field_unsigned, 42) == 0, |
450 | "Set unsigned enumeration container value"); | |
50842bdc | 451 | ret = bt_event_set_payload(simple_event, "enum_field_unsigned", |
7cfd41d6 | 452 | enum_field_unsigned); |
bb34b5a7 | 453 | assert(!ret); |
50842bdc | 454 | iter = bt_field_enumeration_get_mappings(enum_field_unsigned); |
e0f15669 | 455 | assert(iter); |
50842bdc | 456 | ret = bt_field_type_enumeration_mapping_iterator_next(iter); |
74fb0452 | 457 | assert(!ret); |
50842bdc | 458 | (void) bt_field_type_enumeration_mapping_iterator_get_unsigned(iter, &ret_char, NULL, NULL); |
ad9740b4 | 459 | ok(ret_char && !strcmp(ret_char, mapping_name_test), |
50842bdc | 460 | "bt_field_type_enumeration_mapping_iterator_get_unsigned returns the correct mapping name with an unsigned container"); |
7cfd41d6 | 461 | |
39d74371 JG |
462 | ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time"); |
463 | ||
6e1f8ea1 | 464 | /* Populate stream event context */ |
5fd2e9fd | 465 | stream_event_context = |
50842bdc | 466 | bt_event_get_stream_event_context(simple_event); |
5fd2e9fd | 467 | assert(stream_event_context); |
50842bdc | 468 | stream_event_context_field = bt_field_structure_get_field_by_name( |
5edae678 | 469 | stream_event_context, "common_event_context"); |
50842bdc | 470 | bt_field_unsigned_integer_set_value(stream_event_context_field, 42); |
5edae678 JG |
471 | |
472 | /* Populate the event's context */ | |
50842bdc | 473 | event_context = bt_event_get_event_context(simple_event); |
5edae678 | 474 | ok(event_context, |
50842bdc PP |
475 | "bt_event_get_event_context returns a field"); |
476 | returned_type = bt_field_get_type(event_context); | |
5edae678 | 477 | ok(returned_type == event_context_type, |
50842bdc PP |
478 | "bt_event_get_event_context returns a field of the appropriate type"); |
479 | event_context_field = bt_field_structure_get_field_by_name(event_context, | |
5edae678 | 480 | "event_specific_context"); |
50842bdc | 481 | ok(!bt_field_unsigned_integer_set_value(event_context_field, 1234), |
5edae678 | 482 | "Successfully set an event context's value"); |
50842bdc | 483 | ok(!bt_event_set_event_context(simple_event, event_context), |
5edae678 | 484 | "Set an event context successfully"); |
6e1f8ea1 | 485 | |
50842bdc | 486 | ok(bt_stream_append_event(stream, simple_event) == 0, |
39d74371 JG |
487 | "Append simple event to trace stream"); |
488 | ||
50842bdc | 489 | packet_context = bt_stream_get_packet_context(stream); |
12c8a1a3 | 490 | ok(packet_context, |
50842bdc | 491 | "bt_stream_get_packet_context returns a packet context"); |
12c8a1a3 | 492 | |
50842bdc | 493 | packet_context_field = bt_field_structure_get_field_by_name(packet_context, |
12c8a1a3 JG |
494 | "packet_size"); |
495 | ok(packet_context_field, | |
496 | "Packet context contains the default packet_size field."); | |
83509119 | 497 | bt_put(packet_context_field); |
50842bdc | 498 | packet_context_field = bt_field_structure_get_field_by_name(packet_context, |
35e8709f | 499 | "custom_packet_context_field"); |
50842bdc | 500 | ok(bt_field_unsigned_integer_set_value(packet_context_field, 8) == 0, |
12c8a1a3 JG |
501 | "Custom packet context field value successfully set."); |
502 | ||
50842bdc | 503 | ok(bt_stream_set_packet_context(stream, packet_context) == 0, |
12c8a1a3 JG |
504 | "Successfully set a stream's packet context"); |
505 | ||
50842bdc | 506 | ok(bt_stream_flush(stream) == 0, |
39d74371 JG |
507 | "Flush trace stream with one event"); |
508 | ||
83509119 JG |
509 | bt_put(simple_event_class); |
510 | bt_put(simple_event); | |
511 | bt_put(uint_12_type); | |
512 | bt_put(int_64_type); | |
513 | bt_put(float_type); | |
514 | bt_put(enum_type); | |
515 | bt_put(enum_type_unsigned); | |
516 | bt_put(returned_type); | |
517 | bt_put(event_context_type); | |
518 | bt_put(integer_field); | |
519 | bt_put(float_field); | |
520 | bt_put(enum_field); | |
521 | bt_put(enum_field_unsigned); | |
522 | bt_put(enum_container_field); | |
523 | bt_put(enum_container_field_unsigned); | |
524 | bt_put(packet_context); | |
525 | bt_put(packet_context_field); | |
526 | bt_put(stream_event_context); | |
527 | bt_put(stream_event_context_field); | |
528 | bt_put(event_context); | |
529 | bt_put(event_context_field); | |
09840de5 PP |
530 | bt_put(event_payload_type); |
531 | bt_put(ep_integer_field_type); | |
532 | bt_put(ep_enum_field_type); | |
533 | bt_put(ep_enum_field_unsigned_type); | |
e0f15669 | 534 | bt_put(iter); |
39d74371 JG |
535 | } |
536 | ||
a3d8579b | 537 | static |
50842bdc PP |
538 | void append_complex_event(struct bt_stream_class *stream_class, |
539 | struct bt_stream *stream, struct bt_ctf_clock *clock) | |
39d74371 | 540 | { |
74fb0452 | 541 | int i, ret; |
3b3b162e | 542 | struct event_class_attrs_counts ; |
1ff9582c | 543 | const char *complex_test_event_string = "Complex Test Event"; |
a31f4869 | 544 | const char *test_string_1 = "Test "; |
d8f190b2 PP |
545 | const char *test_string_2 = "string "; |
546 | const char *test_string_3 = "abcdefghi"; | |
547 | const char *test_string_4 = "abcd\0efg\0hi"; | |
f6ccaed9 | 548 | const char *test_string_cat = "Test string abcdeefg"; |
50842bdc PP |
549 | struct bt_field_type *uint_35_type = |
550 | bt_field_type_integer_create(35); | |
551 | struct bt_field_type *int_16_type = | |
552 | bt_field_type_integer_create(16); | |
553 | struct bt_field_type *uint_3_type = | |
554 | bt_field_type_integer_create(3); | |
555 | struct bt_field_type *enum_variant_type = | |
556 | bt_field_type_enumeration_create(uint_3_type); | |
557 | struct bt_field_type *variant_type = | |
558 | bt_field_type_variant_create(enum_variant_type, | |
39d74371 | 559 | "variant_selector"); |
50842bdc PP |
560 | struct bt_field_type *string_type = |
561 | bt_field_type_string_create(); | |
562 | struct bt_field_type *sequence_type; | |
563 | struct bt_field_type *array_type; | |
564 | struct bt_field_type *inner_structure_type = | |
565 | bt_field_type_structure_create(); | |
566 | struct bt_field_type *complex_structure_type = | |
567 | bt_field_type_structure_create(); | |
568 | struct bt_field_type *ret_field_type; | |
569 | struct bt_event_class *event_class; | |
570 | struct bt_event *event; | |
571 | struct bt_field *uint_35_field, *int_16_field, *a_string_field, | |
39d74371 JG |
572 | *inner_structure_field, *complex_structure_field, |
573 | *a_sequence_field, *enum_variant_field, *enum_container_field, | |
5fd2e9fd PP |
574 | *variant_field, *an_array_field, *stream_event_ctx_field, |
575 | *stream_event_ctx_int_field, *ret_field; | |
10817e06 | 576 | uint64_t ret_unsigned_int; |
1fac895e | 577 | int64_t ret_signed_int; |
10817e06 | 578 | const char *ret_string; |
50842bdc PP |
579 | struct bt_stream_class *ret_stream_class; |
580 | struct bt_event_class *ret_event_class; | |
581 | struct bt_field *packet_context, *packet_context_field; | |
582 | struct bt_field_type_enumeration_mapping_iterator *iter = NULL; | |
583 | ||
584 | ok(bt_field_type_set_alignment(int_16_type, 0), | |
585 | "bt_field_type_set_alignment handles 0-alignment correctly"); | |
586 | ok(bt_field_type_set_alignment(int_16_type, 3), | |
587 | "bt_field_type_set_alignment handles wrong alignment correctly (3)"); | |
588 | ok(bt_field_type_set_alignment(int_16_type, 24), | |
589 | "bt_field_type_set_alignment handles wrong alignment correctly (24)"); | |
590 | ok(!bt_field_type_set_alignment(int_16_type, 4), | |
591 | "bt_field_type_set_alignment handles correct alignment correctly (4)"); | |
592 | ok(!bt_field_type_set_alignment(int_16_type, 32), | |
6f010556 JG |
593 | "Set alignment of signed 16 bit integer to 32"); |
594 | ok(!bt_ctf_field_type_integer_set_signed(int_16_type, 1), | |
595 | "Set integer signedness to true"); | |
50842bdc PP |
596 | ok(!bt_field_type_integer_set_base(uint_35_type, |
597 | BT_INTEGER_BASE_HEXADECIMAL), | |
6f010556 | 598 | "Set signed 16 bit integer base to hexadecimal"); |
39d74371 | 599 | |
50842bdc PP |
600 | array_type = bt_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH); |
601 | sequence_type = bt_field_type_sequence_create(int_16_type, | |
39d74371 | 602 | "seq_len"); |
7cfd41d6 | 603 | |
50842bdc PP |
604 | ok(bt_field_type_array_get_element_type(NULL) == NULL, |
605 | "bt_field_type_array_get_element_type handles NULL correctly"); | |
606 | ret_field_type = bt_field_type_array_get_element_type( | |
7cfd41d6 JG |
607 | array_type); |
608 | ok(ret_field_type == int_16_type, | |
50842bdc | 609 | "bt_field_type_array_get_element_type returns the correct type"); |
83509119 | 610 | bt_put(ret_field_type); |
7cfd41d6 | 611 | |
50842bdc PP |
612 | ok(bt_field_type_array_get_length(NULL) < 0, |
613 | "bt_field_type_array_get_length handles NULL correctly"); | |
614 | ok(bt_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH, | |
615 | "bt_field_type_array_get_length returns the correct length"); | |
7cfd41d6 | 616 | |
50842bdc | 617 | ok(bt_field_type_structure_add_field(inner_structure_type, |
73892edc | 618 | inner_structure_type, "yes"), "Cannot add self to structure"); |
50842bdc | 619 | ok(!bt_field_type_structure_add_field(inner_structure_type, |
2c53bf6d | 620 | uint_35_type, "seq_len"), "Add seq_len field to inner structure"); |
50842bdc | 621 | ok(!bt_field_type_structure_add_field(inner_structure_type, |
2c53bf6d | 622 | sequence_type, "a_sequence"), "Add a_sequence field to inner structure"); |
50842bdc | 623 | ok(!bt_field_type_structure_add_field(inner_structure_type, |
2c53bf6d | 624 | array_type, "an_array"), "Add an_array field to inner structure"); |
39d74371 JG |
625 | |
626 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
627 | "UINT3_TYPE", 0, 0); | |
628 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
629 | "INT16_TYPE", 1, 1); | |
630 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
631 | "UINT35_TYPE", 2, 7); | |
7cfd41d6 | 632 | |
50842bdc PP |
633 | iter = bt_field_type_enumeration_find_mappings_by_name(NULL, "INT16_TYPE"); |
634 | ok(iter == NULL, "bt_field_type_enumeration_find_mappings_by_name handles a NULL field type correctly"); | |
96e8f959 | 635 | |
50842bdc PP |
636 | iter = bt_field_type_enumeration_find_mappings_by_name(enum_variant_type, "INT16_TYPE"); |
637 | ok(iter != NULL, "bt_field_type_enumeration_find_mappings_by_name returns a non-NULL iterator"); | |
638 | ret = bt_field_type_enumeration_mapping_iterator_next(iter); | |
639 | ok(!ret, "bt_field_type_enumeration_find_mappings_by_name handles an existing mapping correctly"); | |
640 | ok(bt_field_type_enumeration_mapping_iterator_get_unsigned(iter, NULL, NULL, NULL) == 0, | |
641 | "bt_field_type_enumeration_mapping_iterator_get_unsigned handles mapped values correctly"); | |
96e8f959 MD |
642 | BT_PUT(iter); |
643 | ||
50842bdc PP |
644 | iter = bt_field_type_enumeration_find_mappings_by_name(enum_variant_type, NULL); |
645 | ret = bt_field_type_enumeration_mapping_iterator_next(iter); | |
646 | ok(iter && ret, "bt_field_type_enumeration_find_mappings_by_name handles a NULL name correctly"); | |
74fb0452 | 647 | BT_PUT(iter); |
96e8f959 | 648 | |
50842bdc PP |
649 | iter = bt_field_type_enumeration_find_mappings_by_unsigned_value(NULL, 1); |
650 | ok(iter == NULL, "bt_field_type_enumeration_find_mappings_by_unsigned_value handles a NULL field type correctly"); | |
96e8f959 | 651 | |
50842bdc PP |
652 | iter = bt_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type, -42); |
653 | ret = bt_field_type_enumeration_mapping_iterator_next(iter); | |
654 | ok(iter && ret, "bt_field_type_enumeration_find_mappings_by_unsigned_value handles invalid values correctly"); | |
655 | ok(bt_field_type_enumeration_mapping_iterator_get_unsigned(iter, NULL, NULL, NULL) != 0, | |
656 | "bt_field_type_enumeration_mapping_iterator_get_unsigned handles invalid values correctly"); | |
96e8f959 MD |
657 | BT_PUT(iter); |
658 | ||
50842bdc PP |
659 | iter = bt_field_type_enumeration_find_mappings_by_unsigned_value(enum_variant_type, 5); |
660 | ret = bt_field_type_enumeration_mapping_iterator_next(iter); | |
661 | ok(iter != NULL && !ret, "bt_field_type_enumeration_find_mappings_by_unsigned_value handles valid values correctly"); | |
662 | ok(bt_field_type_enumeration_mapping_iterator_get_unsigned(iter, NULL, NULL, NULL) == 0, | |
663 | "bt_field_type_enumeration_mapping_iterator_get_unsigned handles valid values correctly"); | |
96e8f959 | 664 | BT_PUT(iter); |
7cfd41d6 | 665 | |
50842bdc | 666 | ok(bt_field_type_variant_add_field(variant_type, uint_3_type, |
39d74371 | 667 | "An unknown entry"), "Reject a variant field based on an unknown tag value"); |
50842bdc | 668 | ok(bt_field_type_variant_add_field(variant_type, uint_3_type, |
39d74371 | 669 | "UINT3_TYPE") == 0, "Add a field to a variant"); |
50842bdc | 670 | ok(!bt_field_type_variant_add_field(variant_type, int_16_type, |
822b92d5 | 671 | "INT16_TYPE"), "Add INT16_TYPE field to variant"); |
50842bdc | 672 | ok(!bt_field_type_variant_add_field(variant_type, uint_35_type, |
822b92d5 | 673 | "UINT35_TYPE"), "Add UINT35_TYPE field to variant"); |
39d74371 | 674 | |
50842bdc PP |
675 | ok(bt_field_type_variant_get_tag_type(NULL) == NULL, |
676 | "bt_field_type_variant_get_tag_type handles NULL correctly"); | |
677 | ret_field_type = bt_field_type_variant_get_tag_type(variant_type); | |
7cfd41d6 | 678 | ok(ret_field_type == enum_variant_type, |
50842bdc | 679 | "bt_field_type_variant_get_tag_type returns a correct tag type"); |
83509119 | 680 | bt_put(ret_field_type); |
7cfd41d6 | 681 | |
50842bdc PP |
682 | ok(bt_field_type_variant_get_tag_name(NULL) == NULL, |
683 | "bt_field_type_variant_get_tag_name handles NULL correctly"); | |
684 | ret_string = bt_field_type_variant_get_tag_name(variant_type); | |
0fae838f | 685 | ok(ret_string ? !strcmp(ret_string, "variant_selector") : 0, |
50842bdc PP |
686 | "bt_field_type_variant_get_tag_name returns the correct variant tag name"); |
687 | ok(bt_field_type_variant_get_field_type_by_name(NULL, | |
7cfd41d6 | 688 | "INT16_TYPE") == NULL, |
50842bdc PP |
689 | "bt_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly"); |
690 | ok(bt_field_type_variant_get_field_type_by_name(variant_type, | |
7cfd41d6 | 691 | NULL) == NULL, |
50842bdc PP |
692 | "bt_field_type_variant_get_field_type_by_name handles a NULL field name correctly"); |
693 | ret_field_type = bt_field_type_variant_get_field_type_by_name( | |
7cfd41d6 JG |
694 | variant_type, "INT16_TYPE"); |
695 | ok(ret_field_type == int_16_type, | |
50842bdc | 696 | "bt_field_type_variant_get_field_type_by_name returns a correct field type"); |
83509119 | 697 | bt_put(ret_field_type); |
7cfd41d6 | 698 | |
50842bdc PP |
699 | ok(bt_field_type_variant_get_field_count(NULL) < 0, |
700 | "bt_field_type_variant_get_field_count handles NULL correctly"); | |
701 | ok(bt_field_type_variant_get_field_count(variant_type) == 3, | |
702 | "bt_field_type_variant_get_field_count returns the correct count"); | |
7cfd41d6 | 703 | |
50842bdc PP |
704 | ok(bt_field_type_variant_get_field_by_index(NULL, &ret_string, &ret_field_type, 0) < 0, |
705 | "bt_field_type_variant_get_field handles a NULL type correctly"); | |
706 | ok(bt_field_type_variant_get_field_by_index(variant_type, NULL, &ret_field_type, 0) == 0, | |
707 | "bt_field_type_variant_get_field handles a NULL field name correctly"); | |
83509119 | 708 | bt_put(ret_field_type); |
50842bdc PP |
709 | ok(bt_field_type_variant_get_field_by_index(variant_type, &ret_string, NULL, 0) == 0, |
710 | "bt_field_type_variant_get_field handles a NULL field type correctly"); | |
711 | ok(bt_field_type_variant_get_field_by_index(variant_type, &ret_string, &ret_field_type, 200) < 0, | |
712 | "bt_field_type_variant_get_field handles an invalid index correctly"); | |
713 | ok(bt_field_type_variant_get_field_by_index(variant_type, &ret_string, &ret_field_type, 1) == 0, | |
714 | "bt_field_type_variant_get_field returns a field"); | |
7cfd41d6 | 715 | ok(!strcmp("INT16_TYPE", ret_string), |
50842bdc | 716 | "bt_field_type_variant_get_field returns a correct field name"); |
7cfd41d6 | 717 | ok(ret_field_type == int_16_type, |
50842bdc | 718 | "bt_field_type_variant_get_field returns a correct field type"); |
83509119 | 719 | bt_put(ret_field_type); |
7cfd41d6 | 720 | |
50842bdc | 721 | ok(!bt_field_type_structure_add_field(complex_structure_type, |
2c53bf6d JG |
722 | enum_variant_type, "variant_selector"), |
723 | "Add variant_selector field to complex structure"); | |
50842bdc | 724 | ok(!bt_field_type_structure_add_field(complex_structure_type, |
beb0f041 | 725 | string_type, "string"), "Add `string` field to complex structure"); |
50842bdc | 726 | ok(!bt_field_type_structure_add_field(complex_structure_type, |
2c53bf6d JG |
727 | variant_type, "variant_value"), |
728 | "Add variant_value field to complex structure"); | |
50842bdc | 729 | ok(!bt_field_type_structure_add_field(complex_structure_type, |
2c53bf6d JG |
730 | inner_structure_type, "inner_structure"), |
731 | "Add inner_structure field to complex structure"); | |
39d74371 | 732 | |
50842bdc | 733 | event_class = bt_event_class_create(complex_test_event_string); |
39d74371 | 734 | ok(event_class, "Create an event class"); |
50842bdc | 735 | ok(bt_event_class_add_field(event_class, uint_35_type, ""), |
39d74371 | 736 | "Reject addition of a field with an empty name to an event"); |
50842bdc | 737 | ok(bt_event_class_add_field(event_class, NULL, "an_integer"), |
39d74371 | 738 | "Reject addition of a field with a NULL type to an event"); |
50842bdc | 739 | ok(bt_event_class_add_field(event_class, uint_35_type, |
39d74371 JG |
740 | "int"), |
741 | "Reject addition of a type with an illegal name to an event"); | |
50842bdc | 742 | ok(bt_event_class_add_field(event_class, uint_35_type, |
39d74371 JG |
743 | "uint_35") == 0, |
744 | "Add field of type unsigned integer to an event"); | |
50842bdc | 745 | ok(bt_event_class_add_field(event_class, int_16_type, |
39d74371 | 746 | "int_16") == 0, "Add field of type signed integer to an event"); |
50842bdc | 747 | ok(bt_event_class_add_field(event_class, complex_structure_type, |
39d74371 JG |
748 | "complex_structure") == 0, |
749 | "Add composite structure to an event"); | |
750 | ||
50842bdc PP |
751 | ok(bt_event_class_get_name(NULL) == NULL, |
752 | "bt_event_class_get_name handles NULL correctly"); | |
753 | ret_string = bt_event_class_get_name(event_class); | |
1ff9582c | 754 | ok(!strcmp(ret_string, complex_test_event_string), |
50842bdc PP |
755 | "bt_event_class_get_name returns a correct name"); |
756 | ok(bt_event_class_get_id(event_class) < 0, | |
757 | "bt_event_class_get_id returns a negative value when not set"); | |
758 | ok(bt_event_class_get_id(NULL) < 0, | |
759 | "bt_event_class_get_id handles NULL correctly"); | |
760 | ok(bt_event_class_set_id(NULL, 42) < 0, | |
761 | "bt_event_class_set_id handles NULL correctly"); | |
762 | ok(bt_event_class_set_id(event_class, 42) == 0, | |
1ff9582c | 763 | "Set an event class' id"); |
50842bdc PP |
764 | ok(bt_event_class_get_id(event_class) == 42, |
765 | "bt_event_class_get_id returns the correct value"); | |
1ff9582c | 766 | |
3b3b162e | 767 | /* Test event class attributes */ |
50842bdc | 768 | ok(bt_event_class_get_log_level(event_class) == BT_EVENT_CLASS_LOG_LEVEL_UNSPECIFIED, |
cf76ce92 | 769 | "event class has the expected initial log level"); |
50842bdc | 770 | ok(!bt_event_class_get_emf_uri(event_class), |
cf76ce92 | 771 | "as expected, event class has no initial EMF URI"); |
50842bdc PP |
772 | ok(bt_event_class_set_log_level(NULL, BT_EVENT_CLASS_LOG_LEVEL_INFO), |
773 | "bt_event_class_set_log_level handles a NULL event class correctly"); | |
774 | ok(bt_event_class_set_log_level(event_class, BT_EVENT_CLASS_LOG_LEVEL_UNKNOWN), | |
775 | "bt_event_class_set_log_level handles an unknown log level correctly"); | |
776 | ok(!bt_event_class_set_log_level(event_class, BT_EVENT_CLASS_LOG_LEVEL_INFO), | |
777 | "bt_event_class_set_log_level succeeds with a valid log level"); | |
778 | ok(bt_event_class_get_log_level(NULL) == BT_EVENT_CLASS_LOG_LEVEL_UNKNOWN, | |
779 | "bt_event_class_get_log_level handles a NULL event class correctly"); | |
780 | ok(bt_event_class_get_log_level(event_class) == BT_EVENT_CLASS_LOG_LEVEL_INFO, | |
781 | "bt_event_class_get_log_level returns the expected log level"); | |
782 | ok(bt_event_class_set_emf_uri(NULL, "http://diamon.org/babeltrace/"), | |
783 | "bt_event_class_set_emf_uri handles a NULL event class correctly"); | |
784 | ok(!bt_event_class_set_emf_uri(event_class, "http://diamon.org/babeltrace/"), | |
785 | "bt_event_class_set_emf_uri succeeds with a valid EMF URI"); | |
786 | ok(!bt_event_class_get_emf_uri(NULL), | |
787 | "bt_event_class_get_emf_uri handles a NULL event class correctly"); | |
788 | ok(strcmp(bt_event_class_get_emf_uri(event_class), "http://diamon.org/babeltrace/") == 0, | |
789 | "bt_event_class_get_emf_uri returns the expected EMF URI"); | |
790 | ok(!bt_event_class_set_emf_uri(event_class, NULL), | |
791 | "bt_event_class_set_emf_uri succeeds with NULL (to reset)"); | |
792 | ok(!bt_event_class_get_emf_uri(event_class), | |
cf76ce92 | 793 | "as expected, event class has no EMF URI after reset"); |
3b3b162e | 794 | |
39d74371 | 795 | /* Add event class to the stream class */ |
50842bdc | 796 | ok(bt_stream_class_add_event_class(stream_class, NULL), |
39d74371 | 797 | "Reject addition of NULL event class to a stream class"); |
50842bdc | 798 | ok(bt_stream_class_add_event_class(stream_class, |
39d74371 JG |
799 | event_class) == 0, "Add an event class to stream class"); |
800 | ||
50842bdc PP |
801 | ok(bt_event_class_get_stream_class(NULL) == NULL, |
802 | "bt_event_class_get_stream_class handles NULL correctly"); | |
803 | ret_stream_class = bt_event_class_get_stream_class(event_class); | |
1ff9582c | 804 | ok(ret_stream_class == stream_class, |
50842bdc | 805 | "bt_event_class_get_stream_class returns the correct stream class"); |
83509119 | 806 | bt_put(ret_stream_class); |
1ff9582c | 807 | |
50842bdc PP |
808 | ok(bt_event_class_get_payload_type_field_count(NULL) < 0, |
809 | "bt_event_class_get_field_count handles NULL correctly"); | |
810 | ok(bt_event_class_get_payload_type_field_count(event_class) == 3, | |
811 | "bt_event_class_get_field_count returns a correct value"); | |
1ff9582c | 812 | |
50842bdc | 813 | ok(bt_event_class_get_payload_type_field_by_index(NULL, &ret_string, |
1ff9582c | 814 | &ret_field_type, 0) < 0, |
50842bdc PP |
815 | "bt_event_class_get_field handles a NULL event class correctly"); |
816 | ok(bt_event_class_get_payload_type_field_by_index(event_class, NULL, | |
f9b799fc | 817 | &ret_field_type, 0) == 0, |
50842bdc | 818 | "bt_event_class_get_field handles a NULL field name correctly"); |
83509119 | 819 | bt_put(ret_field_type); |
50842bdc | 820 | ok(bt_event_class_get_payload_type_field_by_index(event_class, &ret_string, |
f9b799fc | 821 | NULL, 0) == 0, |
50842bdc PP |
822 | "bt_event_class_get_field handles a NULL field type correctly"); |
823 | ok(bt_event_class_get_payload_type_field_by_index(event_class, &ret_string, | |
1ff9582c | 824 | &ret_field_type, 42) < 0, |
50842bdc PP |
825 | "bt_event_class_get_field handles an invalid index correctly"); |
826 | ok(bt_event_class_get_payload_type_field_by_index(event_class, &ret_string, | |
1ff9582c | 827 | &ret_field_type, 0) == 0, |
50842bdc PP |
828 | "bt_event_class_get_field returns a field"); |
829 | ok(bt_field_type_compare(ret_field_type, uint_35_type) == 0, | |
830 | "bt_event_class_get_field returns a correct field type"); | |
83509119 | 831 | bt_put(ret_field_type); |
1ff9582c | 832 | ok(!strcmp(ret_string, "uint_35"), |
50842bdc | 833 | "bt_event_class_get_field returns a correct field name"); |
1ff9582c | 834 | ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL, |
50842bdc | 835 | "bt_event_class_get_field_by_name handles a NULL event class correctly"); |
1ff9582c | 836 | ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL, |
50842bdc | 837 | "bt_event_class_get_field_by_name handles a NULL field name correctly"); |
1ff9582c | 838 | ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL, |
50842bdc | 839 | "bt_event_class_get_field_by_name handles an invalid field name correctly"); |
1ff9582c JG |
840 | ret_field_type = bt_ctf_event_class_get_field_by_name(event_class, |
841 | "complex_structure"); | |
50842bdc PP |
842 | ok(bt_field_type_compare(ret_field_type, complex_structure_type) == 0, |
843 | "bt_event_class_get_field_by_name returns a correct field type"); | |
83509119 | 844 | bt_put(ret_field_type); |
1ff9582c | 845 | |
50842bdc | 846 | event = bt_event_create(event_class); |
39d74371 JG |
847 | ok(event, "Instanciate a complex event"); |
848 | ||
50842bdc | 849 | ret_event_class = bt_event_get_class(event); |
1ff9582c | 850 | ok(ret_event_class == event_class, |
50842bdc | 851 | "bt_event_get_class returns the correct event class"); |
83509119 | 852 | bt_put(ret_event_class); |
1ff9582c | 853 | |
50842bdc | 854 | uint_35_field = bt_event_get_payload(event, "uint_35"); |
50842bdc PP |
855 | ok(uint_35_field, "Use bt_event_get_payload to get a field instance "); |
856 | bt_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D); | |
50842bdc | 857 | ok(bt_field_unsigned_integer_get_value(uint_35_field, |
10817e06 | 858 | &ret_unsigned_int) == 0, |
50842bdc | 859 | "bt_field_unsigned_integer_get_value succeeds after setting a value"); |
10817e06 | 860 | ok(ret_unsigned_int == 0x0DDF00D, |
50842bdc | 861 | "bt_field_unsigned_integer_get_value returns the correct value"); |
83509119 | 862 | bt_put(uint_35_field); |
39d74371 | 863 | |
50842bdc PP |
864 | int_16_field = bt_event_get_payload(event, "int_16"); |
865 | bt_field_signed_integer_set_value(int_16_field, -12345); | |
50842bdc | 866 | ok(bt_field_signed_integer_get_value(int_16_field, |
10817e06 | 867 | &ret_signed_int) == 0, |
50842bdc | 868 | "bt_field_signed_integer_get_value succeeds after setting a value"); |
10817e06 | 869 | ok(ret_signed_int == -12345, |
50842bdc | 870 | "bt_field_signed_integer_get_value returns the correct value"); |
83509119 | 871 | bt_put(int_16_field); |
39d74371 | 872 | |
50842bdc | 873 | complex_structure_field = bt_event_get_payload(event, |
39d74371 | 874 | "complex_structure"); |
10817e06 | 875 | |
50842bdc | 876 | inner_structure_field = bt_field_structure_get_field_by_index( |
10817e06 | 877 | complex_structure_field, 3); |
50842bdc | 878 | ret_field_type = bt_field_get_type(inner_structure_field); |
83509119 | 879 | bt_put(inner_structure_field); |
50842bdc PP |
880 | ok(bt_field_type_compare(ret_field_type, inner_structure_type) == 0, |
881 | "bt_field_structure_get_field_by_index returns a correct field"); | |
83509119 | 882 | bt_put(ret_field_type); |
10817e06 | 883 | |
50842bdc | 884 | inner_structure_field = bt_field_structure_get_field_by_name( |
39d74371 | 885 | complex_structure_field, "inner_structure"); |
50842bdc | 886 | a_string_field = bt_field_structure_get_field_by_name( |
beb0f041 | 887 | complex_structure_field, "string"); |
50842bdc | 888 | enum_variant_field = bt_field_structure_get_field_by_name( |
39d74371 | 889 | complex_structure_field, "variant_selector"); |
50842bdc | 890 | variant_field = bt_field_structure_get_field_by_name( |
39d74371 | 891 | complex_structure_field, "variant_value"); |
50842bdc | 892 | uint_35_field = bt_field_structure_get_field_by_name( |
39d74371 | 893 | inner_structure_field, "seq_len"); |
50842bdc | 894 | a_sequence_field = bt_field_structure_get_field_by_name( |
39d74371 | 895 | inner_structure_field, "a_sequence"); |
50842bdc | 896 | an_array_field = bt_field_structure_get_field_by_name( |
1fac895e | 897 | inner_structure_field, "an_array"); |
39d74371 | 898 | |
50842bdc | 899 | enum_container_field = bt_field_enumeration_get_container( |
39d74371 | 900 | enum_variant_field); |
50842bdc PP |
901 | bt_field_unsigned_integer_set_value(enum_container_field, 1); |
902 | int_16_field = bt_field_variant_get_field(variant_field, | |
39d74371 | 903 | enum_variant_field); |
50842bdc | 904 | bt_field_signed_integer_set_value(int_16_field, -200); |
83509119 | 905 | bt_put(int_16_field); |
50842bdc | 906 | bt_field_string_set_value(a_string_field, |
a31f4869 | 907 | test_string_1); |
50842bdc PP |
908 | ok(!bt_field_string_append(a_string_field, test_string_2), |
909 | "bt_field_string_append succeeds"); | |
50842bdc PP |
910 | ok(!bt_field_string_append_len(a_string_field, test_string_3, 5), |
911 | "bt_field_string_append_len succeeds (append 5 characters)"); | |
f6ccaed9 | 912 | ok(!bt_field_string_append_len(a_string_field, &test_string_4[5], 3), |
50842bdc PP |
913 | "bt_field_string_append_len succeeds (append 0 characters)"); |
914 | ok(!bt_field_string_append_len(a_string_field, test_string_3, 0), | |
915 | "bt_field_string_append_len succeeds (append 0 characters)"); | |
916 | ||
917 | ret_string = bt_field_string_get_value(a_string_field); | |
918 | ok(ret_string, "bt_field_string_get_value returns a string"); | |
a31f4869 | 919 | ok(ret_string ? !strcmp(ret_string, test_string_cat) : 0, |
50842bdc PP |
920 | "bt_field_string_get_value returns a correct value"); |
921 | bt_field_unsigned_integer_set_value(uint_35_field, | |
39d74371 | 922 | SEQUENCE_TEST_LENGTH); |
10817e06 | 923 | |
50842bdc | 924 | ret_field_type = bt_field_type_variant_get_field_type_from_tag( |
7cfd41d6 JG |
925 | variant_type, enum_variant_field); |
926 | ok(ret_field_type == int_16_type, | |
50842bdc | 927 | "bt_field_type_variant_get_field_type_from_tag returns the correct field type"); |
7cfd41d6 | 928 | |
50842bdc | 929 | ok(bt_field_sequence_set_length(a_sequence_field, |
39d74371 | 930 | uint_35_field) == 0, "Set a sequence field's length"); |
50842bdc | 931 | ret_field = bt_field_sequence_get_length(a_sequence_field); |
cd95e351 | 932 | ok(ret_field == uint_35_field, |
50842bdc | 933 | "bt_field_sequence_get_length returns the correct length field"); |
39d74371 JG |
934 | |
935 | for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) { | |
50842bdc | 936 | int_16_field = bt_field_sequence_get_field( |
39d74371 | 937 | a_sequence_field, i); |
50842bdc | 938 | bt_field_signed_integer_set_value(int_16_field, 4 - i); |
83509119 | 939 | bt_put(int_16_field); |
39d74371 JG |
940 | } |
941 | ||
1fac895e | 942 | for (i = 0; i < ARRAY_TEST_LENGTH; i++) { |
50842bdc | 943 | int_16_field = bt_field_array_get_field( |
1fac895e | 944 | an_array_field, i); |
50842bdc | 945 | bt_field_signed_integer_set_value(int_16_field, i); |
83509119 | 946 | bt_put(int_16_field); |
1fac895e JG |
947 | } |
948 | ||
50842bdc | 949 | stream_event_ctx_field = bt_event_get_stream_event_context(event); |
5fd2e9fd | 950 | assert(stream_event_ctx_field); |
50842bdc | 951 | stream_event_ctx_int_field = bt_field_structure_get_field_by_name( |
5fd2e9fd PP |
952 | stream_event_ctx_field, "common_event_context"); |
953 | BT_PUT(stream_event_ctx_field); | |
50842bdc | 954 | bt_field_unsigned_integer_set_value(stream_event_ctx_int_field, 17); |
5fd2e9fd PP |
955 | BT_PUT(stream_event_ctx_int_field); |
956 | ||
39d74371 | 957 | bt_ctf_clock_set_time(clock, ++current_time); |
50842bdc | 958 | ok(bt_stream_append_event(stream, event) == 0, |
39d74371 | 959 | "Append a complex event to a stream"); |
12c8a1a3 JG |
960 | |
961 | /* | |
962 | * Populate the custom packet context field with a dummy value | |
963 | * otherwise flush will fail. | |
964 | */ | |
50842bdc PP |
965 | packet_context = bt_stream_get_packet_context(stream); |
966 | packet_context_field = bt_field_structure_get_field_by_name(packet_context, | |
35e8709f | 967 | "custom_packet_context_field"); |
50842bdc | 968 | bt_field_unsigned_integer_set_value(packet_context_field, 1); |
12c8a1a3 | 969 | |
50842bdc | 970 | ok(bt_stream_flush(stream) == 0, |
39d74371 JG |
971 | "Flush a stream containing a complex event"); |
972 | ||
83509119 JG |
973 | bt_put(uint_35_field); |
974 | bt_put(a_string_field); | |
975 | bt_put(inner_structure_field); | |
976 | bt_put(complex_structure_field); | |
977 | bt_put(a_sequence_field); | |
978 | bt_put(an_array_field); | |
979 | bt_put(enum_variant_field); | |
980 | bt_put(enum_container_field); | |
981 | bt_put(variant_field); | |
982 | bt_put(ret_field); | |
983 | bt_put(packet_context_field); | |
984 | bt_put(packet_context); | |
985 | bt_put(uint_35_type); | |
986 | bt_put(int_16_type); | |
987 | bt_put(string_type); | |
988 | bt_put(sequence_type); | |
989 | bt_put(array_type); | |
990 | bt_put(inner_structure_type); | |
991 | bt_put(complex_structure_type); | |
992 | bt_put(uint_3_type); | |
993 | bt_put(enum_variant_type); | |
994 | bt_put(variant_type); | |
995 | bt_put(ret_field_type); | |
996 | bt_put(event_class); | |
997 | bt_put(event); | |
39d74371 JG |
998 | } |
999 | ||
a3d8579b | 1000 | static |
50842bdc PP |
1001 | void field_copy_tests_validate_same_type(struct bt_field *field, |
1002 | struct bt_field_type *expected_type, const char *name) | |
e7cb4506 | 1003 | { |
50842bdc | 1004 | struct bt_field_type *copy_type; |
e7cb4506 | 1005 | |
50842bdc | 1006 | copy_type = bt_field_get_type(field); |
e7cb4506 | 1007 | ok(copy_type == expected_type, |
50842bdc | 1008 | "bt_field_copy does not copy the type (%s)", name); |
83509119 | 1009 | bt_put(copy_type); |
e7cb4506 PP |
1010 | } |
1011 | ||
a3d8579b | 1012 | static |
50842bdc PP |
1013 | void field_copy_tests_validate_diff_ptrs(struct bt_field *field_a, |
1014 | struct bt_field *field_b, const char *name) | |
e7cb4506 PP |
1015 | { |
1016 | ok(field_a != field_b, | |
50842bdc | 1017 | "bt_field_copy creates different pointers (%s)", name); |
e7cb4506 PP |
1018 | } |
1019 | ||
a3d8579b | 1020 | static |
e7cb4506 PP |
1021 | void field_copy_tests() |
1022 | { | |
50842bdc PP |
1023 | struct bt_field_type *len_type = NULL; |
1024 | struct bt_field_type *fp_type = NULL; | |
1025 | struct bt_field_type *s_type = NULL; | |
1026 | struct bt_field_type *e_int_type = NULL; | |
1027 | struct bt_field_type *e_type = NULL; | |
1028 | struct bt_field_type *v_type = NULL; | |
1029 | struct bt_field_type *v_label1_type = NULL; | |
1030 | struct bt_field_type *v_label1_array_type = NULL; | |
1031 | struct bt_field_type *v_label2_type = NULL; | |
1032 | struct bt_field_type *v_label2_seq_type = NULL; | |
1033 | struct bt_field_type *strct_type = NULL; | |
1034 | struct bt_field *len = NULL; | |
1035 | struct bt_field *fp = NULL; | |
1036 | struct bt_field *s = NULL; | |
1037 | struct bt_field *e_int = NULL; | |
1038 | struct bt_field *e = NULL; | |
1039 | struct bt_field *v = NULL; | |
1040 | struct bt_field *v_selected = NULL; | |
1041 | struct bt_field *v_selected_cur = NULL; | |
1042 | struct bt_field *v_selected_0 = NULL; | |
1043 | struct bt_field *v_selected_1 = NULL; | |
1044 | struct bt_field *v_selected_2 = NULL; | |
1045 | struct bt_field *v_selected_3 = NULL; | |
1046 | struct bt_field *v_selected_4 = NULL; | |
1047 | struct bt_field *v_selected_5 = NULL; | |
1048 | struct bt_field *v_selected_6 = NULL; | |
1049 | struct bt_field *a = NULL; | |
1050 | struct bt_field *a_0 = NULL; | |
1051 | struct bt_field *a_1 = NULL; | |
1052 | struct bt_field *a_2 = NULL; | |
1053 | struct bt_field *a_3 = NULL; | |
1054 | struct bt_field *a_4 = NULL; | |
1055 | struct bt_field *strct = NULL; | |
1056 | struct bt_field *len_copy = NULL; | |
1057 | struct bt_field *fp_copy = NULL; | |
1058 | struct bt_field *s_copy = NULL; | |
1059 | struct bt_field *e_int_copy = NULL; | |
1060 | struct bt_field *e_copy = NULL; | |
1061 | struct bt_field *v_copy = NULL; | |
1062 | struct bt_field *v_selected_copy = NULL; | |
1063 | struct bt_field *v_selected_copy_len = NULL; | |
1064 | struct bt_field *v_selected_0_copy = NULL; | |
1065 | struct bt_field *v_selected_1_copy = NULL; | |
1066 | struct bt_field *v_selected_2_copy = NULL; | |
1067 | struct bt_field *v_selected_3_copy = NULL; | |
1068 | struct bt_field *v_selected_4_copy = NULL; | |
1069 | struct bt_field *v_selected_5_copy = NULL; | |
1070 | struct bt_field *v_selected_6_copy = NULL; | |
1071 | struct bt_field *a_copy = NULL; | |
1072 | struct bt_field *a_0_copy = NULL; | |
1073 | struct bt_field *a_1_copy = NULL; | |
1074 | struct bt_field *a_2_copy = NULL; | |
1075 | struct bt_field *a_3_copy = NULL; | |
1076 | struct bt_field *a_4_copy = NULL; | |
1077 | struct bt_field *strct_copy = NULL; | |
1078 | struct bt_field_type_enumeration_mapping_iterator *e_iter = NULL; | |
e7cb4506 PP |
1079 | uint64_t uint64_t_val; |
1080 | const char *str_val; | |
1081 | double double_val; | |
1082 | int ret; | |
1083 | ||
1084 | /* create len type */ | |
50842bdc | 1085 | len_type = bt_field_type_integer_create(32); |
e7cb4506 PP |
1086 | assert(len_type); |
1087 | ||
1088 | /* create fp type */ | |
50842bdc | 1089 | fp_type = bt_field_type_floating_point_create(); |
e7cb4506 PP |
1090 | assert(fp_type); |
1091 | ||
1092 | /* create s type */ | |
50842bdc | 1093 | s_type = bt_field_type_string_create(); |
e7cb4506 PP |
1094 | assert(s_type); |
1095 | ||
1096 | /* create e_int type */ | |
50842bdc | 1097 | e_int_type = bt_field_type_integer_create(8); |
e7cb4506 PP |
1098 | assert(e_int_type); |
1099 | ||
1100 | /* create e type */ | |
50842bdc | 1101 | e_type = bt_field_type_enumeration_create(e_int_type); |
e7cb4506 PP |
1102 | assert(e_type); |
1103 | ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL1", | |
1104 | 10, 15); | |
1105 | assert(!ret); | |
1106 | ret = bt_ctf_field_type_enumeration_add_mapping(e_type, "LABEL2", | |
1107 | 23, 23); | |
1108 | assert(!ret); | |
1109 | ||
1110 | /* create v_label1 type */ | |
50842bdc | 1111 | v_label1_type = bt_field_type_string_create(); |
e7cb4506 PP |
1112 | assert(v_label1_type); |
1113 | ||
1114 | /* create v_label1_array type */ | |
50842bdc | 1115 | v_label1_array_type = bt_field_type_array_create(v_label1_type, 5); |
e7cb4506 PP |
1116 | assert(v_label1_array_type); |
1117 | ||
1118 | /* create v_label2 type */ | |
50842bdc | 1119 | v_label2_type = bt_field_type_integer_create(16); |
e7cb4506 PP |
1120 | assert(v_label2_type); |
1121 | ||
1122 | /* create v_label2_seq type */ | |
50842bdc | 1123 | v_label2_seq_type = bt_field_type_sequence_create(v_label2_type, |
e7cb4506 PP |
1124 | "len"); |
1125 | assert(v_label2_seq_type); | |
1126 | ||
1127 | /* create v type */ | |
50842bdc | 1128 | v_type = bt_field_type_variant_create(e_type, "e"); |
e7cb4506 | 1129 | assert(v_type); |
50842bdc | 1130 | ret = bt_field_type_variant_add_field(v_type, v_label1_array_type, |
e7cb4506 PP |
1131 | "LABEL1"); |
1132 | assert(!ret); | |
50842bdc | 1133 | ret = bt_field_type_variant_add_field(v_type, v_label2_seq_type, |
e7cb4506 PP |
1134 | "LABEL2"); |
1135 | assert(!ret); | |
1136 | ||
1137 | /* create strct type */ | |
50842bdc | 1138 | strct_type = bt_field_type_structure_create(); |
e7cb4506 | 1139 | assert(strct_type); |
50842bdc | 1140 | ret = bt_field_type_structure_add_field(strct_type, len_type, |
e7cb4506 PP |
1141 | "len"); |
1142 | assert(!ret); | |
50842bdc | 1143 | ret = bt_field_type_structure_add_field(strct_type, fp_type, "fp"); |
e7cb4506 | 1144 | assert(!ret); |
50842bdc | 1145 | ret = bt_field_type_structure_add_field(strct_type, s_type, "s"); |
e7cb4506 | 1146 | assert(!ret); |
50842bdc | 1147 | ret = bt_field_type_structure_add_field(strct_type, e_type, "e"); |
e7cb4506 | 1148 | assert(!ret); |
50842bdc | 1149 | ret = bt_field_type_structure_add_field(strct_type, v_type, "v"); |
e7cb4506 | 1150 | assert(!ret); |
50842bdc | 1151 | ret = bt_field_type_structure_add_field(strct_type, |
e7cb4506 PP |
1152 | v_label1_array_type, "a"); |
1153 | assert(!ret); | |
1154 | ||
1155 | /* create strct */ | |
50842bdc | 1156 | strct = bt_field_create(strct_type); |
e7cb4506 PP |
1157 | assert(strct); |
1158 | ||
1159 | /* get len field */ | |
50842bdc | 1160 | len = bt_field_structure_get_field_by_name(strct, "len"); |
e7cb4506 PP |
1161 | assert(len); |
1162 | ||
1163 | /* get fp field */ | |
50842bdc | 1164 | fp = bt_field_structure_get_field_by_name(strct, "fp"); |
e7cb4506 PP |
1165 | assert(fp); |
1166 | ||
1167 | /* get s field */ | |
50842bdc | 1168 | s = bt_field_structure_get_field_by_name(strct, "s"); |
e7cb4506 PP |
1169 | assert(s); |
1170 | ||
1171 | /* get e field */ | |
50842bdc | 1172 | e = bt_field_structure_get_field_by_name(strct, "e"); |
e7cb4506 PP |
1173 | assert(e); |
1174 | ||
1175 | /* get e_int (underlying integer) */ | |
50842bdc | 1176 | e_int = bt_field_enumeration_get_container(e); |
e7cb4506 PP |
1177 | assert(e_int); |
1178 | ||
1179 | /* get v field */ | |
50842bdc | 1180 | v = bt_field_structure_get_field_by_name(strct, "v"); |
e7cb4506 PP |
1181 | assert(v); |
1182 | ||
1183 | /* get a field */ | |
50842bdc | 1184 | a = bt_field_structure_get_field_by_name(strct, "a"); |
e7cb4506 PP |
1185 | assert(a); |
1186 | ||
1187 | /* set len field */ | |
50842bdc | 1188 | ret = bt_field_unsigned_integer_set_value(len, 7); |
e7cb4506 PP |
1189 | assert(!ret); |
1190 | ||
1191 | /* set fp field */ | |
50842bdc | 1192 | ret = bt_field_floating_point_set_value(fp, 3.14); |
e7cb4506 PP |
1193 | assert(!ret); |
1194 | ||
1195 | /* set s field */ | |
50842bdc | 1196 | ret = bt_field_string_set_value(s, "btbt"); |
e7cb4506 PP |
1197 | assert(!ret); |
1198 | ||
1199 | /* set e field (LABEL2) */ | |
50842bdc | 1200 | ret = bt_field_unsigned_integer_set_value(e_int, 23); |
e7cb4506 PP |
1201 | assert(!ret); |
1202 | ||
1203 | /* set v field */ | |
50842bdc | 1204 | v_selected = bt_field_variant_get_field(v, e); |
e7cb4506 | 1205 | assert(v_selected); |
50842bdc | 1206 | v_selected_cur = bt_field_variant_get_current_field(v); |
48482b59 | 1207 | ok(v_selected_cur == v_selected, |
50842bdc | 1208 | "bt_field_variant_get_current_field returns the current field"); |
83509119 | 1209 | bt_put(v_selected_cur); |
e7cb4506 PP |
1210 | |
1211 | /* set selected v field */ | |
50842bdc | 1212 | ret = bt_field_sequence_set_length(v_selected, len); |
e7cb4506 | 1213 | assert(!ret); |
50842bdc | 1214 | v_selected_0 = bt_field_sequence_get_field(v_selected, 0); |
e7cb4506 | 1215 | assert(v_selected_0); |
50842bdc | 1216 | ret = bt_field_unsigned_integer_set_value(v_selected_0, 7); |
e7cb4506 | 1217 | assert(!ret); |
50842bdc | 1218 | v_selected_1 = bt_field_sequence_get_field(v_selected, 1); |
e7cb4506 | 1219 | assert(v_selected_1); |
50842bdc | 1220 | ret = bt_field_unsigned_integer_set_value(v_selected_1, 6); |
e7cb4506 | 1221 | assert(!ret); |
50842bdc | 1222 | v_selected_2 = bt_field_sequence_get_field(v_selected, 2); |
e7cb4506 | 1223 | assert(v_selected_2); |
50842bdc | 1224 | ret = bt_field_unsigned_integer_set_value(v_selected_2, 5); |
e7cb4506 | 1225 | assert(!ret); |
50842bdc | 1226 | v_selected_3 = bt_field_sequence_get_field(v_selected, 3); |
e7cb4506 | 1227 | assert(v_selected_3); |
50842bdc | 1228 | ret = bt_field_unsigned_integer_set_value(v_selected_3, 4); |
e7cb4506 | 1229 | assert(!ret); |
50842bdc | 1230 | v_selected_4 = bt_field_sequence_get_field(v_selected, 4); |
e7cb4506 | 1231 | assert(v_selected_4); |
50842bdc | 1232 | ret = bt_field_unsigned_integer_set_value(v_selected_4, 3); |
e7cb4506 | 1233 | assert(!ret); |
50842bdc | 1234 | v_selected_5 = bt_field_sequence_get_field(v_selected, 5); |
e7cb4506 | 1235 | assert(v_selected_5); |
50842bdc | 1236 | ret = bt_field_unsigned_integer_set_value(v_selected_5, 2); |
e7cb4506 | 1237 | assert(!ret); |
50842bdc | 1238 | v_selected_6 = bt_field_sequence_get_field(v_selected, 6); |
e7cb4506 | 1239 | assert(v_selected_6); |
50842bdc | 1240 | ret = bt_field_unsigned_integer_set_value(v_selected_6, 1); |
e7cb4506 PP |
1241 | assert(!ret); |
1242 | ||
1243 | /* set a field */ | |
50842bdc | 1244 | a_0 = bt_field_array_get_field(a, 0); |
e7cb4506 | 1245 | assert(a_0); |
50842bdc | 1246 | ret = bt_field_string_set_value(a_0, "a_0"); |
e7cb4506 | 1247 | assert(!ret); |
50842bdc | 1248 | a_1 = bt_field_array_get_field(a, 1); |
e7cb4506 | 1249 | assert(a_1); |
50842bdc | 1250 | ret = bt_field_string_set_value(a_1, "a_1"); |
e7cb4506 | 1251 | assert(!ret); |
50842bdc | 1252 | a_2 = bt_field_array_get_field(a, 2); |
e7cb4506 | 1253 | assert(a_2); |
50842bdc | 1254 | ret = bt_field_string_set_value(a_2, "a_2"); |
e7cb4506 | 1255 | assert(!ret); |
50842bdc | 1256 | a_3 = bt_field_array_get_field(a, 3); |
e7cb4506 | 1257 | assert(a_3); |
50842bdc | 1258 | ret = bt_field_string_set_value(a_3, "a_3"); |
e7cb4506 | 1259 | assert(!ret); |
50842bdc | 1260 | a_4 = bt_field_array_get_field(a, 4); |
e7cb4506 | 1261 | assert(a_4); |
50842bdc | 1262 | ret = bt_field_string_set_value(a_4, "a_4"); |
e7cb4506 PP |
1263 | assert(!ret); |
1264 | ||
1265 | /* create copy of strct */ | |
50842bdc | 1266 | strct_copy = bt_field_copy(strct); |
e7cb4506 | 1267 | ok(strct_copy, |
50842bdc | 1268 | "bt_field_copy returns a valid pointer"); |
e7cb4506 PP |
1269 | |
1270 | /* get all copied fields */ | |
50842bdc | 1271 | len_copy = bt_field_structure_get_field_by_name(strct_copy, "len"); |
e7cb4506 | 1272 | assert(len_copy); |
50842bdc | 1273 | fp_copy = bt_field_structure_get_field_by_name(strct_copy, "fp"); |
e7cb4506 | 1274 | assert(fp_copy); |
50842bdc | 1275 | s_copy = bt_field_structure_get_field_by_name(strct_copy, "s"); |
e7cb4506 | 1276 | assert(s_copy); |
50842bdc | 1277 | e_copy = bt_field_structure_get_field_by_name(strct_copy, "e"); |
e7cb4506 | 1278 | assert(e_copy); |
50842bdc | 1279 | e_int_copy = bt_field_enumeration_get_container(e_copy); |
e7cb4506 | 1280 | assert(e_int_copy); |
50842bdc | 1281 | v_copy = bt_field_structure_get_field_by_name(strct_copy, "v"); |
e7cb4506 | 1282 | assert(v_copy); |
50842bdc | 1283 | v_selected_copy = bt_field_variant_get_field(v_copy, e_copy); |
e7cb4506 | 1284 | assert(v_selected_copy); |
50842bdc | 1285 | v_selected_0_copy = bt_field_sequence_get_field(v_selected_copy, 0); |
e7cb4506 | 1286 | assert(v_selected_0_copy); |
50842bdc | 1287 | v_selected_1_copy = bt_field_sequence_get_field(v_selected_copy, 1); |
e7cb4506 | 1288 | assert(v_selected_1_copy); |
50842bdc | 1289 | v_selected_2_copy = bt_field_sequence_get_field(v_selected_copy, 2); |
e7cb4506 | 1290 | assert(v_selected_2_copy); |
50842bdc | 1291 | v_selected_3_copy = bt_field_sequence_get_field(v_selected_copy, 3); |
e7cb4506 | 1292 | assert(v_selected_3_copy); |
50842bdc | 1293 | v_selected_4_copy = bt_field_sequence_get_field(v_selected_copy, 4); |
e7cb4506 | 1294 | assert(v_selected_4_copy); |
50842bdc | 1295 | v_selected_5_copy = bt_field_sequence_get_field(v_selected_copy, 5); |
e7cb4506 | 1296 | assert(v_selected_5_copy); |
50842bdc | 1297 | v_selected_6_copy = bt_field_sequence_get_field(v_selected_copy, 6); |
e7cb4506 | 1298 | assert(v_selected_6_copy); |
50842bdc | 1299 | a_copy = bt_field_structure_get_field_by_name(strct_copy, "a"); |
e7cb4506 | 1300 | assert(a_copy); |
50842bdc | 1301 | a_0_copy = bt_field_array_get_field(a_copy, 0); |
e7cb4506 | 1302 | assert(a_0_copy); |
50842bdc | 1303 | a_1_copy = bt_field_array_get_field(a_copy, 1); |
e7cb4506 | 1304 | assert(a_1_copy); |
50842bdc | 1305 | a_2_copy = bt_field_array_get_field(a_copy, 2); |
e7cb4506 | 1306 | assert(a_2_copy); |
50842bdc | 1307 | a_3_copy = bt_field_array_get_field(a_copy, 3); |
e7cb4506 | 1308 | assert(a_3_copy); |
50842bdc | 1309 | a_4_copy = bt_field_array_get_field(a_copy, 4); |
e7cb4506 | 1310 | assert(a_4_copy); |
e7cb4506 PP |
1311 | |
1312 | /* make sure copied fields are different pointers */ | |
1313 | field_copy_tests_validate_diff_ptrs(strct_copy, strct, "strct"); | |
1314 | field_copy_tests_validate_diff_ptrs(len_copy, len, "len"); | |
1315 | field_copy_tests_validate_diff_ptrs(fp_copy, fp, "fp"); | |
1316 | field_copy_tests_validate_diff_ptrs(s_copy, s, "s"); | |
1317 | field_copy_tests_validate_diff_ptrs(e_int_copy, e_int, "e_int"); | |
1318 | field_copy_tests_validate_diff_ptrs(e_copy, e, "e"); | |
1319 | field_copy_tests_validate_diff_ptrs(v_copy, v, "v"); | |
1320 | field_copy_tests_validate_diff_ptrs(v_selected_copy, v_selected, | |
1321 | "v_selected"); | |
1322 | field_copy_tests_validate_diff_ptrs(v_selected_0_copy, v_selected_0, | |
1323 | "v_selected_0"); | |
1324 | field_copy_tests_validate_diff_ptrs(v_selected_1_copy, v_selected_1, | |
1325 | "v_selected_1"); | |
1326 | field_copy_tests_validate_diff_ptrs(v_selected_2_copy, v_selected_2, | |
1327 | "v_selected_2"); | |
1328 | field_copy_tests_validate_diff_ptrs(v_selected_3_copy, v_selected_3, | |
1329 | "v_selected_3"); | |
1330 | field_copy_tests_validate_diff_ptrs(v_selected_4_copy, v_selected_4, | |
1331 | "v_selected_4"); | |
1332 | field_copy_tests_validate_diff_ptrs(v_selected_5_copy, v_selected_5, | |
1333 | "v_selected_5"); | |
1334 | field_copy_tests_validate_diff_ptrs(v_selected_6_copy, v_selected_6, | |
1335 | "v_selected_6"); | |
1336 | field_copy_tests_validate_diff_ptrs(a_copy, a, "a"); | |
1337 | field_copy_tests_validate_diff_ptrs(a_0_copy, a_0, "a_0"); | |
1338 | field_copy_tests_validate_diff_ptrs(a_1_copy, a_1, "a_1"); | |
1339 | field_copy_tests_validate_diff_ptrs(a_2_copy, a_2, "a_2"); | |
1340 | field_copy_tests_validate_diff_ptrs(a_3_copy, a_3, "a_3"); | |
1341 | field_copy_tests_validate_diff_ptrs(a_4_copy, a_4, "a_4"); | |
1342 | ||
1343 | /* make sure copied fields share the same types */ | |
1344 | field_copy_tests_validate_same_type(strct_copy, strct_type, "strct"); | |
1345 | field_copy_tests_validate_same_type(len_copy, len_type, "len"); | |
1346 | field_copy_tests_validate_same_type(fp_copy, fp_type, "fp"); | |
1347 | field_copy_tests_validate_same_type(e_int_copy, e_int_type, "e_int"); | |
1348 | field_copy_tests_validate_same_type(e_copy, e_type, "e"); | |
1349 | field_copy_tests_validate_same_type(v_copy, v_type, "v"); | |
1350 | field_copy_tests_validate_same_type(v_selected_copy, v_label2_seq_type, | |
1351 | "v_selected"); | |
1352 | field_copy_tests_validate_same_type(v_selected_0_copy, v_label2_type, | |
1353 | "v_selected_0"); | |
1354 | field_copy_tests_validate_same_type(v_selected_1_copy, v_label2_type, | |
1355 | "v_selected_1"); | |
1356 | field_copy_tests_validate_same_type(v_selected_2_copy, v_label2_type, | |
1357 | "v_selected_2"); | |
1358 | field_copy_tests_validate_same_type(v_selected_3_copy, v_label2_type, | |
1359 | "v_selected_3"); | |
1360 | field_copy_tests_validate_same_type(v_selected_4_copy, v_label2_type, | |
1361 | "v_selected_4"); | |
1362 | field_copy_tests_validate_same_type(v_selected_5_copy, v_label2_type, | |
1363 | "v_selected_5"); | |
1364 | field_copy_tests_validate_same_type(v_selected_6_copy, v_label2_type, | |
1365 | "v_selected_6"); | |
1366 | field_copy_tests_validate_same_type(a_copy, v_label1_array_type, "a"); | |
1367 | field_copy_tests_validate_same_type(a_0_copy, v_label1_type, "a_0"); | |
1368 | field_copy_tests_validate_same_type(a_1_copy, v_label1_type, "a_1"); | |
1369 | field_copy_tests_validate_same_type(a_2_copy, v_label1_type, "a_2"); | |
1370 | field_copy_tests_validate_same_type(a_3_copy, v_label1_type, "a_3"); | |
1371 | field_copy_tests_validate_same_type(a_4_copy, v_label1_type, "a_4"); | |
1372 | ||
1373 | /* validate len copy */ | |
50842bdc | 1374 | ret = bt_field_unsigned_integer_get_value(len_copy, &uint64_t_val); |
e7cb4506 PP |
1375 | assert(!ret); |
1376 | ok(uint64_t_val == 7, | |
50842bdc | 1377 | "bt_field_copy creates a valid integer field copy"); |
e7cb4506 PP |
1378 | |
1379 | /* validate fp copy */ | |
50842bdc | 1380 | ret = bt_field_floating_point_get_value(fp_copy, &double_val); |
e7cb4506 PP |
1381 | assert(!ret); |
1382 | ok(double_val == 3.14, | |
50842bdc | 1383 | "bt_field_copy creates a valid floating point number field copy"); |
e7cb4506 PP |
1384 | |
1385 | /* validate s copy */ | |
50842bdc | 1386 | str_val = bt_field_string_get_value(s_copy); |
e7cb4506 | 1387 | ok(str_val && !strcmp(str_val, "btbt"), |
50842bdc | 1388 | "bt_field_copy creates a valid string field copy"); |
e7cb4506 PP |
1389 | |
1390 | /* validate e_int copy */ | |
50842bdc | 1391 | ret = bt_field_unsigned_integer_get_value(e_int_copy, |
e7cb4506 PP |
1392 | &uint64_t_val); |
1393 | assert(!ret); | |
1394 | ok(uint64_t_val == 23, | |
50842bdc | 1395 | "bt_field_copy creates a valid enum's integer field copy"); |
e7cb4506 PP |
1396 | |
1397 | /* validate e copy */ | |
50842bdc PP |
1398 | e_iter = bt_field_enumeration_get_mappings(e_copy); |
1399 | (void) bt_field_type_enumeration_mapping_iterator_next(e_iter); | |
1400 | (void) bt_field_type_enumeration_mapping_iterator_get_signed(e_iter, | |
5c3f3b7e | 1401 | &str_val, NULL, NULL); |
e7cb4506 | 1402 | ok(str_val && !strcmp(str_val, "LABEL2"), |
50842bdc | 1403 | "bt_field_copy creates a valid enum field copy"); |
e7cb4506 PP |
1404 | |
1405 | /* validate v_selected copy */ | |
50842bdc | 1406 | v_selected_copy_len = bt_field_sequence_get_length(v_selected); |
e7cb4506 | 1407 | assert(v_selected_copy_len); |
50842bdc | 1408 | ret = bt_field_unsigned_integer_get_value(v_selected_copy_len, |
e7cb4506 PP |
1409 | &uint64_t_val); |
1410 | assert(!ret); | |
1411 | ok(uint64_t_val == 7, | |
50842bdc | 1412 | "bt_field_copy creates a sequence field copy with the proper length"); |
83509119 | 1413 | bt_put(v_selected_copy_len); |
e7cb4506 PP |
1414 | v_selected_copy_len = NULL; |
1415 | ||
1416 | /* validate v_selected copy fields */ | |
50842bdc | 1417 | ret = bt_field_unsigned_integer_get_value(v_selected_0_copy, |
e7cb4506 PP |
1418 | &uint64_t_val); |
1419 | assert(!ret); | |
1420 | ok(uint64_t_val == 7, | |
50842bdc PP |
1421 | "bt_field_copy creates a valid sequence field element copy (v_selected_0)"); |
1422 | ret = bt_field_unsigned_integer_get_value(v_selected_1_copy, | |
e7cb4506 PP |
1423 | &uint64_t_val); |
1424 | assert(!ret); | |
1425 | ok(uint64_t_val == 6, | |
50842bdc PP |
1426 | "bt_field_copy creates a valid sequence field element copy (v_selected_1)"); |
1427 | ret = bt_field_unsigned_integer_get_value(v_selected_2_copy, | |
e7cb4506 PP |
1428 | &uint64_t_val); |
1429 | assert(!ret); | |
1430 | ok(uint64_t_val == 5, | |
50842bdc PP |
1431 | "bt_field_copy creates a valid sequence field element copy (v_selected_2)"); |
1432 | ret = bt_field_unsigned_integer_get_value(v_selected_3_copy, | |
e7cb4506 PP |
1433 | &uint64_t_val); |
1434 | assert(!ret); | |
1435 | ok(uint64_t_val == 4, | |
50842bdc PP |
1436 | "bt_field_copy creates a valid sequence field element copy (v_selected_3)"); |
1437 | ret = bt_field_unsigned_integer_get_value(v_selected_4_copy, | |
e7cb4506 PP |
1438 | &uint64_t_val); |
1439 | assert(!ret); | |
1440 | ok(uint64_t_val == 3, | |
50842bdc PP |
1441 | "bt_field_copy creates a valid sequence field element copy (v_selected_4)"); |
1442 | ret = bt_field_unsigned_integer_get_value(v_selected_5_copy, | |
e7cb4506 PP |
1443 | &uint64_t_val); |
1444 | assert(!ret); | |
1445 | ok(uint64_t_val == 2, | |
50842bdc PP |
1446 | "bt_field_copy creates a valid sequence field element copy (v_selected_5)"); |
1447 | ret = bt_field_unsigned_integer_get_value(v_selected_6_copy, | |
e7cb4506 PP |
1448 | &uint64_t_val); |
1449 | assert(!ret); | |
1450 | ok(uint64_t_val == 1, | |
50842bdc | 1451 | "bt_field_copy creates a valid sequence field element copy (v_selected_6)"); |
e7cb4506 PP |
1452 | |
1453 | /* validate a copy fields */ | |
50842bdc | 1454 | str_val = bt_field_string_get_value(a_0_copy); |
e7cb4506 | 1455 | ok(str_val && !strcmp(str_val, "a_0"), |
50842bdc PP |
1456 | "bt_field_copy creates a valid array field element copy (a_0)"); |
1457 | str_val = bt_field_string_get_value(a_1_copy); | |
e7cb4506 | 1458 | ok(str_val && !strcmp(str_val, "a_1"), |
50842bdc PP |
1459 | "bt_field_copy creates a valid array field element copy (a_1)"); |
1460 | str_val = bt_field_string_get_value(a_2_copy); | |
e7cb4506 | 1461 | ok(str_val && !strcmp(str_val, "a_2"), |
50842bdc PP |
1462 | "bt_field_copy creates a valid array field element copy (a_2)"); |
1463 | str_val = bt_field_string_get_value(a_3_copy); | |
e7cb4506 | 1464 | ok(str_val && !strcmp(str_val, "a_3"), |
50842bdc PP |
1465 | "bt_field_copy creates a valid array field element copy (a_3)"); |
1466 | str_val = bt_field_string_get_value(a_4_copy); | |
e7cb4506 | 1467 | ok(str_val && !strcmp(str_val, "a_4"), |
50842bdc | 1468 | "bt_field_copy creates a valid array field element copy (a_4)"); |
e7cb4506 PP |
1469 | |
1470 | /* put everything */ | |
83509119 JG |
1471 | bt_put(len_type); |
1472 | bt_put(fp_type); | |
1473 | bt_put(s_type); | |
1474 | bt_put(e_int_type); | |
1475 | bt_put(e_type); | |
1476 | bt_put(v_type); | |
1477 | bt_put(v_label1_type); | |
1478 | bt_put(v_label1_array_type); | |
1479 | bt_put(v_label2_type); | |
1480 | bt_put(v_label2_seq_type); | |
1481 | bt_put(strct_type); | |
1482 | bt_put(len); | |
1483 | bt_put(fp); | |
1484 | bt_put(s); | |
1485 | bt_put(e_int); | |
1486 | bt_put(e); | |
1487 | bt_put(v); | |
1488 | bt_put(v_selected); | |
1489 | bt_put(v_selected_0); | |
1490 | bt_put(v_selected_1); | |
1491 | bt_put(v_selected_2); | |
1492 | bt_put(v_selected_3); | |
1493 | bt_put(v_selected_4); | |
1494 | bt_put(v_selected_5); | |
1495 | bt_put(v_selected_6); | |
1496 | bt_put(a); | |
1497 | bt_put(a_0); | |
1498 | bt_put(a_1); | |
1499 | bt_put(a_2); | |
1500 | bt_put(a_3); | |
1501 | bt_put(a_4); | |
1502 | bt_put(strct); | |
1503 | bt_put(len_copy); | |
1504 | bt_put(fp_copy); | |
1505 | bt_put(s_copy); | |
1506 | bt_put(e_int_copy); | |
1507 | bt_put(e_copy); | |
1508 | bt_put(v_copy); | |
1509 | bt_put(v_selected_copy); | |
1510 | bt_put(v_selected_0_copy); | |
1511 | bt_put(v_selected_1_copy); | |
1512 | bt_put(v_selected_2_copy); | |
1513 | bt_put(v_selected_3_copy); | |
1514 | bt_put(v_selected_4_copy); | |
1515 | bt_put(v_selected_5_copy); | |
1516 | bt_put(v_selected_6_copy); | |
1517 | bt_put(a_copy); | |
1518 | bt_put(a_0_copy); | |
1519 | bt_put(a_1_copy); | |
1520 | bt_put(a_2_copy); | |
1521 | bt_put(a_3_copy); | |
1522 | bt_put(a_4_copy); | |
1523 | bt_put(strct_copy); | |
e0f15669 | 1524 | bt_put(e_iter); |
e7cb4506 PP |
1525 | } |
1526 | ||
a3d8579b | 1527 | static |
39d74371 JG |
1528 | void type_field_tests() |
1529 | { | |
50842bdc PP |
1530 | struct bt_field *uint_12; |
1531 | struct bt_field *int_16; | |
1532 | struct bt_field *string; | |
50842bdc PP |
1533 | struct bt_field_type *composite_structure_type; |
1534 | struct bt_field_type *structure_seq_type; | |
1535 | struct bt_field_type *string_type; | |
1536 | struct bt_field_type *sequence_type; | |
1537 | struct bt_field_type *uint_8_type; | |
1538 | struct bt_field_type *int_16_type; | |
1539 | struct bt_field_type *uint_12_type = | |
1540 | bt_field_type_integer_create(12); | |
1541 | struct bt_field_type *enumeration_type; | |
1542 | struct bt_field_type *returned_type; | |
7cfd41d6 | 1543 | const char *ret_string; |
10817e06 | 1544 | |
39d74371 | 1545 | ok(uint_12_type, "Create an unsigned integer type"); |
50842bdc PP |
1546 | ok(bt_field_type_integer_set_base(uint_12_type, |
1547 | BT_INTEGER_BASE_BINARY) == 0, | |
39d74371 | 1548 | "Set integer type's base as binary"); |
50842bdc PP |
1549 | ok(bt_field_type_integer_set_base(uint_12_type, |
1550 | BT_INTEGER_BASE_DECIMAL) == 0, | |
39d74371 | 1551 | "Set integer type's base as decimal"); |
50842bdc PP |
1552 | ok(bt_field_type_integer_set_base(uint_12_type, |
1553 | BT_INTEGER_BASE_UNKNOWN), | |
39d74371 | 1554 | "Reject integer type's base set as unknown"); |
50842bdc PP |
1555 | ok(bt_field_type_integer_set_base(uint_12_type, |
1556 | BT_INTEGER_BASE_OCTAL) == 0, | |
39d74371 | 1557 | "Set integer type's base as octal"); |
50842bdc PP |
1558 | ok(bt_field_type_integer_set_base(uint_12_type, |
1559 | BT_INTEGER_BASE_HEXADECIMAL) == 0, | |
39d74371 | 1560 | "Set integer type's base as hexadecimal"); |
50842bdc | 1561 | ok(bt_field_type_integer_set_base(uint_12_type, 457417), |
39d74371 JG |
1562 | "Reject unknown integer base value"); |
1563 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0, | |
1564 | "Set integer type signedness to signed"); | |
1565 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0, | |
1566 | "Set integer type signedness to unsigned"); | |
50842bdc PP |
1567 | ok(bt_field_type_integer_get_size(NULL) < 0, |
1568 | "bt_field_type_integer_get_size handles NULL correctly"); | |
1569 | ok(bt_field_type_integer_get_size(uint_12_type) == 12, | |
1570 | "bt_field_type_integer_get_size returns a correct value"); | |
7cfd41d6 | 1571 | ok(bt_ctf_field_type_integer_get_signed(NULL) < 0, |
50842bdc | 1572 | "bt_field_type_integer_get_signed handles NULL correctly"); |
7cfd41d6 | 1573 | ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0, |
50842bdc PP |
1574 | "bt_field_type_integer_get_signed returns a correct value for unsigned types"); |
1575 | ||
1576 | ok(bt_field_type_set_byte_order(NULL, | |
1577 | BT_BYTE_ORDER_LITTLE_ENDIAN) < 0, | |
1578 | "bt_field_type_set_byte_order handles NULL correctly"); | |
1579 | ok(bt_field_type_set_byte_order(uint_12_type, | |
1580 | (enum bt_byte_order) 42) < 0, | |
1581 | "bt_field_type_set_byte_order rejects invalid values"); | |
1582 | ok(bt_field_type_set_byte_order(uint_12_type, | |
1583 | BT_BYTE_ORDER_LITTLE_ENDIAN) == 0, | |
7cfd41d6 | 1584 | "Set an integer's byte order to little endian"); |
50842bdc PP |
1585 | ok(bt_field_type_set_byte_order(uint_12_type, |
1586 | BT_BYTE_ORDER_BIG_ENDIAN) == 0, | |
7cfd41d6 | 1587 | "Set an integer's byte order to big endian"); |
50842bdc PP |
1588 | ok(bt_field_type_get_byte_order(uint_12_type) == |
1589 | BT_BYTE_ORDER_BIG_ENDIAN, | |
1590 | "bt_field_type_get_byte_order returns a correct value"); | |
1591 | ok(bt_field_type_get_byte_order(NULL) == | |
1592 | BT_BYTE_ORDER_UNKNOWN, | |
1593 | "bt_field_type_get_byte_order handles NULL correctly"); | |
1594 | ||
1595 | ok(bt_field_type_get_type_id(NULL) == | |
1596 | BT_FIELD_TYPE_ID_UNKNOWN, | |
1597 | "bt_field_type_get_type_id handles NULL correctly"); | |
1598 | ok(bt_field_type_get_type_id(uint_12_type) == | |
1599 | BT_FIELD_TYPE_ID_INTEGER, | |
1600 | "bt_field_type_get_type_id returns a correct value with an integer type"); | |
1601 | ||
1602 | ok(bt_field_type_integer_get_base(NULL) == | |
1603 | BT_INTEGER_BASE_UNKNOWN, | |
1604 | "bt_field_type_integer_get_base handles NULL correctly"); | |
1605 | ok(bt_field_type_integer_get_base(uint_12_type) == | |
1606 | BT_INTEGER_BASE_HEXADECIMAL, | |
1607 | "bt_field_type_integer_get_base returns a correct value"); | |
1608 | ||
1609 | ok(bt_field_type_integer_set_encoding(NULL, | |
1610 | BT_STRING_ENCODING_ASCII) < 0, | |
1611 | "bt_field_type_integer_set_encoding handles NULL correctly"); | |
1612 | ok(bt_field_type_integer_set_encoding(uint_12_type, | |
1613 | (enum bt_string_encoding) 123) < 0, | |
1614 | "bt_field_type_integer_set_encoding handles invalid encodings correctly"); | |
1615 | ok(bt_field_type_integer_set_encoding(uint_12_type, | |
1616 | BT_STRING_ENCODING_UTF8) == 0, | |
7cfd41d6 | 1617 | "Set integer type encoding to UTF8"); |
50842bdc PP |
1618 | ok(bt_field_type_integer_get_encoding(NULL) == |
1619 | BT_STRING_ENCODING_UNKNOWN, | |
1620 | "bt_field_type_integer_get_encoding handles NULL correctly"); | |
1621 | ok(bt_field_type_integer_get_encoding(uint_12_type) == | |
1622 | BT_STRING_ENCODING_UTF8, | |
1623 | "bt_field_type_integer_get_encoding returns a correct value"); | |
1624 | ||
1625 | int_16_type = bt_field_type_integer_create(16); | |
e13d28e1 | 1626 | assert(int_16_type); |
687ae062 JG |
1627 | ok(!bt_ctf_field_type_integer_set_signed(int_16_type, 1), |
1628 | "Set signedness of 16 bit integer to true"); | |
7cfd41d6 | 1629 | ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1, |
50842bdc PP |
1630 | "bt_field_type_integer_get_signed returns a correct value for signed types"); |
1631 | uint_8_type = bt_field_type_integer_create(8); | |
39d74371 | 1632 | sequence_type = |
50842bdc | 1633 | bt_field_type_sequence_create(int_16_type, "seq_len"); |
39d74371 | 1634 | ok(sequence_type, "Create a sequence of int16_t type"); |
50842bdc PP |
1635 | ok(bt_field_type_get_type_id(sequence_type) == |
1636 | BT_FIELD_TYPE_ID_SEQUENCE, | |
1637 | "bt_field_type_get_type_id returns a correct value with a sequence type"); | |
7cfd41d6 | 1638 | |
50842bdc PP |
1639 | ok(bt_field_type_sequence_get_length_field_name(NULL) == NULL, |
1640 | "bt_field_type_sequence_get_length_field_name handles NULL correctly"); | |
1641 | ret_string = bt_field_type_sequence_get_length_field_name( | |
7cfd41d6 JG |
1642 | sequence_type); |
1643 | ok(!strcmp(ret_string, "seq_len"), | |
50842bdc PP |
1644 | "bt_field_type_sequence_get_length_field_name returns the correct value"); |
1645 | ok(bt_field_type_sequence_get_element_type(NULL) == NULL, | |
1646 | "bt_field_type_sequence_get_element_type handles NULL correctly"); | |
1647 | returned_type = bt_field_type_sequence_get_element_type( | |
7cfd41d6 JG |
1648 | sequence_type); |
1649 | ok(returned_type == int_16_type, | |
50842bdc | 1650 | "bt_field_type_sequence_get_element_type returns the correct type"); |
83509119 | 1651 | bt_put(returned_type); |
39d74371 | 1652 | |
50842bdc | 1653 | string_type = bt_field_type_string_create(); |
39d74371 | 1654 | ok(string_type, "Create a string type"); |
50842bdc PP |
1655 | ok(bt_field_type_string_set_encoding(string_type, |
1656 | BT_STRING_ENCODING_NONE), | |
39d74371 | 1657 | "Reject invalid \"None\" string encoding"); |
50842bdc | 1658 | ok(bt_field_type_string_set_encoding(string_type, |
39d74371 JG |
1659 | 42), |
1660 | "Reject invalid string encoding"); | |
50842bdc PP |
1661 | ok(bt_field_type_string_set_encoding(string_type, |
1662 | BT_STRING_ENCODING_ASCII) == 0, | |
39d74371 JG |
1663 | "Set string encoding to ASCII"); |
1664 | ||
50842bdc PP |
1665 | ok(bt_field_type_string_get_encoding(NULL) == |
1666 | BT_STRING_ENCODING_UNKNOWN, | |
1667 | "bt_field_type_string_get_encoding handles NULL correctly"); | |
1668 | ok(bt_field_type_string_get_encoding(string_type) == | |
1669 | BT_STRING_ENCODING_ASCII, | |
1670 | "bt_field_type_string_get_encoding returns the correct value"); | |
1671 | ||
1672 | structure_seq_type = bt_field_type_structure_create(); | |
1673 | ok(bt_field_type_get_type_id(structure_seq_type) == | |
1674 | BT_FIELD_TYPE_ID_STRUCT, | |
1675 | "bt_field_type_get_type_id returns a correct value with a structure type"); | |
39d74371 | 1676 | ok(structure_seq_type, "Create a structure type"); |
50842bdc | 1677 | ok(bt_field_type_structure_add_field(structure_seq_type, |
39d74371 JG |
1678 | uint_8_type, "seq_len") == 0, |
1679 | "Add a uint8_t type to a structure"); | |
50842bdc | 1680 | ok(bt_field_type_structure_add_field(structure_seq_type, |
39d74371 JG |
1681 | sequence_type, "a_sequence") == 0, |
1682 | "Add a sequence type to a structure"); | |
7cfd41d6 | 1683 | |
50842bdc PP |
1684 | ok(bt_field_type_structure_get_field_count(NULL) < 0, |
1685 | "bt_field_type_structure_get_field_count handles NULL correctly"); | |
1686 | ok(bt_field_type_structure_get_field_count(structure_seq_type) == 2, | |
1687 | "bt_field_type_structure_get_field_count returns a correct value"); | |
7cfd41d6 JG |
1688 | |
1689 | ok(bt_ctf_field_type_structure_get_field(NULL, | |
1690 | &ret_string, &returned_type, 1) < 0, | |
50842bdc | 1691 | "bt_field_type_structure_get_field handles a NULL type correctly"); |
7cfd41d6 | 1692 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, |
f9b799fc | 1693 | NULL, &returned_type, 1) == 0, |
50842bdc | 1694 | "bt_field_type_structure_get_field handles a NULL name correctly"); |
83509119 | 1695 | bt_put(returned_type); |
7cfd41d6 | 1696 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, |
f9b799fc | 1697 | &ret_string, NULL, 1) == 0, |
50842bdc | 1698 | "bt_field_type_structure_get_field handles a NULL return type correctly"); |
7cfd41d6 JG |
1699 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, |
1700 | &ret_string, &returned_type, 10) < 0, | |
50842bdc | 1701 | "bt_field_type_structure_get_field handles an invalid index correctly"); |
7cfd41d6 JG |
1702 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, |
1703 | &ret_string, &returned_type, 1) == 0, | |
50842bdc | 1704 | "bt_field_type_structure_get_field returns a field"); |
7cfd41d6 | 1705 | ok(!strcmp(ret_string, "a_sequence"), |
50842bdc | 1706 | "bt_field_type_structure_get_field returns a correct field name"); |
7cfd41d6 | 1707 | ok(returned_type == sequence_type, |
50842bdc | 1708 | "bt_field_type_structure_get_field returns a correct field type"); |
83509119 | 1709 | bt_put(returned_type); |
7cfd41d6 | 1710 | |
50842bdc PP |
1711 | ok(bt_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL, |
1712 | "bt_field_type_structure_get_field_type_by_name handles a NULL structure correctly"); | |
1713 | ok(bt_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL, | |
1714 | "bt_field_type_structure_get_field_type_by_name handles a NULL field name correctly"); | |
1715 | returned_type = bt_field_type_structure_get_field_type_by_name( | |
7cfd41d6 JG |
1716 | structure_seq_type, "a_sequence"); |
1717 | ok(returned_type == sequence_type, | |
50842bdc | 1718 | "bt_field_type_structure_get_field_type_by_name returns the correct field type"); |
83509119 | 1719 | bt_put(returned_type); |
7cfd41d6 | 1720 | |
50842bdc PP |
1721 | composite_structure_type = bt_field_type_structure_create(); |
1722 | ok(bt_field_type_structure_add_field(composite_structure_type, | |
39d74371 JG |
1723 | string_type, "a_string") == 0, |
1724 | "Add a string type to a structure"); | |
50842bdc | 1725 | ok(bt_field_type_structure_add_field(composite_structure_type, |
39d74371 JG |
1726 | structure_seq_type, "inner_structure") == 0, |
1727 | "Add a structure type to a structure"); | |
1728 | ||
50842bdc | 1729 | ok(bt_field_type_structure_get_field_type_by_name( |
7cfd41d6 | 1730 | NULL, "a_sequence") == NULL, |
50842bdc PP |
1731 | "bt_field_type_structure_get_field_type_by_name handles a NULL field correctly"); |
1732 | ok(bt_field_type_structure_get_field_type_by_name( | |
7cfd41d6 | 1733 | structure_seq_type, NULL) == NULL, |
50842bdc PP |
1734 | "bt_field_type_structure_get_field_type_by_name handles a NULL field name correctly"); |
1735 | returned_type = bt_field_type_structure_get_field_type_by_name( | |
7cfd41d6 JG |
1736 | structure_seq_type, "a_sequence"); |
1737 | ok(returned_type == sequence_type, | |
50842bdc | 1738 | "bt_field_type_structure_get_field_type_by_name returns a correct type"); |
83509119 | 1739 | bt_put(returned_type); |
7cfd41d6 | 1740 | |
50842bdc | 1741 | int_16 = bt_field_create(int_16_type); |
39d74371 | 1742 | ok(int_16, "Instanciate a signed 16-bit integer"); |
50842bdc | 1743 | uint_12 = bt_field_create(uint_12_type); |
39d74371 | 1744 | ok(uint_12, "Instanciate an unsigned 12-bit integer"); |
50842bdc | 1745 | returned_type = bt_field_get_type(int_16); |
10817e06 | 1746 | ok(returned_type == int_16_type, |
50842bdc | 1747 | "bt_field_get_type returns the correct type"); |
39d74371 JG |
1748 | |
1749 | /* Can't modify types after instanciating them */ | |
50842bdc PP |
1750 | ok(bt_field_type_integer_set_base(uint_12_type, |
1751 | BT_INTEGER_BASE_DECIMAL), | |
39d74371 JG |
1752 | "Check an integer type' base can't be modified after instanciation"); |
1753 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0), | |
1754 | "Check an integer type's signedness can't be modified after instanciation"); | |
1755 | ||
39d74371 | 1756 | /* Check overflows are properly tested for */ |
50842bdc | 1757 | ok(bt_field_signed_integer_set_value(int_16, -32768) == 0, |
39d74371 | 1758 | "Check -32768 is allowed for a signed 16-bit integer"); |
50842bdc | 1759 | ok(bt_field_signed_integer_set_value(int_16, 32767) == 0, |
39d74371 | 1760 | "Check 32767 is allowed for a signed 16-bit integer"); |
50842bdc | 1761 | ok(bt_field_signed_integer_set_value(int_16, -42) == 0, |
39d74371 JG |
1762 | "Check -42 is allowed for a signed 16-bit integer"); |
1763 | ||
50842bdc | 1764 | ok(bt_field_unsigned_integer_set_value(uint_12, 4095) == 0, |
39d74371 | 1765 | "Check 4095 is allowed for an unsigned 12-bit integer"); |
50842bdc | 1766 | ok(bt_field_unsigned_integer_set_value(uint_12, 0) == 0, |
39d74371 JG |
1767 | "Check 0 is allowed for an unsigned 12-bit integer"); |
1768 | ||
50842bdc | 1769 | string = bt_field_create(string_type); |
39d74371 | 1770 | ok(string, "Instanciate a string field"); |
50842bdc | 1771 | ok(bt_field_string_set_value(string, "A value") == 0, |
39d74371 JG |
1772 | "Set a string's value"); |
1773 | ||
50842bdc | 1774 | enumeration_type = bt_field_type_enumeration_create(uint_12_type); |
0abce37e JG |
1775 | ok(enumeration_type, |
1776 | "Create an enumeration type with an unsigned 12-bit integer as container"); | |
0abce37e | 1777 | |
83509119 JG |
1778 | bt_put(string); |
1779 | bt_put(uint_12); | |
1780 | bt_put(int_16); | |
83509119 JG |
1781 | bt_put(composite_structure_type); |
1782 | bt_put(structure_seq_type); | |
1783 | bt_put(string_type); | |
1784 | bt_put(sequence_type); | |
1785 | bt_put(uint_8_type); | |
1786 | bt_put(int_16_type); | |
1787 | bt_put(uint_12_type); | |
1788 | bt_put(enumeration_type); | |
83509119 | 1789 | bt_put(returned_type); |
39d74371 JG |
1790 | } |
1791 | ||
a3d8579b | 1792 | static |
50842bdc PP |
1793 | void packet_resize_test(struct bt_stream_class *stream_class, |
1794 | struct bt_stream *stream, struct bt_ctf_clock *clock) | |
39d74371 JG |
1795 | { |
1796 | /* | |
1797 | * Append enough events to force the underlying packet to be resized. | |
1798 | * Also tests that a new event can be declared after a stream has been | |
1799 | * instantiated and used/flushed. | |
1800 | */ | |
1801 | int ret = 0; | |
1802 | int i; | |
50842bdc | 1803 | struct bt_event_class *event_class = bt_event_class_create( |
39d74371 | 1804 | "Spammy_Event"); |
50842bdc PP |
1805 | struct bt_field_type *integer_type = |
1806 | bt_field_type_integer_create(17); | |
1807 | struct bt_field_type *string_type = | |
1808 | bt_field_type_string_create(); | |
1809 | struct bt_event *event = NULL; | |
1810 | struct bt_field *ret_field = NULL; | |
1811 | struct bt_field_type *ret_field_type = NULL; | |
6809e227 | 1812 | uint64_t ret_uint64; |
12c8a1a3 | 1813 | int events_appended = 0; |
50842bdc | 1814 | struct bt_field *packet_context = NULL, |
5fd2e9fd | 1815 | *packet_context_field = NULL, *stream_event_context = NULL; |
50842bdc PP |
1816 | struct bt_field_type *ep_field_1_type = NULL; |
1817 | struct bt_field_type *ep_a_string_type = NULL; | |
1818 | struct bt_field_type *ep_type = NULL; | |
39d74371 | 1819 | |
50842bdc | 1820 | ret |= bt_event_class_add_field(event_class, integer_type, |
39d74371 | 1821 | "field_1"); |
50842bdc | 1822 | ret |= bt_event_class_add_field(event_class, string_type, |
39d74371 | 1823 | "a_string"); |
50842bdc | 1824 | ret |= bt_stream_class_add_event_class(stream_class, event_class); |
39d74371 JG |
1825 | ok(ret == 0, "Add a new event class to a stream class after writing an event"); |
1826 | if (ret) { | |
1827 | goto end; | |
1828 | } | |
1829 | ||
09840de5 | 1830 | /* |
50842bdc | 1831 | * bt_stream_class_add_event_class() copies the field types |
09840de5 PP |
1832 | * of event_class, so we retrieve the new ones to create the |
1833 | * appropriate fields. | |
1834 | */ | |
50842bdc | 1835 | ep_type = bt_event_class_get_payload_type(event_class); |
09840de5 | 1836 | assert(ep_type); |
50842bdc | 1837 | ep_field_1_type = bt_field_type_structure_get_field_type_by_name( |
09840de5 PP |
1838 | ep_type, "field_1"); |
1839 | assert(ep_field_1_type); | |
50842bdc | 1840 | ep_a_string_type = bt_field_type_structure_get_field_type_by_name( |
09840de5 PP |
1841 | ep_type, "a_string"); |
1842 | assert(ep_a_string_type); | |
1843 | ||
50842bdc PP |
1844 | event = bt_event_create(event_class); |
1845 | ret_field = bt_event_get_payload_by_index(event, 0); | |
1846 | ret_field_type = bt_field_get_type(ret_field); | |
1847 | ok(bt_field_type_compare(ret_field_type, integer_type) == 0, | |
1848 | "bt_event_get_payload_by_index returns a correct field"); | |
83509119 JG |
1849 | bt_put(ret_field_type); |
1850 | bt_put(ret_field); | |
83509119 | 1851 | bt_put(event); |
1ff9582c | 1852 | |
c2237f17 | 1853 | for (i = 0; i < packet_resize_test_length; i++) { |
50842bdc PP |
1854 | event = bt_event_create(event_class); |
1855 | struct bt_field *integer = | |
1856 | bt_field_create(ep_field_1_type); | |
1857 | struct bt_field *string = | |
1858 | bt_field_create(ep_a_string_type); | |
39d74371 JG |
1859 | |
1860 | ret |= bt_ctf_clock_set_time(clock, ++current_time); | |
50842bdc PP |
1861 | ret |= bt_field_unsigned_integer_set_value(integer, i); |
1862 | ret |= bt_event_set_payload(event, "field_1", | |
39d74371 | 1863 | integer); |
83509119 | 1864 | bt_put(integer); |
50842bdc PP |
1865 | ret |= bt_field_string_set_value(string, "This is a test"); |
1866 | ret |= bt_event_set_payload(event, "a_string", | |
39d74371 | 1867 | string); |
83509119 | 1868 | bt_put(string); |
6e1f8ea1 JG |
1869 | |
1870 | /* Populate stream event context */ | |
5fd2e9fd | 1871 | stream_event_context = |
50842bdc PP |
1872 | bt_event_get_stream_event_context(event); |
1873 | integer = bt_field_structure_get_field_by_name(stream_event_context, | |
6e1f8ea1 | 1874 | "common_event_context"); |
5fd2e9fd | 1875 | BT_PUT(stream_event_context); |
50842bdc | 1876 | ret |= bt_field_unsigned_integer_set_value(integer, |
6e1f8ea1 | 1877 | i % 42); |
83509119 | 1878 | bt_put(integer); |
6e1f8ea1 | 1879 | |
50842bdc | 1880 | ret |= bt_stream_append_event(stream, event); |
83509119 | 1881 | bt_put(event); |
39d74371 JG |
1882 | |
1883 | if (ret) { | |
1884 | break; | |
1885 | } | |
1886 | } | |
12c8a1a3 | 1887 | |
c2237f17 | 1888 | events_appended = !!(i == packet_resize_test_length); |
50842bdc | 1889 | ret = bt_stream_get_discarded_events_count(stream, &ret_uint64); |
6809e227 | 1890 | ok(ret == 0 && ret_uint64 == 0, |
50842bdc PP |
1891 | "bt_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded"); |
1892 | bt_stream_append_discarded_events(stream, 1000); | |
1893 | ret = bt_stream_get_discarded_events_count(stream, &ret_uint64); | |
6809e227 | 1894 | ok(ret == 0 && ret_uint64 == 1000, |
50842bdc | 1895 | "bt_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded"); |
6809e227 | 1896 | |
39d74371 | 1897 | end: |
12c8a1a3 JG |
1898 | ok(events_appended, "Append 100 000 events to a stream"); |
1899 | ||
1900 | /* | |
1901 | * Populate the custom packet context field with a dummy value | |
1902 | * otherwise flush will fail. | |
1903 | */ | |
50842bdc PP |
1904 | packet_context = bt_stream_get_packet_context(stream); |
1905 | packet_context_field = bt_field_structure_get_field_by_name(packet_context, | |
35e8709f | 1906 | "custom_packet_context_field"); |
50842bdc | 1907 | bt_field_unsigned_integer_set_value(packet_context_field, 2); |
12c8a1a3 | 1908 | |
50842bdc | 1909 | ok(bt_stream_flush(stream) == 0, |
39d74371 | 1910 | "Flush a stream that forces a packet resize"); |
50842bdc | 1911 | ret = bt_stream_get_discarded_events_count(stream, &ret_uint64); |
6809e227 | 1912 | ok(ret == 0 && ret_uint64 == 1000, |
50842bdc | 1913 | "bt_stream_get_discarded_events_count returns a correct number of discarded events after a flush"); |
83509119 JG |
1914 | bt_put(integer_type); |
1915 | bt_put(string_type); | |
1916 | bt_put(packet_context); | |
1917 | bt_put(packet_context_field); | |
5fd2e9fd | 1918 | bt_put(stream_event_context); |
83509119 | 1919 | bt_put(event_class); |
09840de5 PP |
1920 | bt_put(ep_field_1_type); |
1921 | bt_put(ep_a_string_type); | |
1922 | bt_put(ep_type); | |
39d74371 JG |
1923 | } |
1924 | ||
a3d8579b | 1925 | static |
fdf80f32 JG |
1926 | void test_empty_stream(struct bt_ctf_writer *writer) |
1927 | { | |
1928 | int ret = 0; | |
50842bdc PP |
1929 | struct bt_trace *trace = NULL, *ret_trace = NULL; |
1930 | struct bt_stream_class *stream_class = NULL; | |
1931 | struct bt_stream *stream = NULL; | |
fdf80f32 JG |
1932 | |
1933 | trace = bt_ctf_writer_get_trace(writer); | |
1934 | if (!trace) { | |
1935 | diag("Failed to get trace from writer"); | |
1936 | ret = -1; | |
1937 | goto end; | |
1938 | } | |
1939 | ||
50842bdc | 1940 | stream_class = bt_stream_class_create("empty_stream"); |
fdf80f32 JG |
1941 | if (!stream_class) { |
1942 | diag("Failed to create stream class"); | |
1943 | ret = -1; | |
1944 | goto end; | |
1945 | } | |
1946 | ||
50842bdc | 1947 | ret = bt_stream_class_set_packet_context_type(stream_class, NULL); |
e011d2c1 | 1948 | assert(ret == 0); |
50842bdc | 1949 | ret = bt_stream_class_set_event_header_type(stream_class, NULL); |
e011d2c1 PP |
1950 | assert(ret == 0); |
1951 | ||
50842bdc PP |
1952 | ok(bt_stream_class_get_trace(NULL) == NULL, |
1953 | "bt_stream_class_get_trace handles NULL correctly"); | |
1954 | ok(bt_stream_class_get_trace(stream_class) == NULL, | |
1955 | "bt_stream_class_get_trace returns NULL when stream class is orphaned"); | |
9b068522 | 1956 | |
fdf80f32 JG |
1957 | stream = bt_ctf_writer_create_stream(writer, stream_class); |
1958 | if (!stream) { | |
1959 | diag("Failed to create writer stream"); | |
1960 | ret = -1; | |
1961 | goto end; | |
1962 | } | |
9b068522 | 1963 | |
50842bdc | 1964 | ret_trace = bt_stream_class_get_trace(stream_class); |
9b068522 | 1965 | ok(ret_trace == trace, |
50842bdc | 1966 | "bt_stream_class_get_trace returns the correct trace after a stream has been created"); |
fdf80f32 JG |
1967 | end: |
1968 | ok(ret == 0, | |
1969 | "Created a stream class with default attributes and an empty stream"); | |
83509119 JG |
1970 | bt_put(trace); |
1971 | bt_put(ret_trace); | |
1972 | bt_put(stream); | |
1973 | bt_put(stream_class); | |
fdf80f32 JG |
1974 | } |
1975 | ||
a3d8579b | 1976 | static |
ac0c6bdd PP |
1977 | void test_custom_event_header_stream(struct bt_ctf_writer *writer, |
1978 | struct bt_ctf_clock *clock) | |
29be776a JG |
1979 | { |
1980 | int i, ret; | |
50842bdc PP |
1981 | struct bt_stream_class *stream_class = NULL; |
1982 | struct bt_stream *stream = NULL; | |
1983 | struct bt_field_type *integer_type = NULL, | |
29be776a | 1984 | *sequence_type = NULL, *event_header_type = NULL; |
50842bdc | 1985 | struct bt_field *integer = NULL, *sequence = NULL, |
29be776a | 1986 | *event_header = NULL, *packet_header = NULL; |
50842bdc PP |
1987 | struct bt_event_class *event_class = NULL; |
1988 | struct bt_event *event = NULL; | |
29be776a | 1989 | |
50842bdc | 1990 | stream_class = bt_stream_class_create("custom_event_header_stream"); |
29be776a JG |
1991 | if (!stream_class) { |
1992 | fail("Failed to create stream class"); | |
1993 | goto end; | |
1994 | } | |
1995 | ||
50842bdc | 1996 | ret = bt_stream_class_set_clock(stream_class, clock); |
29be776a JG |
1997 | if (ret) { |
1998 | fail("Failed to set stream class clock"); | |
1999 | goto end; | |
2000 | } | |
2001 | ||
2002 | /* | |
2003 | * Customize event header to add an "seq_len" integer member | |
2004 | * which will be used as the length of a sequence in an event of this | |
2005 | * stream. | |
2006 | */ | |
50842bdc | 2007 | event_header_type = bt_stream_class_get_event_header_type( |
29be776a JG |
2008 | stream_class); |
2009 | if (!event_header_type) { | |
2010 | fail("Failed to get event header type"); | |
2011 | goto end; | |
2012 | } | |
2013 | ||
50842bdc | 2014 | integer_type = bt_field_type_integer_create(13); |
29be776a JG |
2015 | if (!integer_type) { |
2016 | fail("Failed to create length integer type"); | |
2017 | goto end; | |
2018 | } | |
2019 | ||
50842bdc | 2020 | ret = bt_field_type_structure_add_field(event_header_type, |
29be776a JG |
2021 | integer_type, "seq_len"); |
2022 | if (ret) { | |
2023 | fail("Failed to add a new field to stream event header"); | |
2024 | goto end; | |
2025 | } | |
2026 | ||
50842bdc | 2027 | event_class = bt_event_class_create("sequence_event"); |
29be776a JG |
2028 | if (!event_class) { |
2029 | fail("Failed to create event class"); | |
2030 | goto end; | |
2031 | } | |
2032 | ||
2033 | /* | |
2034 | * This event's payload will contain a sequence which references | |
2035 | * stream.event.header.seq_len as its length field. | |
2036 | */ | |
50842bdc | 2037 | sequence_type = bt_field_type_sequence_create(integer_type, |
29be776a JG |
2038 | "stream.event.header.seq_len"); |
2039 | if (!sequence_type) { | |
2040 | fail("Failed to create a sequence"); | |
2041 | goto end; | |
2042 | } | |
2043 | ||
50842bdc | 2044 | ret = bt_event_class_add_field(event_class, sequence_type, |
29be776a JG |
2045 | "some_sequence"); |
2046 | if (ret) { | |
2047 | fail("Failed to add a sequence to an event class"); | |
2048 | goto end; | |
2049 | } | |
2050 | ||
50842bdc | 2051 | ret = bt_stream_class_add_event_class(stream_class, event_class); |
29be776a JG |
2052 | if (ret) { |
2053 | fail("Failed to add event class to stream class"); | |
2054 | goto end; | |
2055 | } | |
2056 | ||
2057 | stream = bt_ctf_writer_create_stream(writer, stream_class); | |
2058 | if (!stream) { | |
2059 | fail("Failed to create stream") | |
2060 | goto end; | |
2061 | } | |
2062 | ||
2063 | /* | |
2064 | * We have defined a custom packet header field. We have to populate it | |
2065 | * explicitly. | |
2066 | */ | |
50842bdc | 2067 | packet_header = bt_stream_get_packet_header(stream); |
29be776a JG |
2068 | if (!packet_header) { |
2069 | fail("Failed to get stream packet header"); | |
2070 | goto end; | |
2071 | } | |
2072 | ||
50842bdc | 2073 | integer = bt_field_structure_get_field_by_name(packet_header, |
29be776a JG |
2074 | "custom_trace_packet_header_field"); |
2075 | if (!integer) { | |
2076 | fail("Failed to retrieve custom_trace_packet_header_field"); | |
2077 | goto end; | |
2078 | } | |
2079 | ||
50842bdc | 2080 | ret = bt_field_unsigned_integer_set_value(integer, 3487); |
29be776a JG |
2081 | if (ret) { |
2082 | fail("Failed to set custom_trace_packet_header_field value"); | |
2083 | goto end; | |
2084 | } | |
83509119 | 2085 | bt_put(integer); |
29be776a | 2086 | |
50842bdc | 2087 | event = bt_event_create(event_class); |
29be776a JG |
2088 | if (!event) { |
2089 | fail("Failed to create event"); | |
2090 | goto end; | |
2091 | } | |
2092 | ||
50842bdc | 2093 | event_header = bt_event_get_header(event); |
29be776a JG |
2094 | if (!event_header) { |
2095 | fail("Failed to get event header"); | |
2096 | goto end; | |
2097 | } | |
2098 | ||
50842bdc | 2099 | integer = bt_field_structure_get_field_by_name(event_header, |
29be776a JG |
2100 | "seq_len"); |
2101 | if (!integer) { | |
2102 | fail("Failed to get seq_len field from event header"); | |
2103 | goto end; | |
2104 | } | |
2105 | ||
50842bdc | 2106 | ret = bt_field_unsigned_integer_set_value(integer, 2); |
29be776a JG |
2107 | if (ret) { |
2108 | fail("Failed to set seq_len value in event header"); | |
2109 | goto end; | |
2110 | } | |
2111 | ||
2112 | /* Populate both sequence integer fields */ | |
50842bdc | 2113 | sequence = bt_event_get_payload(event, "some_sequence"); |
29be776a JG |
2114 | if (!sequence) { |
2115 | fail("Failed to retrieve sequence from event"); | |
2116 | goto end; | |
2117 | } | |
2118 | ||
50842bdc | 2119 | ret = bt_field_sequence_set_length(sequence, integer); |
29be776a JG |
2120 | if (ret) { |
2121 | fail("Failed to set sequence length"); | |
2122 | goto end; | |
2123 | } | |
83509119 | 2124 | bt_put(integer); |
29be776a JG |
2125 | |
2126 | for (i = 0; i < 2; i++) { | |
50842bdc | 2127 | integer = bt_field_sequence_get_field(sequence, i); |
29be776a JG |
2128 | if (ret) { |
2129 | fail("Failed to retrieve sequence element"); | |
2130 | goto end; | |
2131 | } | |
2132 | ||
50842bdc | 2133 | ret = bt_field_unsigned_integer_set_value(integer, i); |
29be776a JG |
2134 | if (ret) { |
2135 | fail("Failed to set sequence element value"); | |
2136 | goto end; | |
2137 | } | |
2138 | ||
83509119 | 2139 | bt_put(integer); |
29be776a JG |
2140 | integer = NULL; |
2141 | } | |
2142 | ||
50842bdc | 2143 | ret = bt_stream_append_event(stream, event); |
29be776a JG |
2144 | if (ret) { |
2145 | fail("Failed to append event to stream"); | |
2146 | goto end; | |
2147 | } | |
2148 | ||
50842bdc | 2149 | ret = bt_stream_flush(stream); |
29be776a JG |
2150 | if (ret) { |
2151 | fail("Failed to flush custom_event_header stream"); | |
2152 | } | |
2153 | end: | |
83509119 JG |
2154 | bt_put(stream); |
2155 | bt_put(stream_class); | |
2156 | bt_put(event_class); | |
2157 | bt_put(event); | |
2158 | bt_put(integer); | |
2159 | bt_put(sequence); | |
2160 | bt_put(event_header); | |
2161 | bt_put(packet_header); | |
2162 | bt_put(sequence_type); | |
2163 | bt_put(integer_type); | |
2164 | bt_put(event_header_type); | |
29be776a JG |
2165 | } |
2166 | ||
a3d8579b | 2167 | static |
ac0c6bdd PP |
2168 | void test_instanciate_event_before_stream(struct bt_ctf_writer *writer, |
2169 | struct bt_ctf_clock *clock) | |
42f45a8d JG |
2170 | { |
2171 | int ret = 0; | |
50842bdc PP |
2172 | struct bt_stream_class *stream_class = NULL; |
2173 | struct bt_stream *stream = NULL, | |
2fb29fdc | 2174 | *ret_stream = NULL; |
50842bdc PP |
2175 | struct bt_event_class *event_class = NULL; |
2176 | struct bt_event *event = NULL; | |
2177 | struct bt_field_type *integer_type = NULL; | |
2178 | struct bt_field *integer = NULL; | |
42f45a8d | 2179 | |
50842bdc | 2180 | stream_class = bt_stream_class_create("event_before_stream_test"); |
42f45a8d JG |
2181 | if (!stream_class) { |
2182 | diag("Failed to create stream class"); | |
2183 | ret = -1; | |
2184 | goto end; | |
2185 | } | |
2186 | ||
50842bdc | 2187 | ret = bt_stream_class_set_clock(stream_class, clock); |
42f45a8d JG |
2188 | if (ret) { |
2189 | diag("Failed to set stream class clock"); | |
2190 | goto end; | |
2191 | } | |
2192 | ||
50842bdc PP |
2193 | event_class = bt_event_class_create("some_event_class_name"); |
2194 | integer_type = bt_field_type_integer_create(32); | |
42f45a8d JG |
2195 | if (!integer_type) { |
2196 | diag("Failed to create integer field type"); | |
2197 | ret = -1; | |
2198 | goto end; | |
2199 | } | |
2200 | ||
50842bdc | 2201 | ret = bt_event_class_add_field(event_class, integer_type, |
42f45a8d JG |
2202 | "integer_field"); |
2203 | if (ret) { | |
2204 | diag("Failed to add field to event class"); | |
2205 | goto end; | |
2206 | } | |
2207 | ||
50842bdc | 2208 | ret = bt_stream_class_add_event_class(stream_class, |
42f45a8d JG |
2209 | event_class); |
2210 | if (ret) { | |
2211 | diag("Failed to add event class to stream class"); | |
2212 | } | |
2213 | ||
50842bdc | 2214 | event = bt_event_create(event_class); |
42f45a8d JG |
2215 | if (!event) { |
2216 | diag("Failed to create event"); | |
2217 | ret = -1; | |
2218 | goto end; | |
2219 | } | |
2220 | ||
50842bdc | 2221 | integer = bt_event_get_payload_by_index(event, 0); |
42f45a8d JG |
2222 | if (!integer) { |
2223 | diag("Failed to get integer field payload from event"); | |
2224 | ret = -1; | |
2225 | goto end; | |
2226 | } | |
2227 | ||
50842bdc | 2228 | ret = bt_field_unsigned_integer_set_value(integer, 1234); |
42f45a8d JG |
2229 | if (ret) { |
2230 | diag("Failed to set integer field value"); | |
2231 | goto end; | |
2232 | } | |
2233 | ||
2234 | stream = bt_ctf_writer_create_stream(writer, stream_class); | |
2235 | if (!stream) { | |
2236 | diag("Failed to create writer stream"); | |
2237 | ret = -1; | |
2238 | goto end; | |
2239 | } | |
2240 | ||
50842bdc | 2241 | ret = bt_stream_append_event(stream, event); |
42f45a8d JG |
2242 | if (ret) { |
2243 | diag("Failed to append event to stream"); | |
2244 | goto end; | |
2245 | } | |
2fb29fdc | 2246 | |
50842bdc | 2247 | ret_stream = bt_event_get_stream(event); |
2fb29fdc | 2248 | ok(ret_stream == stream, |
50842bdc | 2249 | "bt_event_get_stream returns an event's stream after it has been appended"); |
42f45a8d JG |
2250 | end: |
2251 | ok(ret == 0, | |
2252 | "Create an event before instanciating its associated stream"); | |
83509119 JG |
2253 | bt_put(stream); |
2254 | bt_put(ret_stream); | |
2255 | bt_put(stream_class); | |
2256 | bt_put(event_class); | |
2257 | bt_put(event); | |
2258 | bt_put(integer_type); | |
2259 | bt_put(integer); | |
42f45a8d JG |
2260 | } |
2261 | ||
a3d8579b | 2262 | static |
50842bdc | 2263 | void append_existing_event_class(struct bt_stream_class *stream_class) |
f60fde63 | 2264 | { |
50842bdc | 2265 | struct bt_event_class *event_class; |
f60fde63 | 2266 | |
50842bdc | 2267 | event_class = bt_event_class_create("Simple Event"); |
82faa54a | 2268 | assert(event_class); |
50842bdc | 2269 | ok(bt_stream_class_add_event_class(stream_class, event_class) == 0, |
a9f0d01b | 2270 | "two event classes with the same name may cohabit within the same stream class"); |
83509119 | 2271 | bt_put(event_class); |
f60fde63 | 2272 | |
50842bdc | 2273 | event_class = bt_event_class_create("different name, ok"); |
06a0c632 | 2274 | assert(event_class); |
50842bdc PP |
2275 | assert(!bt_event_class_set_id(event_class, 13)); |
2276 | ok(bt_stream_class_add_event_class(stream_class, event_class), | |
f60fde63 | 2277 | "two event classes with the same ID cannot cohabit within the same stream class"); |
83509119 | 2278 | bt_put(event_class); |
f60fde63 PP |
2279 | } |
2280 | ||
44ac03eb | 2281 | static |
50842bdc | 2282 | struct bt_event_class *create_minimal_event_class(void) |
44ac03eb | 2283 | { |
50842bdc PP |
2284 | struct bt_event_class *ec = NULL; |
2285 | struct bt_field_type *int_ft = NULL; | |
44ac03eb PP |
2286 | int ret; |
2287 | ||
50842bdc | 2288 | int_ft = bt_field_type_integer_create(23); |
44ac03eb | 2289 | assert(int_ft); |
50842bdc | 2290 | ec = bt_event_class_create("minimal"); |
44ac03eb | 2291 | assert(ec); |
50842bdc | 2292 | ret = bt_event_class_add_field(ec, int_ft, "field"); |
44ac03eb PP |
2293 | assert(!ret); |
2294 | BT_PUT(int_ft); | |
2295 | ||
2296 | return ec; | |
2297 | } | |
2298 | ||
2299 | static | |
bcd3a967 | 2300 | void test_create_writer_vs_non_writer_mode(void) |
44ac03eb PP |
2301 | { |
2302 | int ret; | |
32bd47d1 | 2303 | gchar *trace_path; |
b71d7298 | 2304 | const char *writer_stream_name = "writer stream instance"; |
44ac03eb | 2305 | struct bt_ctf_writer *writer = NULL; |
50842bdc PP |
2306 | struct bt_trace *writer_trace = NULL; |
2307 | struct bt_stream_class *writer_sc = NULL; | |
2308 | struct bt_stream *writer_stream = NULL; | |
2309 | struct bt_stream *writer_stream2 = NULL; | |
2310 | struct bt_stream *packet_stream = NULL; | |
2311 | struct bt_trace *non_writer_trace = NULL; | |
2312 | struct bt_stream_class *non_writer_sc = NULL; | |
2313 | struct bt_stream *non_writer_stream = NULL; | |
2314 | struct bt_stream *non_writer_stream2 = NULL; | |
2315 | struct bt_event_class *writer_ec = NULL; | |
2316 | struct bt_event_class *non_writer_ec = NULL; | |
2317 | struct bt_event *event = NULL; | |
2318 | struct bt_event *event2 = NULL; | |
2319 | struct bt_field_type *empty_struct_ft = NULL; | |
2320 | struct bt_field *int_field = NULL; | |
54e27fc1 | 2321 | struct bt_ctf_clock *writer_clock = NULL; |
50842bdc PP |
2322 | struct bt_clock_class *non_writer_clock_class = NULL; |
2323 | struct bt_packet *packet = NULL; | |
2324 | struct bt_packet *packet2 = NULL; | |
44ac03eb | 2325 | |
32bd47d1 | 2326 | trace_path = g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL); |
44ac03eb PP |
2327 | if (!bt_mkdtemp(trace_path)) { |
2328 | perror("# perror"); | |
2329 | } | |
2330 | ||
2331 | /* Create empty structure field type (event header) */ | |
50842bdc | 2332 | empty_struct_ft = bt_field_type_structure_create(); |
44ac03eb PP |
2333 | assert(empty_struct_ft); |
2334 | ||
54e27fc1 | 2335 | /* Create writer, writer stream class, stream, and clock */ |
44ac03eb PP |
2336 | writer = bt_ctf_writer_create(trace_path); |
2337 | assert(writer); | |
e011d2c1 PP |
2338 | writer_clock = bt_ctf_clock_create("writer_clock"); |
2339 | assert(writer_clock); | |
2340 | ret = bt_ctf_writer_add_clock(writer, writer_clock); | |
2341 | assert(!ret); | |
50842bdc | 2342 | ret = bt_ctf_writer_set_byte_order(writer, BT_BYTE_ORDER_LITTLE_ENDIAN); |
dc3fffef | 2343 | assert(!ret); |
44ac03eb PP |
2344 | writer_trace = bt_ctf_writer_get_trace(writer); |
2345 | ok(writer_trace, "bt_ctf_writer_get_trace() returns a trace"); | |
50842bdc | 2346 | writer_sc = bt_stream_class_create("writer_sc"); |
44ac03eb | 2347 | assert(writer_sc); |
50842bdc | 2348 | ret = bt_stream_class_set_event_header_type(writer_sc, |
44ac03eb PP |
2349 | empty_struct_ft); |
2350 | assert(!ret); | |
50842bdc | 2351 | ret = bt_stream_class_set_clock(writer_sc, writer_clock); |
e011d2c1 | 2352 | assert(!ret); |
50842bdc | 2353 | ret = bt_trace_add_stream_class(writer_trace, writer_sc); |
44ac03eb | 2354 | assert(!ret); |
50842bdc | 2355 | writer_stream = bt_stream_create(writer_sc, writer_stream_name); |
44ac03eb | 2356 | assert(writer_stream); |
50842bdc PP |
2357 | ok(!strcmp(bt_stream_get_name(writer_stream), writer_stream_name), |
2358 | "bt_stream_get_name() returns the stream's name"); | |
44ac03eb | 2359 | |
54e27fc1 | 2360 | /* Create non-writer trace, stream class, stream, and clock */ |
50842bdc | 2361 | non_writer_trace = bt_trace_create(); |
44ac03eb | 2362 | assert(non_writer_trace); |
50842bdc | 2363 | non_writer_sc = bt_stream_class_create("nonwriter_sc"); |
44ac03eb | 2364 | assert(non_writer_sc); |
50842bdc | 2365 | ret = bt_stream_class_set_event_header_type(non_writer_sc, |
44ac03eb PP |
2366 | empty_struct_ft); |
2367 | assert(!ret); | |
50842bdc | 2368 | ret = bt_stream_class_set_packet_context_type(non_writer_sc, NULL); |
e011d2c1 | 2369 | assert(!ret); |
50842bdc | 2370 | ret = bt_trace_add_stream_class(non_writer_trace, non_writer_sc); |
44ac03eb | 2371 | assert(!ret); |
50842bdc | 2372 | non_writer_stream = bt_stream_create(non_writer_sc, NULL); |
44ac03eb | 2373 | assert(non_writer_stream); |
ac0c6bdd | 2374 | non_writer_clock_class = |
50842bdc | 2375 | bt_clock_class_create("non_writer_clock_class", |
f3534905 | 2376 | 1000000000); |
ac0c6bdd | 2377 | assert(non_writer_clock_class); |
50842bdc | 2378 | ret = bt_trace_add_clock_class(non_writer_trace, |
ac0c6bdd | 2379 | non_writer_clock_class); |
54e27fc1 | 2380 | assert(!ret); |
44ac03eb PP |
2381 | |
2382 | /* Create event class and event */ | |
bcd3a967 PP |
2383 | writer_ec = create_minimal_event_class(); |
2384 | assert(writer_ec); | |
50842bdc | 2385 | ret = bt_stream_class_add_event_class(writer_sc, writer_ec); |
44ac03eb | 2386 | assert(!ret); |
50842bdc | 2387 | event = bt_event_create(writer_ec); |
44ac03eb | 2388 | assert(event); |
50842bdc | 2389 | int_field = bt_event_get_payload_by_index(event, 0); |
44ac03eb | 2390 | assert(int_field); |
50842bdc | 2391 | bt_field_unsigned_integer_set_value(int_field, 17); |
44ac03eb PP |
2392 | |
2393 | /* | |
2394 | * Verify non-writer stream: it should be impossible to append | |
2395 | * an event to it. | |
2396 | */ | |
50842bdc PP |
2397 | ok(bt_stream_append_event(non_writer_stream, event), |
2398 | "bt_stream_append_event() fails with a non-writer stream"); | |
44ac03eb PP |
2399 | |
2400 | /* | |
2401 | * Verify writer stream: it should be possible to append an | |
2402 | * event to it. | |
2403 | */ | |
50842bdc PP |
2404 | ok(!bt_stream_append_event(writer_stream, event), |
2405 | "bt_stream_append_event() succeeds with a writer stream"); | |
44ac03eb | 2406 | |
bcd3a967 PP |
2407 | /* |
2408 | * It should be possible to create a packet from a non-writer | |
2409 | * stream, but not from a writer stream. | |
2410 | */ | |
50842bdc PP |
2411 | packet = bt_packet_create(non_writer_stream); |
2412 | ok(packet, "bt_packet_create() succeeds with a non-writer stream"); | |
2413 | packet_stream = bt_packet_get_stream(packet); | |
bcd3a967 | 2414 | ok(packet_stream == non_writer_stream, |
50842bdc | 2415 | "bt_packet_get_stream() returns the correct stream"); |
bcd3a967 PP |
2416 | |
2417 | /* | |
2418 | * It should not be possible to append an event associated to | |
2419 | * a stream to a different stream. | |
2420 | */ | |
50842bdc | 2421 | writer_stream2 = bt_stream_create(writer_sc, "zoo"); |
bcd3a967 | 2422 | assert(writer_stream2); |
bcd3a967 PP |
2423 | |
2424 | /* | |
2425 | * It should be possible to set the packet of a fresh event, as | |
2426 | * long as the originating stream classes are the same. | |
2427 | */ | |
50842bdc | 2428 | event2 = bt_event_create(writer_ec); |
bcd3a967 | 2429 | assert(event2); |
bcd3a967 PP |
2430 | non_writer_ec = create_minimal_event_class(); |
2431 | assert(non_writer_ec); | |
50842bdc | 2432 | ret = bt_stream_class_add_event_class(non_writer_sc, non_writer_ec); |
bcd3a967 PP |
2433 | assert(!ret); |
2434 | BT_PUT(event2); | |
50842bdc | 2435 | event2 = bt_event_create(non_writer_ec); |
bcd3a967 | 2436 | assert(event2); |
50842bdc PP |
2437 | ok(!bt_event_set_packet(event2, packet), |
2438 | "bt_event_set_packet() succeeds when the event's and the packet's stream class are the same"); | |
bcd3a967 PP |
2439 | |
2440 | /* | |
2441 | * It should be possible to set a packet created from the same | |
2442 | * stream to an event with an existing packet. | |
2443 | */ | |
50842bdc | 2444 | packet2 = bt_packet_create(non_writer_stream); |
bcd3a967 | 2445 | assert(packet2); |
50842bdc PP |
2446 | ok(!bt_event_set_packet(event2, packet2), |
2447 | "bt_event_set_packet() succeeds when the event's current packet has the same stream"); | |
bcd3a967 PP |
2448 | BT_PUT(packet2); |
2449 | ||
2450 | /* | |
2451 | * It should not be possible to set a packet created from a | |
2452 | * different stream to an event with an existing packet. | |
2453 | */ | |
50842bdc | 2454 | non_writer_stream2 = bt_stream_create(non_writer_sc, "rj45"); |
bcd3a967 | 2455 | assert(non_writer_stream2); |
50842bdc | 2456 | packet2 = bt_packet_create(non_writer_stream); |
bcd3a967 | 2457 | assert(packet2); |
bcd3a967 PP |
2458 | |
2459 | bt_put(writer); | |
2460 | bt_put(writer_trace); | |
2461 | bt_put(writer_sc); | |
2462 | bt_put(writer_stream); | |
2463 | bt_put(writer_stream2); | |
2464 | bt_put(non_writer_trace); | |
2465 | bt_put(non_writer_sc); | |
2466 | bt_put(non_writer_stream); | |
2467 | bt_put(non_writer_stream2); | |
2468 | bt_put(packet_stream); | |
2469 | bt_put(writer_ec); | |
2470 | bt_put(non_writer_ec); | |
2471 | bt_put(event); | |
2472 | bt_put(event2); | |
2473 | bt_put(int_field); | |
2474 | bt_put(empty_struct_ft); | |
2475 | bt_put(writer_clock); | |
ac0c6bdd | 2476 | bt_put(non_writer_clock_class); |
bcd3a967 PP |
2477 | bt_put(packet); |
2478 | bt_put(packet2); | |
851299b9 | 2479 | recursive_rmdir(trace_path); |
32bd47d1 | 2480 | g_free(trace_path); |
44ac03eb PP |
2481 | } |
2482 | ||
a3d8579b | 2483 | static |
44e0165b PP |
2484 | void test_clock_utils(void) |
2485 | { | |
2486 | int ret; | |
2487 | struct bt_ctf_clock *clock = NULL; | |
2488 | ||
2489 | clock = bt_ctf_clock_create("water"); | |
2490 | assert(clock); | |
2491 | ret = bt_ctf_clock_set_offset_s(clock, 1234); | |
72995028 | 2492 | assert(!ret); |
44e0165b PP |
2493 | ret = bt_ctf_clock_set_offset(clock, 1000); |
2494 | assert(!ret); | |
2495 | ret = bt_ctf_clock_set_frequency(clock, 1000000000); | |
2496 | assert(!ret); | |
44e0165b PP |
2497 | ret = bt_ctf_clock_set_frequency(clock, 1534); |
2498 | assert(!ret); | |
44e0165b PP |
2499 | |
2500 | BT_PUT(clock); | |
2501 | } | |
2502 | ||
ac0c6bdd PP |
2503 | void test_set_clock_non_writer_stream_class(void) |
2504 | { | |
2505 | struct bt_ctf_clock *clock; | |
50842bdc PP |
2506 | struct bt_trace *trace; |
2507 | struct bt_stream_class *sc; | |
ac0c6bdd PP |
2508 | int ret; |
2509 | ||
2510 | clock = bt_ctf_clock_create("the_clock"); | |
2511 | assert(clock); | |
2512 | ||
50842bdc | 2513 | trace = bt_trace_create(); |
ac0c6bdd PP |
2514 | assert(trace); |
2515 | ||
50842bdc | 2516 | sc = bt_stream_class_create(NULL); |
ac0c6bdd PP |
2517 | assert(sc); |
2518 | ||
50842bdc | 2519 | ret = bt_stream_class_set_clock(sc, clock); |
ac0c6bdd PP |
2520 | assert(ret == 0); |
2521 | ||
50842bdc | 2522 | ret = bt_trace_add_stream_class(trace, sc); |
ac0c6bdd | 2523 | ok(ret < 0, |
50842bdc | 2524 | "bt_trace_add_stream_class() fails with a stream class with a registered clock"); |
ac0c6bdd PP |
2525 | |
2526 | bt_put(clock); | |
2527 | bt_put(trace); | |
2528 | bt_put(sc); | |
2529 | } | |
2530 | ||
28437b95 PP |
2531 | static |
2532 | void test_static_trace(void) | |
2533 | { | |
50842bdc PP |
2534 | struct bt_trace *trace; |
2535 | struct bt_stream_class *stream_class; | |
2536 | struct bt_stream_class *stream_class2; | |
2537 | struct bt_stream *stream; | |
2538 | struct bt_clock_class *clock_class; | |
28437b95 PP |
2539 | int ret; |
2540 | ||
50842bdc | 2541 | trace = bt_trace_create(); |
28437b95 | 2542 | assert(trace); |
50842bdc | 2543 | stream_class = bt_stream_class_create(NULL); |
28437b95 | 2544 | assert(stream_class); |
50842bdc | 2545 | ret = bt_stream_class_set_packet_context_type(stream_class, NULL); |
e011d2c1 | 2546 | assert(ret == 0); |
50842bdc | 2547 | ret = bt_trace_add_stream_class(trace, stream_class); |
28437b95 | 2548 | assert(ret == 0); |
50842bdc PP |
2549 | stream = bt_stream_create(stream_class, "hello"); |
2550 | ok(stream, "bt_stream_create() succeeds with a non-static trace"); | |
28437b95 | 2551 | bt_put(stream); |
50842bdc PP |
2552 | ok(!bt_trace_is_static(trace), |
2553 | "bt_trace_is_static() returns the expected value"); | |
2554 | ok(bt_trace_set_is_static(trace) == 0, | |
2555 | "bt_trace_set_is_static() succeeds"); | |
2556 | ok(bt_trace_is_static(trace), | |
2557 | "bt_trace_is_static() returns the expected value"); | |
2558 | clock_class = bt_clock_class_create("yes", 1000000000); | |
28437b95 | 2559 | assert(clock_class); |
50842bdc | 2560 | stream_class2 = bt_stream_class_create(NULL); |
28437b95 | 2561 | assert(stream_class2); |
50842bdc PP |
2562 | ok(bt_trace_add_stream_class(trace, stream_class2), |
2563 | "bt_trace_add_stream_class() fails with a static trace"); | |
2564 | ok(bt_trace_add_clock_class(trace, clock_class), | |
2565 | "bt_trace_add_clock_class() fails with a static trace"); | |
2566 | ok(!bt_stream_create(stream_class, "hello2"), | |
2567 | "bt_stream_create() fails with a static trace"); | |
28437b95 PP |
2568 | |
2569 | bt_put(trace); | |
2570 | bt_put(stream_class); | |
2571 | bt_put(stream_class2); | |
2572 | bt_put(clock_class); | |
2573 | } | |
2574 | ||
d40a81d5 | 2575 | static |
50842bdc | 2576 | void trace_is_static_listener(struct bt_trace *trace, void *data) |
d40a81d5 | 2577 | { |
8480c8cc PP |
2578 | *((int *) data) |= 1; |
2579 | } | |
2580 | ||
2581 | static | |
50842bdc | 2582 | void trace_listener_removed(struct bt_trace *trace, void *data) |
8480c8cc PP |
2583 | { |
2584 | *((int *) data) |= 2; | |
d40a81d5 PP |
2585 | } |
2586 | ||
2587 | static | |
2588 | void test_trace_is_static_listener(void) | |
2589 | { | |
50842bdc | 2590 | struct bt_trace *trace; |
d40a81d5 PP |
2591 | int ret; |
2592 | int called1 = 0; | |
2593 | int called2 = 0; | |
2594 | int called3 = 0; | |
2595 | int called4 = 0; | |
2596 | int listener1_id; | |
2597 | int listener2_id; | |
2598 | int listener3_id; | |
2599 | int listener4_id; | |
2600 | ||
50842bdc | 2601 | trace = bt_trace_create(); |
d40a81d5 | 2602 | assert(trace); |
50842bdc | 2603 | ret = bt_trace_add_is_static_listener(NULL, |
8480c8cc | 2604 | trace_is_static_listener, trace_listener_removed, &called1); |
50842bdc PP |
2605 | ok(ret < 0, "bt_trace_add_is_static_listener() handles NULL (trace)"); |
2606 | ret = bt_trace_add_is_static_listener(trace, NULL, | |
8480c8cc | 2607 | trace_listener_removed, &called1); |
50842bdc PP |
2608 | ok(ret < 0, "bt_trace_add_is_static_listener() handles NULL (listener)"); |
2609 | listener1_id = bt_trace_add_is_static_listener(trace, | |
8480c8cc | 2610 | trace_is_static_listener, trace_listener_removed, &called1); |
50842bdc PP |
2611 | ok(listener1_id >= 0, "bt_trace_add_is_static_listener() succeeds (1)"); |
2612 | listener2_id = bt_trace_add_is_static_listener(trace, | |
8480c8cc | 2613 | trace_is_static_listener, trace_listener_removed, &called2); |
50842bdc PP |
2614 | ok(listener2_id >= 0, "bt_trace_add_is_static_listener() succeeds (2)"); |
2615 | listener3_id = bt_trace_add_is_static_listener(trace, | |
8480c8cc | 2616 | trace_is_static_listener, trace_listener_removed, &called3); |
50842bdc PP |
2617 | ok(listener3_id >= 0, "bt_trace_add_is_static_listener() succeeds (3)"); |
2618 | ret = bt_trace_remove_is_static_listener(NULL, 0); | |
2619 | ok(ret < 0, "bt_trace_remove_is_static_listener() handles NULL (trace)"); | |
2620 | ret = bt_trace_remove_is_static_listener(trace, -2); | |
2621 | ok(ret < 0, "bt_trace_remove_is_static_listener() handles invalid ID (negative)"); | |
2622 | ret = bt_trace_remove_is_static_listener(trace, 77); | |
2623 | ok(ret < 0, "bt_trace_remove_is_static_listener() handles invalid ID (non existing)"); | |
2624 | ret = bt_trace_remove_is_static_listener(trace, listener2_id); | |
2625 | ok(ret == 0, "bt_trace_remove_is_static_listener() succeeds"); | |
2626 | ok(called2 == 2, "bt_trace_remove_is_static_listener() calls the remove listener"); | |
2627 | listener4_id = bt_trace_add_is_static_listener(trace, | |
8480c8cc | 2628 | trace_is_static_listener, NULL, &called4); |
50842bdc | 2629 | ok(listener4_id >= 0, "bt_trace_add_is_static_listener() succeeds (4)"); |
d40a81d5 | 2630 | ok(called1 == 0, "\"trace is static\" listener not called before the trace is made static (1)"); |
8480c8cc | 2631 | ok(called2 == 2, "\"trace is static\" listener not called before the trace is made static (2)"); |
d40a81d5 PP |
2632 | ok(called3 == 0, "\"trace is static\" listener not called before the trace is made static (3)"); |
2633 | ok(called4 == 0, "\"trace is static\" listener not called before the trace is made static (4)"); | |
50842bdc | 2634 | ret = bt_trace_set_is_static(trace); |
d40a81d5 | 2635 | assert(ret == 0); |
50842bdc | 2636 | ret = bt_trace_add_is_static_listener(trace, |
8480c8cc | 2637 | trace_is_static_listener, trace_listener_removed, &called1); |
d40a81d5 | 2638 | ok(ret < 0, |
50842bdc | 2639 | "bt_trace_add_is_static_listener() fails when the trace is static"); |
d40a81d5 | 2640 | ok(called1 == 1, "\"trace is static\" listener called when the trace is made static (1)"); |
8480c8cc | 2641 | ok(called2 == 2, "\"trace is static\" listener not called when the trace is made static (2)"); |
d40a81d5 PP |
2642 | ok(called3 == 1, "\"trace is static\" listener called when the trace is made static (3)"); |
2643 | ok(called4 == 1, "\"trace is static\" listener called when the trace is made static (4)"); | |
2644 | called1 = 0; | |
2645 | called2 = 0; | |
2646 | called3 = 0; | |
2647 | called4 = 0; | |
2648 | bt_put(trace); | |
8480c8cc | 2649 | ok(called1 == 2, "\"trace is static\" listener not called after the trace is put (1)"); |
d40a81d5 | 2650 | ok(called2 == 0, "\"trace is static\" listener not called after the trace is put (2)"); |
8480c8cc | 2651 | ok(called3 == 2, "\"trace is static\" listener not called after the trace is put (3)"); |
d40a81d5 PP |
2652 | ok(called4 == 0, "\"trace is static\" listener not called after the trace is put (4)"); |
2653 | } | |
2654 | ||
4a32fda0 PP |
2655 | static |
2656 | void test_trace_uuid(void) | |
2657 | { | |
50842bdc | 2658 | struct bt_trace *trace; |
4a32fda0 PP |
2659 | const unsigned char uuid[] = { |
2660 | 0x35, 0x92, 0x63, 0xab, 0xb4, 0xbe, 0x40, 0xb4, | |
2661 | 0xb2, 0x60, 0xd3, 0xf1, 0x3b, 0xb0, 0xd8, 0x59, | |
2662 | }; | |
2663 | const unsigned char *ret_uuid; | |
2664 | ||
50842bdc | 2665 | trace = bt_trace_create(); |
4a32fda0 | 2666 | assert(trace); |
50842bdc PP |
2667 | ok(!bt_trace_get_uuid(NULL), |
2668 | "bt_trace_get_uuid() handles NULL"); | |
2669 | ok(!bt_trace_get_uuid(trace), | |
2670 | "bt_trace_get_uuid() returns NULL initially"); | |
2671 | ok(bt_trace_set_uuid(NULL, uuid), | |
2672 | "bt_trace_set_uuid() handles NULL (trace)"); | |
2673 | ok(bt_trace_set_uuid(trace, NULL), | |
2674 | "bt_trace_set_uuid() handles NULL (UUID)"); | |
2675 | ok(bt_trace_set_uuid(trace, uuid) == 0, | |
2676 | "bt_trace_set_uuid() succeeds with a valid UUID"); | |
2677 | ret_uuid = bt_trace_get_uuid(trace); | |
2678 | ok(ret_uuid, "bt_trace_get_uuid() returns a UUID"); | |
6de1e606 | 2679 | assert(ret_uuid); |
4a32fda0 | 2680 | ok(memcmp(uuid, ret_uuid, 16) == 0, |
50842bdc | 2681 | "bt_trace_get_uuid() returns the expected UUID"); |
4a32fda0 PP |
2682 | |
2683 | bt_put(trace); | |
2684 | } | |
2685 | ||
39d74371 JG |
2686 | int main(int argc, char **argv) |
2687 | { | |
c2237f17 | 2688 | const char *env_resize_length; |
32bd47d1 MJ |
2689 | gchar *trace_path; |
2690 | gchar *metadata_path; | |
39d74371 JG |
2691 | const char *clock_name = "test_clock"; |
2692 | const char *clock_description = "This is a test clock"; | |
5494ce8b JG |
2693 | const char *returned_clock_name; |
2694 | const char *returned_clock_description; | |
2695 | const uint64_t frequency = 1123456789; | |
23396598 | 2696 | const int64_t offset_s = 13515309; |
61cf588b MD |
2697 | const int64_t offset = 1234567; |
2698 | int64_t get_offset_s, | |
61ec14e6 | 2699 | get_offset; |
39d74371 | 2700 | const uint64_t precision = 10; |
5494ce8b | 2701 | const int is_absolute = 0xFF; |
39d74371 JG |
2702 | char *metadata_string; |
2703 | struct bt_ctf_writer *writer; | |
e2e51e32 MJ |
2704 | struct bt_utsname name = {"GNU/Linux", "testhost", "4.4.0-87-generic", |
2705 | "#110-Ubuntu SMP Tue Jul 18 12:55:35 UTC 2017", "x86_64"}; | |
1ff9582c | 2706 | struct bt_ctf_clock *clock, *ret_clock; |
50842bdc PP |
2707 | struct bt_clock_class *ret_clock_class; |
2708 | struct bt_stream_class *stream_class, *ret_stream_class; | |
2709 | struct bt_stream *stream1; | |
2710 | struct bt_stream *stream; | |
e3c971da | 2711 | const char *ret_string; |
e61caf8e JG |
2712 | const unsigned char *ret_uuid; |
2713 | unsigned char tmp_uuid[16] = { 0 }; | |
50842bdc | 2714 | struct bt_field_type *packet_context_type, |
b34f4d90 | 2715 | *packet_context_field_type, |
751b05c7 JG |
2716 | *packet_header_type, |
2717 | *packet_header_field_type, | |
35e8709f JG |
2718 | *integer_type, |
2719 | *stream_event_context_type, | |
88d26616 JG |
2720 | *ret_field_type, |
2721 | *event_header_field_type; | |
50842bdc PP |
2722 | struct bt_field *packet_header, *packet_header_field; |
2723 | struct bt_trace *trace; | |
12c8a1a3 | 2724 | int ret; |
4ae7c93b | 2725 | int64_t ret_int64_t; |
dac5c838 | 2726 | struct bt_value *obj; |
39d74371 | 2727 | |
dc3fffef PP |
2728 | if (argc < 2) { |
2729 | printf("Usage: tests-ctf-writer path_to_babeltrace\n"); | |
783c9151 | 2730 | return -1; |
39d74371 JG |
2731 | } |
2732 | ||
c2237f17 PP |
2733 | env_resize_length = getenv("PACKET_RESIZE_TEST_LENGTH"); |
2734 | if (env_resize_length) { | |
2735 | packet_resize_test_length = | |
2736 | (unsigned int) atoi(env_resize_length); | |
2737 | } | |
2738 | ||
8bbe269d | 2739 | plan_tests(NR_TESTS); |
39d74371 | 2740 | |
32bd47d1 | 2741 | trace_path = g_build_filename(g_get_tmp_dir(), "ctfwriter_XXXXXX", NULL); |
2bb37f06 | 2742 | if (!bt_mkdtemp(trace_path)) { |
39d74371 JG |
2743 | perror("# perror"); |
2744 | } | |
2745 | ||
32bd47d1 | 2746 | metadata_path = g_build_filename(trace_path, "metadata", NULL); |
39d74371 JG |
2747 | |
2748 | writer = bt_ctf_writer_create(trace_path); | |
50842bdc | 2749 | ok(writer, "bt_create succeeds in creating trace with path"); |
39d74371 | 2750 | |
4ae7c93b JG |
2751 | ok(!bt_ctf_writer_get_trace(NULL), |
2752 | "bt_ctf_writer_get_trace correctly handles NULL"); | |
2753 | trace = bt_ctf_writer_get_trace(writer); | |
50842bdc PP |
2754 | ok(bt_trace_set_native_byte_order(trace, BT_BYTE_ORDER_NATIVE), |
2755 | "Cannot set a trace's byte order to BT_BYTE_ORDER_NATIVE"); | |
2756 | ok(bt_trace_set_native_byte_order(trace, BT_BYTE_ORDER_UNSPECIFIED), | |
2757 | "Cannot set a trace's byte order to BT_BYTE_ORDER_UNSPECIFIED"); | |
4ae7c93b | 2758 | ok(trace, |
50842bdc PP |
2759 | "bt_ctf_writer_get_trace returns a bt_trace object"); |
2760 | ok(bt_trace_set_native_byte_order(trace, BT_BYTE_ORDER_BIG_ENDIAN) == 0, | |
35731220 | 2761 | "Set a trace's byte order to big endian"); |
50842bdc PP |
2762 | ok(bt_trace_get_native_byte_order(trace) == BT_BYTE_ORDER_BIG_ENDIAN, |
2763 | "bt_trace_get_native_byte_order returns a correct endianness"); | |
4ae7c93b | 2764 | |
39d74371 | 2765 | /* Add environment context to the trace */ |
e2e51e32 | 2766 | ok(bt_ctf_writer_add_environment_field(writer, "host", name.nodename) == 0, |
39d74371 | 2767 | "Add host (%s) environment field to writer instance", |
e2e51e32 | 2768 | name.nodename); |
39d74371 JG |
2769 | ok(bt_ctf_writer_add_environment_field(NULL, "test_field", |
2770 | "test_value"), | |
2771 | "bt_ctf_writer_add_environment_field error with NULL writer"); | |
2772 | ok(bt_ctf_writer_add_environment_field(writer, NULL, | |
2773 | "test_value"), | |
2774 | "bt_ctf_writer_add_environment_field error with NULL field name"); | |
2775 | ok(bt_ctf_writer_add_environment_field(writer, "test_field", | |
2776 | NULL), | |
2777 | "bt_ctf_writer_add_environment_field error with NULL field value"); | |
7f800dc7 | 2778 | |
50842bdc | 2779 | /* Test bt_trace_set_environment_field with an integer object */ |
dac5c838 | 2780 | obj = bt_value_integer_create_init(23); |
7f800dc7 | 2781 | assert(obj); |
50842bdc PP |
2782 | ok(bt_trace_set_environment_field(NULL, "test_env_int_obj", obj), |
2783 | "bt_trace_set_environment_field handles a NULL trace correctly"); | |
2784 | ok(bt_trace_set_environment_field(trace, NULL, obj), | |
2785 | "bt_trace_set_environment_field handles a NULL name correctly"); | |
2786 | ok(bt_trace_set_environment_field(trace, "test_env_int_obj", NULL), | |
2787 | "bt_trace_set_environment_field handles a NULL value correctly"); | |
2788 | ok(!bt_trace_set_environment_field(trace, "test_env_int_obj", obj), | |
2789 | "bt_trace_set_environment_field succeeds in adding an integer object"); | |
83509119 | 2790 | BT_PUT(obj); |
7f800dc7 | 2791 | |
50842bdc | 2792 | /* Test bt_trace_set_environment_field with a string object */ |
dac5c838 | 2793 | obj = bt_value_string_create_init("the value"); |
7f800dc7 | 2794 | assert(obj); |
50842bdc PP |
2795 | ok(!bt_trace_set_environment_field(trace, "test_env_str_obj", obj), |
2796 | "bt_trace_set_environment_field succeeds in adding a string object"); | |
83509119 | 2797 | BT_PUT(obj); |
7f800dc7 | 2798 | |
50842bdc PP |
2799 | /* Test bt_trace_set_environment_field_integer */ |
2800 | ok(bt_trace_set_environment_field_integer(NULL, "test_env_int", | |
7f800dc7 | 2801 | -194875), |
50842bdc PP |
2802 | "bt_trace_set_environment_field_integer handles a NULL trace correctly"); |
2803 | ok(bt_trace_set_environment_field_integer(trace, NULL, -194875), | |
2804 | "bt_trace_set_environment_field_integer handles a NULL name correctly"); | |
2805 | ok(!bt_trace_set_environment_field_integer(trace, "test_env_int", | |
7f800dc7 | 2806 | -164973), |
50842bdc | 2807 | "bt_trace_set_environment_field_integer succeeds"); |
7f800dc7 | 2808 | |
50842bdc PP |
2809 | /* Test bt_trace_set_environment_field_string */ |
2810 | ok(bt_trace_set_environment_field_string(NULL, "test_env_str", | |
7f800dc7 | 2811 | "yeah"), |
50842bdc PP |
2812 | "bt_trace_set_environment_field_string handles a NULL trace correctly"); |
2813 | ok(bt_trace_set_environment_field_string(trace, NULL, "yeah"), | |
2814 | "bt_trace_set_environment_field_string handles a NULL name correctly"); | |
2815 | ok(bt_trace_set_environment_field_string(trace, "test_env_str", | |
7f800dc7 | 2816 | NULL), |
50842bdc PP |
2817 | "bt_trace_set_environment_field_string handles a NULL value correctly"); |
2818 | ok(!bt_trace_set_environment_field_string(trace, "test_env_str", | |
7f800dc7 | 2819 | "oh yeah"), |
50842bdc PP |
2820 | "bt_trace_set_environment_field_string succeeds"); |
2821 | ||
2822 | /* Test bt_trace_get_environment_field_count */ | |
2823 | ok(bt_trace_get_environment_field_count(NULL) < 0, | |
2824 | "bt_trace_get_environment_field_count handles a NULL trace correctly"); | |
2825 | ok(bt_trace_get_environment_field_count(trace) == 5, | |
2826 | "bt_trace_get_environment_field_count returns a correct number of environment fields"); | |
2827 | ||
2828 | /* Test bt_trace_get_environment_field_name */ | |
2829 | ok(bt_trace_get_environment_field_name_by_index(NULL, 0) == NULL, | |
2830 | "bt_trace_get_environment_field_name handles a NULL trace correctly"); | |
2831 | ok(bt_trace_get_environment_field_name_by_index(trace, 5) == NULL, | |
2832 | "bt_trace_get_environment_field_name handles an invalid index correctly (too large)"); | |
2833 | ret_string = bt_trace_get_environment_field_name_by_index(trace, 0); | |
4ae7c93b | 2834 | ok(ret_string && !strcmp(ret_string, "host"), |
50842bdc PP |
2835 | "bt_trace_get_environment_field_name returns a correct field name"); |
2836 | ret_string = bt_trace_get_environment_field_name_by_index(trace, 1); | |
7f800dc7 | 2837 | ok(ret_string && !strcmp(ret_string, "test_env_int_obj"), |
50842bdc PP |
2838 | "bt_trace_get_environment_field_name returns a correct field name"); |
2839 | ret_string = bt_trace_get_environment_field_name_by_index(trace, 2); | |
7f800dc7 | 2840 | ok(ret_string && !strcmp(ret_string, "test_env_str_obj"), |
50842bdc PP |
2841 | "bt_trace_get_environment_field_name returns a correct field name"); |
2842 | ret_string = bt_trace_get_environment_field_name_by_index(trace, 3); | |
7f800dc7 | 2843 | ok(ret_string && !strcmp(ret_string, "test_env_int"), |
50842bdc PP |
2844 | "bt_trace_get_environment_field_name returns a correct field name"); |
2845 | ret_string = bt_trace_get_environment_field_name_by_index(trace, 4); | |
7f800dc7 | 2846 | ok(ret_string && !strcmp(ret_string, "test_env_str"), |
50842bdc PP |
2847 | "bt_trace_get_environment_field_name returns a correct field name"); |
2848 | ||
2849 | /* Test bt_trace_get_environment_field_value */ | |
2850 | ok(bt_trace_get_environment_field_value_by_index(NULL, 0) == NULL, | |
2851 | "bt_trace_get_environment_field_value handles a NULL trace correctly"); | |
2852 | ok(bt_trace_get_environment_field_value_by_index(trace, 5) == NULL, | |
2853 | "bt_trace_get_environment_field_value handles an invalid index correctly (too large)"); | |
2854 | obj = bt_trace_get_environment_field_value_by_index(trace, 1); | |
dac5c838 | 2855 | ret = bt_value_integer_get(obj, &ret_int64_t); |
7f800dc7 | 2856 | ok(!ret && ret_int64_t == 23, |
50842bdc | 2857 | "bt_trace_get_environment_field_value succeeds in getting an integer value"); |
83509119 | 2858 | BT_PUT(obj); |
50842bdc | 2859 | obj = bt_trace_get_environment_field_value_by_index(trace, 2); |
dac5c838 | 2860 | ret = bt_value_string_get(obj, &ret_string); |
7f800dc7 | 2861 | ok(!ret && ret_string && !strcmp(ret_string, "the value"), |
50842bdc | 2862 | "bt_trace_get_environment_field_value succeeds in getting a string value"); |
83509119 | 2863 | BT_PUT(obj); |
7f800dc7 | 2864 | |
50842bdc PP |
2865 | /* Test bt_trace_get_environment_field_value_by_name */ |
2866 | ok(!bt_trace_get_environment_field_value_by_name(NULL, | |
7f800dc7 | 2867 | "test_env_str"), |
50842bdc PP |
2868 | "bt_trace_get_environment_field_value_by_name handles a NULL trace correctly"); |
2869 | ok(!bt_trace_get_environment_field_value_by_name(trace, NULL), | |
2870 | "bt_trace_get_environment_field_value_by_name handles a NULL name correctly"); | |
2871 | ok(!bt_trace_get_environment_field_value_by_name(trace, "oh oh"), | |
2872 | "bt_trace_get_environment_field_value_by_name returns NULL or an unknown field name"); | |
2873 | obj = bt_trace_get_environment_field_value_by_name(trace, | |
7f800dc7 | 2874 | "test_env_str"); |
dac5c838 | 2875 | ret = bt_value_string_get(obj, &ret_string); |
7f800dc7 | 2876 | ok(!ret && ret_string && !strcmp(ret_string, "oh yeah"), |
50842bdc | 2877 | "bt_trace_get_environment_field_value_by_name succeeds in getting an existing field"); |
83509119 | 2878 | BT_PUT(obj); |
7f800dc7 PP |
2879 | |
2880 | /* Test environment field replacement */ | |
50842bdc | 2881 | ok(!bt_trace_set_environment_field_integer(trace, "test_env_int", |
7f800dc7 | 2882 | 654321), |
50842bdc PP |
2883 | "bt_trace_set_environment_field_integer succeeds with an existing name"); |
2884 | ok(bt_trace_get_environment_field_count(trace) == 5, | |
2885 | "bt_trace_set_environment_field_integer with an existing key does not increase the environment size"); | |
2886 | obj = bt_trace_get_environment_field_value_by_index(trace, 3); | |
dac5c838 | 2887 | ret = bt_value_integer_get(obj, &ret_int64_t); |
7f800dc7 | 2888 | ok(!ret && ret_int64_t == 654321, |
50842bdc | 2889 | "bt_trace_get_environment_field_value successfully replaces an existing field"); |
83509119 | 2890 | BT_PUT(obj); |
39d74371 | 2891 | |
39d74371 JG |
2892 | ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname) |
2893 | == 0, "Add sysname (%s) environment field to writer instance", | |
2894 | name.sysname); | |
2895 | ok(bt_ctf_writer_add_environment_field(writer, "nodename", | |
2896 | name.nodename) == 0, | |
2897 | "Add nodename (%s) environment field to writer instance", | |
2898 | name.nodename); | |
2899 | ok(bt_ctf_writer_add_environment_field(writer, "release", name.release) | |
2900 | == 0, "Add release (%s) environment field to writer instance", | |
2901 | name.release); | |
2902 | ok(bt_ctf_writer_add_environment_field(writer, "version", name.version) | |
2903 | == 0, "Add version (%s) environment field to writer instance", | |
2904 | name.version); | |
2905 | ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine) | |
2906 | == 0, "Add machine (%s) environment field to writer istance", | |
2907 | name.machine); | |
2908 | ||
2909 | /* Define a clock and add it to the trace */ | |
5494ce8b JG |
2910 | ok(bt_ctf_clock_create("signed") == NULL, |
2911 | "Illegal clock name rejected"); | |
39d74371 JG |
2912 | clock = bt_ctf_clock_create(clock_name); |
2913 | ok(clock, "Clock created sucessfully"); | |
5494ce8b JG |
2914 | returned_clock_name = bt_ctf_clock_get_name(clock); |
2915 | ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name"); | |
d50c7132 | 2916 | ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0, |
5494ce8b JG |
2917 | "Returned clock name is valid"); |
2918 | ||
2919 | returned_clock_description = bt_ctf_clock_get_description(clock); | |
2920 | ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description"); | |
2921 | ok(bt_ctf_clock_set_description(clock, clock_description) == 0, | |
2922 | "Clock description set successfully"); | |
2923 | ||
2924 | returned_clock_description = bt_ctf_clock_get_description(clock); | |
2925 | ok(returned_clock_description, | |
2926 | "bt_ctf_clock_get_description returns a description."); | |
d50c7132 JG |
2927 | ok(returned_clock_description ? |
2928 | !strcmp(returned_clock_description, clock_description) : 0, | |
5494ce8b JG |
2929 | "Returned clock description is valid"); |
2930 | ||
2931 | ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ, | |
2932 | "bt_ctf_clock_get_frequency returns the correct default frequency"); | |
39d74371 JG |
2933 | ok(bt_ctf_clock_set_frequency(clock, frequency) == 0, |
2934 | "Set clock frequency"); | |
5494ce8b JG |
2935 | ok(bt_ctf_clock_get_frequency(clock) == frequency, |
2936 | "bt_ctf_clock_get_frequency returns the correct frequency once it is set"); | |
2937 | ||
61cf588b MD |
2938 | ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0, |
2939 | "bt_ctf_clock_get_offset_s succeeds"); | |
2940 | ok(get_offset_s == DEFAULT_CLOCK_OFFSET_S, | |
5494ce8b | 2941 | "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)"); |
39d74371 JG |
2942 | ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0, |
2943 | "Set clock offset (seconds)"); | |
61cf588b MD |
2944 | ok(bt_ctf_clock_get_offset_s(clock, &get_offset_s) == 0, |
2945 | "bt_ctf_clock_get_offset_s succeeds"); | |
2946 | ok(get_offset_s == offset_s, | |
5494ce8b JG |
2947 | "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set"); |
2948 | ||
61cf588b MD |
2949 | ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0, |
2950 | "bt_ctf_clock_get_offset succeeds"); | |
2951 | ok(get_offset == DEFAULT_CLOCK_OFFSET, | |
2952 | "bt_ctf_clock_get_offset returns the correct default offset (in ticks)"); | |
39d74371 | 2953 | ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset"); |
61cf588b MD |
2954 | ok(bt_ctf_clock_get_offset(clock, &get_offset) == 0, |
2955 | "bt_ctf_clock_get_offset succeeds"); | |
2956 | ok(get_offset == offset, | |
2957 | "bt_ctf_clock_get_offset returns the correct default offset (in ticks) once it is set"); | |
5494ce8b JG |
2958 | |
2959 | ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION, | |
2960 | "bt_ctf_clock_get_precision returns the correct default precision"); | |
39d74371 JG |
2961 | ok(bt_ctf_clock_set_precision(clock, precision) == 0, |
2962 | "Set clock precision"); | |
5494ce8b JG |
2963 | ok(bt_ctf_clock_get_precision(clock) == precision, |
2964 | "bt_ctf_clock_get_precision returns the correct precision once it is set"); | |
2965 | ||
2966 | ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE, | |
2967 | "bt_ctf_clock_get_precision returns the correct default is_absolute attribute"); | |
2968 | ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0, | |
39d74371 | 2969 | "Set clock absolute property"); |
5494ce8b JG |
2970 | ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute, |
2971 | "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set"); | |
2972 | ||
5494ce8b JG |
2973 | ok(bt_ctf_clock_set_time(clock, current_time) == 0, |
2974 | "Set clock time"); | |
39d74371 | 2975 | |
e61caf8e JG |
2976 | ret_uuid = bt_ctf_clock_get_uuid(clock); |
2977 | ok(ret_uuid, | |
2978 | "bt_ctf_clock_get_uuid returns a UUID"); | |
2979 | if (ret_uuid) { | |
2980 | memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid)); | |
2981 | /* Slightly modify UUID */ | |
2982 | tmp_uuid[sizeof(tmp_uuid) - 1]++; | |
2983 | } | |
2984 | ||
e61caf8e | 2985 | ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0, |
4caab45b | 2986 | "bt_ctf_clock_set_uuid sets a new uuid successfully"); |
e61caf8e JG |
2987 | ret_uuid = bt_ctf_clock_get_uuid(clock); |
2988 | ok(ret_uuid, | |
2989 | "bt_ctf_clock_get_uuid returns a UUID after setting a new one"); | |
2990 | ok(uuid_match(ret_uuid, tmp_uuid), | |
2991 | "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one"); | |
5494ce8b | 2992 | |
39d74371 | 2993 | /* Define a stream class */ |
50842bdc | 2994 | stream_class = bt_stream_class_create("test_stream"); |
e3c971da | 2995 | |
50842bdc PP |
2996 | ok(bt_stream_class_get_name(NULL) == NULL, |
2997 | "bt_stream_class_get_name handles NULL correctly"); | |
2998 | ret_string = bt_stream_class_get_name(stream_class); | |
88d26616 | 2999 | ok(ret_string && !strcmp(ret_string, "test_stream"), |
50842bdc | 3000 | "bt_stream_class_get_name returns a correct stream class name"); |
e3c971da | 3001 | |
50842bdc PP |
3002 | ok(bt_stream_class_get_clock(stream_class) == NULL, |
3003 | "bt_stream_class_get_clock returns NULL when a clock was not set"); | |
3004 | ok(bt_stream_class_get_clock(NULL) == NULL, | |
3005 | "bt_stream_class_get_clock handles NULL correctly"); | |
1ff9582c | 3006 | |
39d74371 | 3007 | ok(stream_class, "Create stream class"); |
50842bdc | 3008 | ok(bt_stream_class_set_clock(stream_class, clock) == 0, |
39d74371 | 3009 | "Set a stream class' clock"); |
50842bdc | 3010 | ret_clock = bt_stream_class_get_clock(stream_class); |
1ff9582c | 3011 | ok(ret_clock == clock, |
50842bdc | 3012 | "bt_stream_class_get_clock returns a correct clock"); |
83509119 | 3013 | bt_put(ret_clock); |
39d74371 JG |
3014 | |
3015 | /* Test the event fields and event types APIs */ | |
3016 | type_field_tests(); | |
3017 | ||
e7cb4506 PP |
3018 | /* Test fields copying */ |
3019 | field_copy_tests(); | |
3020 | ||
50842bdc PP |
3021 | ok(bt_stream_class_get_id(stream_class) < 0, |
3022 | "bt_stream_class_get_id returns an error when no id is set"); | |
3023 | ok(bt_stream_class_get_id(NULL) < 0, | |
3024 | "bt_stream_class_get_id handles NULL correctly"); | |
3025 | ok(bt_stream_class_set_id(NULL, 123) < 0, | |
3026 | "bt_stream_class_set_id handles NULL correctly"); | |
3027 | ok(bt_stream_class_set_id(stream_class, 123) == 0, | |
1ff9582c | 3028 | "Set an stream class' id"); |
50842bdc PP |
3029 | ok(bt_stream_class_get_id(stream_class) == 123, |
3030 | "bt_stream_class_get_id returns the correct value"); | |
1ff9582c | 3031 | |
d8469458 | 3032 | /* Validate default event header fields */ |
50842bdc PP |
3033 | ok(bt_stream_class_get_event_header_type(NULL) == NULL, |
3034 | "bt_stream_class_get_event_header_type handles NULL correctly"); | |
3035 | ret_field_type = bt_stream_class_get_event_header_type( | |
88d26616 JG |
3036 | stream_class); |
3037 | ok(ret_field_type, | |
50842bdc PP |
3038 | "bt_stream_class_get_event_header_type returns an event header type"); |
3039 | ok(bt_field_type_get_type_id(ret_field_type) == BT_FIELD_TYPE_ID_STRUCT, | |
88d26616 JG |
3040 | "Default event header type is a structure"); |
3041 | event_header_field_type = | |
50842bdc | 3042 | bt_field_type_structure_get_field_type_by_name( |
88d26616 JG |
3043 | ret_field_type, "id"); |
3044 | ok(event_header_field_type, | |
3045 | "Default event header type contains an \"id\" field"); | |
50842bdc PP |
3046 | ok(bt_field_type_get_type_id( |
3047 | event_header_field_type) == BT_FIELD_TYPE_ID_INTEGER, | |
88d26616 | 3048 | "Default event header \"id\" field is an integer"); |
83509119 | 3049 | bt_put(event_header_field_type); |
88d26616 | 3050 | event_header_field_type = |
50842bdc | 3051 | bt_field_type_structure_get_field_type_by_name( |
88d26616 JG |
3052 | ret_field_type, "timestamp"); |
3053 | ok(event_header_field_type, | |
3054 | "Default event header type contains a \"timestamp\" field"); | |
50842bdc PP |
3055 | ok(bt_field_type_get_type_id( |
3056 | event_header_field_type) == BT_FIELD_TYPE_ID_INTEGER, | |
88d26616 | 3057 | "Default event header \"timestamp\" field is an integer"); |
83509119 JG |
3058 | bt_put(event_header_field_type); |
3059 | bt_put(ret_field_type); | |
88d26616 | 3060 | |
751b05c7 | 3061 | /* Add a custom trace packet header field */ |
50842bdc PP |
3062 | ok(bt_trace_get_packet_header_type(NULL) == NULL, |
3063 | "bt_trace_get_packet_header_type handles NULL correctly"); | |
3064 | packet_header_type = bt_trace_get_packet_header_type(trace); | |
751b05c7 | 3065 | ok(packet_header_type, |
50842bdc PP |
3066 | "bt_trace_get_packet_header_type returns a packet header"); |
3067 | ok(bt_field_type_get_type_id(packet_header_type) == BT_FIELD_TYPE_ID_STRUCT, | |
3068 | "bt_trace_get_packet_header_type returns a packet header of type struct"); | |
3069 | ret_field_type = bt_field_type_structure_get_field_type_by_name( | |
751b05c7 JG |
3070 | packet_header_type, "magic"); |
3071 | ok(ret_field_type, "Default packet header type contains a \"magic\" field"); | |
83509119 | 3072 | bt_put(ret_field_type); |
50842bdc | 3073 | ret_field_type = bt_field_type_structure_get_field_type_by_name( |
751b05c7 JG |
3074 | packet_header_type, "uuid"); |
3075 | ok(ret_field_type, "Default packet header type contains a \"uuid\" field"); | |
83509119 | 3076 | bt_put(ret_field_type); |
50842bdc | 3077 | ret_field_type = bt_field_type_structure_get_field_type_by_name( |
751b05c7 JG |
3078 | packet_header_type, "stream_id"); |
3079 | ok(ret_field_type, "Default packet header type contains a \"stream_id\" field"); | |
83509119 | 3080 | bt_put(ret_field_type); |
751b05c7 | 3081 | |
50842bdc PP |
3082 | packet_header_field_type = bt_field_type_integer_create(22); |
3083 | ok(!bt_field_type_structure_add_field(packet_header_type, | |
751b05c7 JG |
3084 | packet_header_field_type, "custom_trace_packet_header_field"), |
3085 | "Added a custom trace packet header field successfully"); | |
3086 | ||
50842bdc PP |
3087 | ok(bt_trace_set_packet_header_type(NULL, packet_header_type) < 0, |
3088 | "bt_trace_set_packet_header_type handles a NULL trace correctly"); | |
3089 | ok(!bt_trace_set_packet_header_type(trace, packet_header_type), | |
751b05c7 JG |
3090 | "Set a trace packet_header_type successfully"); |
3091 | ||
50842bdc PP |
3092 | ok(bt_stream_class_get_packet_context_type(NULL) == NULL, |
3093 | "bt_stream_class_get_packet_context_type handles NULL correctly"); | |
12c8a1a3 JG |
3094 | |
3095 | /* Add a custom field to the stream class' packet context */ | |
50842bdc | 3096 | packet_context_type = bt_stream_class_get_packet_context_type(stream_class); |
12c8a1a3 | 3097 | ok(packet_context_type, |
50842bdc PP |
3098 | "bt_stream_class_get_packet_context_type returns a packet context type."); |
3099 | ok(bt_field_type_get_type_id(packet_context_type) == BT_FIELD_TYPE_ID_STRUCT, | |
12c8a1a3 JG |
3100 | "Packet context is a structure"); |
3101 | ||
50842bdc PP |
3102 | ok(bt_stream_class_set_packet_context_type(NULL, packet_context_type), |
3103 | "bt_stream_class_set_packet_context_type handles a NULL stream class correctly"); | |
b34f4d90 | 3104 | |
50842bdc PP |
3105 | integer_type = bt_field_type_integer_create(32); |
3106 | ok(bt_stream_class_set_packet_context_type(stream_class, | |
b34f4d90 | 3107 | integer_type) < 0, |
50842bdc | 3108 | "bt_stream_class_set_packet_context_type rejects a packet context that is not a structure"); |
88d26616 | 3109 | /* Create a "uint5_t" equivalent custom packet context field */ |
50842bdc | 3110 | packet_context_field_type = bt_field_type_integer_create(5); |
88d26616 | 3111 | |
50842bdc | 3112 | ret = bt_field_type_structure_add_field(packet_context_type, |
35e8709f | 3113 | packet_context_field_type, "custom_packet_context_field"); |
12c8a1a3 JG |
3114 | ok(ret == 0, "Packet context field added successfully"); |
3115 | ||
35e8709f | 3116 | /* Define a stream event context containing a my_integer field. */ |
50842bdc PP |
3117 | ok(bt_stream_class_get_event_context_type(NULL) == NULL, |
3118 | "bt_stream_class_get_event_context_type handles NULL correctly"); | |
3119 | stream_event_context_type = bt_field_type_structure_create(); | |
3120 | bt_field_type_structure_add_field(stream_event_context_type, | |
35e8709f JG |
3121 | integer_type, "common_event_context"); |
3122 | ||
50842bdc | 3123 | ok(bt_stream_class_set_event_context_type(NULL, |
35e8709f | 3124 | stream_event_context_type) < 0, |
50842bdc PP |
3125 | "bt_stream_class_set_event_context_type handles a NULL stream_class correctly"); |
3126 | ok(bt_stream_class_set_event_context_type(stream_class, | |
35e8709f | 3127 | integer_type) < 0, |
50842bdc | 3128 | "bt_stream_class_set_event_context_type validates that the event context os a structure"); |
35e8709f | 3129 | |
50842bdc | 3130 | ok(bt_stream_class_set_event_context_type( |
35e8709f JG |
3131 | stream_class, stream_event_context_type) == 0, |
3132 | "Set a new stream event context type"); | |
50842bdc | 3133 | ret_field_type = bt_stream_class_get_event_context_type( |
35e8709f JG |
3134 | stream_class); |
3135 | ok(ret_field_type == stream_event_context_type, | |
50842bdc | 3136 | "bt_stream_class_get_event_context_type returns the correct field type."); |
83509119 | 3137 | bt_put(ret_field_type); |
12c8a1a3 | 3138 | |
39d74371 | 3139 | /* Instantiate a stream and append events */ |
ac0c6bdd PP |
3140 | ret = bt_ctf_writer_add_clock(writer, clock); |
3141 | assert(ret == 0); | |
50842bdc PP |
3142 | ok(bt_trace_get_stream_count(trace) == 0, |
3143 | "bt_trace_get_stream_count() succeeds and returns the correct value (0)"); | |
39d74371 JG |
3144 | stream1 = bt_ctf_writer_create_stream(writer, stream_class); |
3145 | ok(stream1, "Instanciate a stream class from writer"); | |
50842bdc PP |
3146 | ok(bt_trace_get_stream_count(trace) == 1, |
3147 | "bt_trace_get_stream_count() succeeds and returns the correct value (1)"); | |
3148 | stream = bt_trace_get_stream_by_index(trace, 0); | |
c1e730fe | 3149 | ok(stream == stream1, |
50842bdc | 3150 | "bt_trace_get_stream_by_index() succeeds and returns the correct value"); |
c1e730fe | 3151 | BT_PUT(stream); |
39d74371 | 3152 | |
b25d20ad PP |
3153 | /* |
3154 | * Creating a stream through a writer adds the given stream | |
3155 | * class to the writer's trace, thus registering the stream | |
3156 | * class's clock to the trace. | |
3157 | */ | |
50842bdc PP |
3158 | ok(bt_trace_get_clock_class_count(NULL) < 0, |
3159 | "bt_trace_get_clock_class_count correctly handles NULL"); | |
3160 | ok(bt_trace_get_clock_class_count(trace) == 1, | |
3161 | "bt_trace_get_clock_class_count returns the correct number of clocks"); | |
3162 | ok(!bt_trace_get_clock_class_by_index(NULL, 0), | |
3163 | "bt_trace_get_clock_class correctly handles NULL"); | |
3164 | ok(!bt_trace_get_clock_class_by_index(trace, 1), | |
3165 | "bt_trace_get_clock_class correctly handles out of bound accesses"); | |
3166 | ret_clock_class = bt_trace_get_clock_class_by_index(trace, 0); | |
3167 | ok(strcmp(bt_clock_class_get_name(ret_clock_class), | |
ac0c6bdd | 3168 | bt_ctf_clock_get_name(clock)) == 0, |
50842bdc | 3169 | "bt_trace_get_clock_class returns the right clock instance"); |
ac0c6bdd | 3170 | bt_put(ret_clock_class); |
50842bdc PP |
3171 | ok(!bt_trace_get_clock_class_by_name(trace, NULL), |
3172 | "bt_trace_get_clock_class_by_name correctly handles NULL (trace)"); | |
3173 | ok(!bt_trace_get_clock_class_by_name(NULL, clock_name), | |
3174 | "bt_trace_get_clock_by_name correctly handles NULL (clock name)"); | |
3175 | ok(!bt_trace_get_clock_class_by_name(NULL, NULL), | |
3176 | "bt_trace_get_clock_by_name correctly handles NULL (both)"); | |
3177 | ret_clock_class = bt_trace_get_clock_class_by_name(trace, clock_name); | |
3178 | ok(strcmp(bt_clock_class_get_name(ret_clock_class), | |
ac0c6bdd | 3179 | bt_ctf_clock_get_name(clock)) == 0, |
50842bdc | 3180 | "bt_trace_get_clock_class returns the right clock instance"); |
ac0c6bdd | 3181 | bt_put(ret_clock_class); |
50842bdc PP |
3182 | ok(!bt_trace_get_clock_class_by_name(trace, "random"), |
3183 | "bt_trace_get_clock_by_name fails when the requested clock doesn't exist"); | |
b25d20ad | 3184 | |
50842bdc PP |
3185 | ok(bt_stream_get_class(NULL) == NULL, |
3186 | "bt_stream_get_class correctly handles NULL"); | |
3187 | ret_stream_class = bt_stream_get_class(stream1); | |
36336d93 | 3188 | ok(ret_stream_class, |
50842bdc | 3189 | "bt_stream_get_class returns a stream class"); |
36336d93 JG |
3190 | ok(ret_stream_class == stream_class, |
3191 | "Returned stream class is of the correct type"); | |
3192 | ||
09840de5 PP |
3193 | /* |
3194 | * Packet header, packet context, event header, and stream | |
3195 | * event context types were copied for the resolving | |
3196 | * process | |
3197 | */ | |
3198 | BT_PUT(packet_header_type); | |
3199 | BT_PUT(packet_context_type); | |
3200 | BT_PUT(stream_event_context_type); | |
50842bdc | 3201 | packet_header_type = bt_trace_get_packet_header_type(trace); |
09840de5 PP |
3202 | assert(packet_header_type); |
3203 | packet_context_type = | |
50842bdc | 3204 | bt_stream_class_get_packet_context_type(stream_class); |
09840de5 PP |
3205 | assert(packet_context_type); |
3206 | stream_event_context_type = | |
50842bdc | 3207 | bt_stream_class_get_event_context_type(stream_class); |
09840de5 PP |
3208 | assert(stream_event_context_type); |
3209 | ||
751b05c7 JG |
3210 | /* |
3211 | * Try to modify the packet context type after a stream has been | |
3212 | * created. | |
3213 | */ | |
50842bdc | 3214 | ret = bt_field_type_structure_add_field(packet_header_type, |
751b05c7 JG |
3215 | packet_header_field_type, "should_fail"); |
3216 | ok(ret < 0, | |
3217 | "Trace packet header type can't be modified once a stream has been instanciated"); | |
3218 | ||
12c8a1a3 JG |
3219 | /* |
3220 | * Try to modify the packet context type after a stream has been | |
3221 | * created. | |
3222 | */ | |
50842bdc | 3223 | ret = bt_field_type_structure_add_field(packet_context_type, |
12c8a1a3 JG |
3224 | packet_context_field_type, "should_fail"); |
3225 | ok(ret < 0, | |
751b05c7 | 3226 | "Packet context type can't be modified once a stream has been instanciated"); |
12c8a1a3 | 3227 | |
35e8709f JG |
3228 | /* |
3229 | * Try to modify the stream event context type after a stream has been | |
3230 | * created. | |
3231 | */ | |
50842bdc | 3232 | ret = bt_field_type_structure_add_field(stream_event_context_type, |
35e8709f JG |
3233 | integer_type, "should_fail"); |
3234 | ok(ret < 0, | |
751b05c7 | 3235 | "Stream event context type can't be modified once a stream has been instanciated"); |
35e8709f JG |
3236 | |
3237 | /* Should fail after instanciating a stream (frozen) */ | |
50842bdc | 3238 | ok(bt_stream_class_set_clock(stream_class, clock), |
39d74371 JG |
3239 | "Changes to a stream class that was already instantiated fail"); |
3240 | ||
751b05c7 | 3241 | /* Populate the custom packet header field only once for all tests */ |
50842bdc PP |
3242 | ok(bt_stream_get_packet_header(NULL) == NULL, |
3243 | "bt_stream_get_packet_header handles NULL correctly"); | |
3244 | packet_header = bt_stream_get_packet_header(stream1); | |
751b05c7 | 3245 | ok(packet_header, |
50842bdc PP |
3246 | "bt_stream_get_packet_header returns a packet header"); |
3247 | ret_field_type = bt_field_get_type(packet_header); | |
751b05c7 JG |
3248 | ok(ret_field_type == packet_header_type, |
3249 | "Stream returns a packet header of the appropriate type"); | |
83509119 | 3250 | bt_put(ret_field_type); |
50842bdc | 3251 | packet_header_field = bt_field_structure_get_field_by_name(packet_header, |
751b05c7 JG |
3252 | "custom_trace_packet_header_field"); |
3253 | ok(packet_header_field, | |
3254 | "Packet header structure contains a custom field with the appropriate name"); | |
50842bdc PP |
3255 | ret_field_type = bt_field_get_type(packet_header_field); |
3256 | ok(bt_field_type_compare(ret_field_type, packet_header_field_type) == 0, | |
751b05c7 | 3257 | "Custom packet header field is of the expected type"); |
50842bdc | 3258 | ok(!bt_field_unsigned_integer_set_value(packet_header_field, |
751b05c7 | 3259 | 54321), "Set custom packet header value successfully"); |
50842bdc PP |
3260 | ok(bt_stream_set_packet_header(stream1, NULL) < 0, |
3261 | "bt_stream_set_packet_header handles a NULL packet header correctly"); | |
3262 | ok(bt_stream_set_packet_header(NULL, packet_header) < 0, | |
3263 | "bt_stream_set_packet_header handles a NULL stream correctly"); | |
3264 | ok(bt_stream_set_packet_header(stream1, packet_header_field) < 0, | |
3265 | "bt_stream_set_packet_header rejects a packet header of the wrong type"); | |
3266 | ok(!bt_stream_set_packet_header(stream1, packet_header), | |
751b05c7 JG |
3267 | "Successfully set a stream's packet header"); |
3268 | ||
3975bd7e JG |
3269 | ok(bt_ctf_writer_add_environment_field(writer, "new_field", "test") == 0, |
3270 | "Add environment field to writer after stream creation"); | |
3271 | ||
44e0165b PP |
3272 | test_clock_utils(); |
3273 | ||
bcd3a967 | 3274 | test_create_writer_vs_non_writer_mode(); |
44ac03eb | 3275 | |
ac0c6bdd PP |
3276 | test_set_clock_non_writer_stream_class(); |
3277 | ||
3278 | test_instanciate_event_before_stream(writer, clock); | |
42f45a8d | 3279 | |
39d74371 JG |
3280 | append_simple_event(stream_class, stream1, clock); |
3281 | ||
3282 | packet_resize_test(stream_class, stream1, clock); | |
3283 | ||
3284 | append_complex_event(stream_class, stream1, clock); | |
3285 | ||
f60fde63 PP |
3286 | append_existing_event_class(stream_class); |
3287 | ||
fdf80f32 JG |
3288 | test_empty_stream(writer); |
3289 | ||
ac0c6bdd | 3290 | test_custom_event_header_stream(writer, clock); |
29be776a | 3291 | |
28437b95 PP |
3292 | test_static_trace(); |
3293 | ||
d40a81d5 PP |
3294 | test_trace_is_static_listener(); |
3295 | ||
4a32fda0 PP |
3296 | test_trace_uuid(); |
3297 | ||
39d74371 JG |
3298 | metadata_string = bt_ctf_writer_get_metadata_string(writer); |
3299 | ok(metadata_string, "Get metadata string"); | |
3300 | ||
3301 | bt_ctf_writer_flush_metadata(writer); | |
39d74371 | 3302 | |
83509119 JG |
3303 | bt_put(clock); |
3304 | bt_put(ret_stream_class); | |
3305 | bt_put(writer); | |
3306 | bt_put(stream1); | |
3307 | bt_put(packet_context_type); | |
3308 | bt_put(packet_context_field_type); | |
3309 | bt_put(integer_type); | |
3310 | bt_put(stream_event_context_type); | |
3311 | bt_put(ret_field_type); | |
3312 | bt_put(packet_header_type); | |
3313 | bt_put(packet_header_field_type); | |
3314 | bt_put(packet_header); | |
3315 | bt_put(packet_header_field); | |
3316 | bt_put(trace); | |
39d74371 | 3317 | free(metadata_string); |
83509119 | 3318 | bt_put(stream_class); |
9b068522 | 3319 | |
dc3fffef | 3320 | validate_trace(argv[1], trace_path); |
245bd444 | 3321 | |
50842bdc | 3322 | //recursive_rmdir(trace_path); |
32bd47d1 MJ |
3323 | g_free(trace_path); |
3324 | g_free(metadata_path); | |
3325 | ||
39d74371 JG |
3326 | return 0; |
3327 | } |