-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.py
71 lines (58 loc) · 2.33 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import unittest
from dstructure.linkedlist import LinkedList
from dstructure.exceptions import *
class TestLinkedList(unittest.TestCase):
def test_init(self):
l = LinkedList(range(5))
self.assertEqual(l.__str__(),'LinkedList([0, 1, 2, 3, 4])')
self.assertEqual(len(l),5)
l1 = LinkedList(0,1,2,3,4)
self.assertEqual(l1.__str__(),'LinkedList([0, 1, 2, 3, 4])')
l1 = LinkedList([0,1,2,3])
self.assertEqual(l1.__str__(),'LinkedList([0, 1, 2, 3])')
l1 = LinkedList((0,1,2,3,4))
self.assertEqual(l1.__str__(),'LinkedList([0, 1, 2, 3, 4])')
def test_headtaillen(self):
l = LinkedList(range(5))
self.assertEqual(l.head.data,0)
self.assertEqual(l.head.next.data,1)
self.assertEqual(l.tail.data,4)
self.assertEqual(l.tail.next,None)
self.assertEqual(len(l),5)
l.append(5)
self.assertEqual(len(l),6)
def test_getsetslice(self):
l = LinkedList(range(5))
self.assertEqual(l[2].data,2)
l[2]=20
self.assertEqual(l[2].data,20)
self.assertEqual(l[0].data,0)
self.assertEqual(l[4].data,4)
self.assertEqual(l.__str__(),'LinkedList([0, 1, 20, 3, 4])')
self.assertEqual(l[:3].__str__(),'LinkedList([0, 1, 20])')
self.assertEqual(l[3:].__str__(),'LinkedList([3, 4])')
def test_reverse(self):
l = LinkedList(range(5))
l.reverse()
self.assertEqual(l.__str__(),'LinkedList([4, 3, 2, 1, 0])')
self.assertEqual(l.head.data,l[0].data)
self.assertEqual(l.head.next.data,l[1].data)
self.assertEqual(l.tail.data,0)
def test_insertremove(self):
l = LinkedList(range(5))
l.insert(0,'front')
self.assertEqual(l.__str__(),'LinkedList([front, 0, 1, 2, 3, 4])')
l.insert(3,'three')
self.assertEqual(l.__str__(),'LinkedList([front, 0, 1, three, 2, 3, 4])')
self.assertEqual(l[3].data,'three')
l.remove(0)
self.assertEqual(l.__str__(),'LinkedList([0, 1, three, 2, 3, 4])')
self.assertEqual(len(l),6)
l.remove(3)
self.assertEqual(l.__str__(),'LinkedList([0, 1, three, 3, 4])')
def test_exceptions(self):
l = LinkedList(1)
l.pop()
self.assertEqual(len(l),0)
if __name__ == '__main__':
unittest.main()