|
1 | 1 | from unittest import TestCase
|
2 | 2 |
|
3 | 3 |
|
| 4 | +def get_random_arr(max_num, len, is_int=False): |
| 5 | + """生成随机序列""" |
| 6 | + import random |
| 7 | + if is_int: |
| 8 | + return [int(random.uniform(0, 5000)) for x in range(1000)] |
| 9 | + else: |
| 10 | + return [random.uniform(0, max_num) for x in range(len)] |
| 11 | + |
| 12 | + |
| 13 | +def create_arr_list(): |
| 14 | + """测试序列""" |
| 15 | + arr_list = [[], [0], [1]] |
| 16 | + for x in range(10): |
| 17 | + arr1 = get_random_arr(10 ** x, 10 ** x) |
| 18 | + arr2 = get_random_arr(10 ** x, 10 ** x, True) |
| 19 | + arr_list.append(arr1) |
| 20 | + arr_list.append(arr2) |
| 21 | + return arr_list |
| 22 | + |
| 23 | + |
| 24 | +def log_sort(func): |
| 25 | + arr_list = create_arr_list() |
| 26 | + |
| 27 | + def wrapper(*args, **kw): |
| 28 | + import time |
| 29 | + start = time.time() |
| 30 | + for arr in arr_list: |
| 31 | + func(arr=arr, *args, **kw) |
| 32 | + over = time.time() |
| 33 | + print(func.__name__ + ' time: %f' % (over - start)) |
| 34 | + return |
| 35 | + |
| 36 | + return wrapper |
| 37 | + |
| 38 | + |
4 | 39 | class TestSort(TestCase):
|
| 40 | + @log_sort |
| 41 | + def test_bubble_sort(self, arr): |
| 42 | + from Sort.bubble_sort import bubble_sort |
| 43 | + if not sorted(arr) == bubble_sort(arr): |
| 44 | + self.fail() |
| 45 | + |
| 46 | + @log_sort |
| 47 | + def test_bubble_sort_flag(self, arr): |
| 48 | + from Sort.bubble_sort import bubble_sort_flag |
| 49 | + if not sorted(arr) == bubble_sort_flag(arr): |
| 50 | + self.fail() |
| 51 | + |
| 52 | + @log_sort |
| 53 | + def test_heap_sort(self, arr): |
| 54 | + from Sort.heap_sort import heap_sort |
| 55 | + if not sorted(arr) == heap_sort(arr): |
| 56 | + self.fail() |
| 57 | + |
| 58 | + @log_sort |
| 59 | + def test_insert_sort(self, arr): |
| 60 | + from Sort.insert_sort import insert_sort |
| 61 | + if not sorted(arr) == insert_sort(arr): |
| 62 | + self.fail() |
| 63 | + |
| 64 | + @log_sort |
| 65 | + def test_merge_sort(self, arr): |
| 66 | + from Sort.marge_sort import merge_sort |
| 67 | + if not sorted(arr) == merge_sort(arr): |
| 68 | + self.fail() |
| 69 | + |
| 70 | + @log_sort |
| 71 | + def test_quick_sort(self, arr): |
| 72 | + from Sort.quick_sort import quick_sort |
| 73 | + if not sorted(arr) == quick_sort(arr): |
| 74 | + self.fail() |
| 75 | + |
| 76 | + @log_sort |
| 77 | + def test_quick_sort_cookbook(self, arr): |
| 78 | + from Sort.quick_sort import quick_sort_cookbook |
| 79 | + if not sorted(arr) == quick_sort_cookbook(arr): |
| 80 | + self.fail() |
5 | 81 |
|
6 |
| - def test_bubble_sort(self): |
| 82 | + @log_sort |
| 83 | + def test_selection_sort(self, arr): |
| 84 | + from Sort.selection_sort import selection_sort |
| 85 | + if not sorted(arr) == selection_sort(arr): |
| 86 | + self.fail() |
7 | 87 |
|
8 |
| - self.fail() |
| 88 | + @log_sort |
| 89 | + def test_shell_sort(self, arr): |
| 90 | + from Sort.shell_sort import shell_sort |
| 91 | + if not sorted(arr) == shell_sort(arr): |
| 92 | + self.fail() |
9 | 93 |
|
| 94 | + @log_sort |
| 95 | + def test_count_sort(self, arr): |
| 96 | + from Sort.count_sort import count_sort |
| 97 | + if len(arr) == 0: |
| 98 | + if not sorted(arr) == count_sort(arr): |
| 99 | + self.fail() |
| 100 | + elif isinstance(arr[0], int): |
| 101 | + if not sorted(arr) == count_sort(arr): |
| 102 | + self.fail() |
0 commit comments