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

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