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