generated from ryukzak/edu-cpo-lab-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_DynamicArray.py
399 lines (350 loc) · 13.3 KB
/
test_DynamicArray.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import unittest
import math
from hypothesis import given
import hypothesis.strategies as st
from DynamicArray import DynamicArray, cons, remove, length
from DynamicArray import isMember, reverse, to_list, from_list
from DynamicArray import find, filter_the_value, map_the_value
from DynamicArray import reduce, iterator_element, next_element
from DynamicArray import empty, concat
class TestDynamicArray(unittest.TestCase):
def test_cons_unittest(self):
emp = DynamicArray()
l1 = cons(None, cons(1, emp))
l2 = cons(1, cons(None, emp))
self.assertEqual(str(emp), "[]")
self.assertEqual(str(l1), "[None, 1]")
self.assertEqual(str(l2), "[1, None]")
self.assertNotEqual(emp, l1)
self.assertNotEqual(emp, l2)
self.assertNotEqual(l1, l2)
self.assertEqual(l1, cons(None, cons(1, emp)))
@given(st.binary(), st.binary())
def test_cons_binary(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
list1 = [a, b]
self.assertEqual(to_list(l1), list1)
@given(st.binary(), st.integers())
def test_cons_binary_integers(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
list1 = [a, b]
self.assertEqual(to_list(l1), list1)
@given(st.text(), st.text())
def test_cons_text(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
list1 = [a, b]
self.assertEqual(to_list(l1), list1)
@given(st.text(), st.floats())
def test_cons_text_floats(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
list1 = [a, b]
self.assertEqual(to_list(l1), list1)
@given(st.none(), st.none())
def test_cons_none(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
list1 = [a, b]
self.assertEqual(to_list(l1), list1)
@given(st.floats(), st.none())
def test_cons_floats_none(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
list1 = [a, b]
self.assertEqual(to_list(l1), list1)
@given(st.floats(), st.floats())
def test_cons_floats(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
list1 = [a, b]
self.assertEqual(to_list(l1), list1)
def test_remove_unittest(self):
emp = DynamicArray()
l1 = cons(None, cons(1, emp))
l2 = cons(1, cons(None, emp))
self.assertEqual(str(remove(l1, 1)), "[None]")
self.assertEqual(str(remove(l2, 1)), "[None]")
@given(st.text(), st.text())
def test_remove_text(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
list1 = []
if a != b:
list1 = [a]
self.assertEqual(to_list(remove(l1, b)), list1)
@given(st.floats(), st.floats())
def test_remove_floats(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
list1 = []
if a != b:
list1 = [a]
if math.isnan(a) and math.isnan(b):
list1 = []
self.assertEqual(to_list(remove(l1, b)), list1)
@given(st.floats(), st.none())
def test_remove_floats_none(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
list1 = []
if a != b:
list1 = [a]
self.assertEqual(to_list(remove(l1, b)), list1)
def test_length_unittest(self):
emp = DynamicArray()
l1 = cons(None, cons(1, emp))
self.assertEqual(length(emp), 0)
self.assertEqual(length(l1), 2)
@given(st.floats(), st.floats())
def test_length_floats(self, a, b):
emp = DynamicArray()
l1 = cons(b, cons(a, emp))
self.assertEqual(length(emp), 0)
self.assertEqual(length(l1), 2)
@given(st.binary(), st.binary())
def test_length_binary(self, a, b):
emp = DynamicArray()
l1 = cons(b, cons(a, emp))
self.assertEqual(length(emp), 0)
self.assertEqual(length(l1), 2)
@given(st.floats(), st.binary())
def test_length_floats_binary(self, a, b):
emp = DynamicArray()
l1 = cons(b, cons(a, emp))
self.assertEqual(length(emp), 0)
self.assertEqual(length(l1), 2)
def test_isMember_unittest(self):
emp = DynamicArray()
l1 = cons(None, cons(1, emp))
self.assertFalse(isMember(emp, None))
self.assertTrue(isMember(l1, None))
self.assertTrue(isMember(l1, 1))
self.assertFalse(isMember(l1, 2))
@given(st.binary(), st.binary())
def test_isMember_binary(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
self.assertFalse(isMember(emp, None))
self.assertTrue(isMember(l1, b))
self.assertTrue(isMember(l1, a))
self.assertFalse(isMember(l1, None))
def test_reverse_unittest(self):
emp = DynamicArray()
l1 = cons(None, cons(1, emp))
l2 = cons(1, cons(None, emp))
self.assertEqual(l1, reverse(l2))
@given(st.floats(), st.floats())
def test_reverse_floats(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
l2 = cons(b, cons(a, emp))
self.assertEqual(l1, reverse(l2))
@given(st.text(), st.text())
def test_reverse_text(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
l2 = cons(b, cons(a, emp))
self.assertEqual(l1, reverse(l2))
@given(st.floats(), st.text())
def test_reverse_floats_text(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
l2 = cons(b, cons(a, emp))
self.assertEqual(l1, reverse(l2))
def test_tolist_unittest(self):
emp = DynamicArray()
l1 = cons(None, cons(1, emp))
self.assertEqual(to_list(l1), [None, 1])
@given(st.floats(), st.floats())
def test_tolist_floats(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
lst = [a, b]
self.assertEqual(to_list(l1), lst)
@given(st.binary(), st.floats())
def test_tolist_binary_floats(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
lst = [a, b]
self.assertEqual(to_list(l1), lst)
def test_fromlist_unittest(self):
emp = DynamicArray()
l1 = cons(None, cons(1, emp))
self.assertEqual(l1, from_list([None, 1]))
@given(st.floats(), st.floats())
def test_fromlist_floats(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
self.assertEqual(l1, from_list([a, b]))
@given(st.binary(), st.floats())
def test_fromlist_binary_floats(self, a, b):
emp = DynamicArray()
l1 = cons(a, cons(b, emp))
self.assertEqual(l1, from_list([a, b]))
def test_find_unittest(self):
emp = DynamicArray()
l1 = cons(8, cons(4, cons(2, emp)))
self.assertEqual(find(l1, lambda x: x % 2 == 0), [8, 4, 2])
@given(st.integers(), st.integers(), st.integers())
def test_find_integers(self, a, b, c):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
lst = []
if a % 2 == 0:
lst.append(a)
if b % 2 == 0:
lst.append(b)
if c % 2 == 0:
lst.append(c)
self.assertEqual(find(l1, lambda x: x % 2 == 0), lst)
@given(st.integers(), st.floats(), st.floats())
def test_find_integers_floats(self, a, b, c):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
lst = []
if a % 2 == 0:
lst.append(a)
if b % 2 == 0:
lst.append(b)
if c % 2 == 0:
lst.append(c)
self.assertEqual(find(l1, lambda x: x % 2 == 0), lst)
def test_filter_the_value_unittest(self):
emp = DynamicArray()
l1 = cons(8, cons(3, cons(2, emp)))
l2 = cons(8, cons(2, emp))
self.assertEqual(filter_the_value(l1, lambda x: x % 2 == 0), l2)
@given(st.integers(), st.integers(), st.integers())
def test_filter_the_value_integers(self, a, b, c):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
l2 = DynamicArray()
if c % 2 == 0:
l2 = cons(c, l2)
if b % 2 == 0:
l2 = cons(b, l2)
if a % 2 == 0:
l2 = cons(a, l2)
self.assertEqual(filter_the_value(l1, lambda x: x % 2 == 0), l2)
@given(st.integers(), st.floats(), st.floats())
def test_filter_the_value_integers_floats(self, a, b, c):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
l2 = DynamicArray()
if c % 2 == 0:
l2 = cons(c, l2)
if b % 2 == 0:
l2 = cons(b, l2)
if a % 2 == 0:
l2 = cons(a, l2)
self.assertEqual(filter_the_value(l1, lambda x: x % 2 == 0), l2)
def test_map_unittest(self):
emp = DynamicArray()
l1 = cons(8, cons(3, cons(2, emp)))
l2 = cons(9, cons(4, cons(3, emp)))
self.assertEqual(map_the_value(l1, lambda x: x + 1), l2)
@given(st.integers(), st.integers(), st.integers())
def test_map_integers(self, a, b, c):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
l2 = cons(a + 1, cons(b + 1, cons(c + 1, emp)))
self.assertEqual(map_the_value(l1, lambda x: x + 1), l2)
@given(st.integers(), st.floats(), st.floats())
def test_map_integers_floats(self, a, b, c):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
l2 = cons(a + 1, cons(b + 1, cons(c + 1, emp)))
self.assertEqual(map_the_value(l1, lambda x: x + 1), l2)
def test_reduce_unittest(self):
emp = DynamicArray()
l1 = cons(8, cons(3, cons(2, emp)))
l2 = cons(10, cons(5, cons(4, emp)))
self.assertEqual(reduce(l1, lambda x, state: x + 1 + state, 1), l2)
@given(st.integers(), st.integers(), st.integers())
def test_reduce_integers(self, a, b, c):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
initial_state = 1
l2 = cons(a + 1 + initial_state,
cons(b + 1 + initial_state,
cons(c + 1 + initial_state, emp)))
self.assertEqual(reduce(l1, lambda x, state: x + 1 + state,
initial_state), l2)
def test_iterator_unittest(self):
emp = DynamicArray()
l1 = cons(8, cons(3, cons(2, emp)))
l2 = iterator_element(l1)
tem, l3 = next_element(l2)
self.assertEqual(tem, 8)
tem, l4 = next_element(l3)
self.assertEqual(tem, 3)
tem, l5 = next_element(l4)
self.assertEqual(tem, 2)
@given(st.integers(), st.integers(), st.integers())
def test_iterator_integers(self, a, b, c):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
l2 = iterator_element(l1)
tem, l3 = next_element(l2)
self.assertEqual(tem, a)
tem, l4 = next_element(l3)
self.assertEqual(tem, b)
tem, l5 = next_element(l4)
self.assertEqual(tem, c)
@given(st.integers(), st.binary(), st.integers())
def test_iterator_integers_binary(self, a, b, c):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
l2 = iterator_element(l1)
tem, l3 = next_element(l2)
self.assertEqual(tem, a)
tem, l4 = next_element(l3)
self.assertEqual(tem, b)
tem, l5 = next_element(l4)
self.assertEqual(tem, c)
def test_empty_unittest(self):
emp = DynamicArray()
l1 = cons(8, cons(3, cons(2, emp)))
self.assertEqual(empty(l1), emp)
@given(st.floats(), st.floats(), st.floats())
def test_empty_floats(self, a, b, c):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
self.assertEqual(empty(l1), emp)
def test_concat_unittset(self):
emp = DynamicArray()
l1 = cons(8, cons(3, cons(2, emp)))
l2 = cons(9, cons(4, cons(3, emp)))
list1 = []
list1.extend(to_list(l1))
list2 = to_list(l2)
list1.extend(list2)
l3 = from_list(list1)
self.assertEqual(concat(l1, l2), l3)
@given(st.integers(), st.integers(), st.integers(),
st.integers(), st.integers(), st.integers())
def test_concat_integers(self, a, b, c, d, e, f):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
l2 = cons(d, cons(e, cons(f, emp)))
list1 = []
list1.extend(to_list(l1))
list2 = to_list(l2)
list1.extend(list2)
l3 = from_list(list1)
self.assertEqual(concat(l1, l2), l3)
@given(st.integers(), st.integers(), st.integers(),
st.floats(), st.floats(), st.floats())
def test_concat_integers_floats(self, a, b, c, d, e, f):
emp = DynamicArray()
l1 = cons(a, cons(b, cons(c, emp)))
l2 = cons(d, cons(e, cons(f, emp)))
list1 = []
list1.extend(to_list(l1))
list2 = to_list(l2)
list1.extend(list2)
l3 = from_list(list1)
self.assertEqual(concat(l1, l2), l3)