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