def test_SinglyCircularLinkedList():
    random.seed(1000)
    scll = SinglyCircularLinkedList()
    assert raises(IndexError, lambda: scll[2])
    scll.appendleft(5)
    scll.append(1)
    scll.appendleft(2)
    scll.append(3)
    scll.insert_after(scll[1], 4)
    scll.insert_after(scll[-1], 6)
    scll.insert_at(0, 2)
    scll.insert_at(-1, 9)
    scll.extract(2)
    assert scll.popleft().key == 2
    assert scll.popright().key == 6
    assert scll.search(-1) is None
    scll[-2].key = 0
    assert str(scll) == "['2', '4', '1', '0', '9']"
    assert len(scll) == 5
    assert raises(IndexError, lambda: scll.insert_at(6, None))
    assert raises(IndexError, lambda: scll.extract(20))
    scll_copy = DoublyCircularLinkedList()
    for i in range(scll.size):
        scll_copy.append(scll[i])
    for i in range(len(scll)):
        if i % 2 == 0:
            scll.popleft()
        else:
            scll.popright()
    assert str(scll) == "[]"
    for _ in range(len(scll_copy)):
        index = random.randint(0, len(scll_copy) - 1)
        scll_copy.extract(index)
    assert str(scll_copy) == "[]"
    assert raises(ValueError, lambda: scll_copy.extract(1))
Ejemplo n.º 2
0
def test_DoublyCircularLinkedList():
    random.seed(1000)
    dcll = DoublyCircularLinkedList()
    assert raises(IndexError, lambda: dcll[2])
    dcll.append_left(5)
    dcll.append(1)
    dcll.append_left(2)
    dcll.append(3)
    dcll.insert_after(dcll[-1], 4)
    dcll.insert_after(dcll[2], 6)
    dcll.insert_before(dcll[4], 1)
    dcll.insert_before(dcll[0], 7)
    dcll.insert_at(0, 2)
    dcll.insert_at(-1, 9)
    dcll.extract(2)
    assert dcll.pop_left().data == 2
    assert dcll.pop_right().data == 4
    dcll[-2].data = 0
    assert str(dcll) == "[7, 5, 1, 6, 1, 0, 9]"
    assert len(dcll) == 7
    assert raises(IndexError, lambda: dcll.insert_at(8, None))
    assert raises(IndexError, lambda: dcll.extract(20))
    dcll_copy = copy.deepcopy(dcll)
    for i in range(len(dcll)):
        if i % 2 == 0:
            dcll.pop_left()
        else:
            dcll.pop_right()
    assert str(dcll) == "[]"
    for _ in range(len(dcll_copy)):
        index = random.randint(0, len(dcll_copy) - 1)
        dcll_copy.extract(index)
    assert str(dcll_copy) == "[]"
    assert raises(ValueError, lambda: dcll_copy.extract(1))
Ejemplo n.º 3
0
def test_DoublyLinkedList():
    random.seed(1000)
    dll = DoublyLinkedList()
    assert raises(IndexError, lambda: dll[2])
    dll.appendleft(5)
    dll.append(1)
    dll.appendleft(2)
    dll.append(3)
    dll.insert_after(dll[-1], 4)
    dll.insert_after(dll[2], 6)
    dll.insert_before(dll[4], 1.1)
    dll.insert_before(dll[0], 7)
    dll.insert_at(0, 2)
    dll.insert_at(-1, 9)
    dll.extract(2)
    assert dll.popleft().key == 2
    assert dll.popright().key == 4
    assert dll.search(3) == dll[-2]
    assert dll.search(-1) is None
    dll[-2].key = 0
    assert str(dll) == ("['(7, None)', '(5, None)', '(1, None)', "
                        "'(6, None)', '(1.1, None)', '(0, None)', "
                        "'(9, None)']")
    assert len(dll) == 7
    assert raises(IndexError, lambda: dll.insert_at(8, None))
    assert raises(IndexError, lambda: dll.extract(20))
    dll_copy = DoublyCircularLinkedList()
    for i in range(dll.size):
        dll_copy.append(dll[i])
    for i in range(len(dll)):
        if i % 2 == 0:
            dll.popleft()
        else:
            dll.popright()
    assert str(dll) == "[]"
    for _ in range(len(dll_copy)):
        index = random.randint(0, len(dll_copy) - 1)
        dll_copy.extract(index)
    assert str(dll_copy) == "[]"
    assert raises(ValueError, lambda: dll_copy.extract(1))
def test_DoublyCircularLinkedList():
    random.seed(1000)
    dcll = DoublyCircularLinkedList()
    assert raises(IndexError, lambda: dcll[2])
    dcll.appendleft(5)
    dcll.append(1)
    dcll.appendleft(2)
    dcll.append(3)
    dcll.insert_after(dcll[-1], 4)
    dcll.insert_after(dcll[2], 6)
    dcll.insert_before(dcll[4], 1)
    dcll.insert_before(dcll[0], 7)
    dcll.insert_at(0, 2)
    dcll.insert_at(-1, 9)
    dcll.extract(2)
    assert dcll.popleft().key == 2
    assert dcll.popright().key == 4
    dcll[-2].key = 0
    assert str(dcll) == "['7', '5', '1', '6', '1', '0', '9']"
    assert len(dcll) == 7
    assert raises(IndexError, lambda: dcll.insert_at(8, None))
    assert raises(IndexError, lambda: dcll.extract(20))
    dcll_copy = DoublyCircularLinkedList()
    for i in range(dcll.size):
        dcll_copy.append(dcll[i])
    for i in range(len(dcll)):
        if i % 2 == 0:
            dcll.popleft()
        else:
            dcll.popright()
    assert str(dcll) == "[]"
    for _ in range(len(dcll_copy)):
        index = random.randint(0, len(dcll_copy) - 1)
        dcll_copy.extract(index)
    assert str(dcll_copy) == "[]"
    assert raises(ValueError, lambda: dcll_copy.extract(1))