Removed DataBlockFixture

This commit is contained in:
Sebastian Messmer 2015-04-25 17:45:18 +02:00
parent a07c03d752
commit 61337eef85
4 changed files with 42 additions and 217 deletions

View File

@ -1,91 +0,0 @@
#include "DataBlockFixture.h"
#include <algorithm>
#include <cstring>
using std::min;
namespace cpputils {
DataBlockFixture::DataBlockFixture(size_t size, long long int IV): _fileData(new char[size]), _size(size) {
fillFileWithRandomData(IV);
}
DataBlockFixture::~DataBlockFixture() {
delete[] _fileData;
}
void DataBlockFixture::fillFileWithRandomData(long long int IV) {
long long int val = IV;
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;
}
uint64_t alreadyWritten = (_size/sizeof(long long int))*sizeof(long long int);
val *= 6364136223846793005L;
val += 1442695040888963407;
char *remainingBytes = reinterpret_cast<char*>(&val);
//Fill remaining bytes
for(size_t i=0; i<_size-alreadyWritten; ++i) {
reinterpret_cast<char*>(_fileData)[alreadyWritten + i] = remainingBytes[i];
}
}
const char *DataBlockFixture::data() const {
return _fileData;
}
int DataBlockFixture::read(void *buf, size_t count, off_t offset) {
size_t realCount = min(count, _size - offset);
memcpy(buf, _fileData+offset, realCount);
return realCount;
}
size_t DataBlockFixture::size() const {
return _size;
}
bool DataBlockFixture::fileContentEqual(const char *content, size_t count, off_t offset) {
return 0 == memcmp(content, _fileData + offset, count);
}
DataBlockFixtureWriteable::DataBlockFixtureWriteable(size_t size, long long int IV)
:DataBlockFixture(size, IV), _originalSize(size) {
_originalFileData = new char[size];
memcpy(_originalFileData, _fileData, size);
}
DataBlockFixtureWriteable::~DataBlockFixtureWriteable() {
delete[] _originalFileData;
}
void DataBlockFixtureWriteable::write(const void *buf, size_t count, off_t offset) {
extendFileSizeIfNecessary(count + offset);
memcpy(_fileData+offset, buf, count);
}
void DataBlockFixtureWriteable::extendFileSizeIfNecessary(size_t size) {
if (size > _size) {
extendFileSize(size);
}
}
void DataBlockFixtureWriteable::extendFileSize(size_t size) {
char *newfile = new char[size];
memcpy(newfile, _fileData, _size);
delete[] _fileData;
_fileData = newfile;
_size = size;
}
bool DataBlockFixtureWriteable::sizeUnchanged() {
return _size == _originalSize;
}
bool DataBlockFixtureWriteable::regionUnchanged(off_t offset, size_t count) {
return 0 == memcmp(_fileData+offset, _originalFileData+offset, count);
}
}

View File

@ -1,54 +0,0 @@
#pragma once
#ifndef MESSMER_CPPUTILS_DATA_DATABLOCKFIXTURE_H_
#define MESSMER_CPPUTILS_DATA_DATABLOCKFIXTURE_H_
#include <cstdio>
namespace cpputils {
//TODO Rewrite to using cpputils::Data (but take care that the cpputils::Data tests actually still test something, they use DataBlockFixture)
//TODO Add tests
class DataBlockFixture {
public:
DataBlockFixture(size_t size, long long int IV = 1);
virtual ~DataBlockFixture();
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(const char *content, size_t count, off_t offset);
const char *data() const;
size_t size() const;
protected:
char *_fileData;
size_t _size;
private:
void fillFileWithRandomData(long long int IV);
};
class DataBlockFixtureWriteable: public DataBlockFixture {
public:
DataBlockFixtureWriteable(size_t size, long long int IV = 1);
virtual ~DataBlockFixtureWriteable();
void write(const void *buf, size_t count, off_t offset);
bool sizeUnchanged();
bool regionUnchanged(off_t offset, size_t count);
private:
void extendFileSizeIfNecessary(size_t size);
void extendFileSize(size_t size);
char *_originalFileData;
size_t _originalSize;
};
}
#endif

View File

