From 70562b87af32d2b9e62964835c940c6a045767fe Mon Sep 17 00:00:00 2001 From: Remo Senekowitsch Date: Mon, 20 Nov 2023 10:24:48 +0100 Subject: [PATCH] Sync run-length-encoding with problem-specifications (#1779) [no important files changed] --- .../run-length-encoding/.docs/instructions.md | 12 +-- .../.meta/test_template.tera | 18 ++++ .../run-length-encoding/.meta/tests.toml | 52 ++++++++++- .../tests/run-length-encoding.rs | 86 ++++++++++++------- rust-tooling/src/exercise_config.rs | 2 +- 5 files changed, 127 insertions(+), 43 deletions(-) create mode 100644 exercises/practice/run-length-encoding/.meta/test_template.tera diff --git a/exercises/practice/run-length-encoding/.docs/instructions.md b/exercises/practice/run-length-encoding/.docs/instructions.md index 95f7a9d69..fc8ce0569 100644 --- a/exercises/practice/run-length-encoding/.docs/instructions.md +++ b/exercises/practice/run-length-encoding/.docs/instructions.md @@ -2,8 +2,7 @@ Implement run-length encoding and decoding. -Run-length encoding (RLE) is a simple form of data compression, where runs -(consecutive data elements) are replaced by just one data value and count. +Run-length encoding (RLE) is a simple form of data compression, where runs (consecutive data elements) are replaced by just one data value and count. For example we can represent the original 53 characters with only 13. @@ -11,14 +10,11 @@ For example we can represent the original 53 characters with only 13. "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB" -> "12WB12W3B24WB" ``` -RLE allows the original data to be perfectly reconstructed from -the compressed data, which makes it a lossless data compression. +RLE allows the original data to be perfectly reconstructed from the compressed data, which makes it a lossless data compression. ```text "AABCCCDEEEE" -> "2AB3CD4E" -> "AABCCCDEEEE" ``` -For simplicity, you can assume that the unencoded string will only contain -the letters A through Z (either lower or upper case) and whitespace. This way -data to be encoded will never contain any numbers and numbers inside data to -be decoded always represent the count for the following character. +For simplicity, you can assume that the unencoded string will only contain the letters A through Z (either lower or upper case) and whitespace. +This way data to be encoded will never contain any numbers and numbers inside data to be decoded always represent the count for the following character. diff --git a/exercises/practice/run-length-encoding/.meta/test_template.tera b/exercises/practice/run-length-encoding/.meta/test_template.tera new file mode 100644 index 000000000..15e8ffe88 --- /dev/null +++ b/exercises/practice/run-length-encoding/.meta/test_template.tera @@ -0,0 +1,18 @@ +use run_length_encoding as rle; + +{% for test in cases %} +#[test] +{% if loop.index != 1 -%} +#[ignore] +{% endif -%} +fn {{ test.property }}_{{ test.description | slugify | replace(from="-", to="_") }}() { + let input = {{ test.input.string | json_encode() }}; + {% if test.property == "consistency" -%} + let output = rle::decode(&rle::encode(input)); + {%- else -%} + let output = rle::{{ test.property }}(input); + {%- endif %} + let expected = {{ test.expected | json_encode() }}; + assert_eq!(output, expected); +} +{% endfor -%} diff --git a/exercises/practice/run-length-encoding/.meta/tests.toml b/exercises/practice/run-length-encoding/.meta/tests.toml index be690e975..7bdb80867 100644 --- a/exercises/practice/run-length-encoding/.meta/tests.toml +++ b/exercises/practice/run-length-encoding/.meta/tests.toml @@ -1,3 +1,49 @@ -# 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. + +[ad53b61b-6ffc-422f-81a6-61f7df92a231] +description = "run-length encode a string -> empty string" + +[52012823-b7e6-4277-893c-5b96d42f82de] +description = "run-length encode a string -> single characters only are encoded without count" + +[b7868492-7e3a-415f-8da3-d88f51f80409] +description = "run-length encode a string -> string with no single characters" + +[859b822b-6e9f-44d6-9c46-6091ee6ae358] +description = "run-length encode a string -> single characters mixed with repeated characters" + +[1b34de62-e152-47be-bc88-469746df63b3] +description = "run-length encode a string -> multiple whitespace mixed in string" + +[abf176e2-3fbd-40ad-bb2f-2dd6d4df721a] +description = "run-length encode a string -> lowercase characters" + +[7ec5c390-f03c-4acf-ac29-5f65861cdeb5] +description = "run-length decode a string -> empty string" + +[ad23f455-1ac2-4b0e-87d0-b85b10696098] +description = "run-length decode a string -> single characters only" + +[21e37583-5a20-4a0e-826c-3dee2c375f54] +description = "run-length decode a string -> string with no single characters" + +[1389ad09-c3a8-4813-9324-99363fba429c] +description = "run-length decode a string -> single characters with repeated characters" + +[3f8e3c51-6aca-4670-b86c-a213bf4706b0] +description = "run-length decode a string -> multiple whitespace mixed in string" + +[29f721de-9aad-435f-ba37-7662df4fb551] +description = "run-length decode a string -> lowercase string" + +[2a762efd-8695-4e04-b0d6-9736899fbc16] +description = "encode and then decode -> encode followed by decode gives original string" diff --git a/exercises/practice/run-length-encoding/tests/run-length-encoding.rs b/exercises/practice/run-length-encoding/tests/run-length-encoding.rs index 5f8354738..4221b3a47 100644 --- a/exercises/practice/run-length-encoding/tests/run-length-encoding.rs +++ b/exercises/practice/run-length-encoding/tests/run-length-encoding.rs @@ -1,93 +1,117 @@ use run_length_encoding as rle; -// encoding tests - #[test] fn encode_empty_string() { - assert_eq!("", rle::encode("")); + let input = ""; + let output = rle::encode(input); + let expected = ""; + assert_eq!(output, expected); } #[test] #[ignore] -fn encode_single_characters() { - assert_eq!("XYZ", rle::encode("XYZ")); +fn encode_single_characters_only_are_encoded_without_count() { + let input = "XYZ"; + let output = rle::encode(input); + let expected = "XYZ"; + assert_eq!(output, expected); } #[test] #[ignore] fn encode_string_with_no_single_characters() { - assert_eq!("2A3B4C", rle::encode("AABBBCCCC")); + let input = "AABBBCCCC"; + let output = rle::encode(input); + let expected = "2A3B4C"; + assert_eq!(output, expected); } #[test] #[ignore] fn encode_single_characters_mixed_with_repeated_characters() { - assert_eq!( - "12WB12W3B24WB", - rle::encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB") - ); + let input = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"; + let output = rle::encode(input); + let expected = "12WB12W3B24WB"; + assert_eq!(output, expected); } #[test] #[ignore] fn encode_multiple_whitespace_mixed_in_string() { - assert_eq!("2 hs2q q2w2 ", rle::encode(" hsqq qww ")); + let input = " hsqq qww "; + let output = rle::encode(input); + let expected = "2 hs2q q2w2 "; + assert_eq!(output, expected); } #[test] #[ignore] fn encode_lowercase_characters() { - assert_eq!("2a3b4c", rle::encode("aabbbcccc")); + let input = "aabbbcccc"; + let output = rle::encode(input); + let expected = "2a3b4c"; + assert_eq!(output, expected); } -// decoding tests - #[test] #[ignore] fn decode_empty_string() { - assert_eq!("", rle::decode("")); + let input = ""; + let output = rle::decode(input); + let expected = ""; + assert_eq!(output, expected); } #[test] #[ignore] fn decode_single_characters_only() { - assert_eq!("XYZ", rle::decode("XYZ")); + let input = "XYZ"; + let output = rle::decode(input); + let expected = "XYZ"; + assert_eq!(output, expected); } #[test] #[ignore] fn decode_string_with_no_single_characters() { - assert_eq!("AABBBCCCC", rle::decode("2A3B4C")); + let input = "2A3B4C"; + let output = rle::decode(input); + let expected = "AABBBCCCC"; + assert_eq!(output, expected); } #[test] #[ignore] fn decode_single_characters_with_repeated_characters() { - assert_eq!( - "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB", - rle::decode("12WB12W3B24WB") - ); + let input = "12WB12W3B24WB"; + let output = rle::decode(input); + let expected = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"; + assert_eq!(output, expected); } #[test] #[ignore] fn decode_multiple_whitespace_mixed_in_string() { - assert_eq!(" hsqq qww ", rle::decode("2 hs2q q2w2 ")); + let input = "2 hs2q q2w2 "; + let output = rle::decode(input); + let expected = " hsqq qww "; + assert_eq!(output, expected); } #[test] #[ignore] -fn decode_lower_case_string() { - assert_eq!("aabbbcccc", rle::decode("2a3b4c")); +fn decode_lowercase_string() { + let input = "2a3b4c"; + let output = rle::decode(input); + let expected = "aabbbcccc"; + assert_eq!(output, expected); } -// consistency test - #[test] #[ignore] -fn consistency() { - assert_eq!( - "zzz ZZ zZ", - rle::decode(rle::encode("zzz ZZ zZ").as_str()) - ); +fn consistency_encode_followed_by_decode_gives_original_string() { + let input = "zzz ZZ zZ"; + let output = rle::decode(&rle::encode(input)); + let expected = "zzz ZZ zZ"; + assert_eq!(output, expected); } diff --git a/rust-tooling/src/exercise_config.rs b/rust-tooling/src/exercise_config.rs index 9ff9b6dae..c41d2807a 100644 --- a/rust-tooling/src/exercise_config.rs +++ b/rust-tooling/src/exercise_config.rs @@ -106,7 +106,7 @@ pub fn get_excluded_tests(slug: &str) -> Vec { let path = std::path::PathBuf::from("exercises/practice") .join(slug) .join(".meta/tests.toml"); - let contents = std::fs::read_to_string(&path).unwrap(); + let contents = std::fs::read_to_string(path).unwrap(); let mut excluded_tests = Vec::new();