Port: Use bash to run tests
[babeltrace.git] / tests / lib / test_seek.c
1 /*
2 * test-seeks.c
3 *
4 * Lib BabelTrace - Seeks test program
5 *
6 * Copyright 2012 - Yannick Brosseau <yannick.brosseau@gmail.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 #define _GNU_SOURCE
22 #include <babeltrace/context.h>
23 #include <babeltrace/iterator.h>
24 #include <babeltrace/ctf/iterator.h>
25 #include <babeltrace/ctf/events.h>
26 #include <babeltrace/babeltrace-internal.h> /* For symbol side-effects */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <babeltrace/compat/limits.h>
32
33 #include <tap/tap.h>
34 #include "common.h"
35
36 #define NR_TESTS 29
37
38 void run_seek_begin(char *path, uint64_t expected_begin)
39 {
40 struct bt_context *ctx;
41 struct bt_ctf_iter *iter;
42 struct bt_ctf_event *event;
43 struct bt_iter_pos newpos;
44 int ret;
45 uint64_t timestamp_begin;
46 uint64_t timestamp_seek_begin;
47 unsigned int nr_seek_begin_test;
48
49 nr_seek_begin_test = 5;
50
51 /* Open the trace */
52 ctx = create_context_with_path(path);
53 if (!ctx) {
54 skip(nr_seek_begin_test, "Cannot create valid context");
55 return;
56 }
57
58 /* Create iterator with null begin and end */
59 iter = bt_ctf_iter_create(ctx, NULL, NULL);
60 if (!iter) {
61 skip(nr_seek_begin_test, "Cannot create valid iterator");
62 return;
63 }
64
65 event = bt_ctf_iter_read_event(iter);
66
67 ok(event, "Event valid");
68
69 /* Validate that the first timestamp is right */
70 timestamp_begin = bt_ctf_get_timestamp(event);
71
72 ok1(timestamp_begin == expected_begin);
73
74 /* Validate that we get the same value after a seek begin */
75 newpos.type = BT_SEEK_BEGIN;
76 ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos);
77
78 ok(ret == 0, "Seek begin retval %d", ret);
79
80 event = bt_ctf_iter_read_event(iter);
81
82 ok(event, "Event valid");
83
84 timestamp_seek_begin = bt_ctf_get_timestamp(event);
85
86 ok1(timestamp_begin == timestamp_seek_begin);
87
88 bt_context_put(ctx);
89 }
90
91
92 void run_seek_last(char *path, uint64_t expected_last)
93 {
94 struct bt_context *ctx;
95 struct bt_ctf_iter *iter;
96 struct bt_ctf_event *event;
97 struct bt_iter_pos newpos;
98 int ret;
99 uint64_t timestamp_last;
100 unsigned int nr_seek_last_tests;
101
102 nr_seek_last_tests = 6;
103
104 /* Open the trace */
105 ctx = create_context_with_path(path);
106 if (!ctx) {
107 skip(nr_seek_last_tests, "Cannot create valid context");
108 return;
109 }
110
111 /* Create iterator with null last and end */
112 iter = bt_ctf_iter_create(ctx, NULL, NULL);
113 if (!iter) {
114 skip(nr_seek_last_tests, "Cannot create valid iterator");
115 return;
116 }
117
118 event = bt_ctf_iter_read_event(iter);
119
120 ok(event, "Event valid at beginning");
121
122 /* Seek to last */
123 newpos.type = BT_SEEK_LAST;
124 ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos);
125
126 ok(ret == 0, "Seek last retval %d", ret);
127
128 event = bt_ctf_iter_read_event(iter);
129
130 ok(event, "Event valid at last position");
131
132 timestamp_last = bt_ctf_get_timestamp(event);
133
134 ok1(timestamp_last == expected_last);
135
136 /* Try to read next event */
137 ret = bt_iter_next(bt_ctf_get_iter(iter));
138
139 ok(ret == 0, "iter next should return an error");
140
141 event = bt_ctf_iter_read_event(iter);
142
143 ok(event == 0, "Event after last should be invalid");
144
145 bt_context_put(ctx);
146 }
147
148 void run_seek_time_at_last(char *path, uint64_t expected_last)
149 {
150 struct bt_context *ctx;
151 struct bt_ctf_iter *iter;
152 struct bt_ctf_event *event;
153 struct bt_iter_pos newpos;
154 int ret;
155 uint64_t timestamp_last;
156 unsigned int nr_seek_time_at_last_tests;
157
158 nr_seek_time_at_last_tests = 6;
159
160 /* Open the trace */
161 ctx = create_context_with_path(path);
162 if (!ctx) {
163 skip(nr_seek_time_at_last_tests,
164 "Cannot create valid context");
165 return;
166 }
167
168 /* Create iterator with null last and end */
169 iter = bt_ctf_iter_create(ctx, NULL, NULL);
170 if (!iter) {
171 skip(nr_seek_time_at_last_tests,
172 "Cannot create valid iterator");
173 return;
174 }
175
176 event = bt_ctf_iter_read_event(iter);
177
178 ok(event, "Event valid at beginning");
179
180 /* Seek to last */
181 newpos.type = BT_SEEK_TIME;
182 newpos.u.seek_time = expected_last;
183 ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos);
184
185 ok(ret == 0, "Seek time at last retval %d", ret);
186
187 event = bt_ctf_iter_read_event(iter);
188
189 ok(event, "Event valid at last position");
190
191 timestamp_last = bt_ctf_get_timestamp(event);
192
193 ok1(timestamp_last == expected_last);
194
195 /* Try to read next event */
196 ret = bt_iter_next(bt_ctf_get_iter(iter));
197
198 ok(ret == 0, "iter next should return an error");
199
200 event = bt_ctf_iter_read_event(iter);
201
202 ok(event == 0, "Event after last should be invalid");
203
204 bt_context_put(ctx);
205 }
206
207 void run_seek_cycles(char *path,
208 uint64_t expected_begin,
209 uint64_t expected_last)
210 {
211 struct bt_context *ctx;
212 struct bt_ctf_iter *iter;
213 struct bt_ctf_event *event;
214 struct bt_iter_pos newpos;
215 int ret;
216 uint64_t timestamp;
217
218 unsigned int nr_seek_cycles_tests;
219
220 nr_seek_cycles_tests = 12;
221
222 /* Open the trace */
223 ctx = create_context_with_path(path);
224 if (!ctx) {
225 skip(nr_seek_cycles_tests, "Cannot create valid context");
226 return;
227 }
228
229 /* Create iterator with null last and end */
230 iter = bt_ctf_iter_create(ctx, NULL, NULL);
231 if (!iter) {
232 skip(nr_seek_cycles_tests, "Cannot create valid iterator");
233 return;
234 }
235
236 event = bt_ctf_iter_read_event(iter);
237
238 ok(event, "Event valid at beginning");
239
240 /* Seek to last */
241 newpos.type = BT_SEEK_LAST;
242 ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos);
243
244 ok(ret == 0, "Seek last retval %d", ret);
245
246 event = bt_ctf_iter_read_event(iter);
247
248 ok(event, "Event valid at last position");
249
250 timestamp = bt_ctf_get_timestamp(event);
251
252 ok1(timestamp == expected_last);
253
254 /* Try to read next event */
255 ret = bt_iter_next(bt_ctf_get_iter(iter));
256
257 ok(ret == 0, "iter next should return an error");
258
259 event = bt_ctf_iter_read_event(iter);
260
261 ok(event == 0, "Event after last should be invalid");
262
263 /* Seek to BEGIN */
264 newpos.type = BT_SEEK_BEGIN;
265 ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos);
266
267 ok(ret == 0, "Seek begin retval %d", ret);
268
269 event = bt_ctf_iter_read_event(iter);
270
271 ok(event, "Event valid at first position");
272
273 timestamp = bt_ctf_get_timestamp(event);
274
275 ok1(timestamp == expected_begin);
276
277 /* Seek last again */
278 newpos.type = BT_SEEK_LAST;
279 ret = bt_iter_set_pos(bt_ctf_get_iter(iter), &newpos);
280
281 ok(ret == 0, "Seek last retval %d", ret);
282
283 event = bt_ctf_iter_read_event(iter);
284
285 ok(event, "Event valid at last position");
286
287 timestamp = bt_ctf_get_timestamp(event);
288
289 ok1(timestamp == expected_last);
290
291 bt_context_put(ctx);
292 }
293
294 int main(int argc, char **argv)
295 {
296 char *path;
297 uint64_t expected_begin;
298 uint64_t expected_last;
299
300 /*
301 * Side-effects ensuring libs are not optimized away by static
302 * linking.
303 */
304 babeltrace_debug = 0; /* libbabeltrace.la */
305 opt_clock_offset = 0; /* libbabeltrace-ctf.la */
306
307 if (argc < 4) {
308 plan_skip_all("Invalid arguments: need a trace path and the start and last timestamp");
309
310 }
311
312 /* Parse arguments (Trace, begin timestamp) */
313 path = argv[1];
314
315 expected_begin = strtoull(argv[2], NULL, 0);
316 if (ULLONG_MAX == expected_begin && errno == ERANGE) {
317 plan_skip_all("Invalid value for begin timestamp");
318 }
319
320 expected_last = strtoull(argv[3], NULL, 0);
321 if (ULLONG_MAX == expected_last && errno == ERANGE) {
322 plan_skip_all("Invalid value for last timestamp");
323 }
324
325 plan_tests(NR_TESTS);
326
327 run_seek_begin(path, expected_begin);
328 run_seek_time_at_last(path, expected_last);
329 run_seek_last(path, expected_last);
330 run_seek_cycles(path, expected_begin, expected_last);
331
332 return exit_status();
333 }
This page took 0.036823 seconds and 4 git commands to generate.