Debian Codegraph @

src/chunklist_test.go

raw
1 package fzf
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/junegunn/fzf/src/util"
8 )
9
10 func TestChunkList(t *testing.T) {
11 // FIXME global
12 sortCriteria = []criterion{byScore, byLength}
13
14 cl := NewChunkList(NewChunkCache(), func(item *Item, s []byte) bool {
15 item.text = util.ToChars(s)
16 return true
17 })
18
19 // Snapshot
20 snapshot, count, _ := cl.Snapshot(0)
21 if len(snapshot) > 0 || count > 0 {
22 t.Error("Snapshot should be empty now")
23 }
24
25 // Add some data
26 cl.Push([]byte("hello"))
27 cl.Push([]byte("world"))
28
29 // Previously created snapshot should remain the same
30 if len(snapshot) > 0 {
31 t.Error("Snapshot should not have changed")
32 }
33
34 // But the new snapshot should contain the added items
35 snapshot, count, _ = cl.Snapshot(0)
36 if len(snapshot) != 1 && count != 2 {
37 t.Error("Snapshot should not be empty now")
38 }
39
40 // Check the content of the ChunkList
41 chunk1 := snapshot[0]
42 if chunk1.count != 2 {
43 t.Error("Snapshot should contain only two items")
44 }
45 if chunk1.items[0].text.ToString() != "hello" ||
46 chunk1.items[1].text.ToString() != "world" {
47 t.Error("Invalid data")
48 }
49 if chunk1.IsFull() {
50 t.Error("Chunk should not have been marked full yet")
51 }
52
53 // Add more data
54 for i := range chunkSize * 2 {
55 cl.Push(fmt.Appendf(nil, "item %d", i))
56 }
57
58 // Previous snapshot should remain the same
59 if len(snapshot) != 1 {
60 t.Error("Snapshot should stay the same")
61 }
62
63 // New snapshot
64 snapshot, count, _ = cl.Snapshot(0)
65 if len(snapshot) != 3 || !snapshot[0].IsFull() ||
66 !snapshot[1].IsFull() || snapshot[2].IsFull() || count != chunkSize*2+2 {
67 t.Error("Expected two full chunks and one more chunk")
68 }
69 if snapshot[2].count != 2 {
70 t.Error("Unexpected number of items")
71 }
72
73 cl.Push([]byte("hello"))
74 cl.Push([]byte("world"))
75
76 lastChunkCount := snapshot[len(snapshot)-1].count
77 if lastChunkCount != 2 {
78 t.Error("Unexpected number of items:", lastChunkCount)
79 }
80 }
81
82 func TestChunkListTail(t *testing.T) {
83 cl := NewChunkList(NewChunkCache(), func(item *Item, s []byte) bool {
84 item.text = util.ToChars(s)
85 return true
86 })
87 total := chunkSize*2 + chunkSize/2
88 for i := range total {
89 cl.Push(fmt.Appendf(nil, "item %d", i))
90 }
91
92 snapshot, count, changed := cl.Snapshot(0)
93 assertCount := func(expected int, shouldChange bool) {
94 if count != expected || CountItems(snapshot) != expected {
95 t.Errorf("Unexpected count: %d (expected: %d)", count, expected)
96 }
97 if changed != shouldChange {
98 t.Error("Unexpected change status")
99 }
100 }
101 assertCount(total, false)
102
103 tail := chunkSize + chunkSize/2
104 snapshot, count, changed = cl.Snapshot(tail)
105 assertCount(tail, true)
106
107 snapshot, count, changed = cl.Snapshot(tail)
108 assertCount(tail, false)
109
110 snapshot, count, changed = cl.Snapshot(0)
111 assertCount(tail, false)
112
113 tail = chunkSize / 2
114 snapshot, count, changed = cl.Snapshot(tail)
115 assertCount(tail, true)
116 }