diff options
Diffstat (limited to 'third_party/rust/fastrand/tests')
-rw-r--r-- | third_party/rust/fastrand/tests/char.rs | 44 | ||||
-rw-r--r-- | third_party/rust/fastrand/tests/smoke.rs | 120 |
2 files changed, 164 insertions, 0 deletions
diff --git a/third_party/rust/fastrand/tests/char.rs b/third_party/rust/fastrand/tests/char.rs new file mode 100644 index 0000000000..0f48c57708 --- /dev/null +++ b/third_party/rust/fastrand/tests/char.rs @@ -0,0 +1,44 @@ +use std::convert::TryFrom; +use std::ops::RangeBounds; + +fn test_char_coverage<R>(n: usize, range: R) +where + R: Iterator<Item = char> + RangeBounds<char> + Clone, +{ + use std::collections::HashSet; + + let all: HashSet<char> = range.clone().collect(); + let mut covered = HashSet::new(); + for _ in 0..n { + let c = fastrand::char(range.clone()); + assert!(all.contains(&c)); + covered.insert(c); + } + assert_eq!(covered, all); +} + +#[test] +fn test_char() { + // ASCII control chars. + let nul = 0u8 as char; + let soh = 1u8 as char; + let stx = 2u8 as char; + // Some undefined Hangul Jamo codepoints just before + // the surrogate area. + let last_jamo = char::try_from(0xd7ffu32).unwrap(); + let penultimate_jamo = char::try_from(last_jamo as u32 - 1).unwrap(); + // Private-use codepoints just after the surrogate area. + let first_private = char::try_from(0xe000u32).unwrap(); + let second_private = char::try_from(first_private as u32 + 1).unwrap(); + // Private-use codepoints at the end of Unicode space. + let last_private = std::char::MAX; + let penultimate_private = char::try_from(last_private as u32 - 1).unwrap(); + + test_char_coverage(100, nul..stx); + test_char_coverage(100, nul..=soh); + + test_char_coverage(400, penultimate_jamo..second_private); + test_char_coverage(400, penultimate_jamo..=second_private); + + test_char_coverage(100, penultimate_private..=last_private); +} diff --git a/third_party/rust/fastrand/tests/smoke.rs b/third_party/rust/fastrand/tests/smoke.rs new file mode 100644 index 0000000000..ff3cea66db --- /dev/null +++ b/third_party/rust/fastrand/tests/smoke.rs @@ -0,0 +1,120 @@ +#[cfg(all(target_family = "wasm", not(target_os = "wasi")))] +use wasm_bindgen_test::wasm_bindgen_test as test; + +#[cfg(all(target_family = "wasm", not(target_os = "wasi")))] +wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + +#[test] +fn bool() { + for x in &[false, true] { + while fastrand::bool() != *x {} + } +} + +#[test] +fn u8() { + for x in 0..10 { + while fastrand::u8(..10) != x {} + } + + for x in 200..=u8::MAX { + while fastrand::u8(200..) != x {} + } +} + +#[test] +fn i8() { + for x in -128..-120 { + while fastrand::i8(..-120) != x {} + } + + for x in 120..=127 { + while fastrand::i8(120..) != x {} + } +} + +#[test] +fn u32() { + for n in 1u32..10_000 { + let n = n.wrapping_mul(n); + let n = n.wrapping_mul(n); + if n != 0 { + for _ in 0..1000 { + assert!(fastrand::u32(..n) < n); + } + } + } +} + +#[test] +fn u64() { + for n in 1u64..10_000 { + let n = n.wrapping_mul(n); + let n = n.wrapping_mul(n); + let n = n.wrapping_mul(n); + if n != 0 { + for _ in 0..1000 { + assert!(fastrand::u64(..n) < n); + } + } + } +} + +#[test] +fn u128() { + for n in 1u128..10_000 { + let n = n.wrapping_mul(n); + let n = n.wrapping_mul(n); + let n = n.wrapping_mul(n); + let n = n.wrapping_mul(n); + if n != 0 { + for _ in 0..1000 { + assert!(fastrand::u128(..n) < n); + } + } + } +} + +#[test] +fn fill() { + let r = fastrand::Rng::new(); + let mut a = [0u8; 64]; + let mut b = [0u8; 64]; + + r.fill(&mut a); + r.fill(&mut b); + + assert_ne!(a, b); +} + +#[test] +fn rng() { + let r = fastrand::Rng::new(); + + assert_ne!(r.u64(..), r.u64(..)); + + r.seed(7); + let a = r.u64(..); + r.seed(7); + let b = r.u64(..); + assert_eq!(a, b); +} + +#[test] +fn rng_init() { + let a = fastrand::Rng::new(); + let b = fastrand::Rng::new(); + assert_ne!(a.u64(..), b.u64(..)); + + a.seed(7); + b.seed(7); + assert_eq!(a.u64(..), b.u64(..)); +} + +#[test] +fn with_seed() { + let a = fastrand::Rng::with_seed(7); + let b = fastrand::Rng::new(); + b.seed(7); + assert_eq!(a.u64(..), b.u64(..)); +} |