-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice_test.go
114 lines (90 loc) · 2.63 KB
/
slice_test.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package kash
import (
"testing"
"reflect"
"fmt"
)
const TestCacheSize int = 10
type TestObject struct{}
func TestNewSliceCache(t *testing.T) {
sliceCache := NewSliceCache()
sliceCache.SetLoader(buildSliceLoader(make([]interface{}, TestCacheSize)))
actualLen := len(sliceCache.Get())
if len(sliceCache.Get()) != TestCacheSize {
t.Error("invalid default cache size", actualLen)
}
}
func TestNilSliceCacheLoader(t *testing.T) {
sliceCache := NewSliceCache()
sliceCache.SetLoader(buildSliceLoader(nil))
if sliceCache.Get() != nil {
t.Error("cache not nil")
}
}
func TestSliceCacheInt(t *testing.T) {
testData := []interface{}{newElement(10), newElement(100)}
sliceCache := NewSliceCache()
sliceCache.SetLoader(buildSliceLoader(testData))
if ! reflect.DeepEqual(sliceCache.Get(), testData) {
t.Error("invalid cache data", sliceCache.Get())
}
}
func TestSliceCacheString(t *testing.T) {
testData := []interface{}{newElement("alpha"), newElement("bravo")}
sliceCache := NewSliceCache()
sliceCache.SetLoader(buildSliceLoader(testData))
if ! reflect.DeepEqual(sliceCache.Get(), testData) {
t.Error("invalid cache data", sliceCache.Get())
}
}
func TestSliceCacheGenericInterface(t *testing.T) {
testData := []interface{}{newElement(&TestObject{}), newElement(&TestObject{})}
sliceCache := NewSliceCache()
sliceCache.SetLoader(buildSliceLoader(testData))
if ! reflect.DeepEqual(sliceCache.Get(), testData) {
t.Error("invalid cache data", sliceCache.Get())
}
}
func TestSliceCacheLoader(t *testing.T) {
testData := [][]interface{}{
[]interface{}{newElement("alpha"), newElement("bravo")},
[]interface{}{newElement("charile"), newElement("delta")},
[]interface{}{newElement("echo"), newElement("foxtrot")},
}
sliceCache := NewSliceCache()
sliceCache.SetLoader(buildSliceLoaderIter(testData))
if ! reflect.DeepEqual(sliceCache.Get(), testData[0]) {
t.Error("invalid cache data", sliceCache)
}
sliceCache.sync()
if ! reflect.DeepEqual(sliceCache.Get(), testData[1]) {
t.Error("invalid cache data", sliceCache)
}
sliceCache.sync()
if ! reflect.DeepEqual(sliceCache.Get(), testData[2]) {
t.Error("invalid cache data", sliceCache)
}
}
func buildSliceLoader(values []interface{}) func() []interface{} {
return func() []interface{} {
return values
}
}
func buildSliceLoaderIter(values [][]interface{}) func() []interface{} {
var i = 0
return func() []interface{} {
for i < len(values) {
p := i
i = i + 1
return values[p]
}
return nil
}
}
func dumpSlice(s []interface{}) {
builder := make([]string, len(s))
for i, v := range s {
builder[i] = fmt.Sprintf("%+v, ", v)
}
println(fmt.Sprintf("%v", builder))
}