1
//! Testing-only functionality, used elsewhere in this crate.
2

            
3
pub(crate) use imp::*;
4

            
5
/// Testing implementation helpers.
6
///
7
/// This module comes in two flavors: a stochastic and a non-stochastic one.
8
/// When stochastic testing is enabled, we use a real PRNG, and therefore
9
/// we require more iterations and broader tolerances.
10
///
11
/// The stochastic testing version of this module is on when the
12
/// `stochastic-tests` feature is enabled.
13
#[cfg(any(doc, not(feature = "stochastic-tests")))]
14
mod imp {
15
    /// Return a new RNG -- possibly a pre-seeded one.
16
2
    pub(crate) fn get_rng() -> impl rand::Rng {
17
2
        // When stochastic tests aren't enabled, we use a RNG seeded
18
2
        // with a fixed value and a small number of iterators for each test.
19
2
        use rand::SeedableRng;
20
2
        // Use this RNG to make the tests reproducible.
21
2
        rand_chacha::ChaCha12Rng::from_seed(
22
2
            // Fun facts:
23
2
            // The Julius Tote was a mechanical computer and point-of-sale
24
2
            // system from the 1920s that used horses as an RNG.
25
2
            *b"George Alfred Julius Totalisator",
26
2
        )
27
2
    }
28

            
29
    /// Return the number of iterations for which to run a randomized test.
30
4
    pub(crate) fn get_iters() -> usize {
31
4
        5000
32
4
    }
33

            
34
    /// Assert that a is close to b.
35
6
    pub(crate) fn check_close(a: isize, b: isize) {
36
6
        assert!((a - b).abs() <= (b / 20) + 5);
37
6
    }
38
}
39

            
40
// ------ stochastic implementations of above features.
41
#[cfg(all(not(doc), feature = "stochastic-tests"))]
42
mod imp {
43
    pub(crate) fn get_rng() -> impl rand::Rng {
44
        rand::thread_rng()
45
    }
46

            
47
    #[cfg(all(not(doc), feature = "stochastic-tests"))]
48
    pub(crate) fn get_iters() -> usize {
49
        1000000
50
    }
51

            
52
    #[cfg(feature = "stochastic-tests")]
53
    pub(crate) fn check_close(a: isize, b: isize) {
54
        assert!((a - b).abs() <= (b / 100));
55
    }
56
}