From 491e7c743acc18af3856425b27f613113dc399f3 Mon Sep 17 00:00:00 2001 From: Bauke Date: Fri, 8 Apr 2022 22:53:04 +0200 Subject: [PATCH] Replace manual tests with test_case. --- Cargo.toml | 3 +++ source/add_binary/mod.rs | 38 --------------------------- source/implement_strstr/mod.rs | 7 ----- source/length_of_last_word/mod.rs | 10 -------- source/longest_common_prefix/mod.rs | 40 ----------------------------- source/missing_number/mod.rs | 7 ----- source/palindrome_number/mod.rs | 14 ---------- source/plus_one/mod.rs | 10 -------- source/reverse_integer/mod.rs | 9 ------- source/roman_to_integer/mod.rs | 9 ------- source/two_sum/mod.rs | 9 ------- source/valid_anagram/mod.rs | 15 ----------- source/valid_parenthesis/mod.rs | 16 ------------ tests/add_binary.rs | 38 +++++++++++++++++++++++++++ tests/implement_strstr.rs | 10 ++++++++ tests/length_of_last_word.rs | 10 ++++++++ tests/longest_common_prefix.rs | 16 ++++++++++++ tests/missing_number.rs | 9 +++++++ tests/palindrome_number.rs | 13 ++++++++++ tests/plus_one.rs | 10 ++++++++ tests/reverse_integer.rs | 12 +++++++++ tests/roman_to_integer.rs | 12 +++++++++ tests/two_sum.rs | 12 +++++++++ tests/valid_anagram.rs | 11 ++++++++ tests/valid_parenthesis.rs | 17 ++++++++++++ 25 files changed, 173 insertions(+), 184 deletions(-) create mode 100644 tests/add_binary.rs create mode 100644 tests/implement_strstr.rs create mode 100644 tests/length_of_last_word.rs create mode 100644 tests/longest_common_prefix.rs create mode 100644 tests/missing_number.rs create mode 100644 tests/palindrome_number.rs create mode 100644 tests/plus_one.rs create mode 100644 tests/reverse_integer.rs create mode 100644 tests/roman_to_integer.rs create mode 100644 tests/two_sum.rs create mode 100644 tests/valid_anagram.rs create mode 100644 tests/valid_parenthesis.rs diff --git a/Cargo.toml b/Cargo.toml index 1a8a2a7..2cd312a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,6 @@ edition = "2021" [lib] path = "source/lib.rs" + +[dev-dependencies] +test-case = "2.0.2" diff --git a/source/add_binary/mod.rs b/source/add_binary/mod.rs index 4eb73d9..26184aa 100644 --- a/source/add_binary/mod.rs +++ b/source/add_binary/mod.rs @@ -59,41 +59,3 @@ pub fn add_binary(a: String, b: String) -> String { 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" - ); -} diff --git a/source/implement_strstr/mod.rs b/source/implement_strstr/mod.rs index e4f3fc9..4c7f8f5 100644 --- a/source/implement_strstr/mod.rs +++ b/source/implement_strstr/mod.rs @@ -5,10 +5,3 @@ pub fn str_str(haystack: String, needle: String) -> i32 { 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); -} diff --git a/source/length_of_last_word/mod.rs b/source/length_of_last_word/mod.rs index e30440c..d131f0e 100644 --- a/source/length_of_last_word/mod.rs +++ b/source/length_of_last_word/mod.rs @@ -5,13 +5,3 @@ pub fn length_of_last_word(string: String) -> i32 { .map(|word| word.len()) .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); -} diff --git a/source/longest_common_prefix/mod.rs b/source/longest_common_prefix/mod.rs index 05594ca..f09373d 100644 --- a/source/longest_common_prefix/mod.rs +++ b/source/longest_common_prefix/mod.rs @@ -29,43 +29,3 @@ pub fn longest_common_prefix(strings: Vec) -> String { // empty string. 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() - ]), - "" - ); -} diff --git a/source/missing_number/mod.rs b/source/missing_number/mod.rs index 31a32d2..1ddab89 100644 --- a/source/missing_number/mod.rs +++ b/source/missing_number/mod.rs @@ -9,10 +9,3 @@ pub fn missing_number(numbers: Vec) -> i32 { 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); -} diff --git a/source/palindrome_number/mod.rs b/source/palindrome_number/mod.rs index 799d715..471bad0 100644 --- a/source/palindrome_number/mod.rs +++ b/source/palindrome_number/mod.rs @@ -7,17 +7,3 @@ pub fn is_palindrome(number: i32) -> bool { let number_string = number.to_string(); number_string == number_string.chars().rev().collect::() } - -#[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)); -} diff --git a/source/plus_one/mod.rs b/source/plus_one/mod.rs index eda99fe..a42fe1c 100644 --- a/source/plus_one/mod.rs +++ b/source/plus_one/mod.rs @@ -31,13 +31,3 @@ pub fn plus_one(digits: Vec) -> Vec { // Then reverse both iterators so they're in the correct order and chain them. 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]); -} diff --git a/source/reverse_integer/mod.rs b/source/reverse_integer/mod.rs index 72aaf47..41f6ce8 100644 --- a/source/reverse_integer/mod.rs +++ b/source/reverse_integer/mod.rs @@ -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. * 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); -} diff --git a/source/roman_to_integer/mod.rs b/source/roman_to_integer/mod.rs index d3ea13e..4bb74ca 100644 --- a/source/roman_to_integer/mod.rs +++ b/source/roman_to_integer/mod.rs @@ -33,12 +33,3 @@ pub fn roman_to_int(string: String) -> i32 { 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); -} diff --git a/source/two_sum/mod.rs b/source/two_sum/mod.rs index e717137..ba9441a 100644 --- a/source/two_sum/mod.rs +++ b/source/two_sum/mod.rs @@ -15,12 +15,3 @@ pub fn two_sum(numbers: Vec, target: i32) -> Vec { 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]); -} diff --git a/source/valid_anagram/mod.rs b/source/valid_anagram/mod.rs index 2aff0d5..2eba4e1 100644 --- a/source/valid_anagram/mod.rs +++ b/source/valid_anagram/mod.rs @@ -14,18 +14,3 @@ pub fn is_anagram(a: String, b: String) -> bool { // And if they are equal, then they're anagrams. 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); - } -} diff --git a/source/valid_parenthesis/mod.rs b/source/valid_parenthesis/mod.rs index 321c3af..d53c04e 100644 --- a/source/valid_parenthesis/mod.rs +++ b/source/valid_parenthesis/mod.rs @@ -37,19 +37,3 @@ pub fn is_valid(string: String) -> bool { // string is invalid. 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())); -} diff --git a/tests/add_binary.rs b/tests/add_binary.rs new file mode 100644 index 0000000..02a1147 --- /dev/null +++ b/tests/add_binary.rs @@ -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); +} diff --git a/tests/implement_strstr.rs b/tests/implement_strstr.rs new file mode 100644 index 0000000..ace107c --- /dev/null +++ b/tests/implement_strstr.rs @@ -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); +} diff --git a/tests/length_of_last_word.rs b/tests/length_of_last_word.rs new file mode 100644 index 0000000..8434157 --- /dev/null +++ b/tests/length_of_last_word.rs @@ -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); +} diff --git a/tests/longest_common_prefix.rs b/tests/longest_common_prefix.rs new file mode 100644 index 0000000..a6dfbbe --- /dev/null +++ b/tests/longest_common_prefix.rs @@ -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::>(); + + assert_eq!(longest_common_prefix(input), expected); +} diff --git a/tests/missing_number.rs b/tests/missing_number.rs new file mode 100644 index 0000000..a8ab068 --- /dev/null +++ b/tests/missing_number.rs @@ -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); +} diff --git a/tests/palindrome_number.rs b/tests/palindrome_number.rs new file mode 100644 index 0000000..78c8aa7 --- /dev/null +++ b/tests/palindrome_number.rs @@ -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); +} diff --git a/tests/plus_one.rs b/tests/plus_one.rs new file mode 100644 index 0000000..65dcdbd --- /dev/null +++ b/tests/plus_one.rs @@ -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()); +} diff --git a/tests/reverse_integer.rs b/tests/reverse_integer.rs new file mode 100644 index 0000000..02c4710 --- /dev/null +++ b/tests/reverse_integer.rs @@ -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); +} diff --git a/tests/roman_to_integer.rs b/tests/roman_to_integer.rs new file mode 100644 index 0000000..d3d4ee6 --- /dev/null +++ b/tests/roman_to_integer.rs @@ -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); +} diff --git a/tests/two_sum.rs b/tests/two_sum.rs new file mode 100644 index 0000000..8ed8f3e --- /dev/null +++ b/tests/two_sum.rs @@ -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); +} diff --git a/tests/valid_anagram.rs b/tests/valid_anagram.rs new file mode 100644 index 0000000..1ec6141 --- /dev/null +++ b/tests/valid_anagram.rs @@ -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); +} diff --git a/tests/valid_parenthesis.rs b/tests/valid_parenthesis.rs new file mode 100644 index 0000000..30fb5ad --- /dev/null +++ b/tests/valid_parenthesis.rs @@ -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); +}