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