From 910bbaff65284bc80e951b052fa8ac248737c2f5 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 1 Nov 2018 16:10:03 +0100 Subject: implement default hash --- hash/hash_func.go | 9 +++++++++ hash/hash_func_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 hash/hash_func.go create mode 100644 hash/hash_func_test.go diff --git a/hash/hash_func.go b/hash/hash_func.go new file mode 100644 index 0000000..853412b --- /dev/null +++ b/hash/hash_func.go @@ -0,0 +1,9 @@ +package hash + +func defaultHash(key []byte) uint32 { + var h uint32 + for _, v := range key { + h = (h << 5) + h + uint32(v) + } + return h +} diff --git a/hash/hash_func_test.go b/hash/hash_func_test.go new file mode 100644 index 0000000..5ce39b4 --- /dev/null +++ b/hash/hash_func_test.go @@ -0,0 +1,28 @@ +package hash + +import "testing" + +func TestDefaultHash(t *testing.T) { + testCases := []struct { + key string + hash uint32 + }{ + {"", 0}, + {"A", 65}, + {"AA", 2210}, + {"AAA", 72995}, + {"AAAA", 2408900}, + {"AAAAA", 79493765}, + {"AAAAAA", 2623294310}, + {"AAAAAAA", 669366375}, + {"AAAAAAAA", 614253960}, + } + for _, tc := range testCases { + t.Run(tc.key, func(t *testing.T) { + x := defaultHash([]byte(tc.key)) + if x != tc.hash { + t.Errorf("got %v, want %v", x, tc.hash) + } + }) + } +} -- cgit v1.2.3