-
Notifications
You must be signed in to change notification settings - Fork 10
/
efaceconv_test.go
256 lines (210 loc) · 4.78 KB
/
efaceconv_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package main
import (
"testing"
)
const (
ecImport = "net/http"
ecVar = "_StringKind uintptr"
ecInit = "var sString string\n _StringKind = ecutils.GetKind(sString)"
ecMethodTemplate = `
// Eface2String returns pointer to string and true if arg is a string
// or nil and false otherwise
func Eface2String(arg interface{}) (*string, bool) {
if ecutils.GetKind(arg) == _StringKind {
return (*string)(ecutils.GetDataPtr(arg)), true
}
return nil, false
}
`
ecTestTemplate = `
func TestEface2String(t *testing.T) {
var String string
res, ok := Eface2String(String)
if !ok {
t.Error("Wrong type!")
}
if !reflect.DeepEqual(*res, String) {
t.Error("Not equal")
}
_, ok = Eface2String(ok)
if ok {
t.Error("Wrong type!")
}
}
`
ecBenchmark = `
func BenchmarkEface2String(b *testing.B) {
var String string
var v *string
var ok bool
for n := 0; n < b.N; n++ {
v, ok = Eface2String(String)
}
b.Log(v, ok) //For don't use compiler optimization
}
func _StringClassic(arg interface{}) (v string, ok bool) {
v, ok = arg.(string)
return v, ok
}
func BenchmarkStringClassic(b *testing.B) {
var String string
var v string
var ok bool
for n := 0; n < b.N; n++ {
v, ok = _StringClassic(String)
}
b.Log(v, ok) //For don't use compiler optimization
}
`
genSrc = `//generated by efaceconv DO NOT EDIT!
package example
import (
"github.com/t0pep0/efaceconv/ecutils"
"net/http"
)
var (
_StringKind uintptr
_SByteKind uintptr
)
func init(){
var sString string
_StringKind = ecutils.GetKind(sString)
var sSByte []byte
_SByteKind = ecutils.GetKind(sSByte)
}
// Eface2String returns pointer to string and true if arg is a string
// or nil and false otherwise
func Eface2String(arg interface{}) (*string, bool) {
if ecutils.GetKind(arg) == _StringKind {
return (*string)(ecutils.GetDataPtr(arg)), true
}
return nil, false
}
// Eface2SByte returns pointer to []byte and true if arg is a string
// or nil and false otherwise
func Eface2SByte(arg interface{}) (*[]byte, bool) {
if ecutils.GetKind(arg) == _SByteKind {
return (*[]byte)(ecutils.GetDataPtr(arg)), true
}
return nil, false
}
`
genTest = `//generated by efaceconv DO NOT EDIT!
package example
import (
"reflect"
"testing"
"net/http"
)
func TestEface2String(t *testing.T) {
var String string
res, ok := Eface2String(String)
if !ok {
t.Error("Wrong type!")
}
if !reflect.DeepEqual(*res, String) {
t.Error("Not equal")
}
_, ok = Eface2String(ok)
if ok {
t.Error("Wrong type!")
}
}
func BenchmarkEface2String(b *testing.B) {
var String string
var v *string
var ok bool
for n := 0; n < b.N; n++ {
v, ok = Eface2String(String)
}
b.Log(v, ok) //For don't use compiler optimization
}
func _StringClassic(arg interface{}) (v string, ok bool) {
v, ok = arg.(string)
return v, ok
}
func BenchmarkStringClassic(b *testing.B) {
var String string
var v string
var ok bool
for n := 0; n < b.N; n++ {
v, ok = _StringClassic(String)
}
b.Log(v, ok) //For don't use compiler optimization
}
func TestEface2SByte(t *testing.T) {
var SByte []byte
res, ok := Eface2SByte(SByte)
if !ok {
t.Error("Wrong type!")
}
if !reflect.DeepEqual(*res, SByte) {
t.Error("Not equal")
}
_, ok = Eface2SByte(ok)
if ok {
t.Error("Wrong type!")
}
}
func BenchmarkEface2SByte(b *testing.B) {
var SByte []byte
var v *[]byte
var ok bool
for n := 0; n < b.N; n++ {
v, ok = Eface2SByte(SByte)
}
b.Log(v, ok) //For don't use compiler optimization
}
func _SByteClassic(arg interface{}) (v []byte, ok bool) {
v, ok = arg.([]byte)
return v, ok
}
func BenchmarkSByteClassic(b *testing.B) {
var SByte []byte
var v []byte
var ok bool
for n := 0; n < b.N; n++ {
v, ok = _SByteClassic(SByte)
}
b.Log(v, ok) //For don't use compiler optimization
}
`
)
var unit = &ecUnit{Import: "net/http", Type: "string", CustomName: "String"}
var unit2 = &ecUnit{Import: "", Type: "[]byte", CustomName: "SByte"}
func TestEcUnitImport(t *testing.T) {
if unit.generateImport() != ecImport {
t.Fatal("Import not correct")
}
}
func TestEcUnitInit(t *testing.T) {
if unit.generateInit() != ecInit {
t.Fatal("Init not correct")
}
}
func TestEcUnitConvMethod(t *testing.T) {
if unit.generateConvMethod() != ecMethodTemplate {
t.Fatal("Method not correct")
}
}
func TestEcUnitTestMethod(t *testing.T) {
if unit.generateTestMethod() != ecTestTemplate {
t.Fatal("Test method not correct")
}
}
func TestEcUnitBenchmarkMethod(t *testing.T) {
if unit.generatebenchmarkMethod() != ecBenchmark {
t.Fatal("Benchmark not correct")
}
}
var gen = &generated{Package: "example", ecUnits: []*ecUnit{unit, unit2}}
func TestGenSrc(t *testing.T) {
if gen.GenSrc() != genSrc {
t.Fatal("Source not corect")
}
}
func TestGenTest(t *testing.T) {
if gen.GenTest() != genTest {
t.Fatal("Test not corect")
}
}