@@ -1109,51 +1109,37 @@ mod test_set {
1109
1109
}
1110
1110
}
1111
1111
1112
- # [ test ]
1113
- fn test_intersection ( ) {
1114
- let mut a = TreeSet :: new( ) ;
1115
- let mut b = TreeSet :: new( ) ;
1112
+ fn check ( a : & [ int ] , b : & [ int ] , expected : & [ int ] ,
1113
+ f : & fn ( & TreeSet <int> , & TreeSet <int> , f : & fn ( & int ) -> bool ) ) {
1114
+ let mut set_a = TreeSet :: new( ) ;
1115
+ let mut set_b = TreeSet :: new( ) ;
1116
1116
1117
- fail_unless!( a. insert( 11 ) ) ;
1118
- fail_unless!( a. insert( 1 ) ) ;
1119
- fail_unless!( a. insert( 3 ) ) ;
1120
- fail_unless!( a. insert( 77 ) ) ;
1121
- fail_unless!( a. insert( 103 ) ) ;
1122
- fail_unless!( a. insert( 5 ) ) ;
1123
- fail_unless!( a. insert( -5 ) ) ;
1124
-
1125
- fail_unless!( b. insert( 2 ) ) ;
1126
- fail_unless!( b. insert( 11 ) ) ;
1127
- fail_unless!( b. insert( 77 ) ) ;
1128
- fail_unless!( b. insert( -9 ) ) ;
1129
- fail_unless!( b. insert( -42 ) ) ;
1130
- fail_unless!( b. insert( 5 ) ) ;
1131
- fail_unless!( b. insert( 3 ) ) ;
1117
+ for a. each |x| { fail_unless!( set_a. insert( * x) ) }
1118
+ for b. each |y| { fail_unless!( set_b. insert( * y) ) }
1132
1119
1133
1120
let mut i = 0 ;
1134
- let expected = [ 3 , 5 , 11 , 77 ] ;
1135
- for a. intersection( & b) |x| {
1121
+ for f( & set_a, & set_b) |x| {
1136
1122
fail_unless!( * x == expected[ i] ) ;
1137
- i += 1
1123
+ i += 1 ;
1138
1124
}
1139
1125
fail_unless!( i == expected. len( ) ) ;
1140
1126
}
1141
1127
1142
1128
#[ test]
1143
- fn test_difference ( ) {
1144
- fn check_difference ( a: & [ int] , b: & [ int] , expected: & [ int] ) {
1145
- let mut set_a = TreeSet :: new ( ) ;
1146
- let mut set_b = TreeSet :: new ( ) ;
1129
+ fn test_intersection ( ) {
1130
+ fn check_intersection ( a: & [ int] , b: & [ int] , expected: & [ int] ) {
1131
+ check ( a , b , expected , |x , y , z| x . intersection ( y , z ) )
1132
+ }
1147
1133
1148
- for a. each |x| { fail_unless!( set_a. insert( * x) ) }
1149
- for b. each |y| { fail_unless!( set_b. insert( * y) ) }
1134
+ check_intersection( [ 11 , 1 , 3 , 77 , 103 , 5 , -5 ] ,
1135
+ [ 2 , 11 , 77 , -9 , -42 , 5 , 3 ] ,
1136
+ [ 3 , 5 , 11 , 77 ] ) ;
1137
+ }
1150
1138
1151
- let mut i = 0 ;
1152
- for set_a. difference( & set_b) |x| {
1153
- fail_unless!( * x == expected[ i] ) ;
1154
- i += 1 ;
1155
- }
1156
- fail_unless!( i == expected. len( ) ) ;
1139
+ #[ test]
1140
+ fn test_difference( ) {
1141
+ fn check_difference( a: & [ int] , b: & [ int] , expected: & [ int] ) {
1142
+ check( a, b, expected, |x, y, z| x. difference( y, z) )
1157
1143
}
1158
1144
1159
1145
check_difference( [ ] , [ ] , [ ] ) ;
@@ -1169,57 +1155,25 @@ mod test_set {
1169
1155
1170
1156
#[ test]
1171
1157
fn test_symmetric_difference( ) {
1172
- let mut a = TreeSet : : new( ) ;
1173
- let mut b = TreeSet :: new( ) ;
1174
-
1175
- fail_unless ! ( a. insert( 1 ) ) ;
1176
- fail_unless ! ( a. insert( 3 ) ) ;
1177
- fail_unless ! ( a. insert( 5 ) ) ;
1178
- fail_unless ! ( a. insert( 9 ) ) ;
1179
- fail_unless ! ( a. insert( 11 ) ) ;
1180
-
1181
- fail_unless ! ( b. insert( -2 ) ) ;
1182
- fail_unless ! ( b. insert( 3 ) ) ;
1183
- fail_unless ! ( b. insert( 9 ) ) ;
1184
- fail_unless ! ( b. insert( 14 ) ) ;
1185
- fail_unless ! ( b. insert( 22 ) ) ;
1186
-
1187
- let mut i = 0 ;
1188
- let expected = [ -2 , 1 , 5 , 11 , 14 , 22 ] ;
1189
- for a. symmetric_difference( & b) |x| {
1190
- fail_unless ! ( * x == expected[ i] ) ;
1191
- i += 1
1158
+ fn check_symmetric_difference( a: & [ int] , b: & [ int] ,
1159
+ expected: & [ int] ) {
1160
+ check( a, b, expected, |x, y, z| x. symmetric_difference( y, z) )
1192
1161
}
1193
- fail_unless ! ( i == expected. len( ) ) ;
1162
+
1163
+ check_symmetric_difference( [ 1 , 3 , 5 , 9 , 11 ] ,
1164
+ [ -2 , 3 , 9 , 14 , 22 ] ,
1165
+ [ -2 , 1 , 5 , 11 , 14 , 22 ] ) ;
1194
1166
}
1195
1167
1196
1168
#[ test]
1197
1169
fn test_union( ) {
1198
- let mut a = TreeSet : : new( ) ;
1199
- let mut b = TreeSet :: new( ) ;
1200
-
1201
- fail_unless ! ( a. insert( 1 ) ) ;
1202
- fail_unless ! ( a. insert( 3 ) ) ;
1203
- fail_unless ! ( a. insert( 5 ) ) ;
1204
- fail_unless ! ( a. insert( 9 ) ) ;
1205
- fail_unless ! ( a. insert( 11 ) ) ;
1206
- fail_unless ! ( a. insert( 16 ) ) ;
1207
- fail_unless ! ( a. insert( 19 ) ) ;
1208
- fail_unless ! ( a. insert( 24 ) ) ;
1209
-
1210
- fail_unless ! ( b. insert( -2 ) ) ;
1211
- fail_unless ! ( b. insert( 1 ) ) ;
1212
- fail_unless ! ( b. insert( 5 ) ) ;
1213
- fail_unless ! ( b. insert( 9 ) ) ;
1214
- fail_unless ! ( b. insert( 13 ) ) ;
1215
- fail_unless ! ( b. insert( 19 ) ) ;
1216
-
1217
- let mut i = 0 ;
1218
- let expected = [ -2 , 1 , 3 , 5 , 9 , 11 , 13 , 16 , 19 , 24 ] ;
1219
- for a. union ( & b) |x| {
1220
- fail_unless ! ( * x == expected[ i] ) ;
1221
- i += 1
1170
+ fn check_union( a: & [ int] , b: & [ int] ,
1171
+ expected: & [ int] ) {
1172
+ check( a, b, expected, |x, y, z| x. union ( y, z) )
1222
1173
}
1223
- fail_unless ! ( i == expected. len( ) ) ;
1174
+
1175
+ check_union( [ 1 , 3 , 5 , 9 , 11 , 16 , 19 , 24 ] ,
1176
+ [ -2 , 1 , 5 , 9 , 13 , 19 ] ,
1177
+ [ -2 , 1 , 3 , 5 , 9 , 11 , 13 , 16 , 19 , 24 ] ) ;
1224
1178
}
1225
1179
}
0 commit comments