Changeset View
Changeset View
Standalone View
Standalone View
src/test/flatfile_tests.cpp
Show All 10 Lines | |||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
BOOST_FIXTURE_TEST_SUITE(flatfile_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(flatfile_tests, BasicTestingSetup) | ||||
BOOST_AUTO_TEST_CASE(flatfile_filename) { | BOOST_AUTO_TEST_CASE(flatfile_filename) { | ||||
auto data_dir = SetDataDir("flatfile_test"); | auto data_dir = SetDataDir("flatfile_test"); | ||||
CDiskBlockPos pos(456, 789); | FlatFilePos pos(456, 789); | ||||
FlatFileSeq seq1(data_dir, "a", 16 * 1024); | FlatFileSeq seq1(data_dir, "a", 16 * 1024); | ||||
BOOST_CHECK_EQUAL(seq1.FileName(pos), data_dir / "a00456.dat"); | BOOST_CHECK_EQUAL(seq1.FileName(pos), data_dir / "a00456.dat"); | ||||
FlatFileSeq seq2(data_dir / "a", "b", 16 * 1024); | FlatFileSeq seq2(data_dir / "a", "b", 16 * 1024); | ||||
BOOST_CHECK_EQUAL(seq2.FileName(pos), data_dir / "a" / "b00456.dat"); | BOOST_CHECK_EQUAL(seq2.FileName(pos), data_dir / "a" / "b00456.dat"); | ||||
} | } | ||||
Show All 9 Lines | std::string line2("Digital signatures provide part of the solution, but " | ||||
"the main benefits are lost if a trusted third party is " | "the main benefits are lost if a trusted third party is " | ||||
"still required to prevent double-spending."); | "still required to prevent double-spending."); | ||||
size_t pos1 = 0; | size_t pos1 = 0; | ||||
size_t pos2 = pos1 + GetSerializeSize(line1, 0, CLIENT_VERSION); | size_t pos2 = pos1 + GetSerializeSize(line1, 0, CLIENT_VERSION); | ||||
// Write first line to file. | // Write first line to file. | ||||
{ | { | ||||
CAutoFile file(seq.Open(CDiskBlockPos(0, pos1)), SER_DISK, | CAutoFile file(seq.Open(FlatFilePos(0, pos1)), SER_DISK, | ||||
CLIENT_VERSION); | CLIENT_VERSION); | ||||
file << LIMITED_STRING(line1, 256); | file << LIMITED_STRING(line1, 256); | ||||
} | } | ||||
// Attempt to append to file opened in read-only mode. | // Attempt to append to file opened in read-only mode. | ||||
{ | { | ||||
CAutoFile file(seq.Open(CDiskBlockPos(0, pos2), true), SER_DISK, | CAutoFile file(seq.Open(FlatFilePos(0, pos2), true), SER_DISK, | ||||
CLIENT_VERSION); | CLIENT_VERSION); | ||||
BOOST_CHECK_THROW(file << LIMITED_STRING(line2, 256), | BOOST_CHECK_THROW(file << LIMITED_STRING(line2, 256), | ||||
std::ios_base::failure); | std::ios_base::failure); | ||||
} | } | ||||
// Append second line to file. | // Append second line to file. | ||||
{ | { | ||||
CAutoFile file(seq.Open(CDiskBlockPos(0, pos2)), SER_DISK, | CAutoFile file(seq.Open(FlatFilePos(0, pos2)), SER_DISK, | ||||
CLIENT_VERSION); | CLIENT_VERSION); | ||||
file << LIMITED_STRING(line2, 256); | file << LIMITED_STRING(line2, 256); | ||||
} | } | ||||
// Read text from file in read-only mode. | // Read text from file in read-only mode. | ||||
{ | { | ||||
std::string text; | std::string text; | ||||
CAutoFile file(seq.Open(CDiskBlockPos(0, pos1), true), SER_DISK, | CAutoFile file(seq.Open(FlatFilePos(0, pos1), true), SER_DISK, | ||||
CLIENT_VERSION); | CLIENT_VERSION); | ||||
file >> LIMITED_STRING(text, 256); | file >> LIMITED_STRING(text, 256); | ||||
BOOST_CHECK_EQUAL(text, line1); | BOOST_CHECK_EQUAL(text, line1); | ||||
file >> LIMITED_STRING(text, 256); | file >> LIMITED_STRING(text, 256); | ||||
BOOST_CHECK_EQUAL(text, line2); | BOOST_CHECK_EQUAL(text, line2); | ||||
} | } | ||||
// Read text from file with position offset. | // Read text from file with position offset. | ||||
{ | { | ||||
std::string text; | std::string text; | ||||
CAutoFile file(seq.Open(CDiskBlockPos(0, pos2)), SER_DISK, | CAutoFile file(seq.Open(FlatFilePos(0, pos2)), SER_DISK, | ||||
CLIENT_VERSION); | CLIENT_VERSION); | ||||
file >> LIMITED_STRING(text, 256); | file >> LIMITED_STRING(text, 256); | ||||
BOOST_CHECK_EQUAL(text, line2); | BOOST_CHECK_EQUAL(text, line2); | ||||
} | } | ||||
// Ensure another file in the sequence has no data. | // Ensure another file in the sequence has no data. | ||||
{ | { | ||||
std::string text; | std::string text; | ||||
CAutoFile file(seq.Open(CDiskBlockPos(1, pos2)), SER_DISK, | CAutoFile file(seq.Open(FlatFilePos(1, pos2)), SER_DISK, | ||||
CLIENT_VERSION); | CLIENT_VERSION); | ||||
BOOST_CHECK_THROW(file >> LIMITED_STRING(text, 256), | BOOST_CHECK_THROW(file >> LIMITED_STRING(text, 256), | ||||
std::ios_base::failure); | std::ios_base::failure); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(flatfile_allocate) { | BOOST_AUTO_TEST_CASE(flatfile_allocate) { | ||||
auto data_dir = SetDataDir("flatfile_test"); | auto data_dir = SetDataDir("flatfile_test"); | ||||
FlatFileSeq seq(data_dir, "a", 100); | FlatFileSeq seq(data_dir, "a", 100); | ||||
bool out_of_space; | bool out_of_space; | ||||
BOOST_CHECK_EQUAL(seq.Allocate(CDiskBlockPos(0, 0), 1, out_of_space), 100); | BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 0), 1, out_of_space), 100); | ||||
BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(CDiskBlockPos(0, 0))), 100); | BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 0))), 100); | ||||
BOOST_CHECK(!out_of_space); | BOOST_CHECK(!out_of_space); | ||||
BOOST_CHECK_EQUAL(seq.Allocate(CDiskBlockPos(0, 99), 1, out_of_space), 0); | BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 99), 1, out_of_space), 0); | ||||
BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(CDiskBlockPos(0, 99))), 100); | BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 99))), 100); | ||||
BOOST_CHECK(!out_of_space); | BOOST_CHECK(!out_of_space); | ||||
BOOST_CHECK_EQUAL(seq.Allocate(CDiskBlockPos(0, 99), 2, out_of_space), 101); | BOOST_CHECK_EQUAL(seq.Allocate(FlatFilePos(0, 99), 2, out_of_space), 101); | ||||
BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(CDiskBlockPos(0, 99))), 200); | BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 99))), 200); | ||||
BOOST_CHECK(!out_of_space); | BOOST_CHECK(!out_of_space); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(flatfile_flush) { | BOOST_AUTO_TEST_CASE(flatfile_flush) { | ||||
auto data_dir = SetDataDir("flatfile_test"); | auto data_dir = SetDataDir("flatfile_test"); | ||||
FlatFileSeq seq(data_dir, "a", 100); | FlatFileSeq seq(data_dir, "a", 100); | ||||
bool out_of_space; | bool out_of_space; | ||||
seq.Allocate(CDiskBlockPos(0, 0), 1, out_of_space); | seq.Allocate(FlatFilePos(0, 0), 1, out_of_space); | ||||
// Flush without finalize should not truncate file. | // Flush without finalize should not truncate file. | ||||
seq.Flush(CDiskBlockPos(0, 1)); | seq.Flush(FlatFilePos(0, 1)); | ||||
BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(CDiskBlockPos(0, 1))), 100); | BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 1))), 100); | ||||
// Flush with finalize should truncate file. | // Flush with finalize should truncate file. | ||||
seq.Flush(CDiskBlockPos(0, 1), true); | seq.Flush(FlatFilePos(0, 1), true); | ||||
BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(CDiskBlockPos(0, 1))), 1); | BOOST_CHECK_EQUAL(fs::file_size(seq.FileName(FlatFilePos(0, 1))), 1); | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |