##// END OF EJS Templates
tests: rewrite test-lrucachedict.py to use unittest...
Gregory Szorc -
r39598:067f7d2c default
parent child Browse files
Show More
@@ -1,77 +1,105 b''
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
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)
27
28 self.assertIsNone(d.get('a'))
23
29
24 # 'a' should be dropped because it was least recently used
30 self.assertEqual(d['b'], 'vb')
25 d['e'] = 've'
31 self.assertEqual(d['c'], 'vc')
26 printifpresent(d, ['a', 'b', 'c', 'd', 'e'])
32 self.assertEqual(d['d'], 'vd')
33 self.assertEqual(d['e'], 've')
27
34
28 assert d.get('a') is None
35 # Touch entries in some order (both get and set).
29 assert d.get('e') == 've'
36 d['e']
37 d['c'] = 'vc2'
38 d['d']
39 d['b'] = 'vb2'
30
40
31 # touch entries in some order (get or set).
41 # 'e' should be dropped now
32 d['e']
42 d['f'] = 'vf'
33 d['c'] = 'vc2'
43 self.assertNotIn('e', d)
34 d['d']
44 self.assertEqual(d['b'], 'vb2')
35 d['b'] = 'vb2'
45 self.assertEqual(d['c'], 'vc2')
46 self.assertEqual(d['d'], 'vd')
47 self.assertEqual(d['f'], 'vf')
36
48
37 # 'e' should be dropped now
49 d.clear()
38 d['f'] = 'vf'
50 for key in ('a', 'b', 'c', 'd', 'e', 'f'):
39 printifpresent(d, ['b', 'c', 'd', 'e', 'f'])
51 self.assertNotIn(key, d)
40
52
41 d.clear()
53 def testunfull(self):
42 printifpresent(d, ['b', 'c', 'd', 'e', 'f'])
54 d = util.lrucachedict(4)
55 d['a'] = 1
56 d['b'] = 2
57 d['a']
58 d['b']
59
60 for key in ('a', 'b'):
61 self.assertIn(key, d)
43
62
44 # Now test dicts that aren't full.
63 def testcopypartial(self):
45 d = util.lrucachedict(4)
64 d = util.lrucachedict(4)
46 d['a'] = 1
65 d['a'] = 'va'
47 d['b'] = 2
66 d['b'] = 'vb'
48 d['a']
67
49 d['b']
68 dc = d.copy()
50 printifpresent(d, ['a', 'b'])
69
70 self.assertEqual(len(dc), 2)
71 # TODO this fails
72 return
73 for key in ('a', 'b'):
74 self.assertIn(key, dc)
75 self.assertEqual(dc[key], 'v%s' % key)
51
76
52 # test copy method
77 def testcopyfull(self):
53 d = util.lrucachedict(4)
78 d = util.lrucachedict(4)
54 d['a'] = 'va3'
79 d['a'] = 'va'
55 d['b'] = 'vb3'
80 d['b'] = 'vb'
56 d['c'] = 'vc3'
81 d['c'] = 'vc'
57 d['d'] = 'vd3'
82 d['d'] = 'vd'
58
83
59 dc = d.copy()
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)
60
89
61 # all of these should be present
90 # 'a' should be dropped because it was least recently used.
62 print("\nAll of these should be present:")
91 dc['e'] = 've'
63 printifpresent(dc, ['a', 'b', 'c', 'd'], 'dc')
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)
64
96
65 # 'a' should be dropped because it was least recently used
97 # Contents and order of original dict should remain unchanged.
66 print("\nAll of these except 'a' should be present:")
98 dc['b'] = 'vb_new'
67 dc['e'] = 've3'
68 printifpresent(dc, ['a', 'b', 'c', 'd', 'e'], 'dc')
69
99
70 # contents and order of original dict should remain unchanged
100 self.assertEqual(list(iter(d)), ['d', 'c', 'b', 'a'])
71 print("\nThese should be in reverse alphabetical order and read 'v?3':")
101 for key in ('a', 'b', 'c', 'd'):
72 dc['b'] = 'vb3_new'
102 self.assertEqual(d[key], 'v%s' % key)
73 for k in list(iter(d)):
74 print("d['%s']: %s" % (k, d[k]))
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