Debian Codegraph @

test/jpypetest/test_jboolean.py

raw symbols SCIP
1 # *****************************************************************************
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # See NOTICE file for details.
16 #
17 # *****************************************************************************
18 import _jpype
19 import jpype
20 from jpype.types import *
21 import sys
22 import logging
23 import time
24 import common
25 try:
26 import numpy as np
27 except ImportError:
28 pass
29
30
31 class JBooleanTestCase(common.JPypeTestCase):
32 def setUp(self):
33 common.JPypeTestCase.setUp(self)
34 self.Test = jpype.JClass("jpype.common.Fixture")()
35
36 @common.requireInstrumentation
37 def testJPBoolean_str(self):
38 jb = JBoolean(True)
39 _jpype.fault("PyJPBoolean_str")
40 with self.assertRaisesRegex(SystemError, "fault"):
41 str(jb)
42 _jpype.fault("PyJPModule_getContext")
43 str(jb)
44
45 @common.requireInstrumentation
46 def testJPBooleanType(self):
47 ja = JArray(JBoolean)(5) # lgtm [py/similar-function]
48 _jpype.fault("JPBooleanType::setArrayRange")
49 with self.assertRaisesRegex(SystemError, "fault"):
50 ja[1:3] = [0, 0]
51 with self.assertRaises(TypeError):
52 ja[1] = object()
53 jf = JClass("jpype.common.Fixture")
54 with self.assertRaises(TypeError):
55 jf.static_bool_field = object()
56 with self.assertRaises(TypeError):
57 jf().bool_field = object()
58
59 @common.requireInstrumentation
60 def testJBooleanGetJavaConversion(self):
61 _jpype.fault("JPBooleanType::findJavaConversion")
62 with self.assertRaisesRegex(SystemError, "fault"):
63 JBoolean._canConvertToJava(object())
64
65 def testBooleanFromInt(self):
66 self.assertEqual(self.Test.callBoolean(int(123)), True)
67 self.assertEqual(self.Test.callBoolean(int(0)), False)
68
69 @common.requireNumpy
70 def testBooleanFromNPInt(self):
71 import numpy as np
72 self.assertEqual(self.Test.callBoolean(np.int_(123)), True)
73
74 @common.requireNumpy
75 def testBooleanFromNPInt8(self):
76 import numpy as np
77 self.assertEqual(self.Test.callBoolean(np.int8(123)), True)
78 self.assertEqual(self.Test.callBoolean(np.uint8(123)), True)
79
80 @common.requireNumpy
81 def testBooleanFromNPInt16(self):
82 import numpy as np
83 self.assertEqual(self.Test.callBoolean(np.int16(123)), True)
84 self.assertEqual(self.Test.callBoolean(np.uint16(123)), True)
85
86 @common.requireNumpy
87 def testBooleanFromNPInt32(self):
88 import numpy as np
89 self.assertEqual(self.Test.callBoolean(np.int32(123)), True)
90 self.assertEqual(self.Test.callBoolean(np.uint32(123)), True)
91
92 @common.requireNumpy
93 def testBooleanFromNPInt64(self):
94 import numpy as np
95 self.assertEqual(self.Test.callBoolean(np.int64(123)), True)
96 self.assertEqual(self.Test.callBoolean(np.uint64(123)), True)
97
98 def testBooleanFromFloat(self):
99 with self.assertRaises(TypeError):
100 self.Test.callBoolean(float(2))
101
102 @common.requireNumpy
103 def testBooleanFromNPFloat16(self):
104 import numpy as np
105 with self.assertRaises(TypeError):
106 self.Test.callBoolean(np.float16(2))
107
108 @common.requireNumpy
109 def testBooleanFromNPFloat32(self):
110 import numpy as np
111 with self.assertRaises(TypeError):
112 self.Test.callBoolean(np.float32(2))
113
114 @common.requireNumpy
115 def testBooleanFromNPFloat64(self):
116 import numpy as np
117 with self.assertRaises(TypeError):
118 self.Test.callBoolean(np.float64(2))
119
120 def testBooleanFromNone(self):
121 with self.assertRaises(TypeError):
122 self.Test.callBoolean(None)
123
124 def testJArrayConversionBool(self):
125 expected = [True, False, False, True]
126 jarr = jpype.JArray(jpype.JBoolean)(expected)
127 self.assertEqual(expected, list(jarr[:]))
128
129 @common.requireNumpy
130 def testSetFromNPBoolArray(self):
131 import numpy as np
132 n = 100
133 a = np.random.randint(0, 1, size=n, dtype=np.bool_)
134 jarr = jpype.JArray(jpype.JBoolean)(n)
135 jarr[:] = a
136 self.assertCountEqual(a, jarr)
137
138 @common.requireNumpy
139 def testArrayBufferDims(self):
140 ja = JArray(JBoolean)(5)
141 a = np.zeros((5, 2))
142 with self.assertRaisesRegex(TypeError, "incorrect"):
143 ja[:] = a
144
145 def testArrayBadItem(self):
146 class q(object):
147 def __bool__(self):
148 raise SystemError("nope")
149 ja = JArray(JBoolean)(5)
150 a = [1, 2, q(), 3, 4]
151 with self.assertRaisesRegex(SystemError, "nope"):
152 ja[:] = a
153
154 def testArrayBadDims(self):
155 class q(bytes):
156 # Lie about our length
157 def __len__(self):
158 return 5
159 a = q([1, 2, 3])
160 ja = JArray(JBoolean)(5)
161 with self.assertRaisesRegex(ValueError, "Slice"):
162 ja[:] = [1, 2, 3]
163 with self.assertRaisesRegex(ValueError, "mismatch"):
164 ja[:] = a