-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist_test.go
108 lines (102 loc) · 2.21 KB
/
list_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
package kv
import (
"reflect"
"testing"
"github.com/jjeffery/kv/internal/pool"
)
func TestListMarshal(t *testing.T) {
tests := []struct {
list List
text string
marshaled string
unmarshaled List
}{
{
list: List{"a", 1, "b", "value 2"},
marshaled: `a=1 b="value 2"`,
unmarshaled: List{"a", "1", "b", "value 2"},
},
{
list: List{"a", 1, "b", "value 2"},
marshaled: `a=1 b="value 2"`,
text: "leading message ",
unmarshaled: List{"msg", "leading message", "a", "1", "b", "value 2"},
},
}
for tn, tt := range tests {
b, err := tt.list.MarshalText()
if err != nil {
t.Error(err)
continue
}
if got, want := string(b), tt.marshaled; got != want {
t.Errorf("%d:\n got=%v\nwant=%v", tn, got, want)
continue
}
if tt.text != "" {
var m = []byte(tt.text)
m = append(m, b...)
b = m
}
var l List
if err = l.UnmarshalText(b); err != nil {
t.Error(err)
continue
}
if got, want := l, tt.unmarshaled; !reflect.DeepEqual(got, want) {
t.Errorf("%d:\n got=%v\nwant=%v", tn, got, want)
continue
}
}
}
func TestListClone(t *testing.T) {
tests := []struct {
list List
cap int
}{
{
list: List{},
cap: 0,
},
{
list: List{"a", 1},
cap: 0,
},
{
list: List{"a", 1},
cap: 11,
},
}
for tn, tt := range tests {
clone := tt.list.clone(tt.cap)
if got, want := clone, tt.list; !reflect.DeepEqual(got, want) {
t.Errorf("%d:\n got=%+v\nwant=%+v", tn, got, want)
continue
}
if got, want := clone.Keyvals(), tt.list.Keyvals(); !reflect.DeepEqual(got, want) {
t.Errorf("%d:\n got=%+v\nwant=%+v", tn, got, want)
continue
}
cloneCap := tt.cap
if n := len(tt.list); cloneCap < n {
cloneCap = n
}
if got, want := cap(clone), cloneCap; got != want {
t.Errorf("%d:\n got=%+v\nwant=%+v", tn, got, want)
continue
}
}
}
func BenchmarkList1(b *testing.B) {
benchmarkListString(With("a", 1), b)
}
func BenchmarkList5(b *testing.B) {
benchmarkListString(With("a", 1, "b", "value 2", "c", "3", "d", 4, "e", true), b)
}
func benchmarkListString(list List, b *testing.B) {
for i := 0; i < b.N; i++ {
buf := pool.AllocBuffer()
list.writeToBuffer(buf)
pool.ReleaseBuffer(buf)
}
}