]> git.lizzy.rs Git - rust.git/commitdiff
test offset_from
authorRalf Jung <post@ralfj.de>
Mon, 4 Nov 2019 12:28:18 +0000 (13:28 +0100)
committerRalf Jung <post@ralfj.de>
Tue, 5 Nov 2019 19:17:35 +0000 (20:17 +0100)
tests/run-pass/ptr_offset_from.rs [new file with mode: 0644]

diff --git a/tests/run-pass/ptr_offset_from.rs b/tests/run-pass/ptr_offset_from.rs
new file mode 100644 (file)
index 0000000..92eb3f6
--- /dev/null
@@ -0,0 +1,29 @@
+#![feature(ptr_offset_from)]
+
+fn test_raw() { unsafe {
+    let buf = [0u32; 4];
+
+    let x = buf.as_ptr() as *const u8;
+    let y = x.offset(12);
+
+    assert_eq!(y.offset_from(x), 12);
+    assert_eq!(x.offset_from(y), -12);
+    assert_eq!((y as *const u32).offset_from(x as *const u32), 12/4);
+    assert_eq!((x as *const u32).offset_from(y as *const u32), -12/4);
+    
+    let x = (((x as usize) * 2) / 2) as *const u8;
+    assert_eq!(y.offset_from(x), 12);
+    assert_eq!(x.offset_from(y), -12);
+} }
+
+// This also internally uses offset_from.
+fn test_vec_into_iter() {
+    let v = Vec::<i32>::new();
+    let i = v.into_iter();
+    i.size_hint();
+}
+
+fn main() {
+    test_raw();
+    test_vec_into_iter();
+}