| 1 |
//! no_std compatibility tests |
| 2 |
//! |
| 3 |
//! Tests the library works without std. The test framework requires std, |
| 4 |
//! but this exercises all no_std code paths. |
| 5 |
//! |
| 6 |
//! Run tests: cargo test --test no_std_tests |
| 7 |
|
| 8 |
use crc_fast::{checksum, CrcAlgorithm, Digest}; |
| 9 |
|
| 10 |
/// Test basic checksum calculation (works without std) |
| 11 |
#[test] |
| 12 |
fn test_no_std_basic_checksum() { |
| 13 |
let data = b"123456789"; |
| 14 |
|
| 15 |
// Test all major CRC variants |
| 16 |
assert_eq!( |
| 17 |
checksum(CrcAlgorithm::Crc32IsoHdlc, data), |
| 18 |
0xcbf43926, |
| 19 |
"CRC-32/ISO-HDLC failed" |
| 20 |
); |
| 21 |
|
| 22 |
assert_eq!( |
| 23 |
checksum(CrcAlgorithm::Crc32Iscsi, data), |
| 24 |
0xe3069283, |
| 25 |
"CRC-32/ISCSI failed" |
| 26 |
); |
| 27 |
|
| 28 |
assert_eq!( |
| 29 |
checksum(CrcAlgorithm::Crc64Nvme, data), |
| 30 |
0xae8b14860a799888, |
| 31 |
"CRC-64/NVME failed" |
| 32 |
); |
| 33 |
|
| 34 |
assert_eq!( |
| 35 |
checksum(CrcAlgorithm::Crc64Xz, data), |
| 36 |
0x995dc9bbdf1939fa, |
| 37 |
"CRC-64/XZ failed" |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
/// Test all 21 standard CRC algorithms |
| 42 |
#[test] |
| 43 |
fn test_no_std_all_algorithms() { |
| 44 |
let data = b"123456789"; |
| 45 |
|
| 46 |
// CRC-32 variants (reflected) |
| 47 |
assert_eq!(checksum(CrcAlgorithm::Crc32IsoHdlc, data), 0xcbf43926); |
| 48 |
assert_eq!(checksum(CrcAlgorithm::Crc32Iscsi, data), 0xe3069283); |
| 49 |
|
| 50 |
// CRC-32 variants (non-reflected/forward) |
| 51 |
assert_eq!(checksum(CrcAlgorithm::Crc32Bzip2, data), 0xfc891918); |
| 52 |
assert_eq!(checksum(CrcAlgorithm::Crc32Mpeg2, data), 0x0376e6e7); |
| 53 |
|
| 54 |
// CRC-64 variants |
| 55 |
assert_eq!( |
| 56 |
checksum(CrcAlgorithm::Crc64Ecma182, data), |
| 57 |
0x6c40df5f0b497347 |
| 58 |
); |
| 59 |
assert_eq!(checksum(CrcAlgorithm::Crc64GoIso, data), 0xb90956c775a41001); |
| 60 |
assert_eq!(checksum(CrcAlgorithm::Crc64Ms, data), 0x75d4b74f024eceea); |
| 61 |
assert_eq!(checksum(CrcAlgorithm::Crc64Nvme, data), 0xae8b14860a799888); |
| 62 |
assert_eq!(checksum(CrcAlgorithm::Crc64Redis, data), 0xe9c6d914c4b8d9ca); |
| 63 |
assert_eq!(checksum(CrcAlgorithm::Crc64We, data), 0x62ec59e3f1a4f00a); |
| 64 |
assert_eq!(checksum(CrcAlgorithm::Crc64Xz, data), 0x995dc9bbdf1939fa); |
| 65 |
} |
| 66 |
|
| 67 |
/// Test Digest API (core functionality without std) |
| 68 |
#[test] |
| 69 |
fn test_no_std_digest_api() { |
| 70 |
let mut digest = Digest::new(CrcAlgorithm::Crc32IsoHdlc); |
| 71 |
digest.update(b"1234"); |
| 72 |
digest.update(b"56789"); |
| 73 |
let result = digest.finalize(); |
| 74 |
|
| 75 |
assert_eq!(result, 0xcbf43926, "Digest API failed"); |
| 76 |
} |
| 77 |
|
| 78 |
/// Test empty input |
| 79 |
#[test] |
| 80 |
fn test_no_std_empty_input() { |
| 81 |
let empty: &[u8] = &[]; |
| 82 |
let result = checksum(CrcAlgorithm::Crc32IsoHdlc, empty); |
| 83 |
// Empty input gives 0 after final XOR with xorout |
| 84 |
assert_eq!(result, 0); |
| 85 |
} |
| 86 |
|
| 87 |
/// Test various input sizes to ensure all code paths work |
| 88 |
#[test] |
| 89 |
fn test_no_std_various_sizes() { |
| 90 |
// Small (< 64 bytes) |
| 91 |
let small = b"hello"; |
| 92 |
let _ = checksum(CrcAlgorithm::Crc32IsoHdlc, small); |
| 93 |
|
| 94 |
// Medium (64-256 bytes) |
| 95 |
let medium = b"The quick brown fox jumps over the lazy dog. \ |
| 96 |
The quick brown fox jumps over the lazy dog. \ |
| 97 |
The quick brown fox jumps over the lazy dog."; |
| 98 |
let _ = checksum(CrcAlgorithm::Crc32IsoHdlc, medium); |
| 99 |
|
| 100 |
// Large (> 256 bytes) - tests SIMD path |
| 101 |
let large = [0xAAu8; 1024]; |
| 102 |
let _ = checksum(CrcAlgorithm::Crc32IsoHdlc, &large); |
| 103 |
} |
| 104 |
|
| 105 |
/// Test that reflected and non-reflected algorithms both work |
| 106 |
#[test] |
| 107 |
fn test_no_std_reflection_modes() { |
| 108 |
let data = b"123456789"; |
| 109 |
|
| 110 |
// Reflected (most common) |
| 111 |
assert_eq!(checksum(CrcAlgorithm::Crc32IsoHdlc, data), 0xcbf43926); |
| 112 |
|
| 113 |
// Non-reflected (forward) |
| 114 |
assert_eq!(checksum(CrcAlgorithm::Crc32Bzip2, data), 0xfc891918); |
| 115 |
} |
| 116 |
|
| 117 |
/// Test custom CRC parameters |
| 118 |
#[cfg(feature = "alloc")] |
| 119 |
#[test] |
| 120 |
fn test_no_std_custom_params() { |
| 121 |
use crc_fast::{checksum_with_params, CrcParams}; |
| 122 |
|
| 123 |
let params = CrcParams::new( |
| 124 |
"CRC-32/CUSTOM", |
| 125 |
32, |
| 126 |
0x04c11db7, |
| 127 |
0xffffffff, |
| 128 |
true, |
| 129 |
0xffffffff, |
| 130 |
0xcbf43926, |
| 131 |
); |
| 132 |
|
| 133 |
let result = checksum_with_params(params, b"123456789"); |
| 134 |
assert_eq!(result, 0xcbf43926, "Custom params failed"); |
| 135 |
} |
| 136 |
|
| 137 |
/// Test that Digest can be reused |
| 138 |
#[test] |
| 139 |
fn test_no_std_digest_reuse() { |
| 140 |
let mut digest = Digest::new(CrcAlgorithm::Crc32IsoHdlc); |
| 141 |
|
| 142 |
digest.update(b"123456789"); |
| 143 |
let result1 = digest.finalize(); |
| 144 |
assert_eq!(result1, 0xcbf43926); |
| 145 |
|
| 146 |
digest.reset(); |
| 147 |
digest.update(b"123456789"); |
| 148 |
let result2 = digest.finalize(); |
| 149 |
assert_eq!(result2, 0xcbf43926); |
| 150 |
} |
| 151 |
|
| 152 |
/// Test incremental digest updates |
| 153 |
#[test] |
| 154 |
fn test_no_std_incremental_digest() { |
| 155 |
let mut digest1 = Digest::new(CrcAlgorithm::Crc64Nvme); |
| 156 |
digest1.update(b"123456789"); |
| 157 |
let result1 = digest1.finalize(); |
| 158 |
|
| 159 |
let mut digest2 = Digest::new(CrcAlgorithm::Crc64Nvme); |
| 160 |
digest2.update(b"123"); |
| 161 |
digest2.update(b"456"); |
| 162 |
digest2.update(b"789"); |
| 163 |
let result2 = digest2.finalize(); |
| 164 |
|
| 165 |
assert_eq!(result1, result2, "Incremental digest failed"); |
| 166 |
} |
| 167 |
|
| 168 |
/// Test checksum_combine (requires alloc) |
| 169 |
#[cfg(feature = "alloc")] |
| 170 |
#[test] |
| 171 |
fn test_no_std_checksum_combine() { |
| 172 |
use crc_fast::checksum_combine; |
| 173 |
|
| 174 |
let crc1 = checksum(CrcAlgorithm::Crc32IsoHdlc, b"1234"); |
| 175 |
let crc2 = checksum(CrcAlgorithm::Crc32IsoHdlc, b"56789"); |
| 176 |
let combined = checksum_combine(CrcAlgorithm::Crc32IsoHdlc, crc1, crc2, 5); |
| 177 |
|
| 178 |
let expected = checksum(CrcAlgorithm::Crc32IsoHdlc, b"123456789"); |
| 179 |
assert_eq!(combined, expected, "checksum_combine failed"); |
| 180 |
} |
| 181 |
|
| 182 |
/// Test that the library works with stack-only data |
| 183 |
#[test] |
| 184 |
fn test_no_std_stack_only() { |
| 185 |
// This should work without any heap allocation |
| 186 |
let data = b"123456789"; |
| 187 |
let result = checksum(CrcAlgorithm::Crc32IsoHdlc, data); |
| 188 |
assert_eq!(result, 0xcbf43926); |
| 189 |
} |
| 190 |
|
| 191 |
/// Test both CRC-32 and CRC-64 widths |
| 192 |
#[test] |
| 193 |
fn test_no_std_both_widths() { |
| 194 |
let data = b"test data"; |
| 195 |
|
| 196 |
// CRC-32 |
| 197 |
let _crc32 = checksum(CrcAlgorithm::Crc32IsoHdlc, data); |
| 198 |
|
| 199 |
// CRC-64 |
| 200 |
let _crc64 = checksum(CrcAlgorithm::Crc64Nvme, data); |
| 201 |
} |
| 202 |
|
| 203 |
/// Test with byte patterns that might expose issues |
| 204 |
#[test] |
| 205 |
fn test_no_std_edge_case_patterns() { |
| 206 |
// All zeros |
| 207 |
let zeros = [0u8; 128]; |
| 208 |
let _ = checksum(CrcAlgorithm::Crc32IsoHdlc, &zeros); |
| 209 |
|
| 210 |
// All ones |
| 211 |
let ones = [0xFFu8; 128]; |
| 212 |
let _ = checksum(CrcAlgorithm::Crc32IsoHdlc, &ones); |
| 213 |
|
| 214 |
// Alternating pattern |
| 215 |
let alt = [0xAAu8; 128]; |
| 216 |
let _ = checksum(CrcAlgorithm::Crc32IsoHdlc, &alt); |
| 217 |
} |