Refactoring test cases

This commit is contained in:
Sebastian Messmer 2015-02-23 17:03:45 +01:00
parent c81e058717
commit fee8cd42eb
8 changed files with 51 additions and 40 deletions

View File

@ -38,7 +38,7 @@ public:
} }
unique_ptr<DataTree> CreateFullTwoLevelTreeWithData(TwoLevelDataFixture *data) { unique_ptr<DataTree> CreateFullTwoLevelTreeWithData(TwoLevelDataFixture *data) {
auto root = LoadInnerNode(CreateFullTwoLevelTree()); auto root = CreateFullTwoLevel();
assert(root->numChildren() == DataInnerNode::MAX_STORED_CHILDREN); assert(root->numChildren() == DataInnerNode::MAX_STORED_CHILDREN);
data->FillInto(root.get()); data->FillInto(root.get());
return make_unique<DataTree>(&nodeStore, std::move(root)); return make_unique<DataTree>(&nodeStore, std::move(root));

View File

@ -37,7 +37,7 @@ TEST_F(DataTreeGrowingTest_DepthFlags, GrowAThreeNodeChainedTree) {
} }
TEST_F(DataTreeGrowingTest_DepthFlags, GrowAFullTwoLevelTree) { TEST_F(DataTreeGrowingTest_DepthFlags, GrowAFullTwoLevelTree) {
auto key = CreateFullTwoLevelTree(); auto key = CreateFullTwoLevel()->key();
AddLeafTo(key); AddLeafTo(key);
CHECK_DEPTH(2, key); CHECK_DEPTH(2, key);
} }
@ -49,7 +49,7 @@ TEST_F(DataTreeGrowingTest_DepthFlags, GrowAThreeLevelTreeWithLowerLevelFull) {
} }
TEST_F(DataTreeGrowingTest_DepthFlags, GrowAFullThreeLevelTree) { TEST_F(DataTreeGrowingTest_DepthFlags, GrowAFullThreeLevelTree) {
auto key = CreateFullThreeLevelTree(); auto key = CreateFullThreeLevel()->key();
AddLeafTo(key); AddLeafTo(key);
CHECK_DEPTH(3, key); CHECK_DEPTH(3, key);
} }

View File

@ -37,12 +37,12 @@ TEST_F(DataTreeGrowingTest_KeyDoesntChange, GrowAThreeLevelTreeWithLowerLevelFul
} }
TEST_F(DataTreeGrowingTest_KeyDoesntChange, GrowAFullTwoLevelTree) { TEST_F(DataTreeGrowingTest_KeyDoesntChange, GrowAFullTwoLevelTree) {
auto root_key = CreateFullTwoLevelTree(); auto root_key = CreateFullTwoLevel()->key();
EXPECT_KEY_DOESNT_CHANGE_WHEN_GROWING(root_key); EXPECT_KEY_DOESNT_CHANGE_WHEN_GROWING(root_key);
} }
TEST_F(DataTreeGrowingTest_KeyDoesntChange, GrowAFullThreeLevelTree) { TEST_F(DataTreeGrowingTest_KeyDoesntChange, GrowAFullThreeLevelTree) {
auto root_key = CreateFullThreeLevelTree(); auto root_key = CreateFullThreeLevel()->key();
EXPECT_KEY_DOESNT_CHANGE_WHEN_GROWING(root_key); EXPECT_KEY_DOESNT_CHANGE_WHEN_GROWING(root_key);
} }

View File

