github.com/MetalBlockchain/metalgo@v1.11.9/cache/lru_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 TestLRU(t *testing.T) { 15 cache := &LRU[ids.ID, int64]{Size: 1} 16 17 TestBasic(t, cache) 18 } 19 20 func TestLRUEviction(t *testing.T) { 21 cache := &LRU[ids.ID, int64]{Size: 2} 22 23 TestEviction(t, cache) 24 } 25 26 func TestLRUResize(t *testing.T) { 27 require := require.New(t) 28 cache := LRU[ids.ID, int64]{Size: 2} 29 30 id1 := ids.ID{1} 31 id2 := ids.ID{2} 32 33 expectedVal1 := int64(1) 34 expectedVal2 := int64(2) 35 cache.Put(id1, expectedVal1) 36 cache.Put(id2, expectedVal2) 37 38 val, found := cache.Get(id1) 39 require.True(found) 40 require.Equal(expectedVal1, val) 41 42 val, found = cache.Get(id2) 43 require.True(found) 44 require.Equal(expectedVal2, val) 45 46 cache.Size = 1 47 // id1 evicted 48 49 _, found = cache.Get(id1) 50 require.False(found) 51 52 val, found = cache.Get(id2) 53 require.True(found) 54 require.Equal(expectedVal2, val) 55 56 cache.Size = 0 57 // We reset the size to 1 in resize 58 59 _, found = cache.Get(id1) 60 require.False(found) 61 62 val, found = cache.Get(id2) 63 require.True(found) 64 require.Equal(expectedVal2, val) 65 }