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