]>
Commit | Line | Data |
---|---|---|
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 | ||
11 | use core::ptr::*; | |
476ff2be | 12 | use core::cell::RefCell; |
1a4d82fc JJ |
13 | |
14 | #[test] | |
15 | fn 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] | |
53 | fn 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] | |
68 | fn 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 | } | |
88 | } | |
89 | } | |
90 | ||
91 | #[test] | |
92 | fn test_as_mut() { | |
93 | unsafe { | |
c34b1796 | 94 | let p: *mut isize = null_mut(); |
1a4d82fc JJ |
95 | assert!(p.as_mut() == None); |
96 | ||
c34b1796 | 97 | let q: *mut isize = &mut 2; |
1a4d82fc JJ |
98 | assert!(q.as_mut().unwrap() == &mut 2); |
99 | ||
100 | // Lifetime inference | |
c34b1796 | 101 | let mut u = 2isize; |
1a4d82fc | 102 | { |
c34b1796 | 103 | let p = &mut u as *mut isize; |
1a4d82fc JJ |
104 | assert!(p.as_mut().unwrap() == &mut 2); |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | #[test] | |
110 | fn test_ptr_addition() { | |
111 | unsafe { | |
c1a9b12d | 112 | let xs = vec![5; 16]; |
1a4d82fc JJ |
113 | let mut ptr = xs.as_ptr(); |
114 | let end = ptr.offset(16); | |
115 | ||
116 | while ptr < end { | |
117 | assert_eq!(*ptr, 5); | |
118 | ptr = ptr.offset(1); | |
119 | } | |
120 | ||
121 | let mut xs_mut = xs; | |
122 | let mut m_ptr = xs_mut.as_mut_ptr(); | |
123 | let m_end = m_ptr.offset(16); | |
124 | ||
125 | while m_ptr < m_end { | |
126 | *m_ptr += 5; | |
127 | m_ptr = m_ptr.offset(1); | |
128 | } | |
129 | ||
c1a9b12d | 130 | assert!(xs_mut == vec![10; 16]); |
1a4d82fc JJ |
131 | } |
132 | } | |
133 | ||
134 | #[test] | |
135 | fn test_ptr_subtraction() { | |
136 | unsafe { | |
137 | let xs = vec![0,1,2,3,4,5,6,7,8,9]; | |
c34b1796 | 138 | let mut idx = 9; |
1a4d82fc JJ |
139 | let ptr = xs.as_ptr(); |
140 | ||
c34b1796 AL |
141 | while idx >= 0 { |
142 | assert_eq!(*(ptr.offset(idx as isize)), idx as isize); | |
143 | idx = idx - 1; | |
1a4d82fc JJ |
144 | } |
145 | ||
146 | let mut xs_mut = xs; | |
147 | let m_start = xs_mut.as_mut_ptr(); | |
148 | let mut m_ptr = m_start.offset(9); | |
149 | ||
150 | while m_ptr >= m_start { | |
151 | *m_ptr += *m_ptr; | |
152 | m_ptr = m_ptr.offset(-1); | |
153 | } | |
154 | ||
c34b1796 | 155 | assert_eq!(xs_mut, [0,2,4,6,8,10,12,14,16,18]); |
1a4d82fc JJ |
156 | } |
157 | } | |
158 | ||
159 | #[test] | |
160 | fn test_set_memory() { | |
161 | let mut xs = [0u8; 20]; | |
162 | let ptr = xs.as_mut_ptr(); | |
c34b1796 | 163 | unsafe { write_bytes(ptr, 5u8, xs.len()); } |
1a4d82fc JJ |
164 | assert!(xs == [5u8; 20]); |
165 | } | |
85aaf69f SL |
166 | |
167 | #[test] | |
168 | fn test_unsized_unique() { | |
c34b1796 AL |
169 | let xs: &mut [i32] = &mut [1, 2, 3]; |
170 | let ptr = unsafe { Unique::new(xs as *mut [i32]) }; | |
85aaf69f | 171 | let ys = unsafe { &mut **ptr }; |
c34b1796 | 172 | let zs: &mut [i32] = &mut [1, 2, 3]; |
85aaf69f SL |
173 | assert!(ys == zs); |
174 | } | |
5bcae85e SL |
175 | |
176 | #[test] | |
9e0c209e SL |
177 | #[allow(warnings)] |
178 | // Have a symbol for the test below. It doesn’t need to be an actual variadic function, match the | |
179 | // ABI, or even point to an actual executable code, because the function itself is never invoked. | |
180 | #[no_mangle] | |
181 | pub fn test_variadic_fnptr() { | |
5bcae85e | 182 | use core::hash::{Hash, SipHasher}; |
9e0c209e SL |
183 | extern { |
184 | fn test_variadic_fnptr(_: u64, ...) -> f64; | |
5bcae85e | 185 | } |
9e0c209e | 186 | let p: unsafe extern fn(u64, ...) -> f64 = test_variadic_fnptr; |
5bcae85e SL |
187 | let q = p.clone(); |
188 | assert_eq!(p, q); | |
189 | assert!(!(p < q)); | |
190 | let mut s = SipHasher::new(); | |
191 | assert_eq!(p.hash(&mut s), q.hash(&mut s)); | |
192 | } | |
476ff2be SL |
193 | |
194 | #[test] | |
195 | fn write_unaligned_drop() { | |
196 | thread_local! { | |
197 | static DROPS: RefCell<Vec<u32>> = RefCell::new(Vec::new()); | |
198 | } | |
199 | ||
200 | struct Dropper(u32); | |
201 | ||
202 | impl Drop for Dropper { | |
203 | fn drop(&mut self) { | |
204 | DROPS.with(|d| d.borrow_mut().push(self.0)); | |
205 | } | |
206 | } | |
207 | ||
208 | { | |
209 | let c = Dropper(0); | |
210 | let mut t = Dropper(1); | |
211 | unsafe { write_unaligned(&mut t, c); } | |
212 | } | |
213 | DROPS.with(|d| assert_eq!(*d.borrow(), [0])); | |
214 | } |