File: quickchecks.rs

package info (click to toggle)
rust-seahash 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 208 kB
  • sloc: makefile: 2
file content (47 lines) | stat: -rw-r--r-- 1,530 bytes parent folder | download
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
extern crate seahash;

#[macro_use]
extern crate quickcheck;
use quickcheck::TestResult;

use seahash::hash;
use seahash::reference::hash as reference;
use seahash::SeaHasher;
use std::hash::Hasher;
use std::num::{NonZeroU8, NonZeroUsize};

quickcheck! {
    #[cfg_attr(miri, ignore)] // very slow to run on miri
    fn chunked_matches_buffered(xs: Vec<u8>, chunk_size: NonZeroUsize, times: NonZeroU8, additional: u8) -> TestResult {
        let target_size = xs.len() * times.get() as usize + additional as usize;
        if xs.is_empty() || target_size > 10_000_000 {
            TestResult::discard()
        } else {
            let xs = xs.into_iter()
                .cycle()
                // the vecs produced by quickcheck are perhaps a bit small by default.
                // additional should add some noise to avoid only getting nice even lengths.
                .take(target_size)
                .collect::<Vec<_>>();

            // write all at once
            let mut h0 = SeaHasher::default();
            h0.write(&xs);
            let h0 = h0.finish();

            // write in chunks
            let mut h1 = SeaHasher::default();
            for chunk in xs.chunks(chunk_size.get()) {
                h1.write(chunk);
            }
            let h1 = h1.finish();

            // compare all, including to buffered and reference
            let outcome = h0 == h1
                && h0 == hash(&xs)
                && h0 == reference(&xs);

            TestResult::from_bool(outcome)
        }
    }
}