11import itertools
2+ from contextlib import suppress
23
34import numpy as np
45import pytest
56
6- try :
7- from numcodecs .checksum32 import CRC32 , CRC32C , Adler32
8- except ImportError : # pragma: no cover
9- pytest .skip ("numcodecs.checksum32 not available" , allow_module_level = True )
10-
7+ from numcodecs .checksum32 import CRC32 , Adler32
118from numcodecs .tests .common import (
129 check_backwards_compatibility ,
1310 check_config ,
1714 check_repr ,
1815)
1916
17+ has_crc32c = False
18+ with suppress (ImportError ):
19+ from numcodecs .checksum32 import CRC32C
20+
21+ has_crc32c = True
22+
2023# mix of dtypes: integer, float, bool, string
2124# mix of shapes: 1D, 2D, 3D
2225# mix of orders: C, F
3942codecs = [
4043 CRC32 (),
4144 CRC32 (location = "end" ),
42- CRC32C (location = "start" ),
43- CRC32C (),
4445 Adler32 (),
4546 Adler32 (location = "end" ),
4647]
48+ if has_crc32c :
49+ codecs .extend (
50+ [
51+ CRC32C (location = "start" ),
52+ CRC32C (),
53+ ]
54+ )
4755
4856
4957@pytest .mark .parametrize (("codec" , "arr" ), itertools .product (codecs , arrays ))
@@ -88,25 +96,28 @@ def test_err_encode_list(codec):
8896def test_err_location ():
8997 with pytest .raises (ValueError ):
9098 CRC32 (location = "foo" )
91- with pytest .raises (ValueError ):
92- CRC32C (location = "foo" )
9399 with pytest .raises (ValueError ):
94100 Adler32 (location = "foo" )
101+ if has_crc32c :
102+ with pytest .raises (ValueError ):
103+ CRC32C (location = "foo" )
95104
96105
97106def test_repr ():
98107 check_repr ("CRC32(location='start')" )
99- check_repr ("CRC32C(location='start')" )
100- check_repr ("Adler32(location='start')" )
101108 check_repr ("CRC32(location='end')" )
102- check_repr ("CRC32C (location='end ')" )
109+ check_repr ("Adler32 (location='start ')" )
103110 check_repr ("Adler32(location='end')" )
111+ if has_crc32c :
112+ check_repr ("CRC32C(location='start')" )
113+ check_repr ("CRC32C(location='end')" )
104114
105115
106116def test_backwards_compatibility ():
107117 check_backwards_compatibility (CRC32 .codec_id , arrays , [CRC32 ()])
108118 check_backwards_compatibility (Adler32 .codec_id , arrays , [Adler32 ()])
109- check_backwards_compatibility (CRC32C .codec_id , arrays , [CRC32C ()])
119+ if has_crc32c :
120+ check_backwards_compatibility (CRC32C .codec_id , arrays , [CRC32C ()])
110121
111122
112123@pytest .mark .parametrize ("codec" , codecs )
@@ -127,6 +138,7 @@ def test_err_out_too_small(codec):
127138 codec .decode (codec .encode (arr ), out )
128139
129140
141+ @pytest .mark .skipif (not has_crc32c , reason = "Needs `crc32c` installed" )
130142def test_crc32c_checksum ():
131143 arr = np .arange (0 , 64 , dtype = "uint8" )
132144 buf = CRC32C (location = "end" ).encode (arr )
0 commit comments