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