-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_insertion.py
More file actions
49 lines (32 loc) · 1.25 KB
/
test_insertion.py
File metadata and controls
49 lines (32 loc) · 1.25 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
import random
def insertion_sort(arr):
for i in range (len(arr)):
comp = i - 1
while comp >= 0 and arr[i] < arr[comp]:
arr[comp + 1] = arr[comp]
comp -= 1
arr[comp + 1] = arr[i]
return arr
def test_empty_list():
assert insertion_sort([]) == []
def test_one_element_list():
rand_num = random.randint(0, 100)
assert insertion_sort([rand_num]) == [rand_num]
def test_already_sorted():
already_sorted = [i for i in range(100)]
assert insertion_sort(already_sorted) == sorted(already_sorted)
def test_decrementing():
decrementing = [i for i in reversed(range(100))]
assert insertion_sort(decrementing) == sorted(decrementing)
def test_random_positives():
rand_nums = random.sample(range(1, 250), 100)
assert insertion_sort(rand_nums) == sorted(rand_nums)
def test_random_negatives():
rand_nums = random.sample(range(-250, -1), 100)
assert insertion_sort(rand_nums) == sorted(rand_nums)
def test_random_numbers():
rand_nums = random.sample(range(-250, 250), 100)
assert insertion_sort(rand_nums) == sorted(rand_nums)
def test_has_duplicates():
rand_nums = [random.randint(0, 24) for x in range(100)]
assert insertion_sort(rand_nums) == sorted(rand_nums)