Added test cases for OnDiskBlob that load and compare data

This commit is contained in:
Sebastian Messmer 2014-12-05 11:50:24 +01:00
parent dc8a81eef5
commit 155bbeaba3
3 changed files with 34 additions and 11 deletions

View File

@ -1,6 +1,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "test/testutils/TempFile.h" #include "test/testutils/TempFile.h"
#include "test/testutils/VirtualTestFile.h"
#include "blobstore/implementations/ondisk/OnDiskBlob.h" #include "blobstore/implementations/ondisk/OnDiskBlob.h"
#include "blobstore/implementations/ondisk/Data.h" #include "blobstore/implementations/ondisk/Data.h"
@ -19,7 +20,7 @@ using namespace blobstore::ondisk;
namespace bf = boost::filesystem; namespace bf = boost::filesystem;
class OnDiskBlobLoadTest: public Test { class OnDiskBlobLoadTest: public Test, public WithParamInterface<size_t> {
public: public:
TempFile file; TempFile file;
@ -27,18 +28,40 @@ public:
Data data(size); Data data(size);
data.StoreToFile(file.path()); data.StoreToFile(file.path());
} }
void StoreData(const VirtualTestFile &data) {
//TODO Implement data.StoreToFile(filepath) instead
Data dataobj(data.size());
std::memcpy(dataobj.data(), data.data(), data.size());
dataobj.StoreToFile(file.path());
}
unique_ptr<OnDiskBlob> LoadBlob() {
return OnDiskBlob::LoadFromDisk(file.path());
}
void EXPECT_BLOB_DATA_EQ(const VirtualTestFile &expected, const OnDiskBlob &actual) {
EXPECT_EQ(expected.size(), actual.size());
EXPECT_EQ(0, std::memcmp(expected.data(), actual.data(), expected.size()));
}
}; };
INSTANTIATE_TEST_CASE_P(OnDiskBlobLoadTest, OnDiskBlobLoadTest, Values(0, 1, 5, 1024, 10*1024*1024));
class OnDiskBlobLoadSizeTest: public OnDiskBlobLoadTest, public WithParamInterface<size_t> {}; TEST_P(OnDiskBlobLoadTest, FileSizeIsCorrect) {
INSTANTIATE_TEST_CASE_P(OnDiskBlobLoadSizeTest, OnDiskBlobLoadSizeTest, Values(0, 1, 5, 1024, 10*1024*1024));
TEST_P(OnDiskBlobLoadSizeTest, FileSizeIsCorrect) {
SetFileSize(GetParam()); SetFileSize(GetParam());
auto blob = OnDiskBlob::LoadFromDisk(file.path()); auto blob = LoadBlob();
EXPECT_EQ(GetParam(), blob->size()); EXPECT_EQ(GetParam(), blob->size());
} }
//TODO Load and compare actual data TEST_P(OnDiskBlobLoadTest, LoadedDataIsCorrect) {
VirtualTestFile randomData(GetParam());
StoreData(randomData);
auto blob = LoadBlob();
EXPECT_BLOB_DATA_EQ(randomData, *blob);
}
//TODO Test file doesn't exist //TODO Test file doesn't exist

View File

@ -23,7 +23,7 @@ void VirtualTestFile::fillFileWithRandomData(long long int IV) {
} }
} }
const char *VirtualTestFile::data() { const char *VirtualTestFile::data() const {
return _fileData; return _fileData;
} }
@ -33,7 +33,7 @@ int VirtualTestFile::read(void *buf, size_t count, off_t offset) {
return realCount; return realCount;
} }
size_t VirtualTestFile::size() { size_t VirtualTestFile::size() const {
return _size; return _size;
} }

View File

@ -14,9 +14,9 @@ public:
// Return true, iff the given data is equal to the data of the file at the given offset. // Return true, iff the given data is equal to the data of the file at the given offset.
bool fileContentEqual(const char *content, size_t count, off_t offset); bool fileContentEqual(const char *content, size_t count, off_t offset);
const char *data(); const char *data() const;
size_t size(); size_t size() const;
protected: protected:
char *_fileData; char *_fileData;