Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update hashcode to always generate a positive index #967

Merged
merged 2 commits into from
Feb 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion helper/hashcode/hashcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import (
)

// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline above this please, nitpick style.

}
11 changes: 10 additions & 1 deletion helper/hashcode/hashcode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ 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 {
if index := String(ip); index < 0 {
t.Fatalf("Bad Index %#v for ip %s", index, ip)
}
}
}