]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/match-vec-alternatives.rs
auto merge of #16626 : ruud-v-a/rust/duration-reform, r=brson
[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])),
72                "None, Ok(at least two elements)");
73     assert_eq!(match_nested_vecs_cons::<uint>(None, Err(())), "None, Ok(less than one element)");
74     assert_eq!(match_nested_vecs_cons::<bool>(Some::<&[_]>(&[]), Ok::<&[_], ()>(&[])),
75                "Some(empty), Ok(empty)");
76     assert_eq!(match_nested_vecs_cons(Some::<&[_]>(&[1i]), Err(())), "Some(non-empty), any");
77     assert_eq!(match_nested_vecs_cons(Some::<&[_]>(&[(42i, ())]), Ok::<&[_], ()>(&[(1i, ())])),
78                "Some(non-empty), any");
79
80     assert_eq!(match_nested_vecs_snoc(None, Ok::<&[_], ()>(&[4u, 2u])),
81                "None, Ok(at least two elements)");
82     assert_eq!(match_nested_vecs_snoc::<uint>(None, Err(())), "None, Ok(less than one element)");
83     assert_eq!(match_nested_vecs_snoc::<bool>(Some::<&[_]>(&[]), Ok::<&[_], ()>(&[])),
84                "Some(empty), Ok(empty)");
85     assert_eq!(match_nested_vecs_snoc(Some::<&[_]>(&[1i]), Err(())), "Some(non-empty), any");
86     assert_eq!(match_nested_vecs_snoc(Some::<&[_]>(&[(42i, ())]), Ok::<&[_], ()>(&[(1i, ())])),
87                "Some(non-empty), any");
88 }