Skip to content

Commit 5371cc4

Browse files
author
Lukas Markeffsky
committed
add coretests for is_aligned
1 parent 5d1c7a2 commit 5371cc4

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

library/core/tests/lib.rs

+2
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

+48
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,54 @@ fn align_offset_with_provenance_const() {
620620
}
621621
}
622622

623+
#[test]
624+
fn is_aligned() {
625+
let data = 42;
626+
let ptr: *const i32 = &data;
627+
assert!(ptr.is_aligned());
628+
assert!(ptr.is_aligned_to(1));
629+
assert!(ptr.is_aligned_to(2));
630+
assert!(ptr.is_aligned_to(4));
631+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(1));
632+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
633+
assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
634+
635+
// At runtime either `ptr` or `ptr+1` is aligned to 8.
636+
assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8));
637+
}
638+
639+
#[test]
640+
#[cfg(not(bootstrap))]
641+
fn is_aligned_const() {
642+
const {
643+
let data = 42;
644+
let ptr: *const i32 = &data;
645+
assert!(ptr.is_aligned());
646+
assert!(ptr.is_aligned_to(1));
647+
assert!(ptr.is_aligned_to(2));
648+
assert!(ptr.is_aligned_to(4));
649+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(1));
650+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
651+
assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
652+
653+
// At comptime neither `ptr` nor `ptr+1` is aligned to 8.
654+
assert!(!ptr.is_aligned_to(8));
655+
assert!(!ptr.wrapping_add(1).is_aligned_to(8));
656+
}
657+
}
658+
659+
#[test]
660+
#[cfg(bootstrap)]
661+
fn is_aligned_const() {
662+
const {
663+
let data = 42;
664+
let ptr: *const i32 = &data;
665+
// The bootstrap compiler always returns false for is_aligned.
666+
assert!(!ptr.is_aligned());
667+
assert!(!ptr.is_aligned_to(1));
668+
}
669+
}
670+
623671
#[test]
624672
fn offset_from() {
625673
let mut a = [0; 5];

0 commit comments

Comments
 (0)