gioui.org@v0.6.1-0.20240506124620-7a9ce51988ce/text/lru_test.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package text
     4  
     5  import (
     6  	"strconv"
     7  	"testing"
     8  
     9  	"gioui.org/op/clip"
    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)}, document{})
    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  	shaped := []Glyph{{ID: 1}}
    27  	put := func(i int) {
    28  		c.Put(uint64(i), shaped, clip.PathSpec{})
    29  	}
    30  	get := func(i int) bool {
    31  		_, ok := c.Get(uint64(i), shaped)
    32  		return ok
    33  	}
    34  	testLRU(t, put, get)
    35  }
    36  
    37  func testLRU(t *testing.T, put func(i int), get func(i int) bool) {
    38  	for i := 0; i < maxSize; i++ {
    39  		put(i)
    40  	}
    41  	for i := 0; i < maxSize; i++ {
    42  		if !get(i) {
    43  			t.Fatalf("key %d was evicted", i)
    44  		}
    45  	}
    46  	put(maxSize)
    47  	for i := 1; i < maxSize+1; i++ {
    48  		if !get(i) {
    49  			t.Fatalf("key %d was evicted", i)
    50  		}
    51  	}
    52  	if i := 0; get(i) {
    53  		t.Fatalf("key %d was not evicted", i)
    54  	}
    55  }