numcodecs 0.16.4__cp313-cp313-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. numcodecs/__init__.py +146 -0
  2. numcodecs/_shuffle.cpython-313-darwin.so +0 -0
  3. numcodecs/abc.py +126 -0
  4. numcodecs/astype.py +72 -0
  5. numcodecs/base64.py +26 -0
  6. numcodecs/bitround.py +80 -0
  7. numcodecs/blosc.cpython-313-darwin.so +0 -0
  8. numcodecs/bz2.py +45 -0
  9. numcodecs/categorize.py +98 -0
  10. numcodecs/checksum32.py +189 -0
  11. numcodecs/compat.py +206 -0
  12. numcodecs/compat_ext.cpython-313-darwin.so +0 -0
  13. numcodecs/delta.py +94 -0
  14. numcodecs/errors.py +26 -0
  15. numcodecs/fixedscaleoffset.py +130 -0
  16. numcodecs/fletcher32.cpython-313-darwin.so +0 -0
  17. numcodecs/gzip.py +50 -0
  18. numcodecs/jenkins.cpython-313-darwin.so +0 -0
  19. numcodecs/json.py +107 -0
  20. numcodecs/lz4.cpython-313-darwin.so +0 -0
  21. numcodecs/lzma.py +71 -0
  22. numcodecs/msgpacks.py +86 -0
  23. numcodecs/ndarray_like.py +65 -0
  24. numcodecs/packbits.py +82 -0
  25. numcodecs/pcodec.py +119 -0
  26. numcodecs/pickles.py +55 -0
  27. numcodecs/quantize.py +98 -0
  28. numcodecs/registry.py +74 -0
  29. numcodecs/shuffle.py +61 -0
  30. numcodecs/tests/__init__.py +3 -0
  31. numcodecs/tests/common.py +275 -0
  32. numcodecs/tests/package_with_entrypoint/__init__.py +11 -0
  33. numcodecs/tests/package_with_entrypoint-0.1.dist-info/entry_points.txt +2 -0
  34. numcodecs/tests/test_astype.py +74 -0
  35. numcodecs/tests/test_base64.py +81 -0
  36. numcodecs/tests/test_bitround.py +81 -0
  37. numcodecs/tests/test_blosc.py +290 -0
  38. numcodecs/tests/test_bz2.py +66 -0
  39. numcodecs/tests/test_categorize.py +87 -0
  40. numcodecs/tests/test_checksum32.py +199 -0
  41. numcodecs/tests/test_compat.py +111 -0
  42. numcodecs/tests/test_delta.py +61 -0
  43. numcodecs/tests/test_entrypoints.py +24 -0
  44. numcodecs/tests/test_entrypoints_backport.py +36 -0
  45. numcodecs/tests/test_fixedscaleoffset.py +77 -0
  46. numcodecs/tests/test_fletcher32.py +56 -0
  47. numcodecs/tests/test_gzip.py +110 -0
  48. numcodecs/tests/test_jenkins.py +150 -0
  49. numcodecs/tests/test_json.py +85 -0
  50. numcodecs/tests/test_lz4.py +83 -0
  51. numcodecs/tests/test_lzma.py +94 -0
  52. numcodecs/tests/test_msgpacks.py +126 -0
  53. numcodecs/tests/test_ndarray_like.py +48 -0
  54. numcodecs/tests/test_packbits.py +39 -0
  55. numcodecs/tests/test_pcodec.py +90 -0
  56. numcodecs/tests/test_pickles.py +61 -0
  57. numcodecs/tests/test_pyzstd.py +76 -0
  58. numcodecs/tests/test_quantize.py +76 -0
  59. numcodecs/tests/test_registry.py +43 -0
  60. numcodecs/tests/test_shuffle.py +166 -0
  61. numcodecs/tests/test_vlen_array.py +97 -0
  62. numcodecs/tests/test_vlen_bytes.py +93 -0
  63. numcodecs/tests/test_vlen_utf8.py +91 -0
  64. numcodecs/tests/test_zarr3.py +48 -0
  65. numcodecs/tests/test_zarr3_import.py +13 -0
  66. numcodecs/tests/test_zfpy.py +104 -0
  67. numcodecs/tests/test_zlib.py +94 -0
  68. numcodecs/tests/test_zstd.py +189 -0
  69. numcodecs/version.py +34 -0
  70. numcodecs/vlen.cpython-313-darwin.so +0 -0
  71. numcodecs/zarr3.py +67 -0
  72. numcodecs/zfpy.py +112 -0
  73. numcodecs/zlib.py +42 -0
  74. numcodecs/zstd.cpython-313-darwin.so +0 -0
  75. numcodecs-0.16.4.dist-info/METADATA +67 -0
  76. numcodecs-0.16.4.dist-info/RECORD +87 -0
  77. numcodecs-0.16.4.dist-info/WHEEL +6 -0
  78. numcodecs-0.16.4.dist-info/licenses/LICENSE.txt +21 -0
  79. numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSE.txt +31 -0
  80. numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/BITSHUFFLE.txt +21 -0
  81. numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/FASTLZ.txt +20 -0
  82. numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/LZ4.txt +25 -0
  83. numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/SNAPPY.txt +28 -0
  84. numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/STDINT.txt +29 -0
  85. numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/ZLIB-NG.txt +17 -0
  86. numcodecs-0.16.4.dist-info/licenses/c-blosc/LICENSES/ZLIB.txt +22 -0
  87. numcodecs-0.16.4.dist-info/top_level.txt +1 -0
