diff options
| author | arcnmx <arcnmx@users.noreply.github.com> | 2016-02-23 01:37:21 -0500 |
|---|---|---|
| committer | arcnmx <arcnmx@users.noreply.github.com> | 2016-02-23 11:52:19 -0500 |
| commit | 71f29cd83704b39a2aefd609eab645decd3bce92 (patch) | |
| tree | 45c0f444f4124bd152d6cd252104e762805c335b /src/libstd | |
| parent | 9414c4ea18dab870bc0acbcd2be9a882aa17d68e (diff) | |
| download | rust-71f29cd83704b39a2aefd609eab645decd3bce92.tar.gz rust-71f29cd83704b39a2aefd609eab645decd3bce92.zip | |
CStr::from_bytes_with_nul tests
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/ffi/c_str.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index f3561622fa3..1db45764552 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -721,4 +721,31 @@ mod tests { assert_eq!(cstr_hash, cstring_hash); } + + #[test] + fn from_bytes_with_nul() { + let data = b"123\0"; + let cstr = CStr::from_bytes_with_nul(data); + assert_eq!(cstr.map(CStr::to_bytes), Some(&b"123"[..])); + assert_eq!(cstr.map(CStr::to_bytes_with_nul), Some(&b"123\0"[..])); + + unsafe { + let cstr_unchecked = CStr::from_bytes_with_nul_unchecked(data); + assert_eq!(cstr, Some(cstr_unchecked)); + } + } + + #[test] + fn from_bytes_with_nul_unterminated() { + let data = b"123"; + let cstr = CStr::from_bytes_with_nul(data); + assert!(cstr.is_none()); + } + + #[test] + fn from_bytes_with_nul_interior() { + let data = b"1\023\0"; + let cstr = CStr::from_bytes_with_nul(data); + assert!(cstr.is_none()); + } } |
