def test_basicfunctionality(self):
     self.assertEqual(binary_search(self.input,5),5)
     self.assertEqual(binary_search([5], 5), 0)
     self.assertEqual(binary_search([5], 4), -1)
     self.assertEqual(binary_search(self.input, 5, 1,3), -1)
     self.assertEqual(binary_search(self.input, 2, 1,3), 2)
     self.assertEqual(binary_search(self.input, 2, 3, 1), -1)
     self.assertEqual(binary_search(self.input, 2, 2, 2), 2)
     self.assertEqual(binary_search(self.input, 5, 2, 2), -1)
 def test_badinput(self):
     import numpy as np
     self.assertEqual(binary_search([1,2,np.inf], 2), 1)
     self.assertEqual(binary_search([1,2,np.inf], np.inf), 2)
     self.assertEqual(binary_search([],1),-1)
     with self.assertRaises(TypeError):
         binary_search(['a',3],1)
     with self.assertRaises(TypeError):
         binary_search(['a',3],'a')
 def test_mymath(self):
     # Test empty array
     self.assertEqual(binary_search([], 2),  -1)
     # Test array with nans
     self.assertEqual(binary_search(['nan', 1, 2], 2), 2)
     # Test array of length 1
     self.assertEqual(binary_search([1], 1), 0)
     # Test array of length 2
     self.assertEqual(binary_search([0,10], 10), 1)
     # Test needle greater than the range of the array
     self.assertEqual(binary_search(range(10), 11), -1)
     # Test needle at extreme of array
     self.assertEqual(binary_search(range(5), 4), 4)
