@@ -137,6 +137,87 @@ def test_crc64nvme_huge_buffer(self):
137137 val = checksums .crc64nvme (huge_buffer )
138138 self .assertEqual (0x2645c28052b1fbb0 , val )
139139
140+ def _test_combine_helper (self , checksum_fn , combine_fn ):
141+ """Helper method to test checksum combine functions with various scenarios."""
142+
143+ # Test 1: Basic combine of two blocks
144+ data1 = b"Hello, "
145+ data2 = b"World!"
146+
147+ crc1 = checksum_fn (data1 )
148+ crc2 = checksum_fn (data2 )
149+ combined = combine_fn (crc1 , crc2 , len (data2 ))
150+ expected = checksum_fn (data1 + data2 )
151+
152+ self .assertEqual (expected , combined )
153+
154+ # Test 2: Empty second block
155+ data1 = b"Hello, World!"
156+ data2 = b""
157+
158+ crc1 = checksum_fn (data1 )
159+ crc2 = checksum_fn (data2 )
160+ combined = combine_fn (crc1 , crc2 , len (data2 ))
161+
162+ self .assertEqual (crc1 , combined )
163+
164+ # Test 3: Multiple blocks
165+ data1 = b"The quick "
166+ data2 = b"brown fox "
167+ data3 = b"jumps over the lazy dog"
168+
169+ crc1 = checksum_fn (data1 )
170+ crc2 = checksum_fn (data2 )
171+ crc3 = checksum_fn (data3 )
172+
173+ combined_12 = combine_fn (crc1 , crc2 , len (data2 ))
174+ combined_123 = combine_fn (combined_12 , crc3 , len (data3 ))
175+ expected = checksum_fn (data1 + data2 + data3 )
176+
177+ self .assertEqual (expected , combined_123 )
178+
179+ # Test 4: Large blocks
180+ data1 = bytes (1024 )
181+ data2 = bytes (range (256 )) * 4
182+
183+ crc1 = checksum_fn (data1 )
184+ crc2 = checksum_fn (data2 )
185+ combined = combine_fn (crc1 , crc2 , len (data2 ))
186+ expected = checksum_fn (data1 + data2 )
187+
188+ self .assertEqual (expected , combined )
189+
190+ def test_crc32_combine (self ):
191+ """Test CRC32 combine function."""
192+ self ._test_combine_helper (checksums .crc32 , checksums .combine_crc32 )
193+
194+ def test_crc32c_combine (self ):
195+ """Test CRC32C combine function."""
196+ self ._test_combine_helper (checksums .crc32c , checksums .combine_crc32c )
197+
198+ def test_crc64nvme_combine (self ):
199+ """Test CRC64-NVME combine function."""
200+ self ._test_combine_helper (checksums .crc64nvme , checksums .combine_crc64nvme )
201+
202+ def test_combine_invalid_inputs (self ):
203+ """Test that combine functions raise ValueError for invalid inputs."""
204+ # Test invalid values (should fail for all algorithms)
205+ for combine_fn in [checksums .combine_crc32 , checksums .combine_crc32c , checksums .combine_crc64nvme ]:
206+ with self .assertRaises (ValueError ) as context :
207+ combine_fn (- 1 , 0 , 0 )
208+ self .assertIn ("not a valid unsigned" , str (context .exception ))
209+
210+ with self .assertRaises (ValueError ) as context :
211+ combine_fn (0 , 0 , - 1 )
212+ self .assertIn ("not a valid unsigned" , str (context .exception ))
213+
214+ # Test that valid inputs don't raise exceptions
215+ for combine_fn in [checksums .combine_crc32 , checksums .combine_crc32c , checksums .combine_crc64nvme ]:
216+ # This should not raise any exception
217+ result = combine_fn (0 , 0 , 0 )
218+ # Result should be an integer
219+ self .assertIsInstance (result , int )
220+
140221
141222if __name__ == '__main__' :
142223 unittest .main ()
0 commit comments