...
1 package ldevents
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func TestLRUCache(t *testing.T) {
10 t.Run("add returns false for never-seen value", func(t *testing.T) {
11 cache := newLruCache(10)
12 assert.False(t, cache.add("a"))
13 })
14
15 t.Run("add returns true for already-seen value", func(t *testing.T) {
16 cache := newLruCache(10)
17 cache.add("a")
18 assert.True(t, cache.add("a"))
19 })
20
21 t.Run("oldest value is discarded when capacity is exceeded", func(t *testing.T) {
22 cache := newLruCache(2)
23 cache.add("a")
24 cache.add("b")
25 cache.add("c")
26 assert.True(t, cache.add("c"))
27 assert.True(t, cache.add("b"))
28 assert.False(t, cache.add("a"))
29 })
30
31 t.Run("re-adding an existing value makes it new again", func(t *testing.T) {
32 cache := newLruCache(2)
33 cache.add("a")
34 cache.add("b")
35 cache.add("a")
36 cache.add("c")
37 assert.True(t, cache.add("c"))
38 assert.True(t, cache.add("a"))
39 assert.False(t, cache.add("b"))
40 })
41
42 t.Run("zero-length cache treats values as new", func(t *testing.T) {
43 cache := newLruCache(0)
44 assert.False(t, cache.add("a"))
45 assert.False(t, cache.add("a"))
46 })
47 }
48
View as plain text