From d03f72f6c9d0e61198300189a6e8db59cd636cdd Mon Sep 17 00:00:00 2001 From: old-tom <892955278@qq.com> Date: Fri, 22 Dec 2023 15:46:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fifo/fifo_test.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) 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()) }