2015-01-01 19:49:30 +00:00
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2016-08-13 15:39:44 +00:00
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/util"
|
2015-01-01 19:49:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestChunkList(t *testing.T) {
|
2016-01-12 18:07:42 +00:00
|
|
|
// FIXME global
|
2016-09-07 00:58:18 +00:00
|
|
|
sortCriteria = []criterion{byScore, byLength}
|
2016-01-12 18:07:42 +00:00
|
|
|
|
2017-08-16 03:26:06 +00:00
|
|
|
cl := NewChunkList(func(item *Item, s []byte) bool {
|
2017-08-14 16:10:41 +00:00
|
|
|
item.text = util.ToChars(s)
|
|
|
|
return true
|
2015-01-01 19:49:30 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Snapshot
|
2015-01-03 20:01:13 +00:00
|
|
|
snapshot, count := cl.Snapshot()
|
|
|
|
if len(snapshot) > 0 || count > 0 {
|
2015-01-01 19:49:30 +00:00
|
|
|
t.Error("Snapshot should be empty now")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add some data
|
2015-08-02 05:25:57 +00:00
|
|
|
cl.Push([]byte("hello"))
|
|
|
|
cl.Push([]byte("world"))
|
2015-01-01 19:49:30 +00:00
|
|
|
|
|
|
|
// Previously created snapshot should remain the same
|
|
|
|
if len(snapshot) > 0 {
|
|
|
|
t.Error("Snapshot should not have changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// But the new snapshot should contain the added items
|
2015-01-03 20:01:13 +00:00
|
|
|
snapshot, count = cl.Snapshot()
|
|
|
|
if len(snapshot) != 1 && count != 2 {
|
2015-01-01 19:49:30 +00:00
|
|
|
t.Error("Snapshot should not be empty now")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the content of the ChunkList
|
|
|
|
chunk1 := snapshot[0]
|
2017-08-14 16:10:41 +00:00
|
|
|
if chunk1.count != 2 {
|
2015-01-01 19:49:30 +00:00
|
|
|
t.Error("Snapshot should contain only two items")
|
|
|
|
}
|
2017-08-14 16:10:41 +00:00
|
|
|
if chunk1.items[0].text.ToString() != "hello" ||
|
2017-08-16 03:26:06 +00:00
|
|
|
chunk1.items[1].text.ToString() != "world" {
|
2015-01-01 19:49:30 +00:00
|
|
|
t.Error("Invalid data")
|
|
|
|
}
|
|
|
|
if chunk1.IsFull() {
|
|
|
|
t.Error("Chunk should not have been marked full yet")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add more data
|
2015-04-17 13:23:52 +00:00
|
|
|
for i := 0; i < chunkSize*2; i++ {
|
2015-08-02 05:25:57 +00:00
|
|
|
cl.Push([]byte(fmt.Sprintf("item %d", i)))
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Previous snapshot should remain the same
|
|
|
|
if len(snapshot) != 1 {
|
|
|
|
t.Error("Snapshot should stay the same")
|
|
|
|
}
|
|
|
|
|
|
|
|
// New snapshot
|
2015-01-03 20:01:13 +00:00
|
|
|
snapshot, count = cl.Snapshot()
|
2015-01-01 19:49:30 +00:00
|
|
|
if len(snapshot) != 3 || !snapshot[0].IsFull() ||
|
2015-04-17 13:23:52 +00:00
|
|
|
!snapshot[1].IsFull() || snapshot[2].IsFull() || count != chunkSize*2+2 {
|
2015-01-01 19:49:30 +00:00
|
|
|
t.Error("Expected two full chunks and one more chunk")
|
|
|
|
}
|
2017-08-14 16:10:41 +00:00
|
|
|
if snapshot[2].count != 2 {
|
2015-01-01 19:49:30 +00:00
|
|
|
t.Error("Unexpected number of items")
|
|
|
|
}
|
2015-01-03 20:01:13 +00:00
|
|
|
|
2015-08-02 05:25:57 +00:00
|
|
|
cl.Push([]byte("hello"))
|
|
|
|
cl.Push([]byte("world"))
|
2015-01-03 20:01:13 +00:00
|
|
|
|
2017-08-14 16:10:41 +00:00
|
|
|
lastChunkCount := snapshot[len(snapshot)-1].count
|
2015-01-03 20:01:13 +00:00
|
|
|
if lastChunkCount != 2 {
|
|
|
|
t.Error("Unexpected number of items:", lastChunkCount)
|
|
|
|
}
|
2015-01-01 19:49:30 +00:00
|
|
|
}
|