Skip to content

Commit

Permalink
Test overflowed Permutations::size_hint
Browse files Browse the repository at this point in the history
Ideally, the lower bound of the size hint would remain `usize::MAX` but I don't see how we could make this happen.
Small bugfix: `n+1` might overflow.
  • Loading branch information
Philippe-Cholet committed Sep 5, 2023
1 parent c12692e commit 06abc3e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/permutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl CompleteState {
if n < k {
return Some(0);
}
(n - k + 1..n + 1).fold(Some(1), |acc, i| {
(n - k + 1..=n).fold(Some(1), |acc, i| {
acc.and_then(|acc| acc.checked_mul(i))
})
}
Expand Down
12 changes: 12 additions & 0 deletions tests/test_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,18 @@ fn permutations_range_count() {
}
}

#[test]
fn permutations_overflowed_size_hints() {
let mut it = std::iter::repeat(()).permutations(2);
assert_eq!(it.size_hint().0, usize::MAX);
assert_eq!(it.size_hint().1, None);
for nb_generated in 1..=1000 {
it.next();
assert!(it.size_hint().0 >= usize::MAX - nb_generated);
assert_eq!(it.size_hint().1, None);
}
}

#[test]
fn combinations_with_replacement() {
// Pool smaller than n
Expand Down

0 comments on commit 06abc3e

Please sign in to comment.