-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tortoiseHare.py
90 lines (65 loc) · 2.65 KB
/
test_tortoiseHare.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import unittest
import random
import tortoiseHare as th
class TestFunctions(unittest.TestCase):
"""
Testing functions: Tests lists of variable lengths with cycles and without cycles.
Cycles generated by passing list through makeALoop(theList, randomInt).
The integer must be less than the length of the list.
"""
def setUp(self):
#test cases with no cycles
self.empty=th.makeLinkedList([])
self.oneElem=th.makeLinkedList([1])
self.twoElem=th.makeLinkedList([2, 3])
self.threeElem=th.makeLinkedList([4, 5, 6])
self.longList=th.makeLinkedList([3, 34, 5, 6, 3, 2])
#test cases with cycles
self.oneLinked=th.makeALoop(th.makeLinkedList(['a']), 0)
self.twoLinked=th.makeALoop(th.makeLinkedList(['b','c']), 1)
self.threeLinked=th.makeALoop(th.makeLinkedList(['d','e','f']), 0)
self.x=(random.randint(0, 6))
self.longLinked=th.makeALoop(th.makeLinkedList(['g', 'hi', 'j', 'klm', 'n', 'o']), self.x)
def tearDown(self):
#kill it with fire
del self.empty
del self.oneElem
del self.twoElem
del self.threeElem
del self.longList
del self.oneLinked
del self.twoLinked
del self.threeLinked
del self.longLinked
del self.x
#test all the straight lists:
def test_emptyCycleFree(self):
#Empty LL should be false.
self.assertFalse(th.tortoiseHare(self.empty))
def test_oneElemCycleFree(self):
#one elem with no cycles should be false.
self.assertFalse(th.tortoiseHare(self.oneElem))
def test_twoElemCycleFree(self):
#two elem with no cycles should be false.
self.assertFalse(th.tortoiseHare(self.twoElem))
def test_threeElemCycleFree(self):
#three elements and no cycles should be false
self.assertFalse(th.tortoiseHare(self.threeElem))
def test_longListCycleFree(self):
#linked list with no cycles should be false
self.assertFalse(th.tortoiseHare(self.longList))
# test all the cycles:
def test_oneLinked(self):
#should be true
self.assertTrue(th.tortoiseHare(self.oneLinked))
def test_twoLinked(self):
#should be true
self.assertTrue(th.tortoiseHare(self.twoLinked))
def test_threeLinked(self):
#should be true
self.assertTrue(th.tortoiseHare(self.threeLinked))
def test_longLinked(self):
#should be true
self.assertTrue(th.tortoiseHare(self.longLinked))
if __name__ == '__main__':
unittest.main()