PageRenderTime 58ms CodeModel.GetById 16ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/tests/v3color_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 309 lines | 254 code | 28 blank | 27 comment | 75 complexity | 81f7c2d96b199a78ac56f04dd0ebd70d MD5 | raw file
  1/**
  2 * @file v3color_test.cpp
  3 * @author Adroit
  4 * @date 2007-03
  5 * @brief v3color 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 "../v3color.h"
 33
 34
 35namespace tut
 36{
 37	struct v3color_data
 38	{
 39	};
 40	typedef test_group<v3color_data> v3color_test;
 41	typedef v3color_test::object v3color_object;
 42	tut::v3color_test v3color_testcase("v3color_h");
 43
 44	template<> template<>
 45	void v3color_object::test<1>()
 46	{
 47		LLColor3 llcolor3;
 48		ensure("1:LLColor3:Fail to default-initialize ", (0.0f == llcolor3.mV[0]) && (0.0f == llcolor3.mV[1]) && (0.0f == llcolor3.mV[2]));
 49		F32 r = 2.0f, g = 3.2f, b = 1.f;
 50		F32 v1,v2,v3;
 51		LLColor3 llcolor3a(r,g,b);
 52		ensure("2:LLColor3:Fail to initialize " ,(2.0f == llcolor3a.mV[0]) && (3.2f == llcolor3a.mV[1]) && (1.f == llcolor3a.mV[2]));
 53		
 54		const F32 vec[3] = {2.0f, 3.2f,1.f};
 55		LLColor3 llcolor3b(vec);
 56		ensure("3:LLColor3:Fail to initialize " ,(2.0f == llcolor3b.mV[0]) && (3.2f == llcolor3b.mV[1]) && (1.f == llcolor3b.mV[2]));
 57		const char* str = "561122";
 58		LLColor3 llcolor3c(str);
 59		v1 = (F32)86.0f/255.0f; // 0x56 = 86
 60		v2 = (F32)17.0f/255.0f; // 0x11 = 17
 61		v3 = (F32)34.0f/255.f;  // 0x22 = 34
 62		ensure("4:LLColor3:Fail to initialize " , is_approx_equal(v1, llcolor3c.mV[0]) && is_approx_equal(v2, llcolor3c.mV[1]) && is_approx_equal(v3, llcolor3c.mV[2]));
 63	}
 64
 65	template<> template<>
 66	void v3color_object::test<2>()
 67	{
 68		LLColor3 llcolor3;
 69		llcolor3.setToBlack();
 70		ensure("setToBlack:Fail to set black ", ((llcolor3.mV[0] == 0.f) && (llcolor3.mV[1] == 0.f) && (llcolor3.mV[2] == 0.f)));
 71		llcolor3.setToWhite();
 72		ensure("setToWhite:Fail to set white  ", ((llcolor3.mV[0] == 1.f) && (llcolor3.mV[1] == 1.f) && (llcolor3.mV[2] == 1.f)));
 73	}
 74
 75	template<> template<>
 76	void v3color_object::test<3>()
 77	{
 78		F32 r = 2.3436212f, g = 1231.f, b = 4.7849321232f;
 79		LLColor3 llcolor3, llcolor3a;
 80		llcolor3.setVec(r,g,b);
 81		ensure("1:setVec(r,g,b) Fail ",((r == llcolor3.mV[0]) && (g == llcolor3.mV[1]) && (b == llcolor3.mV[2])));
 82		llcolor3a.setVec(llcolor3);
 83		ensure_equals("2:setVec(LLColor3) Fail ", llcolor3,llcolor3a);
 84		F32 vec[3] = {1.2324f, 2.45634f, .234563f};
 85		llcolor3.setToBlack();
 86		llcolor3.setVec(vec);
 87		ensure("3:setVec(F32*) Fail ",((vec[0] == llcolor3.mV[0]) && (vec[1] == llcolor3.mV[1]) && (vec[2] == llcolor3.mV[2])));
 88	}
 89
 90	template<> template<>
 91	void v3color_object::test<4>()
 92	{
 93		F32 r = 2.3436212f, g = 1231.f, b = 4.7849321232f;
 94		LLColor3 llcolor3(r,g,b);
 95		ensure("magVecSquared:Fail ", is_approx_equal(llcolor3.magVecSquared(), (r*r + g*g + b*b)));
 96		ensure("magVec:Fail ", is_approx_equal(llcolor3.magVec(), (F32) sqrt(r*r + g*g + b*b)));
 97	}
 98
 99	template<> template<>
100	void v3color_object::test<5>()
101	{
102		F32 r = 2.3436212f, g = 1231.f, b = 4.7849321232f;
103		F32 val1, val2,val3;
104		LLColor3 llcolor3(r,g,b);
105		F32 vecMag = llcolor3.normVec();
106		F32 mag = (F32) sqrt(r*r + g*g + b*b);
107		F32 oomag = 1.f / mag;
108		val1 = r * oomag;
109		val2 = g * oomag;
110		val3 = b * oomag;
111		ensure("1:normVec failed ", (is_approx_equal(val1, llcolor3.mV[0]) && is_approx_equal(val2, llcolor3.mV[1]) && is_approx_equal(val3, llcolor3.mV[2]) && is_approx_equal(vecMag, mag)));
112		r = .000000000f, g = 0.f, b = 0.0f;
113		llcolor3.setVec(r,g,b);
114		vecMag = llcolor3.normVec();
115		ensure("2:normVec failed should be 0. ", (0. == llcolor3.mV[0] && 0. == llcolor3.mV[1] && 0. == llcolor3.mV[2] && vecMag == 0.));
116	}
117
118	template<> template<>
119	void v3color_object::test<6>()
120	{
121		F32 r = 2.3436212f, g = -1231.f, b = .7849321232f;
122		std::ostringstream stream1, stream2;
123		LLColor3 llcolor3(r,g,b),llcolor3a;
124		stream1 << llcolor3;
125		llcolor3a.setVec(r,g,b);
126		stream2 << llcolor3a;
127		ensure("operator << failed ", (stream1.str() == stream2.str()));	
128	}
129		
130	template<> template<>
131	void v3color_object::test<7>()
132	{
133		F32 r = 2.3436212f, g = -1231.f, b = .7849321232f;
134		LLColor3 llcolor3(r,g,b),llcolor3a;
135		llcolor3a = llcolor3;
136		ensure("operator == failed ", (llcolor3a == llcolor3));	
137	}
138
139	template<> template<>
140	void v3color_object::test<8>()
141	{
142		F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
143		LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2),llcolor3b;
144		llcolor3b = llcolor3 + llcolor3a ;
145		ensure("1:operator+ failed",is_approx_equal(r1+r2 ,llcolor3b.mV[0]) && is_approx_equal(g1+g2,llcolor3b.mV[1])&& is_approx_equal(b1+b2,llcolor3b.mV[2]));
146		r1 = -.235f, g1 = -24.32f, b1 = 2.13f,  r2 = -2.3f, g2 = 1.f, b2 = 34.21f;
147		llcolor3.setVec(r1,g1,b1);
148		llcolor3a.setVec(r2,g2,b2);
149		llcolor3b = llcolor3 + llcolor3a;
150		ensure("2:operator+ failed",is_approx_equal(r1+r2 ,llcolor3b.mV[0]) && is_approx_equal(g1+g2,llcolor3b.mV[1])&& is_approx_equal(b1+b2,llcolor3b.mV[2]));
151	}
152
153	template<> template<>
154	void v3color_object::test<9>()
155	{
156		F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
157		LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2),llcolor3b;
158		llcolor3b = llcolor3 - llcolor3a ;
159		ensure("1:operator- failed",is_approx_equal(r1-r2 ,llcolor3b.mV[0]) && is_approx_equal(g1-g2,llcolor3b.mV[1])&& is_approx_equal(b1-b2,llcolor3b.mV[2]));
160		r1 = -.235f, g1 = -24.32f, b1 = 2.13f,  r2 = -2.3f, g2 = 1.f, b2 = 34.21f;
161		llcolor3.setVec(r1,g1,b1);
162		llcolor3a.setVec(r2,g2,b2);
163		llcolor3b = llcolor3 - llcolor3a;
164		ensure("2:operator- failed",is_approx_equal(r1-r2 ,llcolor3b.mV[0]) && is_approx_equal(g1-g2,llcolor3b.mV[1])&& is_approx_equal(b1-b2,llcolor3b.mV[2]));
165	}
166
167	template<> template<>
168	void v3color_object::test<10>()
169	{
170		F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
171		LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2),llcolor3b;
172		llcolor3b = llcolor3 * llcolor3a;
173		ensure("1:operator* failed",is_approx_equal(r1*r2 ,llcolor3b.mV[0]) && is_approx_equal(g1*g2,llcolor3b.mV[1])&& is_approx_equal(b1*b2,llcolor3b.mV[2]));
174		llcolor3a.setToBlack();
175		F32 mulVal = 4.332f;
176		llcolor3a = llcolor3 * mulVal;
177		ensure("2:operator* failed",is_approx_equal(r1*mulVal ,llcolor3a.mV[0]) && is_approx_equal(g1*mulVal,llcolor3a.mV[1])&& is_approx_equal(b1*mulVal,llcolor3a.mV[2]));
178		llcolor3a.setToBlack();
179		llcolor3a = mulVal * llcolor3;
180		ensure("3:operator* failed",is_approx_equal(r1*mulVal ,llcolor3a.mV[0]) && is_approx_equal(g1*mulVal,llcolor3a.mV[1])&& is_approx_equal(b1*mulVal,llcolor3a.mV[2]));
181	}
182
183	template<> template<>
184	void v3color_object::test<11>()
185	{
186		F32 r = 2.3436212f, g = 1231.f, b = 4.7849321232f;
187		LLColor3 llcolor3(r,g,b),llcolor3a;
188		llcolor3a = -llcolor3;
189		ensure("operator- failed ", (-llcolor3a == llcolor3));	
190	}
191
192	template<> template<>
193	void v3color_object::test<12>()
194	{
195		F32 r = 2.3436212f, g = 1231.f, b = 4.7849321232f;
196		LLColor3 llcolor3(r,g,b),llcolor3a(r,g,b);
197		ensure_equals("1:operator== failed",llcolor3a,llcolor3);
198		r = 13.3436212f, g = -11.f, b = .7849321232f;
199		llcolor3.setVec(r,g,b);
200		llcolor3a.setVec(r,g,b);
201		ensure_equals("2:operator== failed",llcolor3a,llcolor3);
202	}
203
204	template<> template<>
205	void v3color_object::test<13>()
206	{
207		F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
208		LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
209		ensure("1:operator!= failed",(llcolor3 != llcolor3a));
210		llcolor3.setToBlack();
211		llcolor3a.setVec(llcolor3);
212		ensure("2:operator!= failed", ( FALSE == (llcolor3a != llcolor3)));
213	}
214
215	template<> template<>
216	void v3color_object::test<14>()
217	{
218		F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
219		LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
220		llcolor3a += llcolor3;
221		ensure("1:operator+= failed",is_approx_equal(r1+r2 ,llcolor3a.mV[0]) && is_approx_equal(g1+g2,llcolor3a.mV[1])&& is_approx_equal(b1+b2,llcolor3a.mV[2]));
222		llcolor3.setVec(r1,g1,b1);
223		llcolor3a.setVec(r2,g2,b2);
224		llcolor3a += llcolor3;
225		ensure("2:operator+= failed",is_approx_equal(r1+r2 ,llcolor3a.mV[0]) && is_approx_equal(g1+g2,llcolor3a.mV[1])&& is_approx_equal(b1+b2,llcolor3a.mV[2]));
226	}
227
228	template<> template<>
229	void v3color_object::test<15>()
230	{
231		F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
232		LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
233		llcolor3a -= llcolor3;
234		ensure("1:operator-= failed", is_approx_equal(r2-r1, llcolor3a.mV[0]));
235		ensure("2:operator-= failed", is_approx_equal(g2-g1, llcolor3a.mV[1]));
236		ensure("3:operator-= failed", is_approx_equal(b2-b1, llcolor3a.mV[2]));
237		llcolor3.setVec(r1,g1,b1);
238		llcolor3a.setVec(r2,g2,b2);
239		llcolor3a -= llcolor3;
240		ensure("4:operator-= failed", is_approx_equal(r2-r1, llcolor3a.mV[0]));
241		ensure("5:operator-= failed", is_approx_equal(g2-g1, llcolor3a.mV[1]));
242		ensure("6:operator-= failed", is_approx_equal(b2-b1, llcolor3a.mV[2]));
243	}
244	
245	template<> template<>
246	void v3color_object::test<16>()
247	{
248		F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
249		LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
250		llcolor3a *= llcolor3;
251		ensure("1:operator*= failed",is_approx_equal(r1*r2 ,llcolor3a.mV[0]) && is_approx_equal(g1*g2,llcolor3a.mV[1])&& is_approx_equal(b1*b2,llcolor3a.mV[2]));
252		F32 mulVal = 4.332f;
253		llcolor3 *=mulVal;
254		ensure("2:operator*= failed",is_approx_equal(r1*mulVal ,llcolor3.mV[0]) && is_approx_equal(g1*mulVal,llcolor3.mV[1])&& is_approx_equal(b1*mulVal,llcolor3.mV[2]));
255	}
256
257	template<> template<>
258	void v3color_object::test<17>()
259	{
260		F32 r = 2.3436212f, g = -1231.f, b = .7849321232f;
261		LLColor3 llcolor3(r,g,b);
262		llcolor3.clamp();
263		ensure("1:clamp:Fail to clamp " ,(1.0f == llcolor3.mV[0]) && (0.f == llcolor3.mV[1]) && (b == llcolor3.mV[2]));
264		r = -2.3436212f, g = -1231.f, b = 67.7849321232f;
265		llcolor3.setVec(r,g,b);
266		llcolor3.clamp();
267		ensure("2:clamp:Fail to clamp " ,(0.f == llcolor3.mV[0]) && (0.f == llcolor3.mV[1]) && (1.f == llcolor3.mV[2]));
268	}
269
270	template<> template<>
271	void v3color_object::test<18>()
272	{
273		F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
274		F32 val = 2.3f,val1,val2,val3;
275		LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
276		val1 = r1 + (r2 - r1)* val;
277		val2 = g1 + (g2 - g1)* val;
278		val3 = b1 + (b2 - b1)* val;
279		LLColor3 llcolor3b = lerp(llcolor3,llcolor3a,val);
280		ensure("lerp failed ", ((val1 ==llcolor3b.mV[0])&& (val2 ==llcolor3b.mV[1]) && (val3 ==llcolor3b.mV[2])));		
281	}
282
283	template<> template<>
284	void v3color_object::test<19>()
285	{
286		F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
287		LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
288		F32 val = distVec(llcolor3,llcolor3a);
289		ensure("distVec failed ", is_approx_equal((F32) sqrt((r1-r2)*(r1-r2) + (g1-g2)*(g1-g2) + (b1-b2)*(b1-b2)) ,val));
290		
291		F32 val1 = distVec_squared(llcolor3,llcolor3a);
292		ensure("distVec_squared failed ", is_approx_equal(((r1-r2)*(r1-r2) + (g1-g2)*(g1-g2) + (b1-b2)*(b1-b2)) ,val1));
293	}
294
295	template<> template<>
296	void v3color_object::test<20>()
297	{
298		F32 r1 = 1.02223f, g1 = 22222.212f, b1 = 122222.00002f;
299		LLColor3 llcolor31(r1,g1,b1);
300
301		LLSD sd = llcolor31.getValue();
302		LLColor3 llcolor32;
303		llcolor32.setValue(sd);
304		ensure_equals("LLColor3::setValue/getValue failed", llcolor31, llcolor32);
305
306		LLColor3 llcolor33(sd);
307		ensure_equals("LLColor3(LLSD) failed", llcolor31, llcolor33);
308	}
309}