test_numpy_array.py revision 14299
12068SN/Aimport pytest
22068SN/Afrom pybind11_tests import numpy_array as m
39829Sandreas.hansson@arm.com
49829Sandreas.hansson@arm.compytestmark = pytest.requires_numpy
59829Sandreas.hansson@arm.com
69829Sandreas.hansson@arm.comwith pytest.suppress(ImportError):
79829Sandreas.hansson@arm.com    import numpy as np
89829Sandreas.hansson@arm.com
99829Sandreas.hansson@arm.com
109829Sandreas.hansson@arm.comdef test_dtypes():
119829Sandreas.hansson@arm.com    # See issue #1328.
129829Sandreas.hansson@arm.com    # - Platform-dependent sizes.
139829Sandreas.hansson@arm.com    for size_check in m.get_platform_dtype_size_checks():
149829Sandreas.hansson@arm.com        print(size_check)
152188SN/A        assert size_check.size_cpp == size_check.size_numpy, size_check
162068SN/A    # - Concrete sizes.
172068SN/A    for check in m.get_concrete_dtype_checks():
182068SN/A        print(check)
192068SN/A        assert check.numpy == check.pybind11, check
202068SN/A        if check.numpy.num != check.pybind11.num:
212068SN/A            print("NOTE: typenum mismatch for {}: {} != {}".format(
222068SN/A                check, check.numpy.num, check.pybind11.num))
232068SN/A
242068SN/A
252068SN/A@pytest.fixture(scope='function')
262068SN/Adef arr():
272068SN/A    return np.array([[1, 2, 3], [4, 5, 6]], '=u2')
282068SN/A
292068SN/A
302068SN/Adef test_array_attributes():
312068SN/A    a = np.array(0, 'f8')
322068SN/A    assert m.ndim(a) == 0
332068SN/A    assert all(m.shape(a) == [])
342068SN/A    assert all(m.strides(a) == [])
352068SN/A    with pytest.raises(IndexError) as excinfo:
362068SN/A        m.shape(a, 0)
372068SN/A    assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
382068SN/A    with pytest.raises(IndexError) as excinfo:
392068SN/A        m.strides(a, 0)
402665Ssaidi@eecs.umich.edu    assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
412665Ssaidi@eecs.umich.edu    assert m.writeable(a)
422068SN/A    assert m.size(a) == 1
432649Ssaidi@eecs.umich.edu    assert m.itemsize(a) == 8
442649Ssaidi@eecs.umich.edu    assert m.nbytes(a) == 8
452649Ssaidi@eecs.umich.edu    assert m.owndata(a)
462649Ssaidi@eecs.umich.edu
472649Ssaidi@eecs.umich.edu    a = np.array([[1, 2, 3], [4, 5, 6]], 'u2').view()
482068SN/A    a.flags.writeable = False
492068SN/A    assert m.ndim(a) == 2
502068SN/A    assert all(m.shape(a) == [2, 3])
512068SN/A    assert m.shape(a, 0) == 2
522068SN/A    assert m.shape(a, 1) == 3
532068SN/A    assert all(m.strides(a) == [6, 2])
542068SN/A    assert m.strides(a, 0) == 6
552068SN/A    assert m.strides(a, 1) == 2
568588Sgblack@eecs.umich.edu    with pytest.raises(IndexError) as excinfo:
578588Sgblack@eecs.umich.edu        m.shape(a, 2)
588588Sgblack@eecs.umich.edu    assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
598588Sgblack@eecs.umich.edu    with pytest.raises(IndexError) as excinfo:
608588Sgblack@eecs.umich.edu        m.strides(a, 2)
618588Sgblack@eecs.umich.edu    assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
622068SN/A    assert not m.writeable(a)
632068SN/A    assert m.size(a) == 6
642068SN/A    assert m.itemsize(a) == 2
658588Sgblack@eecs.umich.edu    assert m.nbytes(a) == 12
668588Sgblack@eecs.umich.edu    assert not m.owndata(a)
672068SN/A
682068SN/A
698588Sgblack@eecs.umich.edu@pytest.mark.parametrize('args, ret', [([], 0), ([0], 0), ([1], 3), ([0, 1], 1), ([1, 2], 5)])
702075SN/Adef test_index_offset(arr, args, ret):
712068SN/A    assert m.index_at(arr, *args) == ret
722068SN/A    assert m.index_at_t(arr, *args) == ret
732068SN/A    assert m.offset_at(arr, *args) == ret * arr.dtype.itemsize
748588Sgblack@eecs.umich.edu    assert m.offset_at_t(arr, *args) == ret * arr.dtype.itemsize
758588Sgblack@eecs.umich.edu
768588Sgblack@eecs.umich.edu
778588Sgblack@eecs.umich.edudef test_dim_check_fail(arr):
788588Sgblack@eecs.umich.edu    for func in (m.index_at, m.index_at_t, m.offset_at, m.offset_at_t, m.data, m.data_t,
798588Sgblack@eecs.umich.edu                 m.mutate_data, m.mutate_data_t):
808588Sgblack@eecs.umich.edu        with pytest.raises(IndexError) as excinfo:
812068SN/A            func(arr, 1, 2, 3)
822068SN/A        assert str(excinfo.value) == 'too many indices for an array: 3 (ndim = 2)'
832068SN/A
848588Sgblack@eecs.umich.edu
852068SN/A@pytest.mark.parametrize('args, ret',
862069SN/A                         [([], [1, 2, 3, 4, 5, 6]),
872068SN/A                          ([1], [4, 5, 6]),
882068SN/A                          ([0, 1], [2, 3, 4, 5, 6]),
894027Sstever@eecs.umich.edu                          ([1, 2], [6])])
904027Sstever@eecs.umich.edudef test_data(arr, args, ret):
914027Sstever@eecs.umich.edu    from sys import byteorder
926076Sgblack@eecs.umich.edu    assert all(m.data_t(arr, *args) == ret)
938588Sgblack@eecs.umich.edu    assert all(m.data(arr, *args)[(0 if byteorder == 'little' else 1)::2] == ret)
942068SN/A    assert all(m.data(arr, *args)[(1 if byteorder == 'little' else 0)::2] == 0)
952069SN/A
962068SN/A
972068SN/A@pytest.mark.parametrize('dim', [0, 1, 3])
982068SN/Adef test_at_fail(arr, dim):
992068SN/A    for func in m.at_t, m.mutate_at_t:
1002068SN/A        with pytest.raises(IndexError) as excinfo:
1012068SN/A            func(arr, *([0] * dim))
1022068SN/A        assert str(excinfo.value) == 'index dimension mismatch: {} (ndim = 2)'.format(dim)
1032068SN/A
1044027Sstever@eecs.umich.edu
1054027Sstever@eecs.umich.edudef test_at(arr):
1064027Sstever@eecs.umich.edu    assert m.at_t(arr, 0, 2) == 3
1074027Sstever@eecs.umich.edu    assert m.at_t(arr, 1, 0) == 4
1084027Sstever@eecs.umich.edu
1094027Sstever@eecs.umich.edu    assert all(m.mutate_at_t(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6])
1106076Sgblack@eecs.umich.edu    assert all(m.mutate_at_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
1112068SN/A
1122068SN/A
1132068SN/Adef test_mutate_readonly(arr):
1142068SN/A    arr.flags.writeable = False
1157799Sgblack@eecs.umich.edu    for func, args in (m.mutate_data, ()), (m.mutate_data_t, ()), (m.mutate_at_t, (0, 0)):
1162068SN/A        with pytest.raises(ValueError) as excinfo:
1178588Sgblack@eecs.umich.edu            func(arr, *args)
1182068SN/A        assert str(excinfo.value) == 'array is not writeable'
1198588Sgblack@eecs.umich.edu
1202068SN/A
1212068SN/Adef test_mutate_data(arr):
1228588Sgblack@eecs.umich.edu    assert all(m.mutate_data(arr).ravel() == [2, 4, 6, 8, 10, 12])
12310474Sandreas.hansson@arm.com    assert all(m.mutate_data(arr).ravel() == [4, 8, 12, 16, 20, 24])
1248588Sgblack@eecs.umich.edu    assert all(m.mutate_data(arr, 1).ravel() == [4, 8, 12, 32, 40, 48])
1252068SN/A    assert all(m.mutate_data(arr, 0, 1).ravel() == [4, 16, 24, 64, 80, 96])
1268588Sgblack@eecs.umich.edu    assert all(m.mutate_data(arr, 1, 2).ravel() == [4, 16, 24, 64, 80, 192])
1278588Sgblack@eecs.umich.edu
1282068SN/A    assert all(m.mutate_data_t(arr).ravel() == [5, 17, 25, 65, 81, 193])
1292068SN/A    assert all(m.mutate_data_t(arr).ravel() == [6, 18, 26, 66, 82, 194])
1302068SN/A    assert all(m.mutate_data_t(arr, 1).ravel() == [6, 18, 26, 67, 83, 195])
1312068SN/A    assert all(m.mutate_data_t(arr, 0, 1).ravel() == [6, 19, 27, 68, 84, 196])
1322068SN/A    assert all(m.mutate_data_t(arr, 1, 2).ravel() == [6, 19, 27, 68, 84, 197])
1332068SN/A
1342068SN/A
13510474Sandreas.hansson@arm.comdef test_bounds_check(arr):
1362068SN/A    for func in (m.index_at, m.index_at_t, m.data, m.data_t,
1372068SN/A                 m.mutate_data, m.mutate_data_t, m.at_t, m.mutate_at_t):
1382068SN/A        with pytest.raises(IndexError) as excinfo:
1392068SN/A            func(arr, 2, 0)
1402068SN/A        assert str(excinfo.value) == 'index 2 is out of bounds for axis 0 with size 2'
1418588Sgblack@eecs.umich.edu        with pytest.raises(IndexError) as excinfo:
1422068SN/A            func(arr, 0, 4)
1438588Sgblack@eecs.umich.edu        assert str(excinfo.value) == 'index 4 is out of bounds for axis 1 with size 3'
1442068SN/A
1452068SN/A
1462068SN/Adef test_make_c_f_array():
1472068SN/A    assert m.make_c_array().flags.c_contiguous
1488588Sgblack@eecs.umich.edu    assert not m.make_c_array().flags.f_contiguous
14910474Sandreas.hansson@arm.com    assert m.make_f_array().flags.f_contiguous
1508588Sgblack@eecs.umich.edu    assert not m.make_f_array().flags.c_contiguous
1512068SN/A
1528588Sgblack@eecs.umich.edu
1538588Sgblack@eecs.umich.edudef test_make_empty_shaped_array():
1542068SN/A    m.make_empty_shaped_array()
1552068SN/A
1562068SN/A    # empty shape means numpy scalar, PEP 3118
1572068SN/A    assert m.scalar_int().ndim == 0
1582068SN/A    assert m.scalar_int().shape == ()
1592068SN/A    assert m.scalar_int() == 42
1602068SN/A
1612068SN/A
1622068SN/Adef test_wrap():
16310474Sandreas.hansson@arm.com    def assert_references(a, b, base=None):
1642068SN/A        from distutils.version import LooseVersion
1652068SN/A        if base is None:
1662068SN/A            base = a
1672068SN/A        assert a is not b
1682068SN/A        assert a.__array_interface__['data'][0] == b.__array_interface__['data'][0]
1692068SN/A        assert a.shape == b.shape
1708588Sgblack@eecs.umich.edu        assert a.strides == b.strides
1718588Sgblack@eecs.umich.edu        assert a.flags.c_contiguous == b.flags.c_contiguous
1728588Sgblack@eecs.umich.edu        assert a.flags.f_contiguous == b.flags.f_contiguous
1738588Sgblack@eecs.umich.edu        assert a.flags.writeable == b.flags.writeable
1742068SN/A        assert a.flags.aligned == b.flags.aligned
1752068SN/A        if LooseVersion(np.__version__) >= LooseVersion("1.14.0"):
1762068SN/A            assert a.flags.writebackifcopy == b.flags.writebackifcopy
1772068SN/A        else:
1782068SN/A            assert a.flags.updateifcopy == b.flags.updateifcopy
1792068SN/A        assert np.all(a == b)
1808588Sgblack@eecs.umich.edu        assert not b.flags.owndata
1812068SN/A        assert b.base is base
1822068SN/A        if a.flags.writeable and a.ndim == 2:
1832068SN/A            a[0, 0] = 1234
1842068SN/A            assert b[0, 0] == 1234
1852068SN/A
1862068SN/A    a1 = np.array([1, 2], dtype=np.int16)
1872068SN/A    assert a1.flags.owndata and a1.base is None
1887799Sgblack@eecs.umich.edu    a2 = m.wrap(a1)
1892068SN/A    assert_references(a1, a2)
1902068SN/A
1912068SN/A    a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order='F')
1922068SN/A    assert a1.flags.owndata and a1.base is None
1932068SN/A    a2 = m.wrap(a1)
1942068SN/A    assert_references(a1, a2)
1952068SN/A
1962068SN/A    a1 = np.array([[1, 2], [3, 4]], dtype=np.float32, order='C')
1972068SN/A    a1.flags.writeable = False
1982068SN/A    a2 = m.wrap(a1)
1992068SN/A    assert_references(a1, a2)
2002068SN/A
2012068SN/A    a1 = np.random.random((4, 4, 4))
2028588Sgblack@eecs.umich.edu    a2 = m.wrap(a1)
2038588Sgblack@eecs.umich.edu    assert_references(a1, a2)
2048588Sgblack@eecs.umich.edu
2058588Sgblack@eecs.umich.edu    a1t = a1.transpose()
2062068SN/A    a2 = m.wrap(a1t)
2072068SN/A    assert_references(a1t, a2, a1)
2082068SN/A
2092068SN/A    a1d = a1.diagonal()
2102068SN/A    a2 = m.wrap(a1d)
2112068SN/A    assert_references(a1d, a2, a1)
2122068SN/A
2132068SN/A    a1m = a1[::-1, ::-1, ::-1]
2142068SN/A    a2 = m.wrap(a1m)
2152068SN/A    assert_references(a1m, a2, a1)
2162068SN/A
2178738Sgblack@eecs.umich.edu
2188738Sgblack@eecs.umich.edudef test_numpy_view(capture):
2192068SN/A    with capture:
2202068SN/A        ac = m.ArrayClass()
2212068SN/A        ac_view_1 = ac.numpy_view()
2222068SN/A        ac_view_2 = ac.numpy_view()
2232068SN/A        assert np.all(ac_view_1 == np.array([1, 2], dtype=np.int32))
2242068SN/A        del ac
2252068SN/A        pytest.gc_collect()
2262068SN/A    assert capture == """
2272068SN/A        ArrayClass()
2282068SN/A        ArrayClass::numpy_view()
2298588Sgblack@eecs.umich.edu        ArrayClass::numpy_view()
2308588Sgblack@eecs.umich.edu    """
2312068SN/A    ac_view_1[0] = 4
2322068SN/A    ac_view_1[1] = 3
2332068SN/A    assert ac_view_2[0] == 4
2342068SN/A    assert ac_view_2[1] == 3
2352068SN/A    with capture:
2362068SN/A        del ac_view_1
2372068SN/A        del ac_view_2
2382068SN/A        pytest.gc_collect()
2392068SN/A        pytest.gc_collect()
2402068SN/A    assert capture == """
2412068SN/A        ~ArrayClass()
2422068SN/A    """
2432068SN/A
2442068SN/A
2452068SN/A@pytest.unsupported_on_pypy
2462068SN/Adef test_cast_numpy_int64_to_uint64():
2472068SN/A    m.function_taking_uint64(123)
2482068SN/A    m.function_taking_uint64(np.uint64(123))
2492068SN/A
2508588Sgblack@eecs.umich.edu
2518588Sgblack@eecs.umich.edudef test_isinstance():
2528588Sgblack@eecs.umich.edu    assert m.isinstance_untyped(np.array([1, 2, 3]), "not an array")
2538588Sgblack@eecs.umich.edu    assert m.isinstance_typed(np.array([1.0, 2.0, 3.0]))
2542068SN/A
2552068SN/A
2562068SN/Adef test_constructors():
2572068SN/A    defaults = m.default_constructors()
2582068SN/A    for a in defaults.values():
2592068SN/A        assert a.size == 0
2602068SN/A    assert defaults["array"].dtype == np.array([]).dtype
2612068SN/A    assert defaults["array_t<int32>"].dtype == np.int32
2622068SN/A    assert defaults["array_t<double>"].dtype == np.float64
2632068SN/A
2642068SN/A    results = m.converting_constructors([1, 2, 3])
2652068SN/A    for a in results.values():
2662068SN/A        np.testing.assert_array_equal(a, [1, 2, 3])
2672068SN/A    assert results["array"].dtype == np.int_
2682068SN/A    assert results["array_t<int32>"].dtype == np.int32
2698588Sgblack@eecs.umich.edu    assert results["array_t<double>"].dtype == np.float64
2702068SN/A
2712068SN/A
2722068SN/Adef test_overload_resolution(msg):
2738588Sgblack@eecs.umich.edu    # Exact overload matches:
2742068SN/A    assert m.overloaded(np.array([1], dtype='float64')) == 'double'
2752068SN/A    assert m.overloaded(np.array([1], dtype='float32')) == 'float'
2762068SN/A    assert m.overloaded(np.array([1], dtype='ushort')) == 'unsigned short'
2778588Sgblack@eecs.umich.edu    assert m.overloaded(np.array([1], dtype='intc')) == 'int'
2782068SN/A    assert m.overloaded(np.array([1], dtype='longlong')) == 'long long'
2792068SN/A    assert m.overloaded(np.array([1], dtype='complex')) == 'double complex'
2802068SN/A    assert m.overloaded(np.array([1], dtype='csingle')) == 'float complex'
2812068SN/A
2822068SN/A    # No exact match, should call first convertible version:
2832068SN/A    assert m.overloaded(np.array([1], dtype='uint8')) == 'double'
2842068SN/A
2852068SN/A    with pytest.raises(TypeError) as excinfo:
2862068SN/A        m.overloaded("not an array")
2872068SN/A    assert msg(excinfo.value) == """
2882068SN/A        overloaded(): incompatible function arguments. The following argument types are supported:
2892068SN/A            1. (arg0: numpy.ndarray[float64]) -> str
2902068SN/A            2. (arg0: numpy.ndarray[float32]) -> str
2912068SN/A            3. (arg0: numpy.ndarray[int32]) -> str
2922068SN/A            4. (arg0: numpy.ndarray[uint16]) -> str
2932068SN/A            5. (arg0: numpy.ndarray[int64]) -> str
2942068SN/A            6. (arg0: numpy.ndarray[complex128]) -> str
2952068SN/A            7. (arg0: numpy.ndarray[complex64]) -> str
2962068SN/A
2972068SN/A        Invoked with: 'not an array'
2987799Sgblack@eecs.umich.edu    """
2998588Sgblack@eecs.umich.edu
3002068SN/A    assert m.overloaded2(np.array([1], dtype='float64')) == 'double'
3012068SN/A    assert m.overloaded2(np.array([1], dtype='float32')) == 'float'
3022068SN/A    assert m.overloaded2(np.array([1], dtype='complex64')) == 'float complex'
3032068SN/A    assert m.overloaded2(np.array([1], dtype='complex128')) == 'double complex'
3042068SN/A    assert m.overloaded2(np.array([1], dtype='float32')) == 'float'
3052068SN/A
3062068SN/A    assert m.overloaded3(np.array([1], dtype='float64')) == 'double'
3072068SN/A    assert m.overloaded3(np.array([1], dtype='intc')) == 'int'
3088588Sgblack@eecs.umich.edu    expected_exc = """
3098588Sgblack@eecs.umich.edu        overloaded3(): incompatible function arguments. The following argument types are supported:
3102068SN/A            1. (arg0: numpy.ndarray[int32]) -> str
3112068SN/A            2. (arg0: numpy.ndarray[float64]) -> str
3122068SN/A
3132068SN/A        Invoked with: """
3142068SN/A
3152068SN/A    with pytest.raises(TypeError) as excinfo:
31610474Sandreas.hansson@arm.com        m.overloaded3(np.array([1], dtype='uintc'))
3178588Sgblack@eecs.umich.edu    assert msg(excinfo.value) == expected_exc + repr(np.array([1], dtype='uint32'))
3182068SN/A    with pytest.raises(TypeError) as excinfo:
3192068SN/A        m.overloaded3(np.array([1], dtype='float32'))
3202068SN/A    assert msg(excinfo.value) == expected_exc + repr(np.array([1.], dtype='float32'))
3212068SN/A    with pytest.raises(TypeError) as excinfo:
3222068SN/A        m.overloaded3(np.array([1], dtype='complex'))
3232068SN/A    assert msg(excinfo.value) == expected_exc + repr(np.array([1. + 0.j]))
3242068SN/A
3252068SN/A    # Exact matches:
3262068SN/A    assert m.overloaded4(np.array([1], dtype='double')) == 'double'
32710474Sandreas.hansson@arm.com    assert m.overloaded4(np.array([1], dtype='longlong')) == 'long long'
3282068SN/A    # Non-exact matches requiring conversion.  Since float to integer isn't a
3292068SN/A    # save conversion, it should go to the double overload, but short can go to
3302068SN/A    # either (and so should end up on the first-registered, the long long).
3312068SN/A    assert m.overloaded4(np.array([1], dtype='float32')) == 'double'
3322068SN/A    assert m.overloaded4(np.array([1], dtype='short')) == 'long long'
3338588Sgblack@eecs.umich.edu
3348588Sgblack@eecs.umich.edu    assert m.overloaded5(np.array([1], dtype='double')) == 'double'
3356804Ssroy@cse.usf.edu    assert m.overloaded5(np.array([1], dtype='uintc')) == 'unsigned int'
3366804Ssroy@cse.usf.edu    assert m.overloaded5(np.array([1], dtype='float32')) == 'unsigned int'
3376804Ssroy@cse.usf.edu
3386804Ssroy@cse.usf.edu
3396804Ssroy@cse.usf.edudef test_greedy_string_overload():
3406804Ssroy@cse.usf.edu    """Tests fix for #685 - ndarray shouldn't go to std::string overload"""
3416804Ssroy@cse.usf.edu
3426804Ssroy@cse.usf.edu    assert m.issue685("abc") == "string"
3436804Ssroy@cse.usf.edu    assert m.issue685(np.array([97, 98, 99], dtype='b')) == "array"
3446804Ssroy@cse.usf.edu    assert m.issue685(123) == "other"
3456804Ssroy@cse.usf.edu
3466804Ssroy@cse.usf.edu
3476804Ssroy@cse.usf.edudef test_array_unchecked_fixed_dims(msg):
3486804Ssroy@cse.usf.edu    z1 = np.array([[1, 2], [3, 4]], dtype='float64')
3496804Ssroy@cse.usf.edu    m.proxy_add2(z1, 10)
3508588Sgblack@eecs.umich.edu    assert np.all(z1 == [[11, 12], [13, 14]])
3518588Sgblack@eecs.umich.edu
35211320Ssteve.reinhardt@amd.com    with pytest.raises(ValueError) as excinfo:
3536804Ssroy@cse.usf.edu        m.proxy_add2(np.array([1., 2, 3]), 5.0)
3546804Ssroy@cse.usf.edu    assert msg(excinfo.value) == "array has incorrect number of dimensions: 1; expected 2"
3556804Ssroy@cse.usf.edu
3566804Ssroy@cse.usf.edu    expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype='int')
3576804Ssroy@cse.usf.edu    assert np.all(m.proxy_init3(3.0) == expect_c)
3586804Ssroy@cse.usf.edu    expect_f = np.transpose(expect_c)
3596804Ssroy@cse.usf.edu    assert np.all(m.proxy_init3F(3.0) == expect_f)
3602068SN/A
3612068SN/A    assert m.proxy_squared_L2_norm(np.array(range(6))) == 55
3622068SN/A    assert m.proxy_squared_L2_norm(np.array(range(6), dtype="float64")) == 55
3632068SN/A
3642068SN/A    assert m.proxy_auxiliaries2(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
3652068SN/A    assert m.proxy_auxiliaries2(z1) == m.array_auxiliaries2(z1)
3662068SN/A
3672068SN/A
3682068SN/Adef test_array_unchecked_dyn_dims(msg):
3692068SN/A    z1 = np.array([[1, 2], [3, 4]], dtype='float64')
3702068SN/A    m.proxy_add2_dyn(z1, 10)
3712068SN/A    assert np.all(z1 == [[11, 12], [13, 14]])
3722068SN/A
3732068SN/A    expect_c = np.ndarray(shape=(3, 3, 3), buffer=np.array(range(3, 30)), dtype='int')
3742068SN/A    assert np.all(m.proxy_init3_dyn(3.0) == expect_c)
3752068SN/A
3762068SN/A    assert m.proxy_auxiliaries2_dyn(z1) == [11, 11, True, 2, 8, 2, 2, 4, 32]
3772068SN/A    assert m.proxy_auxiliaries2_dyn(z1) == m.array_auxiliaries2(z1)
3782068SN/A
3792068SN/A
3802068SN/Adef test_array_failure():
38111320Ssteve.reinhardt@amd.com    with pytest.raises(ValueError) as excinfo:
38211320Ssteve.reinhardt@amd.com        m.array_fail_test()
3836804Ssroy@cse.usf.edu    assert str(excinfo.value) == 'cannot create a pybind11::array from a nullptr'
3842068SN/A
3852068SN/A    with pytest.raises(ValueError) as excinfo:
3862068SN/A        m.array_t_fail_test()
3872068SN/A    assert str(excinfo.value) == 'cannot create a pybind11::array_t from a nullptr'
3886804Ssroy@cse.usf.edu
38911320Ssteve.reinhardt@amd.com    with pytest.raises(ValueError) as excinfo:
3908588Sgblack@eecs.umich.edu        m.array_fail_test_negative_size()
3918588Sgblack@eecs.umich.edu    assert str(excinfo.value) == 'negative dimensions are not allowed'
3928588Sgblack@eecs.umich.edu
3938588Sgblack@eecs.umich.edu
3946804Ssroy@cse.usf.edudef test_initializer_list():
3956804Ssroy@cse.usf.edu    assert m.array_initializer_list1().shape == (1,)
3966804Ssroy@cse.usf.edu    assert m.array_initializer_list2().shape == (1, 2)
3978588Sgblack@eecs.umich.edu    assert m.array_initializer_list3().shape == (1, 2, 3)
3986804Ssroy@cse.usf.edu    assert m.array_initializer_list4().shape == (1, 2, 3, 4)
3996804Ssroy@cse.usf.edu
4006804Ssroy@cse.usf.edu
4018588Sgblack@eecs.umich.edudef test_array_resize(msg):
4028588Sgblack@eecs.umich.edu    a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='float64')
4038588Sgblack@eecs.umich.edu    m.array_reshape2(a)
4048588Sgblack@eecs.umich.edu    assert(a.size == 9)
4056804Ssroy@cse.usf.edu    assert(np.all(a == [[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
4066804Ssroy@cse.usf.edu
4076804Ssroy@cse.usf.edu    # total size change should succced with refcheck off
4088588Sgblack@eecs.umich.edu    m.array_resize3(a, 4, False)
4096804Ssroy@cse.usf.edu    assert(a.size == 64)
4106804Ssroy@cse.usf.edu    # ... and fail with refcheck on
4116804Ssroy@cse.usf.edu    try:
4126804Ssroy@cse.usf.edu        m.array_resize3(a, 3, True)
4136804Ssroy@cse.usf.edu    except ValueError as e:
4146804Ssroy@cse.usf.edu        assert(str(e).startswith("cannot resize an array"))
4156804Ssroy@cse.usf.edu    # transposed array doesn't own data
4168588Sgblack@eecs.umich.edu    b = a.transpose()
4178588Sgblack@eecs.umich.edu    try:
41811320Ssteve.reinhardt@amd.com        m.array_resize3(b, 3, False)
4198588Sgblack@eecs.umich.edu    except ValueError as e:
4208588Sgblack@eecs.umich.edu        assert(str(e).startswith("cannot resize this array: it does not own its data"))
4216804Ssroy@cse.usf.edu    # ... but reshape should be fine
4226804Ssroy@cse.usf.edu    m.array_reshape2(b)
4236804Ssroy@cse.usf.edu    assert(b.shape == (8, 8))
4246804Ssroy@cse.usf.edu
4256804Ssroy@cse.usf.edu
4266804Ssroy@cse.usf.edu@pytest.unsupported_on_pypy
4276804Ssroy@cse.usf.edudef test_array_create_and_resize(msg):
4286804Ssroy@cse.usf.edu    a = m.create_and_resize(2)
4296804Ssroy@cse.usf.edu    assert(a.size == 4)
4306804Ssroy@cse.usf.edu    assert(np.all(a == 42.))
4316804Ssroy@cse.usf.edu
4328588Sgblack@eecs.umich.edu
4338588Sgblack@eecs.umich.edu@pytest.unsupported_on_py2
43411320Ssteve.reinhardt@amd.comdef test_index_using_ellipsis():
4358588Sgblack@eecs.umich.edu    a = m.index_using_ellipsis(np.zeros((5, 6, 7)))
4368588Sgblack@eecs.umich.edu    assert a.shape == (6,)
4376804Ssroy@cse.usf.edu
4386804Ssroy@cse.usf.edu
4396804Ssroy@cse.usf.edu@pytest.unsupported_on_pypy
4406804Ssroy@cse.usf.edudef test_dtype_refcount_leak():
4416804Ssroy@cse.usf.edu    from sys import getrefcount
4426804Ssroy@cse.usf.edu    dtype = np.dtype(np.float_)
4436804Ssroy@cse.usf.edu    a = np.array([1], dtype=dtype)
4446804Ssroy@cse.usf.edu    before = getrefcount(dtype)
4456804Ssroy@cse.usf.edu    m.ndim(a)
4466804Ssroy@cse.usf.edu    after = getrefcount(dtype)
4476804Ssroy@cse.usf.edu    assert after == before
4488588Sgblack@eecs.umich.edu