def test_validate(self): # python list obs = _validate([0, 2, 1, 3]) npt.assert_array_equal(obs, np.array([0, 2, 1, 3])) self.assertEqual(obs.dtype, int) # numpy array (no copy made) data = np.array([0, 2, 1, 3]) obs = _validate(data) npt.assert_array_equal(obs, data) self.assertEqual(obs.dtype, int) self.assertTrue(obs is data) # single element obs = _validate([42]) npt.assert_array_equal(obs, np.array([42])) self.assertEqual(obs.dtype, int) self.assertEqual(obs.shape, (1,)) # suppress casting to int obs = _validate([42.2, 42.1, 0], suppress_cast=True) npt.assert_array_equal(obs, np.array([42.2, 42.1, 0])) self.assertEqual(obs.dtype, float) # all zeros obs = _validate([0, 0, 0]) npt.assert_array_equal(obs, np.array([0, 0, 0])) self.assertEqual(obs.dtype, int) # all zeros (single value) obs = _validate([0]) npt.assert_array_equal(obs, np.array([0])) self.assertEqual(obs.dtype, int)
def test_validate_invalid_input(self): # wrong dtype with self.assertRaises(TypeError): _validate([0, 2, 1.2, 3]) # wrong number of dimensions (2-D) with self.assertRaises(ValueError): _validate([[0, 2, 1, 3], [4, 5, 6, 7]]) # wrong number of dimensions (scalar) with self.assertRaises(ValueError): _validate(1) # negative values with self.assertRaises(ValueError): _validate([0, 0, 2, -1, 3])