spec/01_enum_spec.rb in nmatrix-0.1.0.rc3 vs spec/01_enum_spec.rb in nmatrix-0.1.0.rc4
- old
+ new
@@ -23,33 +23,25 @@
# == 01_enum_spec.rb
#
# Enumerator tests for NMatrix. These should load early, as they
# test functionality essential to matrix printing.
#
+require 'spec_helper'
-require File.dirname(__FILE__) + "/spec_helper.rb"
-
describe "NMatrix enumeration for" do
[:dense, :yale, :list].each do |stype|
context stype do
+ let(:n) { create_rectangular_matrix(stype) }
+ let(:m) { n[1..4,1..3] }
- before :each do
- @n = create_rectangular_matrix(stype)
- @m = @n[1..4,1..3]
- end
-
- #after :each do
- # GC.start
- #end
-
if stype == :yale
it "should iterate properly along each row of a slice" do
vv = []
ii = []
jj = []
- @m.extend NMatrix::YaleFunctions
- @m.each_row do |row|
+ m.extend NMatrix::YaleFunctions
+ m.each_row do |row|
row.each_with_indices do |v,i,j|
vv << v
ii << i
jj << j
end
@@ -62,11 +54,11 @@
it "should iterate along diagonal portion of A array" do
vv = []
ii = []
jj = []
- @n.send :__yale_stored_diagonal_each_with_indices__ do |v,i,j|
+ n.send :__yale_stored_diagonal_each_with_indices__ do |v,i,j|
vv << v
ii << i
jj << j
end
expect(vv).to eq([1,7,13,0,19])
@@ -76,11 +68,11 @@
it "should iterate along non-diagonal portion of A array" do
vv = []
ii = []
jj = []
- @n.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j|
+ n.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j|
vv << v
ii << i
jj << j
end
@@ -88,15 +80,15 @@
expect(ii).to eq([[0]*4, [1]*4, [2]*4, [4]*4].flatten)
expect(jj).to eq([1,2,3,4, 0,2,3,5, 0,1,4,5, 0,2,3,5])
end
it "should iterate along a sliced diagonal portion of an A array" do
- @m = @n[0..3,1..3]
+ m = n[0..3,1..3]
vv = []
ii = []
jj = []
- @m.send :__yale_stored_diagonal_each_with_indices__ do |v,i,j|
+ m.send :__yale_stored_diagonal_each_with_indices__ do |v,i,j|
vv << v
ii << i
jj << j
end
expect(vv).to eq([7,13,0])
@@ -106,13 +98,13 @@
it "should iterate along a sliced non-diagonal portion of a sliced A array" do
vv = []
ii = []
jj = []
- @n.extend NMatrix::YaleFunctions
- @m.extend NMatrix::YaleFunctions
- @m.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j|
+ n.extend NMatrix::YaleFunctions
+ m.extend NMatrix::YaleFunctions
+ m.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j|
vv << v
ii << i
jj << j
end
@@ -123,11 +115,11 @@
it "should visit each stored element of the matrix in order by indices" do
vv = []
ii = []
jj = []
- @n.each_ordered_stored_with_indices do |v,i,j|
+ n.each_ordered_stored_with_indices do |v,i,j|
vv << v
ii << i
jj << j
end
@@ -139,11 +131,11 @@
it "should visit each stored element of the slice in order by indices" do
vv = []
ii = []
jj = []
- @m.each_ordered_stored_with_indices do |v,i,j|
+ m.each_ordered_stored_with_indices do |v,i,j|
vv << v
ii << i
jj << j
end
expect(ii).to eq([0,0,0, 1,1, 2, 3,3 ])
@@ -154,11 +146,11 @@
it "should visit each cell in the matrix as if dense, making indices available" do
vv = []
ii = []
jj = []
- @n.each_with_indices do |v,i,j|
+ n.each_with_indices do |v,i,j|
vv << v
ii << i
jj << j
end
@@ -169,11 +161,11 @@
it "should visit each cell in the slice as if dense, making indices available" do
vv = []
ii = []
jj = []
- @m.each_with_indices do |v,i,j|
+ m.each_with_indices do |v,i,j|
vv << v
ii << i
jj << j
end
expect(jj).to eq([0,1,2]*4)
@@ -181,10 +173,10 @@
expect(vv).to eq([7,8,9,12,13,0,0,0,0,0,17,18])
end
if stype == :list or stype == :dense then
- it "should correctly map to a matrix with a single element" do
+ it "should correctly map to a matrix with a single element" do
nm = N.new([1], [2.0], stype: stype)
expect(nm.map { |e| e**2 }).to eq N.new([1], [4.0], stype: stype)
end
it "should correctly map to a matrix with multiple elements" do