Skip to content

Commit c517a0d

Browse files
committed
add slice::swap tests
1 parent 2a8ff8d commit c517a0d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

library/core/tests/slice.rs

+39
Original file line numberDiff line numberDiff line change
@@ -2152,3 +2152,42 @@ fn test_slice_fill_with_uninit() {
21522152
let mut a = [MaybeUninit::<u8>::uninit(); 10];
21532153
a.fill(MaybeUninit::uninit());
21542154
}
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

Comments
 (0)