Skip to content

Commit 66afa5d

Browse files
committed
treemap: refactor the set operation tests
1 parent d55225f commit 66afa5d

File tree

1 file changed

+34
-80
lines changed

1 file changed

+34
-80
lines changed

src/libstd/treemap.rs

Lines changed: 34 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,51 +1109,37 @@ mod test_set {
11091109
}
11101110
}
11111111

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();
11161116

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)) }
11321119

11331120
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| {
11361122
fail_unless!(*x == expected[i]);
1137-
i += 1
1123+
i += 1;
11381124
}
11391125
fail_unless!(i == expected.len());
11401126
}
11411127

11421128
#[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+
}
11471133

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+
}
11501138

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))
11571143
}
11581144

11591145
check_difference([], [], []);
@@ -1169,57 +1155,25 @@ mod test_set {
11691155

11701156
#[test]
11711157
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))
11921161
}
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]);
11941166
}
11951167

11961168
#[test]
11971169
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))
12221173
}
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]);
12241178
}
12251179
}

0 commit comments

Comments
 (0)