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