diff options
Diffstat (limited to 'vendor/rustix/tests/path')
-rw-r--r-- | vendor/rustix/tests/path/arg.rs | 167 | ||||
-rw-r--r-- | vendor/rustix/tests/path/dec_int.rs | 20 | ||||
-rw-r--r-- | vendor/rustix/tests/path/main.rs | 13 |
3 files changed, 200 insertions, 0 deletions
diff --git a/vendor/rustix/tests/path/arg.rs b/vendor/rustix/tests/path/arg.rs new file mode 100644 index 000000000..0eb511be1 --- /dev/null +++ b/vendor/rustix/tests/path/arg.rs @@ -0,0 +1,167 @@ +use rustix::ffi::{CStr, CString}; +use rustix::io; +use rustix::path::Arg; +#[cfg(feature = "itoa")] +use rustix::path::DecInt; +use std::borrow::Cow; +use std::ffi::{OsStr, OsString}; +use std::path::{Component, Components, Iter, Path, PathBuf}; + +#[test] +fn test_arg() { + use rustix::cstr; + use std::borrow::Borrow; + + let t: &str = "hello"; + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: String = "hello".to_owned(); + assert_eq!("hello", Arg::as_str(&t).unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: &OsStr = OsStr::new("hello"); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: OsString = OsString::from("hello".to_owned()); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: &Path = Path::new("hello"); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: PathBuf = PathBuf::from("hello".to_owned()); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: &CStr = cstr!("hello"); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: CString = cstr!("hello").to_owned(); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!( + cstr!("hello"), + Borrow::borrow(&Arg::as_cow_c_str(&t).unwrap()) + ); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Components = Path::new("hello").components(); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Component = Path::new("hello").components().next().unwrap(); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Iter = Path::new("hello").iter(); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, str> = Cow::Borrowed("hello"); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, str> = Cow::Owned("hello".to_owned()); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, OsStr> = Cow::Borrowed(OsStr::new("hello")); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, OsStr> = Cow::Owned(OsString::from("hello".to_owned())); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, CStr> = Cow::Borrowed(cstr!("hello")); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Cow<'_, CStr> = Cow::Owned(cstr!("hello").to_owned()); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: &[u8] = b"hello"; + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + let t: Vec<u8> = b"hello".to_vec(); + assert_eq!("hello", t.as_str().unwrap()); + assert_eq!("hello".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("hello"), Borrow::borrow(&t.into_c_str().unwrap())); + + #[cfg(feature = "itoa")] + { + let t: DecInt = DecInt::new(43110); + assert_eq!("43110", t.as_str()); + assert_eq!("43110".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(cstr!("43110"), Borrow::borrow(&t.as_cow_c_str().unwrap())); + assert_eq!(cstr!("43110"), t.as_c_str()); + assert_eq!(cstr!("43110"), Borrow::borrow(&t.into_c_str().unwrap())); + } +} + +#[test] +fn test_invalid() { + use std::borrow::Borrow; + + let t: &[u8] = b"hello\xc0world"; + assert_eq!(t.as_str().unwrap_err(), io::Errno::INVAL); + assert_eq!("hello\u{fffd}world".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!( + b"hello\xc0world\0", + Borrow::<CStr>::borrow(&t.as_cow_c_str().unwrap()).to_bytes_with_nul() + ); + assert_eq!( + b"hello\xc0world\0", + Borrow::<CStr>::borrow(&t.into_c_str().unwrap()).to_bytes_with_nul() + ); +} + +#[test] +fn test_embedded_nul() { + let t: &[u8] = b"hello\0world"; + assert_eq!("hello\0world", t.as_str().unwrap()); + assert_eq!("hello\0world".to_owned(), Arg::to_string_lossy(&t)); + assert_eq!(t.as_cow_c_str().unwrap_err(), io::Errno::INVAL); + assert_eq!(t.into_c_str().unwrap_err(), io::Errno::INVAL); +} diff --git a/vendor/rustix/tests/path/dec_int.rs b/vendor/rustix/tests/path/dec_int.rs new file mode 100644 index 000000000..6ce1a5123 --- /dev/null +++ b/vendor/rustix/tests/path/dec_int.rs @@ -0,0 +1,20 @@ +use rustix::path::DecInt; + +#[test] +fn test_dec_int() { + assert_eq!(DecInt::new(0).as_ref().to_str().unwrap(), "0"); + assert_eq!(DecInt::new(-1).as_ref().to_str().unwrap(), "-1"); + assert_eq!(DecInt::new(789).as_ref().to_str().unwrap(), "789"); + assert_eq!( + DecInt::new(i64::MIN).as_ref().to_str().unwrap(), + i64::MIN.to_string() + ); + assert_eq!( + DecInt::new(i64::MAX).as_ref().to_str().unwrap(), + i64::MAX.to_string() + ); + assert_eq!( + DecInt::new(u64::MAX).as_ref().to_str().unwrap(), + u64::MAX.to_string() + ); +} diff --git a/vendor/rustix/tests/path/main.rs b/vendor/rustix/tests/path/main.rs new file mode 100644 index 000000000..3215c7e5e --- /dev/null +++ b/vendor/rustix/tests/path/main.rs @@ -0,0 +1,13 @@ +//! Tests for [`rustix::path`]. + +#![cfg(any(feature = "fs", feature = "net"))] +#![cfg(not(windows))] +#![cfg_attr(target_os = "wasi", feature(wasi_ext))] +#![cfg_attr(io_lifetimes_use_std, feature(io_safety))] +#![cfg_attr(core_c_str, feature(core_c_str))] +#![cfg_attr(alloc_c_string, feature(alloc_c_string))] + +#[cfg(not(feature = "rustc-dep-of-std"))] +mod arg; +#[cfg(feature = "itoa")] +mod dec_int; |