-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestingsuite.py
More file actions
161 lines (107 loc) · 3.37 KB
/
testingsuite.py
File metadata and controls
161 lines (107 loc) · 3.37 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import os
from threading import Thread
import random
import string
import ast
# With help from this post (https://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits)
def gen_random_string(num: int) -> str:
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=num))
def make_addition(word: str) -> None:
os.system(f'triecli add {word}')
def make_deletion(word: str) -> bool:
if 'success' in os.popen(f'triecli delete {word}').read().lower():
return True
else:
return False
def delete_all() -> None:
os.system('triecli deleteall')
def view_trie() -> list:
return ast.literal_eval(os.popen(f'triecli view').read())
def view_trie_fast() -> list:
return ast.literal_eval(os.popen(f'triecli viewfast').read())
def add_list(arr: list) -> None:
for i in arr:
make_addition(i)
def delete_list(arr: list) -> bool:
answer = True
for i in arr:
answer = answer and make_deletion(i)
return answer
def search(val: str) -> bool:
return 'does' not in os.popen(f'triecli search {val}').read()
def search_list(arr: list) -> bool:
answer = True
for i in arr:
answer = answer and search(i)
return answer
def test_addition_and_deletion() -> bool:
words = []
for _ in range(10):
words.append(gen_random_string(int(random.random() * 100 + 1)))
add_list(words)
v = view_trie()
vf = view_trie_fast()
delete_list(words)
return sorted(v) == sorted(vf) == sorted(words)
def test_delete_all() -> bool:
words = []
for _ in range(10):
words.append(gen_random_string(int(random.random() * 100 + 1)))
add_list(words)
os.system('triecli deleteall')
v = view_trie()
vf = view_trie_fast()
return [] == v == vf
def test_search() -> bool:
words = []
for _ in range(10):
words.append(gen_random_string(int(random.random() * 100 + 1)))
words2 = []
for _ in range(10):
wr = gen_random_string(int(random.random() * 100 + 1))
if wr not in words:
words2.append(wr)
add_list(words)
answer = True
for i in words2:
answer = answer and not search(i)
return search_list(words) and answer
def test_threaded() -> bool:
words_1 = []
words_2 = []
for _ in range(10):
words_1.append(gen_random_string(int(random.random() * 100 + 1)))
for _ in range(10):
words_2.append(gen_random_string(int(random.random() * 100 + 1)))
all_words = words_1 + words_2
t1 = Thread(target=add_list, args=(words_1,))
t2 = Thread(target=add_list, args=(words_2,))
t1.start()
t2.start()
t1.join()
t2.join()
v = view_trie()
vf = view_trie_fast()
return sorted(v) == sorted(vf) == sorted(all_words)
if __name__ == '__main__':
delete_all()
if test_threaded():
print("Passed threaded test")
else:
print("Failed threaded test")
delete_all()
if test_addition_and_deletion():
print("Passed addition and deletion test")
else:
print("Failed addition and deletion test")
delete_all()
if test_delete_all():
print("Passed delete all test")
else:
print("Failed delete all test")
delete_all()
if test_search():
print("Passed search test")
else:
print("Failed search test")
delete_all()