|
@@ -3,7 +3,6 @@ package flow_test
|
|
|
import (
|
|
|
"bytes"
|
|
|
"encoding/json"
|
|
|
- "errors"
|
|
|
"testing"
|
|
|
"time"
|
|
|
|
|
@@ -18,35 +17,27 @@ import (
|
|
|
func init() {
|
|
|
assert.Quiet = true
|
|
|
}
|
|
|
-func TestError(t *testing.T) {
|
|
|
- a := assert.A(t)
|
|
|
-
|
|
|
- f := flow.New()
|
|
|
- err := errors.New("test")
|
|
|
- f.Err(err)
|
|
|
- a.Eq(f.Err(), err, "error should be the same")
|
|
|
-}
|
|
|
func TestDefOp(t *testing.T) {
|
|
|
a := assert.A(t)
|
|
|
f := flow.New()
|
|
|
|
|
|
- f.DefOp("2", "vecadd", []float32{1, 1, 1}, []float32{2, 2, 2}) // r:3 3 3
|
|
|
- a.Eq(f.Err(), nil, "doing DefOp")
|
|
|
+ var err error
|
|
|
+ _, err = f.DefOp("2", "vecadd", []float32{1, 1, 1}, []float32{2, 2, 2}) // r:3 3 3
|
|
|
+ a.Eq(err, nil, "doing DefOp")
|
|
|
|
|
|
- f.DefOp("1", "vecadd", []float32{1, 2, 3}, f.Res("2")) // r: 4 5 6
|
|
|
- a.Eq(f.Err(), nil, "doing DefOp")
|
|
|
+ _, err = f.DefOp("1", "vecadd", []float32{1, 2, 3}, f.Res("2")) // r: 4 5 6
|
|
|
+ a.Eq(err, nil, "doing DefOp")
|
|
|
|
|
|
- op := f.Op("vecmul", f.Res("1"), []float32{2, 2, 2}) //r:8 10 12
|
|
|
- a.Eq(f.Err(), nil, "mul operation")
|
|
|
+ op, err := f.Op("vecmul", f.Res("1"), []float32{2, 2, 2}) //r:8 10 12
|
|
|
+ a.Eq(err, nil, "mul operation")
|
|
|
a.NotEq(op, nil, "operation not nil")
|
|
|
|
|
|
desired := []float32{8, 10, 12}
|
|
|
- res := op.Process()
|
|
|
+ res, _ := op.Process()
|
|
|
a.Eq(desired, res, "vector result should match")
|
|
|
|
|
|
- op = f.DefOp("123", "none")
|
|
|
- op.Process()
|
|
|
- a.NotEq(f.Err(), nil, "Error should not be nil")
|
|
|
+ op, err = f.DefOp("123", "none")
|
|
|
+ a.NotEq(err, nil, "Error should not be nil")
|
|
|
}
|
|
|
|
|
|
func TestIDGen(t *testing.T) {
|
|
@@ -63,18 +54,18 @@ func TestIDGen(t *testing.T) {
|
|
|
return newID
|
|
|
})
|
|
|
|
|
|
- o := f.Op("vecadd", f.In(0), f.In(1))
|
|
|
- a.Eq(f.Err(), nil, "Should not nil")
|
|
|
+ o, err := f.Op("vecadd", f.In(0), f.In(1))
|
|
|
+ a.Eq(err, nil, "Should not nil")
|
|
|
a.Eq(o.ID(), "1", "id should be 1")
|
|
|
|
|
|
- o = f.Op("vecadd", f.In(0), f.In(1))
|
|
|
- a.Eq(f.Err(), nil, "Should not nil")
|
|
|
+ o, err = f.Op("vecadd", f.In(0), f.In(1))
|
|
|
+ a.Eq(err, nil, "Should not nil")
|
|
|
a.Eq(o.ID(), "2", "id should be 2")
|
|
|
|
|
|
- c := f.Const(1)
|
|
|
- f.Const(1)
|
|
|
- o = f.Op("vecadd", f.In(0), c)
|
|
|
- a.NotEq(f.Err(), nil, "Should be nil,id generation exausted")
|
|
|
+ c, _ := f.Const(1)
|
|
|
+ _, err = f.Const(1)
|
|
|
+ a.NotEq(err, nil, "Should not be nil,id generation exausted")
|
|
|
+ o, err = f.Op("vecadd", f.In(0), c)
|
|
|
|
|
|
}
|
|
|
|
|
@@ -83,25 +74,25 @@ func TestSerialize(t *testing.T) {
|
|
|
f := flow.New()
|
|
|
var1 := f.Var("var1", []float32{4, 4, 4})
|
|
|
|
|
|
- c1 := f.Const([]float32{1, 2, 3})
|
|
|
- c2 := f.Const([]float32{2, 2, 2})
|
|
|
+ c1, _ := f.Const([]float32{1, 2, 3})
|
|
|
+ c2, _ := f.Const([]float32{2, 2, 2})
|
|
|
|
|
|
- op1 := f.Op("vecmul", // op:0 - expected: [12,16,20,24]
|
|
|
+ op1, _ := f.Op("vecmul", // op:0 - expected: [12,16,20,24]
|
|
|
f.Var("vec1", []float32{4, 4, 4, 4}),
|
|
|
- f.Op("vecadd", // op:1 - expected: [3,4,5,6]
|
|
|
- f.Const([]float32{1, 2, 3, 4}),
|
|
|
- f.Const([]float32{2, 2, 2, 2}),
|
|
|
- ),
|
|
|
+ f.Must(f.Op("vecadd", // op:1 - expected: [3,4,5,6]
|
|
|
+ f.Must(f.Const([]float32{1, 2, 3, 4})),
|
|
|
+ f.Must(f.Const([]float32{2, 2, 2, 2})),
|
|
|
+ )),
|
|
|
)
|
|
|
- mul1 := f.Op("vecmul", c1, op1) // op:2 - expected 12, 32, 60, 0
|
|
|
- mul2 := f.Op("vecmul", mul1, var1) // op:3 - expected 48, 128, 240, 0
|
|
|
- mul3 := f.Op("vecmul", c2, mul2) // op:4 - expected 96, 256, 480, 0
|
|
|
- mul4 := f.Op("vecmul", mul3, f.In(0)) // op:5 - expected 96, 512, 1440,0
|
|
|
+ mul1, _ := f.Op("vecmul", c1, op1) // op:2 - expected 12, 32, 60, 0
|
|
|
+ mul2, _ := f.Op("vecmul", mul1, var1) // op:3 - expected 48, 128, 240, 0
|
|
|
+ mul3, _ := f.Op("vecmul", c2, mul2) // op:4 - expected 96, 256, 480, 0
|
|
|
+ mul4, _ := f.Op("vecmul", mul3, f.In(0)) // op:5 - expected 96, 512, 1440,0
|
|
|
|
|
|
s := bytes.NewBuffer(nil)
|
|
|
f.Analyse(s, []float32{1, 2, 3, 4})
|
|
|
t.Log(s)
|
|
|
- res := mul4.Process([]float32{1, 2, 3, 4})
|
|
|
+ res, _ := mul4.Process([]float32{1, 2, 3, 4})
|
|
|
|
|
|
t.Log("Res:", res)
|
|
|
t.Log("Flow:\n", f)
|
|
@@ -117,20 +108,20 @@ func TestConst(t *testing.T) {
|
|
|
a := assert.A(t)
|
|
|
f := flow.New()
|
|
|
|
|
|
- c := f.Const(1)
|
|
|
- res := c.Process()
|
|
|
- //Expect(t).Eq(res, 1)
|
|
|
+ c, _ := f.Const(1)
|
|
|
+ res, err := c.Process()
|
|
|
a.Eq(res, 1, "It should be one")
|
|
|
+ a.Eq(err, nil, "const should not error")
|
|
|
}
|
|
|
func TestOp(t *testing.T) {
|
|
|
a := assert.A(t)
|
|
|
f := flow.New()
|
|
|
|
|
|
- add := f.Op("vecadd",
|
|
|
- f.Op("vecmul",
|
|
|
+ add, err := f.Op("vecadd",
|
|
|
+ f.Must(f.Op("vecmul",
|
|
|
[]float32{1, 2, 3},
|
|
|
[]float32{2, 2, 2},
|
|
|
- ),
|
|
|
+ )),
|
|
|
[]float32{1, 2, 3},
|
|
|
)
|
|
|
res, err := f.Run(add)
|
|
@@ -141,34 +132,42 @@ func TestOp(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestVariable(t *testing.T) {
|
|
|
+ a := assert.A(t)
|
|
|
f := flow.New()
|
|
|
v := f.Var("v1", 1)
|
|
|
|
|
|
- res := v.Process()
|
|
|
- assert.A(t).Eq(res, 1)
|
|
|
+ res, err := v.Process()
|
|
|
+ a.Eq(err, nil)
|
|
|
+ a.Eq(res, 1)
|
|
|
|
|
|
v.Set(2)
|
|
|
- res = v.Process()
|
|
|
- assert.Eq(t, res, 2)
|
|
|
+ res, err = v.Process()
|
|
|
+ a.Eq(err, nil)
|
|
|
+ a.Eq(res, 2)
|
|
|
}
|
|
|
|
|
|
func TestCache(t *testing.T) {
|
|
|
+ a := assert.A(t)
|
|
|
f := flow.New()
|
|
|
{
|
|
|
- r := f.Op("inc")
|
|
|
+ r, err := f.Op("inc")
|
|
|
+ a.Eq(err, nil, "should not error giving operation")
|
|
|
+
|
|
|
var res interface{}
|
|
|
for i := 1; i < 5; i++ {
|
|
|
- res = r.Process()
|
|
|
- assert.Eq(t, res, i)
|
|
|
+ res, err = r.Process()
|
|
|
+ a.Eq(err, nil)
|
|
|
+ a.Eq(res, i)
|
|
|
}
|
|
|
}
|
|
|
{
|
|
|
var res flow.Data
|
|
|
- inc := f.Op("inc")
|
|
|
- add := f.Op("add", inc, inc)
|
|
|
- res = add.Process() // 1+1
|
|
|
+ inc, _ := f.Op("inc")
|
|
|
+
|
|
|
+ add, _ := f.Op("add", inc, inc)
|
|
|
+ res, _ = add.Process() // 1+1
|
|
|
assert.Eq(t, res, 2)
|
|
|
- res = add.Process() // 2+2
|
|
|
+ res, _ = add.Process() // 2+2
|
|
|
assert.Eq(t, res, 4)
|
|
|
}
|
|
|
}
|
|
@@ -185,19 +184,19 @@ func TestHandler(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func TestLocalRegistry(t *testing.T) {
|
|
|
- assert := assert.A(t)
|
|
|
+ a := assert.A(t)
|
|
|
|
|
|
r := registry.New()
|
|
|
e := r.Register("test", func() string { return "" })
|
|
|
- assert.NotEq(e, nil, "registered in a local register")
|
|
|
+ a.NotEq(e, nil, "registered in a local register")
|
|
|
|
|
|
f := flow.New()
|
|
|
f.SetRegistry(r)
|
|
|
- op := f.Op("test")
|
|
|
- assert.NotEq(op, nil, "operation should be valid")
|
|
|
+ op, _ := f.Op("test")
|
|
|
+ a.NotEq(op, nil, "operation should be valid")
|
|
|
|
|
|
- op = f.Op("none")
|
|
|
- assert.NotEq(f.Err(), nil, "flow should contain an error")
|
|
|
+ op, err := f.Op("none")
|
|
|
+ a.NotEq(err, nil, "flow should contain an error")
|
|
|
}
|
|
|
|
|
|
func BenchmarkComplex(b *testing.B) {
|
|
@@ -235,11 +234,11 @@ func prepareComplex() (*flow.Flow, flow.Operation) {
|
|
|
f1 := f.Var("f1", v1)
|
|
|
f2 := f.Var("f2", v2)
|
|
|
|
|
|
- mul := f.Op("vecmul", f1, f2) // Doubles 2,4,6,8...
|
|
|
- add := f.Op("vecadd", mul, f2) // Sum 4,8,10,12...
|
|
|
- mul2 := f.Op("vecmul", mul, add) // mul again
|
|
|
- mul3 := f.Op("vecmul", mul2, f1) // mul with f1
|
|
|
- div1 := f.Op("vecdiv", mul3, mul2) // div
|
|
|
+ mul, _ := f.Op("vecmul", f1, f2) // Doubles 2,4,6,8...
|
|
|
+ add, _ := f.Op("vecadd", mul, f2) // Sum 4,8,10,12...
|
|
|
+ mul2, _ := f.Op("vecmul", mul, add) // mul again
|
|
|
+ mul3, _ := f.Op("vecmul", mul2, f1) // mul with f1
|
|
|
+ div1, _ := f.Op("vecdiv", mul3, mul2) // div
|
|
|
|
|
|
return f, div1
|
|
|
}
|