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