diff --git a/dynd/nd/test/test_array_basics.py b/dynd/nd/test/test_array_basics.py index 1d4de48b..2627a2cd 100644 --- a/dynd/nd/test/test_array_basics.py +++ b/dynd/nd/test/test_array_basics.py @@ -47,9 +47,9 @@ def test_nonzero(self): self.assertTrue(bool(nd.array(100.0, type=ndt.float32))) self.assertTrue(bool(nd.array(100.0, type=ndt.float64))) # complex values -# self.assertFalse(bool(nd.array(0.0, type=ndt.complex_float32))) ##### FIX ME + self.assertFalse(bool(nd.array(0.0, type=ndt.complex_float32))) self.assertFalse(bool(nd.array(0.0, type=ndt.complex_float64))) -# self.assertTrue(bool(nd.array(100.0+10.0j, type=ndt.complex_float32))) ###### FIX ME + self.assertTrue(bool(nd.array(100.0+10.0j, type=ndt.complex_float32))) self.assertTrue(bool(nd.array(100.0+10.0j, type=ndt.complex_float64))) # strings self.assertFalse(bool(nd.array(''))) @@ -113,7 +113,6 @@ def test_contiguous(self): self.assertFalse(nd.is_c_contiguous(a)) self.assertFalse(nd.is_f_contiguous(a)) - """ def test_uint_value_limits(self): # Valid maximums a = nd.array(0xff, type = ndt.uint8) @@ -129,7 +128,6 @@ def test_uint_value_limits(self): self.assertRaises(OverflowError, nd.array, 0x10000, type = ndt.uint16) self.assertRaises(OverflowError, nd.array, 0x100000000, type = ndt.uint32) self.assertRaises(OverflowError, nd.array, 0x10000000000000000, type = ndt.uint64) - """ def test_inf(self): # Validate nd.inf diff --git a/dynd/nd/test/test_array_cast.py b/dynd/nd/test/test_array_cast.py index 5b739d5e..24bc867a 100644 --- a/dynd/nd/test/test_array_cast.py +++ b/dynd/nd/test/test_array_cast.py @@ -4,10 +4,10 @@ from dynd import nd, ndt class TestNDObjectCast(unittest.TestCase): -# def test_broadcast_cast(self): -# a = nd.array(10) -# b = a.cast('3 * int32') -# self.assertEqual(nd.as_py(b), [10, 10, 10]) + def test_broadcast_cast(self): + a = nd.array(10) + b = a.cast('3 * int32') + self.assertEqual(nd.as_py(b), [10, 10, 10]) def test_strided_to_fixed(self): a = nd.array([5,1,2]) diff --git a/dynd/nd/test/test_array_construct.py b/dynd/nd/test/test_array_construct.py index d9ffc389..82d3cbdb 100644 --- a/dynd/nd/test/test_array_construct.py +++ b/dynd/nd/test/test_array_construct.py @@ -277,15 +277,12 @@ def test_single_struct(self): self.assertEqual(nd.as_py(a[2]), True) def test_nested_struct(self): - """ - # FIX ME a = nd.array([[1,2], ['test', 3.5], [3j]], type='{x: 2 * int16, y: {a: string, b: float64}, z: 1 * complex[float32]}') self.assertEqual(nd.as_py(a.x), [1, 2]) self.assertEqual(nd.as_py(a.y.a), 'test') self.assertEqual(nd.as_py(a.y.b), 3.5) self.assertEqual(nd.as_py(a.z), [3j]) - """ a = nd.array({'x':[1,2], 'y':{'a':'test', 'b':3.5}, 'z':[3j]}, type='{x: 2 * int16, y: {a: string, b: float64}, z: 1 * complex[float64]}') @@ -622,53 +619,50 @@ def test_extra_field(self): # self.assertEqual(nd.type_of(a), ndt.type('var * int32')) # self.assertEqual(nd.as_py(a), [2*x + 1 for x in range(7)]) -""" class TestDeduceDims(unittest.TestCase): + """ def test_simplearr(self): val = [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[11, 12], [13, 14]], [[15, 16], [17, 18]]] # Deduce all the dims - a = nd.array(val, dtype=ndt.int16) + a = nd.array(val, type=ndt.int16) self.assertEqual(nd.type_of(a), ndt.type('4 * 2 * 2 * int16')) self.assertEqual(nd.as_py(a), val) # Specify some dims as fixed - a = nd.array(val, dtype='Fixed * int16') + a = nd.array(val, type='Fixed * int16') self.assertEqual(nd.type_of(a), ndt.type('4 * 2 * 2 * int16')) self.assertEqual(nd.as_py(a), val) - a = nd.array(val, dtype='Fixed * Fixed * int16') + a = nd.array(val, type='Fixed * Fixed * int16') self.assertEqual(nd.type_of(a), ndt.type('4 * 2 * 2 * int16')) self.assertEqual(nd.as_py(a), val) - a = nd.array(val, dtype='Fixed * Fixed * Fixed * int16') + a = nd.array(val, type='Fixed * Fixed * Fixed * int16') self.assertEqual(nd.type_of(a), ndt.type('4 * 2 * 2 * int16')) self.assertEqual(nd.as_py(a), val) # Specify some dims as fixed - a = nd.array(val, dtype='2 * int16') + a = nd.array(val, type='2 * int16') self.assertEqual(nd.type_of(a), ndt.type('4 * 2 * 2 * int16')) self.assertEqual(nd.as_py(a), val) - a = nd.array(val, dtype='2 * 2 * int16') + a = nd.array(val, type='2 * 2 * int16') self.assertEqual(nd.type_of(a), ndt.type('4 * 2 * 2 * int16')) self.assertEqual(nd.as_py(a), val) - a = nd.array(val, dtype='4 * 2 * 2 * int16') + a = nd.array(val, type='4 * 2 * 2 * int16') self.assertEqual(nd.type_of(a), ndt.type('4 * 2 * 2 * int16')) self.assertEqual(nd.as_py(a), val) # Mix fixed, symbolic fixed, and var - a = nd.array(val, dtype='4 * var * Fixed * int16') + a = nd.array(val, type='4 * var * Fixed * int16') self.assertEqual(nd.type_of(a), ndt.type('4 * var * 2 * int16')) self.assertEqual(nd.as_py(a), val) - a = nd.array(val, dtype='var * 2 * int16') + a = nd.array(val, type='var * 2 * int16') self.assertEqual(nd.type_of(a), ndt.type('4 * var * 2 * int16')) self.assertEqual(nd.as_py(a), val) - a = nd.array(val, dtype='Fixed * 2 * int16') + a = nd.array(val, type='Fixed * 2 * int16') self.assertEqual(nd.type_of(a), ndt.type('4 * 2 * 2 * int16')) self.assertEqual(nd.as_py(a), val) + """ def test_empty(self): - # A fixed dimension of non-zero size gets pushed down - a = nd.array([], dtype='3 * int32') - self.assertEqual(nd.type_of(a), ndt.type('0 * 3 * int32')) - self.assertEqual(nd.as_py(a), []) # A fixed dimension of zero size gets absorbed - a = nd.array([], dtype='0 * int32') + a = nd.array([], type='0 * int32') self.assertEqual(nd.type_of(a), ndt.type('0 * int32')) self.assertEqual(nd.as_py(a), []) # A symbolic fixed dimension gets absorbed @@ -677,23 +671,22 @@ def test_empty(self): # self.assertEqual(nd.type_of(a), ndt.type('0 * int32')) # self.assertEqual(nd.as_py(a), []) # A var dimension gets absorbed - a = nd.array([], dtype='var * int32') + a = nd.array([], type='var * int32') self.assertEqual(nd.type_of(a), ndt.type('var * int32')) self.assertEqual(nd.as_py(a), []) -""" -#class TestConstructErrors(unittest.TestCase): -# def test_bad_params(self): -# self.assertRaises(ValueError, nd.array, type='int32') -# self.assertRaises(ValueError, nd.array, type='2 * 2 * int32') -# self.assertRaises(ValueError, nd.array, access='readwrite') +class TestConstructErrors(unittest.TestCase): + def test_bad_params(self): + self.assertRaises(TypeError, nd.array, type='int32') + self.assertRaises(TypeError, nd.array, type='2 * 2 * int32') -# def test_dict_auto_detect(self): -# # Trigger failure in initial auto detect pass -# self.assertRaises(ValueError, nd.array, {'x' : 1}) -# self.assertRaises(ValueError, nd.array, [{'x' : 1}]) -# # Trigger failure in later type promotion -# self.assertRaises(ValueError, nd.array, [['a'], {'x' : 1}]) + def test_dict_auto_detect(self): + # Trigger failure in initial auto detect pass + self.assertRaises(ValueError, nd.array, {'x' : 1}) + self.assertRaises(ValueError, nd.array, [{'x' : 1}]) + # Trigger failure in later type promotion + # TODO: fix + # self.assertRaises(ValueError, nd.array, [['a'], {'x' : 1}]) class TestOptionArrayConstruct(unittest.TestCase): def check_scalars(self, type, input_expected): @@ -704,11 +697,11 @@ def check_scalars(self, type, input_expected): self.assertEqual(nd.as_py(a), expected) def test_scalar_option(self): -# self.check_scalars('?bool', [(None, None), -# ('', None), -# ('NA', None), -# (False, False), -# ('true', True)]) + self.check_scalars('?bool', [(None, None), + #('', None), + #('NA', None), + (False, False), + ('true', True)]) self.check_scalars('?int', [(None, None), ('', None), ('NA', None), (-10, -10), ('12354', 12354)]) self.check_scalars('?real', [(None, None), ('', None), @@ -717,10 +710,11 @@ def test_scalar_option(self): ('12354', 12354), (1.25, 1.25), ('125e20', 125e20)]) -# self.check_scalars('?string', [(None, None), -# ('', ''), -# ('NA', 'NA'), -# (u'\uc548\ub155', u'\uc548\ub155')]) + #self.check_scalars('?string', [(None, None), + # ('', ''), + # ('NA', 'NA'), + # (u'\uc548\ub155', u'\uc548\ub155') + # ]) if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/dynd/nd/test/test_array_dtype.py b/dynd/nd/test/test_array_dtype.py index 8abcb71d..846a1955 100644 --- a/dynd/nd/test/test_array_dtype.py +++ b/dynd/nd/test/test_array_dtype.py @@ -27,25 +27,25 @@ def test_type_type(self): self.assertEqual(nd.type_of(n), d) self.assertEqual(nd.as_py(n), ndt.float64) - #def test_symbolic_type(self): - # tp = ndt.type('(int, real) -> complex') - # self.assertEqual(tp.type_id, 'callable') - # self.assertEqual(nd.as_py(tp.pos_types), [ndt.int32, ndt.float64]) - # self.assertEqual(tp.return_type, ndt.complex_float64) - # tp = ndt.type('MyType') - # self.assertEqual(tp.type_id, 'typevar') - # self.assertEqual(tp.name, 'MyType') - # tp = ndt.type('MyDim * int') - # self.assertEqual(tp.type_id, 'typevar_dim') - # self.assertEqual(tp.name, 'MyDim') - # self.assertEqual(tp.element_type, ndt.int32) - # tp = ndt.type('... * int') - # self.assertEqual(tp.type_id, 'ellipsis_dim') - # self.assertEqual(tp.element_type, ndt.int32) - # tp = ndt.type('MyEll... * int') - # self.assertEqual(tp.type_id, 'ellipsis_dim') - # self.assertEqual(tp.name, 'MyEll') - # self.assertEqual(tp.element_type, ndt.int32) + def test_symbolic_type(self): + tp = ndt.type('(int, real) -> complex') + self.assertTrue(ndt.type('Callable').match(tp)) + self.assertEqual(tp.pos_types, [ndt.int32, ndt.float64]) + self.assertEqual(tp.return_type, ndt.complex_float64) + #tp = ndt.type('MyType') + #self.assertEqual(tp.type_id, 'typevar') + #self.assertEqual(tp.name, 'MyType') + #tp = ndt.type('MyDim * int') + #self.assertEqual(tp.type_id, 'typevar_dim') + #self.assertEqual(tp.name, 'MyDim') + #self.assertEqual(tp.element_type, ndt.int32) + #tp = ndt.type('... * int') + #self.assertEqual(tp.type_id, 'ellipsis_dim') + #self.assertEqual(tp.element_type, ndt.int32) + #tp = ndt.type('MyEll... * int') + #self.assertEqual(tp.type_id, 'ellipsis_dim') + #self.assertEqual(tp.name, 'MyEll') + #self.assertEqual(tp.element_type, ndt.int32) """ ToDo: Fix this. diff --git a/dynd/nd/test/test_array_setitem.py b/dynd/nd/test/test_array_setitem.py index daf86e42..371d03f9 100644 --- a/dynd/nd/test/test_array_setitem.py +++ b/dynd/nd/test/test_array_setitem.py @@ -1,8 +1,8 @@ import sys import unittest +from collections import OrderedDict as odict, namedtuple as ntuple from dynd import nd, ndt -@unittest.skip('Test disabled since callables were reworked') class TestArraySetItem(unittest.TestCase): def test_strided_dim(self): @@ -10,37 +10,47 @@ def test_strided_dim(self): a[...] = nd.range(100) a[0] = 1000 self.assertEqual(nd.as_py(a[0]), 1000) - a[1:8:3] = 120 - self.assertEqual(nd.as_py(a[:11]), - [1000, 120, 2, 3, 120, 5, 6, 120, 8, 9, 10]) - a[5:2:-1] = [-10, -20, -30] - self.assertEqual(nd.as_py(a[:11]), - [1000, 120, 2, -30, -20, -10, 6, 120, 8, 9, 10]) - a[1] = False - self.assertEqual(nd.as_py(a[1]), 0) - a[2] = True - self.assertEqual(nd.as_py(a[2]), 1) - a[3] = -10.0 - self.assertEqual(nd.as_py(a[3]), -10) + # Next line causes a crash. + #a[1:8:3] = 120 + #self.assertEqual(nd.as_py(a[:11]), + # [1000, 120, 2, 3, 120, 5, 6, 120, 8, 9, 10]) + #a[5:2:-1] = [-10, -20, -30] + #self.assertEqual(nd.as_py(a[:11]), + # [1000, 120, 2, -30, -20, -10, 6, 120, 8, 9, 10]) + #a[1] = False + #self.assertEqual(nd.as_py(a[1]), 0) + #a[2] = True + #self.assertEqual(nd.as_py(a[2]), 1) + #a[3] = -10.0 + #self.assertEqual(nd.as_py(a[3]), -10) # Should the following even be supported? # a[4] = 101.0 + 0j # self.assertEqual(nd.as_py(a[4]), 101) - """ - Todo: Fix this test when structs can assign to named tuples. - def test_assign_to_struct(self): value = [(8, u'world', 4.5), (16, u'!', 8.75)] # Assign list of tuples a = nd.empty('2 * { i : int32, msg : string, price : float64 }') a[:] = value - self.assertEqual(nd.as_py(a), value) + keys = ['i', 'msg', 'price'] + out_val = [{key:val for key, val in zip(keys, val_row)} + for val_row in value] + self.assertEqual(nd.as_py(a), out_val) # Assign iterator of tuples a = nd.empty('2 * { i : int32, msg : string, price : float64 }') a[:] = iter(value) - self.assertEqual(nd.as_py(a, tuple=True), value) - """ + self.assertEqual(nd.as_py(a), out_val) + # Assign a list of OrderedDicts + a = nd.empty('2 * { i : int32, msg : string, price : float64 }') + a[:] = [odict(zip(keys, vals)) for vals in value] + self.assertEqual(nd.as_py(a), out_val) + # Assign a list of namedtuples + named_class = ntuple('named_class', ['i', 'msg', 'price']) + nt_vals = [named_class(*item) for item in value] + a = nd.empty('2 * { i : int32, msg : string, price : float64 }') + a[:] = nt_vals + self.assertEqual(nd.as_py(a), out_val) if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/dynd/nd/test/test_array_squeeze.py b/dynd/nd/test/test_array_squeeze.py index 90b044e1..3a8e46f5 100644 --- a/dynd/nd/test/test_array_squeeze.py +++ b/dynd/nd/test/test_array_squeeze.py @@ -18,9 +18,6 @@ def test_squeeze_strided(self): self.assertEqual(a.shape, (3, 1)) self.assertEqual(nd.squeeze(a).shape, (3,)) - """ - ToDo: Fix this. - def test_squeeze_var(self): # Simple var case (squeeze can see into leading size-1 var dims) a = nd.array([[[1], [2,3]]], type='var * var * var * int32') @@ -32,7 +29,6 @@ def test_squeeze_var(self): self.assertEqual(a.shape, (1, 2, -1, 1)) self.assertEqual(nd.squeeze(a).shape, (2, -1)) self.assertEqual(nd.as_py(nd.squeeze(a)), [[1], [2,3]]) - """ def test_squeeze_axis(self): a = nd.zeros(1, 3, 1, 2, 1, ndt.int32) diff --git a/dynd/nd/test/test_arrfunc.py b/dynd/nd/test/test_callable.py similarity index 96% rename from dynd/nd/test/test_arrfunc.py rename to dynd/nd/test/test_callable.py index 83ee07e5..51ebc61d 100644 --- a/dynd/nd/test/test_arrfunc.py +++ b/dynd/nd/test/test_callable.py @@ -13,20 +13,15 @@ else: c_ssize_t = ctypes.c_int64 -class TestArrFunc(unittest.TestCase): +class TestCallable(unittest.TestCase): pass """ def test_creation(self): af = nd.empty('(float32) -> int32') - self.assertEqual(nd.type_of(af).type_id, 'arrfunc') + self.assertTrue(ndt.type('Callable').match(nd.type_of(af))) # Test there is a string version of a NULL arrfunc self.assertTrue(str(af) != '') - # Test there is a string version of an initialized arrfunc - af = _lowlevel.make_arrfunc_from_assignment( - ndt.float32, ndt.int64, "nocheck") - self.assertTrue(str(af) != '') - self.assertEqual(nd.type_of(af), ndt.type("(int64) -> float32")) """ # def test_arrfunc_constructor(self):