Refactor DataTreeGrowingTest_DataStaysIntact

This commit is contained in:
Sebastian Messmer 2015-02-23 20:25:13 +01:00
parent f7e710f305
commit 6d923c5ef5
2 changed files with 66 additions and 71 deletions

View File

@ -16,91 +16,85 @@ using cpputils::dynamic_pointer_move;
class DataTreeGrowingTest_DataStaysIntact: public DataTreeGrowingTest { class DataTreeGrowingTest_DataStaysIntact: public DataTreeGrowingTest {
public: public:
unique_ptr<DataTree> CreateLeafOnlyTreeWithData(TwoLevelDataFixture *data) { unique_ptr<DataTree> TreeWithData(unique_ptr<DataNode> root, TwoLevelDataFixture *data) {
auto leafnode = nodeStore.createNewLeafNode();
data->FillInto(leafnode.get());
return make_unique<DataTree>(&nodeStore, std::move(leafnode));
}
unique_ptr<DataTree> CreateTwoNodeTreeWithData(TwoLevelDataFixture *data) {
auto root = CreateInner({CreateLeaf(), CreateLeaf()});
data->FillInto(root.get()); data->FillInto(root.get());
return make_unique<DataTree>(&nodeStore, std::move(root)); return make_unique<DataTree>(&nodeStore, std::move(root));
} }
unique_ptr<DataTree> CreateThreeNodeChainedTreeWithData(TwoLevelDataFixture *data) { void TestDataStaysIntactOnGrowing(unique_ptr<DataNode> root, TwoLevelDataFixture *data) {
auto leaf = nodeStore.createNewLeafNode(); uint32_t numLeaves = countLeaves(root.get());
data->FillInto(leaf.get()); auto tree = TreeWithData(std::move(root), data);
tree->addDataLeaf();
tree->flush();
auto inner = nodeStore.createNewInnerNode(*leaf); data->EXPECT_DATA_CORRECT(nodeStore.load(tree->key()).get(), numLeaves);
return make_unique<DataTree>(&nodeStore, nodeStore.createNewInnerNode(*inner));
} }
unique_ptr<DataTree> CreateFullTwoLevelTreeWithData(TwoLevelDataFixture *data) { uint32_t countLeaves(DataNode *node) {
auto root = CreateFullTwoLevel(); DataInnerNode *inner = dynamic_cast<DataInnerNode*>(node);
assert(root->numChildren() == DataInnerNode::MAX_STORED_CHILDREN); if (inner == nullptr) {
data->FillInto(root.get()); return 1;
return make_unique<DataTree>(&nodeStore, std::move(root));
} }
uint32_t result = 0;
unique_ptr<DataTree> CreateThreeLevelTreeWithLowerLevelFullWithData(TwoLevelDataFixture *data) { for(int i = 0; i < inner->numChildren(); ++i) {
auto root = CreateInner({CreateFullTwoLevel()}); result += countLeaves(nodeStore.load(inner->getChild(i)->key()).get());
data->FillInto(root.get()); }
return make_unique<DataTree>(&nodeStore, std::move(root)); return result;
} }
}; };
TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowAFullTwoLevelTree) { TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowAFullTwoLevelTree_FullLeaves) {
TwoLevelDataFixture data(&nodeStore); TwoLevelDataFixture data(&nodeStore, 0, true);
auto tree = CreateFullTwoLevelTreeWithData(&data); TestDataStaysIntactOnGrowing(CreateFullTwoLevel(), &data);
tree->addDataLeaf();
tree->flush();
auto root = LoadInnerNode(tree->key());
data.EXPECT_DATA_CORRECT(dynamic_pointer_move<DataInnerNode>(root).get(), DataInnerNode::MAX_STORED_CHILDREN);
} }
TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowAThreeLevelTreeWithLowerLevelFull) { TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowAFullTwoLevelTree_NonFullLeaves) {
TwoLevelDataFixture data(&nodeStore); TwoLevelDataFixture data(&nodeStore, 0, false);
auto tree = CreateThreeLevelTreeWithLowerLevelFullWithData(&data); TestDataStaysIntactOnGrowing(CreateFullTwoLevel(), &data);
tree->addDataLeaf();
tree->flush();
auto root = LoadInnerNode(tree->key());
data.EXPECT_DATA_CORRECT(dynamic_pointer_move<DataInnerNode>(root).get(), DataInnerNode::MAX_STORED_CHILDREN);
} }
class DataTreeGrowingTest_DataStaysIntact_OneDataLeaf: public DataTreeGrowingTest_DataStaysIntact, public WithParamInterface<uint32_t> { TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowAThreeLevelTreeWithLowerLevelFull_FullLeaves) {
}; TwoLevelDataFixture data(&nodeStore, 0, true);
INSTANTIATE_TEST_CASE_P(DataTreeGrowingTest_DataStaysIntact_OneDataLeaf, DataTreeGrowingTest_DataStaysIntact_OneDataLeaf, Values(0, 1, DataLeafNode::MAX_STORED_BYTES-2, DataLeafNode::MAX_STORED_BYTES-1, DataLeafNode::MAX_STORED_BYTES)); auto node = CreateInner({CreateFullTwoLevel()});
TestDataStaysIntactOnGrowing(std::move(node), &data);
TEST_P(DataTreeGrowingTest_DataStaysIntact_OneDataLeaf, GrowAOneNodeTree) {
TwoLevelDataFixture data(&nodeStore, GetParam(), true);
auto tree = CreateLeafOnlyTreeWithData(&data);
tree->addDataLeaf();
tree->flush();
auto root = LoadInnerNode(tree->key());
data.EXPECT_DATA_CORRECT(root.get(), 1);
} }
TEST_P(DataTreeGrowingTest_DataStaysIntact_OneDataLeaf, GrowATwoNodeTree) { TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowAThreeLevelTreeWithLowerLevelFull_NonFullLeaves) {
TwoLevelDataFixture data(&nodeStore, GetParam(), true); TwoLevelDataFixture data(&nodeStore, 0, false);
auto tree = CreateTwoNodeTreeWithData(&data); auto node = CreateInner({CreateFullTwoLevel()});
tree->addDataLeaf(); TestDataStaysIntactOnGrowing(std::move(node), &data);
tree->flush();
auto root = LoadInnerNode(tree->key());
data.EXPECT_DATA_CORRECT(root.get(), 2);
} }
TEST_P(DataTreeGrowingTest_DataStaysIntact_OneDataLeaf, GrowAThreeNodeChainedTree) { TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowAOneNodeTree_FullLeaves) {
TwoLevelDataFixture data(&nodeStore, GetParam(), true); TwoLevelDataFixture data(&nodeStore, 0, true);
auto tree = CreateThreeNodeChainedTreeWithData(&data); TestDataStaysIntactOnGrowing(CreateLeaf(), &data);
tree->addDataLeaf(); }
tree->flush();
TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowAOneNodeTree_NonFullLeaves) {
auto root = LoadInnerNode(tree->key()); TwoLevelDataFixture data(&nodeStore, 0, false);
data.EXPECT_DATA_CORRECT(root.get(), 1); TestDataStaysIntactOnGrowing(CreateLeaf(), &data);
}
TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowATwoNodeTree_FullLeaves) {
TwoLevelDataFixture data(&nodeStore, 0, true);
auto node = CreateInner({CreateLeaf()});
TestDataStaysIntactOnGrowing(std::move(node), &data);
}
TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowATwoNodeTree_NonFullLeaves) {
TwoLevelDataFixture data(&nodeStore, 0, false);
auto node = CreateInner({CreateLeaf()});
TestDataStaysIntactOnGrowing(std::move(node), &data);
}
TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowAThreeNodeChainedTree_FullLeaves) {
TwoLevelDataFixture data(&nodeStore, 0, true);
auto node = CreateInner({CreateInner({CreateLeaf()})});
TestDataStaysIntactOnGrowing(std::move(node), &data);
}
TEST_F(DataTreeGrowingTest_DataStaysIntact, GrowAThreeNodeChainedTree_NonFullLeaves) {
TwoLevelDataFixture data(&nodeStore, 0, false);
auto node = CreateInner({CreateInner({CreateLeaf()})});
TestDataStaysIntactOnGrowing(std::move(node), &data);
} }

