22 lines
829 B
Python
22 lines
829 B
Python
from random import randint
|
|
from typing import Iterator
|
|
from sort import selection_sort, binary_search
|
|
|
|
def get_random_collection(min: int, max: int, size: int) -> Iterator[int]:
|
|
return [randint(min, max) for _ in range(size)]
|
|
|
|
def test_selection_sort():
|
|
xs = [5, 4, 3, 2, 1, 0]
|
|
assert list(selection_sort(xs)) == sorted(xs)
|
|
assert xs == [5, 4, 3, 2, 1, 0], "list was modified in `selection_sort` return a copy instead"
|
|
xs = get_random_collection(0, 100, 100)
|
|
print(xs)
|
|
assert list(selection_sort(xs)) == sorted(xs)
|
|
|
|
def test_binary_search():
|
|
xs = sorted(set(get_random_collection(0, 10000, 100)))
|
|
for i, e in enumerate(xs):
|
|
assert binary_search(xs, e) == i
|
|
assert binary_search([], 1) == None
|
|
assert binary_search([2], 1) == None
|
|
assert binary_search([2, 3], 1) == None |