aboutsummaryrefslogtreecommitdiff
path: root/test/test_collections.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_collections.py')
-rw-r--r--test/test_collections.py55
1 files changed, 35 insertions, 20 deletions
diff --git a/test/test_collections.py b/test/test_collections.py
index 6cb5aca..098b31a 100644
--- a/test/test_collections.py
+++ b/test/test_collections.py
@@ -36,19 +36,7 @@ class TestLRUContainer(unittest.TestCase):
d[5] = '5'
# Check state
- self.assertEqual(list(d.keys()), [0, 2, 3, 4, 5])
-
- def test_pruning(self):
- d = Container(5)
-
- for i in xrange(5):
- d[i] = str(i)
-
- # Contend 2 entries for the most-used slot to balloon the heap
- for i in xrange(100):
- d.get(i % 2)
-
- self.assertTrue(len(d.access_log) <= d.CLEANUP_FACTOR * d._maxsize)
+ self.assertEqual(list(d.keys()), [2, 3, 4, 0, 5])
def test_same_key(self):
d = Container(5)
@@ -57,10 +45,7 @@ class TestLRUContainer(unittest.TestCase):
d['foo'] = i
self.assertEqual(list(d.keys()), ['foo'])
-
- d._prune_invalidated_entries()
-
- self.assertEqual(len(d.access_log), 1)
+ self.assertEqual(len(d), 1)
def test_access_ordering(self):
d = Container(5)
@@ -68,13 +53,14 @@ class TestLRUContainer(unittest.TestCase):
for i in xrange(10):
d[i] = True
- self.assertEqual(d._get_ordered_access_keys(), [9,8,7,6,5])
+ # Keys should be ordered by access time
+ self.assertEqual(list(d.keys()), [5, 6, 7, 8, 9])
new_order = [7,8,6,9,5]
- for k in reversed(new_order):
+ for k in new_order:
d[k]
- self.assertEqual(d._get_ordered_access_keys(), new_order)
+ self.assertEqual(list(d.keys()), new_order)
def test_delete(self):
d = Container(5)
@@ -107,6 +93,35 @@ class TestLRUContainer(unittest.TestCase):
self.assertRaises(KeyError, lambda: d[5])
+ def test_disposal(self):
+ evicted_items = []
+
+ def dispose_func(arg):
+ # Save the evicted datum for inspection
+ evicted_items.append(arg)
+
+ d = Container(5, dispose_func=dispose_func)
+ for i in xrange(5):
+ d[i] = i
+ self.assertEqual(list(d.keys()), list(xrange(5)))
+ self.assertEqual(evicted_items, []) # Nothing disposed
+
+ d[5] = 5
+ self.assertEqual(list(d.keys()), list(xrange(1, 6)))
+ self.assertEqual(evicted_items, [0])
+
+ del d[1]
+ self.assertEqual(evicted_items, [0, 1])
+
+ d.clear()
+ self.assertEqual(evicted_items, [0, 1, 2, 3, 4, 5])
+
+ def test_iter(self):
+ d = Container()
+
+ with self.assertRaises(NotImplementedError):
+ for i in d:
+ self.fail("Iteration shouldn't be implemented.")
if __name__ == '__main__':
unittest.main()