def test_nothing_is_found_when_the_left_and_right_bounds_cross(self): with self.assertRaises(ValueError) as err: find([1, 2], 0) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "value not in array")
def test_a_value_larger_than_the_array_s_largest_value_is_not_found(self): with self.assertRaises(ValueError) as err: find([1, 3, 4, 6, 8, 9, 11], 13) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "value not in array")
def test_nothing_is_found_in_an_empty_array(self): with self.assertRaises(ValueError) as err: find([], 1) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "value not in array")
def test_identifies_that_a_value_is_not_included_in_the_array(self): with self.assertRaises(ValueError) as err: find([1, 3, 4, 6, 8, 9, 11], 7) self.assertEqual(type(err.exception), ValueError) self.assertEqual(err.exception.args[0], "value not in array")
def test_in_order(self): "A test method, which must start with test_" # You can have many test vectors. # Each one contains at least the input that you # feed to your function, or whatever code you want to test. # You can group a lot of them together, # if you're testing the same feature. # Here we have two test vectors, # inside one test method, which must start with "test_" in_order = [([1, 2, 3, 4], 3), ([-4, 3, 6, 19], 2)] # It's always a good idea to have a different technique # for testing the result, rather than recalculating # the same answer with the same technique. # Sometimes, that means manually creating the answers, # and include it in the test vector, # but other times, you can have simpler, but slower ways, # of getting the same results. # Or it's easier to check the result than to create it. for collection, target in in_order: index = find(collection, target) if (index >= 0): # For example, with finding, # you can check if the element is there. # For testing, you'll use "assert*" # methods of the class a lot. # They check if a condition is met. # For most variations, like here, # they check if a boolean or relationship # is True. self.assertEqual(target, collection[index]) else: # Or if it really can't be found self.assertNotIn(target, collection)
def test_bad_order(self): # You can also test if your code reports errors correctly, # by using assertRaises. out_of_order = [([1, 3, 2, 4], 3), ([19, -4, 3, 6], 2)] for collection, target in out_of_order: with self.assertRaises(UnorderedException): index = find(collection, target)
def test_value_larger_than_arrays_maximum(self): with self.assertRaisesWithMessage(ValueError): find([1, 3, 4, 6, 8, 9, 11], 13)
def test_nothing_is_found_when_the_left_and_right_bounds_cross(self): with self.assertRaisesWithMessage(ValueError): find([1, 2], 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_1(self): self.assertEqual(binary_search.find([0, 1, 21, 33, 45, 45, 61, 71, 72, 73], 33), 3)
def test_finds_value_in_array_with_one_element(self): self.assertEqual(find([6], 6), 0)
def test_empty_array(self): with self.assertRaisesWithMessage(ValueError): find([], 1)
def test_value_smaller_than_arrays_minimum(self): with self.assertRaisesWithMessage(ValueError): find([1, 3, 4, 6, 8, 9, 11], 0)
def test_binary_search_returns_first_occurence_of_item_from_longer_list(): assert_that( find('m', [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'l', 'm', 'm', 'o', 'p' ]), equal_to(11))
def test_binary_search_returns_first_occurence_of_item(): assert_that(find('b', ['a', 'b', 'b']), equal_to(1))
def test_binary_search_returns_negative_if_item_not_present_in_longer_list(): assert_that(find('e', ['a', 'b', 'c', 'd']), equal_to(-1))
def test_binary_search_returns_negative_if_item_not_present(): assert_that(find('a', ['b']), equal_to(-1))
def test_nothing_found_when_value_is_outside_the_range_given_by_array(self): ary = [1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634] self.assertEqual(find(ary, 1000, throw=False), 0) self.assertEqual(find(ary, -10, throw=False), 0)
def test_a_value_smaller_than_the_array_s_smallest_value_is_not_found_no_execp(self): self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 0, throw=False), 0)
def test_identifies_that_a_value_is_not_included_in_the_array_no_execp(self): self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 7, throw=False), 0)
def test_finds_value_in_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_value_missing(self): with self.assertRaisesWithMessage(ValueError): find([1, 3, 4, 6, 8, 9, 11], 7)
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_end_of_an_array(self): self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 11), 6)
def test_nothing_is_found_when_left_and_right_bounds_cross(self): with self.assertRaisesWithMessage(ValueError): find([1, 2], 0)
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_binary_search_finds_items_d(): assert_that(find('d', ['a', 'b', 'c', 'd']), equal_to(3))
from read_csv import read_csv from operator import attrgetter from binary_search import find # setting variable to chess player's attribute by_lname = attrgetter('lname') arr = read_csv('chess-players.csv') arr.sort(key=by_lname) # Invoke find method from binary_search.py result = find(arr, value='Zhao', key=by_lname) if (result): print("Player found in the list") print( f'First Name: {result.fname}, Last Name: {result.lname}, Country: {result.country}, Born: {result.born}, ' f'Died: {result.died}') else: print("Not found Player")
def test_binary_search_returns_negative_on_empty_collection(): assert_that(find('a', []), equal_to(-1))
def test_finds_a_value_at_the_beginning_of_an_array(self): self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 1), 0)
from read_csv import read_csv from chess_player import Chessplayer from operator import attrgetter from binary_search import find # setting variable to chess player's attribute by_lname = attrgetter('lname') arr = read_csv('chess-players.csv') arr.sort(key=by_lname) result = find(arr, value='Šulskis', key=by_lname) if (result): print("Player found in the list") print(f'{result}') else: print("Not found Player")
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_value_in_middle_of_array(self): self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 6), 3)
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_finds_value_at_beginning_of_array(self): self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 1), 0)
def test_nothing_is_found_in_an_empty_array(self): with self.assertRaisesWithMessage(ValueError): find([], 1)
def test_finds_value_at_end_of_array(self): self.assertEqual(find([1, 3, 4, 6, 8, 9, 11], 11), 6)
def test_finds_a_value_in_an_array_with_one_element(self): self.assertEqual(find([6], 6), 0)
def test_finds_value_in_array_of_odd_length(self): self.assertEqual( find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144), 9)