forked from gazebosim/gz-math
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector4_TEST.rb
300 lines (234 loc) · 10.1 KB
/
Vector4_TEST.rb
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Copyright (C) 2016 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/usr/bin/env ruby
require 'test/unit/ui/console/testrunner'
require 'test/unit'
require 'math'
class Vector4_TEST < Test::Unit::TestCase
def test_construction
v = Ignition::Math::Vector4d.new
# ::operator== vector4
assert(true,
"Vector4d::Zero should equal [0, 0, 0, 0]")
# ::Distance, ::Length()
v.Set(1, 2, 3, 4)
assert(v.Length() == v.Distance(Ignition::Math::Vector4d.Zero),
"Vector4d::Lenth() should equal Vector4d::Distance(zero)")
# ::operator/ vector4
v.Set(4, 4, 4, 4)
v = v / Ignition::Math::Vector4d.new(1, 2, 2, 4)
assert(v == Ignition::Math::Vector4d.new(4, 2, 2, 1),
"v / Vector4d(1, 2, 2, 4) should equal Vector4d(4, 2, 2, 1)")
# ::operator / double
v = v / 2
assert(v == Ignition::Math::Vector4d.new(2, 1, 1, 0.5),
"v / 2 should equal Vector4d(2, 1, 1, .5)")
# ::operator * vector4
v = v * Ignition::Math::Vector4d.new(2, 3, 3, 4)
assert(v == Ignition::Math::Vector4d.new(4, 3, 3, 2),
"v * Vector4d(2, 3, 3, 4) should equal Vector4d(4, 3, 3, 2)")
# operator /=
v.Set(1, 2, 2, 4)
v /= Ignition::Math::Vector4d.new(1, 4, 8, 4)
assert(v == Ignition::Math::Vector4d.new(1, 0.5, 0.25, 1))
# operator *=
v.Set(1, 2, 2, 4)
v *= Ignition::Math::Vector4d.new(2, 0.5, 0.25, 0.1)
assert(v == Ignition::Math::Vector4d.new(2, 1, 0.5, 0.4))
# Test the static defines.
assert(Ignition::Math::Vector4d.Zero ==
Ignition::Math::Vector4d.new(0, 0, 0, 0),
"Vector4d::Zero should equal [0, 0, 0, 0]")
assert(Ignition::Math::Vector4d.One ==
Ignition::Math::Vector4d.new(1, 1, 1, 1),
"Vector4d::One should equal [1, 1, 1, 1]")
end
def test_distance
vec1 = Ignition::Math::Vector4d.new(0, 0, 0, 0)
vec2 = Ignition::Math::Vector4d.new(1, 2, 3, 4)
dist = vec1.Distance(vec2)
assert((dist - 5.47722557505).abs() < 1e-6,
"Vector4 distance should be near 5,47722557505")
end
def test_squared_length
vec1 = Ignition::Math::Vector4d.new(0, 0, 0, 0)
vec2 = Ignition::Math::Vector4d.new(1, 2, 3, 4)
sum1 = vec1.SquaredLength()
sum2 = vec2.SquaredLength()
assert(sum1 == 0, "Vector4 sum1 should equal 0")
assert(sum2 == 30, "Vector4 sum2 should equal 30")
end
def test_length
# Zero vector
assert(Ignition::Math::Vector4d.Zero.Length() == 0.0,
"Vector4 length of [0, 0, 0, 0] should equal 0")
assert(Ignition::Math::Vector4d.Zero.SquaredLength() == 0.0,
"Vector4 squared length of [0, 0, 0, 0] should equal 0")
# One vector
assert((Ignition::Math::Vector4d.One.Length() -
Math.sqrt(4.0)).abs() < 1e-10,
"Vector4 length of [1, 1, 1, 1] should equal sqrt(4.0)")
assert(Ignition::Math::Vector4d.One.SquaredLength() == 4.0,
"Vector4 squared lenght of [1, 1, 1, 1] should equal 4.0")
# Arbitrary vector
v = Ignition::Math::Vector4d.new(0.1, -4.2, 2.5, -1.2)
assert((v.Length() - 5.03388517946).abs() < 1e-10,
"Vector4 v length should equal 5.03388517946")
assert((v.SquaredLength() - 25.34).abs() < 1e-10 ,
"Vector4 v squared length should equal 25.34")
end
def test_normalize
vec1 = Ignition::Math::Vector4d.new(0, 0, 0, 0)
vec2 = Ignition::Math::Vector4d.new(1, 2, 3, 4)
vec3 = vec1
vec3.Normalize()
assert(vec3 == vec1, "Vector4 vec3 should equal vec1")
assert(vec1 == Ignition::Math::Vector4d.Zero,
"Vector4 should equal [0, 0, 0, 0]")
vec3 = vec2
vec2.Normalize()
assert(vec2.Equal(Ignition::Math::Vector4d.new(0.182575, 0.365150, 0.547725, 0.730300), 1e-5),
"Vector4 vec3 should equal [0.182575, 0.365150, 0.547725, 0.730300]")
end
def test_add
vec1 = Ignition::Math::Vector4d.new(0.1, 0.2, 0.4, 0.8)
vec2 = Ignition::Math::Vector4d.new(1.1, 2.2, 3.4, 4.3)
vec3 = vec1
vec3 += vec2
assert(vec1 + vec2 == Ignition::Math::Vector4d.new(1.2, 2.4, 3.8, 5.1),
"Vector4 vec1 + vec2 should equal [1.2, 2.4, 3.8, 4.9]")
assert(vec3 == Ignition::Math::Vector4d.new(1.2, 2.4, 3.8, 5.1),
"Vector4 vec3 should equal [1.2, 2.4, 3.8, 4.9]")
# Addition with zeros
# Scalar left and right
assert(vec1 + 0 == vec1, "Vector4 vec1+0 should equal vec1")
# Vector left and right
assert(Ignition::Math::Vector4d.Zero + vec1 == vec1,
"Vector4 Zero + vec1 should equal vec1")
assert(vec1 + Ignition::Math::Vector4d.Zero == vec1,
"Vector4 vec1 + Zero should equal vec1")
# Addition assignment
vec4 = vec1
vec4 += 0
assert(vec4 == vec1, "Vector4 vec4 should equal vec1")
vec4 += Ignition::Math::Vector4d.Zero
assert(vec4 == vec1, "Vector4 vec4 should equal vec1")
# Add non-trivial scalar values left and right
assert(vec1 + 2.5 == Ignition::Math::Vector4d.new(2.6, 2.7, 2.9, 3.3),
"Vector4 vec1 + 2.5 should equal [2.6, 2.7, 2.9, 3.3]")
vec1 = vec4
vec4 += 2.5
assert(vec4 == Ignition::Math::Vector4d.new(2.6, 2.7, 2.9, 3.3),
"Vector4 vec4 should equal [2.6, 2.7, 2.9, 3.3]")
end
def test_sub
vec1 = Ignition::Math::Vector4d.new(0.1, 0.2, 0.4, 0.8)
vec2 = Ignition::Math::Vector4d.new(1.1, 2.2, 3.4, 4.3)
vec3 = vec2
vec3 -= vec1
assert(vec2 - vec1 === Ignition::Math::Vector4d.new(1.0, 2.0, 3.0, 3.5),
"Vector4 vec2 - vec1 should equal [1.0, 2.0, 3.0, 3.5]")
assert(vec3 == Ignition::Math::Vector4d.new(1.0, 2.0, 3.0, 3.5),
"Vector4 vec3 should equal [1.0, 2.0, 3.0, 3.5]")
# Subtraction with zeros
# Scalar left and right
assert(vec1 - 0 == vec1, "Vector4 vec1 - 0 should equal vec1")
# Vector left and right
assert(Ignition::Math::Vector4d.Zero - vec1 == -vec1,
"Vector4 Zero - vec1 should equal -vec1")
assert(vec1 - Ignition::Math::Vector4d.Zero == vec1,
"Vector4 vec1 - Zero should equal vec1")
# Subtraction assignment
vec4 = vec1
vec4 -= 0
assert(vec4 == vec1, "Vector4 vec4 should equal vec1")
vec4 -= Ignition::Math::Vector4d.Zero
assert(vec4 == vec1, "Vector4 vec4 should equal vec1")
# Subtract non-trivial scalar values left and right
assert(vec1 - 2.5 == -Ignition::Math::Vector4d.new(2.4, 2.3, 2.1, 1.7),
"Vecetor3 vec1 - 2.5 should equal [2.4, 2.3, 2.1, 1.7]")
vec4 = vec1
vec4 -= 2.5
assert(vec4 == -Ignition::Math::Vector4d.new(2.4, 2.3, 2.1, 1.7),
"Vector4 vec4 - 2.5 should equal [2.4, 2.3, 2.1, 1.7]")
end
def test_divide
vec1 = Ignition::Math::Vector4d.new(0.1, 0.2, 0.4, 0.8)
vec3 = vec1 / 2.0
assert(vec3 == Ignition::Math::Vector4d.new(0.05, 0.1, 0.2, 0.4),
"Vector4 vec3 should equal [0.05, 0.1, 0.2, 0.4]")
vec3 /= 4.0
assert(vec3 == Ignition::Math::Vector4d.new(0.0125, 0.025, 0.05, 0.1),
"Vector4 vec3 should qual [0.0125, 0.025, 0.05, 0.1]")
end
def test_multiply
v = Ignition::Math::Vector4d.new(0.1, 0.2, 0.3, 0.4)
vec3 = v * 2.0
assert(vec3 == Ignition::Math::Vector4d.new(0.2, 0.4, 0.6, 0.8),
"Vector4 vec3 should equal[0.2, 0.4, 0.6, 0.8]")
vec3 *= 4.0
assert(vec3 == Ignition::Math::Vector4d.new(0.8, 1.6, 2.4, 3.2),
"Vector4 vec3 should equal [0.8, 1.6, 2.4, 3.2]")
# Multiply by zero
# Scalar left and right
assert(v * 0 == Ignition::Math::Vector4d.Zero,
"Vector4 v * 0 should equal Zero")
# Element-wise vector multiplication
assert(v * Ignition::Math::Vector4d.Zero == Ignition::Math::Vector4d.Zero,
"Vector4 v * Zero should equal Zero")
# Multiply by one
# Scalar left and right
assert(v * 1 == v, "Vector4 v * 1 should equal v")
# Element-wise vector multiplication
assert(v * Ignition::Math::Vector4d.One == v,
"Vector4 v * One should equal v")
# Multiply by non-trivial scalar value
scalar = 2.5
expect = Ignition::Math::Vector4d.new(0.25, 0.5, 0.75, 1.0)
assert(v * scalar == expect,
"Vector4 v * scalar should equal [0.25, 0.5, 0.75, 1.0]")
# Multiply by itself element-wise
assert(v*v == Ignition::Math::Vector4d.new(0.01, 0.04, 0.09, 0.16),
"Vector4 v * v should euqal [0.01, 0.04, 0.09, 0.16]")
end
def test_not_equal
vec1 = Ignition::Math::Vector4d.new(0.1, 0.2, 0.3, 0.4)
vec2 = Ignition::Math::Vector4d.new(0.2, 0.2, 0.3, 0.4)
vec3 = Ignition::Math::Vector4d.new(0.1, 0.2, 0.3, 0.4)
assert(vec1 != vec2, "Vector4 vec1 should not equal vec2")
assert(!(vec1 != vec3), "Vector4 vec1 should equal vec3" )
end
def test_equal
assert(!Ignition::Math::Vector4d.Zero.Equal(
Ignition::Math::Vector4d.One, 1e-6),
"Vector4 Zero should not equal 1 with tolerance of 1e-6")
assert(!Ignition::Math::Vector4d.Zero.Equal(
Ignition::Math::Vector4d.One, 1e-3),
"Vector4 Zero should not equal 1 with tolerance of 1e-3")
assert(!Ignition::Math::Vector4d.Zero.Equal(
Ignition::Math::Vector4d.One, 1e-1),
"Vector4 Zero should not equal 1 with tolerance of 1e-1")
assert(Ignition::Math::Vector4d.Zero.Equal(
Ignition::Math::Vector4d.One, 1),
"Vector4 Zero should equal 1 with tolerance of 1")
assert(Ignition::Math::Vector4d.Zero.Equal(
Ignition::Math::Vector4d.One, 1.1),
"Vector4 Zero should equal 1 with tolerance of 1.1")
end
def test_finite
vec1 = Ignition::Math::Vector4d.new(0.1, 0.2, 0.3, 0.4)
assert(vec1.IsFinite(), "Vector4 vec1 should be be finite")
end
end
exit Test::Unit::UI::Console::TestRunner.run(Vector4_TEST).passed? ? 0 : -1