@@ -1000,3 +1000,49 @@ fn test_align_to_empty_mid() {
1000
1000
assert_eq ! ( mid. as_ptr( ) as usize % mem:: align_of:: <Chunk >( ) , 0 ) ;
1001
1001
}
1002
1002
}
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