1 | // This file is part of the PACPUS framework distributed under the
|
---|
2 | // CECILL-C License, Version 1.0.
|
---|
3 | //
|
---|
4 | /// @version $Id: TestFileLib.cpp 64 2013-01-09 16:41:12Z kurdejma $
|
---|
5 |
|
---|
6 | #ifdef _MSC_VER
|
---|
7 | # pragma warning(push)
|
---|
8 | # pragma warning(disable:4265 4365 4571 4640)
|
---|
9 | #endif // _MSC_VER
|
---|
10 |
|
---|
11 | #define BOOST_TEST_MODULE TestFileLib
|
---|
12 | #include <boost/test/unit_test.hpp>
|
---|
13 |
|
---|
14 | #ifdef _MSC_VER
|
---|
15 | # pragma warning(pop)
|
---|
16 | #endif // _MSC_VER
|
---|
17 |
|
---|
18 | #include <boost/noncopyable.hpp>
|
---|
19 | #include <cstdio>
|
---|
20 |
|
---|
21 | #include "kernel/DbiteException.h"
|
---|
22 | #include "kernel/DbiteFile.h"
|
---|
23 | #include "kernel/Log.h"
|
---|
24 |
|
---|
25 | DECLARE_STATIC_LOGGER("pacpus.core.TestFileLib");
|
---|
26 |
|
---|
27 | using namespace boost;
|
---|
28 | using namespace pacpus;
|
---|
29 | using namespace std;
|
---|
30 |
|
---|
31 | struct HasExceptionMessage
|
---|
32 | : noncopyable
|
---|
33 | {
|
---|
34 | HasExceptionMessage(const string expectedMessage)
|
---|
35 | : mExpectedMessage(expectedMessage)
|
---|
36 | {
|
---|
37 | }
|
---|
38 |
|
---|
39 | bool operator()(const DbiteException & ex)
|
---|
40 | {
|
---|
41 | BOOST_TEST_CHECKPOINT("what() =" << ex.what());
|
---|
42 | return mExpectedMessage == ex.what();
|
---|
43 | }
|
---|
44 |
|
---|
45 | private:
|
---|
46 | const string mExpectedMessage;
|
---|
47 | };
|
---|
48 |
|
---|
49 | BOOST_AUTO_TEST_SUITE(suiteBasic)
|
---|
50 |
|
---|
51 | BOOST_AUTO_TEST_CASE(isNotOpenAfterConstruction)
|
---|
52 | {
|
---|
53 | DbiteFile df;
|
---|
54 | BOOST_CHECK(!df.isOpen());
|
---|
55 | }
|
---|
56 |
|
---|
57 | BOOST_AUTO_TEST_SUITE_END()
|
---|
58 |
|
---|
59 | BOOST_AUTO_TEST_SUITE(suiteRead)
|
---|
60 |
|
---|
61 | BOOST_AUTO_TEST_CASE(opensFileWithoutData)
|
---|
62 | {
|
---|
63 | DbiteFile df;
|
---|
64 | df.open("data/ok-empty-data.dbt", ReadMode);
|
---|
65 |
|
---|
66 | BOOST_CHECK_EQUAL(df.getDataOffset(), 48);
|
---|
67 | BOOST_CHECK_EQUAL(df.getPath(), "data/ok-empty-data.dbt");
|
---|
68 | BOOST_CHECK_EQUAL(df.getFileSize(), 48);
|
---|
69 | BOOST_CHECK_EQUAL(df.getFileSize(), df.getRealFileSize());
|
---|
70 | BOOST_CHECK_EQUAL(df.getRecordCount(), 0);
|
---|
71 | BOOST_CHECK_EQUAL(df.getRecordSize(), 32);
|
---|
72 | BOOST_CHECK_EQUAL(df.getSignature(), "ROAD");
|
---|
73 | BOOST_CHECK(df.getTimeMin() <= df.getTimeMax());
|
---|
74 | BOOST_CHECK_EQUAL(df.getType(), 12210);
|
---|
75 | BOOST_CHECK_EQUAL(df.getVersion(), 2);
|
---|
76 | }
|
---|
77 |
|
---|
78 | BOOST_AUTO_TEST_CASE(readsFileSingleData)
|
---|
79 | {
|
---|
80 | DbiteFile df;
|
---|
81 | df.open("data/ok-single-data.dbt", ReadMode);
|
---|
82 |
|
---|
83 | BOOST_CHECK_EQUAL(df.getDataOffset(), 44);
|
---|
84 | BOOST_CHECK_EQUAL(df.getPath(), "data/ok-single-data.dbt");
|
---|
85 | BOOST_CHECK_EQUAL(df.getFileSize(), 64);
|
---|
86 | BOOST_CHECK_EQUAL(df.getFileSize(), df.getRealFileSize());
|
---|
87 | BOOST_CHECK_EQUAL(df.getRecordCount(), 1);
|
---|
88 | BOOST_CHECK_EQUAL(df.getRecordSize(), 8);
|
---|
89 | BOOST_CHECK_EQUAL(df.getSignature(), "ROAD");
|
---|
90 | BOOST_CHECK_EQUAL(df.getTimeMin(), df.getTimeMax());
|
---|
91 | BOOST_CHECK_EQUAL(df.getTimeMin(), static_cast<road_time_t>(1311847165023717));
|
---|
92 | BOOST_CHECK_EQUAL(df.getTimeMax(), static_cast<road_time_t>(1311847165023717));
|
---|
93 | BOOST_CHECK_EQUAL(df.getType(), 100);
|
---|
94 | BOOST_CHECK_EQUAL(df.getVersion(), 2);
|
---|
95 |
|
---|
96 | road_time_t t;
|
---|
97 | road_timerange_t tr;
|
---|
98 | char data[8];
|
---|
99 |
|
---|
100 | df.readRecord(t, tr, data);
|
---|
101 | BOOST_CHECK_EQUAL(t, static_cast<road_time_t>(1311847165023717));
|
---|
102 | BOOST_CHECK_EQUAL(tr, 0);
|
---|
103 |
|
---|
104 | int64_t value = *((uint64_t *)data);
|
---|
105 | BOOST_CHECK_EQUAL(value, (int64_t) (static_cast<uint64_t>(15) << 32) + 0);
|
---|
106 | }
|
---|
107 |
|
---|
108 | BOOST_AUTO_TEST_CASE(readsFileManyData)
|
---|
109 | {
|
---|
110 | DbiteFile df;
|
---|
111 | df.open("data/ok-many-data.dbt", ReadMode);
|
---|
112 |
|
---|
113 | BOOST_CHECK_EQUAL(df.getDataOffset(), 44);
|
---|
114 | BOOST_CHECK_EQUAL(df.getPath(), "data/ok-many-data.dbt");
|
---|
115 | BOOST_CHECK_EQUAL(df.getFileSize(), 344);
|
---|
116 | BOOST_CHECK_EQUAL(df.getFileSize(), df.getRealFileSize());
|
---|
117 | BOOST_CHECK_EQUAL(df.getRecordCount(), 15);
|
---|
118 | BOOST_CHECK_EQUAL(df.getRecordSize(), 8);
|
---|
119 | BOOST_CHECK_EQUAL(df.getSignature(), "ROAD");
|
---|
120 | BOOST_CHECK(df.getTimeMin() < df.getTimeMax());
|
---|
121 | BOOST_CHECK_EQUAL(df.getTimeMin(), static_cast<road_time_t>(1311847165023717));
|
---|
122 | BOOST_CHECK_EQUAL(df.getTimeMax(), static_cast<road_time_t>(1311847166112739));
|
---|
123 | BOOST_CHECK_EQUAL(df.getType(), 100);
|
---|
124 | BOOST_CHECK_EQUAL(df.getVersion(), 2);
|
---|
125 |
|
---|
126 | for (uint64_t ir = 0; ir < df.getRecordCount(); ++ir) {
|
---|
127 | road_time_t t;
|
---|
128 | road_timerange_t tr;
|
---|
129 | char data[8];
|
---|
130 |
|
---|
131 | df.readRecord(t, tr, data);
|
---|
132 | BOOST_CHECK(df.getTimeMin() <= t);
|
---|
133 | BOOST_CHECK(t <= df.getTimeMax());
|
---|
134 | BOOST_CHECK_EQUAL(tr, 0);
|
---|
135 |
|
---|
136 | int64_t value = *((uint64_t *)data);
|
---|
137 | BOOST_CHECK_EQUAL(value, (int64_t) (ir << 32) + 0x01A0);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | BOOST_AUTO_TEST_CASE(throwsDbiteExceptionOnEmptyFile)
|
---|
142 | {
|
---|
143 | DbiteFile df;
|
---|
144 | BOOST_CHECK_THROW(
|
---|
145 | (df.open("data/bad-empty-file.dbt", ReadMode))
|
---|
146 | , DbiteException
|
---|
147 | );
|
---|
148 | BOOST_CHECK_EXCEPTION(
|
---|
149 | (df.open("data/bad-empty-file.dbt", ReadMode))
|
---|
150 | , DbiteException
|
---|
151 | , HasExceptionMessage("cannot read header")
|
---|
152 | );
|
---|
153 | }
|
---|
154 |
|
---|
155 | BOOST_AUTO_TEST_CASE(throwsDbiteExceptionOnIncompleteHeader)
|
---|
156 | {
|
---|
157 | DbiteFile df;
|
---|
158 | BOOST_CHECK_THROW(
|
---|
159 | (df.open("data/bad-incomplete-header.dbt", ReadMode))
|
---|
160 | , DbiteException
|
---|
161 | );
|
---|
162 | BOOST_CHECK_EXCEPTION(
|
---|
163 | (df.open("data/bad-incomplete-header.dbt", ReadMode))
|
---|
164 | , DbiteException
|
---|
165 | , HasExceptionMessage("cannot read header")
|
---|
166 | );
|
---|
167 | }
|
---|
168 |
|
---|
169 | BOOST_AUTO_TEST_CASE(throwsDbiteExceptionOnBadSignature)
|
---|
170 | {
|
---|
171 | DbiteFile df;
|
---|
172 | BOOST_CHECK_THROW(
|
---|
173 | (df.open("data/bad-signature.dbt", ReadMode))
|
---|
174 | , DbiteException
|
---|
175 | );
|
---|
176 | BOOST_CHECK_EXCEPTION(
|
---|
177 | (df.open("data/bad-signature.dbt", ReadMode))
|
---|
178 | , DbiteException
|
---|
179 | , HasExceptionMessage("bad signature")
|
---|
180 | );
|
---|
181 | }
|
---|
182 |
|
---|
183 | BOOST_AUTO_TEST_CASE(throwsDbiteExceptionOnWrongVersion)
|
---|
184 | {
|
---|
185 | DbiteFile df;
|
---|
186 | BOOST_CHECK_THROW(
|
---|
187 | (df.open("data/bad-wrong-version.dbt", ReadMode))
|
---|
188 | , DbiteException
|
---|
189 | );
|
---|
190 | BOOST_CHECK_EXCEPTION(
|
---|
191 | (df.open("data/bad-wrong-version.dbt", ReadMode))
|
---|
192 | , DbiteException
|
---|
193 | , HasExceptionMessage("bad version number")
|
---|
194 | );
|
---|
195 | }
|
---|
196 |
|
---|
197 | BOOST_AUTO_TEST_SUITE_END()
|
---|
198 |
|
---|
199 | BOOST_AUTO_TEST_SUITE(suiteWrite)
|
---|
200 |
|
---|
201 | BOOST_AUTO_TEST_CASE(throwsDbiteExceptionWhenFileDoesntExist)
|
---|
202 | {
|
---|
203 | DbiteFile df;
|
---|
204 | const char * kInexistingFilename = "";
|
---|
205 |
|
---|
206 | BOOST_CHECK_THROW(
|
---|
207 | (df.open(kInexistingFilename, ReadMode))
|
---|
208 | , DbiteException
|
---|
209 | );
|
---|
210 | BOOST_CHECK_EXCEPTION(
|
---|
211 | (df.open(kInexistingFilename, ReadMode))
|
---|
212 | , DbiteException
|
---|
213 | , HasExceptionMessage("cannot open file for reading")
|
---|
214 | );
|
---|
215 | }
|
---|
216 |
|
---|
217 | BOOST_AUTO_TEST_CASE(throwsDbiteExceptionWhenCannotCreateTheFile)
|
---|
218 | {
|
---|
219 | DbiteFile df;
|
---|
220 | const char * kIllegalFilename = "";
|
---|
221 |
|
---|
222 | BOOST_CHECK_THROW(
|
---|
223 | (df.open(kIllegalFilename, WriteMode))
|
---|
224 | , DbiteException
|
---|
225 | );
|
---|
226 | BOOST_CHECK_EXCEPTION(
|
---|
227 | (df.open(kIllegalFilename, WriteMode))
|
---|
228 | , DbiteException
|
---|
229 | , HasExceptionMessage("cannot open file for writing")
|
---|
230 | );
|
---|
231 | }
|
---|
232 |
|
---|
233 | // TODO: creates file for writing when it doesn't exist
|
---|
234 | // TODO: creates file for writing when it exists ??? warning?
|
---|
235 |
|
---|
236 | BOOST_AUTO_TEST_SUITE_END()
|
---|
237 |
|
---|
238 | BOOST_AUTO_TEST_SUITE(suiteWrite)
|
---|
239 |
|
---|
240 | BOOST_AUTO_TEST_CASE(writesNormallyWhenTheFileDoesntExist)
|
---|
241 | {
|
---|
242 | DbiteFile df;
|
---|
243 | const char * filename = "data/unexisting-file.dbt";
|
---|
244 |
|
---|
245 | // delete file
|
---|
246 | ::remove(filename);
|
---|
247 |
|
---|
248 | // create
|
---|
249 | df.open(filename, WriteMode, 0x4321, 0x1234);
|
---|
250 | BOOST_REQUIRE(df.isOpen());
|
---|
251 | df.close();
|
---|
252 |
|
---|
253 | df.open(filename, ReadMode);
|
---|
254 | BOOST_REQUIRE(df.isOpen());
|
---|
255 | BOOST_CHECK_EQUAL(df.getPath(), filename);
|
---|
256 | BOOST_CHECK_EQUAL(df.getFileSize(), df.getRealFileSize());
|
---|
257 | BOOST_CHECK_EQUAL(df.getRecordCount(), 0);
|
---|
258 | BOOST_CHECK_EQUAL(df.getRecordSize(), 0x1234);
|
---|
259 | BOOST_CHECK_EQUAL(df.getSignature(), "ROAD");
|
---|
260 | BOOST_CHECK_EQUAL(df.getType(), 0x4321);
|
---|
261 | BOOST_CHECK_EQUAL(df.getVersion(), 2);
|
---|
262 | df.close();
|
---|
263 | }
|
---|
264 |
|
---|
265 | BOOST_AUTO_TEST_CASE(appendsAtEndWhenFileExists)
|
---|
266 | {
|
---|
267 | DbiteFile df;
|
---|
268 | const hdfile_header_t::DataSizeT dataSize = 0x1234;
|
---|
269 | const hdfile_header_t::DataTypeT dataType = 0x4321;
|
---|
270 | const char * filename = "data/existing-file-no-data.dbt";
|
---|
271 |
|
---|
272 | road_time_t time = 0xAABBCCDD;
|
---|
273 | road_timerange_t tr = 0xABCD;
|
---|
274 | char data[dataSize];
|
---|
275 |
|
---|
276 | // delete file
|
---|
277 | ::remove(filename);
|
---|
278 |
|
---|
279 | // create
|
---|
280 | df.open(filename, WriteMode, dataType, 0x1234);
|
---|
281 | BOOST_REQUIRE(df.isOpen());
|
---|
282 | df.close();
|
---|
283 | BOOST_REQUIRE(!df.isOpen());
|
---|
284 |
|
---|
285 | // append
|
---|
286 | df.open(filename, WriteMode, dataType, dataSize);
|
---|
287 | BOOST_REQUIRE(df.isOpen());
|
---|
288 | df.writeRecord(time++, tr, data, dataSize);
|
---|
289 | df.close();
|
---|
290 | BOOST_REQUIRE(!df.isOpen());
|
---|
291 |
|
---|
292 | // check
|
---|
293 | df.open(filename, ReadMode);
|
---|
294 | BOOST_CHECK_EQUAL(df.getPath(), filename);
|
---|
295 | BOOST_CHECK_EQUAL(df.getFileSize(), df.getRealFileSize());
|
---|
296 | BOOST_CHECK_EQUAL(df.getRecordCount(), 1);
|
---|
297 | BOOST_CHECK_EQUAL(df.getRecordSize(), dataSize);
|
---|
298 | BOOST_CHECK_EQUAL(df.getSignature(), "ROAD");
|
---|
299 | BOOST_CHECK_EQUAL(df.getType(), dataType);
|
---|
300 | BOOST_CHECK_EQUAL(df.getVersion(), 2);
|
---|
301 | df.close();
|
---|
302 |
|
---|
303 | // append
|
---|
304 | df.open(filename, WriteMode, dataType, dataSize);
|
---|
305 | BOOST_REQUIRE(df.isOpen());
|
---|
306 | df.writeRecord(time++, tr, data, dataSize);
|
---|
307 | df.close();
|
---|
308 | BOOST_REQUIRE(!df.isOpen());
|
---|
309 |
|
---|
310 | // check
|
---|
311 | df.open(filename, ReadMode);
|
---|
312 | BOOST_CHECK_EQUAL(df.getPath(), filename);
|
---|
313 | BOOST_CHECK_EQUAL(df.getFileSize(), df.getRealFileSize());
|
---|
314 | BOOST_CHECK_EQUAL(df.getRecordCount(), 2);
|
---|
315 | BOOST_CHECK_EQUAL(df.getRecordSize(), dataSize);
|
---|
316 | BOOST_CHECK_EQUAL(df.getSignature(), "ROAD");
|
---|
317 | BOOST_CHECK_EQUAL(df.getType(), dataType);
|
---|
318 | BOOST_CHECK_EQUAL(df.getVersion(), 2);
|
---|
319 | df.close();
|
---|
320 | }
|
---|
321 |
|
---|
322 | BOOST_AUTO_TEST_SUITE_END()
|
---|