This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapply_test.go
134 lines (116 loc) · 3.8 KB
/
apply_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
/*
** Copyright © 2018, Oracle and/or its affiliates. All rights reserved.
** Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
*/
package graphpipe
import (
"fmt"
"reflect"
"testing"
fb "github.com/google/flatbuffers/go"
graphpipefb "github.com/oracle/graphpipe-go/graphpipefb"
)
func TestSimpleApplyString(t *testing.T) {
v := []string{"foo", "bar", "baz"}
tmp, _ := nativeToTensor(v)
tensor := TensorToNativeTensor(tmp)
if err := testSimpleApplyFunc(t, tensor, applyInterface); err != nil {
t.Fatal(err)
}
if err := testSimpleApplyFunc(t, tensor, applyString); err != nil {
t.Fatal(err)
}
if err := testSimpleApplyFunc(t, tensor, applyInterfaceError); err != nil {
t.Fatal(err)
}
if err := testSimpleApplyFunc(t, tensor, applyStringError); err != nil {
t.Fatal(err)
}
}
func TestSimpleApplyFloat(t *testing.T) {
v := []float32{1., 2., 3.}
tmp, _ := nativeToTensor(v)
tensor := TensorToNativeTensor(tmp)
if err := testSimpleApplyFunc(t, tensor, applyInterface); err != nil {
t.Fatal(err)
}
if err := testSimpleApplyFunc(t, tensor, applyFloat); err != nil {
t.Fatal(err)
}
if err := testSimpleApplyFunc(t, tensor, applyInterfaceError); err != nil {
t.Fatal(err)
}
if err := testSimpleApplyFunc(t, tensor, applyFloatError); err != nil {
t.Fatal(err)
}
}
func TestSimpleApplyMulti(t *testing.T) {
tmp, _ := nativeToTensor([][]string{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}})
tensor := TensorToNativeTensor(tmp)
if err := testSimpleApplyFunc(t, tensor, applyMulti); err != nil {
t.Fatal(err)
}
}
func testSimpleApplyFunc(t *testing.T, tensor *NativeTensor, f interface{}) error {
return testSimpleApplyFuncBounded(t, tensor, f, nil, nil)
}
func testSimpleApplyFuncBounded(t *testing.T, tensor *NativeTensor, f interface{}, inshape, outshape [][]int64) error {
opts := BuildSimpleApply(f, inshape, outshape)
inputs := map[string]*NativeTensor{"input0": tensor}
outputNames := []string{"output0"}
rc := RequestContext{builder: fb.NewBuilder(1024)}
res, err := opts.Apply(&rc, "", inputs, outputNames)
if err != nil {
return err
}
offset := res[0].Build(rc.builder)
buf := Serialize(rc.builder, offset)
t2 := graphpipefb.GetRootAsTensor(buf, 0)
a, _ := NativeTensorToNative(tensor)
b, _ := TensorToNative(t2)
if a == nil || !reflect.DeepEqual(a, b) {
return fmt.Errorf("tps %v and %v are not equal", a, b)
}
return nil
}
func TestSimpleApplyShapes(t *testing.T) {
tmp, _ := nativeToTensor([][]string{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}})
tensor := TensorToNativeTensor(tmp)
shape := [][]int64{{-1, 3}}
if err := testSimpleApplyFuncBounded(t, tensor, applyMulti, shape, nil); err != nil {
t.Fatal(err)
}
if err := testSimpleApplyFuncBounded(t, tensor, applyMulti, nil, shape); err != nil {
t.Fatal(err)
}
shape = [][]int64{{2, -1}}
if err := testSimpleApplyFuncBounded(t, tensor, applyMulti, shape, nil); err == nil {
t.Fatal("Sending wrong size failed to error")
}
/*
if err := testSimpleApplyFuncBounded(t, tensor, applyMulti, nil, shape); err == nil {
t.Fatal("Returning wrong size failed to error")
}
*/
}
func applyInterface(_ *RequestContext, config string, in interface{}) interface{} {
return in
}
func applyString(_ *RequestContext, config string, in []string) []string {
return in
}
func applyFloat(_ *RequestContext, config string, in []float32) []float32 {
return in
}
func applyInterfaceError(_ *RequestContext, config string, in interface{}) (interface{}, error) {
return in, nil
}
func applyStringError(_ *RequestContext, config string, in []string) ([]string, error) {
return in, nil
}
func applyFloatError(_ *RequestContext, config string, in []float32) ([]float32, error) {
return in, nil
}
func applyMulti(_ *RequestContext, config string, in [][]string) [][]string {
return in
}