Files
cache/lru_sized_cache_test.go
zeekayandHanzo Dev d90e7473ee Take in the sized LRU that only luxfi/node had
luxfi/node required this module and also carried its own cache package, and
the one file the two did not share was lru_sized_cache.go. It is here now,
with the cachetest helper its test uses.

It reached for utils.Zero, and luxfi/utils reaches constants, which reached
geth, which reaches crypto — a chain no cache should pull in to name the zero
value of a type parameter. The helper is four lines here instead.

lru's TestCacheWithEvictionCallback fails before and after this commit; that
directory is untouched.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-06 00:09:15 -07:00

55 lines
1.0 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package cache_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/luxfi/ids"
"github.com/luxfi/cache"
"github.com/luxfi/cache/cachetest"
)
func TestSizedLRU(t *testing.T) {
c := cache.NewSizedLRU[ids.ID, int64](cachetest.IntSize, cachetest.IntSizeFunc)
cachetest.Basic(t, c)
}
func TestSizedLRUEviction(t *testing.T) {
c := cache.NewSizedLRU[ids.ID, int64](2*cachetest.IntSize, cachetest.IntSizeFunc)
cachetest.Eviction(t, c)
}
func TestSizedLRUWrongKeyEvictionRegression(t *testing.T) {
require := require.New(t)
c := cache.NewSizedLRU[string, struct{}](
3,
func(key string, _ struct{}) int {
return len(key)
},
)
c.Put("a", struct{}{})
c.Put("b", struct{}{})
c.Put("c", struct{}{})
c.Put("dd", struct{}{})
_, ok := c.Get("a")
require.False(ok)
_, ok = c.Get("b")
require.False(ok)
_, ok = c.Get("c")
require.True(ok)
_, ok = c.Get("dd")
require.True(ok)
}