From 46d3761e54684e6af9112cc0bd1aabef42370472 Mon Sep 17 00:00:00 2001
From: Pavel Vesely <vesely@iuuk.mff.cuni.cz>
Date: Mon, 30 Sep 2024 12:59:54 +0200
Subject: [PATCH] 1st task update
---
.../cpp/tree_successor_more_tests.cpp | 105 ------------------
01-tree_successor/cpp/tree_successor_test.cpp | 56 +++++++++-
.../python/tree_successor_more_tests.py | 80 -------------
.../python/tree_successor_test.py | 39 ++++++-
01-tree_successor/task.md | 4 -
5 files changed, 87 insertions(+), 197 deletions(-)
delete mode 100644 01-tree_successor/cpp/tree_successor_more_tests.cpp
delete mode 100644 01-tree_successor/python/tree_successor_more_tests.py
diff --git a/01-tree_successor/cpp/tree_successor_more_tests.cpp b/01-tree_successor/cpp/tree_successor_more_tests.cpp
deleted file mode 100644
index 552762a..0000000
--- a/01-tree_successor/cpp/tree_successor_more_tests.cpp
+++ /dev/null
@@ -1,105 +0,0 @@
-#include <algorithm>
-#include <functional>
-#include <cstdint>
-#include <string>
-#include <utility>
-#include <vector>
-
-#include "tree_successor.h"
-
-using namespace std;
-
-// If the condition is not true, report an error and halt.
-#define EXPECT(condition, message) do { if (!(condition)) expect_failed(message); } while (0)
-void expect_failed(const string& message);
-
-void test(const vector<int>& sequence, Tree &tree) {
- Node* node = tree.successor(nullptr);
- for (const auto& element : sequence) {
- EXPECT(node, "Expected successor " + to_string(element) + ", got nullptr");
- EXPECT(node->key == element,
- "Expected successor " + to_string(element) + ", got " + to_string(node->key));
- node = tree.successor(node);
- }
- EXPECT(!node, "Expected no successor, got " + to_string(node->key));
-}
-
-vector<int> get_linear_sequence() {
- vector<int> numbers;
- for (int i = 0; i < 10000000; i++)
- numbers.push_back((int)(7.13*i));
- return numbers;
-}
-
-void test_path(bool right) {
- vector<int> numbers = get_linear_sequence();
- Tree tree;
- Node *node = nullptr;
- if (right)
- for (int key : numbers)
- node = tree.insert(key, node);
- else
- for (int index = numbers.size() - 1; index >= 0; --index)
- node = tree.insert(numbers[index], node);
-
- test(numbers, tree);
-}
-
-void test_two_paths() {
- 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--)
- node = tree.insert(numbers[i], node);
-
- test(numbers, tree);
-}
-
-void test_sequence(vector<int> &&sequence) {
- Tree tree;
- for (const auto& element : sequence)
- tree.insert(element);
-
- sort(sequence.begin(), sequence.end());
- 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 = {
- { "trivial", test_trivial },
- { "right_path", []{ test_path(true); } },
- { "left_path", []{ test_path(false); } },
- { "random_tree", test_random },
- { "two_paths", test_two_paths },
- { "comb", test_comb }
-};
diff --git a/01-tree_successor/cpp/tree_successor_test.cpp b/01-tree_successor/cpp/tree_successor_test.cpp
index d26af4d..552762a 100644
--- a/01-tree_successor/cpp/tree_successor_test.cpp
+++ b/01-tree_successor/cpp/tree_successor_test.cpp
@@ -24,11 +24,15 @@ void test(const vector<int>& sequence, Tree &tree) {
EXPECT(!node, "Expected no successor, got " + to_string(node->key));
}
-void test_path(bool right) {
+vector<int> get_linear_sequence() {
vector<int> numbers;
for (int i = 0; i < 10000000; i++)
numbers.push_back((int)(7.13*i));
+ return numbers;
+}
+void test_path(bool right) {
+ vector<int> numbers = get_linear_sequence();
Tree tree;
Node *node = nullptr;
if (right)
@@ -41,11 +45,20 @@ void test_path(bool right) {
test(numbers, tree);
}
-void test_random() {
- vector<int> sequence = {997};
- for (int i = 2; i < 199999; i++)
- sequence.push_back((sequence.back() * int64_t(997)) % 199999);
+void test_two_paths() {
+ 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--)
+ node = tree.insert(numbers[i], node);
+
+ test(numbers, tree);
+}
+void test_sequence(vector<int> &&sequence) {
Tree tree;
for (const auto& element : sequence)
tree.insert(element);
@@ -54,8 +67,39 @@ void test_random() {
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 = {
+ { "trivial", test_trivial },
{ "right_path", []{ test_path(true); } },
{ "left_path", []{ test_path(false); } },
- {"random_tree", test_random },
+ { "random_tree", test_random },
+ { "two_paths", test_two_paths },
+ { "comb", test_comb }
};
diff --git a/01-tree_successor/python/tree_successor_more_tests.py b/01-tree_successor/python/tree_successor_more_tests.py
deleted file mode 100644
index 3415b8f..0000000
--- a/01-tree_successor/python/tree_successor_more_tests.py
+++ /dev/null
@@ -1,80 +0,0 @@
-#!/usr/bin/env python3
-import sys
-
-from tree_successor import Tree
-
-def test_tree(tree, sequence):
- node = tree.successor(None)
- for element in sequence:
- assert node is not None, "Expected successor {}, got None".format(element)
- assert node.key == element, "Expected successor {}, got {}".format(element, node.key)
- node = tree.successor(node)
- assert node is None, "Expected no successor, got {}".format(node.key)
-
-def test_sequence(sequence):
- tree = Tree()
- for i in sequence:
- tree.insert(i)
- sequence.sort()
- 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):
- sequence = [int(7.13*i) for i in range(1000000)]
- tree = Tree()
- node = None
- sequence_insert = sequence if right else reversed(sequence)
- for key in sequence_insert:
- node = tree.insert(key, node)
- 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 = [
- ("trivial", test_trivial_tree),
- ("random_tree", test_random_tree),
- ("right_path", lambda: test_path(True)),
- ("left_path", lambda: test_path(False)),
- ("two_paths", test_two_paths),
- ("comb", test_comb),
-]
-
-if __name__ == "__main__":
- for required_test in sys.argv[1:] or [name for name, _ in tests]:
- for name, test in tests:
- if name == required_test:
- print("Running test {}".format(name), file=sys.stderr)
- test()
- break
- else:
- raise ValueError("Unknown test {}".format(name))
diff --git a/01-tree_successor/python/tree_successor_test.py b/01-tree_successor/python/tree_successor_test.py
index db3df55..3415b8f 100644
--- a/01-tree_successor/python/tree_successor_test.py
+++ b/01-tree_successor/python/tree_successor_test.py
@@ -11,14 +11,22 @@ def test_tree(tree, sequence):
node = tree.successor(node)
assert node is None, "Expected no successor, got {}".format(node.key)
-def test_random_tree():
- sequence = [pow(997, i, 199999) for i in range(1, 199999)]
+def test_sequence(sequence):
tree = Tree()
for i in sequence:
tree.insert(i)
sequence.sort()
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):
sequence = [int(7.13*i) for i in range(1000000)]
tree = Tree()
@@ -28,10 +36,37 @@ def test_path(right):
node = tree.insert(key, node)
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 = [
+ ("trivial", test_trivial_tree),
("random_tree", test_random_tree),
("right_path", lambda: test_path(True)),
("left_path", lambda: test_path(False)),
+ ("two_paths", test_two_paths),
+ ("comb", test_comb),
]
if __name__ == "__main__":
diff --git a/01-tree_successor/task.md b/01-tree_successor/task.md
index 05441ac..ec268c4 100644
--- a/01-tree_successor/task.md
+++ b/01-tree_successor/task.md
@@ -15,7 +15,3 @@ You should submit the file `tree_successor.*` (but not the
`tree_successor_test.*`).
Source code templates can be found in [the git repository](https://gitlab.kam.mff.cuni.cz/datovky/assignments/-/tree/master).
-
-Files tree_successor_more_tests.{cpp,py} contain additional tests
-for bugs discovered in students' solutions. They are not included on
-recodex, but your program should pass them in few seconds.
--
GitLab