source: pacpusframework/trunk/src/FileLib/test/TestFileLib.cpp@ 317

Last change on this file since 317 was 317, checked in by Marek Kurdej, 10 years ago

TestFileLib: minor fixes. Tests pass.

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