Skip to content

Commit 9b55f6b

Browse files
committed
Added tests for the respective AddAssign/Add operations for char concatenation to String and Cow<str>.
1 parent 342277f commit 9b55f6b

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/liballoc/tests/cow_str.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,33 @@ fn check_cow_clone_from() {
139139
c1.clone_from(&c2);
140140
assert!(c1.into_owned().capacity() >= 25);
141141
}
142+
143+
#[test]
144+
fn check_cow_add_assign_char() {
145+
let test_char = '👋';
146+
147+
let mut borrowed = Cow::Borrowed("Hello, World! ");
148+
let borrow_empty = Cow::Borrowed("");
149+
150+
let mut owned: Cow<'_, str> = Cow::Owned(String::from("Hi, World! "));
151+
let owned_empty: Cow<'_, str> = Cow::Owned(String::new());
152+
153+
let mut s = borrow_empty.clone();
154+
s += test_char;
155+
assert_eq!(test_char.to_string(), s);
156+
if let Cow::Owned(_) = s {
157+
panic!("Adding empty strings to a borrow should note allocate");
158+
}
159+
let mut s = owned_empty.clone();
160+
s += test_char;
161+
assert_eq!(test_char.to_string(), s);
162+
if let Cow::Owned(_) = s {
163+
panic!("Adding empty strings to a borrow should note allocate");
164+
}
165+
166+
owned += test_char;
167+
borrowed += test_char;
168+
169+
assert_eq!(format!("Hi, World! {}", test_char), owned);
170+
assert_eq!(format!("Hello, World! {}", test_char), borrowed);
171+
}

src/liballoc/tests/string.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ fn test_add_assign() {
195195
assert_eq!(s.as_str(), "");
196196
s += "abc";
197197
assert_eq!(s.as_str(), "abc");
198-
s += "ประเทศไทย中华Việt Nam";
199-
assert_eq!(s.as_str(), "abcประเทศไทย中华Việt Nam");
198+
s += "ประเทศไทย中华Việt Nam ";
199+
assert_eq!(s.as_str(), "abcประเทศไทย中华Việt Nam ");
200+
s += '👋';
201+
assert_eq!(s.as_str(), "abcประเทศไทย中华Việt Nam 👋")
200202
}
201203

202204
#[test]
@@ -304,9 +306,10 @@ fn test_str_clear() {
304306
fn test_str_add() {
305307
let a = String::from("12345");
306308
let b = a + "2";
307-
let b = b + "2";
308-
assert_eq!(b.len(), 7);
309-
assert_eq!(b, "1234522");
309+
let b = b + "2 ";
310+
let b = b + '👋';
311+
assert_eq!(b.len(), 12);
312+
assert_eq!(b, "1234522 👋");
310313
}
311314

312315
#[test]

0 commit comments

Comments
 (0)