1require 'test/unit'
2require 'matrix'
3
4class TestVector < Test::Unit::TestCase
5  def setup
6    @v1 = Vector[1,2,3]
7    @v2 = Vector[1,2,3]
8    @v3 = @v1.clone
9    @v4 = Vector[1.0, 2.0, 3.0]
10    @w1 = Vector[2,3,4]
11  end
12
13  def test_identity
14    assert_same @v1, @v1
15    assert_not_same @v1, @v2
16    assert_not_same @v1, @v3
17    assert_not_same @v1, @v4
18    assert_not_same @v1, @w1
19  end
20
21  def test_equality
22    assert_equal @v1, @v1
23    assert_equal @v1, @v2
24    assert_equal @v1, @v3
25    assert_equal @v1, @v4
26    assert_not_equal @v1, @w1
27  end
28
29  def test_hash_equality
30    assert @v1.eql?(@v1)
31    assert @v1.eql?(@v2)
32    assert @v1.eql?(@v3)
33    assert !@v1.eql?(@v4)
34    assert !@v1.eql?(@w1)
35
36    hash = { @v1 => :value }
37    assert hash.key?(@v1)
38    assert hash.key?(@v2)
39    assert hash.key?(@v3)
40    assert !hash.key?(@v4)
41    assert !hash.key?(@w1)
42  end
43
44  def test_hash
45    assert_equal @v1.hash, @v1.hash
46    assert_equal @v1.hash, @v2.hash
47    assert_equal @v1.hash, @v3.hash
48  end
49
50  def test_aref
51    assert_equal(1, @v1[0])
52    assert_equal(2, @v1[1])
53    assert_equal(3, @v1[2])
54    assert_equal(3, @v1[-1])
55    assert_equal(nil, @v1[3])
56  end
57
58  def test_size
59    assert_equal(3, @v1.size)
60  end
61
62  def test_each2
63    a = []
64    @v1.each2(@v4) {|x, y| a << [x, y] }
65    assert_equal([[1,1.0],[2,2.0],[3,3.0]], a)
66  end
67
68  def test_collect
69    a = @v1.collect {|x| x + 1 }
70    assert_equal(Vector[2,3,4], a)
71  end
72
73  def test_collect2
74    a = @v1.collect2(@v4) {|x, y| x + y }
75    assert_equal([2.0,4.0,6.0], a)
76  end
77
78  def test_map2
79    a = @v1.map2(@v4) {|x, y| x + y }
80    assert_equal(Vector[2.0,4.0,6.0], a)
81  end
82
83  def test_mul
84    assert_equal(Vector[2,4,6], @v1 * 2)
85    assert_equal(Matrix[[1, 4, 9], [2, 8, 18], [3, 12, 27]], @v1 * Matrix[[1,4,9]])
86    assert_raise(Matrix::ErrOperationNotDefined) { @v1 * Vector[1,4,9] }
87    o = Object.new
88    def o.coerce(x)
89      [1, 1]
90    end
91    assert_equal(1, Vector[1, 2, 3] * o)
92  end
93
94  def test_add
95    assert_equal(Vector[2,4,6], @v1 + @v1)
96    assert_equal(Matrix[[2],[6],[12]], @v1 + Matrix[[1],[4],[9]])
97    o = Object.new
98    def o.coerce(x)
99      [1, 1]
100    end
101    assert_equal(2, Vector[1, 2, 3] + o)
102  end
103
104  def test_sub
105    assert_equal(Vector[0,0,0], @v1 - @v1)
106    assert_equal(Matrix[[0],[-2],[-6]], @v1 - Matrix[[1],[4],[9]])
107    o = Object.new
108    def o.coerce(x)
109      [1, 1]
110    end
111    assert_equal(0, Vector[1, 2, 3] - o)
112  end
113
114  def test_inner_product
115    assert_equal(1+4+9, @v1.inner_product(@v1))
116  end
117
118  def test_r
119    assert_equal(5, Vector[3, 4].r)
120  end
121
122  def test_covector
123    assert_equal(Matrix[[1,2,3]], @v1.covector)
124  end
125
126  def test_to_s
127    assert_equal("Vector[1, 2, 3]", @v1.to_s)
128  end
129
130  def test_inspect
131    assert_equal("Vector[1, 2, 3]", @v1.inspect)
132  end
133
134  def test_magnitude
135    assert_in_epsilon(3.7416573867739413, @v1.norm)
136    assert_in_epsilon(3.7416573867739413, @v4.norm)
137  end
138
139  def test_complex_magnitude
140    bug6966 = '[ruby-dev:46100]'
141    v = Vector[Complex(0,1), 0]
142    assert_equal(1.0, v.norm, bug6966)
143  end
144
145  def test_rational_magnitude
146    v = Vector[Rational(1,2), 0]
147    assert_equal(0.5, v.norm)
148  end
149end
150