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