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 sys 

23import unittest 

24import numpy as np 

25 

26import lsst.utils.tests 

27from _cppIndex import cppIndex 

28import _example 

29 

30 

31class Pybind11TestCase(lsst.utils.tests.TestCase): 

32 """A test case basic pybind11 wrapping""" 

33 

34 def setUp(self): 

35 self.example = _example.Example("foo") 

36 

37 def testConstructors(self): 

38 self.assertEqual(self.example.getValue(), "foo") 

39 self.assertRaises(Exception, _example.Example, [5]) 

40 self.assertEqual(_example.Example("bar").getValue(), "bar") 

41 

42 @unittest.skip("it is questionable that this is desirable with pybind11") 

43 def testReturnNone(self): 

44 result = self.example.get1() 

45 self.assertIsNone(result) 

46 

47 @unittest.skip("it is questionable that this is desirable with pybind11") 

48 def testReturnSelf(self): 

49 result = self.example.get2() 

50 self.assertIs(result, self.example) 

51 

52 @unittest.skip("it is questionable that this is desirable with pybind11") 

53 def testReturnCopy(self): 

54 result = self.example.get3() 

55 self.assertIsNot(result, self.example) 

56 self.assertIsInstance(result, _example.Example) 

57 result.setValue("bar") 

58 self.assertEqual(self.example.getValue(), "foo") 

59 

60 def testStringification(self): 

61 s = "Example(foo)" 

62 self.assertEqual(str(self.example), s) 

63 self.assertEqual(repr(self.example), s) 

64 

65 def testEqualityComparison(self): 

66 self.assertNotEqual(self.example, _example.Example("bar")) 

67 self.assertEqual(self.example, _example.Example("foo")) 

68 

69 @unittest.skip("it is questionable that this is desirable with pybind11") 

70 def testListEqualityComparison(self): 

71 self.assertNotEqual(self.example, [3, 4, 5]) # should not throw 

72 self.assertNotEqual([3, 4, 5], self.example) # should not throw 

73 

74 def assertAccepts(self, function, value, msg): 

75 try: 

76 self.assertEqual(function(value), value, msg="%s: %r != %r" % (msg, function(value), value)) 

77 except TypeError: 

78 self.fail(msg) 

79 

80 def checkNumeric(self, function): 

81 self.assertAccepts(function, int(1), msg="Failure passing int to %s" % function.__name__) 

82 self.assertRaises((TypeError, NotImplementedError), 

83 function, "5") # should fail to convert even numeric strings 

84 # We should be able to coerce integers with different signedness and size to any numeric 

85 # type argument (as long as we don't trigger overflow) 

86 for size in (8, 16, 32, 64): 

87 for name in ("int%d" % size, "uint%d" % size): 

88 array = np.ones(1, dtype=getattr(np, name)) 

89 self.assertAccepts(function, array[0], 

90 msg="Failure passing numpy.%s to %s" % (name, function.__name__)) 

91 

92 def checkFloating(self, function): 

93 self.checkNumeric(function) 

94 self.assertAccepts(function, float(3.5), "Failure passing float to %s" % function.__name__) 

95 

96 def checkInteger(self, function, size): 

97 """If we pass an integer that doesn't fit in the C++ argument type, we should raise OverflowError""" 

98 self.checkNumeric(function) 

99 tooBig = 2**(size + 1) 

100 self.assertRaises(OverflowError, function, tooBig) 

101 

102 def testFloatingPoints(self): 

103 """Test our customized numeric scalar typemaps, including support for NumPy scalars.""" 

104 self.checkFloating(_example.accept_float32) 

105 self.checkFloating(_example.accept_cref_float32) 

106 self.checkFloating(_example.accept_cref_float64) 

107 

108 @unittest.skip("pybind11 does not (yet) support numpy scalar types") 

109 def testExtendedIntegers(self): 

110 for size in (8, 16, 32, 64): 

111 self.checkInteger(getattr(_example, "accept_int%d" % size), size) 

112 self.checkInteger(getattr(_example, "accept_uint%d" % size), size) 

113 self.checkInteger(getattr(_example, "accept_cref_int%d" % size), size) 

