Remove redundant code
[scons.git] / SCons / MemoizeTests.py
blob825256ccfaf054532b3a1586c40ad7acce2ad567
1 # MIT License
3 # Copyright The SCons Foundation
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 import unittest
26 import SCons.Memoize
28 # Enable memoization counting
29 SCons.Memoize.EnableMemoization()
31 class FakeObject:
32 def __init__(self) -> None:
33 self._memo = {}
35 def _dict_key(self, argument):
36 return argument
38 @SCons.Memoize.CountDictCall(_dict_key)
39 def dict(self, argument):
41 memo_key = argument
42 try:
43 memo_dict = self._memo['dict']
44 except KeyError:
45 memo_dict = {}
46 self._memo['dict'] = memo_dict
47 else:
48 try:
49 return memo_dict[memo_key]
50 except KeyError:
51 pass
53 result = self.compute_dict(argument)
55 memo_dict[memo_key] = result
57 return result
59 @SCons.Memoize.CountMethodCall
60 def value(self):
62 try:
63 return self._memo['value']
64 except KeyError:
65 pass
67 result = self.compute_value()
69 self._memo['value'] = result
71 return result
73 def get_memoizer_counter(self, name):
74 return SCons.Memoize.CounterList.get(self.__class__.__name__+'.'+name, None)
76 class Returner:
77 def __init__(self, result) -> None:
78 self.result = result
79 self.calls = 0
80 def __call__(self, *args, **kw):
81 self.calls = self.calls + 1
82 return self.result
85 class CountDictTestCase(unittest.TestCase):
87 def test___call__(self) -> None:
88 """Calling a Memoized dict method
89 """
90 obj = FakeObject()
92 called = []
94 fd1 = Returner(1)
95 fd2 = Returner(2)
97 obj.compute_dict = fd1
99 r = obj.dict(11)
100 assert r == 1, r
102 obj.compute_dict = fd2
104 r = obj.dict(12)
105 assert r == 2, r
107 r = obj.dict(11)
108 assert r == 1, r
110 obj.compute_dict = fd1
112 r = obj.dict(11)
113 assert r == 1, r
115 r = obj.dict(12)
116 assert r == 2, r
118 assert fd1.calls == 1, fd1.calls
119 assert fd2.calls == 1, fd2.calls
121 c = obj.get_memoizer_counter('dict')
123 assert c.hit == 3, c.hit
124 assert c.miss == 2, c.miss
127 class CountValueTestCase(unittest.TestCase):
129 def test___call__(self) -> None:
130 """Calling a Memoized value method
132 obj = FakeObject()
134 called = []
136 fv1 = Returner(1)
137 fv2 = Returner(2)
139 obj.compute_value = fv1
141 r = obj.value()
142 assert r == 1, r
143 r = obj.value()
144 assert r == 1, r
146 obj.compute_value = fv2
148 r = obj.value()
149 assert r == 1, r
150 r = obj.value()
151 assert r == 1, r
153 assert fv1.calls == 1, fv1.calls
154 assert fv2.calls == 0, fv2.calls
156 c = obj.get_memoizer_counter('value')
158 assert c.hit == 3, c.hit
159 assert c.miss == 1, c.miss
162 if __name__ == "__main__":
163 unittest.main()
165 # Local Variables:
166 # tab-width:4
167 # indent-tabs-mode:nil
168 # End:
169 # vim: set expandtab tabstop=4 shiftwidth=4: