def test_sort_perf(): matrix = [] cases = gen_special_sort_case(2000) for entry in sort_funcs: func = entry['func'] desc = entry.get('name', get_func_name(func)) func_name = get_func_name(func) kwargs = getattr(func, 'keywords', {}) if entry.get('skip_large', False): print(desc, 'skipping large testcase.') continue matrix.append([desc, []]) print('BEGIN {}'.format(desc)) if entry.get('is_random', False): times = 5 else: times = 1 for case, arr in cases.items(): durations = [] for n in range(times): with timed_test('{desc}(_{case}_) #{n}'.format_map(locals()), 'performance') as get_duration: func(arr.copy()) durations.append(get_duration()) matrix[-1][1].append((case, statistics.mean(durations))) print('END {}'.format(desc)) print() print_matrix(matrix)
def test_nth_small(): for entry in all_select_funcs: func = entry['func'] desc = entry.get('name', get_func_name(func)) if entry.get('skip_large', False): print(desc, 'skipping large testcase.') case_size = 500 else: case_size = 5000 with timed_test(desc): for seq in chain(gen_sort_case(7), gen_special_sort_case(case_size).values()): copy = seq.copy() if len(copy) == 0: continue sorted_input = sorted(copy) if len(copy) > 20: ranger = range(0, len(copy), len(copy) // 10) else: ranger = range(len(copy)) for i in ranger: n = i + 1 selected = func(copy.copy(), n) assert selected == sorted_input[i], \ '{desc}({copy}, {n}) -> {selected}'.format_map(vars()) for large_func in (nth_large, nth_large_mm): with timed_test(get_func_name(large_func)): assert large_func(list(range(100)), 5) == 95
def test_nth_small_perf(): matrix = [] cases = gen_special_sort_case(2000) for entry in all_select_funcs: func = entry['func'] func_name = entry.get('name', get_func_name(func)) if entry.get('is_random', False): times = 10 else: times = 2 matrix.append([func_name, []]) print('BEGIN', func_name) for case, arr in cases.items(): durations = [] for n in range(times): desc = '{func_name}(_{case}_) #{n}'.format_map(locals()) with timed_test(desc, 'performance') as get_duration: func(arr.copy(), len(arr) // 3) durations.append(get_duration()) matrix[-1][1].append((case, statistics.mean(durations))) print('END', func_name) print() print_matrix(matrix)
def test_stable_sort(): def check_stable(indexed): for k, g in groupby(indexed, key=lambda x: x[1]): # group by key indexes = list(x[0] for x in g) assert sorted(indexes) == indexes # for func, desc in stable_sort_funcs: for entry in sort_funcs: if not entry.get('stable', False): continue func = entry['func'] desc = entry.get('name', get_func_name(func)) def gen_spec_case(size): spec_cases = gen_special_sort_case(size) for case in ('rand', 'dup', 'dup99'): yield spec_cases[case] if entry.get('skip_large', False): print(desc, 'skipping large testcase.') case_size = 500 else: case_size = 5000 with timed_test(desc, 'stable sort'): for arr in chain(gen_sort_case(7), gen_spec_case(case_size)): indexed = list(enumerate(arr)) # index, key func(indexed, key=lambda x: x[1]) # sort by key check_stable(indexed)
def test_sort(): def check_sorted(arr, func=qsort, **kwargs): copy = arr.copy() try: func(copy, **kwargs) except Exception: print('exception', copy) raise std_kwargs = { x: kwargs[x] for x in ('reverse', 'key') if x in kwargs } # this is the fastest way to check wether arr is sorted ans = sorted(copy, **std_kwargs) assert copy == ans, 'sort({arr}) -> {copy}'.format_map(vars()) def perm_test(func, desc, maxlen=7): kwargs = getattr(func, 'keywords', {}) with timed_test(desc, 'permutation'): for seq in gen_sort_case(maxlen): check_sorted(seq, func=func, **kwargs) for entry in sort_funcs: sorter = entry['func'] desc = entry.get('name', get_func_name(sorter)) perm_test(sorter, desc) # test ReversedKey and reversed sort check_sorted(list(range(100)), func=sorter, reverse=True) check_sorted(list(range(100, 0, -1)), func=sorter, reverse=True)
def test_topk(): for topk in (topk_by_bigheap, topk_by_smallheap): with timed_test(get_func_name(topk)): for seq in gen_sort_case(7): sorted_input = sorted(seq) for k in range(len(seq)): copy = seq.copy() kseq = topk(copy, k) assert sorted(kseq) == sorted_input[len(seq) - k:], \ 'topk({copy}, {k}) -> {kseq}'.format_map(vars())
def test_middle_iter(): for iterator in (middle_iter, middle_iter_bystack, middle_iter_sm): with timed_test(get_func_name(iterator)): for root in gen_bst(4): count = BSTree(root).count() expanded = list(iterator(root)) expanded_sorted = sorted(expanded, key=lambda n: n.data) assert len(expanded) == count \ and expanded_sorted == expanded == list(middle_iter(root)), \ ('{expanded}'.format_map(vars()), print_tree(BSTree(root)))
def test_is_bstree(): for iterator in (middle_iter, middle_iter_bystack, middle_iter_sm): is_bstree_func = partial(is_bstree, iterator=iterator) with timed_test('is_bstree(*, iterator={})'.format( get_func_name(iterator))): for bst in gen_bst(4): assert is_bstree_func(BSTree(bst)) not_bst = ([0, 1, 2], [0, 1], [0, -1, 2, -2, 1]) for heap in not_bst: tree = BaseTree.from_heap(heap) assert not is_bstree_func(tree)