class BinarySearchTest(TestCase): """Get the index of the item with an expected number of loops in\ array [1, 2 . . . 20] Returns a dictionary containing {count: value, index: value} """ def setUp(self): self.one_to_twenty = BinarySearch(20, 1) self.two_to_forty = BinarySearch(20, 2) self.ten_to_thousand = BinarySearch(100, 10) def test_small_list_search(self): search = self.one_to_twenty.search(16) self.assertGreater(5, search['count'], msg='should return {count: 4, index: 15} for 16') self.assertEqual(15, search['index'], msg='should return {count: 4, index: 15} for 16') def test_medium_list_search(self): search1 = self.two_to_forty.search(16) search2 = self.two_to_forty.search(40) search3 = self.two_to_forty.search(33) self.assertGreater(5, search1['count'], msg='should return {count: 4, index: 7} for 16') self.assertEqual(7, search1['index'], msg='should return {count: 4, index: 7} for 16') self.assertEqual(0, search2['count'], msg='should return {count: 0, index: 19} for 40') self.assertEqual(19, search2['index'], msg='should return {count: 5, index: 19} for 40')
def test_binary_search(): numbers = [0,3,5,12,17,23,34,78,134] sut = BinarySearch(numbers) eq_(sut.search(0), 0) eq_(sut.search(3), 1) eq_(sut.search(17), 4) eq_(sut.search(134), 8) eq_(sut.search(135), -1)
def test_binary_search_medium_list(): # test large list new_search = BinarySearch(10, 3) assert new_search.search(5) == {'work_done': 2}
def test_binary_search_medium_list(): # test medium list new_search = BinarySearch(5, 2) assert new_search.search(8) == {'work_done': 2}
def test_binary_search_small_list(): # test small list new_search = BinarySearch(3, 1) assert new_search.search(3) == {'work_done': 1}
class TestBinarySearch(unittest.TestCase): """ Test cases for finding if the search function gets the index of the item with an expected number of loops in an array and returns a dictionary containing {count: value, index: value} """ # Instantiating objects for using in the tests def setUp(self): self.one_to_twenty = BinarySearch(20, 1) self.two_to_forty = BinarySearch(20, 2) self.ten_to_thousand = BinarySearch(100, 10) # Testing if the function returns {count: 4, index: 15} for input 16 in a small sized list def test_small_list_search(self): search = self.one_to_twenty.search(16) self.assertGreater(5, search['count'], msg='should return {count: 4, index: 15} for 16') self.assertEqual(15, search['index'], msg='should return {count: 4, index: 15} for 16') # Testing if the function returns the right results for different inputs in a medium sized list def test_medium_list_search(self): search1 = self.two_to_forty.search(16) search2 = self.two_to_forty.search(40) search3 = self.two_to_forty.search(33) self.assertGreater(5, search1['count'], msg='should return {count: 4, index: 7} for 16') self.assertEqual(7, search1['index'], msg='should return {count: 4, index: 7} for 16') self.assertEqual(5, search2['count'], msg='should return {count: 5, index: 19} for 40') self.assertEqual(19, search2['index'], msg='should return {count: 5, index: 19} for 40') self.assertGreater(4, search3['count'], msg='should return {count: 3, index: -1} for 33') self.assertEqual(-1, search3['index'], msg='should return {count: 3, index: -1} for 33') # Testing if the function returns the right results for different inputs in a large sized list def test_large_list_search(self): search1 = self.ten_to_thousand.search(40) search2 = self.ten_to_thousand.search(880) search3 = self.ten_to_thousand.search(10000) self.assertGreater( 7, search1['count'], msg='should return {count: # <= 7, index: 3} for 40') self.assertEqual(3, search1['index'], msg='should return {count: # <= 7, index: 3} for 40') self.assertGreater( 4, search2['count'], msg='should return {count: # <= 3, index: 87} for 880') self.assertEqual( 87, search2['index'], msg='should return {count: # <= 3, index: 87} for 880') self.assertGreater(7, search3['count'], msg='should return {count: 3, index: -1} for 10000') self.assertEqual(-1, search3['index'], msg='should return {count: 3, index: -1} for 10000')