2015-02-17 00:48:49 +01:00
|
|
|
#include "DataBlockFixture.h"
|
2014-11-27 15:23:51 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using std::min;
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
DataBlockFixture::DataBlockFixture(size_t size, long long int IV): _fileData(new char[size]), _size(size) {
|
2014-11-27 16:40:22 +01:00
|
|
|
fillFileWithRandomData(IV);
|
2014-11-27 15:23:51 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
DataBlockFixture::~DataBlockFixture() {
|
2014-11-27 15:23:51 +01:00
|
|
|
delete[] _fileData;
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
void DataBlockFixture::fillFileWithRandomData(long long int IV) {
|
2014-11-27 16:40:22 +01:00
|
|
|
long long int val = IV;
|
2014-11-27 15:23:51 +01:00
|
|
|
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;
|
|
|
|
}
|
2015-03-04 02:59:46 +01:00
|
|
|
uint64_t alreadyWritten = (_size/sizeof(long long int))*sizeof(long long int);
|
|
|
|
val *= 6364136223846793005L;
|
|
|
|
val += 1442695040888963407;
|
|
|
|
char *remainingBytes = reinterpret_cast<char*>(&val);
|
2014-12-11 01:31:21 +01:00
|
|
|
//Fill remaining bytes
|
2015-03-04 02:59:46 +01:00
|
|
|
for(size_t i=0; i<_size-alreadyWritten; ++i) {
|
|
|
|
reinterpret_cast<char*>(_fileData)[alreadyWritten + i] = remainingBytes[i];
|
2014-12-11 01:31:21 +01:00
|
|
|
}
|
2014-11-27 15:23:51 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
const char *DataBlockFixture::data() const {
|
2014-11-27 16:40:22 +01:00
|
|
|
return _fileData;
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
int DataBlockFixture::read(void *buf, size_t count, off_t offset) {
|
2014-11-27 15:23:51 +01:00
|
|
|
size_t realCount = min(count, _size - offset);
|
|
|
|
memcpy(buf, _fileData+offset, realCount);
|
|
|
|
return realCount;
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
size_t DataBlockFixture::size() const {
|
2014-11-27 16:40:22 +01:00
|
|
|
return _size;
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
bool DataBlockFixture::fileContentEqual(const char *content, size_t count, off_t offset) {
|
2014-11-27 15:23:51 +01:00
|
|
|
return 0 == memcmp(content, _fileData + offset, count);
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
DataBlockFixtureWriteable::DataBlockFixtureWriteable(size_t size, long long int IV)
|
|
|
|
:DataBlockFixture(size, IV), _originalSize(size) {
|
2014-11-27 16:40:22 +01:00
|
|
|
_originalFileData = new char[size];
|
|
|
|
memcpy(_originalFileData, _fileData, size);
|
2014-11-27 15:23:51 +01:00
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
DataBlockFixtureWriteable::~DataBlockFixtureWriteable() {
|
2014-11-27 16:40:22 +01:00
|
|
|
delete[] _originalFileData;
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
void DataBlockFixtureWriteable::write(const void *buf, size_t count, off_t offset) {
|
2014-11-27 16:40:22 +01:00
|
|
|
extendFileSizeIfNecessary(count + offset);
|
|
|
|
|
|
|
|
memcpy(_fileData+offset, buf, count);
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
void DataBlockFixtureWriteable::extendFileSizeIfNecessary(size_t size) {
|
2014-11-27 16:40:22 +01:00
|
|
|
if (size > _size) {
|
|
|
|
extendFileSize(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
void DataBlockFixtureWriteable::extendFileSize(size_t size) {
|
2014-11-27 16:40:22 +01:00
|
|
|
char *newfile = new char[size];
|
|
|
|
memcpy(newfile, _fileData, _size);
|
|
|
|
delete[] _fileData;
|
|
|
|
_fileData = newfile;
|
|
|
|
_size = size;
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
bool DataBlockFixtureWriteable::sizeUnchanged() {
|
2014-11-27 16:40:22 +01:00
|
|
|
return _size == _originalSize;
|
|
|
|
}
|
|
|
|
|
2014-12-09 11:01:32 +01:00
|
|
|
bool DataBlockFixtureWriteable::regionUnchanged(off_t offset, size_t count) {
|
2014-11-27 16:40:22 +01:00
|
|
|
return 0 == memcmp(_fileData+offset, _originalFileData+offset, count);
|
2014-11-27 15:23:51 +01:00
|
|
|
}
|