import (
"fmt"
+ "internal/abi"
"math"
"runtime"
"strconv"
})
}
+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()
"internal/abi"
"reflect"
"runtime"
+ "strconv"
"strings"
"testing"
"time"
}
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")
+ }
+ }
+}