예제 #1
0
 def test_search(self):
     sample_linked_list = linked_list.Linked_list()
     for i in range(10):
         sample_linked_list.insert(i)
     self.assertEqual(sample_linked_list.head.key, 9)
     self.assertEqual(sample_linked_list.head.next.next.key, 7)
     self.assertEqual(sample_linked_list.head.next.next.next.next.key, 5)
예제 #2
0
def test_pop():
    """Test pop method with a populated and empty list.

    Validate pop output and removal"""

    a = ll.Linked_list()
    a.insert("Mark")
    a.insert("Henry")
    assert a.pop() == "Henry"
    # Confirming that Mark is the new Head and has no pointer
    assert "Mark" == a.head.value
    assert a.head.pointer is None
    b = ll.Linked_list()
    # Confirm Attribute error with pop from empty list
    with pytest.raises(AttributeError):
        assert b.pop() is None
예제 #3
0
 def test_delete(self):
     sample_linked_list = linked_list.Linked_list()
     for i in range(10):
         sample_linked_list.insert(i)
     sample_linked_list.delete(9)
     self.assertEqual(sample_linked_list.head.key, 8)
     sample_linked_list.delete(5)
     self.assertEqual(sample_linked_list.head.next.next.next.next.key, 3)
예제 #4
0
 def test_insert(self):
     sample_linked_list = linked_list.Linked_list()
     sample_linked_list.insert(3)
     self.assertEqual(sample_linked_list.head.key, 3)
     sample_linked_list.insert(5)
     self.assertEqual(sample_linked_list.head.key, 5)
     self.assertEqual(sample_linked_list.head.next.key, 3)
     sample_linked_list.insert(7)
     self.assertEqual(sample_linked_list.head.key, 7)
     self.assertEqual(sample_linked_list.head.next.key, 5)
     self.assertEqual(sample_linked_list.head.next.next.key, 3)
예제 #5
0
def test_str():
    """Test  of string formatting with integers and strings."""

    a = ll.Linked_list()
    assert str(a) == "()"
    a.insert("Mark")
    a.insert("Henry")
    a.insert(1232)
    a.insert("Efrain")
    a.insert("Last")
    a.insert(4545)
    assert str(a) == "(4545, 'Last', 'Efrain', 1232, 'Henry', 'Mark')"
    # Test string evaluates to a tuple literal
    assert eval(str(a)) == (4545, 'Last', 'Efrain', 1232, 'Henry', 'Mark')
예제 #6
0
def test_insert():
    """Test insert by adding node to empty list and

    adding list with exisiting nodes."""
    # Test insert into empty list.
    a = ll.Linked_list()
    assert a.head is None
    a.insert("Mark")
    assert "Mark" == a.head.value
    assert a.head.pointer is None
    # Test insert into populated list.
    a.insert("Henry")
    assert "Henry" == a.head.value
    assert "Mark" == a.head.pointer.value
예제 #7
0
def test_search():
    """Return the node containing value in the list, if present, else None."""

    a = ll.Linked_list()
    a.insert("Mark")
    a.insert("Henry")
    a.insert(1232)
    a.insert("Efrain")
    a.insert("Last")
    a.insert(4545)
    assert a.search("Mark").value == "Mark"
    assert a.search(1232).value == 1232
    # Test ability to find first node value
    assert a.search(4545).value == 4545
    # Test search for non-existing item
    assert a.search("George") is None
예제 #8
0
def test_size():
    """Test Size with pop and remove commands."""

    a = ll.Linked_list()
    a.insert("Mark")
    a.insert("Henry")
    a.insert("Efrain")
    a.insert("Last")
    a.display()
    assert a.size() == 4
    a.remove(a.search("Henry"))
    assert a.size() == 3
    a.pop()
    assert a.size() == 2
    a.pop()
    assert a.size() == 1
    a.pop()
    assert a.size() == 0
예제 #9
0
def test_remove():
    """Test removal of nodes."""
    # Test remove of node in middle of linked list
    a = ll.Linked_list()
    a.insert("Henry")
    a.insert("Efrain")
    a.insert("Last")
    a.remove(a.search("Henry"))
    assert a.size() == 2
    assert str(a) == "('Last', 'Efrain')"
    # Test removal of the first node
    a.remove(a.search("Last"))
    assert a.size() == 1
    assert str(a) == "('Efrain')"
    # Test removal of last node
    a.remove(a.search("Efrain"))
    assert a.size() == 0
    assert str(a) == "()"
예제 #10
0
 def test_traverse(self):
     sample_linked_list = linked_list.Linked_list()
     for i in range(10):
         sample_linked_list.insert(i)
     self.assertEqual(sample_linked_list.traverse(),
                      [9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
예제 #11
0
def test_init():
    """Test link_list class constructor."""

    a = ll.Linked_list()
    assert isinstance(a, ll.Linked_list)