예제 #1
0
def test_single_member():

    assert binary_search.binary_search([7], misc_tools.identity_function, 7,
                                       binary_search.LOW) == 7

    assert binary_search.binary_search([7], misc_tools.identity_function, 7,
                                       binary_search.HIGH) == 7

    assert binary_search.binary_search([7], misc_tools.identity_function, 7,
                                       binary_search.HIGH_IF_BOTH) == 7

    assert binary_search.binary_search([7], misc_tools.identity_function, 7,
                                       binary_search.LOW_IF_BOTH) == 7

    assert binary_search.binary_search([7], misc_tools.identity_function, 7,
                                       binary_search.EXACT) == 7

    assert binary_search.binary_search([7], misc_tools.identity_function, 7,
                                       binary_search.BOTH) == (7, 7)

    assert binary_search.binary_search([7], misc_tools.identity_function, 7,
                                       binary_search.CLOSEST) == 7

    assert binary_search.binary_search([7], misc_tools.identity_function, 7,
                                       binary_search.CLOSEST_IF_BOTH) == 7

    assert binary_search.binary_search([7], misc_tools.identity_function, 7,
                                       binary_search.LOW_OTHERWISE_HIGH) == 7

    assert binary_search.binary_search([7], misc_tools.identity_function, 7,
                                       binary_search.HIGH_OTHERWISE_LOW) == 7
예제 #2
0
def test():
    '''Test the basic workings of `binary_search`.'''
    my_list = [0, 1, 2, 3, 4]

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       3, binary_search.EXACT) == 3

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       3.2, binary_search.CLOSEST) == 3

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       3.2, binary_search.LOW) == 3

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       3.2, binary_search.HIGH) == 4

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       3.2, binary_search.BOTH) == (3, 4)

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       -5, binary_search.BOTH) == (None, 0)

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       -5, binary_search.LOW) == None

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       -5, binary_search.HIGH) == 0

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       -5,
                                       binary_search.HIGH_OTHERWISE_LOW) == 0

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       -5,
                                       binary_search.LOW_OTHERWISE_HIGH) == 0

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       100, binary_search.BOTH) == (4, None)

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       100, binary_search.LOW) == 4

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       100, binary_search.HIGH) == None

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       100,
                                       binary_search.LOW_OTHERWISE_HIGH) == 4

    assert binary_search.binary_search(my_list, misc_tools.identity_function,
                                       100,
                                       binary_search.HIGH_OTHERWISE_LOW) == 4

    assert binary_search.binary_search_by_index([(number * 10)
                                                 for number in my_list],
                                                misc_tools.identity_function,
                                                32,
                                                binary_search.BOTH) == (3, 4)

    assert binary_search.binary_search([], misc_tools.identity_function, 32,
                                       binary_search.BOTH) == (None, None)

    assert binary_search.binary_search(
        [],
        misc_tools.identity_function,
        32,
    ) == None
예제 #3
0
def test_single_member():
    
    assert binary_search.binary_search(
        [7],
        7,
        misc_tools.identity_function,
        binary_search.LOW
    ) == 7
    
    assert binary_search.binary_search(
        [7],
        7,
        misc_tools.identity_function,
        binary_search.HIGH
    ) == 7
    
    assert binary_search.binary_search(
        [7],
        7,
        misc_tools.identity_function,
        binary_search.HIGH_IF_BOTH
    ) == 7
    
    assert binary_search.binary_search(
        [7],
        7,
        misc_tools.identity_function,
        binary_search.LOW_IF_BOTH
    ) == 7
    
    assert binary_search.binary_search(
        [7],
        7,
        misc_tools.identity_function,
        binary_search.EXACT
    ) == 7
    
    assert binary_search.binary_search(
        [7],
        7,
        misc_tools.identity_function,
        binary_search.BOTH
    ) == (7, 7)
    
    assert binary_search.binary_search(
        [7],
        7,
        misc_tools.identity_function,
        binary_search.CLOSEST
    ) == 7
    
    assert binary_search.binary_search(
        [7],
        7,
        misc_tools.identity_function,
        binary_search.CLOSEST_IF_BOTH
    ) == 7
    
    assert binary_search.binary_search(
        [7],
        7,
        misc_tools.identity_function,
        binary_search.LOW_OTHERWISE_HIGH
    ) == 7
    
    assert binary_search.binary_search(
        [7],
        7,
        misc_tools.identity_function,
        binary_search.HIGH_OTHERWISE_LOW
    ) == 7
    
예제 #4
0
def test():
    '''Test the basic workings of `binary_search`.'''
    my_list = [0, 1, 2, 3, 4]
    
    assert binary_search.binary_search(
        my_list,
        3,
        misc_tools.identity_function,
        binary_search.EXACT
    ) == 3
    
    assert binary_search.binary_search(
        my_list,
        3.2,
        misc_tools.identity_function,
        binary_search.CLOSEST
    ) == 3
    
    assert binary_search.binary_search(
        my_list,
        3.2,
        misc_tools.identity_function,
        binary_search.LOW
    ) == 3
    
    assert binary_search.binary_search(
        my_list,
        3.2,
        misc_tools.identity_function,
        binary_search.HIGH
    ) == 4
    
    assert binary_search.binary_search(
        my_list,
        3.2,
        misc_tools.identity_function,
        binary_search.BOTH
    ) == (3, 4)
    
    assert binary_search.binary_search(
        my_list,
        -5,
        misc_tools.identity_function,
        binary_search.BOTH
    ) == (None, 0)
    
    assert binary_search.binary_search(
        my_list,
        -5,
        misc_tools.identity_function,
        binary_search.LOW
    ) == None
    
    assert binary_search.binary_search(
        my_list,
        -5,
        misc_tools.identity_function,
        binary_search.HIGH
    ) == 0
    
    assert binary_search.binary_search(
        my_list,
        -5,
        misc_tools.identity_function,
        binary_search.HIGH_OTHERWISE_LOW
    ) == 0
    
    assert binary_search.binary_search(
        my_list,
        -5,
        misc_tools.identity_function,
        binary_search.LOW_OTHERWISE_HIGH
    ) == 0
    
    assert binary_search.binary_search(
        my_list,
        100,
        misc_tools.identity_function,
        binary_search.BOTH
    ) == (4, None)
    
    assert binary_search.binary_search(
        my_list,
        100,
        misc_tools.identity_function,
        binary_search.LOW
    ) == 4
    
    assert binary_search.binary_search(
        my_list,
        100,
        misc_tools.identity_function,
        binary_search.HIGH
    ) == None
    
    assert binary_search.binary_search(
        my_list,
        100,
        misc_tools.identity_function,
        binary_search.LOW_OTHERWISE_HIGH
    ) == 4
    
    assert binary_search.binary_search(
        my_list,
        100,
        misc_tools.identity_function,
        binary_search.HIGH_OTHERWISE_LOW
    ) == 4

    assert binary_search.binary_search_by_index(
        [(number * 10) for number in my_list],
        32,
        misc_tools.identity_function,
        binary_search.BOTH
    ) == (3, 4)
    
    assert binary_search.binary_search(
        [], 
        32,
        misc_tools.identity_function,
        binary_search.BOTH
    ) == (None, None)
    
    assert binary_search.binary_search(
        [], 
        32,
        misc_tools.identity_function,
    ) == None