-
Notifications
You must be signed in to change notification settings - Fork 0
/
memo_decorator_test.py
339 lines (309 loc) · 10.1 KB
/
memo_decorator_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
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
import unittest
import time
from functools import reduce
import threading
from memoization.memo_decorator import memo
def get_stats(f):
return (f.calls, f.hits, f.invalidates)
g1 = 1
def gf(x):
return x
class GlobalClass():
pass
class TestMemoDecorator(unittest.TestCase):
def test_global_variable_changes(self):
global g1
@memo
def f(i):
return (i, g1)
# to invalidates cache
g1 = time.time()
f(0)
initial_invalidates = f.invalidates
g1 = 1
self.assertEqual(f(1), (1, 1))
self.assertEqual(get_stats(f), (2, 0, initial_invalidates + 1))
self.assertEqual(f(1), (1, 1))
self.assertEqual(get_stats(f), (3, 1, initial_invalidates + 1))
self.assertEqual(f(2), (2, 1))
self.assertEqual(get_stats(f), (4, 1, initial_invalidates + 1))
self.assertEqual(f(1), (1, 1))
self.assertEqual(get_stats(f), (5, 2, initial_invalidates + 1))
g1 = 2
self.assertEqual(f(1), (1, 2))
self.assertEqual(get_stats(f), (6, 2, initial_invalidates + 2))
g1 = 1
self.assertEqual(f(1), (1, 1))
self.assertEqual(get_stats(f), (7, 2, initial_invalidates + 3))
def test_free_variable_changes(self):
free = 1
@memo
def f(i):
return (i, free)
# to invalidates cache
free = time.time()
f(0)
initial_invalidates = f.invalidates
free = 1
self.assertEqual(f(1), (1, 1))
self.assertEqual(get_stats(f), (2, 0, initial_invalidates + 1))
self.assertEqual(f(1), (1, 1))
self.assertEqual(get_stats(f), (3, 1, initial_invalidates + 1))
self.assertEqual(f(2), (2, 1))
self.assertEqual(get_stats(f), (4, 1, initial_invalidates + 1))
self.assertEqual(f(1), (1, 1))
self.assertEqual(get_stats(f), (5, 2, initial_invalidates + 1))
free = 2
self.assertEqual(f(1), (1, 2))
self.assertEqual(get_stats(f), (6, 2, initial_invalidates + 2))
free = 1
self.assertEqual(f(1), (1, 1))
self.assertEqual(get_stats(f), (7, 2, initial_invalidates + 3))
def test_same_method_of_different_instances(self):
free = 0
class C:
@memo
def f(self):
return free
c1, c2 = C(), C()
# to invalidate cache
free = time.time()
c1.f()
initial_invalidates = c1.f.invalidates
c1.f()
self.assertEqual(get_stats(c1.f), (2, 1, initial_invalidates))
c2.f()
self.assertEqual(get_stats(c2.f), (3, 1, initial_invalidates))
c1.f()
self.assertEqual(get_stats(c1.f), (4, 2, initial_invalidates))
def test_equality_of_different_string_instances(self):
free = time.time()
@memo
def strlen(s):
dummy = free
return len(s)
strlen("")
initial_invalidates = strlen.invalidates
strlen("aaa")
self.assertEqual(get_stats(strlen), (2, 0, initial_invalidates))
strlen("aaa")
self.assertEqual(get_stats(strlen), (3, 1, initial_invalidates))
strlen("".join(["a", "aa"]))
self.assertEqual(get_stats(strlen), (4, 2, initial_invalidates))
strlen("bbb")
self.assertEqual(get_stats(strlen), (5, 2, initial_invalidates))
def test_equality_of_different_tuple_instances(self):
free = time.time()
@memo
def tuple_hash(t):
dummy = free
return hash(t)
tuple_hash(())
initial_invalidates = tuple_hash.invalidates
tuple_hash((1, "aaa"))
self.assertEqual(get_stats(tuple_hash), (2, 0, initial_invalidates))
tuple_hash((1, "".join(["a", "aa"])))
self.assertEqual(get_stats(tuple_hash), (3, 1, initial_invalidates))
tuple_hash((2, "aaa"))
self.assertEqual(get_stats(tuple_hash), (4, 1, initial_invalidates))
tuple_hash((2, "bbb"))
self.assertEqual(get_stats(tuple_hash), (5, 1, initial_invalidates))
tuple_hash((1, "aaa"))
self.assertEqual(get_stats(tuple_hash), (6, 2, initial_invalidates))
def test_deffensive_against_collision(self):
@memo
def sum(i, j):
return i + j
self.assertEqual(sum(0, 0), 0)
self.assertEqual(sum(0, 1), 1)
self.assertEqual(sum(1, 1), 2)
def test_redefining_function(self):
# same constants but different byte code
if True:
@memo
def f(n):
return n+100
self.assertEqual(101, f(1))
self.assertEqual(102, f(2))
if True:
@memo
def f(n):
return n-100
self.assertEqual(-99, f(1))
self.assertEqual(-98, f(2))
# same byte code but different constants
if True:
@memo
def g(n):
return n+10
self.assertEqual(11, g(1))
self.assertEqual(12, g(2))
if True:
@memo
def g(n):
return n+100
self.assertEqual(101, g(1))
self.assertEqual(102, g(2))
def test_list_comprehension_in_function(self):
@memo
def f(n):
l = [i for i in range(0, n+1)]
return sum(l)
self.assertEqual(55, f(10))
def test_recursive_function(self):
free = time.time()
@memo
def f(n):
dummy = free
if n <= 0:
return 0
return n + f(n-1)
f(0)
initial_invalidates = f.invalidates
self.assertEqual(55, f(10))
self.assertEqual((1+10+1, 1, initial_invalidates), get_stats(f))
self.assertEqual(15, f(5))
self.assertEqual((1+10+1+1, 2, initial_invalidates), get_stats(f))
def test_tail_recursive_function(self):
free = time.time()
@memo
def f(n, s=0):
dummy = free
if n <= 0:
return s
return f(n-1, s+n)
f(0)
initial_invalidates = f.invalidates
self.assertEqual(55, f(10))
self.assertEqual((1+10+1, 0, initial_invalidates), get_stats(f))
self.assertEqual(15, f(5))
self.assertEqual((1+10+1+5+1, 0, initial_invalidates), get_stats(f))
def test_mutual_recursive_function(self):
@memo
def f(n):
if n > 0:
return 1+g(n-1)
else:
return 0
@memo
def g(n):
if n > 0:
return 1+f(n-1)
else:
return 0
f(0)
g(0)
initial_invalidates_f = f.invalidates
initial_invalidates_g = g.invalidates
self.assertEqual(3, f(3))
self.assertEqual((1+2, 0, initial_invalidates_f), get_stats(f))
self.assertEqual((1+2, 1, initial_invalidates_g), get_stats(g))
def test_global_function_usage(self):
@memo
def f(x):
return gf(x)
self.assertEqual(1, f(1))
self.assertEqual(2, f(2))
def test_reduce_usage(self):
@memo
def f(*args):
def add(x, y):
return x+y
return reduce(add, args)
self.assertEqual(6, f(1, 2, 3))
def test_class_usage(self):
@memo
def f(s):
return GlobalClass.__name__ + s
self.assertEqual("GlobalClass-1", f("-1"))
def test_exception_handling(self):
@memo
def f(n):
if n == 0:
raise ValueError()
return f(n-1)
with self.assertRaises(ValueError):
f(1)
self.assertEqual(2, f.calls)
with self.assertRaises(ValueError):
f(2)
self.assertEqual(2+3, f.calls)
# TODO should not know implementation details
self.assertEqual(0, f.lock.thread_id)
self.assertEqual(0, f.lock.recursion_count)
def test_multithread_safety(self):
free = 0
@memo
def f(i):
return free + i
class Caller(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.stopRequested = False
self.lastError = None
def run(self):
i = 0
while not self.stopRequested:
try:
f(i)
except Exception as e:
self.lastError = e
i += 1
if i > 100:
i = 0
def stop(self):
self.stopRequested = True
callers = [Caller(), Caller()]
for t in callers:
t.start()
for i in range(10):
time.sleep(0.1)
free = i
for t in callers:
t.stop()
t.join()
for t in callers:
self.assertIsNone(t.lastError)
@unittest.skip("No support for lock ordering")
def test_multithread_mutual_recursive(self):
@memo
def f(n):
if n > 0:
return 1+g(n-1)
else:
return 0
@memo
def g(n):
if n > 0:
return 1+f(n-1)
else:
return 0
f(0)
g(0)
initial_invalidates_f = f.invalidates
initial_invalidates_g = g.invalidates
class Caller(threading.Thread):
def __init__(self, entry):
threading.Thread.__init__(self)
self.entry = entry
self.stopRequested = False
self.lastError = None
def run(self):
i = 0
while not self.stopRequested:
try:
self.entry(2 * i)
except Exception as e:
self.lastError = e
i += 1
def stop(self):
self.stopRequested = True
callers = [Caller(f), Caller(g)]
for t in callers:
t.start()
time.sleep(1)
for t in callers:
t.stop()
t.join()
for t in callers:
self.assertIsNone(t.lastError)