Coverage for tests/test_cache.py: 11%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

61 statements  

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# 

21 

22import unittest 

23 

24from _cache import NumbersCache 

25 

26 

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 "") 

70 

71 

72class CacheTestCase(unittest.TestCase): 

73 """Tests of lsst.cpputils.Cache""" 

74 def check(self, addFunction): 

75 """Exercise the Cache 

76 

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 expectedContents = list(range(maximum - 1, maximum - capacity - 1, -1)) # Last in, first out 

93 actualContents = cache.keys() 

94 for ii in expectedContents: 

95 self.assertIn(ii, cache, "Should be present") 

96 self.assertEqual(cache[ii], numberToWords(ii), "Value accessible and as expected") 

97 self.assertListEqual(actualContents, expectedContents, "Contents are as expected") 

98 with self.assertRaises(LookupError): 

99 cache[maximum - capacity - 1] 

100 newCapacity = 5 

101 cache.reserve(newCapacity) 

102 # The new list of contents is smaller, but also reversed because we've gone through the cache 

103 # touching items. 

104 newExpectedContents = list(reversed(expectedContents))[:newCapacity] 

105 self.assertEqual(cache.capacity(), newCapacity, "Capacity changed") 

106 self.assertEqual(cache.size(), newCapacity, "Size changed to correspond to new capacity") 

107 self.assertListEqual(cache.keys(), newExpectedContents, "Most recent kept") 

108 cache.flush() 

109 self.assertEqual(cache.size(), 0, "Flushed everything out") 

110 self.assertEqual(cache.capacity(), newCapacity, "Capacity unchanged") 

111 return cache 

112 

113 def testDirect(self): 

114 """Directly add key,value pairs into the cache with Cache.add""" 

115 self.check(lambda cache, index: cache.add(index, numberToWords(index))) 

116 

117 def testLazy(self): 

118 """Exercise the lazy function call in Cache.operator()""" 

119 def addFunction(cache, index): 

120 value = cache(index, lambda ii: numberToWords(ii)) 

121 self.assertEqual(value, numberToWords(index)) 

122 

123 cache = self.check(addFunction) 

124 

125 # Check that the function call doesn't fire when we pull out something that's in there already 

126 def trap(key): 

127 raise AssertionError("Failed") 

128 

129 for index in cache.keys(): 

130 value = cache(index, trap) 

131 self.assertEqual(value, numberToWords(index)) 

132 

133 # Check that this checking technique actually works... 

134 with self.assertRaises(AssertionError): 

135 cache(999, trap) 

136 

137 

138if __name__ == "__main__": 138 ↛ 139line 138 didn't jump to line 139, because the condition on line 138 was never true

139 unittest.main()