Moved tests inside the designated tests module for every problem

This commit is contained in:
Egor 2024-05-05 18:31:04 +03:00
parent e4dbd39192
commit 0819e90e80
26 changed files with 431 additions and 275 deletions

View file

@ -24,3 +24,4 @@ pub mod p21_merge_two_sorted_lists;
pub mod p22_generate_parentheses; pub mod p22_generate_parentheses;
pub mod p23_merge_k_sorted_lists; pub mod p23_merge_k_sorted_lists;
pub mod p24_swap_nodes_in_pairs; pub mod p24_swap_nodes_in_pairs;
pub mod p25_reverse_nodes_in_k_group;

View file

@ -18,6 +18,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::is_match("aa".to_string(), "a".to_string()), false); assert_eq!(Solution::is_match("aa".to_string(), "a".to_string()), false);
@ -32,3 +36,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::is_match("ab".to_string(), ".*".to_string()), true); assert_eq!(Solution::is_match("ab".to_string(), ".*".to_string()), true);
} }
}

View file

@ -18,6 +18,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::max_area(vec![1,8,6,2,5,4,8,3,7]), 49); assert_eq!(Solution::max_area(vec![1,8,6,2,5,4,8,3,7]), 49);
@ -27,3 +31,4 @@ fn test1() {
fn test2() { fn test2() {
assert_eq!(Solution::max_area(vec![1,1]), 1); assert_eq!(Solution::max_area(vec![1,1]), 1);
} }
}

View file

@ -33,6 +33,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::int_to_roman(3), "III"); assert_eq!(Solution::int_to_roman(3), "III");
@ -47,3 +51,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::int_to_roman(1994), "MCMXCIV"); assert_eq!(Solution::int_to_roman(1994), "MCMXCIV");
} }
}

View file

@ -11,6 +11,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::roman_to_int("III".to_string()), 3); assert_eq!(Solution::roman_to_int("III".to_string()), 3);
@ -25,3 +29,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::roman_to_int("MCMXCIV".to_string()), 1994); assert_eq!(Solution::roman_to_int("MCMXCIV".to_string()), 1994);
} }
}

View file

@ -17,6 +17,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
let strs = vec!["flower".to_string(),"flow".to_string(),"flight".to_string()]; let strs = vec!["flower".to_string(),"flow".to_string(),"flight".to_string()];
@ -28,3 +32,4 @@ fn test2() {
let strs = vec!["dog".to_string(),"racecar".to_string(),"car".to_string()]; let strs = vec!["dog".to_string(),"racecar".to_string(),"car".to_string()];
assert_eq!(Solution::longest_common_prefix(strs), ""); assert_eq!(Solution::longest_common_prefix(strs), "");
} }
}

View file

@ -28,6 +28,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::three_sum(vec![-1,0,1,2,-1,-4]), vec![vec![-1,-1,2], vec![-1,0,1]]); assert_eq!(Solution::three_sum(vec![-1,0,1,2,-1,-4]), vec![vec![-1,-1,2], vec![-1,0,1]]);
@ -42,3 +46,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::three_sum(vec![0,0,0]), vec![vec![0,0,0]]); assert_eq!(Solution::three_sum(vec![0,0,0]), vec![vec![0,0,0]]);
} }
}

View file

@ -22,6 +22,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::three_sum_closest(vec![-1,2,1,-4], 1), 2); assert_eq!(Solution::three_sum_closest(vec![-1,2,1,-4], 1), 2);
@ -31,3 +35,4 @@ fn test1() {
fn test2() { fn test2() {
assert_eq!(Solution::three_sum_closest(vec![0,0,0], 1), 0); assert_eq!(Solution::three_sum_closest(vec![0,0,0], 1), 0);
} }
}

View file

@ -23,6 +23,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
let mut actual = Solution::letter_combinations("23".to_string()); let mut actual = Solution::letter_combinations("23".to_string());
@ -39,3 +43,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::letter_combinations("2".to_string()), vec!["a","b","c"]); assert_eq!(Solution::letter_combinations("2".to_string()), vec!["a","b","c"]);
} }
}

View file

