2021-02-15 17:52:06 +00:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore')
|
|
|
|
from pyqtgraph.util.lru_cache import LRUCache
|
|
|
|
|
2014-03-05 14:16:53 +00:00
|
|
|
|
|
|
|
def testLRU():
|
|
|
|
lru = LRUCache(2, 1)
|
|
|
|
# check twice
|
|
|
|
checkLru(lru)
|
|
|
|
checkLru(lru)
|
|
|
|
|
|
|
|
def checkLru(lru):
|
|
|
|
lru[1] = 1
|
|
|
|
lru[2] = 2
|
|
|
|
lru[3] = 3
|
|
|
|
|
|
|
|
assert len(lru) == 2
|
|
|
|
assert set([2, 3]) == set(lru.keys())
|
|
|
|
assert set([2, 3]) == set(lru.values())
|
|
|
|
|
|
|
|
lru[2] = 2
|
|
|
|
assert set([2, 3]) == set(lru.values())
|
2021-05-28 14:39:00 +00:00
|
|
|
|
2014-03-05 14:16:53 +00:00
|
|
|
lru[1] = 1
|
|
|
|
set([2, 1]) == set(lru.values())
|
|
|
|
|
|
|
|
#Iterates from the used in the last access to others based on access time.
|
2018-05-15 23:37:20 +00:00
|
|
|
assert [(2, 2), (1, 1)] == list(lru.items(accessTime=True))
|
2014-03-05 14:16:53 +00:00
|
|
|
lru[2] = 2
|
2018-05-15 23:37:20 +00:00
|
|
|
assert [(1, 1), (2, 2)] == list(lru.items(accessTime=True))
|
2014-03-05 14:16:53 +00:00
|
|
|
|
|
|
|
del lru[2]
|
2018-05-15 23:37:20 +00:00
|
|
|
assert [(1, 1), ] == list(lru.items(accessTime=True))
|
2014-03-05 14:16:53 +00:00
|
|
|
|
|
|
|
lru[2] = 2
|
2018-05-15 23:37:20 +00:00
|
|
|
assert [(1, 1), (2, 2)] == list(lru.items(accessTime=True))
|
2014-03-05 14:16:53 +00:00
|
|
|
|
2021-05-28 14:39:00 +00:00
|
|
|
_ = lru[1]
|
2018-05-15 23:37:20 +00:00
|
|
|
assert [(2, 2), (1, 1)] == list(lru.items(accessTime=True))
|
2014-03-05 14:16:53 +00:00
|
|
|
|
2021-05-28 14:39:00 +00:00
|
|
|
_ = lru[2]
|
2018-05-15 23:37:20 +00:00
|
|
|
assert [(1, 1), (2, 2)] == list(lru.items(accessTime=True))
|
2014-03-05 14:16:53 +00:00
|
|
|
|
|
|
|
assert lru.get(2) == 2
|
2021-05-28 14:39:00 +00:00
|
|
|
assert lru.get(3) is None
|
2018-05-15 23:37:20 +00:00
|
|
|
assert [(1, 1), (2, 2)] == list(lru.items(accessTime=True))
|
2014-03-05 14:16:53 +00:00
|
|
|
|
|
|
|
lru.clear()
|
2018-05-15 23:37:20 +00:00
|
|
|
assert [] == list(lru.items())
|
2014-03-05 14:16:53 +00:00
|
|
|
|