1
use super::*;
2

            
3
const SMALL: Rect = Rect::new(0, 1, 80, 22);
4
const LARGE: Rect = Rect::new(0, 1, 120, 34);
5

            
6
13
fn covers_once(area: Rect, rects: &[Rect]) -> bool {
7
52
    let sum: u32 = rects.iter().map(|rect| rect.area()).sum();
8
13
    sum == area.area()
9
13
        && area
10
13
            .positions()
11
163440
            .all(|at| rects.iter().filter(|rect| rect.contains(at)).count() == 1)
12
13
}
13

            
14
#[test]
15
1
fn the_grid_of_each_count_is_tvs() {
16
1
    let grids: Vec<(u32, u32)> = (1..=10).map(grid).collect();
17
1
    assert_eq!(
18
1
        grids,
19
1
        [
20
1
            (1, 1),
21
1
            (1, 2),
22
1
            (1, 3),
23
1
            (2, 2),
24
1
            (2, 2),
25
1
            (2, 3),
26
1
            (2, 3),
27
1
            (2, 4),
28
1
            (3, 3),
29
1
            (3, 3)
30
1
        ]
31
    );
32
1
}
33

            
34
#[test]
35
1
fn tiles_cover_the_area_exactly_once() {
36
1
    for count in [1, 2, 3, 4, 5, 6, 10] {
37
7
        let rects = tile(count, LARGE).unwrap_or_default();
38
7
        assert_eq!(rects.len(), count, "{count} tiles");
39
7
        assert!(covers_once(LARGE, &rects), "{count} tiles: {rects:?}");
40
    }
41
1
    for count in 1..=6 {
42
6
        let rects = tile(count, SMALL).unwrap_or_default();
43
6
        assert!(covers_once(SMALL, &rects), "{count} tiles: {rects:?}");
44
    }
45
1
}
46

            
47
#[test]
48
1
fn five_tiles_give_the_right_column_a_row_more() {
49
1
    assert_eq!(
50
1
        tile(5, SMALL),
51
1
        Some(Vec::from([
52
1
            Rect::new(0, 1, 40, 11),
53
1
            Rect::new(0, 12, 40, 11),
54
1
            Rect::new(40, 1, 40, 7),
55
1
            Rect::new(40, 8, 40, 7),
56
1
            Rect::new(40, 15, 40, 8),
57
1
        ]))
58
    );
59
1
}
60

            
61
#[test]
62
1
fn ten_tiles_keep_the_minimum_height() {
63
1
    let rects = tile(10, SMALL).unwrap_or_default();
64
1
    assert_eq!(rects[0], Rect::new(0, 1, 26, 7));
65
1
    assert_eq!(rects[3], Rect::new(26, 1, 27, 7));
66
1
    assert_eq!(rects[6], Rect::new(53, 1, 27, 6));
67
1
    assert_eq!(rects[8], Rect::new(53, 12, 27, 6));
68
1
}
69

            
70
#[test]
71
1
fn a_cell_without_room_is_too_many_windows() {
72
1
    assert_eq!(tile(3, Rect::new(0, 0, 80, 2)), None);
73
1
    assert_eq!(tile(5, Rect::new(0, 0, 1, 24)), None);
74
1
}
75

            
76
#[test]
77
1
fn no_windows_tile_to_nothing() {
78
1
    assert_eq!(tile(0, SMALL), Some(Vec::new()));
79
1
}
80

            
81
#[test]
82
1
fn a_cascade_steps_down_and_right_towards_the_front() {
83
1
    assert_eq!(
84
1
        cascade(3, SMALL),
85
1
        Some(Vec::from([
86
1
            Rect::new(0, 1, 80, 22),
87
1
            Rect::new(1, 2, 79, 21),
88
1
            Rect::new(2, 3, 78, 20),
89
1
        ]))
90
    );
91
1
}
92

            
93
#[test]
94
1
fn a_cascade_keeps_the_bottom_right_corner() {
95
1
    let rects = cascade(6, LARGE).unwrap_or_default();
96
1
    assert_eq!(rects.len(), 6);
97
6
    assert!(rects.iter().all(|rect| rect.right() == LARGE.right()));
98
6
    assert!(rects.iter().all(|rect| rect.bottom() == LARGE.bottom()));
99
1
}
100

            
101
#[test]
102
1
fn a_cascade_too_deep_for_the_minimum_is_too_many_windows() {
103
1
    assert_eq!(cascade(7, Rect::new(0, 0, 80, 12)), None);
104
1
    assert!(cascade(6, Rect::new(0, 0, 80, 12)).is_some());
105
1
}