helper/schema: disallow negative hash codes

This commit is contained in:
Mitchell Hashimoto 2015-04-23 16:57:26 +02:00
parent f0c8ae7d3f
commit 77314a01d2
2 changed files with 17 additions and 0 deletions

View File

@ -123,6 +123,9 @@ func (s *Set) add(item interface{}) int {
s.once.Do(s.init) s.once.Do(s.init)
code := s.F(item) code := s.F(item)
if code < 0 {
code *= -1
}
if _, ok := s.m[code]; !ok { if _, ok := s.m[code]; !ok {
s.m[code] = item s.m[code] = item
} }

View File

@ -18,6 +18,20 @@ func TestSetAdd(t *testing.T) {
} }
} }
func TestSetAdd_negative(t *testing.T) {
// Since we don't allow negative hashes, this should just hash to the
// same thing...
s := &Set{F: testSetInt}
s.Add(-1)
s.Add(1)
expected := []interface{}{-1}
actual := s.List()
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestSetContains(t *testing.T) { func TestSetContains(t *testing.T) {
s := &Set{F: testSetInt} s := &Set{F: testSetInt}
s.Add(5) s.Add(5)