about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlbin Hedman <albin9604@gmail.com>2021-06-14 17:30:12 +0200
committerAlbin Hedman <albin9604@gmail.com>2021-06-27 12:05:17 +0200
commit6c890bb9697c0971e70e1eba495b666516ecece6 (patch)
treec32953c9a3beea5e564363c694f4b985d03e1991
parent5fbb1354ce9c88c689cd11e127755f0cd3cf96af (diff)
downloadrust-6c890bb9697c0971e70e1eba495b666516ecece6.tar.gz
rust-6c890bb9697c0971e70e1eba495b666516ecece6.zip
Revert "Revert tests added by PR 81167."
This reverts commit cebfcd3256a6ec8655f0d9f45426d6f42a92da9c.
-rw-r--r--library/core/tests/const_ptr.rs50
1 files changed, 50 insertions, 0 deletions
diff --git a/library/core/tests/const_ptr.rs b/library/core/tests/const_ptr.rs
index 4acd059ab03..152fed803ec 100644
--- a/library/core/tests/const_ptr.rs
+++ b/library/core/tests/const_ptr.rs
@@ -49,3 +49,53 @@ fn mut_ptr_read() {
     const UNALIGNED: u16 = unsafe { UNALIGNED_PTR.read_unaligned() };
     assert_eq!(UNALIGNED, u16::from_ne_bytes([0x23, 0x45]));
 }
+
+#[test]
+fn write() {
+    use core::ptr;
+
+    const fn write_aligned() -> i32 {
+        let mut res = 0;
+        unsafe {
+            ptr::write(&mut res as *mut _, 42);
+        }
+        res
+    }
+    const ALIGNED: i32 = write_aligned();
+    assert_eq!(ALIGNED, 42);
+
+    const fn write_unaligned() -> [u16; 2] {
+        let mut two_aligned = [0u16; 2];
+        unsafe {
+            let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
+            ptr::write_unaligned(unaligned_ptr, u16::from_ne_bytes([0x23, 0x45]));
+        }
+        two_aligned
+    }
+    const UNALIGNED: [u16; 2] = write_unaligned();
+    assert_eq!(UNALIGNED, [u16::from_ne_bytes([0x00, 0x23]), u16::from_ne_bytes([0x45, 0x00])]);
+}
+
+#[test]
+fn mut_ptr_write() {
+    const fn aligned() -> i32 {
+        let mut res = 0;
+        unsafe {
+            (&mut res as *mut i32).write(42);
+        }
+        res
+    }
+    const ALIGNED: i32 = aligned();
+    assert_eq!(ALIGNED, 42);
+
+    const fn write_unaligned() -> [u16; 2] {
+        let mut two_aligned = [0u16; 2];
+        unsafe {
+            let unaligned_ptr = (two_aligned.as_mut_ptr() as *mut u8).add(1) as *mut u16;
+            unaligned_ptr.write_unaligned(u16::from_ne_bytes([0x23, 0x45]));
+        }
+        two_aligned
+    }
+    const UNALIGNED: [u16; 2] = write_unaligned();
+    assert_eq!(UNALIGNED, [u16::from_ne_bytes([0x00, 0x23]), u16::from_ne_bytes([0x45, 0x00])]);
+}