]> git.lizzy.rs Git - rust.git/blob - src/test/ui/traits/typeclasses-eq-example-static.rs
Add regression test for #82830
[rust.git] / src / test / ui / traits / typeclasses-eq-example-static.rs
1 // run-pass
2
3 #![allow(non_camel_case_types)]
4 #![allow(non_snake_case)]
5 #![allow(dead_code)]
6
7 // Example from lkuper's intern talk, August 2012 -- now with static
8 // methods!
9 use Color::{cyan, magenta, yellow, black};
10 use ColorTree::{leaf, branch};
11
12 trait Equal {
13     fn isEq(a: &Self, b: &Self) -> bool;
14 }
15
16 #[derive(Clone, Copy)]
17 enum Color { cyan, magenta, yellow, black }
18
19 impl Equal for Color {
20     fn isEq(a: &Color, b: &Color) -> bool {
21         match (*a, *b) {
22           (cyan, cyan)       => { true  }
23           (magenta, magenta) => { true  }
24           (yellow, yellow)   => { true  }
25           (black, black)     => { true  }
26           _                  => { false }
27         }
28     }
29 }
30
31 #[derive(Clone)]
32 enum ColorTree {
33     leaf(Color),
34     branch(Box<ColorTree>, Box<ColorTree>)
35 }
36
37 impl Equal for ColorTree {
38     fn isEq(a: &ColorTree, b: &ColorTree) -> bool {
39         match (a, b) {
40           (&leaf(ref x), &leaf(ref y)) => {
41               Equal::isEq(&(*x).clone(), &(*y).clone())
42           }
43           (&branch(ref l1, ref r1), &branch(ref l2, ref r2)) => {
44             Equal::isEq(&(**l1).clone(), &(**l2).clone()) &&
45                 Equal::isEq(&(**r1).clone(), &(**r2).clone())
46           }
47           _ => { false }
48         }
49     }
50 }
51
52 pub fn main() {
53     assert!(Equal::isEq(&cyan, &cyan));
54     assert!(Equal::isEq(&magenta, &magenta));
55     assert!(!Equal::isEq(&cyan, &yellow));
56     assert!(!Equal::isEq(&magenta, &cyan));
57
58     assert!(Equal::isEq(&leaf(cyan), &leaf(cyan)));
59     assert!(!Equal::isEq(&leaf(cyan), &leaf(yellow)));
60
61     assert!(Equal::isEq(&branch(Box::new(leaf(magenta)), Box::new(leaf(cyan))),
62                 &branch(Box::new(leaf(magenta)), Box::new(leaf(cyan)))));
63
64     assert!(!Equal::isEq(&branch(Box::new(leaf(magenta)), Box::new(leaf(cyan))),
65                  &branch(Box::new(leaf(magenta)), Box::new(leaf(magenta)))));
66
67     println!("Assertions all succeeded!");
68 }