Skip to content

Commit d0e59f5

Browse files
committed
add tests for copy_within
1 parent b3ffd33 commit d0e59f5

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/libcore/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#![feature(inner_deref)]
4040
#![feature(slice_internals)]
4141
#![feature(option_replace)]
42+
#![feature(copy_within)]
4243

4344
extern crate core;
4445
extern crate test;

src/libcore/tests/slice.rs

+46
Original file line numberDiff line numberDiff line change
@@ -1000,3 +1000,49 @@ fn test_align_to_empty_mid() {
10001000
assert_eq!(mid.as_ptr() as usize % mem::align_of::<Chunk>(), 0);
10011001
}
10021002
}
1003+
1004+
#[test]
1005+
fn test_copy_within() {
1006+
// Start to end, with a RangeTo.
1007+
let mut bytes = *b"Hello, World!";
1008+
bytes.copy_within(..3, 10);
1009+
assert_eq!(&bytes, b"Hello, WorHel");
1010+
1011+
// End to start, with a RangeFrom.
1012+
let mut bytes = *b"Hello, World!";
1013+
bytes.copy_within(10.., 0);
1014+
assert_eq!(&bytes, b"ld!lo, World!");
1015+
1016+
// Overlapping, with a RangeInclusive.
1017+
let mut bytes = *b"Hello, World!";
1018+
bytes.copy_within(0..=11, 1);
1019+
assert_eq!(&bytes, b"HHello, World");
1020+
1021+
// Whole slice, with a RangeFull.
1022+
let mut bytes = *b"Hello, World!";
1023+
bytes.copy_within(.., 0);
1024+
assert_eq!(&bytes, b"Hello, World!");
1025+
}
1026+
1027+
#[test]
1028+
#[should_panic(expected = "src is out of bounds")]
1029+
fn test_copy_within_panics_src_too_long() {
1030+
let mut bytes = *b"Hello, World!";
1031+
// The length is only 13, so 14 is out of bounds.
1032+
bytes.copy_within(10..14, 0);
1033+
}
1034+
1035+
#[test]
1036+
#[should_panic(expected = "dest is out of bounds")]
1037+
fn test_copy_within_panics_dest_too_long() {
1038+
let mut bytes = *b"Hello, World!";
1039+
// The length is only 13, so a slice of length 4 starting at index 10 is out of bounds.
1040+
bytes.copy_within(0..4, 10);
1041+
}
1042+
#[test]
1043+
#[should_panic(expected = "src end is before src start")]
1044+
fn test_copy_within_panics_src_inverted() {
1045+
let mut bytes = *b"Hello, World!";
1046+
// 2 is greater than 1, so this range is invalid.
1047+
bytes.copy_within(2..1, 0);
1048+
}

0 commit comments

Comments
 (0)