1
Fork 0

Replace manual tests with test_case.

This commit is contained in:
Bauke 2022-04-08 22:53:04 +02:00
parent b5b0d152b8
commit 491e7c743a
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
25 changed files with 173 additions and 184 deletions

View File

@ -7,3 +7,6 @@ edition = "2021"
[lib] [lib]
path = "source/lib.rs" path = "source/lib.rs"
[dev-dependencies]
test-case = "2.0.2"

View File

@ -59,41 +59,3 @@ pub fn add_binary(a: String, b: String) -> String {
sum.chars().rev().collect() sum.chars().rev().collect()
} }
#[test]
fn test_add_binary() {
assert_eq!(add_binary("11".to_string(), "1".to_string()), "100");
assert_eq!(add_binary("1010".to_string(), "1011".to_string()), "10101");
assert_eq!(add_binary("1111".to_string(), "1111".to_string()), "11110");
assert_eq!(
add_binary(
"10110101010001101011001011100011\
10100111101001010011000011000100\
11110111001001111010110000000010\
11100100110010110111101001100010\
10000101100010101010000111100100\
10100110010010100110111010011001\
11110111011101010000000110101001\
10000101110010101101011011011000"
.to_string(),
"11101111111010110101001000000110\
10010101100011001110011100101111\
10011111011100000010100111010100\
11101111111010111010011111001011\
00000001001010111100100000010010\
10110001010101011100001100000010\
11010000010010011101100000000111\
10000100111011110110000001110100"
.to_string()
),
"11010010100110010000001001110101\
00011110100110010000101111111010\
01001011010010111110101011101011\
11101010010110111001000100010110\
11000011010110110011010011111011\
10101011110100000001100011001110\
01100011110111110110110011011000\
10000101010111010001101110100110\
0"
);
}

View File

@ -5,10 +5,3 @@ pub fn str_str(haystack: String, needle: String) -> i32 {
haystack.find(&needle).map(|n| n as i32).unwrap_or(-1) haystack.find(&needle).map(|n| n as i32).unwrap_or(-1)
} }
#[test]
fn test_implement_strstr() {
assert_eq!(str_str("hello".to_string(), "ll".to_string()), 2);
assert_eq!(str_str("aaaaa".to_string(), "bba".to_string()), -1);
assert_eq!(str_str("aaaaa".to_string(), "".to_string()), 0);
}

View File

@ -5,13 +5,3 @@ pub fn length_of_last_word(string: String) -> i32 {
.map(|word| word.len()) .map(|word| word.len())
.unwrap_or_default() as i32 .unwrap_or_default() as i32
} }
#[test]
fn test_length_of_last_word() {
assert_eq!(length_of_last_word("Hello World".to_string()), 5);
assert_eq!(
length_of_last_word(" fly me to the moon ".to_string()),
4
);
assert_eq!(length_of_last_word("luffy is still joyboy".to_string()), 6);
}

View File

@ -29,43 +29,3 @@ pub fn longest_common_prefix(strings: Vec<String>) -> String {
// empty string. // empty string.
prefix prefix
} }
#[test]
fn test_longest_common_prefix() {
assert_eq!(
longest_common_prefix(vec![
"doggo".to_string(),
"doggie".to_string(),
"dog".to_string(),
]),
"dog"
);
assert_eq!(
longest_common_prefix(vec![
"flower".to_string(),
"flow".to_string(),
"flight".to_string(),
]),
"fl"
);
assert_eq!(
longest_common_prefix(vec![
"dog".to_string(),
"racecar".to_string(),
"car".to_string(),
]),
""
);
assert_eq!(
longest_common_prefix(vec![
"dog".to_string(),
"racecar".to_string(),
"car".to_string(),
"".to_string()
]),
""
);
}

View File

@ -9,10 +9,3 @@ pub fn missing_number(numbers: Vec<i32>) -> i32 {
total_range - sum_of_numbers total_range - sum_of_numbers
} }
#[test]
fn test_missing_number() {
assert_eq!(missing_number(vec![3, 0, 1]), 2);
assert_eq!(missing_number(vec![0, 1]), 2);
assert_eq!(missing_number(vec![9, 6, 4, 2, 3, 5, 7, 0, 1]), 8);
}

