1 # Copyright (C) 2011 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
14 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 from webkitpy.common import lru_cache
32 class LRUCacheTest(unittest.TestCase):
35 self.lru = lru_cache.LRUCache(3)
36 self.lru['key_1'] = 'item_1'
37 self.lru['key_2'] = 'item_2'
38 self.lru['key_3'] = 'item_3'
40 self.lru2 = lru_cache.LRUCache(1)
41 self.lru2['key_1'] = 'item_1'
44 self.assertEqual(set(self.lru.items()), set([('key_1', 'item_1'), ('key_3', 'item_3'), ('key_2', 'item_2')]))
47 self.lru['key_4'] = 'item_4'
48 self.assertEqual(set(self.lru.items()), set([('key_4', 'item_4'), ('key_3', 'item_3'), ('key_2', 'item_2')]))
50 def test_update(self):
52 self.lru['key_5'] = 'item_5'
53 self.assertEqual(set(self.lru.items()), set([('key_1', 'item_1'), ('key_3', 'item_3'), ('key_5', 'item_5')]))
56 self.assertEqual(set(self.lru.keys()), set(['key_1', 'key_2', 'key_3']))
58 def test_delete(self):
60 self.assertFalse('key_1' in self.lru)
62 def test_contain(self):
63 self.assertTrue('key_1' in self.lru)
64 self.assertFalse('key_4' in self.lru)
66 def test_values(self):
67 self.assertEqual(set(self.lru.values()), set(['item_1', 'item_2', 'item_3']))
70 self.assertEqual(len(self.lru), 3)
72 def test_size_one_pop(self):
73 self.lru2['key_2'] = 'item_2'
74 self.assertEqual(self.lru2.keys(), ['key_2'])
76 def test_size_one_delete(self):
77 del self.lru2['key_1']
78 self.assertFalse('key_1' in self.lru2)
80 def test_pop_error(self):
81 self.assertRaises(KeyError, self.lru2.__getitem__, 'key_2')
82 del self.lru2['key_1']
83 self.assertRaises(KeyError, self.lru2.__getitem__, 'key_2')
85 def test_get_middle_item(self):
87 self.lru['key_4'] = 'item_4'
88 self.lru['key_5'] = 'item_5'
89 self.assertEqual(set(self.lru.keys()), set(['key_2', 'key_4', 'key_5']))
91 def test_set_again(self):
92 self.lru['key_1'] = 'item_4'
93 self.assertEqual(set(self.lru.items()), set([('key_1', 'item_4'), ('key_3', 'item_3'), ('key_2', 'item_2')]))