forked from translunar/nmatrix
-
Notifications
You must be signed in to change notification settings - Fork 133
/
nmatrix_list_spec.rb
113 lines (100 loc) · 2.85 KB
/
nmatrix_list_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
# = 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
#
# == nmatrix_list_spec.rb
#
# Basic tests for NMatrix's list-of-lists storage type.
#
require "./lib/nmatrix"
describe NMatrix do
context :list do
it "should compare with ==" do
n = NMatrix.new(:list, [3,3,3], :int64)
m = NMatrix.new(:list, [3,3,3], :int64)
n.should == m
n[0,0,0] = 5
n.should_not == m
n[0,0,1] = 52
n[1,2,1] = -4
m[0,0,0] = 5
m[0,0,1] = 52
m[1,2,1] = -4
n.should == m
end
it "should compare 0-1 matrices with different default values with ==" do
n = NMatrix.new(:list, 2, 0, :object)
n[0,1] = 1
n[1,0] = 1
m = NMatrix.new(:list, 2, 1, :object)
m[0,0] = 0
m[1,1] = 0
n.should == m
end
it "should allow creation of both true- and false-based matrix" do
r1 = NMatrix.new(:list, 2, false, :object)
r2 = NMatrix.new(:list, 2, true, :object)
r1.should_not == r2
end
it "should handle missing default value" do
NMatrix.new(:list, 3, :int8)[0,0].should == 0
NMatrix.new(:list, 4, :float64)[0,0].should == 0.0
end
it "should allow conversion to a Ruby Hash" do
n = NMatrix.new(:list, 3, 1, :int64)
n[0,1] = 50
h = n.to_h
h.size.should == 1
h[0].size.should == 1
h[0][1].should == 50
h[0][2].should == 1
h[1][0].should == 1
end
##TODO: Make this test better. It's not nearly exhaustive enough as is.
it "should handle recursive removal" do
n = NMatrix.new(:list, [3,3,3], 0)
n[0,0,0] = 2
n[1,1,1] = 1
n[1,0,0] = 3
n[0,0,1] = 4
n[0,0,0].should == 2
n[1,1,1].should == 1
n[1,0,0].should == 3
n[0,0,1].should == 4
n[1,1,1] = 0
n[0,0,0].should == 2
n[1,1,1].should == 0
n[1,0,0].should == 3
n[0,0,1].should == 4
end
it "should correctly insert a value between the middle and last entries of a three-element list" do
n = NMatrix.new(:list, 5940, 0, :float64)
n[0,0] = -7.0710685196786e-01
n[330,0] = 7.0710685196786e-01
n[1,0] = -7.0710685196786e-01
n[2,0] = -7.0710685196786e-01
n[0,0].should_not == 0
n[330,0].should_not == 0
n[2,0].should_not == 0
n[1,0].should_not == 0
end
end
end