about summary refs log tree commit diff
path: root/src/libcore/hash.rs
diff options
context:
space:
mode:
authorBen Blum <bblum@andrew.cmu.edu>2012-07-31 18:32:42 -0400
committerBen Blum <bblum@andrew.cmu.edu>2012-07-31 18:32:58 -0400
commita89ed49d3d01abbd49889832d87650b6c99ff85c (patch)
treebf087b7f700ead17dc838938d2d25d4fa47c62ba /src/libcore/hash.rs
parent513557b465a6393aebf8d309320c7eaab9ceae2c (diff)
Add hash tests: idempotent, no_bytes_dropped.
Diffstat (limited to 'src/libcore/hash.rs')
-rw-r--r--src/libcore/hash.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/libcore/hash.rs b/src/libcore/hash.rs
index b6efa8fe728..8f8f7dd40ed 100644
--- a/src/libcore/hash.rs
+++ b/src/libcore/hash.rs
@@ -401,3 +401,45 @@ fn test_hash_uint() {
     assert hash_u64(val as u64) != hash_uint(val as uint);
     assert hash_u32(val as u32) == hash_uint(val as uint);
 }
+
+#[test]
+fn test_hash_idempotent() {
+    let val64 = 0xdeadbeef_deadbeef_u64;
+    assert hash_u64(val64) == hash_u64(val64);
+    let val32 = 0xdeadbeef_u32;
+    assert hash_u32(val32) == hash_u32(val32);
+}
+
+#[test]
+fn test_hash_no_bytes_dropped_64() {
+    let val = 0xdeadbeef_deadbeef_u64;
+
+    assert hash_u64(val) != hash_u64(zero_byte(val, 0));
+    assert hash_u64(val) != hash_u64(zero_byte(val, 1));
+    assert hash_u64(val) != hash_u64(zero_byte(val, 2));
+    assert hash_u64(val) != hash_u64(zero_byte(val, 3));
+    assert hash_u64(val) != hash_u64(zero_byte(val, 4));
+    assert hash_u64(val) != hash_u64(zero_byte(val, 5));
+    assert hash_u64(val) != hash_u64(zero_byte(val, 6));
+    assert hash_u64(val) != hash_u64(zero_byte(val, 7));
+
+    fn zero_byte(val: u64, byte: uint) -> u64 {
+        assert 0 <= byte; assert byte < 8;
+        val & !(0xff << (byte * 8))
+    }
+}
+
+#[test]
+fn test_hash_no_bytes_dropped_32() {
+    let val = 0xdeadbeef_u32;
+
+    assert hash_u32(val) != hash_u32(zero_byte(val, 0));
+    assert hash_u32(val) != hash_u32(zero_byte(val, 1));
+    assert hash_u32(val) != hash_u32(zero_byte(val, 2));
+    assert hash_u32(val) != hash_u32(zero_byte(val, 3));
+
+    fn zero_byte(val: u32, byte: uint) -> u32 {
+        assert 0 <= byte; assert byte < 4;
+        val & !(0xff << (byte * 8))
+    }
+}