]> git.lizzy.rs Git - rust.git/blob - library/alloc/tests/c_str.rs
Merge commit '266e96785ab71834b917bf474f130a6d8fdecd4b' into sync_cg_clif-2022-10-23
[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 }