Skip to content

Commit af02abc

Browse files
committed
✅ test: add unit test #5
1 parent b8fb841 commit af02abc

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module github.com/pnguyen215/cachify
22

33
go 1.23.1
4+
5+
require github.com/stretchr/testify v1.10.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
6+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

test/lru_test.go

+160
Original file line numberDiff line numberDiff line change
@@ -1 +1,161 @@
11
package test
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/pnguyen215/cachify"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// Test cache creation and basic functionality
12+
func TestLRU_SetAndGet(t *testing.T) {
13+
cache := cachify.NewLRU(2)
14+
15+
cache.Set("a", "alpha")
16+
cache.Set("b", "beta")
17+
18+
// Verify the values
19+
val, ok := cache.Get("a")
20+
assert.True(t, ok)
21+
assert.Equal(t, "alpha", val)
22+
23+
val, ok = cache.Get("b")
24+
assert.True(t, ok)
25+
assert.Equal(t, "beta", val)
26+
27+
// Add a new entry to exceed capacity and evict the least recently used
28+
cache.Set("c", "gamma")
29+
_, ok = cache.Get("a")
30+
assert.False(t, ok) // "a" should be evicted
31+
}
32+
33+
// Test eviction callback
34+
func TestLRU_Callback(t *testing.T) {
35+
evicted := make(map[string]interface{})
36+
callback := func(key string, value interface{}) {
37+
evicted[key] = value
38+
}
39+
40+
cache := cachify.NewLRUCallback(2, callback)
41+
42+
cache.Set("x", "X-ray")
43+
cache.Set("y", "Yankee")
44+
cache.Set("z", "Zulu")
45+
46+
// Verify eviction
47+
assert.Len(t, evicted, 1)
48+
assert.Equal(t, "X-ray", evicted["x"])
49+
}
50+
51+
// Test expiration functionality
52+
func TestLRU_Expiration(t *testing.T) {
53+
cache := cachify.NewLRUExpires(2, 12*time.Second)
54+
55+
cache.Set("key", "value")
56+
time.Sleep(1 * time.Second)
57+
58+
// Before expiration
59+
val, ok := cache.Get("key")
60+
assert.True(t, ok)
61+
assert.Equal(t, "value", val)
62+
63+
// After expiration
64+
time.Sleep(1 * time.Second)
65+
_, ok = cache.Get("key")
66+
assert.True(t, ok)
67+
}
68+
69+
// Test dynamic capacity adjustment
70+
func TestLRU_SetCapacity(t *testing.T) {
71+
cache := cachify.NewLRU(2)
72+
73+
cache.Set("a", "alpha")
74+
cache.Set("b", "beta")
75+
76+
// Increase capacity
77+
cache.SetCapacity(3)
78+
cache.Set("c", "gamma")
79+
80+
assert.Equal(t, 3, cache.Len())
81+
assert.Contains(t, cache.GetAll(), "c")
82+
83+
// Decrease capacity
84+
cache.SetCapacity(2)
85+
_, ok := cache.Get("a") // "a" should be evicted
86+
assert.False(t, ok)
87+
}
88+
89+
// Test IsMostRecentlyUsed and GetMostRecentlyUsed
90+
func TestLRU_MostRecentlyUsed(t *testing.T) {
91+
cache := cachify.NewLRU(3)
92+
93+
cache.Set("a", "alpha")
94+
cache.Set("b", "beta")
95+
cache.Set("c", "gamma")
96+
97+
assert.True(t, cache.IsMostRecentlyUsed("c"))
98+
99+
state, ok := cache.GetMostRecentlyUsed()
100+
assert.True(t, ok)
101+
assert.Equal(t, "c", state.Key())
102+
assert.Equal(t, "gamma", state.Value())
103+
}
104+
105+
// Test Clear and IsEmpty
106+
func TestLRU_Clear(t *testing.T) {
107+
cache := cachify.NewLRU(2)
108+
109+
cache.Set("a", "alpha")
110+
cache.Set("b", "beta")
111+
112+
cache.Clear()
113+
assert.Equal(t, 0, cache.Len())
114+
assert.True(t, cache.IsEmpty())
115+
}
116+
117+
// Test ExpandExpiry
118+
func TestLRU_ExpandExpiry(t *testing.T) {
119+
cache := cachify.NewLRUExpires(2, 12*time.Second)
120+
121+
cache.Set("key", "value")
122+
cache.ExpandExpiry("key", 3*time.Second)
123+
124+
time.Sleep(1 * time.Second)
125+
val, ok := cache.Get("key")
126+
assert.True(t, ok)
127+
assert.Equal(t, "value", val)
128+
}
129+
130+
// Test PersistExpiry
131+
func TestLRU_PersistExpiry(t *testing.T) {
132+
cache := cachify.NewLRUExpires(2, 5*time.Second)
133+
134+
cache.Set("key", "value")
135+
remain, ok := cache.PersistExpiry("key")
136+
assert.True(t, ok)
137+
assert.LessOrEqual(t, remain, 5*time.Second)
138+
}
139+
140+
// Test Update
141+
func TestLRU_Update(t *testing.T) {
142+
cache := cachify.NewLRU(2)
143+
144+
cache.Set("key", "old_value")
145+
cache.Update("key", "new_value")
146+
147+
val, ok := cache.Get("key")
148+
assert.True(t, ok)
149+
assert.Equal(t, "new_value", val)
150+
}
151+
152+
// Test Remove
153+
func TestLRU_Remove(t *testing.T) {
154+
cache := cachify.NewLRU(2)
155+
156+
cache.Set("a", "alpha")
157+
cache.Remove("a")
158+
159+
_, ok := cache.Get("a")
160+
assert.False(t, ok)
161+
}

0 commit comments

Comments
 (0)