-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutable_version_test.py
146 lines (120 loc) · 4.91 KB
/
mutable_version_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
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
137
138
139
140
141
142
143
144
145
146
from mutable_version import *
import unittest
from hypothesis import given
import hypothesis.strategies as st
"""
folloing function is used in map and reduce
"""
def judge(num):
return num % 2 == 0
def fun(x, y):
return x + y
class TestMutableList(unittest.TestCase):
@given(st.lists(st.integers()))
def test_mutable(self, a):
test_list1 = LinkedList([1, 2, None, 4, 5, 6])
test_list2 = LinkedList([1, 2, 3, 4, 5, 6, 7, 8], 4)
# set single node capacity as 4 not default 2
test_list3 = LinkedList(a)
self.assertEqual(test_list1.to_list(), [1, 2, None, 4, 5, 6])
self.assertEqual(test_list2.to_list(), [1, 2, 3, 4, 5, 6, 7, 8])
self.assertEqual(test_list3.to_list(), a)
"""
these code shows that the function mutable and can change by function
"""
@given(st.lists(st.integers()))
def test_from_list(self, a):
test_data = [
[],
['a'],
['a', 'b', 'c']
]
lst = LinkedList([])
for e in iter(test_data):
lst.from_list(e)
self.assertEqual(lst.to_list(), e)
testlist1 = LinkedList()
testlist1.from_list(a)
self.assertEqual(testlist1.to_list(), a)
@given(st.lists(st.integers()))
def test_to_list(self, a):
testlist1 = LinkedList(['a', 2, 'b', 3, 5, 6])
self.assertEqual(testlist1.to_list(), ['a', 2, 'b', 3, 5, 6])
testlist2 = LinkedList(a)
self.assertEqual(testlist2.to_list(), a)
@given(st.lists(st.integers()))
def test_size(self, b):
test_list1 = LinkedList(b)
test_list2 = LinkedList(['a'])
test_list3 = LinkedList([])
self.assertEqual(test_list3.size(), 0)
self.assertEqual(test_list1.size(), len(b))
self.assertEqual(test_list2.size(), 1)
# find not even
def test_find(self):
test_list1 = LinkedList([1, 2, 4, 3, 5, 6])
self.assertEqual(test_list1.find(judge), [2, 4, 6])
def test_add(self):
test_list1 = LinkedList([1, 2, 4, 3, 5, 6])
test_list1.add(3, 22)
self.assertEqual(test_list1.to_list(), [1, 2, 4, 22, 3, 5, 6])
# filter and what left is even
def test_filter(self):
test_list1 = LinkedList([1, 2, 4, 3, 5, 6])
self.assertEqual(test_list1.filter(judge), [1, 3, 5])
def test_map(self):
test_list1 = LinkedList([1, 2, 4, 3, 5, 6])
result = test_list1.map(lambda x: x + 2)
self.assertEqual(result.to_list(), [3, 4, 6, 5, 7, 8])
def test_reduce(self):
test_list1 = LinkedList([1, 2, 4, 3, 5, 6])
self.assertEqual(test_list1.reduce(fun, 0), 21)
@given(st.lists(st.integers()))
def test_mempty(self, a):
x = [1, 2, 4, 3, 7, 5, 6]
test_list1 = LinkedList(x)
result1 = test_list1.mempty()
test_list2 = LinkedList(a)
result2 = test_list2.mempty()
self.assertEqual(result1.to_list(), [])
self.assertEqual(result1.size(), 0)
self.assertEqual(result2.size(), 0)
# remove by index
def test_remove(self):
test_list1 = LinkedList([1, 2, 4, 3, 5, 6])
test_list1.remove(2)
self.assertEqual(test_list1.to_list(), [1, 2, 3, 5, 6])
@given(st.lists(st.integers()))
def test_mconcat(self, a):
test_list1 = LinkedList([1, 2, 4, 3, 5, 6])
test_list2 = LinkedList()
test_list3 = LinkedList([1, 2, 4, 'a', 'b'])
test_list4 = LinkedList(a)
self.assertEqual(test_list1.mconcat(test_list2), [1, 2, 4, 3, 5, 6])
self.assertEqual(test_list3.mconcat(test_list2), [1, 2, 4, 'a', 'b'])
self.assertEqual(test_list4.mconcat(test_list2), a)
@given(a=st.lists(st.integers()), b=st.lists(st.integers()))
def test_monoid_identity(self, a, b):
testlist_a = LinkedList(a)
testlist_b = LinkedList(b)
self.assertEqual(testlist_a.mconcat(testlist_b), testlist_b.mconcat(testlist_a))
@given(a=st.lists(st.integers()), b=st.lists(st.integers()), c=st.lists(st.integers()))
def test_monoid_associativity(self, a, b, c):
testlist_a = LinkedList(a)
testlist_b = LinkedList(b)
testlist_c = LinkedList(c)
a_b = testlist_a.mconcat(testlist_b)
b_a = testlist_b.mconcat(testlist_a)
self.assertEqual(a_b, b_a)
c_b = testlist_c.mconcat(testlist_b)
b_c = testlist_b.mconcat(testlist_c)
self.assertEqual(c_b, b_c)
a_b__c = testlist_c.mconcat(LinkedList(a_b))
a__b_c = testlist_a.mconcat(LinkedList(b_c))
self.assertEqual(a_b__c, a__b_c)
def test_add_to_tail(self):
test_list2 = LinkedList([1, 2, 4, 3, 5, 6])
test_list2.add_to_tail('x')
self.assertEqual(test_list2.to_list(), [1, 2, 4, 3, 5, 6, 'x'])
if __name__ == '__main__':
unittest.main()