From af02abc324aaf7388e6b86998db3f718659a6099 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Wed, 22 Jan 2025 16:25:49 +0700 Subject: [PATCH] :white_check_mark: test: add unit test #5 --- go.mod | 8 +++ go.sum | 10 +++ test/lru_test.go | 160 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) diff --git a/go.mod b/go.mod index 8031698..69a6b49 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,11 @@ module github.com/pnguyen215/cachify go 1.23.1 + +require github.com/stretchr/testify v1.10.0 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index e69de29..713a0b4 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/test/lru_test.go b/test/lru_test.go index 56e5404..91b3544 100644 --- a/test/lru_test.go +++ b/test/lru_test.go @@ -1 +1,161 @@ package test + +import ( + "testing" + "time" + + "github.com/pnguyen215/cachify" + "github.com/stretchr/testify/assert" +) + +// Test cache creation and basic functionality +func TestLRU_SetAndGet(t *testing.T) { + cache := cachify.NewLRU(2) + + cache.Set("a", "alpha") + cache.Set("b", "beta") + + // Verify the values + val, ok := cache.Get("a") + assert.True(t, ok) + assert.Equal(t, "alpha", val) + + val, ok = cache.Get("b") + assert.True(t, ok) + assert.Equal(t, "beta", val) + + // Add a new entry to exceed capacity and evict the least recently used + cache.Set("c", "gamma") + _, ok = cache.Get("a") + assert.False(t, ok) // "a" should be evicted +} + +// Test eviction callback +func TestLRU_Callback(t *testing.T) { + evicted := make(map[string]interface{}) + callback := func(key string, value interface{}) { + evicted[key] = value + } + + cache := cachify.NewLRUCallback(2, callback) + + cache.Set("x", "X-ray") + cache.Set("y", "Yankee") + cache.Set("z", "Zulu") + + // Verify eviction + assert.Len(t, evicted, 1) + assert.Equal(t, "X-ray", evicted["x"]) +} + +// Test expiration functionality +func TestLRU_Expiration(t *testing.T) { + cache := cachify.NewLRUExpires(2, 12*time.Second) + + cache.Set("key", "value") + time.Sleep(1 * time.Second) + + // Before expiration + val, ok := cache.Get("key") + assert.True(t, ok) + assert.Equal(t, "value", val) + + // After expiration + time.Sleep(1 * time.Second) + _, ok = cache.Get("key") + assert.True(t, ok) +} + +// Test dynamic capacity adjustment +func TestLRU_SetCapacity(t *testing.T) { + cache := cachify.NewLRU(2) + + cache.Set("a", "alpha") + cache.Set("b", "beta") + + // Increase capacity + cache.SetCapacity(3) + cache.Set("c", "gamma") + + assert.Equal(t, 3, cache.Len()) + assert.Contains(t, cache.GetAll(), "c") + + // Decrease capacity + cache.SetCapacity(2) + _, ok := cache.Get("a") // "a" should be evicted + assert.False(t, ok) +} + +// Test IsMostRecentlyUsed and GetMostRecentlyUsed +func TestLRU_MostRecentlyUsed(t *testing.T) { + cache := cachify.NewLRU(3) + + cache.Set("a", "alpha") + cache.Set("b", "beta") + cache.Set("c", "gamma") + + assert.True(t, cache.IsMostRecentlyUsed("c")) + + state, ok := cache.GetMostRecentlyUsed() + assert.True(t, ok) + assert.Equal(t, "c", state.Key()) + assert.Equal(t, "gamma", state.Value()) +} + +// Test Clear and IsEmpty +func TestLRU_Clear(t *testing.T) { + cache := cachify.NewLRU(2) + + cache.Set("a", "alpha") + cache.Set("b", "beta") + + cache.Clear() + assert.Equal(t, 0, cache.Len()) + assert.True(t, cache.IsEmpty()) +} + +// Test ExpandExpiry +func TestLRU_ExpandExpiry(t *testing.T) { + cache := cachify.NewLRUExpires(2, 12*time.Second) + + cache.Set("key", "value") + cache.ExpandExpiry("key", 3*time.Second) + + time.Sleep(1 * time.Second) + val, ok := cache.Get("key") + assert.True(t, ok) + assert.Equal(t, "value", val) +} + +// Test PersistExpiry +func TestLRU_PersistExpiry(t *testing.T) { + cache := cachify.NewLRUExpires(2, 5*time.Second) + + cache.Set("key", "value") + remain, ok := cache.PersistExpiry("key") + assert.True(t, ok) + assert.LessOrEqual(t, remain, 5*time.Second) +} + +// Test Update +func TestLRU_Update(t *testing.T) { + cache := cachify.NewLRU(2) + + cache.Set("key", "old_value") + cache.Update("key", "new_value") + + val, ok := cache.Get("key") + assert.True(t, ok) + assert.Equal(t, "new_value", val) +} + +// Test Remove +func TestLRU_Remove(t *testing.T) { + cache := cachify.NewLRU(2) + + cache.Set("a", "alpha") + cache.Remove("a") + + _, ok := cache.Get("a") + assert.False(t, ok) +}