Skip to content

Commit 23f71fe

Browse files
committed
Add tests from Cow
1 parent 6d40751 commit 23f71fe

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/liballoc/tests/borrow.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::borrow::{Cow, ToOwned};
2+
use std::ffi::{CStr, OsStr};
3+
use std::path::Path;
4+
use std::rc::Rc;
5+
use std::sync::Arc;
6+
7+
macro_rules! test_from_cow {
8+
($value:ident => $($ty:ty),+) => {$(
9+
let borrowed = <$ty>::from(Cow::Borrowed($value));
10+
let owned = <$ty>::from(Cow::Owned($value.to_owned()));
11+
assert_eq!($value, &*borrowed);
12+
assert_eq!($value, &*owned);
13+
)+};
14+
($value:ident : & $ty:ty) => {
15+
test_from_cow!($value => Box<$ty>, Rc<$ty>, Arc<$ty>);
16+
}
17+
}
18+
19+
#[test]
20+
fn test_from_cow_slice() {
21+
let slice: &[i32] = &[1, 2, 3];
22+
test_from_cow!(slice: &[i32]);
23+
}
24+
25+
#[test]
26+
fn test_from_cow_str() {
27+
let string = "hello";
28+
test_from_cow!(string: &str);
29+
}
30+
31+
#[test]
32+
fn test_from_cow_c_str() {
33+
let string = CStr::from_bytes_with_nul(b"hello\0").unwrap();
34+
test_from_cow!(string: &CStr);
35+
}
36+
37+
#[test]
38+
fn test_from_cow_os_str() {
39+
let string = OsStr::new("hello");
40+
test_from_cow!(string: &OsStr);
41+
}
42+
43+
#[test]
44+
fn test_from_cow_path() {
45+
let path = Path::new("hello");
46+
test_from_cow!(path: &Path);
47+
}

src/liballoc/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use std::hash::{Hash, Hasher};
2020

2121
mod arc;
2222
mod binary_heap;
23+
mod borrow;
2324
mod boxed;
2425
mod btree;
2526
mod cow_str;

0 commit comments

Comments
 (0)