|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from numcodecs.crc32c_ import Crc32c |
| 5 | +from numcodecs.tests.common import ( |
| 6 | + check_encode_decode, |
| 7 | + check_err_decode_object_buffer, |
| 8 | + check_err_encode_object_buffer, |
| 9 | + check_repr, |
| 10 | +) |
| 11 | + |
| 12 | +# mix of dtypes: integer, float, bool, string |
| 13 | +# mix of shapes: 1D, 2D, 3D |
| 14 | +# mix of orders: C, F |
| 15 | +arrays = [ |
| 16 | + np.arange(1000, dtype='i4'), |
| 17 | + np.linspace(1000, 1001, 1000, dtype='f8'), |
| 18 | + np.random.normal(loc=1000, scale=1, size=(100, 10)), |
| 19 | + np.random.randint(0, 2, size=1000, dtype=bool).reshape(100, 10, order='F'), |
| 20 | + np.random.choice([b'a', b'bb', b'ccc'], size=1000).reshape(10, 10, 10), |
| 21 | + np.random.randint(0, 2**60, size=1000, dtype='u8').view('M8[ns]'), |
| 22 | + np.random.randint(0, 2**60, size=1000, dtype='u8').view('m8[ns]'), |
| 23 | + np.random.randint(0, 2**25, size=1000, dtype='u8').view('M8[m]'), |
| 24 | + np.random.randint(0, 2**25, size=1000, dtype='u8').view('m8[m]'), |
| 25 | + np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('M8[ns]'), |
| 26 | + np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[ns]'), |
| 27 | + np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('M8[m]'), |
| 28 | + np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[m]'), |
| 29 | +] |
| 30 | + |
| 31 | + |
| 32 | +def test_encode_decode(): |
| 33 | + for arr in arrays: |
| 34 | + check_encode_decode(arr, Crc32c()) |
| 35 | + |
| 36 | + |
| 37 | +def test_checksum(): |
| 38 | + arr = np.arange(0, 64, dtype="uint8") |
| 39 | + buf = Crc32c().encode(arr) |
| 40 | + assert np.frombuffer(buf, dtype="<u4", offset=(len(buf) - 4))[0] == np.uint32(4218238699) |
| 41 | + |
| 42 | + |
| 43 | +def test_repr(): |
| 44 | + check_repr("Crc32c()") |
| 45 | + |
| 46 | + |
| 47 | +def test_eq(): |
| 48 | + assert Crc32c() == Crc32c() |
| 49 | + assert not Crc32c() != Crc32c() |
| 50 | + assert Crc32c() != 'foo' |
| 51 | + assert 'foo' != Crc32c() |
| 52 | + assert not Crc32c() == 'foo' |
| 53 | + |
| 54 | + |
| 55 | +def test_err_decode_object_buffer(): |
| 56 | + check_err_decode_object_buffer(Crc32c()) |
| 57 | + |
| 58 | + |
| 59 | +def test_err_encode_object_buffer(): |
| 60 | + check_err_encode_object_buffer(Crc32c()) |
| 61 | + |
| 62 | + |
| 63 | +def test_err_encode_list(): |
| 64 | + data = ['foo', 'bar', 'baz'] |
| 65 | + with pytest.raises(TypeError): |
| 66 | + Crc32c().encode(data) |
| 67 | + |
| 68 | + |
| 69 | +def test_err_encode_non_contiguous(): |
| 70 | + # non-contiguous memory |
| 71 | + arr = np.arange(1000, dtype='i4')[::2] |
| 72 | + with pytest.raises(ValueError): |
| 73 | + Crc32c().encode(arr) |
| 74 | + |
| 75 | + |
| 76 | +def test_err_out_too_small(): |
| 77 | + arr = np.arange(10, dtype='i4') |
| 78 | + out = np.empty_like(arr)[:-1] |
| 79 | + with pytest.raises(ValueError): |
| 80 | + Crc32c().decode(Crc32c().encode(arr), out) |
| 81 | + |
| 82 | + |
| 83 | +def test_err_out_too_large(): |
| 84 | + out = np.empty((10,), dtype='i4') |
| 85 | + arr = out[:-1] |
| 86 | + arr[:] = 5 |
| 87 | + with pytest.raises(ValueError): |
| 88 | + Crc32c().decode(Crc32c().encode(arr), out) |
0 commit comments