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