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