Skip to content

Commit ade43ce

Browse files
authored
style: cleanup run_length_encoding.rs (#744)
* style: include `string_to_string` * style: reduce code duplication * style: empty input is a regular input
1 parent 2544d28 commit ade43ce

File tree

2 files changed

+20
-58
lines changed

2 files changed

+20
-58
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ std_instead_of_core = { level = "allow", priority = 1 }
140140
str_to_string = { level = "allow", priority = 1 }
141141
string_add = { level = "allow", priority = 1 }
142142
string_slice = { level = "allow", priority = 1 }
143-
string_to_string = { level = "allow", priority = 1 }
144143
undocumented_unsafe_blocks = { level = "allow", priority = 1 }
145144
unnecessary_safety_comment = { level = "allow", priority = 1 }
146145
unneeded_field_pattern = { level = "allow", priority = 1 }

src/string/run_length_encoding.rs

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub fn run_length_encoding(target: &str) -> String {
22
if target.trim().is_empty() {
3-
return "String is Empty!".to_string();
3+
return "".to_string();
44
}
55
let mut count: i32 = 0;
66
let mut base_character: String = "".to_string();
@@ -27,9 +27,8 @@ pub fn run_length_encoding(target: &str) -> String {
2727

2828
pub fn run_length_decoding(target: &str) -> String {
2929
if target.trim().is_empty() {
30-
return "String is Empty!".to_string();
30+
return "".to_string();
3131
}
32-
3332
let mut character_count: String = String::new();
3433
let mut decoded_target = String::new();
3534

@@ -55,61 +54,25 @@ pub fn run_length_decoding(target: &str) -> String {
5554
mod tests {
5655
use super::*;
5756

58-
#[test]
59-
fn encode_empty() {
60-
assert_eq!(run_length_encoding(""), "String is Empty!")
61-
}
62-
63-
#[test]
64-
fn encode_identical_character() {
65-
assert_eq!(run_length_encoding("aaaaaaaaaa"), "10a")
66-
}
67-
#[test]
68-
fn encode_continuous_character() {
69-
assert_eq!(run_length_encoding("abcdefghijk"), "1a1b1c1d1e1f1g1h1i1j1k")
70-
}
71-
72-
#[test]
73-
fn encode_random_character() {
74-
assert_eq!(run_length_encoding("aaaaabbbcccccdddddddddd"), "5a3b5c10d")
75-
}
76-
77-
#[test]
78-
fn encode_long_character() {
79-
assert_eq!(
80-
run_length_encoding(
81-
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbcccccdddddddddd"
82-
),
83-
"200a3b5c10d"
84-
)
85-
}
86-
#[test]
87-
fn decode_empty() {
88-
assert_eq!(run_length_decoding(""), "String is Empty!")
89-
}
90-
91-
#[test]
92-
fn decode_identical_character() {
93-
assert_eq!(run_length_decoding("10a"), "aaaaaaaaaa")
94-
}
95-
#[test]
96-
fn decode_continuous_character() {
97-
assert_eq!(run_length_decoding("1a1b1c1d1e1f1g1h1i1j1k"), "abcdefghijk")
98-
}
99-
100-
#[test]
101-
fn decode_random_character() {
102-
assert_eq!(
103-
run_length_decoding("5a3b5c10d").to_string(),
104-
"aaaaabbbcccccdddddddddd"
105-
)
57+
macro_rules! test_run_length {
58+
($($name:ident: $test_case:expr,)*) => {
59+
$(
60+
#[test]
61+
fn $name() {
62+
let (raw_str, encoded) = $test_case;
63+
assert_eq!(run_length_encoding(raw_str), encoded);
64+
assert_eq!(run_length_decoding(encoded), raw_str);
65+
}
66+
)*
67+
};
10668
}
10769

108-
#[test]
109-
fn decode_long_character() {
110-
assert_eq!(
111-
run_length_decoding("200a3b5c10d"),
112-
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbcccccdddddddddd"
113-
)
70+
test_run_length! {
71+
empty_input: ("", ""),
72+
repeated_char: ("aaaaaaaaaa", "10a"),
73+
no_repeated: ("abcdefghijk", "1a1b1c1d1e1f1g1h1i1j1k"),
74+
regular_input: ("aaaaabbbcccccdddddddddd", "5a3b5c10d"),
75+
two_blocks_with_same_char: ("aaabbaaaa", "3a2b4a"),
76+
long_input: ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbcccccdddddddddd", "200a3b5c10d"),
11477
}
11578
}

0 commit comments

Comments
 (0)