コード例 #1
0
def test_len_safe():
    """Return True or False if the test of len_safe passes/fails"""
    TEST_PASSED = True  # Assume the test will succeed
    my_dict = {'a': 23, 'b': 8}
    result = len_safe(my_dict)
    if result != 2:
        TEST_PASSED = False
    OBJECT = []
    result = len_safe(OBJECT)
    if result != 0:
        TEST_PASSED = False
    OBJECT2 = 0.25
    result = len_safe(OBJECT2)
    if result != -1:
        TEST_PASSED = False
    OBJECT3 = 'cat'
    result = len_safe(OBJECT3)
    if result != 3:
        TEST_PASSED = False
    OBJECT4 = ''
    result = len_safe(OBJECT4)
    if result != 0:
        TEST_PASSED = False
    ANIMALS = ['dog', 'cat', 'bird', 'cat', 'fish']
    result = len_safe(ANIMALS)
    if result != 5:
        TEST_PASSED = False
    cat = Cat()
    result = len_safe(cat)
    if result != -1:
        TEST_PASSED = False
    return TEST_PASSED
コード例 #2
0
def test_len_safe():
    """Return True/False if the test of len_safe passes/fails"""
    # Test 1, dictionary with 2 entries
    my_dict = {'a': 23, 'b': 8}
    result = len_safe(my_dict)
    if result != 2:
        return False

    # Test 2, integer should return -1
    result = len_safe(7)
    if result != -1:
        return False

    # Test 3, empty list, should return 0
    result = len_safe([])
    if result != 0:
        return False

    # All tests passed if we get here
    return True
コード例 #3
0
ファイル: HW3_test.py プロジェクト: xiaoxibianma/python1
def test_len_safe():
    # Test1
    list1 = [1, 2, 3, 4, 5, 6, 7, 8]
    if len_safe(list1) != 8:
        return False

    # Test2
    dic1 = {'1': 23, '2': 45, '3': 89, '4': "yoyo", '5': list1}
    if len_safe(dic1) != 5:
        return False

    # Test3
    if len_safe([]) != 0:
        return False

    # Test4
    if len_safe(8) != -1:
        return False
    else:
        return True
    """Return True/False if the test of len_safe passes/fails"""
コード例 #4
0
def test_len_safe():
    """Return True/False if the test of len_safe passes/fails"""
    my_dict = {'a': 23, 'b': 8}
    x = len_safe(my_dict)
    y = len_safe([])
    z = len_safe(0.25)
    n = len_safe(7)
    m = len_safe('cat')
    p = len_safe('')
    animals = ['dog', 'cat', 'bird', 'cat', 'fish']
    q = len_safe(animals)

    if x == 2:
        if y == 0:
            if z == -1:
                if n == -1:
                    if m == 3:
                        if p == 0:
                            if q == 5:
                                return True
    else:
        return False
コード例 #5
0
ファイル: HW3_cli.py プロジェクト: amiremadz/python-codes
    result = words_containing(sentence, 't')
    result.sort()
    gt = ['The', 'the']
    gt.sort()
    assert (result == gt)

    result = words_containing(sentence, 'o')
    result.sort()
    gt = ['cow', 'over', 'moon']
    gt.sort()
    assert (result == gt)

    ### Part2: len_safe
    my_dict = {'a': 23, 'b': 8}
    assert (len_safe(my_dict) == 2)

    assert (len_safe([]) == 0)

    assert (len_safe(0.25) == -1)

    assert (len_safe(7) == -1)

    assert (len_safe(None) == -1)

    assert (len_safe('cat') == 3)

    assert (len_safe('') == 0)

    animals = ['dog', 'cat', 'bird', 'cat', 'fish']
    assert (len_safe(animals) == 5)