Skip to content

Commit 3db41fe

Browse files
committed
helper/hashcode for generating hashcodes
1 parent 6530a0b commit 3db41fe

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎helper/hashcode/hashcode.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package hashcode
2+
3+
import (
4+
"hash/crc32"
5+
)
6+
7+
// String hashes a string to a unique hashcode.
8+
func String(s string) int {
9+
return int(crc32.ChecksumIEEE([]byte(s)))
10+
}

‎helper/hashcode/hashcode_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package hashcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestString(t *testing.T) {
8+
v := "hello, world"
9+
expected := String(v)
10+
for i := 0; i < 100; i++ {
11+
actual := String(v)
12+
if actual != expected {
13+
t.Fatalf("bad: %#v", actual)
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)