@ -1,4 +1,4 @@
#include "../../data/DataBlockFixture.h"
#include "../../data/DataFixture.h"
#include "../../data/Data.h"
#include "google/gtest/gtest.h"
@ -29,58 +29,46 @@ public:
}
return true;
}
void FillData(const DataBlockFixture &fillData, Data *data) {
ASSERT_EQ(fillData.size(), data->size());
std::memcpy(data->data(), fillData.data(), fillData.size());
}
void EXPECT_DATA_CORRECT(const DataBlockFixture &expectedData, const Data &data) {
ASSERT_EQ(expectedData.size(), data.size());
EXPECT_EQ(0, std::memcmp(expectedData.data(), data.data(), expectedData.size()));
}
};
class DataTestWithSizeParam: public DataTest, public WithParamInterface<size_t> {
public:
DataBlockFixture randomData;
Data randomData;
DataTestWithSizeParam(): randomData(GetParam()) {}
DataTestWithSizeParam(): randomData(DataFixture::generate(GetParam())) {}
void FillData(Data *data) {
DataTest::FillData(randomData, data);
}
void StoreData(const bf::path &filepath) {
static void StoreData(const Data &data, const bf::path &filepath) {
ofstream file(filepath.c_str(), std::ios::binary | std::ios::trunc);
file.write(randomData.data(), randomData.size());
file.write((char*)data.data(), data.size());
}
void EXPECT_STORED_FILE_DATA_CORRECT(const bf::path &filepath) {
EXPECT_EQ(randomData.size(), bf::file_size(filepath));
static void EXPECT_STORED_FILE_DATA_CORRECT(const Data &data, const bf::path &filepath) {
EXPECT_EQ(data.size(), bf::file_size(filepath));
ifstream file(filepath.c_str(), std::ios::binary);
char *read_data = new char[randomData.size()];
file.read(read_data, randomData.size());
char *read_data = new char[data.size()];
file.read(read_data, data.size());
EXPECT_EQ(0, std::memcmp(randomData.data(), read_data, randomData.size()));
EXPECT_EQ(0, std::memcmp(data.data(), read_data, data.size()));
delete[] read_data;
}
void EXPECT_DATA_CORRECT(const Data &data) {
DataTest::EXPECT_DATA_CORRECT(randomData, data);
}
};
INSTANTIATE_TEST_CASE_P(DataTestWithSizeParam, DataTestWithSizeParam, Values(0, 1, 2, 1024, 4096, 10*1024*1024));
TEST_P(DataTestWithSizeParam, ZeroInitializedDataIsDifferentToRandomData) {
if (GetParam() != 0) {
Data data(GetParam());
data.FillWithZeroes();
EXPECT_NE(randomData, data);
}
}
// Working on a large data area without a crash is a good indicator that we
// are actually working on memory that was validly allocated for us.
TEST_P(DataTestWithSizeParam, WriteAndCheck) {
Data data(GetParam());
FillData(&data);
EXPECT_DATA_CORRECT(data);
Data data = randomData.copy();
EXPECT_EQ(randomData, data);
}
TEST_P(DataTestWithSizeParam, Size) {
@ -89,52 +77,41 @@ TEST_P(DataTestWithSizeParam, Size) {
}
TEST_P(DataTestWithSizeParam, CheckStoredFile) {
Data data(GetParam());
FillData(&data);
TempFile file;
data.StoreToFile(file.path());
randomData.StoreToFile(file.path());
EXPECT_STORED_FILE_DATA_CORRECT(file.path());
EXPECT_STORED_FILE_DATA_CORRECT(randomData, file.path());
}
TEST_P(DataTestWithSizeParam, CheckLoadedData) {
TempFile file;
StoreData(file.path());
StoreData(randomData, file.path());
Data data = Data::LoadFromFile(file.path()).value();
EXPECT_DATA_CORRECT(data);
EXPECT_EQ(randomData, data);
}
TEST_P(DataTestWithSizeParam, StoreDoesntChangeData) {
Data data(GetParam());
FillData(&data);
Data data = randomData.copy();
TempFile file;
data.StoreToFile(file.path());
EXPECT_DATA_CORRECT(data);
EXPECT_EQ(randomData, data);
}
TEST_P(DataTestWithSizeParam, StoreAndLoad) {
Data data(GetParam());
FillData(&data);
TempFile file;
data.StoreToFile(file.path());
randomData.StoreToFile(file.path());
Data loaded_data = Data::LoadFromFile(file.path()).value();
EXPECT_DATA_CORRECT(loaded_data);
EXPECT_EQ(randomData, loaded_data);
}
TEST_P(DataTestWithSizeParam, Copy) {
Data data(GetParam());
FillData(&data);
Data copy = data.copy();
EXPECT_DATA_CORRECT(copy);
Data copy = randomData.copy();
EXPECT_EQ(randomData, copy);
}
TEST_F(DataTest, InitializeWithZeroes) {
@ -144,9 +121,7 @@ TEST_F(DataTest, InitializeWithZeroes) {
}
TEST_F(DataTest, FillModifiedDataWithZeroes) {
Data data(10*1024);
DataBlockFixture randomData(10*1024);
FillData(randomData, &data);
Data data = DataFixture::generate(10*1024);
EXPECT_FALSE(DataIsZeroes(data));
data.FillWithZeroes();

View File

@ -1,4 +1,4 @@
#include "../../data/DataBlockFixture.h"
#include "../../data/DataFixture.h"
#include "../../data/FixedSizeData.h"
#include "../../data/Data.h"
#include "google/gtest/gtest.h"
@ -19,18 +19,13 @@ public:
const string DATA1_AS_STRING = "1491BB4932A389EE14BC7090AC772972";
const string DATA2_AS_STRING = "272EE5517627CFA147A971A8E6E747E0";
const DataBlockFixture DATA3_AS_BINARY;
const DataBlockFixture DATA4_AS_BINARY;
const Data DATA3_AS_BINARY;
const Data DATA4_AS_BINARY;
FixedSizeDataTest() : DATA3_AS_BINARY(FixedSizeData<SIZE>::BINARY_LENGTH, 1), DATA4_AS_BINARY(FixedSizeData<SIZE>::BINARY_LENGTH, 2) {}
void EXPECT_DATA_EQ(const DataBlockFixture &expected, const Data &actual) {
EXPECT_EQ(expected.size(), actual.size());
EXPECT_EQ(0, std::memcmp(expected.data(), actual.data(), expected.size()));
}
FixedSizeDataTest() : DATA3_AS_BINARY(DataFixture::generate(SIZE, 1)), DATA4_AS_BINARY(DataFixture::generate(SIZE, 2)) {}
template<unsigned int SIZE>
void EXPECT_DATA_EQ(const DataBlockFixture &expected, const FixedSizeData<SIZE> &actual) {
void EXPECT_DATA_EQ(const Data &expected, const FixedSizeData<SIZE> &actual) {
EXPECT_EQ(expected.size(), SIZE);
EXPECT_EQ(0, std::memcmp(expected.data(), actual.data(), SIZE));
}
@ -93,13 +88,13 @@ TEST_P(FixedSizeDataTestWithStringParam, ToAndFromString) {
EXPECT_EQ(data, data2);
}
class FixedSizeDataTestWithBinaryParam: public FixedSizeDataTest, public WithParamInterface<const DataBlockFixture*> {
class FixedSizeDataTestWithBinaryParam: public FixedSizeDataTest, public WithParamInterface<const Data*> {
public:
static const DataBlockFixture VALUE1;
static const DataBlockFixture VALUE2;
static const Data VALUE1;
static const Data VALUE2;
};
const DataBlockFixture FixedSizeDataTestWithBinaryParam::VALUE1(FixedSizeData<SIZE>::BINARY_LENGTH, 3);
const DataBlockFixture FixedSizeDataTestWithBinaryParam::VALUE2(FixedSizeData<SIZE>::BINARY_LENGTH, 4);
const Data FixedSizeDataTestWithBinaryParam::VALUE1(DataFixture::generate(SIZE, 3));
const Data FixedSizeDataTestWithBinaryParam::VALUE2(DataFixture::generate(SIZE, 4));
INSTANTIATE_TEST_CASE_P(FixedSizeDataTestWithBinaryParam, FixedSizeDataTestWithBinaryParam, Values(&FixedSizeDataTestWithBinaryParam::VALUE1, &FixedSizeDataTestWithBinaryParam::VALUE2));
TEST_P(FixedSizeDataTestWithBinaryParam, FromBinary) {
@ -111,7 +106,7 @@ TEST_P(FixedSizeDataTestWithBinaryParam, FromAndToBinary) {
FixedSizeData<SIZE> data = FixedSizeData<SIZE>::FromBinary((uint8_t*)GetParam()->data());
Data output(FixedSizeData<SIZE>::BINARY_LENGTH);
data.ToBinary(output.data());
EXPECT_DATA_EQ(*GetParam(), output);
EXPECT_EQ(*GetParam(), output);
}
TEST_P(FixedSizeDataTestWithBinaryParam, ToAndFromBinary) {