mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
helper/schema: disallow negative hash codes
This commit is contained in:
parent
f0c8ae7d3f
commit
77314a01d2
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user