1
Fork 0

Improve splice docs and tests

This commit is contained in:
Matt Ickstadt 2017-04-08 16:04:30 -05:00
parent b85e2e4735
commit cec00bab1d
4 changed files with 97 additions and 4 deletions

View file

@ -1386,8 +1386,8 @@ impl String {
/// replaces with the given string, and yields the removed chars.
/// The given string doesnt need to be the same length as the range.
///
/// Note: The element range is removed even if the iterator is not
/// consumed until the end.
/// Note: The element range is removed when the `Splice` is dropped,
/// even if the iterator is not consumed until the end.
///
/// # Panics
///

View file

@ -427,6 +427,54 @@ fn test_splice() {
assert_eq!(t, "world");
}
#[test]
#[should_panic]
fn test_splice_char_boundary() {
let mut s = "Hello, 世界!".to_owned();
s.splice(..8, "");
}
#[test]
fn test_splice_inclusive_range() {
let mut v = String::from("12345");
let t: String = v.splice(2...3, "789").collect();
assert_eq!(v, "127895");
assert_eq!(t, "34");
let t2: String = v.splice(1...2, "A").collect();
assert_eq!(v, "1A895");
assert_eq!(t2, "27");
}
#[test]
#[should_panic]
fn test_splice_out_of_bounds() {
let mut s = String::from("12345");
s.splice(5..6, "789");
}
#[test]
#[should_panic]
fn test_splice_inclusive_out_of_bounds() {
let mut s = String::from("12345");
s.splice(5...5, "789");
}
#[test]
fn test_splice_empty() {
let mut s = String::from("12345");
let t: String = s.splice(1..2, "").collect();
assert_eq!(s, "1345");
assert_eq!(t, "2");
}
#[test]
fn test_splice_unbounded() {
let mut s = String::from("12345");
let t: String = s.splice(.., "").collect();
assert_eq!(s, "");
assert_eq!(t, "12345");
}
#[test]
fn test_extend_ref() {
let mut a = "foo".to_string();

View file

@ -580,7 +580,7 @@ fn test_drain_inclusive_out_of_bounds() {
}
#[test]
fn splice() {
fn test_splice() {
let mut v = vec![1, 2, 3, 4, 5];
let a = [10, 11, 12];
v.splice(2..4, a.iter().cloned());
@ -589,6 +589,51 @@ fn splice() {
assert_eq!(v, &[1, 20, 11, 12, 5]);
}
#[test]
fn test_splice_inclusive_range() {
let mut v = vec![1, 2, 3, 4, 5];
let a = [10, 11, 12];
let t1: Vec<_> = v.splice(2...3, a.iter().cloned()).collect();
assert_eq!(v, &[1, 2, 10, 11, 12, 5]);
assert_eq!(t1, &[3, 4]);
let t2: Vec<_> = v.splice(1...2, Some(20)).collect();
assert_eq!(v, &[1, 20, 11, 12, 5]);
assert_eq!(t2, &[2, 10]);
}
#[test]
#[should_panic]
fn test_splice_out_of_bounds() {
let mut v = vec![1, 2, 3, 4, 5];
let a = [10, 11, 12];
v.splice(5..6, a.iter().cloned());
}
#[test]
#[should_panic]
fn test_splice_inclusive_out_of_bounds() {
let mut v = vec![1, 2, 3, 4, 5];
let a = [10, 11, 12];
v.splice(5...5, a.iter().cloned());
}
#[test]
fn test_splice_items_zero_sized() {
let mut vec = vec![(), (), ()];
let vec2 = vec![];
let t: Vec<_> = vec.splice(1..2, vec2.iter().cloned()).collect();
assert_eq!(vec, &[(), ()]);
assert_eq!(t, &[()]);
}
#[test]
fn test_splice_unbounded() {
let mut vec = vec![1, 2, 3, 4, 5];
let t: Vec<_> = vec.splice(.., None).collect();
assert_eq!(vec, &[]);
assert_eq!(t, &[1, 2, 3, 4, 5]);
}
#[test]
fn test_into_boxed_slice() {
let xs = vec![1, 2, 3];

View file

@ -1063,7 +1063,7 @@ impl<T> Vec<T> {
/// Note 1: The element range is removed even if the iterator is only
/// partially consumed or not consumed at all.
///
/// Note 2: It is unspecified how many elements are removed from the vector,
/// Note 2: It is unspecified how many elements are removed from the vector
/// if the `Drain` value is leaked.
///
/// # Panics