MCPcopy Create free account
hub / github.com/numpy/numpy / test_python_scalar_construtors

Function test_python_scalar_construtors

numpy/array_api/tests/test_array_object.py:274–310  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

272
273
274def test_python_scalar_construtors():
275 b = asarray(False)
276 i = asarray(0)
277 f = asarray(0.0)
278 c = asarray(0j)
279
280 assert bool(b) == False
281 assert int(i) == 0
282 assert float(f) == 0.0
283 assert operator.index(i) == 0
284
285 # bool/int/float/complex should only be allowed on 0-D arrays.
286 assert_raises(TypeError, lambda: bool(asarray([False])))
287 assert_raises(TypeError, lambda: int(asarray([0])))
288 assert_raises(TypeError, lambda: float(asarray([0.0])))
289 assert_raises(TypeError, lambda: complex(asarray([0j])))
290 assert_raises(TypeError, lambda: operator.index(asarray([0])))
291
292 # bool should work on all types of arrays
293 assert bool(b) is bool(i) is bool(f) is bool(c) is False
294
295 # int should fail on complex arrays
296 assert int(b) == int(i) == int(f) == 0
297 assert_raises(TypeError, lambda: int(c))
298
299 # float should fail on complex arrays
300 assert float(b) == float(i) == float(f) == 0.0
301 assert_raises(TypeError, lambda: float(c))
302
303 # complex should work on all types of arrays
304 assert complex(b) == complex(i) == complex(f) == complex(c) == 0j
305
306 # index should only work on integer arrays
307 assert operator.index(i) == 0
308 assert_raises(TypeError, lambda: operator.index(b))
309 assert_raises(TypeError, lambda: operator.index(f))
310 assert_raises(TypeError, lambda: operator.index(c))
311
312
313def test_device_property():

Callers

nothing calls this directly

Calls 3

assert_raisesFunction · 0.90
asarrayFunction · 0.50
indexMethod · 0.45

Tested by

no test coverage detected