-
Notifications
You must be signed in to change notification settings - Fork 0
/
Testing.py
107 lines (86 loc) · 3.07 KB
/
Testing.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
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
import random
import timeit
import Plotting as Plt
from settings import *
# creates a random list of a certain length
def generate_random_array(length):
return [random.randint(1, 1000) for _ in range(length)]
def test_sorting_algorithm(sorting_algorithm, list_length):
# print(list_length)
times = []
for _ in range(num_tests):
test_list = generate_random_array(list_length)
start_time = timeit.default_timer()
_ = sorting_algorithm(test_list)
end_time = timeit.default_timer()
execution_time = end_time - start_time
times.append(execution_time)
return times
def test_algorithm(sorting_algorithm, x_data):
min_times, avg_times, max_times = [], [], []
for list_length in x_data:
times = test_sorting_algorithm(sorting_algorithm, list_length)
min_times.append(min(times))
max_times.append(max(times))
avg_times.append(sum(times) / num_tests)
return min_times, avg_times, max_times
def test_and_plot(sorting_algorithm):
x_data = list(range(1, max_array_length + 1))
print(f"Iterations (nº tests * max array length): {max_array_length * num_tests}")
start_time = timeit.default_timer()
min_times, avg_times, max_times = test_algorithm(sorting_algorithm, x_data)
end_time = timeit.default_timer()
print(f"\tTotal Execution Time: {end_time - start_time}")
Plt.create_algorithm_plot(x_data, min_times, avg_times, max_times)
def test_various_and_plot():
x_data = list(range(1, max_array_length + 1))
min_times = {algorithm_name: [] for algorithm_name in ALGORITHMS.keys()}
max_times = {algorithm_name: [] for algorithm_name in ALGORITHMS.keys()}
avg_times = {algorithm_name: [] for algorithm_name in ALGORITHMS.keys()}
for algorithm_name, algorithm in ALGORITHMS.items():
n_items = max_array_length * (max_array_length + 1) * 2 # 4*n(n+1)/2 = 1 + 2 + 3 + ... + n
print(f"Algorithm: {algorithm_name} | Elements to order: {n_items} | Iterations: {max_array_length * num_tests}")
start_time = timeit.default_timer()
mn_times, a_times, mx_times = test_algorithm(algorithm, x_data)
end_time = timeit.default_timer()
min_times[algorithm_name] = mn_times
max_times[algorithm_name] = mx_times
avg_times[algorithm_name] = a_times
print(f"\tTotal Execution Time: {end_time - start_time}")
Plt.create_algorithms_plot(
x_data,
min_times,
"Sorting Algorithm Performance (Minimum Time)",
"List Length",
"Minimum Time (seconds)",
1)
Plt.create_algorithms_plot(
x_data,
max_times,
"Sorting Algorithm Performance (Maximum Time)",
"List Length",
"Maximum Time (seconds)",
2)
Plt.create_algorithms_plot(
x_data,
avg_times,
"Sorting Algorithm Performance (Average Time)",
"List Length",
"Average Time (seconds)",
3)
def is_sorted(arr):
for i in range(1, len(arr)):
if arr[i] < arr[i - 1]:
return False
return True
def test_if_works(sorting_algorithm):
for i in range(num_tests):
arr = generate_random_array(max_array_length)
sorted_arr = sorting_algorithm(arr)
if is_sorted(sorted_arr):
print(f"Test {i+1}: SUCCESS!")
else:
print(f"Test {i+1}: FAIL!")
return False
print(f"All {num_tests} Tests Where Successful!")
return True