Skip to content

Commit cec00ba

Browse files
committed
Improve splice docs and tests
1 parent b85e2e4 commit cec00ba

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

src/libcollections/string.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1386,8 +1386,8 @@ impl String {
13861386
/// replaces with the given string, and yields the removed chars.
13871387
/// The given string doesn’t need to be the same length as the range.
13881388
///
1389-
/// Note: The element range is removed even if the iterator is not
1390-
/// consumed until the end.
1389+
/// Note: The element range is removed when the `Splice` is dropped,
1390+
/// even if the iterator is not consumed until the end.
13911391
///
13921392
/// # Panics
13931393
///

src/libcollections/tests/string.rs

+48
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,54 @@ fn test_splice() {
427427
assert_eq!(t, "world");
428428
}
429429

430+
#[test]
431+
#[should_panic]
432+
fn test_splice_char_boundary() {
433+
let mut s = "Hello, 世界!".to_owned();
434+
s.splice(..8, "");
435+
}
436+
437+
#[test]
438+
fn test_splice_inclusive_range() {
439+
let mut v = String::from("12345");
440+
let t: String = v.splice(2...3, "789").collect();
441+
assert_eq!(v, "127895");
442+
assert_eq!(t, "34");
443+
let t2: String = v.splice(1...2, "A").collect();
444+
assert_eq!(v, "1A895");
445+
assert_eq!(t2, "27");
446+
}
447+
448+
#[test]
449+
#[should_panic]
450+
fn test_splice_out_of_bounds() {
451+
let mut s = String::from("12345");
452+
s.splice(5..6, "789");
453+
}
454+
455+
#[test]
456+
#[should_panic]
457+
fn test_splice_inclusive_out_of_bounds() {
458+
let mut s = String::from("12345");
459+
s.splice(5...5, "789");
460+
}
461+
462+
#[test]
463+
fn test_splice_empty() {
464+
let mut s = String::from("12345");
465+
let t: String = s.splice(1..2, "").collect();
466+
assert_eq!(s, "1345");
467+
assert_eq!(t, "2");
468+
}
469+
470+
#[test]
471+
fn test_splice_unbounded() {
472+
let mut s = String::from("12345");
473+
let t: String = s.splice(.., "").collect();
474+
assert_eq!(s, "");
475+
assert_eq!(t, "12345");
476+
}
477+
430478
#[test]
431479
fn test_extend_ref() {
432480
let mut a = "foo".to_string();

src/libcollections/tests/vec.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ fn test_drain_inclusive_out_of_bounds() {
580580
}
581581

582582
#[test]
583-
fn splice() {
583+
fn test_splice() {
584584
let mut v = vec![1, 2, 3, 4, 5];
585585
let a = [10, 11, 12];
586586
v.splice(2..4, a.iter().cloned());
@@ -589,6 +589,51 @@ fn splice() {
589589
assert_eq!(v, &[1, 20, 11, 12, 5]);
590590
}
591591

592+
#[test]
593+
fn test_splice_inclusive_range() {
594+
let mut v = vec![1, 2, 3, 4, 5];
595+
let a = [10, 11, 12];
596+
let t1: Vec<_> = v.splice(2...3, a.iter().cloned()).collect();
597+
assert_eq!(v, &[1, 2, 10, 11, 12, 5]);
598+
assert_eq!(t1, &[3, 4]);
599+
let t2: Vec<_> = v.splice(1...2, Some(20)).collect();
600+
assert_eq!(v, &[1, 20, 11, 12, 5]);
601+
assert_eq!(t2, &[2, 10]);
602+
}
603+
604+
#[test]
605+
#[should_panic]
606+
fn test_splice_out_of_bounds() {
607+
let mut v = vec![1, 2, 3, 4, 5];
608+
let a = [10, 11, 12];
609+
v.splice(5..6, a.iter().cloned());
610+
}
611+
612+
#[test]
613+
#[should_panic]
614+
fn test_splice_inclusive_out_of_bounds() {
615+
let mut v = vec![1, 2, 3, 4, 5];
616+
let a = [10, 11, 12];
617+
v.splice(5...5, a.iter().cloned());
618+
}
619+
620+
#[test]
621+
fn test_splice_items_zero_sized() {
622+
let mut vec = vec![(), (), ()];
623+
let vec2 = vec![];
624+
let t: Vec<_> = vec.splice(1..2, vec2.iter().cloned()).collect();
625+
assert_eq!(vec, &[(), ()]);
626+
assert_eq!(t, &[()]);
627+
}
628+
629+
#[test]
630+
fn test_splice_unbounded() {
631+
let mut vec = vec![1, 2, 3, 4, 5];
632+
let t: Vec<_> = vec.splice(.., None).collect();
633+
assert_eq!(vec, &[]);
634+
assert_eq!(t, &[1, 2, 3, 4, 5]);
635+
}
636+
592637
#[test]
593638
fn test_into_boxed_slice() {
594639
let xs = vec![1, 2, 3];

src/libcollections/vec.rs

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

0 commit comments

Comments
 (0)