import nilmdb from nilmdb.utils.printf import * import nose from nose.tools import * from nose.tools import assert_raises import threading import time from test_helpers import * @nilmdb.utils.lru_cache(size = 3) def foo1(n): return n @nilmdb.utils.lru_cache(size = 5) def foo2(n): return n def foo3d(n): foo3d.destructed.append(n) foo3d.destructed = [] @nilmdb.utils.lru_cache(size = 3, onremove = foo3d) def foo3(n): return n class TestLRUCache(object): def test(self): [ foo1(n) for n in [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ] ] eq_((foo1.cache_hits, foo1.cache_misses), (6, 3)) [ foo1(n) for n in [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ] ] eq_((foo1.cache_hits, foo1.cache_misses), (15, 3)) [ foo1(n) for n in [ 4, 2, 1, 1, 4 ] ] eq_((foo1.cache_hits, foo1.cache_misses), (18, 5)) [ foo2(n) for n in [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ] ] eq_((foo2.cache_hits, foo2.cache_misses), (6, 3)) [ foo2(n) for n in [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ] ] eq_((foo2.cache_hits, foo2.cache_misses), (15, 3)) [ foo2(n) for n in [ 4, 2, 1, 1, 4 ] ] eq_((foo2.cache_hits, foo2.cache_misses), (19, 4)) [ foo3(n) for n in [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ] ] eq_((foo3.cache_hits, foo3.cache_misses), (6, 3)) [ foo3(n) for n in [ 1, 2, 3, 1, 2, 3, 1, 2, 3 ] ] eq_((foo3.cache_hits, foo3.cache_misses), (15, 3)) [ foo3(n) for n in [ 4, 2, 1, 1, 4 ] ] eq_((foo3.cache_hits, foo3.cache_misses), (18, 5)) eq_(foo3d.destructed, [1, 3]) foo3.cache_remove(1) eq_(foo3d.destructed, [1, 3, 1]) foo3.cache_remove_all() eq_(foo3d.destructed, [1, 3, 1, 2, 4 ])