Hide keyboard shortcuts

Hot-keys 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

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 

24import lsst.utils.tests 

25from _cache import NumbersCache 

26 

27 

28def numberToWords(value): 

29 """Convert a number in the range [0, 1000) to words""" 

30 assert value >= 0 and isinstance(value, int), "Only non-negative integers" 

31 if value < 20: 

32 return { 

33 0: "zero", 

34 1: "one", 

35 2: "two", 

36 3: "three", 

37 4: "four", 

38 5: "five", 

39 6: "six", 

40 7: "seven", 

41 8: "eight", 

42 9: "nine", 

43 10: "ten", 

44 11: "eleven", 

45 12: "twelve", 

46 13: "thirteen", 

47 14: "fourteen", 

48 15: "fifteen", 

49 16: "sixteen", 

50 17: "seventeen", 

51 18: "eighteen", 

52 19: "nineteen", 

53 }[value] 

54 if value < 100: 

55 tens = value//10 

56 ones = value % 10 

57 return { 

58 2: "twenty", 

59 3: "thirty", 

60 4: "forty", 

61 5: "fifty", 

62 6: "sixty", 

63 7: "seventy", 

64 8: "eighty", 

65 9: "ninety", 

66 }[tens] + (("-" + numberToWords(ones)) if ones > 0 else "") 

67 assert value < 1000, "Value exceeds limit of 999" 

68 hundreds = value//100 

69 rest = value % 100 

70 return numberToWords(hundreds) + " hundred" + ((" " + numberToWords(rest)) if rest > 0 else "") 

71 

72 

73class CacheTestCase(lsst.utils.tests.TestCase): 

74 """Tests of lsst.utils.Cache""" 

75 def check(self, addFunction): 

76 """Exercise the Cache 

77 

78 The `addFunction` should take a cache and number, 

79 and add the number (and its corresponding string) 

80 into the cache. 

81 """ 

82 capacity = 10 

83 cache = NumbersCache(capacity) 

84 self.assertEqual(cache.size(), 0, "Starts empty") 

85 self.assertEqual(cache.capacity(), capacity, "Capacity as requested") 

86 maximum = 20 

87 for ii in range(maximum): 

88 addFunction(cache, ii) 

89 self.assertEqual(cache.size(), capacity, "Filled to capacity") 

90 self.assertEqual(cache.capacity(), capacity, "Capacity unchanged") 

91 for ii in range(maximum - capacity): 

92 self.assertNotIn(ii, cache, "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.assertListEqual(actualContents, expectedContents, "Contents are as expected") 

99 with self.assertRaises(LookupError): 

100 cache[maximum - capacity - 1] 

101 newCapacity = 5 

102 cache.reserve(newCapacity) 

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

104 # touching items. 

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

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

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

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

109 cache.flush() 

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

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

112 return cache 

113 

114 def testDirect(self): 

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

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

117 

118 def testLazy(self): 

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

120 def addFunction(cache, index): 

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

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

123 

124 cache = self.check(addFunction) 

125 

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

127 def trap(key): 

128 raise AssertionError("Failed") 

129 

130 for index in cache.keys(): 

131 value = cache(index, trap) 

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

133 

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

135 with self.assertRaises(AssertionError): 

136 cache(999, trap) 

137 

138 

139class TestMemory(lsst.utils.tests.MemoryTestCase): 

140 pass 

141 

142 

143def setup_module(module): 

144 lsst.utils.tests.init() 

145 

146 

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

148 import sys 

149 setup_module(sys.modules[__name__]) 

150 unittest.main()