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+ has_crc32c = True
21+
2022# mix of dtypes: integer, float, bool, string
2123# mix of shapes: 1D, 2D, 3D
2224# mix of orders: C, F
3941codecs = [
4042 CRC32 (),
4143 CRC32 (location = "end" ),
42- CRC32C (location = "start" ),
43- CRC32C (),
4444 Adler32 (),
4545 Adler32 (location = "end" ),
4646]
47+ if has_crc32c :
48+ codecs .extend ([
49+ CRC32C (location = "start" ),
50+ CRC32C (),
51+ ])
4752
4853
4954@pytest .mark .parametrize (("codec" , "arr" ), itertools .product (codecs , arrays ))
@@ -86,27 +91,30 @@ def test_err_encode_list(codec):
8691
8792
8893def test_err_location ():
89- with pytest .raises (ValueError ):
90- CRC32 (location = "foo" )
91- with pytest .raises (ValueError ):
92- CRC32C (location = "foo" )
9394 with pytest .raises (ValueError ):
9495 Adler32 (location = "foo" )
96+ if has_crc32c :
97+ with pytest .raises (ValueError ):
98+ CRC32 (location = "foo" )
99+ with pytest .raises (ValueError ):
100+ CRC32C (location = "foo" )
95101
96102
97103def test_repr ():
98104 check_repr ("CRC32(location='start')" )
99- check_repr ("CRC32C(location='start')" )
100- check_repr ("Adler32(location='start')" )
101105 check_repr ("CRC32(location='end')" )
102- check_repr ("CRC32C (location='end ')" )
106+ check_repr ("Adler32 (location='start ')" )
103107 check_repr ("Adler32(location='end')" )
108+ if has_crc32c :
109+ check_repr ("CRC32C(location='start')" )
110+ check_repr ("CRC32C(location='end')" )
104111
105112
106113def test_backwards_compatibility ():
107114 check_backwards_compatibility (CRC32 .codec_id , arrays , [CRC32 ()])
108115 check_backwards_compatibility (Adler32 .codec_id , arrays , [Adler32 ()])
109- check_backwards_compatibility (CRC32C .codec_id , arrays , [CRC32C ()])
116+ if has_crc32c :
117+ check_backwards_compatibility (CRC32C .codec_id , arrays , [CRC32C ()])
110118
111119
112120@pytest .mark .parametrize ("codec" , codecs )
@@ -127,6 +135,7 @@ def test_err_out_too_small(codec):
127135 codec .decode (codec .encode (arr ), out )
128136
129137
138+ @pytest .mark .skipif (not has_crc32c , reason = "Needs `crc32c` installed" )
130139def test_crc32c_checksum ():
131140 arr = np .arange (0 , 64 , dtype = "uint8" )
132141 buf = CRC32C (location = "end" ).encode (arr )
0 commit comments