-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie_remove_test.cpp
104 lines (77 loc) · 2.4 KB
/
trie_remove_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "trie.h"
struct TestingTrie : public Trie {
public:
using Node::children;
using Node::operator[];
};
using ::testing::Contains;
using ::testing::Eq;
using ::testing::Key;
using ::testing::Not;
TEST(TrieRemoveTest, RemoveElementFromSignleElementTree) {
TestingTrie tree;
tree["One"] = Node{true};
tree.Remove("One");
EXPECT_THAT(tree.children->empty(), Eq(true));
}
TEST(TrieRemoveTest, RemoveElementWithoutChildrenFromFirstLevel) {
TestingTrie tree;
tree["One"] = Node{true};
tree["Two"] = Node{true};
tree.Remove("One");
EXPECT_THAT(*tree.children, Not(Contains(Key("One"))));
}
TEST(TrieRemoveTest, NoElementToRemoveFomFirstLevel) {
TestingTrie tree;
tree["On"] = Node{};
tree["On"]["e"] = Node{true};
tree["On"]["ce"] = Node{true};
tree.Remove("On");
EXPECT_THAT(*tree.children, Contains(Key("On")));
}
TEST(TrieRemoveTest, UnmarkRootElementWithChildren) {
TestingTrie tree;
tree["Deep"] = Node{true};
tree["Deep"]["water"] = Node{true};
tree["Deep"]["sea"] = Node{true};
tree.Remove("Deep");
ASSERT_THAT(*tree.children, Contains(Key("Deep")));
EXPECT_THAT(tree["Deep"].marker, Eq(false));
}
TEST(TrieRemoveTest, MoveUpChildElementIfGrandParentIsMarked) {
TestingTrie tree;
tree["Deep"] = Node{true};
tree["Deep"]["er"] = Node{true};
tree["Deep"]["er"]["sea"] = Node{true};
tree.Remove("Deeper");
ASSERT_THAT(*tree["Deep"].children, Not(Contains(Key("er"))));
EXPECT_THAT(*tree["Deep"].children, Contains(Key("ersea")));
}
TEST(TrieRemoveTest, RemoveParentAndCompressWithSingleChild) {
TestingTrie tree;
tree["Deep"] = Node{true};
tree["Deep"]["sea"] = Node{true};
tree.Remove("Deep");
EXPECT_THAT(*tree.children, Contains(Key("Deepsea")));
EXPECT_THAT(*tree.children, Not(Contains(Key("Deep"))));
}
TEST(TrieRemoveTest, RemoveElementFromSecondLevel) {
TestingTrie tree;
tree["On"] = Node{};
tree["On"]["e"] = Node{true};
tree["On"]["ce"] = Node{true};
tree["On"]["ion"] = Node{true};
tree.Remove("Once");
EXPECT_THAT(*tree["On"].children, Not(Contains(Key("ce"))));
}
TEST(TrieRemoveTest, RemoveChildAndCompressWithParentLastChild) {
TestingTrie tree;
tree["On"] = Node{};
tree["On"]["e"] = Node{true};
tree["On"]["ce"] = Node{true};
tree.Remove("One");
EXPECT_THAT(*tree.children, Not(Contains(Key("On"))));
EXPECT_THAT(*tree.children, Contains(Key("Once")));
}