示例#1
0
def test_linked_list_remove():
    ll = LinkedList()
    ll.append(3)
    ll.append(4)
    ll.append(5)
    ll.append(6)
    ll.append(7)
    ll.remove(7)
    print(list(ll))
示例#2
0
def test_linked_list_insert():
    ll = LinkedList()
    ll.append(3)
    ll.append(4)
    ll.append(5)
    result = ll.insert(4, 10)
    assert result == 1
    assert len(ll) == 4
    assert list(ll) == [3, 10, 4, 5]

    result = ll.insert(100, 1000)
    assert result == -1
示例#3
0
def test_linked_list():
    ll = LinkedList()

    ll.append(0)
    ll.append(1)
    ll.append(2)
    ll.append(3)

    assert len(ll) == 4
    assert ll.find(2) == 2
    assert ll.find(-1) == -1

    assert ll.remove(0) == 1
    assert ll.remove(10) == -1
    assert ll.remove(2) == 1
    assert len(ll) == 2
    assert list(ll) == [1, 3]
    assert ll.find(0) == -1

    ll.appendleft(0)
    assert list(ll) == [0, 1, 3]
    assert len(ll) == 3

    headvalue = ll.popleft()
    assert headvalue == 0
    assert len(ll) == 2
    assert list(ll) == [1, 3]

    ll.clear()
    assert len(ll) == 0
示例#4
0
 def test_init_empty(self):
     ll = LinkedList()
     self.assertEqual([], list(ll))
示例#5
0
 def test_str(self):
     ll = LinkedList()
     self.assertEqual("[]", str(ll))
     ll = LinkedList([1, 2, "3", 4, False])
     self.assertEqual("[1, 2, 3, 4, False]", str(ll))
示例#6
0
 def test_prepend(self):
     ll = LinkedList()
     ll.prepend(1)
     ll.prepend(2)
     ll.prepend(3)
     self.assertEqual([3, 2, 1], list(ll))
示例#7
0
 def test_append(self):
     ll = LinkedList()
     ll.append(1)
     ll.append(2)
     ll.append(3)
     self.assertEqual([1, 2, 3], list(ll))
示例#8
0
 def test_init_from_array(self):
     lst = [1, 2, 3, 4, 5]
     ll = LinkedList(lst)
     self.assertEqual(lst, list(ll))