Jim Paris
f355c73209
There's some bug with the testing harness where placing e.g. from du import du in nilmdb/utils/__init__.py doesn't quite work -- sometimes the module "du" replaces the function "du". Not exactly sure why; we work around that by just renaming files so they don't match the imported names directly.
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
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 ])
|