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