diff --git a/tests/run-pass/ptr_arith_offset.rs b/tests/run-pass/ptr_arith_offset.rs deleted file mode 100644 index a6ee151e3e13..000000000000 --- a/tests/run-pass/ptr_arith_offset.rs +++ /dev/null @@ -1,6 +0,0 @@ -fn main() { - let v = [1i16, 2]; - let x = &v as *const [i16] as *const i16; - let x = x.wrapping_offset(1); - assert_eq!(unsafe { *x }, 2); -} diff --git a/tests/run-pass/ptr_arith_offset_overflow.rs b/tests/run-pass/ptr_arith_offset_overflow.rs deleted file mode 100644 index fdd980e2177b..000000000000 --- a/tests/run-pass/ptr_arith_offset_overflow.rs +++ /dev/null @@ -1,12 +0,0 @@ -use std::ptr; - -fn main() { - let v = [1i16, 2]; - let x = &mut ptr::null(); // going through memory as there are more sanity checks along that path - *x = v.as_ptr().wrapping_offset(1); // ptr to the 2nd element - // Adding 2*isize::max and then 1 is like substracting 1 - *x = x.wrapping_offset(isize::MAX); - *x = x.wrapping_offset(isize::MAX); - *x = x.wrapping_offset(1); - assert_eq!(unsafe { **x }, 1); -} diff --git a/tests/run-pass/ptr_int_casts.rs b/tests/run-pass/ptr_int_casts.rs index 468b37af5bea..b9815126a8c7 100644 --- a/tests/run-pass/ptr_int_casts.rs +++ b/tests/run-pass/ptr_int_casts.rs @@ -7,7 +7,7 @@ fn eq_ref(x: &T, y: &T) -> bool { fn f() -> i32 { 42 } -fn main() { +fn ptr_int_casts() { // int-ptr-int assert_eq!(1 as *const i32 as usize, 1); assert_eq!((1 as *const i32).wrapping_offset(4) as usize, 1 + 4*4); @@ -40,3 +40,29 @@ fn main() { // involving types other than usize assert_eq!((-1i32) as usize as *const i32 as usize, (-1i32) as usize); } + +fn ptr_int_ops() { + let v = [1i16, 2]; + let x = &v[1] as *const i16 as usize; + // arithmetic + let _y = x + 4; + let _y = 4 + x; + let _y = x - 2; + // bit-operations, covered by alignment + assert_eq!(x & 1, 0); + assert_eq!(x & 0, 0); + assert_eq!(1 & (x+1), 1); + let _y = !1 & x; + let _y = !0 & x; + let _y = x & !1; + // remainder, covered by alignment + assert_eq!(x % 2, 0); + assert_eq!((x+1) % 2, 1); + // remainder with 1 is always 0 + assert_eq!(x % 1, 0); +} + +fn main() { + ptr_int_casts(); + ptr_int_ops(); +} diff --git a/tests/run-pass/ptr_int_ops.rs b/tests/run-pass/ptr_int_ops.rs deleted file mode 100644 index 9a29c2d30837..000000000000 --- a/tests/run-pass/ptr_int_ops.rs +++ /dev/null @@ -1,20 +0,0 @@ -fn main() { - let v = [1i16, 2]; - let x = &v[1] as *const i16 as usize; - // arithmetic - let _y = x + 4; - let _y = 4 + x; - let _y = x - 2; - // bit-operations, covered by alignment - assert_eq!(x & 1, 0); - assert_eq!(x & 0, 0); - assert_eq!(1 & (x+1), 1); - let _y = !1 & x; - let _y = !0 & x; - let _y = x & !1; - // remainder, covered by alignment - assert_eq!(x % 2, 0); - assert_eq!((x+1) % 2, 1); - // remainder with 1 is always 0 - assert_eq!(x % 1, 0); -} diff --git a/tests/run-pass/ptr_offset.rs b/tests/run-pass/ptr_offset.rs index 1c7f0eb71797..f83720b547c0 100644 --- a/tests/run-pass/ptr_offset.rs +++ b/tests/run-pass/ptr_offset.rs @@ -1,6 +1,58 @@ -fn f() -> i32 { 42 } +#![feature(ptr_offset_from)] +use std::{mem, ptr}; fn main() { + test_offset_from(); + test_vec_into_iter(); + ptr_arith_offset(); + ptr_arith_offset_overflow(); + ptr_offset(); +} + +fn test_offset_from() { unsafe { + let buf = [0u32; 4]; + + let x = buf.as_ptr() as *const u8; + let y = x.offset(12); + + assert_eq!(y.offset_from(x), 12); + assert_eq!(x.offset_from(y), -12); + assert_eq!((y as *const u32).offset_from(x as *const u32), 12/4); + assert_eq!((x as *const u32).offset_from(y as *const u32), -12/4); + + let x = (((x as usize) * 2) / 2) as *const u8; + assert_eq!(y.offset_from(x), 12); + assert_eq!(x.offset_from(y), -12); +} } + +// This also internally uses offset_from. +fn test_vec_into_iter() { + let v = Vec::::new(); + let i = v.into_iter(); + i.size_hint(); +} + +fn ptr_arith_offset() { + let v = [1i16, 2]; + let x = &v as *const [i16] as *const i16; + let x = x.wrapping_offset(1); + assert_eq!(unsafe { *x }, 2); +} + +fn ptr_arith_offset_overflow() { + let v = [1i16, 2]; + let x = &mut ptr::null(); // going through memory as there are more sanity checks along that path + *x = v.as_ptr().wrapping_offset(1); // ptr to the 2nd element + // Adding 2*isize::max and then 1 is like substracting 1 + *x = x.wrapping_offset(isize::MAX); + *x = x.wrapping_offset(isize::MAX); + *x = x.wrapping_offset(1); + assert_eq!(unsafe { **x }, 1); +} + +fn ptr_offset() { + fn f() -> i32 { 42 } + let v = [1i16, 2]; let x = &v as *const [i16; 2] as *const i16; let x = unsafe { x.offset(1) }; @@ -10,7 +62,7 @@ fn main() { unsafe { let p = f as fn() -> i32 as usize; let x = (p as *mut u32).offset(0) as usize; - let f: fn() -> i32 = std::mem::transmute(x); + let f: fn() -> i32 = mem::transmute(x); assert_eq!(f(), 42); } } diff --git a/tests/run-pass/ptr_offset_from.rs b/tests/run-pass/ptr_offset_from.rs deleted file mode 100644 index 92eb3f6e46e3..000000000000 --- a/tests/run-pass/ptr_offset_from.rs +++ /dev/null @@ -1,29 +0,0 @@ -#![feature(ptr_offset_from)] - -fn test_raw() { unsafe { - let buf = [0u32; 4]; - - let x = buf.as_ptr() as *const u8; - let y = x.offset(12); - - assert_eq!(y.offset_from(x), 12); - assert_eq!(x.offset_from(y), -12); - assert_eq!((y as *const u32).offset_from(x as *const u32), 12/4); - assert_eq!((x as *const u32).offset_from(y as *const u32), -12/4); - - let x = (((x as usize) * 2) / 2) as *const u8; - assert_eq!(y.offset_from(x), 12); - assert_eq!(x.offset_from(y), -12); -} } - -// This also internally uses offset_from. -fn test_vec_into_iter() { - let v = Vec::::new(); - let i = v.into_iter(); - i.size_hint(); -} - -fn main() { - test_raw(); - test_vec_into_iter(); -} diff --git a/tests/run-pass/raw.rs b/tests/run-pass/ptr_raw.rs similarity index 100% rename from tests/run-pass/raw.rs rename to tests/run-pass/ptr_raw.rs