##// END OF EJS Templates
util: properly copy lrucachedict instances...
Gregory Szorc -
r39599:b31b01f9 default
parent child Browse files
Show More
@@ -1313,11 +1313,19 b' class lrucachedict(object):'
1313
1313
1314 def copy(self):
1314 def copy(self):
1315 result = lrucachedict(self._capacity)
1315 result = lrucachedict(self._capacity)
1316
1317 # We copy entries by iterating in oldest-to-newest order so the copy
1318 # has the correct ordering.
1319
1320 # Find the first non-empty entry.
1316 n = self._head.prev
1321 n = self._head.prev
1317 # Iterate in oldest-to-newest order, so the copy has the right ordering
1322 while n.key is _notset and n is not self._head:
1323 n = n.prev
1324
1318 for i in range(len(self._cache)):
1325 for i in range(len(self._cache)):
1319 result[n.key] = n.value
1326 result[n.key] = n.value
1320 n = n.prev
1327 n = n.prev
1328
1321 return result
1329 return result
1322
1330
1323 def _movetohead(self, node):
1331 def _movetohead(self, node):
@@ -68,12 +68,28 b' class testlrucachedict(unittest.TestCase'
68 dc = d.copy()
68 dc = d.copy()
69
69
70 self.assertEqual(len(dc), 2)
70 self.assertEqual(len(dc), 2)
71 # TODO this fails
72 return
73 for key in ('a', 'b'):
71 for key in ('a', 'b'):
74 self.assertIn(key, dc)
72 self.assertIn(key, dc)
75 self.assertEqual(dc[key], 'v%s' % key)
73 self.assertEqual(dc[key], 'v%s' % key)
76
74
75 self.assertEqual(len(d), 2)
76 for key in ('a', 'b'):
77 self.assertIn(key, d)
78 self.assertEqual(d[key], 'v%s' % key)
79
80 d['c'] = 'vc'
81 del d['b']
82 dc = d.copy()
83 self.assertEqual(len(dc), 2)
84 for key in ('a', 'c'):
85 self.assertIn(key, dc)
86 self.assertEqual(dc[key], 'v%s' % key)
87
88 def testcopyempty(self):
89 d = util.lrucachedict(4)
90 dc = d.copy()
91 self.assertEqual(len(dc), 0)
92
77 def testcopyfull(self):
93 def testcopyfull(self):
78 d = util.lrucachedict(4)
94 d = util.lrucachedict(4)
79 d['a'] = 'va'
95 d['a'] = 'va'
General Comments 0
You need to be logged in to leave comments. Login now