Skip to content

Commit 640bda9

Browse files
committed
tests: Add CacheInstances tests
1 parent fe711ec commit 640bda9

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

tests/test_ir.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from devito.ir.support.basic import (IterationInstance, TimedAccess, Scope,
1313
Vector, AFFINE, REGULAR, IRREGULAR, mocksym0,
1414
mocksym1)
15+
from devito.ir.support.caching import CacheInstances
1516
from devito.ir.support.space import (NullInterval, Interval, Forward, Backward,
1617
IntervalGroup, IterationSpace)
1718
from devito.ir.support.guards import GuardOverflow
@@ -1101,3 +1102,46 @@ def test_guard_overflow(self):
11011102
guard = GuardOverflow(freespace, size)
11021103

11031104
assert ccode(guard) == 'freespace >= f_vec->size[0]*f_vec->size[1]'
1105+
1106+
1107+
class TestCacheInstances:
1108+
1109+
def test_caching(self):
1110+
"""
1111+
Tests basic functionality of cached instances.
1112+
"""
1113+
class Object(CacheInstances):
1114+
def __init__(self, value: int):
1115+
self.value = value
1116+
1117+
obj1 = Object(1)
1118+
obj2 = Object(1)
1119+
obj3 = Object(2)
1120+
1121+
assert obj1 is obj2
1122+
assert obj1 is not obj3
1123+
1124+
def test_cache_size(self):
1125+
"""
1126+
Tests specifying the size of the instance cache.
1127+
"""
1128+
class Object(CacheInstances):
1129+
_instance_cache_size = 2
1130+
1131+
def __init__(self, value: int):
1132+
self.value = value
1133+
1134+
obj1 = Object(1)
1135+
obj2 = Object(2)
1136+
obj3 = Object(3)
1137+
obj4 = Object(1)
1138+
obj5 = Object(3)
1139+
1140+
# obj1 should have been evicted before obj4 was created
1141+
assert obj1 is not obj4
1142+
assert obj1 is not obj2
1143+
assert obj3 is obj5
1144+
1145+
hits, _, _, cursize = Object._instance_cache.cache_info()
1146+
assert hits == 1 # obj5 hit the cache
1147+
assert cursize == 2

0 commit comments

Comments
 (0)