]> git.lizzy.rs Git - rust.git/commitdiff
Test that CStr and CString have equivalent hashes.
authorJonathan Reem <jonathan.reem@gmail.com>
Fri, 10 Jul 2015 00:33:00 +0000 (17:33 -0700)
committerJonathan Reem <jonathan.reem@gmail.com>
Fri, 10 Jul 2015 00:41:09 +0000 (17:41 -0700)
src/libstd/ffi/c_str.rs
src/libstd/lib.rs

index 1ddd74d3f4f3427352c9bdef4d011959af9aa4a6..f13c10156f579acd7b01801ba1f29a0d415d5584 100644 (file)
@@ -538,4 +538,19 @@ fn to_owned() {
         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);
+    }
 }
index caf3f497e10b4c22e6a54088b9902def278c7655..1e82a03f286302d0e951d62c3e2622f198ebd598 100644 (file)
 #![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))]