diff options
author | Allan Haldane <allan.haldane@gmail.com> | 2015-07-28 09:37:26 -0400 |
---|---|---|
committer | Allan Haldane <allan.haldane@gmail.com> | 2017-09-07 16:47:39 -0400 |
commit | 6752d5fb21c7aad538b3ab2c638dd52e38ed6f2a (patch) | |
tree | 0cb07b4378539729567082146409610d7e257e3d | |
parent | 50f4d0d810efbc8a3e3a4bdf8908faa9df4aa710 (diff) | |
download | python-numpy-6752d5fb21c7aad538b3ab2c638dd52e38ed6f2a.tar.gz python-numpy-6752d5fb21c7aad538b3ab2c638dd52e38ed6f2a.tar.bz2 python-numpy-6752d5fb21c7aad538b3ab2c638dd52e38ed6f2a.zip |
TST: Add new unit tests for structure assignment
-rw-r--r-- | numpy/core/tests/test_dtype.py | 9 | ||||
-rw-r--r-- | numpy/core/tests/test_indexing.py | 6 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 42 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 2 |
4 files changed, 58 insertions, 1 deletions
diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 87c6f8b98..9cefb2ad1 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -104,6 +104,15 @@ class TestBuiltin(object): 'formats':['i1', 'f4'], 'offsets':[0, 2]}, align=True) + def test_field_order_equality(self): + x = np.dtype({'names': ['A', 'B'], + 'formats': ['i4', 'f4'], + 'offsets': [0, 4]}) + y = np.dtype({'names': ['B', 'A'], + 'formats': ['f4', 'i4'], + 'offsets': [4, 0]}) + assert_equal(x == y, False) + class TestRecord(object): def test_equivalent_record(self): """Test whether equivalent record dtypes hash the same.""" diff --git a/numpy/core/tests/test_indexing.py b/numpy/core/tests/test_indexing.py index 43965d994..4c3bac529 100644 --- a/numpy/core/tests/test_indexing.py +++ b/numpy/core/tests/test_indexing.py @@ -106,6 +106,12 @@ class TestIndexing(object): a = np.array(0) assert_(isinstance(a[()], np.int_)) + def test_void_scalar_empty_tuple(self): + s = np.zeros((), dtype='V4') + assert_equal(s[()].dtype, s.dtype) + assert_equal(s[()], s) + assert_equal(type(s[...]), np.ndarray) + def test_same_kind_index_casting(self): # Indexes should be cast with same-kind and not safe, even if that # is somewhat unsafe. So test various different code paths. diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index a3d5fd2b9..7fdab7641 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1093,6 +1093,48 @@ class TestStructured(object): b = a[0] assert_(b.base is a) + def test_assignment(self): + def testassign(arr, v): + c = arr.copy() + c[0] = v # assign using setitem + c[1:] = v # assign using "dtype_transfer" code paths + return c + + dt = np.dtype([('foo', 'i8'), ('bar', 'i8')]) + arr = np.ones(2, dt) + v1 = np.array([(2,3)], dtype=[('foo', 'i8'), ('bar', 'i8')]) + v2 = np.array([(2,3)], dtype=[('bar', 'i8'), ('foo', 'i8')]) + v3 = np.array([(2,3)], dtype=[('bar', 'i8'), ('baz', 'i8')]) + v4 = np.array([(2,)], dtype=[('bar', 'i8')]) + v5 = np.array([(2,3)], dtype=[('foo', 'f8'), ('bar', 'f8')]) + w = arr.view({'names': ['bar'], 'formats': ['i8'], 'offsets': [8]}) + + ans = np.array([(2,3),(2,3)], dtype=dt) + assert_equal(testassign(arr, v1), ans) + assert_equal(testassign(arr, v2), ans) + assert_equal(testassign(arr, v3), ans) + assert_raises(ValueError, lambda: testassign(arr, v4)) + assert_equal(testassign(arr, v5), ans) + w[:] = 4 + assert_equal(arr, np.array([(1,4),(1,4)], dtype=dt)) + + # test field-reordering, assignment by position, and self-assignment + a = np.array([(1,2,3)], + dtype=[('foo', 'i8'), ('bar', 'i8'), ('baz', 'f4')]) + a[['foo', 'bar']] = a[['bar', 'foo']] + assert_equal(a[0].item(), (2,1,3)) + + # test that this works even for 'simple_unaligned' structs + # (ie, that PyArray_EquivTypes cares about field order too) + a = np.array([(1,2)], dtype=[('a', 'i4'), ('b', 'i4')]) + a[['a', 'b']] = a[['b', 'a']] + assert_equal(a[0].item(), (2,1)) + + def test_structuredscalar_indexing(self): + # test gh-7262 + x = np.empty(shape=1, dtype="(2)3S,(2)3U") + assert_equal(x[["f0","f1"]][0], x[0][["f0","f1"]]) + assert_equal(x[0], x[0][()]) class TestBool(object): def test_test_interning(self): diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index c06a42de3..fa9f97233 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1765,7 +1765,7 @@ class TestFillingValues(object): fill_val = np.array((-999, -12345678.9, "???"), dtype=[("A", int), ("B", float), ("C", "|S3")]) fval = _check_fill_value(fill_val, ndtype) - self.assertTrue(isinstance(fval, ndarray)) + assert_(isinstance(fval, ndarray)) assert_equal(fval.item(), [-999, -12345678.9, b"???"]) #.....Using an object-array shouldn't matter either |