diff options
Diffstat (limited to 'rust/minesweeper/tests/minesweeper.rs')
-rw-r--r-- | rust/minesweeper/tests/minesweeper.rs | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/rust/minesweeper/tests/minesweeper.rs b/rust/minesweeper/tests/minesweeper.rs new file mode 100644 index 0000000..c27bbd5 --- /dev/null +++ b/rust/minesweeper/tests/minesweeper.rs | |||
@@ -0,0 +1,141 @@ | |||
1 | use minesweeper::annotate; | ||
2 | |||
3 | fn remove_annotations(board: &[&str]) -> Vec<String> { | ||
4 | board.iter().map(|r| remove_annotations_in_row(r)).collect() | ||
5 | } | ||
6 | |||
7 | fn remove_annotations_in_row(row: &str) -> String { | ||
8 | row.chars() | ||
9 | .map(|ch| match ch { | ||
10 | '*' => '*', | ||
11 | _ => ' ', | ||
12 | }) | ||
13 | .collect() | ||
14 | } | ||
15 | |||
16 | fn run_test(test_case: &[&str]) { | ||
17 | let cleaned = remove_annotations(test_case); | ||
18 | let cleaned_strs = cleaned.iter().map(|r| &r[..]).collect::<Vec<_>>(); | ||
19 | let expected = test_case.iter().map(|&r| r.to_string()).collect::<Vec<_>>(); | ||
20 | assert_eq!(expected, annotate(&cleaned_strs)); | ||
21 | } | ||
22 | |||
23 | #[test] | ||
24 | fn no_rows() { | ||
25 | #[rustfmt::skip] | ||
26 | run_test(&[ | ||
27 | ]); | ||
28 | } | ||
29 | |||
30 | #[test] | ||
31 | fn no_columns() { | ||
32 | #[rustfmt::skip] | ||
33 | run_test(&[ | ||
34 | "", | ||
35 | ]); | ||
36 | } | ||
37 | |||
38 | #[test] | ||
39 | fn no_mines() { | ||
40 | #[rustfmt::skip] | ||
41 | run_test(&[ | ||
42 | " ", | ||
43 | " ", | ||
44 | " ", | ||
45 | ]); | ||
46 | } | ||
47 | |||
48 | #[test] | ||
49 | fn board_with_only_mines() { | ||
50 | #[rustfmt::skip] | ||
51 | run_test(&[ | ||
52 | "***", | ||
53 | "***", | ||
54 | "***", | ||
55 | ]); | ||
56 | } | ||
57 | |||
58 | #[test] | ||
59 | fn mine_surrounded_by_spaces() { | ||
60 | #[rustfmt::skip] | ||
61 | run_test(&[ | ||
62 | "111", | ||
63 | "1*1", | ||
64 | "111", | ||
65 | ]); | ||
66 | } | ||
67 | |||
68 | #[test] | ||
69 | fn space_surrounded_by_mines() { | ||
70 | #[rustfmt::skip] | ||
71 | run_test(&[ | ||
72 | "***", | ||
73 | "*8*", | ||
74 | "***", | ||
75 | ]); | ||
76 | } | ||
77 | |||
78 | #[test] | ||
79 | fn horizontal_line() { | ||
80 | #[rustfmt::skip] | ||
81 | run_test(&[ | ||
82 | "1*2*1", | ||
83 | ]); | ||
84 | } | ||
85 | |||
86 | #[test] | ||
87 | fn horizontal_line_mines_at_edges() { | ||
88 | #[rustfmt::skip] | ||
89 | run_test(&[ | ||
90 | "*1 1*", | ||
91 | ]); | ||
92 | } | ||
93 | |||
94 | #[test] | ||
95 | fn vertical_line() { | ||
96 | #[rustfmt::skip] | ||
97 | run_test(&[ | ||
98 | "1", | ||
99 | "*", | ||
100 | "2", | ||
101 | "*", | ||
102 | "1", | ||
103 | ]); | ||
104 | } | ||
105 | |||
106 | #[test] | ||
107 | fn vertical_line_mines_at_edges() { | ||
108 | #[rustfmt::skip] | ||
109 | run_test(&[ | ||
110 | "*", | ||
111 | "1", | ||
112 | " ", | ||
113 | "1", | ||
114 | "*", | ||
115 | ]); | ||
116 | } | ||
117 | |||
118 | #[test] | ||
119 | fn cross() { | ||
120 | #[rustfmt::skip] | ||
121 | run_test(&[ | ||
122 | " 2*2 ", | ||
123 | "25*52", | ||
124 | "*****", | ||
125 | "25*52", | ||
126 | " 2*2 ", | ||
127 | ]); | ||
128 | } | ||
129 | |||
130 | #[test] | ||
131 | fn large_board() { | ||
132 | #[rustfmt::skip] | ||
133 | run_test(&[ | ||
134 | "1*22*1", | ||
135 | "12*322", | ||
136 | " 123*2", | ||
137 | "112*4*", | ||
138 | "1*22*2", | ||
139 | "111111", | ||
140 | ]); | ||
141 | } | ||