Example #1
0
def test_add_three_items(
    method_name,
    expected_head_value,
    expected_middle_value,
    expected_tail_value,
):
    linked_list = DoublyLinkedList()
    method = getattr(linked_list, method_name)

    method(1)
    method(2)
    method(3)

    assert len(linked_list) == 3

    head = linked_list.head
    middle = head.next_
    tail = linked_list.tail

    assert head == expected_head_value
    assert head.previous is None
    assert head.next_ == middle

    assert middle == expected_middle_value
    assert middle.previous == head
    assert middle.next_ == tail

    assert tail == expected_tail_value
    assert tail.previous == middle
    assert tail.next_ is None
Example #2
0
def test_append():
    doubly_linked_list = DoublyLinkedList()
    doubly_linked_list.append(3)
    doubly_linked_list.append(4)
    assert doubly_linked_list.size() == 2
    assert doubly_linked_list.get(0) == 3
    assert doubly_linked_list.get(1) == 4
Example #3
0
def test_size():
    linked_list = DoublyLinkedList()
    assert linked_list.size() == 0

    linked_list.append(1)
    assert linked_list.size() == 1
    assert get_multi_node_list().size() == 4
Example #4
0
def get_multi_node_list() -> DoublyLinkedList:
    multi_node_list = DoublyLinkedList()
    multi_node_list.append(5)
    multi_node_list.append(2)
    multi_node_list.append(4)
    multi_node_list.append(1)
    return multi_node_list
Example #5
0
    def test_first_item(self):
        test_list = DoublyLinkedList()
        test_list.insert_front(5)
        test_list.insert_front(9)
        pop = test_list.pop_front()

        self.assertEqual(9, pop, "first item did not insert correctly")
Example #6
0
def test_container():
    linked_list = DoublyLinkedList()
    linked_list.add_last(1)
    linked_list.add_last(2)

    assert 1 in linked_list
    assert 2 in linked_list
    assert 3 not in linked_list
Example #7
0
def test_iteration():
    linked_list = DoublyLinkedList()
    linked_list.add_last(1)
    linked_list.add_last(2)
    linked_list.add_last(3)

    values_list = [item for item in linked_list]

    assert values_list == [1, 2, 3]
Example #8
0
def test_reversed():
    linked_list = DoublyLinkedList()
    linked_list.add_last(1)
    linked_list.add_last(2)
    linked_list.add_last(3)

    values_list = [item for item in reversed(linked_list)]

    assert values_list == [3, 2, 1]
Example #9
0
    def test_pop_from_front(self):
        test_list = DoublyLinkedList()
        test_list.insert_front(5)
        test_list.insert_front(7)
        test_list.insert_front(9)
        front_pop = test_list.pop_front()

        node = test_list.pop_front()
        self.assertEqual(
            7, node._node_value,
            "First item isn't the second item before pop from front")
Example #10
0
def test_remove_added_last(method_name, size, head, tail):
    dll = DoublyLinkedList()

    for n in range(size):
        dll.add_last(n)

    method = getattr(dll, method_name)
    method()

    assert len(dll) == (size - 1)
    assert dll.head == head
    assert dll.tail == tail
    def test_pop_back(self):

        #Arrange
        double_linked = DoublyLinkedList()
        double_linked.insert_front(1)
        double_linked.insert_front(2)

        #Act
        double_linked.pop_back()

        #Assert
        self.assertEqual(None, double_linked.first_node.next_node,
                         "pop_back did not return correct node value")
    def test_insert_back(self):

        #Arrange
        double_linked = DoublyLinkedList()

        #Act
        double_linked.insert_back(3)
        double_linked.insert_back(2)
        double_linked.insert_back(1)

        #Assert
        self.assertEqual(3, double_linked.first_node.value,
                         "The value returned is not correct")
    def test_insert_front(self):

        #Arrange
        double_linked = DoublyLinkedList()

        #Act
        double_linked.insert_front(1)
        double_linked.insert_front(2)
        double_linked.insert_front(3)

        #Assert
        self.assertEqual(3, double_linked.first_node.value,
                         "The value returned is not correct")
        self.assertEqual(None, double_linked.first_node.prev_node,
                         "The value returned is not correct")
    def test_pop_front(self):

        #Arrange
        double_linked = DoublyLinkedList()
        double_linked.insert_front(1)
        double_linked.insert_front(2)
        double_linked.insert_front(3)

        #Act
        double_linked.pop_front()

        #Assert
        self.assertEqual(2, double_linked.first_node.value,
                         "pop_front did not return the correct node value")
        self.assertEqual(None, double_linked.first_node.prev_node,
                         "pop_front did not return the correct node value")
