~eliasnaur/gio

ref: d2e06d938930 gio/text/lru_test.go -rw-r--r-- 979 bytes
d2e06d93Elias Naur widget: update Editor dimensions after input 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: Unlicense OR MIT

package text

import (
	"strconv"
	"testing"

	"gioui.org/op"
)

func TestLayoutLRU(t *testing.T) {
	c := new(layoutCache)
	put := func(i int) {
		c.Put(layoutKey{str: strconv.Itoa(i)}, nil)
	}
	get := func(i int) bool {
		_, ok := c.Get(layoutKey{str: strconv.Itoa(i)})
		return ok
	}
	testLRU(t, put, get)
}

func TestPathLRU(t *testing.T) {
	c := new(pathCache)
	put := func(i int) {
		c.Put(pathKey{str: strconv.Itoa(i)}, op.CallOp{})
	}
	get := func(i int) bool {
		_, ok := c.Get(pathKey{str: strconv.Itoa(i)})
		return ok
	}
	testLRU(t, put, get)
}

func testLRU(t *testing.T, put func(i int), get func(i int) bool) {
	for i := 0; i < maxSize; i++ {
		put(i)
	}
	for i := 0; i < maxSize; i++ {
		if !get(i) {
			t.Fatalf("key %d was evicted", i)
		}
	}
	put(maxSize)
	for i := 1; i < maxSize+1; i++ {
		if !get(i) {
			t.Fatalf("key %d was evicted", i)
		}
	}
	if i := 0; get(i) {
		t.Fatalf("key %d was not evicted", i)
	}
}