Mercurial > hg
comparison tests/test-lrucachedict.py @ 39562:067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
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.
Differential Revision: https://phab.mercurial-scm.org/D4497
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Thu, 06 Sep 2018 11:27:25 -0700 |
parents | 79add5a4e857 |
children | b31b01f93b11 |
comparison
equal
deleted
inserted
replaced
39561:d06834e0f48e | 39562:067f7d2c7d60 |
---|---|
1 from __future__ import absolute_import, print_function | 1 from __future__ import absolute_import, print_function |
2 | |
3 import unittest | |
4 | |
5 import silenttestrunner | |
2 | 6 |
3 from mercurial import ( | 7 from mercurial import ( |
4 util, | 8 util, |
5 ) | 9 ) |
6 | 10 |
7 def printifpresent(d, xs, name='d'): | 11 class testlrucachedict(unittest.TestCase): |
8 for x in xs: | 12 def testsimple(self): |
9 present = x in d | 13 d = util.lrucachedict(4) |
10 print("'%s' in %s: %s" % (x, name, present)) | 14 d['a'] = 'va' |
11 if present: | 15 d['b'] = 'vb' |
12 print("%s['%s']: %s" % (name, x, d[x])) | 16 d['c'] = 'vc' |
17 d['d'] = 'vd' | |
13 | 18 |
14 def test_lrucachedict(): | 19 self.assertEqual(d['a'], 'va') |
15 d = util.lrucachedict(4) | 20 self.assertEqual(d['b'], 'vb') |
16 d['a'] = 'va' | 21 self.assertEqual(d['c'], 'vc') |
17 d['b'] = 'vb' | 22 self.assertEqual(d['d'], 'vd') |
18 d['c'] = 'vc' | |
19 d['d'] = 'vd' | |
20 | 23 |
21 # all of these should be present | 24 # 'a' should be dropped because it was least recently used. |
22 printifpresent(d, ['a', 'b', 'c', 'd']) | 25 d['e'] = 've' |
26 self.assertNotIn('a', d) | |
23 | 27 |
24 # 'a' should be dropped because it was least recently used | 28 self.assertIsNone(d.get('a')) |
25 d['e'] = 've' | |
26 printifpresent(d, ['a', 'b', 'c', 'd', 'e']) | |
27 | 29 |
28 assert d.get('a') is None | 30 self.assertEqual(d['b'], 'vb') |
29 assert d.get('e') == 've' | 31 self.assertEqual(d['c'], 'vc') |
32 self.assertEqual(d['d'], 'vd') | |
33 self.assertEqual(d['e'], 've') | |
30 | 34 |
31 # touch entries in some order (get or set). | 35 # Touch entries in some order (both get and set). |
32 d['e'] | 36 d['e'] |
33 d['c'] = 'vc2' | 37 d['c'] = 'vc2' |
34 d['d'] | 38 d['d'] |
35 d['b'] = 'vb2' | 39 d['b'] = 'vb2' |
36 | 40 |
37 # 'e' should be dropped now | 41 # 'e' should be dropped now |
38 d['f'] = 'vf' | 42 d['f'] = 'vf' |
39 printifpresent(d, ['b', 'c', 'd', 'e', 'f']) | 43 self.assertNotIn('e', d) |
44 self.assertEqual(d['b'], 'vb2') | |
45 self.assertEqual(d['c'], 'vc2') | |
46 self.assertEqual(d['d'], 'vd') | |
47 self.assertEqual(d['f'], 'vf') | |
40 | 48 |
41 d.clear() | 49 d.clear() |
42 printifpresent(d, ['b', 'c', 'd', 'e', 'f']) | 50 for key in ('a', 'b', 'c', 'd', 'e', 'f'): |
51 self.assertNotIn(key, d) | |
43 | 52 |
44 # Now test dicts that aren't full. | 53 def testunfull(self): |
45 d = util.lrucachedict(4) | 54 d = util.lrucachedict(4) |
46 d['a'] = 1 | 55 d['a'] = 1 |
47 d['b'] = 2 | 56 d['b'] = 2 |
48 d['a'] | 57 d['a'] |
49 d['b'] | 58 d['b'] |
50 printifpresent(d, ['a', 'b']) | |
51 | 59 |
52 # test copy method | 60 for key in ('a', 'b'): |
53 d = util.lrucachedict(4) | 61 self.assertIn(key, d) |
54 d['a'] = 'va3' | |
55 d['b'] = 'vb3' | |
56 d['c'] = 'vc3' | |
57 d['d'] = 'vd3' | |
58 | 62 |
59 dc = d.copy() | 63 def testcopypartial(self): |
64 d = util.lrucachedict(4) | |
65 d['a'] = 'va' | |
66 d['b'] = 'vb' | |
60 | 67 |
61 # all of these should be present | 68 dc = d.copy() |
62 print("\nAll of these should be present:") | |
63 printifpresent(dc, ['a', 'b', 'c', 'd'], 'dc') | |
64 | 69 |
65 # 'a' should be dropped because it was least recently used | 70 self.assertEqual(len(dc), 2) |
66 print("\nAll of these except 'a' should be present:") | 71 # TODO this fails |
67 dc['e'] = 've3' | 72 return |
68 printifpresent(dc, ['a', 'b', 'c', 'd', 'e'], 'dc') | 73 for key in ('a', 'b'): |
74 self.assertIn(key, dc) | |
75 self.assertEqual(dc[key], 'v%s' % key) | |
69 | 76 |
70 # contents and order of original dict should remain unchanged | 77 def testcopyfull(self): |
71 print("\nThese should be in reverse alphabetical order and read 'v?3':") | 78 d = util.lrucachedict(4) |
72 dc['b'] = 'vb3_new' | 79 d['a'] = 'va' |
73 for k in list(iter(d)): | 80 d['b'] = 'vb' |
74 print("d['%s']: %s" % (k, d[k])) | 81 d['c'] = 'vc' |
82 d['d'] = 'vd' | |
83 | |
84 dc = d.copy() | |
85 | |
86 for key in ('a', 'b', 'c', 'd'): | |
87 self.assertIn(key, dc) | |
88 self.assertEqual(dc[key], 'v%s' % key) | |
89 | |
90 # 'a' should be dropped because it was least recently used. | |
91 dc['e'] = 've' | |
92 self.assertNotIn('a', dc) | |
93 for key in ('b', 'c', 'd', 'e'): | |
94 self.assertIn(key, dc) | |
95 self.assertEqual(dc[key], 'v%s' % key) | |
96 | |
97 # Contents and order of original dict should remain unchanged. | |
98 dc['b'] = 'vb_new' | |
99 | |
100 self.assertEqual(list(iter(d)), ['d', 'c', 'b', 'a']) | |
101 for key in ('a', 'b', 'c', 'd'): | |
102 self.assertEqual(d[key], 'v%s' % key) | |
75 | 103 |
76 if __name__ == '__main__': | 104 if __name__ == '__main__': |
77 test_lrucachedict() | 105 silenttestrunner.main(__name__) |