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