diff options
| author | Michael Pratt <mpratt@google.com> | 2024-09-17 18:00:21 -0400 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-10-30 15:11:27 +0000 |
| commit | b5fec2cf54ff9f7b562cb904a2a025266aec2763 (patch) | |
| tree | be774c2cef2df29292da32aa083577c207fe2500 /src/internal/runtime/maps/map_test.go | |
| parent | 2220fd36368c96da3dd833bdc2bbd13be291216a (diff) | |
| download | go-b5fec2cf54ff9f7b562cb904a2a025266aec2763.tar.xz | |
cmd/compile,runtime: add indirect key/elem to swissmap
We use the same heuristics as existing maps.
For #54766.
Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest-swissmap
Change-Id: I44bb51483cae2c1714717f1b501850fb9e55a39a
Reviewed-on: https://go-review.googlesource.com/c/go/+/616461
Auto-Submit: Michael Pratt <mpratt@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/internal/runtime/maps/map_test.go')
| -rw-r--r-- | src/internal/runtime/maps/map_test.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/internal/runtime/maps/map_test.go b/src/internal/runtime/maps/map_test.go index cd40db8712..42db55c6a4 100644 --- a/src/internal/runtime/maps/map_test.go +++ b/src/internal/runtime/maps/map_test.go @@ -628,3 +628,74 @@ func TestMapZeroSizeSlot(t *testing.T) { t.Errorf("elem address outside groups allocation; got %p want [%p, %p]", got, start, end) } } + +func TestMapIndirect(t *testing.T) { + type big [abi.SwissMapMaxKeyBytes + abi.SwissMapMaxElemBytes]byte + + m, typ := maps.NewTestMap[big, big](8) + + key := big{} + elem := big{} + elem[0] = 128 + + for i := 0; i < 31; i++ { + key[0] += 1 + elem[0] += 1 + m.Put(typ, unsafe.Pointer(&key), unsafe.Pointer(&elem)) + + if maps.DebugLog { + fmt.Printf("After put %v: %v\n", key, m) + } + } + + if m.Used() != 31 { + t.Errorf("Used() used got %d want 31", m.Used()) + } + + key = big{} + elem = big{} + elem[0] = 128 + + for i := 0; i < 31; i++ { + key[0] += 1 + elem[0] += 1 + got, ok := m.Get(typ, unsafe.Pointer(&key)) + if !ok { + t.Errorf("Get(%v) got ok false want true", key) + } + gotElem := *(*big)(got) + if gotElem != elem { + t.Errorf("Get(%v) got elem %v want %v", key, gotElem, elem) + } + } +} + +// Delete should clear element. See https://go.dev/issue/25936. +func TestMapDeleteClear(t *testing.T) { + m, typ := maps.NewTestMap[int64, int64](8) + + key := int64(0) + elem := int64(128) + + m.Put(typ, unsafe.Pointer(&key), unsafe.Pointer(&elem)) + + if maps.DebugLog { + fmt.Printf("After put %d: %v\n", key, m) + } + + got, ok := m.Get(typ, unsafe.Pointer(&key)) + if !ok { + t.Errorf("Get(%d) got ok false want true", key) + } + gotElem := *(*int64)(got) + if gotElem != elem { + t.Errorf("Get(%d) got elem %d want %d", key, gotElem, elem) + } + + m.Delete(typ, unsafe.Pointer(&key)) + + gotElem = *(*int64)(got) + if gotElem != 0 { + t.Errorf("Delete(%d) failed to clear element. got %d want 0", key, gotElem) + } +} |
