Exemplo n.º 1
0
def test_encode_none():
    a = np.array([[1, 3], None, [4, 7]], dtype=object)
    codec = VLenArray(int)
    enc = codec.encode(a)
    dec = codec.decode(enc)
    expect = np.array(
        [np.array([1, 3]), np.array([]),
         np.array([4, 7])], dtype=object)
    assert_array_items_equal(expect, dec)
Exemplo n.º 2
0
def test_decode_errors():
    codec = VLenArray('<i8')
    with pytest.raises(TypeError):
        codec.decode(1234)
    # these should look like corrupt data
    with pytest.raises(ValueError):
        codec.decode(b'foo')
    with pytest.raises(ValueError):
        codec.decode(np.arange(2, 3, dtype='i4'))
    with pytest.raises(ValueError):
        codec.decode(np.arange(10, 20, dtype='i4'))
    with pytest.raises(ValueError if PY2 else TypeError):
        # exports old-style buffer interface on PY2, hence ValueError
        codec.decode(u'foo')

    # test out parameter
    enc = codec.encode(arrays[0])
    with pytest.raises(TypeError):
        codec.decode(enc, out=b'foo')
    with pytest.raises(TypeError):
        codec.decode(enc, out=u'foo')
    with pytest.raises(TypeError):
        codec.decode(enc, out=123)
    with pytest.raises(ValueError):
        codec.decode(enc, out=np.zeros(10, dtype='i4'))
Exemplo n.º 3
0
def test_encode_errors():
    codec = VLenArray('<i8')
    with pytest.raises(ValueError):
        codec.encode('foo')
    with pytest.raises(ValueError):
        codec.encode(['foo', 'bar'])
Exemplo n.º 4
0
def test_config():
    codec = VLenArray('<i8')
    check_config(codec)
Exemplo n.º 5
0
                                    check_backwards_compatibility,
                                    assert_array_items_equal)

arrays = [
    np.array(
        [np.array([1, 2, 3]),
         np.array([4]), np.array([5, 6])] * 300,
        dtype=object),
    np.array(
        [np.array([1, 2, 3]),
         np.array([4]), np.array([5, 6])] * 300,
        dtype=object).reshape(90, 10),
]

codecs = [
    VLenArray('<i1'),
    VLenArray('<i2'),
    VLenArray('<i4'),
    VLenArray('<i8'),
    VLenArray('<u1'),
    VLenArray('<u2'),
    VLenArray('<u4'),
    VLenArray('<u8'),
]


def test_encode_decode():
    for arr in arrays:
        for codec in codecs:
            check_encode_decode_array(arr, codec)
Exemplo n.º 6
0
def test_decode_errors():
    codec = VLenArray('<i8')
    with pytest.raises(TypeError):
        codec.decode(1234)
    # these should look like corrupt data
    with pytest.raises(ValueError):
        codec.decode(b'foo')
    with pytest.raises(ValueError):
        codec.decode(np.arange(2, 3, dtype='i4'))
    with pytest.raises(ValueError):
        codec.decode(np.arange(10, 20, dtype='i4'))
    with pytest.raises(TypeError):
        codec.decode('foo')

    # test out parameter
    enc = codec.encode(arrays[0])
    with pytest.raises(TypeError):
        codec.decode(enc, out=b'foo')
    with pytest.raises(TypeError):
        codec.decode(enc, out='foo')
    with pytest.raises(TypeError):
        codec.decode(enc, out=123)
    with pytest.raises(ValueError):
        codec.decode(enc, out=np.zeros(10, dtype='i4'))