Example #15
0
def test_add_one_item(method_name):
    linked_list = DoublyLinkedList()
    method = getattr(linked_list, method_name)
    method(1)

    assert len(linked_list) == 1

    head = linked_list.head
    tail = linked_list.tail

    assert head == 1
    assert head.previous is None
    assert head.next_ is None

    assert tail == 1
    assert tail.previous is None
    assert tail.next_ is None
Example #16
0
def test_add_two_items(method_name, expected_head_value, expected_tail_value):
    linked_list = DoublyLinkedList()
    method = getattr(linked_list, method_name)

    method(1)
    method(2)

    assert len(linked_list) == 2

    head = linked_list.head
    tail = linked_list.tail

    assert head == expected_head_value
    assert head.previous is None
    assert head.next_ == linked_list.tail

    assert tail == expected_tail_value
    assert tail.previous == head
    assert tail.next_ is None
Example #17
0
def test_add_before_tail():
    linked_list = DoublyLinkedList()
    linked_list.add_last(1)
    linked_list.add_last(3)

    linked_list.add_before(linked_list.tail, 2)

    head = linked_list.head
    middle = head.next_
    tail = linked_list.tail

    assert head == 1
    assert head.previous is None
    assert head.next_ == middle

    assert middle == 2
    assert middle.previous == head
    assert middle.next_ == tail

    assert tail == 3
    assert tail.previous == middle
    assert tail.next_ is None
Example #18
0
 def __init__(self, capacity):
     self._cache = {}  # dictionary to contain key-value
     self._doubly_linked_list = DoublyLinkedList()
     self._capacity = capacity
     self._size = 0
Example #19
0
from data_structures.doubly_linked_list import DoublyLinkedList

# creating a doubly linked list
animals = DoublyLinkedList()

print('Adding a few elements on the front...')
animals.insert_front('Cow')
animals.insert_front('Tiger')
animals.display()

print('\nAdding a few elements on the back...')
animals.insert_back('Lion')
animals.insert_back('Wolf')
animals.display()

print('\nAdding an item after Lion')
animals.insert_after('Lion', 'Lioness')
animals.display()

print('\nRemoving a few items from beginning..')
animals.remove_front()
animals.remove_front()
animals.display()

print('\nRemoving Lioness from the list')
animals.remove('Lioness')
animals.display()

print('\nRemoving a few items from the back')
animals.remove_back()
animals.remove_back()
Example #20
0
def test_append_empty():
    doubly_linked_list = DoublyLinkedList()
    doubly_linked_list.append(1)
    assert doubly_linked_list.size() == 1
    assert doubly_linked_list.get(0) == 1
Example #21
0
def test_remove_empty():
    doubly_linked_list = DoublyLinkedList()
    assert not doubly_linked_list.remove(0)
Example #22
0
def test_insert_empty():
    doubly_linked_list = DoublyLinkedList()
    doubly_linked_list.insert(3, 0)
    assert doubly_linked_list.size() == 1
    assert doubly_linked_list.get(0) == 3
Example #23
0
def test_get_value():
    assert not DoublyLinkedList().get(0)
    assert not get_multi_node_list().get(10)
    assert get_multi_node_list().get(3) == 1
Example #24
0
def test_init():
    linked_list = DoublyLinkedList()
    linked_list.append(3)
    assert linked_list.size() == 1
    assert linked_list.get(0) == 3
Example #25
0
 def __init__(self):
     self.stack = DoublyLinkedList()
Example #26
0
 def __init__(self):
     self.queue = DoublyLinkedList()
Example #27
0
 def setUp(self):
     self.items = DoublyLinkedList()
Example #28
0
 def __init__(self):
     self.linked_list = DoublyLinkedList()
Example #29
0
def test_get_node():
    doubly_linked_list = DoublyLinkedList()
    doubly_linked_list.insert(3, 0)
    assert doubly_linked_list.get_node(0).data == 3
Example #30
0
def test_init_none():
    assert not DoublyLinkedList().get(0)
    assert DoublyLinkedList().size() == 0