PageRenderTime 76ms CodeModel.GetById 12ms app.highlight 59ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/tests/llrect_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 526 lines | 342 code | 90 blank | 94 comment | 29 complexity | 9250f38033998a1b3c9c3aa06a1aee0c MD5 | raw file
  1/**
  2 * @file   llrect_test.cpp
  3 * @author Martin Reddy
  4 * @date   2009-06-25
  5 * @brief  Test for llrect.cpp.
  6 * 
  7 * $LicenseInfo:firstyear=2009&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 "../llrect.h"
 32
 33namespace tut
 34{
 35	struct LLRectData
 36	{
 37	};
 38
 39	typedef test_group<LLRectData> factory;
 40	typedef factory::object object;
 41}
 42
 43namespace
 44{
 45	tut::factory llrect_test_factory("LLRect");
 46}
 47
 48namespace tut
 49{
 50	template<> template<>
 51	void object::test<1>()
 52	{	
 53		//
 54		// test the LLRect default constructor
 55		//
 56		
 57		LLSD zero;
 58		zero.append(0);	zero.append(0); zero.append(0); zero.append(0);
 59		
 60		// default constructor
 61		LLRect rect1;
 62		ensure_equals("Empty rect", rect1.getValue(), zero);
 63		ensure_equals("Empty rect left", rect1.mLeft, 0);
 64		ensure_equals("Empty rect top", rect1.mTop, 0);
 65		ensure_equals("Empty rect right", rect1.mRight, 0);
 66		ensure_equals("Empty rect bottom", rect1.mBottom, 0);
 67		ensure_equals("Empty rect width", rect1.getWidth(), 0);
 68		ensure_equals("Empty rect height", rect1.getHeight(), 0);
 69		ensure_equals("Empty rect centerx", rect1.getCenterX(), 0);
 70		ensure_equals("Empty rect centery", rect1.getCenterY(), 0);
 71	}
 72	
 73	template<> template<>
 74	void object::test<2>()
 75	{	
 76		//
 77		// test the LLRectf default constructor
 78		//
 79		
 80		LLSD zerof;
 81		zerof.append(0.0f); zerof.append(0.0f); zerof.append(0.0f); zerof.append(0.0f);
 82
 83		LLRectf rect2;
 84		ensure_equals("Empty rectf", rect2.getValue(), zerof);
 85		ensure_equals("Empty rectf left", rect2.mLeft, 0.0f);
 86		ensure_equals("Empty rectf top", rect2.mTop, 0.0f);
 87		ensure_equals("Empty rectf right", rect2.mRight, 0.0f);
 88		ensure_equals("Empty rectf bottom", rect2.mBottom, 0.0f);
 89		ensure_equals("Empty rectf width", rect2.getWidth(), 0.0f);
 90		ensure_equals("Empty rectf height", rect2.getHeight(), 0.0f);
 91		ensure_equals("Empty rectf centerx", rect2.getCenterX(), 0.0f);
 92		ensure_equals("Empty rectf centery", rect2.getCenterY(), 0.0f);
 93	}
 94
 95	template<> template<>
 96	void object::test<3>()
 97	{	
 98		//
 99		// test the LLRect constructor from another LLRect
100		//	
101		
102		LLRect rect3(LLRect(1, 6, 7, 2));
103		ensure_equals("Default rect left", rect3.mLeft, 1);
104		ensure_equals("Default rect top", rect3.mTop, 6);
105		ensure_equals("Default rect right", rect3.mRight, 7);
106		ensure_equals("Default rect bottom", rect3.mBottom, 2);
107		ensure_equals("Default rect width", rect3.getWidth(), 6);
108		ensure_equals("Default rect height", rect3.getHeight(), 4);
109		ensure_equals("Default rect centerx", rect3.getCenterX(), 4);
110		ensure_equals("Default rect centery", rect3.getCenterY(), 4);
111	}
112
113	template<> template<>
114	void object::test<4>()
115	{	
116		//
117		// test the LLRectf four-float constructor
118		//	
119		
120		LLRectf rect4(1.0f, 5.0f, 6.0f, 2.0f);
121		ensure_equals("Default rectf left", rect4.mLeft, 1.0f);
122		ensure_equals("Default rectf top", rect4.mTop, 5.0f);
123		ensure_equals("Default rectf right", rect4.mRight, 6.0f);
124		ensure_equals("Default rectf bottom", rect4.mBottom, 2.0f);
125		ensure_equals("Default rectf width", rect4.getWidth(), 5.0f);
126		ensure_equals("Default rectf height", rect4.getHeight(), 3.0f);
127		ensure_equals("Default rectf centerx", rect4.getCenterX(), 3.5f);
128		ensure_equals("Default rectf centery", rect4.getCenterY(), 3.5f);
129	}
130
131	template<> template<>
132	void object::test<5>()
133	{	
134		//
135		// test the LLRectf LLSD constructor
136		//
137
138		LLSD array;
139		array.append(-1.0f); array.append(0.0f); array.append(0.0f); array.append(-1.0f);
140		LLRectf rect5(array);
141		ensure_equals("LLSD rectf left", rect5.mLeft, -1.0f);
142		ensure_equals("LLSD rectf top", rect5.mTop, 0.0f);
143		ensure_equals("LLSD rectf right", rect5.mRight, 0.0f);
144		ensure_equals("LLSD rectf bottom", rect5.mBottom, -1.0f);
145		ensure_equals("LLSD rectf width", rect5.getWidth(), 1.0f);
146		ensure_equals("LLSD rectf height", rect5.getHeight(), 1.0f);
147		ensure_equals("LLSD rectf centerx", rect5.getCenterX(), -0.5f);
148		ensure_equals("LLSD rectf centery", rect5.getCenterY(), -0.5f);		
149	}
150	
151	template<> template<>
152	void object::test<6>()
153	{	
154		//
155		// test directly setting the member variables for dimensions
156		//
157		
158		LLRectf rectf;
159
160		rectf.mLeft = -1.0f;
161		rectf.mTop = 1.0f;
162		rectf.mRight = 1.0f;
163		rectf.mBottom = -1.0f;
164		ensure_equals("Member-set rectf left", rectf.mLeft, -1.0f);
165		ensure_equals("Member-set rectf top", rectf.mTop, 1.0f);
166		ensure_equals("Member-set rectf right", rectf.mRight, 1.0f);
167		ensure_equals("Member-set rectf bottom", rectf.mBottom, -1.0f);
168		ensure_equals("Member-set rectf width", rectf.getWidth(), 2.0f);
169		ensure_equals("Member-set rectf height", rectf.getHeight(), 2.0f);
170		ensure_equals("Member-set rectf centerx", rectf.getCenterX(), 0.0f);
171		ensure_equals("Member-set rectf centery", rectf.getCenterY(), 0.0f);
172	}
173		
174	template<> template<>
175	void object::test<7>()
176	{	
177		//
178		// test the setValue() method
179		//
180			
181		LLRectf rectf;
182		
183		LLSD array;
184		array.append(-1.0f); array.append(0.0f); array.append(0.0f); array.append(-1.0f);
185		rectf.setValue(array);
186		ensure_equals("setValue() rectf left", rectf.mLeft, -1.0f);
187		ensure_equals("setValue() rectf top", rectf.mTop, 0.0f);
188		ensure_equals("setValue() rectf right", rectf.mRight, 0.0f);
189		ensure_equals("setValue() rectf bottom", rectf.mBottom, -1.0f);
190		ensure_equals("setValue() rectf width", rectf.getWidth(), 1.0f);
191		ensure_equals("setValue() rectf height", rectf.getHeight(), 1.0f);
192		ensure_equals("setValue() rectf centerx", rectf.getCenterX(), -0.5f);
193		ensure_equals("setValue() rectf centery", rectf.getCenterY(), -0.5f);
194	}
195		
196	template<> template<>
197	void object::test<8>()
198	{	
199		//
200		// test the set() method
201		//
202		
203		LLRect rect;
204		
205		rect.set(10, 90, 70, 10);
206		ensure_equals("set() rectf left", rect.mLeft, 10);
207		ensure_equals("set() rectf top", rect.mTop, 90);
208		ensure_equals("set() rectf right", rect.mRight, 70);
209		ensure_equals("set() rectf bottom", rect.mBottom, 10);
210		ensure_equals("set() rectf width", rect.getWidth(), 60);
211		ensure_equals("set() rectf height", rect.getHeight(), 80);
212		ensure_equals("set() rectf centerx", rect.getCenterX(), 40);
213		ensure_equals("set() rectf centery", rect.getCenterY(), 50);
214	}
215
216	template<> template<>
217	void object::test<9>()
218	{	
219		//
220		// test the setOriginAndSize() method
221		//
222		
223		LLRectf rectf;
224		
225		rectf.setOriginAndSize(0.0f, 0.0f, 2.0f, 1.0f);
226		ensure_equals("setOriginAndSize() rectf left", rectf.mLeft, 0.0f);
227		ensure_equals("setOriginAndSize() rectf top", rectf.mTop, 1.0f);
228		ensure_equals("setOriginAndSize() rectf right", rectf.mRight, 2.0f);
229		ensure_equals("setOriginAndSize() rectf bottom", rectf.mBottom, 0.0f);
230		ensure_equals("setOriginAndSize() rectf width", rectf.getWidth(), 2.0f);
231		ensure_equals("setOriginAndSize() rectf height", rectf.getHeight(), 1.0f);
232		ensure_equals("setOriginAndSize() rectf centerx", rectf.getCenterX(), 1.0f);
233		ensure_equals("setOriginAndSize() rectf centery", rectf.getCenterY(), 0.5f);	
234	}
235	
236	template<> template<>
237	void object::test<10>()
238	{	
239		//
240		// test the setLeftTopAndSize() method
241		//
242		
243		LLRectf rectf;
244
245		rectf.setLeftTopAndSize(0.0f, 0.0f, 2.0f, 1.0f);
246		ensure_equals("setLeftTopAndSize() rectf left", rectf.mLeft, 0.0f);
247		ensure_equals("setLeftTopAndSize() rectf top", rectf.mTop, 0.0f);
248		ensure_equals("setLeftTopAndSize() rectf right", rectf.mRight, 2.0f);
249		ensure_equals("setLeftTopAndSize() rectf bottom", rectf.mBottom, -1.0f);
250		ensure_equals("setLeftTopAndSize() rectf width", rectf.getWidth(), 2.0f);
251		ensure_equals("setLeftTopAndSize() rectf height", rectf.getHeight(), 1.0f);
252		ensure_equals("setLeftTopAndSize() rectf centerx", rectf.getCenterX(), 1.0f);
253		ensure_equals("setLeftTopAndSize() rectf centery", rectf.getCenterY(), -0.5f);
254	}
255
256	template<> template<>
257	void object::test<11>()
258	{	
259		//
260		// test the setCenterAndSize() method
261		//
262		
263		LLRectf rectf;
264		
265		rectf.setCenterAndSize(0.0f, 0.0f, 2.0f, 1.0f);
266		ensure_equals("setCenterAndSize() rectf left", rectf.mLeft, -1.0f);
267		ensure_equals("setCenterAndSize() rectf top", rectf.mTop, 0.5f);
268		ensure_equals("setCenterAndSize() rectf right", rectf.mRight, 1.0f);
269		ensure_equals("setCenterAndSize() rectf bottom", rectf.mBottom, -0.5f);
270		ensure_equals("setCenterAndSize() rectf width", rectf.getWidth(), 2.0f);
271		ensure_equals("setCenterAndSize() rectf height", rectf.getHeight(), 1.0f);
272		ensure_equals("setCenterAndSize() rectf centerx", rectf.getCenterX(), 0.0f);
273		ensure_equals("setCenterAndSize() rectf centery", rectf.getCenterY(), 0.0f);
274	}
275
276	template<> template<>
277	void object::test<12>()
278	{	
279		//
280		// test the validity checking method
281		//
282		
283		LLRectf rectf;
284
285		rectf.set(-1.0f, 1.0f, 1.0f, -1.0f);
286		ensure("BBox is valid", rectf.isValid());
287
288		rectf.mLeft = 2.0f;
289		ensure("BBox is not valid", ! rectf.isValid());
290
291		rectf.makeValid();
292		ensure("BBox forced valid", rectf.isValid());
293
294		rectf.set(-1.0f, -1.0f, -1.0f, -1.0f);
295		ensure("BBox(0,0,0,0) is valid", rectf.isValid());
296	}
297
298	template<> template<>
299	void object::test<13>()
300	{	
301		//
302		// test the null checking methods
303		//
304		
305		LLRectf rectf;
306		
307		rectf.set(-1.0f, 1.0f, 1.0f, -1.0f);
308		ensure("BBox is not Null", ! rectf.isEmpty());
309		ensure("BBox notNull", rectf.notEmpty());
310		
311		rectf.mLeft = 2.0f;
312		rectf.makeValid();
313		ensure("BBox is now Null", rectf.isEmpty());
314		
315		rectf.set(-1.0f, -1.0f, -1.0f, -1.0f);
316		ensure("BBox(0,0,0,0) is Null", rectf.isEmpty());
317	}
318		
319	template<> template<>
320	void object::test<14>()
321	{	
322		//
323		// test the (in)equality operators
324		//
325		
326		LLRectf rect1, rect2;
327
328		rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
329		rect2.set(-1.0f, 0.9f, 1.0f, -1.0f);
330
331		ensure("rect1 == rect2 (false)", ! (rect1 == rect2));
332		ensure("rect1 != rect2 (true)", rect1 != rect2);
333
334		ensure("rect1 == rect1 (true)", rect1 == rect1);
335		ensure("rect1 != rect1 (false)", ! (rect1 != rect1));
336	}
337	
338	template<> template<>
339	void object::test<15>()
340	{	
341		//
342		// test the copy constructor
343		//
344		
345		LLRectf rect1, rect2(rect1);
346
347		ensure("rect1 == rect2 (true)", rect1 == rect2);
348		ensure("rect1 != rect2 (false)", ! (rect1 != rect2));
349	}
350
351	template<> template<>
352	void object::test<16>()
353	{	
354		//
355		// test the translate() method
356		//
357		
358		LLRectf rect1(-1.0f, 1.0f, 1.0f, -1.0f);
359		LLRectf rect2(rect1);
360
361		rect1.translate(0.0f, 0.0f);
362
363		ensure("translate(0, 0)", rect1 == rect2);
364
365		rect1.translate(100.0f, 100.0f);
366		rect1.translate(-100.0f, -100.0f);
367
368		ensure("translate(100, 100) + translate(-100, -100)", rect1 == rect2);
369
370		rect1.translate(10.0f, 0.0f);
371		rect2.set(9.0f, 1.0f, 11.0f, -1.0f);
372		ensure("translate(10, 0)", rect1 == rect2);
373
374		rect1.translate(0.0f, 10.0f);
375		rect2.set(9.0f, 11.0f, 11.0f, 9.0f);
376		ensure("translate(0, 10)", rect1 == rect2);
377
378		rect1.translate(-10.0f, -10.0f);
379		rect2.set(-1.0f, 1.0f, 1.0f, -1.0f);
380		ensure("translate(-10, -10)", rect1 == rect2);
381	}
382
383	template<> template<>
384	void object::test<17>()
385	{	
386		//
387		// test the stretch() method
388		//
389		
390		LLRectf rect1(-1.0f, 1.0f, 1.0f, -1.0f);
391		LLRectf rect2(rect1);
392		
393		rect1.stretch(0.0f);
394		ensure("stretch(0)", rect1 == rect2);
395		
396		rect1.stretch(0.0f, 0.0f);
397		ensure("stretch(0, 0)", rect1 == rect2);
398		
399		rect1.stretch(10.0f);
400		rect1.stretch(-10.0f);
401		ensure("stretch(10) + stretch(-10)", rect1 == rect2);
402		
403		rect1.stretch(2.0f, 1.0f);
404		rect2.set(-3.0f, 2.0f, 3.0f, -2.0f);
405		ensure("stretch(2, 1)", rect1 == rect2);
406	}
407	
408	
409	template<> template<>
410	void object::test<18>()
411	{	
412		//
413		// test the unionWith() method
414		//
415		
416		LLRectf rect1, rect2, rect3;
417
418		rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
419		rect2.set(-1.0f, 1.0f, 1.0f, -1.0f);
420		rect3 = rect1;
421		rect3.unionWith(rect2);
422		ensure_equals("union with self", rect3, rect1);
423
424		rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
425		rect2.set(-2.0f, 2.0f, 0.0f, 0.0f);
426		rect3 = rect1;
427		rect3.unionWith(rect2);
428		ensure_equals("union - overlap", rect3, LLRectf(-2.0f, 2.0f, 1.0f, -1.0f));
429		
430		rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
431		rect2.set(5.0f, 10.0f, 10.0f, 5.0f);
432		rect3 = rect1;
433		rect3.unionWith(rect2);
434		ensure_equals("union - no overlap", rect3, LLRectf(-1.0f, 10.0f, 10.0f, -1.0f));
435	}
436
437	template<> template<>
438	void object::test<19>()
439	{	
440		//
441		// test the intersectWith() methods
442		//
443		
444		LLRectf rect1, rect2, rect3;
445		
446		rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
447		rect2.set(-1.0f, 1.0f, 1.0f, -1.0f);
448		rect3 = rect1;
449		rect3.intersectWith(rect2);
450		ensure_equals("intersect with self", rect3, rect1);
451		
452		rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
453		rect2.set(-2.0f, 2.0f, 0.0f, 0.0f);
454		rect3 = rect1;
455		rect3.intersectWith(rect2);
456		ensure_equals("intersect - overlap", rect3, LLRectf(-1.0f, 1.0f, 0.0f, 0.0f));
457		
458		rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
459		rect2.set(5.0f, 10.0f, 10.0f, 5.0f);
460		rect3 = rect1;
461		rect3.intersectWith(rect2);
462		ensure("intersect - no overlap", rect3.isEmpty());
463	}
464		
465	template<> template<>
466	void object::test<20>()
467	{
468		//
469		// test the pointInRect() method
470		//
471		
472		LLRectf rect(1.0f, 3.0f, 3.0f, 1.0f);
473
474		ensure("(0,0) not in rect", rect.pointInRect(0.0f, 0.0f) == FALSE);
475		ensure("(2,2) in rect", rect.pointInRect(2.0f, 2.0f) == TRUE);
476		ensure("(1,1) in rect", rect.pointInRect(1.0f, 1.0f) == TRUE);
477		ensure("(3,3) not in rect", rect.pointInRect(3.0f, 3.0f) == FALSE);
478		ensure("(2.999,2.999) in rect", rect.pointInRect(2.999f, 2.999f) == TRUE);
479		ensure("(2.999,3.0) not in rect", rect.pointInRect(2.999f, 3.0f) == FALSE);
480		ensure("(3.0,2.999) not in rect", rect.pointInRect(3.0f, 2.999f) == FALSE);
481	}
482
483	template<> template<>
484	void object::test<21>()
485	{
486		//
487		// test the localPointInRect() method
488		//
489		
490		LLRectf rect(1.0f, 3.0f, 3.0f, 1.0f);
491		
492		ensure("(0,0) in local rect", rect.localPointInRect(0.0f, 0.0f) == TRUE);
493		ensure("(-0.0001,-0.0001) not in local rect", rect.localPointInRect(-0.0001f, -0.001f) == FALSE);
494		ensure("(1,1) in local rect", rect.localPointInRect(1.0f, 1.0f) == TRUE);
495		ensure("(2,2) not in local rect", rect.localPointInRect(2.0f, 2.0f) == FALSE);
496		ensure("(1.999,1.999) in local rect", rect.localPointInRect(1.999f, 1.999f) == TRUE);
497		ensure("(1.999,2.0) not in local rect", rect.localPointInRect(1.999f, 2.0f) == FALSE);
498		ensure("(2.0,1.999) not in local rect", rect.localPointInRect(2.0f, 1.999f) == FALSE);
499	}
500	
501	template<> template<>
502	void object::test<22>()
503	{
504		//
505		// test the clampPointToRect() method
506		//
507
508		LLRectf rect(1.0f, 3.0f, 3.0f, 1.0f);
509		F32 x, y;
510
511		x = 2.0f; y = 2.0f;
512		rect.clampPointToRect(x, y);
513		ensure_equals("clamp x-coord within rect", x, 2.0f);
514		ensure_equals("clamp y-coord within rect", y, 2.0f);
515
516		x = -100.0f; y = 100.0f;
517		rect.clampPointToRect(x, y);
518		ensure_equals("clamp x-coord outside rect", x, 1.0f);
519		ensure_equals("clamp y-coord outside rect", y, 3.0f);
520
521		x = 3.0f; y = 1.0f;
522		rect.clampPointToRect(x, y);
523		ensure_equals("clamp x-coord edge rect", x, 3.0f);
524		ensure_equals("clamp y-coord edge rect", y, 1.0f);
525	}
526}