@ -39,7 +39,7 @@ TEST_F(DataTreeGrowingTest_Structure, GrowAFullTwoLevelTreeFromGroundUp) {
} }
TEST_F(DataTreeGrowingTest_Structure, GrowAFullTwoLevelTree) { TEST_F(DataTreeGrowingTest_Structure, GrowAFullTwoLevelTree) {
auto root_key = CreateFullTwoLevelTree(); auto root_key = CreateFullTwoLevel()->key();
AddLeafTo(root_key); AddLeafTo(root_key);
auto root = LoadInnerNode(root_key); auto root = LoadInnerNode(root_key);
@ -69,7 +69,7 @@ TEST_F(DataTreeGrowingTest_Structure, GrowAFullThreeLevelTreeFromGroundUp) {
} }
TEST_F(DataTreeGrowingTest_Structure, GrowAFullThreeLevelTree) { TEST_F(DataTreeGrowingTest_Structure, GrowAFullThreeLevelTree) {
auto root_key = CreateFullThreeLevelTree(); auto root_key = CreateFullThreeLevel()->key();
AddLeafTo(root_key); AddLeafTo(root_key);
auto root = LoadInnerNode(root_key); auto root = LoadInnerNode(root_key);

View File

@ -96,13 +96,13 @@ TEST_F(GetLowestInnerRightBorderNodeWithLessThanKChildrenOrNullTest, LargerTree)
} }
TEST_F(GetLowestInnerRightBorderNodeWithLessThanKChildrenOrNullTest, FullTwoLevelTree) { TEST_F(GetLowestInnerRightBorderNodeWithLessThanKChildrenOrNullTest, FullTwoLevelTree) {
auto root = nodeStore.load(CreateFullTwoLevelTree()); auto root = CreateFullTwoLevel();
auto result = GetLowestInnerRightBorderNodeWithLessThanKChildrenOrNull(&nodeStore, root.get()); auto result = GetLowestInnerRightBorderNodeWithLessThanKChildrenOrNull(&nodeStore, root.get());
EXPECT_EQ(nullptr, result.get()); EXPECT_EQ(nullptr, result.get());
} }
TEST_F(GetLowestInnerRightBorderNodeWithLessThanKChildrenOrNullTest, FullThreeLevelTree) { TEST_F(GetLowestInnerRightBorderNodeWithLessThanKChildrenOrNullTest, FullThreeLevelTree) {
auto root = nodeStore.load(CreateFullThreeLevelTree()); auto root = CreateFullThreeLevel();
auto result = GetLowestInnerRightBorderNodeWithLessThanKChildrenOrNull(&nodeStore, root.get()); auto result = GetLowestInnerRightBorderNodeWithLessThanKChildrenOrNull(&nodeStore, root.get());
EXPECT_EQ(nullptr, result.get()); EXPECT_EQ(nullptr, result.get());
} }

View File

@ -44,7 +44,7 @@ Key DataTreeShrinkingTest::CreateTwoInnerNodeTwoOneLeavesTree() {
} }
Key DataTreeShrinkingTest::CreateThreeLevelMinDataTree() { Key DataTreeShrinkingTest::CreateThreeLevelMinDataTree() {
auto fullTwoLevelRoot = nodeStore.load(CreateFullTwoLevelTree()); auto fullTwoLevelRoot = CreateFullTwoLevel();
auto root = nodeStore.createNewInnerNode(*fullTwoLevelRoot); auto root = nodeStore.createNewInnerNode(*fullTwoLevelRoot);
auto leaf = nodeStore.createNewLeafNode(); auto leaf = nodeStore.createNewLeafNode();
auto inner = nodeStore.createNewInnerNode(*leaf); auto inner = nodeStore.createNewInnerNode(*leaf);
@ -53,7 +53,7 @@ Key DataTreeShrinkingTest::CreateThreeLevelMinDataTree() {
} }
Key DataTreeShrinkingTest::CreateFourLevelMinDataTree() { Key DataTreeShrinkingTest::CreateFourLevelMinDataTree() {
auto fullThreeLevelRoot = nodeStore.load(CreateFullThreeLevelTree()); auto fullThreeLevelRoot = CreateFullThreeLevel();
auto root = nodeStore.createNewInnerNode(*fullThreeLevelRoot); auto root = nodeStore.createNewInnerNode(*fullThreeLevelRoot);
auto leaf = nodeStore.createNewLeafNode(); auto leaf = nodeStore.createNewLeafNode();
auto inner = nodeStore.createNewInnerNode(*leaf); auto inner = nodeStore.createNewInnerNode(*leaf);
@ -63,7 +63,7 @@ Key DataTreeShrinkingTest::CreateFourLevelMinDataTree() {
} }
Key DataTreeShrinkingTest::CreateFourLevelTreeWithTwoSiblingLeaves1() { Key DataTreeShrinkingTest::CreateFourLevelTreeWithTwoSiblingLeaves1() {
auto fullThreeLevelRoot = nodeStore.load(CreateFullThreeLevelTree()); auto fullThreeLevelRoot = CreateFullThreeLevel();
auto root = nodeStore.createNewInnerNode(*fullThreeLevelRoot); auto root = nodeStore.createNewInnerNode(*fullThreeLevelRoot);
auto leaf = nodeStore.createNewLeafNode(); auto leaf = nodeStore.createNewLeafNode();
auto inner = nodeStore.createNewInnerNode(*leaf); auto inner = nodeStore.createNewInnerNode(*leaf);
@ -74,7 +74,7 @@ Key DataTreeShrinkingTest::CreateFourLevelTreeWithTwoSiblingLeaves1() {
} }
Key DataTreeShrinkingTest::CreateFourLevelTreeWithTwoSiblingLeaves2() { Key DataTreeShrinkingTest::CreateFourLevelTreeWithTwoSiblingLeaves2() {
auto fullThreeLevelRoot = nodeStore.load(CreateFullThreeLevelTree()); auto fullThreeLevelRoot = CreateFullThreeLevel();
auto root = nodeStore.createNewInnerNode(*fullThreeLevelRoot); auto root = nodeStore.createNewInnerNode(*fullThreeLevelRoot);
auto leaf1 = nodeStore.createNewLeafNode(); auto leaf1 = nodeStore.createNewLeafNode();
auto inner1 = nodeStore.createNewInnerNode(*leaf1); auto inner1 = nodeStore.createNewInnerNode(*leaf1);
@ -89,7 +89,7 @@ Key DataTreeShrinkingTest::CreateFourLevelTreeWithTwoSiblingLeaves2() {
} }
Key DataTreeShrinkingTest::CreateTreeWithFirstChildOfRootFullThreelevelAndSecondChildMindataThreelevel() { Key DataTreeShrinkingTest::CreateTreeWithFirstChildOfRootFullThreelevelAndSecondChildMindataThreelevel() {
auto fullThreeLevelRoot = nodeStore.load(CreateFullThreeLevelTree()); auto fullThreeLevelRoot = CreateFullThreeLevel();
auto root = nodeStore.createNewInnerNode(*fullThreeLevelRoot); auto root = nodeStore.createNewInnerNode(*fullThreeLevelRoot);
auto leaf1 = nodeStore.createNewLeafNode(); auto leaf1 = nodeStore.createNewLeafNode();
auto inner1 = nodeStore.createNewInnerNode(*leaf1); auto inner1 = nodeStore.createNewInnerNode(*leaf1);
@ -103,8 +103,8 @@ Key DataTreeShrinkingTest::CreateTreeWithFirstChildOfRootFullThreelevelAndSecond
} }
Key DataTreeShrinkingTest::CreateThreeLevelTreeWithThreeChildrenOfRoot() { Key DataTreeShrinkingTest::CreateThreeLevelTreeWithThreeChildrenOfRoot() {
auto fullTwoLevelTree1 = nodeStore.load(CreateFullTwoLevelTree()); auto fullTwoLevelTree1 = CreateFullTwoLevel();
auto fullTwoLevelTree2 = nodeStore.load(CreateFullTwoLevelTree()); auto fullTwoLevelTree2 = CreateFullTwoLevel();
auto twonodechain = nodeStore.createNewInnerNode(*nodeStore.createNewLeafNode()); auto twonodechain = nodeStore.createNewInnerNode(*nodeStore.createNewLeafNode());
auto root = nodeStore.createNewInnerNode(*fullTwoLevelTree1); auto root = nodeStore.createNewInnerNode(*fullTwoLevelTree1);
root->addChild(*fullTwoLevelTree2); root->addChild(*fullTwoLevelTree2);

View File

@ -4,6 +4,7 @@
#include <messmer/cpp-utils/pointer.h> #include <messmer/cpp-utils/pointer.h>
using blobstore::onblocks::datanodestore::DataNodeStore; using blobstore::onblocks::datanodestore::DataNodeStore;
using blobstore::onblocks::datanodestore::DataNode;
using blobstore::onblocks::datanodestore::DataInnerNode; using blobstore::onblocks::datanodestore::DataInnerNode;
using blobstore::onblocks::datanodestore::DataLeafNode; using blobstore::onblocks::datanodestore::DataLeafNode;
using blobstore::onblocks::datatreestore::DataTree; using blobstore::onblocks::datatreestore::DataTree;
@ -11,47 +12,52 @@ using blockstore::testfake::FakeBlockStore;
using blockstore::Key; using blockstore::Key;
using std::make_unique; using std::make_unique;
using std::unique_ptr; using std::unique_ptr;
using std::initializer_list;
using cpputils::dynamic_pointer_move; using cpputils::dynamic_pointer_move;
DataTreeTest::DataTreeTest() DataTreeTest::DataTreeTest()
:nodeStore(make_unique<FakeBlockStore>()) { :nodeStore(make_unique<FakeBlockStore>()) {
} }
unique_ptr<DataLeafNode> DataTreeTest::CreateLeaf() {
return nodeStore.createNewLeafNode();
}
unique_ptr<DataInnerNode> DataTreeTest::CreateInner(initializer_list<const DataNode*> children) {
assert(children.size() >= 1);
auto node = nodeStore.createNewInnerNode(**children.begin());
for(auto child = children.begin()+1; child != children.end(); ++child) {
node->addChild(**child);
}
return node;
}
unique_ptr<DataTree> DataTreeTest::CreateLeafOnlyTree() { unique_ptr<DataTree> DataTreeTest::CreateLeafOnlyTree() {
auto leafnode = nodeStore.createNewLeafNode(); return make_unique<DataTree>(&nodeStore, CreateLeaf());
return make_unique<DataTree>(&nodeStore, std::move(leafnode));
} }
void DataTreeTest::FillNode(DataInnerNode *node) { void DataTreeTest::FillNode(DataInnerNode *node) {
for(unsigned int i=node->numChildren(); i < DataInnerNode::MAX_STORED_CHILDREN; ++i) { for(unsigned int i=node->numChildren(); i < DataInnerNode::MAX_STORED_CHILDREN; ++i) {
node->addChild(*nodeStore.createNewLeafNode()); node->addChild(*CreateLeaf());
} }
} }
void DataTreeTest::FillNodeTwoLevel(DataInnerNode *node) { void DataTreeTest::FillNodeTwoLevel(DataInnerNode *node) {
for(unsigned int i=node->numChildren(); i < DataInnerNode::MAX_STORED_CHILDREN; ++i) { for(unsigned int i=node->numChildren(); i < DataInnerNode::MAX_STORED_CHILDREN; ++i) {
auto inner_node = nodeStore.createNewInnerNode(*nodeStore.createNewLeafNode()); node->addChild(*CreateFullTwoLevel());
for(unsigned int j = 1;j < DataInnerNode::MAX_STORED_CHILDREN; ++j) {
inner_node->addChild(*nodeStore.createNewLeafNode());
}
node->addChild(*inner_node);
} }
} }
Key DataTreeTest::CreateFullTwoLevelTree() { unique_ptr<DataInnerNode> DataTreeTest::CreateFullTwoLevel() {
auto leaf = nodeStore.createNewLeafNode(); auto root = CreateInner({CreateLeaf().get()});
auto root = nodeStore.createNewInnerNode(*leaf);
FillNode(root.get()); FillNode(root.get());
return root->key(); return root;
} }
Key DataTreeTest::CreateFullThreeLevelTree() { unique_ptr<DataInnerNode> DataTreeTest::CreateFullThreeLevel() {
auto leaf = nodeStore.createNewLeafNode(); auto root = CreateInner({CreateFullTwoLevel().get()});
auto node = nodeStore.createNewInnerNode(*leaf);
auto root = nodeStore.createNewInnerNode(*node);
FillNode(node.get());
FillNodeTwoLevel(root.get()); FillNodeTwoLevel(root.get());
return root->key(); return root;
} }
unique_ptr<DataInnerNode> DataTreeTest::LoadInnerNode(const Key &key) { unique_ptr<DataInnerNode> DataTreeTest::LoadInnerNode(const Key &key) {
@ -68,11 +74,12 @@ unique_ptr<DataLeafNode> DataTreeTest::LoadLeafNode(const Key &key) {
return casted; return casted;
} }
unique_ptr<DataInnerNode> DataTreeTest::CreateTwoLeaf() {
return CreateInner({CreateLeaf().get(), CreateLeaf().get()});
}
unique_ptr<DataTree> DataTreeTest::CreateTwoLeafTree() { unique_ptr<DataTree> DataTreeTest::CreateTwoLeafTree() {
auto leaf1 = nodeStore.createNewLeafNode(); return make_unique<DataTree>(&nodeStore, CreateTwoLeaf());
auto root = nodeStore.createNewInnerNode(*leaf1);
root->addChild(*nodeStore.createNewLeafNode());
return make_unique<DataTree>(&nodeStore, std::move(root));
} }
void DataTreeTest::EXPECT_IS_LEAF_NODE(const Key &key) { void DataTreeTest::EXPECT_IS_LEAF_NODE(const Key &key) {

View File

@ -13,12 +13,16 @@ class DataTreeTest: public ::testing::Test {
public: public:
DataTreeTest(); DataTreeTest();
std::unique_ptr<blobstore::onblocks::datanodestore::DataLeafNode> CreateLeaf();
std::unique_ptr<blobstore::onblocks::datanodestore::DataInnerNode> CreateInner(std::initializer_list<const blobstore::onblocks::datanodestore::DataNode *> children);
std::unique_ptr<blobstore::onblocks::datatreestore::DataTree> CreateLeafOnlyTree(); std::unique_ptr<blobstore::onblocks::datatreestore::DataTree> CreateLeafOnlyTree();
std::unique_ptr<blobstore::onblocks::datanodestore::DataInnerNode> CreateTwoLeaf();
std::unique_ptr<blobstore::onblocks::datatreestore::DataTree> CreateTwoLeafTree(); std::unique_ptr<blobstore::onblocks::datatreestore::DataTree> CreateTwoLeafTree();
void FillNode(blobstore::onblocks::datanodestore::DataInnerNode *node); void FillNode(blobstore::onblocks::datanodestore::DataInnerNode *node);
void FillNodeTwoLevel(blobstore::onblocks::datanodestore::DataInnerNode *node); void FillNodeTwoLevel(blobstore::onblocks::datanodestore::DataInnerNode *node);
blockstore::Key CreateFullTwoLevelTree(); std::unique_ptr<blobstore::onblocks::datanodestore::DataInnerNode> CreateFullTwoLevel();
blockstore::Key CreateFullThreeLevelTree(); std::unique_ptr<blobstore::onblocks::datanodestore::DataInnerNode> CreateFullThreeLevel();
blobstore::onblocks::datanodestore::DataNodeStore nodeStore; blobstore::onblocks::datanodestore::DataNodeStore nodeStore;
std::unique_ptr<blobstore::onblocks::datanodestore::DataInnerNode> LoadInnerNode(const blockstore::Key &key); std::unique_ptr<blobstore::onblocks::datanodestore::DataInnerNode> LoadInnerNode(const blockstore::Key &key);