Skip to content

Commit

Permalink
Test that CStr and CString have equivalent hashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
reem committed Jul 10, 2015
1 parent 84f9c61 commit 69579e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/libstd/ffi/c_str.rs
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/libstd/lib.rs
Expand Up @@ -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))]

Expand Down

0 comments on commit 69579e4

Please sign in to comment.