-
Notifications
You must be signed in to change notification settings - Fork 3
/
tester.py
executable file
·265 lines (213 loc) · 7.95 KB
/
tester.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
#!/usr/bin/env python3
import unittest
import itertools
import lazylist
range_size = 10
class TestGetItem(unittest.TestCase):
def test_incremental(self):
lazy = lazylist.List(range(range_size))
for i in range(range_size):
lazy[i]
self.assertEqual(len(lazy._list), i + 1)
self.assertEqual(len(lazy), range_size)
for a, b in zip(lazy._list, range(range_size)):
self.assertEqual(a, b)
def test_all_at_once(self):
lazy = lazylist.List(range(range_size))
lazy[range_size - 1]
self.assertEqual(len(lazy._list), range_size)
def test_negative_index(self):
lazy = lazylist.List(range(range_size))
for i in range(range_size):
self.assertEqual(lazy[-(i + 1)], range_size - i - 1)
class TestSetItem(unittest.TestCase):
def test_zero_out(self):
lazy = lazylist.List(range(range_size))
for i in range(range_size):
lazy[i] = 0
for i in range(range_size):
self.assertEqual(lazy[i], 0)
def test_slice_assign(self):
lazy = lazylist.List(range(range_size))
seq = list(range(range_size))
lazy[2:5] = [10,20,30,40,50,60]
seq[2:5] = [10,20,30,40,50,60]
for i, e in enumerate(seq):
self.assertEqual(lazy[i], e)
def test_negative_slice_assign(self):
lazy = lazylist.List(range(range_size))
seq = list(range(range_size))
lazy[-2:-5] = [10,20,30,40,50,60]
seq[-2:-5] = [10,20,30,40,50,60]
for i, e in enumerate(seq):
self.assertEqual(lazy[i], e)
class TestDelItem(unittest.TestCase):
def test_remove_middle(self):
lazy = lazylist.List(range(range_size))
del lazy[4]
self.assertEqual(lazy[3] + 2, lazy[4])
class TestLen(unittest.TestCase):
def test_lens(self):
seq = list(range(10))
self.assertEqual(len(seq), len(lazylist.List(seq)))
class TestContains(unittest.TestCase):
def test_range(self):
range_min = 10
range_max = 20
lazy = lazylist.List(range(range_min, range_max))
for i in range(range_min, range_max):
self.assertTrue(i in lazy)
for i in range(0, range_min):
self.assertFalse(i in lazy)
for i in range(range_max, range_max + 10):
self.assertFalse(i in lazy)
class TestBool(unittest.TestCase):
def test_for_false(self):
self.assertFalse(lazylist.List(range(0)))
self.assertFalse(lazylist.List([]))
def test_for_true(self):
self.assertTrue(lazylist.List(range(1)))
self.assertTrue(lazylist.List([1]))
class TestExtend(unittest.TestCase):
def test_two_range(self):
lazy = lazylist.List(range(10))
lazy[3]
lazy.extend(range(10, 20))
for i in range(20):
self.assertEqual(lazy[i], i)
def test_iadd(self):
lazy = lazylist.List(range(10))
lazy[3]
lazy += range(10, 20)
for i in range(20):
self.assertEqual(lazy[i], i)
class TestRepr(unittest.TestCase):
def test_simple(self):
lazy = lazylist.List(range(3))
self.assertEqual(repr(lazy), '[0, 1, 2]')
class TestEquality(unittest.TestCase):
def test_should_equal(self):
a = lazylist.List(range(3))
b = lazylist.List(range(3))
self.assertTrue(a == b)
self.assertFalse(a != b)
def test_totally_different(self):
a = lazylist.List(range(3))
b = lazylist.List(range(3, 10))
self.assertFalse(a == b)
self.assertEqual(len(a._list), 1)
self.assertEqual(len(b._list), 1)
self.assertTrue(a != b)
def test_different_length(self):
a = lazylist.List(range(range_size))
b = lazylist.List(range(range_size + 1))
self.assertFalse(a == b)
self.assertTrue(a != b)
def test_with_list(self):
a = lazylist.List(range(range_size))
b = list(range(range_size))
self.assertTrue(a == b)
self.assertFalse(a != b)
class TestReversed(unittest.TestCase):
def test_backwards_range(self):
lazy = lazylist.List(range(range_size))
for i, v in zip(range(9, -1, -1), reversed(lazy)):
self.assertEqual(i, v)
def test_in_place(self):
lazy = lazylist.List(range(range_size))
lazy.reverse()
for i, v in zip(range(9, -1, -1), lazy):
self.assertEqual(i, v)
class TestSort(unittest.TestCase):
def test_sort(self):
lazy = lazylist.List(range(range_size))
lazy.sort()
for i in range(len(lazy) - 1):
self.assertLess(lazy[i], lazy[i] + 1)
class TestPop(unittest.TestCase):
def test_pop_default(self):
lazy = lazylist.List(range(range_size))
for i in range(range_size):
self.assertEqual(len(lazy), range_size - i)
self.assertEqual(lazy.pop(), range_size - i - 1)
self.assertEqual(len(lazy), range_size - i - 1)
class TestIndex(unittest.TestCase):
def test_index(self):
lazy = lazylist.List(range(range_size))
self.assertEqual(lazy.index(5), 5)
self.assertEqual(len(lazy._list), 6)
self.assertRaises(ValueError, lazy.index, 10)
self.assertRaises(ValueError, lazy.index, -1)
def test_index_with_bounds(self):
lazy = lazylist.List(range(range_size))
self.assertEqual(lazy.index(9, 5), 9)
self.assertEqual(lazy.index(2, 0, -2), 2)
self.assertEqual(lazy.index(5, -7, -2), 5)
class TestCount(unittest.TestCase):
def test_count(self):
lazy = lazylist.List(
itertools.chain.from_iterable([i]*i for i in range(range_size)))
for i in range(range_size):
self.assertEqual(i, lazy.count(i))
class TestRemove(unittest.TestCase):
def test_remove(self):
lazy = lazylist.List(range(range_size))
for i in range(range_size):
lazy.remove(i)
self.assertEqual(len(lazy), 0)
def test_remove_fails(self):
lazy = lazylist.List(range(range_size))
for i in range(range_size, range_size*2):
self.assertRaises(ValueError, lazy.remove, i)
class TestInsert(unittest.TestCase):
def test_insert(self):
lazy = lazylist.List(range(range_size))
lazy.insert(2, 'a')
self.assertEqual(lazy[2], 'a')
lazy.insert(10, 'b')
self.assertEqual(lazy[10], 'b')
def test_insert_negative(self):
lazy = lazylist.List(range(range_size))
lazy.insert(-1, 'c')
self.assertEqual(lazy[-2], 'c')
class TestAppend(unittest.TestCase):
def test_append(self):
lazy = lazylist.List(range(range_size // 2))
for i in range(range_size // 2, range_size):
lazy.append(i)
for i, j in zip(lazy, range(range_size)):
self.assertEqual(i, j)
class TestClear(unittest.TestCase):
def test_unevaluated(self):
lazy = lazylist.List(range(range_size))
lazy.clear()
self.assertEqual(len(lazy), 0)
def test_evaluated(self):
lazy = lazylist.List(range(range_size))
len(lazy)
lazy.clear()
self.assertEqual(len(lazy), 0)
def test_clear_and_add(self):
lazy = lazylist.List(range(range_size))
lazy.clear()
self.assertEqual(len(lazy), 0)
lazy.extend(range(range_size))
self.assertEqual(len(lazy), range_size)
class TestLessThan(unittest.TestCase):
def test_simple(self):
a = lazylist.List(range(1, range_size + 1))
b = lazylist.List(range(range_size))
self.assertTrue(b < a)
self.assertFalse(a < b)
def test_uneven(self):
a = lazylist.List(range(range_size))
b = lazylist.List(range(range_size - 1))
self.assertTrue(b < a)
self.assertFalse(a < b)
def test_unordered(self):
a = lazylist.List([1,9])
b = lazylist.List([2, -1])
self.assertTrue(a < b)
self.assertFalse(b < a)
if __name__ == '__main__':
unittest.main()