Skip to content

Commit 69579e4

Browse files
committed
Test that CStr and CString have equivalent hashes.
1 parent 84f9c61 commit 69579e4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/libstd/ffi/c_str.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,4 +538,19 @@ mod tests {
538538
let owned = unsafe { CStr::from_ptr(ptr).to_owned() };
539539
assert_eq!(owned.as_bytes_with_nul(), data);
540540
}
541+
542+
#[test]
543+
fn equal_hash() {
544+
use hash;
545+
546+
let data = b"123\xE2\xFA\xA6\0";
547+
let ptr = data.as_ptr() as *const libc::c_char;
548+
let cstr: &'static CStr = unsafe { CStr::from_ptr(ptr) };
549+
550+
let cstr_hash = hash::hash::<_, hash::SipHasher>(&cstr);
551+
let cstring_hash =
552+
hash::hash::<_, hash::SipHasher>(&CString::new(&data[..data.len() - 1]).unwrap());
553+
554+
assert_eq!(cstr_hash, cstring_hash);
555+
}
541556
}

src/libstd/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
#![feature(wrapping)]
151151
#![feature(zero_one)]
152152
#![cfg_attr(windows, feature(str_utf16))]
153-
#![cfg_attr(test, feature(float_from_str_radix, range_inclusive, float_extras))]
153+
#![cfg_attr(test, feature(float_from_str_radix, range_inclusive, float_extras, hash_default))]
154154
#![cfg_attr(test, feature(test, rustc_private, float_consts))]
155155
#![cfg_attr(target_env = "msvc", feature(link_args))]
156156

0 commit comments

Comments
 (0)