From bf2f49bc191472171221382cf642bbec1e1d63ed Mon Sep 17 00:00:00 2001 From: Remo Senekowitsch Date: Mon, 13 Nov 2023 13:48:00 +0100 Subject: [PATCH] Sync spiral-matrix with problem-specifications --- .../spiral-matrix/.docs/instructions.md | 5 +- .../practice/spiral-matrix/.meta/config.json | 2 +- .../spiral-matrix/.meta/test_template.tera | 16 ++++ .../practice/spiral-matrix/.meta/tests.toml | 28 ++++++- .../spiral-matrix/tests/spiral-matrix.rs | 74 ++++++++++--------- 5 files changed, 85 insertions(+), 40 deletions(-) create mode 100644 exercises/practice/spiral-matrix/.meta/test_template.tera diff --git a/exercises/practice/spiral-matrix/.docs/instructions.md b/exercises/practice/spiral-matrix/.docs/instructions.md index af412dd83..ba99e12c7 100644 --- a/exercises/practice/spiral-matrix/.docs/instructions.md +++ b/exercises/practice/spiral-matrix/.docs/instructions.md @@ -2,11 +2,10 @@ Given the size, return a square matrix of numbers in spiral order. -The matrix should be filled with natural numbers, starting from 1 -in the top-left corner, increasing in an inward, clockwise spiral order, -like these examples: +The matrix should be filled with natural numbers, starting from 1 in the top-left corner, increasing in an inward, clockwise spiral order, like these examples: ## Examples + ### Spiral matrix of size 3 ```text diff --git a/exercises/practice/spiral-matrix/.meta/config.json b/exercises/practice/spiral-matrix/.meta/config.json index 99b94dacf..0e2997e71 100644 --- a/exercises/practice/spiral-matrix/.meta/config.json +++ b/exercises/practice/spiral-matrix/.meta/config.json @@ -26,7 +26,7 @@ ".meta/example.rs" ] }, - "blurb": " Given the size, return a square matrix of numbers in spiral order.", + "blurb": "Given the size, return a square matrix of numbers in spiral order.", "source": "Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension.", "source_url": "https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/" } diff --git a/exercises/practice/spiral-matrix/.meta/test_template.tera b/exercises/practice/spiral-matrix/.meta/test_template.tera new file mode 100644 index 000000000..ab4e24399 --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/test_template.tera @@ -0,0 +1,16 @@ +{% for test in cases %} +#[test] +{% if loop.index != 1 -%} +#[ignore] +{% endif -%} +fn {{ test.description | slugify | replace(from="-", to="_") }}() { + let input = {{ test.input.size | json_encode() }}; + let output = {{ crate_name }}::{{ fn_names[0] }}(input); + let expected: [[u32; {{ test.input.size }}]; {{ test.input.size }}] = [ + {% for i in test.expected %} + {{ i | json_encode }}, + {% endfor %} + ]; + assert_eq!(output, expected); +} +{% endfor -%} diff --git a/exercises/practice/spiral-matrix/.meta/tests.toml b/exercises/practice/spiral-matrix/.meta/tests.toml index cb988949d..9ac5bacaa 100644 --- a/exercises/practice/spiral-matrix/.meta/tests.toml +++ b/exercises/practice/spiral-matrix/.meta/tests.toml @@ -1,6 +1,28 @@ -# 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. [8f584201-b446-4bc9-b132-811c8edd9040] description = "empty spiral" + +[e40ae5f3-e2c9-4639-8116-8a119d632ab2] +description = "trivial spiral" + +[cf05e42d-eb78-4098-a36e-cdaf0991bc48] +description = "spiral of size 2" + +[1c475667-c896-4c23-82e2-e033929de939] +description = "spiral of size 3" + +[05ccbc48-d891-44f5-9137-f4ce462a759d] +description = "spiral of size 4" + +[f4d2165b-1738-4e0c-bed0-c459045ae50d] +description = "spiral of size 5" diff --git a/exercises/practice/spiral-matrix/tests/spiral-matrix.rs b/exercises/practice/spiral-matrix/tests/spiral-matrix.rs index 0c957b9b5..42c127edb 100644 --- a/exercises/practice/spiral-matrix/tests/spiral-matrix.rs +++ b/exercises/practice/spiral-matrix/tests/spiral-matrix.rs @@ -1,55 +1,63 @@ -use spiral_matrix::*; - #[test] fn empty_spiral() { - let expected: Vec> = Vec::new(); - assert_eq!(spiral_matrix(0), expected); + let input = 0; + let output = spiral_matrix::spiral_matrix(input); + let expected: [[u32; 0]; 0] = []; + assert_eq!(output, expected); } #[test] #[ignore] -fn size_one_spiral() { - let expected: Vec> = vec![vec![1]]; - assert_eq!(spiral_matrix(1), expected); +fn trivial_spiral() { + let input = 1; + let output = spiral_matrix::spiral_matrix(input); + let expected: [[u32; 1]; 1] = [[1]]; + assert_eq!(output, expected); } + #[test] #[ignore] -fn size_two_spiral() { - let expected: Vec> = vec![vec![1, 2], vec![4, 3]]; - assert_eq!(spiral_matrix(2), expected); +fn spiral_of_size_2() { + let input = 2; + let output = spiral_matrix::spiral_matrix(input); + let expected: [[u32; 2]; 2] = [[1, 2], [4, 3]]; + assert_eq!(output, expected); } #[test] #[ignore] -fn size_three_spiral() { - #[rustfmt::skip] - let expected: Vec> = vec![ - vec![1, 2, 3], - vec![8, 9, 4], - vec![7, 6, 5], - ]; - assert_eq!(spiral_matrix(3), expected); +fn spiral_of_size_3() { + let input = 3; + let output = spiral_matrix::spiral_matrix(input); + let expected: [[u32; 3]; 3] = [[1, 2, 3], [8, 9, 4], [7, 6, 5]]; + assert_eq!(output, expected); } + #[test] #[ignore] -fn size_four_spiral() { - let expected: Vec> = vec![ - vec![1, 2, 3, 4], - vec![12, 13, 14, 5], - vec![11, 16, 15, 6], - vec![10, 9, 8, 7], +fn spiral_of_size_4() { + let input = 4; + let output = spiral_matrix::spiral_matrix(input); + let expected: [[u32; 4]; 4] = [ + [1, 2, 3, 4], + [12, 13, 14, 5], + [11, 16, 15, 6], + [10, 9, 8, 7], ]; - assert_eq!(spiral_matrix(4), expected); + assert_eq!(output, expected); } + #[test] #[ignore] -fn size_five_spiral() { - let expected: Vec> = vec![ - vec![1, 2, 3, 4, 5], - vec![16, 17, 18, 19, 6], - vec![15, 24, 25, 20, 7], - vec![14, 23, 22, 21, 8], - vec![13, 12, 11, 10, 9], +fn spiral_of_size_5() { + let input = 5; + let output = spiral_matrix::spiral_matrix(input); + let expected: [[u32; 5]; 5] = [ + [1, 2, 3, 4, 5], + [16, 17, 18, 19, 6], + [15, 24, 25, 20, 7], + [14, 23, 22, 21, 8], + [13, 12, 11, 10, 9], ]; - assert_eq!(spiral_matrix(5), expected); + assert_eq!(output, expected); }