From: Michael Anthony Knyszek Date: Thu, 19 Sep 2024 15:43:14 +0000 (+0000) Subject: unique,internal/concurrent: add some more tests X-Git-Tag: go1.24rc1~821 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1596a6c8ec6d4ccb4a219b0c467bed192720de12;p=gostls13.git unique,internal/concurrent: add some more tests One is a test of unsafe.String usage, which was broken before CL 610738 was merged. The other is trying to improve coverage of "near collision" scenarios in the HashTrieMap where only the last few bits differ. This is intended to catch off-by-one errors in iterating down the tree. For #69534. Change-Id: I3f302e148e81269a50e93b5edf83cafc2d291098 Reviewed-on: https://go-review.googlesource.com/c/go/+/614475 Auto-Submit: Michael Knyszek LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor Reviewed-by: Cuong Manh Le --- diff --git a/src/internal/concurrent/hashtriemap_test.go b/src/internal/concurrent/hashtriemap_test.go index e233824c0f..498ead8c1d 100644 --- a/src/internal/concurrent/hashtriemap_test.go +++ b/src/internal/concurrent/hashtriemap_test.go @@ -6,6 +6,7 @@ package concurrent import ( "fmt" + "internal/abi" "math" "runtime" "strconv" @@ -33,6 +34,23 @@ func TestHashTrieMapBadHash(t *testing.T) { }) } +func TestHashTrieMapTruncHash(t *testing.T) { + testHashTrieMap(t, func() *HashTrieMap[string, int] { + // Stub out the good hash function with a different terrible one + // (truncated hash). Everything should still work as expected. + // This is useful to test independently to catch issues with + // near collisions, where only the last few bits of the hash differ. + m := NewHashTrieMap[string, int]() + var mx map[string]int + mapType := abi.TypeOf(mx).MapType() + hasher := mapType.Hasher + m.keyHash = func(p unsafe.Pointer, n uintptr) uintptr { + return hasher(p, n) & ((uintptr(1) << 4) - 1) + } + return m + }) +} + func testHashTrieMap(t *testing.T, newMap func() *HashTrieMap[string, int]) { t.Run("LoadEmpty", func(t *testing.T) { m := newMap() diff --git a/src/unique/handle_test.go b/src/unique/handle_test.go index 98a1b731cf..e271770651 100644 --- a/src/unique/handle_test.go +++ b/src/unique/handle_test.go @@ -9,6 +9,7 @@ import ( "internal/abi" "reflect" "runtime" + "strconv" "strings" "testing" "time" @@ -138,3 +139,26 @@ func TestMakeClonesStrings(t *testing.T) { } runtime.KeepAlive(h) } + +func TestHandleUnsafeString(t *testing.T) { + var testData []string + for i := range 1024 { + testData = append(testData, strconv.Itoa(i)) + } + var buf []byte + var handles []Handle[string] + for _, s := range testData { + if len(buf) < len(s) { + buf = make([]byte, len(s)*2) + } + copy(buf, s) + sbuf := unsafe.String(&buf[0], len(s)) + handles = append(handles, Make(sbuf)) + } + for i, s := range testData { + h := Make(s) + if handles[i].Value() != h.Value() { + t.Fatal("unsafe string improperly retained internally") + } + } +}