Skip to content

Commit

Permalink
Test const Hash, fix nits
Browse files Browse the repository at this point in the history
  • Loading branch information
onestacked committed Nov 8, 2022
1 parent cebce1e commit 56e59bc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 21 deletions.
24 changes: 15 additions & 9 deletions library/core/src/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
#![stable(feature = "rust1", since = "1.0.0")]

use crate::fmt;
use crate::intrinsics::const_eval_select;
use crate::marker::{self, Destruct};

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -239,16 +240,21 @@ pub trait Hash {
where
Self: Sized,
{
//FIXME(const_iter_slice): Revert to for loop
//for piece in data {
// piece.hash(state);
//}

let mut i = 0;
while i < data.len() {
data[i].hash(state);
i += 1;
//FIXME(const_trait_impl): revert to only a for loop
fn rt<T: Hash, H: Hasher>(data: &[T], state: &mut H) {
for piece in data {
piece.hash(state)
}
}
const fn ct<T: ~const Hash, H: ~const Hasher>(data: &[T], state: &mut H) {
let mut i = 0;
while i < data.len() {
data[i].hash(state);
i += 1;
}
}
// SAFETY: same behavior, CT just uses while instead of for
unsafe { const_eval_select((data, state), ct, rt) };
}
}

Expand Down
1 change: 1 addition & 0 deletions library/core/src/hash/sip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
out |= (unsafe { *buf.get_unchecked(start + i) } as u64) << (i * 8);
i += 1;
}
//FIXME(fee1-dead): use debug_assert_eq
debug_assert!(i == len);
out
}
Expand Down
38 changes: 28 additions & 10 deletions library/core/tests/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ struct MyHasher {
hash: u64,
}

impl Default for MyHasher {
impl const Default for MyHasher {
fn default() -> MyHasher {
MyHasher { hash: 0 }
}
}

impl Hasher for MyHasher {
impl const Hasher for MyHasher {
fn write(&mut self, buf: &[u8]) {
for byte in buf {
self.hash += *byte as u64;
// FIXME(const_trait_impl): change to for loop
let mut i = 0;
while i < buf.len() {
self.hash += buf[i] as u64;
i += 1;
}
}
fn write_str(&mut self, s: &str) {
Expand All @@ -32,12 +35,25 @@ impl Hasher for MyHasher {

#[test]
fn test_writer_hasher() {
fn hash<T: Hash>(t: &T) -> u64 {
const fn hash<T: ~const Hash>(t: &T) -> u64 {
let mut s = MyHasher { hash: 0 };
t.hash(&mut s);
s.finish()
}

const {
// FIXME(fee1-dead): assert_eq
assert!(hash(&()) == 0);
assert!(hash(&5_u8) == 5);
assert!(hash(&5_u16) == 5);
assert!(hash(&5_u32) == 5);

assert!(hash(&'a') == 97);

let s: &str = "a";
assert!(hash(&s) == 97 + 0xFF);
};

assert_eq!(hash(&()), 0);

assert_eq!(hash(&5_u8), 5);
Expand Down Expand Up @@ -97,7 +113,7 @@ struct CustomHasher {
output: u64,
}

impl Hasher for CustomHasher {
impl const Hasher for CustomHasher {
fn finish(&self) -> u64 {
self.output
}
Expand All @@ -109,27 +125,29 @@ impl Hasher for CustomHasher {
}
}

impl Default for CustomHasher {
impl const Default for CustomHasher {
fn default() -> CustomHasher {
CustomHasher { output: 0 }
}
}

impl Hash for Custom {
fn hash<H: Hasher>(&self, state: &mut H) {
impl const Hash for Custom {
fn hash<H: ~const Hasher>(&self, state: &mut H) {
state.write_u64(self.hash);
}
}

#[test]
fn test_custom_state() {
fn hash<T: Hash>(t: &T) -> u64 {
const fn hash<T: ~const Hash>(t: &T) -> u64 {
let mut c = CustomHasher { output: 0 };
t.hash(&mut c);
c.finish()
}

assert_eq!(hash(&Custom { hash: 5 }), 5);

const { assert!(hash(&Custom { hash: 6 }) == 6) };
}

// FIXME: Instantiated functions with i128 in the signature is not supported in Emscripten.
Expand Down
15 changes: 14 additions & 1 deletion library/core/tests/hash/sip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use core::{mem, slice};
struct Bytes<'a>(&'a [u8]);

impl<'a> Hash for Bytes<'a> {
#[allow(unused_must_use)]
fn hash<H: Hasher>(&self, state: &mut H) {
let Bytes(v) = *self;
state.write(v);
Expand All @@ -24,6 +23,20 @@ fn hash<T: Hash>(x: &T) -> u64 {
hash_with(SipHasher::new(), x)
}

#[test]
const fn test_const_sip() {
let val1 = 0x45;
let val2 = 0xfeed;

const fn const_hash<T: ~const Hash>(x: &T) -> u64 {
let mut st = SipHasher::new();
x.hash(&mut st);
st.finish()
}

assert!(const_hash(&(val1)) != const_hash(&(val2)));
}

#[test]
#[allow(unused_must_use)]
fn test_siphash_1_3() {
Expand Down
1 change: 1 addition & 0 deletions library/core/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![feature(const_caller_location)]
#![feature(const_cell_into_inner)]
#![feature(const_convert)]
#![feature(const_hash)]
#![feature(const_heap)]
#![feature(const_maybe_uninit_as_mut_ptr)]
#![feature(const_maybe_uninit_assume_init_read)]
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@
#![feature(maybe_uninit_uninit_array)]
#![feature(const_maybe_uninit_uninit_array)]
#![feature(const_waker)]
#![feature(const_hash)]
//
// Library features (alloc):
#![feature(alloc_layout_extra)]
Expand Down Expand Up @@ -353,6 +352,7 @@
//
// Only for const-ness:
#![feature(const_collections_with_hasher)]
#![feature(const_hash)]
#![feature(const_io_structs)]
#![feature(const_ip)]
#![feature(const_ipv4)]
Expand Down

0 comments on commit 56e59bc

Please sign in to comment.