| 1 |
//! WASM compatibility tests |
| 2 |
//! |
| 3 |
//! Tests that the library works in WebAssembly. These tests run natively |
| 4 |
//! (test framework requires std) but exercise code paths used in WASM. |
| 5 |
//! |
| 6 |
//! Run tests: cargo test --test wasm_tests |
| 7 |
|
| 8 |
use crc_fast::{checksum, CrcAlgorithm, Digest}; |
| 9 |
|
| 10 |
#[cfg(feature = "alloc")] |
| 11 |
use crc_fast::{checksum_combine, checksum_with_params, CrcParams}; |
| 12 |
|
| 13 |
/// Test basic CRC calculation |
| 14 |
#[test] |
| 15 |
fn test_wasm_basic_crc32() { |
| 16 |
let data = b"123456789"; |
| 17 |
assert_eq!(checksum(CrcAlgorithm::Crc32IsoHdlc, data), 0xcbf43926); |
| 18 |
assert_eq!(checksum(CrcAlgorithm::Crc32Iscsi, data), 0xe3069283); |
| 19 |
} |
| 20 |
|
| 21 |
/// Test CRC-64 variants |
| 22 |
#[test] |
| 23 |
fn test_wasm_crc64() { |
| 24 |
let data = b"123456789"; |
| 25 |
assert_eq!(checksum(CrcAlgorithm::Crc64Nvme, data), 0xae8b14860a799888); |
| 26 |
assert_eq!(checksum(CrcAlgorithm::Crc64Xz, data), 0x995dc9bbdf1939fa); |
| 27 |
} |
| 28 |
|
| 29 |
/// Test Digest API (incremental hashing) |
| 30 |
#[test] |
| 31 |
fn test_wasm_digest() { |
| 32 |
let mut digest = Digest::new(CrcAlgorithm::Crc32IsoHdlc); |
| 33 |
digest.update(b"1234"); |
| 34 |
digest.update(b"56789"); |
| 35 |
assert_eq!(digest.finalize(), 0xcbf43926); |
| 36 |
} |
| 37 |
|
| 38 |
/// Test all CRC-32 algorithms |
| 39 |
#[test] |
| 40 |
fn test_wasm_all_crc32() { |
| 41 |
let data = b"123456789"; |
| 42 |
assert_eq!(checksum(CrcAlgorithm::Crc32IsoHdlc, data), 0xcbf43926); |
| 43 |
assert_eq!(checksum(CrcAlgorithm::Crc32Bzip2, data), 0xfc891918); |
| 44 |
assert_eq!(checksum(CrcAlgorithm::Crc32Iscsi, data), 0xe3069283); |
| 45 |
assert_eq!(checksum(CrcAlgorithm::Crc32Mpeg2, data), 0x0376e6e7); |
| 46 |
} |
| 47 |
|
| 48 |
/// Test all CRC-64 algorithms |
| 49 |
#[test] |
| 50 |
fn test_wasm_all_crc64() { |
| 51 |
let data = b"123456789"; |
| 52 |
assert_eq!( |
| 53 |
checksum(CrcAlgorithm::Crc64Ecma182, data), |
| 54 |
0x6c40df5f0b497347 |
| 55 |
); |
| 56 |
assert_eq!(checksum(CrcAlgorithm::Crc64Nvme, data), 0xae8b14860a799888); |
| 57 |
assert_eq!(checksum(CrcAlgorithm::Crc64Xz, data), 0x995dc9bbdf1939fa); |
| 58 |
} |
| 59 |
|
| 60 |
/// Test various buffer sizes |
| 61 |
#[test] |
| 62 |
#[cfg(feature = "alloc")] |
| 63 |
fn test_wasm_various_sizes() { |
| 64 |
extern crate alloc; |
| 65 |
use alloc::vec::Vec; |
| 66 |
|
| 67 |
let small = b"hello"; |
| 68 |
let _ = checksum(CrcAlgorithm::Crc32IsoHdlc, small); |
| 69 |
|
| 70 |
let medium = b"The quick brown fox jumps over the lazy dog"; |
| 71 |
let _ = checksum(CrcAlgorithm::Crc32IsoHdlc, medium); |
| 72 |
|
| 73 |
let large: Vec<u8> = (0..1024).map(|i| (i % 256) as u8).collect(); |
| 74 |
let _ = checksum(CrcAlgorithm::Crc32IsoHdlc, &large); |
| 75 |
|
| 76 |
let very_large: Vec<u8> = (0..8192).map(|i| (i % 256) as u8).collect(); |
| 77 |
let _ = checksum(CrcAlgorithm::Crc64Nvme, &very_large); |
| 78 |
} |
| 79 |
|
| 80 |
/// Test empty input |
| 81 |
#[test] |
| 82 |
fn test_wasm_empty() { |
| 83 |
let empty: &[u8] = &[]; |
| 84 |
assert_eq!(checksum(CrcAlgorithm::Crc32IsoHdlc, empty), 0); |
| 85 |
} |
| 86 |
|
| 87 |
/// Test incremental hashing |
| 88 |
#[test] |
| 89 |
fn test_wasm_incremental() { |
| 90 |
let mut digest = Digest::new(CrcAlgorithm::Crc64Nvme); |
| 91 |
for chunk in [b"123", b"456", b"789"].iter() { |
| 92 |
digest.update(*chunk); |
| 93 |
} |
| 94 |
assert_eq!(digest.finalize(), 0xae8b14860a799888); |
| 95 |
} |
| 96 |
|
| 97 |
/// Test digest reset |
| 98 |
#[test] |
| 99 |
fn test_wasm_reset() { |
| 100 |
let mut digest = Digest::new(CrcAlgorithm::Crc32IsoHdlc); |
| 101 |
digest.update(b"123456789"); |
| 102 |
let result1 = digest.finalize(); |
| 103 |
digest.reset(); |
| 104 |
digest.update(b"123456789"); |
| 105 |
let result2 = digest.finalize(); |
| 106 |
assert_eq!(result1, result2); |
| 107 |
} |
| 108 |
|
| 109 |
/// Test custom CRC parameters |
| 110 |
#[test] |
| 111 |
#[cfg(feature = "alloc")] |
| 112 |
fn test_wasm_custom_params() { |
| 113 |
let params = CrcParams::new( |
| 114 |
"CRC-32/CUSTOM", |
| 115 |
32, |
| 116 |
0x04c11db7, |
| 117 |
0xffffffff, |
| 118 |
true, |
| 119 |
0xffffffff, |
| 120 |
0xcbf43926, |
| 121 |
); |
| 122 |
assert_eq!(checksum_with_params(params, b"123456789"), 0xcbf43926); |
| 123 |
} |
| 124 |
|
| 125 |
/// Test checksum combining |
| 126 |
#[test] |
| 127 |
#[cfg(feature = "alloc")] |
| 128 |
fn test_wasm_combine() { |
| 129 |
let crc1 = checksum(CrcAlgorithm::Crc32IsoHdlc, b"1234"); |
| 130 |
let crc2 = checksum(CrcAlgorithm::Crc32IsoHdlc, b"56789"); |
| 131 |
let combined = checksum_combine(CrcAlgorithm::Crc32IsoHdlc, crc1, crc2, 5); |
| 132 |
let expected = checksum(CrcAlgorithm::Crc32IsoHdlc, b"123456789"); |
| 133 |
assert_eq!(combined, expected); |
| 134 |
} |
| 135 |
|
| 136 |
/// Test reflected vs non-reflected |
| 137 |
#[test] |
| 138 |
fn test_wasm_reflection() { |
| 139 |
let data = b"123456789"; |
| 140 |
assert_eq!(checksum(CrcAlgorithm::Crc32IsoHdlc, data), 0xcbf43926); // reflected |
| 141 |
assert_eq!(checksum(CrcAlgorithm::Crc32Bzip2, data), 0xfc891918); // non-reflected |
| 142 |
} |
| 143 |
|
| 144 |
/// Test standard test vectors |
| 145 |
#[test] |
| 146 |
fn test_wasm_vectors() { |
| 147 |
let vectors = [ |
| 148 |
(b"" as &[u8], CrcAlgorithm::Crc32IsoHdlc, 0_u64), |
| 149 |
(b"a", CrcAlgorithm::Crc32IsoHdlc, 0xe8b7be43), |
| 150 |
(b"abc", CrcAlgorithm::Crc32IsoHdlc, 0x352441c2), |
| 151 |
(b"123456789", CrcAlgorithm::Crc32IsoHdlc, 0xcbf43926), |
| 152 |
]; |
| 153 |
|
| 154 |
for (data, algo, expected) in &vectors { |
| 155 |
assert_eq!(checksum(*algo, data), *expected); |
| 156 |
} |
| 157 |
} |