github.com/phuslu/lru@v1.0.16-0.20240421170520-46288a2fd47c/ttl_shard_test.go (about)

     1  package lru
     2  
     3  import (
     4  	"testing"
     5  	"unsafe"
     6  )
     7  
     8  func TestTTLShardPadding(t *testing.T) {
     9  	var s ttlshard[string, int]
    10  
    11  	if n := unsafe.Sizeof(s); n != 128 {
    12  		t.Errorf("shard size is %d, not 128", n)
    13  	}
    14  }
    15  
    16  func TestTTLShardListSet(t *testing.T) {
    17  	var s ttlshard[string, uint32]
    18  	s.Init(1024, getRuntimeHasher[string](), 0)
    19  
    20  	key := "foobar"
    21  	hash := uint32(s.table_hasher(noescape(unsafe.Pointer(&key)), s.table_seed))
    22  
    23  	s.Set(hash, key, 42, 0)
    24  
    25  	if index := s.list_Back(); s.list[index].key == key {
    26  		t.Errorf("foobar should be list back: %v %v", index, s.list[index].key)
    27  	}
    28  }
    29  
    30  func TestTTLShardTableSet(t *testing.T) {
    31  	var s ttlshard[string, uint32]
    32  	s.Init(1024, getRuntimeHasher[string](), 0)
    33  
    34  	key := "foobar"
    35  	hash := uint32(s.table_hasher(noescape(unsafe.Pointer(&key)), s.table_seed))
    36  
    37  	s.Set(hash, key, 42, 0)
    38  
    39  	i, ok := s.table_Set(hash, key, 123)
    40  	if v := s.list[i].value; !ok || v != 42 {
    41  		t.Errorf("foobar should be set to 42: %v %v", i, ok)
    42  	}
    43  }