]> git.proxmox.com Git - rustc.git/blob - src/libcore/tests/mem.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / libcore / tests / mem.rs
1 // Copyright 2014-2015 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 use core::mem::*;
12
13 #[test]
14 fn size_of_basic() {
15 assert_eq!(size_of::<u8>(), 1);
16 assert_eq!(size_of::<u16>(), 2);
17 assert_eq!(size_of::<u32>(), 4);
18 assert_eq!(size_of::<u64>(), 8);
19 }
20
21 #[test]
22 #[cfg(target_pointer_width = "16")]
23 fn size_of_16() {
24 assert_eq!(size_of::<usize>(), 2);
25 assert_eq!(size_of::<*const usize>(), 2);
26 }
27
28 #[test]
29 #[cfg(target_pointer_width = "32")]
30 fn size_of_32() {
31 assert_eq!(size_of::<usize>(), 4);
32 assert_eq!(size_of::<*const usize>(), 4);
33 }
34
35 #[test]
36 #[cfg(target_pointer_width = "64")]
37 fn size_of_64() {
38 assert_eq!(size_of::<usize>(), 8);
39 assert_eq!(size_of::<*const usize>(), 8);
40 }
41
42 #[test]
43 fn size_of_val_basic() {
44 assert_eq!(size_of_val(&1u8), 1);
45 assert_eq!(size_of_val(&1u16), 2);
46 assert_eq!(size_of_val(&1u32), 4);
47 assert_eq!(size_of_val(&1u64), 8);
48 }
49
50 #[test]
51 fn align_of_basic() {
52 assert_eq!(align_of::<u8>(), 1);
53 assert_eq!(align_of::<u16>(), 2);
54 assert_eq!(align_of::<u32>(), 4);
55 }
56
57 #[test]
58 #[cfg(target_pointer_width = "16")]
59 fn align_of_16() {
60 assert_eq!(align_of::<usize>(), 2);
61 assert_eq!(align_of::<*const usize>(), 2);
62 }
63
64 #[test]
65 #[cfg(target_pointer_width = "32")]
66 fn align_of_32() {
67 assert_eq!(align_of::<usize>(), 4);
68 assert_eq!(align_of::<*const usize>(), 4);
69 }
70
71 #[test]
72 #[cfg(target_pointer_width = "64")]
73 fn align_of_64() {
74 assert_eq!(align_of::<usize>(), 8);
75 assert_eq!(align_of::<*const usize>(), 8);
76 }
77
78 #[test]
79 fn align_of_val_basic() {
80 assert_eq!(align_of_val(&1u8), 1);
81 assert_eq!(align_of_val(&1u16), 2);
82 assert_eq!(align_of_val(&1u32), 4);
83 }
84
85 #[test]
86 fn test_swap() {
87 let mut x = 31337;
88 let mut y = 42;
89 swap(&mut x, &mut y);
90 assert_eq!(x, 42);
91 assert_eq!(y, 31337);
92 }
93
94 #[test]
95 fn test_replace() {
96 let mut x = Some("test".to_string());
97 let y = replace(&mut x, None);
98 assert!(x.is_none());
99 assert!(y.is_some());
100 }
101
102 #[test]
103 fn test_transmute_copy() {
104 assert_eq!(1, unsafe { transmute_copy(&1) });
105 }
106
107 #[test]
108 fn test_transmute() {
109 trait Foo { fn dummy(&self) { } }
110 impl Foo for isize {}
111
112 let a = box 100isize as Box<Foo>;
113 unsafe {
114 let x: ::core::raw::TraitObject = transmute(a);
115 assert!(*(x.data as *const isize) == 100);
116 let _x: Box<Foo> = transmute(x);
117 }
118
119 unsafe {
120 assert_eq!(transmute::<_, Vec<u8>>("L".to_string()), [76]);
121 }
122 }
123
124 #[test]
125 #[allow(dead_code)]
126 fn test_discriminant_send_sync() {
127 enum Regular {
128 A,
129 B(i32)
130 }
131 enum NotSendSync {
132 A(*const i32)
133 }
134
135 fn is_send_sync<T: Send + Sync>() { }
136
137 is_send_sync::<Discriminant<Regular>>();
138 is_send_sync::<Discriminant<NotSendSync>>();
139 }