1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! # Common Contents
//!
//! This module provide common structs, enums and functions used in solutions,
//! test cases, test suites.
//!
//! # 公共内容
//!
//! 此模块提供用于解法、测试用例、测试程序使用的公共 Struct、Enum 和函数。
//!

////////////////////////////////////////////////////////////////////////////////

use crate::macros::codegen_case_create_impl;
// use crate::macros::codegen_vector_case_create_impl;
use std::fmt::Debug;

/// Test case wrapper struct
///
/// # Generics
/// * `T` - type of test inputs
/// * `G` - type of expectations, must implement `PartialEq` and `Display` traits
/// * `P` - type of optional parameters
pub struct Case<T, G, P> {
    /// Input values of test case
    pub inputs: Vec<T>,

    /// Optional parameters when executing test case
    pub params: Vec<P>,

    /// Expected values of given input
    pub values: Vec<G>,

    /// Test case index (for labeling)
    index: i32,
}

impl<T, G, P> Case<T, G, P>
where
    T: PartialEq + Debug,
    G: PartialEq + Debug,
    P: PartialEq + Debug,
{
    /// Create new test case with no parameters
    ///
    /// # Arguments
    /// * `input` - test input
    /// * `values` - expected values, accept single- or multi-value vector
    pub fn new(input: T, values: Vec<G>) -> Case<T, G, P> {
        Case {
            inputs: vec![input],
            params: vec![],
            values: values,
            index: 0,
        }
    }

    /// Create new test case with no parameters but multiple inputs
    ///
    /// # Arguments
    /// * `inputs` - test input, accept single- or multi-value vector
    /// * `values` - expected values, accept single- or multi-value vector
    pub fn new_multi(inputs: Vec<T>, values: Vec<G>) -> Case<T, G, P> {
        Case {
            inputs: inputs,
            params: vec![],
            values: values,
            index: 0,
        }
    }

    /// Create new test case with parameters
    ///
    /// # Arguments
    /// * `input` - test input
    /// * `params` - test parameters that vary among different cases
    /// * `values` - expected values, accept single- or multi-value vector
    pub fn new_params(input: T, params: Vec<P>, values: Vec<G>) -> Case<T, G, P> {
        Case {
            inputs: vec![input],
            params: params,
            values: values,
            index: 0,
        }
    }

    /// Create new test case with parameters and multi input
    ///
    /// # Arguments
    /// * `inputs` - test input, accept single- or multi-value vector
    /// * `params` - test parameters that vary among different cases
    /// * `values` - expected values, accept single- or multi-value vector
    pub fn new_params_multi(inputs: Vec<T>, params: Vec<P>, values: Vec<G>) -> Case<T, G, P> {
        Case {
            inputs: inputs,
            params: params,
            values: values,
            index: 0,
        }
    }

    /// Check if solution output matches any expectations
    ///
    /// # Arguments
    /// * `&self` - inmutable borrow to object itself
    /// * `result` - solution output
    ///
    /// ```
    /// use leetcode_rust::common::Case;
    /// let case:Case<String, String, i32> = Case::new(String::from("A"), vec![String::from("A")]);
    /// case.is_valid(String::from("A"))
    /// ```
    pub fn is_valid(&self, result: G) {
        if self.values.len() == 0 {
            assert!(false);
        }
        for val in self.values.iter() {
            if *val == result {
                return assert!(true);
            }
        }

        if self.values.len() == 1 {
            if self.inputs.len() == 1 {
                assert!(
                    false,
                    "[#{}] INPUT=`{:?}`, OUTPUT=`{:?}`, EXPECTATION=`{:?}`",
                    self.index, &self.inputs[0], &result, self.values[0]
                );
            } else {
                assert!(
                    false,
                    "[#{}] INPUT=`[{:?}]`, OUTPUT=`{:?}`, EXPECTATION=`{:?}`",
                    self.index, self.inputs, &result, self.values[0]
                );
            }
        } else {
            assert!(
                false,
                "Result `{:?}` doesn't match any expectations",
                &result
            );
        }
    }

    /// Returns the index value in String form.
    pub fn label(&self) -> String {
        self.index.to_string()
    }

    /// Returns the first element of inputs
    pub fn input(&self) -> &T {
        &self.inputs[0]
    }
}

/// A easy to use test case collection struct that also provide functions for
/// simple test case creation.
pub struct CaseGroup<T, G, P> {
    /// A vector containing all test cases.
    cases: Vec<Case<T, G, P>>,

    // Number of test cases included. Used when labeling each case
    count: i32,
}

impl<T, G, P> CaseGroup<T, G, P> {
    /// Create a new CaseGroup instance.
    pub fn new() -> CaseGroup<T, G, P> {
        CaseGroup {
            cases: vec![],
            count: 0,
        }
    }

    /// Add existing test case instance to the collection.
    ///
    /// Note: an additional `index` value will be set by calling this method.
    pub fn add(&mut self, mut case: Case<T, G, P>) {
        self.count += 1;
        case.index = self.count;
        self.cases.push(case);
    }

    /// Get all test cases within current test case collection.
    pub fn all(self) -> Vec<Case<T, G, P>> {
        self.cases
    }
}

/// Implement two handy methods on CaseGroup<String, G, P> struct.
impl<G, P> CaseGroup<String, G, P>
where
    P: PartialEq + Debug,
    G: PartialEq + Debug,
{
    /// Create a new test case (no input parameters) matching
    /// &str and other generic types.
    ///
    /// # Argument
    /// * `ipt` - this argument is set to `&str` to simplify method calls.
    /// * `exp` - expected values in `Vec<G>` form.
    pub fn create(&mut self, ipt: &str, exp: Vec<G>) {
        self.add(Case::new(ipt.to_string(), exp));
    }

    /// Create a new test case (with input parameters) matching
    /// &str and other generic types.
    ///
    /// # Argument
    /// * `ipt` - this argument is set to `&str` to simplify method calls.
    /// * `exp` - expected values in `Vec<G>` form.
    /// * `params` - expected values in `Vec<P>` form.
    pub fn create_param(&mut self, ipt: &str, exp: Vec<G>, params: Vec<P>) {
        self.add(Case::new_params(ipt.to_string(), params, exp));
    }

    /// Create a new test case (no input parameters but multiple inputs)
    /// matching &str and other generic types.
    ///
    /// # Argument
    /// * `ipts` - this argument is set to `&str` to simplify method calls.
    /// * `exp` - expected values in `Vec<G>` form.
    pub fn create_multi(&mut self, ipts: Vec<&str>, exp: Vec<G>) {
        self.add(Case::new_multi(
            ipts.iter().map(|x| x.to_string()).collect(),
            exp,
        ));
    }

    /// Create a new test case (with input parameters and multiple inputs)
    /// matching &str and other generic types.
    ///
    /// # Argument
    /// * `ipts` - this argument is set to `&str` to simplify method calls.
    /// * `exp` - expected values in `Vec<G>` form.
    /// * `params` - expected values in `Vec<P>` form.
    pub fn create_param_multi(&mut self, ipts: Vec<&str>, exp: Vec<G>, params: Vec<P>) {
        self.add(Case::new_params_multi(
            ipts.iter().map(|x| x.to_string()).collect(),
            params,
            exp,
        ));
    }
}

codegen_case_create_impl!(i32, i32, i32);
codegen_case_create_impl!(i32, String, i32);
codegen_case_create_impl!(i32, bool, i32);
codegen_case_create_impl!(Vec<i32>, i32, i32);
codegen_case_create_impl!(Vec<i32>, Vec<Vec<i32>>, i32);