-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
function_test.go
349 lines (308 loc) · 9.2 KB
/
function_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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* \brief gotvm package
* \file function_test.go
*/
package gotvm
import (
"testing"
"reflect"
"math/rand"
"strings"
"fmt"
)
// Check global function list API
func TestFunctionGlobals(t *testing.T) {
funcNames, err := FuncListGlobalNames()
if err != nil {
t.Error(err.Error())
return
}
if len(funcNames) < 1 {
t.Errorf("Global Function names received:%v\n", funcNames)
}
}
// Check GetFunction API
func TestFunctionGlobalGet(t *testing.T) {
funp, err := GetGlobalFunction("tvm.graph_executor.create")
if err != nil {
t.Error(err.Error())
return
}
if reflect.TypeOf(funp).Kind() != reflect.Ptr {
t.Error("Function type mis matched\n")
return
}
}
func TestFunctionModuleGet(t *testing.T) {
modp, err := LoadModuleFromFile("./deploy.so")
if err != nil {
t.Error(err.Error())
return
}
funp, err := modp.GetFunction("myadd")
if err != nil {
t.Error(err.Error())
return
}
if reflect.TypeOf(funp).Kind() != reflect.Ptr {
t.Error("Function type mis matched\n")
return
}
dlen := int64(1024)
shape := []int64{dlen}
inX, _ := Empty(shape)
inY, _ := Empty(shape)
out, _ := Empty(shape)
dataX := make([]float32, (dlen))
dataY := make([]float32, (dlen))
outExpected := make([]float32, (dlen))
for i := range dataX {
dataX[i] = rand.Float32()
dataY[i] = rand.Float32()
outExpected[i] = dataX[i] + dataY[i]
}
inX.CopyFrom(dataX)
inY.CopyFrom(dataY)
funp.Invoke(inX, inY, out)
outi, _ := out.AsSlice()
outSlice := outi.([]float32)
if len(outSlice) != len(outExpected) {
t.Errorf("Data expected Len: %v Got :%v\n", len(outExpected), len(outSlice))
return
}
for i := range outSlice {
if outExpected[i] != outSlice[i] {
t.Errorf("Data expected: %v Got :%v at index %v\n", outExpected[i], outSlice[i], i)
return
}
}
}
// Check FunctionConvert API
func TestFunctionConvert(t *testing.T) {
sampleCb := func (args ...*Value) (retVal interface{}, err error) {
val1 := args[0].AsInt64()
val2 := args[1].AsInt64()
retVal = int64(val1+val2)
return
}
fhandle, err := ConvertFunction(sampleCb)
if err != nil {
t.Error(err.Error())
return
}
retVal, err := fhandle.Invoke(10, 20)
if err != nil {
t.Error(err.Error())
return
}
if retVal.AsInt64() != int64(30) {
t.Errorf("Expected result :30 got:%v\n", retVal.AsInt64())
return
}
}
func TestFunctionError(t *testing.T) {
sampleCb := func (args ...*Value) (retVal interface{}, err error) {
err = fmt.Errorf("Sample Error XYZABC");
return
}
fhandle, err := ConvertFunction(sampleCb)
if err != nil {
t.Error(err.Error())
return
}
_, err = fhandle.Invoke()
if err == nil {
t.Error("Expected error but didn't received\n")
return
}
if !strings.Contains(err.Error(), string("Sample Error XYZABC")) {
t.Errorf("Expected Error should contain :\"Sample Error XYZABC\" got :%v\n", err.Error())
}
}
// Check FunctionRegister
func TestFunctionRegister(t *testing.T) {
sampleCb := func (args ...*Value) (retVal interface{}, err error) {
val1 := args[0].AsInt64()
val2 := args[1].AsInt64()
retVal = int64(val1+val2)
return
}
RegisterFunction(sampleCb, "TestFunctionRegister.sampleCb");
// Query global functions available
funcNames, err := FuncListGlobalNames()
if err != nil {
t.Error(err.Error())
return
}
found := 0
for ii := range (funcNames) {
if strings.Compare(funcNames[ii], "TestFunctionRegister.sampleCb") == 0 {
found = 1
}
}
if found == 0 {
t.Error("Registered function not found in global function list.")
return
}
// Get "sampleCb" and verify the call.
funp, err := GetGlobalFunction("TestFunctionRegister.sampleCb")
if err != nil {
t.Error(err.Error())
return
}
// Call function
result, err := funp.Invoke((int64)(10), (int64)(20))
if err != nil {
t.Error(err.Error())
return
}
if result.AsInt64() != int64(30) {
t.Errorf("Expected result :30 got:%v\n", result.AsInt64())
return
}
}
// Check packed function receiving go-closure as argument.
func TestFunctionClosureArg(t *testing.T) {
// sampleFunctionArg receives a Packed Function handle and calls it.
sampleFunctionArg := func (args ...*Value) (retVal interface{}, err error) {
// Reveive Packed Function Handle
pfunc := args[0].AsFunction()
// Call Packed Function by Value
ret, err := pfunc.Invoke(args[1], args[2])
if err != nil {
return
}
// Call Packed Function with extracted values
ret1, err := pfunc.Invoke(args[1].AsInt64(), args[2].AsInt64())
if err != nil {
return
}
if ret1.AsInt64() != ret.AsInt64() {
err = fmt.Errorf("Invoke with int64 didn't match with Value")
return
}
retVal = ret
return
}
RegisterFunction(sampleFunctionArg, "TestFunctionClosureArg.sampleFunctionArg");
funp, err := GetGlobalFunction("TestFunctionClosureArg.sampleFunctionArg")
if err != nil {
t.Error(err.Error())
return
}
// funccall is a simple golang callback function like C = A + B.
funccall := func (args ...*Value) (retVal interface{}, err error) {
val1 := args[0].AsInt64()
val2 := args[1].AsInt64()
retVal = int64(val1+val2)
return
}
// Call function
result, err := funp.Invoke(funccall, 30, 50)
if err != nil {
t.Error(err.Error())
return
}
if result.AsInt64() != int64(80) {
t.Errorf("Expected result :80 got:%v\n", result.AsInt64())
return
}
}
// Check packed function returning a go-closure.
func TestFunctionClosureReturn(t *testing.T) {
// sampleFunctionCb returns a function closure which is embed as packed function in TVMValue.
sampleFunctionCb := func (args ...*Value) (retVal interface{}, err error) {
funccall := func (cargs ...*Value) (fret interface{}, ferr error) {
val1 := cargs[0].AsInt64()
val2 := cargs[1].AsInt64()
fret = int64(val1+val2)
return
}
retVal = funccall
return
}
RegisterFunction(sampleFunctionCb, "TestFunctionClosureReturn.sampleFunctionCb");
funp, err := GetGlobalFunction("TestFunctionClosureReturn.sampleFunctionCb")
if err != nil {
t.Error(err.Error())
return
}
// Call function
result, err := funp.Invoke()
if err != nil {
t.Error(err.Error())
return
}
pfunc := result.AsFunction()
pfuncRet, err := pfunc.Invoke(30, 40)
if err != nil {
t.Error(err.Error())
return
}
if pfuncRet.AsInt64() != int64(70) {
t.Errorf("Expected result :70 got:%v\n", pfuncRet.AsInt64())
return
}
}
// Check packed function with no arguments and no return values.
func TestFunctionNoArgsReturns(t *testing.T) {
sampleFunction := func (args ...*Value) (retVal interface{}, err error) {
return
}
fhandle, err := ConvertFunction(sampleFunction)
if err != nil {
t.Error(err.Error())
return
}
_, err = fhandle.Invoke()
if err != nil {
t.Error(err.Error())
return
}
}
// Check packed function returning a go-closure with no arg and returns.
func TestFunctionNoArgsReturns2(t *testing.T) {
// sampleFunctionCb returns a function closure which is embed as packed function in TVMValue.
sampleFunctionCb := func (args ...*Value) (retVal interface{}, err error) {
funccall := func (cargs ...*Value) (fret interface{}, ferr error) {
return
}
retVal = funccall
return
}
funp, err := ConvertFunction(sampleFunctionCb)
if err != nil {
t.Error(err.Error())
return
}
// Call function
result, err := funp.Invoke()
if err != nil {
t.Error(err.Error())
return
}
pfunc := result.AsFunction()
_, err = pfunc.Invoke()
if err != nil {
t.Error(err.Error())
return
}
}