-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtensor_test.go
54 lines (44 loc) · 974 Bytes
/
tensor_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
package torch
import (
"testing"
"unsafe"
)
func Test_createTensor(t *testing.T) {
data := []float32{1, 2}
ctx := createTensor(unsafe.Pointer(&data[0]), []int64{2}, Float)
if ctx == nil {
t.Error("should have returned an array")
}
}
func Test_NewTensor(t *testing.T) {
tensor, err := NewTensor([]float32{1, 2})
if err != nil {
t.Error(err)
}
if tensor == nil {
t.Error("no tensor returned")
}
if tensor.DType() != Float {
t.Error("should be a float tensor")
}
val := tensor.Value().([]float32)
if val[0] != 1.0 {
t.Error("wrong value returned by tensor")
}
if val[1] != 2.0 {
t.Error("wrong value returned by tensor")
}
if tensor.Shape()[0] != 2 {
t.Error("wrong shape returned by tensor")
}
}
func Test_PrintTensors(t *testing.T) {
a, _ := NewTensor([]float32{1, 2})
b, _ := NewTensor([]float32{1, 2})
PrintTensors(a, b)
}
func Benchmark_NewTensor(b *testing.B) {
for i := 0; i < b.N; i++ {
NewTensor([]float32{1, 2})
}
}