@@ -2152,3 +2152,42 @@ fn test_slice_fill_with_uninit() {
2152
2152
let mut a = [ MaybeUninit :: < u8 > :: uninit ( ) ; 10 ] ;
2153
2153
a. fill ( MaybeUninit :: uninit ( ) ) ;
2154
2154
}
2155
+
2156
+ #[ test]
2157
+ fn test_swap ( ) {
2158
+ let mut x = [ "a" , "b" , "c" , "d" ] ;
2159
+ x. swap ( 1 , 3 ) ;
2160
+ assert_eq ! ( x, [ "a" , "d" , "c" , "b" ] ) ;
2161
+ x. swap ( 0 , 3 ) ;
2162
+ assert_eq ! ( x, [ "b" , "d" , "c" , "a" ] ) ;
2163
+ }
2164
+
2165
+ mod swap_panics {
2166
+ #[ test]
2167
+ #[ should_panic( expected = "index out of bounds: the len is 4 but the index is 4" ) ]
2168
+ fn index_a_equals_len ( ) {
2169
+ let mut x = [ "a" , "b" , "c" , "d" ] ;
2170
+ x. swap ( 4 , 2 ) ;
2171
+ }
2172
+
2173
+ #[ test]
2174
+ #[ should_panic( expected = "index out of bounds: the len is 4 but the index is 4" ) ]
2175
+ fn index_b_equals_len ( ) {
2176
+ let mut x = [ "a" , "b" , "c" , "d" ] ;
2177
+ x. swap ( 2 , 4 ) ;
2178
+ }
2179
+
2180
+ #[ test]
2181
+ #[ should_panic( expected = "index out of bounds: the len is 4 but the index is 5" ) ]
2182
+ fn index_a_greater_than_len ( ) {
2183
+ let mut x = [ "a" , "b" , "c" , "d" ] ;
2184
+ x. swap ( 5 , 2 ) ;
2185
+ }
2186
+
2187
+ #[ test]
2188
+ #[ should_panic( expected = "index out of bounds: the len is 4 but the index is 5" ) ]
2189
+ fn index_b_greater_than_len ( ) {
2190
+ let mut x = [ "a" , "b" , "c" , "d" ] ;
2191
+ x. swap ( 2 , 5 ) ;
2192
+ }
2193
+ }
0 commit comments