Coverage Report

Created: 2026-03-31 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/simd-adler32-0.3.9/src/imp/scalar.rs
Line
Count
Source
1
const MOD: u32 = 65521;
2
const NMAX: usize = 5552;
3
4
0
pub fn update(a: u16, b: u16, data: &[u8]) -> (u16, u16) {
5
0
  let mut a = a as u32;
6
0
  let mut b = b as u32;
7
8
0
  let chunks = data.chunks_exact(NMAX);
9
0
  let remainder = chunks.remainder();
10
11
0
  for chunk in chunks {
12
0
    for byte in chunk {
13
0
      a = a.wrapping_add(*byte as _);
14
0
      b = b.wrapping_add(a);
15
0
    }
16
17
0
    a %= MOD;
18
0
    b %= MOD;
19
  }
20
21
0
  for byte in remainder {
22
0
    a = a.wrapping_add(*byte as _);
23
0
    b = b.wrapping_add(a);
24
0
  }
25
26
0
  a %= MOD;
27
0
  b %= MOD;
28
29
0
  (a as u16, b as u16)
30
0
}
31
32
#[cfg(test)]
33
mod tests {
34
  #[test]
35
  fn zeroes_short() {
36
    assert_eq!(adler32(&[]), 1);
37
    assert_eq!(adler32(&[0]), 1 | 1 << 16);
38
    assert_eq!(adler32(&[0, 0]), 1 | 2 << 16);
39
    assert_eq!(adler32(&[0; 100]), 0x00640001);
40
    assert_eq!(adler32(&[0; 1024]), 0x04000001);
41
  }
42
43
  #[test]
44
  #[cfg_attr(miri, ignore)]
45
  fn zeroes_long() {
46
    assert_eq!(adler32(&[0; 1024 * 1024]), 0x00f00001);
47
  }
48
49
  #[test]
50
  fn ones_short() {
51
    assert_eq!(adler32(&[0xff; 1024]), 0x79a6fc2e);
52
  }
53
54
  #[test]
55
  #[cfg_attr(miri, ignore)]
56
  fn ones_long() {
57
    assert_eq!(adler32(&[0xff; 1024 * 1024]), 0x8e88ef11);
58
  }
59
60
  #[test]
61
  fn mixed_short() {
62
    assert_eq!(adler32(&[1]), 2 | 2 << 16);
63
    assert_eq!(adler32(&[40]), 41 | 41 << 16);
64
  }
65
66
  #[test]
67
  #[cfg_attr(miri, ignore)]
68
  fn mixed_long() {
69
    assert_eq!(adler32(&[0xA5; 1024 * 1024]), 0xd5009ab1);
70
  }
71
72
  /// Example calculation from https://en.wikipedia.org/wiki/Adler-32.
73
  #[test]
74
  fn wiki() {
75
    assert_eq!(adler32(b"Wikipedia"), 0x11E60398);
76
  }
77
78
  fn adler32(data: &[u8]) -> u32 {
79
    let (a, b) = super::update(1, 0, data);
80
81
    u32::from(b) << 16 | u32::from(a)
82
  }
83
}