Lines
100 %
Functions
Branches
use super::*;
const SMALL: Rect = Rect::new(0, 1, 80, 22);
const LARGE: Rect = Rect::new(0, 1, 120, 34);
fn covers_once(area: Rect, rects: &[Rect]) -> bool {
let sum: u32 = rects.iter().map(|rect| rect.area()).sum();
sum == area.area()
&& area
.positions()
.all(|at| rects.iter().filter(|rect| rect.contains(at)).count() == 1)
}
#[test]
fn the_grid_of_each_count_is_tvs() {
let grids: Vec<(u32, u32)> = (1..=10).map(grid).collect();
assert_eq!(
grids,
[
(1, 1),
(1, 2),
(1, 3),
(2, 2),
(2, 3),
(2, 4),
(3, 3),
(3, 3)
]
);
fn tiles_cover_the_area_exactly_once() {
for count in [1, 2, 3, 4, 5, 6, 10] {
let rects = tile(count, LARGE).unwrap_or_default();
assert_eq!(rects.len(), count, "{count} tiles");
assert!(covers_once(LARGE, &rects), "{count} tiles: {rects:?}");
for count in 1..=6 {
let rects = tile(count, SMALL).unwrap_or_default();
assert!(covers_once(SMALL, &rects), "{count} tiles: {rects:?}");
fn five_tiles_give_the_right_column_a_row_more() {
tile(5, SMALL),
Some(Vec::from([
Rect::new(0, 1, 40, 11),
Rect::new(0, 12, 40, 11),
Rect::new(40, 1, 40, 7),
Rect::new(40, 8, 40, 7),
Rect::new(40, 15, 40, 8),
]))
fn ten_tiles_keep_the_minimum_height() {
let rects = tile(10, SMALL).unwrap_or_default();
assert_eq!(rects[0], Rect::new(0, 1, 26, 7));
assert_eq!(rects[3], Rect::new(26, 1, 27, 7));
assert_eq!(rects[6], Rect::new(53, 1, 27, 6));
assert_eq!(rects[8], Rect::new(53, 12, 27, 6));
fn a_cell_without_room_is_too_many_windows() {
assert_eq!(tile(3, Rect::new(0, 0, 80, 2)), None);
assert_eq!(tile(5, Rect::new(0, 0, 1, 24)), None);
fn no_windows_tile_to_nothing() {
assert_eq!(tile(0, SMALL), Some(Vec::new()));
fn a_cascade_steps_down_and_right_towards_the_front() {
cascade(3, SMALL),
Rect::new(0, 1, 80, 22),
Rect::new(1, 2, 79, 21),
Rect::new(2, 3, 78, 20),
fn a_cascade_keeps_the_bottom_right_corner() {
let rects = cascade(6, LARGE).unwrap_or_default();
assert_eq!(rects.len(), 6);
assert!(rects.iter().all(|rect| rect.right() == LARGE.right()));
assert!(rects.iter().all(|rect| rect.bottom() == LARGE.bottom()));
fn a_cascade_too_deep_for_the_minimum_is_too_many_windows() {
assert_eq!(cascade(7, Rect::new(0, 0, 80, 12)), None);
assert!(cascade(6, Rect::new(0, 0, 80, 12)).is_some());