iter_extra/
lib.rs

1pub mod prelude;
2pub use prelude::IterExtra;
3
4#[cfg(test)]
5mod tests {
6    use crate::prelude::*;
7
8    #[test]
9    fn min_by_partial_key_basic() {
10        let numbers = vec![3.2, 1.5, 2.8, 0.9];
11        let min = numbers.iter().min_by_partial_key(|&x| x);
12        assert_eq!(min, Some(&0.9));
13    }
14
15    #[test]
16    fn min_by_partial_key_empty() {
17        let empty: Vec<f64> = vec![];
18        let min = empty.iter().min_by_partial_key(|&x| x);
19        assert_eq!(min, None);
20    }
21
22    #[test]
23    fn min_by_partial_key_single_element() {
24        let single = vec![42.0];
25        let min = single.iter().min_by_partial_key(|&x| x);
26        assert_eq!(min, Some(&42.0));
27    }
28
29    #[test]
30    fn min_by_partial_key_with_nan() {
31        let with_nan = vec![1.0, f64::NAN, 2.0, 0.5];
32        let min = with_nan.iter().min_by_partial_key(|&x| x);
33        assert_eq!(min, Some(&0.5));
34    }
35
36    #[test]
37    fn min_by_partial_key_all_nan() {
38        let all_nan = vec![f64::NAN, f64::NAN, f64::NAN];
39        let min = all_nan.iter().min_by_partial_key(|&x| x);
40        assert!(min.is_some());
41        assert!(min.unwrap().is_nan());
42    }
43
44    #[test]
45    fn min_by_partial_key_with_key_function() {
46        let people = vec![
47            ("Alice", 25),
48            ("Bob", 30),
49            ("Charlie", 20),
50            ("Diana", 35),
51        ];
52        let youngest = people.iter().min_by_partial_key(|(_, age)| *age);
53        assert_eq!(youngest, Some(&("Charlie", 20)));
54    }
55
56    #[test]
57    fn min_by_partial_key_negative_numbers() {
58        let numbers = vec![-1.5, -3.2, -0.8, -2.1];
59        let min = numbers.iter().min_by_partial_key(|&x| x);
60        assert_eq!(min, Some(&-3.2));
61    }
62
63    #[test]
64    fn max_by_partial_key_basic() {
65        let numbers = vec![3.2, 1.5, 2.8, 0.9];
66        let max = numbers.iter().max_by_partial_key(|&x| x);
67        assert_eq!(max, Some(&3.2));
68    }
69
70    #[test]
71    fn max_by_partial_key_empty() {
72        let empty: Vec<f64> = vec![];
73        let max = empty.iter().max_by_partial_key(|&x| x);
74        assert_eq!(max, None);
75    }
76
77    #[test]
78    fn max_by_partial_key_single_element() {
79        let single = vec![42.0];
80        let max = single.iter().max_by_partial_key(|&x| x);
81        assert_eq!(max, Some(&42.0));
82    }
83
84    #[test]
85    fn max_by_partial_key_with_nan() {
86        let with_nan = vec![1.0, f64::NAN, 2.0, 0.5];
87        let max = with_nan.iter().max_by_partial_key(|&x| x);
88        assert_eq!(max, Some(&2.0));
89    }
90
91    #[test]
92    fn max_by_partial_key_all_nan() {
93        let all_nan = vec![f64::NAN, f64::NAN, f64::NAN];
94        let max = all_nan.iter().max_by_partial_key(|&x| x);
95        assert!(max.is_some());
96        assert!(max.unwrap().is_nan());
97    }
98
99    #[test]
100    fn max_by_partial_key_with_key_function() {
101        let people = vec![
102            ("Alice", 25),
103            ("Bob", 30),
104            ("Charlie", 20),
105            ("Diana", 35),
106        ];
107        let oldest = people.iter().max_by_partial_key(|(_, age)| *age);
108        assert_eq!(oldest, Some(&("Diana", 35)));
109    }
110
111    #[test]
112    fn max_by_partial_key_negative_numbers() {
113        let numbers = vec![-1.5, -3.2, -0.8, -2.1];
114        let max = numbers.iter().max_by_partial_key(|&x| x);
115        assert_eq!(max, Some(&-0.8));
116    }
117
118    #[test]
119    fn min_max_with_equal_elements() {
120        let equal = vec![5.0, 5.0, 5.0];
121        let min = equal.iter().min_by_partial_key(|&x| x);
122        let max = equal.iter().max_by_partial_key(|&x| x);
123        assert_eq!(min, Some(&5.0));
124        assert_eq!(max, Some(&5.0));
125    }
126
127    #[test]
128    fn min_max_with_infinity() {
129        let with_inf = vec![1.0, f64::INFINITY, -f64::INFINITY, 2.0];
130        let min = with_inf.iter().min_by_partial_key(|&x| x);
131        let max = with_inf.iter().max_by_partial_key(|&x| x);
132        assert_eq!(min, Some(&f64::NEG_INFINITY));
133        assert_eq!(max, Some(&f64::INFINITY));
134    }
135
136    #[test]
137    fn min_max_with_option_flatten() {
138        let arr = vec![Some(1.1f64), None, Some(1.2f64), Some(0.5f64)];
139        assert_eq!(
140            arr.iter().flatten().min_by_partial_key(|&x| x),
141            Some(&0.5f64)
142        );
143
144        let arr = vec![Some(1.1f64), None, Some(1.2f64), Some(0.5f64)];
145        assert_eq!(
146            arr.iter().flatten().max_by_partial_key(|&x| x),
147            Some(&1.2f64)
148        );
149    }
150
151    #[test]
152    fn min_max_with_complex_key_function() {
153        let points = vec![(1.0, 2.0), (3.0, 1.0), (2.0, 3.0), (0.5, 0.5)];
154        
155        let closest_to_origin = points.iter().min_by_partial_key(|(x, y)| x * x + y * y);
156        assert_eq!(closest_to_origin, Some(&(0.5, 0.5)));
157        
158        let farthest_from_origin = points.iter().max_by_partial_key(|(x, y)| x * x + y * y);
159        assert_eq!(farthest_from_origin, Some(&(2.0, 3.0)));
160    }
161
162    #[test]
163    fn min_max_with_string_length() {
164        let words = vec!["hello", "world", "rust", "programming"];
165        
166        let shortest = words.iter().min_by_partial_key(|s| s.len());
167        assert_eq!(shortest, Some(&"rust"));
168        
169        let longest = words.iter().max_by_partial_key(|s| s.len());
170        assert_eq!(longest, Some(&"programming"));
171    }
172}