Skip to content

Commit

Permalink
🎨 Standarize code ./spec directory
Browse files Browse the repository at this point in the history
  • Loading branch information
WaKeMaTTa committed Apr 3, 2019
1 parent e717871 commit 9a8c821
Show file tree
Hide file tree
Showing 23 changed files with 2,049 additions and 2,103 deletions.
609 changes: 305 additions & 304 deletions spec/00_nmatrix_spec.rb

Large diffs are not rendered by default.

77 changes: 38 additions & 39 deletions spec/01_enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
# Enumerator tests for NMatrix. These should load early, as they
# test functionality essential to matrix printing.
#
require 'spec_helper'
require "spec_helper"

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] }
let(:m) { n[1..4, 1..3] }

if stype == :yale
it "should iterate properly along each row of a slice" do
Expand All @@ -41,30 +41,30 @@
jj = []
m.extend NMatrix::YaleFunctions
m.each_row do |row|
row.each_with_indices do |v,i,j|
row.each_with_indices do |v, i, j|
vv << v
ii << i
jj << j
end
end

expect(vv).to eq([7,8,9, 12,13,0, 0,0,0, 0,17,18])
expect(ii).to eq([0]*12)
expect(jj).to eq([0,1,2]*4)
expect(vv).to eq([7, 8, 9, 12, 13, 0, 0, 0, 0, 0, 17, 18])
expect(ii).to eq([0] * 12)
expect(jj).to eq([0, 1, 2] * 4)
end

it "should iterate along diagonal portion of A array" do
pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby?
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])
expect(ii).to eq([0,1,2,3,4])
expect(vv).to eq([1, 7, 13, 0, 19])
expect(ii).to eq([0, 1, 2, 3, 4])
expect(jj).to eq(ii)
end

Expand All @@ -73,31 +73,31 @@
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

expect(vv).to eq([2,3,4,5, 6,8,9,10, 11,12,14,15, 16,17,18,20])
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])
expect(vv).to eq([2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 20])
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
pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby?
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])
expect(ii).to eq([1,2,3])
expect(jj).to eq([0,1,2])
expect(vv).to eq([7, 13, 0])
expect(ii).to eq([1, 2, 3])
expect(jj).to eq([0, 1, 2])
end

it "should iterate along a sliced non-diagonal portion of a sliced A array" do
Expand All @@ -107,80 +107,79 @@
jj = []
n.extend NMatrix::YaleFunctions
m.extend NMatrix::YaleFunctions
m.send :__yale_stored_nondiagonal_each_with_indices__ do |v,i,j|
m.send :__yale_stored_nondiagonal_each_with_indices__ do |v, i, j|
vv << v
ii << i
jj << j
end

expect(ii).to eq([0,0, 1, 3,3 ])
expect(jj).to eq([1,2, 0, 1,2 ])
expect(vv).to eq([8,9, 12, 17,18])
expect(ii).to eq([0, 0, 1, 3, 3])
expect(jj).to eq([1, 2, 0, 1, 2])
expect(vv).to eq([8, 9, 12, 17, 18])
end

it "should visit each stored element of the matrix in order by indices" do
pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby?
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

expect(vv).to eq([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 16, 17, 18, 19, 20])
expect(ii).to eq([[0]*5, [1]*5, [2]*5, [3]*1, [4]*5].flatten)
expect(jj).to eq([0,1,2,3,4, 0,1,2,3,5, 0,1,2,4,5, 3, 0,2,3,4,5])
expect(ii).to eq([[0] * 5, [1] * 5, [2] * 5, [3] * 1, [4] * 5].flatten)
expect(jj).to eq([0, 1, 2, 3, 4, 0, 1, 2, 3, 5, 0, 1, 2, 4, 5, 3, 0, 2, 3, 4, 5])
end

it "should visit each stored element of the slice in order by indices" do
pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby?
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 ])
expect(jj).to eq([0,1,2, 0,1, 2, 1,2 ])
expect(vv).to eq([7,8,9, 12,13, 0, 17,18 ])
expect(ii).to eq([0, 0, 0, 1, 1, 2, 3, 3])
expect(jj).to eq([0, 1, 2, 0, 1, 2, 1, 2])
expect(vv).to eq([7, 8, 9, 12, 13, 0, 17, 18])
end
end

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

expect(vv).to eq([1,2,3,4,5,0,6,7,8,9,0,10,11,12,13,0,14,15,0,0,0,0,0,0,16,0,17,18,19,20])
expect(ii).to eq([[0]*6, [1]*6, [2]*6, [3]*6, [4]*6].flatten)
expect(jj).to eq([0,1,2,3,4,5]*5)
expect(vv).to eq([1, 2, 3, 4, 5, 0, 6, 7, 8, 9, 0, 10, 11, 12, 13, 0, 14, 15, 0, 0, 0, 0, 0, 0, 16, 0, 17, 18, 19, 20])
expect(ii).to eq([[0] * 6, [1] * 6, [2] * 6, [3] * 6, [4] * 6].flatten)
expect(jj).to eq([0, 1, 2, 3, 4, 5] * 5)
end

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)
expect(ii).to eq([[0]*3, [1]*3, [2]*3, [3]*3].flatten)
expect(vv).to eq([7,8,9,12,13,0,0,0,0,0,17,18])

expect(jj).to eq([0, 1, 2] * 4)
expect(ii).to eq([[0] * 3, [1] * 3, [2] * 3, [3] * 3].flatten)
expect(vv).to eq([7, 8, 9, 12, 13, 0, 0, 0, 0, 0, 17, 18])
end

if stype == :list or stype == :dense then
if (stype == :list) || (stype == :dense)
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)
Expand Down
Loading

0 comments on commit 9a8c821

Please sign in to comment.