View File

@ -7,17 +7,3 @@ pub fn is_palindrome(number: i32) -> bool {
let number_string = number.to_string(); let number_string = number.to_string();
number_string == number_string.chars().rev().collect::<String>() number_string == number_string.chars().rev().collect::<String>()
} }
#[test]
fn test_palindrome_number() {
assert!(is_palindrome(0));
assert!(is_palindrome(-0));
assert!(is_palindrome(1));
assert!(is_palindrome(121));
assert!(is_palindrome(12321));
assert!(!is_palindrome(123));
assert!(!is_palindrome(321));
assert!(!is_palindrome(10));
assert!(!is_palindrome(-121));
}

View File

@ -31,13 +31,3 @@ pub fn plus_one(digits: Vec<i32>) -> Vec<i32> {
// Then reverse both iterators so they're in the correct order and chain them. // Then reverse both iterators so they're in the correct order and chain them.
digits.rev().chain(incremented.into_iter().rev()).collect() digits.rev().chain(incremented.into_iter().rev()).collect()
} }
#[test]
fn test_plus_one() {
assert_eq!(plus_one(vec![1, 2, 3]), vec![1, 2, 4]);
assert_eq!(plus_one(vec![4, 3, 2, 5]), vec![4, 3, 2, 6]);
assert_eq!(plus_one(vec![9]), vec![1, 0]);
assert_eq!(plus_one(vec![9, 9]), vec![1, 0, 0]);
assert_eq!(plus_one(vec![7, 9, 8]), vec![7, 9, 9]);
assert_eq!(plus_one(vec![8, 9, 9]), vec![9, 0, 0]);
}

View File

@ -15,12 +15,3 @@ pub fn reverse(number: i32) -> i32 {
// Then multiply by 0, 1 or -1 depending on the sign of the original number. // Then multiply by 0, 1 or -1 depending on the sign of the original number.
* number.signum() * number.signum()
} }
#[test]
fn test_reverse_integer() {
assert_eq!(reverse(123), 321);
assert_eq!(reverse(-123), -321);
assert_eq!(reverse(120), 21);
assert_eq!(reverse(i32::MAX), 0);
assert_eq!(reverse(i32::MIN), 0);
}

View File

@ -33,12 +33,3 @@ pub fn roman_to_int(string: String) -> i32 {
number number
} }
#[test]
fn test_roman_to_integer() {
assert_eq!(roman_to_int("III".to_string()), 3);
assert_eq!(roman_to_int("LVIII".to_string()), 58);
assert_eq!(roman_to_int("MCMXCIV".to_string()), 1994);
assert_eq!(roman_to_int("XX".to_string()), 20);
assert_eq!(roman_to_int("DCXXI".to_string()), 621);
}

View File

@ -15,12 +15,3 @@ pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
panic!("No pair of numbers found that sum to {}", target) panic!("No pair of numbers found that sum to {}", target)
} }
#[test]
fn test_two_sum() {
assert_eq!(two_sum(vec![2, 7, 11, 15], 9), [0, 1]);
assert_eq!(two_sum(vec![3, 2, 4], 6), [1, 2]);
assert_eq!(two_sum(vec![3, 3], 6), [0, 1]);
assert_eq!(two_sum(vec![3, 2, 3], 6), [0, 2]);
assert_eq!(two_sum(vec![-3, 4, 3, 90], 0), [0, 2]);
}

View File

@ -14,18 +14,3 @@ pub fn is_anagram(a: String, b: String) -> bool {
// And if they are equal, then they're anagrams. // And if they are equal, then they're anagrams.
a == b a == b
} }
#[test]
fn test_valid_anagram() {
let samples = [
(("💖🌠banana", "an💖anab🌠"), true),
(("anagram", "nagaram"), true),
(("bats", "tabs"), true),
(("rat", "cat"), false),
(("bats", "bat"), false),
];
for ((a, b), expected) in samples {
assert_eq!(is_anagram(a.to_string(), b.to_string()), expected);
}
}

View File

