Esempio n. 1
0
 def test_bin_search(self):
     """test bin_searc function. Edge cases include empty lists, lists of length 1, and cases
         where the target value is not found"""
     with self.assertRaises(ValueError):
         lab1.bin_search(1, 0, 0, None)
     self.assertEqual(lab1.bin_search(1, 0, 0, []), None)
     self.assertEqual(lab1.bin_search(1, 0, 0, [1]), 0)
     self.assertEqual(lab1.bin_search(2, 0, 0, [1]), None)
     x = list(range(2, 50000, 2))
     for i in range(1, 50000, 2):
         self.assertEqual(lab1.bin_search(i, 0, 2498, x), None)
     self.assertEqual(lab1.bin_search(3, 0, 4, [1, 2, 3, 4, 5]), 2)
     self.assertEqual(lab1.bin_search(4, 0, 4, [1, 2, 3, 4, 5]), 3)
     self.assertEqual(lab1.bin_search(2, 0, 4, [1, 2, 3, 4, 5]), 1)
     list_val = [0, 1, 2, 3, 4, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(4, 0, len(list_val) - 1, list_val), 4)
Esempio n. 2
0
 def test_bin_search(self):
     list_val1 = [1, 2, 3, 4]
     self.assertEqual(lab1.bin_search(3, 0, 3, list_val1), 2)
     list_val2 = [8, 8, 7, 4, 8, 2, 3]
     self.assertEqual(lab1.bin_search(4, 2, 6, list_val2), 1)
     self.assertEqual(lab1.bin_search(8, 2, 6, list_val2), 2)
     self.assertEqual(lab1.bin_search(8, 0, 6, list_val2), 0)
     list_val3 = []
     with self.assertRaises(ValueError):
         lab1.bin_search(4, 0, 6, list_val3)
Esempio n. 3
0
 def test_bin_search(self):
     #Testing a normal list
     list_val = [10, 12, 21, 34, 56, 79, 999]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(56, low, high, list_val), 4)
     #Testing negative numbers
     list_val = [-100, -90, -75, -60, -20, -5]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(-100, low, high, list_val), 0)
     #Testing negative numbers with ZERO
     list_val = [-100, -90, -75, -60, -20, -5, 0]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(0, low, high, list_val), 6)
     #Testing an empty list
     list_val = []
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(21, low, high, list_val), None)
     #Testing when target is in lower half
     list_val = [1, 2, 3, 4, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(2, low, high, list_val), 1)
     #Testing when target is in upper half
     list_val = [1, 2, 3, 4, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(9, low, high, list_val), 6)
     #Testing when target is in the middle
     list_val = [1, 2, 3, 4, 7, 8, 9, 10, 11]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(7, low, high, list_val), 4)
     #Testing when there are multiple targets
     list_val = [1, 2, 2, 2, 2, 2, 3, 4, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(2, low, high, list_val), 5)
     #Testing when the target is not in the list
     list_val = [1, 2, 3, 4, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(11, low, high, list_val), None)
     #Testing when the low and high bounds are mixed
     list_val = [1, 2, 3, 4, 7, 8, 9, 10]
     low = len(list_val) - 1
     high = 0
     with self.assertRaises(ValueError):
         lab1.bin_search(1, low, high, list_val)
     #Testing when the target is in the beginning
     list_val = [1, 2, 3, 4, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(1, low, high, list_val), 0)
     #Testing when the target is in the end
     list_val = [1, 2, 3, 4, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(10, low, high, list_val), 7)
     #Testing when the list is in decimal form
     list_val = [1.0, 2.0, 3.0, 4.0, 7.0, 8.0, 9.0, 10.0]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(4, low, high, list_val), 3)
     #Testing when the target is in decimal form
     list_val = [1.0, 2.0, 3.0, 4.0, 7.0, 8.0, 9.0, 10.0]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(4.0, low, high, list_val), 3)
     #Testing when the list is in negative decimal form
     list_val = [-10.0, -9.0, -8.0, -7.0]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(-7, low, high, list_val), 3)
Esempio n. 4
0
 def test_bin_search5(self):  # the target is not in the list
     list_val = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(12, 0,
                                      len(list_val) - 1, list_val), None)
Esempio n. 5
0
 def test_bin_search4(self):  # the target is the index of the mid
     list_val = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(2, 0, len(list_val) - 8, list_val), 2)
Esempio n. 6
0
 def test_bin_search3(
         self):  # testing the whole list and finding the 4 in the list
     list_val = [0, 1, 2, 3, 4, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 1
     self.assertEqual(lab1.bin_search(4, 0, len(list_val) - 1, list_val), 4)
Esempio n. 7
0
 def test_bin_search2(self):  #an empty list which should return none
     list_val = []
     low = 0
     high = 0
     self.assertEqual(lab1.bin_search(4, 0, 0, list_val), None)
Esempio n. 8
0
 def test_bin_search(self):  #a mid of 0 which should return the first index
     list_val = [0, 1, 2, 3, 4, 7, 8, 9, 10]
     low = 0
     high = len(list_val) - 5
     self.assertEqual(lab1.bin_search(0, 0, len(list_val) - 5, list_val), 0)