Skip to content

Commit 1819250

Browse files
committed
Add tests to ensure that Iterator::min and Iterator::max are stable
1 parent 0de63d9 commit 1819250

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libcore/tests/iter.rs

+28
Original file line numberDiff line numberDiff line change
@@ -1082,12 +1082,39 @@ fn test_iterator_product_result() {
10821082
assert_eq!(v.iter().cloned().product::<Result<i32, _>>(), Err(()));
10831083
}
10841084

1085+
/// A wrapper struct that implements `Eq` and `Ord` based on the wrapped
1086+
/// integer modulo 3. Used to test that `Iterator::max` and `Iterator::min`
1087+
/// return the correct element if some of them are equal.
1088+
#[derive(Debug)]
1089+
struct Mod3(i32);
1090+
1091+
impl PartialEq for Mod3 {
1092+
fn eq(&self, other: &Self) -> bool {
1093+
self.0 % 3 == other.0 % 3
1094+
}
1095+
}
1096+
1097+
impl Eq for Mod3 {}
1098+
1099+
impl PartialOrd for Mod3 {
1100+
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
1101+
Some(self.cmp(other))
1102+
}
1103+
}
1104+
1105+
impl Ord for Mod3 {
1106+
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
1107+
(self.0 % 3).cmp(&(other.0 % 3))
1108+
}
1109+
}
1110+
10851111
#[test]
10861112
fn test_iterator_max() {
10871113
let v: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
10881114
assert_eq!(v[..4].iter().cloned().max(), Some(3));
10891115
assert_eq!(v.iter().cloned().max(), Some(10));
10901116
assert_eq!(v[..0].iter().cloned().max(), None);
1117+
assert_eq!(v.iter().cloned().map(Mod3).max().map(|x| x.0), Some(8));
10911118
}
10921119

10931120
#[test]
@@ -1096,6 +1123,7 @@ fn test_iterator_min() {
10961123
assert_eq!(v[..4].iter().cloned().min(), Some(0));
10971124
assert_eq!(v.iter().cloned().min(), Some(0));
10981125
assert_eq!(v[..0].iter().cloned().min(), None);
1126+
assert_eq!(v.iter().cloned().map(Mod3).min().map(|x| x.0), Some(0));
10991127
}
11001128

11011129
#[test]

0 commit comments

Comments
 (0)