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