Sha256: 6b4c5aae72de479899d2c9dcb3c11ba35fa0c700fcadd3e14d32576c82a3cf52

Contents?: true

Size: 1.93 KB

Versions: 52

Compression:

Stored size: 1.93 KB

Contents

import unittest

from binary_search import binary_search


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0

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):
        with self.assertRaisesWithMessage(ValueError):
            binary_search([1, 3, 4, 6, 8, 9, 11], 7)

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

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

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

    # Utility functions
    def setUp(self):
        try:
            self.assertRaisesRegex
        except AttributeError:
            self.assertRaisesRegex = self.assertRaisesRegexp

    def assertRaisesWithMessage(self, exception):
        return self.assertRaisesRegex(exception, r".+")


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

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
trackler-2.2.1.160 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.159 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.158 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.157 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.156 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.155 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.154 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.153 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.152 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.151 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.150 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.149 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.148 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.147 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.146 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.145 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.144 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.143 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.142 tracks/python/exercises/binary-search/binary_search_test.py
trackler-2.2.1.141 tracks/python/exercises/binary-search/binary_search_test.py