Skip to content

Commit af97dc1

Browse files
committed
add tests for slow path joining
old tests cover the new fast paths already this adds tests for joining into Strings with long separators (>4 byte) and joining into Vec<T>, T: !Copy
1 parent f676ddd commit af97dc1

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/liballoc/tests/slice.rs

+14
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,20 @@ fn test_join() {
609609
assert_eq!(v.join(&0), [1, 0, 2, 0, 3]);
610610
}
611611

612+
#[test]
613+
fn test_join_nocopy() {
614+
let v: [Vec<String>; 0] = [];
615+
let comma = String::new(",");
616+
assert_eq!(v.join(&",".into()), []);
617+
assert_eq!(["a".into(), "ab".into()].join(comma), "a,ab");
618+
assert_eq!(["a".into(), "ab".into(), "abc".into()].join(comma), "a,ab,abc");
619+
620+
let v: [&[_]; 2] = [&["a".into()], &["a".into(), "b".into()]];
621+
assert_eq!(v.join(comma), "a,ab");
622+
let v: [&[_]; 2] = [&["a".into()], &["b".into()], ["c".into()]];
623+
assert_eq!(v.join(comma), "a,b,c");
624+
}
625+
612626
#[test]
613627
fn test_insert() {
614628
let mut a = vec![1, 2, 4];

src/liballoc/tests/str.rs

+13
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,19 @@ fn test_join_for_different_lengths() {
162162
test_join!("-a-bc", ["", "a", "bc"], "-");
163163
}
164164

165+
// join has fast paths for small separators up to 4 bytes
166+
// this tests the slow paths.
167+
#[test]
168+
fn test_join_for_different_lengths_with_long_separator() {
169+
assert_eq!("~~~~~".len(), 15);
170+
171+
let empty: &[&str] = &[];
172+
test_join!("", empty, "~~~~~");
173+
test_join!("a", ["a"], "~~~~~");
174+
test_join!("a~~~~~b", ["a", "b"], "~~~~~");
175+
test_join!("~~~~~a~~~~~bc", ["", "a", "bc"], "~~~~~");
176+
}
177+
165178
#[test]
166179
fn test_unsafe_slice() {
167180
assert_eq!("ab", unsafe {"abc".slice_unchecked(0, 2)});

0 commit comments

Comments
 (0)