2012-12-31 12:38:32 -05:00
|
|
|
import nilmdb
|
2012-12-31 15:52:28 -05:00
|
|
|
from nilmdb.utils.printf import *
|
2012-12-31 12:38:32 -05:00
|
|
|
|
|
|
|
import nose
|
|
|
|
from nose.tools import *
|
|
|
|
from nose.tools import assert_raises
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
|
|
|
from test_helpers import *
|
|
|
|
|
2012-12-31 15:52:28 -05:00
|
|
|
@nilmdb.utils.lru_cache(size = 3)
|
2012-12-31 12:38:32 -05:00
|
|
|
def foo1(n):
|
|
|
|
return n
|
|
|
|
|
2012-12-31 15:52:28 -05:00
|
|
|
@nilmdb.utils.lru_cache(size = 5)
|
2012-12-31 12:38:32 -05:00
|
|
|
def foo2(n):
|
|
|
|
return n
|
|
|
|
|
|
|
|
def foo3d(n):
|
|
|
|
foo3d.destructed.append(n)
|
|
|
|
foo3d.destructed = []
|
2012-12-31 15:52:28 -05:00
|
|
|
@nilmdb.utils.lru_cache(size = 3, onremove = foo3d)
|
2012-12-31 12:38:32 -05:00
|
|
|
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 ])
|