PageRenderTime 88ms CodeModel.GetById 9ms app.highlight 72ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llmath/tests/v4math_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 382 lines | 331 code | 24 blank | 27 comment | 124 complexity | 7aa006ba0845841a29780715609ddb93 MD5 | raw file
  1/**
  2 * @file v4math_test.cpp
  3 * @author Adroit
  4 * @date 2007-03
  5 * @brief v4math test cases.
  6 *
  7 * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28 
 29#include "linden_common.h"
 30#include "../test/lltut.h"
 31#include "llsd.h"
 32
 33#include "../m4math.h"
 34#include "../v4math.h"
 35#include "../llquaternion.h"
 36
 37namespace tut
 38{
 39	struct v4math_data
 40	{
 41	};
 42	typedef test_group<v4math_data> v4math_test;
 43	typedef v4math_test::object v4math_object;
 44	tut::v4math_test v4math_testcase("v4math_h");
 45
 46	template<> template<>
 47	void v4math_object::test<1>()
 48	{
 49		LLVector4 vec4;
 50		ensure("1:LLVector4:Fail to initialize " ,((0 == vec4.mV[VX]) && (0 == vec4.mV[VY]) && (0 == vec4.mV[VZ])&& (1.0f == vec4.mV[VW])));
 51		F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
 52		LLVector4 vec4a(x,y,z);
 53		ensure("2:LLVector4:Fail to initialize " ,((x == vec4a.mV[VX]) && (y == vec4a.mV[VY]) && (z == vec4a.mV[VZ])&& (1.0f == vec4a.mV[VW])));
 54		LLVector4 vec4b(x,y,z,w);
 55		ensure("3:LLVector4:Fail to initialize " ,((x == vec4b.mV[VX]) && (y == vec4b.mV[VY]) && (z == vec4b.mV[VZ])&& (w == vec4b.mV[VW])));
 56		const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
 57		LLVector4 vec4c(vec);
 58		ensure("4:LLVector4:Fail to initialize " ,((vec[0] == vec4c.mV[VX]) && (vec[1] == vec4c.mV[VY]) && (vec[2] == vec4c.mV[VZ])&& (vec[3] == vec4c.mV[VW])));
 59		LLVector3 vec3(-2.23f,1.01f,42.3f);
 60		LLVector4 vec4d(vec3);
 61		ensure("5:LLVector4:Fail to initialize " ,((vec3.mV[VX] == vec4d.mV[VX]) && (vec3.mV[VY] == vec4d.mV[VY]) && (vec3.mV[VZ] == vec4d.mV[VZ])&& (1.f == vec4d.mV[VW])));
 62		F32 w1 = -.234f;
 63		LLVector4 vec4e(vec3,w1);
 64		ensure("6:LLVector4:Fail to initialize " ,((vec3.mV[VX] == vec4e.mV[VX]) && (vec3.mV[VY] == vec4e.mV[VY]) && (vec3.mV[VZ] == vec4e.mV[VZ])&& (w1 == vec4e.mV[VW])));
 65	}
 66
 67	template<> template<>
 68	void v4math_object::test<2>()
 69	{
 70		F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
 71		LLVector4 vec4;
 72		vec4.setVec(x,y,z);
 73		ensure("1:setVec:Fail to initialize " ,((x == vec4.mV[VX]) && (y == vec4.mV[VY]) && (z == vec4.mV[VZ])&& (1.0f == vec4.mV[VW])));
 74		vec4.clearVec();
 75		ensure("2:clearVec:Fail " ,((0 == vec4.mV[VX]) && (0 == vec4.mV[VY]) && (0 == vec4.mV[VZ])&& (1.0f == vec4.mV[VW])));
 76		vec4.setVec(x,y,z,w);
 77		ensure("3:setVec:Fail to initialize " ,((x == vec4.mV[VX]) && (y == vec4.mV[VY]) && (z == vec4.mV[VZ])&& (w == vec4.mV[VW])));
 78		vec4.zeroVec();
 79		ensure("4:zeroVec:Fail " ,((0 == vec4.mV[VX]) && (0 == vec4.mV[VY]) && (0 == vec4.mV[VZ])&& (0 == vec4.mV[VW])));
 80		LLVector3 vec3(-2.23f,1.01f,42.3f);
 81		vec4.clearVec();
 82		vec4.setVec(vec3);
 83		ensure("5:setVec:Fail to initialize " ,((vec3.mV[VX] == vec4.mV[VX]) && (vec3.mV[VY] == vec4.mV[VY]) && (vec3.mV[VZ] == vec4.mV[VZ])&& (1.f == vec4.mV[VW])));
 84		F32 w1 = -.234f;
 85		vec4.zeroVec();
 86		vec4.setVec(vec3,w1);
 87		ensure("6:setVec:Fail to initialize " ,((vec3.mV[VX] == vec4.mV[VX]) && (vec3.mV[VY] == vec4.mV[VY]) && (vec3.mV[VZ] == vec4.mV[VZ])&& (w1 == vec4.mV[VW])));
 88		const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
 89		LLVector4 vec4a;
 90		vec4a.setVec(vec);
 91		ensure("7:setVec:Fail to initialize " ,((vec[0] == vec4a.mV[VX]) && (vec[1] == vec4a.mV[VY]) && (vec[2] == vec4a.mV[VZ])&& (vec[3] == vec4a.mV[VW])));
 92	}
 93
 94	template<> template<>
 95	void v4math_object::test<3>()
 96	{
 97		F32 x = 10.f, y = -2.3f, z = -.023f;
 98		LLVector4 vec4(x,y,z);
 99		ensure("magVec:Fail ", is_approx_equal(vec4.magVec(), (F32) sqrt(x*x + y*y + z*z)));
100		ensure("magVecSquared:Fail ", is_approx_equal(vec4.magVecSquared(), (x*x + y*y + z*z)));
101	}
102
103	template<> template<>
104	void v4math_object::test<4>()
105	{
106		F32 x = 10.f, y = -2.3f, z = -.023f;
107		LLVector4 vec4(x,y,z);
108		F32 mag = vec4.normVec();
109		mag = 1.f/ mag;
110		ensure("1:normVec: Fail " ,is_approx_equal(mag*x,vec4.mV[VX]) && is_approx_equal(mag*y, vec4.mV[VY])&& is_approx_equal(mag*z, vec4.mV[VZ]));
111		x = 0.000000001f, y = 0.000000001f, z = 0.000000001f;
112		vec4.clearVec();
113		vec4.setVec(x,y,z);
114		mag = vec4.normVec();
115		ensure("2:normVec: Fail " ,is_approx_equal(mag*x,vec4.mV[VX]) && is_approx_equal(mag*y, vec4.mV[VY])&& is_approx_equal(mag*z, vec4.mV[VZ]));
116	}
117
118	template<> template<>
119	void v4math_object::test<5>()
120	{
121		F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
122		LLVector4 vec4(x,y,z,w);
123		vec4.abs();
124		ensure("abs:Fail " ,((x == vec4.mV[VX]) && (-y == vec4.mV[VY]) && (-z == vec4.mV[VZ])&& (-w == vec4.mV[VW])));
125		vec4.clearVec();
126		ensure("isExactlyClear:Fail " ,(TRUE == vec4.isExactlyClear()));
127		vec4.zeroVec();
128		ensure("isExactlyZero:Fail " ,(TRUE == vec4.isExactlyZero()));
129	}
130
131	template<> template<>
132	void v4math_object::test<6>()
133	{
134		F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
135		LLVector4 vec4(x,y,z,w),vec4a;
136		vec4a = vec4.scaleVec(vec4);
137		ensure("scaleVec:Fail " ,(is_approx_equal(x*x, vec4a.mV[VX]) && is_approx_equal(y*y, vec4a.mV[VY]) && is_approx_equal(z*z, vec4a.mV[VZ])&& is_approx_equal(w*w, vec4a.mV[VW])));
138	}
139
140	template<> template<>
141	void v4math_object::test<7>()
142	{
143		F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
144		LLVector4 vec4(x,y,z,w);
145		ensure("1:operator [] failed " ,( x ==  vec4[0]));	
146		ensure("2:operator [] failed " ,( y ==  vec4[1]));
147		ensure("3:operator [] failed " ,( z ==  vec4[2]));
148		ensure("4:operator [] failed " ,( w ==  vec4[3]));
149		x = 23.f, y = -.2361f, z = 3.25;
150		vec4.setVec(x,y,z);
151		F32 &ref1 = vec4[0];
152		ensure("5:operator [] failed " ,( ref1 ==  vec4[0]));
153		F32 &ref2 = vec4[1];
154		ensure("6:operator [] failed " ,( ref2 ==  vec4[1]));
155		F32 &ref3 = vec4[2];
156		ensure("7:operator [] failed " ,( ref3 ==  vec4[2]));	
157		F32 &ref4 = vec4[3];
158		ensure("8:operator [] failed " ,( ref4 ==  vec4[3]));
159	}
160
161	template<> template<>
162	void v4math_object::test<8>()
163	{
164		F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
165		const  F32 val[16] = {
166            1.f,  2.f,   3.f,    0.f,
167            .34f, .1f,   -.5f,   0.f,
168            2.f,  1.23f, 1.234f, 0.f,
169            .89f, 0.f,   0.f,    0.f
170        };
171		LLMatrix4 mat(val);
172		LLVector4 vec4(x,y,z,w),vec4a;
173		vec4.rotVec(mat);
174		vec4a.setVec(x,y,z,w);
175		vec4a.rotVec(mat);
176		ensure_equals("1:rotVec: Fail " ,vec4a, vec4);
177		F32 a = 2.32f, b = -23.2f, c = -34.1112f, d = 1.010112f;
178		LLQuaternion q(a,b,c,d);
179		LLVector4 vec4b(a,b,c,d),vec4c;
180		vec4b.rotVec(q);
181		vec4c.setVec(a, b, c, d);
182		vec4c.rotVec(q);
183		ensure_equals("2:rotVec: Fail " ,vec4b, vec4c);
184	}
185	
186	template<> template<>
187	void v4math_object::test<9>()
188	{
189		F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
190		LLVector4 vec4(x,y,z,w),vec4a;;
191		std::ostringstream stream1, stream2;
192		stream1 << vec4;
193		vec4a.setVec(x,y,z,w);
194		stream2 << vec4a;
195		ensure("operator << failed",(stream1.str() == stream2.str()));	
196	}
197	
198	template<> template<>
199	void v4math_object::test<10>()
200	{
201		F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f, w1 = .23f;
202		F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f, w2 = 1.3f;
203		LLVector4 vec4(x1,y1,z1,w1),vec4a(x2,y2,z2,w2),vec4b;
204		vec4b = vec4a + vec4;
205		ensure("1:operator+:Fail to initialize " ,(is_approx_equal(x1+x2,vec4b.mV[VX]) && is_approx_equal(y1+y2,vec4b.mV[VY]) && is_approx_equal(z1+z2,vec4b.mV[VZ])));
206		x1 = -2.45f, y1 = 2.1f, z1 = 3.0f;
207		vec4.clearVec();
208		vec4a.clearVec();
209		vec4.setVec(x1,y1,z1);
210		vec4a +=vec4;
211		ensure_equals("2:operator+=: Fail to initialize", vec4a,vec4);
212		vec4a += vec4;
213		ensure("3:operator+=:Fail to initialize " ,(is_approx_equal(2*x1,vec4a.mV[VX]) && is_approx_equal(2*y1,vec4a.mV[VY]) && is_approx_equal(2*z1,vec4a.mV[VZ])));
214	}
215	template<> template<>
216	void v4math_object::test<11>()
217	{
218		F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f, w1 = .23f;
219		F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f, w2 = 1.3f;
220		LLVector4 vec4(x1,y1,z1,w1),vec4a(x2,y2,z2,w2),vec4b;
221		vec4b = vec4a - vec4;
222		ensure("1:operator-:Fail to initialize " ,(is_approx_equal(x2-x1,vec4b.mV[VX]) && is_approx_equal(y2-y1,vec4b.mV[VY]) && is_approx_equal(z2-z1,vec4b.mV[VZ])));
223		x1 = -2.45f, y1 = 2.1f, z1 = 3.0f;
224		vec4.clearVec();
225		vec4a.clearVec();
226		vec4.setVec(x1,y1,z1);
227		vec4a -=vec4;
228		ensure_equals("2:operator-=: Fail to initialize" , vec4a,-vec4);
229		vec4a -=vec4;
230		ensure("3:operator-=:Fail to initialize " ,(is_approx_equal(-2*x1,vec4a.mV[VX]) && is_approx_equal(-2*y1,vec4a.mV[VY]) && is_approx_equal(-2*z1,vec4a.mV[VZ])));
231	}
232
233	template<> template<>
234	void v4math_object::test<12>()
235	{
236		F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f;
237		F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f;
238		LLVector4 vec4(x1,y1,z1),vec4a(x2,y2,z2);
239		F32 res = vec4 * vec4a;
240		ensure("1:operator* failed " ,is_approx_equal(res, x1*x2 + y1*y2 + z1*z2));
241		vec4a.clearVec();
242		F32 mulVal = 4.2f;
243		vec4a = vec4 * mulVal;
244		ensure("2:operator* failed " ,is_approx_equal(x1*mulVal,vec4a.mV[VX]) && is_approx_equal(y1*mulVal, vec4a.mV[VY])&& is_approx_equal(z1*mulVal, vec4a.mV[VZ]));
245		vec4a.clearVec();
246		vec4a = mulVal *  vec4 ;
247		ensure("3:operator* failed " ,is_approx_equal(x1*mulVal, vec4a.mV[VX]) && is_approx_equal(y1*mulVal, vec4a.mV[VY])&& is_approx_equal(z1*mulVal, vec4a.mV[VZ]));
248		vec4 *= mulVal;
249		ensure("4:operator*= failed " ,is_approx_equal(x1*mulVal, vec4.mV[VX]) && is_approx_equal(y1*mulVal, vec4.mV[VY])&& is_approx_equal(z1*mulVal, vec4.mV[VZ]));
250	}
251
252	template<> template<>
253	void v4math_object::test<13>()
254	{
255		F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f;
256		F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f;
257		LLVector4 vec4(x1,y1,z1),vec4a(x2,y2,z2),vec4b;
258		vec4b = vec4 % vec4a;
259		ensure("1:operator% failed " ,is_approx_equal(y1*z2 - y2*z1, vec4b.mV[VX]) && is_approx_equal(z1*x2 -z2*x1, vec4b.mV[VY]) && is_approx_equal(x1*y2-x2*y1, vec4b.mV[VZ])); 
260		vec4 %= vec4a;
261		ensure_equals("operator%= failed " ,vec4,vec4b); 
262	}
263
264	template<> template<>
265	void v4math_object::test<14>()
266	{
267		F32 x = 1.f, y = 2.f, z = -1.1f,div = 4.2f;
268		F32 t = 1.f / div;
269		LLVector4 vec4(x,y,z), vec4a;
270		vec4a = vec4/div;
271		ensure("1:operator/ failed " ,is_approx_equal(x*t, vec4a.mV[VX]) && is_approx_equal(y*t, vec4a.mV[VY])&& is_approx_equal(z*t, vec4a.mV[VZ]));
272		x = 1.23f, y = 4.f, z = -2.32f;
273		vec4.clearVec();
274		vec4a.clearVec();
275		vec4.setVec(x,y,z);
276		vec4a = vec4/div;
277		ensure("2:operator/ failed " ,is_approx_equal(x*t, vec4a.mV[VX]) && is_approx_equal(y*t, vec4a.mV[VY])&& is_approx_equal(z*t, vec4a.mV[VZ]));
278		vec4 /= div;
279		ensure("3:operator/ failed " ,is_approx_equal(x*t, vec4.mV[VX]) && is_approx_equal(y*t, vec4.mV[VY])&& is_approx_equal(z*t, vec4.mV[VZ]));
280	}
281
282	template<> template<>
283	void v4math_object::test<15>()
284	{
285		F32 x = 1.f, y = 2.f, z = -1.1f;
286		LLVector4 vec4(x,y,z), vec4a;
287		ensure("operator!= failed " ,(vec4 != vec4a));
288		vec4a = vec4;
289		ensure("operator== failed " ,(vec4 ==vec4a)); 
290	}
291
292	template<> template<>
293	void v4math_object::test<16>()
294	{
295		F32 x = 1.f, y = 2.f, z = -1.1f;
296		LLVector4 vec4(x,y,z), vec4a;
297		vec4a = - vec4;
298		ensure("operator- failed " , (vec4 == - vec4a));	
299	}
300
301	template<> template<>
302	void v4math_object::test<17>()
303	{
304		F32 x = 1.f, y = 2.f, z = -1.1f,epsilon = .23425f;
305		LLVector4 vec4(x,y,z), vec4a(x,y,z);
306		ensure("1:are_parallel: Fail " ,(TRUE == are_parallel(vec4a,vec4,epsilon)));
307		x = 21.f, y = 12.f, z = -123.1f;
308		vec4a.clearVec();
309		vec4a.setVec(x,y,z);
310		ensure("2:are_parallel: Fail " ,(FALSE == are_parallel(vec4a,vec4,epsilon)));
311	}
312
313	template<> template<>
314	void v4math_object::test<18>()
315	{
316		F32 x = 1.f, y = 2.f, z = -1.1f;
317		F32 angle1, angle2;
318		LLVector4 vec4(x,y,z), vec4a(x,y,z);
319		angle1 = angle_between(vec4, vec4a);
320		vec4.normVec();
321		vec4a.normVec();
322		angle2 = acos(vec4 * vec4a);
323		ensure_approximately_equals("1:angle_between: Fail " ,angle1,angle2,8);
324		F32 x1 = 21.f, y1 = 2.23f, z1 = -1.1f;
325		LLVector4 vec4b(x,y,z), vec4c(x1,y1,z1);
326		angle1 = angle_between(vec4b, vec4c);
327		vec4b.normVec();
328		vec4c.normVec();
329		angle2 = acos(vec4b * vec4c);
330		ensure_approximately_equals("2:angle_between: Fail " ,angle1,angle2,8);
331	}
332
333	template<> template<>
334	void v4math_object::test<19>()
335	{
336		F32 x1 =-2.3f, y1 = 2.f,z1 = 1.2f, x2 = 1.3f, y2 = 1.f, z2 = 1.f;
337		F32 val1,val2;
338		LLVector4 vec4(x1,y1,z1),vec4a(x2,y2,z2);
339		val1 = dist_vec(vec4,vec4a);
340		val2 = (F32) sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2) + (z1 - z2)* (z1 -z2));
341		ensure_equals("dist_vec: Fail ",val2, val1);
342		val1 = dist_vec_squared(vec4,vec4a);
343		val2 =((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2) + (z1 - z2)* (z1 -z2));
344		ensure_equals("dist_vec_squared: Fail ",val2, val1);
345	}
346
347	template<> template<>
348	void v4math_object::test<20>()
349	{
350		F32 x1 =-2.3f, y1 = 2.f,z1 = 1.2f, w1 = -.23f, x2 = 1.3f, y2 = 1.f, z2 = 1.f,w2 = .12f;
351		F32 val = 2.3f,val1,val2,val3,val4;
352		LLVector4 vec4(x1,y1,z1,w1),vec4a(x2,y2,z2,w2);
353		val1 = x1 + (x2 - x1)* val;
354		val2 = y1 + (y2 - y1)* val;
355		val3 = z1 + (z2 - z1)* val;
356		val4 = w1 + (w2 - w1)* val;
357		LLVector4 vec4b = lerp(vec4,vec4a,val);
358		ensure("lerp failed", ((val1 ==vec4b.mV[VX])&& (val2 ==vec4b.mV[VY]) && (val3 ==vec4b.mV[VZ])&& (val4 ==vec4b.mV[VW])));	
359	}
360
361	template<> template<>
362	void v4math_object::test<21>()
363	{
364		F32 x = 1.f, y = 2.f, z = -1.1f;
365		LLVector4 vec4(x,y,z);
366		LLVector3 vec3 = vec4to3(vec4);
367		ensure("vec4to3 failed", ((x == vec3.mV[VX])&& (y == vec3.mV[VY]) && (z == vec3.mV[VZ])));	
368		LLVector4 vec4a = vec3to4(vec3);
369		ensure_equals("vec3to4 failed",vec4a,vec4);	
370	}
371
372	template<> template<>
373	void v4math_object::test<22>()
374	{
375		F32 x = 1.f, y = 2.f, z = -1.1f;
376		LLVector4 vec4(x,y,z);
377		LLSD llsd = vec4.getValue();
378		LLVector3 vec3(llsd);
379		LLVector4 vec4a = vec3to4(vec3);
380		ensure_equals("getValue failed",vec4a,vec4);	
381	}		
382}