@ -35,6 +35,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
let mut actual = Solution::four_sum(vec![1,0,-1,0,-2,2], 0); let mut actual = Solution::four_sum(vec![1,0,-1,0,-2,2], 0);
@ -53,3 +57,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::four_sum(vec![1000000000,1000000000,1000000000,1000000000], -294967296), Vec::<Vec<i32>>::new()); assert_eq!(Solution::four_sum(vec![1000000000,1000000000,1000000000,1000000000], -294967296), Vec::<Vec<i32>>::new());
} }
}

View file

@ -22,6 +22,11 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use crate::linked_list::ListNode;
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::remove_nth_from_end(list![1,2,3,4,5], 2), list![1,2,3,5]); assert_eq!(Solution::remove_nth_from_end(list![1,2,3,4,5], 2), list![1,2,3,5]);
@ -41,3 +46,4 @@ fn test3() {
fn test4() { fn test4() {
assert_eq!(Solution::remove_nth_from_end(list![1,2], 2), list![2]); assert_eq!(Solution::remove_nth_from_end(list![1,2], 2), list![2]);
} }
}

View file

@ -15,6 +15,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
let actual = Solution::two_sum(vec![2,7,11,15], 9); let actual = Solution::two_sum(vec![2,7,11,15], 9);
@ -32,4 +36,4 @@ fn test3() {
let actual = Solution::two_sum(vec![3,3], 6); let actual = Solution::two_sum(vec![3,3], 6);
assert!(actual == vec![1,0] || actual == vec![0,1]); assert!(actual == vec![1,0] || actual == vec![0,1]);
} }
}

View file

@ -20,6 +20,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::is_valid("()".to_string()), true); assert_eq!(Solution::is_valid("()".to_string()), true);
@ -34,3 +38,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::is_valid("(]".to_string()), false); assert_eq!(Solution::is_valid("(]".to_string()), false);
} }
}

View file

@ -27,6 +27,11 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use crate::linked_list::ListNode;
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::merge_two_lists(list![1,2,4], list![1,3,4]), list![1,1,2,3,4,4]); assert_eq!(Solution::merge_two_lists(list![1,2,4], list![1,3,4]), list![1,1,2,3,4,4]);
@ -41,3 +46,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::merge_two_lists(list![], list![0]), list![0]); assert_eq!(Solution::merge_two_lists(list![], list![0]), list![0]);
} }
}

View file

@ -15,6 +15,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
let mut actual = Solution::generate_parenthesis(3); let mut actual = Solution::generate_parenthesis(3);
@ -26,3 +30,4 @@ fn test1() {
fn test2() { fn test2() {
assert_eq!(Solution::generate_parenthesis(1), vec!["()"]); assert_eq!(Solution::generate_parenthesis(1), vec!["()"]);
} }
}

View file

@ -22,6 +22,11 @@ pub mod sorted_vec {
} }
} }
#[cfg(test)]
mod tests {
use crate::linked_list::ListNode;
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::merge_k_lists(vec![ assert_eq!(Solution::merge_k_lists(vec![
@ -39,6 +44,7 @@ pub mod sorted_vec {
assert_eq!(Solution::merge_k_lists(vec![list![]]), list![]); assert_eq!(Solution::merge_k_lists(vec![list![]]), list![]);
} }
} }
}
pub mod binary_heap { pub mod binary_heap {
use crate::linked_list::ListNode; use crate::linked_list::ListNode;
@ -71,6 +77,11 @@ pub mod binary_heap {
fn cmp(&self, other: &Self) -> Ordering { self.val.cmp(&other.val) } fn cmp(&self, other: &Self) -> Ordering { self.val.cmp(&other.val) }
} }
#[cfg(test)]
mod tests {
use crate::linked_list::ListNode;
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::merge_k_lists(vec![ assert_eq!(Solution::merge_k_lists(vec![
@ -88,3 +99,4 @@ pub mod binary_heap {
assert_eq!(Solution::merge_k_lists(vec![list![]]), list![]); assert_eq!(Solution::merge_k_lists(vec![list![]]), list![]);
} }
} }
}

View file

@ -18,6 +18,11 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use crate::linked_list::ListNode;
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::swap_pairs(list![1,2,3,4]), list![2,1,4,3]); assert_eq!(Solution::swap_pairs(list![1,2,3,4]), list![2,1,4,3]);
@ -32,3 +37,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::swap_pairs(list![1]), list![1]); assert_eq!(Solution::swap_pairs(list![1]), list![1]);
} }
}

