Use Data, DataBlockFixture from cpputils

This commit is contained in:
Sebastian Messmer 2015-04-25 02:55:34 +02:00
parent 59865030de
commit 6a0623956d
15 changed files with 28 additions and 158 deletions

View File

@ -4,7 +4,7 @@
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using blockstore::Block; using blockstore::Block;
using blockstore::Data; using cpputils::Data;
using blockstore::Key; using blockstore::Key;
namespace blobstore { namespace blobstore {

View File

@ -4,7 +4,7 @@
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using blockstore::Block; using blockstore::Block;
using blockstore::Data; using cpputils::Data;
using blockstore::Key; using blockstore::Key;
namespace blobstore { namespace blobstore {

View File

@ -3,7 +3,7 @@
#define BLOBSTORE_IMPLEMENTATIONS_ONBLOCKS_DATANODESTORE_DATANODE_H_ #define BLOBSTORE_IMPLEMENTATIONS_ONBLOCKS_DATANODESTORE_DATANODE_H_
#include "DataNodeView.h" #include "DataNodeView.h"
#include "messmer/blockstore/utils/Data.h" #include <messmer/cpp-utils/data/Data.h>
namespace blobstore { namespace blobstore {
namespace onblocks { namespace onblocks {

View File

@ -9,7 +9,7 @@
using blockstore::BlockStore; using blockstore::BlockStore;
using blockstore::Block; using blockstore::Block;
using blockstore::Key; using blockstore::Key;
using blockstore::Data; using cpputils::Data;
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using std::runtime_error; using std::runtime_error;

View File

@ -1,6 +1,6 @@
#include "testutils/BlobStoreTest.h" #include "testutils/BlobStoreTest.h"
#include <messmer/blockstore/utils/Data.h> #include <messmer/cpp-utils/data/Data.h>
#include "../../testutils/DataBlockFixture.h" #include <messmer/cpp-utils/data/DataBlockFixture.h>
#include "../../../implementations/onblocks/datanodestore/DataNodeView.h" #include "../../../implementations/onblocks/datanodestore/DataNodeView.h"
using std::unique_ptr; using std::unique_ptr;
@ -10,7 +10,8 @@ using ::testing::Values;
using namespace blobstore; using namespace blobstore;
using blobstore::onblocks::datanodestore::DataNodeLayout; using blobstore::onblocks::datanodestore::DataNodeLayout;
using blockstore::Key; using blockstore::Key;
using blockstore::Data; using cpputils::Data;
using cpputils::DataBlockFixture;
class BlobReadWriteTest: public BlobStoreTest { class BlobReadWriteTest: public BlobStoreTest {
public: public:

View File

@ -1,12 +1,13 @@
#include "testutils/BlobStoreTest.h" #include "testutils/BlobStoreTest.h"
#include <messmer/blockstore/utils/Data.h> #include <messmer/cpp-utils/data/Data.h>
#include "../../testutils/DataBlockFixture.h" #include <messmer/cpp-utils/data/DataBlockFixture.h>
using std::unique_ptr; using std::unique_ptr;
using namespace blobstore; using namespace blobstore;
using blockstore::Key; using blockstore::Key;
using blockstore::Data; using cpputils::Data;
using cpputils::DataBlockFixture;
class BlobSizeTest: public BlobStoreTest { class BlobSizeTest: public BlobStoreTest {
public: public:

View File

@ -17,7 +17,7 @@ using cpputils::dynamic_pointer_move;
using blockstore::Key; using blockstore::Key;
using blockstore::testfake::FakeBlockStore; using blockstore::testfake::FakeBlockStore;
using blockstore::BlockStore; using blockstore::BlockStore;
using blockstore::Data; using cpputils::Data;
using namespace blobstore; using namespace blobstore;
using namespace blobstore::onblocks; using namespace blobstore::onblocks;
using namespace blobstore::onblocks::datanodestore; using namespace blobstore::onblocks::datanodestore;

View File

@ -8,7 +8,7 @@
#include "messmer/blockstore/implementations/testfake/FakeBlockStore.h" #include "messmer/blockstore/implementations/testfake/FakeBlockStore.h"
#include "messmer/blockstore/implementations/testfake/FakeBlock.h" #include "messmer/blockstore/implementations/testfake/FakeBlock.h"
#include "../../../testutils/DataBlockFixture.h" #include <messmer/cpp-utils/data/DataBlockFixture.h>
using ::testing::Test; using ::testing::Test;
using ::testing::WithParamInterface; using ::testing::WithParamInterface;
@ -17,13 +17,14 @@ using ::testing::Combine;
using std::unique_ptr; using std::unique_ptr;
using std::make_unique; using std::make_unique;
using std::string; using std::string;
using cpputils::DataBlockFixture;
//TODO Split into multiple files //TODO Split into multiple files
using cpputils::dynamic_pointer_move; using cpputils::dynamic_pointer_move;
using blockstore::BlockStore; using blockstore::BlockStore;
using blockstore::Data; using cpputils::Data;
using blockstore::Key; using blockstore::Key;
using blockstore::testfake::FakeBlockStore; using blockstore::testfake::FakeBlockStore;
using namespace blobstore; using namespace blobstore;

View File

@ -16,7 +16,7 @@ using std::string;
using blockstore::BlockStore; using blockstore::BlockStore;
using blockstore::testfake::FakeBlockStore; using blockstore::testfake::FakeBlockStore;
using blockstore::Key; using blockstore::Key;
using blockstore::Data; using cpputils::Data;
using namespace blobstore; using namespace blobstore;
using namespace blobstore::onblocks; using namespace blobstore::onblocks;
using namespace blobstore::onblocks::datanodestore; using namespace blobstore::onblocks::datanodestore;

View File

@ -4,7 +4,7 @@
#include "messmer/blockstore/implementations/testfake/FakeBlockStore.h" #include "messmer/blockstore/implementations/testfake/FakeBlockStore.h"
#include "messmer/blockstore/implementations/testfake/FakeBlock.h" #include "messmer/blockstore/implementations/testfake/FakeBlock.h"
#include "../../../../implementations/onblocks/BlobStoreOnBlocks.h" #include "../../../../implementations/onblocks/BlobStoreOnBlocks.h"
#include "../../../testutils/DataBlockFixture.h" #include <messmer/cpp-utils/data/DataBlockFixture.h>
using ::testing::Test; using ::testing::Test;
using ::testing::WithParamInterface; using ::testing::WithParamInterface;
@ -15,7 +15,8 @@ using std::string;
using blockstore::BlockStore; using blockstore::BlockStore;
using blockstore::testfake::FakeBlockStore; using blockstore::testfake::FakeBlockStore;
using blockstore::Data; using cpputils::Data;
using cpputils::DataBlockFixture;
using namespace blobstore; using namespace blobstore;
using namespace blobstore::onblocks; using namespace blobstore::onblocks;
using namespace blobstore::onblocks::datanodestore; using namespace blobstore::onblocks::datanodestore;

View File

@ -1,7 +1,7 @@
#include "testutils/DataTreeTest.h" #include "testutils/DataTreeTest.h"
#include "testutils/TwoLevelDataFixture.h" #include "testutils/TwoLevelDataFixture.h"
#include "../../../../implementations/onblocks/utils/Math.h" #include "../../../../implementations/onblocks/utils/Math.h"
#include <messmer/blockstore/utils/Data.h> #include <messmer/cpp-utils/data/Data.h>
#include <tuple> #include <tuple>
@ -21,7 +21,7 @@ using blobstore::onblocks::datanodestore::DataNodeLayout;
using blobstore::onblocks::datatreestore::DataTree; using blobstore::onblocks::datatreestore::DataTree;
using blobstore::onblocks::utils::ceilDivision; using blobstore::onblocks::utils::ceilDivision;
using blockstore::Key; using blockstore::Key;
using blockstore::Data; using cpputils::Data;
using std::unique_ptr; using std::unique_ptr;

View File

@ -1,7 +1,7 @@
#include "testutils/DataTreeTest.h" #include "testutils/DataTreeTest.h"
#include "testutils/TwoLevelDataFixture.h" #include "testutils/TwoLevelDataFixture.h"
#include "../../../../implementations/onblocks/utils/Math.h" #include "../../../../implementations/onblocks/utils/Math.h"
#include <messmer/blockstore/utils/Data.h> #include <messmer/cpp-utils/data/Data.h>
#include <tuple> #include <tuple>
@ -21,7 +21,7 @@ using blobstore::onblocks::datanodestore::DataNodeLayout;
using blobstore::onblocks::datatreestore::DataTree; using blobstore::onblocks::datatreestore::DataTree;
using blobstore::onblocks::utils::ceilDivision; using blobstore::onblocks::utils::ceilDivision;
using blockstore::Key; using blockstore::Key;
using blockstore::Data; using cpputils::Data;
using std::unique_ptr; using std::unique_ptr;

View File

@ -4,7 +4,7 @@
#include <google/gtest/gtest.h> #include <google/gtest/gtest.h>
#include "../../../../testutils/DataBlockFixture.h" #include <messmer/cpp-utils/data/DataBlockFixture.h>
// A data fixture containing data for a leaf. // A data fixture containing data for a leaf.
// The class can fill this data into a given leaf // The class can fill this data into a given leaf
@ -29,12 +29,12 @@ public:
} }
private: private:
static blockstore::Data loadData(const blobstore::onblocks::datanodestore::DataLeafNode &leaf) { static cpputils::Data loadData(const blobstore::onblocks::datanodestore::DataLeafNode &leaf) {
blockstore::Data data(leaf.numBytes()); cpputils::Data data(leaf.numBytes());
leaf.read(data.data(), 0, leaf.numBytes()); leaf.read(data.data(), 0, leaf.numBytes());
return data; return data;
} }
DataBlockFixture _data; cpputils::DataBlockFixture _data;
}; };
#endif #endif

View File

@ -1,87 +0,0 @@
#include "DataBlockFixture.h"
#include <algorithm>
#include <cstring>
using std::min;
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,47 +0,0 @@
#pragma once
#ifndef TEST_TESTUTILS_DATABLOCKFIXTURE_H_
#define TEST_TESTUTILS_DATABLOCKFIXTURE_H_
#include <cstdio>
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