コード例 #1
0
def test_list_with_same_value(helper):
    length = helper.get_pos_int()
    val = helper.get_value()
    ll = LinkedList()
    # test add_end
    for _ in range(length):
        ll.add_end(val)
    # test add_front
    for _ in range(length):
        ll.add_front(val)
    # test __setitem__()
    ll[1] = val
    assert ll == ll.reverse()
    assert ll == ll.copy()
    assert not ll.is_empty()
    assert len(ll) == 2 * length
    assert ll.count(val) == 2 * length
    assert ll.to_list() == [val] * (2 * length)
    # test split
    left_list, right_list = ll.split(length)
    assert len(left_list) == len(right_list) == length
    # test clear
    left_list.clear()
    right_list.clear()
    assert len(left_list) == len(right_list) == 0
    # test remove
    for i in range(length):
        if i > length // 2:
            ll.remove_end()
        else:
            ll.remove_front()
    assert len(ll) == ll.count(val) == length
    ll.remove(val, all=True)
    assert ll.is_empty()
    assert len(ll) == 0
コード例 #2
0
def test_list_with_one_element(helper):
    val = helper.get_value()
    ll = LinkedList()
    ll.insert(0, val)
    assert isinstance(ll._head, Node)
    assert ll._head.get_data() == val
    assert ll._head.get_next() is None
    assert len(ll) == 1
    assert not ll.is_empty()
    assert val in ll
    assert [item for item in ll] == [val]
    assert ll.to_list() == [val]
    assert ll == ll.copy()
    assert ll == ll.reverse()
    # ==================== test rotate ====================
    assert ll == ll.rotate_left(helper.get_pos_int(), inplace=False)
    assert ll == ll.rotate_right(helper.get_pos_int(), inplace=False)
    # ==================== test operators ====================
    assert ll != LinkedList()
    assert ll > LinkedList()
    assert ll >= LinkedList()
    assert LinkedList() < ll
    assert LinkedList() <= ll
    # ==================== test add/remove ====================
    new_value = helper.get_value()
    ll.add_front(new_value)
    ll.remove_front()
    ll.add_end(new_value)
    ll.remove_end()
    assert ll == LinkedList([val])
    # ==================== test insert/split ====================
    with pytest.raises(IndexError):
        ll.insert(2, helper.get_value())
    with pytest.raises(IndexError):
        ll.insert(-1, helper.get_value())
    with pytest.raises(IndexError):
        ll.split(helper.get_pos_int(a=2))
コード例 #3
0
def test_split_method(helper):
    lst = helper.get_list(length=100)
    ll = LinkedList(lst)
    for i in range(len(lst)):
        # test left list
        left_list, right_list = ll.split(i)
        assert isinstance(left_list, LinkedList)
        assert left_list.to_list() == lst[:i]
        assert left_list.copy().to_list() == lst[:i]
        assert left_list.reverse().to_list() == lst[:i][::-1]
        # test right list
        assert isinstance(right_list, LinkedList)
        assert right_list.to_list() == lst[i:]
        assert right_list.copy().to_list() == lst[i:]
        assert right_list.reverse().to_list() == lst[i:][::-1]
    ll.add_front(0)
    ll.add_end("apple")
    assert ll._length == len(ll) == len(lst) + 2
    assert ll.to_list() == [0] + lst + ["apple"]
コード例 #4
0
def test_empty_linked_list(helper):
    EMPTY = "┌─\n│\n└─"  # represents empty LinkedList
    ll = LinkedList([])
    assert str(ll) == EMPTY
    assert ll._length == len(ll) == 0
    assert ll.is_empty()
    assert ll.to_list() == []
    assert [_ for _ in ll] == []
    assert len(ll.copy()) == 0
    assert len(ll.reverse()) == 0
    # =================== test operators ====================
    assert LinkedList() == LinkedList()
    assert ll == ll.copy()
    assert ll == ll.reverse()
    assert LinkedList() != LinkedList([helper.get_value()])
    assert LinkedList() < LinkedList([helper.get_value()])
    assert LinkedList() <= LinkedList([helper.get_value()])
    assert LinkedList([helper.get_value()]) > LinkedList()
    assert LinkedList([helper.get_value()]) >= LinkedList()
    # ==================== test count ====================
    assert ll.count(0) == 0
    assert ll.count(None) == 0
    assert ll.count(helper.get_value()) == 0
    assert ll.count(Node(helper.get_value())) == 0
    # ==================== test __contains__ ===================
    assert None not in ll
    assert Node(helper.get_value()) not in ll
    assert 0 not in ll
    assert helper.get_value() not in ll
    assert LinkedList() not in ll
    # ==================== test split ====================
    left_list, right_list = ll.split(0)
    assert str(left_list) == str(right_list) == EMPTY
    with pytest.raises(TypeError):
        ll.split(helper.get_string())
    with pytest.raises(TypeError):
        ll.split(helper.get_float())
    with pytest.raises(TypeError):
        ll.split(True)
    with pytest.raises(IndexError):
        ll.split(-1)
    with pytest.raises(IndexError):
        ll.split(helper.get_int())
    # ==================== test rotate ====================
    assert ll.rotate_left(helper.get_pos_int(), inplace=False) == ll
    assert ll.rotate_right(helper.get_pos_int(), inplace=False) == ll
    assert len(ll.rotate_left(helper.get_pos_int(), inplace=False)) == 0
    assert len(ll.rotate_right(helper.get_pos_int(), inplace=False)) == 0
    with pytest.raises(TypeError):
        ll.rotate_left(helper.get_string())
    with pytest.raises(TypeError):
        ll.rotate_right(helper.get_float())
    with pytest.raises(TypeError):
        ll.rotate_left([])
    with pytest.raises(ValueError):
        ll.rotate_left(helper.get_neg_int())
    with pytest.raises(ValueError):
        ll.rotate_right(helper.get_neg_int())
    # ==================== test remove/del ====================
    ll.remove_front()  # shouldn't raise any Error
    ll.remove_end()  # shouldn't raise any Error
    ll.remove(helper.get_value())
    ll.remove(helper.get_value(), False)
    with pytest.raises(TypeError):
        ll.remove(helper.get_value(), all=helper.get_string(1))
    with pytest.raises(IndexError):
        del ll[0]
    with pytest.raises(IndexError):
        del ll[helper.get_pos_int()]
    with pytest.raises(IndexError):
        del ll[helper.get_neg_int()]
    # ==================== test __getitem__ ====================
    with pytest.raises(IndexError):
        _ = ll[0]
    with pytest.raises(IndexError):
        _ = ll[helper.get_pos_int()]
    with pytest.raises(IndexError):
        _ = ll[helper.get_neg_int()]
    assert LinkedList() == ll[0:10]
    # ==================== test insert/set ====================
    with pytest.raises(IndexError):
        ll.insert(helper.get_pos_int(), helper.get_value())
    with pytest.raises(IndexError):
        ll.insert(helper.get_neg_int(), helper.get_value())
    with pytest.raises(IndexError):
        ll[0] = helper.get_float()
    with pytest.raises(IndexError):
        ll[helper.get_int()] = Node(helper.get_float())
    with pytest.raises(ValueError):
        ll.insert(0, None)