]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/match-arm-statics.rs
prefer "FIXME" to "TODO".
[rust.git] / src / test / run-pass / match-arm-statics.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 struct NewBool(bool);
12
13 enum Direction {
14     North,
15     East,
16     South,
17     West
18 }
19 struct Foo {
20     bar: Option<Direction>,
21     baz: NewBool
22 }
23 enum EnumWithStructVariants {
24     Variant1(bool),
25     Variant2 {
26         dir: Direction
27     }
28 }
29
30 const TRUE_TRUE: (bool, bool) = (true, true);
31 const NONE: Option<Direction> = None;
32 const EAST: Direction = Direction::East;
33 const NEW_FALSE: NewBool = NewBool(false);
34 const STATIC_FOO: Foo = Foo { bar: Some(Direction::South), baz: NEW_FALSE };
35 const VARIANT2_NORTH: EnumWithStructVariants = EnumWithStructVariants::Variant2 {
36     dir: Direction::North };
37
38 pub mod glfw {
39     pub struct InputState(uint);
40
41     pub const RELEASE  : InputState = InputState(0);
42     pub const PRESS    : InputState = InputState(1);
43     pub const REPEAT   : InputState = InputState(2);
44 }
45
46 fn issue_6533() {
47     use glfw;
48
49     fn action_to_str(state: glfw::InputState) -> &'static str {
50         use glfw::{RELEASE, PRESS, REPEAT};
51         match state {
52             RELEASE => { "Released" }
53             PRESS   => { "Pressed"  }
54             REPEAT  => { "Repeated" }
55             _       => { "Unknown"  }
56         }
57     }
58
59     assert_eq!(action_to_str(glfw::RELEASE), "Released");
60     assert_eq!(action_to_str(glfw::PRESS), "Pressed");
61     assert_eq!(action_to_str(glfw::REPEAT), "Repeated");
62 }
63
64 fn issue_13626() {
65     const VAL: [u8, ..1] = [0];
66     match [1] {
67         VAL => unreachable!(),
68         _ => ()
69     }
70 }
71
72 fn issue_14576() {
73     type Foo = (i32, i32);
74     const ON: Foo = (1, 1);
75     const OFF: Foo = (0, 0);
76
77     match (1, 1) {
78         OFF => unreachable!(),
79         ON => (),
80         _ => unreachable!()
81     }
82
83     enum C { D = 3, E = 4 }
84     const F : C = C::D;
85
86     assert_eq!(match C::D { F => 1i, _ => 2, }, 1);
87 }
88
89 fn issue_13731() {
90     enum A { AA(()) }
91     const B: A = A::AA(());
92
93     match A::AA(()) {
94         B => ()
95     }
96 }
97
98 fn issue_15393() {
99     #![allow(dead_code)]
100     struct Flags {
101         bits: uint
102     }
103
104     const FOO: Flags = Flags { bits: 0x01 };
105     const BAR: Flags = Flags { bits: 0x02 };
106     match (Flags { bits: 0x02 }) {
107         FOO => unreachable!(),
108         BAR => (),
109         _ => unreachable!()
110     }
111 }
112
113 fn main() {
114     assert_eq!(match (true, false) {
115         TRUE_TRUE => 1i,
116         (false, false) => 2,
117         (false, true) => 3,
118         (true, false) => 4
119     }, 4);
120
121     assert_eq!(match Some(Some(Direction::North)) {
122         Some(NONE) => 1i,
123         Some(Some(Direction::North)) => 2,
124         Some(Some(EAST)) => 3,
125         Some(Some(Direction::South)) => 4,
126         Some(Some(Direction::West)) => 5,
127         None => 6
128     }, 2);
129
130     assert_eq!(match (Foo { bar: Some(Direction::West), baz: NewBool(true) }) {
131         Foo { bar: None, baz: NewBool(true) } => 1i,
132         Foo { bar: NONE, baz: NEW_FALSE } => 2,
133         STATIC_FOO => 3,
134         Foo { bar: _, baz: NEW_FALSE } => 4,
135         Foo { bar: Some(Direction::West), baz: NewBool(true) } => 5,
136         Foo { bar: Some(Direction::South), baz: NewBool(true) } => 6,
137         Foo { bar: Some(EAST), .. } => 7,
138         Foo { bar: Some(Direction::North), baz: NewBool(true) } => 8
139     }, 5);
140
141     assert_eq!(match (EnumWithStructVariants::Variant2 { dir: Direction::North }) {
142         EnumWithStructVariants::Variant1(true) => 1i,
143         EnumWithStructVariants::Variant1(false) => 2,
144         EnumWithStructVariants::Variant2 { dir: Direction::West } => 3,
145         VARIANT2_NORTH => 4,
146         EnumWithStructVariants::Variant2 { dir: Direction::South } => 5,
147         EnumWithStructVariants::Variant2 { dir: Direction::East } => 6
148     }, 4);
149
150     issue_6533();
151     issue_13626();
152     issue_13731();
153     issue_14576();
154     issue_15393();
155 }