diff options
author | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-01-27 14:34:03 +0000 |
---|---|---|
committer | mame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2010-01-27 14:34:03 +0000 |
commit | 5f7f5098a64f7651d8e120b54f00f3b0be1ad66a (patch) | |
tree | 8add462d54930e3f72d5a93e6bee8d18e5d68046 /test/matrix/test_vector.rb | |
parent | 0aafb0b4b645bf41540bc2f625d040d63bd06baf (diff) |
* test/matrix/test_matrix.rb, test/matrix/test_vector.rb: add some
tests.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26450 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/matrix/test_vector.rb')
-rw-r--r-- | test/matrix/test_vector.rb | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/test/matrix/test_vector.rb b/test/matrix/test_vector.rb index 95a39693fc..9be54af4ea 100644 --- a/test/matrix/test_vector.rb +++ b/test/matrix/test_vector.rb @@ -46,4 +46,106 @@ class TestVector < Test::Unit::TestCase assert_equal @v1.hash, @v2.hash assert_equal @v1.hash, @v3.hash end + + def test_aref + assert_equal(1, @v1[0]) + assert_equal(2, @v1[1]) + assert_equal(3, @v1[2]) + assert_equal(3, @v1[-1]) + assert_equal(nil, @v1[3]) + end + + def test_size + assert_equal(3, @v1.size) + end + + def test_each2 + a = [] + @v1.each2(@v4) {|x, y| a << [x, y] } + assert_equal([[1,1.0],[2,2.0],[3,3.0]], a) + end + + def test_collect + a = @v1.collect {|x| x + 1 } + assert_equal(Vector[2,3,4], a) + end + + def test_collect2 + a = @v1.collect2(@v4) {|x, y| x + y } + assert_equal([2.0,4.0,6.0], a) + end + + def test_map2 + a = @v1.map2(@v4) {|x, y| x + y } + assert_equal(Vector[2.0,4.0,6.0], a) + end + + def test_compare_by + assert(@v1.compare_by([1,2,3], :==)) + assert(!@v1.compare_by([1,2,3], :equal?)) + end + + def test_mul + assert_equal(Vector[2,4,6], @v1 * 2) + assert_equal(Matrix[[1, 4, 9], [2, 8, 18], [3, 12, 27]], @v1 * Matrix[[1,4,9]]) + assert_raise(Matrix::ErrOperationNotDefined) { @v1 * Vector[1,4,9] } + o = Object.new + def o.coerce(x) + [1, 1] + end + assert_equal(1, Vector[1, 2, 3] * o) + end + + def test_add + assert_equal(Vector[2,4,6], @v1 + @v1) + assert_equal(Matrix[[2],[6],[12]], @v1 + Matrix[[1],[4],[9]]) + o = Object.new + def o.coerce(x) + [1, 1] + end + assert_equal(2, Vector[1, 2, 3] + o) + end + + def test_sub + assert_equal(Vector[0,0,0], @v1 - @v1) + assert_equal(Matrix[[0],[-2],[-6]], @v1 - Matrix[[1],[4],[9]]) + o = Object.new + def o.coerce(x) + [1, 1] + end + assert_equal(0, Vector[1, 2, 3] - o) + end + + def test_inner_product + assert_equal(1+4+9, @v1.inner_product(@v1)) + end + + def test_r + assert_equal(5, Vector[3, 4].r) + end + + def test_covector + assert_equal(Matrix[[1,2,3]], @v1.covector) + end + + def test_elements_to_f + assert_equal(Vector[0.5,1.0,1.5], @v1.elements_to_f * Rational(1, 2)) + end + + def test_elements_to_i + assert_equal(Vector[0,1,1], (@v1.elements_to_f * Rational(1, 2)).elements_to_i) + end + + def test_elements_to_r + assert_equal(@v1.collect {|x| Rational(x, 2) }, @v1.elements_to_r * Rational(1, 2)) + end + + def test_to_s + assert_equal("Vector[1, 2, 3]", @v1.to_s) + end + + def test_inspect + assert_equal("Vector[1, 2, 3]", @v1.inspect) + end + end |