This makes the code so much easier to test and debug.
Along the way, I discovered a bug in copy(), which I kind of
added test coverage for.
| hg-reviewers |
This makes the code so much easier to test and debug.
Along the way, I discovered a bug in copy(), which I kind of
added test coverage for.
| Lint Skipped |
| Unit Tests Skipped |
| Path | Packages | |||
|---|---|---|---|---|
| M | tests/test-lrucachedict.py (140 lines) | |||
| D | M | tests/test-lrucachedict.py.out (62 lines) |
| Commit | Parents | Author | Summary | Date |
|---|---|---|---|---|
| Gregory Szorc | Sep 6 2018, 2:27 PM |
| from __future__ import absolute_import, print_function | from __future__ import absolute_import, print_function | ||||
| import unittest | |||||
| import silenttestrunner | |||||
| from mercurial import ( | from mercurial import ( | ||||
| util, | util, | ||||
| ) | ) | ||||
| def printifpresent(d, xs, name='d'): | class testlrucachedict(unittest.TestCase): | ||||
| for x in xs: | def testsimple(self): | ||||
| present = x in d | |||||
| print("'%s' in %s: %s" % (x, name, present)) | |||||
| if present: | |||||
| print("%s['%s']: %s" % (name, x, d[x])) | |||||
| def test_lrucachedict(): | |||||
| d = util.lrucachedict(4) | d = util.lrucachedict(4) | ||||
| d['a'] = 'va' | d['a'] = 'va' | ||||
| d['b'] = 'vb' | d['b'] = 'vb' | ||||
| d['c'] = 'vc' | d['c'] = 'vc' | ||||
| d['d'] = 'vd' | d['d'] = 'vd' | ||||
| # all of these should be present | self.assertEqual(d['a'], 'va') | ||||
| printifpresent(d, ['a', 'b', 'c', 'd']) | self.assertEqual(d['b'], 'vb') | ||||
| self.assertEqual(d['c'], 'vc') | |||||
| self.assertEqual(d['d'], 'vd') | |||||
| # 'a' should be dropped because it was least recently used | # 'a' should be dropped because it was least recently used. | ||||
| d['e'] = 've' | d['e'] = 've' | ||||
| printifpresent(d, ['a', 'b', 'c', 'd', 'e']) | self.assertNotIn('a', d) | ||||
| self.assertIsNone(d.get('a')) | |||||
| assert d.get('a') is None | self.assertEqual(d['b'], 'vb') | ||||
| assert d.get('e') == 've' | self.assertEqual(d['c'], 'vc') | ||||
| self.assertEqual(d['d'], 'vd') | |||||
| self.assertEqual(d['e'], 've') | |||||
| # touch entries in some order (get or set). | # Touch entries in some order (both get and set). | ||||
| d['e'] | d['e'] | ||||
| d['c'] = 'vc2' | d['c'] = 'vc2' | ||||
| d['d'] | d['d'] | ||||
| d['b'] = 'vb2' | d['b'] = 'vb2' | ||||
| # 'e' should be dropped now | # 'e' should be dropped now | ||||
| d['f'] = 'vf' | d['f'] = 'vf' | ||||
| printifpresent(d, ['b', 'c', 'd', 'e', 'f']) | self.assertNotIn('e', d) | ||||
| self.assertEqual(d['b'], 'vb2') | |||||
| self.assertEqual(d['c'], 'vc2') | |||||
| self.assertEqual(d['d'], 'vd') | |||||
| self.assertEqual(d['f'], 'vf') | |||||
| d.clear() | d.clear() | ||||
| printifpresent(d, ['b', 'c', 'd', 'e', 'f']) | for key in ('a', 'b', 'c', 'd', 'e', 'f'): | ||||
| self.assertNotIn(key, d) | |||||
| # Now test dicts that aren't full. | def testunfull(self): | ||||
| d = util.lrucachedict(4) | d = util.lrucachedict(4) | ||||
| d['a'] = 1 | d['a'] = 1 | ||||
| d['b'] = 2 | d['b'] = 2 | ||||
| d['a'] | d['a'] | ||||
| d['b'] | d['b'] | ||||
| printifpresent(d, ['a', 'b']) | |||||
| # test copy method | for key in ('a', 'b'): | ||||
| self.assertIn(key, d) | |||||
| def testcopypartial(self): | |||||
| d = util.lrucachedict(4) | |||||
| d['a'] = 'va' | |||||
| d['b'] = 'vb' | |||||
| dc = d.copy() | |||||
| self.assertEqual(len(dc), 2) | |||||
| # TODO this fails | |||||
| return | |||||
| for key in ('a', 'b'): | |||||
| self.assertIn(key, dc) | |||||
| self.assertEqual(dc[key], 'v%s' % key) | |||||
| def testcopyfull(self): | |||||
| d = util.lrucachedict(4) | d = util.lrucachedict(4) | ||||
| d['a'] = 'va3' | d['a'] = 'va' | ||||
| d['b'] = 'vb3' | d['b'] = 'vb' | ||||
| d['c'] = 'vc3' | d['c'] = 'vc' | ||||
| d['d'] = 'vd3' | d['d'] = 'vd' | ||||
| dc = d.copy() | dc = d.copy() | ||||
| # all of these should be present | for key in ('a', 'b', 'c', 'd'): | ||||
| print("\nAll of these should be present:") | self.assertIn(key, dc) | ||||
| printifpresent(dc, ['a', 'b', 'c', 'd'], 'dc') | self.assertEqual(dc[key], 'v%s' % key) | ||||
| # 'a' should be dropped because it was least recently used | # 'a' should be dropped because it was least recently used. | ||||
| print("\nAll of these except 'a' should be present:") | dc['e'] = 've' | ||||
| dc['e'] = 've3' | self.assertNotIn('a', dc) | ||||
| printifpresent(dc, ['a', 'b', 'c', 'd', 'e'], 'dc') | for key in ('b', 'c', 'd', 'e'): | ||||
| self.assertIn(key, dc) | |||||
| # contents and order of original dict should remain unchanged | self.assertEqual(dc[key], 'v%s' % key) | ||||
| print("\nThese should be in reverse alphabetical order and read 'v?3':") | |||||
| dc['b'] = 'vb3_new' | # Contents and order of original dict should remain unchanged. | ||||
| for k in list(iter(d)): | dc['b'] = 'vb_new' | ||||
| print("d['%s']: %s" % (k, d[k])) | |||||
| self.assertEqual(list(iter(d)), ['d', 'c', 'b', 'a']) | |||||
| for key in ('a', 'b', 'c', 'd'): | |||||
| self.assertEqual(d[key], 'v%s' % key) | |||||
| if __name__ == '__main__': | if __name__ == '__main__': | ||||
| test_lrucachedict() | silenttestrunner.main(__name__) | ||||
| 'a' in d: True | |||||
| d['a']: va | |||||
| 'b' in d: True | |||||
| d['b']: vb | |||||
| 'c' in d: True | |||||
| d['c']: vc | |||||
| 'd' in d: True | |||||
| d['d']: vd | |||||
| 'a' in d: False | |||||
| 'b' in d: True | |||||
| d['b']: vb | |||||
| 'c' in d: True | |||||
| d['c']: vc | |||||
| 'd' in d: True | |||||
| d['d']: vd | |||||
| 'e' in d: True | |||||
| d['e']: ve | |||||
| 'b' in d: True | |||||
| d['b']: vb2 | |||||
| 'c' in d: True | |||||
| d['c']: vc2 | |||||
| 'd' in d: True | |||||
| d['d']: vd | |||||
| 'e' in d: False | |||||
| 'f' in d: True | |||||
| d['f']: vf | |||||
| 'b' in d: False | |||||
| 'c' in d: False | |||||
| 'd' in d: False | |||||
| 'e' in d: False | |||||
| 'f' in d: False | |||||
| 'a' in d: True | |||||
| d['a']: 1 | |||||
| 'b' in d: True | |||||
| d['b']: 2 | |||||
| All of these should be present: | |||||
| 'a' in dc: True | |||||
| dc['a']: va3 | |||||
| 'b' in dc: True | |||||
| dc['b']: vb3 | |||||
| 'c' in dc: True | |||||
| dc['c']: vc3 | |||||
| 'd' in dc: True | |||||
| dc['d']: vd3 | |||||
| All of these except 'a' should be present: | |||||
| 'a' in dc: False | |||||
| 'b' in dc: True | |||||
| dc['b']: vb3 | |||||
| 'c' in dc: True | |||||
| dc['c']: vc3 | |||||
| 'd' in dc: True | |||||
| dc['d']: vd3 | |||||
| 'e' in dc: True | |||||
| dc['e']: ve3 | |||||
| These should be in reverse alphabetical order and read 'v?3': | |||||
| d['d']: vd3 | |||||
| d['c']: vc3 | |||||
| d['b']: vb3 | |||||
| d['a']: va3 | |||||