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.
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.
12 use core::cell::RefCell;
21 let mut p = Pair {fst: 10, snd: 20};
22 let pptr: *mut Pair = &mut p;
23 let iptr: *mut isize = pptr as *mut isize;
24 assert_eq!(*iptr, 10);
26 assert_eq!(*iptr, 30);
27 assert_eq!(p.fst, 30);
29 *pptr = Pair {fst: 50, snd: 60};
30 assert_eq!(*iptr, 50);
31 assert_eq!(p.fst, 50);
32 assert_eq!(p.snd, 60);
34 let v0 = vec![32000u16, 32001u16, 32002u16];
35 let mut v1 = vec![0u16, 0u16, 0u16];
37 copy(v0.as_ptr().offset(1), v1.as_mut_ptr().offset(1), 1);
38 assert!((v1[0] == 0u16 &&
41 copy(v0.as_ptr().offset(2), v1.as_mut_ptr(), 1);
42 assert!((v1[0] == 32002u16 &&
45 copy(v0.as_ptr(), v1.as_mut_ptr().offset(2), 1);
46 assert!((v1[0] == 32002u16 &&
54 let p: *const isize = null();
57 let q = unsafe { p.offset(1) };
58 assert!(!q.is_null());
60 let mp: *mut isize = null_mut();
61 assert!(mp.is_null());
63 let mq = unsafe { mp.offset(1) };
64 assert!(!mq.is_null());
66 // Pointers to unsized types -- slices
67 let s: &mut [u8] = &mut [1, 2, 3];
68 let cs: *const [u8] = s;
69 assert!(!cs.is_null());
71 let ms: *mut [u8] = s;
72 assert!(!ms.is_null());
74 let cz: *const [u8] = &[];
75 assert!(!cz.is_null());
77 let mz: *mut [u8] = &mut [];
78 assert!(!mz.is_null());
80 let ncs: *const [u8] = null::<[u8; 3]>();
81 assert!(ncs.is_null());
83 let nms: *mut [u8] = null_mut::<[u8; 3]>();
84 assert!(nms.is_null());
86 // Pointers to unsized types -- trait objects
87 let ci: *const ToString = &3;
88 assert!(!ci.is_null());
90 let mi: *mut ToString = &mut 3;
91 assert!(!mi.is_null());
93 let nci: *const ToString = null::<isize>();
94 assert!(nci.is_null());
96 let nmi: *mut ToString = null_mut::<isize>();
97 assert!(nmi.is_null());
103 let p: *const isize = null();
104 assert_eq!(p.as_ref(), None);
106 let q: *const isize = &2;
107 assert_eq!(q.as_ref().unwrap(), &2);
109 let p: *mut isize = null_mut();
110 assert_eq!(p.as_ref(), None);
112 let q: *mut isize = &mut 2;
113 assert_eq!(q.as_ref().unwrap(), &2);
115 // Lifetime inference
118 let p = &u as *const isize;
119 assert_eq!(p.as_ref().unwrap(), &2);
122 // Pointers to unsized types -- slices
123 let s: &mut [u8] = &mut [1, 2, 3];
124 let cs: *const [u8] = s;
125 assert_eq!(cs.as_ref(), Some(&*s));
127 let ms: *mut [u8] = s;
128 assert_eq!(ms.as_ref(), Some(&*s));
130 let cz: *const [u8] = &[];
131 assert_eq!(cz.as_ref(), Some(&[][..]));
133 let mz: *mut [u8] = &mut [];
134 assert_eq!(mz.as_ref(), Some(&[][..]));
136 let ncs: *const [u8] = null::<[u8; 3]>();
137 assert_eq!(ncs.as_ref(), None);
139 let nms: *mut [u8] = null_mut::<[u8; 3]>();
140 assert_eq!(nms.as_ref(), None);
142 // Pointers to unsized types -- trait objects
143 let ci: *const ToString = &3;
144 assert!(ci.as_ref().is_some());
146 let mi: *mut ToString = &mut 3;
147 assert!(mi.as_ref().is_some());
149 let nci: *const ToString = null::<isize>();
150 assert!(nci.as_ref().is_none());
152 let nmi: *mut ToString = null_mut::<isize>();
153 assert!(nmi.as_ref().is_none());
160 let p: *mut isize = null_mut();
161 assert!(p.as_mut() == None);
163 let q: *mut isize = &mut 2;
164 assert!(q.as_mut().unwrap() == &mut 2);
166 // Lifetime inference
169 let p = &mut u as *mut isize;
170 assert!(p.as_mut().unwrap() == &mut 2);
173 // Pointers to unsized types -- slices
174 let s: &mut [u8] = &mut [1, 2, 3];
175 let ms: *mut [u8] = s;
176 assert_eq!(ms.as_mut(), Some(s));
178 let mz: *mut [u8] = &mut [];
179 assert_eq!(mz.as_mut(), Some(&mut [][..]));
181 let nms: *mut [u8] = null_mut::<[u8; 3]>();
182 assert_eq!(nms.as_mut(), None);
184 // Pointers to unsized types -- trait objects
185 let mi: *mut ToString = &mut 3;
186 assert!(mi.as_mut().is_some());
188 let nmi: *mut ToString = null_mut::<isize>();
189 assert!(nmi.as_mut().is_none());
194 fn test_ptr_addition() {
196 let xs = vec![5; 16];
197 let mut ptr = xs.as_ptr();
198 let end = ptr.offset(16);
206 let mut m_ptr = xs_mut.as_mut_ptr();
207 let m_end = m_ptr.offset(16);
209 while m_ptr < m_end {
211 m_ptr = m_ptr.offset(1);
214 assert!(xs_mut == vec![10; 16]);
219 fn test_ptr_subtraction() {
221 let xs = vec![0,1,2,3,4,5,6,7,8,9];
223 let ptr = xs.as_ptr();
226 assert_eq!(*(ptr.offset(idx as isize)), idx as isize);
231 let m_start = xs_mut.as_mut_ptr();
232 let mut m_ptr = m_start.offset(9);
234 while m_ptr >= m_start {
236 m_ptr = m_ptr.offset(-1);
239 assert_eq!(xs_mut, [0,2,4,6,8,10,12,14,16,18]);
244 fn test_set_memory() {
245 let mut xs = [0u8; 20];
246 let ptr = xs.as_mut_ptr();
247 unsafe { write_bytes(ptr, 5u8, xs.len()); }
248 assert!(xs == [5u8; 20]);
252 fn test_unsized_nonnull() {
253 let xs: &[i32] = &[1, 2, 3];
254 let ptr = unsafe { NonNull::new_unchecked(xs as *const [i32] as *mut [i32]) };
255 let ys = unsafe { ptr.as_ref() };
256 let zs: &[i32] = &[1, 2, 3];
262 // Have a symbol for the test below. It doesn’t need to be an actual variadic function, match the
263 // ABI, or even point to an actual executable code, because the function itself is never invoked.
265 pub fn test_variadic_fnptr() {
266 use core::hash::{Hash, SipHasher};
268 fn test_variadic_fnptr(_: u64, ...) -> f64;
270 let p: unsafe extern fn(u64, ...) -> f64 = test_variadic_fnptr;
274 let mut s = SipHasher::new();
275 assert_eq!(p.hash(&mut s), q.hash(&mut s));
279 fn write_unaligned_drop() {
281 static DROPS: RefCell<Vec<u32>> = RefCell::new(Vec::new());
286 impl Drop for Dropper {
288 DROPS.with(|d| d.borrow_mut().push(self.0));
294 let mut t = Dropper(1);
295 unsafe { write_unaligned(&mut t, c); }
297 DROPS.with(|d| assert_eq!(*d.borrow(), [0]));
301 fn align_offset_zst() {
302 // For pointers of stride = 0, the pointer is already aligned or it cannot be aligned at
303 // all, because no amount of elements will align the pointer.
306 assert_eq!((p as *const ()).align_offset(p), 0);
308 assert_eq!(((p + 1) as *const ()).align_offset(p), !0);
310 p = (p + 1).next_power_of_two();
315 fn align_offset_stride1() {
316 // For pointers of stride = 1, the pointer can always be aligned. The offset is equal to
320 for ptr in 1..2*align {
321 let expected = ptr % align;
322 let offset = if expected == 0 { 0 } else { align - expected };
323 assert_eq!((ptr as *const u8).align_offset(align), offset,
324 "ptr = {}, align = {}, size = 1", ptr, align);
326 align = (align + 1).next_power_of_two();
331 fn align_offset_weird_strides() {
340 struct A7(u32, u16, u8);
344 struct A9(u32, u32, u8);
346 struct A10(u32, u32, u16);
348 unsafe fn test_weird_stride<T>(ptr: *const T, align: usize) -> bool {
349 let numptr = ptr as usize;
350 let mut expected = usize::max_value();
351 // Naive but definitely correct way to find the *first* aligned element of stride::<T>.
353 if (numptr + el * ::std::mem::size_of::<T>()) % align == 0 {
358 let got = ptr.align_offset(align);
360 eprintln!("aligning {:p} (with stride of {}) to {}, expected {}, got {}", ptr,
361 ::std::mem::size_of::<T>(), align, expected, got);
367 // For pointers of stride != 1, we verify the algorithm against the naivest possible
372 for ptr in 1usize..4*align {
374 x |= test_weird_stride::<A3>(ptr as *const A3, align);
375 x |= test_weird_stride::<A4>(ptr as *const A4, align);
376 x |= test_weird_stride::<A5>(ptr as *const A5, align);
377 x |= test_weird_stride::<A6>(ptr as *const A6, align);
378 x |= test_weird_stride::<A7>(ptr as *const A7, align);
379 x |= test_weird_stride::<A8>(ptr as *const A8, align);
380 x |= test_weird_stride::<A9>(ptr as *const A9, align);
381 x |= test_weird_stride::<A10>(ptr as *const A10, align);
384 align = (align + 1).next_power_of_two();