lru_test.go raw
1 // SPDX-License-Identifier: Unlicense OR MIT
2
3 package text
4
5 import (
6 "strconv"
7 "testing"
8
9 "github.com/p9c/p9/pkg/gel/gio/op"
10 )
11
12 func TestLayoutLRU(t *testing.T) {
13 c := new(layoutCache)
14 put := func(i int) {
15 c.Put(layoutKey{str: strconv.Itoa(i)}, nil)
16 }
17 get := func(i int) bool {
18 _, ok := c.Get(layoutKey{str: strconv.Itoa(i)})
19 return ok
20 }
21 testLRU(t, put, get)
22 }
23
24 func TestPathLRU(t *testing.T) {
25 c := new(pathCache)
26 put := func(i int) {
27 c.Put(pathKey{str: strconv.Itoa(i)}, op.CallOp{})
28 }
29 get := func(i int) bool {
30 _, ok := c.Get(pathKey{str: strconv.Itoa(i)})
31 return ok
32 }
33 testLRU(t, put, get)
34 }
35
36 func testLRU(t *testing.T, put func(i int), get func(i int) bool) {
37 for i := 0; i < maxSize; i++ {
38 put(i)
39 }
40 for i := 0; i < maxSize; i++ {
41 if !get(i) {
42 t.Fatalf("key %d was evicted", i)
43 }
44 }
45 put(maxSize)
46 for i := 1; i < maxSize+1; i++ {
47 if !get(i) {
48 t.Fatalf("key %d was evicted", i)
49 }
50 }
51 if i := 0; get(i) {
52 t.Fatalf("key %d was not evicted", i)
53 }
54 }
55