def test_len(): """ Test function to test len method of the ListADT @post: Works without any error and exception if method len working fine @complexity: The best and worst case is O(1) """ x = ListADT() assert len(x) == 0, "Length should be 0 but is " + str(len(x)) x.insert(0, 2) assert len(x) == 1, "Length should be 1 but is " + str(len(x)) x.unsafe_set_array([1,2,3,3,4,5,5,6,7,6,8,None,None],11) assert len(x) == 11,"Length should be 11 but is" + str(len(x))
def test_str(): """ Test function to test str method of the ListADT @post: Works without any error and exception if method str working fine @complexity: The best O(1) and worst case is O(M) where M is the length of the string """ x = ListADT() assert str(x) == "", "Should be empty string but it is" + str(x) x.insert(0, 2) assert str(x) == "2\n", "Should be a 2 string but it is" + str(x) x.unsafe_set_array([1, 2, 3, None, None], 3) assert str(x) == "1\n2\n3\n", "Should be a 1 2 3 string but it is" + str(x)
def test_contains(): """ Test function to test contain method of the ListADT @post: Works without any error and exception if method contains working fine @complexity: The best and the worst case O(n) """ x = ListADT() assert (2 in x) == False, "Should be False as x is empty but it is" + str(x) x.insert(0, 2) assert (2 in x), "Should be True as x=[2] but it is" + str(x) x.unsafe_set_array([4, 2, 3, None, None], 3) assert (3 in x), "Should be True as x=[4,2,3] but it is" + str(x)
def test_insert(): """ Test function to test insert method of the ListADT @post: Works without any error and exception if method insert works correctly @complexity: The worst case is O(1) and worst case O(length) """ x = ListADT() x.unsafe_set_array([1, 2, 5, 6, 7, None, None], 5) y = ListADT() y.unsafe_set_array([1, 2, 3, 5, 6, 7, None, None], 6) x.insert(2, 3) assert (x == y), "Should be True but it is" + "x=" + str(x) + "y=" + str(y) x.insert(0, 0) y.unsafe_set_array([0, 1, 2, 3, 5, 6, 7, None, None], 7) assert (x == y), "Should be True but it is" + "x=" + str(x) + "y=" + str(y) try: x.insert(12,2) print("Should have raised IndexError") except IndexError: try: x.insert(1, 2) print("Should have raised Exception") except Exception: True