From 3db41fe9f66f842e114a28ab3b9bcf7f61a14f2b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 20 Aug 2014 22:09:40 -0700 Subject: [PATCH] helper/hashcode for generating hashcodes --- helper/hashcode/hashcode.go | 10 ++++++++++ helper/hashcode/hashcode_test.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 helper/hashcode/hashcode.go create mode 100644 helper/hashcode/hashcode_test.go diff --git a/helper/hashcode/hashcode.go b/helper/hashcode/hashcode.go new file mode 100644 index 000000000..1cc8fa164 --- /dev/null +++ b/helper/hashcode/hashcode.go @@ -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))) +} diff --git a/helper/hashcode/hashcode_test.go b/helper/hashcode/hashcode_test.go new file mode 100644 index 000000000..5406f40bf --- /dev/null +++ b/helper/hashcode/hashcode_test.go @@ -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) + } + } +}