Show More
@@ -1,34 +1,38 | |||||
1 | from __future__ import absolute_import, print_function |
|
1 | from __future__ import absolute_import, print_function | |
2 |
|
2 | |||
|
3 | import unittest | |||
|
4 | ||||
|
5 | import silenttestrunner | |||
|
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 |
|
|||
10 | print("'%s' in %s: %s" % (x, name, present)) |
|
|||
11 | if present: |
|
|||
12 | print("%s['%s']: %s" % (name, x, d[x])) |
|
|||
13 |
|
||||
14 | def test_lrucachedict(): |
|
|||
15 | d = util.lrucachedict(4) |
|
13 | d = util.lrucachedict(4) | |
16 | d['a'] = 'va' |
|
14 | d['a'] = 'va' | |
17 | d['b'] = 'vb' |
|
15 | d['b'] = 'vb' | |
18 | d['c'] = 'vc' |
|
16 | d['c'] = 'vc' | |
19 | d['d'] = 'vd' |
|
17 | d['d'] = 'vd' | |
20 |
|
18 | |||
21 | # all of these should be present |
|
19 | self.assertEqual(d['a'], 'va') | |
22 | printifpresent(d, ['a', 'b', 'c', 'd']) |
|
20 | self.assertEqual(d['b'], 'vb') | |
|
21 | self.assertEqual(d['c'], 'vc') | |||
|
22 | self.assertEqual(d['d'], 'vd') | |||
23 |
|
23 | |||
24 | # 'a' should be dropped because it was least recently used |
|
24 | # 'a' should be dropped because it was least recently used. | |
25 | d['e'] = 've' |
|
25 | d['e'] = 've' | |
26 | printifpresent(d, ['a', 'b', 'c', 'd', 'e']) |
|
26 | self.assertNotIn('a', d) | |
|
27 | ||||
|
28 | self.assertIsNone(d.get('a')) | |||
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 |
# |
|
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'] | |
@@ -36,42 +40,66 def test_lrucachedict(): | |||||
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 |
|
|
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'): | |
|
61 | self.assertIn(key, d) | |||
|
62 | ||||
|
63 | def testcopypartial(self): | |||
53 | d = util.lrucachedict(4) |
|
64 | d = util.lrucachedict(4) | |
54 |
d['a'] = 'va |
|
65 | d['a'] = 'va' | |
55 |
d['b'] = 'vb |
|
66 | d['b'] = 'vb' | |
56 | d['c'] = 'vc3' |
|
|||
57 | d['d'] = 'vd3' |
|
|||
58 |
|
67 | |||
59 | dc = d.copy() |
|
68 | dc = d.copy() | |
60 |
|
69 | |||
61 | # all of these should be present |
|
70 | self.assertEqual(len(dc), 2) | |
62 | print("\nAll of these should be present:") |
|
71 | # TODO this fails | |
63 | printifpresent(dc, ['a', 'b', 'c', 'd'], 'dc') |
|
72 | return | |
|
73 | for key in ('a', 'b'): | |||
|
74 | self.assertIn(key, dc) | |||
|
75 | self.assertEqual(dc[key], 'v%s' % key) | |||
|
76 | ||||
|
77 | def testcopyfull(self): | |||
|
78 | d = util.lrucachedict(4) | |||
|
79 | d['a'] = 'va' | |||
|
80 | d['b'] = 'vb' | |||
|
81 | d['c'] = 'vc' | |||
|
82 | d['d'] = 'vd' | |||
|
83 | ||||
|
84 | dc = d.copy() | |||
64 |
|
85 | |||
65 | # 'a' should be dropped because it was least recently used |
|
86 | for key in ('a', 'b', 'c', 'd'): | |
66 | print("\nAll of these except 'a' should be present:") |
|
87 | self.assertIn(key, dc) | |
67 | dc['e'] = 've3' |
|
88 | self.assertEqual(dc[key], 'v%s' % key) | |
68 | printifpresent(dc, ['a', 'b', 'c', 'd', 'e'], 'dc') |
|
|||
69 |
|
89 | |||
70 | # contents and order of original dict should remain unchanged |
|
90 | # 'a' should be dropped because it was least recently used. | |
71 | print("\nThese should be in reverse alphabetical order and read 'v?3':") |
|
91 | dc['e'] = 've' | |
72 | dc['b'] = 'vb3_new' |
|
92 | self.assertNotIn('a', dc) | |
73 | for k in list(iter(d)): |
|
93 | for key in ('b', 'c', 'd', 'e'): | |
74 | print("d['%s']: %s" % (k, d[k])) |
|
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__) |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now