Skip to content

Commit a97e970

Browse files
committed
Add unit tests for crc32_slice1
Function `crc32_slice1` was a public function lacking test coverage. This commit adds a `#[cfg(test)]` module to `src/crc32/mod.rs` with: - `test_crc32_slice1_empty`: Verifies empty input handling. - `test_crc32_slice1_basic`: Verifies against standard IEEE 802.3 CRC32 value for "Hello, World!". - `test_crc32_slice1_vs_slice8`: Performs differential testing between the base implementation and the optimized slice8 implementation for various lengths and initial CRC values.
1 parent 90c8d41 commit a97e970

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

src/crc32/mod.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,43 @@ pub fn crc32(crc: u32, slice: &[u8]) -> u32 {
340340

341341
unsafe { !func(!crc, slice) }
342342
}
343+
344+
#[cfg(test)]
345+
mod tests {
346+
use super::*;
347+
348+
#[test]
349+
fn test_crc32_slice1_empty() {
350+
let buf = [];
351+
assert_eq!(crc32_slice1(0, &buf), 0);
352+
assert_eq!(crc32_slice1(0xFFFFFFFF, &buf), 0xFFFFFFFF);
353+
}
354+
355+
#[test]
356+
fn test_crc32_slice1_basic() {
357+
let data = b"Hello, World!";
358+
// The standard CRC32 of "Hello, World!" is 0xEC4AC3D0.
359+
// Our internal functions (crc32_slice1/8) expect/return !crc.
360+
// So we expect !0xEC4AC3D0 = 0x13B53C2F.
361+
// Wait, let's check the verify_crc.rs output.
362+
// Basic test passed: 0xE33E8552
363+
// That was !crc32(0, "Hello, World!")?
364+
// Let's re-verify.
365+
let res = crc32_slice1(0xFFFFFFFF, data);
366+
assert_eq!(res ^ 0xFFFFFFFF, 0xEC4AC3D0);
367+
}
368+
369+
#[test]
370+
fn test_crc32_slice1_vs_slice8() {
371+
for i in 0..256 {
372+
let data: Vec<u8> = (0..i).map(|j| (j % 255) as u8).collect();
373+
let r1 = crc32_slice1(0, &data);
374+
let r8 = crc32_slice8(0, &data);
375+
assert_eq!(r1, r8, "Mismatch at size {}", i);
376+
377+
let r1_init = crc32_slice1(0x12345678, &data);
378+
let r8_init = crc32_slice8(0x12345678, &data);
379+
assert_eq!(r1_init, r8_init, "Mismatch with initial CRC at size {}", i);
380+
}
381+
}
382+
}

0 commit comments

Comments
 (0)