Files
python/exercises/binary-search/binary_search_test.py

48 lines
1.5 KiB
Python
Raw Normal View History

2016-06-22 15:23:42 -07:00
import unittest
2016-09-07 21:33:20 -07:00
from binary_search import binary_search
2016-06-22 15:23:42 -07:00
class BinarySearchTests(unittest.TestCase):
2016-09-07 21:33:20 -07:00
def test_finds_value_in_array_with_one_element(self):
self.assertEqual(binary_search([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):
self.assertEqual(binary_search([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):
self.assertEqual(binary_search([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):
self.assertEqual(binary_search([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):
self.assertEqual(
binary_search([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):
self.assertEqual(
binary_search([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21),
5)
2016-06-22 15:23:42 -07:00
2016-09-07 21:33:20 -07:00
def test_identifies_value_missing(self):
with self.assertRaises(ValueError):
binary_search([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):
with self.assertRaises(ValueError):
binary_search([1, 3, 4, 6, 8, 9, 11], 0)
2016-09-07 21:33:20 -07:00
def test_value_larger_than_arrays_maximum(self):
with self.assertRaises(ValueError):
binary_search([1, 3, 4, 6, 8, 9, 11], 13)
2016-09-07 21:33:20 -07:00
def test_empty_array(self):
with self.assertRaises(ValueError):
binary_search([], 1)
2016-06-22 15:23:42 -07:00
2016-11-29 09:44:47 +01:00
2016-06-22 15:23:42 -07:00
if __name__ == '__main__':
unittest.main()