Skip to content

Commit

Permalink
add slice::swap tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Oct 11, 2021
1 parent 2a8ff8d commit c517a0d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions library/core/tests/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2152,3 +2152,42 @@ fn test_slice_fill_with_uninit() {
let mut a = [MaybeUninit::<u8>::uninit(); 10];
a.fill(MaybeUninit::uninit());
}

#[test]
fn test_swap() {
let mut x = ["a", "b", "c", "d"];
x.swap(1, 3);
assert_eq!(x, ["a", "d", "c", "b"]);
x.swap(0, 3);
assert_eq!(x, ["b", "d", "c", "a"]);
}

mod swap_panics {
#[test]
#[should_panic(expected = "index out of bounds: the len is 4 but the index is 4")]
fn index_a_equals_len() {
let mut x = ["a", "b", "c", "d"];
x.swap(4, 2);
}

#[test]
#[should_panic(expected = "index out of bounds: the len is 4 but the index is 4")]
fn index_b_equals_len() {
let mut x = ["a", "b", "c", "d"];
x.swap(2, 4);
}

#[test]
#[should_panic(expected = "index out of bounds: the len is 4 but the index is 5")]
fn index_a_greater_than_len() {
let mut x = ["a", "b", "c", "d"];
x.swap(5, 2);
}

#[test]
#[should_panic(expected = "index out of bounds: the len is 4 but the index is 5")]
fn index_b_greater_than_len() {
let mut x = ["a", "b", "c", "d"];
x.swap(2, 5);
}
}

0 comments on commit c517a0d

Please sign in to comment.