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> | |
34 | #include <limits.h> | |
35 | #include <string.h> | |
36 | #include <assert.h> | |
37 | #include <unistd.h> | |
38 | #include <sys/wait.h> | |
39 | #include <fcntl.h> | |
40 | #include <dirent.h> | |
41 | #include "tap/tap.h" | |
10817e06 JG |
42 | #include <math.h> |
43 | #include <float.h> | |
39d74371 JG |
44 | |
45 | #define METADATA_LINE_SIZE 512 | |
46 | #define SEQUENCE_TEST_LENGTH 10 | |
1fac895e | 47 | #define ARRAY_TEST_LENGTH 5 |
39d74371 JG |
48 | #define PACKET_RESIZE_TEST_LENGTH 100000 |
49 | ||
5494ce8b JG |
50 | #define DEFAULT_CLOCK_FREQ 1000000000 |
51 | #define DEFAULT_CLOCK_PRECISION 1 | |
52 | #define DEFAULT_CLOCK_OFFSET 0 | |
53 | #define DEFAULT_CLOCK_OFFSET_S 0 | |
54 | #define DEFAULT_CLOCK_IS_ABSOLUTE 0 | |
55 | #define DEFAULT_CLOCK_TIME 0 | |
56 | ||
57 | static uint64_t current_time = 42; | |
39d74371 JG |
58 | |
59 | void validate_metadata(char *parser_path, char *metadata_path) | |
60 | { | |
61 | int ret = 0; | |
62 | char parser_output_path[] = "/tmp/parser_output_XXXXXX"; | |
63 | int parser_output_fd = -1, metadata_fd = -1; | |
64 | ||
65 | if (!metadata_path) { | |
66 | ret = -1; | |
67 | goto result; | |
68 | } | |
69 | ||
70 | parser_output_fd = mkstemp(parser_output_path); | |
71 | metadata_fd = open(metadata_path, O_RDONLY); | |
72 | ||
73 | unlink(parser_output_path); | |
74 | ||
75 | if (parser_output_fd == -1 || metadata_fd == -1) { | |
76 | diag("Failed create temporary files for metadata parsing."); | |
77 | ret = -1; | |
78 | goto result; | |
79 | } | |
80 | ||
81 | pid_t pid = fork(); | |
82 | if (pid) { | |
83 | int status = 0; | |
84 | waitpid(pid, &status, 0); | |
85 | ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1; | |
86 | } else { | |
87 | /* ctf-parser-test expects a metadata string on stdin. */ | |
88 | ret = dup2(metadata_fd, STDIN_FILENO); | |
89 | if (ret < 0) { | |
90 | perror("# dup2 metadata_fd to STDIN"); | |
91 | goto result; | |
92 | } | |
93 | ||
94 | ret = dup2(parser_output_fd, STDOUT_FILENO); | |
95 | if (ret < 0) { | |
96 | perror("# dup2 parser_output_fd to STDOUT"); | |
97 | goto result; | |
98 | } | |
99 | ||
100 | ret = dup2(parser_output_fd, STDERR_FILENO); | |
101 | if (ret < 0) { | |
102 | perror("# dup2 parser_output_fd to STDERR"); | |
103 | goto result; | |
104 | } | |
105 | ||
106 | execl(parser_path, "ctf-parser-test", NULL); | |
107 | perror("# Could not launch the ctf metadata parser process"); | |
108 | exit(-1); | |
109 | } | |
110 | result: | |
111 | ok(ret == 0, "Metadata string is valid"); | |
112 | ||
25afa9e5 | 113 | if (ret && metadata_fd >= 0 && parser_output_fd >= 0) { |
39d74371 JG |
114 | char *line; |
115 | size_t len = METADATA_LINE_SIZE; | |
116 | FILE *metadata_fp = NULL, *parser_output_fp = NULL; | |
117 | ||
118 | metadata_fp = fdopen(metadata_fd, "r"); | |
119 | if (!metadata_fp) { | |
120 | perror("fdopen on metadata_fd"); | |
121 | goto close_fp; | |
122 | } | |
e1d776cd | 123 | metadata_fd = -1; |
39d74371 JG |
124 | |
125 | parser_output_fp = fdopen(parser_output_fd, "r"); | |
126 | if (!parser_output_fp) { | |
127 | perror("fdopen on parser_output_fd"); | |
128 | goto close_fp; | |
129 | } | |
e1d776cd | 130 | parser_output_fd = -1; |
39d74371 JG |
131 | |
132 | line = malloc(len); | |
133 | if (!line) { | |
134 | diag("malloc failure"); | |
135 | } | |
136 | ||
137 | rewind(metadata_fp); | |
138 | ||
139 | /* Output the metadata and parser output as diagnostic */ | |
140 | while (getline(&line, &len, metadata_fp) > 0) { | |
141 | diag("%s", line); | |
142 | } | |
143 | ||
144 | rewind(parser_output_fp); | |
145 | while (getline(&line, &len, parser_output_fp) > 0) { | |
146 | diag("%s", line); | |
147 | } | |
148 | ||
149 | free(line); | |
150 | close_fp: | |
e1d776cd MD |
151 | if (metadata_fp) { |
152 | if (fclose(metadata_fp)) { | |
153 | diag("fclose failure"); | |
154 | } | |
155 | } | |
156 | if (parser_output_fp) { | |
157 | if (fclose(parser_output_fp)) { | |
158 | diag("fclose failure"); | |
159 | } | |
160 | } | |
39d74371 JG |
161 | } |
162 | ||
25afa9e5 | 163 | if (parser_output_fd >= 0) { |
e1d776cd MD |
164 | if (close(parser_output_fd)) { |
165 | diag("close error"); | |
166 | } | |
167 | } | |
25afa9e5 | 168 | if (metadata_fd >= 0) { |
e1d776cd MD |
169 | if (close(metadata_fd)) { |
170 | diag("close error"); | |
171 | } | |
172 | } | |
39d74371 JG |
173 | } |
174 | ||
175 | void validate_trace(char *parser_path, char *trace_path) | |
176 | { | |
177 | int ret = 0; | |
178 | char babeltrace_output_path[] = "/tmp/babeltrace_output_XXXXXX"; | |
179 | int babeltrace_output_fd = -1; | |
180 | ||
181 | if (!trace_path) { | |
182 | ret = -1; | |
183 | goto result; | |
184 | } | |
185 | ||
186 | babeltrace_output_fd = mkstemp(babeltrace_output_path); | |
187 | unlink(babeltrace_output_path); | |
188 | ||
189 | if (babeltrace_output_fd == -1) { | |
190 | diag("Failed to create a temporary file for trace parsing."); | |
191 | ret = -1; | |
192 | goto result; | |
193 | } | |
194 | ||
195 | pid_t pid = fork(); | |
196 | if (pid) { | |
197 | int status = 0; | |
198 | waitpid(pid, &status, 0); | |
199 | ret = WIFEXITED(status) ? WEXITSTATUS(status) : -1; | |
200 | } else { | |
201 | ret = dup2(babeltrace_output_fd, STDOUT_FILENO); | |
202 | if (ret < 0) { | |
203 | perror("# dup2 babeltrace_output_fd to STDOUT"); | |
204 | goto result; | |
205 | } | |
206 | ||
207 | ret = dup2(babeltrace_output_fd, STDERR_FILENO); | |
208 | if (ret < 0) { | |
209 | perror("# dup2 babeltrace_output_fd to STDERR"); | |
210 | goto result; | |
211 | } | |
212 | ||
213 | execl(parser_path, "babeltrace", trace_path, NULL); | |
214 | perror("# Could not launch the babeltrace process"); | |
215 | exit(-1); | |
216 | } | |
217 | result: | |
218 | ok(ret == 0, "Babeltrace could read the resulting trace"); | |
219 | ||
25afa9e5 | 220 | if (ret && babeltrace_output_fd >= 0) { |
39d74371 JG |
221 | char *line; |
222 | size_t len = METADATA_LINE_SIZE; | |
223 | FILE *babeltrace_output_fp = NULL; | |
224 | ||
225 | babeltrace_output_fp = fdopen(babeltrace_output_fd, "r"); | |
226 | if (!babeltrace_output_fp) { | |
227 | perror("fdopen on babeltrace_output_fd"); | |
228 | goto close_fp; | |
229 | } | |
e1d776cd | 230 | babeltrace_output_fd = -1; |
39d74371 JG |
231 | |
232 | line = malloc(len); | |
e1d776cd MD |
233 | if (!line) { |
234 | diag("malloc error"); | |
235 | } | |
39d74371 JG |
236 | rewind(babeltrace_output_fp); |
237 | while (getline(&line, &len, babeltrace_output_fp) > 0) { | |
238 | diag("%s", line); | |
239 | } | |
240 | ||
241 | free(line); | |
242 | close_fp: | |
e1d776cd MD |
243 | if (babeltrace_output_fp) { |
244 | if (fclose(babeltrace_output_fp)) { | |
245 | diag("fclose error"); | |
246 | } | |
247 | } | |
39d74371 JG |
248 | } |
249 | ||
25afa9e5 | 250 | if (babeltrace_output_fd >= 0) { |
e1d776cd MD |
251 | if (close(babeltrace_output_fd)) { |
252 | diag("close error"); | |
253 | } | |
254 | } | |
39d74371 JG |
255 | } |
256 | ||
257 | void append_simple_event(struct bt_ctf_stream_class *stream_class, | |
258 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) | |
259 | { | |
260 | /* Create and add a simple event class */ | |
261 | struct bt_ctf_event_class *simple_event_class = | |
262 | bt_ctf_event_class_create("Simple Event"); | |
263 | struct bt_ctf_field_type *uint_12_type = | |
264 | bt_ctf_field_type_integer_create(12); | |
7cfd41d6 JG |
265 | struct bt_ctf_field_type *int_64_type = |
266 | bt_ctf_field_type_integer_create(64); | |
39d74371 JG |
267 | struct bt_ctf_field_type *float_type = |
268 | bt_ctf_field_type_floating_point_create(); | |
7cfd41d6 JG |
269 | struct bt_ctf_field_type *enum_type; |
270 | struct bt_ctf_field_type *enum_type_unsigned = | |
8382544f | 271 | bt_ctf_field_type_enumeration_create(uint_12_type); |
7cfd41d6 | 272 | struct bt_ctf_field_type *returned_type; |
39d74371 JG |
273 | struct bt_ctf_event *simple_event; |
274 | struct bt_ctf_field *integer_field; | |
275 | struct bt_ctf_field *float_field; | |
8382544f | 276 | struct bt_ctf_field *enum_field; |
7cfd41d6 | 277 | struct bt_ctf_field *enum_field_unsigned; |
8382544f | 278 | struct bt_ctf_field *enum_container_field; |
10817e06 | 279 | const char *mapping_name_test = "truie"; |
10817e06 | 280 | const double double_test_value = 3.1415; |
7cfd41d6 JG |
281 | struct bt_ctf_field *enum_container_field_unsigned; |
282 | const char *mapping_name_negative_test = "negative_value"; | |
283 | const char *ret_char; | |
10817e06 | 284 | double ret_double; |
7cfd41d6 JG |
285 | size_t ret_size_t; |
286 | int64_t ret_range_start_int64_t, ret_range_end_int64_t; | |
287 | uint64_t ret_range_start_uint64_t, ret_range_end_uint64_t; | |
288 | ||
289 | ok(uint_12_type, "Create an unsigned integer type"); | |
290 | ||
291 | bt_ctf_field_type_integer_set_signed(int_64_type, 1); | |
292 | ok(int_64_type, "Create a signed integer type"); | |
293 | enum_type = bt_ctf_field_type_enumeration_create(int_64_type); | |
294 | ||
295 | returned_type = bt_ctf_field_type_enumeration_get_container_type(enum_type); | |
296 | ok(returned_type == int_64_type, "bt_ctf_field_type_enumeration_get_container_type returns the right type"); | |
297 | ok(!bt_ctf_field_type_enumeration_get_container_type(NULL), "bt_ctf_field_type_enumeration_get_container_type handles NULL correctly"); | |
298 | ok(!bt_ctf_field_type_enumeration_create(enum_type), | |
299 | "bt_ctf_field_enumeration_type_create rejects non-integer container field types"); | |
39d74371 JG |
300 | |
301 | bt_ctf_field_type_set_alignment(float_type, 32); | |
7cfd41d6 JG |
302 | ok(bt_ctf_field_type_get_alignment(NULL) < 0, |
303 | "bt_ctf_field_type_get_alignment handles NULL correctly"); | |
304 | ok(bt_ctf_field_type_get_alignment(float_type) == 32, | |
305 | "bt_ctf_field_type_get_alignment returns a correct value"); | |
306 | ||
307 | ok(bt_ctf_field_type_floating_point_set_exponent_digits(float_type, 11) == 0, | |
308 | "Set a floating point type's exponent digit count"); | |
309 | ok(bt_ctf_field_type_floating_point_set_mantissa_digits(float_type, 53) == 0, | |
310 | "Set a floating point type's mantissa digit count"); | |
311 | ||
312 | ok(bt_ctf_field_type_floating_point_get_exponent_digits(NULL) < 0, | |
313 | "bt_ctf_field_type_floating_point_get_exponent_digits handles NULL properly"); | |
314 | ok(bt_ctf_field_type_floating_point_get_mantissa_digits(NULL) < 0, | |
315 | "bt_ctf_field_type_floating_point_get_mantissa_digits handles NULL properly"); | |
316 | ok(bt_ctf_field_type_floating_point_get_exponent_digits(float_type) == 11, | |
317 | "bt_ctf_field_type_floating_point_get_exponent_digits returns the correct value"); | |
318 | ok(bt_ctf_field_type_floating_point_get_mantissa_digits(float_type) == 53, | |
319 | "bt_ctf_field_type_floating_point_get_mantissa_digits returns the correct value"); | |
8382544f JG |
320 | |
321 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, | |
7cfd41d6 JG |
322 | mapping_name_negative_test, -12345, 0) == 0, |
323 | "bt_ctf_field_type_enumeration_add_mapping accepts negative enumeration mappings"); | |
8382544f | 324 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
7cfd41d6 JG |
325 | "escaping; \"test\"", 1, 1) == 0, |
326 | "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing quotes"); | |
327 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, | |
328 | "\tanother \'escaping\'\n test\"", 2, 4) == 0, | |
329 | "bt_ctf_field_type_enumeration_add_mapping accepts enumeration mapping strings containing special characters"); | |
8382544f JG |
330 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, |
331 | "event clock int float", 5, 22) == 0, | |
332 | "Accept enumeration mapping strings containing reserved keywords"); | |
7cfd41d6 JG |
333 | bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, |
334 | 42, 42); | |
335 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, | |
336 | 43, 51), "bt_ctf_field_type_enumeration_add_mapping rejects duplicate mapping names"); | |
337 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, "something", | |
338 | -500, -400), "bt_ctf_field_type_enumeration_add_mapping rejects overlapping enum entries"); | |
339 | ok(bt_ctf_field_type_enumeration_add_mapping(enum_type, mapping_name_test, | |
340 | -54, -55), "bt_ctf_field_type_enumeration_add_mapping rejects mapping where end < start"); | |
341 | bt_ctf_field_type_enumeration_add_mapping(enum_type, "another entry", -42000, -13000); | |
342 | ||
343 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(NULL, -42, &ret_size_t) < 0, | |
344 | "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL field type correctly"); | |
345 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -42, NULL) < 0, | |
346 | "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles a NULL index correctly"); | |
347 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, 1000000, &ret_size_t) < 0, | |
348 | "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly"); | |
349 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_value(enum_type, -55, &ret_size_t) == 0, | |
350 | "bt_ctf_field_type_enumeration_get_mapping_index_by_value handles invalid values correctly"); | |
351 | ok(ret_size_t == 1, | |
352 | "bt_ctf_field_type_enumeration_get_mapping_index_by_value returns the correct index"); | |
353 | ||
8382544f | 354 | ok(bt_ctf_event_class_add_field(simple_event_class, enum_type, |
7cfd41d6 JG |
355 | "enum_field") == 0, "Add signed enumeration field to event"); |
356 | ||
357 | ok(bt_ctf_field_type_enumeration_get_mapping(NULL, 0, &ret_char, | |
358 | &ret_range_start_int64_t, &ret_range_end_int64_t) < 0, | |
359 | "bt_ctf_field_type_enumeration_get_mapping handles a NULL enumeration correctly"); | |
360 | ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, NULL, | |
361 | &ret_range_start_int64_t, &ret_range_end_int64_t) < 0, | |
362 | "bt_ctf_field_type_enumeration_get_mapping handles a NULL string correctly"); | |
363 | ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char, | |
364 | NULL, &ret_range_end_int64_t) < 0, | |
365 | "bt_ctf_field_type_enumeration_get_mapping handles a NULL start correctly"); | |
366 | ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 0, &ret_char, | |
367 | &ret_range_start_int64_t, NULL) < 0, | |
368 | "bt_ctf_field_type_enumeration_get_mapping handles a NULL end correctly"); | |
369 | ok(bt_ctf_field_type_enumeration_get_mapping(enum_type, 5, &ret_char, | |
370 | &ret_range_start_int64_t, &ret_range_end_int64_t) == 0, | |
371 | "bt_ctf_field_type_enumeration_get_mapping returns a value"); | |
372 | ok(!strcmp(ret_char, mapping_name_test), | |
373 | "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping name"); | |
374 | ok(ret_range_start_int64_t == 42, | |
375 | "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping start"); | |
376 | ok(ret_range_end_int64_t == 42, | |
377 | "bt_ctf_field_type_enumeration_get_mapping returns a correct mapping end"); | |
378 | ||
379 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, | |
380 | "escaping; \"test\"", 0, 0) == 0, | |
381 | "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing quotes"); | |
382 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, | |
383 | "\tanother \'escaping\'\n test\"", 1, 4) == 0, | |
384 | "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing special characters"); | |
385 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, | |
386 | "event clock int float", 5, 22) == 0, | |
387 | "bt_ctf_field_type_enumeration_add_mapping_unsigned accepts enumeration mapping strings containing reserved keywords"); | |
388 | bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, | |
389 | 42, 42); | |
390 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, | |
391 | 43, 51), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects duplicate mapping names"); | |
392 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, "something", | |
393 | 7, 8), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects overlapping enum entries"); | |
394 | ok(bt_ctf_field_type_enumeration_add_mapping_unsigned(enum_type_unsigned, mapping_name_test, | |
395 | 55, 54), "bt_ctf_field_type_enumeration_add_mapping_unsigned rejects mapping where end < start"); | |
396 | ok(bt_ctf_event_class_add_field(simple_event_class, enum_type_unsigned, | |
397 | "enum_field_unsigned") == 0, "Add unsigned enumeration field to event"); | |
398 | ||
399 | ok(bt_ctf_field_type_enumeration_get_mapping_count(NULL) < 0, | |
400 | "bt_ctf_field_type_enumeration_get_mapping_count handles NULL correctly"); | |
401 | ok(bt_ctf_field_type_enumeration_get_mapping_count(enum_type_unsigned) == 4, | |
402 | "bt_ctf_field_type_enumeration_get_mapping_count returns the correct value"); | |
403 | ||
404 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(NULL, 0, &ret_char, | |
405 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0, | |
406 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL enumeration correctly"); | |
407 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, NULL, | |
408 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) < 0, | |
409 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL string correctly"); | |
410 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char, | |
411 | NULL, &ret_range_end_uint64_t) < 0, | |
412 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL start correctly"); | |
413 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 0, &ret_char, | |
414 | &ret_range_start_uint64_t, NULL) < 0, | |
415 | "bt_ctf_field_type_enumeration_get_mapping_unsigned handles a NULL end correctly"); | |
416 | ok(bt_ctf_field_type_enumeration_get_mapping_unsigned(enum_type_unsigned, 3, &ret_char, | |
417 | &ret_range_start_uint64_t, &ret_range_end_uint64_t) == 0, | |
418 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a value"); | |
419 | ok(!strcmp(ret_char, mapping_name_test), | |
420 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping name"); | |
421 | ok(ret_range_start_uint64_t == 42, | |
422 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping start"); | |
423 | ok(ret_range_end_uint64_t == 42, | |
424 | "bt_ctf_field_type_enumeration_get_mapping_unsigned returns a correct mapping end"); | |
8382544f | 425 | |
39d74371 JG |
426 | bt_ctf_event_class_add_field(simple_event_class, uint_12_type, |
427 | "integer_field"); | |
428 | bt_ctf_event_class_add_field(simple_event_class, float_type, | |
429 | "float_field"); | |
430 | bt_ctf_stream_class_add_event_class(stream_class, | |
431 | simple_event_class); | |
432 | ||
433 | simple_event = bt_ctf_event_create(simple_event_class); | |
434 | ||
435 | ok(simple_event, | |
436 | "Instantiate an event containing a single integer field"); | |
437 | ||
438 | integer_field = bt_ctf_field_create(uint_12_type); | |
439 | bt_ctf_field_unsigned_integer_set_value(integer_field, 42); | |
440 | ok(bt_ctf_event_set_payload(simple_event, "integer_field", | |
441 | integer_field) == 0, "Use bt_ctf_event_set_payload to set a manually allocated field"); | |
442 | ||
443 | float_field = bt_ctf_event_get_payload(simple_event, "float_field"); | |
10817e06 JG |
444 | ok(bt_ctf_field_floating_point_get_value(float_field, &ret_double), |
445 | "bt_ctf_field_floating_point_get_value fails on an unset float field"); | |
446 | bt_ctf_field_floating_point_set_value(float_field, double_test_value); | |
447 | ok(bt_ctf_field_floating_point_get_value(NULL, &ret_double), | |
448 | "bt_ctf_field_floating_point_get_value properly handles a NULL field"); | |
449 | ok(bt_ctf_field_floating_point_get_value(float_field, NULL), | |
450 | "bt_ctf_field_floating_point_get_value properly handles a NULL return value pointer"); | |
451 | ok(!bt_ctf_field_floating_point_get_value(float_field, &ret_double), | |
452 | "bt_ctf_field_floating_point_get_value returns a double value"); | |
453 | ok(fabs(ret_double - double_test_value) <= DBL_EPSILON, | |
454 | "bt_ctf_field_floating_point_get_value returns a correct value"); | |
455 | ||
8382544f | 456 | enum_field = bt_ctf_field_create(enum_type); |
7cfd41d6 JG |
457 | ret_char = bt_ctf_field_enumeration_get_mapping_name(NULL); |
458 | ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name handles NULL correctly"); | |
459 | ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field); | |
460 | ok(!ret_char, "bt_ctf_field_enumeration_get_mapping_name returns NULL if the enumeration's container field is unset"); | |
8382544f JG |
461 | enum_container_field = bt_ctf_field_enumeration_get_container( |
462 | enum_field); | |
7cfd41d6 JG |
463 | ok(bt_ctf_field_signed_integer_set_value( |
464 | enum_container_field, -42) == 0, | |
465 | "Set signed enumeration container value"); | |
466 | ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field); | |
467 | ok(!strcmp(ret_char, mapping_name_negative_test), | |
468 | "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an signed container"); | |
8382544f | 469 | bt_ctf_event_set_payload(simple_event, "enum_field", enum_field); |
39d74371 | 470 | |
7cfd41d6 JG |
471 | enum_field_unsigned = bt_ctf_field_create(enum_type_unsigned); |
472 | enum_container_field_unsigned = bt_ctf_field_enumeration_get_container( | |
473 | enum_field_unsigned); | |
474 | ok(bt_ctf_field_unsigned_integer_set_value( | |
475 | enum_container_field_unsigned, 42) == 0, | |
476 | "Set unsigned enumeration container value"); | |
477 | bt_ctf_event_set_payload(simple_event, "enum_field_unsigned", | |
478 | enum_field_unsigned); | |
479 | ret_char = bt_ctf_field_enumeration_get_mapping_name(enum_field_unsigned); | |
480 | ok(!strcmp(ret_char, mapping_name_test), | |
481 | "bt_ctf_field_enumeration_get_mapping_name returns the correct mapping name with an unsigned container"); | |
482 | ||
39d74371 JG |
483 | ok(bt_ctf_clock_set_time(clock, current_time) == 0, "Set clock time"); |
484 | ||
485 | ok(bt_ctf_stream_append_event(stream, simple_event) == 0, | |
486 | "Append simple event to trace stream"); | |
487 | ||
488 | ok(bt_ctf_stream_flush(stream) == 0, | |
489 | "Flush trace stream with one event"); | |
490 | ||
491 | bt_ctf_event_class_put(simple_event_class); | |
492 | bt_ctf_event_put(simple_event); | |
493 | bt_ctf_field_type_put(uint_12_type); | |
7cfd41d6 | 494 | bt_ctf_field_type_put(int_64_type); |
39d74371 | 495 | bt_ctf_field_type_put(float_type); |
8382544f | 496 | bt_ctf_field_type_put(enum_type); |
7cfd41d6 JG |
497 | bt_ctf_field_type_put(enum_type_unsigned); |
498 | bt_ctf_field_type_put(returned_type); | |
39d74371 JG |
499 | bt_ctf_field_put(integer_field); |
500 | bt_ctf_field_put(float_field); | |
8382544f | 501 | bt_ctf_field_put(enum_field); |
7cfd41d6 | 502 | bt_ctf_field_put(enum_field_unsigned); |
8382544f | 503 | bt_ctf_field_put(enum_container_field); |
7cfd41d6 | 504 | bt_ctf_field_put(enum_container_field_unsigned); |
39d74371 JG |
505 | } |
506 | ||
507 | void append_complex_event(struct bt_ctf_stream_class *stream_class, | |
508 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) | |
509 | { | |
510 | int i; | |
10817e06 | 511 | const char *test_string = "Test string"; |
39d74371 JG |
512 | struct bt_ctf_field_type *uint_35_type = |
513 | bt_ctf_field_type_integer_create(35); | |
514 | struct bt_ctf_field_type *int_16_type = | |
515 | bt_ctf_field_type_integer_create(16); | |
516 | struct bt_ctf_field_type *uint_3_type = | |
517 | bt_ctf_field_type_integer_create(3); | |
518 | struct bt_ctf_field_type *enum_variant_type = | |
519 | bt_ctf_field_type_enumeration_create(uint_3_type); | |
520 | struct bt_ctf_field_type *variant_type = | |
521 | bt_ctf_field_type_variant_create(enum_variant_type, | |
522 | "variant_selector"); | |
523 | struct bt_ctf_field_type *string_type = | |
524 | bt_ctf_field_type_string_create(); | |
525 | struct bt_ctf_field_type *sequence_type; | |
1fac895e | 526 | struct bt_ctf_field_type *array_type; |
39d74371 JG |
527 | struct bt_ctf_field_type *inner_structure_type = |
528 | bt_ctf_field_type_structure_create(); | |
529 | struct bt_ctf_field_type *complex_structure_type = | |
530 | bt_ctf_field_type_structure_create(); | |
7cfd41d6 | 531 | struct bt_ctf_field_type *ret_field_type; |
39d74371 JG |
532 | struct bt_ctf_event_class *event_class; |
533 | struct bt_ctf_event *event; | |
534 | struct bt_ctf_field *uint_35_field, *int_16_field, *a_string_field, | |
535 | *inner_structure_field, *complex_structure_field, | |
536 | *a_sequence_field, *enum_variant_field, *enum_container_field, | |
1fac895e | 537 | *variant_field, *an_array_field, *ret_field; |
10817e06 | 538 | uint64_t ret_unsigned_int; |
1fac895e | 539 | int64_t ret_signed_int; |
10817e06 | 540 | const char *ret_string; |
7cfd41d6 | 541 | size_t ret_size_t; |
39d74371 JG |
542 | |
543 | bt_ctf_field_type_set_alignment(int_16_type, 32); | |
544 | bt_ctf_field_type_integer_set_signed(int_16_type, 1); | |
545 | bt_ctf_field_type_integer_set_base(uint_35_type, | |
546 | BT_CTF_INTEGER_BASE_HEXADECIMAL); | |
547 | ||
1fac895e | 548 | array_type = bt_ctf_field_type_array_create(int_16_type, ARRAY_TEST_LENGTH); |
39d74371 JG |
549 | sequence_type = bt_ctf_field_type_sequence_create(int_16_type, |
550 | "seq_len"); | |
7cfd41d6 JG |
551 | |
552 | ok(bt_ctf_field_type_array_get_element_type(NULL) == NULL, | |
553 | "bt_ctf_field_type_array_get_element_type handles NULL correctly"); | |
554 | ret_field_type = bt_ctf_field_type_array_get_element_type( | |
555 | array_type); | |
556 | ok(ret_field_type == int_16_type, | |
557 | "bt_ctf_field_type_array_get_element_type returns the correct type"); | |
558 | bt_ctf_field_type_put(ret_field_type); | |
559 | ||
560 | ok(bt_ctf_field_type_array_get_length(NULL) < 0, | |
561 | "bt_ctf_field_type_array_get_length handles NULL correctly"); | |
562 | ok(bt_ctf_field_type_array_get_length(array_type) == ARRAY_TEST_LENGTH, | |
563 | "bt_ctf_field_type_array_get_length returns the correct length"); | |
564 | ||
39d74371 JG |
565 | bt_ctf_field_type_structure_add_field(inner_structure_type, |
566 | uint_35_type, "seq_len"); | |
567 | bt_ctf_field_type_structure_add_field(inner_structure_type, | |
568 | sequence_type, "a_sequence"); | |
1fac895e JG |
569 | bt_ctf_field_type_structure_add_field(inner_structure_type, |
570 | array_type, "an_array"); | |
39d74371 JG |
571 | |
572 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
573 | "UINT3_TYPE", 0, 0); | |
574 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
575 | "INT16_TYPE", 1, 1); | |
576 | bt_ctf_field_type_enumeration_add_mapping(enum_variant_type, | |
577 | "UINT35_TYPE", 2, 7); | |
7cfd41d6 JG |
578 | |
579 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(NULL, "INT16_TYPE", | |
580 | &ret_size_t) < 0, | |
581 | "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL field type correctly"); | |
582 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type, NULL, | |
583 | &ret_size_t) < 0, | |
584 | "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL name correctly"); | |
585 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type, "INT16_TYPE", | |
586 | NULL) < 0, | |
587 | "bt_ctf_field_type_enumeration_get_mapping_index_by_name handles a NULL index correctly"); | |
588 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_name(enum_variant_type, "INT16_TYPE", | |
589 | &ret_size_t) == 0, | |
590 | "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns a value"); | |
591 | ok(ret_size_t == 1, | |
592 | "bt_ctf_field_type_enumeration_get_mapping_index_by_name returns the correct index"); | |
593 | ||
594 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(NULL, 1, &ret_size_t) < 0, | |
595 | "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL field type correctly"); | |
596 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 1, NULL) < 0, | |
597 | "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles a NULL index correctly"); | |
598 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, -42, &ret_size_t) < 0, | |
599 | "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly"); | |
600 | ok(bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value(enum_variant_type, 5, &ret_size_t) == 0, | |
601 | "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value handles invalid values correctly"); | |
602 | ok(ret_size_t == 2, | |
603 | "bt_ctf_field_type_enumeration_get_mapping_index_by_unsigned_value returns the correct index"); | |
604 | ||
39d74371 JG |
605 | ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type, |
606 | "An unknown entry"), "Reject a variant field based on an unknown tag value"); | |
607 | ok(bt_ctf_field_type_variant_add_field(variant_type, uint_3_type, | |
608 | "UINT3_TYPE") == 0, "Add a field to a variant"); | |
609 | bt_ctf_field_type_variant_add_field(variant_type, int_16_type, | |
610 | "INT16_TYPE"); | |
611 | bt_ctf_field_type_variant_add_field(variant_type, uint_35_type, | |
612 | "UINT35_TYPE"); | |
613 | ||
7cfd41d6 JG |
614 | ok(bt_ctf_field_type_variant_get_tag_type(NULL) == NULL, |
615 | "bt_ctf_field_type_variant_get_tag_type handles NULL correctly"); | |
616 | ret_field_type = bt_ctf_field_type_variant_get_tag_type(variant_type); | |
617 | ok(ret_field_type == enum_variant_type, | |
618 | "bt_ctf_field_type_variant_get_tag_type returns a correct tag type"); | |
619 | bt_ctf_field_type_put(ret_field_type); | |
620 | ||
621 | ok(bt_ctf_field_type_variant_get_tag_name(NULL) == NULL, | |
622 | "bt_ctf_field_type_variant_get_tag_name handles NULL correctly"); | |
623 | ret_string = bt_ctf_field_type_variant_get_tag_name(variant_type); | |
624 | ok(!strcmp(ret_string, "variant_selector"), | |
625 | "bt_ctf_field_type_variant_get_tag_name returns the correct variant tag name"); | |
626 | ok(bt_ctf_field_type_variant_get_field_type_by_name(NULL, | |
627 | "INT16_TYPE") == NULL, | |
628 | "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL variant_type correctly"); | |
629 | ok(bt_ctf_field_type_variant_get_field_type_by_name(variant_type, | |
630 | NULL) == NULL, | |
631 | "bt_ctf_field_type_variant_get_field_type_by_name handles a NULL field name correctly"); | |
632 | ret_field_type = bt_ctf_field_type_variant_get_field_type_by_name( | |
633 | variant_type, "INT16_TYPE"); | |
634 | ok(ret_field_type == int_16_type, | |
635 | "bt_ctf_field_type_variant_get_field_type_by_name returns a correct field type"); | |
636 | bt_ctf_field_type_put(ret_field_type); | |
637 | ||
638 | ok(bt_ctf_field_type_variant_get_field_count(NULL) < 0, | |
639 | "bt_ctf_field_type_variant_get_field_count handles NULL correctly"); | |
640 | ok(bt_ctf_field_type_variant_get_field_count(variant_type) == 3, | |
641 | "bt_ctf_field_type_variant_get_field_count returns the correct count"); | |
642 | ||
643 | ok(bt_ctf_field_type_variant_get_field(NULL, &ret_string, &ret_field_type, 0) < 0, | |
644 | "bt_ctf_field_type_variant_get_field handles a NULL type correctly"); | |
645 | ok(bt_ctf_field_type_variant_get_field(variant_type, NULL, &ret_field_type, 0) < 0, | |
646 | "bt_ctf_field_type_variant_get_field handles a NULL field name correctly"); | |
647 | ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, NULL, 0) < 0, | |
648 | "bt_ctf_field_type_variant_get_field handles a NULL field type correctly"); | |
649 | ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 200) < 0, | |
650 | "bt_ctf_field_type_variant_get_field handles an invalid index correctly"); | |
651 | ok(bt_ctf_field_type_variant_get_field(variant_type, &ret_string, &ret_field_type, 1) == 0, | |
652 | "bt_ctf_field_type_variant_get_field returns a field"); | |
653 | ok(!strcmp("INT16_TYPE", ret_string), | |
654 | "bt_ctf_field_type_variant_get_field returns a correct field name"); | |
655 | ok(ret_field_type == int_16_type, | |
656 | "bt_ctf_field_type_variant_get_field returns a correct field type"); | |
657 | bt_ctf_field_type_put(ret_field_type); | |
658 | ||
39d74371 JG |
659 | bt_ctf_field_type_structure_add_field(complex_structure_type, |
660 | enum_variant_type, "variant_selector"); | |
661 | bt_ctf_field_type_structure_add_field(complex_structure_type, | |
662 | string_type, "a_string"); | |
663 | bt_ctf_field_type_structure_add_field(complex_structure_type, | |
664 | variant_type, "variant_value"); | |
665 | bt_ctf_field_type_structure_add_field(complex_structure_type, | |
666 | inner_structure_type, "inner_structure"); | |
667 | ||
668 | ok(bt_ctf_event_class_create("clock") == NULL, | |
669 | "Reject creation of an event class with an illegal name"); | |
670 | event_class = bt_ctf_event_class_create("Complex Test Event"); | |
671 | ok(event_class, "Create an event class"); | |
672 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, ""), | |
673 | "Reject addition of a field with an empty name to an event"); | |
674 | ok(bt_ctf_event_class_add_field(event_class, NULL, "an_integer"), | |
675 | "Reject addition of a field with a NULL type to an event"); | |
676 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, | |
677 | "int"), | |
678 | "Reject addition of a type with an illegal name to an event"); | |
679 | ok(bt_ctf_event_class_add_field(event_class, uint_35_type, | |
680 | "uint_35") == 0, | |
681 | "Add field of type unsigned integer to an event"); | |
682 | ok(bt_ctf_event_class_add_field(event_class, int_16_type, | |
683 | "int_16") == 0, "Add field of type signed integer to an event"); | |
684 | ok(bt_ctf_event_class_add_field(event_class, complex_structure_type, | |
685 | "complex_structure") == 0, | |
686 | "Add composite structure to an event"); | |
687 | ||
688 | /* Add event class to the stream class */ | |
689 | ok(bt_ctf_stream_class_add_event_class(stream_class, NULL), | |
690 | "Reject addition of NULL event class to a stream class"); | |
691 | ok(bt_ctf_stream_class_add_event_class(stream_class, | |
692 | event_class) == 0, "Add an event class to stream class"); | |
693 | ||
694 | event = bt_ctf_event_create(event_class); | |
695 | ok(event, "Instanciate a complex event"); | |
696 | ||
697 | uint_35_field = bt_ctf_event_get_payload(event, "uint_35"); | |
7cfd41d6 | 698 | if (!uint_35_field) { |
10817e06 | 699 | printf("uint_35_field is NULL\n"); |
7cfd41d6 JG |
700 | } |
701 | ||
39d74371 JG |
702 | ok(uint_35_field, "Use bt_ctf_event_get_payload to get a field instance "); |
703 | bt_ctf_field_unsigned_integer_set_value(uint_35_field, 0x0DDF00D); | |
10817e06 JG |
704 | ok(bt_ctf_field_unsigned_integer_get_value(NULL, &ret_unsigned_int) == -1, |
705 | "bt_ctf_field_unsigned_integer_get_value properly properly handles a NULL field."); | |
706 | ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, NULL) == -1, | |
707 | "bt_ctf_field_unsigned_integer_get_value properly handles a NULL return value"); | |
708 | ok(bt_ctf_field_unsigned_integer_get_value(uint_35_field, | |
709 | &ret_unsigned_int) == 0, | |
710 | "bt_ctf_field_unsigned_integer_get_value succeeds after setting a value"); | |
711 | ok(ret_unsigned_int == 0x0DDF00D, | |
712 | "bt_ctf_field_unsigned_integer_get_value returns the correct value"); | |
713 | ok(bt_ctf_field_signed_integer_get_value(uint_35_field, | |
714 | &ret_signed_int) == -1, | |
715 | "bt_ctf_field_signed_integer_get_value fails on an unsigned field"); | |
39d74371 JG |
716 | bt_ctf_field_put(uint_35_field); |
717 | ||
718 | int_16_field = bt_ctf_event_get_payload(event, "int_16"); | |
719 | bt_ctf_field_signed_integer_set_value(int_16_field, -12345); | |
10817e06 JG |
720 | ok(bt_ctf_field_signed_integer_get_value(NULL, &ret_signed_int) == -1, |
721 | "bt_ctf_field_signed_integer_get_value properly handles a NULL field"); | |
722 | ok(bt_ctf_field_signed_integer_get_value(int_16_field, NULL) == -1, | |
723 | "bt_ctf_field_signed_integer_get_value properly handles a NULL return value"); | |
724 | ok(bt_ctf_field_signed_integer_get_value(int_16_field, | |
725 | &ret_signed_int) == 0, | |
726 | "bt_ctf_field_signed_integer_get_value succeeds after setting a value"); | |
727 | ok(ret_signed_int == -12345, | |
728 | "bt_ctf_field_signed_integer_get_value returns the correct value"); | |
729 | ok(bt_ctf_field_unsigned_integer_get_value(int_16_field, | |
730 | &ret_unsigned_int) == -1, | |
731 | "bt_ctf_field_unsigned_integer_get_value fails on a signed field"); | |
39d74371 JG |
732 | bt_ctf_field_put(int_16_field); |
733 | ||
734 | complex_structure_field = bt_ctf_event_get_payload(event, | |
735 | "complex_structure"); | |
10817e06 JG |
736 | |
737 | ok(bt_ctf_field_structure_get_field_by_index(NULL, 0) == NULL, | |
738 | "bt_ctf_field_structure_get_field_by_index handles NULL correctly"); | |
739 | ok(bt_ctf_field_structure_get_field_by_index(NULL, 9) == NULL, | |
740 | "bt_ctf_field_structure_get_field_by_index handles an invalid index correctly"); | |
741 | inner_structure_field = bt_ctf_field_structure_get_field_by_index( | |
742 | complex_structure_field, 3); | |
743 | ret_field_type = bt_ctf_field_get_type(inner_structure_field); | |
744 | bt_ctf_field_put(inner_structure_field); | |
745 | ok(ret_field_type == inner_structure_type, | |
746 | "bt_ctf_field_structure_get_field_by_index returns a correct field"); | |
747 | bt_ctf_field_type_put(ret_field_type); | |
748 | ||
39d74371 JG |
749 | inner_structure_field = bt_ctf_field_structure_get_field( |
750 | complex_structure_field, "inner_structure"); | |
751 | a_string_field = bt_ctf_field_structure_get_field( | |
752 | complex_structure_field, "a_string"); | |
753 | enum_variant_field = bt_ctf_field_structure_get_field( | |
754 | complex_structure_field, "variant_selector"); | |
755 | variant_field = bt_ctf_field_structure_get_field( | |
756 | complex_structure_field, "variant_value"); | |
757 | uint_35_field = bt_ctf_field_structure_get_field( | |
758 | inner_structure_field, "seq_len"); | |
759 | a_sequence_field = bt_ctf_field_structure_get_field( | |
760 | inner_structure_field, "a_sequence"); | |
1fac895e JG |
761 | an_array_field = bt_ctf_field_structure_get_field( |
762 | inner_structure_field, "an_array"); | |
39d74371 JG |
763 | |
764 | enum_container_field = bt_ctf_field_enumeration_get_container( | |
765 | enum_variant_field); | |
766 | bt_ctf_field_unsigned_integer_set_value(enum_container_field, 1); | |
767 | int_16_field = bt_ctf_field_variant_get_field(variant_field, | |
768 | enum_variant_field); | |
769 | bt_ctf_field_signed_integer_set_value(int_16_field, -200); | |
770 | bt_ctf_field_put(int_16_field); | |
10817e06 JG |
771 | ok(!bt_ctf_field_string_get_value(a_string_field), |
772 | "bt_ctf_field_string_get_value returns NULL on an unset field"); | |
39d74371 | 773 | bt_ctf_field_string_set_value(a_string_field, |
10817e06 JG |
774 | test_string); |
775 | ok(!bt_ctf_field_string_get_value(NULL), | |
776 | "bt_ctf_field_string_get_value correctly handles NULL"); | |
777 | ret_string = bt_ctf_field_string_get_value(a_string_field); | |
778 | ok(ret_string, "bt_ctf_field_string_get_value returns a string"); | |
779 | ok(!strcmp(ret_string, test_string), | |
780 | "bt_ctf_field_string_get_value returns a correct value"); | |
39d74371 JG |
781 | bt_ctf_field_unsigned_integer_set_value(uint_35_field, |
782 | SEQUENCE_TEST_LENGTH); | |
10817e06 | 783 | |
7cfd41d6 JG |
784 | ok(bt_ctf_field_type_variant_get_field_type_from_tag(NULL, |
785 | enum_container_field) == NULL, | |
786 | "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL variant type correctly"); | |
787 | ok(bt_ctf_field_type_variant_get_field_type_from_tag(variant_type, | |
788 | NULL) == NULL, | |
789 | "bt_ctf_field_type_variant_get_field_type_from_tag handles a NULL tag correctly"); | |
790 | ret_field_type = bt_ctf_field_type_variant_get_field_type_from_tag( | |
791 | variant_type, enum_variant_field); | |
792 | ok(ret_field_type == int_16_type, | |
793 | "bt_ctf_field_type_variant_get_field_type_from_tag returns the correct field type"); | |
794 | ||
10817e06 JG |
795 | ok(bt_ctf_field_sequence_get_length(a_sequence_field) == NULL, |
796 | "bt_ctf_field_sequence_get_length returns NULL when length is unset"); | |
39d74371 JG |
797 | ok(bt_ctf_field_sequence_set_length(a_sequence_field, |
798 | uint_35_field) == 0, "Set a sequence field's length"); | |
cd95e351 JG |
799 | ret_field = bt_ctf_field_sequence_get_length(a_sequence_field); |
800 | ok(ret_field == uint_35_field, | |
801 | "bt_ctf_field_sequence_get_length returns the correct length field"); | |
802 | ok(bt_ctf_field_sequence_get_length(NULL) == NULL, | |
803 | "bt_ctf_field_sequence_get_length properly handles NULL"); | |
39d74371 JG |
804 | |
805 | for (i = 0; i < SEQUENCE_TEST_LENGTH; i++) { | |
806 | int_16_field = bt_ctf_field_sequence_get_field( | |
807 | a_sequence_field, i); | |
808 | bt_ctf_field_signed_integer_set_value(int_16_field, 4 - i); | |
809 | bt_ctf_field_put(int_16_field); | |
810 | } | |
811 | ||
1fac895e JG |
812 | for (i = 0; i < ARRAY_TEST_LENGTH; i++) { |
813 | int_16_field = bt_ctf_field_array_get_field( | |
814 | an_array_field, i); | |
815 | bt_ctf_field_signed_integer_set_value(int_16_field, i); | |
816 | bt_ctf_field_put(int_16_field); | |
817 | } | |
818 | ||
39d74371 JG |
819 | bt_ctf_clock_set_time(clock, ++current_time); |
820 | ok(bt_ctf_stream_append_event(stream, event) == 0, | |
821 | "Append a complex event to a stream"); | |
822 | ok(bt_ctf_stream_flush(stream) == 0, | |
823 | "Flush a stream containing a complex event"); | |
824 | ||
825 | bt_ctf_field_put(uint_35_field); | |
826 | bt_ctf_field_put(a_string_field); | |
827 | bt_ctf_field_put(inner_structure_field); | |
828 | bt_ctf_field_put(complex_structure_field); | |
829 | bt_ctf_field_put(a_sequence_field); | |
1fac895e | 830 | bt_ctf_field_put(an_array_field); |
39d74371 JG |
831 | bt_ctf_field_put(enum_variant_field); |
832 | bt_ctf_field_put(enum_container_field); | |
833 | bt_ctf_field_put(variant_field); | |
cd95e351 | 834 | bt_ctf_field_put(ret_field); |
39d74371 JG |
835 | bt_ctf_field_type_put(uint_35_type); |
836 | bt_ctf_field_type_put(int_16_type); | |
837 | bt_ctf_field_type_put(string_type); | |
838 | bt_ctf_field_type_put(sequence_type); | |
1fac895e | 839 | bt_ctf_field_type_put(array_type); |
39d74371 JG |
840 | bt_ctf_field_type_put(inner_structure_type); |
841 | bt_ctf_field_type_put(complex_structure_type); | |
842 | bt_ctf_field_type_put(uint_3_type); | |
843 | bt_ctf_field_type_put(enum_variant_type); | |
844 | bt_ctf_field_type_put(variant_type); | |
7cfd41d6 | 845 | bt_ctf_field_type_put(ret_field_type); |
39d74371 JG |
846 | bt_ctf_event_class_put(event_class); |
847 | bt_ctf_event_put(event); | |
848 | } | |
849 | ||
850 | void type_field_tests() | |
851 | { | |
852 | struct bt_ctf_field *uint_12; | |
853 | struct bt_ctf_field *int_16; | |
854 | struct bt_ctf_field *string; | |
0abce37e | 855 | struct bt_ctf_field *enumeration; |
39d74371 JG |
856 | struct bt_ctf_field_type *composite_structure_type; |
857 | struct bt_ctf_field_type *structure_seq_type; | |
858 | struct bt_ctf_field_type *string_type; | |
859 | struct bt_ctf_field_type *sequence_type; | |
860 | struct bt_ctf_field_type *uint_8_type; | |
861 | struct bt_ctf_field_type *int_16_type; | |
862 | struct bt_ctf_field_type *uint_12_type = | |
863 | bt_ctf_field_type_integer_create(12); | |
0abce37e JG |
864 | struct bt_ctf_field_type *enumeration_type; |
865 | struct bt_ctf_field_type *enumeration_sequence_type; | |
866 | struct bt_ctf_field_type *enumeration_array_type; | |
10817e06 | 867 | struct bt_ctf_field_type *returned_type; |
7cfd41d6 | 868 | const char *ret_string; |
10817e06 JG |
869 | |
870 | returned_type = bt_ctf_field_get_type(NULL); | |
871 | ok(!returned_type, "bt_ctf_field_get_type handles NULL correctly"); | |
39d74371 JG |
872 | |
873 | ok(uint_12_type, "Create an unsigned integer type"); | |
874 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
875 | BT_CTF_INTEGER_BASE_BINARY) == 0, | |
876 | "Set integer type's base as binary"); | |
877 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
878 | BT_CTF_INTEGER_BASE_DECIMAL) == 0, | |
879 | "Set integer type's base as decimal"); | |
880 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
881 | BT_CTF_INTEGER_BASE_UNKNOWN), | |
882 | "Reject integer type's base set as unknown"); | |
883 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
884 | BT_CTF_INTEGER_BASE_OCTAL) == 0, | |
885 | "Set integer type's base as octal"); | |
886 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
887 | BT_CTF_INTEGER_BASE_HEXADECIMAL) == 0, | |
888 | "Set integer type's base as hexadecimal"); | |
889 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, 457417), | |
890 | "Reject unknown integer base value"); | |
891 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 952835) == 0, | |
892 | "Set integer type signedness to signed"); | |
893 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0) == 0, | |
894 | "Set integer type signedness to unsigned"); | |
7cfd41d6 JG |
895 | ok(bt_ctf_field_type_integer_get_size(NULL) < 0, |
896 | "bt_ctf_field_type_integer_get_size handles NULL correctly"); | |
897 | ok(bt_ctf_field_type_integer_get_size(uint_12_type) == 12, | |
898 | "bt_ctf_field_type_integer_get_size returns a correct value"); | |
899 | ok(bt_ctf_field_type_integer_get_signed(NULL) < 0, | |
900 | "bt_ctf_field_type_integer_get_signed handles NULL correctly"); | |
901 | ok(bt_ctf_field_type_integer_get_signed(uint_12_type) == 0, | |
902 | "bt_ctf_field_type_integer_get_signed returns a correct value for unsigned types"); | |
903 | ||
904 | ok(bt_ctf_field_type_set_byte_order(NULL, | |
905 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) < 0, | |
906 | "bt_ctf_field_type_set_byte_order handles NULL correctly"); | |
907 | ok(bt_ctf_field_type_set_byte_order(uint_12_type, | |
908 | (enum bt_ctf_byte_order) 42) < 0, | |
909 | "bt_ctf_field_type_set_byte_order rejects invalid values"); | |
910 | ok(bt_ctf_field_type_set_byte_order(uint_12_type, | |
911 | BT_CTF_BYTE_ORDER_LITTLE_ENDIAN) == 0, | |
912 | "Set an integer's byte order to little endian"); | |
913 | ok(bt_ctf_field_type_set_byte_order(uint_12_type, | |
914 | BT_CTF_BYTE_ORDER_BIG_ENDIAN) == 0, | |
915 | "Set an integer's byte order to big endian"); | |
916 | ok(bt_ctf_field_type_get_byte_order(uint_12_type) == | |
917 | BT_CTF_BYTE_ORDER_BIG_ENDIAN, | |
918 | "bt_ctf_field_type_get_byte_order returns a correct value"); | |
919 | ok(bt_ctf_field_type_get_byte_order(NULL) == | |
920 | BT_CTF_BYTE_ORDER_UNKNOWN, | |
921 | "bt_ctf_field_type_get_byte_order handles NULL correctly"); | |
922 | ||
923 | ok(bt_ctf_field_type_get_type_id(NULL) == | |
924 | CTF_TYPE_UNKNOWN, | |
925 | "bt_ctf_field_type_get_type_id handles NULL correctly"); | |
926 | ok(bt_ctf_field_type_get_type_id(uint_12_type) == | |
927 | CTF_TYPE_INTEGER, | |
928 | "bt_ctf_field_type_get_type_id returns a correct value with an integer type"); | |
929 | ||
930 | ok(bt_ctf_field_type_integer_get_base(NULL) == | |
931 | BT_CTF_INTEGER_BASE_UNKNOWN, | |
932 | "bt_ctf_field_type_integer_get_base handles NULL correctly"); | |
933 | ok(bt_ctf_field_type_integer_get_base(uint_12_type) == | |
934 | BT_CTF_INTEGER_BASE_HEXADECIMAL, | |
935 | "bt_ctf_field_type_integer_get_base returns a correct value"); | |
936 | ||
937 | ok(bt_ctf_field_type_integer_set_encoding(NULL, CTF_STRING_ASCII) < 0, | |
938 | "bt_ctf_field_type_integer_set_encoding handles NULL correctly"); | |
939 | ok(bt_ctf_field_type_integer_set_encoding(uint_12_type, | |
940 | (enum ctf_string_encoding) 123) < 0, | |
941 | "bt_ctf_field_type_integer_set_encoding handles invalid encodings correctly"); | |
942 | ok(bt_ctf_field_type_integer_set_encoding(uint_12_type, | |
943 | CTF_STRING_UTF8) == 0, | |
944 | "Set integer type encoding to UTF8"); | |
945 | ok(bt_ctf_field_type_integer_get_encoding(NULL) == CTF_STRING_UNKNOWN, | |
946 | "bt_ctf_field_type_integer_get_encoding handles NULL correctly"); | |
947 | ok(bt_ctf_field_type_integer_get_encoding(uint_12_type) == CTF_STRING_UTF8, | |
948 | "bt_ctf_field_type_integer_get_encoding returns a correct value"); | |
39d74371 JG |
949 | |
950 | int_16_type = bt_ctf_field_type_integer_create(16); | |
951 | bt_ctf_field_type_integer_set_signed(int_16_type, 1); | |
7cfd41d6 JG |
952 | ok(bt_ctf_field_type_integer_get_signed(int_16_type) == 1, |
953 | "bt_ctf_field_type_integer_get_signed returns a correct value for signed types"); | |
39d74371 JG |
954 | uint_8_type = bt_ctf_field_type_integer_create(8); |
955 | sequence_type = | |
956 | bt_ctf_field_type_sequence_create(int_16_type, "seq_len"); | |
957 | ok(sequence_type, "Create a sequence of int16_t type"); | |
7cfd41d6 JG |
958 | ok(bt_ctf_field_type_get_type_id(sequence_type) == |
959 | CTF_TYPE_SEQUENCE, | |
960 | "bt_ctf_field_type_get_type_id returns a correct value with a sequence type"); | |
961 | ||
962 | ok(bt_ctf_field_type_sequence_get_length_field_name(NULL) == NULL, | |
963 | "bt_ctf_field_type_sequence_get_length_field_name handles NULL correctly"); | |
964 | ret_string = bt_ctf_field_type_sequence_get_length_field_name( | |
965 | sequence_type); | |
966 | ok(!strcmp(ret_string, "seq_len"), | |
967 | "bt_ctf_field_type_sequence_get_length_field_name returns the correct value"); | |
968 | ok(bt_ctf_field_type_sequence_get_element_type(NULL) == NULL, | |
969 | "bt_ctf_field_type_sequence_get_element_type handles NULL correctly"); | |
970 | returned_type = bt_ctf_field_type_sequence_get_element_type( | |
971 | sequence_type); | |
972 | ok(returned_type == int_16_type, | |
973 | "bt_ctf_field_type_sequence_get_element_type returns the correct type"); | |
974 | bt_ctf_field_type_put(returned_type); | |
39d74371 JG |
975 | |
976 | string_type = bt_ctf_field_type_string_create(); | |
977 | ok(string_type, "Create a string type"); | |
978 | ok(bt_ctf_field_type_string_set_encoding(string_type, | |
979 | CTF_STRING_NONE), | |
980 | "Reject invalid \"None\" string encoding"); | |
981 | ok(bt_ctf_field_type_string_set_encoding(string_type, | |
982 | 42), | |
983 | "Reject invalid string encoding"); | |
984 | ok(bt_ctf_field_type_string_set_encoding(string_type, | |
985 | CTF_STRING_ASCII) == 0, | |
986 | "Set string encoding to ASCII"); | |
987 | ||
7cfd41d6 JG |
988 | ok(bt_ctf_field_type_string_get_encoding(NULL) == |
989 | CTF_STRING_UNKNOWN, | |
990 | "bt_ctf_field_type_string_get_encoding handles NULL correctly"); | |
991 | ok(bt_ctf_field_type_string_get_encoding(string_type) == | |
992 | CTF_STRING_ASCII, | |
993 | "bt_ctf_field_type_string_get_encoding returns the correct value"); | |
994 | ||
39d74371 | 995 | structure_seq_type = bt_ctf_field_type_structure_create(); |
7cfd41d6 JG |
996 | ok(bt_ctf_field_type_get_type_id(structure_seq_type) == |
997 | CTF_TYPE_STRUCT, | |
998 | "bt_ctf_field_type_get_type_id returns a correct value with a structure type"); | |
39d74371 JG |
999 | ok(structure_seq_type, "Create a structure type"); |
1000 | ok(bt_ctf_field_type_structure_add_field(structure_seq_type, | |
1001 | uint_8_type, "seq_len") == 0, | |
1002 | "Add a uint8_t type to a structure"); | |
1003 | ok(bt_ctf_field_type_structure_add_field(structure_seq_type, | |
1004 | sequence_type, "a_sequence") == 0, | |
1005 | "Add a sequence type to a structure"); | |
7cfd41d6 JG |
1006 | |
1007 | ok(bt_ctf_field_type_structure_get_field_count(NULL) < 0, | |
1008 | "bt_ctf_field_type_structure_get_field_count handles NULL correctly"); | |
1009 | ok(bt_ctf_field_type_structure_get_field_count(structure_seq_type) == 2, | |
1010 | "bt_ctf_field_type_structure_get_field_count returns a correct value"); | |
1011 | ||
1012 | ok(bt_ctf_field_type_structure_get_field(NULL, | |
1013 | &ret_string, &returned_type, 1) < 0, | |
1014 | "bt_ctf_field_type_structure_get_field handles a NULL type correctly"); | |
1015 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, | |
1016 | NULL, &returned_type, 1) < 0, | |
1017 | "bt_ctf_field_type_structure_get_field handles a NULL name correctly"); | |
1018 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, | |
1019 | &ret_string, NULL, 1) < 0, | |
1020 | "bt_ctf_field_type_structure_get_field handles a NULL return type correctly"); | |
1021 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, | |
1022 | &ret_string, &returned_type, 10) < 0, | |
1023 | "bt_ctf_field_type_structure_get_field handles an invalid index correctly"); | |
1024 | ok(bt_ctf_field_type_structure_get_field(structure_seq_type, | |
1025 | &ret_string, &returned_type, 1) == 0, | |
1026 | "bt_ctf_field_type_structure_get_field returns a field"); | |
1027 | ok(!strcmp(ret_string, "a_sequence"), | |
1028 | "bt_ctf_field_type_structure_get_field returns a correct field name"); | |
1029 | ok(returned_type == sequence_type, | |
1030 | "bt_ctf_field_type_structure_get_field returns a correct field type"); | |
1031 | bt_ctf_field_type_put(returned_type); | |
1032 | ||
1033 | ok(bt_ctf_field_type_structure_get_field_type_by_name(NULL, "a_sequence") == NULL, | |
1034 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL structure correctly"); | |
1035 | ok(bt_ctf_field_type_structure_get_field_type_by_name(structure_seq_type, NULL) == NULL, | |
1036 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly"); | |
1037 | returned_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
1038 | structure_seq_type, "a_sequence"); | |
1039 | ok(returned_type == sequence_type, | |
1040 | "bt_ctf_field_type_structure_get_field_type_by_name returns the correct field type"); | |
1041 | bt_ctf_field_type_put(returned_type); | |
1042 | ||
39d74371 JG |
1043 | composite_structure_type = bt_ctf_field_type_structure_create(); |
1044 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, | |
1045 | string_type, "a_string") == 0, | |
1046 | "Add a string type to a structure"); | |
1047 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, | |
1048 | structure_seq_type, "inner_structure") == 0, | |
1049 | "Add a structure type to a structure"); | |
1050 | ||
7cfd41d6 JG |
1051 | ok(bt_ctf_field_type_structure_get_field_type_by_name( |
1052 | NULL, "a_sequence") == NULL, | |
1053 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field correctly"); | |
1054 | ok(bt_ctf_field_type_structure_get_field_type_by_name( | |
1055 | structure_seq_type, NULL) == NULL, | |
1056 | "bt_ctf_field_type_structure_get_field_type_by_name handles a NULL field name correctly"); | |
1057 | returned_type = bt_ctf_field_type_structure_get_field_type_by_name( | |
1058 | structure_seq_type, "a_sequence"); | |
1059 | ok(returned_type == sequence_type, | |
1060 | "bt_ctf_field_type_structure_get_field_type_by_name returns a correct type"); | |
1061 | bt_ctf_field_type_put(returned_type); | |
1062 | ||
39d74371 JG |
1063 | int_16 = bt_ctf_field_create(int_16_type); |
1064 | ok(int_16, "Instanciate a signed 16-bit integer"); | |
1065 | uint_12 = bt_ctf_field_create(uint_12_type); | |
1066 | ok(uint_12, "Instanciate an unsigned 12-bit integer"); | |
10817e06 JG |
1067 | returned_type = bt_ctf_field_get_type(int_16); |
1068 | ok(returned_type == int_16_type, | |
1069 | "bt_ctf_field_get_type returns the correct type"); | |
39d74371 JG |
1070 | |
1071 | /* Can't modify types after instanciating them */ | |
1072 | ok(bt_ctf_field_type_integer_set_base(uint_12_type, | |
1073 | BT_CTF_INTEGER_BASE_DECIMAL), | |
1074 | "Check an integer type' base can't be modified after instanciation"); | |
1075 | ok(bt_ctf_field_type_integer_set_signed(uint_12_type, 0), | |
1076 | "Check an integer type's signedness can't be modified after instanciation"); | |
1077 | ||
1078 | /* Check signed property is checked */ | |
1079 | ok(bt_ctf_field_signed_integer_set_value(uint_12, -52), | |
1080 | "Check bt_ctf_field_signed_integer_set_value is not allowed on an unsigned integer"); | |
1081 | ok(bt_ctf_field_unsigned_integer_set_value(int_16, 42), | |
1082 | "Check bt_ctf_field_unsigned_integer_set_value is not allowed on a signed integer"); | |
1083 | ||
1084 | /* Check overflows are properly tested for */ | |
1085 | ok(bt_ctf_field_signed_integer_set_value(int_16, -32768) == 0, | |
1086 | "Check -32768 is allowed for a signed 16-bit integer"); | |
1087 | ok(bt_ctf_field_signed_integer_set_value(int_16, 32767) == 0, | |
1088 | "Check 32767 is allowed for a signed 16-bit integer"); | |
1089 | ok(bt_ctf_field_signed_integer_set_value(int_16, 32768), | |
1090 | "Check 32768 is not allowed for a signed 16-bit integer"); | |
1091 | ok(bt_ctf_field_signed_integer_set_value(int_16, -32769), | |
1092 | "Check -32769 is not allowed for a signed 16-bit integer"); | |
1093 | ok(bt_ctf_field_signed_integer_set_value(int_16, -42) == 0, | |
1094 | "Check -42 is allowed for a signed 16-bit integer"); | |
1095 | ||
1096 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4095) == 0, | |
1097 | "Check 4095 is allowed for an unsigned 12-bit integer"); | |
1098 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 4096), | |
1099 | "Check 4096 is not allowed for a unsigned 12-bit integer"); | |
1100 | ok(bt_ctf_field_unsigned_integer_set_value(uint_12, 0) == 0, | |
1101 | "Check 0 is allowed for an unsigned 12-bit integer"); | |
1102 | ||
1103 | string = bt_ctf_field_create(string_type); | |
1104 | ok(string, "Instanciate a string field"); | |
1105 | ok(bt_ctf_field_string_set_value(string, "A value") == 0, | |
1106 | "Set a string's value"); | |
1107 | ||
0abce37e JG |
1108 | enumeration_type = bt_ctf_field_type_enumeration_create(uint_12_type); |
1109 | ok(enumeration_type, | |
1110 | "Create an enumeration type with an unsigned 12-bit integer as container"); | |
1111 | enumeration_sequence_type = bt_ctf_field_type_sequence_create( | |
1112 | enumeration_type, "count"); | |
1113 | ok(!enumeration_sequence_type, | |
1114 | "Check enumeration types are validated when creating a sequence"); | |
1115 | enumeration_array_type = bt_ctf_field_type_array_create( | |
1116 | enumeration_type, 10); | |
1117 | ok(!enumeration_array_type, | |
1118 | "Check enumeration types are validated when creating an array"); | |
1119 | ok(bt_ctf_field_type_structure_add_field(composite_structure_type, | |
1120 | enumeration_type, "enumeration") == 0, | |
1121 | "Check enumeration types are validated when adding them as structure members"); | |
1122 | enumeration = bt_ctf_field_create(enumeration_type); | |
1123 | ok(!enumeration, | |
1124 | "Check enumeration types are validated before instantiation"); | |
1125 | ||
39d74371 JG |
1126 | bt_ctf_field_put(string); |
1127 | bt_ctf_field_put(uint_12); | |
1128 | bt_ctf_field_put(int_16); | |
0abce37e | 1129 | bt_ctf_field_put(enumeration); |
39d74371 JG |
1130 | bt_ctf_field_type_put(composite_structure_type); |
1131 | bt_ctf_field_type_put(structure_seq_type); | |
1132 | bt_ctf_field_type_put(string_type); | |
1133 | bt_ctf_field_type_put(sequence_type); | |
1134 | bt_ctf_field_type_put(uint_8_type); | |
1135 | bt_ctf_field_type_put(int_16_type); | |
1136 | bt_ctf_field_type_put(uint_12_type); | |
0abce37e JG |
1137 | bt_ctf_field_type_put(enumeration_type); |
1138 | bt_ctf_field_type_put(enumeration_sequence_type); | |
1139 | bt_ctf_field_type_put(enumeration_array_type); | |
7cfd41d6 | 1140 | bt_ctf_field_type_put(returned_type); |
39d74371 JG |
1141 | } |
1142 | ||
1143 | void packet_resize_test(struct bt_ctf_stream_class *stream_class, | |
1144 | struct bt_ctf_stream *stream, struct bt_ctf_clock *clock) | |
1145 | { | |
1146 | /* | |
1147 | * Append enough events to force the underlying packet to be resized. | |
1148 | * Also tests that a new event can be declared after a stream has been | |
1149 | * instantiated and used/flushed. | |
1150 | */ | |
1151 | int ret = 0; | |
1152 | int i; | |
1153 | struct bt_ctf_event_class *event_class = bt_ctf_event_class_create( | |
1154 | "Spammy_Event"); | |
1155 | struct bt_ctf_field_type *integer_type = | |
1156 | bt_ctf_field_type_integer_create(17); | |
1157 | struct bt_ctf_field_type *string_type = | |
1158 | bt_ctf_field_type_string_create(); | |
1159 | ||
1160 | ret |= bt_ctf_event_class_add_field(event_class, integer_type, | |
1161 | "field_1"); | |
1162 | ret |= bt_ctf_event_class_add_field(event_class, string_type, | |
1163 | "a_string"); | |
1164 | ret |= bt_ctf_stream_class_add_event_class(stream_class, event_class); | |
1165 | ok(ret == 0, "Add a new event class to a stream class after writing an event"); | |
1166 | if (ret) { | |
1167 | goto end; | |
1168 | } | |
1169 | ||
1170 | for (i = 0; i < PACKET_RESIZE_TEST_LENGTH; i++) { | |
1171 | struct bt_ctf_event *event = bt_ctf_event_create(event_class); | |
1172 | struct bt_ctf_field *integer = | |
1173 | bt_ctf_field_create(integer_type); | |
1174 | struct bt_ctf_field *string = | |
1175 | bt_ctf_field_create(string_type); | |
1176 | ||
1177 | ret |= bt_ctf_clock_set_time(clock, ++current_time); | |
1178 | ret |= bt_ctf_field_unsigned_integer_set_value(integer, i); | |
1179 | ret |= bt_ctf_event_set_payload(event, "field_1", | |
1180 | integer); | |
1181 | bt_ctf_field_put(integer); | |
1182 | ret |= bt_ctf_field_string_set_value(string, "This is a test"); | |
1183 | ret |= bt_ctf_event_set_payload(event, "a_string", | |
1184 | string); | |
1185 | bt_ctf_field_put(string); | |
1186 | ret |= bt_ctf_stream_append_event(stream, event); | |
1187 | bt_ctf_event_put(event); | |
1188 | ||
1189 | if (ret) { | |
1190 | break; | |
1191 | } | |
1192 | } | |
1193 | end: | |
1194 | ok(ret == 0, "Append 100 000 events to a stream"); | |
1195 | ok(bt_ctf_stream_flush(stream) == 0, | |
1196 | "Flush a stream that forces a packet resize"); | |
1197 | bt_ctf_field_type_put(integer_type); | |
1198 | bt_ctf_field_type_put(string_type); | |
1199 | bt_ctf_event_class_put(event_class); | |
1200 | } | |
1201 | ||
1202 | int main(int argc, char **argv) | |
1203 | { | |
1204 | char trace_path[] = "/tmp/ctfwriter_XXXXXX"; | |
1205 | char metadata_path[sizeof(trace_path) + 9]; | |
1206 | const char *clock_name = "test_clock"; | |
1207 | const char *clock_description = "This is a test clock"; | |
5494ce8b JG |
1208 | const char *returned_clock_name; |
1209 | const char *returned_clock_description; | |
1210 | const uint64_t frequency = 1123456789; | |
39d74371 JG |
1211 | const uint64_t offset_s = 1351530929945824323; |
1212 | const uint64_t offset = 1234567; | |
1213 | const uint64_t precision = 10; | |
5494ce8b | 1214 | const int is_absolute = 0xFF; |
39d74371 JG |
1215 | char *metadata_string; |
1216 | struct bt_ctf_writer *writer; | |
1217 | struct utsname name; | |
1218 | char hostname[HOST_NAME_MAX]; | |
1219 | struct bt_ctf_clock *clock; | |
1220 | struct bt_ctf_stream_class *stream_class; | |
1221 | struct bt_ctf_stream *stream1; | |
1222 | ||
1223 | if (argc < 3) { | |
1224 | printf("Usage: tests-ctf-writer path_to_ctf_parser_test path_to_babeltrace\n"); | |
1225 | exit(-1); | |
1226 | } | |
1227 | ||
1228 | plan_no_plan(); | |
1229 | ||
1230 | if (!mkdtemp(trace_path)) { | |
1231 | perror("# perror"); | |
1232 | } | |
1233 | ||
1234 | strcpy(metadata_path, trace_path); | |
1235 | strcat(metadata_path + sizeof(trace_path) - 1, "/metadata"); | |
1236 | ||
1237 | writer = bt_ctf_writer_create(trace_path); | |
1238 | ok(writer, "bt_ctf_create succeeds in creating trace with path"); | |
1239 | ||
1240 | /* Add environment context to the trace */ | |
1241 | gethostname(hostname, HOST_NAME_MAX); | |
1242 | ok(bt_ctf_writer_add_environment_field(writer, "host", hostname) == 0, | |
1243 | "Add host (%s) environment field to writer instance", | |
1244 | hostname); | |
1245 | ok(bt_ctf_writer_add_environment_field(NULL, "test_field", | |
1246 | "test_value"), | |
1247 | "bt_ctf_writer_add_environment_field error with NULL writer"); | |
1248 | ok(bt_ctf_writer_add_environment_field(writer, NULL, | |
1249 | "test_value"), | |
1250 | "bt_ctf_writer_add_environment_field error with NULL field name"); | |
1251 | ok(bt_ctf_writer_add_environment_field(writer, "test_field", | |
1252 | NULL), | |
1253 | "bt_ctf_writer_add_environment_field error with NULL field value"); | |
1254 | ||
1255 | if (uname(&name)) { | |
1256 | perror("uname"); | |
1257 | return -1; | |
1258 | } | |
1259 | ||
1260 | ok(bt_ctf_writer_add_environment_field(writer, "sysname", name.sysname) | |
1261 | == 0, "Add sysname (%s) environment field to writer instance", | |
1262 | name.sysname); | |
1263 | ok(bt_ctf_writer_add_environment_field(writer, "nodename", | |
1264 | name.nodename) == 0, | |
1265 | "Add nodename (%s) environment field to writer instance", | |
1266 | name.nodename); | |
1267 | ok(bt_ctf_writer_add_environment_field(writer, "release", name.release) | |
1268 | == 0, "Add release (%s) environment field to writer instance", | |
1269 | name.release); | |
1270 | ok(bt_ctf_writer_add_environment_field(writer, "version", name.version) | |
1271 | == 0, "Add version (%s) environment field to writer instance", | |
1272 | name.version); | |
1273 | ok(bt_ctf_writer_add_environment_field(writer, "machine", name.machine) | |
1274 | == 0, "Add machine (%s) environment field to writer istance", | |
1275 | name.machine); | |
1276 | ||
1277 | /* Define a clock and add it to the trace */ | |
5494ce8b JG |
1278 | ok(bt_ctf_clock_create("signed") == NULL, |
1279 | "Illegal clock name rejected"); | |
39d74371 JG |
1280 | ok(bt_ctf_clock_create(NULL) == NULL, "NULL clock name rejected"); |
1281 | clock = bt_ctf_clock_create(clock_name); | |
1282 | ok(clock, "Clock created sucessfully"); | |
5494ce8b JG |
1283 | returned_clock_name = bt_ctf_clock_get_name(clock); |
1284 | ok(returned_clock_name, "bt_ctf_clock_get_name returns a clock name"); | |
1285 | ok(strcmp(returned_clock_name, clock_name) == 0, | |
1286 | "Returned clock name is valid"); | |
1287 | ||
1288 | returned_clock_description = bt_ctf_clock_get_description(clock); | |
1289 | ok(!returned_clock_description, "bt_ctf_clock_get_description returns NULL on an unset description"); | |
1290 | ok(bt_ctf_clock_set_description(clock, clock_description) == 0, | |
1291 | "Clock description set successfully"); | |
1292 | ||
1293 | returned_clock_description = bt_ctf_clock_get_description(clock); | |
1294 | ok(returned_clock_description, | |
1295 | "bt_ctf_clock_get_description returns a description."); | |
1296 | ok(strcmp(returned_clock_description, clock_description) == 0, | |
1297 | "Returned clock description is valid"); | |
1298 | ||
1299 | ok(bt_ctf_clock_get_frequency(clock) == DEFAULT_CLOCK_FREQ, | |
1300 | "bt_ctf_clock_get_frequency returns the correct default frequency"); | |
39d74371 JG |
1301 | ok(bt_ctf_clock_set_frequency(clock, frequency) == 0, |
1302 | "Set clock frequency"); | |
5494ce8b JG |
1303 | ok(bt_ctf_clock_get_frequency(clock) == frequency, |
1304 | "bt_ctf_clock_get_frequency returns the correct frequency once it is set"); | |
1305 | ||
1306 | ok(bt_ctf_clock_get_offset_s(clock) == DEFAULT_CLOCK_OFFSET_S, | |
1307 | "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds)"); | |
39d74371 JG |
1308 | ok(bt_ctf_clock_set_offset_s(clock, offset_s) == 0, |
1309 | "Set clock offset (seconds)"); | |
5494ce8b JG |
1310 | ok(bt_ctf_clock_get_offset_s(clock) == offset_s, |
1311 | "bt_ctf_clock_get_offset_s returns the correct default offset (in seconds) once it is set"); | |
1312 | ||
1313 | ok(bt_ctf_clock_get_offset(clock) == DEFAULT_CLOCK_OFFSET, | |
1314 | "bt_ctf_clock_get_frequency returns the correct default offset (in ticks)"); | |
39d74371 | 1315 | ok(bt_ctf_clock_set_offset(clock, offset) == 0, "Set clock offset"); |
5494ce8b JG |
1316 | ok(bt_ctf_clock_get_offset(clock) == offset, |
1317 | "bt_ctf_clock_get_frequency returns the correct default offset (in ticks) once it is set"); | |
1318 | ||
1319 | ok(bt_ctf_clock_get_precision(clock) == DEFAULT_CLOCK_PRECISION, | |
1320 | "bt_ctf_clock_get_precision returns the correct default precision"); | |
39d74371 JG |
1321 | ok(bt_ctf_clock_set_precision(clock, precision) == 0, |
1322 | "Set clock precision"); | |
5494ce8b JG |
1323 | ok(bt_ctf_clock_get_precision(clock) == precision, |
1324 | "bt_ctf_clock_get_precision returns the correct precision once it is set"); | |
1325 | ||
1326 | ok(bt_ctf_clock_get_is_absolute(clock) == DEFAULT_CLOCK_IS_ABSOLUTE, | |
1327 | "bt_ctf_clock_get_precision returns the correct default is_absolute attribute"); | |
1328 | ok(bt_ctf_clock_set_is_absolute(clock, is_absolute) == 0, | |
39d74371 | 1329 | "Set clock absolute property"); |
5494ce8b JG |
1330 | ok(bt_ctf_clock_get_is_absolute(clock) == !!is_absolute, |
1331 | "bt_ctf_clock_get_precision returns the correct is_absolute attribute once it is set"); | |
1332 | ||
1333 | ok(bt_ctf_clock_get_time(clock) == DEFAULT_CLOCK_TIME, | |
1334 | "bt_ctf_clock_get_time returns the correct default time"); | |
1335 | ok(bt_ctf_clock_set_time(clock, current_time) == 0, | |
1336 | "Set clock time"); | |
1337 | ok(bt_ctf_clock_get_time(clock) == current_time, | |
1338 | "bt_ctf_clock_get_time returns the correct time once it is set"); | |
39d74371 JG |
1339 | |
1340 | ok(bt_ctf_writer_add_clock(writer, clock) == 0, | |
1341 | "Add clock to writer instance"); | |
1342 | ok(bt_ctf_writer_add_clock(writer, clock), | |
1343 | "Verify a clock can't be added twice to a writer instance"); | |
1344 | ||
5494ce8b JG |
1345 | ok(!bt_ctf_clock_get_name(NULL), |
1346 | "bt_ctf_clock_get_name correctly handles NULL"); | |
1347 | ok(!bt_ctf_clock_get_description(NULL), | |
1348 | "bt_ctf_clock_get_description correctly handles NULL"); | |
1349 | ok(bt_ctf_clock_get_frequency(NULL) == -1ULL, | |
1350 | "bt_ctf_clock_get_frequency correctly handles NULL"); | |
1351 | ok(bt_ctf_clock_get_precision(NULL) == -1ULL, | |
1352 | "bt_ctf_clock_get_precision correctly handles NULL"); | |
1353 | ok(bt_ctf_clock_get_offset_s(NULL) == -1ULL, | |
1354 | "bt_ctf_clock_get_offset_s correctly handles NULL"); | |
1355 | ok(bt_ctf_clock_get_offset(NULL) == -1ULL, | |
1356 | "bt_ctf_clock_get_offset correctly handles NULL"); | |
1357 | ok(bt_ctf_clock_get_is_absolute(NULL) == -1, | |
1358 | "bt_ctf_clock_get_is_absolute correctly handles NULL"); | |
1359 | ok(bt_ctf_clock_get_time(NULL) == -1ULL, | |
1360 | "bt_ctf_clock_get_time correctly handles NULL"); | |
1361 | ||
1362 | ok(bt_ctf_clock_set_description(NULL, NULL) == -1, | |
1363 | "bt_ctf_clock_set_description correctly handles NULL clock"); | |
1364 | ok(bt_ctf_clock_set_frequency(NULL, frequency) == -1, | |
1365 | "bt_ctf_clock_set_frequency correctly handles NULL clock"); | |
1366 | ok(bt_ctf_clock_set_precision(NULL, precision) == -1, | |
1367 | "bt_ctf_clock_get_precision correctly handles NULL clock"); | |
1368 | ok(bt_ctf_clock_set_offset_s(NULL, offset_s) == -1, | |
1369 | "bt_ctf_clock_set_offset_s correctly handles NULL clock"); | |
1370 | ok(bt_ctf_clock_set_offset(NULL, offset) == -1, | |
1371 | "bt_ctf_clock_set_offset correctly handles NULL clock"); | |
1372 | ok(bt_ctf_clock_set_is_absolute(NULL, is_absolute) == -1, | |
1373 | "bt_ctf_clock_set_is_absolute correctly handles NULL clock"); | |
1374 | ok(bt_ctf_clock_set_time(NULL, current_time) == -1, | |
1375 | "bt_ctf_clock_set_time correctly handles NULL clock"); | |
1376 | ||
39d74371 JG |
1377 | /* Define a stream class */ |
1378 | stream_class = bt_ctf_stream_class_create("test_stream"); | |
1379 | ok(stream_class, "Create stream class"); | |
1380 | ok(bt_ctf_stream_class_set_clock(stream_class, clock) == 0, | |
1381 | "Set a stream class' clock"); | |
1382 | ||
1383 | /* Test the event fields and event types APIs */ | |
1384 | type_field_tests(); | |
1385 | ||
1386 | /* Instantiate a stream and append events */ | |
1387 | stream1 = bt_ctf_writer_create_stream(writer, stream_class); | |
1388 | ok(stream1, "Instanciate a stream class from writer"); | |
1389 | ||
1390 | /* Should fail after instanciating a stream (locked)*/ | |
1391 | ok(bt_ctf_stream_class_set_clock(stream_class, clock), | |
1392 | "Changes to a stream class that was already instantiated fail"); | |
1393 | ||
1394 | append_simple_event(stream_class, stream1, clock); | |
1395 | ||
1396 | packet_resize_test(stream_class, stream1, clock); | |
1397 | ||
1398 | append_complex_event(stream_class, stream1, clock); | |
1399 | ||
1400 | metadata_string = bt_ctf_writer_get_metadata_string(writer); | |
1401 | ok(metadata_string, "Get metadata string"); | |
1402 | ||
1403 | bt_ctf_writer_flush_metadata(writer); | |
1404 | validate_metadata(argv[1], metadata_path); | |
1405 | validate_trace(argv[2], trace_path); | |
1406 | ||
1407 | bt_ctf_clock_put(clock); | |
1408 | bt_ctf_stream_class_put(stream_class); | |
1409 | bt_ctf_writer_put(writer); | |
1410 | bt_ctf_stream_put(stream1); | |
1411 | free(metadata_string); | |
1412 | ||
1413 | /* Remove all trace files and delete temporary trace directory */ | |
1414 | DIR *trace_dir = opendir(trace_path); | |
1415 | if (!trace_dir) { | |
1416 | perror("# opendir"); | |
1417 | return -1; | |
1418 | } | |
1419 | ||
1420 | struct dirent *entry; | |
1421 | while ((entry = readdir(trace_dir))) { | |
1422 | if (entry->d_type == DT_REG) { | |
1423 | unlinkat(dirfd(trace_dir), entry->d_name, 0); | |
1424 | } | |
1425 | } | |
1426 | ||
1427 | rmdir(trace_path); | |
1428 | closedir(trace_dir); | |
1429 | ||
1430 | return 0; | |
1431 | } |