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