]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/ufcs-polymorphic-paths.rs
Auto merge of #25056 - jooert:sometests, r=alexcrichton
[rust.git] / src / test / run-pass / ufcs-polymorphic-paths.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
12 #![feature(collections, rand, into_cow)]
13
14 use std::borrow::{Cow, IntoCow};
15 use std::collections::BitVec;
16 use std::default::Default;
17 use std::iter::FromIterator;
18 use std::ops::Add;
19 use std::option::IntoIter as OptionIter;
20
21 pub struct XorShiftRng;
22 use XorShiftRng as DummyRng;
23 impl Rng for XorShiftRng {}
24 pub trait Rng {}
25 pub trait Rand: Default + Sized {
26     fn rand<R: Rng>(rng: &mut R) -> Self { Default::default() }
27 }
28 impl Rand for i32 { }
29
30 #[derive(PartialEq, Eq)]
31 struct Newt<T>(T);
32
33 fn id<T>(x: T) -> T { x }
34 fn eq<T: Eq>(a: T, b: T) -> bool { a == b }
35 fn u8_as_i8(x: u8) -> i8 { x as i8 }
36 fn odd(x: usize) -> bool { x % 2 == 1 }
37 fn dummy_rng() -> DummyRng { XorShiftRng }
38
39 trait Size: Sized {
40     fn size() -> usize { std::mem::size_of::<Self>() }
41 }
42 impl<T> Size for T {}
43
44 macro_rules! tests {
45     ($($expr:expr, $ty:ty, ($($test:expr),*);)+) => (pub fn main() {$({
46         const C: $ty = $expr;
47         static S: $ty = $expr;
48         assert!(eq(C($($test),*), $expr($($test),*)));
49         assert!(eq(S($($test),*), $expr($($test),*)));
50         assert!(eq(C($($test),*), S($($test),*)));
51     })+})
52 }
53
54 tests! {
55     // Free function.
56     id, fn(i32) -> i32, (5);
57     id::<i32>, fn(i32) -> i32, (5);
58
59     // Enum variant constructor.
60     Some, fn(i32) -> Option<i32>, (5);
61     Some::<i32>, fn(i32) -> Option<i32>, (5);
62
63     // Tuple struct constructor.
64     Newt, fn(i32) -> Newt<i32>, (5);
65     Newt::<i32>, fn(i32) -> Newt<i32>, (5);
66
67     // Inherent static methods.
68     Vec::new, fn() -> Vec<()>, ();
69     Vec::<()>::new, fn() -> Vec<()>, ();
70     <Vec<()>>::new, fn() -> Vec<()>, ();
71     Vec::with_capacity, fn(usize) -> Vec<()>, (5);
72     Vec::<()>::with_capacity, fn(usize) -> Vec<()>, (5);
73     <Vec<()>>::with_capacity, fn(usize) -> Vec<()>, (5);
74     BitVec::from_fn, fn(usize, fn(usize) -> bool) -> BitVec, (5, odd);
75     BitVec::from_fn::<fn(usize) -> bool>, fn(usize, fn(usize) -> bool) -> BitVec, (5, odd);
76
77     // Inherent non-static method.
78     Vec::map_in_place, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>, (vec![b'f', b'o', b'o'], u8_as_i8);
79     Vec::map_in_place::<i8, fn(u8) -> i8>, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>,
80         (vec![b'f', b'o', b'o'], u8_as_i8);
81     Vec::<u8>::map_in_place, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>
82         , (vec![b'f', b'o', b'o'], u8_as_i8);
83     Vec::<u8>::map_in_place::<i8, fn(u8) -> i8>, fn(Vec<u8>, fn(u8) -> i8) -> Vec<i8>
84         , (vec![b'f', b'o', b'o'], u8_as_i8);
85
86     // Trait static methods.
87     bool::size, fn() -> usize, ();
88     <bool>::size, fn() -> usize, ();
89     <bool as Size>::size, fn() -> usize, ();
90
91     Default::default, fn() -> i32, ();
92     i32::default, fn() -> i32, ();
93     <i32>::default, fn() -> i32, ();
94     <i32 as Default>::default, fn() -> i32, ();
95
96     Rand::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
97     i32::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
98     <i32>::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
99     <i32 as Rand>::rand, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
100     Rand::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
101     i32::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
102     <i32>::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
103     <i32 as Rand>::rand::<DummyRng>, fn(&mut DummyRng) -> i32, (&mut dummy_rng());
104
105     // Trait non-static methods.
106     Clone::clone, fn(&i32) -> i32, (&5);
107     i32::clone, fn(&i32) -> i32, (&5);
108     <i32>::clone, fn(&i32) -> i32, (&5);
109     <i32 as Clone>::clone, fn(&i32) -> i32, (&5);
110
111     FromIterator::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
112     Vec::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
113     <Vec<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>, (Some(5).into_iter());
114     <Vec<_> as FromIterator<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>,
115         (Some(5).into_iter());
116     <Vec<i32> as FromIterator<_>>::from_iter, fn(OptionIter<i32>) -> Vec<i32>,
117         (Some(5).into_iter());
118     FromIterator::from_iter::<OptionIter<i32>>, fn(OptionIter<i32>) -> Vec<i32>,
119         (Some(5).into_iter());
120     <Vec<i32> as FromIterator<_>>::from_iter::<OptionIter<i32>>, fn(OptionIter<i32>) -> Vec<i32>,
121         (Some(5).into_iter());
122
123     Add::add, fn(i32, i32) -> i32, (5, 6);
124     i32::add, fn(i32, i32) -> i32, (5, 6);
125     <i32>::add, fn(i32, i32) -> i32, (5, 6);
126     <i32 as Add<_>>::add, fn(i32, i32) -> i32, (5, 6);
127     <i32 as Add<i32>>::add, fn(i32, i32) -> i32, (5, 6);
128
129     String::into_cow, fn(String) -> Cow<'static, str>,
130         ("foo".to_string());
131     <String>::into_cow, fn(String) -> Cow<'static, str>,
132         ("foo".to_string());
133     <String as IntoCow<_>>::into_cow, fn(String) -> Cow<'static, str>,
134         ("foo".to_string());
135     <String as IntoCow<'static, _>>::into_cow, fn(String) -> Cow<'static, str>,
136         ("foo".to_string());
137 }