forked from gorgonia/tensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdense_compat_test.go
107 lines (96 loc) · 3.36 KB
/
dense_compat_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
// Code generated by genlib2. DO NOT EDIT.
package tensor
import (
"testing"
"github.com/stretchr/testify/assert"
"gonum.org/v1/gonum/mat"
)
var toMat64Tests = []struct {
data interface{}
sliced interface{}
shape Shape
dt Dtype
}{
{Range(Int, 0, 6), []int{0, 1, 3, 4}, Shape{2, 3}, Int},
{Range(Int8, 0, 6), []int8{0, 1, 3, 4}, Shape{2, 3}, Int8},
{Range(Int16, 0, 6), []int16{0, 1, 3, 4}, Shape{2, 3}, Int16},
{Range(Int32, 0, 6), []int32{0, 1, 3, 4}, Shape{2, 3}, Int32},
{Range(Int64, 0, 6), []int64{0, 1, 3, 4}, Shape{2, 3}, Int64},
{Range(Uint, 0, 6), []uint{0, 1, 3, 4}, Shape{2, 3}, Uint},
{Range(Uint8, 0, 6), []uint8{0, 1, 3, 4}, Shape{2, 3}, Uint8},
{Range(Uint16, 0, 6), []uint16{0, 1, 3, 4}, Shape{2, 3}, Uint16},
{Range(Uint32, 0, 6), []uint32{0, 1, 3, 4}, Shape{2, 3}, Uint32},
{Range(Uint64, 0, 6), []uint64{0, 1, 3, 4}, Shape{2, 3}, Uint64},
{Range(Float32, 0, 6), []float32{0, 1, 3, 4}, Shape{2, 3}, Float32},
{Range(Float64, 0, 6), []float64{0, 1, 3, 4}, Shape{2, 3}, Float64},
{Range(Complex64, 0, 6), []complex64{0, 1, 3, 4}, Shape{2, 3}, Complex64},
{Range(Complex128, 0, 6), []complex128{0, 1, 3, 4}, Shape{2, 3}, Complex128},
}
func TestToMat64(t *testing.T) {
assert := assert.New(t)
for i, tmt := range toMat64Tests {
T := New(WithBacking(tmt.data), WithShape(tmt.shape...))
var m *mat.Dense
var err error
if m, err = ToMat64(T); err != nil {
t.Errorf("ToMat basic test %d failed : %v", i, err)
continue
}
conv := anyToFloat64s(tmt.data)
assert.Equal(conv, m.RawMatrix().Data, "i %d from %v", i, tmt.dt)
if T, err = sliceDense(T, nil, makeRS(0, 2)); err != nil {
t.Errorf("Slice failed %v", err)
continue
}
if m, err = ToMat64(T); err != nil {
t.Errorf("ToMat of slice test %d failed : %v", i, err)
continue
}
conv = anyToFloat64s(tmt.sliced)
assert.Equal(conv, m.RawMatrix().Data, "sliced test %d from %v", i, tmt.dt)
t.Logf("Done")
if tmt.dt == Float64 {
T = New(WithBacking(tmt.data), WithShape(tmt.shape...))
if m, err = ToMat64(T, UseUnsafe()); err != nil {
t.Errorf("ToMat64 unsafe test %d failed: %v", i, err)
}
conv = anyToFloat64s(tmt.data)
assert.Equal(conv, m.RawMatrix().Data, "float64 unsafe i %d from %v", i, tmt.dt)
conv[0] = 1000
assert.Equal(conv, m.RawMatrix().Data, "float64 unsafe i %d from %v", i, tmt.dt)
conv[0] = 0 // reset for future tests that use the same backing
}
}
// idiocy test
T := New(Of(Float64), WithShape(2, 3, 4))
_, err := ToMat64(T)
if err == nil {
t.Error("Expected an error when trying to convert a 3-T to *mat.Dense")
}
}
func TestFromMat64(t *testing.T) {
assert := assert.New(t)
var m *mat.Dense
var T *Dense
var backing []float64
for i, tmt := range toMat64Tests {
backing = Range(Float64, 0, 6).([]float64)
m = mat.NewDense(2, 3, backing)
T = FromMat64(m)
conv := anyToFloat64s(tmt.data)
assert.Equal(conv, T.Float64s(), "test %d: []float64 from %v", i, tmt.dt)
assert.True(T.Shape().Eq(tmt.shape))
T = FromMat64(m, As(tmt.dt))
assert.Equal(tmt.data, T.Data())
assert.True(T.Shape().Eq(tmt.shape))
if tmt.dt == Float64 {
backing = Range(Float64, 0, 6).([]float64)
m = mat.NewDense(2, 3, backing)
T = FromMat64(m, UseUnsafe())
assert.Equal(backing, T.Float64s())
assert.True(T.Shape().Eq(tmt.shape))
backing[0] = 1000
assert.Equal(backing, T.Float64s(), "test %d - unsafe float64", i)
}
}
}