test-lrucachedict.py
221 lines
| 6.2 KiB
| text/x-python
|
PythonLexer
/ tests / test-lrucachedict.py
Pulkit Goyal
|
r28931 | from __future__ import absolute_import, print_function | ||
Pulkit Goyal
|
r28930 | |||
Gregory Szorc
|
r39598 | import unittest | ||
import silenttestrunner | ||||
Pulkit Goyal
|
r28930 | from mercurial import ( | ||
util, | ||||
) | ||||
Siddharth Agarwal
|
r18603 | |||
Gregory Szorc
|
r39598 | class testlrucachedict(unittest.TestCase): | ||
def testsimple(self): | ||||
d = util.lrucachedict(4) | ||||
Gregory Szorc
|
r39600 | self.assertEqual(d.capacity, 4) | ||
Gregory Szorc
|
r39603 | d.insert('a', 'va', cost=2) | ||
Gregory Szorc
|
r39598 | d['b'] = 'vb' | ||
d['c'] = 'vc' | ||||
Gregory Szorc
|
r39603 | d.insert('d', 'vd', cost=42) | ||
Siddharth Agarwal
|
r18603 | |||
Gregory Szorc
|
r39598 | self.assertEqual(d['a'], 'va') | ||
self.assertEqual(d['b'], 'vb') | ||||
self.assertEqual(d['c'], 'vc') | ||||
self.assertEqual(d['d'], 'vd') | ||||
Siddharth Agarwal
|
r18603 | |||
Gregory Szorc
|
r39603 | self.assertEqual(d.totalcost, 44) | ||
Gregory Szorc
|
r39598 | # 'a' should be dropped because it was least recently used. | ||
d['e'] = 've' | ||||
self.assertNotIn('a', d) | ||||
self.assertIsNone(d.get('a')) | ||||
Gregory Szorc
|
r39603 | self.assertEqual(d.totalcost, 42) | ||
Siddharth Agarwal
|
r18603 | |||
Gregory Szorc
|
r39598 | self.assertEqual(d['b'], 'vb') | ||
self.assertEqual(d['c'], 'vc') | ||||
self.assertEqual(d['d'], 'vd') | ||||
self.assertEqual(d['e'], 've') | ||||
Siddharth Agarwal
|
r18603 | |||
Gregory Szorc
|
r39603 | # Replacing item with different cost adjusts totalcost. | ||
d.insert('e', 've', cost=4) | ||||
self.assertEqual(d.totalcost, 46) | ||||
Gregory Szorc
|
r39598 | # Touch entries in some order (both get and set). | ||
d['e'] | ||||
d['c'] = 'vc2' | ||||
d['d'] | ||||
d['b'] = 'vb2' | ||||
Gregory Szorc
|
r29828 | |||
Gregory Szorc
|
r39598 | # 'e' should be dropped now | ||
d['f'] = 'vf' | ||||
self.assertNotIn('e', d) | ||||
self.assertEqual(d['b'], 'vb2') | ||||
self.assertEqual(d['c'], 'vc2') | ||||
self.assertEqual(d['d'], 'vd') | ||||
self.assertEqual(d['f'], 'vf') | ||||
Siddharth Agarwal
|
r18603 | |||
Gregory Szorc
|
r39598 | d.clear() | ||
for key in ('a', 'b', 'c', 'd', 'e', 'f'): | ||||
self.assertNotIn(key, d) | ||||
Siddharth Agarwal
|
r18603 | |||
Gregory Szorc
|
r39598 | def testunfull(self): | ||
d = util.lrucachedict(4) | ||||
d['a'] = 1 | ||||
d['b'] = 2 | ||||
d['a'] | ||||
d['b'] | ||||
for key in ('a', 'b'): | ||||
self.assertIn(key, d) | ||||
Siddharth Agarwal
|
r19710 | |||
Gregory Szorc
|
r39598 | def testcopypartial(self): | ||
d = util.lrucachedict(4) | ||||
Gregory Szorc
|
r39603 | d.insert('a', 'va', cost=4) | ||
d.insert('b', 'vb', cost=2) | ||||
Gregory Szorc
|
r39598 | |||
dc = d.copy() | ||||
self.assertEqual(len(dc), 2) | ||||
Gregory Szorc
|
r39603 | self.assertEqual(dc.totalcost, 6) | ||
Gregory Szorc
|
r39598 | for key in ('a', 'b'): | ||
self.assertIn(key, dc) | ||||
self.assertEqual(dc[key], 'v%s' % key) | ||||
Gregory Szorc
|
r27371 | |||
Gregory Szorc
|
r39599 | self.assertEqual(len(d), 2) | ||
for key in ('a', 'b'): | ||||
self.assertIn(key, d) | ||||
self.assertEqual(d[key], 'v%s' % key) | ||||
d['c'] = 'vc' | ||||
del d['b'] | ||||
Gregory Szorc
|
r39603 | self.assertEqual(d.totalcost, 4) | ||
Gregory Szorc
|
r39599 | dc = d.copy() | ||
self.assertEqual(len(dc), 2) | ||||
Gregory Szorc
|
r39603 | self.assertEqual(dc.totalcost, 4) | ||
Gregory Szorc
|
r39599 | for key in ('a', 'c'): | ||
self.assertIn(key, dc) | ||||
self.assertEqual(dc[key], 'v%s' % key) | ||||
def testcopyempty(self): | ||||
d = util.lrucachedict(4) | ||||
dc = d.copy() | ||||
self.assertEqual(len(dc), 0) | ||||
Gregory Szorc
|
r39598 | def testcopyfull(self): | ||
d = util.lrucachedict(4) | ||||
Gregory Szorc
|
r39603 | d.insert('a', 'va', cost=42) | ||
Gregory Szorc
|
r39598 | d['b'] = 'vb' | ||
d['c'] = 'vc' | ||||
d['d'] = 'vd' | ||||
Eric Sumner
|
r27576 | |||
Gregory Szorc
|
r39598 | dc = d.copy() | ||
for key in ('a', 'b', 'c', 'd'): | ||||
self.assertIn(key, dc) | ||||
self.assertEqual(dc[key], 'v%s' % key) | ||||
Eric Sumner
|
r27576 | |||
Gregory Szorc
|
r39603 | self.assertEqual(d.totalcost, 42) | ||
self.assertEqual(dc.totalcost, 42) | ||||
Gregory Szorc
|
r39598 | # 'a' should be dropped because it was least recently used. | ||
dc['e'] = 've' | ||||
self.assertNotIn('a', dc) | ||||
for key in ('b', 'c', 'd', 'e'): | ||||
self.assertIn(key, dc) | ||||
self.assertEqual(dc[key], 'v%s' % key) | ||||
Eric Sumner
|
r27576 | |||
Gregory Szorc
|
r39603 | self.assertEqual(d.totalcost, 42) | ||
self.assertEqual(dc.totalcost, 0) | ||||
Gregory Szorc
|
r39598 | # Contents and order of original dict should remain unchanged. | ||
dc['b'] = 'vb_new' | ||||
Eric Sumner
|
r27576 | |||
Gregory Szorc
|
r39598 | self.assertEqual(list(iter(d)), ['d', 'c', 'b', 'a']) | ||
for key in ('a', 'b', 'c', 'd'): | ||||
self.assertEqual(d[key], 'v%s' % key) | ||||
Eric Sumner
|
r27576 | |||
Gregory Szorc
|
r39601 | def testcopydecreasecapacity(self): | ||
d = util.lrucachedict(5) | ||||
Gregory Szorc
|
r39603 | d.insert('a', 'va', cost=4) | ||
d.insert('b', 'vb', cost=2) | ||||
Gregory Szorc
|
r39601 | d['c'] = 'vc' | ||
d['d'] = 'vd' | ||||
dc = d.copy(2) | ||||
Gregory Szorc
|
r39603 | self.assertEqual(dc.totalcost, 0) | ||
Gregory Szorc
|
r39601 | for key in ('a', 'b'): | ||
self.assertNotIn(key, dc) | ||||
for key in ('c', 'd'): | ||||
self.assertIn(key, dc) | ||||
self.assertEqual(dc[key], 'v%s' % key) | ||||
Gregory Szorc
|
r39603 | dc.insert('e', 've', cost=7) | ||
self.assertEqual(dc.totalcost, 7) | ||||
Gregory Szorc
|
r39601 | self.assertNotIn('c', dc) | ||
for key in ('d', 'e'): | ||||
self.assertIn(key, dc) | ||||
self.assertEqual(dc[key], 'v%s' % key) | ||||
# Original should remain unchanged. | ||||
Gregory Szorc
|
r39603 | self.assertEqual(d.totalcost, 6) | ||
Gregory Szorc
|
r39601 | for key in ('a', 'b', 'c', 'd'): | ||
self.assertIn(key, d) | ||||
self.assertEqual(d[key], 'v%s' % key) | ||||
def testcopyincreasecapacity(self): | ||||
d = util.lrucachedict(5) | ||||
d['a'] = 'va' | ||||
d['b'] = 'vb' | ||||
d['c'] = 'vc' | ||||
d['d'] = 'vd' | ||||
dc = d.copy(6) | ||||
for key in ('a', 'b', 'c', 'd'): | ||||
self.assertIn(key, dc) | ||||
self.assertEqual(dc[key], 'v%s' % key) | ||||
dc['e'] = 've' | ||||
dc['f'] = 'vf' | ||||
for key in ('a', 'b', 'c', 'd', 'e', 'f'): | ||||
self.assertIn(key, dc) | ||||
self.assertEqual(dc[key], 'v%s' % key) | ||||
dc['g'] = 'vg' | ||||
self.assertNotIn('a', dc) | ||||
for key in ('b', 'c', 'd', 'e', 'f', 'g'): | ||||
self.assertIn(key, dc) | ||||
self.assertEqual(dc[key], 'v%s' % key) | ||||
# Original should remain unchanged. | ||||
for key in ('a', 'b', 'c', 'd'): | ||||
self.assertIn(key, d) | ||||
self.assertEqual(d[key], 'v%s' % key) | ||||
Gregory Szorc
|
r39602 | def testpopoldest(self): | ||
d = util.lrucachedict(4) | ||||
Gregory Szorc
|
r39603 | d.insert('a', 'va', cost=10) | ||
d.insert('b', 'vb', cost=5) | ||||
Gregory Szorc
|
r39602 | |||
self.assertEqual(len(d), 2) | ||||
self.assertEqual(d.popoldest(), ('a', 'va')) | ||||
self.assertEqual(len(d), 1) | ||||
Gregory Szorc
|
r39603 | self.assertEqual(d.totalcost, 5) | ||
Gregory Szorc
|
r39602 | self.assertEqual(d.popoldest(), ('b', 'vb')) | ||
self.assertEqual(len(d), 0) | ||||
Gregory Szorc
|
r39603 | self.assertEqual(d.totalcost, 0) | ||
Gregory Szorc
|
r39602 | self.assertIsNone(d.popoldest()) | ||
d['a'] = 'va' | ||||
d['b'] = 'vb' | ||||
d['c'] = 'vc' | ||||
d['d'] = 'vd' | ||||
self.assertEqual(d.popoldest(), ('a', 'va')) | ||||
self.assertEqual(len(d), 3) | ||||
for key in ('b', 'c', 'd'): | ||||
self.assertEqual(d[key], 'v%s' % key) | ||||
d['a'] = 'va' | ||||
self.assertEqual(d.popoldest(), ('b', 'vb')) | ||||
Siddharth Agarwal
|
r18603 | if __name__ == '__main__': | ||
Gregory Szorc
|
r39598 | silenttestrunner.main(__name__) | ||