Commits

Brian Kearns  committed 73a4cbc

test/fix bug in string arrays with null characters

  • Participants
  • Parent commits c6b37a9

Comments (0)

Files changed (3)

File pypy/module/micronumpy/test/test_numarray.py

         assert b[0] == "\x00\x01"
         assert b[1] == "\x00\x02\x03"
         assert b.tostring() == "\x00\x01\x00\x00\x02\x03"
+        c = b.astype(b.dtype)
+        assert (b == c).all()
+        assert c.tostring() == "\x00\x01\x00\x00\x02\x03"
         raises(TypeError, a, 'sum')
         raises(TypeError, 'a+a')
         b = array(['abcdefg', 'ab', 'cd'])

File pypy/module/micronumpy/test/test_sorting.py

         assert (r == array([('a', 1), ('c', 3), ('b', 255), ('d', 258)],
                                  dtype=mydtype)).all()
 
-# tests from numpy/tests/test_regression.py
+# tests from numpy/core/tests/test_regression.py
     def test_sort_bigendian(self):
         skip('not implemented yet')
         from numpypy import array, dtype
         c.sort()
         assert max(abs(a-c)) < 1e-32
 
+    def test_string_argsort_with_zeros(self):
+        import numpy as np
+        import sys
+        x = np.fromstring("\x00\x02\x00\x01", dtype="|S2")
+        if '__pypy__' in sys.builtin_module_names:
+            exc = raises(NotImplementedError, "x.argsort(kind='m')")
+            assert 'non-numeric types' in exc.value.message
+            exc = raises(NotImplementedError, "x.argsort(kind='q')")
+            assert 'non-numeric types' in exc.value.message
+        else:
+            assert (x.argsort(kind='m') == np.array([1, 0])).all()
+            assert (x.argsort(kind='q') == np.array([1, 0])).all()
+
     def test_string_sort_with_zeros(self):
-        skip('not implemented yet')
-        from numpypy import fromstring
-        """Check sort for strings containing zeros."""
-        x = fromstring("\x00\x02\x00\x01", dtype="S2")
-        y = fromstring("\x00\x01\x00\x02", dtype="S2")
-        x.sort(kind='q')
-        assert (x == y).all()
+        import numpy as np
+        import sys
+        x = np.fromstring("\x00\x02\x00\x01", dtype="S2")
+        y = np.fromstring("\x00\x01\x00\x02", dtype="S2")
+        if '__pypy__' in sys.builtin_module_names:
+            exc = raises(NotImplementedError, "x.sort(kind='q')")
+            assert 'non-numeric types' in exc.value.message
+        else:
+            x.sort(kind='q')
+            assert (x == y).all()
 
     def test_string_mergesort(self):
         import numpypy as np

File pypy/module/micronumpy/types.py

         return bool(self.to_str(v))
 
     def build_and_convert(self, space, mydtype, box):
+        if isinstance(box, interp_boxes.W_StringBox):
+            return box
         assert isinstance(box, interp_boxes.W_GenericBox)
         if box.get_dtype(space).is_str_or_unicode():
             arg = box.get_dtype(space).itemtype.to_str(box)