diff --git a/connect/src/shuffle_vec.rs b/connect/src/shuffle_vec.rs index 595a392ba..7a4b6f1bf 100644 --- a/connect/src/shuffle_vec.rs +++ b/connect/src/shuffle_vec.rs @@ -146,8 +146,7 @@ mod test { ); } - #[test] - fn test_shuffle_with_first() { + fn get_shuffle_vec_without_and_with_first() -> (ShuffleVec, ShuffleVec) { const MAX_RANGE: usize = 200; let (base_vec, seed) = base(0..MAX_RANGE); @@ -165,13 +164,36 @@ mod test { let mut shuffled_without_first = base_vec.clone(); shuffled_without_first.shuffle_with_seed(seed, |_| false); + (shuffled_without_first, shuffled_with_first) + } + + #[test] + fn test_shuffle_with_first() { + const MAX_VEC_RECREATION: usize = 20; + + let mut prevent_running_indefinitely = 0; + let (shuffled_without_first, shuffled_with_first) = loop { + let (vec1, vec2) = get_shuffle_vec_without_and_with_first(); + if vec1 != vec2 { + break (vec1, vec2); + } + + // vec without and with first is equal, aka the vec first item is the same value as + // the randomly determined first values, we just create another set of two shuffled vec + assert!( + prevent_running_indefinitely < MAX_VEC_RECREATION, + "failed to retrieve two different vector after {MAX_VEC_RECREATION}" + ); + prevent_running_indefinitely += 1; + }; + let mut switched_positions = Vec::with_capacity(2); - for (i, without_first_value) in shuffled_without_first.iter().enumerate() { - if without_first_value != &shuffled_with_first[i] { + for (i, shuffled_without_first_value) in shuffled_without_first.iter().enumerate() { + if shuffled_without_first_value != &shuffled_with_first[i] { switched_positions.push(i); } else { assert_eq!( - without_first_value, &shuffled_with_first[i], + shuffled_without_first_value, &shuffled_with_first[i], "shuffling with the same seed has the same result" ); }