forked from translunar/nmatrix
-
Notifications
You must be signed in to change notification settings - Fork 133
/
io_spec.rb
137 lines (114 loc) · 3.87 KB
/
io_spec.rb
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# = NMatrix
#
# A linear algebra library for scientific computation in Ruby.
# NMatrix is part of SciRuby.
#
# NMatrix was originally inspired by and derived from NArray, by
# Masahiro Tanaka: http://narray.rubyforge.org
#
# == Copyright Information
#
# SciRuby is Copyright (c) 2010 - 2012, Ruby Science Foundation
# NMatrix is Copyright (c) 2012, Ruby Science Foundation
#
# Please see LICENSE.txt for additional copyright notices.
#
# == Contributing
#
# By contributing source code to SciRuby, you agree to be bound by
# our Contributor Agreement:
#
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
#
# == io_spec.rb
#
# Basic tests for NMatrix::IO.
#
require "./lib/nmatrix"
describe NMatrix::IO do
it "repacks a string" do
NMatrix::IO::Matlab.repack("hello", :miUINT8, :dtype => :byte).should == "hello"
end
it "creates yale from internal byte-string function" do
n = NMatrix.new(:yale, [4,4], :byte, "\0\1\3\3\4", "\0\1\3\0\0\0\0\0\0\0\0", "\2\3\5\4", :byte)
n[0,0].should == 2
n[1,1].should == 3
n[1,3].should == 5
n[3,0].should == 4
n[2,2].should == 0
n[3,3].should == 0
end
it "reads MATLAB .mat file containing a single square sparse matrix" do
# Note: same matrix as above
n = NMatrix::IO::Matlab.load_mat("spec/4x4_sparse.mat")
n[0,0].should == 2
n[1,1].should == 3
n[1,3].should == 5
n[3,0].should == 4
n[2,2].should == 0
n[3,3].should == 0
end
it "reads MATLAB .mat file containing a single dense integer matrix" do
n = NMatrix::IO::Matlab.load_mat("spec/4x5_dense.mat")
m = NMatrix.new(:dense, [4,5], [16,17,18,19,20,15,14,13,12,11,6,7,8,9,10,5,4,3,2,1])
n.should == m
end
it "reads MATLAB .mat file containing a single dense double matrix" do
n = NMatrix::IO::Matlab.load_mat("spec/2x2_dense_double.mat")
m = NMatrix.new(:dense, 2, [1.1, 2.0, 3.0, 4.0], :float64)
n.should == m
end
it "loads and saves MatrixMarket .mtx file containing a single large sparse double matrix" do
pending "spec disabled because it's so slow"
n = NMatrix::IO::Market.load("spec/utm5940.mtx")
NMatrix::IO::Market.save(n, "spec/utm5940.saved.mtx")
`wc -l spec/utm5940.mtx`.split[0].should == `wc -l spec/utm5940.saved.mtx`.split[0]
end
it "raises an error when reading a non-existent file" do
fn = rand(10000000).to_i.to_s
while File.exist?(fn)
fn = rand(10000000).to_i.to_s
end
expect{ NMatrix.read(fn) }.to raise_error(Errno::ENOENT)
end
it "reads and writes NMatrix dense" do
n = NMatrix.new(:dense, [4,3], [0,1,2,3,4,5,6,7,8,9,10,11], :int32)
n.write("test-out")
m = NMatrix.read("test-out")
n.should == m
end
it "reads and writes NMatrix dense as symmetric" do
n = NMatrix.new(:dense, 3, [0,1,2,1,3,4,2,4,5], :int16)
n.write("test-out", :symmetric)
m = NMatrix.read("test-out")
n.should == m
end
it "reads and writes NMatrix dense as skew" do
n = NMatrix.new(:dense, 3, [0,1,2,-1,3,4,-2,-4,5], :float64)
n.write("test-out", :skew)
m = NMatrix.read("test-out")
n.should == m
end
it "reads and writes NMatrix dense as hermitian" do
n = NMatrix.new(:dense, 3, [0,1,2,1,3,4,2,4,5], :complex64)
n.write("test-out", :hermitian)
m = NMatrix.read("test-out")
n.should == m
end
it "reads and writes NMatrix dense as upper" do
n = NMatrix.new(:dense, 3, [-1,1,2,3,4,5,6,7,8], :int32)
n.write("test-out", :upper)
m = NMatrix.new(:dense, 3, [-1,1,2,0,4,5,0,0,8], :int32) # lower version of the same
o = NMatrix.read("test-out")
o.should == m
o.should_not == n
end
it "reads and writes NMatrix dense as lower" do
n = NMatrix.new(:dense, 3, [-1,1,2,3,4,5,6,7,8], :int32)
n.write("test-out", :lower)
m = NMatrix.new(:dense, 3, [-1,0,0,3,4,0,6,7,8], :int32) # lower version of the same
o = NMatrix.read("test-out")
o.should == m
o.should_not == n
end
end