Add example from lkuper's intern talk to the test suite.
This commit is contained in:
parent
e82d2ef763
commit
8703d088ea
54
src/test/run-pass/typeclasses-eq-example.rs
Executable file
54
src/test/run-pass/typeclasses-eq-example.rs
Executable 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!");
|
||||
}
|
Loading…
Reference in New Issue
Block a user