]> git.lizzy.rs Git - rust.git/commitdiff
Add tests for `Cow::from` for strings, vectors and slices
authorTobias Bucher <tobiasbucher5991@gmail.com>
Wed, 3 Feb 2016 12:57:25 +0000 (13:57 +0100)
committerTobias Bucher <tobiasbucher5991@gmail.com>
Wed, 3 Feb 2016 19:45:30 +0000 (20:45 +0100)
src/libcollectionstest/str.rs
src/libcollectionstest/vec.rs

index 0fde70aacdca9b5821c26a8048d867ebbc19289d..25457043a9df45dd5a5274ae83e9c184b351a240 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use std::borrow::Cow;
 use std::cmp::Ordering::{Equal, Greater, Less};
 use std::str::from_utf8;
 
@@ -1267,6 +1268,16 @@ fn test_box_slice_clone() {
     assert_eq!(data, data2);
 }
 
+#[test]
+fn test_cow_from() {
+    let borrowed = "borrowed";
+    let owned = String::from("owned");
+    match (Cow::from(owned.clone()), Cow::from(borrowed)) {
+        (Cow::Owned(o), Cow::Borrowed(b)) => assert!(o == owned && b == borrowed),
+        _ => panic!("invalid `Cow::from`"),
+    }
+}
+
 mod pattern {
     use std::str::pattern::Pattern;
     use std::str::pattern::{Searcher, ReverseSearcher};
index b799be218e624cf7d98116c213647f50c9c48632..6a47f16c5ca7723d20316ba13f1e75287b7166e9 100644 (file)
@@ -8,6 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+use std::borrow::Cow;
 use std::iter::{FromIterator, repeat};
 use std::mem::size_of;
 
@@ -466,6 +467,16 @@ fn test_into_iter_count() {
     assert_eq!(vec![1, 2, 3].into_iter().count(), 3);
 }
 
+#[test]
+fn test_cow_from() {
+    let borrowed: &[_] = &["borrowed", "(slice)"];
+    let owned = vec!["owned", "(vec)"];
+    match (Cow::from(owned.clone()), Cow::from(borrowed)) {
+        (Cow::Owned(o), Cow::Borrowed(b)) => assert!(o == owned && b == borrowed),
+        _ => panic!("invalid `Cow::from`"),
+    }
+}
+
 #[bench]
 fn bench_new(b: &mut Bencher) {
     b.iter(|| {