@ -37,19 +37,3 @@ pub fn is_valid(string: String) -> bool {
// string is invalid. // string is invalid.
stack.len() == 0 stack.len() == 0
} }
#[test]
fn test_valid_parenthesis() {
assert!(is_valid("".to_string()));
assert!(is_valid("[]".to_string()));
assert!(is_valid("[()]".to_string()));
assert!(is_valid("[{}()]".to_string()));
assert!(!is_valid("[".to_string()));
assert!(!is_valid("[()".to_string()));
assert!(!is_valid("((".to_string()));
assert!(!is_valid("[)".to_string()));
assert!(!is_valid("[(}]".to_string()));
assert!(!is_valid("([{}()]}".to_string()));
}

38
tests/add_binary.rs Normal file
View File

@ -0,0 +1,38 @@
use leetcode::add_binary::add_binary;
use test_case::test_case;
const A_256: &str = "10110101010001101011001011100011\
10100111101001010011000011000100\
11110111001001111010110000000010\
11100100110010110111101001100010\
10000101100010101010000111100100\
10100110010010100110111010011001\
11110111011101010000000110101001\
10000101110010101101011011011000";
const B_256: &str = "11101111111010110101001000000110\
10010101100011001110011100101111\
10011111011100000010100111010100\
11101111111010111010011111001011\
00000001001010111100100000010010\
10110001010101011100001100000010\
11010000010010011101100000000111\
10000100111011110110000001110100";
const EXPECTED_257: &str = "11010010100110010000001001110101\
00011110100110010000101111111010\
01001011010010111110101011101011\
11101010010110111001000100010110\
11000011010110110011010011111011\
10101011110100000001100011001110\
01100011110111110110110011011000\
10000101010111010001101110100110\
0";
#[test_case("11", "1", "100"; "carry multiple")]
#[test_case("1010", "1011", "10101"; "carry single")]
#[test_case(A_256, B_256, EXPECTED_257; "256 bit numbers")]
fn test_add_binary(a: &str, b: &str, expected: &str) {
assert_eq!(add_binary(a.to_string(), b.to_string()), expected);
}

10
tests/implement_strstr.rs Normal file
View File

@ -0,0 +1,10 @@
use leetcode::implement_strstr::str_str;
use test_case::test_case;
#[test_case("hello", "ll", 2; "simple")]
#[test_case("aaaaa", "bba", -1; "not found")]
#[test_case("aaaaa", "", 0; "empty needle")]
fn test_implement_strstr(haystack: &str, needle: &str, expected: i32) {
assert_eq!(str_str(haystack.to_string(), needle.to_string()), expected);
}

View File

@ -0,0 +1,10 @@
use leetcode::length_of_last_word::length_of_last_word;
use test_case::test_case;
#[test_case("hello to the world", 5; "simple")]
#[test_case(" fly me to the moon ", 4; "extra whitespace")]
#[test_case("", 0; "empty")]
fn test_length_of_last_word(input: &str, expected: i32) {
assert_eq!(length_of_last_word(input.to_string()), expected);
}

View File

@ -0,0 +1,16 @@
use leetcode::longest_common_prefix::longest_common_prefix;
use test_case::test_case;
#[test_case(&["flow", "flower", "flight"], "fl"; "simple")]
#[test_case(&["dog", "doggo", "doggie"], "dog"; "shortest string")]
#[test_case(&["racecar", "car"], ""; "no common prefix")]
#[test_case(&[], ""; "empty")]
fn test_longest_common_prefix(input: &[&str], expected: &str) {
let input = input
.into_iter()
.map(ToString::to_string)
.collect::<Vec<String>>();
assert_eq!(longest_common_prefix(input), expected);
}

9
tests/missing_number.rs Normal file
View File

@ -0,0 +1,9 @@
use leetcode::missing_number::missing_number;
use test_case::test_case;
#[test_case(&[0, 1, 3], 2; "sorted")]
#[test_case(&[9, 6, 4, 2, 3, 5, 7, 0, 1], 8; "unsorted")]
fn test_missing_number(input: &[i32], expected: i32) {
assert_eq!(missing_number(input.to_vec()), expected);
}

View File

