-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·63 lines (41 loc) · 1.61 KB
/
test.py
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
#!/usr/bin/env python3
import random
import sorting_algorithms
import sys
from typing import Callable, List
GREEN = '\033[32m'
RED = '\033[31m'
ENDC = '\033[m'
def _print_test_run_output(name: str, is_sorted: bool, result: List[float]) -> None:
if is_sorted:
print(f"{name}{GREEN} PASS {ENDC}")
else:
print(f"{name}{RED} FAIL {ENDC}")
print(f"The array was not sorted:\n{result}\n")
def _print_test_exception_output(name: str, error_message: str) -> None:
print(f"{name}{RED} FAIL {ENDC}")
print(f"{error_message}\n")
def _is_algorithm(key: str, value: str) -> bool:
# Ignore imported typings and helper functions.
return not value.startswith("typing") and not key.startswith("_")
def _generate_test_input() -> List[float]:
return [random.random() for _ in range(10)]
def _is_sorted(arr: List[float]) -> bool:
return all(arr[i] <= arr[i + 1] for i in range(len(arr) - 1))
def _execute_test(algorithm: Callable[[List[float]], List[float]], algorithm_name: str) -> None:
try:
result = algorithm(_generate_test_input())
_print_test_run_output(algorithm_name, _is_sorted(result), result)
except:
_print_test_exception_output(algorithm_name, str(sys.exc_info()[1]))
def test_one() -> None:
algorithm = getattr(sorting_algorithms, sys.argv[1])
_execute_test(algorithm, algorithm.__name__)
def test_all() -> None:
for key, value in sorting_algorithms.__dict__.items():
if callable(value) and _is_algorithm(str(key), str(value)):
_execute_test(value, key)
if len(sys.argv) == 1:
test_all()
else:
test_one()