diff options
Diffstat (limited to 'spec/ruby/library/matrix/shared')
-rw-r--r-- | spec/ruby/library/matrix/shared/collect.rb | 26 | ||||
-rw-r--r-- | spec/ruby/library/matrix/shared/conjugate.rb | 20 | ||||
-rw-r--r-- | spec/ruby/library/matrix/shared/determinant.rb | 38 | ||||
-rw-r--r-- | spec/ruby/library/matrix/shared/equal_value.rb | 33 | ||||
-rw-r--r-- | spec/ruby/library/matrix/shared/identity.rb | 19 | ||||
-rw-r--r-- | spec/ruby/library/matrix/shared/imaginary.rb | 20 | ||||
-rw-r--r-- | spec/ruby/library/matrix/shared/inverse.rb | 38 | ||||
-rw-r--r-- | spec/ruby/library/matrix/shared/rectangular.rb | 18 | ||||
-rw-r--r-- | spec/ruby/library/matrix/shared/trace.rb | 12 | ||||
-rw-r--r-- | spec/ruby/library/matrix/shared/transpose.rb | 19 |
10 files changed, 243 insertions, 0 deletions
diff --git a/spec/ruby/library/matrix/shared/collect.rb b/spec/ruby/library/matrix/shared/collect.rb new file mode 100644 index 0000000000..256cd6a190 --- /dev/null +++ b/spec/ruby/library/matrix/shared/collect.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../fixtures/classes', __FILE__) +require 'matrix' + +describe :collect, shared: true do + before :all do + @m = Matrix[ [1, 2], [1, 2] ] + end + + it "returns an instance of Matrix" do + @m.send(@method){|n| n * 2 }.should be_kind_of(Matrix) + end + + it "returns a Matrix where each element is the result of the block" do + @m.send(@method) { |n| n * 2 }.should == Matrix[ [2, 4], [2, 4] ] + end + + it "returns an enumerator if no block is given" do + @m.send(@method).should be_an_instance_of(Enumerator) + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.ins.send(@method){1}.should be_an_instance_of(MatrixSub) + end + end +end diff --git a/spec/ruby/library/matrix/shared/conjugate.rb b/spec/ruby/library/matrix/shared/conjugate.rb new file mode 100644 index 0000000000..180ff4fa98 --- /dev/null +++ b/spec/ruby/library/matrix/shared/conjugate.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../fixtures/classes', __FILE__) +require 'matrix' + +describe :matrix_conjugate, shared: true do + it "returns a matrix with all entries 'conjugated'" do + Matrix[ [1, 2], [3, 4] ].send(@method).should == Matrix[ [1, 2], [3, 4] ] + Matrix[ [1.9, Complex(1,1)], [3, 4] ].send(@method).should == Matrix[ [1.9, Complex(1,-1)], [3, 4] ] + end + + it "returns empty matrices on the same size if empty" do + Matrix.empty(0, 3).send(@method).should == Matrix.empty(0, 3) + Matrix.empty(3, 0).send(@method).should == Matrix.empty(3, 0) + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.ins.send(@method).should be_an_instance_of(MatrixSub) + end + end +end diff --git a/spec/ruby/library/matrix/shared/determinant.rb b/spec/ruby/library/matrix/shared/determinant.rb new file mode 100644 index 0000000000..47a58c62a6 --- /dev/null +++ b/spec/ruby/library/matrix/shared/determinant.rb @@ -0,0 +1,38 @@ +require 'matrix' + +describe :determinant, shared: true do + it "returns the determinant of a square Matrix" do + m = Matrix[ [7,6], [3,9] ] + m.send(@method).should == 45 + + m = Matrix[ [9, 8], [6,5] ] + m.send(@method).should == -3 + + m = Matrix[ [9,8,3], [4,20,5], [1,1,1] ] + m.send(@method).should == 95 + end + + it "returns the determinant of a single-element Matrix" do + m = Matrix[ [2] ] + m.send(@method).should == 2 + end + + it "returns 1 for an empty Matrix" do + m = Matrix[ ] + m.send(@method).should == 1 + end + + it "returns the determinant even for Matrices containing 0 as first entry" do + Matrix[[0,1],[1,0]].send(@method).should == -1 + end + + it "raises an error for rectangular matrices" do + lambda { + Matrix[[1], [2], [3]].send(@method) + }.should raise_error(Matrix::ErrDimensionMismatch) + + lambda { + Matrix.empty(3,0).send(@method) + }.should raise_error(Matrix::ErrDimensionMismatch) + end +end diff --git a/spec/ruby/library/matrix/shared/equal_value.rb b/spec/ruby/library/matrix/shared/equal_value.rb new file mode 100644 index 0000000000..e2102e823a --- /dev/null +++ b/spec/ruby/library/matrix/shared/equal_value.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../fixtures/classes', __FILE__) +require 'matrix' + +describe :equal, shared: true do + before do + @matrix = Matrix[ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6] ] + end + + it "returns true for self" do + @matrix.send(@method, @matrix).should be_true + end + + it "returns true for equal matrices" do + @matrix.send(@method, Matrix[ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6] ]).should be_true + end + + it "returns false for different matrices" do + @matrix.send(@method, Matrix[ [42, 2, 3, 4, 5], [2, 3, 4, 5, 6] ]).should be_false + @matrix.send(@method, Matrix[ [1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7] ]).should be_false + @matrix.send(@method, Matrix[ [1, 2, 3], [2, 3, 4] ]).should be_false + end + + it "returns false for different empty matrices" do + Matrix.empty(42, 0).send(@method, Matrix.empty(6, 0)).should be_false + Matrix.empty(0, 42).send(@method, Matrix.empty(0, 6)).should be_false + Matrix.empty(0, 0).send(@method, Matrix.empty(6, 0)).should be_false + Matrix.empty(0, 0).send(@method, Matrix.empty(0, 6)).should be_false + end + + it "doesn't distinguish on subclasses" do + MatrixSub.ins.send(@method, Matrix.I(2)).should be_true + end +end diff --git a/spec/ruby/library/matrix/shared/identity.rb b/spec/ruby/library/matrix/shared/identity.rb new file mode 100644 index 0000000000..20b35ae8e3 --- /dev/null +++ b/spec/ruby/library/matrix/shared/identity.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../fixtures/classes', __FILE__) +require 'matrix' + +describe :matrix_identity, shared: true do + it "returns a Matrix" do + Matrix.send(@method, 2).should be_kind_of(Matrix) + end + + it "returns a n x n identity matrix" do + Matrix.send(@method, 3).should == Matrix.scalar(3, 1) + Matrix.send(@method, 100).should == Matrix.scalar(100, 1) + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.send(@method, 2).should be_an_instance_of(MatrixSub) + end + end +end diff --git a/spec/ruby/library/matrix/shared/imaginary.rb b/spec/ruby/library/matrix/shared/imaginary.rb new file mode 100644 index 0000000000..61a65a62ec --- /dev/null +++ b/spec/ruby/library/matrix/shared/imaginary.rb @@ -0,0 +1,20 @@ +require File.expand_path('../../fixtures/classes', __FILE__) +require 'matrix' + +describe :matrix_imaginary, shared: true do + it "returns a matrix with the imaginary part of the elements of the receiver" do + Matrix[ [1, 2], [3, 4] ].send(@method).should == Matrix[ [0, 0], [0, 0] ] + Matrix[ [1.9, Complex(1,1)], [Complex(-2,0.42), 4] ].send(@method).should == Matrix[ [0, 1], [0.42, 0] ] + end + + it "returns empty matrices on the same size if empty" do + Matrix.empty(0, 3).send(@method).should == Matrix.empty(0, 3) + Matrix.empty(3, 0).send(@method).should == Matrix.empty(3, 0) + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.ins.send(@method).should be_an_instance_of(MatrixSub) + end + end +end diff --git a/spec/ruby/library/matrix/shared/inverse.rb b/spec/ruby/library/matrix/shared/inverse.rb new file mode 100644 index 0000000000..c6996df4a3 --- /dev/null +++ b/spec/ruby/library/matrix/shared/inverse.rb @@ -0,0 +1,38 @@ +require File.expand_path('../../fixtures/classes', __FILE__) +require 'matrix' + +describe :inverse, shared: true do + + it "returns a Matrix" do + Matrix[ [1,2], [2,1] ].send(@method).should be_an_instance_of(Matrix) + end + + it "returns the inverse of the Matrix" do + Matrix[ + [1, 3, 3], [1, 4, 3], [1, 3, 4] + ].send(@method).should == + Matrix[ + [7, -3, -3], [-1, 1, 0], [-1, 0, 1] + ] + end + + it "returns the inverse of the Matrix (other case)" do + Matrix[ + [1, 2, 3], [0, 1, 4], [5, 6, 0] + ].send(@method).should be_close_to_matrix([ + [-24, 18, 5], [20, -15, -4], [-5, 4, 1] + ]) + end + + it "raises a ErrDimensionMismatch if the Matrix is not square" do + lambda{ + Matrix[ [1,2,3], [1,2,3] ].send(@method) + }.should raise_error(Matrix::ErrDimensionMismatch) + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.ins.send(@method).should be_an_instance_of(MatrixSub) + end + end +end diff --git a/spec/ruby/library/matrix/shared/rectangular.rb b/spec/ruby/library/matrix/shared/rectangular.rb new file mode 100644 index 0000000000..4206311586 --- /dev/null +++ b/spec/ruby/library/matrix/shared/rectangular.rb @@ -0,0 +1,18 @@ +require File.expand_path('../../fixtures/classes', __FILE__) +require 'matrix' + +describe :matrix_rectangular, shared: true do + it "returns [receiver.real, receiver.imag]" do + m = Matrix[ [1.2, Complex(1,2)], [Complex(-2,0.42), 4] ] + m.send(@method).should == [m.real, m.imag] + + m = Matrix.empty(3, 0) + m.send(@method).should == [m.real, m.imag] + end + + describe "for a subclass of Matrix" do + it "returns instances of that subclass" do + MatrixSub.ins.send(@method).each{|m| m.should be_an_instance_of(MatrixSub) } + end + end +end diff --git a/spec/ruby/library/matrix/shared/trace.rb b/spec/ruby/library/matrix/shared/trace.rb new file mode 100644 index 0000000000..2a42839f5d --- /dev/null +++ b/spec/ruby/library/matrix/shared/trace.rb @@ -0,0 +1,12 @@ +require 'matrix' + +describe :trace, shared: true do + it "returns the sum of diagonal elements in a square Matrix" do + Matrix[[7,6], [3,9]].trace.should == 16 + end + + it "returns the sum of diagonal elements in a rectangular Matrix" do + lambda{ Matrix[[1,2,3], [4,5,6]].trace}.should raise_error(Matrix::ErrDimensionMismatch) + end + +end diff --git a/spec/ruby/library/matrix/shared/transpose.rb b/spec/ruby/library/matrix/shared/transpose.rb new file mode 100644 index 0000000000..dba6c71041 --- /dev/null +++ b/spec/ruby/library/matrix/shared/transpose.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../fixtures/classes', __FILE__) +require 'matrix' + +describe :matrix_transpose, shared: true do + it "returns a transposed matrix" do + Matrix[[1, 2], [3, 4], [5, 6]].send(@method).should == Matrix[[1, 3, 5], [2, 4, 6]] + end + + it "can transpose empty matrices" do + m = Matrix[[], [], []] + m.send(@method).send(@method).should == m + end + + describe "for a subclass of Matrix" do + it "returns an instance of that subclass" do + MatrixSub.ins.send(@method).should be_an_instance_of(MatrixSub) + end + end +end |