1#!/usr/bin/env python3
2
3"""Test the memoized2 decorator."""
4
5import logging
6
7from chart.common.decorators import memoized2
8
9cc = 0
10
11class A(object):
12 """Test class which increments the `cc` global when the
13 `doubler` function is called."""
14 @memoized2
15 def doubler(self, value):
16 """."""
17 global cc
18 logging.info('A.doubling {v}'.format(v=value))
19 cc += 1
20 return value * 2
21
22
23@memoized2
24def b(value):
25 """Test function which increments the `cc` global when called."""
26 global cc
27 logging.info('doubling {v}'.format(v=value))
28 cc += 1
29 return value * 2
30
31
32def test_memoized():
33 """Test all memoized functions."""
34 a = A()
35
36 a.doubler(1)
37 assert cc == 1
38
39 a.doubler(2)
40 assert cc == 2
41
42 a.doubler(1)
43 assert cc == 2
44
45 b(5)
46 assert cc == 3
47
48 b(6)
49 assert cc == 4
50
51 b(5)
52 assert cc == 4
53
54if __name__ == '__main__':
55 from chart.common.log import init_log
56 init_log()
57 test_memoized()