Skip to content

Commit 6c890bb

Browse files
committed
Revert "Revert tests added by PR 81167."
This reverts commit cebfcd3.
1 parent 5fbb135 commit 6c890bb

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

library/core/tests/const_ptr.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,53 @@ fn mut_ptr_read() {
4949
const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() };
5050
assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
5151
}
52+
53+
#[test]
54+
fn write() {
55+
use core::ptr;
56+
57+
const fn write_aligned() -> i32 {
58+
let mut res = 0;
59+
unsafe {
60+
ptr::write(&mut res as *mut _, 42);
61+
}
62+
res
63+
}
64+
const ALIGNED: i32 = write_aligned();
65+
assert_eq!(ALIGNED, 42);
66+
67+
const fn write_unaligned() -> [u16; 2] {
68+
let mut two_aligned = [0u16; 2];
69+
unsafe {
70+
let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
71+
ptr::write_unaligned(unaligned_ptr, u16::from_ne_bytes([0x23, 0x45]));
72+
}
73+
two_aligned
74+
}
75+
const UNALIGNED: [u16; 2] = write_unaligned();
76+
assert_eq!(UNALIGNED, [u16::from_ne_bytes([0x00, 0x23]), u16::from_ne_bytes([0x45, 0x00])]);
77+
}
78+
79+
#[test]
80+
fn mut_ptr_write() {
81+
const fn aligned() -> i32 {
82+
let mut res = 0;
83+
unsafe {
84+
(&mut res as *mut i32).write(42);
85+
}
86+
res
87+
}
88+
const ALIGNED: i32 = aligned();
89+
assert_eq!(ALIGNED, 42);
90+
91+
const fn write_unaligned() -> [u16; 2] {
92+
let mut two_aligned = [0u16; 2];
93+
unsafe {
94+
let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
95+
unaligned_ptr.write_unaligned(u16::from_ne_bytes([0x23, 0x45]));
96+
}
97+
two_aligned
98+
}
99+
const UNALIGNED: [u16; 2] = write_unaligned();
100+
assert_eq!(UNALIGNED, [u16::from_ne_bytes([0x00, 0x23]), u16::from_ne_bytes([0x45, 0x00])]);
101+
}

0 commit comments

Comments
 (0)