Skip to content

Commit cbd30e7

Browse files
committed
chore: test shuffle with first
1 parent 9921f4c commit cbd30e7

File tree

1 file changed

+73
-22
lines changed

1 file changed

+73
-22
lines changed

connect/src/shuffle_vec.rs

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,14 @@ impl<T> From<Vec<T>> for ShuffleVec<T> {
5656
}
5757

5858
impl<T> ShuffleVec<T> {
59-
pub fn shuffle_with_seed<F: Fn(&T) -> bool>(
60-
&mut self,
61-
seed: u64,
62-
is_first: F,
63-
) -> Option<usize> {
59+
pub fn shuffle_with_seed<F: Fn(&T) -> bool>(&mut self, seed: u64, is_first: F) {
6460
self.shuffle_with_rng(SmallRng::seed_from_u64(seed), is_first)
6561
}
6662

67-
pub fn shuffle_with_rng<F: Fn(&T) -> bool>(
68-
&mut self,
69-
mut rng: impl Rng,
70-
is_first: F,
71-
) -> Option<usize> {
72-
if self.vec.len() < 3 {
73-
info!("skipped shuffling for less than three items");
74-
return None;
63+
pub fn shuffle_with_rng<F: Fn(&T) -> bool>(&mut self, mut rng: impl Rng, is_first: F) {
64+
if self.vec.len() <= 1 {
65+
info!("skipped shuffling for less or equal one item");
66+
return;
7567
}
7668

7769
if self.indices.is_some() {
@@ -95,8 +87,6 @@ impl<T> ShuffleVec<T> {
9587
if let Some(first_pos) = self.original_first_position {
9688
self.vec.swap(0, first_pos)
9789
}
98-
99-
self.original_first_position
10090
}
10191

10292
pub fn unshuffle(&mut self) {
@@ -123,25 +113,86 @@ impl<T> ShuffleVec<T> {
123113
mod test {
124114
use super::*;
125115
use rand::Rng;
116+
use std::ops::Range;
126117

127-
#[test]
128-
fn test_shuffle_with_seed() {
129-
let seed = rand::rng().random_range(0..10000000000000);
118+
fn base(range: Range<usize>) -> (ShuffleVec<usize>, u64) {
119+
let seed = rand::rng().random_range(0..10_000_000_000_000);
120+
121+
let vec = range.collect::<Vec<_>>();
122+
(vec.into(), seed)
123+
}
130124

131-
let vec = (0..100).collect::<Vec<_>>();
132-
let base_vec: ShuffleVec<i32> = vec.into();
125+
#[test]
126+
fn test_shuffle_without_first() {
127+
let (base_vec, seed) = base(0..100);
133128

134129
let mut shuffled_vec = base_vec.clone();
135130
shuffled_vec.shuffle_with_seed(seed, |_| false);
136131

137132
let mut different_shuffled_vec = base_vec.clone();
138133
different_shuffled_vec.shuffle_with_seed(seed, |_| false);
139134

140-
assert_eq!(shuffled_vec, different_shuffled_vec);
135+
assert_eq!(
136+
shuffled_vec, different_shuffled_vec,
137+
"shuffling with the same seed has the same result"
138+
);
141139

142140
let mut unshuffled_vec = shuffled_vec.clone();
143141
unshuffled_vec.unshuffle();
144142

145-
assert_eq!(base_vec, unshuffled_vec);
143+
assert_eq!(
144+
base_vec, unshuffled_vec,
145+
"unshuffle restores the original state"
146+
);
147+
}
148+
149+
#[test]
150+
fn test_shuffle_with_first() {
151+
const MAX_RANGE: usize = 200;
152+
153+
let (base_vec, seed) = base(0..MAX_RANGE);
154+
let rand_first = rand::rng().random_range(0..MAX_RANGE);
155+
156+
let mut shuffled_with_first = base_vec.clone();
157+
shuffled_with_first.shuffle_with_seed(seed, |i| i == &rand_first);
158+
159+
assert_eq!(
160+
Some(&rand_first),
161+
shuffled_with_first.first(),
162+
"after shuffling the first is expected to be the given item"
163+
);
164+
165+
let mut shuffled_without_first = base_vec.clone();
166+
shuffled_without_first.shuffle_with_seed(seed, |_| false);
167+
168+
let mut switched_positions = Vec::with_capacity(2);
169+
for (i, without_first_value) in shuffled_without_first.iter().enumerate() {
170+
if without_first_value != &shuffled_with_first[i] {
171+
switched_positions.push(i);
172+
} else {
173+
assert_eq!(
174+
without_first_value, &shuffled_with_first[i],
175+
"shuffling with the same seed has the same result"
176+
);
177+
}
178+
}
179+
180+
assert_eq!(
181+
switched_positions.len(),
182+
2,
183+
"only the switched positions should be different"
184+
);
185+
186+
assert_eq!(
187+
shuffled_with_first[switched_positions[0]],
188+
shuffled_without_first[switched_positions[1]],
189+
"the switched values should be equal"
190+
);
191+
192+
assert_eq!(
193+
shuffled_with_first[switched_positions[1]],
194+
shuffled_without_first[switched_positions[0]],
195+
"the switched values should be equal"
196+
)
146197
}
147198
}

0 commit comments

Comments
 (0)