Source file src/internal/sync/export_test.go

     1  // Copyright 2024 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package sync
     6  
     7  import (
     8  	"internal/abi"
     9  	"unsafe"
    10  )
    11  
    12  // NewBadHashTrieMap creates a new HashTrieMap for the provided key and value
    13  // but with an intentionally bad hash function.
    14  func NewBadHashTrieMap[K, V comparable]() *HashTrieMap[K, V] {
    15  	// Stub out the good hash function with a terrible one.
    16  	// Everything should still work as expected.
    17  	var m HashTrieMap[K, V]
    18  	m.init()
    19  	m.keyHash = func(_ unsafe.Pointer, _ uintptr) uintptr {
    20  		return 0
    21  	}
    22  	return &m
    23  }
    24  
    25  // NewTruncHashTrieMap creates a new HashTrieMap for the provided key and value
    26  // but with an intentionally bad hash function.
    27  func NewTruncHashTrieMap[K, V comparable]() *HashTrieMap[K, V] {
    28  	// Stub out the good hash function with a terrible one.
    29  	// Everything should still work as expected.
    30  	var m HashTrieMap[K, V]
    31  	var mx map[string]int
    32  	mapType := abi.TypeOf(mx).MapType()
    33  	hasher := mapType.Hasher
    34  	m.keyHash = func(p unsafe.Pointer, n uintptr) uintptr {
    35  		return hasher(p, n) & ((uintptr(1) << 4) - 1)
    36  	}
    37  	return &m
    38  }
    39  

View as plain text