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