-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
113 lines (83 loc) · 2.73 KB
/
test.py
File metadata and controls
113 lines (83 loc) · 2.73 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
# some common functions will be used to test a method's correctness.
import sorts
from sorts import bubble_sort
def random_array(maxlength: int = 20, presicion: int = 0, maxvalue: int =100) -> list:
""" The produced array length is in (0,100), the value of elements
are all in (-100,100), type is float.
If the pramater presicion set to 0, the type is int.
"""
import random
result = []
length = random.randint(0,maxlength)
if presicion > 0:
for j in range(length):
result.append(round(random.uniform(-1,1)*100,presicion))
else:
for j in range(length):
result.append(random.randint(-maxvalue,maxvalue))
return result
def equal_list(list1: list, list2: list) -> bool:
""" decide whether two lists is equal ?
If equal return true ; else return false.
Note that if this function meet the first unequal pair, it will return and don't do the following compare operation
"""
if len(list1) != len(list2):
return False
work = 1
for (i,v) in enumerate(list1):
if v != list2[i]:
print("The difference starts in {0} vs {1}".format((i,v),(i,list2[i])))
print(list1)
print(list2)
work = 0
break
if work:
return True
else:
return False
def test_sorts(testmethod: 'function' = bubble_sort,testcases: 'int' =3000) -> bool:
work = 1
for i in range(testcases):
testlist = random_array()
copylist = testlist[:]
# a little stupid in here
if testmethod == sorts.merge_sort:
testmethod(copylist,0,len(copylist)-1)
result_test = copylist[:]
else:
result_test = testmethod(copylist)
result_true = sorted(testlist)
if not equal_list(result_test,result_true):
work = 0
print("orginal: %s" % testlist)
print("test: %s" % result_test)
print("true: %s" % result_true)
break
if work:
print(str(testmethod) + 'is OK!')
print(testlist)
print(result_test)
print(result_true)
return True
else:
return False
def main():
testcases = 3000
testmethod = sorts.merge_sort
test_sorts(testmethod)
# sigel testmethod
# lst = [i for i in range(10,-1,-2)]
# print("input: ",lst)
# if testmethod == sorts.merge_sort:
# testmethod(lst,0,len(lst)-1)
# print(lst)
# else:
# print(testmethod(lst))
# test the equal_list() function
# for j in range(testcases):
# case = random_array();
# equal_list(case,case)
#
# equal_list(case,sorted(case))
if __name__ == '__main__':
main()