예제 #1
0
def test_array_attributes():
    from pybind11_tests.array import (
        ndim, shape, strides, writeable, size, itemsize, nbytes, owndata
    )

    a = np.array(0, 'f8')
    assert ndim(a) == 0
    assert all(shape(a) == [])
    assert all(strides(a) == [])
    with pytest.raises(IndexError) as excinfo:
        shape(a, 0)
    assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
    with pytest.raises(IndexError) as excinfo:
        strides(a, 0)
    assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
    assert writeable(a)
    assert size(a) == 1
    assert itemsize(a) == 8
    assert nbytes(a) == 8
    assert owndata(a)

    a = np.array([[1, 2, 3], [4, 5, 6]], 'u2').view()
    a.flags.writeable = False
    assert ndim(a) == 2
    assert all(shape(a) == [2, 3])
    assert shape(a, 0) == 2
    assert shape(a, 1) == 3
    assert all(strides(a) == [6, 2])
    assert strides(a, 0) == 6
    assert strides(a, 1) == 2
    with pytest.raises(IndexError) as excinfo:
        shape(a, 2)
    assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
    with pytest.raises(IndexError) as excinfo:
        strides(a, 2)
    assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
    assert not writeable(a)
    assert size(a) == 6
    assert itemsize(a) == 2
    assert nbytes(a) == 12
    assert not owndata(a)
예제 #2
0
def test_array_attributes():
    from pybind11_tests.array import (
        ndim, shape, strides, writeable, size, itemsize, nbytes, owndata
    )

    a = np.array(0, 'f8')
    assert ndim(a) == 0
    assert all(shape(a) == [])
    assert all(strides(a) == [])
    with pytest.raises(IndexError) as excinfo:
        shape(a, 0)
    assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
    with pytest.raises(IndexError) as excinfo:
        strides(a, 0)
    assert str(excinfo.value) == 'invalid axis: 0 (ndim = 0)'
    assert writeable(a)
    assert size(a) == 1
    assert itemsize(a) == 8
    assert nbytes(a) == 8
    assert owndata(a)

    a = np.array([[1, 2, 3], [4, 5, 6]], 'u2').view()
    a.flags.writeable = False
    assert ndim(a) == 2
    assert all(shape(a) == [2, 3])
    assert shape(a, 0) == 2
    assert shape(a, 1) == 3
    assert all(strides(a) == [6, 2])
    assert strides(a, 0) == 6
    assert strides(a, 1) == 2
    with pytest.raises(IndexError) as excinfo:
        shape(a, 2)
    assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
    with pytest.raises(IndexError) as excinfo:
        strides(a, 2)
    assert str(excinfo.value) == 'invalid axis: 2 (ndim = 2)'
    assert not writeable(a)
    assert size(a) == 6
    assert itemsize(a) == 2
    assert nbytes(a) == 12
    assert not owndata(a)
예제 #3
0
def test_array_attributes():
    from pybind11_tests.array import (ndim, shape, strides, writeable, size,
                                      itemsize, nbytes, owndata)

    a = np.array(0, 'f8')
    if ndim(a) != 0:
        raise AssertionError
    if not all(shape(a) == []):
        raise AssertionError
    if not all(strides(a) == []):
        raise AssertionError
    with pytest.raises(IndexError) as excinfo:
        shape(a, 0)
    if str(excinfo.value) != 'invalid axis: 0 (ndim = 0)':
        raise AssertionError
    with pytest.raises(IndexError) as excinfo:
        strides(a, 0)
    if str(excinfo.value) != 'invalid axis: 0 (ndim = 0)':
        raise AssertionError
    if not writeable(a):
        raise AssertionError
    if size(a) != 1:
        raise AssertionError
    if itemsize(a) != 8:
        raise AssertionError
    if nbytes(a) != 8:
        raise AssertionError
    if not owndata(a):
        raise AssertionError

    a = np.array([[1, 2, 3], [4, 5, 6]], 'u2').view()
    a.flags.writeable = False
    if ndim(a) != 2:
        raise AssertionError
    if not all(shape(a) == [2, 3]):
        raise AssertionError
    if shape(a, 0) != 2:
        raise AssertionError
    if shape(a, 1) != 3:
        raise AssertionError
    if not all(strides(a) == [6, 2]):
        raise AssertionError
    if strides(a, 0) != 6:
        raise AssertionError
    if strides(a, 1) != 2:
        raise AssertionError
    with pytest.raises(IndexError) as excinfo:
        shape(a, 2)
    if str(excinfo.value) != 'invalid axis: 2 (ndim = 2)':
        raise AssertionError
    with pytest.raises(IndexError) as excinfo:
        strides(a, 2)
    if str(excinfo.value) != 'invalid axis: 2 (ndim = 2)':
        raise AssertionError
    if writeable(a):
        raise AssertionError
    if size(a) != 6:
        raise AssertionError
    if itemsize(a) != 2:
        raise AssertionError
    if nbytes(a) != 12:
        raise AssertionError
    if owndata(a):
        raise AssertionError