View File

@ -2,6 +2,7 @@
#include "../testutils/TwoLevelDataFixture.h" #include "../testutils/TwoLevelDataFixture.h"
using blobstore::onblocks::datanodestore::DataInnerNode; using blobstore::onblocks::datanodestore::DataInnerNode;
using blobstore::onblocks::datanodestore::DataNode;
using blobstore::onblocks::datatreestore::DataTree; using blobstore::onblocks::datatreestore::DataTree;
using blockstore::Key; using blockstore::Key;
@ -11,13 +12,13 @@ using std::function;
class DataTreeShrinkingTest_DataStaysIntact: public DataTreeShrinkingTest { class DataTreeShrinkingTest_DataStaysIntact: public DataTreeShrinkingTest {
public: public:
unique_ptr<DataTree> TwoLevelTreeWithData(unique_ptr<DataInnerNode> root, TwoLevelDataFixture *data) { unique_ptr<DataTree> TreeWithData(unique_ptr<DataNode> root, TwoLevelDataFixture *data) {
data->FillInto(root.get()); data->FillInto(root.get());
return make_unique<DataTree>(&nodeStore, std::move(root)); return make_unique<DataTree>(&nodeStore, std::move(root));
} }
void TestDataStaysIntactOnShrinking(unique_ptr<DataInnerNode> root, TwoLevelDataFixture *data) { void TestDataStaysIntactOnShrinking(unique_ptr<DataInnerNode> root, TwoLevelDataFixture *data) {
auto tree = TwoLevelTreeWithData(std::move(root), data); auto tree = TreeWithData(std::move(root), data);
tree->removeLastDataLeaf(); tree->removeLastDataLeaf();
tree->flush(); tree->flush();