| 1 |
package lfu_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/Code-Hex/go-generics-cache/policy/lfu" |
| 7 |
) |
| 8 |
|
| 9 |
type tmp struct { |
| 10 |
i int |
| 11 |
} |
| 12 |
|
| 13 |
func (t *tmp) GetReferenceCount() int { return t.i } |
| 14 |
|
| 15 |
func TestSet(t *testing.T) { |
| 16 |
|
| 17 |
cache := lfu.NewCache[string, int](lfu.WithCapacity(1)) |
| 18 |
cache.Set("foo", 1) |
| 19 |
if got := cache.Len(); got != 1 { |
| 20 |
t.Fatalf("invalid length: %d", got) |
| 21 |
} |
| 22 |
if got, ok := cache.Get("foo"); got != 1 || !ok { |
| 23 |
t.Fatalf("invalid value got %d, cachehit %v", got, ok) |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
cache.Set("bar", 2) |
| 28 |
if got := cache.Len(); got != 1 { |
| 29 |
t.Fatalf("invalid length: %d", got) |
| 30 |
} |
| 31 |
bar, ok := cache.Get("bar") |
| 32 |
if bar != 2 || !ok { |
| 33 |
t.Fatalf("invalid value bar %d, cachehit %v", bar, ok) |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
if _, ok := cache.Get("foo"); ok { |
| 38 |
t.Fatalf("invalid delete oldest value foo %v", ok) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
cache.Set("bar", 100) |
| 43 |
if got := cache.Len(); got != 1 { |
| 44 |
t.Fatalf("invalid length: %d", got) |
| 45 |
} |
| 46 |
bar, ok = cache.Get("bar") |
| 47 |
if bar != 100 || !ok { |
| 48 |
t.Fatalf("invalid replacing value bar %d, cachehit %v", bar, ok) |
| 49 |
} |
| 50 |
|
| 51 |
t.Run("with initilal reference count", func(t *testing.T) { |
| 52 |
cache := lfu.NewCache[string, *tmp](lfu.WithCapacity(2)) |
| 53 |
cache.Set("foo", &tmp{i: 10}) |
| 54 |
cache.Set("foo2", &tmp{i: 2}) |
| 55 |
if got := cache.Len(); got != 2 { |
| 56 |
t.Fatalf("invalid length: %d", got) |
| 57 |
} |
| 58 |
|
| 59 |
cache.Set("foo3", &tmp{i: 3}) |
| 60 |
|
| 61 |
|
| 62 |
if _, ok := cache.Get("foo2"); ok { |
| 63 |
t.Fatalf("invalid delete oldest value foo2 %v", ok) |
| 64 |
} |
| 65 |
if _, ok := cache.Get("foo"); !ok { |
| 66 |
t.Fatalf("invalid value foo is not found") |
| 67 |
} |
| 68 |
}) |
| 69 |
} |
| 70 |
|
| 71 |
func TestDelete(t *testing.T) { |
| 72 |
cache := lfu.NewCache[string, int](lfu.WithCapacity(2)) |
| 73 |
cache.Set("foo", 1) |
| 74 |
if got := cache.Len(); got != 1 { |
| 75 |
t.Fatalf("invalid length: %d", got) |
| 76 |
} |
| 77 |
|
| 78 |
cache.Delete("foo2") |
| 79 |
if got := cache.Len(); got != 1 { |
| 80 |
t.Fatalf("invalid length after deleted does not exist key: %d", got) |
| 81 |
} |
| 82 |
|
| 83 |
cache.Delete("foo") |
| 84 |
if got := cache.Len(); got != 0 { |
| 85 |
t.Fatalf("invalid length after deleted: %d", got) |
| 86 |
} |
| 87 |
if _, ok := cache.Get("foo"); ok { |
| 88 |
t.Fatalf("invalid get after deleted %v", ok) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
func TestIssue33(t *testing.T) { |
| 94 |
cache := lfu.NewCache[string, int](lfu.WithCapacity(2)) |
| 95 |
cache.Set("foo", 1) |
| 96 |
cache.Set("foo2", 2) |
| 97 |
cache.Set("foo3", 3) |
| 98 |
|
| 99 |
cache.Delete("foo") |
| 100 |
cache.Delete("foo2") |
| 101 |
cache.Delete("foo3") |
| 102 |
} |
| 103 |
|
| 104 |
func TestZeroCap(t *testing.T) { |
| 105 |
cache := lfu.NewCache[string, int](lfu.WithCapacity(0)) |
| 106 |
cache.Set("foo", 1) |
| 107 |
|
| 108 |
v, ok := cache.Get("foo") |
| 109 |
if !ok { |
| 110 |
t.Error(ok) |
| 111 |
} |
| 112 |
if v != 1 { |
| 113 |
t.Error(v) |
| 114 |
} |
| 115 |
} |