rpn_cli/core/
count.rs

1use num::CheckedSub;
2use std::fmt::{Display, Formatter};
3use std::ops::{Add, Sub};
4
5#[derive(Clone, Copy, Debug, PartialEq)]
6pub enum Count {
7    N(usize),
8    All,
9}
10
11pub fn fold_counts(
12    input: Count,
13    output: Count,
14    input2: Count,
15    output2: Count,
16) -> (Count, Count) {
17    // Check if the previous series is consumed.
18    if (output, input2) == (Count::All, Count::All) {
19        // Echo the input and output parameters.
20        (input, output2)
21    } else {
22        // Check there are enough input parameters.
23        if let Some(output) = output.checked_sub(&input2) {
24            // Append the output parameters.
25            (input, output + output2)
26        } else {
27            // Prepend the input parameters.
28            let input2 = input2.checked_sub(&output).unwrap_or(Count::N(0));
29            (input + input2, output2)
30        }
31    }
32}
33
34impl Add for Count {
35    type Output = Self;
36
37    fn add(self, rhs: Self) -> Self::Output {
38        match (self, rhs) {
39            (Self::N(lhs), Self::N(rhs)) => Self::N(lhs + rhs),
40            (Self::N(_), Self::All) => Self::All,
41            (Self::All, _) => Self::All,
42        }
43    }
44}
45
46impl Sub for Count {
47    type Output = Self;
48
49    fn sub(self, rhs: Self) -> Self::Output {
50        match (self, rhs) {
51            (Self::N(lhs), Self::N(rhs)) => Self::N(lhs.checked_sub(rhs).unwrap_or_default()),
52            (Self::N(_), Self::All) => Self::N(0),
53            (Self::All, _) => Self::All,
54        }
55    }
56}
57
58impl CheckedSub for Count {
59    fn checked_sub(&self, rhs: &Self) -> Option<Self> {
60        match (self, rhs) {
61            (Self::N(lhs), Self::N(rhs)) => lhs.checked_sub(rhs).map(Self::N),
62            (Self::N(_), Self::All) => None,
63            (Self::All, _) => Some(Self::All),
64        }
65    }
66}
67
68impl Display for Count {
69    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
70        match self {
71            Self::N(0) => Display::fmt(f.sign_minus().then_some("-").unwrap_or(""), f),
72            Self::N(1) => Display::fmt("N", f),
73            Self::N(2) => Display::fmt("N N", f),
74            Self::N(count) => Display::fmt(count.to_string().as_str(), f),
75            Self::All => Display::fmt("*", f),
76        }
77    }
78}
79
80#[cfg(test)]
81pub mod tests {
82    use crate::core::count;
83    use crate::core::count::Count::{All, N};
84    use num::CheckedSub;
85
86    #[test]
87    fn test_counts_are_added() {
88        assert_eq!(N(0) + N(0), N(0));
89        assert_eq!(N(0) + N(1), N(1));
90        assert_eq!(N(0) + N(2), N(2));
91        assert_eq!(N(0) + All, All);
92        assert_eq!(N(1) + N(0), N(1));
93        assert_eq!(N(1) + N(1), N(2));
94        assert_eq!(N(1) + N(2), N(3));
95        assert_eq!(N(1) + All, All);
96        assert_eq!(N(2) + N(0), N(2));
97        assert_eq!(N(2) + N(1), N(3));
98        assert_eq!(N(2) + N(2), N(4));
99        assert_eq!(N(2) + All, All);
100        assert_eq!(All + N(0), All);
101        assert_eq!(All + N(1), All);
102        assert_eq!(All + N(2), All);
103        assert_eq!(All + All, All);
104    }
105
106    #[test]
107    fn test_counts_are_subtracted_with_floor() {
108        assert_eq!(N(0) - N(0), N(0));
109        assert_eq!(N(0) - N(1), N(0));
110        assert_eq!(N(0) - N(2), N(0));
111        assert_eq!(N(0) - All, N(0));
112        assert_eq!(N(1) - N(0), N(1));
113        assert_eq!(N(1) - N(1), N(0));
114        assert_eq!(N(1) - N(2), N(0));
115        assert_eq!(N(1) - All, N(0));
116        assert_eq!(N(2) - N(0), N(2));
117        assert_eq!(N(2) - N(1), N(1));
118        assert_eq!(N(2) - N(2), N(0));
119        assert_eq!(N(2) - All, N(0));
120        assert_eq!(All - N(0), All);
121        assert_eq!(All - N(1), All);
122        assert_eq!(All - N(2), All);
123        assert_eq!(All - All, All);
124    }
125
126    #[test]
127    fn test_counts_are_subtracted_with_check() {
128        assert_eq!(N(0).checked_sub(&N(0)), Some(N(0)));
129        assert_eq!(N(0).checked_sub(&N(1)), None);
130        assert_eq!(N(0).checked_sub(&N(2)), None);
131        assert_eq!(N(0).checked_sub(&All), None);
132        assert_eq!(N(1).checked_sub(&N(0)), Some(N(1)));
133        assert_eq!(N(1).checked_sub(&N(1)), Some(N(0)));
134        assert_eq!(N(1).checked_sub(&N(2)), None);
135        assert_eq!(N(1).checked_sub(&All), None);
136        assert_eq!(N(2).checked_sub(&N(0)), Some(N(2)));
137        assert_eq!(N(2).checked_sub(&N(1)), Some(N(1)));
138        assert_eq!(N(2).checked_sub(&N(2)), Some(N(0)));
139        assert_eq!(N(2).checked_sub(&All), None);
140        assert_eq!(All.checked_sub(&N(0)), Some(All));
141        assert_eq!(All.checked_sub(&N(1)), Some(All));
142        assert_eq!(All.checked_sub(&N(2)), Some(All));
143        assert_eq!(All.checked_sub(&All), Some(All));
144    }
145
146    //                                       E L E M E N T
147    //         |     NULLARY     |      UNARY      |     BINARY      |     SERIES
148    //     I/O | 0/0 0/1 0/2 0/* | 1/0 1/1 1/2 1/* | 2/0 2/1 2/2 2/* | */0 */1 */2 */*
149    //    -----+-----------------+-----------------+-----------------+-----------------
150    //  .  0/0 | 0/0 0/1 0/2 0/* | 1/0 1/1 1/2 1/* | 2/0 2/1 2/2 2/* | */0 */1 */2 */*
151    //  .  0/1 | 0/1 0/2 0/3 0/* | 0/0 0/1 0/2 0/* | 1/0 1/1 1/2 1/* | */0 */1 */2 */*
152    //  .  0/2 | 0/2 0/3 0/4 0/* | 0/1 0/2 0/3 0/* | 0/0 0/1 0/2 0/* | */0 */1 */2 */*
153    //  .  0/* | 0/* 0/* 0/* 0/* | 0/* 0/* 0/* 0/* | 0/* 0/* 0/* 0/* | 0/0 0/1 0/2 0/*
154    //  A -----+-----------------+-----------------+-----------------+-----------------
155    //  C  1/0 | 1/0 1/1 1/2 1/* | 2/0 2/1 2/2 2/* | 3/0 3/1 3/2 3/* | */0 */1 */2 */*
156    //  C  1/1 | 1/1 1/2 1/3 1/* | 1/0 1/1 1/2 1/* | 2/0 2/1 2/2 2/* | */0 */1 */2 */*
157    //  U  1/2 | 1/2 1/3 1/4 1/* | 1/1 1/2 1/3 1/* | 1/0 1/1 1/2 1/* | */0 */1 */2 */*
158    //  M  1/* | 1/* 1/* 1/* 1/* | 1/* 1/* 1/* 1/* | 1/* 1/* 1/* 1/* | 1/0 1/1 1/2 1/*
159    //  U -----+-----------------+-----------------+-----------------+-----------------
160    //  L  2/0 | 2/0 2/1 2/2 2/* | 3/0 3/1 3/2 3/* | 4/0 4/1 4/2 4/* | */0 */1 */2 */*
161    //  A  2/1 | 2/1 2/2 2/3 2/* | 2/0 2/1 2/2 2/* | 3/0 3/1 3/2 3/* | */0 */1 */2 */*
162    //  T  2/2 | 2/2 2/3 2/4 2/* | 2/1 2/2 2/3 2/* | 2/0 2/1 2/2 2/* | */0 */1 */2 */*
163    //  O  2/* | 2/* 2/* 2/* 2/* | 2/* 2/* 2/* 2/* | 2/* 2/* 2/* 2/* | 2/0 2/1 2/2 2/*
164    //  R -----+-----------------+-----------------+-----------------+-----------------
165    //  .  */0 | */0 */1 */2 */* | */0 */1 */2 */* | */0 */1 */2 */* | */0 */1 */2 */*
166    //  .  */1 | */1 */2 */3 */* | */0 */1 */2 */* | */0 */1 */2 */* | */0 */1 */2 */*
167    //  .  */2 | */2 */3 */4 */* | */1 */2 */3 */* | */0 */1 */2 */* | */0 */1 */2 */*
168    //  .  */* | */* */* */* */* | */* */* */* */* | */* */* */* */* | */0 */1 */2 */*
169
170    #[test]
171    fn test_counts_are_combined() {
172        assert_eq!(count::fold_counts(N(0), N(0), N(0), N(0)), (N(0), N(0)));
173        assert_eq!(count::fold_counts(N(0), N(0), N(0), N(1)), (N(0), N(1)));
174        assert_eq!(count::fold_counts(N(0), N(0), N(0), N(2)), (N(0), N(2)));
175        assert_eq!(count::fold_counts(N(0), N(0), N(0), All), (N(0), All));
176        assert_eq!(count::fold_counts(N(0), N(0), N(1), N(0)), (N(1), N(0)));
177        assert_eq!(count::fold_counts(N(0), N(0), N(1), N(1)), (N(1), N(1)));
178        assert_eq!(count::fold_counts(N(0), N(0), N(1), N(2)), (N(1), N(2)));
179        assert_eq!(count::fold_counts(N(0), N(0), N(1), All), (N(1), All));
180        assert_eq!(count::fold_counts(N(0), N(0), N(2), N(0)), (N(2), N(0)));
181        assert_eq!(count::fold_counts(N(0), N(0), N(2), N(1)), (N(2), N(1)));
182        assert_eq!(count::fold_counts(N(0), N(0), N(2), N(2)), (N(2), N(2)));
183        assert_eq!(count::fold_counts(N(0), N(0), N(2), All), (N(2), All));
184        assert_eq!(count::fold_counts(N(0), N(0), All, N(0)), (All, N(0)));
185        assert_eq!(count::fold_counts(N(0), N(0), All, N(1)), (All, N(1)));
186        assert_eq!(count::fold_counts(N(0), N(0), All, N(2)), (All, N(2)));
187        assert_eq!(count::fold_counts(N(0), N(0), All, All), (All, All));
188
189        assert_eq!(count::fold_counts(N(0), N(1), N(0), N(0)), (N(0), N(1)));
190        assert_eq!(count::fold_counts(N(0), N(1), N(0), N(1)), (N(0), N(2)));
191        assert_eq!(count::fold_counts(N(0), N(1), N(0), N(2)), (N(0), N(3)));
192        assert_eq!(count::fold_counts(N(0), N(1), N(0), All), (N(0), All));
193        assert_eq!(count::fold_counts(N(0), N(1), N(1), N(0)), (N(0), N(0)));
194        assert_eq!(count::fold_counts(N(0), N(1), N(1), N(1)), (N(0), N(1)));
195        assert_eq!(count::fold_counts(N(0), N(1), N(1), N(2)), (N(0), N(2)));
196        assert_eq!(count::fold_counts(N(0), N(1), N(1), All), (N(0), All));
197        assert_eq!(count::fold_counts(N(0), N(1), N(2), N(0)), (N(1), N(0)));
198        assert_eq!(count::fold_counts(N(0), N(1), N(2), N(1)), (N(1), N(1)));
199        assert_eq!(count::fold_counts(N(0), N(1), N(2), N(2)), (N(1), N(2)));
200        assert_eq!(count::fold_counts(N(0), N(1), N(2), All), (N(1), All));
201        assert_eq!(count::fold_counts(N(0), N(1), All, N(0)), (All, N(0)));
202        assert_eq!(count::fold_counts(N(0), N(1), All, N(1)), (All, N(1)));
203        assert_eq!(count::fold_counts(N(0), N(1), All, N(2)), (All, N(2)));
204        assert_eq!(count::fold_counts(N(0), N(1), All, All), (All, All));
205
206        assert_eq!(count::fold_counts(N(0), N(2), N(0), N(0)), (N(0), N(2)));
207        assert_eq!(count::fold_counts(N(0), N(2), N(0), N(1)), (N(0), N(3)));
208        assert_eq!(count::fold_counts(N(0), N(2), N(0), N(2)), (N(0), N(4)));
209        assert_eq!(count::fold_counts(N(0), N(2), N(0), All), (N(0), All));
210        assert_eq!(count::fold_counts(N(0), N(2), N(1), N(0)), (N(0), N(1)));
211        assert_eq!(count::fold_counts(N(0), N(2), N(1), N(1)), (N(0), N(2)));
212        assert_eq!(count::fold_counts(N(0), N(2), N(1), N(2)), (N(0), N(3)));
213        assert_eq!(count::fold_counts(N(0), N(2), N(1), All), (N(0), All));
214        assert_eq!(count::fold_counts(N(0), N(2), N(2), N(0)), (N(0), N(0)));
215        assert_eq!(count::fold_counts(N(0), N(2), N(2), N(1)), (N(0), N(1)));
216        assert_eq!(count::fold_counts(N(0), N(2), N(2), N(2)), (N(0), N(2)));
217        assert_eq!(count::fold_counts(N(0), N(2), N(2), All), (N(0), All));
218        assert_eq!(count::fold_counts(N(0), N(2), All, N(0)), (All, N(0)));
219        assert_eq!(count::fold_counts(N(0), N(2), All, N(1)), (All, N(1)));
220        assert_eq!(count::fold_counts(N(0), N(2), All, N(2)), (All, N(2)));
221        assert_eq!(count::fold_counts(N(0), N(2), All, All), (All, All));
222
223        assert_eq!(count::fold_counts(N(0), All, N(0), N(0)), (N(0), All));
224        assert_eq!(count::fold_counts(N(0), All, N(0), N(1)), (N(0), All));
225        assert_eq!(count::fold_counts(N(0), All, N(0), N(2)), (N(0), All));
226        assert_eq!(count::fold_counts(N(0), All, N(0), All), (N(0), All));
227        assert_eq!(count::fold_counts(N(0), All, N(1), N(0)), (N(0), All));
228        assert_eq!(count::fold_counts(N(0), All, N(1), N(1)), (N(0), All));
229        assert_eq!(count::fold_counts(N(0), All, N(1), N(2)), (N(0), All));
230        assert_eq!(count::fold_counts(N(0), All, N(1), All), (N(0), All));
231        assert_eq!(count::fold_counts(N(0), All, N(2), N(0)), (N(0), All));
232        assert_eq!(count::fold_counts(N(0), All, N(2), N(1)), (N(0), All));
233        assert_eq!(count::fold_counts(N(0), All, N(2), N(2)), (N(0), All));
234        assert_eq!(count::fold_counts(N(0), All, N(2), All), (N(0), All));
235        assert_eq!(count::fold_counts(N(0), All, All, N(0)), (N(0), N(0)));
236        assert_eq!(count::fold_counts(N(0), All, All, N(1)), (N(0), N(1)));
237        assert_eq!(count::fold_counts(N(0), All, All, N(2)), (N(0), N(2)));
238        assert_eq!(count::fold_counts(N(0), All, All, All), (N(0), All));
239
240        assert_eq!(count::fold_counts(N(1), N(0), N(0), N(0)), (N(1), N(0)));
241        assert_eq!(count::fold_counts(N(1), N(0), N(0), N(1)), (N(1), N(1)));
242        assert_eq!(count::fold_counts(N(1), N(0), N(0), N(2)), (N(1), N(2)));
243        assert_eq!(count::fold_counts(N(1), N(0), N(0), All), (N(1), All));
244        assert_eq!(count::fold_counts(N(1), N(0), N(1), N(0)), (N(2), N(0)));
245        assert_eq!(count::fold_counts(N(1), N(0), N(1), N(1)), (N(2), N(1)));
246        assert_eq!(count::fold_counts(N(1), N(0), N(1), N(2)), (N(2), N(2)));
247        assert_eq!(count::fold_counts(N(1), N(0), N(1), All), (N(2), All));
248        assert_eq!(count::fold_counts(N(1), N(0), N(2), N(0)), (N(3), N(0)));
249        assert_eq!(count::fold_counts(N(1), N(0), N(2), N(1)), (N(3), N(1)));
250        assert_eq!(count::fold_counts(N(1), N(0), N(2), N(2)), (N(3), N(2)));
251        assert_eq!(count::fold_counts(N(1), N(0), N(2), All), (N(3), All));
252        assert_eq!(count::fold_counts(N(1), N(0), All, N(0)), (All, N(0)));
253        assert_eq!(count::fold_counts(N(1), N(0), All, N(1)), (All, N(1)));
254        assert_eq!(count::fold_counts(N(1), N(0), All, N(2)), (All, N(2)));
255        assert_eq!(count::fold_counts(N(1), N(0), All, All), (All, All));
256
257        assert_eq!(count::fold_counts(N(1), N(1), N(0), N(0)), (N(1), N(1)));
258        assert_eq!(count::fold_counts(N(1), N(1), N(0), N(1)), (N(1), N(2)));
259        assert_eq!(count::fold_counts(N(1), N(1), N(0), N(2)), (N(1), N(3)));
260        assert_eq!(count::fold_counts(N(1), N(1), N(0), All), (N(1), All));
261        assert_eq!(count::fold_counts(N(1), N(1), N(1), N(0)), (N(1), N(0)));
262        assert_eq!(count::fold_counts(N(1), N(1), N(1), N(1)), (N(1), N(1)));
263        assert_eq!(count::fold_counts(N(1), N(1), N(1), N(2)), (N(1), N(2)));
264        assert_eq!(count::fold_counts(N(1), N(1), N(1), All), (N(1), All));
265        assert_eq!(count::fold_counts(N(1), N(1), N(2), N(0)), (N(2), N(0)));
266        assert_eq!(count::fold_counts(N(1), N(1), N(2), N(1)), (N(2), N(1)));
267        assert_eq!(count::fold_counts(N(1), N(1), N(2), N(2)), (N(2), N(2)));
268        assert_eq!(count::fold_counts(N(1), N(1), N(2), All), (N(2), All));
269        assert_eq!(count::fold_counts(N(1), N(1), All, N(0)), (All, N(0)));
270        assert_eq!(count::fold_counts(N(1), N(1), All, N(1)), (All, N(1)));
271        assert_eq!(count::fold_counts(N(1), N(1), All, N(2)), (All, N(2)));
272        assert_eq!(count::fold_counts(N(1), N(1), All, All), (All, All));
273
274        assert_eq!(count::fold_counts(N(1), N(2), N(0), N(0)), (N(1), N(2)));
275        assert_eq!(count::fold_counts(N(1), N(2), N(0), N(1)), (N(1), N(3)));
276        assert_eq!(count::fold_counts(N(1), N(2), N(0), N(2)), (N(1), N(4)));
277        assert_eq!(count::fold_counts(N(1), N(2), N(0), All), (N(1), All));
278        assert_eq!(count::fold_counts(N(1), N(2), N(1), N(0)), (N(1), N(1)));
279        assert_eq!(count::fold_counts(N(1), N(2), N(1), N(1)), (N(1), N(2)));
280        assert_eq!(count::fold_counts(N(1), N(2), N(1), N(2)), (N(1), N(3)));
281        assert_eq!(count::fold_counts(N(1), N(2), N(1), All), (N(1), All));
282        assert_eq!(count::fold_counts(N(1), N(2), N(2), N(0)), (N(1), N(0)));
283        assert_eq!(count::fold_counts(N(1), N(2), N(2), N(1)), (N(1), N(1)));
284        assert_eq!(count::fold_counts(N(1), N(2), N(2), N(2)), (N(1), N(2)));
285        assert_eq!(count::fold_counts(N(1), N(2), N(2), All), (N(1), All));
286        assert_eq!(count::fold_counts(N(1), N(2), All, N(0)), (All, N(0)));
287        assert_eq!(count::fold_counts(N(1), N(2), All, N(1)), (All, N(1)));
288        assert_eq!(count::fold_counts(N(1), N(2), All, N(2)), (All, N(2)));
289        assert_eq!(count::fold_counts(N(1), N(2), All, All), (All, All));
290
291        assert_eq!(count::fold_counts(N(1), All, N(0), N(0)), (N(1), All));
292        assert_eq!(count::fold_counts(N(1), All, N(0), N(1)), (N(1), All));
293        assert_eq!(count::fold_counts(N(1), All, N(0), N(2)), (N(1), All));
294        assert_eq!(count::fold_counts(N(1), All, N(0), All), (N(1), All));
295        assert_eq!(count::fold_counts(N(1), All, N(1), N(0)), (N(1), All));
296        assert_eq!(count::fold_counts(N(1), All, N(1), N(1)), (N(1), All));
297        assert_eq!(count::fold_counts(N(1), All, N(1), N(2)), (N(1), All));
298        assert_eq!(count::fold_counts(N(1), All, N(1), All), (N(1), All));
299        assert_eq!(count::fold_counts(N(1), All, N(2), N(0)), (N(1), All));
300        assert_eq!(count::fold_counts(N(1), All, N(2), N(1)), (N(1), All));
301        assert_eq!(count::fold_counts(N(1), All, N(2), N(2)), (N(1), All));
302        assert_eq!(count::fold_counts(N(1), All, N(2), All), (N(1), All));
303        assert_eq!(count::fold_counts(N(1), All, All, N(0)), (N(1), N(0)));
304        assert_eq!(count::fold_counts(N(1), All, All, N(1)), (N(1), N(1)));
305        assert_eq!(count::fold_counts(N(1), All, All, N(2)), (N(1), N(2)));
306        assert_eq!(count::fold_counts(N(1), All, All, All), (N(1), All));
307
308        assert_eq!(count::fold_counts(N(2), N(0), N(0), N(0)), (N(2), N(0)));
309        assert_eq!(count::fold_counts(N(2), N(0), N(0), N(1)), (N(2), N(1)));
310        assert_eq!(count::fold_counts(N(2), N(0), N(0), N(2)), (N(2), N(2)));
311        assert_eq!(count::fold_counts(N(2), N(0), N(0), All), (N(2), All));
312        assert_eq!(count::fold_counts(N(2), N(0), N(1), N(0)), (N(3), N(0)));
313        assert_eq!(count::fold_counts(N(2), N(0), N(1), N(1)), (N(3), N(1)));
314        assert_eq!(count::fold_counts(N(2), N(0), N(1), N(2)), (N(3), N(2)));
315        assert_eq!(count::fold_counts(N(2), N(0), N(1), All), (N(3), All));
316        assert_eq!(count::fold_counts(N(2), N(0), N(2), N(0)), (N(4), N(0)));
317        assert_eq!(count::fold_counts(N(2), N(0), N(2), N(1)), (N(4), N(1)));
318        assert_eq!(count::fold_counts(N(2), N(0), N(2), N(2)), (N(4), N(2)));
319        assert_eq!(count::fold_counts(N(2), N(0), N(2), All), (N(4), All));
320        assert_eq!(count::fold_counts(N(2), N(0), All, N(0)), (All, N(0)));
321        assert_eq!(count::fold_counts(N(2), N(0), All, N(1)), (All, N(1)));
322        assert_eq!(count::fold_counts(N(2), N(0), All, N(2)), (All, N(2)));
323        assert_eq!(count::fold_counts(N(2), N(0), All, All), (All, All));
324
325        assert_eq!(count::fold_counts(N(2), N(1), N(0), N(0)), (N(2), N(1)));
326        assert_eq!(count::fold_counts(N(2), N(1), N(0), N(1)), (N(2), N(2)));
327        assert_eq!(count::fold_counts(N(2), N(1), N(0), N(2)), (N(2), N(3)));
328        assert_eq!(count::fold_counts(N(2), N(1), N(0), All), (N(2), All));
329        assert_eq!(count::fold_counts(N(2), N(1), N(1), N(0)), (N(2), N(0)));
330        assert_eq!(count::fold_counts(N(2), N(1), N(1), N(1)), (N(2), N(1)));
331        assert_eq!(count::fold_counts(N(2), N(1), N(1), N(2)), (N(2), N(2)));
332        assert_eq!(count::fold_counts(N(2), N(1), N(1), All), (N(2), All));
333        assert_eq!(count::fold_counts(N(2), N(1), N(2), N(0)), (N(3), N(0)));
334        assert_eq!(count::fold_counts(N(2), N(1), N(2), N(1)), (N(3), N(1)));
335        assert_eq!(count::fold_counts(N(2), N(1), N(2), N(2)), (N(3), N(2)));
336        assert_eq!(count::fold_counts(N(2), N(1), N(2), All), (N(3), All));
337        assert_eq!(count::fold_counts(N(2), N(1), All, N(0)), (All, N(0)));
338        assert_eq!(count::fold_counts(N(2), N(1), All, N(1)), (All, N(1)));
339        assert_eq!(count::fold_counts(N(2), N(1), All, N(2)), (All, N(2)));
340        assert_eq!(count::fold_counts(N(2), N(1), All, All), (All, All));
341
342        assert_eq!(count::fold_counts(N(2), N(2), N(0), N(0)), (N(2), N(2)));
343        assert_eq!(count::fold_counts(N(2), N(2), N(0), N(1)), (N(2), N(3)));
344        assert_eq!(count::fold_counts(N(2), N(2), N(0), N(2)), (N(2), N(4)));
345        assert_eq!(count::fold_counts(N(2), N(2), N(0), All), (N(2), All));
346        assert_eq!(count::fold_counts(N(2), N(2), N(1), N(0)), (N(2), N(1)));
347        assert_eq!(count::fold_counts(N(2), N(2), N(1), N(1)), (N(2), N(2)));
348        assert_eq!(count::fold_counts(N(2), N(2), N(1), N(2)), (N(2), N(3)));
349        assert_eq!(count::fold_counts(N(2), N(2), N(1), All), (N(2), All));
350        assert_eq!(count::fold_counts(N(2), N(2), N(2), N(0)), (N(2), N(0)));
351        assert_eq!(count::fold_counts(N(2), N(2), N(2), N(1)), (N(2), N(1)));
352        assert_eq!(count::fold_counts(N(2), N(2), N(2), N(2)), (N(2), N(2)));
353        assert_eq!(count::fold_counts(N(2), N(2), N(2), All), (N(2), All));
354        assert_eq!(count::fold_counts(N(2), N(2), All, N(0)), (All, N(0)));
355        assert_eq!(count::fold_counts(N(2), N(2), All, N(1)), (All, N(1)));
356        assert_eq!(count::fold_counts(N(2), N(2), All, N(2)), (All, N(2)));
357        assert_eq!(count::fold_counts(N(2), N(2), All, All), (All, All));
358
359        assert_eq!(count::fold_counts(N(2), All, N(0), N(0)), (N(2), All));
360        assert_eq!(count::fold_counts(N(2), All, N(0), N(1)), (N(2), All));
361        assert_eq!(count::fold_counts(N(2), All, N(0), N(2)), (N(2), All));
362        assert_eq!(count::fold_counts(N(2), All, N(0), All), (N(2), All));
363        assert_eq!(count::fold_counts(N(2), All, N(1), N(0)), (N(2), All));
364        assert_eq!(count::fold_counts(N(2), All, N(1), N(1)), (N(2), All));
365        assert_eq!(count::fold_counts(N(2), All, N(1), N(2)), (N(2), All));
366        assert_eq!(count::fold_counts(N(2), All, N(1), All), (N(2), All));
367        assert_eq!(count::fold_counts(N(2), All, N(2), N(0)), (N(2), All));
368        assert_eq!(count::fold_counts(N(2), All, N(2), N(1)), (N(2), All));
369        assert_eq!(count::fold_counts(N(2), All, N(2), N(2)), (N(2), All));
370        assert_eq!(count::fold_counts(N(2), All, N(2), All), (N(2), All));
371        assert_eq!(count::fold_counts(N(2), All, All, N(0)), (N(2), N(0)));
372        assert_eq!(count::fold_counts(N(2), All, All, N(1)), (N(2), N(1)));
373        assert_eq!(count::fold_counts(N(2), All, All, N(2)), (N(2), N(2)));
374        assert_eq!(count::fold_counts(N(2), All, All, All), (N(2), All));
375
376        assert_eq!(count::fold_counts(All, N(0), N(0), N(0)), (All, N(0)));
377        assert_eq!(count::fold_counts(All, N(0), N(0), N(1)), (All, N(1)));
378        assert_eq!(count::fold_counts(All, N(0), N(0), N(2)), (All, N(2)));
379        assert_eq!(count::fold_counts(All, N(0), N(0), All), (All, All));
380        assert_eq!(count::fold_counts(All, N(0), N(1), N(0)), (All, N(0)));
381        assert_eq!(count::fold_counts(All, N(0), N(1), N(1)), (All, N(1)));
382        assert_eq!(count::fold_counts(All, N(0), N(1), N(2)), (All, N(2)));
383        assert_eq!(count::fold_counts(All, N(0), N(1), All), (All, All));
384        assert_eq!(count::fold_counts(All, N(0), N(2), N(0)), (All, N(0)));
385        assert_eq!(count::fold_counts(All, N(0), N(2), N(1)), (All, N(1)));
386        assert_eq!(count::fold_counts(All, N(0), N(2), N(2)), (All, N(2)));
387        assert_eq!(count::fold_counts(All, N(0), N(2), All), (All, All));
388        assert_eq!(count::fold_counts(All, N(0), All, N(0)), (All, N(0)));
389        assert_eq!(count::fold_counts(All, N(0), All, N(1)), (All, N(1)));
390        assert_eq!(count::fold_counts(All, N(0), All, N(2)), (All, N(2)));
391        assert_eq!(count::fold_counts(All, N(0), All, All), (All, All));
392
393        assert_eq!(count::fold_counts(All, N(1), N(0), N(0)), (All, N(1)));
394        assert_eq!(count::fold_counts(All, N(1), N(0), N(1)), (All, N(2)));
395        assert_eq!(count::fold_counts(All, N(1), N(0), N(2)), (All, N(3)));
396        assert_eq!(count::fold_counts(All, N(1), N(0), All), (All, All));
397        assert_eq!(count::fold_counts(All, N(1), N(1), N(0)), (All, N(0)));
398        assert_eq!(count::fold_counts(All, N(1), N(1), N(1)), (All, N(1)));
399        assert_eq!(count::fold_counts(All, N(1), N(1), N(2)), (All, N(2)));
400        assert_eq!(count::fold_counts(All, N(1), N(1), All), (All, All));
401        assert_eq!(count::fold_counts(All, N(1), N(2), N(0)), (All, N(0)));
402        assert_eq!(count::fold_counts(All, N(1), N(2), N(1)), (All, N(1)));
403        assert_eq!(count::fold_counts(All, N(1), N(2), N(2)), (All, N(2)));
404        assert_eq!(count::fold_counts(All, N(1), N(2), All), (All, All));
405        assert_eq!(count::fold_counts(All, N(1), All, N(0)), (All, N(0)));
406        assert_eq!(count::fold_counts(All, N(1), All, N(1)), (All, N(1)));
407        assert_eq!(count::fold_counts(All, N(1), All, N(2)), (All, N(2)));
408        assert_eq!(count::fold_counts(All, N(1), All, All), (All, All));
409
410        assert_eq!(count::fold_counts(All, N(2), N(0), N(0)), (All, N(2)));
411        assert_eq!(count::fold_counts(All, N(2), N(0), N(1)), (All, N(3)));
412        assert_eq!(count::fold_counts(All, N(2), N(0), N(2)), (All, N(4)));
413        assert_eq!(count::fold_counts(All, N(2), N(0), All), (All, All));
414        assert_eq!(count::fold_counts(All, N(2), N(1), N(0)), (All, N(1)));
415        assert_eq!(count::fold_counts(All, N(2), N(1), N(1)), (All, N(2)));
416        assert_eq!(count::fold_counts(All, N(2), N(1), N(2)), (All, N(3)));
417        assert_eq!(count::fold_counts(All, N(2), N(1), All), (All, All));
418        assert_eq!(count::fold_counts(All, N(2), N(2), N(0)), (All, N(0)));
419        assert_eq!(count::fold_counts(All, N(2), N(2), N(1)), (All, N(1)));
420        assert_eq!(count::fold_counts(All, N(2), N(2), N(2)), (All, N(2)));
421        assert_eq!(count::fold_counts(All, N(2), N(2), All), (All, All));
422        assert_eq!(count::fold_counts(All, N(2), All, N(0)), (All, N(0)));
423        assert_eq!(count::fold_counts(All, N(2), All, N(1)), (All, N(1)));
424        assert_eq!(count::fold_counts(All, N(2), All, N(2)), (All, N(2)));
425        assert_eq!(count::fold_counts(All, N(2), All, All), (All, All));
426
427        assert_eq!(count::fold_counts(All, All, N(0), N(0)), (All, All));
428        assert_eq!(count::fold_counts(All, All, N(0), N(1)), (All, All));
429        assert_eq!(count::fold_counts(All, All, N(0), N(2)), (All, All));
430        assert_eq!(count::fold_counts(All, All, N(0), All), (All, All));
431        assert_eq!(count::fold_counts(All, All, N(1), N(0)), (All, All));
432        assert_eq!(count::fold_counts(All, All, N(1), N(1)), (All, All));
433        assert_eq!(count::fold_counts(All, All, N(1), N(2)), (All, All));
434        assert_eq!(count::fold_counts(All, All, N(1), All), (All, All));
435        assert_eq!(count::fold_counts(All, All, N(2), N(0)), (All, All));
436        assert_eq!(count::fold_counts(All, All, N(2), N(1)), (All, All));
437        assert_eq!(count::fold_counts(All, All, N(2), N(2)), (All, All));
438        assert_eq!(count::fold_counts(All, All, N(2), All), (All, All));
439        assert_eq!(count::fold_counts(All, All, All, N(0)), (All, N(0)));
440        assert_eq!(count::fold_counts(All, All, All, N(1)), (All, N(1)));
441        assert_eq!(count::fold_counts(All, All, All, N(2)), (All, N(2)));
442        assert_eq!(count::fold_counts(All, All, All, All), (All, All));
443    }
444
445    #[test]
446    fn test_counts_are_left_justified() {
447        assert_eq!(format!("[{:5}]", N(0)), "[     ]");
448        assert_eq!(format!("[{:5}]", N(1)), "[N    ]");
449        assert_eq!(format!("[{:5}]", N(2)), "[N N  ]");
450        assert_eq!(format!("[{:5}]", N(3)), "[3    ]");
451        assert_eq!(format!("[{:5}]", N(4)), "[4    ]");
452        assert_eq!(format!("[{:5}]", All), "[*    ]");
453    }
454
455    #[test]
456    fn test_counts_are_right_justified() {
457        assert_eq!(format!("[{:>5}]", N(0)), "[     ]");
458        assert_eq!(format!("[{:>5}]", N(1)), "[    N]");
459        assert_eq!(format!("[{:>5}]", N(2)), "[  N N]");
460        assert_eq!(format!("[{:>5}]", N(3)), "[    3]");
461        assert_eq!(format!("[{:>5}]", N(4)), "[    4]");
462        assert_eq!(format!("[{:>5}]", All), "[    *]");
463    }
464
465    #[test]
466    fn test_empty_is_replaced_with_hyphen() {
467        assert_eq!(format!("[{:-}]", N(0)), "[-]");
468        assert_eq!(format!("[{:-}]", N(1)), "[N]");
469        assert_eq!(format!("[{:-}]", N(2)), "[N N]");
470        assert_eq!(format!("[{:-}]", N(3)), "[3]");
471        assert_eq!(format!("[{:-}]", N(4)), "[4]");
472        assert_eq!(format!("[{:-}]", All), "[*]");
473    }
474}