]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-13027.rs
auto merge of #15222 : alexcrichton/rust/relnotes, r=brson
[rust.git] / src / test / run-pass / issue-13027.rs
1 // Copyright 2012-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 // Tests that match expression handles overlapped literal and range
12 // properly in the presence of guard function.
13
14 fn val() -> uint { 1 }
15
16 static CONST: uint = 1;
17
18 pub fn main() {
19     lit_shadow_range();
20     range_shadow_lit();
21     range_shadow_range();
22     multi_pats_shadow_lit();
23     multi_pats_shadow_range();
24     lit_shadow_multi_pats();
25     range_shadow_multi_pats();
26     misc();
27 }
28
29 fn lit_shadow_range() {
30     assert_eq!(2i, match 1i {
31         1 if false => 1i,
32         1..2 => 2,
33         _ => 3
34     });
35
36     let x = 0i;
37     assert_eq!(2i, match x+1 {
38         0 => 0i,
39         1 if false => 1,
40         1..2 => 2,
41         _ => 3
42     });
43
44     assert_eq!(2i, match val() {
45         1 if false => 1i,
46         1..2 => 2,
47         _ => 3
48     });
49
50     assert_eq!(2i, match CONST {
51         0 => 0i,
52         1 if false => 1,
53         1..2 => 2,
54         _ => 3
55     });
56
57     // value is out of the range of second arm, should match wildcard pattern
58     assert_eq!(3i, match 3i {
59         1 if false => 1i,
60         1..2 => 2,
61         _ => 3
62     });
63 }
64
65 fn range_shadow_lit() {
66     assert_eq!(2i, match 1i {
67         1..2 if false => 1i,
68         1 => 2,
69         _ => 3
70     });
71
72     let x = 0i;
73     assert_eq!(2i, match x+1 {
74         0 => 0i,
75         1..2 if false => 1,
76         1 => 2,
77         _ => 3
78     });
79
80     assert_eq!(2i, match val() {
81         1..2 if false => 1i,
82         1 => 2,
83         _ => 3
84     });
85
86     assert_eq!(2i, match CONST {
87         0 => 0i,
88         1..2 if false => 1,
89         1 => 2,
90         _ => 3
91     });
92
93     // ditto
94     assert_eq!(3i, match 3i {
95         1..2 if false => 1i,
96         1 => 2,
97         _ => 3
98     });
99 }
100
101 fn range_shadow_range() {
102     assert_eq!(2i, match 1i {
103         0..2 if false => 1i,
104         1..3 => 2,
105         _ => 3,
106     });
107
108     let x = 0i;
109     assert_eq!(2i, match x+1 {
110         100 => 0,
111         0..2 if false => 1,
112         1..3 => 2,
113         _ => 3,
114     });
115
116     assert_eq!(2i, match val() {
117         0..2 if false => 1,
118         1..3 => 2,
119         _ => 3,
120     });
121
122     assert_eq!(2i, match CONST {
123         100 => 0,
124         0..2 if false => 1,
125         1..3 => 2,
126         _ => 3,
127     });
128
129     // ditto
130     assert_eq!(3i, match 5i {
131         0..2 if false => 1i,
132         1..3 => 2,
133         _ => 3,
134     });
135 }
136
137 fn multi_pats_shadow_lit() {
138     assert_eq!(2i, match 1i {
139         100 => 0i,
140         0 | 1..10 if false => 1,
141         1 => 2,
142         _ => 3,
143     });
144 }
145
146 fn multi_pats_shadow_range() {
147     assert_eq!(2i, match 1i {
148         100 => 0i,
149         0 | 1..10 if false => 1,
150         1..3 => 2,
151         _ => 3,
152     });
153 }
154
155 fn lit_shadow_multi_pats() {
156     assert_eq!(2i, match 1i {
157         100 => 0i,
158         1 if false => 1,
159         0 | 1..10 => 2,
160         _ => 3,
161     });
162 }
163
164 fn range_shadow_multi_pats() {
165     assert_eq!(2i, match 1i {
166         100 => 0i,
167         1..3 if false => 1,
168         0 | 1..10 => 2,
169         _ => 3,
170     });
171 }
172
173 fn misc() {
174     enum Foo {
175         Bar(uint, bool)
176     }
177     // This test basically mimics how trace_macros! macro is implemented,
178     // which is a rare combination of vector patterns, multiple wild-card
179     // patterns and guard functions.
180     let r = match [Bar(0, false)].as_slice() {
181         [Bar(_, pred)] if pred => 1i,
182         [Bar(_, pred)] if !pred => 2i,
183         _ => 0i,
184     };
185     assert_eq!(2i, r);
186 }