about summary refs log tree commit diff
path: root/src/libcore/tests
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2017-04-30 23:50:59 -0700
committerScott McMurray <scottmcm@users.noreply.github.com>2017-05-21 01:55:43 -0700
commitc05676b97f2151242a07e98bbde53e9c5d7f1e7a (patch)
tree39dddd3ba1fe72fb5d21b6049ec02b178dcf2683 /src/libcore/tests
parent0bd9e1f5e6e9832691d033f1cc32409f5e2a9145 (diff)
downloadrust-c05676b97f2151242a07e98bbde53e9c5d7f1e7a.tar.gz
rust-c05676b97f2151242a07e98bbde53e9c5d7f1e7a.zip
Add an in-place rotate method for slices to libcore
A helpful primitive for moving chunks of data around inside a slice.
In particular, adding elements to the end of a Vec then moving them
somewhere else, as a way to do efficient multiple-insert.  (There's
drain for efficient block-remove, but no easy way to block-insert.)

Talk with another example: <https://youtu.be/qH6sSOr-yk8?t=560>
Diffstat (limited to 'src/libcore/tests')
-rw-r--r--src/libcore/tests/lib.rs1
-rw-r--r--src/libcore/tests/slice.rs16
2 files changed, 17 insertions, 0 deletions
diff --git a/src/libcore/tests/lib.rs b/src/libcore/tests/lib.rs
index c52155ead4f..a9f88e9fd02 100644
--- a/src/libcore/tests/lib.rs
+++ b/src/libcore/tests/lib.rs
@@ -29,6 +29,7 @@
 #![feature(raw)]
 #![feature(sip_hash_13)]
 #![feature(slice_patterns)]
+#![feature(slice_rotate)]
 #![feature(sort_internals)]
 #![feature(sort_unstable)]
 #![feature(step_by)]
diff --git a/src/libcore/tests/slice.rs b/src/libcore/tests/slice.rs
index 15047204e50..f34f9497500 100644
--- a/src/libcore/tests/slice.rs
+++ b/src/libcore/tests/slice.rs
@@ -239,6 +239,22 @@ fn test_find_rfind() {
 }
 
 #[test]
+fn test_rotate() {
+    const N: usize = 600;
+    let a: &mut [_] = &mut [0; N];
+    for i in 0..N {
+        a[i] = i;
+    }
+
+    let k = a.rotate(42);
+
+    assert_eq!(k, N - 42);
+    for i in 0..N {
+        assert_eq!(a[(i+k)%N], i);
+    }
+}
+
+#[test]
 fn sort_unstable() {
     let mut v = [0; 600];
     let mut tmp = [0; 600];