-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparam_test.go
185 lines (149 loc) · 4.12 KB
/
param_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package shift
import (
"fmt"
"testing"
)
func TestParams_Get(t *testing.T) {
ip := newInternalParams(3)
ip.setKeys(&[]string{"k3", "k2", "k1"})
ip.appendValue("xyz")
ip.appendValue("bar")
ip.appendValue("foo")
p := newParams(ip)
tests := map[string]string{
"k3": "xyz",
"k2": "bar",
"k1": "foo",
}
for k, v := range tests {
val := p.Get(k)
assert(t, val == v, fmt.Sprintf("expected: %s, got: %s", v, val))
}
}
func TestParams_ForEach(t *testing.T) {
ip := newInternalParams(3)
ip.setKeys(&[]string{"k3", "k2", "k1"})
ip.appendValue("xyz")
ip.appendValue("bar")
ip.appendValue("foo")
p := newParams(ip)
tests := []struct{ k, v string }{
{"k1", "foo"},
{"k2", "bar"},
{"k3", "xyz"},
}
i := 0
p.ForEach(func(k, v string) {
assert(t, k == tests[i].k, fmt.Sprintf("key > expected: %s, got: %s", tests[i].k, k))
assert(t, v == tests[i].v, fmt.Sprintf("value > expected: %s, got: %s", tests[i].v, v))
i++
})
assert(t, i == 3, fmt.Sprintf("i > expected: %d, got: %d", 3, i))
}
func TestParams_Map(t *testing.T) {
ip := newInternalParams(3)
ip.setKeys(&[]string{"k3", "k2", "k1"})
ip.appendValue("xyz")
ip.appendValue("bar")
ip.appendValue("foo")
p := newParams(ip)
tests := map[string]string{
"k1": "foo",
"k2": "bar",
"k3": "xyz",
}
params := p.Map()
for k, v := range tests {
val := params[k]
assert(t, val == v, fmt.Sprintf("value for key %s, expected: %s, got: %s", k, v, val))
}
assert(t, len(params) == 3, fmt.Sprintf("map length > expected: %d, got: %d", 3, len(params)))
}
func TestParams_Slice(t *testing.T) {
ip := newInternalParams(3)
ip.setKeys(&[]string{"k3", "k2", "k1"})
ip.appendValue("xyz")
ip.appendValue("bar")
ip.appendValue("foo")
p := newParams(ip)
tests := []struct{ k, v string }{
{"k1", "foo"},
{"k2", "bar"},
{"k3", "xyz"},
}
params := p.Slice()
for i := 0; i < len(tests); i++ {
assert(t, params[i].Key == tests[i].k, fmt.Sprintf("key at index %d > expected: %s, got: %s", i, tests[i].k, params[i].Key))
assert(t, params[i].Value == tests[i].v, fmt.Sprintf("value at index %d > expected: %s, got: %s", i, tests[i].v, params[i].Value))
}
assert(t, len(params) == 3, fmt.Sprintf("params count > expected: %d, got: %d", 3, len(params)))
}
func TestParams_Copy(t *testing.T) {
t.Run("copied Params should not be equal to source Params", func(t *testing.T) {
ip := newInternalParams(1)
ip.setKeys(&[]string{"foo"})
ip.appendValue("bar")
p := newParams(ip)
cp := p.Copy()
assert(t, cp != p, "expected to be unequal")
assert(t, cp.internal != p.internal, "expected internal to be unequal")
})
t.Run("copied Params should not be affected when source is reset", func(t *testing.T) {
ip := newInternalParams(1)
ip.setKeys(&[]string{"foo"})
ip.appendValue("bar")
p := newParams(ip)
cp := p.Copy()
ip.reset() // resets internal of source
val := cp.Get("foo")
assert(t, val == "bar", fmt.Sprintf("expected: bar, got: %s", val))
})
t.Run("copied Params should not be affected when source is reset and reused", func(t *testing.T) {
ip := newInternalParams(2)
ip.setKeys(&[]string{"foo", "woo"})
ip.appendValue("bar")
ip.appendValue("abc")
p := newParams(ip)
cp := p.Copy()
ip.reset() // resets internal of source
ip.setKeys(&[]string{"foo"})
ip.appendValue("xyz")
val := cp.Get("foo")
assert(t, val == "bar", fmt.Sprintf("expected: bar, got: %s", val))
})
}
func BenchmarkParams_Copy(b *testing.B) {
b.Run("with non-<nil> internal", func(b *testing.B) {
ip := newInternalParams(10)
ip.setKeys(&[]string{"1", "2", "3"})
ip.appendValue("abc")
ip.appendValue("xyz")
ip.appendValue("cap")
p := newParams(ip)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.Copy()
}
})
b.Run("with <nil> internal", func(b *testing.B) {
p := newParams(nil)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.Copy()
}
})
}
func BenchmarkInternalParams_DeepCopy(b *testing.B) {
ip := newInternalParams(10)
ip.setKeys(&[]string{"1", "2", "3"})
ip.appendValue("abc")
ip.appendValue("xyz")
ip.appendValue("cap")
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ip.deepCopy()
}
}