]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/match-vec-alternatives.rs
Change exhaustiveness analysis to permit multiple constructors per pattern
[rust.git] / src / test / run-pass / match-vec-alternatives.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 fn match_vecs<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str {
12     match (l1, l2) {
13         ([], []) => "both empty",
14         ([], [..]) | ([..], []) => "one empty",
15         ([..], [..]) => "both non-empty"
16     }
17 }
18
19 fn match_vecs_cons<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str {
20     match (l1, l2) {
21         ([], []) => "both empty",
22         ([], [_, ..]) | ([_, ..], []) => "one empty",
23         ([_, ..], [_, ..]) => "both non-empty"
24     }
25 }
26
27 fn match_vecs_snoc<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str {
28     match (l1, l2) {
29         ([], []) => "both empty",
30         ([], [.., _]) | ([.., _], []) => "one empty",
31         ([.., _], [.., _]) => "both non-empty"
32     }
33 }
34
35 fn match_nested_vecs_cons<'a, T>(l1: Option<&'a [T]>, l2: Result<&'a [T], ()>) -> &'static str {
36     match (l1, l2) {
37         (Some([]), Ok([])) => "Some(empty), Ok(empty)",
38         (Some([_, ..]), Ok(_)) | (Some([_, ..]), Err(())) => "Some(non-empty), any",
39         (None, Ok([])) | (None, Err(())) | (None, Ok([_])) => "None, Ok(less than one element)",
40         (None, Ok([_, _, ..])) => "None, Ok(at least two elements)",
41         _ => "other"
42     }
43 }
44
45 fn match_nested_vecs_snoc<'a, T>(l1: Option<&'a [T]>, l2: Result<&'a [T], ()>) -> &'static str {
46     match (l1, l2) {
47         (Some([]), Ok([])) => "Some(empty), Ok(empty)",
48         (Some([.., _]), Ok(_)) | (Some([.., _]), Err(())) => "Some(non-empty), any",
49         (None, Ok([])) | (None, Err(())) | (None, Ok([_])) => "None, Ok(less than one element)",
50         (None, Ok([.., _, _])) => "None, Ok(at least two elements)",
51         _ => "other"
52     }
53 }
54
55 fn main() {
56     assert_eq!(match_vecs(&[1i, 2], &[2i, 3]), "both non-empty");
57     assert_eq!(match_vecs(&[], &[1i, 2, 3, 4]), "one empty");
58     assert_eq!(match_vecs::<uint>(&[], &[]), "both empty");
59     assert_eq!(match_vecs(&[1i, 2, 3], &[]), "one empty");
60
61     assert_eq!(match_vecs_cons(&[1i, 2], &[2i, 3]), "both non-empty");
62     assert_eq!(match_vecs_cons(&[], &[1i, 2, 3, 4]), "one empty");
63     assert_eq!(match_vecs_cons::<uint>(&[], &[]), "both empty");
64     assert_eq!(match_vecs_cons(&[1i, 2, 3], &[]), "one empty");
65
66     assert_eq!(match_vecs_snoc(&[1i, 2], &[2i, 3]), "both non-empty");
67     assert_eq!(match_vecs_snoc(&[], &[1i, 2, 3, 4]), "one empty");
68     assert_eq!(match_vecs_snoc::<uint>(&[], &[]), "both empty");
69     assert_eq!(match_vecs_snoc(&[1i, 2, 3], &[]), "one empty");
70
71     assert_eq!(match_nested_vecs_cons(None, Ok(&[4u, 2u])), "None, Ok(at least two elements)");
72     assert_eq!(match_nested_vecs_cons::<uint>(None, Err(())), "None, Ok(less than one element)");
73     assert_eq!(match_nested_vecs_cons::<bool>(Some(&[]), Ok(&[])), "Some(empty), Ok(empty)");
74     assert_eq!(match_nested_vecs_cons(Some(&[1i]), Err(())), "Some(non-empty), any");
75     assert_eq!(match_nested_vecs_cons(Some(&[(42i, ())]), Ok(&[(1i, ())])), "Some(non-empty), any");
76
77     assert_eq!(match_nested_vecs_snoc(None, Ok(&[4u, 2u])), "None, Ok(at least two elements)");
78     assert_eq!(match_nested_vecs_snoc::<uint>(None, Err(())), "None, Ok(less than one element)");
79     assert_eq!(match_nested_vecs_snoc::<bool>(Some(&[]), Ok(&[])), "Some(empty), Ok(empty)");
80     assert_eq!(match_nested_vecs_snoc(Some(&[1i]), Err(())), "Some(non-empty), any");
81     assert_eq!(match_nested_vecs_snoc(Some(&[(42i, ())]), Ok(&[(1i, ())])), "Some(non-empty), any");
82 }