-
Notifications
You must be signed in to change notification settings - Fork 3
/
min_max.rs
71 lines (56 loc) · 2.57 KB
/
min_max.rs
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
#![cfg(feature = "min_max")]
use std::cmp::Reverse;
use std::iter;
use itermore::prelude::*;
#[test]
fn min_max() {
assert_eq!(iter::empty::<i32>().min_max(), None);
assert_eq!([1].into_iter().min_max(), Some((1, 1)));
assert_eq!([1, 2].into_iter().min_max(), Some((1, 2)));
assert_eq!([2, 1].into_iter().min_max(), Some((1, 2)));
assert_eq!([1, 2, 3].into_iter().min_max(), Some((1, 3)));
assert_eq!([1, 3, 2].into_iter().min_max(), Some((1, 3)));
assert_eq!([2, 1, 3].into_iter().min_max(), Some((1, 3)));
assert_eq!([2, 3, 1].into_iter().min_max(), Some((1, 3)));
assert_eq!([3, 1, 2].into_iter().min_max(), Some((1, 3)));
assert_eq!([3, 2, 1].into_iter().min_max(), Some((1, 3)));
}
#[test]
fn min_max_by() {
let rev = |a: &i32, b: &i32| Reverse(a).cmp(&Reverse(b));
assert_eq!(iter::empty::<i32>().min_max_by(rev), None);
assert_eq!([1].into_iter().min_max_by(rev), Some((1, 1)));
assert_eq!([1, 2].into_iter().min_max_by(rev), Some((2, 1)));
assert_eq!([2, 1].into_iter().min_max_by(rev), Some((2, 1)));
assert_eq!([1, 2, 3].into_iter().min_max_by(rev), Some((3, 1)));
assert_eq!([1, 3, 2].into_iter().min_max_by(rev), Some((3, 1)));
assert_eq!([2, 1, 3].into_iter().min_max_by(rev), Some((3, 1)));
assert_eq!([2, 3, 1].into_iter().min_max_by(rev), Some((3, 1)));
assert_eq!([3, 1, 2].into_iter().min_max_by(rev), Some((3, 1)));
assert_eq!([3, 2, 1].into_iter().min_max_by(rev), Some((3, 1)));
}
#[test]
fn min_max_by_key() {
let key = |item: &i32| -item;
assert_eq!(iter::empty::<i32>().min_max_by_key(key), None);
assert_eq!([1].into_iter().min_max_by_key(key), Some((1, 1)));
assert_eq!([1, 2].into_iter().min_max_by_key(key), Some((2, 1)));
assert_eq!([2, 1].into_iter().min_max_by_key(key), Some((2, 1)));
assert_eq!([1, 2, 3].into_iter().min_max_by_key(key), Some((3, 1)));
assert_eq!([1, 3, 2].into_iter().min_max_by_key(key), Some((3, 1)));
assert_eq!([2, 1, 3].into_iter().min_max_by_key(key), Some((3, 1)));
assert_eq!([2, 3, 1].into_iter().min_max_by_key(key), Some((3, 1)));
assert_eq!([3, 1, 2].into_iter().min_max_by_key(key), Some((3, 1)));
assert_eq!([3, 2, 1].into_iter().min_max_by_key(key), Some((3, 1)));
}
#[test]
fn min_max_shuffled() {
use rand::prelude::*;
let mut rng = rand::thread_rng();
let mut data = (0..100).collect::<Vec<_>>();
for _ in 0..100 {
data.shuffle(&mut rng);
assert_eq!(data.iter().copied().min_max(), Some((0, 99)));
assert_eq!(data.iter().copied().min_max_by_key(|x| -x), Some((99, 0)));
}
}