-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtensor_gob_test.go
37 lines (30 loc) · 940 Bytes
/
tensor_gob_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
package gotorch_test
import (
"bytes"
"crypto/md5"
"encoding/gob"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wangkuiyi/gotorch"
)
func TestTensorGobEncode(t *testing.T) {
a := gotorch.NewTensor([][]float32{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}})
b, e := a.GobEncode()
assert.NoError(t, e)
// The ground-truth length and MD5 checksum come from the C++ program
// example/pickle.
assert.Equal(t, 747, len(b))
assert.Equal(t, fmt.Sprintf("%x", md5.Sum(b)), "dd65752601bf4d4ca19ae903baf96799")
a = gotorch.Tensor{nil}
_, e = a.GobEncode()
assert.Error(t, e)
}
func TestTensorGobDecode(t *testing.T) {
x := gotorch.NewTensor([][]float32{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}})
var buf bytes.Buffer
assert.NoError(t, gob.NewEncoder(&buf).Encode(x))
var y gotorch.Tensor
assert.NoError(t, gob.NewDecoder(&buf).Decode(&y))
assert.Equal(t, " 1 0 0\n 0 1 0\n 0 0 1\n[ CPUFloatType{3,3} ]", y.String())
}