2016-02-11 15:19:58 +01:00
|
|
|
#include "blockstore/implementations/testfake/FakeBlockStore.h"
|
|
|
|
#include <cpp-utils/data/DataFixture.h>
|
|
|
|
#include "blockstore/utils/BlockStoreUtils.h"
|
|
|
|
#include <gtest/gtest.h>
|
2015-01-24 01:24:37 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
using ::testing::Test;
|
|
|
|
using ::testing::WithParamInterface;
|
|
|
|
using ::testing::Values;
|
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
using cpputils::Data;
|
2015-04-25 16:43:52 +02:00
|
|
|
using cpputils::DataFixture;
|
2015-07-21 18:19:34 +02:00
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
2015-01-24 01:24:37 +01:00
|
|
|
|
|
|
|
using namespace blockstore;
|
|
|
|
using namespace blockstore::utils;
|
|
|
|
|
|
|
|
using blockstore::testfake::FakeBlockStore;
|
|
|
|
|
|
|
|
class BlockStoreUtilsTest: public Test {
|
|
|
|
public:
|
|
|
|
unsigned int SIZE = 1024 * 1024;
|
|
|
|
BlockStoreUtilsTest():
|
|
|
|
ZEROES(SIZE),
|
2015-04-25 16:43:52 +02:00
|
|
|
dataFixture(DataFixture::generate(SIZE)),
|
2015-07-21 18:19:34 +02:00
|
|
|
blockStore(make_unique_ref<FakeBlockStore>()) {
|
2015-01-24 01:24:37 +01:00
|
|
|
ZEROES.FillWithZeroes();
|
|
|
|
}
|
|
|
|
|
|
|
|
Data ZEROES;
|
2015-04-25 16:43:52 +02:00
|
|
|
Data dataFixture;
|
2015-07-21 18:19:34 +02:00
|
|
|
unique_ref<BlockStore> blockStore;
|
2015-01-24 01:24:37 +01:00
|
|
|
};
|
|
|
|
|
2015-03-08 00:36:59 +01:00
|
|
|
TEST_F(BlockStoreUtilsTest, FillWithZeroes) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = blockStore->create(Data(SIZE));
|
2015-03-08 00:36:59 +01:00
|
|
|
block->write(dataFixture.data(), 0, SIZE);
|
|
|
|
EXPECT_NE(0, std::memcmp(ZEROES.data(), block->data(), SIZE));
|
|
|
|
fillWithZeroes(block.get());
|
|
|
|
EXPECT_EQ(0, std::memcmp(ZEROES.data(), block->data(), SIZE));
|
|
|
|
}
|
|
|
|
|
2015-02-22 19:29:30 +01:00
|
|
|
class BlockStoreUtilsTest_CopyToNewBlock: public BlockStoreUtilsTest {};
|
|
|
|
|
|
|
|
TEST_F(BlockStoreUtilsTest_CopyToNewBlock, CopyEmptyBlock) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = blockStore->create(Data(0));
|
2015-01-24 22:27:14 +01:00
|
|
|
auto block2 = copyToNewBlock(blockStore.get(), *block);
|
2015-01-24 01:24:37 +01:00
|
|
|
|
2015-01-24 22:27:14 +01:00
|
|
|
EXPECT_EQ(0u, block2->size());
|
2015-01-24 01:24:37 +01:00
|
|
|
}
|
|
|
|
|
2015-02-22 19:29:30 +01:00
|
|
|
TEST_F(BlockStoreUtilsTest_CopyToNewBlock, CopyZeroBlock) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = blockStore->create(ZEROES);
|
2015-01-24 22:27:14 +01:00
|
|
|
auto block2 = copyToNewBlock(blockStore.get(), *block);
|
2015-01-24 01:24:37 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(SIZE, block2->size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(ZEROES.data(), block2->data(), SIZE));
|
|
|
|
}
|
|
|
|
|
2015-02-22 19:29:30 +01:00
|
|
|
TEST_F(BlockStoreUtilsTest_CopyToNewBlock, CopyDataBlock) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = blockStore->create(Data(SIZE));
|
2015-03-04 20:47:02 +01:00
|
|
|
block->write(dataFixture.data(), 0, SIZE);
|
2015-01-24 22:27:14 +01:00
|
|
|
auto block2 = copyToNewBlock(blockStore.get(), *block);
|
2015-01-24 01:24:37 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(SIZE, block2->size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(dataFixture.data(), block2->data(), SIZE));
|
|
|
|
}
|
|
|
|
|
2015-02-22 19:29:30 +01:00
|
|
|
TEST_F(BlockStoreUtilsTest_CopyToNewBlock, OriginalBlockUnchanged) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = blockStore->create(Data(SIZE));
|
2015-03-04 20:47:02 +01:00
|
|
|
block->write(dataFixture.data(), 0, SIZE);
|
2015-01-24 22:27:14 +01:00
|
|
|
auto block2 = copyToNewBlock(blockStore.get(), *block);
|
2015-01-24 01:24:37 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(SIZE, block->size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(dataFixture.data(), block->data(), SIZE));
|
|
|
|
}
|
2015-02-22 19:29:30 +01:00
|
|
|
|
|
|
|
class BlockStoreUtilsTest_CopyToExistingBlock: public BlockStoreUtilsTest {};
|
|
|
|
|
|
|
|
TEST_F(BlockStoreUtilsTest_CopyToExistingBlock, CopyEmptyBlock) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = blockStore->create(Data(0));
|
|
|
|
auto block2 = blockStore->create(Data(0));
|
2015-02-22 19:29:30 +01:00
|
|
|
copyTo(block2.get(), *block);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlockStoreUtilsTest_CopyToExistingBlock, CopyZeroBlock) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = blockStore->create(ZEROES);
|
|
|
|
auto block2 = blockStore->create(Data(SIZE));
|
2015-03-04 20:47:02 +01:00
|
|
|
block2->write(dataFixture.data(), 0, SIZE);
|
2015-02-22 19:29:30 +01:00
|
|
|
copyTo(block2.get(), *block);
|
|
|
|
|
|
|
|
EXPECT_EQ(0, std::memcmp(ZEROES.data(), block2->data(), SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlockStoreUtilsTest_CopyToExistingBlock, CopyDataBlock) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = blockStore->create(Data(SIZE));
|
2015-03-04 20:47:02 +01:00
|
|
|
block->write(dataFixture.data(), 0, SIZE);
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block2 = blockStore->create(Data(SIZE));
|
2015-02-22 19:29:30 +01:00
|
|
|
copyTo(block2.get(), *block);
|
|
|
|
|
|
|
|
EXPECT_EQ(0, std::memcmp(dataFixture.data(), block2->data(), SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BlockStoreUtilsTest_CopyToExistingBlock, OriginalBlockUnchanged) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = blockStore->create(Data(SIZE));
|
2015-03-04 20:47:02 +01:00
|
|
|
block->write(dataFixture.data(), 0, SIZE);
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block2 = blockStore->create(Data(SIZE));
|
2015-02-22 19:29:30 +01:00
|
|
|
copyTo(block2.get(), *block);
|
|
|
|
|
|
|
|
EXPECT_EQ(0, std::memcmp(dataFixture.data(), block->data(), SIZE));
|
|
|
|
}
|