forked from exercism/python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_search_test.py
More file actions
55 lines (38 loc) · 1.93 KB
/
binary_search_test.py
File metadata and controls
55 lines (38 loc) · 1.93 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
50
51
52
53
54
55
import unittest
from binary_search import find
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.3.0
class BinarySearchTest(unittest.TestCase):
def test_finds_a_value_in_an_array_with_one_element(self):
self.assertEqual(find([6], 6), 0)
def test_finds_a_value_in_the_middle_of_an_array(self):
self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 6), 3)
def test_finds_a_value_at_the_beginning_of_an_array(self):
self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 1), 0)
def test_finds_a_value_at_the_end_of_an_array(self):
self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 11), 6)
def test_finds_a_value_in_an_array_of_odd_length(self):
self.assertEqual(
find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144), 9
)
def test_finds_a_value_in_an_array_of_even_length(self):
self.assertEqual(find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21), 5)
def test_identifies_that_a_value_is_not_included_in_the_array(self):
with self.assertRaisesWithMessage(ValueError):
find([1, 3, 4, 6, 8, 9, 11], 7)
def test_a_value_smaller_than_the_array_s_smallest_value_is_not_found(self):
with self.assertRaisesWithMessage(ValueError):
find([1, 3, 4, 6, 8, 9, 11], 0)
def test_a_value_larger_than_the_array_s_largest_value_is_not_found(self):
with self.assertRaisesWithMessage(ValueError):
find([1, 3, 4, 6, 8, 9, 11], 13)
def test_nothing_is_found_in_an_empty_array(self):
with self.assertRaisesWithMessage(ValueError):
find([], 1)
def test_nothing_is_found_when_the_left_and_right_bounds_cross(self):
with self.assertRaisesWithMessage(ValueError):
find([1, 2], 0)
# Utility functions
def assertRaisesWithMessage(self, exception):
return self.assertRaisesRegex(exception, r".+")
if __name__ == "__main__":
unittest.main()