forked from JimmyJimJamJamvilleJam/fa18-hw-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw5_test.py
More file actions
136 lines (85 loc) · 2.89 KB
/
Copy pathhw5_test.py
File metadata and controls
136 lines (85 loc) · 2.89 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import unittest
from hw5 import *
# test file for homework 5
LOCAL_TIMEOUT = 10
class tester_add_position(unittest.TestCase):
def test__given(self):
#add_position(head, data, position)
a = Node(2, Node(3, Node(4, Node(6))))
a = add_position(a, 5, 3)
self.assertEqual(a.next_node.next_node.next_node.data, 5)
a = add_position(a, 1, 0)
self.assertEqual(a.data, 1)
a = Node(2, Node(3))
a = add_position(a, 8, 0)
self.assertEqual(a.data, 8)
class tester_remove_position(unittest.TestCase):
def test__given(self):
a = Node(2, Node(3, Node(4)))
a = remove_position(a, 1)
self.assertEqual(a.next_node.data, 4)
a = Node(2, Node(3, Node(4)))
a = remove_position(a, 0)
self.assertEqual(a.data, 3)
def test_vacuous_given(self):
#if head is none
self.assertIsNone(None, 1)
class tester_merge(unittest.TestCase):
def test_given(self):
head_a = Node(2, Node(3))
head_b = Node(1, Node(4))
head_result = merge_lists(head_a, head_b)
self.assertEqual(head_result.data, 1)
self.assertEqual(head_result.next_node.data, 2)
self.assertEqual(head_result.next_node.next_node.data, 3)
self.assertEqual(head_result.next_node.next_node.next_node.data, 4)
class tester_reverse_list(unittest.TestCase):
def test_given(self):
a = Node(2, Node(3, Node(4)))
reversed = reverse_list(a)
self.assertEqual(reversed.data, 4)
self.assertEqual(reversed.next_node.data, 3)
self.assertEqual(reversed.next_node.next_node.data, 2)
self.assertIsNone(reverse_list(None))
class tester_find_merge_point(unittest.TestCase):
def test__given(self):
a = Node(2, Node(6, Node(-8)))
a1 = Node(3, a)
a2 = Node(5, a)
self.assertEqual(find_merge_point(a1, a2), 2)
a3 = Node(6)
self.assertIsNone(find_merge_point(a3, a2))
class tester_find_cycle(unittest.TestCase):
def test__given(self):
node_a = Node(1)
node_b = Node(2)
node_c = Node(3)
node_a.next_node = node_b
node_b.next_node = node_c
# create cycle between node_b and node_b
node_c.next_node = node_b
self.assertTrue(find_cycle(node_a))
#===================================
# suppress stdout, but keep stderr since that's what unittest uses
# https://stackoverflow.com/questions/30715337
from io import StringIO
import sys
class ReplaceStd(object):
""" Let's make it pythonic. """
def __init__(self):
self.stdout = None
#self.stderr = None
def __enter__(self):
self.stdout = sys.stdout
#self.stderr = sys.stderr
# as it was suggested already:
sys.stdout = StringIO()
#sys.stderr = StringIO()
def __exit__(self, type, value, traceback):
sys.stdout.close()
#sys.stderr.close()
sys.stdout = self.stdout
#sys.stderr = self.stderr
if __name__ == "__main__":
with ReplaceStd():
unittest.main(module=__name__, buffer=True, exit=False)