Skip to content

Commit 2ef9a8a

Browse files
author
Lukas Markeffsky
committed
add coretests for is_aligned
1 parent 6f6320a commit 2ef9a8a

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

library/core/tests/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#![feature(const_nonnull_new)]
2020
#![feature(const_num_from_num)]
2121
#![feature(const_pointer_byte_offsets)]
22+
#![feature(const_pointer_is_aligned)]
2223
#![feature(const_ptr_as_ref)]
2324
#![feature(const_ptr_read)]
2425
#![feature(const_ptr_write)]
@@ -82,6 +83,7 @@
8283
#![feature(never_type)]
8384
#![feature(unwrap_infallible)]
8485
#![feature(pointer_byte_offsets)]
86+
#![feature(pointer_is_aligned)]
8587
#![feature(portable_simd)]
8688
#![feature(ptr_metadata)]
8789
#![feature(once_cell)]

library/core/tests/ptr.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,54 @@ fn align_offset_issue_103361() {
632632
let _ = (SIZE as *const HugeSize).align_offset(SIZE);
633633
}
634634

635+
#[test]
636+
fn is_aligned() {
637+
let data = 42;
638+
let ptr: *const i32 = &data;
639+
assert!(ptr.is_aligned());
640+
assert!(ptr.is_aligned_to(1));
641+
assert!(ptr.is_aligned_to(2));
642+
assert!(ptr.is_aligned_to(4));
643+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(1));
644+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
645+
assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
646+
647+
// At runtime either `ptr` or `ptr+1` is aligned to 8.
648+
assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8));
649+
}
650+
651+
#[test]
652+
#[cfg(not(bootstrap))]
653+
fn is_aligned_const() {
654+
const {
655+
let data = 42;
656+
let ptr: *const i32 = &data;
657+
assert!(ptr.is_aligned());
658+
assert!(ptr.is_aligned_to(1));
659+
assert!(ptr.is_aligned_to(2));
660+
assert!(ptr.is_aligned_to(4));
661+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(1));
662+
assert!(ptr.wrapping_byte_add(2).is_aligned_to(2));
663+
assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4));
664+
665+
// At comptime neither `ptr` nor `ptr+1` is aligned to 8.
666+
assert!(!ptr.is_aligned_to(8));
667+
assert!(!ptr.wrapping_add(1).is_aligned_to(8));
668+
}
669+
}
670+
671+
#[test]
672+
#[cfg(bootstrap)]
673+
fn is_aligned_const() {
674+
const {
675+
let data = 42;
676+
let ptr: *const i32 = &data;
677+
// The bootstrap compiler always returns false for is_aligned.
678+
assert!(!ptr.is_aligned());
679+
assert!(!ptr.is_aligned_to(1));
680+
}
681+
}
682+
635683
#[test]
636684
fn offset_from() {
637685
let mut a = [0; 5];

0 commit comments

Comments
 (0)