Skip to content

Commit

Permalink
[InclusiveRange]: Allow size one ranges
Browse files Browse the repository at this point in the history
Removed the check that denied InclusiveRanges where start was
equal with end.
Added a test to check that allocation succeds when there is a size 1
range before or after the requested range.

Signed-off-by: AlexandruCihodaru <[email protected]>
  • Loading branch information
AlexandruCihodaru authored and alxiord committed Sep 13, 2022
1 parent 6f78066 commit e55eb23
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/address_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ mod tests {
);
}

#[test]
fn test_allow_range_size_one_left() {
let mut pool = AddressAllocator::new(1, 1000).unwrap();
assert_eq!(
pool.allocate(10, 2, AllocPolicy::FirstMatch).unwrap(),
RangeInclusive::new(2, 11).unwrap()
);
assert_eq!(
pool.allocate(1, 1, AllocPolicy::FirstMatch).unwrap(),
RangeInclusive::new(1, 1).unwrap()
);
}

#[test]
fn test_allocate_address_fail_free_and_realloc() {
let mut pool = AddressAllocator::new(0x0, 0x1000).unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl RangeInclusive {
// The length of the interval [0, u64::MAX] is u64::MAX + 1 which does
// not fit in a u64::MAX, hence we return `Error::InvalidRange` when
// there is an attempt to use that range.
if start >= end || (start == 0 && end == u64::MAX) {
if start > end || (start == 0 && end == u64::MAX) {
return Err(Error::InvalidRange(start, end));
}
Ok(RangeInclusive { start, end })
Expand Down Expand Up @@ -311,8 +311,8 @@ mod tests {
Error::InvalidRange(2, 1)
);
assert_eq!(
RangeInclusive::new(1, 1).unwrap_err(),
Error::InvalidRange(1, 1)
RangeInclusive::new(0, u64::MAX).unwrap_err(),
Error::InvalidRange(0, u64::MAX)
);
}

Expand Down

0 comments on commit e55eb23

Please sign in to comment.