forked from EINDEX/Python-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_search.py
More file actions
51 lines (39 loc) · 1.31 KB
/
test_search.py
File metadata and controls
51 lines (39 loc) · 1.31 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
from unittest import TestCase
def get_random_arr(max_num, length):
import random
return [random.uniform(0, max_num) for x in range(length)]
def create_arr_list():
"""测试序列"""
arr_list = [[0], [0, 1]]
for x in range(6):
arr = get_random_arr(10 ** x, 10 ** x)
arr_list.append(arr)
return arr_list
def search_log(func):
import time, random
arr_list = create_arr_list()
def wrapper(*args, **kw):
start = time.time()
for arr in arr_list:
key = random.choice(arr)
func(arr=arr, key=key, *args, **kw)
over = time.time()
print(func.__name__ + ' time: %f' % (over - start))
return
return wrapper
class TestSearch(TestCase):
@search_log
def test_linear_search(self, arr, key):
from Search.linear_search import linear_search
if not key == linear_search(arr, key):
self.fail()
@search_log
def test_binary_search_while(self, arr, key):
from Search.binary_search import binary_search_while
if not key == binary_search_while(arr, key):
self.fail()
@search_log
def test_binary_search_re(self, arr, key):
from Search.binary_search import binary_search_re
if not key == binary_search_re(arr, key):
self.fail()