File: lru_test.go

package info (click to toggle)
golang-github-aws-smithy-go 1.19.0-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 2,680 kB
  • sloc: java: 15,917; xml: 166; sh: 131; makefile: 66
file content (69 lines) | stat: -rw-r--r-- 1,626 bytes parent folder | download | duplicates (2)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package lru

import "testing"

func TestCache(t *testing.T) {
	cache := New(4).(*lru)

	// fill cache
	cache.Put(1, 2)
	cache.Put(2, 3)
	cache.Put(3, 4)
	cache.Put(4, 5)
	assertEntry(t, cache, 1, 2)
	assertEntry(t, cache, 2, 3)
	assertEntry(t, cache, 3, 4)
	assertEntry(t, cache, 4, 5)

	// touch the last entry
	cache.Get(1)
	cache.Put(5, 6)
	assertNoEntry(t, cache, 2)
	assertEntry(t, cache, 3, 4)
	assertEntry(t, cache, 4, 5)
	assertEntry(t, cache, 1, 2)
	assertEntry(t, cache, 5, 6)

	// put something new, 3 should now be the oldest
	cache.Put(6, 7)
	assertNoEntry(t, cache, 3)
	assertEntry(t, cache, 4, 5)
	assertEntry(t, cache, 1, 2)
	assertEntry(t, cache, 5, 6)
	assertEntry(t, cache, 6, 7)

	// touch something in the middle
	cache.Get(5)
	assertEntry(t, cache, 4, 5)
	assertEntry(t, cache, 1, 2)
	assertEntry(t, cache, 5, 6)
	assertEntry(t, cache, 6, 7)

	// put 3 new things, 5 should remain after the touch
	cache.Put(7, 8)
	cache.Put(8, 9)
	cache.Put(9, 0)
	assertNoEntry(t, cache, 4)
	assertNoEntry(t, cache, 1)
	assertNoEntry(t, cache, 6)
	assertEntry(t, cache, 5, 6)
	assertEntry(t, cache, 7, 8)
	assertEntry(t, cache, 8, 9)
	assertEntry(t, cache, 9, 0)
}

func assertEntry(t *testing.T, c *lru, k interface{}, v interface{}) {
	e, ok := c.entries[k]
	if !ok {
		t.Errorf("expected entry %v=%v, but no entry found", k, v)
	}
	if actual := e.Value.(*element).value; actual != v {
		t.Errorf("expected entry %v=%v, but got entry value %v", k, v, actual)
	}
}

func assertNoEntry(t *testing.T, c *lru, k interface{}) {
	if _, ok := c.Get(k); ok {
		t.Errorf("expected no entry for %v, but one was found", k)
	}
}