@@ -1082,12 +1082,39 @@ fn test_iterator_product_result() {
1082
1082
assert_eq ! ( v. iter( ) . cloned( ) . product:: <Result <i32 , _>>( ) , Err ( ( ) ) ) ;
1083
1083
}
1084
1084
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
+
1085
1111
#[ test]
1086
1112
fn test_iterator_max ( ) {
1087
1113
let v: & [ _ ] = & [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] ;
1088
1114
assert_eq ! ( v[ ..4 ] . iter( ) . cloned( ) . max( ) , Some ( 3 ) ) ;
1089
1115
assert_eq ! ( v. iter( ) . cloned( ) . max( ) , Some ( 10 ) ) ;
1090
1116
assert_eq ! ( v[ ..0 ] . iter( ) . cloned( ) . max( ) , None ) ;
1117
+ assert_eq ! ( v. iter( ) . cloned( ) . map( Mod3 ) . max( ) . map( |x| x. 0 ) , Some ( 8 ) ) ;
1091
1118
}
1092
1119
1093
1120
#[ test]
@@ -1096,6 +1123,7 @@ fn test_iterator_min() {
1096
1123
assert_eq ! ( v[ ..4 ] . iter( ) . cloned( ) . min( ) , Some ( 0 ) ) ;
1097
1124
assert_eq ! ( v. iter( ) . cloned( ) . min( ) , Some ( 0 ) ) ;
1098
1125
assert_eq ! ( v[ ..0 ] . iter( ) . cloned( ) . min( ) , None ) ;
1126
+ assert_eq ! ( v. iter( ) . cloned( ) . map( Mod3 ) . min( ) . map( |x| x. 0 ) , Some ( 0 ) ) ;
1099
1127
}
1100
1128
1101
1129
#[ test]
0 commit comments