Merge branch 'idx' into 'main'

tor-circmgr: Fix random_idx_where with empty slice

Closes #918

See merge request tpo/core/arti!1296
This commit is contained in:
Ian Jackson 2023-06-23 13:12:22 +00:00
commit 03960b5048
1 changed files with 10 additions and 0 deletions

View File

@ -144,6 +144,9 @@ where
P: Fn(&T) -> bool,
{
let n_circuits = slice.len();
if n_circuits == 0 {
return None;
}
let shift = rng.gen_range(0..n_circuits);
(shift..n_circuits)
.chain(0..shift)
@ -183,4 +186,11 @@ mod test {
assert!(found[idx] == (num & 1 == 1));
}
}
#[test]
fn random_idx_empty() {
let mut rng = testing_rng();
let idx = random_idx_where(&mut rng, &[], |_: &i32| panic!());
assert_eq!(idx, None);
}
}