114 self.checkInteger(getattr(_example, "accept_cref_uint%d" % size), size) 

115 # Test that we choose the floating point overload when we pass a float, 

116 # and we get the integer overload when we pass an int. 

117 # We can't ever distinguish between different kinds of ints or different 

118 # kinds of floats in an overloading context, but that's a Pybind11 limitation. 

119 

120 def testOverloads(self): 

121 self.assertEqual(_example.getName(int(1)), "int") 

122 self.assertEqual(_example.getName(float(1)), "double") 

123 

124 def testCppIndex1Axis(self): 

125 """Test the 1-axis (2 argument) version of cppIndex 

126 """ 

127 # loop over various sizes 

128 # note that when size == 0 no indices are valid, but the "invalid indices" tests still run 

129 for size in range(4): 

130 # loop over all valid indices 

131 for ind in range(size): 

132 # the negative index that points to the same element as ind 

133 # for example if size = 3 and ind = 2 then negind = -1 

134 negind = ind - size 

135 

136 self.assertEqual(cppIndex(size, ind), ind) 

137 self.assertEqual(cppIndex(size, negind), ind) 

138 

139 # invalid indices (the two closest to zero) 

140 with self.assertRaises(IndexError): 

141 cppIndex(size, size) 

142 with self.assertRaises(IndexError): 

143 cppIndex(size, -size - 1) 

144 

145 def testCppIndex2Axis(self): 

146 """Test the 2-axis (4 argument) version of cppindex 

147 """ 

148 # loop over various sizes 

149 # if either size is 0 then no pairs of indices are valid, 

150 # but the "both indices invalid" tests still run 

151 for size0 in range(4): 

152 for size1 in range(4): 

153 # the first (closest to 0) invalid negative indices 

154 negbad0 = -size0 - 1 

155 negbad1 = -size1 - 1 

156 

157 # loop over all valid indices 

158 for ind0 in range(size0): 

159 for ind1 in range(size1): 

160 # negative indices that point to the same element as the positive index 

161 negind0 = ind0 - size0 

162 negind1 = ind1 - size1 

163 

164 # both indeces valid 

165 self.assertEqual(cppIndex(size0, size1, ind0, ind1), (ind0, ind1)) 

166 self.assertEqual(cppIndex(size0, size1, ind0, negind1), (ind0, ind1)) 

167 self.assertEqual(cppIndex(size0, size1, negind0, ind1), (ind0, ind1)) 

168 self.assertEqual(cppIndex(size0, size1, negind0, negind1), (ind0, ind1)) 

169 

170 # one index invalid 

171 with self.assertRaises(IndexError): 

172 cppIndex(size0, size1, ind0, size1) 

173 with self.assertRaises(IndexError): 

174 cppIndex(size0, size1, ind0, negbad1) 

175 with self.assertRaises(IndexError): 

176 cppIndex(size0, size1, size0, ind1) 

177 with self.assertRaises(IndexError): 

178 cppIndex(size0, size1, negbad0, ind1) 

179 

180 # both indices invalid (just test the invalid indices closest to 0) 

181 with self.assertRaises(IndexError): 

182 cppIndex(size0, size1, size0, size1) 

183 with self.assertRaises(IndexError): 

184 cppIndex(size0, size1, size0, -size1 - 1) 

185 with self.assertRaises(IndexError): 

186 cppIndex(size0, size1, negbad0, size1) 

187 with self.assertRaises(IndexError): 

188 cppIndex(size0, size1, negbad0, negbad1) 

189 

190 def testTemplateInvoker(self): 

191 """Test using TemplateInvoker to transform a C++ template to a 

192 Python function with a dtype argument. 

193 """ 

194 for t in (np.uint16, np.int32, np.float32): 

195 dtype = np.dtype(t) 

196 a = _example.returnTypeHolder(dtype) 

197 self.assertEqual(a.dtype, dtype) 

198 self.assertIsNone(_example.returnTypeHolder(np.dtype(np.float64))) 

199 

200 

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

202 pass 

203 

204 

205def setup_module(module): 

206 lsst.utils.tests.init() 

207 

208 

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

210 setup_module(sys.modules[__name__]) 

211 unittest.main()