Written more test cases for DataInnerNode and refactored them a bit

This commit is contained in:
Sebastian Messmer 2015-01-23 03:05:55 +01:00
parent a427b0e677
commit dabe6e2567
2 changed files with 92 additions and 14 deletions

View File

@ -31,20 +31,22 @@ public:
void InitializeNewNode(const DataNode &first_child_key);
ChildEntry *ChildrenBegin();
ChildEntry *ChildrenEnd();
const ChildEntry *ChildrenBegin() const;
const ChildEntry *ChildrenEnd() const;
ChildEntry *getChild(unsigned int index);
const ChildEntry *getChild(unsigned int index) const;
ChildEntry *LastChild();
const ChildEntry *LastChild() const;
uint32_t numChildren() const;
void addChild(const DataNode &child_key);
private:
ChildEntry *LastChild();
const ChildEntry *LastChild() const;
ChildEntry *ChildrenBegin();
ChildEntry *ChildrenEnd();
const ChildEntry *ChildrenBegin() const;
const ChildEntry *ChildrenEnd() const;
};
}

View File

@ -8,9 +8,12 @@
#include "blockstore/implementations/testfake/FakeBlock.h"
#include <memory>
#include "fspp/utils/pointer.h"
using ::testing::Test;
using fspp::dynamic_pointer_move;
using blockstore::Key;
using blockstore::testfake::FakeBlockStore;
using blockstore::BlockStore;
@ -31,6 +34,46 @@ public:
node(nodeStore->createNewInnerNode(*leaf)) {
}
unique_ptr<DataInnerNode> LoadInnerNode(const Key &key) {
auto node = nodeStore->load(key);
return dynamic_pointer_move<DataInnerNode>(node);
}
Key CreateNewInnerNodeReturnKey(const DataNode &firstChild) {
return nodeStore->createNewInnerNode(firstChild)->key();
}
unique_ptr<DataInnerNode> CreateNewInnerNode() {
auto new_leaf = nodeStore->createNewLeafNode();
return nodeStore->createNewInnerNode(*new_leaf);
}
unique_ptr<DataInnerNode> CreateAndLoadNewInnerNode(const DataNode &firstChild) {
auto key = CreateNewInnerNodeReturnKey(firstChild);
return LoadInnerNode(key);
}
unique_ptr<DataInnerNode> CreateNewInnerNode(const DataNode &firstChild, const DataNode &secondChild) {
auto node = nodeStore->createNewInnerNode(firstChild);
node->addChild(secondChild);
return node;
}
Key CreateNewInnerNodeReturnKey(const DataNode &firstChild, const DataNode &secondChild) {
return CreateNewInnerNode(firstChild, secondChild)->key();
}
unique_ptr<DataInnerNode> CreateAndLoadNewInnerNode(const DataNode &firstChild, const DataNode &secondChild) {
auto key = CreateNewInnerNodeReturnKey(firstChild, secondChild);
return LoadInnerNode(key);
}
Key AddALeafTo(DataInnerNode *node) {
auto leaf2 = nodeStore->createNewLeafNode();
node->addChild(*leaf2);
return leaf2->key();
}
unique_ptr<BlockStore> _blockStore;
BlockStore *blockStore;
unique_ptr<DataNodeStore> nodeStore;
@ -40,25 +83,58 @@ public:
TEST_F(DataInnerNodeTest, InitializesCorrectly) {
node->InitializeNewNode(*leaf);
EXPECT_EQ(1u, node->numChildren());
EXPECT_EQ(leaf->key(), node->getChild(0)->key());
}
TEST_F(DataInnerNodeTest, ReinitializesCorrectly) {
node->InitializeNewNode(*leaf);
auto leaf2 = nodeStore->createNewLeafNode();
node->addChild(*leaf2);
AddALeafTo(node.get());
node->InitializeNewNode(*leaf);
EXPECT_EQ(1u, node->numChildren());
EXPECT_EQ(leaf->key(), node->getChild(0)->key());
}
TEST_F(DataInnerNodeTest, IsCorrectlyInitializedAfterLoading) {
auto loaded = CreateAndLoadNewInnerNode(*leaf);
EXPECT_EQ(1u, loaded->numChildren());
EXPECT_EQ(leaf->key(), loaded->getChild(0)->key());
}
TEST_F(DataInnerNodeTest, AddingASecondLeaf) {
auto leaf2 = nodeStore->createNewLeafNode();
node->addChild(*leaf2);
Key leaf2_key = AddALeafTo(node.get());
EXPECT_EQ(2u, node->numChildren());
EXPECT_EQ(leaf->key(), node->getChild(0)->key());
EXPECT_EQ(leaf2->key(), node->getChild(1)->key());
EXPECT_EQ(leaf2_key, node->getChild(1)->key());
}
TEST_F(DataInnerNodeTest, AddingASecondLeafAndReload) {
auto leaf2 = nodeStore->createNewLeafNode();
auto loaded = CreateAndLoadNewInnerNode(*leaf, *leaf2);
EXPECT_EQ(2u, loaded->numChildren());
EXPECT_EQ(leaf->key(), loaded->getChild(0)->key());
EXPECT_EQ(leaf2->key(), loaded->getChild(1)->key());
}
TEST_F(DataInnerNodeTest, BuildingAThreeLevelTree) {
auto node2 = CreateNewInnerNode();
auto parent = CreateNewInnerNode(*node, *node2);
EXPECT_EQ(2u, parent->numChildren());
EXPECT_EQ(node->key(), parent->getChild(0)->key());
EXPECT_EQ(node2->key(), parent->getChild(1)->key());
}
TEST_F(DataInnerNodeTest, BuildingAThreeLevelTreeAndReload) {
auto node2 = CreateNewInnerNode();
auto parent = CreateAndLoadNewInnerNode(*node, *node2);
EXPECT_EQ(2u, parent->numChildren());
EXPECT_EQ(node->key(), parent->getChild(0)->key());
EXPECT_EQ(node2->key(), parent->getChild(1)->key());
}