Skip to content

Commit

Permalink
Ensure test coverage for const-random case.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkaitchuck committed Nov 21, 2020
1 parent e785e22 commit c5fdfb0
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/random_state.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#[cfg(feature = "runtime-rng")]
#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
use crate::convert::Convert;
use crate::{AHasher};
#[cfg(all(not(feature = "runtime-rng"), feature = "compile-time-rng"))]
#[cfg(all(feature = "compile-time-rng", any(not(feature = "runtime-rng"), test)))]
use const_random::const_random;
use core::fmt;
use core::hash::BuildHasher;
use core::hash::Hasher;
#[cfg(feature = "runtime-rng")]
#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
use lazy_static::*;
use core::sync::atomic::{AtomicUsize, Ordering};

#[cfg(feature = "runtime-rng")]
#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
lazy_static! {
static ref SEEDS: [[u64; 4]; 2] = {
let mut result: [u8; 64] = [0; 64];
Expand Down Expand Up @@ -38,9 +38,9 @@ const PI2: [u64; 4] = [

#[inline]
pub(crate) fn seeds() -> [u64; 4] {
#[cfg(feature = "runtime-rng")]
#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
{ SEEDS[1] }
#[cfg(all(not(feature = "runtime-rng"), feature = "compile-time-rng"))]
#[cfg(all(feature = "compile-time-rng", any(not(feature = "runtime-rng"), test)))]
{ [const_random!(u64), const_random!(u64), const_random!(u64), const_random!(u64)] }
#[cfg(all(not(feature = "runtime-rng"), not(feature = "compile-time-rng")))]
{ PI }
Expand Down Expand Up @@ -72,12 +72,12 @@ impl RandomState {
/// Use randomly generated keys
#[inline]
pub fn new() -> RandomState {
#[cfg(feature = "runtime-rng")]
#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
{
let seeds = *SEEDS;
RandomState::from_keys(seeds[0], seeds[1])
}
#[cfg(all(not(feature = "runtime-rng"), feature = "compile-time-rng"))]
#[cfg(all(feature = "compile-time-rng", any(not(feature = "runtime-rng"), test)))]
{
RandomState::from_keys(
[const_random!(u64), const_random!(u64), const_random!(u64), const_random!(u64)],
Expand Down Expand Up @@ -193,12 +193,24 @@ mod test {
assert_ne!(a.build_hasher().finish(), b.build_hasher().finish());
}

#[cfg(feature = "runtime-rng")]
#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
#[test]
fn test_not_pi() {
assert_ne!(PI, seeds());
}

#[cfg(all(feature = "compile-time-rng", any(not(feature = "runtime-rng"), test)))]
#[test]
fn test_not_pi_const() {
assert_ne!(PI, seeds());
}

#[cfg(all(not(feature = "runtime-rng"), not(feature = "compile-time-rng")))]
#[test]
fn test_pi() {
assert_eq!(PI, seeds());
}

#[test]
fn test_with_seeds_const() {
const _CONST_RANDOM_STATE: RandomState = RandomState::with_seeds(17, 19, 21, 23);
Expand Down

0 comments on commit c5fdfb0

Please sign in to comment.