Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Tools / Scripts / webkitpy / common / lru_cache_unittest.py
1 # Copyright (C) 2011 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
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
12 # distribution.
13
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.
25
26
27 import unittest
28
29 from webkitpy.common import lru_cache
30
31
32 class LRUCacheTest(unittest.TestCase):
33
34     def setUp(self):
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'
39
40         self.lru2 = lru_cache.LRUCache(1)
41         self.lru2['key_1'] = 'item_1'
42
43     def test_items(self):
44         self.assertEqual(set(self.lru.items()), set([('key_1', 'item_1'), ('key_3', 'item_3'), ('key_2', 'item_2')]))
45
46     def test_put(self):
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')]))
49
50     def test_update(self):
51         self.lru['key_1']
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')]))
54
55     def test_keys(self):
56         self.assertEqual(set(self.lru.keys()), set(['key_1', 'key_2', 'key_3']))
57
58     def test_delete(self):
59         del self.lru['key_1']
60         self.assertFalse('key_1' in self.lru)
61
62     def test_contain(self):
63         self.assertTrue('key_1' in self.lru)
64         self.assertFalse('key_4' in self.lru)
65
66     def test_values(self):
67         self.assertEqual(set(self.lru.values()), set(['item_1', 'item_2', 'item_3']))
68
69     def test_len(self):
70         self.assertEqual(len(self.lru), 3)
71
72     def test_size_one_pop(self):
73         self.lru2['key_2'] = 'item_2'
74         self.assertEqual(self.lru2.keys(), ['key_2'])
75
76     def test_size_one_delete(self):
77         del self.lru2['key_1']
78         self.assertFalse('key_1' in self.lru2)
79
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')
84
85     def test_get_middle_item(self):
86         self.lru['key_2']
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']))
90
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')]))