13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Tests for the fifo_cache module."""
41
41
self.assertEqual([2], list(c.itervalues()))
42
42
self.assertEqual({1: 2}, c)
44
def test_cache_size(self):
45
c = fifo_cache.FIFOCache()
46
self.assertEqual(100, c.cache_size())
48
self.assertEqual(20, c.cache_size())
50
44
def test_missing(self):
51
45
c = fifo_cache.FIFOCache()
52
46
self.assertRaises(KeyError, c.__getitem__, 1)
120
114
c = fifo_cache.FIFOCache()
121
115
self.assertRaises(NotImplementedError, c.popitem)
123
def test_resize_smaller(self):
124
c = fifo_cache.FIFOCache()
130
# No cleanup, because it is the exact size
132
self.assertEqual({1: 2, 2: 3, 3: 4, 4: 5, 5: 6}, c)
133
self.assertEqual(5, c.cache_size())
134
# Adding one more will trigger a cleanup, though
136
self.assertEqual({3: 4, 4: 5, 5: 6, 6: 7}, c)
138
self.assertEqual({5: 6, 6: 7}, c)
140
def test_resize_larger(self):
141
c = fifo_cache.FIFOCache(5, 4)
147
# No cleanup, because it is the exact size
149
self.assertEqual({1: 2, 2: 3, 3: 4, 4: 5, 5: 6}, c)
150
self.assertEqual(10, c.cache_size())
156
self.assertEqual({1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9,
159
self.assertEqual({4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10, 10: 11,
162
117
def test_setdefault(self):
163
118
c = fifo_cache.FIFOCache(5, 4)
229
184
c = fifo_cache.FIFOCache()
230
185
c.add(1, 2, cleanup=logging_cleanup)
232
# As a matter of design, bzr does not (can not) count on anything
233
# being run from Python __del__ methods, because they may not run for
234
# a long time, and because in cPython merely having them defined
235
# interferes with garbage collection.
236
self.assertEqual([], log)
187
# TODO: We currently don't support calling the cleanup() funcs during
188
# __del__. We might want to consider implementing this.
189
self.expectFailure("we don't call cleanups during __del__",
190
self.assertEqual, [(1, 2)], log)
191
self.assertEqual([(1, 2)], log)
239
194
class TestFIFOSizeCache(tests.TestCase):
253
208
self.assertEqual(['2'], c.values())
254
209
self.assertEqual(['2'], list(c.itervalues()))
255
210
self.assertEqual({1: '2'}, c)
256
self.assertEqual(1024*1024, c.cache_size())
258
212
def test_missing(self):
259
213
c = fifo_cache.FIFOSizeCache()
294
248
c[1] = 'abcdefgh'
295
249
self.assertEqual({}, c)
296
250
self.assertEqual(0, c._value_size)
298
def test_resize_smaller(self):
299
c = fifo_cache.FIFOSizeCache(20, 16)
304
# No cleanup, because it is the exact size
306
self.assertEqual({1: 'a', 2: 'bc', 3: 'def', 4: 'ghij'}, c)
307
self.assertEqual(10, c.cache_size())
308
# Adding one more will trigger a cleanup, though
310
self.assertEqual({3: 'def', 4: 'ghij', 5: 'k'}, c)
312
self.assertEqual({5: 'k'}, c)
314
def test_resize_larger(self):
315
c = fifo_cache.FIFOSizeCache(10, 8)
321
self.assertEqual({1: 'a', 2: 'bc', 3: 'def', 4: 'ghij'}, c)
323
self.assertEqual({1: 'a', 2: 'bc', 3: 'def', 4: 'ghij', 5: 'kl'}, c)
325
self.assertEqual({4: 'ghij', 5: 'kl', 6: 'mn'}, c)