helper/hashcode for generating hashcodes

This commit is contained in:
Mitchell Hashimoto 2014-08-20 22:09:40 -07:00
parent 6530a0ba2e
commit 3db41fe9f6
2 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,10 @@
package hashcode
import (
"hash/crc32"
)
// String hashes a string to a unique hashcode.
func String(s string) int {
return int(crc32.ChecksumIEEE([]byte(s)))
}

View File

@ -0,0 +1,16 @@
package hashcode
import (
"testing"
)
func TestString(t *testing.T) {
v := "hello, world"
expected := String(v)
for i := 0; i < 100; i++ {
actual := String(v)
if actual != expected {
t.Fatalf("bad: %#v", actual)
}
}
}