-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblack_box_tests
131 lines (111 loc) · 2.71 KB
/
black_box_tests
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <vector>
#include "gtest/gtest.h"
#include "red_black_tree.h"
class EmptyTree : public ::testing::Test {
protected:
BinaryTree tree;
};
TEST_F(EmptyTree, InsertNode) {
auto result = tree.InsertNode(-58);
EXPECT_TRUE(result.first);
EXPECT_EQ(result.second->key, -58);
result = tree.InsertNode(0);
EXPECT_TRUE(result.first);
EXPECT_EQ(result.second->key, 0);
}
TEST_F(EmptyTree, DeleteNode) {
auto result = tree.DeleteNode(0);
EXPECT_FALSE(result);
}
TEST_F(EmptyTree, FindNode) {
auto result = tree.FindNode(0);
EXPECT_FALSE(result);
}
class NonEmptyTree : public ::testing::Test {
protected:
virtual void SetUp() {
int values[] = {1, 25, 3, 4, 12, 58, 754, 87, 54, 123};
for (int i = 0; i < 10; i++) {
tree.InsertNode(values[i]);
}
}
BinaryTree tree;
};
TEST_F(NonEmptyTree, InsertNode) {
auto result = tree.InsertNode(5);
EXPECT_TRUE(result.first);
EXPECT_EQ(result.second->key, 5);
}
TEST_F(NonEmptyTree, DeleteNode_01) {
auto result = tree.DeleteNode(1);
EXPECT_TRUE(result);
}
TEST_F(NonEmptyTree, DeleteNode_02) {
auto result = tree.DeleteNode(2);
EXPECT_FALSE(result);
}
TEST_F(NonEmptyTree, FindNode_01) {
auto result = tree.FindNode(25);
EXPECT_TRUE(result);
}
TEST_F(NonEmptyTree, FindNode_02) {
auto result = tree.FindNode(0);
EXPECT_FALSE(result);
}
class TreeAxioms : public ::testing::Test {
protected:
virtual void SetUp() {
int values[] = {1, 25, 3, 4, 12, 58, 754, 87, 54, 123};
for (int i = 0; i < 10; i++) {
tree.InsertNode(values[i]);
}
}
BinaryTree tree;
};
TEST_F(TreeAxioms, Axiom1) {
std::vector<Node_t *> leaves;
tree.GetLeafNodes(leaves);
for (size_t i = 0; i < leaves.size(); i++) {
auto leaf = leaves.at(i);
auto result = leaf->color;
EXPECT_EQ(result, BLACK);
}
}
TEST_F(TreeAxioms, Axiom2) {
std::vector<Node_t *> nodes;
tree.GetNonLeafNodes(nodes);
for (size_t i = 0; i < nodes.size(); i++) {
auto node = nodes.at(i);
auto result = node->color;
if (result == RED) {
if (node->pLeft != nullptr) {
EXPECT_EQ(node->pLeft->color, BLACK);
}
if (node->pRight != nullptr) {
EXPECT_EQ(node->pRight->color, BLACK);
}
}
}
}
TEST_F(TreeAxioms, Axiom3) {
std::vector<Node_t *> nodes;
tree.GetLeafNodes(nodes);
int blackCount = 0;
int lastBlackCount = 0;
bool result = true;
for (size_t i = 0; i < nodes.size(); i++) {
auto node = nodes.at(i);
while (node != NULL) {
if (node->color == BLACK) {
blackCount++;
}
node = node->pParent;
}
if (lastBlackCount != blackCount && lastBlackCount != 0) {
result = false;
}
lastBlackCount = blackCount;
blackCount = 0;
}
EXPECT_TRUE(result);
}