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