diff --git a/fifo/fifo_test.go b/fifo/fifo_test.go index 05b3bd7..5a80fb4 100644 --- a/fifo/fifo_test.go +++ b/fifo/fifo_test.go @@ -1,10 +1,42 @@ package fifo_test import ( + "cache/fifo" "github.com/matryer/is" "testing" ) +// TestFifo +// +// @Description: 单元测试 +// @param t func TestFifo(t *testing.T) { - is.New() + is := is.New(t) + cache := fifo.New(24, nil) + cache.DelOldest() + cache.Set("k1", 1) + v := cache.Get("k1") + is.Equal(v, 1) + cache.Del("k1") + is.Equal(0, cache.Len()) +} + +// TestFifoWithCallBack +// +// @Description: 带删除回调 +// @param t +func TestFifoWithCallBack(t *testing.T) { + is := is.New(t) + keys := make([]string, 8) + onEvicted := func(key string, value any) { + keys = append(keys, key) + } + cache := fifo.New(24, onEvicted) + cache.Set("k1", int8(1)) + cache.Set("k2", int8(2)) + cache.Set("k3", int8(3)) + cache.Set("k4", int8(4)) + expected := []string{"k1", "k2"} + is.Equal(expected, keys) + is.Equal(2, cache.Len()) }