From 61337eef85f43206025b5f3d611d71204840cf2b Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sat, 25 Apr 2015 17:45:18 +0200 Subject: [PATCH] Removed DataBlockFixture --- data/DataBlockFixture.cpp | 91 --------------------------------- data/DataBlockFixture.h | 54 ------------------- test/data/DataTest.cpp | 87 +++++++++++-------------------- test/data/FixedSizeDataTest.cpp | 27 ++++------ 4 files changed, 42 insertions(+), 217 deletions(-) delete mode 100644 data/DataBlockFixture.cpp delete mode 100644 data/DataBlockFixture.h diff --git a/data/DataBlockFixture.cpp b/data/DataBlockFixture.cpp deleted file mode 100644 index 096481e0..00000000 --- a/data/DataBlockFixture.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "DataBlockFixture.h" -#include -#include - -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(_fileData)[i] = val; - } - uint64_t alreadyWritten = (_size/sizeof(long long int))*sizeof(long long int); - val *= 6364136223846793005L; - val += 1442695040888963407; - char *remainingBytes = reinterpret_cast(&val); - //Fill remaining bytes - for(size_t i=0; i<_size-alreadyWritten; ++i) { - reinterpret_cast(_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); -} - -} diff --git a/data/DataBlockFixture.h b/data/DataBlockFixture.h deleted file mode 100644 index 1a67a74b..00000000 --- a/data/DataBlockFixture.h +++ /dev/null @@ -1,54 +0,0 @@ -#pragma once -#ifndef MESSMER_CPPUTILS_DATA_DATABLOCKFIXTURE_H_ -#define MESSMER_CPPUTILS_DATA_DATABLOCKFIXTURE_H_ - -#include - -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 diff --git a/test/data/DataTest.cpp b/test/data/DataTest.cpp index 74d01fe6..a3b8819c 100644 --- a/test/data/DataTest.cpp +++ b/test/data/DataTest.cpp @@ -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 { 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(); diff --git a/test/data/FixedSizeDataTest.cpp b/test/data/FixedSizeDataTest.cpp index 69380911..065b7425 100644 --- a/test/data/FixedSizeDataTest.cpp +++ b/test/data/FixedSizeDataTest.cpp @@ -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::BINARY_LENGTH, 1), DATA4_AS_BINARY(FixedSizeData::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 - void EXPECT_DATA_EQ(const DataBlockFixture &expected, const FixedSizeData &actual) { + void EXPECT_DATA_EQ(const Data &expected, const FixedSizeData &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 { +class FixedSizeDataTestWithBinaryParam: public FixedSizeDataTest, public WithParamInterface { public: - static const DataBlockFixture VALUE1; - static const DataBlockFixture VALUE2; + static const Data VALUE1; + static const Data VALUE2; }; -const DataBlockFixture FixedSizeDataTestWithBinaryParam::VALUE1(FixedSizeData::BINARY_LENGTH, 3); -const DataBlockFixture FixedSizeDataTestWithBinaryParam::VALUE2(FixedSizeData::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 data = FixedSizeData::FromBinary((uint8_t*)GetParam()->data()); Data output(FixedSizeData::BINARY_LENGTH); data.ToBinary(output.data()); - EXPECT_DATA_EQ(*GetParam(), output); + EXPECT_EQ(*GetParam(), output); } TEST_P(FixedSizeDataTestWithBinaryParam, ToAndFromBinary) {