Commit | Line | Data |
---|---|---|
39d74371 JG |
1 | /* |
2 | * test-ctf-writer.c | |
3 | * | |
4 | * CTF Writer test | |
5 | * | |
6 | * Copyright 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
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 | ||
22 | #define _GNU_SOURCE | |
23 | #include <babeltrace/ctf-writer/writer.h> | |
24 | #include <babeltrace/ctf-writer/clock.h> | |
25 | #include <babeltrace/ctf-writer/stream.h> | |
26 | #include <babeltrace/ctf-writer/event.h> | |
27 | #include <babeltrace/ctf-writer/event-types.h> | |
28 | #include <babeltrace/ctf-writer/event-fields.h> | |
29 | #include <babeltrace/ctf/events.h> | |
30 | #include <unistd.h> | |
31 | #include <stdlib.h> | |
32 | #include <stdio.h> | |
33 | #include <sys/utsname.h> | |
22843b66 | 34 | #include <babeltrace/compat/limits.h> |
39d74371 JG |
35 | #include <string.h> |
36 | #include <assert.h> | |
39d74371 JG |
37 | #include <sys/wait.h> |
38 | #include <fcntl.h> | |
39 | #include <dirent.h> | |
40 | #include "tap/tap.h" | |
10817e06 JG |
41 | #include <math.h> |
42 | #include <float.h> | |
39d74371 JG |
43 | |
44 | #define METADATA_LINE_SIZE 512 | |
45 | #define SEQUENCE_TEST_LENGTH 10 | |
1fac895e | 46 | #define ARRAY_TEST_LENGTH 5 |
39d74371 JG |
47 | #define PACKET_RESIZE_TEST_LENGTH 100000 |
48 | ||
5494ce8b JG |
49 | #define DEFAULT_CLOCK_FREQ 1000000000 |
50 | #define DEFAULT_CLOCK_PRECISION 1 | |
51 | #define DEFAULT_CLOCK_OFFSET 0 | |
52 | #define DEFAULT_CLOCK_OFFSET_S 0 | |
53 | #define DEFAULT_CLOCK_IS_ABSOLUTE 0 | |
54 | #define DEFAULT_CLOCK_TIME 0 | |
55 | ||
56 | static uint64_t current_time = 42; | |
39d74371 | 57 | |
e61caf8e JG |
58 | /* Return 1 if uuids match, zero if different. */ |
59 | int uuid_match(const unsigned char *uuid_a, const unsigned char *uuid_b) | |
60 | { | |
61 | int ret = 0; | |
62 | int i; | |
63 | ||
64 | if (!uuid_a || !uuid_b) { | |
65 | goto end; | |
66 | } | |
67 | ||
23f1c913 | 68 | for (i = 0; i < 16; i++) { |
e61caf8e JG |
69 | if (uuid_a[i] != uuid_b[i]) { |
70 | goto end; | |
71 | } | |
72 | } | |
73 | ||
74 | ret = 1; | |
75 | end: | |
76 | return ret; | |
77 | } | |
78 | ||
39d74371 JG |
79 | void validate_metadata(char *parser_path, char *metadata_path) |
80 | { | |
81 | int ret = 0; | |
82 | char parser_output_path[] = "/tmp/parser_output_XXXXXX"; | |
83 | int parser_output_fd = -1, metadata_fd = -1; | |
84 | ||
85 | if (!metadata_path) { | |
86 | ret = -1; | |
87 | goto result; | |
88 | } | |
89 | ||
90 | parser_output_fd = mkstemp(parser_output_path); | |
91 | metadata_fd = open(metadata_path, O_RDONLY); | |
92 | ||
543409b0 | 93 | unlink(parser_output_path); |
39d74371 JG |
94 | |
95 | if (parser_output_fd == -1 || metadata_fd == -1) { | |
96 | diag("Failed create temporary files for metadata parsing."); | |
97 | ret = -1; | |
98 | goto result; | |
99 | } | |
100 | ||
101 | pid_t pid = fork(); | |
102 | if (pid) { | |
103 | int status = 0; | |
104 | waitpid(pid, &status, 0); | |
105 | ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1; | |
106 | } else { | |
107 | /* ctf-parser-test expects a metadata string on stdin. */ | |
108 | ret = dup2(metadata_fd, STDIN_FILENO); | |
109 | if (ret < 0) { | |
110 | perror("# dup2 metadata_fd to STDIN"); | |
111 | goto result; | |
112 | } | |
113 | ||
114 | ret = dup2(parser_output_fd, STDOUT_FILENO); | |
115 | if (ret < 0) { | |
116 | perror("# dup2 parser_output_fd to STDOUT"); | |
117 | goto result; | |
118 | } | |
119 | ||
120 | ret = dup2(parser_output_fd, STDERR_FILENO); | |
121 | if (ret < 0) { | |
122 | perror("# dup2 parser_output_fd to STDERR"); | |
123 | goto result; | |
124 | } | |
125 | ||
126 | execl(parser_path, "ctf-parser-test", NULL); | |
127 | perror("# Could not launch the ctf metadata parser process"); | |
128 | exit(-1); | |
129 | } | |
130 | result: | |
131 | ok(ret == 0, "Metadata string is valid"); | |
132 | ||
25afa9e5 | 133 | if (ret && metadata_fd >= 0 && parser_output_fd >= 0) { |
39d74371 JG |
134 | char *line; |
135 | size_t len = METADATA_LINE_SIZE; | |
136 | FILE *metadata_fp = NULL, *parser_output_fp = NULL; | |
137 | ||
138 | metadata_fp = fdopen(metadata_fd, "r"); | |
139 | if (!metadata_fp) { | |
140 | perror("fdopen on metadata_fd"); | |
141 | goto close_fp; | |
142 | } | |
e1d776cd | 143 | metadata_fd = -1; |
39d74371 JG |
144 | |
145 | parser_output_fp = fdopen(parser_output_fd, "r"); | |
146 | if (!parser_output_fp) { | |
147 | perror("fdopen on parser_output_fd"); | |
148 | goto close_fp; | |
149 | } | |
e1d776cd | 150 | parser_output_fd = -1; |
39d74371 JG |
151 | |
152 | line = malloc(len); | |
153 | if (!line) { | |
154 | diag("malloc failure"); | |
155 | } | |
156 | ||
157 | rewind(metadata_fp); | |
158 | ||
159 | /* Output the metadata and parser output as diagnostic */ | |
160 | while (getline(&line, &len, metadata_fp) > 0) { | |
161 | diag("%s", line); | |
162 | } | |
163 | ||
164 | rewind(parser_output_fp); | |
165 | while (getline(&line, &len, parser_output_fp) > 0) { | |
166 | diag("%s", line); | |
167 | } | |
168 | ||
169 | free(line); | |
170 | close_fp: | |
e1d776cd MD |
171 | if (metadata_fp) { |
172 | if (fclose(metadata_fp)) { | |
173 | diag("fclose failure"); | |
174 | } | |
175 | } | |
176 | if (parser_output_fp) { | |
177 | if (fclose(parser_output_fp)) { | |
178 | diag("fclose failure"); | |
179 | } | |
180 | } | |
39d74371 JG |
181 | } |
182 | ||
25afa9e5 | 183 | if (parser_output_fd >= 0) { |
e1d776cd MD |
184 | if (close(parser_output_fd)) { |
185 | diag("close error"); | |
186 | } | |
187 | } | |
25afa9e5 | 188 | if (metadata_fd >= 0) { |
e1d776cd MD |
189 | if (close(metadata_fd)) { |
190 | diag("close error"); | |
191 | } | |
192 | } | |
39d74371 JG |
193 | } |
194 | ||
195 | void validate_trace(char *parser_path, char *trace_path) | |
196 | { | |
197 | int ret = 0; | |
198 | char babeltrace_output_path[] = "/tmp/babeltrace_output_XXXXXX"; | |
199 | int babeltrace_output_fd = -1; | |
200 | ||
201 | if (!trace_path) { | |
202 | ret = -1; | |
203 | goto result; | |
204 | } | |
205 | ||
206 | babeltrace_output_fd = mkstemp(babeltrace_output_path); | |
543409b0 | 207 | unlink(babeltrace_output_path); |
39d74371 JG |
208 | |
209 | if (babeltrace_output_fd == -1) { | |
210 | diag("Failed to create a temporary file for trace parsing."); | |
211 | ret = -1; | |
212 | goto result; | |
213 | } | |
214 | ||
215 | pid_t pid = fork(); | |
216 | if (pid) { | |
217 | int status = 0; | |
218 | waitpid(pid, &status, 0); | |
219 | ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1; | |
220 | } else { | |
221 | ret = dup2(babeltrace_output_fd, STDOUT_FILENO); | |
222 | if (ret < 0) { | |
223 | perror("# dup2 babeltrace_output_fd to STDOUT"); | |
224 | goto result; | |
225 | } | |
226 | ||
227 | ret = dup2(babeltrace_output_fd, STDERR_FILENO); | |
228 | if (ret < 0) { | |
229 | perror("# dup2 babeltrace_output_fd to STDERR"); | |
230 | goto result; | |
231 | } | |
232 | ||
233 | execl(parser_path, "babeltrace", trace_path, NULL); | |
234 | perror("# Could not launch the babeltrace process"); | |
235 | exit(-1); | |
236 | } | |
237 | result: | |
238 | ok(ret == 0, "Babeltrace could read the resulting trace"); | |
239 | ||
25afa9e5 | 240 | if (ret && babeltrace_output_fd >= 0) { |
39d74371 JG |
241 | char *line; |
242 | size_t len = METADATA_LINE_SIZE; | |
243 | FILE *babeltrace_output_fp = NULL; | |
244 | ||
245 | babeltrace_output_fp = fdopen(babeltrace_output_fd, "r"); | |
246 | if (!babeltrace_output_fp) { | |
247 | perror("fdopen on babeltrace_output_fd"); | |
248 | goto close_fp; | |
249 | } | |
e1d776cd | 250 | babeltrace_output_fd = -1; |
39d74371 JG |
251 | |
252 | line = malloc(len); | |
e1d776cd MD |
253 | if (!line) { |
254 | diag("malloc error"); | |
255 | } | |
39d74371 JG |
256 | rewind(babeltrace_output_fp); |
257 | while (getline(&line, &len, babeltrace_output_fp) > 0) { | |
258 | diag("%s", line); | |
259 | } | |
260 | ||
261 | free(line); | |
262 | close_fp: | |
e1d776cd MD |
263 | if (babeltrace_output_fp) { |
264 | if (fclose(babeltrace_output_fp)) { | |
265 | diag("fclose error"); | |
266 | } | |
267 | } | |
39d74371 JG |
268 | } |
269 | ||
25afa9e5 | 270 | if (babeltrace_output_fd >= 0) { |
e1d776cd MD |
271 | if (close(babeltrace_output_fd)) { |
272 | diag("close error"); | |
273 | } | |
274 | } | |
39d74371 JG |
275 | } |
276 | ||
277 | void append_simple_event(struct bt_ctf_stream_class *stream_class, | |
278 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) | |
279 | { | |
280 | /* Create and add a simple event class */ | |
281 | struct bt_ctf_event_class *simple_event_class = | |
282 | bt_ctf_event_class_create("Simple Event"); | |
283 | struct bt_ctf_field_type *uint_12_type = | |
284 | bt_ctf_field_type_integer_create(12); | |
7cfd41d6 JG |
285 | struct bt_ctf_field_type *int_64_type = |
286 | bt_ctf_field_type_integer_create(64); | |
39d74371 JG |
287 | struct bt_ctf_field_type *float_type = |
288 | bt_ctf_field_type_floating_point_create(); | |
7cfd41d6 JG |
289 | struct bt_ctf_field_type *enum_type; |
290 | struct bt_ctf_field_type *enum_type_unsigned = | |
8382544f | 291 | bt_ctf_field_type_enumeration_create(uint_12_type); |
5edae678 JG |
292 | struct bt_ctf_field_type *event_context_type = |
293 | bt_ctf_field_type_structure_create(); | |
7cfd41d6 | 294 | struct bt_ctf_field_type *returned_type; |
39d74371 JG |
295 | struct bt_ctf_event *simple_event; |
296 | struct bt_ctf_field *integer_field; | |
297 | struct bt_ctf_field *float_field; | |
8382544f | 298 | struct bt_ctf_field *enum_field; |
7cfd41d6 | 299 | struct bt_ctf_field *enum_field_unsigned; |
8382544f | 300 | struct bt_ctf_field *enum_container_field; |
10817e06 | 301 | const char *mapping_name_test = "truie"; |
10817e06 | 302 | const double double_test_value = 3.1415; |
7cfd41d6 JG |
303 | struct bt_ctf_field *enum_container_field_unsigned; |
304 | const char *mapping_name_negative_test = "negative_value"; | |
305 | const char *ret_char; | |
10817e06 | 306 | double ret_double; |
7cfd41d6 JG |
307 | int64_t ret_range_start_int64_t, ret_range_end_int64_t; |
308 | uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t; | |
1ff9582c | 309 | struct bt_ctf_clock *ret_clock; |
e3c971da | 310 | struct bt_ctf_event_class *ret_event_class; |
12c8a1a3 JG |
311 | struct bt_ctf_field *packet_context; |
312 | struct bt_ctf_field *packet_context_field; | |
5edae678 JG |
313 | struct bt_ctf_field *stream_event_context; |
314 | struct bt_ctf_field *stream_event_context_field; | |
6e1f8ea1 JG |
315 | struct bt_ctf_field *event_context; |
316 | struct bt_ctf_field *event_context_field; | |
7cfd41d6 JG |
317 | |
318 | ok(uint_12_type, "Create an unsigned integer type"); | |
319 | ||
320 | bt_ctf_field_type_integer_set_signed(int_64_type, 1); | |
321 | ok(int_64_type, "Create a signed integer type"); | |
322 | enum_type = bt_ctf_field_type_enumeration_create(int_64_type); | |
323 | ||
324 | returned_type = bt_ctf_field_type_enumeration_get_container_type(enum_type); | |
325 | ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_type returns the right type"); | |
326 | ok(!bt_ctf_field_type_enumeration_get_container_type(NULL), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly"); | |
327 | ok(!bt_ctf_field_type_enumeration_create(enum_type), | |
328 | "bt_ctf_field_enumeration_type_create rejects non-integer container field types"); | |
5edae678 | 329 | bt_ctf_field_type_put(returned_type); |
39d74371 JG |
330 | |
331 | bt_ctf_field_type_set_alignment(float_type, 32); | |
7cfd41d6 JG |
332 | ok(bt_ctf_field_type_get_alignment(NULL) < 0, |
333 | "bt_ctf_field_type_get_alignment handles NULL correctly"); | |
334 | ok(bt_ctf_field_type_get_alignment(float_type) == 32, | |
335 | "bt_ctf_field_type_get_alignment returns a correct value"); | |
336 | ||
337 | ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0, | |
338 | "Set a floating point type's exponent digit count"); | |
339 | ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0, | |
340 | "Set a floating point type's mantissa digit count"); | |
341 | ||
342 | ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL) < 0, | |
343 | "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly"); | |
344 | ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL) < 0, | |
345 | "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly"); | |
346 | ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11, | |
347 | "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value"); | |
348 | ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53, | |
349 | "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value"); | |
8382544f JG |
350 | |
351 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, | |
7cfd41d6 JG |
352 | mapping_name_negative_test, -12345, 0) == 0, |
353 | "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings"); | |
8382544f | 354 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
7cfd41d6 JG |
355 | "escaping; \"test\"", 1, 1) == 0, |
356 | "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes"); | |
357 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, | |
358 | "\tanother \'escaping\'\n test\"", 2, 4) == 0, | |
359 | "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters"); | |
8382544f JG |
360 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
361 | "event clock int float", 5, 22) == 0, | |
362 | "Accept enumeration mapping strings containing reserved keywords"); | |
7cfd41d6 JG |
363 | bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, |
364 | 42, 42); | |
365 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, | |
366 | 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names"); | |
367 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something", | |
368 | -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries"); | |
369 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, | |
370 | -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start"); | |
371 | bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000); | |
372 | ||
074ee56d | 373 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL, -42) < 0, |
7cfd41d6 | 374 | "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly"); |
074ee56d | 375 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, 1000000) < 0, |
7cfd41d6 | 376 | "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly"); |
074ee56d | 377 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -55) == 1, |
7cfd41d6 JG |
378 | "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index"); |
379 | ||
8382544f | 380 | ok(bt_ctf_event_class_add_field(simple_event_class, enum_type, |
7cfd41d6 JG |
381 | "enum_field") == 0, "Add signed enumeration field to event"); |
382 | ||
383 | ok(bt_ctf_field_type_enumeration_get_mapping(NULL, 0, &ret_char, | |
384 | &ret_range_start_int64_t, &ret_range_end_int64_t) < 0, | |
385 | "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly"); | |
386 | ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, NULL, | |
387 | &ret_range_start_int64_t, &ret_range_end_int64_t) < 0, | |
388 | "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly"); | |
389 | ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char, | |
390 | NULL, &ret_range_end_int64_t) < 0, | |
391 | "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly"); | |
392 | ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char, | |
393 | &ret_range_start_int64_t, NULL) < 0, | |
394 | "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly"); | |
395 | ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 5, &ret_char, | |
396 | &ret_range_start_int64_t, &ret_range_end_int64_t) == 0, | |
397 | "bt_ctf_field_type_enumeration_get_mapping returns a value"); | |
398 | ok(!strcmp(ret_char, mapping_name_test), | |
399 | "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name"); | |
400 | ok(ret_range_start_int64_t == 42, | |
401 | "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start"); | |
402 | ok(ret_range_end_int64_t == 42, | |
403 | "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end"); | |
404 | ||
405 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, | |
406 | "escaping; \"test\"", 0, 0) == 0, | |
407 | "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes"); | |
408 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, | |
409 | "\tanother \'escaping\'\n test\"", 1, 4) == 0, | |
410 | "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters"); | |
411 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, | |
412 | "event clock int float", 5, 22) == 0, | |
413 | "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords"); | |
414 | bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, | |
415 | 42, 42); | |
416 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, | |
417 | 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names"); | |
418 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something", | |
419 | 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries"); | |
420 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, | |
421 | 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start"); | |
422 | ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned, | |
423 | "enum_field_unsigned") == 0, "Add unsigned enumeration field to event"); | |
424 | ||
425 | ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL) < 0, | |
426 | "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly"); | |
427 | ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 4, | |
428 | "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value"); | |
429 | ||
430 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char, | |
431 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0, | |
432 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly"); | |
433 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL, | |
434 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0, | |
435 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly"); | |
436 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char, | |
437 | NULL, &ret_range_end_uint64_t) < 0, | |
438 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly"); | |
439 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char, | |
440 | &ret_range_start_uint64_t, NULL) < 0, | |
441 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly"); | |
442 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 3, &ret_char, | |
443 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0, | |
444 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value"); | |
445 | ok(!strcmp(ret_char, mapping_name_test), | |
446 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name"); | |
447 | ok(ret_range_start_uint64_t == 42, | |
448 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start"); | |
449 | ok(ret_range_end_uint64_t == 42, | |
450 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end"); | |
8382544f | 451 | |
39d74371 JG |
452 | bt_ctf_event_class_add_field(simple_event_class, uint_12_type, |
453 | "integer_field"); | |
454 | bt_ctf_event_class_add_field(simple_event_class, float_type, | |
455 | "float_field"); | |
456 | bt_ctf_stream_class_add_event_class(stream_class, | |
457 | simple_event_class); | |
458 | ||
e3c971da JG |
459 | ok(bt_ctf_stream_class_get_event_class_count(NULL) < 0, |
460 | "bt_ctf_stream_class_get_event_class_count handles NULL correctly"); | |
461 | ok(bt_ctf_stream_class_get_event_class_count(stream_class) == 1, | |
462 | "bt_ctf_stream_class_get_event_class_count returns a correct number of event classes"); | |
463 | ok(bt_ctf_stream_class_get_event_class(NULL, 0) == NULL, | |
464 | "bt_ctf_stream_class_get_event_class handles NULL correctly"); | |
465 | ok(bt_ctf_stream_class_get_event_class(stream_class, 8724) == NULL, | |
466 | "bt_ctf_stream_class_get_event_class handles invalid indexes correctly"); | |
467 | ret_event_class = bt_ctf_stream_class_get_event_class(stream_class, 0); | |
468 | ok(ret_event_class == simple_event_class, | |
469 | "bt_ctf_stream_class_get_event_class returns the correct event class"); | |
470 | bt_ctf_event_class_put(ret_event_class); | |
471 | ||
472 | ok(bt_ctf_stream_class_get_event_class_by_name(NULL, "some event name") == NULL, | |
473 | "bt_ctf_stream_class_get_event_class_by_name handles a NULL stream class correctly"); | |
474 | ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, NULL) == NULL, | |
475 | "bt_ctf_stream_class_get_event_class_by_name handles a NULL event class name correctly"); | |
476 | ok(bt_ctf_stream_class_get_event_class_by_name(stream_class, "some event name") == NULL, | |
477 | "bt_ctf_stream_class_get_event_class_by_name handles non-existing event class names correctly"); | |
5edae678 | 478 | ret_event_class = bt_ctf_stream_class_get_event_class_by_name(stream_class, "Simple Event"); |
e3c971da JG |
479 | ok(ret_event_class == simple_event_class, |
480 | "bt_ctf_stream_class_get_event_class_by_name returns a correct event class"); | |
481 | bt_ctf_event_class_put(ret_event_class); | |
482 | ||
5edae678 | 483 | /* Set an event context type which will contain a single integer*/ |
8c627304 JG |
484 | ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type, |
485 | "event_specific_context"), | |
486 | "Add event specific context field"); | |
5edae678 JG |
487 | ok(bt_ctf_event_class_get_context_type(NULL) == NULL, |
488 | "bt_ctf_event_class_get_context_type handles NULL correctly"); | |
489 | ok(bt_ctf_event_class_get_context_type(simple_event_class) == NULL, | |
490 | "bt_ctf_event_class_get_context_type returns NULL when no event context type is set"); | |
491 | ok(bt_ctf_event_class_set_context_type(simple_event_class, NULL) < 0, | |
492 | "bt_ctf_event_class_set_context_type handles a NULL context type correctly"); | |
493 | ok(bt_ctf_event_class_set_context_type(NULL, event_context_type) < 0, | |
494 | "bt_ctf_event_class_set_context_type handles a NULL event class correctly"); | |
495 | ok(!bt_ctf_event_class_set_context_type(simple_event_class, event_context_type), | |
496 | "Set an event class' context type successfully"); | |
497 | returned_type = bt_ctf_event_class_get_context_type(simple_event_class); | |
498 | ok(returned_type == event_context_type, | |
499 | "bt_ctf_event_class_get_context_type returns the appropriate type"); | |
500 | bt_ctf_field_type_put(returned_type); | |
501 | ||
39d74371 | 502 | simple_event = bt_ctf_event_create(simple_event_class); |
39d74371 JG |
503 | ok(simple_event, |
504 | "Instantiate an event containing a single integer field"); | |
505 | ||
1ff9582c JG |
506 | ok(bt_ctf_event_get_clock(NULL) == NULL, |
507 | "bt_ctf_event_get_clock handles NULL correctly"); | |
508 | ret_clock = bt_ctf_event_get_clock(simple_event); | |
509 | ok(ret_clock == clock, | |
510 | "bt_ctf_event_get_clock returns a correct clock"); | |
511 | bt_ctf_clock_put(clock); | |
512 | ||
39d74371 JG |
513 | integer_field = bt_ctf_field_create(uint_12_type); |
514 | bt_ctf_field_unsigned_integer_set_value(integer_field, 42); | |
515 | ok(bt_ctf_event_set_payload(simple_event, "integer_field", | |
516 | integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field"); | |
517 | ||
518 | float_field = bt_ctf_event_get_payload(simple_event, "float_field"); | |
10817e06 JG |
519 | ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double), |
520 | "bt_ctf_field_floating_point_get_value fails on an unset float field"); | |
521 | bt_ctf_field_floating_point_set_value(float_field, double_test_value); | |
522 | ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double), | |
523 | "bt_ctf_field_floating_point_get_value properly handles a NULL field"); | |
524 | ok(bt_ctf_field_floating_point_get_value(float_field, NULL), | |
525 | "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer"); | |
526 | ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double), | |
527 | "bt_ctf_field_floating_point_get_value returns a double value"); | |
528 | ok(fabs(ret_double - double_test_value) <= DBL_EPSILON, | |
529 | "bt_ctf_field_floating_point_get_value returns a correct value"); | |
530 | ||
8382544f | 531 | enum_field = bt_ctf_field_create(enum_type); |
7cfd41d6 JG |
532 | ret_char = bt_ctf_field_enumeration_get_mapping_name(NULL); |
533 | ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly"); | |
534 | ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field); | |
535 | ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset"); | |
8382544f JG |
536 | enum_container_field = bt_ctf_field_enumeration_get_container( |
537 | enum_field); | |
7cfd41d6 JG |
538 | ok(bt_ctf_field_signed_integer_set_value( |
539 | enum_container_field, -42) == 0, | |
540 | "Set signed enumeration container value"); | |
541 | ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field); | |
542 | ok(!strcmp(ret_char, mapping_name_negative_test), | |
543 | "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container"); | |
8382544f | 544 | bt_ctf_event_set_payload(simple_event, "enum_field", enum_field); |
39d74371 | 545 | |
7cfd41d6 JG |
546 | enum_field_unsigned = bt_ctf_field_create(enum_type_unsigned); |
547 | enum_container_field_unsigned = bt_ctf_field_enumeration_get_container( | |
548 | enum_field_unsigned); | |
549 | ok(bt_ctf_field_unsigned_integer_set_value( | |
550 | enum_container_field_unsigned, 42) == 0, | |
551 | "Set unsigned enumeration container value"); | |
552 | bt_ctf_event_set_payload(simple_event, "enum_field_unsigned", | |
553 | enum_field_unsigned); | |
554 | ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned); | |
555 | ok(!strcmp(ret_char, mapping_name_test), | |
556 | "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container"); | |
557 | ||
39d74371 JG |
558 | ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time"); |
559 | ||
6e1f8ea1 | 560 | /* Populate stream event context */ |
5edae678 JG |
561 | stream_event_context = bt_ctf_stream_get_event_context(stream); |
562 | stream_event_context_field = bt_ctf_field_structure_get_field( | |
563 | stream_event_context, "common_event_context"); | |
564 | bt_ctf_field_unsigned_integer_set_value(stream_event_context_field, 42); | |
565 | ||
566 | /* Populate the event's context */ | |
567 | ok(bt_ctf_event_get_event_context(NULL) == NULL, | |
568 | "bt_ctf_event_get_event_context handles NULL correctly"); | |
569 | event_context = bt_ctf_event_get_event_context(simple_event); | |
570 | ok(event_context, | |
571 | "bt_ctf_event_get_event_context returns a field"); | |
572 | returned_type = bt_ctf_field_get_type(event_context); | |
573 | ok(returned_type == event_context_type, | |
574 | "bt_ctf_event_get_event_context returns a field of the appropriate type"); | |
575 | event_context_field = bt_ctf_field_structure_get_field(event_context, | |
576 | "event_specific_context"); | |
577 | ok(!bt_ctf_field_unsigned_integer_set_value(event_context_field, 1234), | |
578 | "Successfully set an event context's value"); | |
579 | ok(bt_ctf_event_set_event_context(NULL, event_context) < 0, | |
580 | "bt_ctf_event_set_event_context handles a NULL event correctly"); | |
581 | ok(bt_ctf_event_set_event_context(simple_event, NULL) < 0, | |
582 | "bt_ctf_event_set_event_context handles a NULL event context correctly"); | |
583 | ok(bt_ctf_event_set_event_context(simple_event, event_context_field) < 0, | |
584 | "bt_ctf_event_set_event_context rejects a context of the wrong type"); | |
585 | ok(!bt_ctf_event_set_event_context(simple_event, event_context), | |
586 | "Set an event context successfully"); | |
6e1f8ea1 | 587 | |
39d74371 JG |
588 | ok(bt_ctf_stream_append_event(stream, simple_event) == 0, |
589 | "Append simple event to trace stream"); | |
590 | ||
12c8a1a3 JG |
591 | ok(bt_ctf_stream_get_packet_context(NULL) == NULL, |
592 | "bt_ctf_stream_get_packet_context handles NULL correctly"); | |
593 | packet_context = bt_ctf_stream_get_packet_context(stream); | |
594 | ok(packet_context, | |
595 | "bt_ctf_stream_get_packet_context returns a packet context"); | |
596 | ||
597 | packet_context_field = bt_ctf_field_structure_get_field(packet_context, | |
598 | "packet_size"); | |
599 | ok(packet_context_field, | |
600 | "Packet context contains the default packet_size field."); | |
601 | bt_ctf_field_put(packet_context_field); | |
602 | packet_context_field = bt_ctf_field_structure_get_field(packet_context, | |
35e8709f | 603 | "custom_packet_context_field"); |
12c8a1a3 JG |
604 | ok(bt_ctf_field_unsigned_integer_set_value(packet_context_field, 8) == 0, |
605 | "Custom packet context field value successfully set."); | |
606 | ||
607 | ok(bt_ctf_stream_set_packet_context(NULL, packet_context_field) < 0, | |
608 | "bt_ctf_stream_set_packet_context handles a NULL stream correctly"); | |
609 | ok(bt_ctf_stream_set_packet_context(stream, NULL) < 0, | |
610 | "bt_ctf_stream_set_packet_context handles a NULL packet context correctly"); | |
611 | ok(bt_ctf_stream_set_packet_context(stream, packet_context) == 0, | |
612 | "Successfully set a stream's packet context"); | |
613 | ||
39d74371 JG |
614 | ok(bt_ctf_stream_flush(stream) == 0, |
615 | "Flush trace stream with one event"); | |
616 | ||
617 | bt_ctf_event_class_put(simple_event_class); | |
618 | bt_ctf_event_put(simple_event); | |
619 | bt_ctf_field_type_put(uint_12_type); | |
7cfd41d6 | 620 | bt_ctf_field_type_put(int_64_type); |
39d74371 | 621 | bt_ctf_field_type_put(float_type); |
8382544f | 622 | bt_ctf_field_type_put(enum_type); |
7cfd41d6 JG |
623 | bt_ctf_field_type_put(enum_type_unsigned); |
624 | bt_ctf_field_type_put(returned_type); | |
5edae678 | 625 | bt_ctf_field_type_put(event_context_type); |
39d74371 JG |
626 | bt_ctf_field_put(integer_field); |
627 | bt_ctf_field_put(float_field); | |
8382544f | 628 | bt_ctf_field_put(enum_field); |
7cfd41d6 | 629 | bt_ctf_field_put(enum_field_unsigned); |
8382544f | 630 | bt_ctf_field_put(enum_container_field); |
7cfd41d6 | 631 | bt_ctf_field_put(enum_container_field_unsigned); |
12c8a1a3 JG |
632 | bt_ctf_field_put(packet_context); |
633 | bt_ctf_field_put(packet_context_field); | |
5edae678 JG |
634 | bt_ctf_field_put(stream_event_context); |
635 | bt_ctf_field_put(stream_event_context_field); | |
6e1f8ea1 JG |
636 | bt_ctf_field_put(event_context); |
637 | bt_ctf_field_put(event_context_field); | |
39d74371 JG |
638 | } |
639 | ||
640 | void append_complex_event(struct bt_ctf_stream_class *stream_class, | |
641 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) | |
642 | { | |
643 | int i; | |
1ff9582c | 644 | const char *complex_test_event_string = "Complex Test Event"; |
10817e06 | 645 | const char *test_string = "Test string"; |
39d74371 JG |
646 | struct bt_ctf_field_type *uint_35_type = |
647 | bt_ctf_field_type_integer_create(35); | |
648 | struct bt_ctf_field_type *int_16_type = | |
649 | bt_ctf_field_type_integer_create(16); | |
650 | struct bt_ctf_field_type *uint_3_type = | |
651 | bt_ctf_field_type_integer_create(3); | |
652 | struct bt_ctf_field_type *enum_variant_type = | |
653 | bt_ctf_field_type_enumeration_create(uint_3_type); | |
654 | struct bt_ctf_field_type *variant_type = | |
655 | bt_ctf_field_type_variant_create(enum_variant_type, | |
656 | "variant_selector"); | |
657 | struct bt_ctf_field_type *string_type = | |
658 | bt_ctf_field_type_string_create(); | |
659 | struct bt_ctf_field_type *sequence_type; | |
1fac895e | 660 | struct bt_ctf_field_type *array_type; |
39d74371 JG |
661 | struct bt_ctf_field_type *inner_structure_type = |
662 | bt_ctf_field_type_structure_create(); | |
663 | struct bt_ctf_field_type *complex_structure_type = | |
664 | bt_ctf_field_type_structure_create(); | |
7cfd41d6 | 665 | struct bt_ctf_field_type *ret_field_type; |
39d74371 JG |
666 | struct bt_ctf_event_class *event_class; |
667 | struct bt_ctf_event *event; | |
668 | struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field, | |
669 | *inner_structure_field, *complex_structure_field, | |
670 | *a_sequence_field, *enum_variant_field, *enum_container_field, | |
1fac895e | 671 | *variant_field, *an_array_field, *ret_field; |
10817e06 | 672 | uint64_t ret_unsigned_int; |
1fac895e | 673 | int64_t ret_signed_int; |
10817e06 | 674 | const char *ret_string; |
1ff9582c JG |
675 | struct bt_ctf_stream_class *ret_stream_class; |
676 | struct bt_ctf_event_class *ret_event_class; | |
12c8a1a3 | 677 | struct bt_ctf_field *packet_context, *packet_context_field; |
39d74371 JG |
678 | |
679 | bt_ctf_field_type_set_alignment(int_16_type, 32); | |
680 | bt_ctf_field_type_integer_set_signed(int_16_type, 1); | |
681 | bt_ctf_field_type_integer_set_base(uint_35_type, | |
682 | BT_CTF_INTEGER_BASE_HEXADECIMAL); | |
683 | ||
1fac895e | 684 | array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH); |
39d74371 JG |
685 | sequence_type = bt_ctf_field_type_sequence_create(int_16_type, |
686 | "seq_len"); | |
7cfd41d6 JG |
687 | |
688 | ok(bt_ctf_field_type_array_get_element_type(NULL) == NULL, | |
689 | "bt_ctf_field_type_array_get_element_type handles NULL correctly"); | |
690 | ret_field_type = bt_ctf_field_type_array_get_element_type( | |
691 | array_type); | |
692 | ok(ret_field_type == int_16_type, | |
693 | "bt_ctf_field_type_array_get_element_type returns the correct type"); | |
694 | bt_ctf_field_type_put(ret_field_type); | |
695 | ||
696 | ok(bt_ctf_field_type_array_get_length(NULL) < 0, | |
697 | "bt_ctf_field_type_array_get_length handles NULL correctly"); | |
698 | ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH, | |
699 | "bt_ctf_field_type_array_get_length returns the correct length"); | |
700 | ||
39d74371 JG |
701 | bt_ctf_field_type_structure_add_field(inner_structure_type, |
702 | uint_35_type, "seq_len"); | |
703 | bt_ctf_field_type_structure_add_field(inner_structure_type, | |
704 | sequence_type, "a_sequence"); | |
1fac895e JG |
705 | bt_ctf_field_type_structure_add_field(inner_structure_type, |
706 | array_type, "an_array"); | |
39d74371 JG |
707 | |
708 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
709 | "UINT3_TYPE", 0, 0); | |
710 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
711 | "INT16_TYPE", 1, 1); | |
712 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
713 | "UINT35_TYPE", 2, 7); | |
7cfd41d6 | 714 | |
074ee56d JG |
715 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL, |
716 | "INT16_TYPE") < 0, | |
7cfd41d6 | 717 | "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly"); |
074ee56d JG |
718 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name( |
719 | enum_variant_type, NULL) < 0, | |
7cfd41d6 | 720 | "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly"); |
074ee56d JG |
721 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name( |
722 | enum_variant_type, "INT16_TYPE") == 1, | |
7cfd41d6 JG |
723 | "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index"); |
724 | ||
074ee56d | 725 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL, 1) < 0, |
7cfd41d6 | 726 | "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly"); |
074ee56d | 727 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, -42) < 0, |
7cfd41d6 | 728 | "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly"); |
074ee56d | 729 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 5) == 2, |
7cfd41d6 JG |
730 | "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index"); |
731 | ||
39d74371 JG |
732 | ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type, |
733 | "An unknown entry"), "Reject a variant field based on an unknown tag value"); | |
734 | ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type, | |
735 | "UINT3_TYPE") == 0, "Add a field to a variant"); | |
736 | bt_ctf_field_type_variant_add_field(variant_type, int_16_type, | |
737 | "INT16_TYPE"); | |
738 | bt_ctf_field_type_variant_add_field(variant_type, uint_35_type, | |
739 | "UINT35_TYPE"); | |
740 | ||
7cfd41d6 JG |
741 | ok(bt_ctf_field_type_variant_get_tag_type(NULL) == NULL, |
742 | "bt_ctf_field_type_variant_get_tag_type handles NULL correctly"); | |
743 | ret_field_type = bt_ctf_field_type_variant_get_tag_type(variant_type); | |
744 | ok(ret_field_type == enum_variant_type, | |
745 | "bt_ctf_field_type_variant_get_tag_type returns a correct tag type"); | |
746 | bt_ctf_field_type_put(ret_field_type); | |
747 | ||
748 | ok(bt_ctf_field_type_variant_get_tag_name(NULL) == NULL, | |
749 | "bt_ctf_field_type_variant_get_tag_name handles NULL correctly"); | |
750 | ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type); | |
751 | ok(!strcmp(ret_string, "variant_selector"), | |
752 | "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name"); | |
753 | ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL, | |
754 | "INT16_TYPE") == NULL, | |
755 | "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly"); | |
756 | ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type, | |
757 | NULL) == NULL, | |
758 | "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly"); | |
759 | ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name( | |
760 | variant_type, "INT16_TYPE"); | |
761 | ok(ret_field_type == int_16_type, | |
762 | "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type"); | |
763 | bt_ctf_field_type_put(ret_field_type); | |
764 | ||
765 | ok(bt_ctf_field_type_variant_get_field_count(NULL) < 0, | |
766 | "bt_ctf_field_type_variant_get_field_count handles NULL correctly"); | |
767 | ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3, | |
768 | "bt_ctf_field_type_variant_get_field_count returns the correct count"); | |
769 | ||
770 | ok(bt_ctf_field_type_variant_get_field(NULL, &ret_string, &ret_field_type, 0) < 0, | |
771 | "bt_ctf_field_type_variant_get_field handles a NULL type correctly"); | |
772 | ok(bt_ctf_field_type_variant_get_field(variant_type, NULL, &ret_field_type, 0) < 0, | |
773 | "bt_ctf_field_type_variant_get_field handles a NULL field name correctly"); | |
774 | ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, NULL, 0) < 0, | |
775 | "bt_ctf_field_type_variant_get_field handles a NULL field type correctly"); | |
776 | ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 200) < 0, | |
777 | "bt_ctf_field_type_variant_get_field handles an invalid index correctly"); | |
778 | ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 1) == 0, | |
779 | "bt_ctf_field_type_variant_get_field returns a field"); | |
780 | ok(!strcmp("INT16_TYPE", ret_string), | |
781 | "bt_ctf_field_type_variant_get_field returns a correct field name"); | |
782 | ok(ret_field_type == int_16_type, | |
783 | "bt_ctf_field_type_variant_get_field returns a correct field type"); | |
784 | bt_ctf_field_type_put(ret_field_type); | |
785 | ||
39d74371 JG |
786 | bt_ctf_field_type_structure_add_field(complex_structure_type, |
787 | enum_variant_type, "variant_selector"); | |
788 | bt_ctf_field_type_structure_add_field(complex_structure_type, | |
789 | string_type, "a_string"); | |
790 | bt_ctf_field_type_structure_add_field(complex_structure_type, | |
791 | variant_type, "variant_value"); | |
792 | bt_ctf_field_type_structure_add_field(complex_structure_type, | |
793 | inner_structure_type, "inner_structure"); | |
794 | ||
795 | ok(bt_ctf_event_class_create("clock") == NULL, | |
796 | "Reject creation of an event class with an illegal name"); | |
1ff9582c | 797 | event_class = bt_ctf_event_class_create(complex_test_event_string); |
39d74371 JG |
798 | ok(event_class, "Create an event class"); |
799 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""), | |
800 | "Reject addition of a field with an empty name to an event"); | |
801 | ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"), | |
802 | "Reject addition of a field with a NULL type to an event"); | |
803 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, | |
804 | "int"), | |
805 | "Reject addition of a type with an illegal name to an event"); | |
806 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, | |
807 | "uint_35") == 0, | |
808 | "Add field of type unsigned integer to an event"); | |
809 | ok(bt_ctf_event_class_add_field(event_class, int_16_type, | |
810 | "int_16") == 0, "Add field of type signed integer to an event"); | |
811 | ok(bt_ctf_event_class_add_field(event_class, complex_structure_type, | |
812 | "complex_structure") == 0, | |
813 | "Add composite structure to an event"); | |
814 | ||
1ff9582c JG |
815 | ok(bt_ctf_event_class_get_name(NULL) == NULL, |
816 | "bt_ctf_event_class_get_name handles NULL correctly"); | |
817 | ret_string = bt_ctf_event_class_get_name(event_class); | |
818 | ok(!strcmp(ret_string, complex_test_event_string), | |
819 | "bt_ctf_event_class_get_name returns a correct name"); | |
820 | ok(bt_ctf_event_class_get_id(event_class) < 0, | |
821 | "bt_ctf_event_class_get_id returns a negative value when not set"); | |
822 | ok(bt_ctf_event_class_get_id(NULL) < 0, | |
823 | "bt_ctf_event_class_get_id handles NULL correctly"); | |
824 | ok(bt_ctf_event_class_set_id(NULL, 42) < 0, | |
825 | "bt_ctf_event_class_set_id handles NULL correctly"); | |
826 | ok(bt_ctf_event_class_set_id(event_class, 42) == 0, | |
827 | "Set an event class' id"); | |
828 | ok(bt_ctf_event_class_get_id(event_class) == 42, | |
829 | "bt_ctf_event_class_get_id returns the correct value"); | |
830 | ||
39d74371 JG |
831 | /* Add event class to the stream class */ |
832 | ok(bt_ctf_stream_class_add_event_class(stream_class, NULL), | |
833 | "Reject addition of NULL event class to a stream class"); | |
834 | ok(bt_ctf_stream_class_add_event_class(stream_class, | |
835 | event_class) == 0, "Add an event class to stream class"); | |
836 | ||
1ff9582c JG |
837 | ok(bt_ctf_event_class_get_stream_class(NULL) == NULL, |
838 | "bt_ctf_event_class_get_stream_class handles NULL correctly"); | |
839 | ret_stream_class = bt_ctf_event_class_get_stream_class(event_class); | |
840 | ok(ret_stream_class == stream_class, | |
841 | "bt_ctf_event_class_get_stream_class returns the correct stream class"); | |
842 | bt_ctf_stream_class_put(ret_stream_class); | |
843 | ||
844 | ok(bt_ctf_event_class_get_field_count(NULL) < 0, | |
845 | "bt_ctf_event_class_get_field_count handles NULL correctly"); | |
846 | ok(bt_ctf_event_class_get_field_count(event_class) == 3, | |
847 | "bt_ctf_event_class_get_field_count returns a correct value"); | |
848 | ||
849 | ok(bt_ctf_event_class_get_field(NULL, &ret_string, | |
850 | &ret_field_type, 0) < 0, | |
851 | "bt_ctf_event_class_get_field handles a NULL event class correctly"); | |
852 | ok(bt_ctf_event_class_get_field(event_class, NULL, | |
853 | &ret_field_type, 0) < 0, | |
854 | "bt_ctf_event_class_get_field handles a NULL field name correctly"); | |
855 | ok(bt_ctf_event_class_get_field(event_class, &ret_string, | |
856 | NULL, 0) < 0, | |
857 | "bt_ctf_event_class_get_field handles a NULL field type correctly"); | |
858 | ok(bt_ctf_event_class_get_field(event_class, &ret_string, | |
859 | &ret_field_type, 42) < 0, | |
860 | "bt_ctf_event_class_get_field handles an invalid index correctly"); | |
861 | ok(bt_ctf_event_class_get_field(event_class, &ret_string, | |
862 | &ret_field_type, 0) == 0, | |
863 | "bt_ctf_event_class_get_field returns a field"); | |
864 | ok(ret_field_type == uint_35_type, | |
865 | "bt_ctf_event_class_get_field returns a correct field type"); | |
866 | bt_ctf_field_type_put(ret_field_type); | |
867 | ok(!strcmp(ret_string, "uint_35"), | |
868 | "bt_ctf_event_class_get_field returns a correct field name"); | |
869 | ok(bt_ctf_event_class_get_field_by_name(NULL, "") == NULL, | |
870 | "bt_ctf_event_class_get_field_by_name handles a NULL event class correctly"); | |
871 | ok(bt_ctf_event_class_get_field_by_name(event_class, NULL) == NULL, | |
872 | "bt_ctf_event_class_get_field_by_name handles a NULL field name correctly"); | |
873 | ok(bt_ctf_event_class_get_field_by_name(event_class, "truie") == NULL, | |
874 | "bt_ctf_event_class_get_field_by_name handles an invalid field name correctly"); | |
875 | ret_field_type = bt_ctf_event_class_get_field_by_name(event_class, | |
876 | "complex_structure"); | |
877 | ok(ret_field_type == complex_structure_type, | |
878 | "bt_ctf_event_class_get_field_by_name returns a correct field type"); | |
879 | bt_ctf_field_type_put(ret_field_type); | |
880 | ||
39d74371 JG |
881 | event = bt_ctf_event_create(event_class); |
882 | ok(event, "Instanciate a complex event"); | |
883 | ||
1ff9582c JG |
884 | ok(bt_ctf_event_get_class(NULL) == NULL, |
885 | "bt_ctf_event_get_class handles NULL correctly"); | |
886 | ret_event_class = bt_ctf_event_get_class(event); | |
887 | ok(ret_event_class == event_class, | |
888 | "bt_ctf_event_get_class returns the correct event class"); | |
889 | bt_ctf_event_class_put(ret_event_class); | |
890 | ||
39d74371 | 891 | uint_35_field = bt_ctf_event_get_payload(event, "uint_35"); |
7cfd41d6 | 892 | if (!uint_35_field) { |
10817e06 | 893 | printf("uint_35_field is NULL\n"); |
7cfd41d6 JG |
894 | } |
895 | ||
39d74371 JG |
896 | ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance "); |
897 | bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D); | |
3c1d148b | 898 | ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) < 0, |
10817e06 | 899 | "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field."); |
3c1d148b | 900 | ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) < 0, |
10817e06 JG |
901 | "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value"); |
902 | ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, | |
903 | &ret_unsigned_int) == 0, | |
904 | "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value"); | |
905 | ok(ret_unsigned_int == 0x0DDF00D, | |
906 | "bt_ctf_field_unsigned_integer_get_value returns the correct value"); | |
907 | ok(bt_ctf_field_signed_integer_get_value(uint_35_field, | |
3c1d148b | 908 | &ret_signed_int) < 0, |
10817e06 | 909 | "bt_ctf_field_signed_integer_get_value fails on an unsigned field"); |
39d74371 JG |
910 | bt_ctf_field_put(uint_35_field); |
911 | ||
912 | int_16_field = bt_ctf_event_get_payload(event, "int_16"); | |
913 | bt_ctf_field_signed_integer_set_value(int_16_field, -12345); | |
3c1d148b | 914 | ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) < 0, |
10817e06 | 915 | "bt_ctf_field_signed_integer_get_value properly handles a NULL field"); |
3c1d148b | 916 | ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) < 0, |
10817e06 JG |
917 | "bt_ctf_field_signed_integer_get_value properly handles a NULL return value"); |
918 | ok(bt_ctf_field_signed_integer_get_value(int_16_field, | |
919 | &ret_signed_int) == 0, | |
920 | "bt_ctf_field_signed_integer_get_value succeeds after setting a value"); | |
921 | ok(ret_signed_int == -12345, | |
922 | "bt_ctf_field_signed_integer_get_value returns the correct value"); | |
923 | ok(bt_ctf_field_unsigned_integer_get_value(int_16_field, | |
3c1d148b | 924 | &ret_unsigned_int) < 0, |
10817e06 | 925 | "bt_ctf_field_unsigned_integer_get_value fails on a signed field"); |
39d74371 JG |
926 | bt_ctf_field_put(int_16_field); |
927 | ||
928 | complex_structure_field = bt_ctf_event_get_payload(event, | |
929 | "complex_structure"); | |
10817e06 JG |
930 | |
931 | ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL, | |
932 | "bt_ctf_field_structure_get_field_by_index handles NULL correctly"); | |
933 | ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL, | |
934 | "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly"); | |
935 | inner_structure_field = bt_ctf_field_structure_get_field_by_index( | |
936 | complex_structure_field, 3); | |
937 | ret_field_type = bt_ctf_field_get_type(inner_structure_field); | |
938 | bt_ctf_field_put(inner_structure_field); | |
939 | ok(ret_field_type == inner_structure_type, | |
940 | "bt_ctf_field_structure_get_field_by_index returns a correct field"); | |
941 | bt_ctf_field_type_put(ret_field_type); | |
942 | ||
39d74371 JG |
943 | inner_structure_field = bt_ctf_field_structure_get_field( |
944 | complex_structure_field, "inner_structure"); | |
945 | a_string_field = bt_ctf_field_structure_get_field( | |
946 | complex_structure_field, "a_string"); | |
947 | enum_variant_field = bt_ctf_field_structure_get_field( | |
948 | complex_structure_field, "variant_selector"); | |
949 | variant_field = bt_ctf_field_structure_get_field( | |
950 | complex_structure_field, "variant_value"); | |
951 | uint_35_field = bt_ctf_field_structure_get_field( | |
952 | inner_structure_field, "seq_len"); | |
953 | a_sequence_field = bt_ctf_field_structure_get_field( | |
954 | inner_structure_field, "a_sequence"); | |
1fac895e JG |
955 | an_array_field = bt_ctf_field_structure_get_field( |
956 | inner_structure_field, "an_array"); | |
39d74371 JG |
957 | |
958 | enum_container_field = bt_ctf_field_enumeration_get_container( | |
959 | enum_variant_field); | |
960 | bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1); | |
961 | int_16_field = bt_ctf_field_variant_get_field(variant_field, | |
962 | enum_variant_field); | |
963 | bt_ctf_field_signed_integer_set_value(int_16_field, -200); | |
964 | bt_ctf_field_put(int_16_field); | |
10817e06 JG |
965 | ok(!bt_ctf_field_string_get_value(a_string_field), |
966 | "bt_ctf_field_string_get_value returns NULL on an unset field"); | |
39d74371 | 967 | bt_ctf_field_string_set_value(a_string_field, |
10817e06 JG |
968 | test_string); |
969 | ok(!bt_ctf_field_string_get_value(NULL), | |
970 | "bt_ctf_field_string_get_value correctly handles NULL"); | |
971 | ret_string = bt_ctf_field_string_get_value(a_string_field); | |
972 | ok(ret_string, "bt_ctf_field_string_get_value returns a string"); | |
d50c7132 | 973 | ok(ret_string ? !strcmp(ret_string, test_string) : 0, |
10817e06 | 974 | "bt_ctf_field_string_get_value returns a correct value"); |
39d74371 JG |
975 | bt_ctf_field_unsigned_integer_set_value(uint_35_field, |
976 | SEQUENCE_TEST_LENGTH); | |
10817e06 | 977 | |
7cfd41d6 JG |
978 | ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL, |
979 | enum_container_field) == NULL, | |
980 | "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly"); | |
981 | ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type, | |
982 | NULL) == NULL, | |
983 | "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly"); | |
984 | ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag( | |
985 | variant_type, enum_variant_field); | |
986 | ok(ret_field_type == int_16_type, | |
987 | "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type"); | |
988 | ||
10817e06 JG |
989 | ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL, |
990 | "bt_ctf_field_sequence_get_length returns NULL when length is unset"); | |
39d74371 JG |
991 | ok(bt_ctf_field_sequence_set_length(a_sequence_field, |
992 | uint_35_field) == 0, "Set a sequence field's length"); | |
cd95e351 JG |
993 | ret_field = bt_ctf_field_sequence_get_length(a_sequence_field); |
994 | ok(ret_field == uint_35_field, | |
995 | "bt_ctf_field_sequence_get_length returns the correct length field"); | |
996 | ok(bt_ctf_field_sequence_get_length(NULL) == NULL, | |
997 | "bt_ctf_field_sequence_get_length properly handles NULL"); | |
39d74371 JG |
998 | |
999 | for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) { | |
1000 | int_16_field = bt_ctf_field_sequence_get_field( | |
1001 | a_sequence_field, i); | |
1002 | bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i); | |
1003 | bt_ctf_field_put(int_16_field); | |
1004 | } | |
1005 | ||
1fac895e JG |
1006 | for (i = 0; i < ARRAY_TEST_LENGTH; i++) { |
1007 | int_16_field = bt_ctf_field_array_get_field( | |
1008 | an_array_field, i); | |
1009 | bt_ctf_field_signed_integer_set_value(int_16_field, i); | |
1010 | bt_ctf_field_put(int_16_field); | |
1011 | } | |
1012 | ||
39d74371 JG |
1013 | bt_ctf_clock_set_time(clock, ++current_time); |
1014 | ok(bt_ctf_stream_append_event(stream, event) == 0, | |
1015 | "Append a complex event to a stream"); | |
12c8a1a3 JG |
1016 | |
1017 | /* | |
1018 | * Populate the custom packet context field with a dummy value | |
1019 | * otherwise flush will fail. | |
1020 | */ | |
1021 | packet_context = bt_ctf_stream_get_packet_context(stream); | |
1022 | packet_context_field = bt_ctf_field_structure_get_field(packet_context, | |
35e8709f | 1023 | "custom_packet_context_field"); |
12c8a1a3 JG |
1024 | bt_ctf_field_unsigned_integer_set_value(packet_context_field, 1); |
1025 | ||
39d74371 JG |
1026 | ok(bt_ctf_stream_flush(stream) == 0, |
1027 | "Flush a stream containing a complex event"); | |
1028 | ||
1029 | bt_ctf_field_put(uint_35_field); | |
1030 | bt_ctf_field_put(a_string_field); | |
1031 | bt_ctf_field_put(inner_structure_field); | |
1032 | bt_ctf_field_put(complex_structure_field); | |
1033 | bt_ctf_field_put(a_sequence_field); | |
1fac895e | 1034 | bt_ctf_field_put(an_array_field); |
39d74371 JG |
1035 | bt_ctf_field_put(enum_variant_field); |
1036 | bt_ctf_field_put(enum_container_field); | |
1037 | bt_ctf_field_put(variant_field); | |
cd95e351 | 1038 | bt_ctf_field_put(ret_field); |
12c8a1a3 JG |
1039 | bt_ctf_field_put(packet_context_field); |
1040 | bt_ctf_field_put(packet_context); | |
39d74371 JG |
1041 | bt_ctf_field_type_put(uint_35_type); |
1042 | bt_ctf_field_type_put(int_16_type); | |
1043 | bt_ctf_field_type_put(string_type); | |
1044 | bt_ctf_field_type_put(sequence_type); | |
1fac895e | 1045 | bt_ctf_field_type_put(array_type); |
39d74371 JG |
1046 | bt_ctf_field_type_put(inner_structure_type); |
1047 | bt_ctf_field_type_put(complex_structure_type); | |
1048 | bt_ctf_field_type_put(uint_3_type); | |
1049 | bt_ctf_field_type_put(enum_variant_type); | |
1050 | bt_ctf_field_type_put(variant_type); | |
7cfd41d6 | 1051 | bt_ctf_field_type_put(ret_field_type); |
39d74371 JG |
1052 | bt_ctf_event_class_put(event_class); |
1053 | bt_ctf_event_put(event); | |
1054 | } | |
1055 | ||
1056 | void type_field_tests() | |
1057 | { | |
1058 | struct bt_ctf_field *uint_12; | |
1059 | struct bt_ctf_field *int_16; | |
1060 | struct bt_ctf_field *string; | |
0abce37e | 1061 | struct bt_ctf_field *enumeration; |
39d74371 JG |
1062 | struct bt_ctf_field_type *composite_structure_type; |
1063 | struct bt_ctf_field_type *structure_seq_type; | |
1064 | struct bt_ctf_field_type *string_type; | |
1065 | struct bt_ctf_field_type *sequence_type; | |
1066 | struct bt_ctf_field_type *uint_8_type; | |
1067 | struct bt_ctf_field_type *int_16_type; | |
1068 | struct bt_ctf_field_type *uint_12_type = | |
1069 | bt_ctf_field_type_integer_create(12); | |
0abce37e JG |
1070 | struct bt_ctf_field_type *enumeration_type; |
1071 | struct bt_ctf_field_type *enumeration_sequence_type; | |
1072 | struct bt_ctf_field_type *enumeration_array_type; | |
10817e06 | 1073 | struct bt_ctf_field_type *returned_type; |
7cfd41d6 | 1074 | const char *ret_string; |
10817e06 JG |
1075 | |
1076 | returned_type = bt_ctf_field_get_type(NULL); | |
1077 | ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly"); | |
39d74371 JG |
1078 | |
1079 | ok(uint_12_type, "Create an unsigned integer type"); | |
1080 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
1081 | BT_CTF_INTEGER_BASE_BINARY) == 0, | |
1082 | "Set integer type's base as binary"); | |
1083 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
1084 | BT_CTF_INTEGER_BASE_DECIMAL) == 0, | |
1085 | "Set integer type's base as decimal"); | |
1086 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
1087 | BT_CTF_INTEGER_BASE_UNKNOWN), | |
1088 | "Reject integer type's base set as unknown"); | |
1089 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
1090 | BT_CTF_INTEGER_BASE_OCTAL) == 0, | |
1091 | "Set integer type's base as octal"); | |
1092 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
1093 | BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0, | |
1094 | "Set integer type's base as hexadecimal"); | |
1095 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417), | |
1096 | "Reject unknown integer base value"); | |
1097 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0, | |
1098 | "Set integer type signedness to signed"); | |
1099 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0, | |
1100 | "Set integer type signedness to unsigned"); | |
7cfd41d6 JG |
1101 | ok(bt_ctf_field_type_integer_get_size(NULL) < 0, |
1102 | "bt_ctf_field_type_integer_get_size handles NULL correctly"); | |
1103 | ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12, | |
1104 | "bt_ctf_field_type_integer_get_size returns a correct value"); | |
1105 | ok(bt_ctf_field_type_integer_get_signed(NULL) < 0, | |
1106 | "bt_ctf_field_type_integer_get_signed handles NULL correctly"); | |
1107 | ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0, | |
1108 | "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types"); | |
1109 | ||
1110 | ok(bt_ctf_field_type_set_byte_order(NULL, | |
1111 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0, | |
1112 | "bt_ctf_field_type_set_byte_order handles NULL correctly"); | |
1113 | ok(bt_ctf_field_type_set_byte_order(uint_12_type, | |
1114 | (enum bt_ctf_byte_order) 42) < 0, | |
1115 | "bt_ctf_field_type_set_byte_order rejects invalid values"); | |
1116 | ok(bt_ctf_field_type_set_byte_order(uint_12_type, | |
1117 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0, | |
1118 | "Set an integer's byte order to little endian"); | |
1119 | ok(bt_ctf_field_type_set_byte_order(uint_12_type, | |
1120 | BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0, | |
1121 | "Set an integer's byte order to big endian"); | |
1122 | ok(bt_ctf_field_type_get_byte_order(uint_12_type) == | |
1123 | BT_CTF_BYTE_ORDER_BIG_ENDIAN, | |
1124 | "bt_ctf_field_type_get_byte_order returns a correct value"); | |
1125 | ok(bt_ctf_field_type_get_byte_order(NULL) == | |
1126 | BT_CTF_BYTE_ORDER_UNKNOWN, | |
1127 | "bt_ctf_field_type_get_byte_order handles NULL correctly"); | |
1128 | ||
1129 | ok(bt_ctf_field_type_get_type_id(NULL) == | |
1130 | CTF_TYPE_UNKNOWN, | |
1131 | "bt_ctf_field_type_get_type_id handles NULL correctly"); | |
1132 | ok(bt_ctf_field_type_get_type_id(uint_12_type) == | |
1133 | CTF_TYPE_INTEGER, | |
1134 | "bt_ctf_field_type_get_type_id returns a correct value with an integer type"); | |
1135 | ||
1136 | ok(bt_ctf_field_type_integer_get_base(NULL) == | |
1137 | BT_CTF_INTEGER_BASE_UNKNOWN, | |
1138 | "bt_ctf_field_type_integer_get_base handles NULL correctly"); | |
1139 | ok(bt_ctf_field_type_integer_get_base(uint_12_type) == | |
1140 | BT_CTF_INTEGER_BASE_HEXADECIMAL, | |
1141 | "bt_ctf_field_type_integer_get_base returns a correct value"); | |
1142 | ||
1143 | ok(bt_ctf_field_type_integer_set_encoding(NULL, CTF_STRING_ASCII) < 0, | |
1144 | "bt_ctf_field_type_integer_set_encoding handles NULL correctly"); | |
1145 | ok(bt_ctf_field_type_integer_set_encoding(uint_12_type, | |
1146 | (enum ctf_string_encoding) 123) < 0, | |
1147 | "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly"); | |
1148 | ok(bt_ctf_field_type_integer_set_encoding(uint_12_type, | |
1149 | CTF_STRING_UTF8) == 0, | |
1150 | "Set integer type encoding to UTF8"); | |
1151 | ok(bt_ctf_field_type_integer_get_encoding(NULL) == CTF_STRING_UNKNOWN, | |
1152 | "bt_ctf_field_type_integer_get_encoding handles NULL correctly"); | |
1153 | ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) == CTF_STRING_UTF8, | |
1154 | "bt_ctf_field_type_integer_get_encoding returns a correct value"); | |
39d74371 JG |
1155 | |
1156 | int_16_type = bt_ctf_field_type_integer_create(16); | |
1157 | bt_ctf_field_type_integer_set_signed(int_16_type, 1); | |
7cfd41d6 JG |
1158 | ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1, |
1159 | "bt_ctf_field_type_integer_get_signed returns a correct value for signed types"); | |
39d74371 JG |
1160 | uint_8_type = bt_ctf_field_type_integer_create(8); |
1161 | sequence_type = | |
1162 | bt_ctf_field_type_sequence_create(int_16_type, "seq_len"); | |
1163 | ok(sequence_type, "Create a sequence of int16_t type"); | |
7cfd41d6 JG |
1164 | ok(bt_ctf_field_type_get_type_id(sequence_type) == |
1165 | CTF_TYPE_SEQUENCE, | |
1166 | "bt_ctf_field_type_get_type_id returns a correct value with a sequence type"); | |
1167 | ||
1168 | ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL, | |
1169 | "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly"); | |
1170 | ret_string = bt_ctf_field_type_sequence_get_length_field_name( | |
1171 | sequence_type); | |
1172 | ok(!strcmp(ret_string, "seq_len"), | |
1173 | "bt_ctf_field_type_sequence_get_length_field_name returns the correct value"); | |
1174 | ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL, | |
1175 | "bt_ctf_field_type_sequence_get_element_type handles NULL correctly"); | |
1176 | returned_type = bt_ctf_field_type_sequence_get_element_type( | |
1177 | sequence_type); | |
1178 | ok(returned_type == int_16_type, | |
1179 | "bt_ctf_field_type_sequence_get_element_type returns the correct type"); | |
1180 | bt_ctf_field_type_put(returned_type); | |
39d74371 JG |
1181 | |
1182 | string_type = bt_ctf_field_type_string_create(); | |
1183 | ok(string_type, "Create a string type"); | |
1184 | ok(bt_ctf_field_type_string_set_encoding(string_type, | |
1185 | CTF_STRING_NONE), | |
1186 | "Reject invalid \"None\" string encoding"); | |
1187 | ok(bt_ctf_field_type_string_set_encoding(string_type, | |
1188 | 42), | |
1189 | "Reject invalid string encoding"); | |
1190 | ok(bt_ctf_field_type_string_set_encoding(string_type, | |
1191 | CTF_STRING_ASCII) == 0, | |
1192 | "Set string encoding to ASCII"); | |
1193 | ||
7cfd41d6 JG |
1194 | ok(bt_ctf_field_type_string_get_encoding(NULL) == |
1195 | CTF_STRING_UNKNOWN, | |
1196 | "bt_ctf_field_type_string_get_encoding handles NULL correctly"); | |
1197 | ok(bt_ctf_field_type_string_get_encoding(string_type) == | |
1198 | CTF_STRING_ASCII, | |
1199 | "bt_ctf_field_type_string_get_encoding returns the correct value"); | |
1200 | ||
39d74371 | 1201 | structure_seq_type = bt_ctf_field_type_structure_create(); |
7cfd41d6 JG |
1202 | ok(bt_ctf_field_type_get_type_id(structure_seq_type) == |
1203 | CTF_TYPE_STRUCT, | |
1204 | "bt_ctf_field_type_get_type_id returns a correct value with a structure type"); | |
39d74371 JG |
1205 | ok(structure_seq_type, "Create a structure type"); |
1206 | ok(bt_ctf_field_type_structure_add_field(structure_seq_type, | |
1207 | uint_8_type, "seq_len") == 0, | |
1208 | "Add a uint8_t type to a structure"); | |
1209 | ok(bt_ctf_field_type_structure_add_field(structure_seq_type, | |
1210 | sequence_type, "a_sequence") == 0, | |
1211 | "Add a sequence type to a structure"); | |
7cfd41d6 JG |
1212 | |
1213 | ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0, | |
1214 | "bt_ctf_field_type_structure_get_field_count handles NULL correctly"); | |
1215 | ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2, | |
1216 | "bt_ctf_field_type_structure_get_field_count returns a correct value"); | |
1217 | ||
1218 | ok(bt_ctf_field_type_structure_get_field(NULL, | |
1219 | &ret_string, &returned_type, 1) < 0, | |
1220 | "bt_ctf_field_type_structure_get_field handles a NULL type correctly"); | |
1221 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, | |
1222 | NULL, &returned_type, 1) < 0, | |
1223 | "bt_ctf_field_type_structure_get_field handles a NULL name correctly"); | |
1224 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, | |
1225 | &ret_string, NULL, 1) < 0, | |
1226 | "bt_ctf_field_type_structure_get_field handles a NULL return type correctly"); | |
1227 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, | |
1228 | &ret_string, &returned_type, 10) < 0, | |
1229 | "bt_ctf_field_type_structure_get_field handles an invalid index correctly"); | |
1230 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, | |
1231 | &ret_string, &returned_type, 1) == 0, | |
1232 | "bt_ctf_field_type_structure_get_field returns a field"); | |
1233 | ok(!strcmp(ret_string, "a_sequence"), | |
1234 | "bt_ctf_field_type_structure_get_field returns a correct field name"); | |
1235 | ok(returned_type == sequence_type, | |
1236 | "bt_ctf_field_type_structure_get_field returns a correct field type"); | |
1237 | bt_ctf_field_type_put(returned_type); | |
1238 | ||
1239 | ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL, | |
1240 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly"); | |
1241 | ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL, | |
1242 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly"); | |
1243 | returned_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
1244 | structure_seq_type, "a_sequence"); | |
1245 | ok(returned_type == sequence_type, | |
1246 | "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type"); | |
1247 | bt_ctf_field_type_put(returned_type); | |
1248 | ||
39d74371 JG |
1249 | composite_structure_type = bt_ctf_field_type_structure_create(); |
1250 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, | |
1251 | string_type, "a_string") == 0, | |
1252 | "Add a string type to a structure"); | |
1253 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, | |
1254 | structure_seq_type, "inner_structure") == 0, | |
1255 | "Add a structure type to a structure"); | |
1256 | ||
7cfd41d6 JG |
1257 | ok(bt_ctf_field_type_structure_get_field_type_by_name( |
1258 | NULL, "a_sequence") == NULL, | |
1259 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly"); | |
1260 | ok(bt_ctf_field_type_structure_get_field_type_by_name( | |
1261 | structure_seq_type, NULL) == NULL, | |
1262 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly"); | |
1263 | returned_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
1264 | structure_seq_type, "a_sequence"); | |
1265 | ok(returned_type == sequence_type, | |
1266 | "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type"); | |
1267 | bt_ctf_field_type_put(returned_type); | |
1268 | ||
39d74371 JG |
1269 | int_16 = bt_ctf_field_create(int_16_type); |
1270 | ok(int_16, "Instanciate a signed 16-bit integer"); | |
1271 | uint_12 = bt_ctf_field_create(uint_12_type); | |
1272 | ok(uint_12, "Instanciate an unsigned 12-bit integer"); | |
10817e06 JG |
1273 | returned_type = bt_ctf_field_get_type(int_16); |
1274 | ok(returned_type == int_16_type, | |
1275 | "bt_ctf_field_get_type returns the correct type"); | |
39d74371 JG |
1276 | |
1277 | /* Can't modify types after instanciating them */ | |
1278 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
1279 | BT_CTF_INTEGER_BASE_DECIMAL), | |
1280 | "Check an integer type' base can't be modified after instanciation"); | |
1281 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0), | |
1282 | "Check an integer type's signedness can't be modified after instanciation"); | |
1283 | ||
1284 | /* Check signed property is checked */ | |
1285 | ok(bt_ctf_field_signed_integer_set_value(uint_12, -52), | |
1286 | "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer"); | |
1287 | ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42), | |
1288 | "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer"); | |
1289 | ||
1290 | /* Check overflows are properly tested for */ | |
1291 | ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0, | |
1292 | "Check -32768 is allowed for a signed 16-bit integer"); | |
1293 | ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0, | |
1294 | "Check 32767 is allowed for a signed 16-bit integer"); | |
1295 | ok(bt_ctf_field_signed_integer_set_value(int_16, 32768), | |
1296 | "Check 32768 is not allowed for a signed 16-bit integer"); | |
1297 | ok(bt_ctf_field_signed_integer_set_value(int_16, -32769), | |
1298 | "Check -32769 is not allowed for a signed 16-bit integer"); | |
1299 | ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0, | |
1300 | "Check -42 is allowed for a signed 16-bit integer"); | |
1301 | ||
1302 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0, | |
1303 | "Check 4095 is allowed for an unsigned 12-bit integer"); | |
1304 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096), | |
1305 | "Check 4096 is not allowed for a unsigned 12-bit integer"); | |
1306 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0, | |
1307 | "Check 0 is allowed for an unsigned 12-bit integer"); | |
1308 | ||
1309 | string = bt_ctf_field_create(string_type); | |
1310 | ok(string, "Instanciate a string field"); | |
1311 | ok(bt_ctf_field_string_set_value(string, "A value") == 0, | |
1312 | "Set a string's value"); | |
1313 | ||
0abce37e JG |
1314 | enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type); |
1315 | ok(enumeration_type, | |
1316 | "Create an enumeration type with an unsigned 12-bit integer as container"); | |
1317 | enumeration_sequence_type = bt_ctf_field_type_sequence_create( | |
1318 | enumeration_type, "count"); | |
1319 | ok(!enumeration_sequence_type, | |
1320 | "Check enumeration types are validated when creating a sequence"); | |
1321 | enumeration_array_type = bt_ctf_field_type_array_create( | |
1322 | enumeration_type, 10); | |
1323 | ok(!enumeration_array_type, | |
1324 | "Check enumeration types are validated when creating an array"); | |
1325 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, | |
e6235f1f | 1326 | enumeration_type, "enumeration"), |
0abce37e JG |
1327 | "Check enumeration types are validated when adding them as structure members"); |
1328 | enumeration = bt_ctf_field_create(enumeration_type); | |
1329 | ok(!enumeration, | |
1330 | "Check enumeration types are validated before instantiation"); | |
1331 | ||
39d74371 JG |
1332 | bt_ctf_field_put(string); |
1333 | bt_ctf_field_put(uint_12); | |
1334 | bt_ctf_field_put(int_16); | |
0abce37e | 1335 | bt_ctf_field_put(enumeration); |
39d74371 JG |
1336 | bt_ctf_field_type_put(composite_structure_type); |
1337 | bt_ctf_field_type_put(structure_seq_type); | |
1338 | bt_ctf_field_type_put(string_type); | |
1339 | bt_ctf_field_type_put(sequence_type); | |
1340 | bt_ctf_field_type_put(uint_8_type); | |
1341 | bt_ctf_field_type_put(int_16_type); | |
1342 | bt_ctf_field_type_put(uint_12_type); | |
0abce37e JG |
1343 | bt_ctf_field_type_put(enumeration_type); |
1344 | bt_ctf_field_type_put(enumeration_sequence_type); | |
1345 | bt_ctf_field_type_put(enumeration_array_type); | |
7cfd41d6 | 1346 | bt_ctf_field_type_put(returned_type); |
39d74371 JG |
1347 | } |
1348 | ||
1349 | void packet_resize_test(struct bt_ctf_stream_class *stream_class, | |
1350 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) | |
1351 | { | |
1352 | /* | |
1353 | * Append enough events to force the underlying packet to be resized. | |
1354 | * Also tests that a new event can be declared after a stream has been | |
1355 | * instantiated and used/flushed. | |
1356 | */ | |
1357 | int ret = 0; | |
1358 | int i; | |
1359 | struct bt_ctf_event_class *event_class = bt_ctf_event_class_create( | |
1360 | "Spammy_Event"); | |
1361 | struct bt_ctf_field_type *integer_type = | |
1362 | bt_ctf_field_type_integer_create(17); | |
1363 | struct bt_ctf_field_type *string_type = | |
1364 | bt_ctf_field_type_string_create(); | |
35e8709f JG |
1365 | struct bt_ctf_event *event = NULL; |
1366 | struct bt_ctf_field *ret_field = NULL; | |
1367 | struct bt_ctf_field_type *ret_field_type = NULL; | |
6809e227 | 1368 | uint64_t ret_uint64; |
12c8a1a3 | 1369 | int events_appended = 0; |
35e8709f JG |
1370 | struct bt_ctf_field *packet_context = NULL, |
1371 | *packet_context_field = NULL, *event_context = NULL; | |
39d74371 JG |
1372 | |
1373 | ret |= bt_ctf_event_class_add_field(event_class, integer_type, | |
1374 | "field_1"); | |
1375 | ret |= bt_ctf_event_class_add_field(event_class, string_type, | |
1376 | "a_string"); | |
1377 | ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class); | |
1378 | ok(ret == 0, "Add a new event class to a stream class after writing an event"); | |
1379 | if (ret) { | |
1380 | goto end; | |
1381 | } | |
1382 | ||
1ff9582c JG |
1383 | event = bt_ctf_event_create(event_class); |
1384 | ret_field = bt_ctf_event_get_payload_by_index(event, 0); | |
1385 | ret_field_type = bt_ctf_field_get_type(ret_field); | |
1386 | ok(ret_field_type == integer_type, | |
1387 | "bt_ctf_event_get_payload_by_index returns a correct field"); | |
1388 | bt_ctf_field_type_put(ret_field_type); | |
1389 | bt_ctf_field_put(ret_field); | |
1390 | ||
1391 | ok(bt_ctf_event_get_payload_by_index(NULL, 0) == NULL, | |
1392 | "bt_ctf_event_get_payload_by_index handles NULL correctly"); | |
1393 | ok(bt_ctf_event_get_payload_by_index(event, 4) == NULL, | |
1394 | "bt_ctf_event_get_payload_by_index handles an invalid index correctly"); | |
1395 | bt_ctf_event_put(event); | |
1396 | ||
6e1f8ea1 JG |
1397 | ok(bt_ctf_stream_get_event_context(NULL) == NULL, |
1398 | "bt_ctf_stream_get_event_context handles NULL correctly"); | |
1399 | event_context = bt_ctf_stream_get_event_context(stream); | |
1400 | ok(event_context, | |
1401 | "bt_ctf_stream_get_event_context returns a stream event context"); | |
1402 | ok(bt_ctf_stream_set_event_context(NULL, event_context) < 0, | |
1403 | "bt_ctf_stream_set_event_context handles a NULL stream correctly"); | |
1404 | ok(bt_ctf_stream_set_event_context(stream, NULL) < 0, | |
1405 | "bt_ctf_stream_set_event_context handles a NULL stream event context correctly"); | |
1406 | ok(!bt_ctf_stream_set_event_context(stream, event_context), | |
1407 | "bt_ctf_stream_set_event_context correctly set a stream event context"); | |
1408 | ret_field = bt_ctf_field_create(integer_type); | |
1409 | ok(bt_ctf_stream_set_event_context(stream, ret_field) < 0, | |
1410 | "bt_ctf_stream_set_event_context rejects an event context of incorrect type"); | |
1411 | bt_ctf_field_put(ret_field); | |
1412 | ||
39d74371 | 1413 | for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) { |
1ff9582c | 1414 | event = bt_ctf_event_create(event_class); |
39d74371 JG |
1415 | struct bt_ctf_field *integer = |
1416 | bt_ctf_field_create(integer_type); | |
1417 | struct bt_ctf_field *string = | |
1418 | bt_ctf_field_create(string_type); | |
1419 | ||
1420 | ret |= bt_ctf_clock_set_time(clock, ++current_time); | |
1421 | ret |= bt_ctf_field_unsigned_integer_set_value(integer, i); | |
1422 | ret |= bt_ctf_event_set_payload(event, "field_1", | |
1423 | integer); | |
1424 | bt_ctf_field_put(integer); | |
1425 | ret |= bt_ctf_field_string_set_value(string, "This is a test"); | |
1426 | ret |= bt_ctf_event_set_payload(event, "a_string", | |
1427 | string); | |
1428 | bt_ctf_field_put(string); | |
6e1f8ea1 JG |
1429 | |
1430 | /* Populate stream event context */ | |
1431 | integer = bt_ctf_field_structure_get_field(event_context, | |
1432 | "common_event_context"); | |
1433 | ret |= bt_ctf_field_unsigned_integer_set_value(integer, | |
1434 | i % 42); | |
1435 | bt_ctf_field_put(integer); | |
1436 | ||
39d74371 JG |
1437 | ret |= bt_ctf_stream_append_event(stream, event); |
1438 | bt_ctf_event_put(event); | |
1439 | ||
1440 | if (ret) { | |
1441 | break; | |
1442 | } | |
1443 | } | |
12c8a1a3 | 1444 | |
6e1f8ea1 | 1445 | events_appended = !!(i == PACKET_RESIZE_TEST_LENGTH); |
3c1d148b | 1446 | ok(bt_ctf_stream_get_discarded_events_count(NULL, &ret_uint64) < 0, |
6809e227 | 1447 | "bt_ctf_stream_get_discarded_events_count handles a NULL stream correctly"); |
3c1d148b | 1448 | ok(bt_ctf_stream_get_discarded_events_count(stream, NULL) < 0, |
6809e227 JG |
1449 | "bt_ctf_stream_get_discarded_events_count handles a NULL return pointer correctly"); |
1450 | ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64); | |
1451 | ok(ret == 0 && ret_uint64 == 0, | |
1452 | "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when none were discarded"); | |
1453 | bt_ctf_stream_append_discarded_events(stream, 1000); | |
1454 | ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64); | |
1455 | ok(ret == 0 && ret_uint64 == 1000, | |
1456 | "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events when some were discarded"); | |
1457 | ||
39d74371 | 1458 | end: |
12c8a1a3 JG |
1459 | ok(events_appended, "Append 100 000 events to a stream"); |
1460 | ||
1461 | /* | |
1462 | * Populate the custom packet context field with a dummy value | |
1463 | * otherwise flush will fail. | |
1464 | */ | |
1465 | packet_context = bt_ctf_stream_get_packet_context(stream); | |
1466 | packet_context_field = bt_ctf_field_structure_get_field(packet_context, | |
35e8709f | 1467 | "custom_packet_context_field"); |
12c8a1a3 JG |
1468 | bt_ctf_field_unsigned_integer_set_value(packet_context_field, 2); |
1469 | ||
39d74371 JG |
1470 | ok(bt_ctf_stream_flush(stream) == 0, |
1471 | "Flush a stream that forces a packet resize"); | |
6809e227 JG |
1472 | ret = bt_ctf_stream_get_discarded_events_count(stream, &ret_uint64); |
1473 | ok(ret == 0 && ret_uint64 == 1000, | |
1474 | "bt_ctf_stream_get_discarded_events_count returns a correct number of discarded events after a flush"); | |
39d74371 JG |
1475 | bt_ctf_field_type_put(integer_type); |
1476 | bt_ctf_field_type_put(string_type); | |
12c8a1a3 JG |
1477 | bt_ctf_field_put(packet_context); |
1478 | bt_ctf_field_put(packet_context_field); | |
6e1f8ea1 | 1479 | bt_ctf_field_put(event_context); |
39d74371 JG |
1480 | bt_ctf_event_class_put(event_class); |
1481 | } | |
1482 | ||
fdf80f32 JG |
1483 | void test_empty_stream(struct bt_ctf_writer *writer) |
1484 | { | |
1485 | int ret = 0; | |
1486 | struct bt_ctf_trace *trace = NULL; | |
1487 | struct bt_ctf_clock *clock = NULL; | |
1488 | struct bt_ctf_stream_class *stream_class = NULL; | |
1489 | struct bt_ctf_stream *stream = NULL; | |
1490 | ||
1491 | trace = bt_ctf_writer_get_trace(writer); | |
1492 | if (!trace) { | |
1493 | diag("Failed to get trace from writer"); | |
1494 | ret = -1; | |
1495 | goto end; | |
1496 | } | |
1497 | ||
1498 | stream_class = bt_ctf_stream_class_create("empty_stream"); | |
1499 | if (!stream_class) { | |
1500 | diag("Failed to create stream class"); | |
1501 | ret = -1; | |
1502 | goto end; | |
1503 | } | |
1504 | ||
1505 | stream = bt_ctf_writer_create_stream(writer, stream_class); | |
1506 | if (!stream) { | |
1507 | diag("Failed to create writer stream"); | |
1508 | ret = -1; | |
1509 | goto end; | |
1510 | } | |
1511 | end: | |
1512 | ok(ret == 0, | |
1513 | "Created a stream class with default attributes and an empty stream"); | |
1514 | bt_ctf_trace_put(trace); | |
1515 | bt_ctf_clock_put(clock); | |
1516 | bt_ctf_stream_put(stream); | |
1517 | bt_ctf_stream_class_put(stream_class); | |
1518 | } | |
1519 | ||
39d74371 JG |
1520 | int main(int argc, char **argv) |
1521 | { | |
1522 | char trace_path[] = "/tmp/ctfwriter_XXXXXX"; | |
1523 | char metadata_path[sizeof(trace_path) + 9]; | |
1524 | const char *clock_name = "test_clock"; | |
1525 | const char *clock_description = "This is a test clock"; | |
5494ce8b JG |
1526 | const char *returned_clock_name; |
1527 | const char *returned_clock_description; | |
1528 | const uint64_t frequency = 1123456789; | |
39d74371 JG |
1529 | const uint64_t offset_s = 1351530929945824323; |
1530 | const uint64_t offset = 1234567; | |
1531 | const uint64_t precision = 10; | |
5494ce8b | 1532 | const int is_absolute = 0xFF; |
39d74371 JG |
1533 | char *metadata_string; |
1534 | struct bt_ctf_writer *writer; | |
1535 | struct utsname name; | |
22843b66 | 1536 | char hostname[BABELTRACE_HOST_NAME_MAX]; |
1ff9582c | 1537 | struct bt_ctf_clock *clock, *ret_clock; |
36336d93 | 1538 | struct bt_ctf_stream_class *stream_class, *ret_stream_class; |
39d74371 | 1539 | struct bt_ctf_stream *stream1; |
e3c971da | 1540 | const char *ret_string; |
e61caf8e JG |
1541 | const unsigned char *ret_uuid; |
1542 | unsigned char tmp_uuid[16] = { 0 }; | |
b34f4d90 JG |
1543 | struct bt_ctf_field_type *packet_context_type, |
1544 | *packet_context_field_type, | |
751b05c7 JG |
1545 | *packet_header_type, |
1546 | *packet_header_field_type, | |
35e8709f JG |
1547 | *integer_type, |
1548 | *stream_event_context_type, | |
1549 | *ret_field_type; | |
751b05c7 | 1550 | struct bt_ctf_field *packet_header, *packet_header_field; |
8cdae8c6 | 1551 | struct bt_ctf_trace *trace; |
12c8a1a3 | 1552 | int ret; |
39d74371 JG |
1553 | |
1554 | if (argc < 3) { | |
1555 | printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n"); | |
783c9151 | 1556 | return -1; |
39d74371 JG |
1557 | } |
1558 | ||
1559 | plan_no_plan(); | |
1560 | ||
1561 | if (!mkdtemp(trace_path)) { | |
1562 | perror("# perror"); | |
1563 | } | |
1564 | ||
1565 | strcpy(metadata_path, trace_path); | |
1566 | strcat(metadata_path + sizeof(trace_path) - 1, "/metadata"); | |
1567 | ||
1568 | writer = bt_ctf_writer_create(trace_path); | |
1569 | ok(writer, "bt_ctf_create succeeds in creating trace with path"); | |
1570 | ||
1571 | /* Add environment context to the trace */ | |
22843b66 JG |
1572 | ret = gethostname(hostname, sizeof(hostname)); |
1573 | if (ret < 0) { | |
1574 | return ret; | |
1575 | } | |
39d74371 JG |
1576 | ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0, |
1577 | "Add host (%s) environment field to writer instance", | |
1578 | hostname); | |
1579 | ok(bt_ctf_writer_add_environment_field(NULL, "test_field", | |
1580 | "test_value"), | |
1581 | "bt_ctf_writer_add_environment_field error with NULL writer"); | |
1582 | ok(bt_ctf_writer_add_environment_field(writer, NULL, | |
1583 | "test_value"), | |
1584 | "bt_ctf_writer_add_environment_field error with NULL field name"); | |
1585 | ok(bt_ctf_writer_add_environment_field(writer, "test_field", | |
1586 | NULL), | |
1587 | "bt_ctf_writer_add_environment_field error with NULL field value"); | |
1588 | ||
1589 | if (uname(&name)) { | |
1590 | perror("uname"); | |
1591 | return -1; | |
1592 | } | |
1593 | ||
1594 | ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname) | |
1595 | == 0, "Add sysname (%s) environment field to writer instance", | |
1596 | name.sysname); | |
1597 | ok(bt_ctf_writer_add_environment_field(writer, "nodename", | |
1598 | name.nodename) == 0, | |
1599 | "Add nodename (%s) environment field to writer instance", | |
1600 | name.nodename); | |
1601 | ok(bt_ctf_writer_add_environment_field(writer, "release", name.release) | |
1602 | == 0, "Add release (%s) environment field to writer instance", | |
1603 | name.release); | |
1604 | ok(bt_ctf_writer_add_environment_field(writer, "version", name.version) | |
1605 | == 0, "Add version (%s) environment field to writer instance", | |
1606 | name.version); | |
1607 | ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine) | |
1608 | == 0, "Add machine (%s) environment field to writer istance", | |
1609 | name.machine); | |
1610 | ||
1611 | /* Define a clock and add it to the trace */ | |
5494ce8b JG |
1612 | ok(bt_ctf_clock_create("signed") == NULL, |
1613 | "Illegal clock name rejected"); | |
39d74371 JG |
1614 | ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected"); |
1615 | clock = bt_ctf_clock_create(clock_name); | |
1616 | ok(clock, "Clock created sucessfully"); | |
5494ce8b JG |
1617 | returned_clock_name = bt_ctf_clock_get_name(clock); |
1618 | ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name"); | |
d50c7132 | 1619 | ok(returned_clock_name ? !strcmp(returned_clock_name, clock_name) : 0, |
5494ce8b JG |
1620 | "Returned clock name is valid"); |
1621 | ||
1622 | returned_clock_description = bt_ctf_clock_get_description(clock); | |
1623 | ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description"); | |
1624 | ok(bt_ctf_clock_set_description(clock, clock_description) == 0, | |
1625 | "Clock description set successfully"); | |
1626 | ||
1627 | returned_clock_description = bt_ctf_clock_get_description(clock); | |
1628 | ok(returned_clock_description, | |
1629 | "bt_ctf_clock_get_description returns a description."); | |
d50c7132 JG |
1630 | ok(returned_clock_description ? |
1631 | !strcmp(returned_clock_description, clock_description) : 0, | |
5494ce8b JG |
1632 | "Returned clock description is valid"); |
1633 | ||
1634 | ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ, | |
1635 | "bt_ctf_clock_get_frequency returns the correct default frequency"); | |
39d74371 JG |
1636 | ok(bt_ctf_clock_set_frequency(clock, frequency) == 0, |
1637 | "Set clock frequency"); | |
5494ce8b JG |
1638 | ok(bt_ctf_clock_get_frequency(clock) == frequency, |
1639 | "bt_ctf_clock_get_frequency returns the correct frequency once it is set"); | |
1640 | ||
1641 | ok(bt_ctf_clock_get_offset_s(clock) == DEFAULT_CLOCK_OFFSET_S, | |
1642 | "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)"); | |
39d74371 JG |
1643 | ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0, |
1644 | "Set clock offset (seconds)"); | |
5494ce8b JG |
1645 | ok(bt_ctf_clock_get_offset_s(clock) == offset_s, |
1646 | "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set"); | |
1647 | ||
1648 | ok(bt_ctf_clock_get_offset(clock) == DEFAULT_CLOCK_OFFSET, | |
1649 | "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)"); | |
39d74371 | 1650 | ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset"); |
5494ce8b JG |
1651 | ok(bt_ctf_clock_get_offset(clock) == offset, |
1652 | "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set"); | |
1653 | ||
1654 | ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION, | |
1655 | "bt_ctf_clock_get_precision returns the correct default precision"); | |
39d74371 JG |
1656 | ok(bt_ctf_clock_set_precision(clock, precision) == 0, |
1657 | "Set clock precision"); | |
5494ce8b JG |
1658 | ok(bt_ctf_clock_get_precision(clock) == precision, |
1659 | "bt_ctf_clock_get_precision returns the correct precision once it is set"); | |
1660 | ||
1661 | ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE, | |
1662 | "bt_ctf_clock_get_precision returns the correct default is_absolute attribute"); | |
1663 | ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0, | |
39d74371 | 1664 | "Set clock absolute property"); |
5494ce8b JG |
1665 | ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute, |
1666 | "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set"); | |
1667 | ||
1668 | ok(bt_ctf_clock_get_time(clock) == DEFAULT_CLOCK_TIME, | |
1669 | "bt_ctf_clock_get_time returns the correct default time"); | |
1670 | ok(bt_ctf_clock_set_time(clock, current_time) == 0, | |
1671 | "Set clock time"); | |
1672 | ok(bt_ctf_clock_get_time(clock) == current_time, | |
1673 | "bt_ctf_clock_get_time returns the correct time once it is set"); | |
39d74371 JG |
1674 | |
1675 | ok(bt_ctf_writer_add_clock(writer, clock) == 0, | |
1676 | "Add clock to writer instance"); | |
1677 | ok(bt_ctf_writer_add_clock(writer, clock), | |
1678 | "Verify a clock can't be added twice to a writer instance"); | |
1679 | ||
8cdae8c6 JG |
1680 | ok(!bt_ctf_writer_get_trace(NULL), |
1681 | "bt_ctf_writer_get_trace correctly handles NULL"); | |
1682 | trace = bt_ctf_writer_get_trace(writer); | |
1683 | ok(trace, | |
1684 | "bt_ctf_writer_get_trace returns a bt_ctf_trace object"); | |
1685 | ok(bt_ctf_trace_get_clock_count(NULL) < 0, | |
1686 | "bt_ctf_trace_get_clock_count correctly handles NULL"); | |
1687 | ok(bt_ctf_trace_get_clock_count(trace) == 1, | |
1688 | "bt_ctf_trace_get_clock_count returns the correct number of clocks"); | |
1689 | ok(!bt_ctf_trace_get_clock(NULL, 0), | |
1690 | "bt_ctf_trace_get_clock correctly handles NULL"); | |
1691 | ok(!bt_ctf_trace_get_clock(trace, -1), | |
1692 | "bt_ctf_trace_get_clock correctly handles negative indexes"); | |
1693 | ok(!bt_ctf_trace_get_clock(trace, 1), | |
1694 | "bt_ctf_trace_get_clock correctly handles out of bound accesses"); | |
1695 | ret_clock = bt_ctf_trace_get_clock(trace, 0); | |
1696 | ok(ret_clock == clock, | |
1697 | "bt_ctf_trace_get_clock returns the right clock instance"); | |
1698 | bt_ctf_clock_put(ret_clock); | |
1699 | ||
5494ce8b JG |
1700 | ok(!bt_ctf_clock_get_name(NULL), |
1701 | "bt_ctf_clock_get_name correctly handles NULL"); | |
1702 | ok(!bt_ctf_clock_get_description(NULL), | |
1703 | "bt_ctf_clock_get_description correctly handles NULL"); | |
1704 | ok(bt_ctf_clock_get_frequency(NULL) == -1ULL, | |
1705 | "bt_ctf_clock_get_frequency correctly handles NULL"); | |
1706 | ok(bt_ctf_clock_get_precision(NULL) == -1ULL, | |
1707 | "bt_ctf_clock_get_precision correctly handles NULL"); | |
1708 | ok(bt_ctf_clock_get_offset_s(NULL) == -1ULL, | |
1709 | "bt_ctf_clock_get_offset_s correctly handles NULL"); | |
1710 | ok(bt_ctf_clock_get_offset(NULL) == -1ULL, | |
1711 | "bt_ctf_clock_get_offset correctly handles NULL"); | |
3c1d148b | 1712 | ok(bt_ctf_clock_get_is_absolute(NULL) < 0, |
5494ce8b JG |
1713 | "bt_ctf_clock_get_is_absolute correctly handles NULL"); |
1714 | ok(bt_ctf_clock_get_time(NULL) == -1ULL, | |
1715 | "bt_ctf_clock_get_time correctly handles NULL"); | |
1716 | ||
3c1d148b | 1717 | ok(bt_ctf_clock_set_description(NULL, NULL) < 0, |
5494ce8b | 1718 | "bt_ctf_clock_set_description correctly handles NULL clock"); |
3c1d148b | 1719 | ok(bt_ctf_clock_set_frequency(NULL, frequency) < 0, |
5494ce8b | 1720 | "bt_ctf_clock_set_frequency correctly handles NULL clock"); |
3c1d148b | 1721 | ok(bt_ctf_clock_set_precision(NULL, precision) < 0, |
5494ce8b | 1722 | "bt_ctf_clock_get_precision correctly handles NULL clock"); |
3c1d148b | 1723 | ok(bt_ctf_clock_set_offset_s(NULL, offset_s) < 0, |
5494ce8b | 1724 | "bt_ctf_clock_set_offset_s correctly handles NULL clock"); |
3c1d148b | 1725 | ok(bt_ctf_clock_set_offset(NULL, offset) < 0, |
5494ce8b | 1726 | "bt_ctf_clock_set_offset correctly handles NULL clock"); |
3c1d148b | 1727 | ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) < 0, |
5494ce8b | 1728 | "bt_ctf_clock_set_is_absolute correctly handles NULL clock"); |
3c1d148b | 1729 | ok(bt_ctf_clock_set_time(NULL, current_time) < 0, |
5494ce8b | 1730 | "bt_ctf_clock_set_time correctly handles NULL clock"); |
e61caf8e JG |
1731 | ok(bt_ctf_clock_get_uuid(NULL) == NULL, |
1732 | "bt_ctf_clock_get_uuid correctly handles NULL clock"); | |
1733 | ret_uuid = bt_ctf_clock_get_uuid(clock); | |
1734 | ok(ret_uuid, | |
1735 | "bt_ctf_clock_get_uuid returns a UUID"); | |
1736 | if (ret_uuid) { | |
1737 | memcpy(tmp_uuid, ret_uuid, sizeof(tmp_uuid)); | |
1738 | /* Slightly modify UUID */ | |
1739 | tmp_uuid[sizeof(tmp_uuid) - 1]++; | |
1740 | } | |
1741 | ||
1742 | ok(bt_ctf_clock_set_uuid(NULL, tmp_uuid) < 0, | |
1743 | "bt_ctf_clock_set_uuid correctly handles a NULL clock"); | |
1744 | ok(bt_ctf_clock_set_uuid(clock, NULL) < 0, | |
1745 | "bt_ctf_clock_set_uuid correctly handles a NULL UUID"); | |
1746 | ok(bt_ctf_clock_set_uuid(clock, tmp_uuid) == 0, | |
1747 | "bt_ctf_clock_set_uuid sets a new uuid succesfully"); | |
1748 | ret_uuid = bt_ctf_clock_get_uuid(clock); | |
1749 | ok(ret_uuid, | |
1750 | "bt_ctf_clock_get_uuid returns a UUID after setting a new one"); | |
1751 | ok(uuid_match(ret_uuid, tmp_uuid), | |
1752 | "bt_ctf_clock_get_uuid returns the correct UUID after setting a new one"); | |
5494ce8b | 1753 | |
39d74371 JG |
1754 | /* Define a stream class */ |
1755 | stream_class = bt_ctf_stream_class_create("test_stream"); | |
e3c971da JG |
1756 | |
1757 | ok(bt_ctf_stream_class_get_name(NULL) == NULL, | |
1758 | "bt_ctf_stream_class_get_name handles NULL correctly"); | |
1759 | ret_string = bt_ctf_stream_class_get_name(stream_class); | |
1760 | ok(!strcmp(ret_string, "test_stream"), | |
12c8a1a3 | 1761 | "bt_ctf_stream_class_get_name returns a correct stream class name"); |
e3c971da | 1762 | |
1ff9582c JG |
1763 | ok(bt_ctf_stream_class_get_clock(stream_class) == NULL, |
1764 | "bt_ctf_stream_class_get_clock returns NULL when a clock was not set"); | |
1765 | ok(bt_ctf_stream_class_get_clock(NULL) == NULL, | |
1766 | "bt_ctf_stream_class_get_clock handles NULL correctly"); | |
1767 | ||
39d74371 JG |
1768 | ok(stream_class, "Create stream class"); |
1769 | ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0, | |
1770 | "Set a stream class' clock"); | |
1ff9582c JG |
1771 | ret_clock = bt_ctf_stream_class_get_clock(stream_class); |
1772 | ok(ret_clock == clock, | |
1773 | "bt_ctf_stream_class_get_clock returns a correct clock"); | |
1774 | bt_ctf_clock_put(ret_clock); | |
39d74371 JG |
1775 | |
1776 | /* Test the event fields and event types APIs */ | |
1777 | type_field_tests(); | |
1778 | ||
1ff9582c JG |
1779 | ok(bt_ctf_stream_class_get_id(stream_class) < 0, |
1780 | "bt_ctf_stream_class_get_id returns an error when no id is set"); | |
1781 | ok(bt_ctf_stream_class_get_id(NULL) < 0, | |
1782 | "bt_ctf_stream_class_get_id handles NULL correctly"); | |
1783 | ok(bt_ctf_stream_class_set_id(NULL, 123) < 0, | |
1784 | "bt_ctf_stream_class_set_id handles NULL correctly"); | |
1785 | ok(bt_ctf_stream_class_set_id(stream_class, 123) == 0, | |
1786 | "Set an stream class' id"); | |
1787 | ok(bt_ctf_stream_class_get_id(stream_class) == 123, | |
1788 | "bt_ctf_stream_class_get_id returns the correct value"); | |
1789 | ||
751b05c7 JG |
1790 | /* Add a custom trace packet header field */ |
1791 | ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL, | |
1792 | "bt_ctf_trace_get_packet_header_type handles NULL correctly"); | |
1793 | packet_header_type = bt_ctf_trace_get_packet_header_type(trace); | |
1794 | ok(packet_header_type, | |
1795 | "bt_ctf_trace_get_packet_header_type returns a packet header"); | |
1796 | ok(bt_ctf_field_type_get_type_id(packet_header_type) == CTF_TYPE_STRUCT, | |
1797 | "bt_ctf_trace_get_packet_header_type returns a packet header of type struct"); | |
1798 | ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
1799 | packet_header_type, "magic"); | |
1800 | ok(ret_field_type, "Default packet header type contains a \"magic\" field"); | |
1801 | bt_ctf_field_type_put(ret_field_type); | |
1802 | ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
1803 | packet_header_type, "uuid"); | |
1804 | ok(ret_field_type, "Default packet header type contains a \"uuid\" field"); | |
1805 | bt_ctf_field_type_put(ret_field_type); | |
1806 | ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
1807 | packet_header_type, "stream_id"); | |
1808 | ok(ret_field_type, "Default packet header type contains a \"stream_id\" field"); | |
1809 | bt_ctf_field_type_put(ret_field_type); | |
1810 | ||
1811 | packet_header_field_type = bt_ctf_field_type_integer_create(22); | |
1812 | ok(!bt_ctf_field_type_structure_add_field(packet_header_type, | |
1813 | packet_header_field_type, "custom_trace_packet_header_field"), | |
1814 | "Added a custom trace packet header field successfully"); | |
1815 | ||
1816 | ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0, | |
1817 | "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly"); | |
1818 | ok(bt_ctf_trace_set_packet_header_type(trace, NULL) < 0, | |
1819 | "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly"); | |
1820 | ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type), | |
1821 | "Set a trace packet_header_type successfully"); | |
1822 | ||
12c8a1a3 JG |
1823 | /* Create a "uint5_t" equivalent custom packet context field */ |
1824 | packet_context_field_type = bt_ctf_field_type_integer_create(5); | |
1825 | ||
1826 | ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL, | |
1827 | "bt_ctf_stream_class_get_packet_context_type handles NULL correctly"); | |
1828 | ||
1829 | /* Add a custom field to the stream class' packet context */ | |
1830 | packet_context_type = bt_ctf_stream_class_get_packet_context_type(stream_class); | |
1831 | ok(packet_context_type, | |
1832 | "bt_ctf_stream_class_get_packet_context_type returns a packet context type."); | |
1833 | ok(bt_ctf_field_type_get_type_id(packet_context_type) == CTF_TYPE_STRUCT, | |
1834 | "Packet context is a structure"); | |
1835 | ||
1836 | ok(bt_ctf_stream_class_set_packet_context_type(NULL, packet_context_type), | |
1837 | "bt_ctf_stream_class_set_packet_context_type handles a NULL stream class correctly"); | |
1838 | ok(bt_ctf_stream_class_set_packet_context_type(stream_class, NULL), | |
1839 | "bt_ctf_stream_class_set_packet_context_type handles a NULL packet context type correctly"); | |
b34f4d90 JG |
1840 | |
1841 | integer_type = bt_ctf_field_type_integer_create(32); | |
1842 | ok(bt_ctf_stream_class_set_packet_context_type(stream_class, | |
1843 | integer_type) < 0, | |
1844 | "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure"); | |
12c8a1a3 | 1845 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, |
35e8709f | 1846 | packet_context_field_type, "custom_packet_context_field"); |
12c8a1a3 JG |
1847 | ok(ret == 0, "Packet context field added successfully"); |
1848 | ||
35e8709f JG |
1849 | /* Define a stream event context containing a my_integer field. */ |
1850 | ok(bt_ctf_stream_class_get_event_context_type(NULL) == NULL, | |
1851 | "bt_ctf_stream_class_get_event_context_type handles NULL correctly"); | |
1852 | ok(bt_ctf_stream_class_get_event_context_type( | |
1853 | stream_class) == NULL, | |
1854 | "bt_ctf_stream_class_get_event_context_type returns NULL when no stream event context type was set."); | |
1855 | stream_event_context_type = bt_ctf_field_type_structure_create(); | |
1856 | bt_ctf_field_type_structure_add_field(stream_event_context_type, | |
1857 | integer_type, "common_event_context"); | |
1858 | ||
1859 | ok(bt_ctf_stream_class_set_event_context_type(NULL, | |
1860 | stream_event_context_type) < 0, | |
1861 | "bt_ctf_stream_class_set_event_context_type handles a NULL stream_class correctly"); | |
1862 | ok(bt_ctf_stream_class_set_event_context_type(stream_class, | |
1863 | NULL) < 0, | |
1864 | "bt_ctf_stream_class_set_event_context_type handles a NULL event_context correctly"); | |
1865 | ok(bt_ctf_stream_class_set_event_context_type(stream_class, | |
1866 | integer_type) < 0, | |
1867 | "bt_ctf_stream_class_set_event_context_type validates that the event context os a structure"); | |
1868 | ||
1869 | ok(bt_ctf_stream_class_set_event_context_type( | |
1870 | stream_class, stream_event_context_type) == 0, | |
1871 | "Set a new stream event context type"); | |
1872 | ret_field_type = bt_ctf_stream_class_get_event_context_type( | |
1873 | stream_class); | |
1874 | ok(ret_field_type == stream_event_context_type, | |
1875 | "bt_ctf_stream_class_get_event_context_type returns the correct field type."); | |
751b05c7 | 1876 | bt_ctf_field_type_put(ret_field_type); |
12c8a1a3 | 1877 | |
39d74371 JG |
1878 | /* Instantiate a stream and append events */ |
1879 | stream1 = bt_ctf_writer_create_stream(writer, stream_class); | |
1880 | ok(stream1, "Instanciate a stream class from writer"); | |
1881 | ||
36336d93 JG |
1882 | ok(bt_ctf_stream_get_class(NULL) == NULL, |
1883 | "bt_ctf_stream_get_class correctly handles NULL"); | |
1884 | ret_stream_class = bt_ctf_stream_get_class(stream1); | |
1885 | ok(ret_stream_class, | |
1886 | "bt_ctf_stream_get_class returns a stream class"); | |
1887 | ok(ret_stream_class == stream_class, | |
1888 | "Returned stream class is of the correct type"); | |
1889 | ||
751b05c7 JG |
1890 | /* |
1891 | * Try to modify the packet context type after a stream has been | |
1892 | * created. | |
1893 | */ | |
1894 | ret = bt_ctf_field_type_structure_add_field(packet_header_type, | |
1895 | packet_header_field_type, "should_fail"); | |
1896 | ok(ret < 0, | |
1897 | "Trace packet header type can't be modified once a stream has been instanciated"); | |
1898 | ||
12c8a1a3 JG |
1899 | /* |
1900 | * Try to modify the packet context type after a stream has been | |
1901 | * created. | |
1902 | */ | |
1903 | ret = bt_ctf_field_type_structure_add_field(packet_context_type, | |
1904 | packet_context_field_type, "should_fail"); | |
1905 | ok(ret < 0, | |
751b05c7 | 1906 | "Packet context type can't be modified once a stream has been instanciated"); |
12c8a1a3 | 1907 | |
35e8709f JG |
1908 | /* |
1909 | * Try to modify the stream event context type after a stream has been | |
1910 | * created. | |
1911 | */ | |
1912 | ret = bt_ctf_field_type_structure_add_field(stream_event_context_type, | |
1913 | integer_type, "should_fail"); | |
1914 | ok(ret < 0, | |
751b05c7 | 1915 | "Stream event context type can't be modified once a stream has been instanciated"); |
35e8709f JG |
1916 | |
1917 | /* Should fail after instanciating a stream (frozen) */ | |
39d74371 JG |
1918 | ok(bt_ctf_stream_class_set_clock(stream_class, clock), |
1919 | "Changes to a stream class that was already instantiated fail"); | |
1920 | ||
751b05c7 JG |
1921 | /* Populate the custom packet header field only once for all tests */ |
1922 | ok(bt_ctf_stream_get_packet_header(NULL) == NULL, | |
1923 | "bt_ctf_stream_get_packet_header handles NULL correctly"); | |
1924 | packet_header = bt_ctf_stream_get_packet_header(stream1); | |
1925 | ok(packet_header, | |
1926 | "bt_ctf_stream_get_packet_header returns a packet header"); | |
1927 | ret_field_type = bt_ctf_field_get_type(packet_header); | |
1928 | ok(ret_field_type == packet_header_type, | |
1929 | "Stream returns a packet header of the appropriate type"); | |
1930 | bt_ctf_field_type_put(ret_field_type); | |
1931 | packet_header_field = bt_ctf_field_structure_get_field(packet_header, | |
1932 | "custom_trace_packet_header_field"); | |
1933 | ok(packet_header_field, | |
1934 | "Packet header structure contains a custom field with the appropriate name"); | |
1935 | ret_field_type = bt_ctf_field_get_type(packet_header_field); | |
1936 | ok(ret_field_type == packet_header_field_type, | |
1937 | "Custom packet header field is of the expected type"); | |
1938 | ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field, | |
1939 | 54321), "Set custom packet header value successfully"); | |
1940 | ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0, | |
1941 | "bt_ctf_stream_set_packet_header handles a NULL packet header correctly"); | |
1942 | ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0, | |
1943 | "bt_ctf_stream_set_packet_header handles a NULL stream correctly"); | |
1944 | ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0, | |
1945 | "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type"); | |
1946 | ok(!bt_ctf_stream_set_packet_header(stream1, packet_header), | |
1947 | "Successfully set a stream's packet header"); | |
1948 | ||
39d74371 JG |
1949 | append_simple_event(stream_class, stream1, clock); |
1950 | ||
1951 | packet_resize_test(stream_class, stream1, clock); | |
1952 | ||
1953 | append_complex_event(stream_class, stream1, clock); | |
1954 | ||
fdf80f32 JG |
1955 | test_empty_stream(writer); |
1956 | ||
39d74371 JG |
1957 | metadata_string = bt_ctf_writer_get_metadata_string(writer); |
1958 | ok(metadata_string, "Get metadata string"); | |
1959 | ||
1960 | bt_ctf_writer_flush_metadata(writer); | |
1961 | validate_metadata(argv[1], metadata_path); | |
1962 | validate_trace(argv[2], trace_path); | |
1963 | ||
1964 | bt_ctf_clock_put(clock); | |
1965 | bt_ctf_stream_class_put(stream_class); | |
36336d93 | 1966 | bt_ctf_stream_class_put(ret_stream_class); |
39d74371 JG |
1967 | bt_ctf_writer_put(writer); |
1968 | bt_ctf_stream_put(stream1); | |
12c8a1a3 JG |
1969 | bt_ctf_field_type_put(packet_context_type); |
1970 | bt_ctf_field_type_put(packet_context_field_type); | |
b34f4d90 | 1971 | bt_ctf_field_type_put(integer_type); |
35e8709f JG |
1972 | bt_ctf_field_type_put(stream_event_context_type); |
1973 | bt_ctf_field_type_put(ret_field_type); | |
751b05c7 JG |
1974 | bt_ctf_field_type_put(packet_header_type); |
1975 | bt_ctf_field_type_put(packet_header_field_type); | |
1976 | bt_ctf_field_put(packet_header); | |
1977 | bt_ctf_field_put(packet_header_field); | |
8cdae8c6 | 1978 | bt_ctf_trace_put(trace); |
39d74371 JG |
1979 | free(metadata_string); |
1980 | ||
1981 | /* Remove all trace files and delete temporary trace directory */ | |
1982 | DIR *trace_dir = opendir(trace_path); | |
1983 | if (!trace_dir) { | |
1984 | perror("# opendir"); | |
1985 | return -1; | |
1986 | } | |
1987 | ||
1988 | struct dirent *entry; | |
1989 | while ((entry = readdir(trace_dir))) { | |
1990 | if (entry->d_type == DT_REG) { | |
543409b0 | 1991 | unlinkat(dirfd(trace_dir), entry->d_name, 0); |
39d74371 JG |
1992 | } |
1993 | } | |
1994 | ||
1995 | rmdir(trace_path); | |
1996 | closedir(trace_dir); | |
1997 | ||
1998 | return 0; | |
1999 | } |