Sha256: 2b76bba4cd2d7441421c1870e06f1a8a9972f0d6c36ff426267304c4782f50b6

Contents?: true

Size: 1.48 KB

Versions: 139

Compression:

Stored size: 1.48 KB

Contents

import unittest

from binary_search import binary_search


class BinarySearchTests(unittest.TestCase):
    def test_finds_value_in_array_with_one_element(self):
        self.assertEqual(binary_search([6], 6), 0)

    def test_finds_value_in_middle_of_array(self):
        self.assertEqual(binary_search([1, 3, 4, 6, 8, 9, 11], 6), 3)

    def test_finds_value_at_beginning_of_array(self):
        self.assertEqual(binary_search([1, 3, 4, 6, 8, 9, 11], 1), 0)

    def test_finds_value_at_end_of_array(self):
        self.assertEqual(binary_search([1, 3, 4, 6, 8, 9, 11], 11), 6)

    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)

    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)

    def test_identifies_value_missing(self):
        self.assertRaises(ValueError, binary_search, [1, 3, 4, 6, 8, 9, 11], 7)

    def test_value_smaller_than_arrays_minimum(self):
        self.assertRaises(ValueError, binary_search, [1, 3, 4, 6, 8, 9, 11], 0)

    def test_value_larger_than_arrays_maximum(self):
        self.assertRaises(ValueError, binary_search, [1, 3, 4, 6, 8, 9, 11],
                          13)

    def test_empty_array(self):
        self.assertRaises(ValueError, binary_search, [], 1)


if __name__ == '__main__':
    unittest.main()

Version data entries

139 entries across 139 versions & 1 rubygems

Version Path
trackler-2.2.1.8 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.7 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.6 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.5 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.4 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.3 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.2 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.1 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.0 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.0.6 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.0.5 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.0.4 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.0.3 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.0.2 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.0.1 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.0.0 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.1.0.55 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.1.0.54 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.1.0.53 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.1.0.52 tracks/python/exercises/binary-search/binary_search_test.py