cryptos/utils/
check.rs

1use std::fmt::Debug;
2use std::time::Instant;
3
4/// Compare and check the result of the function
5///
6/// # Arguments
7///
8/// * `cases` - The test cases
9/// * `name` - The name of the test
10/// * `func` - The function to test
11///
12/// # Returns
13///
14/// Returns true if all tests passed; otherwise returns false
15#[allow(dead_code)]
16pub fn compare_check<I, O, F>(
17    cases: Vec<(I, O)>,
18    name: &str,
19    func: F
20) -> bool
21where
22    I: Clone + Debug,
23    O: PartialEq + Debug + Clone,
24    F: Fn(&I) -> O
25{
26    let mut all_tests_passed = true;
27
28    for (input, expected) in cases.iter() {
29        let start = Instant::now();
30        let result = func(input);
31        let duration = start.elapsed();
32
33        let truncated_input = truncate_with_ellipsis(format!("{:?}", input), 62);
34        print!("Test '{:<65}' {}: ", truncated_input, name);
35
36        if result == *expected {
37            println!(" \x1b[32msuccess\x1b[0m in \x1b[35m{:?}\x1b[0m", duration);
38        } else {
39            println!(" \x1b[31mfailed\x1b[0m in \x1b[35m{:?}\x1b[0m", duration);
40            println!("  Expected: {:?}", expected);
41            println!("  Got:      {:?}", result);
42            all_tests_passed = false;
43        }
44    }
45
46    if all_tests_passed {
47        println!("Test {}: \x1b[32msuccess\x1b[0m", name);
48        println!("");
49    } else {
50        println!("Test {}: \x1b[31mfailed\x1b[0m", name);
51        println!("");
52    }
53
54    all_tests_passed
55}
56
57/// Truncate the string with ellipsis
58fn truncate_with_ellipsis(s: String, max_length: usize) -> String {
59    if s.len() <= max_length {
60        s
61    } else {
62        format!("{}...", &s[..max_length - 3])
63    }
64}
65
66/// Compare and check the result of the function with multiple parameters
67/// 
68/// # Arguments
69/// 
70/// * `cases` - The test cases
71/// * `name` - The name of the test
72/// * `func` - The function to test
73/// * `parser` - Function to parse input string into parameters
74///
75/// # Returns
76///
77/// Returns true if all tests passed; otherwise returns false
78#[allow(dead_code)]
79pub fn compare_check_with_params<I, O, P, F, T>(
80    cases: Vec<(I, O)>,
81    name: &str,
82    func: F,
83    parser: P,
84) -> bool
85where
86    I: Clone + Debug,
87    O: PartialEq + Debug + Clone,
88    T: Debug + Clone,
89    F: Fn(Vec<T>) -> O,
90    P: Fn(&I) -> Vec<T>,
91{
92    let mut all_tests_passed = true;
93
94    for (input, expected) in cases.iter() {
95        let start = Instant::now();
96        let params = parser(input);
97        let result = func(params.clone());
98        let duration = start.elapsed();
99
100        let truncated_input = truncate_with_ellipsis(format!("{:?}", input), 62);
101        print!("Test '{:<65}' {}: ", truncated_input, name);
102
103        if result == *expected {
104            println!(" \x1b[32msuccess\x1b[0m in \x1b[35m{:?}\x1b[0m", duration);
105        } else {
106            println!(" \x1b[31mfailed\x1b[0m in \x1b[35m{:?}\x1b[0m", duration);
107            println!("  Expected: {:?}", expected);
108            println!("  Got:      {:?}", result);
109            all_tests_passed = false;
110        }
111    }
112
113    if all_tests_passed {
114        println!("Test {}: \x1b[32msuccess\x1b[0m", name);
115        println!("");
116    } else {
117        println!("Test {}: \x1b[31mfailed\x1b[0m", name);
118        println!("");
119    }
120
121    all_tests_passed
122}