1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
pub fn checksum<T>(data: T) -> u8
where
T: AsRef<[u8]>,
{
(0 as u8).wrapping_sub(
data.as_ref()
.iter()
.fold(0, |acc, &value| acc.wrapping_add(value as u8)),
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_checksum_empty() {
assert_eq!(checksum(&[]), 0x00);
}
#[test]
fn test_checksum_eof_record() {
assert_eq!(checksum(&[0x00, 0x00, 0x00, 0x01]), 0xFF);
}
#[test]
fn test_checksum_ela_record() {
assert_eq!(checksum(&[0x02, 0x00, 0x00, 0x04, 0xFF, 0xFF]), 0xFC);
}
#[test]
fn test_checksum_sla_record() {
assert_eq!(
checksum(&[0x04, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0xCD]),
0x2A
);
}
}