~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_fifo_cache.py

  • Committer: Ian Clatworthy
  • Date: 2008-12-15 04:37:10 UTC
  • mfrom: (3892.1.6 bzr.help-formats)
  • mto: This revision was merged to the branch mainline in revision 3905.
  • Revision ID: ian.clatworthy@canonical.com-20081215043710-ybhxvqjeir13k5ht
Improved help on storage formats (Ian Clatworthy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
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
16
16
 
17
17
"""Tests for the fifo_cache module."""
18
18
 
41
41
        self.assertEqual([2], list(c.itervalues()))
42
42
        self.assertEqual({1: 2}, c)
43
43
 
44
 
    def test_cache_size(self):
45
 
        c = fifo_cache.FIFOCache()
46
 
        self.assertEqual(100, c.cache_size())
47
 
        c.resize(20, 5)
48
 
        self.assertEqual(20, c.cache_size())
49
 
 
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)
122
116
 
123
 
    def test_resize_smaller(self):
124
 
        c = fifo_cache.FIFOCache()
125
 
        c[1] = 2
126
 
        c[2] = 3
127
 
        c[3] = 4
128
 
        c[4] = 5
129
 
        c[5] = 6
130
 
        # No cleanup, because it is the exact size
131
 
        c.resize(5)
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
135
 
        c[6] = 7
136
 
        self.assertEqual({3: 4, 4: 5, 5: 6, 6: 7}, c)
137
 
        c.resize(3, 2)
138
 
        self.assertEqual({5: 6, 6: 7}, c)
139
 
 
140
 
    def test_resize_larger(self):
141
 
        c = fifo_cache.FIFOCache(5, 4)
142
 
        c[1] = 2
143
 
        c[2] = 3
144
 
        c[3] = 4
145
 
        c[4] = 5
146
 
        c[5] = 6
147
 
        # No cleanup, because it is the exact size
148
 
        c.resize(10)
149
 
        self.assertEqual({1: 2, 2: 3, 3: 4, 4: 5, 5: 6}, c)
150
 
        self.assertEqual(10, c.cache_size())
151
 
        c[6] = 7
152
 
        c[7] = 8
153
 
        c[8] = 9
154
 
        c[9] = 10
155
 
        c[10] = 11
156
 
        self.assertEqual({1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9,
157
 
                          9: 10, 10: 11}, c)
158
 
        c[11] = 12
159
 
        self.assertEqual({4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10, 10: 11,
160
 
                          11: 12}, c)
161
 
 
162
117
    def test_setdefault(self):
163
118
        c = fifo_cache.FIFOCache(5, 4)
164
119
        c['one'] = 1
229
184
        c = fifo_cache.FIFOCache()
230
185
        c.add(1, 2, cleanup=logging_cleanup)
231
186
        del c
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)
237
192
 
238
193
 
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())
257
211
 
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)
297
 
 
298
 
    def test_resize_smaller(self):
299
 
        c = fifo_cache.FIFOSizeCache(20, 16)
300
 
        c[1] = 'a'
301
 
        c[2] = 'bc'
302
 
        c[3] = 'def'
303
 
        c[4] = 'ghij'
304
 
        # No cleanup, because it is the exact size
305
 
        c.resize(10, 8)
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
309
 
        c[5] = 'k'
310
 
        self.assertEqual({3: 'def', 4: 'ghij', 5: 'k'}, c)
311
 
        c.resize(5, 4)
312
 
        self.assertEqual({5: 'k'}, c)
313
 
 
314
 
    def test_resize_larger(self):
315
 
        c = fifo_cache.FIFOSizeCache(10, 8)
316
 
        c[1] = 'a'
317
 
        c[2] = 'bc'
318
 
        c[3] = 'def'
319
 
        c[4] = 'ghij'
320
 
        c.resize(12, 10)
321
 
        self.assertEqual({1: 'a', 2: 'bc', 3: 'def', 4: 'ghij'}, c)
322
 
        c[5] = 'kl'
323
 
        self.assertEqual({1: 'a', 2: 'bc', 3: 'def', 4: 'ghij', 5: 'kl'}, c)
324
 
        c[6] = 'mn'
325
 
        self.assertEqual({4: 'ghij', 5: 'kl', 6: 'mn'}, c)