@ -0,0 +1,13 @@
use leetcode::palindrome_number::is_palindrome;
use test_case::test_case;
#[test_case(0, true; "zero")]
#[test_case(-0, true; "negative zero")]
#[test_case(1, true; "single")]
#[test_case(121, true; "simple valid")]
#[test_case(123, false; "simple invalid")]
#[test_case(-121, false; "negative invalid")]
fn test_palindrome_number(input: i32, expected: bool) {
assert_eq!(is_palindrome(input), expected);
}

10
tests/plus_one.rs Normal file
View File

@ -0,0 +1,10 @@
use leetcode::plus_one::plus_one;
use test_case::test_case;
#[test_case(&[1, 2, 3], &[1, 2, 4]; "simple")]
#[test_case(&[9], &[1, 0]; "carry")]
#[test_case(&[9, 9], &[1, 0, 0]; "carry multiple")]
fn test_plus_one(input: &[i32], expected: &[i32]) {
assert_eq!(plus_one(input.to_vec()), expected.to_vec());
}

12
tests/reverse_integer.rs Normal file
View File

@ -0,0 +1,12 @@
use leetcode::reverse_integer::reverse;
use test_case::test_case;
#[test_case(123, 321; "positive")]
#[test_case(-123, -321; "negative")]
#[test_case(1200, 21; "trailing zeroes")]
#[test_case(i32::MAX, 0; "overflow with max")]
#[test_case(i32::MIN, 0; "overflow with min")]
fn test_reverse_integer(input: i32, expected: i32) {
assert_eq!(reverse(input), expected);
}

12
tests/roman_to_integer.rs Normal file
View File

@ -0,0 +1,12 @@
use leetcode::roman_to_integer::roman_to_int;
use test_case::test_case;
#[test_case("", 0; "minimum")]
#[test_case("MMMCMXCIX", 3999; "maximum")]
#[test_case("MDCLXVI", 1666; "every character")]
#[test_case("CM", 900; "with subtracting")]
#[test_case("XX", 20; "without subtracting")]
fn test_roman_to_integer(input: &str, expected: i32) {
assert_eq!(roman_to_int(input.to_string()), expected);
}

12
tests/two_sum.rs Normal file
View File

@ -0,0 +1,12 @@
use leetcode::two_sum::two_sum;
use test_case::test_case;
#[test_case(&[2, 7, 11, 15], 9, &[0, 1]; "1")]
#[test_case(&[3, 2, 4], 6, &[1, 2]; "2")]
#[test_case(&[3, 3], 6, &[0, 1]; "3")]
#[test_case(&[3, 2, 3], 6, &[0, 2]; "4")]
#[test_case(&[-3, 4, 3, 90], 0, &[0, 2]; "negative")]
fn test_two_sum(numbers: &[i32], target: i32, expected: &[i32]) {
assert_eq!(two_sum(numbers.to_vec(), target), expected);
}

11
tests/valid_anagram.rs Normal file
View File

@ -0,0 +1,11 @@
use leetcode::valid_anagram::is_anagram;
use test_case::test_case;
#[test_case("anagram", "nagaram", true; "simple")]
#[test_case("💖🌠banana", "an💖anab🌠", true; "unicode")]
#[test_case("rat", "cat", false; "invalid")]
#[test_case("bats", "bat", false; "invalid because inequal length")]
fn test_valid_anagram(a: &str, b: &str, expected: bool) {
assert_eq!(is_anagram(a.to_string(), b.to_string()), expected);
}

View File

@ -0,0 +1,17 @@
use leetcode::valid_parenthesis::is_valid;
use test_case::test_case;
#[test_case("", true; "empty")]
#[test_case("[]", true; "simple")]
#[test_case("[()]", true; "nested")]
#[test_case("[{}()]", true; "multiple nested")]
#[test_case("[", false; "only closing")]
#[test_case("[()", false; "missing closing")]
#[test_case("[(", false; "only opening")]
#[test_case("[)", false; "mismatched simple")]
#[test_case("[(}]", false; "mismatched nested")]
#[test_case("([{}()]}", false; "mismatched multiple nested")]
fn test_valid_parenthesis(input: &str, expected: bool) {
assert_eq!(is_valid(input.to_string()), expected);
}