Commits

Brian Kearns committed c6a40e5

enhance tests for numpypy string record/array to demonstrate existing bugs

Comments (0)

Files changed (4)

pypy/module/micronumpy/interp_boxes.py

         for i in range(len(arg)):
             arr.storage[i] = arg[i]
         return W_StringBox(arr, 0, arr.dtype)
-            
+
     def convert_to(self, dtype):
         from pypy.module.micronumpy import types
         assert isinstance(dtype.itemtype, types.StringType)
-        return self        
+        return self
 
 class W_UnicodeBox(W_CharacterBox):
     def descr__new__unicode_box(space, w_subtype, w_arg):
     def convert_to(self, dtype):
         from pypy.module.micronumpy import types
         assert isinstance(dtype.itemtype, types.UnicodeType)
-        return self        
+        return self
 
 
 class W_ComplexFloatingBox(W_InexactBox):

pypy/module/micronumpy/interp_dtype.py

             name="float64",
             char="d",
             w_box_type = space.gettypefor(interp_boxes.W_Float64Box),
-            alternate_constructors=[space.w_float, 
+            alternate_constructors=[space.w_float,
                                     space.gettypefor(interp_boxes.W_NumberBox),
                                    ],
             aliases=["float"],

pypy/module/micronumpy/test/test_numarray.py

 
     def test_string_record(self):
         from _numpypy import dtype, array
+
         d = dtype([('x', str), ('y', 'int32')])
-        assert d.fields['x'] == (dtype(str), 0)
-        assert d.fields['y'] == (dtype('int32'), 1)
+        assert str(d.fields['x'][0]) == '|S0'
+        assert d.fields['x'][1] == 0
+        assert str(d.fields['y'][0]) == 'int32'
+        assert d.fields['y'][1] == 0
+        assert d.name == 'void32'
+
+        a = array([('a', 2), ('cde', 1)], dtype=d)
+        assert a[0]['x'] == '\x02'
+        assert a[0]['y'] == 2
+        assert a[1]['x'] == '\x01'
+        assert a[1]['y'] == 1
+
         d = dtype([('x', 'S1'), ('y', 'int32')])
-        assert d.fields['x'] == (dtype(str), 0)
-        assert d.fields['y'] == (dtype('int32'), 1)
-        a = array([('a', 2), ('c', 1)], dtype=d)
+        assert str(d.fields['x'][0]) == '|S1'
+        assert d.fields['x'][1] == 0
+        assert str(d.fields['y'][0]) == 'int32'
+        assert d.fields['y'][1] == 1
+        assert d.name == 'void40'
+
+        a = array([('a', 2), ('cde', 1)], dtype=d)
+        assert a[0]['x'] == 'a'
+        assert a[0]['y'] == 2
+        assert a[1]['x'] == 'c'
         assert a[1]['y'] == 1
-        assert a[0]['x'] == 'a'
 
-    def test_stringarray(self):
+    def test_string_array(self):
         from _numpypy import array
-        a = array(['abc'],'S3')
-        assert str(a.dtype) == '|S3'
         a = array(['abc'])
         assert str(a.dtype) == '|S3'
-        a = array(['abc','defg','ab'])
+        a = array(['abc'], 'S')
+        assert str(a.dtype) == '|S3'
+        a = array(['abc'], 'S3')
+        assert str(a.dtype) == '|S3'
+        a = array(['abcde'], 'S3')
+        assert str(a.dtype) == '|S3'
+        a = array(['abc', 'defg', 'ab'])
         assert str(a.dtype) == '|S4'
         assert a[0] == 'abc'
         assert a[1] == 'defg'
         from _numpypy import array
         a = array('ffff')
         assert a.shape == ()
+        a = array([], dtype='S')
+        assert str(a.dtype) == '|S1'
         a = array('x', dtype='>S')
         assert str(a.dtype) == '|S1'
         a = array('x', dtype='c')
         s = repr(a)
         assert s.replace('\n', '') == \
                       "array(['abc', 'defg', 'ab'],       dtype='|S4')"
-        
-       
+
+
 class AppTestPyPy(BaseNumpyAppTest):
     def setup_class(cls):
         if option.runappdirect and '__pypy__' not in sys.builtin_module_names:
             py.test.skip("pypy only test")
         BaseNumpyAppTest.setup_class.im_func(cls)
-    
+
     def test_init_2(self):
         # this test is pypy only since in numpy it becomes an object dtype
         import _numpypy

pypy/module/micronumpy/types.py

     def str_format(self, box):
         real, imag = self.for_computation(self.unbox(box))
         imag_str = str_format(imag) + 'j'
-        
+
         # (0+2j) => 2j
         if real == 0:
-            return imag_str        
+            return imag_str
 
         real_str = str_format(real)
         op = '+' if imag >= 0 else ''
         return ''.join(['(', real_str, op, imag_str, ')'])
 
     @staticmethod
-    def for_computation(v):   
+    def for_computation(v):
         return float(v[0]), float(v[1])
 
     @raw_unary_op
     @complex_binary_op
     def mul(self, v1, v2):
         return rcomplex.c_mul(v1, v2)
-    
+
     @complex_binary_op
     def div(self, v1, v2):
         try: