]> git.lizzy.rs Git - rust.git/blob - library/alloc/tests/c_str.rs
Sync core::simd up to rust-lang/portable-simd@2e081db92aa3ee0a4563bc28ce01bdad5b1b2efd
[rust.git] / library / alloc / tests / c_str.rs
1 use std::borrow::Cow::{Borrowed, Owned};
2 use std::ffi::CStr;
3 use std::os::raw::c_char;
4
5 #[test]
6 fn to_str() {
7     let data = b"123\xE2\x80\xA6\0";
8     let ptr = data.as_ptr() as *const c_char;
9     unsafe {
10         assert_eq!(CStr::from_ptr(ptr).to_str(), Ok("123…"));
11         assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Borrowed("123…"));
12     }
13     let data = b"123\xE2\0";
14     let ptr = data.as_ptr() as *const c_char;
15     unsafe {
16         assert!(CStr::from_ptr(ptr).to_str().is_err());
17         assert_eq!(CStr::from_ptr(ptr).to_string_lossy(), Owned::<str>(format!("123\u{FFFD}")));
18     }
19 }