Skip to content

Commit d20431f

Browse files
author
Lukas Markeffsky
committed
add coretests for is_aligned
1 parent 186a428 commit d20431f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

library/core/tests/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#![feature(const_nonnull_new)]
1818
#![feature(const_num_from_num)]
1919
#![feature(const_pointer_byte_offsets)]
20+
#![feature(const_pointer_is_aligned)]
2021
#![feature(const_ptr_as_ref)]
2122
#![feature(const_ptr_read)]
2223
#![feature(const_ptr_write)]
@@ -80,6 +81,7 @@
8081
#![feature(never_type)]
8182
#![feature(unwrap_infallible)]
8283
#![feature(pointer_byte_offsets)]
84+
#![feature(pointer_is_aligned)]
8385
#![feature(portable_simd)]
8486
#![feature(ptr_metadata)]
8587
#![feature(once_cell)]

library/core/tests/ptr.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,41 @@ fn align_offset_with_provenance_const() {
616616
}
617617
}
618618

619+
#[test]
620+
fn is_aligned() {
621+
let data = 42;
622+
let ptr: *const i32 = &data;
623+
assert!(ptr.is_aligned());
624+
assert!(ptr.is_aligned_to(1));
625+
assert!(ptr.is_aligned_to(2));
626+
assert!(ptr.is_aligned_to(4));
627+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(1));
628+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
629+
assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
630+
631+
// At runtime either `ptr` or `ptr+1` is aligned to 8.
632+
assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8));
633+
}
634+
635+
#[test]
636+
fn is_aligned_const() {
637+
const {
638+
let data = 42;
639+
let ptr: *const i32 = &data;
640+
assert!(ptr.is_aligned());
641+
assert!(ptr.is_aligned_to(1));
642+
assert!(ptr.is_aligned_to(2));
643+
assert!(ptr.is_aligned_to(4));
644+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(1));
645+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
646+
assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
647+
648+
// At comptime neither `ptr` nor `ptr+1` is aligned to 8.
649+
assert!(!ptr.is_aligned_to(8));
650+
assert!(!ptr.wrapping_add(1).is_aligned_to(8));
651+
}
652+
}
653+
619654
#[test]
620655
fn offset_from() {
621656
let mut a = [0; 5];

0 commit comments

Comments
 (0)