summaryrefslogtreecommitdiffstats
path: root/src/engine/SCons/MemoizeTests.py
blob: 3606d576253de0485a17b5c9ae9a0e9c5bc71804 (plain)
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
#
# __COPYRIGHT__
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"

import sys
import unittest

import TestUnit

import SCons.Memoize



class FakeObject(object):

    __metaclass__ = SCons.Memoize.Memoized_Metaclass

    memoizer_counters = []

    def __init__(self):
        self._memo = {}

    def _dict_key(self, argument):
        return argument

    memoizer_counters.append(SCons.Memoize.CountDict('dict', _dict_key))

    def dict(self, argument):

        memo_key = argument
        try:
            memo_dict = self._memo['dict']
        except KeyError:
            memo_dict = {}
            self._memo['dict'] = memo_dict
        else:
            try:
                return memo_dict[memo_key]
            except KeyError:
                pass

        result = self.compute_dict(argument)

        memo_dict[memo_key] = result

        return result

    memoizer_counters.append(SCons.Memoize.CountValue('value'))

    def value(self):

        try:
            return self._memo['value']
        except KeyError:
            pass

        result = self.compute_value()

        self._memo['value'] = result

        return result

    def get_memoizer_counter(self, name):
        for mc in self.memoizer_counters:
            if mc.method_name == name:
                return mc
        return  None

class Returner(object):
    def __init__(self, result):
        self.result = result
        self.calls = 0
    def __call__(self, *args, **kw):
        self.calls = self.calls + 1
        return self.result


class CountDictTestCase(unittest.TestCase):

    def test___call__(self):
        """Calling a Memoized dict method
        """
        obj = FakeObject()

        called = []

        fd1 = Returner(1)
        fd2 = Returner(2)

        obj.compute_dict = fd1

        r = obj.dict(11)
        assert r == 1, r

        obj.compute_dict = fd2

        r = obj.dict(12)
        assert r == 2, r

        r = obj.dict(11)
        assert r == 1, r

        obj.compute_dict = fd1

        r = obj.dict(11)
        assert r == 1, r

        r = obj.dict(12)
        assert r == 2, r

        assert fd1.calls == 1, fd1.calls
        assert fd2.calls == 1, fd2.calls

        c = obj.get_memoizer_counter('dict')

        assert c.hit == 3, c.hit
        assert c.miss == 2, c.miss


class CountValueTestCase(unittest.TestCase):

    def test___call__(self):
        """Calling a Memoized value method
        """
        obj = FakeObject()

        called = []

        fv1 = Returner(1)
        fv2 = Returner(2)

        obj.compute_value = fv1

        r = obj.value()
        assert r == 1, r
        r = obj.value()
        assert r == 1, r

        obj.compute_value = fv2

        r = obj.value()
        assert r == 1, r
        r = obj.value()
        assert r == 1, r

        assert fv1.calls == 1, fv1.calls
        assert fv2.calls == 0, fv2.calls

        c = obj.get_memoizer_counter('value')

        assert c.hit == 3, c.hit
        assert c.miss == 1, c.miss


if __name__ == "__main__":
    suite = unittest.TestSuite()
    tclasses = [
        CountDictTestCase,
        CountValueTestCase,
    ]
    for tclass in tclasses:
        names = unittest.getTestCaseNames(tclass, 'test_')
        suite.addTests(list(map(tclass, names)))
    TestUnit.run(suite)

# Local Variables:
# tab-width:4
# indent-tabs-mode:nil
# End:
# vim: set expandtab tabstop=4 shiftwidth=4: