Skip to content

Commit 3fb07dd

Browse files
committed
fix bugs
1 parent a76832f commit 3fb07dd

File tree

1 file changed

+61
-29
lines changed

1 file changed

+61
-29
lines changed

test.py

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,51 @@ def setUp(self):
3535
def tearDown(self):
3636
memoization.clear()
3737

38+
@staticmethod
39+
def _wrapped_func_id(func):
40+
"""
41+
Get the id of the function wrapped by decorator
42+
:param func: The inspecting function
43+
:return: id of the function wrapped by the decorator of func
44+
"""
45+
return id(memoization._retrieve_undecorated_function(func))
46+
47+
def _make_cache_key(self, *args, **kwargs):
48+
"""
49+
Convert arguments to cache key
50+
51+
e.g.
52+
_make_cache_key(1, 2) == '(<__main__.TestedFunctions instance at 0x100000000>, 1, 2)'
53+
_make_cache_key(1, 2, a=3) == '(<__main__.TestedFunctions instance at 0x100000000>, 1, 2, {a: 3})'
54+
55+
:param args: function args
56+
:param kwargs: function kwargs
57+
:return: cache key
58+
"""
59+
result = [self.f]
60+
for arg in args:
61+
result.append(arg)
62+
if len(kwargs) != 0:
63+
result.append(kwargs)
64+
return str(tuple(result))
65+
3866
def test_cache(self):
3967
"""
4068
Test memoization.cached
4169
"""
4270

4371
# test add
4472
self.assertEqual(self.f.add(1, 2), 3)
45-
self.assertEqual(memoization._cache.get(id(self.f.add)), {(1, 2): 3})
46-
self.assertEqual(memoization._access_count.get(id(self.f.add)), {(1, 2): 0})
73+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.add)), {str((self.f, 1, 2)): 3})
74+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 0})
4775
self.assertEqual(self.f.add(1, 2), 3)
48-
self.assertEqual(memoization._access_count.get(id(self.f.add)), {(1, 2): 1})
76+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 1})
4977

5078
# test function_with_side_effects
5179
self.assertEqual(self.f.function_with_side_effects(1), 1)
52-
self.assertEqual(self.f.number, 2)
80+
self.assertEqual(self.f.number, 1)
5381
self.assertEqual(self.f.function_with_side_effects(1), 1) # the real function should not be executed
54-
self.assertEqual(self.f.number, 2)
82+
self.assertEqual(self.f.number, 1)
5583

5684
def _make_cache(self):
5785
"""
@@ -71,72 +99,76 @@ def test_make_cache(self):
7199
Test self._make_cache
72100
"""
73101
self._make_cache()
74-
self.assertEqual(memoization._cache.get(id(self.f.add)), {(1, 2): 3, (3, 4): 7, (5, 6): 11})
75-
self.assertEqual(memoization._cache.get(id(self.f.subtract)), {(10, 5): 5, (20, 10): 10, (30, 20): 10})
76-
self.assertEqual(memoization._access_count.get(id(self.f.add)), {(1, 2): 5, (3, 4): 4, (5, 6): 0})
77-
self.assertEqual(memoization._access_count.get(id(self.f.subtract)), {(10, 5): 5, (20, 10): 4, (30, 20): 0})
102+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 3, self._make_cache_key(3, 4): 7, self._make_cache_key(5, 6): 11})
103+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5, self._make_cache_key(20, 10): 10, self._make_cache_key(30, 20): 10})
104+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 5, self._make_cache_key(3, 4): 4, self._make_cache_key(5, 6): 0})
105+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5, self._make_cache_key(20, 10): 4, self._make_cache_key(30, 20): 0})
78106

79107
def test_clean_with_safe_access_count_and_func_restriction(self):
80108
"""
81109
Test memoization.clean with safe_access_count and func restriction
82110
"""
83111
self._make_cache()
84112
memoization.clean(safe_access_count=5, func=self.f.add)
85-
self.assertEqual(memoization._cache, {id(self.f.add): {(1, 2): 3},
86-
id(self.f.subtract): {(10, 5): 5, (20, 10): 10, (30, 20): 10}})
87-
self.assertEqual(memoization._access_count, {id(self.f.add): {(1, 2): 5},
88-
id(self.f.subtract): {(10, 5): 5, (20, 10): 4, (30, 20): 0}})
113+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 3})
114+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5, self._make_cache_key(20, 10): 10, self._make_cache_key(30, 20): 10})
115+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 5})
116+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5, self._make_cache_key(20, 10): 4, self._make_cache_key(30, 20): 0})
89117

90118
def test_clean_with_safe_access_count_restriction(self):
91119
"""
92120
Test memoization.clean with safe_access_count restriction
93121
"""
94122
self._make_cache()
95123
memoization.clean(safe_access_count=5)
96-
self.assertEqual(memoization._cache, {id(self.f.add): {(1, 2): 3},
97-
id(self.f.subtract): {(10, 5): 5}})
98-
self.assertEqual(memoization._access_count, {id(self.f.add): {(1, 2): 5},
99-
id(self.f.subtract): {(10, 5): 5}})
124+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 3})
125+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5})
126+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 5})
127+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.subtract)),{self._make_cache_key(10, 5): 5})
100128

101129
def test_clean_with_func_restriction(self):
102130
"""
103131
Test memoization.clean with func restriction
104132
"""
105133
self._make_cache()
106134
memoization.clean(func=self.f.add)
107-
self.assertEqual(memoization._cache, {id(self.f.add): {(1, 2): 3, (3, 4): 7},
108-
id(self.f.subtract): {(10, 5): 5, (20, 10): 10, (30, 20): 10}})
109-
self.assertEqual(memoization._access_count, {id(self.f.add): {(1, 2): 5, (3, 4): 4},
110-
id(self.f.subtract): {(10, 5): 5, (20, 10): 4, (30, 20): 0}})
135+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 3, self._make_cache_key(3, 4): 7})
136+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5, self._make_cache_key(20, 10): 10, self._make_cache_key(30, 20): 10})
137+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 5, self._make_cache_key(3, 4): 4})
138+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5, self._make_cache_key(20, 10): 4, self._make_cache_key(30, 20): 0})
111139

112140
def test_clean_without_restriction(self):
113141
"""
114142
Test memoization.clean with default arguments
115143
"""
116144
self._make_cache()
117145
memoization.clean()
118-
self.assertEqual(memoization._cache, {id(self.f.add): {(1, 2): 3, (3, 4): 7},
119-
id(self.f.subtract): {(10, 5): 5, (20, 10): 10}})
120-
self.assertEqual(memoization._access_count, {id(self.f.add): {(1, 2): 5, (3, 4): 4},
121-
id(self.f.subtract): {(10, 5): 5, (20, 10): 4}})
146+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 3, self._make_cache_key(3, 4): 7})
147+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5, self._make_cache_key(20, 10): 10})
148+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.add)), {self._make_cache_key(1, 2): 5, self._make_cache_key(3, 4): 4})
149+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5, self._make_cache_key(20, 10): 4})
122150

123151
def test_clear_with_func_restriction(self):
124152
"""
125153
Test memoization.clear with func restriction
126154
"""
127155
self._make_cache()
128156
memoization.clear(func=self.f.add)
129-
self.assertEqual(memoization._cache, {id(self.f.subtract): {(10, 5): 5, (20, 10): 10, (30, 20): 10}})
130-
self.assertEqual(memoization._access_count, {id(self.f.subtract): {(10, 5): 5, (20, 10): 4, (30, 20): 0}})
157+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.add)), {})
158+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5, self._make_cache_key(20, 10): 10, self._make_cache_key(30, 20): 10})
159+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.add)), {})
160+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.subtract)), {self._make_cache_key(10, 5): 5, self._make_cache_key(20, 10): 4, self._make_cache_key(30, 20): 0})
131161

132162
def test_clear_without_restriction(self):
133163
"""
134164
Test memoization.clear without any restriction
135165
"""
136166
self._make_cache()
137167
memoization.clear()
138-
self.assertEqual(memoization._cache, {})
139-
self.assertEqual(memoization._access_count, {})
168+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.add)), {})
169+
self.assertEqual(memoization._cache.get(self._wrapped_func_id(self.f.subtract)), {})
170+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.add)), {})
171+
self.assertEqual(memoization._access_count.get(self._wrapped_func_id(self.f.subtract)), {})
140172

141173
def test_size(self):
142174
"""

0 commit comments

Comments
 (0)