]> git.proxmox.com Git - rustc.git/blame - src/libcore/tests/ptr.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / libcore / tests / ptr.rs
CommitLineData
1a4d82fc
JJ
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
11use core::ptr::*;
476ff2be 12use core::cell::RefCell;
1a4d82fc
JJ
13
14#[test]
15fn test() {
16 unsafe {
17 struct Pair {
c34b1796
AL
18 fst: isize,
19 snd: isize
1a4d82fc
JJ
20 };
21 let mut p = Pair {fst: 10, snd: 20};
22 let pptr: *mut Pair = &mut p;
e9174d1e 23 let iptr: *mut isize = pptr as *mut isize;
1a4d82fc
JJ
24 assert_eq!(*iptr, 10);
25 *iptr = 30;
26 assert_eq!(*iptr, 30);
27 assert_eq!(p.fst, 30);
28
29 *pptr = Pair {fst: 50, snd: 60};
30 assert_eq!(*iptr, 50);
31 assert_eq!(p.fst, 50);
32 assert_eq!(p.snd, 60);
33
34 let v0 = vec![32000u16, 32001u16, 32002u16];
35 let mut v1 = vec![0u16, 0u16, 0u16];
36
c34b1796 37 copy(v0.as_ptr().offset(1), v1.as_mut_ptr().offset(1), 1);
1a4d82fc
JJ
38 assert!((v1[0] == 0u16 &&
39 v1[1] == 32001u16 &&
40 v1[2] == 0u16));
c34b1796 41 copy(v0.as_ptr().offset(2), v1.as_mut_ptr(), 1);
1a4d82fc
JJ
42 assert!((v1[0] == 32002u16 &&
43 v1[1] == 32001u16 &&
44 v1[2] == 0u16));
c34b1796 45 copy(v0.as_ptr(), v1.as_mut_ptr().offset(2), 1);
1a4d82fc
JJ
46 assert!((v1[0] == 32002u16 &&
47 v1[1] == 32001u16 &&
48 v1[2] == 32000u16));
49 }
50}
51
52#[test]
53fn test_is_null() {
c34b1796 54 let p: *const isize = null();
1a4d82fc
JJ
55 assert!(p.is_null());
56
57 let q = unsafe { p.offset(1) };
58 assert!(!q.is_null());
59
c34b1796 60 let mp: *mut isize = null_mut();
1a4d82fc
JJ
61 assert!(mp.is_null());
62
63 let mq = unsafe { mp.offset(1) };
64 assert!(!mq.is_null());
65}
66
67#[test]
68fn test_as_ref() {
69 unsafe {
c34b1796 70 let p: *const isize = null();
1a4d82fc
JJ
71 assert_eq!(p.as_ref(), None);
72
c34b1796 73 let q: *const isize = &2;
1a4d82fc
JJ
74 assert_eq!(q.as_ref().unwrap(), &2);
75
c34b1796 76 let p: *mut isize = null_mut();
1a4d82fc
JJ
77 assert_eq!(p.as_ref(), None);
78
c34b1796 79 let q: *mut isize = &mut 2;
1a4d82fc
JJ
80 assert_eq!(q.as_ref().unwrap(), &2);
81
82 // Lifetime inference
c34b1796 83 let u = 2isize;
1a4d82fc 84 {
c34b1796 85 let p = &u as *const isize;
1a4d82fc
JJ
86 assert_eq!(p.as_ref().unwrap(), &2);
87 }
abe05a73
XL
88
89 // Pointers to unsized types -- slices
90 let s: &mut [u8] = &mut [1, 2, 3];
91 let cs: *const [u8] = s;
92 assert_eq!(cs.as_ref(), Some(&*s));
93
94 let ms: *mut [u8] = s;
95 assert_eq!(ms.as_ref(), Some(&*s));
96
97 let cz: *const [u8] = &[];
98 assert_eq!(cz.as_ref(), Some(&[][..]));
99
100 let mz: *mut [u8] = &mut [];
101 assert_eq!(mz.as_ref(), Some(&[][..]));
102
103 let ncs: *const [u8] = null::<[u8; 3]>();
104 assert_eq!(ncs.as_ref(), None);
105
106 let nms: *mut [u8] = null_mut::<[u8; 3]>();
107 assert_eq!(nms.as_ref(), None);
108
109 // Pointers to unsized types -- trait objects
110 let ci: *const ToString = &3;
111 assert!(ci.as_ref().is_some());
112
113 let mi: *mut ToString = &mut 3;
114 assert!(mi.as_ref().is_some());
115
116 let nci: *const ToString = null::<isize>();
117 assert!(nci.as_ref().is_none());
118
119 let nmi: *mut ToString = null_mut::<isize>();
120 assert!(nmi.as_ref().is_none());
1a4d82fc
JJ
121 }
122}
123
124#[test]
125fn test_as_mut() {
126 unsafe {
c34b1796 127 let p: *mut isize = null_mut();
1a4d82fc
JJ
128 assert!(p.as_mut() == None);
129
c34b1796 130 let q: *mut isize = &mut 2;
1a4d82fc
JJ
131 assert!(q.as_mut().unwrap() == &mut 2);
132
133 // Lifetime inference
c34b1796 134 let mut u = 2isize;
1a4d82fc 135 {
c34b1796 136 let p = &mut u as *mut isize;
1a4d82fc
JJ
137 assert!(p.as_mut().unwrap() == &mut 2);
138 }
abe05a73
XL
139
140 // Pointers to unsized types -- slices
141 let s: &mut [u8] = &mut [1, 2, 3];
142 let ms: *mut [u8] = s;
143 assert_eq!(ms.as_mut(), Some(s));
144
145 let mz: *mut [u8] = &mut [];
146 assert_eq!(mz.as_mut(), Some(&mut [][..]));
147
148 let nms: *mut [u8] = null_mut::<[u8; 3]>();
149 assert_eq!(nms.as_mut(), None);
150
151 // Pointers to unsized types -- trait objects
152 let mi: *mut ToString = &mut 3;
153 assert!(mi.as_mut().is_some());
154
155 let nmi: *mut ToString = null_mut::<isize>();
156 assert!(nmi.as_mut().is_none());
1a4d82fc
JJ
157 }
158}
159
160#[test]
161fn test_ptr_addition() {
162 unsafe {
c1a9b12d 163 let xs = vec![5; 16];
1a4d82fc
JJ
164 let mut ptr = xs.as_ptr();
165 let end = ptr.offset(16);
166
167 while ptr < end {
168 assert_eq!(*ptr, 5);
169 ptr = ptr.offset(1);
170 }
171
172 let mut xs_mut = xs;
173 let mut m_ptr = xs_mut.as_mut_ptr();
174 let m_end = m_ptr.offset(16);
175
176 while m_ptr < m_end {
177 *m_ptr += 5;
178 m_ptr = m_ptr.offset(1);
179 }
180
c1a9b12d 181 assert!(xs_mut == vec![10; 16]);
1a4d82fc
JJ
182 }
183}
184
185#[test]
186fn test_ptr_subtraction() {
187 unsafe {
188 let xs = vec![0,1,2,3,4,5,6,7,8,9];
c34b1796 189 let mut idx = 9;
1a4d82fc
JJ
190 let ptr = xs.as_ptr();
191
c34b1796
AL
192 while idx >= 0 {
193 assert_eq!(*(ptr.offset(idx as isize)), idx as isize);
194 idx = idx - 1;
1a4d82fc
JJ
195 }
196
197 let mut xs_mut = xs;
198 let m_start = xs_mut.as_mut_ptr();
199 let mut m_ptr = m_start.offset(9);
200
201 while m_ptr >= m_start {
202 *m_ptr += *m_ptr;
203 m_ptr = m_ptr.offset(-1);
204 }
205
c34b1796 206 assert_eq!(xs_mut, [0,2,4,6,8,10,12,14,16,18]);
1a4d82fc
JJ
207 }
208}
209
210#[test]
211fn test_set_memory() {
212 let mut xs = [0u8; 20];
213 let ptr = xs.as_mut_ptr();
c34b1796 214 unsafe { write_bytes(ptr, 5u8, xs.len()); }
1a4d82fc
JJ
215 assert!(xs == [5u8; 20]);
216}
85aaf69f
SL
217
218#[test]
219fn test_unsized_unique() {
7cac9316 220 let xs: &[i32] = &[1, 2, 3];
3b2f2976 221 let ptr = unsafe { Unique::new_unchecked(xs as *const [i32] as *mut [i32]) };
7cac9316
XL
222 let ys = unsafe { ptr.as_ref() };
223 let zs: &[i32] = &[1, 2, 3];
85aaf69f
SL
224 assert!(ys == zs);
225}
5bcae85e
SL
226
227#[test]
9e0c209e
SL
228#[allow(warnings)]
229// Have a symbol for the test below. It doesn’t need to be an actual variadic function, match the
230// ABI, or even point to an actual executable code, because the function itself is never invoked.
231#[no_mangle]
232pub fn test_variadic_fnptr() {
5bcae85e 233 use core::hash::{Hash, SipHasher};
9e0c209e
SL
234 extern {
235 fn test_variadic_fnptr(_: u64, ...) -> f64;
5bcae85e 236 }
9e0c209e 237 let p: unsafe extern fn(u64, ...) -> f64 = test_variadic_fnptr;
5bcae85e
SL
238 let q = p.clone();
239 assert_eq!(p, q);
240 assert!(!(p < q));
241 let mut s = SipHasher::new();
242 assert_eq!(p.hash(&mut s), q.hash(&mut s));
243}
476ff2be
SL
244
245#[test]
246fn write_unaligned_drop() {
247 thread_local! {
248 static DROPS: RefCell<Vec<u32>> = RefCell::new(Vec::new());
249 }
250
251 struct Dropper(u32);
252
253 impl Drop for Dropper {
254 fn drop(&mut self) {
255 DROPS.with(|d| d.borrow_mut().push(self.0));
256 }
257 }
258
259 {
260 let c = Dropper(0);
261 let mut t = Dropper(1);
262 unsafe { write_unaligned(&mut t, c); }
263 }
264 DROPS.with(|d| assert_eq!(*d.borrow(), [0]));
265}