@@ -0,0 +1,111 @@
1
+ import array
2
+ import mmap
3
+
4
+ import numpy as np
5
+ import pytest
6
+
7
+ from numcodecs.compat import ensure_bytes, ensure_contiguous_ndarray, ensure_text
8
+
9
+
10
+ def test_ensure_text():
11
+ bufs = [
12
+ b'adsdasdas',
13
+ 'adsdasdas',
14
+ np.asarray(memoryview(b'adsdasdas')),
15
+ array.array('B', b'qwertyuiqwertyui'),
16
+ ]
17
+ for buf in bufs:
18
+ b = ensure_text(buf)
19
+ assert isinstance(b, str)
20
+
21
+
22
+ def test_ensure_bytes():
23
+ bufs = [
24
+ b'adsdasdas',
25
+ bytes(20),
26
+ np.arange(100),
27
+ array.array('l', b'qwertyuiqwertyui'),
28
+ ]
29
+ for buf in bufs:
30
+ b = ensure_bytes(buf)
31
+ assert isinstance(b, bytes)
32
+
33
+
34
+ def test_ensure_contiguous_ndarray_shares_memory():
35
+ typed_bufs = [
36
+ ('u', 1, b'adsdasdas'),
37
+ ('u', 1, bytes(20)),
38
+ ('i', 8, np.arange(100, dtype=np.int64)),
39
+ ('f', 8, np.linspace(0, 1, 100, dtype=np.float64)),
40
+ ('i', 4, array.array('i', b'qwertyuiqwertyui')),
41
+ ('u', 4, array.array('I', b'qwertyuiqwertyui')),
42
+ ('f', 4, array.array('f', b'qwertyuiqwertyui')),
43
+ ('f', 8, array.array('d', b'qwertyuiqwertyui')),
44
+ ('i', 1, array.array('b', b'qwertyuiqwertyui')),
45
+ ('u', 1, array.array('B', b'qwertyuiqwertyui')),
46
+ ('u', 1, mmap.mmap(-1, 10)),
47
+ ]
48
+ for expected_kind, expected_itemsize, buf in typed_bufs:
49
+ a = ensure_contiguous_ndarray(buf)
50
+ assert isinstance(a, np.ndarray)
51
+ assert expected_kind == a.dtype.kind
52
+ if isinstance(buf, array.array):
53
+ assert buf.itemsize == a.dtype.itemsize
54
+ else:
55
+ assert expected_itemsize == a.dtype.itemsize
56
+ assert np.shares_memory(a, memoryview(buf))
57
+
58
+
59
+ def test_ensure_bytes_invalid_inputs():
60
+ # object array not allowed
61
+ a = np.array(['Xin chào thế giới'], dtype=object)
62
+ for e in (a, memoryview(a)):
63
+ with pytest.raises(TypeError):
64
+ ensure_bytes(e)
65
+
66
+
67
+ @pytest.mark.filterwarnings(
68
+ "ignore:The 'u' type code is deprecated and will be removed in Python 3.16"
69
+ )
70
+ def test_ensure_contiguous_ndarray_invalid_inputs():
71
+ # object array not allowed
72
+ a = np.array(['Xin chào thế giới'], dtype=object)
73
+ for e in (a, memoryview(a)):
74
+ with pytest.raises(TypeError):
75
+ ensure_contiguous_ndarray(e)
76
+
77
+ # non-contiguous arrays not allowed
78
+ with pytest.raises(ValueError):
79
+ ensure_contiguous_ndarray(np.arange(100)[::2])
80
+
81
+ # unicode array.array not allowed
82
+ a = array.array('u', 'qwertyuiqwertyui')
83
+ with pytest.raises(TypeError):
84
+ ensure_contiguous_ndarray(a)
85
+
86
+
87
+ def test_ensure_contiguous_ndarray_writeable():
88
+ # check that the writeability of the underlying buffer is preserved
89
+ for writeable in (False, True):
90
+ a = np.arange(100)
91
+ a.setflags(write=writeable)
92
+ m = ensure_contiguous_ndarray(a)
93
+ assert m.flags.writeable == writeable
94
+ m = ensure_contiguous_ndarray(memoryview(a))
95
+ assert m.flags.writeable == writeable
96
+
97
+
98
+ def test_ensure_contiguous_ndarray_max_buffer_size():
99
+ for max_buffer_size in (4, 64, 1024):
100
+ ensure_contiguous_ndarray(np.zeros(max_buffer_size - 1, dtype=np.int8), max_buffer_size)
101
+ ensure_contiguous_ndarray(np.zeros(max_buffer_size, dtype=np.int8), max_buffer_size)
102
+ buffers = [
103
+ bytes(b"x" * (max_buffer_size + 1)),
104
+ np.zeros(max_buffer_size + 1, dtype=np.int8),
105
+ np.zeros(max_buffer_size + 2, dtype=np.int8),
106
+ np.zeros(max_buffer_size, dtype=np.int16),
107
+ np.zeros(max_buffer_size, dtype=np.int32),
108
+ ]
109
+ for buf in buffers:
110
+ with pytest.raises(ValueError):
111
+ ensure_contiguous_ndarray(buf, max_buffer_size=max_buffer_size)
@@ -0,0 +1,61 @@
1
+ import numpy as np
2
+ import pytest
3
+ from numpy.testing import assert_array_equal
4
+
5
+ from numcodecs.delta import Delta
6
+ from numcodecs.tests.common import (
7
+ check_backwards_compatibility,
8
+ check_config,
9
+ check_encode_decode,
10
+ check_repr,
11
+ )
12
+
13
+ # mix of dtypes: integer, float
14
+ # mix of shapes: 1D, 2D, 3D
15
+ # mix of orders: C, F
16
+ arrays = [
17
+ np.random.randint(0, 1, size=110, dtype='?').reshape(10, 11),
18
+ np.arange(1000, dtype='<i4'),
19
+ np.linspace(1000, 1001, 1000, dtype='<f4').reshape(100, 10),
20
+ np.random.normal(loc=1000, scale=1, size=(10, 10, 10)).astype('<f8'),
21
+ np.random.randint(0, 200, size=1000, dtype='u2').astype('<u2').reshape(100, 10, order='F'),
22
+ ]
23
+
24
+
25
+ def test_encode_decode():
26
+ for arr in arrays:
27
+ codec = Delta(dtype=arr.dtype)
28
+ check_encode_decode(arr, codec)
29
+
30
+
31
+ def test_encode():
32
+ dtype = 'i8'
33
+ astype = 'i4'
34
+ codec = Delta(dtype=dtype, astype=astype)
35
+ arr = np.arange(10, 20, 1, dtype=dtype)
36
+ expect = np.array([10] + ([1] * 9), dtype=astype)
37
+ actual = codec.encode(arr)
38
+ assert_array_equal(expect, actual)
39
+ assert np.dtype(astype) == actual.dtype
40
+
41
+
42
+ def test_config():
43
+ codec = Delta(dtype='<i4', astype='<i2')
44
+ check_config(codec)
45
+
46
+
47
+ def test_repr():
48
+ check_repr("Delta(dtype='<i4', astype='<i2')")
49
+
50
+
51
+ def test_backwards_compatibility():
52
+ for arr in arrays:
53
+ codec = Delta(dtype=arr.dtype)
54
+ check_backwards_compatibility(Delta.codec_id, [arr], [codec], prefix=str(arr.dtype))
55
+
56
+
57
+ def test_errors():
58
+ with pytest.raises(ValueError):
59
+ Delta(dtype=object)
60
+ with pytest.raises(ValueError):
61
+ Delta(dtype='i8', astype=object)
@@ -0,0 +1,24 @@
1
+ import os.path
2
+ import sys
3
+
4
+ import pytest
5
+
6
+ import numcodecs.registry
7
+
8
+ here = os.path.abspath(os.path.dirname(__file__))
9
+
10
+
11
+ @pytest.fixture
12
+ def set_path():
13
+ sys.path.append(here)
14
+ numcodecs.registry.run_entrypoints()
15
+ yield
16
+ sys.path.remove(here)
17
+ numcodecs.registry.run_entrypoints()
18
+ numcodecs.registry.codec_registry.pop("test")
19
+
20
+
21
+ @pytest.mark.usefixtures("set_path")
22
+ def test_entrypoint_codec():
23
+ cls = numcodecs.registry.get_codec({"id": "test"})
24
+ assert cls.codec_id == "test"
@@ -0,0 +1,36 @@
1
+ import importlib.util
2
+ import os.path
3
+ import sys
4
+ from multiprocessing import Process
5
+
6
+ import pytest
7
+
8
+ import numcodecs.registry
9
+
10
+ importlib_spec = importlib.util.find_spec("importlib_metadata")
11
+ if importlib_spec is None or importlib_spec.loader is None: # pragma: no cover
12
+ pytest.skip(
13
+ "This test module requires importlib_metadata to be installed",
14
+ allow_module_level=True,
15
+ )
16
+
17
+ here = os.path.abspath(os.path.dirname(__file__))
18
+
19
+
20
+ def get_entrypoints_with_importlib_metadata_loaded():
21
+ # importlib_metadata patches importlib.metadata, which can lead to breaking changes
22
+ # to the APIs of EntryPoint objects used when registering entrypoints. Attempt to
23
+ # isolate those changes to just this test.
24
+ import importlib_metadata # noqa: F401
25
+
26
+ sys.path.append(here)
27
+ numcodecs.registry.run_entrypoints()
28
+ cls = numcodecs.registry.get_codec({"id": "test"})
29
+ assert cls.codec_id == "test"
30
+
31
+
32
+ def test_entrypoint_codec_with_importlib_metadata():
33
+ p = Process(target=get_entrypoints_with_importlib_metadata_loaded)
34
+ p.start()
35
+ p.join()
36
+ assert p.exitcode == 0
@@ -0,0 +1,77 @@
1
+ import itertools
2
+
3
+ import numpy as np
4
+ import pytest
5
+ from numpy.testing import assert_array_equal
6
+
7
+ from numcodecs.fixedscaleoffset import FixedScaleOffset
8
+ from numcodecs.tests.common import (
9
+ check_backwards_compatibility,
10
+ check_config,
11
+ check_encode_decode,
12
+ check_repr,
13
+ )
14
+
15
+ arrays = [
16
+ np.linspace(1000, 1001, 1000, dtype='<f8'),
17
+ np.random.normal(loc=1000, scale=1, size=1000).astype('<f8'),
18
+ np.linspace(1000, 1001, 1000, dtype='<f8').reshape(100, 10),
19
+ np.linspace(1000, 1001, 1000, dtype='<f8').reshape(100, 10, order='F'),
20
+ np.linspace(1000, 1001, 1000, dtype='<f8').reshape(10, 10, 10),
21
+ ]
22
+
23
+
24
+ codecs = [
25
+ FixedScaleOffset(offset=1000, scale=10, dtype='<f8', astype='<i1'),
26
+ FixedScaleOffset(offset=1000, scale=10**2, dtype='<f8', astype='<i2'),
27
+ FixedScaleOffset(offset=1000, scale=10**6, dtype='<f8', astype='<i4'),
28
+ FixedScaleOffset(offset=1000, scale=10**12, dtype='<f8', astype='<i8'),
29
+ FixedScaleOffset(offset=1000, scale=10**12, dtype='<f8'),
30
+ ]
31
+
32
+
33
+ def test_encode_decode():
34
+ for arr, codec in itertools.product(arrays, codecs):
35
+ precision = int(np.log10(codec.scale))
36
+ check_encode_decode(arr, codec, precision=precision)
37
+
38
+
39
+ @pytest.mark.parametrize(
40
+ ("offset", "scale", "expected"),
41
+ [
42
+ (1000, 10, [0, 6, 11, 17, 22, 28, 33, 39, 44, 50]),
43
+ (1002.5, 10, [-25, -19, -14, -8, -3, 3, 8, 14, 19, 25]),
44
+ (1000, 0.5, [0, 0, 1, 1, 1, 1, 2, 2, 2, 2]),
45
+ ],
46
+ )
47
+ def test_encode(offset: float, scale: float, expected: list[int]):
48
+ dtype = '<f8'
49
+ astype = np.int16
50
+ codec = FixedScaleOffset(scale=scale, offset=offset, dtype=dtype, astype=astype)
51
+ arr = np.linspace(1000, 1005, 10, dtype=dtype)
52
+ expect = np.array(expected, dtype=astype)
53
+ actual = codec.encode(arr)
54
+ assert_array_equal(expect, actual)
55
+ assert np.dtype(astype) == actual.dtype
56
+
57
+
58
+ def test_config():
59
+ codec = FixedScaleOffset(dtype='<f8', astype='<i4', scale=10, offset=100)
60
+ check_config(codec)
61
+
62
+
63
+ def test_repr():
64
+ stmt = "FixedScaleOffset(scale=10, offset=100, dtype='<f8', astype='<i4')"
65
+ check_repr(stmt)
66
+
67
+
68
+ def test_backwards_compatibility():
69
+ precision = [int(np.log10(codec.scale)) for codec in codecs]
70
+ check_backwards_compatibility(FixedScaleOffset.codec_id, arrays, codecs, precision=precision)
71
+
72
+
73
+ def test_errors():
74
+ with pytest.raises(ValueError):
75
+ FixedScaleOffset(dtype=object, astype='i4', scale=10, offset=100)
76
+ with pytest.raises(ValueError):
77
+ FixedScaleOffset(dtype='f8', astype=object, scale=10, offset=100)
@@ -0,0 +1,56 @@
1
+ import numpy as np
2
+ import pytest
3
+
4
+ from numcodecs.fletcher32 import Fletcher32
5
+
6
+
7
+ @pytest.mark.parametrize("dtype", ["uint8", "int32", "float32"])
8
+ def test_with_data(dtype):
9
+ data = np.arange(100, dtype=dtype)
10
+ f = Fletcher32()
11
+ arr = np.frombuffer(f.decode(f.encode(data)), dtype=dtype)
12
+ assert (arr == data).all()
13
+
14
+
15
+ def test_error():
16
+ data = np.arange(100)
17
+ f = Fletcher32()
18
+ enc = f.encode(data)
19
+ enc2 = bytearray(enc)
20
+ enc2[0] += 1
21
+ with pytest.raises(RuntimeError) as e:
22
+ f.decode(enc2)
23
+ assert "fletcher32 checksum" in str(e.value)
24
+
25
+
26
+ def test_known():
27
+ data = (
28
+ b'w\x07\x00\x00\x00\x00\x00\x00\x85\xf6\xff\xff\xff\xff\xff\xff'
29
+ b'i\x07\x00\x00\x00\x00\x00\x00\x94\xf6\xff\xff\xff\xff\xff\xff'
30
+ b'\x88\t\x00\x00\x00\x00\x00\x00i\x03\x00\x00\x00\x00\x00\x00'
31
+ b'\x93\xfd\xff\xff\xff\xff\xff\xff\xc3\xfc\xff\xff\xff\xff\xff\xff'
32
+ b"'\x02\x00\x00\x00\x00\x00\x00\xba\xf7\xff\xff\xff\xff\xff\xff"
33
+ b'\xfd%\x86d'
34
+ )
35
+ data3 = Fletcher32().decode(data)
36
+ outarr = np.frombuffer(data3, dtype="<i8")
37
+ expected = [
38
+ 1911,
39
+ -2427,
40
+ 1897,
41
+ -2412,
42
+ 2440,
43
+ 873,
44
+ -621,
45
+ -829,
46
+ 551,
47
+ -2118,
48
+ ]
49
+ assert outarr.tolist() == expected
50
+
51
+
52
+ def test_out():
53
+ data = np.frombuffer(bytearray(b"Hello World"), dtype="uint8")
54
+ f = Fletcher32()
55
+ result = f.encode(data)
56
+ f.decode(result, out=data)
@@ -0,0 +1,110 @@
1
+ import itertools
2
+
3
+ import numpy as np
4
+ import pytest
5
+
6
+ from numcodecs.gzip import GZip
7
+ from numcodecs.tests.common import (
8
+ check_backwards_compatibility,
9
+ check_config,
10
+ check_encode_decode,
11
+ check_err_decode_object_buffer,
12
+ check_err_encode_object_buffer,
13
+ check_repr,
14
+ )
15
+
16
+ codecs = [
17
+ GZip(),
18
+ GZip(level=-1),
19
+ GZip(level=0),
20
+ GZip(level=1),
21
+ GZip(level=5),
22
+ GZip(level=9),
23
+ ]
24
+
25
+
26
+ # mix of dtypes: integer, float, bool, string
27
+ # mix of shapes: 1D, 2D, 3D
28
+ # mix of orders: C, F
29
+ arrays = [
30
+ np.arange(1000, dtype='i4'),
31
+ np.linspace(1000, 1001, 1000, dtype='f8'),
32
+ np.random.normal(loc=1000, scale=1, size=(100, 10)),
33
+ np.random.randint(0, 2, size=1000, dtype=bool).reshape(100, 10, order='F'),
34
+ np.random.choice([b'a', b'bb', b'ccc'], size=1000).reshape(10, 10, 10),
35
+ np.random.randint(0, 2**60, size=1000, dtype='u8').view('M8[ns]'),
36
+ np.random.randint(0, 2**60, size=1000, dtype='u8').view('m8[ns]'),
37
+ np.random.randint(0, 2**25, size=1000, dtype='u8').view('M8[m]'),
38
+ np.random.randint(0, 2**25, size=1000, dtype='u8').view('m8[m]'),
39
+ np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('M8[ns]'),
40
+ np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[ns]'),
41
+ np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('M8[m]'),
42
+ np.random.randint(-(2**63), -(2**63) + 20, size=1000, dtype='i8').view('m8[m]'),
43
+ ]
44
+
45
+
46
+ def test_encode_decode():
47
+ for arr, codec in itertools.product(arrays, codecs):
48
+ check_encode_decode(arr, codec)
49
+
50
+
51
+ def test_config():
52
+ codec = GZip(level=3)
53
+ check_config(codec)
54
+
55
+
56
+ def test_repr():
57
+ check_repr("GZip(level=3)")
58
+
59
+
60
+ def test_eq():
61
+ assert GZip() == GZip()
62
+ assert not GZip() != GZip()
63
+ assert GZip(1) == GZip(1)
64
+ assert GZip(1) != GZip(9)
65
+ assert GZip() != 'foo'
66
+ assert 'foo' != GZip()
67
+ assert not GZip() == 'foo'
68
+
69
+
70
+ def test_backwards_compatibility():
71
+ check_backwards_compatibility(GZip.codec_id, arrays, codecs)
72
+
73
+
74
+ def test_err_decode_object_buffer():
75
+ check_err_decode_object_buffer(GZip())
76
+
77
+
78
+ def test_err_encode_object_buffer():
79
+ check_err_encode_object_buffer(GZip())
80
+
81
+
82
+ def test_err_encode_list():
83
+ data = ['foo', 'bar', 'baz']
84
+ for codec in codecs:
85
+ with pytest.raises(TypeError):
86
+ codec.encode(data)
87
+
88
+
89
+ def test_err_encode_non_contiguous():
90
+ # non-contiguous memory
91
+ arr = np.arange(1000, dtype='i4')[::2]
92
+ for codec in codecs:
93
+ with pytest.raises(ValueError):
94
+ codec.encode(arr)
95
+
96
+
97
+ def test_err_out_too_small():
98
+ arr = np.arange(10, dtype='i4')
99
+ out = np.empty_like(arr)[:-1]
100
+ for codec in codecs:
101
+ with pytest.raises(ValueError):
102
+ codec.decode(codec.encode(arr), out)
103
+
104
+
105
+ def test_out_too_large():
106
+ out = np.empty((10,), dtype='i4')
107
+ arr = out[:-1]
108
+ arr[:] = 5
109
+ for codec in codecs:
110
+ codec.decode(codec.encode(arr), out)
@@ -0,0 +1,150 @@
1
+ import numpy as np
2
+ import pytest
3
+
4
+ from numcodecs.checksum32 import JenkinsLookup3
5
+ from numcodecs.jenkins import jenkins_lookup3
6
+
7
+
8
+ def test_jenkins_lookup3():
9
+ h = jenkins_lookup3(b"", 0)
10
+ assert h == 0xDEADBEEF
11
+ h = jenkins_lookup3(b"", 0xDEADBEEF)
12
+ assert h == 0xBD5B7DDE
13
+ h = jenkins_lookup3(b"Four score and seven years ago", 0)
14
+ assert h == 0x17770551
15
+ h = jenkins_lookup3(b"Four score and seven years ago", 1)
16
+ assert h == 0xCD628161
17
+
18
+ # jenkins-cffi example
19
+ h = jenkins_lookup3(b"jenkins", 0)
20
+ assert h == 202276345
21
+
22
+ h_last = [0]
23
+
24
+ h = jenkins_lookup3(b"", h_last[-1])
25
+ assert h not in h_last
26
+ h_last.append(h)
27
+
28
+ h = jenkins_lookup3(b"", h_last[-1])
29
+ assert h not in h_last
30
+ h_last.append(h)
31
+
32
+ h = jenkins_lookup3(b"", h_last[-1])
33
+ assert h not in h_last
34
+ h_last.append(h)
35
+
36
+ h = jenkins_lookup3(b"", h_last[-1])
37
+ assert h not in h_last
38
+ h_last.append(h)
39
+
40
+ h = jenkins_lookup3(b"", h_last[-1])
41
+ assert h not in h_last
42
+ h_last.append(h)
43
+
44
+ h = jenkins_lookup3(b"", h_last[-1])
45
+ assert h not in h_last
46
+ h_last.append(h)
47
+
48
+ h = jenkins_lookup3(b"", h_last[-1])
49
+ assert h not in h_last
50
+ h_last.append(h)
51
+
52
+ h = jenkins_lookup3(b"", h_last[-1])
53
+ assert h not in h_last
54
+ h_last.append(h)
55
+
56
+ h = jenkins_lookup3(b"", h_last[-1])
57
+ assert h not in h_last
58
+ h_last.append(h)
59
+
60
+ a = np.frombuffer(b"Four score and seven years ago", dtype="uint8")
61
+ h = jenkins_lookup3(a, 0)
62
+ assert h == 0x17770551
63
+
64
+
65
+ def test_jenkins_lookup3_codec():
66
+ s = b"Four score and seven years ago"
67
+ j = JenkinsLookup3()
68
+ result = j.encode(s)
69
+ assert result[-4:] == b'\x51\x05\x77\x17'
70
+ assert bytes(j.decode(result)) == s
71
+
72
+ j = JenkinsLookup3(initval=0xDEADBEEF)
73
+ result = j.encode(s)
74
+ assert bytes(j.decode(result)) == s
75
+
76
+ j = JenkinsLookup3(initval=1230)
77
+ result = j.encode(s)
78
+ assert result[-4:] == b'\xd7Z\xe2\x0e'
79
+ assert bytes(j.decode(result)) == s
80
+
81
+ j = JenkinsLookup3(initval=1230, prefix=b"Hello world")
82
+ result = j.encode(s)
83
+ assert bytes(j.decode(result)) == s
84
+
85
+ chunk_index = (
86
+ b"\x00\x08\x00\x00\x00\x00\x00\x00"
87
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
88
+ b"\xf7\x17\x00\x00\x00\x00\x00\x00"
89
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
90
+ b"\xee'\x00\x00\x00\x00\x00\x00"
91
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
92
+ b"\xe57\x00\x00\x00\x00\x00\x00"
93
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
94
+ b"\xdcG\x00\x00\x00\x00\x00\x00"
95
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
96
+ b"\xd3W\x00\x00\x00\x00\x00\x00"
97
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
98
+ b"\xcag\x00\x00\x00\x00\x00\x00"
99
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
100
+ b"\xc1w\x00\x00\x00\x00\x00\x00"
101
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
102
+ b"\xb8\x87\x00\x00\x00\x00\x00\x00"
103
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
104
+ b"\xaf\x97\x00\x00\x00\x00\x00\x00"
105
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
106
+ b"\xa6\xa7\x00\x00\x00\x00\x00\x00"
107
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
108
+ b"\x9d\xb7\x00\x00\x00\x00\x00\x00"
109
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
110
+ b"\x94\xc7\x00\x00\x00\x00\x00\x00"
111
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
112
+ b"\x8b\xd7\x00\x00\x00\x00\x00\x00"
113
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
114
+ b"\x82\xe7\x00\x00\x00\x00\x00\x00"
115
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
116
+ b"y\xf7\x00\x00\x00\x00\x00\x00"
117
+ b"\xf7\x0f\x00\x00\x00\x00\x00\x00"
118
+ b"n\x96\x07\x85"
119
+ )
120
+ hdf5_fadb_prefix = b'FADB\x00\x01\xcf\x01\x00\x00\x00\x00\x00\x00'
121
+ j = JenkinsLookup3(prefix=hdf5_fadb_prefix)
122
+ result = j.encode(chunk_index[:-4])
123
+ j.decode(result)
124
+ assert result == chunk_index
125
+
126
+
127
+ @pytest.mark.parametrize("dtype", ["uint8", "int32", "float32"])
128
+ def test_with_data(dtype):
129
+ data = np.arange(100, dtype=dtype)
130
+ j = JenkinsLookup3()
131
+ arr = np.frombuffer(j.decode(j.encode(data)), dtype=dtype)
132
+ assert (arr == data).all()
133
+
134
+
135
+ def test_error():
136
+ data = np.arange(100)
137
+ j = JenkinsLookup3()
138
+ enc = j.encode(data)
139
+ enc2 = bytearray(enc)
140
+ enc2[0] += 1
141
+ with pytest.raises(RuntimeError) as e:
142
+ j.decode(enc2)
143
+ assert "Bob Jenkin's lookup3 checksum" in str(e.value)
144
+
145
+
146
+ def test_out():
147
+ data = np.frombuffer(bytearray(b"Hello World"), dtype="uint8")
148
+ j = JenkinsLookup3()
149
+ result = j.encode(data)
150
+ j.decode(result, out=data)