View file

@ -0,0 +1,24 @@
use crate::linked_list::ListNode;
pub struct Solution;
impl Solution {
pub fn reverse_k_group(mut head: Option<Box<ListNode>>, k: i32) -> Option<Box<ListNode>> {
unimplemented!()
}
}
#[cfg(test)]
mod tests {
use crate::linked_list::ListNode;
use super::Solution;
#[test]
fn test1() {
assert_eq!(Solution::reverse_k_group(list![1,2,3,4,5], 2), list![2,1,4,3,5]);
}
#[test]
fn test2() {
assert_eq!(Solution::reverse_k_group(list![1,2,3,4,5], 3), list![3,2,1,4,5]);
}
}

View file

@ -29,6 +29,11 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use crate::linked_list::ListNode;
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::add_two_numbers(list![2,4,3], list![5,6,4]), list![7,0,8]); assert_eq!(Solution::add_two_numbers(list![2,4,3], list![5,6,4]), list![7,0,8]);
@ -43,3 +48,5 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::add_two_numbers(list![9,9,9,9,9,9,9], list![9,9,9,9]), list![8,9,9,9,0,0,0,1]); assert_eq!(Solution::add_two_numbers(list![9,9,9,9,9,9,9], list![9,9,9,9]), list![8,9,9,9,0,0,0,1]);
} }
}

View file

@ -18,6 +18,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::length_of_longest_substring("abcabcbb".to_string()), 3); assert_eq!(Solution::length_of_longest_substring("abcabcbb".to_string()), 3);
@ -32,3 +36,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::length_of_longest_substring("pwwkew".to_string()), 3); assert_eq!(Solution::length_of_longest_substring("pwwkew".to_string()), 3);
} }
}

View file

@ -20,6 +20,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::find_median_sorted_arrays(vec![1,3], vec![2]), 2.0); assert_eq!(Solution::find_median_sorted_arrays(vec![1,3], vec![2]), 2.0);
@ -29,3 +33,4 @@ fn test1() {
fn test2() { fn test2() {
assert_eq!(Solution::find_median_sorted_arrays(vec![1,2], vec![3,4]), 2.5); assert_eq!(Solution::find_median_sorted_arrays(vec![1,2], vec![3,4]), 2.5);
} }
}

View file

@ -15,6 +15,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
let actual = Solution::longest_palindrome("babad".to_string()); let actual = Solution::longest_palindrome("babad".to_string());
@ -25,3 +29,4 @@ fn test1() {
fn test2() { fn test2() {
assert_eq!(Solution::longest_palindrome("cbbd".to_string()), "bb"); assert_eq!(Solution::longest_palindrome("cbbd".to_string()), "bb");
} }
}

View file

@ -18,6 +18,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::convert("PAYPALISHIRING".to_string(), 3), "PAHNAPLSIIGYIR"); assert_eq!(Solution::convert("PAYPALISHIRING".to_string(), 3), "PAHNAPLSIIGYIR");
@ -32,3 +36,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::convert("A".to_string(), 1), "A"); assert_eq!(Solution::convert("A".to_string(), 1), "A");
} }
}

View file

@ -17,6 +17,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::reverse(123), 321); assert_eq!(Solution::reverse(123), 321);
@ -31,3 +35,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::reverse(120), 21); assert_eq!(Solution::reverse(120), 21);
} }
}

View file

@ -19,6 +19,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::my_atoi("42".to_string()), 42); assert_eq!(Solution::my_atoi("42".to_string()), 42);
@ -33,3 +37,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::my_atoi("4193 with words".to_string()), 4193); assert_eq!(Solution::my_atoi("4193 with words".to_string()), 4193);
} }
}

View file

@ -12,6 +12,10 @@ impl Solution {
} }
} }
#[cfg(test)]
mod tests {
use super::Solution;
#[test] #[test]
fn test1() { fn test1() {
assert_eq!(Solution::is_palindrome(121), true); assert_eq!(Solution::is_palindrome(121), true);
@ -26,3 +30,4 @@ fn test2() {
fn test3() { fn test3() {
assert_eq!(Solution::is_palindrome(10), false); assert_eq!(Solution::is_palindrome(10), false);
} }
}