]> git.proxmox.com Git - rustc.git/blame - src/libcoretest/ptr.rs
New upstream version 1.13.0+dfsg1
[rustc.git] / src / libcoretest / 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::*;
1a4d82fc
JJ
12
13#[test]
14fn test() {
15 unsafe {
16 struct Pair {
c34b1796
AL
17 fst: isize,
18 snd: isize
1a4d82fc
JJ
19 };
20 let mut p = Pair {fst: 10, snd: 20};
21 let pptr: *mut Pair = &mut p;
e9174d1e 22 let iptr: *mut isize = pptr as *mut isize;
1a4d82fc
JJ
23 assert_eq!(*iptr, 10);
24 *iptr = 30;
25 assert_eq!(*iptr, 30);
26 assert_eq!(p.fst, 30);
27
28 *pptr = Pair {fst: 50, snd: 60};
29 assert_eq!(*iptr, 50);
30 assert_eq!(p.fst, 50);
31 assert_eq!(p.snd, 60);
32
33 let v0 = vec![32000u16, 32001u16, 32002u16];
34 let mut v1 = vec![0u16, 0u16, 0u16];
35
c34b1796 36 copy(v0.as_ptr().offset(1), v1.as_mut_ptr().offset(1), 1);
1a4d82fc
JJ
37 assert!((v1[0] == 0u16 &&
38 v1[1] == 32001u16 &&
39 v1[2] == 0u16));
c34b1796 40 copy(v0.as_ptr().offset(2), v1.as_mut_ptr(), 1);
1a4d82fc
JJ
41 assert!((v1[0] == 32002u16 &&
42 v1[1] == 32001u16 &&
43 v1[2] == 0u16));
c34b1796 44 copy(v0.as_ptr(), v1.as_mut_ptr().offset(2), 1);
1a4d82fc
JJ
45 assert!((v1[0] == 32002u16 &&
46 v1[1] == 32001u16 &&
47 v1[2] == 32000u16));
48 }
49}
50
51#[test]
52fn test_is_null() {
c34b1796 53 let p: *const isize = null();
1a4d82fc
JJ
54 assert!(p.is_null());
55
56 let q = unsafe { p.offset(1) };
57 assert!(!q.is_null());
58
c34b1796 59 let mp: *mut isize = null_mut();
1a4d82fc
JJ
60 assert!(mp.is_null());
61
62 let mq = unsafe { mp.offset(1) };
63 assert!(!mq.is_null());
64}
65
66#[test]
67fn test_as_ref() {
68 unsafe {
c34b1796 69 let p: *const isize = null();
1a4d82fc
JJ
70 assert_eq!(p.as_ref(), None);
71
c34b1796 72 let q: *const isize = &2;
1a4d82fc
JJ
73 assert_eq!(q.as_ref().unwrap(), &2);
74
c34b1796 75 let p: *mut isize = null_mut();
1a4d82fc
JJ
76 assert_eq!(p.as_ref(), None);
77
c34b1796 78 let q: *mut isize = &mut 2;
1a4d82fc
JJ
79 assert_eq!(q.as_ref().unwrap(), &2);
80
81 // Lifetime inference
c34b1796 82 let u = 2isize;
1a4d82fc 83 {
c34b1796 84 let p = &u as *const isize;
1a4d82fc
JJ
85 assert_eq!(p.as_ref().unwrap(), &2);
86 }
87 }
88}
89
90#[test]
91fn test_as_mut() {
92 unsafe {
c34b1796 93 let p: *mut isize = null_mut();
1a4d82fc
JJ
94 assert!(p.as_mut() == None);
95
c34b1796 96 let q: *mut isize = &mut 2;
1a4d82fc
JJ
97 assert!(q.as_mut().unwrap() == &mut 2);
98
99 // Lifetime inference
c34b1796 100 let mut u = 2isize;
1a4d82fc 101 {
c34b1796 102 let p = &mut u as *mut isize;
1a4d82fc
JJ
103 assert!(p.as_mut().unwrap() == &mut 2);
104 }
105 }
106}
107
108#[test]
109fn test_ptr_addition() {
110 unsafe {
c1a9b12d 111 let xs = vec![5; 16];
1a4d82fc
JJ
112 let mut ptr = xs.as_ptr();
113 let end = ptr.offset(16);
114
115 while ptr < end {
116 assert_eq!(*ptr, 5);
117 ptr = ptr.offset(1);
118 }
119
120 let mut xs_mut = xs;
121 let mut m_ptr = xs_mut.as_mut_ptr();
122 let m_end = m_ptr.offset(16);
123
124 while m_ptr < m_end {
125 *m_ptr += 5;
126 m_ptr = m_ptr.offset(1);
127 }
128
c1a9b12d 129 assert!(xs_mut == vec![10; 16]);
1a4d82fc
JJ
130 }
131}
132
133#[test]
134fn test_ptr_subtraction() {
135 unsafe {
136 let xs = vec![0,1,2,3,4,5,6,7,8,9];
c34b1796 137 let mut idx = 9;
1a4d82fc
JJ
138 let ptr = xs.as_ptr();
139
c34b1796
AL
140 while idx >= 0 {
141 assert_eq!(*(ptr.offset(idx as isize)), idx as isize);
142 idx = idx - 1;
1a4d82fc
JJ
143 }
144
145 let mut xs_mut = xs;
146 let m_start = xs_mut.as_mut_ptr();
147 let mut m_ptr = m_start.offset(9);
148
149 while m_ptr >= m_start {
150 *m_ptr += *m_ptr;
151 m_ptr = m_ptr.offset(-1);
152 }
153
c34b1796 154 assert_eq!(xs_mut, [0,2,4,6,8,10,12,14,16,18]);
1a4d82fc
JJ
155 }
156}
157
158#[test]
159fn test_set_memory() {
160 let mut xs = [0u8; 20];
161 let ptr = xs.as_mut_ptr();
c34b1796 162 unsafe { write_bytes(ptr, 5u8, xs.len()); }
1a4d82fc
JJ
163 assert!(xs == [5u8; 20]);
164}
85aaf69f
SL
165
166#[test]
167fn test_unsized_unique() {
c34b1796
AL
168 let xs: &mut [i32] = &mut [1, 2, 3];
169 let ptr = unsafe { Unique::new(xs as *mut [i32]) };
85aaf69f 170 let ys = unsafe { &mut **ptr };
c34b1796 171 let zs: &mut [i32] = &mut [1, 2, 3];
85aaf69f
SL
172 assert!(ys == zs);
173}
5bcae85e
SL
174
175#[test]
9e0c209e
SL
176#[allow(warnings)]
177// Have a symbol for the test below. It doesn’t need to be an actual variadic function, match the
178// ABI, or even point to an actual executable code, because the function itself is never invoked.
179#[no_mangle]
180pub fn test_variadic_fnptr() {
5bcae85e 181 use core::hash::{Hash, SipHasher};
9e0c209e
SL
182 extern {
183 fn test_variadic_fnptr(_: u64, ...) -> f64;
5bcae85e 184 }
9e0c209e 185 let p: unsafe extern fn(u64, ...) -> f64 = test_variadic_fnptr;
5bcae85e
SL
186 let q = p.clone();
187 assert_eq!(p, q);
188 assert!(!(p < q));
189 let mut s = SipHasher::new();
190 assert_eq!(p.hash(&mut s), q.hash(&mut s));
191}