mirror of
https://github.com/Magnus167/rustframe.git
synced 2025-08-20 04:00:01 +00:00
Add unit tests for SliceRandom trait and shuffle functionality
This commit is contained in:
parent
afcb29e716
commit
3f56b378b2
@ -14,3 +14,63 @@ impl<T> SliceRandom for [T] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use crate::random::{CryptoRng, Prng};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_shuffle_slice() {
|
||||||
|
let mut rng = Prng::new(3);
|
||||||
|
let mut arr = [1, 2, 3, 4, 5];
|
||||||
|
let orig = arr.clone();
|
||||||
|
arr.shuffle(&mut rng);
|
||||||
|
assert_eq!(arr.len(), orig.len());
|
||||||
|
let mut sorted = arr.to_vec();
|
||||||
|
sorted.sort();
|
||||||
|
assert_eq!(sorted, orig.to_vec());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_slice_shuffle_deterministic_with_prng() {
|
||||||
|
let mut rng1 = Prng::new(11);
|
||||||
|
let mut rng2 = Prng::new(11);
|
||||||
|
let mut a = [1u8, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||||
|
let mut b = a.clone();
|
||||||
|
a.shuffle(&mut rng1);
|
||||||
|
b.shuffle(&mut rng2);
|
||||||
|
assert_eq!(a, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_slice_shuffle_crypto_random_changes() {
|
||||||
|
let mut rng1 = CryptoRng::new();
|
||||||
|
let mut rng2 = CryptoRng::new();
|
||||||
|
let orig = [1u8, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||||
|
let mut a = orig.clone();
|
||||||
|
let mut b = orig.clone();
|
||||||
|
a.shuffle(&mut rng1);
|
||||||
|
b.shuffle(&mut rng2);
|
||||||
|
assert!(a != orig || b != orig, "Shuffles did not change order");
|
||||||
|
assert_ne!(a, b, "Two Crypto RNG shuffles produced same order");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_shuffle_single_element_no_change() {
|
||||||
|
let mut rng = Prng::new(1);
|
||||||
|
let mut arr = [42];
|
||||||
|
arr.shuffle(&mut rng);
|
||||||
|
assert_eq!(arr, [42]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_multiple_shuffles_different_results() {
|
||||||
|
let mut rng = Prng::new(5);
|
||||||
|
let mut arr1 = [1, 2, 3, 4];
|
||||||
|
let mut arr2 = [1, 2, 3, 4];
|
||||||
|
arr1.shuffle(&mut rng);
|
||||||
|
arr2.shuffle(&mut rng);
|
||||||
|
assert_ne!(arr1, arr2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user