Coverage for tests/test_cache.py: 11%
63 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-14 02:25 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-01-14 02:25 -0800
1#
2# Developed for the LSST Data Management System.
3# This product includes software developed by the LSST Project
4# (https://www.lsst.org).
5# See the COPYRIGHT file at the top-level directory of this distribution
6# for details of code ownership.
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program. If not, see <https://www.gnu.org/licenses/>.
20#
22import unittest
24from _cache import NumbersCache
27def numberToWords(value):
28 """Convert a number in the range [0, 1000) to words"""
29 assert value >= 0 and isinstance(value, int), "Only non-negative integers"
30 if value < 20:
31 return {
32 0: "zero",
33 1: "one",
34 2: "two",
35 3: "three",
36 4: "four",
37 5: "five",
38 6: "six",
39 7: "seven",
40 8: "eight",
41 9: "nine",
42 10: "ten",
43 11: "eleven",
44 12: "twelve",
45 13: "thirteen",
46 14: "fourteen",
47 15: "fifteen",
48 16: "sixteen",
49 17: "seventeen",
50 18: "eighteen",
51 19: "nineteen",
52 }[value]
53 if value < 100:
54 tens = value//10
55 ones = value % 10
56 return {
57 2: "twenty",
58 3: "thirty",
59 4: "forty",
60 5: "fifty",
61 6: "sixty",
62 7: "seventy",
63 8: "eighty",
64 9: "ninety",
65 }[tens] + (("-" + numberToWords(ones)) if ones > 0 else "")
66 assert value < 1000, "Value exceeds limit of 999"
67 hundreds = value//100
68 rest = value % 100
69 return numberToWords(hundreds) + " hundred" + ((" " + numberToWords(rest)) if rest > 0 else "")
72class CacheTestCase(unittest.TestCase):
73 """Tests of lsst.cpputils.Cache"""
74 def check(self, addFunction):
75 """Exercise the Cache
77 The `addFunction` should take a cache and number,
78 and add the number (and its corresponding string)
79 into the cache.
80 """
81 capacity = 10
82 cache = NumbersCache(capacity)
83 self.assertEqual(cache.size(), 0, "Starts empty")
84 self.assertEqual(cache.capacity(), capacity, "Capacity as requested")
85 maximum = 20
86 for ii in range(maximum):
87 addFunction(cache, ii)
88 self.assertEqual(cache.size(), capacity, "Filled to capacity")
89 self.assertEqual(cache.capacity(), capacity, "Capacity unchanged")
90 for ii in range(maximum - capacity):
91 self.assertNotIn(ii, cache, "Should have been expunged")
92 self.assertIsNone(cache.get(ii), "Should have been expunged")
93 expectedContents = list(range(maximum - 1, maximum - capacity - 1, -1)) # Last in, first out
94 actualContents = cache.keys()
95 for ii in expectedContents:
96 self.assertIn(ii, cache, "Should be present")
97 self.assertEqual(cache[ii], numberToWords(ii), "Value accessible and as expected")
98 self.assertEqual(cache.get(ii), numberToWords(ii), "Value accessible via get and as expected")
99 self.assertListEqual(actualContents, expectedContents, "Contents are as expected")
100 with self.assertRaises(LookupError):
101 cache[maximum - capacity - 1]
102 newCapacity = 5
103 cache.reserve(newCapacity)
104 # The new list of contents is smaller, but also reversed because we've gone through the cache
105 # touching items.
106 newExpectedContents = list(reversed(expectedContents))[:newCapacity]
107 self.assertEqual(cache.capacity(), newCapacity, "Capacity changed")
108 self.assertEqual(cache.size(), newCapacity, "Size changed to correspond to new capacity")
109 self.assertListEqual(cache.keys(), newExpectedContents, "Most recent kept")
110 cache.flush()
111 self.assertEqual(cache.size(), 0, "Flushed everything out")
112 self.assertEqual(cache.capacity(), newCapacity, "Capacity unchanged")
113 return cache
115 def testDirect(self):
116 """Directly add key,value pairs into the cache with Cache.add"""
117 self.check(lambda cache, index: cache.add(index, numberToWords(index)))
119 def testLazy(self):
120 """Exercise the lazy function call in Cache.operator()"""
121 def addFunction(cache, index):
122 value = cache(index, lambda ii: numberToWords(ii))
123 self.assertEqual(value, numberToWords(index))
125 cache = self.check(addFunction)
127 # Check that the function call doesn't fire when we pull out something that's in there already
128 def trap(key):
129 raise AssertionError("Failed")
131 for index in cache.keys():
132 value = cache(index, trap)
133 self.assertEqual(value, numberToWords(index))
135 # Check that this checking technique actually works...
136 with self.assertRaises(AssertionError):
137 cache(999, trap)
140if __name__ == "__main__": 140 ↛ 141line 140 didn't jump to line 141, because the condition on line 140 was never true
141 unittest.main()