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