Skip to content

Commit 84542d2

Browse files
committed
ffi::c_str added tests for empty strings
1 parent d4c96de commit 84542d2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

library/std/src/ffi/c_str/tests.rs

+16
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,19 @@ fn cstr_index_from_empty() {
193193
let cstr = CStr::from_bytes_with_nul(original).unwrap();
194194
let _ = &cstr[original.len()..];
195195
}
196+
197+
#[test]
198+
fn c_string_from_empty_string() {
199+
let original = "";
200+
let cstring = CString::new(original).unwrap();
201+
assert_eq!(original.as_bytes(), cstring.as_bytes());
202+
assert_eq!([b'\0'], cstring.as_bytes_with_nul());
203+
}
204+
205+
#[test]
206+
fn c_str_from_empty_string() {
207+
let original = b"\0";
208+
let cstr = CStr::from_bytes_with_nul(original).unwrap();
209+
assert_eq!([] as [u8; 0], cstr.to_bytes());
210+
assert_eq!([b'\0'], cstr.to_bytes_with_nul());
211+
}

0 commit comments

Comments
 (0)