source: pacpusframework/branches/0.0.x/src/FileLib/test/TestFileLib.cpp

Last change on this file was 76, checked in by Marek Kurdej, 12 years ago

Added: automated license updating lines:
%pacpus:license{
%pacpus:license}

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