2016-06-22 15:23:42 -07:00
|
|
|
import unittest
|
|
|
|
|
|
2019-05-23 09:15:14 -07:00
|
|
|
from binary_search import find
|
2016-06-22 15:23:42 -07:00
|
|
|
|
2019-01-15 08:21:46 -06:00
|
|
|
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.3.0
|
2016-06-22 15:23:42 -07:00
|
|
|
|
2017-10-27 17:53:25 +03:00
|
|
|
|
2018-06-13 09:12:09 -04:00
|
|
|
class BinarySearchTest(unittest.TestCase):
|
2016-09-07 21:33:20 -07:00
|
|
|
def test_finds_value_in_array_with_one_element(self):
|
2019-05-23 09:15:14 -07:00
|
|
|
self.assertEqual(find([6], 6), 0)
|
2016-06-22 15:23:42 -07:00
|
|
|
|
2016-09-07 21:33:20 -07:00
|
|
|
def test_finds_value_in_middle_of_array(self):
|
2019-05-23 09:15:14 -07:00
|
|
|
self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 6), 3)
|
2016-09-07 21:33:20 -07:00
|
|
|
|
|
|
|
|
def test_finds_value_at_beginning_of_array(self):
|
2019-05-23 09:15:14 -07:00
|
|
|
self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 1), 0)
|
2016-09-07 21:33:20 -07:00
|
|
|
|
|
|
|
|
def test_finds_value_at_end_of_array(self):
|
2019-05-23 09:15:14 -07:00
|
|
|
self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 11), 6)
|
2016-06-22 15:23:42 -07:00
|
|
|
|
2016-09-07 21:33:20 -07:00
|
|
|
def test_finds_value_in_array_of_odd_length(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(
|
2019-05-23 09:15:14 -07:00
|
|
|
find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144), 9)
|
2016-06-22 15:23:42 -07:00
|
|
|
|
2016-09-07 21:33:20 -07:00
|
|
|
def test_finds_value_in_array_of_even_length(self):
|
2017-03-23 13:37:20 +01:00
|
|
|
self.assertEqual(
|
2019-05-23 09:15:14 -07:00
|
|
|
find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21),
|
2017-03-23 13:37:20 +01:00
|
|
|
5)
|
2016-06-22 15:23:42 -07:00
|
|
|
|
2016-09-07 21:33:20 -07:00
|
|
|
def test_identifies_value_missing(self):
|
2017-12-12 18:11:43 +00:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-05-23 09:15:14 -07:00
|
|
|
find([1, 3, 4, 6, 8, 9, 11], 7)
|
2016-06-22 15:23:42 -07:00
|
|
|
|
2016-09-07 21:33:20 -07:00
|
|
|
def test_value_smaller_than_arrays_minimum(self):
|
2017-12-12 18:11:43 +00:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-05-23 09:15:14 -07:00
|
|
|
find([1, 3, 4, 6, 8, 9, 11], 0)
|
2016-09-07 21:33:20 -07:00
|
|
|
|
|
|
|
|
def test_value_larger_than_arrays_maximum(self):
|
2017-12-12 18:11:43 +00:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-05-23 09:15:14 -07:00
|
|
|
find([1, 3, 4, 6, 8, 9, 11], 13)
|
2016-09-07 21:33:20 -07:00
|
|
|
|
|
|
|
|
def test_empty_array(self):
|
2017-12-12 18:11:43 +00:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-05-23 09:15:14 -07:00
|
|
|
find([], 1)
|
2016-06-22 15:23:42 -07:00
|
|
|
|
2019-01-15 08:21:46 -06:00
|
|
|
def test_nothing_is_found_when_left_and_right_bounds_cross(self):
|
|
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-05-23 09:15:14 -07:00
|
|
|
find([1, 2], 0)
|
2019-01-15 08:21:46 -06:00
|
|
|
|
2017-12-12 18:11:43 +00:00
|
|
|
# Utility functions
|
|
|
|
|
def setUp(self):
|
|
|
|
|
try:
|
2018-01-18 10:47:11 -06:00
|
|
|
self.assertRaisesRegex
|
2017-12-12 18:11:43 +00:00
|
|
|
except AttributeError:
|
2018-01-18 10:47:11 -06:00
|
|
|
self.assertRaisesRegex = self.assertRaisesRegexp
|
2017-12-12 18:11:43 +00:00
|
|
|
|
|
|
|
|
def assertRaisesWithMessage(self, exception):
|
|
|
|
|
return self.assertRaisesRegex(exception, r".+")
|
|
|
|
|
|
2016-11-29 09:44:47 +01:00
|
|
|
|
2016-06-22 15:23:42 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|