Add a bunch of tests for blocks.

This commit is contained in:
Michael Sullivan 2011-07-25 16:57:27 -07:00
parent 9ca0ce91bf
commit 4c17cb73a2
6 changed files with 90 additions and 0 deletions

View file

@ -0,0 +1,20 @@
// error-pattern: mismatched types
// xfail-stage0
// Make sure that fn-to-block coercion isn't incorrectly lifted over
// other tycons.
fn coerce(&block() b) -> fn() {
fn lol(&fn(&block()) -> fn() f, &block() g) -> fn() {
ret f(g);
}
fn fn_id (&fn() f) -> fn() { ret f }
ret lol(fn_id, b);
}
fn main() {
auto i = 8;
auto f = coerce(block() { log_err i; } );
f();
}

View file

@ -0,0 +1,9 @@
// error-pattern: non-copyable
// xfail-stage0
fn lol(&block() f) -> block() { ret f; }
fn main() {
auto i = 8;
auto f = lol(block() { log_err i; } );
f();
}

View file

@ -0,0 +1,8 @@
// error-pattern: Unsatisfied precondition constraint
// xfail-stage0
fn force(&block() f) { f(); }
fn main() {
let int x;
force(block() { log_err x; });
}

View file

@ -0,0 +1,9 @@
// xfail-stage0
fn force(&block() -> int f) -> int { ret f(); }
fn main() {
auto f = fn() -> int { ret 7 };
assert(force(f) == 7);
auto g = bind force(f);
assert(g() == 7);
}

View file

@ -0,0 +1,22 @@
// xfail-stage0
fn iter_vec[T](&vec[T] v, &block (&T) f) {
for (T x in v) {
f(x);
}
}
fn main() {
auto v = [1,2,3,4,5,6,7];
auto odds = 0;
iter_vec(v,
block (&int i) {
log_err i;
if (i % 2 == 1) {
odds += 1;
}
log_err odds;
});
log_err odds;
assert(odds == 4);
}

View file

@ -0,0 +1,22 @@
// xfail-stage0
fn iter_vec[T](&vec[T] v, &block (&T) f) {
for (T x in v) {
f(x);
}
}
fn main() {
auto v = [1,2,3,4,5];
auto sum = 0;
iter_vec(v, block (&int i)
{
iter_vec(v, block (&int j)
{
log_err i*j;
sum += i*j;
});
});
log_err sum;
assert(sum == 225);
}