Skip to content

Commit b0fe99e

Browse files
committed
consolidate ptr tests in fewer files
1 parent 5c823a1 commit b0fe99e

File tree

7 files changed

+81
-70
lines changed

7 files changed

+81
-70
lines changed

tests/run-pass/ptr_arith_offset.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/run-pass/ptr_arith_offset_overflow.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/run-pass/ptr_int_casts.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn eq_ref<T>(x: &T, y: &T) -> bool {
77

88
fn f() -> i32 { 42 }
99

10-
fn main() {
10+
fn ptr_int_casts() {
1111
// int-ptr-int
1212
assert_eq!(1 as *const i32 as usize, 1);
1313
assert_eq!((1 as *const i32).wrapping_offset(4) as usize, 1 + 4*4);
@@ -40,3 +40,29 @@ fn main() {
4040
// involving types other than usize
4141
assert_eq!((-1i32) as usize as *const i32 as usize, (-1i32) as usize);
4242
}
43+
44+
fn ptr_int_ops() {
45+
let v = [1i16, 2];
46+
let x = &v[1] as *const i16 as usize;
47+
// arithmetic
48+
let _y = x + 4;
49+
let _y = 4 + x;
50+
let _y = x - 2;
51+
// bit-operations, covered by alignment
52+
assert_eq!(x & 1, 0);
53+
assert_eq!(x & 0, 0);
54+
assert_eq!(1 & (x+1), 1);
55+
let _y = !1 & x;
56+
let _y = !0 & x;
57+
let _y = x & !1;
58+
// remainder, covered by alignment
59+
assert_eq!(x % 2, 0);
60+
assert_eq!((x+1) % 2, 1);
61+
// remainder with 1 is always 0
62+
assert_eq!(x % 1, 0);
63+
}
64+
65+
fn main() {
66+
ptr_int_casts();
67+
ptr_int_ops();
68+
}

tests/run-pass/ptr_int_ops.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/run-pass/ptr_offset.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,58 @@
1-
fn f() -> i32 { 42 }
1+
#![feature(ptr_offset_from)]
2+
use std::{mem, ptr};
23

34
fn main() {
5+
test_offset_from();
6+
test_vec_into_iter();
7+
ptr_arith_offset();
8+
ptr_arith_offset_overflow();
9+
ptr_offset();
10+
}
11+
12+
fn test_offset_from() { unsafe {
13+
let buf = [0u32; 4];
14+
15+
let x = buf.as_ptr() as *const u8;
16+
let y = x.offset(12);
17+
18+
assert_eq!(y.offset_from(x), 12);
19+
assert_eq!(x.offset_from(y), -12);
20+
assert_eq!((y as *const u32).offset_from(x as *const u32), 12/4);
21+
assert_eq!((x as *const u32).offset_from(y as *const u32), -12/4);
22+
23+
let x = (((x as usize) * 2) / 2) as *const u8;
24+
assert_eq!(y.offset_from(x), 12);
25+
assert_eq!(x.offset_from(y), -12);
26+
} }
27+
28+
// This also internally uses offset_from.
29+
fn test_vec_into_iter() {
30+
let v = Vec::<i32>::new();
31+
let i = v.into_iter();
32+
i.size_hint();
33+
}
34+
35+
fn ptr_arith_offset() {
36+
let v = [1i16, 2];
37+
let x = &v as *const [i16] as *const i16;
38+
let x = x.wrapping_offset(1);
39+
assert_eq!(unsafe { *x }, 2);
40+
}
41+
42+
fn ptr_arith_offset_overflow() {
43+
let v = [1i16, 2];
44+
let x = &mut ptr::null(); // going through memory as there are more sanity checks along that path
45+
*x = v.as_ptr().wrapping_offset(1); // ptr to the 2nd element
46+
// Adding 2*isize::max and then 1 is like substracting 1
47+
*x = x.wrapping_offset(isize::MAX);
48+
*x = x.wrapping_offset(isize::MAX);
49+
*x = x.wrapping_offset(1);
50+
assert_eq!(unsafe { **x }, 1);
51+
}
52+
53+
fn ptr_offset() {
54+
fn f() -> i32 { 42 }
55+
456
let v = [1i16, 2];
557
let x = &v as *const [i16; 2] as *const i16;
658
let x = unsafe { x.offset(1) };
@@ -10,7 +62,7 @@ fn main() {
1062
unsafe {
1163
let p = f as fn() -> i32 as usize;
1264
let x = (p as *mut u32).offset(0) as usize;
13-
let f: fn() -> i32 = std::mem::transmute(x);
65+
let f: fn() -> i32 = mem::transmute(x);
1466
assert_eq!(f(), 42);
1567
}
1668
}

tests/run-pass/ptr_offset_from.rs

Lines changed: 0 additions & 29 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)