File: vector_test.rb

package info (click to toggle)
ruby-gsl 1.14.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,116 kB
  • sloc: ansic: 62,186; ruby: 17,804; makefile: 18; sh: 15
file content (141 lines) | stat: -rwxr-xr-x 4,489 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env ruby

require("gsl")
require("test/unit")

class VectorTest < Test::Unit::TestCase
	def test_vector_get
		v = GSL::Vector::Int.indgen(5)
		assert_equal(GSL::Vector::Int[3, 1, 2], v.get([3, 1, 2]))
	end
	
	def test_vector_addsub
		a = GSL::Vector::Int[2, 5, 4]
		b = GSL::Vector::Int[10, 30, 20]
		c = GSL::Vector::Int[12, 35, 24]
		d = GSL::Vector::Int[8, 25, 16]
		assert_equal(c, a+b)
		assert_equal(d, b-a)		
	end
	
	def test_vector_collect
		v = GSL::Vector::Int.indgen(5)
		u = GSL::Vector::Int[0, 1, 4, 9, 16]
		w = v.collect { |val| val*val }
		assert_equal(u, w)
	end

	def test_vector_ispos_neg
		v = GSL::Vector::Int.indgen(5)
		assert_equal(v.ispos, 0)
		assert_equal(v.ispos?, false)		
		assert_equal(v.isneg, 0)
		assert_equal(v.isneg?, false)
		
		v += 1
		assert_equal(v.ispos, 1)
		assert_equal(v.ispos?, true)		
		assert_equal(v.isneg, 0)
		assert_equal(v.isneg?, false)		
		
		v -= 100
		assert_equal(v.ispos, 0)
		assert_equal(v.ispos?, false)		
		assert_equal(v.isneg, 1)
		assert_equal(v.isneg?, true)				
	end
		
	def test_vector_isnonneg
		v = GSL::Vector::Int.indgen(5)
		assert_equal(v.isnonneg, 1)
		assert_equal(v.isnonneg?, true)		
		assert_equal(v.isneg, 0)
		assert_equal(v.isneg?, false)
		
		v -= 100
		assert_equal(v.isnonneg, 0)
		assert_equal(v.isnonneg?, false)		
		assert_equal(v.isneg, 1)
		assert_equal(v.isneg?, true)		
		
		v += 200
		assert_equal(v.isnonneg, 1)
		assert_equal(v.isnonneg?, true)		
		assert_equal(v.ispos, 1)
		assert_equal(v.ispos?, true)				
	end

  def test_vector_subvector
    v = GSL::Vector::Int.indgen(12)

    # args = []
    vv = v.subvector
    assert_not_equal(v.object_id, vv.object_id)
    assert_equal(v.subvector, v)

    # args = [Fixnum]
    vv = v.subvector(3)
    assert_equal([0, 1, 2], vv.to_a)
    assert_nothing_raised("subvector(-1)") {v.subvector(-1)}
    vv = v.subvector(-1)
    assert_equal([11], vv.to_a)
    vv = v.subvector(-2)
    assert_equal([10, 11], vv.to_a)
    assert_raise(RangeError) {v.subvector(-13)}

    # args = [Fixnum, Fixnum]
    vv = v.subvector(2, 3)
    assert_equal([2, 3, 4], vv.to_a)

    vv = v.subvector(-4, 3)
    assert_equal([8, 9, 10], vv.to_a)
    assert_nothing_raised("subvector(-4, -3)") {v.subvector(-4, -3)}
    vv = v.subvector(-4, -3)
    assert_equal([8, 7, 6], vv.to_a)
    assert_raise(GSL::ERROR::EINVAL) {v.subvector(-11, -3)}

    # args = [Fixnum, Fixnum, Fixnum]
    vv = v.subvector(1, 3, 4)
    assert_equal([1, 4, 7, 10], vv.to_a)

    # args = [Range]
    tests = {
    # ( range ) => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
      ( 1..  3) => [   1, 2, 3                          ],                                   
      ( 1... 3) => [   1, 2                             ],
      ( 3..  1) => [   3, 2, 1                          ],
      ( 3... 1) => [      3, 2                          ],
      (-7..  9) => [               5, 6, 7, 8, 9        ],
      (-7... 9) => [               5, 6, 7, 8           ],
      ( 4.. -3) => [            4, 5, 6, 7, 8, 9        ],
      ( 4...-3) => [            4, 5, 6, 7, 8           ],
      ( 2.. -2) => [      2, 3, 4, 5, 6, 7, 8, 9, 10    ],
      ( 2...-2) => [      2, 3, 4, 5, 6, 7, 8, 9        ],
      (-2..  2) => [     10, 9, 8, 7, 6, 5, 4, 3,  2    ],
      (-2... 2) => [     10, 9, 8, 7, 6, 5, 4, 3        ],
      (-3.. -1) => [                           9, 10, 11],
      (-3...-1) => [                           9, 10    ],
      (-1.. -3) => [                          11, 10,  9],
      (-1...-3) => [                          11, 10    ],
      # Add more test cases here...
    }
    tests.each do |r, x|
      assert_nothing_raised("subvector(#{r})") {v.subvector(r)}
      assert_equal(x, v.subvector(r).to_a, "subvector(#{r})")
    end

    # args = [Range, Fixnum]
    tests = {
    # [( range ), s] => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
      [( 1..  6), 2] => [   1,    3,    5                    ],
      [( 1... 6), 2] => [   1,    3,    5                    ],
      [( 0..  6), 3] => [0,       3,      6                  ],
      [( 0... 6), 3] => [0,    3                             ],
      # Add more test cases here...
    }
    tests.each do |(r,s), x|
      assert_nothing_raised("subvector(#{r},#{s})") {v.subvector(r)}
      assert_equal(x, v.subvector(r,s).to_a, "subvector(#{r},#{s})")
    end
  end
end