Skip to content

Commit

Permalink
value stability: test seq::SliceRandom
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Jan 14, 2020
1 parent a7a8cdc commit 08fff2c
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/seq/mod.rs
Expand Up @@ -567,6 +567,39 @@ mod test {
assert_eq!(v.choose_mut(&mut r), None);
}

#[test]
fn value_stability_slice() {
let mut r = crate::test::rng(413);
let chars = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
];
let mut nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

assert_eq!(chars.choose(&mut r), Some(&'l'));
assert_eq!(nums.choose_mut(&mut r), Some(&mut 10));

#[cfg(feature = "alloc")]
assert_eq!(
&chars
.choose_multiple(&mut r, 8)
.cloned()
.collect::<Vec<char>>(),
&['d', 'm', 'b', 'n', 'c', 'k', 'h', 'e']
);

#[cfg(feature = "alloc")]
assert_eq!(chars.choose_weighted(&mut r, |_| 1), Ok(&'f'));
#[cfg(feature = "alloc")]
assert_eq!(nums.choose_weighted_mut(&mut r, |_| 1), Ok(&mut 5));

nums.shuffle(&mut r);
assert_eq!(nums, [11, 12, 1, 2, 9, 3, 5, 0, 10, 6, 8, 7, 4]);
nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let res = nums.partial_shuffle(&mut r, 6);
assert_eq!(res.0, &mut [7, 5, 9, 6, 1, 8]);
assert_eq!(res.1, &mut [0, 11, 2, 3, 4, 12, 10]);
}

#[derive(Clone)]
struct UnhintedIterator<I: Iterator + Clone> {
iter: I,
Expand Down

0 comments on commit 08fff2c

Please sign in to comment.