]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/typeid-intrinsic.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / typeid-intrinsic.rs
1 // Copyright 2013-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 // aux-build:typeid-intrinsic.rs
12 // aux-build:typeid-intrinsic2.rs
13
14 #![feature(core_intrinsics)]
15
16 extern crate typeid_intrinsic as other1;
17 extern crate typeid_intrinsic2 as other2;
18
19 use std::hash::{SipHasher, Hasher, Hash};
20 use std::any::TypeId;
21
22 struct A;
23 struct Test;
24
25 pub fn main() {
26     unsafe {
27         assert_eq!(TypeId::of::<other1::A>(), other1::id_A());
28         assert_eq!(TypeId::of::<other1::B>(), other1::id_B());
29         assert_eq!(TypeId::of::<other1::C>(), other1::id_C());
30         assert_eq!(TypeId::of::<other1::D>(), other1::id_D());
31         assert_eq!(TypeId::of::<other1::E>(), other1::id_E());
32         assert_eq!(TypeId::of::<other1::F>(), other1::id_F());
33         assert_eq!(TypeId::of::<other1::G>(), other1::id_G());
34         assert_eq!(TypeId::of::<other1::H>(), other1::id_H());
35
36         assert_eq!(TypeId::of::<other2::A>(), other2::id_A());
37         assert_eq!(TypeId::of::<other2::B>(), other2::id_B());
38         assert_eq!(TypeId::of::<other2::C>(), other2::id_C());
39         assert_eq!(TypeId::of::<other2::D>(), other2::id_D());
40         assert_eq!(TypeId::of::<other2::E>(), other2::id_E());
41         assert_eq!(TypeId::of::<other2::F>(), other2::id_F());
42         assert_eq!(TypeId::of::<other2::G>(), other2::id_G());
43         assert_eq!(TypeId::of::<other2::H>(), other2::id_H());
44
45         assert_eq!(other1::id_F(), other2::id_F());
46         assert_eq!(other1::id_G(), other2::id_G());
47         assert_eq!(other1::id_H(), other2::id_H());
48
49         assert_eq!(TypeId::of::<isize>(), other2::foo::<isize>());
50         assert_eq!(TypeId::of::<isize>(), other1::foo::<isize>());
51         assert_eq!(other2::foo::<isize>(), other1::foo::<isize>());
52         assert_eq!(TypeId::of::<A>(), other2::foo::<A>());
53         assert_eq!(TypeId::of::<A>(), other1::foo::<A>());
54         assert_eq!(other2::foo::<A>(), other1::foo::<A>());
55     }
56
57     // sanity test of TypeId
58     let (a, b, c) = (TypeId::of::<usize>(), TypeId::of::<&'static str>(),
59                      TypeId::of::<Test>());
60     let (d, e, f) = (TypeId::of::<usize>(), TypeId::of::<&'static str>(),
61                      TypeId::of::<Test>());
62
63     assert!(a != b);
64     assert!(a != c);
65     assert!(b != c);
66
67     assert_eq!(a, d);
68     assert_eq!(b, e);
69     assert_eq!(c, f);
70
71     // check it has a hash
72     let (a, b) = (TypeId::of::<usize>(), TypeId::of::<usize>());
73
74     let mut s1 = SipHasher::new();
75     a.hash(&mut s1);
76     let mut s2 = SipHasher::new();
77     b.hash(&mut s2);
78
79     assert_eq!(s1.finish(), s2.finish());
80 }