about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorUlrik Sverdrup <bluss@users.noreply.github.com>2015-07-14 02:13:26 +0200
committerUlrik Sverdrup <bluss@users.noreply.github.com>2015-07-14 18:41:04 +0200
commit86d954ba2b97d971e866a70b588d73b53f3c29a0 (patch)
tree6628f6db542bc95c3777fe47062ee5bd69323e63 /src
parent1b28ffa5216c845d1cef6b0cb3e5ac7db12025d0 (diff)
downloadrust-86d954ba2b97d971e866a70b588d73b53f3c29a0.tar.gz
rust-86d954ba2b97d971e866a70b588d73b53f3c29a0.zip
core: Revive SipHash's tests
These tests were bitrotting, include them in the crate and bring them up
to date and compiling.. and they pass.
Diffstat (limited to 'src')
-rw-r--r--src/libcore/hash/sip.rs2
-rw-r--r--src/libcoretest/hash/mod.rs2
-rw-r--r--src/libcoretest/hash/sip.rs126
3 files changed, 61 insertions, 69 deletions
diff --git a/src/libcore/hash/sip.rs b/src/libcore/hash/sip.rs
index a92b72e0f00..d26e9ab7072 100644
--- a/src/libcore/hash/sip.rs
+++ b/src/libcore/hash/sip.rs
@@ -10,8 +10,6 @@
 
 //! An implementation of SipHash 2-4.
 
-#![allow(deprecated)] // until the next snapshot for inherent wrapping ops
-
 use prelude::*;
 use super::Hasher;
 
diff --git a/src/libcoretest/hash/mod.rs b/src/libcoretest/hash/mod.rs
index 5c11f0196ae..697c3ee254b 100644
--- a/src/libcoretest/hash/mod.rs
+++ b/src/libcoretest/hash/mod.rs
@@ -8,6 +8,8 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
+mod sip;
+
 use std::mem;
 use std::hash::{Hash, Hasher};
 use std::default::Default;
diff --git a/src/libcoretest/hash/sip.rs b/src/libcoretest/hash/sip.rs
index 8289d06d04c..7832985d3f1 100644
--- a/src/libcoretest/hash/sip.rs
+++ b/src/libcoretest/hash/sip.rs
@@ -8,28 +8,55 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 use test::Bencher;
-use std::prelude::*;
-use std::fmt;
 
-use str::Str;
-use string::String;
-use slice::{AsSlice, SlicePrelude};
-use vec::Vec;
-
-use core::hash::{Hash, Writer};
-use core::hash::sip::{SipState, hash, hash_with_keys};
+use core::hash::{Hash, Hasher};
+use core::hash::SipHasher;
 
 // Hash just the bytes of the slice, without length prefix
 struct Bytes<'a>(&'a [u8]);
 
