Ejemplo n.º 1
0
def test__get(constructor_arg):
    ll = LinkedList(constructor_arg)
    if constructor_arg:
        for index, item in enumerate(constructor_arg):
            assert isinstance(ll._get(index), LinkedListNode)
            assert ll._get(index).value == item
    else:
        assert ll.head is None
Ejemplo n.º 2
0
def test_insert(constructor_arg):
    max_index = len(constructor_arg) if constructor_arg else 0
    idx = random.randint(0, max_index)

    # Test a valid index
    ll = LinkedList(constructor_arg)
    ll.insert(idx, 'test')
    assert ll._get(idx) == LinkedListNode('test')

    # Test some invalid cases
    if max_index:
        ll = LinkedList(constructor_arg)
        with pytest.raises(IndexError):
            ll.insert(2 * max_index, 'test')

        ll = LinkedList(constructor_arg)
        with pytest.raises(IndexError):
            ll.insert(-max_index, 'test')

    ll = LinkedList(constructor_arg)
    with pytest.raises(IndexError):
        ll.insert('Um, an index', 'test')

    ll = LinkedList(constructor_arg)
    with pytest.raises(IndexError):
        ll.insert(None, 'test')
Ejemplo n.º 3
0
def test_pop(constructor_arg):
    max_index = len(constructor_arg) - 1 if constructor_arg else 0
    idx = random.randint(0, max_index)

    ll = LinkedList(constructor_arg)

    if len(ll) == 0:
        with pytest.raises(IndexError):
            ll.pop(idx)
    else:
        node_to_pop = ll._get(idx)
        assert ll.pop(idx) == node_to_pop.value

    ll = LinkedList(constructor_arg)
    with pytest.raises(IndexError):
        ll.pop(2 * max_index)

    ll = LinkedList(constructor_arg)
    with pytest.raises(IndexError):
        ll.pop(-max_index)

    ll = LinkedList(constructor_arg)
    with pytest.raises(IndexError):
        ll.pop('Um, an index')

    ll = LinkedList(constructor_arg)
    if len(ll) == 0:
        with pytest.raises(IndexError):
            ll.pop()
    else:
        node_to_pop = ll.head
        assert ll.pop() == node_to_pop.value
Ejemplo n.º 4
0
def test_append(constructor_arg):
    ll = LinkedList(constructor_arg)
    ll.append('test')
    assert ll._get(len(ll) - 1) == LinkedListNode('test')