From 155bbeaba33801c551c1036b55877aa956ff30c8 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Fri, 5 Dec 2014 11:50:24 +0100 Subject: [PATCH] Added test cases for OnDiskBlob that load and compare data --- .../blobstore/ondisk/OnDiskBlobLoadTest.cpp | 37 +++++++++++++++---- src/test/testutils/VirtualTestFile.cpp | 4 +- src/test/testutils/VirtualTestFile.h | 4 +- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/test/blobstore/ondisk/OnDiskBlobLoadTest.cpp b/src/test/blobstore/ondisk/OnDiskBlobLoadTest.cpp index f84cc228..b4465c45 100644 --- a/src/test/blobstore/ondisk/OnDiskBlobLoadTest.cpp +++ b/src/test/blobstore/ondisk/OnDiskBlobLoadTest.cpp @@ -1,6 +1,7 @@ #include "gtest/gtest.h" #include "test/testutils/TempFile.h" +#include "test/testutils/VirtualTestFile.h" #include "blobstore/implementations/ondisk/OnDiskBlob.h" #include "blobstore/implementations/ondisk/Data.h" @@ -19,7 +20,7 @@ using namespace blobstore::ondisk; namespace bf = boost::filesystem; -class OnDiskBlobLoadTest: public Test { +class OnDiskBlobLoadTest: public Test, public WithParamInterface { public: TempFile file; @@ -27,18 +28,40 @@ public: Data data(size); 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 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 {}; -INSTANTIATE_TEST_CASE_P(OnDiskBlobLoadSizeTest, OnDiskBlobLoadSizeTest, Values(0, 1, 5, 1024, 10*1024*1024)); - -TEST_P(OnDiskBlobLoadSizeTest, FileSizeIsCorrect) { +TEST_P(OnDiskBlobLoadTest, FileSizeIsCorrect) { SetFileSize(GetParam()); - auto blob = OnDiskBlob::LoadFromDisk(file.path()); + auto blob = LoadBlob(); 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 diff --git a/src/test/testutils/VirtualTestFile.cpp b/src/test/testutils/VirtualTestFile.cpp index 24be134d..ac863572 100644 --- a/src/test/testutils/VirtualTestFile.cpp +++ b/src/test/testutils/VirtualTestFile.cpp @@ -23,7 +23,7 @@ void VirtualTestFile::fillFileWithRandomData(long long int IV) { } } -const char *VirtualTestFile::data() { +const char *VirtualTestFile::data() const { return _fileData; } @@ -33,7 +33,7 @@ int VirtualTestFile::read(void *buf, size_t count, off_t offset) { return realCount; } -size_t VirtualTestFile::size() { +size_t VirtualTestFile::size() const { return _size; } diff --git a/src/test/testutils/VirtualTestFile.h b/src/test/testutils/VirtualTestFile.h index 1b3de2aa..2751be2f 100644 --- a/src/test/testutils/VirtualTestFile.h +++ b/src/test/testutils/VirtualTestFile.h @@ -14,9 +14,9 @@ public: // 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); - const char *data(); + const char *data() const; - size_t size(); + size_t size() const; protected: char *_fileData;