Skip to content

Commit 71f29cd

Browse files
committed
CStr::from_bytes_with_nul tests
1 parent 9414c4e commit 71f29cd

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/libstd/ffi/c_str.rs

+27
Original file line numberDiff line numberDiff line change
@@ -721,4 +721,31 @@ mod tests {
721721

722722
assert_eq!(cstr_hash, cstring_hash);
723723
}
724+
725+
#[test]
726+
fn from_bytes_with_nul() {
727+
let data = b"123\0";
728+
let cstr = CStr::from_bytes_with_nul(data);
729+
assert_eq!(cstr.map(CStr::to_bytes), Some(&b"123"[..]));
730+
assert_eq!(cstr.map(CStr::to_bytes_with_nul), Some(&b"123\0"[..]));
731+
732+
unsafe {
733+
let cstr_unchecked = CStr::from_bytes_with_nul_unchecked(data);
734+
assert_eq!(cstr, Some(cstr_unchecked));
735+
}
736+
}
737+
738+
#[test]
739+
fn from_bytes_with_nul_unterminated() {
740+
let data = b"123";
741+
let cstr = CStr::from_bytes_with_nul(data);
742+
assert!(cstr.is_none());
743+
}
744+
745+
#[test]
746+
fn from_bytes_with_nul_interior() {
747+
let data = b"1\023\0";
748+
let cstr = CStr::from_bytes_with_nul(data);
749+
assert!(cstr.is_none());
750+
}
724751
}

0 commit comments

Comments
 (0)