From a5040ecc032e314229ca0393dbac9f8affcb28e1 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Wed, 11 Feb 2015 10:59:21 -0800 Subject: [PATCH 1/2] Update hashcode to always generate a positive --- helper/hashcode/hashcode.go | 14 ++++++++++---- helper/hashcode/hashcode_test.go | 16 ++++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/helper/hashcode/hashcode.go b/helper/hashcode/hashcode.go index 1cc8fa1645fb..5927b4e7b335 100644 --- a/helper/hashcode/hashcode.go +++ b/helper/hashcode/hashcode.go @@ -1,10 +1,16 @@ package hashcode -import ( - "hash/crc32" -) +import "hash/crc32" // String hashes a string to a unique hashcode. +// +// crc32 returns a uint32, but for our use we need +// and non negative integer. Here we cast to an integer +// and invert it if the result is negative. func String(s string) int { - return int(crc32.ChecksumIEEE([]byte(s))) + v := int(crc32.ChecksumIEEE([]byte(s))) + if v < 0 { + return -v + } + return v } diff --git a/helper/hashcode/hashcode_test.go b/helper/hashcode/hashcode_test.go index 5406f40bf677..538551ff102b 100644 --- a/helper/hashcode/hashcode_test.go +++ b/helper/hashcode/hashcode_test.go @@ -1,8 +1,6 @@ package hashcode -import ( - "testing" -) +import "testing" func TestString(t *testing.T) { v := "hello, world" @@ -10,7 +8,17 @@ func TestString(t *testing.T) { for i := 0; i < 100; i++ { actual := String(v) if actual != expected { - t.Fatalf("bad: %#v", actual) + t.Fatalf("bad: %#v\n\t%#v", actual, expected) + } + } +} + +func TestString_positiveIndex(t *testing.T) { + ips := []string{"192.168.1.3", "192.168.1.5"} + for _, ip := range ips { + index := String(ip) + if index < 0 { + t.Fatalf("Bad Index %#v for ip %s", index, ip) } } } From 5602348695af75169528a01dc4850ee9305fb994 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Wed, 11 Feb 2015 11:40:49 -0800 Subject: [PATCH 2/2] formatting, cleanups --- helper/hashcode/hashcode.go | 5 ++++- helper/hashcode/hashcode_test.go | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/helper/hashcode/hashcode.go b/helper/hashcode/hashcode.go index 5927b4e7b335..214a2f1e5908 100644 --- a/helper/hashcode/hashcode.go +++ b/helper/hashcode/hashcode.go @@ -1,6 +1,8 @@ package hashcode -import "hash/crc32" +import ( + "hash/crc32" +) // String hashes a string to a unique hashcode. // @@ -12,5 +14,6 @@ func String(s string) int { if v < 0 { return -v } + return v } diff --git a/helper/hashcode/hashcode_test.go b/helper/hashcode/hashcode_test.go index 538551ff102b..47bc85760e98 100644 --- a/helper/hashcode/hashcode_test.go +++ b/helper/hashcode/hashcode_test.go @@ -1,6 +1,8 @@ package hashcode -import "testing" +import ( + "testing" +) func TestString(t *testing.T) { v := "hello, world" @@ -16,8 +18,7 @@ func TestString(t *testing.T) { func TestString_positiveIndex(t *testing.T) { ips := []string{"192.168.1.3", "192.168.1.5"} for _, ip := range ips { - index := String(ip) - if index < 0 { + if index := String(ip); index < 0 { t.Fatalf("Bad Index %#v for ip %s", index, ip) } }