Include a test for SEEK_BEGIN and SEEK_LAST
[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 "common.h"
33 #include "tap.h"
34
35 #define NR_TESTS 11
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 int main(int argc, char **argv)
138 {
139 char *path;
140 uint64_t expected_begin;
141 uint64_t expected_last;
142
143 plan_tests(NR_TESTS);
144
145 if (argc < 4) {
146 plan_skip_all("Invalid arguments: need a trace path and the start and last timestamp");
147
148 }
149
150 /* Parse arguments (Trace, begin timestamp) */
151 path = argv[1];
152
153 expected_begin = strtoull(argv[2], NULL, 0);
154 if (ULLONG_MAX == expected_begin && errno == ERANGE) {
155 plan_skip_all("Invalid value for begin timestamp");
156 }
157
158 expected_last = strtoull(argv[3], NULL, 0);
159 if (ULLONG_MAX == expected_last && errno == ERANGE) {
160 plan_skip_all("Invalid value for last timestamp");
161 }
162
163 run_seek_begin(path, expected_begin);
164 run_seek_last(path, expected_last);
165
166 return exit_status();
167 }
This page took 0.031341 seconds and 4 git commands to generate.