Skip to content

Commit

Permalink
minesweeper: sync
Browse files Browse the repository at this point in the history
part of #1824
  • Loading branch information
senekor committed Aug 16, 2024
1 parent e5968cc commit eb38ae5
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 50 deletions.
33 changes: 33 additions & 0 deletions exercises/practice/minesweeper/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use minesweeper::*;

{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | make_ident }}() {
{% if test.input.minefield | length < 2 -%}
let input = &[
{%- for line in test.input.minefield %}
{{ line | json_encode() }},
{%- endfor %}
];
let expected{% if test.expected | length == 0 %}: &[&str]{% endif %} = &[
{%- for line in test.expected %}
{{ line | json_encode() }},
{%- endfor %}
];
{% else -%}
#[rustfmt::skip]
let (input, expected) = (&[
{%- for line in test.input.minefield %}
{{ line | json_encode() }},
{%- endfor %}
], &[
{%- for line in test.expected %}
{{ line | json_encode() }},
{%- endfor %}
]);
{% endif -%}
let actual = annotate(input);
assert_eq!(actual, expected);
}
{% endfor -%}
19 changes: 16 additions & 3 deletions exercises/practice/minesweeper/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[0c5ec4bd-dea7-4138-8651-1203e1cb9f44]
description = "no rows"
Expand All @@ -11,6 +18,9 @@ description = "no columns"
[6fbf8f6d-a03b-42c9-9a58-b489e9235478]
description = "no mines"

[61aff1c4-fb31-4078-acad-cd5f1e635655]
description = "minefield with only mines"

[84167147-c504-4896-85d7-246b01dea7c5]
description = "mine surrounded by spaces"

Expand All @@ -31,3 +41,6 @@ description = "vertical line, mines at edges"

[4b098563-b7f3-401c-97c6-79dd1b708f34]
description = "cross"

[04a260f1-b40a-4e89-839e-8dd8525abe0e]
description = "large minefield"
131 changes: 84 additions & 47 deletions exercises/practice/minesweeper/tests/minesweeper.rs
Original file line number Diff line number Diff line change
@@ -1,153 +1,190 @@
use minesweeper::annotate;

fn remove_annotations(board: &[&str]) -> Vec<String> {
board.iter().map(|r| remove_annotations_in_row(r)).collect()
}

fn remove_annotations_in_row(row: &str) -> String {
row.as_bytes()
.iter()
.map(|&ch| match ch {
b'*' => '*',
_ => ' ',
})
.collect()
}

fn run_test(test_case: &[&str]) {
let cleaned = remove_annotations(test_case);
let cleaned_strs = cleaned.iter().map(|r| &r[..]).collect::<Vec<_>>();
let expected = test_case.iter().map(|&r| r.to_string()).collect::<Vec<_>>();
assert_eq!(expected, annotate(&cleaned_strs));
}
use minesweeper::*;

#[test]
fn no_rows() {
#[rustfmt::skip]
run_test(&[
]);
let input = &[];
let expected: &[&str] = &[];
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn no_columns() {
#[rustfmt::skip]
run_test(&[
"",
]);
let input = &[""];
let expected = &[""];
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn no_mines() {
#[rustfmt::skip]
run_test(&[
let (input, expected) = (&[
" ",
" ",
" ",
], &[
" ",
" ",
" ",
]);
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn board_with_only_mines() {
fn minefield_with_only_mines() {
#[rustfmt::skip]
run_test(&[
let (input, expected) = (&[
"***",
"***",
"***",
], &[
"***",
"***",
"***",
]);
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn mine_surrounded_by_spaces() {
#[rustfmt::skip]
run_test(&[
let (input, expected) = (&[
" ",
" * ",
" ",
], &[
"111",
"1*1",
"111",
]);
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn space_surrounded_by_mines() {
#[rustfmt::skip]
run_test(&[
let (input, expected) = (&[
"***",
"* *",
"***",
], &[
"***",
"*8*",
"***",
]);
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn horizontal_line() {
#[rustfmt::skip]
run_test(&[
"1*2*1",
]);
let input = &[" * * "];
let expected = &["1*2*1"];
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn horizontal_line_mines_at_edges() {
#[rustfmt::skip]
run_test(&[
"*1 1*",
]);
let input = &["* *"];
let expected = &["*1 1*"];
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn vertical_line() {
#[rustfmt::skip]
run_test(&[
let (input, expected) = (&[
" ",
"*",
" ",
"*",
" ",
], &[
"1",
"*",
"2",
"*",
"1",
]);
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn vertical_line_mines_at_edges() {
#[rustfmt::skip]
run_test(&[
let (input, expected) = (&[
"*",
" ",
" ",
" ",
"*",
], &[
"*",
"1",
" ",
"1",
"*",
]);
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn cross() {
#[rustfmt::skip]
run_test(&[
let (input, expected) = (&[
" * ",
" * ",
"*****",
" * ",
" * ",
], &[
" 2*2 ",
"25*52",
"*****",
"25*52",
" 2*2 ",
]);
let actual = annotate(input);
assert_eq!(actual, expected);
}

#[test]
#[ignore]
fn large_board() {
fn large_minefield() {
#[rustfmt::skip]
run_test(&[
let (input, expected) = (&[
" * * ",
" * ",
" * ",
" * *",
" * * ",
" ",
], &[
"1*22*1",
"12*322",
" 123*2",
"112*4*",
"1*22*2",
"111111",
]);
let actual = annotate(input);
assert_eq!(actual, expected);
}

0 comments on commit eb38ae5

Please sign in to comment.