Refactor FuseReadReturnedDataTest to make VirtualTestFile reusable for the upcoming write tests

This commit is contained in:
Sebastian Messmer 2014-11-27 15:23:51 +01:00
parent 51cc539f99
commit 22c1dff622
2 changed files with 77 additions and 0 deletions

View 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;
}

View 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