Coverage for tests/test_pybind11.py: 24%

117 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-15 07:52 +0000

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 

23import numpy as np 

24 

25from _cppIndex import cppIndex 

26import _example 

27 

28 

29class Pybind11TestCase(unittest.TestCase): 

30 """A test case basic pybind11 wrapping""" 

31 

32 def setUp(self): 

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

34 

35 def testConstructors(self): 

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

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

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

39 

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

41 def testReturnNone(self): 

42 result = self.example.get1() 

43 self.assertIsNone(result) 

44 

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

46 def testReturnSelf(self): 

47 result = self.example.get2() 

48 self.assertIs(result, self.example) 

49 

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

51 def testReturnCopy(self): 

52 result = self.example.get3() 

53 self.assertIsNot(result, self.example) 

54 self.assertIsInstance(result, _example.Example) 

55 result.setValue("bar") 

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

57 

58 def testStringification(self): 

59 s = "Example(foo)" 

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

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

62 

63 def testEqualityComparison(self): 

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

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

66 

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

68 def testListEqualityComparison(self): 

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

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

71 

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

73 try: 

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

75 except TypeError: 

76 self.fail(msg) 

77 

78 def checkNumeric(self, function): 

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

80 self.assertRaises((TypeError, NotImplementedError), 

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

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

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

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

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

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

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

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

89 

90 def checkFloating(self, function): 

91 self.checkNumeric(function) 

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

93 

94 def checkInteger(self, function, size): 

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

96 self.checkNumeric(function) 

97 tooBig = 2**(size + 1) 

98 self.assertRaises(OverflowError, function, tooBig) 

99 

100 def testFloatingPoints(self): 

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

102 self.checkFloating(_example.accept_float32) 

103 self.checkFloating(_example.accept_cref_float32) 

104 self.checkFloating(_example.accept_cref_float64) 

105 

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

107 def testExtendedIntegers(self): 

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

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

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

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

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

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

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

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

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

117 

118 def testOverloads(self): 

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

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

121 

122 def testCppIndex1Axis(self): 

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

124 """ 

125 # loop over various sizes 

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

127 for size in range(4): 

128 # loop over all valid indices 

129 for ind in range(size): 

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

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

132 negind = ind - size 

133 

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

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

136 

137 # invalid indices (the two closest to zero) 

138 with self.assertRaises(IndexError): 

139 cppIndex(size, size) 

140 with self.assertRaises(IndexError): 

141 cppIndex(size, -size - 1) 

142 

143 def testCppIndex2Axis(self): 

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

145 """ 

146 # loop over various sizes 

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

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

149 for size0 in range(4): 

150 for size1 in range(4): 

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

152 negbad0 = -size0 - 1 

153 negbad1 = -size1 - 1 

154 

155 # loop over all valid indices 

156 for ind0 in range(size0): 

157 for ind1 in range(size1): 

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

159 negind0 = ind0 - size0 

160 negind1 = ind1 - size1 

161 

162 # both indeces valid 

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

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

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

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

167 

168 # one index invalid 

169 with self.assertRaises(IndexError): 

170 cppIndex(size0, size1, ind0, size1) 

171 with self.assertRaises(IndexError): 

172 cppIndex(size0, size1, ind0, negbad1) 

173 with self.assertRaises(IndexError): 

174 cppIndex(size0, size1, size0, ind1) 

175 with self.assertRaises(IndexError): 

176 cppIndex(size0, size1, negbad0, ind1) 

177 

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

179 with self.assertRaises(IndexError): 

180 cppIndex(size0, size1, size0, size1) 

181 with self.assertRaises(IndexError): 

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

183 with self.assertRaises(IndexError): 

184 cppIndex(size0, size1, negbad0, size1) 

185 with self.assertRaises(IndexError): 

186 cppIndex(size0, size1, negbad0, negbad1) 

187 

188 def testTemplateInvoker(self): 

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

190 Python function with a dtype argument. 

191 """ 

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

193 dtype = np.dtype(t) 

194 a = _example.returnTypeHolder(dtype) 

195 self.assertEqual(a.dtype, dtype) 

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

197 

198 

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

200 unittest.main()