Skip to content

Commit

Permalink
map.go: Fix Clear() to actually clear the values and keys.
Browse files Browse the repository at this point in the history
Because groups are using value semantics, the implementation of Clear() was
accidentally clearing a copy of the group instead of the group itself.

Fixes dolthub#20.
  • Loading branch information
reltuk authored and userpro committed Nov 8, 2023
1 parent b0361de commit e0a1027
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ func (m *Map[K, V]) Clear() {
}
var k K
var v V
for _, g := range m.groups {
for i := range m.groups {
g := &m.groups[i]
for i := range g.keys {
g.keys[i] = k
g.values[i] = v
Expand Down
9 changes: 9 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ func testMapClear[K comparable](t *testing.T, keys []K) {
return
})
assert.Equal(t, 0, calls)

// Assert that the map was actually cleared...
var k K
for _, g := range m.groups {
for i := range g.keys {
assert.Equal(t, k, g.keys[i])
assert.Equal(t, 0, g.values[i])
}
}
}

func testMapIter[K comparable](t *testing.T, keys []K) {
Expand Down

0 comments on commit e0a1027

Please sign in to comment.