github.com/MetalBlockchain/metalgo@v1.11.9/cache/lru_sized_cache_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package cache
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/MetalBlockchain/metalgo/ids"
    12  )
    13  
    14  func TestSizedLRU(t *testing.T) {
    15  	cache := NewSizedLRU[ids.ID, int64](TestIntSize, TestIntSizeFunc)
    16  
    17  	TestBasic(t, cache)
    18  }
    19  
    20  func TestSizedLRUEviction(t *testing.T) {
    21  	cache := NewSizedLRU[ids.ID, int64](2*TestIntSize, TestIntSizeFunc)
    22  
    23  	TestEviction(t, cache)
    24  }
    25  
    26  func TestSizedLRUWrongKeyEvictionRegression(t *testing.T) {
    27  	require := require.New(t)
    28  
    29  	cache := NewSizedLRU[string, struct{}](
    30  		3,
    31  		func(key string, _ struct{}) int {
    32  			return len(key)
    33  		},
    34  	)
    35  
    36  	cache.Put("a", struct{}{})
    37  	cache.Put("b", struct{}{})
    38  	cache.Put("c", struct{}{})
    39  	cache.Put("dd", struct{}{})
    40  
    41  	_, ok := cache.Get("a")
    42  	require.False(ok)
    43  
    44  	_, ok = cache.Get("b")
    45  	require.False(ok)
    46  
    47  	_, ok = cache.Get("c")
    48  	require.True(ok)
    49  
    50  	_, ok = cache.Get("dd")
    51  	require.True(ok)
    52  }