You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rust-puzzles/hostnames_allocator/tests/ranged_number_allocator_tes...

125 lines
3.6 KiB

extern crate hostnames_allocator;
use hostnames_allocator::ranged_number_allocator::RangedNumberAllocator;
fn create_allocator(data: &[(u32, u32)]) -> RangedNumberAllocator {
RangedNumberAllocator {
tree: data.to_owned().into_iter().collect()
}
}
fn check_allocator(allocator: RangedNumberAllocator, expected: &[(u32, u32)]) {
itertools::assert_equal(allocator.tree, expected.to_owned());
}
#[test]
fn remove_preserves_tree_when_empty() {
let mut allocator = create_allocator(&[]);
allocator.remove(5);
check_allocator(allocator, &[]);
}
#[test]
fn remove_preserves_tree_when_not_found() {
let mut allocator = create_allocator(&[(1, 3), (11, 13)]);
allocator.remove(5);
check_allocator(allocator, &[(1, 3), (11, 13)]);
}
#[test]
fn remove_removes_range_with_single_value() {
let mut allocator = create_allocator(&[(1, 3), (5, 5), (11, 13)]);
allocator.remove(5);
check_allocator(allocator, &[(1, 3), (11, 13)]);
}
#[test]
fn remove_replaces_range_starting_with_value() {
let mut allocator = create_allocator(&[(1, 3), (5, 7), (11, 13)]);
allocator.remove(5);
check_allocator(allocator, &[(1, 3), (6, 7), (11, 13)]);
}
#[test]
fn remove_changes_range_ending_with_value() {
let mut allocator = create_allocator(&[(1, 3), (5, 7), (11, 13)]);
allocator.remove(7);
check_allocator(allocator, &[(1, 3), (5, 6), (11, 13)]);
}
#[test]
fn remove_splits_range_containing_value() {
let mut allocator = create_allocator(&[(1, 3), (5, 9), (11, 13)]);
allocator.remove(7);
check_allocator(allocator, &[(1, 3), (5, 6), (8, 9), (11, 13)]);
}
#[test]
fn remove_removes_last_value() {
let mut allocator = create_allocator(&[(5, 5)]);
allocator.remove(5);
check_allocator(allocator, &[]);
}
#[test]
fn allocate_initializes_empty() {
let mut allocator = create_allocator(&[]);
assert_eq!(allocator.allocate(), 1);
check_allocator(allocator, &[(1, 1)]);
}
#[test]
fn allocate_adds_second_value() {
let mut allocator = create_allocator(&[(1, 1)]);
assert_eq!(allocator.allocate(), 2);
check_allocator(allocator, &[(1, 2)]);
}
#[test]
fn allocate_adds_tenth_value() {
let mut allocator = create_allocator(&[(1, 9)]);
assert_eq!(allocator.allocate(), 10);
check_allocator(allocator, &[(1, 10)]);
}
#[test]
fn allocate_adds_value_for_two_ranges_starting_with_one() {
let mut allocator = create_allocator(&[(1, 5), (10, 15)]);
assert_eq!(allocator.allocate(), 6);
check_allocator(allocator, &[(1, 6), (10, 15)]);
}
#[test]
fn allocate_combines_two_adjacent_ranges_starting_with_one() {
let mut allocator = create_allocator(&[(1, 5), (7, 15)]);
assert_eq!(allocator.allocate(), 6);
check_allocator(allocator, &[(1, 15)]);
}
#[test]
fn allocate_combines_two_adjacent_ranges_starting_with_one_extra() {
let mut allocator = create_allocator(&[(1, 5), (7, 15), (20, 30)]);
assert_eq!(allocator.allocate(), 6);
check_allocator(allocator, &[(1, 15), (20, 30)]);
}
#[test]
fn allocate_adds_value_for_range_starting_with_two() {
let mut allocator = create_allocator(&[(2, 10)]);
assert_eq!(allocator.allocate(), 1);
check_allocator(allocator, &[(1, 10)]);
}
#[test]
fn allocate_adds_value_for_range_starting_with_two_extra() {
let mut allocator = create_allocator(&[(2, 10), (20, 30)]);
assert_eq!(allocator.allocate(), 1);
check_allocator(allocator, &[(1, 10), (20, 30)]);
}
#[test]
fn allocate_adds_value_for_range_starting_with_ten() {
let mut allocator = create_allocator(&[(10, 20)]);
assert_eq!(allocator.allocate(), 1);
check_allocator(allocator, &[(1, 1), (10, 20)]);
}