def test_empty_arrays(self): # Empty arrays of any type (including float) should be allowed for dtype in self.dtypes_to_test: target = np.array([], dtype=dtype) converted = util.safe_np_int_cast([], dtype=dtype) assert pickle.dumps(converted) == pickle.dumps(target) target = np.array([[]], dtype=dtype) converted = util.safe_np_int_cast([[]], dtype=dtype) assert pickle.dumps(converted) == pickle.dumps(target)
def test_bad_types(self): # Shouldn't be able to convert a float (possibility of rounding error) for dtype in self.dtypes_to_test: for bad_type in [ [0.1], ["str"], {}, [{}], np.array([0, 1], dtype=float), ]: with pytest.raises(TypeError): util.safe_np_int_cast(bad_type, dtype)
def test_nonrectangular_input(self): bad_inputs = [ [0, 1, [2]], [[0, 1, 2], []], [(0, 1, 2), [2, 3]], [(0, 1, 2), tuple()], [(0, 1, 2), (2, )], [(0, 1, 2), [2, 3]]] for dtype in self.dtypes_to_test: for bad_input in bad_inputs: with self.assertRaises(TypeError): util.safe_np_int_cast(bad_input, dtype)
def test_basic_arrays(self): # Simple array for dtype in self.dtypes_to_test: target = np.array([0, 1], dtype=dtype) for test_array in [[0, 1], (0, 1), np.array([0, 1]), target]: converted = util.safe_np_int_cast(test_array, dtype=dtype) # Use pickle to test exact equality including dtype assert pickle.dumps(converted) == pickle.dumps(target) # Nested array target = np.array([[0, 1], [2, 3]], dtype=dtype) for test_array in [[[0, 1], [2, 3]], np.array([[0, 1], [2, 3]]), target]: converted = util.safe_np_int_cast(test_array, dtype=dtype) assert pickle.dumps(converted) == pickle.dumps(target)
def test_copy(self): # Check that a copy is not returned if copy=False & the original matches # the specs for dtype in self.dtypes_to_test: for orig in (np.array([0, 1], dtype=dtype), np.array([], dtype=dtype)): converted = util.safe_np_int_cast(orig, dtype=dtype, copy=True) assert id(orig) != id(converted) converted = util.safe_np_int_cast(orig, dtype=dtype, copy=False) assert id(orig) == id(converted) for dtype in [d for d in self.dtypes_to_test if d != np.int64]: # non numpy arrays, or arrays of a different dtype don't get converted for orig in ([0, 1], np.array([0, 1], dtype=np.int64)): converted = util.safe_np_int_cast(orig, dtype=dtype, copy=False) assert id(orig) != id(converted)
def test_nonrectangular_input(self): bad_inputs = [ [0, 1, [2]], [[0, 1, 2], []], [(0, 1, 2), [2, 3]], [(0, 1, 2), tuple()], [(0, 1, 2), (2,)], [(0, 1, 2), [2, 3]], ] for dtype in self.dtypes_to_test: for bad_input in bad_inputs: # On some platforms and Python / numpy versions, a ValueError # occurs instead with self.assertRaises((TypeError, ValueError)): util.safe_np_int_cast(bad_input, dtype)
def test_overflow(self): for dtype in self.dtypes_to_test: for bad_node in [np.iinfo(dtype).min - 1, np.iinfo(dtype).max + 1]: self.assertRaises( # Test plain array OverflowError, util.safe_np_int_cast, [0, bad_node], dtype) self.assertRaises( # Test numpy array OverflowError, util.safe_np_int_cast, np.array([0, bad_node]), dtype) for good_node in [np.iinfo(dtype).min, np.iinfo(dtype).max]: target = np.array([good_node], dtype=dtype) self.assertEqual( # Test plain array pickle.dumps(target), pickle.dumps(util.safe_np_int_cast([good_node], dtype))) self.assertEqual( # Test numpy array pickle.dumps(target), pickle.dumps(util.safe_np_int_cast(np.array([good_node]), dtype)))
def test_overflow(self): for dtype in self.dtypes_to_test: for bad_node in [np.iinfo(dtype).min - 1, np.iinfo(dtype).max + 1]: with pytest.raises(OverflowError): util.safe_np_int_cast([0, bad_node], dtype) with pytest.raises(OverflowError): util.safe_np_int_cast(np.array([0, bad_node]), dtype) for good_node in [np.iinfo(dtype).min, np.iinfo(dtype).max]: target = np.array([good_node], dtype=dtype) assert pickle.dumps(target) == pickle.dumps( util.safe_np_int_cast([good_node], dtype)) assert pickle.dumps(target) == pickle.dumps( util.safe_np_int_cast(np.array([good_node]), dtype))