add coretests for is_aligned
This commit is contained in:
parent
6f6320a0a9
commit
2ef9a8ae0f
2 changed files with 50 additions and 0 deletions
|
@ -19,6 +19,7 @@
|
||||||
#![feature(const_nonnull_new)]
|
#![feature(const_nonnull_new)]
|
||||||
#![feature(const_num_from_num)]
|
#![feature(const_num_from_num)]
|
||||||
#![feature(const_pointer_byte_offsets)]
|
#![feature(const_pointer_byte_offsets)]
|
||||||
|
#![feature(const_pointer_is_aligned)]
|
||||||
#![feature(const_ptr_as_ref)]
|
#![feature(const_ptr_as_ref)]
|
||||||
#![feature(const_ptr_read)]
|
#![feature(const_ptr_read)]
|
||||||
#![feature(const_ptr_write)]
|
#![feature(const_ptr_write)]
|
||||||
|
@ -82,6 +83,7 @@
|
||||||
#![feature(never_type)]
|
#![feature(never_type)]
|
||||||
#![feature(unwrap_infallible)]
|
#![feature(unwrap_infallible)]
|
||||||
#![feature(pointer_byte_offsets)]
|
#![feature(pointer_byte_offsets)]
|
||||||
|
#![feature(pointer_is_aligned)]
|
||||||
#![feature(portable_simd)]
|
#![feature(portable_simd)]
|
||||||
#![feature(ptr_metadata)]
|
#![feature(ptr_metadata)]
|
||||||
#![feature(once_cell)]
|
#![feature(once_cell)]
|
||||||
|
|
|
@ -632,6 +632,54 @@ fn align_offset_issue_103361() {
|
||||||
let _ = (SIZE as *const HugeSize).align_offset(SIZE);
|
let _ = (SIZE as *const HugeSize).align_offset(SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn is_aligned() {
|
||||||
|
let data = 42;
|
||||||
|
let ptr: *const i32 = &data;
|
||||||
|
assert!(ptr.is_aligned());
|
||||||
|
assert!(ptr.is_aligned_to(1));
|
||||||
|
assert!(ptr.is_aligned_to(2));
|
||||||
|
assert!(ptr.is_aligned_to(4));
|
||||||
|
assert!(ptr.wrapping_byte_add(2).is_aligned_to(1));
|
||||||
|
assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
|
||||||
|
assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
|
||||||
|
|
||||||
|
// At runtime either `ptr` or `ptr+1` is aligned to 8.
|
||||||
|
assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
fn is_aligned_const() {
|
||||||
|
const {
|
||||||
|
let data = 42;
|
||||||
|
let ptr: *const i32 = &data;
|
||||||
|
assert!(ptr.is_aligned());
|
||||||
|
assert!(ptr.is_aligned_to(1));
|
||||||
|
assert!(ptr.is_aligned_to(2));
|
||||||
|
assert!(ptr.is_aligned_to(4));
|
||||||
|
assert!(ptr.wrapping_byte_add(2).is_aligned_to(1));
|
||||||
|
assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
|
||||||
|
assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
|
||||||
|
|
||||||
|
// At comptime neither `ptr` nor `ptr+1` is aligned to 8.
|
||||||
|
assert!(!ptr.is_aligned_to(8));
|
||||||
|
assert!(!ptr.wrapping_add(1).is_aligned_to(8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(bootstrap)]
|
||||||
|
fn is_aligned_const() {
|
||||||
|
const {
|
||||||
|
let data = 42;
|
||||||
|
let ptr: *const i32 = &data;
|
||||||
|
// The bootstrap compiler always returns false for is_aligned.
|
||||||
|
assert!(!ptr.is_aligned());
|
||||||
|
assert!(!ptr.is_aligned_to(1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn offset_from() {
|
fn offset_from() {
|
||||||
let mut a = [0; 5];
|
let mut a = [0; 5];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue