Permalink
Browse files

getting tests all set up

  • Loading branch information...
1 parent ef035d0 commit 455c4e47b641da9f7c3412d4fd8b663476eeccbc @hmcfletch committed May 24, 2011
View
@@ -1,2 +1,7 @@
require 'bundler'
Bundler::GemHelper.install_tasks
+
+desc "Run all tests"
+task :test do
+ sh "ruby test/creation_test.rb"
+end
@@ -327,6 +327,59 @@ def column_nz(j) # :yield: e
end
end
+ #
+ # Returns a matrix that is the result of iteration of the given block over all
+ # elements of the matrix.
+ # Matrix[ [1,2], [3,4] ].collect { |e| e**2 }
+ # => 1 4
+ # 9 16
+ #
+ def collect(&block) # :yield: e
+ raise "NOT IMPLEMENTED"
+ return to_enum(:collect) unless block_given?
+ rows = @rows.collect{|row| row.collect(&block)}
+ new_matrix rows, column_size
+ end
+ alias map collect
+
+ #
+ # Yields all elements of the matrix, starting with those of the first row,
+ # or returns an Enumerator is no block given
+ # Matrix[ [1,2], [3,4] ].each { |e| puts e }
+ # # => prints the numbers 1 to 4
+ #
+ def each(&block) # :yield: e
+ raise "NOT IMPLEMENTED"
+ return to_enum(:each) unless block_given?
+ @rows.each do |row|
+ row.each(&block)
+ end
+ self
+ end
+
+ #
+ # Yields all elements of the matrix, starting with those of the first row,
+ # along with the row index and column index,
+ # or returns an Enumerator is no block given
+ # Matrix[ [1,2], [3,4] ].each_with_index do |e, row, col|
+ # puts "#{e} at #{row}, #{col}"
+ # end
+ # # => 1 at 0, 0
+ # # => 2 at 0, 1
+ # # => 3 at 1, 0
+ # # => 4 at 1, 1
+ #
+ def each_with_index(&block) # :yield: e, row, column
+ raise "NOT IMPLEMENTED"
+ return to_enum(:each_with_index) unless block_given?
+ @rows.each_with_index do |row, row_index|
+ row.each_with_index do |e, col_index|
+ yield e, row_index, col_index
+ end
+ end
+ self
+ end
+
#--
# TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#++
@@ -107,7 +107,6 @@ def each(&block)
raise "NOT IMPLEMENTED" unless block_given?
# return to_enum(:each) unless block_given?
size.times.each { |i| yield @elements[i] }
- end
self
end
@@ -251,7 +250,7 @@ def map2(v, &block) # :yield: e1, e2
#++
#
- # Returns +true+ iff the two vectors have the same elements in the same order.
+ # Returns +true+ iff the two sparse vectors have the same elements in the same order.
#
def ==(other)
return false unless SparseVector === other
@@ -264,14 +263,14 @@ def eql?(other)
end
#
- # Return a copy of the vector.
+ # Return a copy of the sparse vector.
#
def clone
SparseVector.elements(@elements)
end
#
- # Return a hash-code for the vector.
+ # Return a hash-code for the sparse vector.
#
def hash
@elements.hash
@@ -397,7 +396,7 @@ def inner_product(v)
# Vector[5,8,2].r => 9.643650761
#
def r
- Math.sqrt(@elements.keys.inject(0) { |v, e| v + ( @elements[e] * @elements[e] ) } )
+ Math.sqrt(@elements.values.inject(0) { |v, e| v + ( e * e ) } )
end
#--
@@ -408,7 +407,6 @@ def r
# Creates a single-row sparse matrix from this sparse vector.
#
def covector
- # raise "NOT IMPLEMENTED"
SparseMatrix.row_vector(self)
end
View
@@ -17,4 +17,6 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
+
+ s.add_development_dependency('test-unit', '~> 2.2.0')
end
View
@@ -0,0 +1,49 @@
+# require 'test_helper'
+require File.expand_path( File.join( File.dirname( __FILE__ ), 'test_helper' ) )
+
+# add somce accessor so we can check under the hood
+class SparseVector
+ def elems; @elements end
+end
+
+class SparseMatrix
+ def elems; @rows end
+end
+
+class TestCreation < Test::Unit::TestCase
+
+ def setup
+ # just in case
+ end
+
+ def test_sv_creation_array
+ sv1 = SparseVector[1,0,2,3,0,0]
+ assert sv1.size == 6
+ assert sv1.elems == { 0 => 1, 2 => 2, 3 => 3 }
+ assert sv1.elems.default == 0
+ end
+
+ def test_sv_creation_elements
+ sv1 = SparseVector.elements([0,3,6,2,0,0])
+ assert sv1.size == 6
+ assert sv1.elems == { 1 => 3, 2 => 6, 3 => 2 }
+ assert sv1.elems.default == 0
+
+ sv2 = SparseVector.elements({ 2 => 3, 5 => 1, 6 => 7 })
+ assert sv2.size == 7
+ assert sv2.elems == { 2 => 3, 5 => 1, 6 => 7 }
+ assert sv2.elems.default == 0
+
+ sv3 = SparseVector.elements(Vector[0,2,4,0,0,1,0,0])
+ assert sv3.size == 8
+ assert sv3.elems == { 1 => 2, 2 => 4, 5 => 1 }
+ assert sv3.elems.default == 0
+ end
+
+ def test_sm_creation_array
+ end
+
+ def test_sm_creation_rows_and_columns
+ end
+
+end
View
@@ -0,0 +1,4 @@
+require 'test/unit'
+
+require File.expand_path( File.join( File.dirname( __FILE__ ), '..', 'lib', 'sparse-matrix' ) )
+# require '../lib/sparse-matrix'

0 comments on commit 455c4e4

Please sign in to comment.