Add example from lkuper's intern talk to the test suite.

This commit is contained in:
Lindsey Kuper 2012-08-09 10:45:54 -07:00
parent e82d2ef763
commit 8703d088ea

View File

@ -0,0 +1,54 @@
// Example from lkuper's intern talk, August 2012.
trait Equal {
fn isEq(a: self) -> bool;
}
enum Color { cyan, magenta, yellow, black }
impl Color : Equal {
fn isEq(a: Color) -> bool {
match (self, a) {
(cyan, cyan) => { true }
(magenta, magenta) => { true }
(yellow, yellow) => { true }
(black, black) => { true }
_ => { false }
}
}
}
enum ColorTree {
leaf(Color),
branch(@ColorTree, @ColorTree)
}
impl ColorTree : Equal {
fn isEq(a: ColorTree) -> bool {
match (self, a) {
(leaf(x), leaf(y)) => { x.isEq(y) }
(branch(l1, r1), branch(l2, r2)) => {
(*l1).isEq(*l2) && (*r1).isEq(*r2)
}
_ => { false }
}
}
}
fn main() {
assert cyan.isEq(cyan);
assert magenta.isEq(magenta);
assert !cyan.isEq(yellow);
assert !magenta.isEq(cyan);
assert leaf(cyan).isEq(leaf(cyan));
assert !leaf(cyan).isEq(leaf(yellow));
assert branch(@leaf(magenta), @leaf(cyan))
.isEq(branch(@leaf(magenta), @leaf(cyan)));
assert !branch(@leaf(magenta), @leaf(cyan))
.isEq(branch(@leaf(magenta), @leaf(magenta)));
log(error, "Assertions all succeeded!");
}