Refactor FuseReadReturnedDataTest to make VirtualTestFile reusable for the upcoming write tests
This commit is contained in:
parent
6c86b4ab2a
commit
b429432d83
@ -1,5 +1,7 @@
|
||||
#include "testutils/FuseReadTest.h"
|
||||
|
||||
#include "test/testutils/VirtualTestFile.h"
|
||||
|
||||
#include "fspp/impl/FuseErrnoException.h"
|
||||
|
||||
#include <tuple>
|
||||
@ -18,6 +20,8 @@ using ::testing::Action;
|
||||
using std::tuple;
|
||||
using std::get;
|
||||
using std::min;
|
||||
using std::unique_ptr;
|
||||
using std::make_unique;
|
||||
|
||||
using namespace fspp;
|
||||
|
||||
@ -41,12 +45,12 @@ struct TestData {
|
||||
// memory region and check methods to check for data equality of a region.
|
||||
class FuseReadReturnedDataTest: public FuseReadTest, public WithParamInterface<tuple<size_t, off_t, size_t>> {
|
||||
public:
|
||||
char *fileData;
|
||||
unique_ptr<VirtualTestFile> testFile;
|
||||
TestData testData;
|
||||
|
||||
void SetUp() override {
|
||||
FuseReadReturnedDataTest() {
|
||||
testData = GetParam();
|
||||
setupFileData();
|
||||
testFile = make_unique<VirtualTestFile>(testData.fileSize());
|
||||
|
||||
ReturnIsFileOnLstatWithSize(FILENAME, testData.fileSize());
|
||||
OnOpenReturnFileDescriptor(FILENAME, 0);
|
||||
@ -54,35 +58,10 @@ public:
|
||||
.WillRepeatedly(ReadFromFile);
|
||||
}
|
||||
|
||||
void TearDown() override {
|
||||
delete[] fileData;
|
||||
}
|
||||
|
||||
// Return true, iff the given data is equal to the data of the file at the given offset.
|
||||
bool fileContentCorrect(char *content, size_t count, off_t offset) {
|
||||
return 0 == memcmp(content, fileData + offset, count);
|
||||
}
|
||||
|
||||
// This read() mock implementation reads from the stored random data.
|
||||
// This read() mock implementation reads from the stored virtual file.
|
||||
Action<int(int, void*, size_t, off_t)> ReadFromFile = Invoke([this](int, void *buf, size_t count, off_t offset) {
|
||||
size_t realCount = min(count, testData.fileSize() - offset);
|
||||
memcpy(buf, fileData+offset, realCount);
|
||||
return realCount;
|
||||
return testFile->read(buf, count, offset);
|
||||
});
|
||||
private:
|
||||
void setupFileData() {
|
||||
fileData = new char[testData.fileSize()];
|
||||
fillFileWithRandomData();
|
||||
}
|
||||
void fillFileWithRandomData() {
|
||||
long long int val = 1;
|
||||
for(unsigned int i=0; i<testData.fileSize()/sizeof(long long int); ++i) {
|
||||
//MMIX linear congruential generator
|
||||
val *= 6364136223846793005L;
|
||||
val += 1442695040888963407;
|
||||
reinterpret_cast<long long int*>(fileData)[i] = val;
|
||||
}
|
||||
}
|
||||
};
|
||||
INSTANTIATE_TEST_CASE_P(FuseReadReturnedDataTest, FuseReadReturnedDataTest, Combine(Values(0,1,10,1000,1024, 10*1024*1024), Values(0, 1, 10, 1024, 10*1024*1024), Values(0, 1, 10, 1024, 10*1024*1024)));
|
||||
|
||||
@ -90,6 +69,6 @@ INSTANTIATE_TEST_CASE_P(FuseReadReturnedDataTest, FuseReadReturnedDataTest, Comb
|
||||
TEST_P(FuseReadReturnedDataTest, ReturnedDataRangeIsCorrect) {
|
||||
char *buf = new char[testData.count];
|
||||
ReadFile(FILENAME, buf, testData.count, testData.offset);
|
||||
EXPECT_TRUE(fileContentCorrect(buf, testData.count, testData.offset));
|
||||
EXPECT_TRUE(testFile->fileContentEqual(buf, testData.count, testData.offset));
|
||||
delete[] buf;
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ public:
|
||||
return count;
|
||||
});
|
||||
|
||||
// This read() mock implementation reads from the stored random data.
|
||||
::testing::Action<int(int, void*, size_t, off_t)> ReturnSuccessfulReadRegardingSize(size_t filesize) {
|
||||
return ::testing::Invoke([filesize](int, void *, size_t count, off_t offset) {
|
||||
size_t ableToReadCount = std::min(count, filesize - offset);
|
||||
|
44
src/test/testutils/VirtualTestFile.cpp
Normal file
44
src/test/testutils/VirtualTestFile.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#include "VirtualTestFile.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
using std::min;
|
||||
|
||||
VirtualTestFile::VirtualTestFile(size_t size): _fileData(new char[size]), _size(size) {
|
||||
fillFileWithRandomData();
|
||||
}
|
||||
|
||||
VirtualTestFile::~VirtualTestFile() {
|
||||
delete[] _fileData;
|
||||
}
|
||||
|
||||
void VirtualTestFile::fillFileWithRandomData() {
|
||||
long long int val = 1;
|
||||
for(size_t i=0; i<_size/sizeof(long long int); ++i) {
|
||||
//MMIX linear congruential generator
|
||||
val *= 6364136223846793005L;
|
||||
val += 1442695040888963407;
|
||||
reinterpret_cast<long long int*>(_fileData)[i] = val;
|
||||
}
|
||||
}
|
||||
|
||||
int VirtualTestFile::read(void *buf, size_t count, off_t offset) {
|
||||
size_t realCount = min(count, _size - offset);
|
||||
memcpy(buf, _fileData+offset, realCount);
|
||||
return realCount;
|
||||
}
|
||||
|
||||
bool VirtualTestFile::fileContentEqual(char *content, size_t count, off_t offset) {
|
||||
return 0 == memcmp(content, _fileData + offset, count);
|
||||
}
|
||||
|
||||
VirtualTestFileWriteable::VirtualTestFileWriteable(size_t size)
|
||||
:VirtualTestFile(size) {
|
||||
originalFileData = new char[size];
|
||||
memcpy(originalFileData, _fileData, size);
|
||||
}
|
||||
|
||||
VirtualTestFileWriteable::~VirtualTestFileWriteable() {
|
||||
delete[] originalFileData;
|
||||
}
|
33
src/test/testutils/VirtualTestFile.h
Normal file
33
src/test/testutils/VirtualTestFile.h
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#ifndef TEST_TESTUTILS_VIRTUALTESTFILE_H_
|
||||
#define TEST_TESTUTILS_VIRTUALTESTFILE_H_
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
class VirtualTestFile {
|
||||
public:
|
||||
VirtualTestFile(size_t size);
|
||||
virtual ~VirtualTestFile();
|
||||
|
||||
int read(void *buf, size_t count, off_t offset);
|
||||
|
||||
// Return true, iff the given data is equal to the data of the file at the given offset.
|
||||
bool fileContentEqual(char *content, size_t count, off_t offset);
|
||||
|
||||
protected:
|
||||
char *_fileData;
|
||||
size_t _size;
|
||||
|
||||
private:
|
||||
void fillFileWithRandomData();
|
||||
};
|
||||
|
||||
class VirtualTestFileWriteable: public VirtualTestFile {
|
||||
public:
|
||||
VirtualTestFileWriteable(size_t size);
|
||||
virtual ~VirtualTestFileWriteable();
|
||||
private:
|
||||
char *originalFileData;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user