4 * Lib BabelTrace - Seeks test program
6 * Copyright 2012 - Yannick Brosseau <yannick.brosseau@gmail.com>
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.
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.
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.
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 */
31 #include <babeltrace/compat/limits.h>
38 void run_seek_begin(char *path
, uint64_t expected_begin
)
40 struct bt_context
*ctx
;
41 struct bt_ctf_iter
*iter
;
42 struct bt_ctf_event
*event
;
43 struct bt_iter_pos newpos
;
45 uint64_t timestamp_begin
;
46 uint64_t timestamp_seek_begin
;
47 unsigned int nr_seek_begin_test
;
49 nr_seek_begin_test
= 5;
52 ctx
= create_context_with_path(path
);
54 skip(nr_seek_begin_test
, "Cannot create valid context");
58 /* Create iterator with null begin and end */
59 iter
= bt_ctf_iter_create(ctx
, NULL
, NULL
);
61 skip(nr_seek_begin_test
, "Cannot create valid iterator");
65 event
= bt_ctf_iter_read_event(iter
);
67 ok(event
, "Event valid");
69 /* Validate that the first timestamp is right */
70 timestamp_begin
= bt_ctf_get_timestamp(event
);
72 ok1(timestamp_begin
== expected_begin
);
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
);
78 ok(ret
== 0, "Seek begin retval %d", ret
);
80 event
= bt_ctf_iter_read_event(iter
);
82 ok(event
, "Event valid");
84 timestamp_seek_begin
= bt_ctf_get_timestamp(event
);
86 ok1(timestamp_begin
== timestamp_seek_begin
);
92 void run_seek_last(char *path
, uint64_t expected_last
)
94 struct bt_context
*ctx
;
95 struct bt_ctf_iter
*iter
;
96 struct bt_ctf_event
*event
;
97 struct bt_iter_pos newpos
;
99 uint64_t timestamp_last
;
100 unsigned int nr_seek_last_tests
;
102 nr_seek_last_tests
= 6;
105 ctx
= create_context_with_path(path
);
107 skip(nr_seek_last_tests
, "Cannot create valid context");
111 /* Create iterator with null last and end */
112 iter
= bt_ctf_iter_create(ctx
, NULL
, NULL
);
114 skip(nr_seek_last_tests
, "Cannot create valid iterator");
118 event
= bt_ctf_iter_read_event(iter
);
120 ok(event
, "Event valid at beginning");
123 newpos
.type
= BT_SEEK_LAST
;
124 ret
= bt_iter_set_pos(bt_ctf_get_iter(iter
), &newpos
);
126 ok(ret
== 0, "Seek last retval %d", ret
);
128 event
= bt_ctf_iter_read_event(iter
);
130 ok(event
, "Event valid at last position");
132 timestamp_last
= bt_ctf_get_timestamp(event
);
134 ok1(timestamp_last
== expected_last
);
136 /* Try to read next event */
137 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
139 ok(ret
== 0, "iter next should return an error");
141 event
= bt_ctf_iter_read_event(iter
);
143 ok(event
== 0, "Event after last should be invalid");
148 void run_seek_time_at_last(char *path
, uint64_t expected_last
)
150 struct bt_context
*ctx
;
151 struct bt_ctf_iter
*iter
;
152 struct bt_ctf_event
*event
;
153 struct bt_iter_pos newpos
;
155 uint64_t timestamp_last
;
156 unsigned int nr_seek_time_at_last_tests
;
158 nr_seek_time_at_last_tests
= 6;
161 ctx
= create_context_with_path(path
);
163 skip(nr_seek_time_at_last_tests
,
164 "Cannot create valid context");
168 /* Create iterator with null last and end */
169 iter
= bt_ctf_iter_create(ctx
, NULL
, NULL
);
171 skip(nr_seek_time_at_last_tests
,
172 "Cannot create valid iterator");
176 event
= bt_ctf_iter_read_event(iter
);
178 ok(event
, "Event valid at beginning");
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
);
185 ok(ret
== 0, "Seek time at last retval %d", ret
);
187 event
= bt_ctf_iter_read_event(iter
);
189 ok(event
, "Event valid at last position");
191 timestamp_last
= bt_ctf_get_timestamp(event
);
193 ok1(timestamp_last
== expected_last
);
195 /* Try to read next event */
196 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
198 ok(ret
== 0, "iter next should return an error");
200 event
= bt_ctf_iter_read_event(iter
);
202 ok(event
== 0, "Event after last should be invalid");
207 void run_seek_cycles(char *path
,
208 uint64_t expected_begin
,
209 uint64_t expected_last
)
211 struct bt_context
*ctx
;
212 struct bt_ctf_iter
*iter
;
213 struct bt_ctf_event
*event
;
214 struct bt_iter_pos newpos
;
218 unsigned int nr_seek_cycles_tests
;
220 nr_seek_cycles_tests
= 12;
223 ctx
= create_context_with_path(path
);
225 skip(nr_seek_cycles_tests
, "Cannot create valid context");
229 /* Create iterator with null last and end */
230 iter
= bt_ctf_iter_create(ctx
, NULL
, NULL
);
232 skip(nr_seek_cycles_tests
, "Cannot create valid iterator");
236 event
= bt_ctf_iter_read_event(iter
);
238 ok(event
, "Event valid at beginning");
241 newpos
.type
= BT_SEEK_LAST
;
242 ret
= bt_iter_set_pos(bt_ctf_get_iter(iter
), &newpos
);
244 ok(ret
== 0, "Seek last retval %d", ret
);
246 event
= bt_ctf_iter_read_event(iter
);
248 ok(event
, "Event valid at last position");
250 timestamp
= bt_ctf_get_timestamp(event
);
252 ok1(timestamp
== expected_last
);
254 /* Try to read next event */
255 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
257 ok(ret
== 0, "iter next should return an error");
259 event
= bt_ctf_iter_read_event(iter
);
261 ok(event
== 0, "Event after last should be invalid");
264 newpos
.type
= BT_SEEK_BEGIN
;
265 ret
= bt_iter_set_pos(bt_ctf_get_iter(iter
), &newpos
);
267 ok(ret
== 0, "Seek begin retval %d", ret
);
269 event
= bt_ctf_iter_read_event(iter
);
271 ok(event
, "Event valid at first position");
273 timestamp
= bt_ctf_get_timestamp(event
);
275 ok1(timestamp
== expected_begin
);
277 /* Seek last again */
278 newpos
.type
= BT_SEEK_LAST
;
279 ret
= bt_iter_set_pos(bt_ctf_get_iter(iter
), &newpos
);
281 ok(ret
== 0, "Seek last retval %d", ret
);
283 event
= bt_ctf_iter_read_event(iter
);
285 ok(event
, "Event valid at last position");
287 timestamp
= bt_ctf_get_timestamp(event
);
289 ok1(timestamp
== expected_last
);
294 int main(int argc
, char **argv
)
297 uint64_t expected_begin
;
298 uint64_t expected_last
;
301 * Side-effects ensuring libs are not optimized away by static
304 babeltrace_debug
= 0; /* libbabeltrace.la */
305 opt_clock_offset
= 0; /* libbabeltrace-ctf.la */
308 plan_skip_all("Invalid arguments: need a trace path and the start and last timestamp");
312 /* Parse arguments (Trace, begin timestamp) */
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");
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");
325 plan_tests(NR_TESTS
);
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
);
332 return exit_status();