Add tests from Cow
This commit is contained in:
parent
6d40751b37
commit
23f71fe5b5
2 changed files with 48 additions and 0 deletions
47
src/liballoc/tests/borrow.rs
Normal file
47
src/liballoc/tests/borrow.rs
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
use std::borrow::{Cow, ToOwned};
|
||||||
|
use std::ffi::{CStr, OsStr};
|
||||||
|
use std::path::Path;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
macro_rules! test_from_cow {
|
||||||
|
($value:ident => $($ty:ty),+) => {$(
|
||||||
|
let borrowed = <$ty>::from(Cow::Borrowed($value));
|
||||||
|
let owned = <$ty>::from(Cow::Owned($value.to_owned()));
|
||||||
|
assert_eq!($value, &*borrowed);
|
||||||
|
assert_eq!($value, &*owned);
|
||||||
|
)+};
|
||||||
|
($value:ident : & $ty:ty) => {
|
||||||
|
test_from_cow!($value => Box<$ty>, Rc<$ty>, Arc<$ty>);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_cow_slice() {
|
||||||
|
let slice: &[i32] = &[1, 2, 3];
|
||||||
|
test_from_cow!(slice: &[i32]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_cow_str() {
|
||||||
|
let string = "hello";
|
||||||
|
test_from_cow!(string: &str);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_cow_c_str() {
|
||||||
|
let string = CStr::from_bytes_with_nul(b"hello\0").unwrap();
|
||||||
|
test_from_cow!(string: &CStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_cow_os_str() {
|
||||||
|
let string = OsStr::new("hello");
|
||||||
|
test_from_cow!(string: &OsStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_from_cow_path() {
|
||||||
|
let path = Path::new("hello");
|
||||||
|
test_from_cow!(path: &Path);
|
||||||
|
}
|
|
@ -20,6 +20,7 @@ use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
mod arc;
|
mod arc;
|
||||||
mod binary_heap;
|
mod binary_heap;
|
||||||
|
mod borrow;
|
||||||
mod boxed;
|
mod boxed;
|
||||||
mod btree;
|
mod btree;
|
||||||
mod cow_str;
|
mod cow_str;
|
||||||
|
|
Loading…
Reference in a new issue