Skip to content
Snippets Groups Projects
Commit ee2f9094 authored by Jirka Fink's avatar Jirka Fink
Browse files

More tests for the tree_successor assignment

parent 118c7b92
No related branches found
No related tags found
No related merge requests found
...@@ -24,11 +24,15 @@ void test(const vector<int>& sequence, Tree &tree) { ...@@ -24,11 +24,15 @@ void test(const vector<int>& sequence, Tree &tree) {
EXPECT(!node, "Expected no successor, got " + to_string(node->key)); EXPECT(!node, "Expected no successor, got " + to_string(node->key));
} }
void test_path(bool right) { vector<int> get_linear_sequence() {
vector<int> numbers; vector<int> numbers;
for (int i = 0; i < 10000000; i++) for (int i = 0; i < 10000000; i++)
numbers.push_back((int)(7.13*i)); numbers.push_back((int)(7.13*i));
return numbers;
}
void test_path(bool right) {
vector<int> numbers = get_linear_sequence();
Tree tree; Tree tree;
Node *node = nullptr; Node *node = nullptr;
if (right) if (right)
...@@ -41,11 +45,20 @@ void test_path(bool right) { ...@@ -41,11 +45,20 @@ void test_path(bool right) {
test(numbers, tree); test(numbers, tree);
} }
void test_random() { void test_two_paths() {
vector<int> sequence = {997}; vector<int> numbers = get_linear_sequence();
for (int i = 2; i < 199999; i++) Tree tree;
sequence.push_back((sequence.back() * int64_t(997)) % 199999); Node *node = nullptr;
for(size_t i = numbers.size()/2; i < numbers.size(); i++)
node = tree.insert(numbers[i], node);
node = nullptr;
for(int i = numbers.size()/2 - 1; i >= 0; i--)
node = tree.insert(numbers[i], node);
test(numbers, tree);
}
void test_sequence(vector<int> &&sequence) {
Tree tree; Tree tree;
for (const auto& element : sequence) for (const auto& element : sequence)
tree.insert(element); tree.insert(element);
...@@ -54,8 +67,39 @@ void test_random() { ...@@ -54,8 +67,39 @@ void test_random() {
test(sequence, tree); test(sequence, tree);
} }
void test_random() {
vector<int> sequence = {997};
for (int i = 2; i < 199999; i++)
sequence.push_back((sequence.back() * int64_t(997)) % 199999);
test_sequence(move(sequence));
}
void test_trivial() {
test_sequence({5});
test_sequence({7,9});
test_sequence({7,3});
test_sequence({5,3,7});
}
void test_comb() {
vector<int> numbers = get_linear_sequence();
Tree tree;
Node *node = nullptr;
for(size_t i = numbers.size()/2; i < numbers.size(); i++)
node = tree.insert(numbers[i], node);
node = nullptr;
for(int i = numbers.size()/2 - 1; i >= 0; i-=2) {
node = tree.insert(numbers[i-1], node);
tree.insert(numbers[i], node);
}
test(numbers, tree);
}
vector<pair<string, function<void()>>> tests = { vector<pair<string, function<void()>>> tests = {
{ "trivial", test_trivial },
{ "right_path", []{ test_path(true); } }, { "right_path", []{ test_path(true); } },
{ "left_path", []{ test_path(false); } }, { "left_path", []{ test_path(false); } },
{ "random_tree", test_random }, { "random_tree", test_random },
{ "two_paths", test_two_paths },
{ "comb", test_comb }
}; };
...@@ -11,14 +11,22 @@ def test_tree(tree, sequence): ...@@ -11,14 +11,22 @@ def test_tree(tree, sequence):
node = tree.successor(node) node = tree.successor(node)
assert node is None, "Expected no successor, got {}".format(node.key) assert node is None, "Expected no successor, got {}".format(node.key)
def test_random_tree(): def test_sequence(sequence):
sequence = [pow(997, i, 199999) for i in range(1, 199999)]
tree = Tree() tree = Tree()
for i in sequence: for i in sequence:
tree.insert(i) tree.insert(i)
sequence.sort() sequence.sort()
test_tree(tree, sequence) test_tree(tree, sequence)
def test_trivial_tree():
test_sequence([5])
test_sequence([7,9])
test_sequence([7,3])
test_sequence([5,3,7])
def test_random_tree():
test_sequence([pow(997, i, 199999) for i in range(1, 199999)])
def test_path(right): def test_path(right):
sequence = [int(7.13*i) for i in range(1000000)] sequence = [int(7.13*i) for i in range(1000000)]
tree = Tree() tree = Tree()
...@@ -28,10 +36,37 @@ def test_path(right): ...@@ -28,10 +36,37 @@ def test_path(right):
node = tree.insert(key, node) node = tree.insert(key, node)
test_tree(tree, sequence) test_tree(tree, sequence)
def test_two_paths():
sequence_left = [int(7.13*i) for i in range(1000000)]
sequence_right = [int(7.13*i) for i in range(1000000, 2000000)]
tree = Tree()
node = None
for key in sequence_right:
node = tree.insert(key, node)
node = None
for key in reversed(sequence_left):
node = tree.insert(key, node)
test_tree(tree, sequence_left + sequence_right)
def test_comb():
sequence = [int(7.13*i) for i in range(1000000)]
tree = Tree()
node = None
for i in range(len(sequence)//2, len(sequence)):
node = tree.insert(sequence[i], node)
node = None
for i in range(len(sequence)//2-1, 0, -2):
node = tree.insert(sequence[i-1], node)
tree.insert(sequence[i], node)
test_tree(tree, sequence)
tests = [ tests = [
("trivial", test_trivial_tree),
("random_tree", test_random_tree), ("random_tree", test_random_tree),
("right_path", lambda: test_path(True)), ("right_path", lambda: test_path(True)),
("left_path", lambda: test_path(False)), ("left_path", lambda: test_path(False)),
("two_paths", test_two_paths),
("comb", test_comb),
] ]
if __name__ == "__main__": if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment