Пример #1
0
def test_recarray():
    from pybind11_tests import (create_rec_simple, create_rec_packed,
                                create_rec_nested, print_rec_simple,
                                print_rec_packed, print_rec_nested,
                                create_rec_partial, create_rec_partial_nested)

    elements = [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)]

    for func, dtype in [(create_rec_simple, simple_dtype),
                        (create_rec_packed, packed_dtype)]:
        arr = func(0)
        assert arr.dtype == dtype
        assert_equal(arr, [], simple_dtype)
        assert_equal(arr, [], packed_dtype)

        arr = func(3)
        assert arr.dtype == dtype
        assert_equal(arr, elements, simple_dtype)
        assert_equal(arr, elements, packed_dtype)

        if dtype == simple_dtype:
            assert print_rec_simple(arr) == ["s:0,0,0", "s:1,1,1.5", "s:0,2,3"]
        else:
            assert print_rec_packed(arr) == ["p:0,0,0", "p:1,1,1.5", "p:0,2,3"]

    nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)])

    arr = create_rec_nested(0)
    assert arr.dtype == nested_dtype
    assert_equal(arr, [], nested_dtype)

    arr = create_rec_nested(3)
    assert arr.dtype == nested_dtype
    assert_equal(arr, [((False, 0, 0.0), (True, 1, 1.5)),
                       ((True, 1, 1.5), (False, 2, 3.0)),
                       ((False, 2, 3.0), (True, 3, 4.5))], nested_dtype)
    assert print_rec_nested(arr) == [
        "n:a=s:0,0,0;b=p:1,1,1.5", "n:a=s:1,1,1.5;b=p:0,2,3",
        "n:a=s:0,2,3;b=p:1,3,4.5"
    ]

    arr = create_rec_partial(3)
    assert str(
        arr.dtype
    ) == "{'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':24}"
    partial_dtype = arr.dtype
    assert '' not in arr.dtype.fields
    assert partial_dtype.itemsize > simple_dtype.itemsize
    assert_equal(arr, elements, simple_dtype)
    assert_equal(arr, elements, packed_dtype)

    arr = create_rec_partial_nested(3)
    assert str(
        arr.dtype
    ) == "{'names':['a'], 'formats':[{'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':24}], 'offsets':[8], 'itemsize':40}"
    assert '' not in arr.dtype.fields
    assert '' not in arr.dtype.fields['a'][0].fields
    assert arr.dtype.itemsize > partial_dtype.itemsize
    np.testing.assert_equal(arr['a'], create_rec_partial(3))
Пример #2
0
def test_recarray(simple_dtype, packed_dtype):
    from pybind11_tests import (create_rec_simple, create_rec_packed,
                                create_rec_nested, print_rec_simple,
                                print_rec_packed, print_rec_nested,
                                create_rec_partial, create_rec_partial_nested)

    elements = [(False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5),
                (False, 2, 3.0, -5.0)]

    for func, dtype in [(create_rec_simple, simple_dtype),
                        (create_rec_packed, packed_dtype)]:
        arr = func(0)
        assert arr.dtype == dtype
        assert_equal(arr, [], simple_dtype)
        assert_equal(arr, [], packed_dtype)

        arr = func(3)
        assert arr.dtype == dtype
        assert_equal(arr, elements, simple_dtype)
        assert_equal(arr, elements, packed_dtype)

        if dtype == simple_dtype:
            assert print_rec_simple(arr) == [
                "s:0,0,0,-0", "s:1,1,1.5,-2.5", "s:0,2,3,-5"
            ]
        else:
            assert print_rec_packed(arr) == [
                "p:0,0,0,-0", "p:1,1,1.5,-2.5", "p:0,2,3,-5"
            ]

    nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)])

    arr = create_rec_nested(0)
    assert arr.dtype == nested_dtype
    assert_equal(arr, [], nested_dtype)

    arr = create_rec_nested(3)
    assert arr.dtype == nested_dtype
    assert_equal(arr, [((False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5)),
                       ((True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)),
                       ((False, 2, 3.0, -5.0), (True, 3, 4.5, -7.5))],
                 nested_dtype)
    assert print_rec_nested(arr) == [
        "n:a=s:0,0,0,-0;b=p:1,1,1.5,-2.5", "n:a=s:1,1,1.5,-2.5;b=p:0,2,3,-5",
        "n:a=s:0,2,3,-5;b=p:1,3,4.5,-7.5"
    ]

    arr = create_rec_partial(3)
    assert str(arr.dtype) == partial_dtype_fmt()
    partial_dtype = arr.dtype
    assert '' not in arr.dtype.fields
    assert partial_dtype.itemsize > simple_dtype.itemsize
    assert_equal(arr, elements, simple_dtype)
    assert_equal(arr, elements, packed_dtype)

    arr = create_rec_partial_nested(3)
    assert str(arr.dtype) == partial_nested_fmt()
    assert '' not in arr.dtype.fields
    assert '' not in arr.dtype.fields['a'][0].fields
    assert arr.dtype.itemsize > partial_dtype.itemsize
    np.testing.assert_equal(arr['a'], create_rec_partial(3))
Пример #3
0
def test_recarray(simple_dtype, packed_dtype):
    from pybind11_tests import (create_rec_simple, create_rec_packed, create_rec_nested,
                                print_rec_simple, print_rec_packed, print_rec_nested,
                                create_rec_partial, create_rec_partial_nested)

    elements = [(False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)]

    for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packed_dtype)]:
        arr = func(0)
        assert arr.dtype == dtype
        assert_equal(arr, [], simple_dtype)
        assert_equal(arr, [], packed_dtype)

        arr = func(3)
        assert arr.dtype == dtype
        assert_equal(arr, elements, simple_dtype)
        assert_equal(arr, elements, packed_dtype)

        if dtype == simple_dtype:
            assert print_rec_simple(arr) == [
                "s:0,0,0,-0",
                "s:1,1,1.5,-2.5",
                "s:0,2,3,-5"
            ]
        else:
            assert print_rec_packed(arr) == [
                "p:0,0,0,-0",
                "p:1,1,1.5,-2.5",
                "p:0,2,3,-5"
            ]

    nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)])

    arr = create_rec_nested(0)
    assert arr.dtype == nested_dtype
    assert_equal(arr, [], nested_dtype)

    arr = create_rec_nested(3)
    assert arr.dtype == nested_dtype
    assert_equal(arr, [((False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5)),
                       ((True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)),
                       ((False, 2, 3.0, -5.0), (True, 3, 4.5, -7.5))], nested_dtype)
    assert print_rec_nested(arr) == [
        "n:a=s:0,0,0,-0;b=p:1,1,1.5,-2.5",
        "n:a=s:1,1,1.5,-2.5;b=p:0,2,3,-5",
        "n:a=s:0,2,3,-5;b=p:1,3,4.5,-7.5"
    ]

    arr = create_rec_partial(3)
    assert str(arr.dtype) == partial_dtype_fmt()
    partial_dtype = arr.dtype
    assert '' not in arr.dtype.fields
    assert partial_dtype.itemsize > simple_dtype.itemsize
    assert_equal(arr, elements, simple_dtype)
    assert_equal(arr, elements, packed_dtype)

    arr = create_rec_partial_nested(3)
    assert str(arr.dtype) == partial_nested_fmt()
    assert '' not in arr.dtype.fields
    assert '' not in arr.dtype.fields['a'][0].fields
    assert arr.dtype.itemsize > partial_dtype.itemsize
    np.testing.assert_equal(arr['a'], create_rec_partial(3))
Пример #4
0
def test_recarray(simple_dtype, packed_dtype):
    from pybind11_tests import (create_rec_simple, create_rec_packed, create_rec_nested,
                                print_rec_simple, print_rec_packed, print_rec_nested,
                                create_rec_partial, create_rec_partial_nested)

    elements = [(False, 0, 0.0), (True, 1, 1.5), (False, 2, 3.0)]

    for func, dtype in [(create_rec_simple, simple_dtype), (create_rec_packed, packed_dtype)]:
        arr = func(0)
        assert arr.dtype == dtype
        assert_equal(arr, [], simple_dtype)
        assert_equal(arr, [], packed_dtype)

        arr = func(3)
        assert arr.dtype == dtype
        assert_equal(arr, elements, simple_dtype)
        assert_equal(arr, elements, packed_dtype)

        if dtype == simple_dtype:
            assert print_rec_simple(arr) == [
                "s:0,0,0",
                "s:1,1,1.5",
                "s:0,2,3"
            ]
        else:
            assert print_rec_packed(arr) == [
                "p:0,0,0",
                "p:1,1,1.5",
                "p:0,2,3"
            ]

    nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)])

    arr = create_rec_nested(0)
    assert arr.dtype == nested_dtype
    assert_equal(arr, [], nested_dtype)

    arr = create_rec_nested(3)
    assert arr.dtype == nested_dtype
    assert_equal(arr, [((False, 0, 0.0), (True, 1, 1.5)),
                       ((True, 1, 1.5), (False, 2, 3.0)),
                       ((False, 2, 3.0), (True, 3, 4.5))], nested_dtype)
    assert print_rec_nested(arr) == [
        "n:a=s:0,0,0;b=p:1,1,1.5",
        "n:a=s:1,1,1.5;b=p:0,2,3",
        "n:a=s:0,2,3;b=p:1,3,4.5"
    ]

    arr = create_rec_partial(3)
    assert str(arr.dtype) == "{'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':24}"
    partial_dtype = arr.dtype
    assert '' not in arr.dtype.fields
    assert partial_dtype.itemsize > simple_dtype.itemsize
    assert_equal(arr, elements, simple_dtype)
    assert_equal(arr, elements, packed_dtype)

    arr = create_rec_partial_nested(3)
    assert str(arr.dtype) == "{'names':['a'], 'formats':[{'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':24}], 'offsets':[8], 'itemsize':40}"
    assert '' not in arr.dtype.fields
    assert '' not in arr.dtype.fields['a'][0].fields
    assert arr.dtype.itemsize > partial_dtype.itemsize
    np.testing.assert_equal(arr['a'], create_rec_partial(3))
Пример #5
0
def test_recarray(simple_dtype, packed_dtype):
    from pybind11_tests import (create_rec_simple, create_rec_packed,
                                create_rec_nested, print_rec_simple,
                                print_rec_packed, print_rec_nested,
                                create_rec_partial, create_rec_partial_nested)

    elements = [(False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5),
                (False, 2, 3.0, -5.0)]

    for func, dtype in [(create_rec_simple, simple_dtype),
                        (create_rec_packed, packed_dtype)]:
        arr = func(0)
        if arr.dtype != dtype:
            raise AssertionError
        assert_equal(arr, [], simple_dtype)
        assert_equal(arr, [], packed_dtype)

        arr = func(3)
        if arr.dtype != dtype:
            raise AssertionError
        assert_equal(arr, elements, simple_dtype)
        assert_equal(arr, elements, packed_dtype)

        if dtype == simple_dtype:
            if print_rec_simple(arr) != [
                    "s:0,0,0,-0", "s:1,1,1.5,-2.5", "s:0,2,3,-5"
            ]:
                raise AssertionError
        else:
            if print_rec_packed(arr) != [
                    "p:0,0,0,-0", "p:1,1,1.5,-2.5", "p:0,2,3,-5"
            ]:
                raise AssertionError

    nested_dtype = np.dtype([('a', simple_dtype), ('b', packed_dtype)])

    arr = create_rec_nested(0)
    if arr.dtype != nested_dtype:
        raise AssertionError
    assert_equal(arr, [], nested_dtype)

    arr = create_rec_nested(3)
    if arr.dtype != nested_dtype:
        raise AssertionError
    assert_equal(arr, [((False, 0, 0.0, -0.0), (True, 1, 1.5, -2.5)),
                       ((True, 1, 1.5, -2.5), (False, 2, 3.0, -5.0)),
                       ((False, 2, 3.0, -5.0), (True, 3, 4.5, -7.5))],
                 nested_dtype)
    if print_rec_nested(arr) != [
            "n:a=s:0,0,0,-0;b=p:1,1,1.5,-2.5",
            "n:a=s:1,1,1.5,-2.5;b=p:0,2,3,-5",
            "n:a=s:0,2,3,-5;b=p:1,3,4.5,-7.5"
    ]:
        raise AssertionError

    arr = create_rec_partial(3)
    if str(arr.dtype) != partial_dtype_fmt():
        raise AssertionError
    partial_dtype = arr.dtype
    if '' in arr.dtype.fields:
        raise AssertionError
    if partial_dtype.itemsize <= simple_dtype.itemsize:
        raise AssertionError
    assert_equal(arr, elements, simple_dtype)
    assert_equal(arr, elements, packed_dtype)

    arr = create_rec_partial_nested(3)
    if str(arr.dtype) != partial_nested_fmt():
        raise AssertionError
    if '' in arr.dtype.fields:
        raise AssertionError
    if '' in arr.dtype.fields['a'][0].fields:
        raise AssertionError
    if arr.dtype.itemsize <= partial_dtype.itemsize:
        raise AssertionError
    np.testing.assert_equal(arr['a'], create_rec_partial(3))