/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_fifo_cache.py

  • Committer: John Arbash Meinel
  • Date: 2008-12-09 21:35:49 UTC
  • mto: This revision was merged to the branch mainline in revision 3888.
  • Revision ID: john@arbash-meinel.com-20081209213549-yc1mqv3l5gun9c63
Start working on a FIFOCache.

This is designed to have less runtime cost than the LRUCache.
Mostly because we don't have to do any recording work on *access*
only on update or delete.
As such, we subclass dict directly, because experiments show that
it performed the best. Unfortunately, even though we don't have
a custom __getitem__ implementation, it is still approx 2x slower
than using a raw dict.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Tests for the fifo_cache module."""
 
18
 
 
19
from bzrlib import (
 
20
    fifo_cache,
 
21
    tests,
 
22
    )
 
23
 
 
24
 
 
25
class TestFIFOCache(tests.TestCase):
 
26
    """Test that FIFO cache properly keeps track of entries."""
 
27
 
 
28
    def test_add_is_present(self):
 
29
        c = fifo_cache.FIFOCache()
 
30
        c[1] = 2
 
31
        self.assertTrue(1 in c)
 
32
        self.assertEqual(1, len(c))
 
33
        self.assertEqual(2, c[1])
 
34
        self.assertEqual(2, c.get(1))
 
35
        self.assertEqual(2, c.get(1, None))
 
36
        self.assertEqual([1], c.keys())
 
37
        self.assertEqual([1], list(c.iterkeys()))
 
38
        self.assertEqual([(1, 2)], c.items())
 
39
        self.assertEqual([(1, 2)], list(c.iteritems()))
 
40
        self.assertEqual([2], c.values())
 
41
        self.assertEqual([2], list(c.itervalues()))
 
42
 
 
43
    def test_missing(self):
 
44
        c = fifo_cache.FIFOCache()
 
45
        self.assertRaises(KeyError, c.__getitem__, 1)
 
46
        self.assertFalse(1 in c)
 
47
        self.assertEqual(0, len(c))
 
48
        self.assertEqual(None, c.get(1))
 
49
        self.assertEqual(None, c.get(1, None))
 
50
        self.assertEqual([], c.keys())
 
51
        self.assertEqual([], list(c.iterkeys()))
 
52
        self.assertEqual([], c.items())
 
53
        self.assertEqual([], list(c.iteritems()))
 
54
        self.assertEqual([], c.values())
 
55
        self.assertEqual([], list(c.itervalues()))
 
56
 
 
57
    def test_add_maintains_fifo(self):
 
58
        c = fifo_cache.FIFOCache(4, 4)
 
59
        c[1] = 2
 
60
        c[2] = 3
 
61
        c[3] = 4
 
62
        c[4] = 5
 
63
        self.assertEqual([1, 2, 3, 4], sorted(c.keys()))
 
64
        c[5] = 6
 
65
        # This should pop out the oldest entry
 
66
        self.assertEqual([2, 3, 4, 5], sorted(c.keys()))
 
67
        # Replacing an item doesn't change the stored keys
 
68
        c[2] = 7
 
69
        self.assertEqual([2, 3, 4, 5], sorted(c.keys()))
 
70
        # But it does change the position in the FIFO
 
71
        c[6] = 7
 
72
        self.assertEqual([2, 4, 5, 6], sorted(c.keys()))
 
73
        self.assertEqual([4, 5, 2, 6], list(c._queue))
 
74
 
 
75
    def test_default_after_cleanup_count(self):
 
76
        c = fifo_cache.FIFOCache(5)
 
77
        self.assertEqual(4, c._after_cleanup_count)
 
78
        c[1] = 2
 
79
        c[2] = 3
 
80
        c[3] = 4
 
81
        c[4] = 5
 
82
        c[5] = 6
 
83
        # So far, everything fits
 
84
        self.assertEqual([1, 2, 3, 4, 5], sorted(c.keys()))
 
85
        c[6] = 7
 
86
        # But adding one more should shrink down to after_cleanup_count
 
87
        self.assertEqual([3, 4, 5, 6], sorted(c.keys()))
 
88
 
 
89
    def test_clear(self):
 
90
        c = fifo_cache.FIFOCache(5)
 
91
        c[1] = 2
 
92
        c[2] = 3
 
93
        c[3] = 4
 
94
        c[4] = 5
 
95
        c[5] = 6
 
96
        c.cleanup()
 
97
        self.assertEqual([2, 3, 4, 5], sorted(c.keys()))
 
98
        c.clear()
 
99
        self.assertEqual([], c.keys())
 
100
        self.assertEqual([], list(c._queue))
 
101
 
 
102
    def test_copy_not_implemented(self):
 
103
        c = fifo_cache.FIFOCache()
 
104
        self.assertRaises(NotImplementedError, c.copy)
 
105
 
 
106
    def test_pop_not_implemeted(self):
 
107
        c = fifo_cache.FIFOCache()
 
108
        self.assertRaises(NotImplementedError, c.pop, 'key')
 
109
 
 
110
    def test_popitem_not_implemeted(self):
 
111
        c = fifo_cache.FIFOCache()
 
112
        self.assertRaises(NotImplementedError, c.popitem)
 
113
 
 
114
    def test_setdefault_not_implemented(self):
 
115
        c = fifo_cache.FIFOCache()
 
116
        self.assertRaises(NotImplementedError, c.setdefault)
 
117
 
 
118
    def test_update_not_implemented(self):
 
119
        c = fifo_cache.FIFOCache()
 
120
        self.assertRaises(NotImplementedError, c.update)
 
121
 
 
122
    def test_cleanup_funcs(self):
 
123
        log = []
 
124
        def logging_cleanup(key, value):
 
125
            log.append((key, value))
 
126
        c = fifo_cache.FIFOCache(5, 4)
 
127
        c.add(1, 2, cleanup=logging_cleanup)
 
128
        c.add(2, 3, cleanup=logging_cleanup)
 
129
        c.add(3, 4, cleanup=logging_cleanup)
 
130
        c.add(4, 5, cleanup=None) # no cleanup for 4
 
131
        c[5] = 6 # no cleanup for 5
 
132
        self.assertEqual([], log)
 
133
        # Adding another key should cleanup 1 & 2
 
134
        c[6] = 7
 
135
        self.assertEqual([(1, 2), (2, 3)], log)
 
136
        del log[:]
 
137
        c.clear()
 
138
        self.assertEqual([(3, 4)], log)