| 1 |
package clock_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"strconv" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/Code-Hex/go-generics-cache/policy/clock" |
| 9 |
) |
| 10 |
|
| 11 |
type tmp struct { |
| 12 |
i int |
| 13 |
} |
| 14 |
|
| 15 |
func (t *tmp) GetReferenceCount() int { return t.i } |
| 16 |
|
| 17 |
func TestSet(t *testing.T) { |
| 18 |
|
| 19 |
cache := clock.NewCache[string, int](clock.WithCapacity(1)) |
| 20 |
cache.Set("foo", 1) |
| 21 |
if got := cache.Len(); got != 1 { |
| 22 |
t.Fatalf("invalid length: %d", got) |
| 23 |
} |
| 24 |
if got, ok := cache.Get("foo"); got != 1 || !ok { |
| 25 |
t.Fatalf("invalid value got %d, cachehit %v", got, ok) |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
cache.Set("bar", 2) |
| 30 |
if got := cache.Len(); got != 1 { |
| 31 |
t.Fatalf("invalid length: %d", got) |
| 32 |
} |
| 33 |
bar, ok := cache.Get("bar") |
| 34 |
if bar != 2 || !ok { |
| 35 |
t.Fatalf("invalid value bar %d, cachehit %v", bar, ok) |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
if _, ok := cache.Get("foo"); ok { |
| 40 |
t.Fatalf("invalid eviction the oldest value for foo %v", ok) |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
cache.Set("bar", 100) |
| 45 |
if got := cache.Len(); got != 1 { |
| 46 |
t.Fatalf("invalid length: %d", got) |
| 47 |
} |
| 48 |
bar, ok = cache.Get("bar") |
| 49 |
if bar != 100 || !ok { |
| 50 |
t.Fatalf("invalid replacing value bar %d, cachehit %v", bar, ok) |
| 51 |
} |
| 52 |
|
| 53 |
t.Run("with initilal reference count", func(t *testing.T) { |
| 54 |
cache := clock.NewCache[string, *tmp](clock.WithCapacity(2)) |
| 55 |
cache.Set("foo", &tmp{i: 10}) |
| 56 |
cache.Set("foo2", &tmp{i: 2}) |
| 57 |
if got := cache.Len(); got != 2 { |
| 58 |
t.Fatalf("invalid length: %d", got) |
| 59 |
} |
| 60 |
|
| 61 |
cache.Set("foo3", &tmp{i: 3}) |
| 62 |
|
| 63 |
|
| 64 |
if _, ok := cache.Get("foo2"); ok { |
| 65 |
t.Fatalf("invalid delete oldest value foo2 %v", ok) |
| 66 |
} |
| 67 |
if _, ok := cache.Get("foo"); !ok { |
| 68 |
t.Fatalf("invalid value foo is not found") |
| 69 |
} |
| 70 |
}) |
| 71 |
} |
| 72 |
|
| 73 |
func TestDelete(t *testing.T) { |
| 74 |
cache := clock.NewCache[string, int](clock.WithCapacity(1)) |
| 75 |
cache.Set("foo", 1) |
| 76 |
if got := cache.Len(); got != 1 { |
| 77 |
t.Fatalf("invalid length: %d", got) |
| 78 |
} |
| 79 |
|
| 80 |
cache.Delete("foo2") |
| 81 |
if got := cache.Len(); got != 1 { |
| 82 |
t.Fatalf("invalid length after deleted does not exist key: %d", got) |
| 83 |
} |
| 84 |
|
| 85 |
cache.Delete("foo") |
| 86 |
if got := cache.Len(); got != 0 { |
| 87 |
t.Fatalf("invalid length after deleted: %d", got) |
| 88 |
} |
| 89 |
if _, ok := cache.Get("foo"); ok { |
| 90 |
t.Fatalf("invalid get after deleted %v", ok) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestKeys(t *testing.T) { |
| 95 |
t.Run("normal", func(t *testing.T) { |
| 96 |
cache := clock.NewCache[string, int]() |
| 97 |
if len(cache.Keys()) != 0 { |
| 98 |
t.Errorf("want number of keys 0, but got %d", cache.Len()) |
| 99 |
} |
| 100 |
|
| 101 |
cache.Set("foo", 1) |
| 102 |
cache.Set("bar", 2) |
| 103 |
cache.Set("baz", 3) |
| 104 |
cache.Set("bar", 4) |
| 105 |
cache.Set("foo", 5) |
| 106 |
|
| 107 |
got := strings.Join(cache.Keys(), ",") |
| 108 |
want := strings.Join([]string{ |
| 109 |
"foo", |
| 110 |
"bar", |
| 111 |
"baz", |
| 112 |
}, ",") |
| 113 |
if got != want { |
| 114 |
t.Errorf("want %q, but got %q", want, got) |
| 115 |
} |
| 116 |
if len(cache.Keys()) != cache.Len() { |
| 117 |
t.Errorf("want number of keys %d, but got %d", len(cache.Keys()), cache.Len()) |
| 118 |
} |
| 119 |
}) |
| 120 |
|
| 121 |
t.Run("with deletion", func(t *testing.T) { |
| 122 |
cache := clock.NewCache[string, int](clock.WithCapacity(4)) |
| 123 |
cache.Set("foo", 1) |
| 124 |
cache.Set("bar", 2) |
| 125 |
cache.Set("baz", 3) |
| 126 |
|
| 127 |
cache.Delete("bar") |
| 128 |
|
| 129 |
got := strings.Join(cache.Keys(), ",") |
| 130 |
want := strings.Join([]string{ |
| 131 |
"foo", |
| 132 |
"baz", |
| 133 |
}, ",") |
| 134 |
if got != want { |
| 135 |
t.Errorf("want %q, but got %q", want, got) |
| 136 |
} |
| 137 |
if len(cache.Keys()) != cache.Len() { |
| 138 |
t.Errorf("want number of keys %d, but got %d", len(cache.Keys()), cache.Len()) |
| 139 |
} |
| 140 |
|
| 141 |
cache.Set("hoge", 4) |
| 142 |
cache.Set("fuga", 5) |
| 143 |
|
| 144 |
got2 := strings.Join(cache.Keys(), ",") |
| 145 |
want2 := strings.Join([]string{ |
| 146 |
"foo", |
| 147 |
"fuga", |
| 148 |
"baz", |
| 149 |
"hoge", |
| 150 |
}, ",") |
| 151 |
if got2 != want2 { |
| 152 |
t.Errorf("want2 %q, but got2 %q", want2, got2) |
| 153 |
} |
| 154 |
if len(cache.Keys()) != cache.Len() { |
| 155 |
t.Errorf("want2 number of keys %d, but got2 %d", len(cache.Keys()), cache.Len()) |
| 156 |
} |
| 157 |
}) |
| 158 |
} |
| 159 |
|
| 160 |
func TestIssue29(t *testing.T) { |
| 161 |
cap := 3 |
| 162 |
cache := clock.NewCache[string, int](clock.WithCapacity(cap)) |
| 163 |
for i := 0; i < cap; i++ { |
| 164 |
cache.Set(strconv.Itoa(i), i) |
| 165 |
} |
| 166 |
cache.Set(strconv.Itoa(cap), cap) |
| 167 |
|
| 168 |
keys := cache.Keys() |
| 169 |
if got := len(keys); cap != got { |
| 170 |
t.Errorf("want number of keys %d, but got %d", cap, got) |
| 171 |
} |
| 172 |
wantKeys := "3,1,2" |
| 173 |
gotKeys := strings.Join(keys, ",") |
| 174 |
if wantKeys != gotKeys { |
| 175 |
t.Errorf("want keys %q, but got keys %q", wantKeys, gotKeys) |
| 176 |
} |
| 177 |
} |