Skip to content

Commit b3cd056

Browse files
author
Jorge Aparicio
committed
libcollections: fix unit tests
1 parent 10a14d5 commit b3cd056

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

src/libcollections/btree/set.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -603,22 +603,31 @@ mod test {
603603
assert!(hash::hash(&x) == hash::hash(&y));
604604
}
605605

606-
fn check(a: &[int],
607-
b: &[int],
608-
expected: &[int],
609-
f: |&BTreeSet<int>, &BTreeSet<int>, f: |&int| -> bool| -> bool) {
606+
struct Counter<'a, 'b> {
607+
i: &'a mut uint,
608+
expected: &'b [int],
609+
}
610+
611+
impl<'a, 'b> FnMut(&int) -> bool for Counter<'a, 'b> {
612+
extern "rust-call" fn call_mut(&mut self, (&x,): (&int,)) -> bool {
613+
assert_eq!(x, self.expected[*self.i]);
614+
*self.i += 1;
615+
true
616+
}
617+
}
618+
619+
fn check<F>(a: &[int], b: &[int], expected: &[int], f: F) where
620+
// FIXME Replace Counter with `Box<FnMut(_) -> _>`
621+
F: FnOnce(&BTreeSet<int>, &BTreeSet<int>, Counter) -> bool,
622+
{
610623
let mut set_a = BTreeSet::new();
611624
let mut set_b = BTreeSet::new();
612625

613626
for x in a.iter() { assert!(set_a.insert(*x)) }
614627
for y in b.iter() { assert!(set_b.insert(*y)) }
615628

616629
let mut i = 0;
617-
f(&set_a, &set_b, |x| {
618-
assert_eq!(*x, expected[i]);
619-
i += 1;
620-
true
621-
});
630+
f(&set_a, &set_b, Counter { i: &mut i, expected: expected });
622631
assert_eq!(i, expected.len());
623632
}
624633

src/libcollections/tree/set.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -936,22 +936,31 @@ mod test {
936936
assert!(hash::hash(&x) == hash::hash(&y));
937937
}
938938

939-
fn check(a: &[int],
940-
b: &[int],
941-
expected: &[int],
942-
f: |&TreeSet<int>, &TreeSet<int>, f: |&int| -> bool| -> bool) {
939+
struct Counter<'a, 'b> {
940+
i: &'a mut uint,
941+
expected: &'b [int],
942+
}
943+
944+
impl<'a, 'b> FnMut(&int) -> bool for Counter<'a, 'b> {
945+
extern "rust-call" fn call_mut(&mut self, (&x,): (&int,)) -> bool {
946+
assert_eq!(x, self.expected[*self.i]);
947+
*self.i += 1;
948+
true
949+
}
950+
}
951+
952+
fn check<F>(a: &[int], b: &[int], expected: &[int], f: F) where
953+
// FIXME Replace `Counter` with `Box<FnMut(&int) -> bool>`
954+
F: FnOnce(&TreeSet<int>, &TreeSet<int>, Counter) -> bool,
955+
{
943956
let mut set_a = TreeSet::new();
944957
let mut set_b = TreeSet::new();
945958

946959
for x in a.iter() { assert!(set_a.insert(*x)) }
947960
for y in b.iter() { assert!(set_b.insert(*y)) }
948961

949962
let mut i = 0;
950-
f(&set_a, &set_b, |x| {
951-
assert_eq!(*x, expected[i]);
952-
i += 1;
953-
true
954-
});
963+
f(&set_a, &set_b, Counter { i: &mut i, expected: expected });
955964
assert_eq!(i, expected.len());
956965
}
957966

src/libcollections/trie/set.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -743,22 +743,31 @@ mod test {
743743
assert!(a < b && a <= b);
744744
}
745745

746-
fn check(a: &[uint],
747-
b: &[uint],
748-
expected: &[uint],
749-
f: |&TrieSet, &TrieSet, f: |uint| -> bool| -> bool) {
746+
struct Counter<'a, 'b> {
747+
i: &'a mut uint,
748+
expected: &'b [uint],
749+
}
750+
751+
impl<'a, 'b> FnMut(uint) -> bool for Counter<'a, 'b> {
752+
extern "rust-call" fn call_mut(&mut self, (x,): (uint,)) -> bool {
753+
assert_eq!(x, self.expected[*self.i]);
754+
*self.i += 1;
755+
true
756+
}
757+
}
758+
759+
fn check<F>(a: &[uint], b: &[uint], expected: &[uint], f: F) where
760+
// FIXME Replace `Counter` with `Box<FnMut(&uint) -> bool>`
761+
F: FnOnce(&TrieSet, &TrieSet, Counter) -> bool,
762+
{
750763
let mut set_a = TrieSet::new();
751764
let mut set_b = TrieSet::new();
752765

753766
for x in a.iter() { assert!(set_a.insert(*x)) }
754767
for y in b.iter() { assert!(set_b.insert(*y)) }
755768

756769
let mut i = 0;
757-
f(&set_a, &set_b, |x| {
758-
assert_eq!(x, expected[i]);
759-
i += 1;
760-
true
761-
});
770+
f(&set_a, &set_b, Counter { i: &mut i, expected: expected });
762771
assert_eq!(i, expected.len());
763772
}
764773

0 commit comments

Comments
 (0)