-impl<'a, S: Writer> Hash<S> for Bytes<'a> {
+impl<'a> Hash for Bytes<'a> {
     #[allow(unused_must_use)]
-    fn hash(&self, state: &mut S) {
+    fn hash<H: Hasher>(&self, state: &mut H) {
         let Bytes(v) = *self;
         state.write(v);
     }
 }
 
+macro_rules! u8to64_le {
+    ($buf:expr, $i:expr) =>
+    ($buf[0+$i] as u64 |
+     ($buf[1+$i] as u64) << 8 |
+     ($buf[2+$i] as u64) << 16 |
+     ($buf[3+$i] as u64) << 24 |
+     ($buf[4+$i] as u64) << 32 |
+     ($buf[5+$i] as u64) << 40 |
+     ($buf[6+$i] as u64) << 48 |
+     ($buf[7+$i] as u64) << 56);
+    ($buf:expr, $i:expr, $len:expr) =>
+    ({
+        let mut t = 0;
+        let mut out = 0;
+        while t < $len {
+            out |= ($buf[t+$i] as u64) << t*8;
+            t += 1;
+        }
+        out
+    });
+}
+
+fn hash<T: Hash>(x: &T) -> u64 {
+    let mut st = SipHasher::new();
+    x.hash(&mut st);
+    st.finish()
+}
+
+fn hash_with_keys<T: Hash>(k1: u64, k2: u64, x: &T) -> u64 {
+    let mut st = SipHasher::new_with_keys(k1, k2);
+    x.hash(&mut st);
+    st.finish()
+}
+
 #[test]
 #[allow(unused_must_use)]
 fn test_siphash() {
@@ -104,79 +131,43 @@ fn test_siphash() {
     let k1 = 0x_0f_0e_0d_0c_0b_0a_09_08;
     let mut buf = Vec::new();
     let mut t = 0;
-    let mut state_inc = SipState::new_with_keys(k0, k1);
-    let mut state_full = SipState::new_with_keys(k0, k1);
-
-    fn to_hex_str(r: &[u8; 8]) -> String {
-        let mut s = String::new();
-        for b in r {
-            s.push_str(format!("{}", fmt::radix(*b, 16)));
-        }
-        s
-    }
-
-    fn result_bytes(h: u64) -> Vec<u8> {
-        vec![(h >> 0) as u8,
-          (h >> 8) as u8,
-          (h >> 16) as u8,
-          (h >> 24) as u8,
-          (h >> 32) as u8,
-          (h >> 40) as u8,
-          (h >> 48) as u8,
-          (h >> 56) as u8,
-        ]
-    }
-
-    fn result_str(h: u64) -> String {
-        let r = result_bytes(h);
-        let mut s = String::new();
-        for b in &r {
-            s.push_str(format!("{}", fmt::radix(*b, 16)));
-        }
-        s
-    }
+    let mut state_inc = SipHasher::new_with_keys(k0, k1);
 
     while t < 64 {
-        debug!("siphash test {}: {}", t, buf);
         let vec = u8to64_le!(vecs[t], 0);
-        let out = hash_with_keys(k0, k1, &Bytes(buf));
-        debug!("got {}, expected {}", out, vec);
+        let out = hash_with_keys(k0, k1, &Bytes(&buf));
         assert_eq!(vec, out);
 
-        state_full.reset();
-        state_full.write(buf);
-        let f = result_str(state_full.result());
-        let i = result_str(state_inc.result());
-        let v = to_hex_str(&vecs[t]);
-        debug!("{}: ({}) => inc={} full={}", t, v, i, f);
+        let full = hash_with_keys(k0, k1, &Bytes(&buf));
+        let i = state_inc.finish();
 
-        assert_eq!(f, i);
-        assert_eq!(f, v);
+        assert_eq!(full, i);
+        assert_eq!(full, vec);
 
         buf.push(t as u8);
-        state_inc.write(&[t as u8]);
+        Hasher::write(&mut state_inc, &[t as u8]);
 
         t += 1;
     }
 }
 
 #[test] #[cfg(target_arch = "arm")]
-fn test_hash_uint() {
+fn test_hash_usize() {
     let val = 0xdeadbeef_deadbeef_u64;
-    assert!(hash(&(val as u64)) != hash(&(val as uint)));
-    assert_eq!(hash(&(val as u32)), hash(&(val as uint)));
+    assert!(hash(&(val as u64)) != hash(&(val as usize)));
+    assert_eq!(hash(&(val as u32)), hash(&(val as usize)));
 }
 #[test] #[cfg(target_arch = "x86_64")]
-fn test_hash_uint() {
+fn test_hash_usize() {
     let val = 0xdeadbeef_deadbeef_u64;
-    assert_eq!(hash(&(val as u64)), hash(&(val as uint)));
-    assert!(hash(&(val as u32)) != hash(&(val as uint)));
+    assert_eq!(hash(&(val as u64)), hash(&(val as usize)));
+    assert!(hash(&(val as u32)) != hash(&(val as usize)));
 }
 #[test] #[cfg(target_arch = "x86")]
-fn test_hash_uint() {
+fn test_hash_usize() {
     let val = 0xdeadbeef_deadbeef_u64;
-    assert!(hash(&(val as u64)) != hash(&(val as uint)));
-    assert_eq!(hash(&(val as u32)), hash(&(val as uint)));
+    assert!(hash(&(val as u64)) != hash(&(val as usize)));
+    assert_eq!(hash(&(val as u32)), hash(&(val as usize)));
 }
 
 #[test]
@@ -200,7 +191,7 @@ fn test_hash_no_bytes_dropped_64() {
     assert!(hash(&val) != hash(&zero_byte(val, 6)));
     assert!(hash(&val) != hash(&zero_byte(val, 7)));
 
-    fn zero_byte(val: u64, byte: uint) -> u64 {
+    fn zero_byte(val: u64, byte: usize) -> u64 {
         assert!(byte < 8);
         val & !(0xff << (byte * 8))
     }
@@ -215,7 +206,7 @@ fn test_hash_no_bytes_dropped_32() {
     assert!(hash(&val) != hash(&zero_byte(val, 2)));
     assert!(hash(&val) != hash(&zero_byte(val, 3)));
 
-    fn zero_byte(val: u32, byte: uint) -> u32 {
+    fn zero_byte(val: u32, byte: usize) -> u32 {
         assert!(byte < 4);
         val & !(0xff << (byte * 8))
     }
@@ -230,8 +221,9 @@ fn test_hash_no_concat_alias() {
     assert!(s != t && t != u);
     assert!(hash(&s) != hash(&t) && hash(&s) != hash(&u));
 
-    let v: (&[u8], &[u8], &[u8]) = (&[1], &[0, 0], &[0]);
-    let w: (&[u8], &[u8], &[u8]) = (&[1, 0, 0, 0], &[], &[]);
+    let u = [1, 0, 0, 0];
+    let v = (&u[..1], &u[1..3], &u[3..]);
+    let w = (&u[..], &u[4..4], &u[4..4]);
 
     assert!(v != w);
     assert!(hash(&v) != hash(&w));