Exemple #4
0
 def test_wierd_inputs(self):
     # Test NaN
     test_list = list(range(9)) + [np.nan]
     self.assertEqual(binary_search(test_list, np.nan), (len(test_list) - 1) // 2)  # Always returns midpoint! Undesirable?
     
     # Test empty
     self.assertEqual(binary_search([], 12), -1)
     
     # Test left right inversion
     self.assertEqual(binary_search(test_list, 5, 8, 2), -1)
     self.assertEqual(binary_search(test_list, 5, 1, 11), 5)
     with self.assertRaises(IndexError):
         binary_search(test_list, 21, 1, 15)
    def manual_binsearch():
        print("Think of a number between 0 and 100.")
        print("I will guess it after you answer 7 simple yes/no questions.")

        qnum = 0
        def manual_testfn(tested_value):
            nonlocal qnum
            qnum += 1
            for _ in range(0,3):
                question = str(qnum) + ": Is your number lower than "+str(tested_value)+"? [y/n]: "
                input_var = input(question)
                if input_var.lower().startswith('y'):
                    return True
                if input_var.lower().startswith('n'):
                    return False
                else:
                    print("Invalid answer. Please answer y or n.")
            print("Too many invalid answers were given. Exiting..")
            exit()

        result = binary_search(manual_testfn, 0, 100)
        print("Your number was " + str(result))
def test_big():
    assert binary_search([2, 3, 4, 5], 5) == 3
def test_binary_too_small():
    assert binary_search([2, 3], 1) == -1
Exemple #8
0
def test_1():
    assert binary_search(input, 5) == 5
Exemple #9
0
def test_needle_1():
    assert binary_search(input, 5, 2, 2) == -1
Exemple #10
0
def test_missing_args():
    with raises(TypeError):
        binary_search([1, 2])
Exemple #11
0
def test_4():
    assert binary_search([5], 4) == -1
Exemple #12
0
def test_needle_5():
    assert binary_search(input, 9) == 9
Exemple #13
0
def test_needle_4():
    assert binary_search(input, 2, 3, 1) == -1
Exemple #14
0
def test_needle_3():
    assert binary_search(input, 2, 1,3) == 2
Exemple #15
0
def test_needle_2():
    assert binary_search(input, 2, 2, 2) == 2
Exemple #16
0
 def test_sort(self):
     input  = [2,1,3,4,0,23,18]
     with self.assertRaises(AssertionError):
         binary_search(input,2)
Exemple #17
0
def test_2():
    assert binary_search(input, 4.5) == -1
Exemple #18
0
def test_both():
    assert binary_search([5], 7) == -1
Exemple #19
0
def test_6():
    binary_search(input, 11) == -1
Exemple #20
0
def test_1():
    assert binary_search(input, 5) == 5
Exemple #21
0
def test_other():
    assert binary_search(['hello', 'there', 'you', 'abc'], 'abc') == -1
Exemple #22
0
def test_empty():
    assert binary_search([], 1) == -1
Exemple #23
0
def test_needle_3():
    assert binary_search(input, 2, 1, 3) == 2
Exemple #24
0
def test_oneelem():
    assert binary_search([9], 9) == 0
Exemple #25
0
def test_both():
    assert binary_search([5], 7) == -1
Exemple #26
0
def test_twoelem_first():
    assert binary_search([10, 50], 10) == 0
def test_middle_needle():
    assert binary_search([2, 4], 3) == -1
Exemple #28
0
def test_twoelem_second():
    assert binary_search([10, 50], 50) == 1
def test_binary_search():
    input = list(range(10))
    assert binary_search(input, 5) == 5
Exemple #30
0
def test_negative():
    assert binary_search([-10.5, -5.2, 10, 20.3, 30.9], -5.2), 1
Exemple #31
0
 def test_overflow(self):
     input = list(range(10))
     self.assertEqual(binary_search(input,np.inf+1),-1)
Exemple #32
0
def test_multiple_res():
    res = binary_search([1, 10, 10, 10, 50], 10)
    assert (res >=1 and res <= 3)
Exemple #33
0
 def test_binsearch(self):
     input = list(range(10))
     self.assertEqual(binary_search(input,5), 5)
Exemple #34
0
def test_lt_range(input_data):
    assert binary_search(input_data, -10) == -1
Exemple #35
0
def test_3():
    assert binary_search([5], 5) == 0
Exemple #36
0
def test_gt_range(input_data):
    assert binary_search(input_data, 10000) == -1
Exemple #37
0
def test_5():
    binary_search([], 4) == -1
Exemple #38
0
def test_not_found_inrange(input_data):
    assert binary_search(input_data, 11) == -1
Exemple #39
0
def test_not_alist():
    with raises(TypeError):
        binary_search('a', 1)
Exemple #40
0
def test_unsorted():
    assert binary_search([10, 5, 3, 5], 4) == -1
Exemple #41
0
def test_not_numeric():
    assert binary_search(['a', 'b'], 'b') == 1
Exemple #42
0
def test_weird_data():
    with raises(TypeError):
        binary_search([np.nan(), None, 0.1, 0.5], 0.5)
Exemple #43
0
def test_nan():
    assert binary_search([np.nan, 0, 2, 3, 4], np.nan) == 2
Exemple #44
0
def test_name_NAN():
    with raises(NameError):
        binary_search([NAN, 1], 3)
Exemple #45
0
def test_needle_2():
    assert binary_search(input, 2, 2, 2) == 2
Exemple #46
0
def test_left_equal_right_diff_needle():
    assert binary_search(input, 5, 2, 2) == -1            
Exemple #47
0
def test_needle_4():
    assert binary_search(input, 2, 3, 1) == -1
Exemple #48
0
 def test_Nan(self):
     input = [np.nan]*5
     with self.assertRaises(TypeError):
         binary_search(input,2)
Exemple #49
0
def test_needle_5():
    assert binary_search(input, 9) == 9
Exemple #50
0
 def test_numeric(self):
     input=['a']*5
     with self.assertRaises(TypeError):
         binary_search(input,3)
def test_binary_too_big():
    assert binary_search([2, 3], 4) == -1
Exemple #52
0
 def test_empty(self):
     self.assertEqual(binary_search([],1),-1)
def test_binary_NaN():
    assert binary_search([2, 3, 'a'], 1) == -1
Exemple #54
0
 def test_boundary(self):
     input = list(range(10))
     self.assertEqual(binary_search(input,1,3,1),-1)
def test_small():
    assert binary_search([2, 3, 4, 5], 2) == 0
Exemple #56
0
 def test_needle_oversize(self):
     input = list(range(10))
     self.assertEqual(binary_search(input,11),-1)
     self.assertEqual(binary_search(input,-1),-1)
def test_doubles():
    assert (binary_search([2, 2], 2) == 0 or binary_search([2, 2], 2) == 1)
Exemple #58
0
 def test_extremes(self):
     input = list(range(10))
     self.assertEqual(binary_search(input,9),9)
     self.assertEqual(binary_search(input,0),0)
def test_empty_array():
    assert binary_search([], 4) == -1
Exemple #60
0
 def test_multiple(self):
     input = [3]*10
     self.assertEqual(binary_search(input,3),0)