summaryrefslogtreecommitdiffstats
path: root/src/cache_test.go
blob: 8703fc410b9c4570d8722834b66436b142157aa1 (plain)
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
package fzf

import "testing"

func TestChunkCache(t *testing.T) {
	cache := NewChunkCache()
	chunk2 := make(Chunk, chunkSize)
	chunk1p := &Chunk{}
	chunk2p := &chunk2
	items1 := []*Result{&Result{}}
	items2 := []*Result{&Result{}, &Result{}}
	cache.Add(chunk1p, "foo", items1)
	cache.Add(chunk2p, "foo", items1)
	cache.Add(chunk2p, "bar", items2)

	{ // chunk1 is not full
		cached, found := cache.Find(chunk1p, "foo")
		if found {
			t.Error("Cached disabled for non-empty chunks", found, cached)
		}
	}
	{
		cached, found := cache.Find(chunk2p, "foo")
		if !found || len(cached) != 1 {
			t.Error("Expected 1 item cached", found, cached)
		}
	}
	{
		cached, found := cache.Find(chunk2p, "bar")
		if !found || len(cached) != 2 {
			t.Error("Expected 2 items cached", found, cached)
		}
	}
	{
		cached, found := cache.Find(chunk1p, "foobar")
		if found {
			t.Error("Expected 0 item cached", found, cached)
		}
	}
}