Skip to content

Commit 8703d08

Browse files
committed
Add example from lkuper's intern talk to the test suite.
1 parent e82d2ef commit 8703d08

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Example from lkuper's intern talk, August 2012.
2+
3+
trait Equal {
4+
fn isEq(a: self) -> bool;
5+
}
6+
7+
enum Color { cyan, magenta, yellow, black }
8+
9+
impl Color : Equal {
10+
fn isEq(a: Color) -> bool {
11+
match (self, a) {
12+
(cyan, cyan) => { true }
13+
(magenta, magenta) => { true }
14+
(yellow, yellow) => { true }
15+
(black, black) => { true }
16+
_ => { false }
17+
}
18+
}
19+
}
20+
21+
enum ColorTree {
22+
leaf(Color),
23+
branch(@ColorTree, @ColorTree)
24+
}
25+
26+
impl ColorTree : Equal {
27+
fn isEq(a: ColorTree) -> bool {
28+
match (self, a) {
29+
(leaf(x), leaf(y)) => { x.isEq(y) }
30+
(branch(l1, r1), branch(l2, r2)) => {
31+
(*l1).isEq(*l2) && (*r1).isEq(*r2)
32+
}
33+
_ => { false }
34+
}
35+
}
36+
}
37+
38+
fn main() {
39+
assert cyan.isEq(cyan);
40+
assert magenta.isEq(magenta);
41+
assert !cyan.isEq(yellow);
42+
assert !magenta.isEq(cyan);
43+
44+
assert leaf(cyan).isEq(leaf(cyan));
45+
assert !leaf(cyan).isEq(leaf(yellow));
46+
47+
assert branch(@leaf(magenta), @leaf(cyan))
48+
.isEq(branch(@leaf(magenta), @leaf(cyan)));
49+
50+
assert !branch(@leaf(magenta), @leaf(cyan))
51+
.isEq(branch(@leaf(magenta), @leaf(magenta)));
52+
53+
log(error, "Assertions all succeeded!");
54+
}

0 commit comments

Comments
 (0)