]> git.lizzy.rs Git - rust.git/commitdiff
Add test for `volatile_set_memory`
authorniluxv <niluxv.opensource.C-h2ty6xl@yandex.com>
Mon, 23 Aug 2021 10:42:13 +0000 (12:42 +0200)
committerniluxv <niluxv.opensource.C-h2ty6xl@yandex.com>
Mon, 23 Aug 2021 10:42:13 +0000 (12:42 +0200)
tests/run-pass/write-bytes.rs

index 7c9a38fca696dd3e9401c355cb79b5ad00233cc4..b2050c5393e221f2e3874c0828c12fcd7f6650f7 100644 (file)
@@ -1,3 +1,5 @@
+#![feature(core_intrinsics)] // for `volatile_set_memory`
+
 #[repr(C)]
 #[derive(Copy, Clone)]
 struct Foo {
@@ -42,4 +44,42 @@ fn main() {
         assert_eq!(w[idx].b, 0xcdcdcdcdcdcdcdcd);
         assert_eq!(w[idx].c, 0xcdcdcdcdcdcdcdcd);
     }
+
+    // -----
+    // `std::intrinsics::volatile_set_memory` should behave identically
+
+    let mut v: [u64; LENGTH] = [0; LENGTH];
+
+    for idx in 0..LENGTH {
+        assert_eq!(v[idx], 0);
+    }
+
+    unsafe {
+        let p = v.as_mut_ptr();
+        ::std::intrinsics::volatile_set_memory(p, 0xab, LENGTH);
+    }
+
+    for idx in 0..LENGTH {
+        assert_eq!(v[idx], 0xabababababababab);
+    }
+
+    // -----
+
+    let mut w: [Foo; LENGTH] = [Foo { a: 0, b: 0, c: 0 }; LENGTH];
+    for idx in 0..LENGTH {
+        assert_eq!(w[idx].a, 0);
+        assert_eq!(w[idx].b, 0);
+        assert_eq!(w[idx].c, 0);
+    }
+
+    unsafe {
+        let p = w.as_mut_ptr();
+        ::std::intrinsics::volatile_set_memory(p, 0xcd, LENGTH);
+    }
+
+    for idx in 0..LENGTH {
+        assert_eq!(w[idx].a, 0xcdcdcdcdcdcdcdcd);
+        assert_eq!(w[idx].b, 0xcdcdcdcdcdcdcdcd);
+        assert_eq!(w[idx].c, 0xcdcdcdcdcdcdcdcd);
+    }
 }