]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/mem.rs
Rollup merge of #69997 - WaffleLapkin:option_zip, r=LukasKalbertodt
[rust.git] / src / libcore / tests / mem.rs
1 use core::mem::*;
2
3 #[test]
4 fn size_of_basic() {
5     assert_eq!(size_of::<u8>(), 1);
6     assert_eq!(size_of::<u16>(), 2);
7     assert_eq!(size_of::<u32>(), 4);
8     assert_eq!(size_of::<u64>(), 8);
9 }
10
11 #[test]
12 #[cfg(target_pointer_width = "16")]
13 fn size_of_16() {
14     assert_eq!(size_of::<usize>(), 2);
15     assert_eq!(size_of::<*const usize>(), 2);
16 }
17
18 #[test]
19 #[cfg(target_pointer_width = "32")]
20 fn size_of_32() {
21     assert_eq!(size_of::<usize>(), 4);
22     assert_eq!(size_of::<*const usize>(), 4);
23 }
24
25 #[test]
26 #[cfg(target_pointer_width = "64")]
27 fn size_of_64() {
28     assert_eq!(size_of::<usize>(), 8);
29     assert_eq!(size_of::<*const usize>(), 8);
30 }
31
32 #[test]
33 fn size_of_val_basic() {
34     assert_eq!(size_of_val(&1u8), 1);
35     assert_eq!(size_of_val(&1u16), 2);
36     assert_eq!(size_of_val(&1u32), 4);
37     assert_eq!(size_of_val(&1u64), 8);
38 }
39
40 #[test]
41 fn align_of_basic() {
42     assert_eq!(align_of::<u8>(), 1);
43     assert_eq!(align_of::<u16>(), 2);
44     assert_eq!(align_of::<u32>(), 4);
45 }
46
47 #[test]
48 #[cfg(target_pointer_width = "16")]
49 fn align_of_16() {
50     assert_eq!(align_of::<usize>(), 2);
51     assert_eq!(align_of::<*const usize>(), 2);
52 }
53
54 #[test]
55 #[cfg(target_pointer_width = "32")]
56 fn align_of_32() {
57     assert_eq!(align_of::<usize>(), 4);
58     assert_eq!(align_of::<*const usize>(), 4);
59 }
60
61 #[test]
62 #[cfg(target_pointer_width = "64")]
63 fn align_of_64() {
64     assert_eq!(align_of::<usize>(), 8);
65     assert_eq!(align_of::<*const usize>(), 8);
66 }
67
68 #[test]
69 fn align_of_val_basic() {
70     assert_eq!(align_of_val(&1u8), 1);
71     assert_eq!(align_of_val(&1u16), 2);
72     assert_eq!(align_of_val(&1u32), 4);
73 }
74
75 #[test]
76 fn test_swap() {
77     let mut x = 31337;
78     let mut y = 42;
79     swap(&mut x, &mut y);
80     assert_eq!(x, 42);
81     assert_eq!(y, 31337);
82 }
83
84 #[test]
85 fn test_replace() {
86     let mut x = Some("test".to_string());
87     let y = replace(&mut x, None);
88     assert!(x.is_none());
89     assert!(y.is_some());
90 }
91
92 #[test]
93 fn test_transmute_copy() {
94     assert_eq!(1, unsafe { transmute_copy(&1) });
95 }
96
97 #[test]
98 fn test_transmute() {
99     trait Foo {
100         fn dummy(&self) {}
101     }
102     impl Foo for isize {}
103
104     let a = box 100isize as Box<dyn Foo>;
105     unsafe {
106         let x: ::core::raw::TraitObject = transmute(a);
107         assert!(*(x.data as *const isize) == 100);
108         let _x: Box<dyn Foo> = transmute(x);
109     }
110
111     unsafe {
112         assert_eq!(transmute::<_, Vec<u8>>("L".to_string()), [76]);
113     }
114 }
115
116 #[test]
117 #[allow(dead_code)]
118 fn test_discriminant_send_sync() {
119     enum Regular {
120         A,
121         B(i32),
122     }
123     enum NotSendSync {
124         A(*const i32),
125     }
126
127     fn is_send_sync<T: Send + Sync>() {}
128
129     is_send_sync::<Discriminant<Regular>>();
130     is_send_sync::<Discriminant<NotSendSync>>();
131 }
132
133 #[test]
134 fn test_const_forget() {
135     const _: () = forget(0i32);
136     const _: () = forget(Vec::<Vec<Box<i32>>>::new());
137
138     // Writing this function signature without const-forget
139     // triggers compiler errors:
140     // 1) That we use a non-const fn inside a const fn
141     // 2) without the forget, it complains about the destructor of Box
142     const fn const_forget_box<T>(x: Box<T>) {
143         forget(x);
144     }
145
146     // Call the forget_box at runtime,
147     // as we can't const-construct a box yet.
148     const_forget_box(Box::new(0i32));
149 }