PageRenderTime 93ms CodeModel.GetById 15ms app.highlight 74ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/tests/v2math_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 448 lines | 365 code | 56 blank | 27 comment | 102 complexity | 63e005f2ecd6236bb4dbbc3dc015ee04 MD5 | raw file
  1/**
  2 * @file v2math_test.cpp
  3 * @author Adroit
  4 * @date 2007-02
  5 * @brief v2math 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
 32#include "../v2math.h"
 33
 34
 35namespace tut
 36{
 37	struct v2math_data
 38	{
 39	};
 40	typedef test_group<v2math_data> v2math_test;
 41	typedef v2math_test::object v2math_object;
 42	tut::v2math_test v2math_testcase("v2math_h");
 43
 44	template<> template<>
 45	void v2math_object::test<1>()
 46	{
 47		LLVector2 vec2;
 48		ensure("LLVector2:Fail to initialize ", (0.f == vec2.mV[VX] && 0.f == vec2.mV[VY]));
 49
 50		F32 x =2.0f, y = 3.2f ;
 51		LLVector2 vec3(x,y);
 52		ensure("LLVector2(F32 x, F32 y):Fail to initialize ", (x == vec3.mV[VX]) && (y == vec3.mV[VY]));
 53
 54		const F32 vec[2] = {3.2f, 4.5f};
 55		LLVector2 vec4(vec);
 56		ensure("LLVector2(const F32 *vec):Fail to initialize ", (vec[0] == vec4.mV[VX]) && (vec[1] == vec4.mV[VY]));
 57
 58		vec4.clearVec();
 59		ensure("clearVec():Fail to clean the values ", (0.f == vec4.mV[VX] && 0.f == vec4.mV[VY]));
 60
 61		vec3.zeroVec();
 62		ensure("zeroVec():Fail to fill the zero ", (0.f == vec3.mV[VX] && 0.f == vec3.mV[VY]));
 63	}
 64
 65	template<> template<>
 66	void v2math_object::test<2>()
 67	{
 68		F32 x = 123.356f, y = 2387.453f;
 69		LLVector2 vec2,vec3;
 70		vec2.setVec(x, y);
 71		ensure("1:setVec: Fail  ", (x == vec2.mV[VX]) && (y == vec2.mV[VY]));
 72
 73		vec3.setVec(vec2);
 74		ensure("2:setVec: Fail   " ,(vec2 == vec3));
 75
 76		vec3.zeroVec();
 77		const F32 vec[2] = {3.24653f, 457653.4f};
 78		vec3.setVec(vec);
 79		ensure("3:setVec: Fail  ", (vec[0] == vec3.mV[VX]) && (vec[1] == vec3.mV[VY]));
 80	}
 81
 82	template<> template<>
 83	void v2math_object::test<3>()
 84	{
 85		F32 x = 2.2345f, y = 3.5678f ;
 86		LLVector2 vec2(x,y);
 87		ensure("magVecSquared:Fail ", is_approx_equal(vec2.magVecSquared(), (x*x + y*y)));
 88		ensure("magVec:Fail ", is_approx_equal(vec2.magVec(), (F32) sqrt(x*x + y*y)));
 89	}
 90
 91	template<> template<>
 92	void v2math_object::test<4>()
 93	{
 94		F32 x =-2.0f, y = -3.0f ;
 95		LLVector2 vec2(x,y);
 96		ensure_equals("abs():Fail", vec2.abs(), TRUE);
 97		ensure("abs() x", is_approx_equal(vec2.mV[VX], 2.f));
 98		ensure("abs() y", is_approx_equal(vec2.mV[VY], 3.f));
 99
100		ensure("isNull():Fail ", FALSE == vec2.isNull());	//Returns TRUE if vector has a _very_small_ length
101
102		x =.00000001f, y = .000001001f;
103		vec2.setVec(x, y);
104		ensure("isNull(): Fail ", TRUE == vec2.isNull());	
105	}
106
107	template<> template<>
108	void v2math_object::test<5>()
109	{
110		F32 x =1.f, y = 2.f;
111		LLVector2 vec2(x, y), vec3;
112		vec3 = vec3.scaleVec(vec2);
113		ensure("scaleVec: Fail ", vec3.mV[VX] == 0. && vec3.mV[VY] == 0.);
114		ensure("isExactlyZero(): Fail", TRUE == vec3.isExactlyZero());
115
116		vec3.setVec(2.f, 1.f);
117		vec3 = vec3.scaleVec(vec2);
118		ensure("scaleVec: Fail ", (2.f == vec3.mV[VX]) && (2.f == vec3.mV[VY]));
119		ensure("isExactlyZero():Fail", FALSE == vec3.isExactlyZero());
120	}
121
122	template<> template<>
123	void v2math_object::test<6>()
124	{
125		F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
126		F32 val1, val2;
127		LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
128		vec4 = vec2 + vec3 ;
129		val1 = x1+x2;
130		val2 = y1+y2;
131		ensure("1:operator+ failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY]))); 
132
133		vec2.clearVec();
134		vec3.clearVec();
135		x1 = -.235f, y1 = -24.32f,  x2 = -2.3f, y2 = 1.f;
136		vec2.setVec(x1, y1);
137		vec3.setVec(x2, y2);
138		vec4 = vec2 + vec3;
139		val1 = x1+x2;
140		val2 = y1+y2;
141		ensure("2:operator+ failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY]))); 
142	}
143
144	template<> template<>
145	void v2math_object::test<7>()
146	{
147		F32 x1 =1.f, y1 = 2.f,  x2 = -2.3f, y2 = 1.11f;
148		F32 val1, val2;
149		LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
150		vec4 = vec2 - vec3 ;
151		val1 = x1-x2;
152		val2 = y1-y2;
153		ensure("1:operator- failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY]))); 
154
155		vec2.clearVec();
156		vec3.clearVec();
157		vec4.clearVec();
158		x1 = -.235f, y1 = -24.32f,  x2 = -2.3f, y2 = 1.f;
159		vec2.setVec(x1, y1);
160		vec3.setVec(x2, y2);
161		vec4 = vec2 - vec3;
162		val1 = x1-x2;
163		val2 = y1-y2;
164		ensure("2:operator- failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY]))); 
165	}
166
167	template<> template<>
168	void v2math_object::test<8>()
169	{
170		F32 x1 =1.f, y1 = 2.f,  x2 = -2.3f, y2 = 1.11f;
171		F32 val1, val2;
172		LLVector2 vec2(x1, y1), vec3(x2, y2);
173		val1 = vec2 * vec3;
174		val2 = x1*x2 + y1*y2;
175		ensure("1:operator* failed",(val1 == val2));
176
177		vec3.clearVec();
178		F32 mulVal = 4.332f;
179		vec3 = vec2 * mulVal;
180		val1 = x1*mulVal;
181		val2 = y1*mulVal;
182		ensure("2:operator* failed",(val1 == vec3.mV[VX]) && (val2 == vec3.mV[VY]));
183
184		vec3.clearVec();
185		vec3 = mulVal * vec2;
186		ensure("3:operator* failed",(val1 == vec3.mV[VX]) && (val2 == vec3.mV[VY]));		
187	}
188
189	template<> template<>
190	void v2math_object::test<9>()
191	{
192		F32 x1 =1.f, y1 = 2.f, div = 3.2f;
193		F32 val1, val2;
194		LLVector2 vec2(x1, y1), vec3;
195		vec3 = vec2 / div;
196		val1 = x1 / div;
197		val2 = y1 / div;
198		ensure("1:operator/ failed", is_approx_equal(val1, vec3.mV[VX]) && is_approx_equal(val2, vec3.mV[VY]));		
199
200		vec3.clearVec();
201		x1 = -.235f, y1 = -24.32f, div = -2.2f;
202		vec2.setVec(x1, y1);
203		vec3 = vec2 / div;
204		val1 = x1 / div;
205		val2 = y1 / div;
206		ensure("2:operator/ failed", is_approx_equal(val1, vec3.mV[VX]) && is_approx_equal(val2, vec3.mV[VY]));		
207	}
208
209	template<> template<>
210	void v2math_object::test<10>()
211	{
212		F32 x1 =1.f, y1 = 2.f,  x2 = -2.3f, y2 = 1.11f;
213		F32 val1, val2;
214		LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
215		vec4 = vec2 % vec3;
216		val1 = x1*y2 - x2*y1;
217		val2 = y1*x2 - y2*x1;
218		ensure("1:operator% failed",(val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY]));	
219
220		vec2.clearVec();
221		vec3.clearVec();
222		vec4.clearVec();
223		x1 = -.235f, y1 = -24.32f,  x2 = -2.3f, y2 = 1.f;
224		vec2.setVec(x1, y1);
225		vec3.setVec(x2, y2);
226		vec4 = vec2 % vec3;
227		val1 = x1*y2 - x2*y1;
228		val2 = y1*x2 - y2*x1;
229		ensure("2:operator% failed",(val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY]));	
230	}
231	template<> template<>
232	void v2math_object::test<11>()
233	{
234		F32 x1 =1.f, y1 = 2.f;
235		LLVector2 vec2(x1, y1), vec3(x1, y1);
236		ensure("1:operator== failed",(vec2 == vec3));
237		
238		vec2.clearVec();
239		vec3.clearVec();
240		x1 = -.235f, y1 = -24.32f;
241		vec2.setVec(x1, y1);
242		vec3.setVec(vec2);
243		ensure("2:operator== failed",(vec2 == vec3));
244	}
245
246	template<> template<>
247	void v2math_object::test<12>()
248	{
249		F32 x1 = 1.f, y1 = 2.f,x2 = 2.332f, y2 = -1.23f;
250		LLVector2 vec2(x1, y1), vec3(x2, y2);
251		ensure("1:operator!= failed",(vec2 != vec3));
252		
253		vec2.clearVec();
254		vec3.clearVec();
255		vec2.setVec(x1, y1);
256		vec3.setVec(vec2);
257		ensure("2:operator!= failed", (FALSE == (vec2 != vec3)));
258	}
259	template<> template<>
260	void v2math_object::test<13>()
261	{
262		F32 x1 = 1.f, y1 = 2.f,x2 = 2.332f, y2 = -1.23f;
263		F32 val1, val2;
264		LLVector2 vec2(x1, y1), vec3(x2, y2);
265		vec2 +=vec3;
266		val1 = x1+x2;
267		val2 = y1+y2;
268		ensure("1:operator+= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
269		
270		vec2.setVec(x1, y1);
271		vec2 -=vec3;
272		val1 = x1-x2;
273		val2 = y1-y2;
274		ensure("2:operator-= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
275		
276		vec2.clearVec();
277		vec3.clearVec();
278		x1 = -21.000466f, y1 = 2.98382f,x2 = 0.332f, y2 = -01.23f;
279		vec2.setVec(x1, y1);
280		vec3.setVec(x2, y2);
281		vec2 +=vec3;
282		val1 = x1+x2;
283		val2 = y1+y2;
284		ensure("3:operator+= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
285
286		vec2.setVec(x1, y1);
287		vec2 -=vec3;
288		val1 = x1-x2;
289		val2 = y1-y2;
290		ensure("4:operator-= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
291	}
292
293	template<> template<>
294	void v2math_object::test<14>()
295	{
296		F32 x1 =1.f, y1 = 2.f;
297		F32 val1, val2, mulVal = 4.332f;
298		LLVector2 vec2(x1, y1);
299		vec2 /=mulVal;
300		val1 = x1 / mulVal;
301		val2 = y1 / mulVal;
302		ensure("1:operator/= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
303		
304		vec2.clearVec();
305		x1 = .213f, y1 = -2.34f, mulVal = -.23f;
306		vec2.setVec(x1, y1);
307		vec2 /=mulVal;
308		val1 = x1 / mulVal;
309		val2 = y1 / mulVal;
310		ensure("2:operator/= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
311	}
312
313	template<> template<>
314	void v2math_object::test<15>()
315	{
316		F32 x1 =1.f, y1 = 2.f;
317		F32 val1, val2, mulVal = 4.332f;
318		LLVector2 vec2(x1, y1);
319		vec2 *=mulVal;
320		val1 = x1*mulVal;
321		val2 = y1*mulVal;
322		ensure("1:operator*= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
323		
324		vec2.clearVec();
325		x1 = .213f, y1 = -2.34f, mulVal = -.23f;
326		vec2.setVec(x1, y1);
327		vec2 *=mulVal;
328		val1 = x1*mulVal;
329		val2 = y1*mulVal;
330		ensure("2:operator*= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
331	}
332	
333	template<> template<>
334	void v2math_object::test<16>()
335	{
336		F32 x1 =1.f, y1 = 2.f,  x2 = -2.3f, y2 = 1.11f;
337		F32 val1, val2;
338		LLVector2 vec2(x1, y1), vec3(x2, y2);
339		vec2 %= vec3;
340		val1 = x1*y2 - x2*y1;
341		val2 = y1*x2 - y2*x1;
342		ensure("1:operator%= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));	
343	}
344
345	template<> template<>
346	void v2math_object::test<17>()
347	{
348		F32 x1 =1.f, y1 = 2.f;
349		LLVector2 vec2(x1, y1),vec3;
350		vec3 = -vec2;
351		ensure("1:operator- failed",(-vec3 == vec2));	
352	}
353
354	template<> template<>
355	void v2math_object::test<18>()
356	{
357		F32 x1 =1.f, y1 = 2.f;
358		std::ostringstream stream1, stream2;
359		LLVector2 vec2(x1, y1),vec3;
360		stream1 << vec2;
361		vec3.setVec(x1, y1);
362		stream2 << vec3;
363		ensure("1:operator << failed",(stream1.str() == stream2.str()));	
364	}
365
366	template<> template<>
367	void v2math_object::test<19>()
368	{
369		F32 x1 =1.0f, y1 = 2.0f, x2 = -.32f, y2 = .2234f;
370		LLVector2 vec2(x1, y1),vec3(x2, y2);
371		ensure("1:operator < failed",(vec3 < vec2));	
372
373		x1 = 1.0f, y1 = 2.0f, x2 = 1.0f, y2 = 3.2234f;
374		vec2.setVec(x1, y1);
375		vec3.setVec(x2, y2);
376		ensure("2:operator < failed", (FALSE == (vec3 < vec2)));	
377	}
378
379	template<> template<>
380	void v2math_object::test<20>()
381	{
382		F32 x1 =1.0f, y1 = 2.0f;
383		LLVector2 vec2(x1, y1);
384		ensure("1:operator [] failed",( x1 ==  vec2[0]));	
385		ensure("2:operator [] failed",( y1 ==  vec2[1]));
386
387		vec2.clearVec();
388		x1 = 23.0f, y1 = -.2361f;
389		vec2.setVec(x1, y1);
390		F32 ref1 = vec2[0];
391		ensure("3:operator [] failed", ( ref1 ==  x1));
392		F32 ref2 = vec2[1];
393		ensure("4:operator [] failed", ( ref2 ==  y1));
394	}
395
396	template<> template<>
397	void v2math_object::test<21>()
398	{
399		F32 x1 =1.f, y1 = 2.f, x2 = -.32f, y2 = .2234f;
400		F32 val1, val2;
401		LLVector2 vec2(x1, y1),vec3(x2, y2);		
402		val1 = dist_vec_squared2D(vec2, vec3);
403		val2 = (x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2);
404		ensure_equals("dist_vec_squared2D values are not equal",val2, val1);
405
406		val1 = dist_vec_squared(vec2, vec3);
407		ensure_equals("dist_vec_squared values are not equal",val2, val1);
408
409		val1 = 	dist_vec(vec2, vec3);
410		val2 = (F32) sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2));
411		ensure_equals("dist_vec values are not equal",val2, val1);
412	}
413
414	template<> template<>
415	void v2math_object::test<22>()
416	{
417		F32 x1 =1.f, y1 = 2.f, x2 = -.32f, y2 = .2234f,fVal = .0121f;
418		F32 val1, val2;
419		LLVector2 vec2(x1, y1),vec3(x2, y2);
420		LLVector2 vec4 = lerp(vec2, vec3, fVal);
421		val1 = x1 + (x2 - x1) * fVal;
422		val2 = y1 + (y2 - y1) * fVal;
423		ensure("lerp values are not equal", ((val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY])));
424	}
425
426	template<> template<>
427	void v2math_object::test<23>()
428	{
429		F32 x1 =1.f, y1 = 2.f;
430		F32 val1, val2;
431		LLVector2 vec2(x1, y1);
432
433		F32 vecMag = vec2.normVec();
434		F32 mag = (F32) sqrt(x1*x1 + y1*y1);
435
436		F32 oomag = 1.f / mag;
437		val1 = x1 * oomag;
438		val2 = y1 * oomag;
439
440		ensure("normVec failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]) && is_approx_equal(vecMag, mag));
441
442		x1 =.00000001f, y1 = 0.f;
443
444		vec2.setVec(x1, y1);
445		vecMag = vec2.normVec();
446		ensure("normVec failed should be 0.", 0. == vec2.mV[VX] && 0. == vec2.mV[VY] && vecMag == 0.);
447	}
448}