Skip to content

Commit a661c96

Browse files
committed
Add tests
1 parent 3c679a9 commit a661c96

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

core/tests/const_ptr.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Aligned to two bytes
2+
const DATA: [u16; 2] = [u16::from_ne_bytes([0x01, 0x23]), u16::from_ne_bytes([0x45, 0x67])];
3+
4+
const fn unaligned_ptr() -> *const u16 {
5+
// Since DATA.as_ptr() is aligned to two bytes, adding 1 byte to that produces an unaligned *const u16
6+
unsafe { (DATA.as_ptr() as *const u8).add(1) as *const u16 }
7+
}
8+
9+
#[test]
10+
fn read() {
11+
use core::ptr;
12+
13+
const FOO: i32 = unsafe { ptr::read(&42 as *const i32) };
14+
assert_eq!(FOO, 42);
15+
16+
const ALIGNED: i32 = unsafe { ptr::read_unaligned(&42 as *const i32) };
17+
assert_eq!(ALIGNED, 42);
18+
19+
const UNALIGNED_PTR: *const u16 = unaligned_ptr();
20+
21+
const UNALIGNED: u16 = unsafe { ptr::read_unaligned(UNALIGNED_PTR) };
22+
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
23+
}
24+
25+
#[test]
26+
fn const_ptr_read() {
27+
const FOO: i32 = unsafe { (&42 as *const i32).read() };
28+
assert_eq!(FOO, 42);
29+
30+
const ALIGNED: i32 = unsafe { (&42 as *const i32).read_unaligned() };
31+
assert_eq!(ALIGNED, 42);
32+
33+
const UNALIGNED_PTR: *const u16 = unaligned_ptr();
34+
35+
const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() };
36+
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
37+
}
38+
39+
#[test]
40+
fn mut_ptr_read() {
41+
const FOO: i32 = unsafe { (&42 as *const i32 as *mut i32).read() };
42+
assert_eq!(FOO, 42);
43+
44+
const ALIGNED: i32 = unsafe { (&42 as *const i32 as *mut i32).read_unaligned() };
45+
assert_eq!(ALIGNED, 42);
46+
47+
const UNALIGNED_PTR: *mut u16 = unaligned_ptr() as *mut u16;
48+
49+
const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() };
50+
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
51+
}

core/tests/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#![feature(const_assume)]
1414
#![feature(const_cell_into_inner)]
1515
#![feature(const_maybe_uninit_assume_init)]
16+
#![feature(const_ptr_read)]
17+
#![feature(const_ptr_offset)]
1618
#![feature(core_intrinsics)]
1719
#![feature(core_private_bignum)]
1820
#![feature(core_private_diy_float)]
@@ -34,6 +36,7 @@
3436
#![feature(raw)]
3537
#![feature(sort_internals)]
3638
#![feature(slice_partition_at_index)]
39+
#![feature(maybe_uninit_extra)]
3740
#![feature(maybe_uninit_write_slice)]
3841
#![feature(min_specialization)]
3942
#![feature(step_trait)]
@@ -82,6 +85,10 @@ mod cell;
8285
mod char;
8386
mod clone;
8487
mod cmp;
88+
89+
#[cfg(not(bootstrap))]
90+
mod const_ptr;
91+
8592
mod fmt;
8693
mod hash;
8794
mod intrinsics;

core/tests/mem.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,10 @@ fn uninit_write_slice_cloned_no_drop() {
267267

268268
forget(src);
269269
}
270+
271+
#[test]
272+
#[cfg(not(bootstrap))]
273+
fn uninit_const_assume_init_read() {
274+
const FOO: u32 = unsafe { MaybeUninit::new(42).assume_init_read() };
275+
assert_eq!(FOO, 42);
276+
}

0 commit comments

Comments
 (0)