Skip to content

Commit b27b8f6

Browse files
committed
Add tests for Cow::from for strings, vectors and slices
1 parent f30f569 commit b27b8f6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/libcollectionstest/str.rs

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::borrow::Cow;
1112
use std::cmp::Ordering::{Equal, Greater, Less};
1213
use std::str::from_utf8;
1314

@@ -1267,6 +1268,16 @@ fn test_box_slice_clone() {
12671268
assert_eq!(data, data2);
12681269
}
12691270

1271+
#[test]
1272+
fn test_cow_from() {
1273+
let borrowed = "borrowed";
1274+
let owned = String::from("owned");
1275+
match (Cow::from(owned.clone()), Cow::from(borrowed)) {
1276+
(Cow::Owned(o), Cow::Borrowed(b)) => assert!(o == owned && b == borrowed),
1277+
_ => panic!("invalid `Cow::from`"),
1278+
}
1279+
}
1280+
12701281
mod pattern {
12711282
use std::str::pattern::Pattern;
12721283
use std::str::pattern::{Searcher, ReverseSearcher};

src/libcollectionstest/vec.rs

+11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::borrow::Cow;
1112
use std::iter::{FromIterator, repeat};
1213
use std::mem::size_of;
1314

@@ -466,6 +467,16 @@ fn test_into_iter_count() {
466467
assert_eq!(vec![1, 2, 3].into_iter().count(), 3);
467468
}
468469

470+
#[test]
471+
fn test_cow_from() {
472+
let borrowed: &[_] = &["borrowed", "(slice)"];
473+
let owned = vec!["owned", "(vec)"];
474+
match (Cow::from(owned.clone()), Cow::from(borrowed)) {
475+
(Cow::Owned(o), Cow::Borrowed(b)) => assert!(o == owned && b == borrowed),
476+
_ => panic!("invalid `Cow::from`"),
477+
}
478+
}
479+
469480
#[bench]
470481
fn bench_new(b: &mut Bencher) {
471482
b.iter(|| {

0 commit comments

Comments
 (0)