From 69579e4d37849fe64cbdfc7e40dc614af802704d Mon Sep 17 00:00:00 2001 From: Jonathan Reem Date: Thu, 9 Jul 2015 17:33:00 -0700 Subject: [PATCH] Test that CStr and CString have equivalent hashes. --- src/libstd/ffi/c_str.rs | 15 +++++++++++++++ src/libstd/lib.rs | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/libstd/ffi/c_str.rs b/src/libstd/ffi/c_str.rs index 1ddd74d3f4f34..f13c10156f579 100644 --- a/src/libstd/ffi/c_str.rs +++ b/src/libstd/ffi/c_str.rs @@ -538,4 +538,19 @@ mod tests { let owned = unsafe { CStr::from_ptr(ptr).to_owned() }; assert_eq!(owned.as_bytes_with_nul(), data); } + + #[test] + fn equal_hash() { + use hash; + + let data = b"123\xE2\xFA\xA6\0"; + let ptr = data.as_ptr() as *const libc::c_char; + let cstr: &'static CStr = unsafe { CStr::from_ptr(ptr) }; + + let cstr_hash = hash::hash::<_, hash::SipHasher>(&cstr); + let cstring_hash = + hash::hash::<_, hash::SipHasher>(&CString::new(&data[..data.len() - 1]).unwrap()); + + assert_eq!(cstr_hash, cstring_hash); + } } diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index caf3f497e10b4..1e82a03f28630 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -150,7 +150,7 @@ #![feature(wrapping)] #![feature(zero_one)] #![cfg_attr(windows, feature(str_utf16))] -#![cfg_attr(test, feature(float_from_str_radix, range_inclusive, float_extras))] +#![cfg_attr(test, feature(float_from_str_radix, range_inclusive, float_extras, hash_default))] #![cfg_attr(test, feature(test, rustc_private, float_consts))] #![cfg_attr(target_env = "msvc", feature(link_args))]