Skip to content

Commit 0687055

Browse files
dlrobertsonmatthewjasper
authored andcommitted
Basic run-pass tests for or-patterns
Add some basic run-pass ui tests for or-patterns.
1 parent 864a65f commit 0687055

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Test basic or-patterns when the target pattern type will be lowered to a
2+
// `SwitchInt` (an `enum`).
3+
4+
// run-pass
5+
6+
#![feature(or_patterns)]
7+
8+
#[derive(Debug)]
9+
enum Test {
10+
Foo,
11+
Bar,
12+
Baz,
13+
Qux,
14+
}
15+
16+
fn test(x: Option<Test>) -> bool {
17+
match x {
18+
// most simple case
19+
Some(Test::Bar | Test::Qux) => true,
20+
// wild case
21+
Some(_) => false,
22+
// empty case
23+
None => false,
24+
}
25+
}
26+
27+
fn main() {
28+
assert!(!test(Some(Test::Foo)));
29+
assert!(test(Some(Test::Bar)));
30+
assert!(!test(Some(Test::Baz)));
31+
assert!(test(Some(Test::Qux)));
32+
assert!(!test(None))
33+
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Test basic or-patterns when the target pattern type will be lowered to
2+
// a `Switch`. This will happen when the target type is an integer.
3+
4+
// run-pass
5+
6+
#![feature(or_patterns)]
7+
8+
#[derive(Debug, PartialEq)]
9+
enum MatchArm {
10+
Arm(usize),
11+
Wild,
12+
}
13+
14+
#[derive(Debug)]
15+
enum Foo {
16+
One(usize),
17+
Two(usize, usize),
18+
}
19+
20+
fn test_foo(x: Foo) -> MatchArm {
21+
match x {
22+
// normal pattern.
23+
Foo::One(0) | Foo::One(1) | Foo::One(2) => MatchArm::Arm(0),
24+
// most simple or-pattern.
25+
Foo::One(42 | 255) => MatchArm::Arm(1),
26+
// multiple or-patterns for one structure.
27+
Foo::Two(42 | 255, 1024 | 2048) => MatchArm::Arm(2),
28+
// mix of pattern types in one or-pattern (range).
29+
//
30+
// FIXME(dlrobertson | Nadrieril): Fix or-pattern completeness and
31+
// unreachabilitychecks for ranges.
32+
Foo::One(100 | 110..=120 | 210..=220) => MatchArm::Arm(3),
33+
// multiple or-patterns with wild.
34+
Foo::Two(0..=10 | 100..=110, 0 | _) => MatchArm::Arm(4),
35+
// wild
36+
_ => MatchArm::Wild,
37+
}
38+
}
39+
40+
fn main() {
41+
// `Foo` tests.
42+
assert_eq!(test_foo(Foo::One(0)), MatchArm::Arm(0));
43+
assert_eq!(test_foo(Foo::One(42)), MatchArm::Arm(1));
44+
assert_eq!(test_foo(Foo::One(43)), MatchArm::Wild);
45+
assert_eq!(test_foo(Foo::One(255)), MatchArm::Arm(1));
46+
assert_eq!(test_foo(Foo::One(256)), MatchArm::Wild);
47+
assert_eq!(test_foo(Foo::Two(42, 1023)), MatchArm::Wild);
48+
assert_eq!(test_foo(Foo::Two(255, 2048)), MatchArm::Arm(2));
49+
assert_eq!(test_foo(Foo::One(100)), MatchArm::Arm(3));
50+
assert_eq!(test_foo(Foo::One(115)), MatchArm::Arm(3));
51+
assert_eq!(test_foo(Foo::One(105)), MatchArm::Wild);
52+
assert_eq!(test_foo(Foo::One(215)), MatchArm::Arm(3));
53+
assert_eq!(test_foo(Foo::One(121)), MatchArm::Wild);
54+
assert_eq!(test_foo(Foo::Two(0, 42)), MatchArm::Arm(4));
55+
assert_eq!(test_foo(Foo::Two(100, 0)), MatchArm::Arm(4));
56+
assert_eq!(test_foo(Foo::Two(42, 0)), MatchArm::Wild);
57+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Test that an or-pattern works with a wild pattern. This tests two things:
2+
//
3+
// 1) The Wild pattern should cause the pattern to always succeed.
4+
// 2) or-patterns should work with simplifyable patterns.
5+
6+
// run-pass
7+
#![feature(or_patterns)]
8+
9+
pub fn test(x: Option<usize>) -> bool {
10+
match x {
11+
Some(0 | _) => true,
12+
_ => false,
13+
}
14+
}
15+
16+
fn main() {
17+
assert!(test(Some(42)));
18+
assert!(!test(None));
19+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// run-pass
2+
3+
#![feature(or_patterns)]
4+
5+
#[derive(Debug)]
6+
enum Other {
7+
One,
8+
Two,
9+
Three,
10+
}
11+
12+
#[derive(Debug)]
13+
enum Test {
14+
Foo { first: usize, second: usize },
15+
Bar { other: Option<Other> },
16+
Baz,
17+
}
18+
19+
fn test(x: Option<Test>) -> bool {
20+
match x {
21+
Some(
22+
Test::Foo { first: 1024 | 2048, second: 2048 | 4096 }
23+
| Test::Bar { other: Some(Other::One | Other::Two) },
24+
) => true,
25+
// wild case
26+
Some(_) => false,
27+
// empty case
28+
None => false,
29+
}
30+
}
31+
32+
fn main() {
33+
assert!(test(Some(Test::Foo { first: 1024, second: 4096 })));
34+
assert!(!test(Some(Test::Foo { first: 2048, second: 8192 })));
35+
assert!(!test(Some(Test::Foo { first: 42, second: 2048 })));
36+
assert!(test(Some(Test::Bar { other: Some(Other::One) })));
37+
assert!(test(Some(Test::Bar { other: Some(Other::Two) })));
38+
assert!(!test(Some(Test::Bar { other: Some(Other::Three) })));
39+
assert!(!test(Some(Test::Bar { other: None })));
40+
assert!(!test(Some(Test::Baz)));
41+
assert!(!test(None));
42+
}

0 commit comments

Comments
 (0)