Mercurial > public > mercurial-scm > hg
annotate tests/test-lrucachedict.py @ 39563:b31b01f93b11
util: properly copy lrucachedict instances
Previously, copy() only worked if the cache was full. We teach
copy() to only copy defined nodes.
Differential Revision: https://phab.mercurial-scm.org/D4498
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Thu, 06 Sep 2018 11:33:40 -0700 |
parents | 067f7d2c7d60 |
children | 5d75a3c16193 |
rev | line source |
---|---|
28931
ba0e4789bd2e
tests: make test-lrucachedict use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28930
diff
changeset
|
1 from __future__ import absolute_import, print_function |
28930
e3f01188d439
tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27576
diff
changeset
|
2 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
3 import unittest |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
4 |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
5 import silenttestrunner |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
6 |
28930
e3f01188d439
tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27576
diff
changeset
|
7 from mercurial import ( |
e3f01188d439
tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27576
diff
changeset
|
8 util, |
e3f01188d439
tests: make test-lrucachedict use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27576
diff
changeset
|
9 ) |
18603 | 10 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
11 class testlrucachedict(unittest.TestCase): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
12 def testsimple(self): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
13 d = util.lrucachedict(4) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
14 d['a'] = 'va' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
15 d['b'] = 'vb' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
16 d['c'] = 'vc' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
17 d['d'] = 'vd' |
18603 | 18 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
19 self.assertEqual(d['a'], 'va') |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
20 self.assertEqual(d['b'], 'vb') |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
21 self.assertEqual(d['c'], 'vc') |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
22 self.assertEqual(d['d'], 'vd') |
18603 | 23 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
24 # 'a' should be dropped because it was least recently used. |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
25 d['e'] = 've' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
26 self.assertNotIn('a', d) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
27 |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
28 self.assertIsNone(d.get('a')) |
18603 | 29 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
30 self.assertEqual(d['b'], 'vb') |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
31 self.assertEqual(d['c'], 'vc') |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
32 self.assertEqual(d['d'], 'vd') |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
33 self.assertEqual(d['e'], 've') |
18603 | 34 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
35 # Touch entries in some order (both get and set). |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
36 d['e'] |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
37 d['c'] = 'vc2' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
38 d['d'] |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
39 d['b'] = 'vb2' |
29828
79add5a4e857
util: properly implement lrucachedict.get()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28931
diff
changeset
|
40 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
41 # 'e' should be dropped now |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
42 d['f'] = 'vf' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
43 self.assertNotIn('e', d) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
44 self.assertEqual(d['b'], 'vb2') |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
45 self.assertEqual(d['c'], 'vc2') |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
46 self.assertEqual(d['d'], 'vd') |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
47 self.assertEqual(d['f'], 'vf') |
18603 | 48 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
49 d.clear() |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
50 for key in ('a', 'b', 'c', 'd', 'e', 'f'): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
51 self.assertNotIn(key, d) |
18603 | 52 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
53 def testunfull(self): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
54 d = util.lrucachedict(4) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
55 d['a'] = 1 |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
56 d['b'] = 2 |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
57 d['a'] |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
58 d['b'] |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
59 |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
60 for key in ('a', 'b'): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
61 self.assertIn(key, d) |
19710
887ffa22fd0d
lrucachedict: implement clear()
Siddharth Agarwal <sid0@fb.com>
parents:
18603
diff
changeset
|
62 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
63 def testcopypartial(self): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
64 d = util.lrucachedict(4) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
65 d['a'] = 'va' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
66 d['b'] = 'vb' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
67 |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
68 dc = d.copy() |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
69 |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
70 self.assertEqual(len(dc), 2) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
71 for key in ('a', 'b'): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
72 self.assertIn(key, dc) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
73 self.assertEqual(dc[key], 'v%s' % key) |
27371
45d996a566d7
util: reimplement lrucachedict
Gregory Szorc <gregory.szorc@gmail.com>
parents:
19710
diff
changeset
|
74 |
39563
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
75 self.assertEqual(len(d), 2) |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
76 for key in ('a', 'b'): |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
77 self.assertIn(key, d) |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
78 self.assertEqual(d[key], 'v%s' % key) |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
79 |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
80 d['c'] = 'vc' |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
81 del d['b'] |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
82 dc = d.copy() |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
83 self.assertEqual(len(dc), 2) |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
84 for key in ('a', 'c'): |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
85 self.assertIn(key, dc) |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
86 self.assertEqual(dc[key], 'v%s' % key) |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
87 |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
88 def testcopyempty(self): |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
89 d = util.lrucachedict(4) |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
90 dc = d.copy() |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
91 self.assertEqual(len(dc), 0) |
b31b01f93b11
util: properly copy lrucachedict instances
Gregory Szorc <gregory.szorc@gmail.com>
parents:
39562
diff
changeset
|
92 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
93 def testcopyfull(self): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
94 d = util.lrucachedict(4) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
95 d['a'] = 'va' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
96 d['b'] = 'vb' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
97 d['c'] = 'vc' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
98 d['d'] = 'vd' |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
99 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
100 dc = d.copy() |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
101 |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
102 for key in ('a', 'b', 'c', 'd'): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
103 self.assertIn(key, dc) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
104 self.assertEqual(dc[key], 'v%s' % key) |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
105 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
106 # 'a' should be dropped because it was least recently used. |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
107 dc['e'] = 've' |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
108 self.assertNotIn('a', dc) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
109 for key in ('b', 'c', 'd', 'e'): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
110 self.assertIn(key, dc) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
111 self.assertEqual(dc[key], 'v%s' % key) |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
112 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
113 # Contents and order of original dict should remain unchanged. |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
114 dc['b'] = 'vb_new' |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
115 |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
116 self.assertEqual(list(iter(d)), ['d', 'c', 'b', 'a']) |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
117 for key in ('a', 'b', 'c', 'd'): |
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
118 self.assertEqual(d[key], 'v%s' % key) |
27576
6cd3044985c2
lrucachedict: add copy method
Eric Sumner <ericsumner@fb.com>
parents:
27371
diff
changeset
|
119 |
18603 | 120 if __name__ == '__main__': |
39562
067f7d2c7d60
tests: rewrite test-lrucachedict.py to use unittest
Gregory Szorc <gregory.szorc@gmail.com>
parents:
29828
diff
changeset
|
121 silenttestrunner.main(__name__) |