コード例 #1
0
def test_add_node_method_adds_new_Node_class_to_Singly_Linked_Class():
    """Test that the add_node method of the Singly Linked List class
    successfully adds a new instance of the Node class."""

    ll = SinglyLinkedList()
    ll.add_node(1)
    assert isinstance(ll.head, Node)
コード例 #2
0
def test_search_method_returns_node_if_node_containing_data_exists_in_list():
    """Test that the search method of the Singly Linked List class returns Node
    containing given data if it exists in the list."""

    ll = SinglyLinkedList()
    ll.add_node(1)

    assert ll.search(1) is ll.head
コード例 #3
0
def test_pop_method_returns_data_value_of_head_node():
    """Test that the pop method of the Singly Linked List class returns the
    data value of the head Node."""

    ll = SinglyLinkedList()
    ll.add_node(10)

    assert ll.pop() == 10
コード例 #4
0
def test_delete_node_method_returns_data_value_of_deleted_node():
    """Test that the delete_node method of the Singly Linked List class returns
    the data value of the Node that it deletes."""

    ll = SinglyLinkedList()
    ll.add_node(1)

    assert ll.delete_node(1) == 1
コード例 #5
0
def test_delete_node_raises_exception_if_node_with_given_data_not_in_list():
    """Test that the delete_node method of the Singly Linked List class raises
    an exception if no Node with the given data exists in the list."""

    ll = SinglyLinkedList()
    ll.add_node(1)

    with pytest.raises(LookupError):
        assert ll.delete_node(5)
コード例 #6
0
def test_delete_node_method_removes_node_with_given_data():
    """Test that the delete_node method of the Singly Linked List class
    removes a Node from the list with the given data."""

    ll = SinglyLinkedList()
    ll.add_node(1)
    ll.delete_node(1)

    assert ll.head is None
コード例 #7
0
def test_add_node_method_replaces_head_when_multiple_nodes_added():
    """Test that the add_node method of the Singly Linked List class
    consistently replaces the head of the list with the newest added Node."""

    ll = SinglyLinkedList()

    for i in range(5):
        ll.add_node(i)
        assert ll.head.data == i
コード例 #8
0
def test_add_node_method_replaces_None_value_in_head_with_Node_object():
    """Test that the add_node method of the Singly Linked List class replaces
    the value of None in an empty list's head attribute with the Node class
    object."""

    ll = SinglyLinkedList()
    ll.add_node(1)
    assert ll.head is not None
    assert hasattr(ll.head, 'data') and hasattr(ll.head, 'next_node')
コード例 #9
0
def test_pop_method_removes_head_of_list():
    """Test that the pop method of the Singly Linked List class removes the
    head of the list."""

    ll = SinglyLinkedList()
    ll.add_node(1)

    ll.pop()
    assert ll.head is None
コード例 #10
0
def test_search_method_raises_exception_if_node_containing_data_absent():
    """Test that the search method of the Singly Linked List class raises a
    LookupError if no Node in list contains given data."""

    ll = SinglyLinkedList()
    ll.add_node(1)

    with pytest.raises(LookupError):
        assert ll.search(5)
コード例 #11
0
def test_display_method_returns_correct_visual_representation_of_list():
    """Test that the display method of the Singly Linked List returns an
    accurate visual representation of the list, complete with all Nodes."""

    ll = SinglyLinkedList()

    for i in range(3):
        ll.add_node(i)

    assert ll.display() == '[ 2 ] -> [ 1 ] -> [ 0 ] -> None'
コード例 #12
0
def test_pop_method_removes_and_reassigns_head_if_multiple_nodes_in_list():
    """Test that the pop method of the Singly Linked List class removes the
    head of the list and then reassigns the head to be the next Node in the
    list, if multiple Nodes exist."""

    ll = SinglyLinkedList()
    ll.add_node(1)
    ll.add_node(5)

    ll.pop()
    assert ll.head.data == 1
コード例 #13
0
def test_delete_node_method_reassigns_next_node_to_none_if_deleting_end_Node():
    """Test that the delete_node method of the Singly Linked List correctly
    reassigns the next_node to Node if it's removing the last Node of the list
    ."""

    ll = SinglyLinkedList()
    ll.add_node(1)
    ll.add_node(2)

    ll.delete_node(1)
    assert ll.head.next_node is None
コード例 #14
0
def test_delete_node_method_reassigns_head_of_list_if_head_is_deleted():
    """Test that the delete_node method of the Singly Linked List class
    correctly reassigns the head Node of the list to the next Node if the head
    Node is removed."""

    ll = SinglyLinkedList()
    ll.add_node(1)
    ll.add_node(2)

    ll.delete_node(2)
    assert ll.head.data == 1
コード例 #15
0
def test_delete_node_method_correctly_reassigns_pointers_if_deleting_middle():
    """Test that the delete_node method of the Singly Linked List class
    correctly reassigns pointers if deleting a Node that has two Nodes on
    either side of it."""

    ll = SinglyLinkedList()

    for i in range(3):
        ll.add_node(i)

    ll.delete_node(1)
    assert ll.head.data == 2 and ll.head.next_node.data == 0