forked from qri-io/matlab
-
Notifications
You must be signed in to change notification settings - Fork 3
/
matlab_test.go
90 lines (79 loc) · 2.15 KB
/
matlab_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
package matlab
import (
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewFileFromReader(t *testing.T) {
qm7, err := os.Open("testdata/qm7.mat")
if err != nil {
t.Fatal(err.Error())
}
defer qm7.Close()
f, err := NewFileFromReader(qm7)
if err != nil {
t.Log(f.Header.String())
t.Fatal(err.Error())
}
expect := "MATLAB 5.0 MAT-file, Platform: posix, Created on: Mon Feb 18 17:12:08 2013"
if f.Header.String() != expect {
t.Errorf("header mismatch. expected:\n%s\ngot:\n%s", expect, f.Header.String())
}
}
func TestReadElement(t *testing.T) {
qm7, err := os.Open("testdata/qm7.mat")
if err != nil {
t.Fatal(err.Error())
}
defer qm7.Close()
f, err := NewFileFromReader(qm7)
if err != nil {
t.Log(f.Header.String())
t.Fatal(err.Error())
}
vars := f.GetVarsNames()
assert.Len(t, vars, 5)
assert.Subset(t, vars, strings.Split("XRZTP", ""))
r, hasVar := f.GetVar("R")
assert.True(t, hasVar)
assert.Equal(t, r.Dimension, []int32{7165, 23, 3})
}
func TestMixedCells(t *testing.T) {
qm7, err := os.Open("testdata/mixedCells.mat")
if err != nil {
t.Fatal(err.Error())
}
defer qm7.Close()
f, err := NewFileFromReader(qm7)
assert.NoError(t, err)
vars := f.GetVarsNames()
assert.Len(t, vars, 1)
assert.Subset(t, vars, strings.Split("Z", ""))
r, hasVar := f.GetVar("Z")
assert.True(t, hasVar)
assert.Equal(t, []int32{1, 2}, r.Dimension)
assert.Equal(t, []rune("someString"), r.GetAtLocation(0).(*Matrix).String())
assert.Equal(t, []float64{123.0}, r.GetAtLocation(1).(*Matrix).DoubleArray())
assert.Nil(t, r.GetAtLocation(100))
}
func TestStruct(t *testing.T) {
file, err := os.Open("testdata/simpleStruct.mat")
if err != nil {
t.Fatal(err.Error())
}
defer file.Close()
f, err := NewFileFromReader(file)
assert.NoError(t, err)
vars := f.GetVarsNames()
assert.Len(t, vars, 1)
assert.Subset(t, vars, strings.Split("X", ""))
r, hasVar := f.GetVar("X")
assert.True(t, hasVar)
assert.Equal(t, []int32{1, 1}, r.Dimension)
s := r.Struct()
assert.Equal(t, 1.0, s["w"].GetAtLocation(0))
assert.Empty(t, s["w"].Name)
assert.Equal(t, 2.0, s["y"].GetAtLocation(0))
assert.Equal(t, []rune("abc"), s["z"].String())
}