PageRenderTime 107ms CodeModel.GetById 25ms app.highlight 77ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmath/tests/v4color_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 359 lines | 284 code | 48 blank | 27 comment | 132 complexity | 502bd93e0f89a5f7f9a22775f61c9d3d MD5 | raw file
  1/**
  2 * @file v4color_test.cpp
  3 * @author Adroit
  4 * @date 2007-03
  5 * @brief v4color 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
 30#include "linden_common.h"
 31#include "../test/lltut.h"
 32
 33#include "../v4coloru.h"
 34#include "llsd.h"
 35#include "../v3color.h"
 36#include "../v4color.h"
 37
 38
 39namespace tut
 40{
 41	struct v4color_data
 42	{
 43	};
 44	typedef test_group<v4color_data> v4color_test;
 45	typedef v4color_test::object v4color_object;
 46	tut::v4color_test v4color_testcase("v4color_h");
 47
 48	template<> template<>
 49	void v4color_object::test<1>()
 50	{
 51		LLColor4 llcolor4;
 52		ensure("1:LLColor4:Fail to initialize ", ((0 == llcolor4.mV[VX]) && (0 == llcolor4.mV[VY]) && (0 == llcolor4.mV[VZ])&& (1.0f == llcolor4.mV[VW])));
 53
 54		F32 r = 0x20, g = 0xFFFF, b = 0xFF, a = 0xAF;
 55		LLColor4 llcolor4a(r,g,b);
 56		ensure("2:LLColor4:Fail to initialize ", ((r == llcolor4a.mV[VX]) && (g == llcolor4a.mV[VY]) && (b == llcolor4a.mV[VZ])&& (1.0f == llcolor4a.mV[VW])));
 57		
 58		LLColor4 llcolor4b(r,g,b,a);
 59		ensure("3:LLColor4:Fail to initialize ", ((r == llcolor4b.mV[VX]) && (g == llcolor4b.mV[VY]) && (b == llcolor4b.mV[VZ])&& (a == llcolor4b.mV[VW])));
 60		
 61		const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
 62		LLColor4 llcolor4c(vec);
 63		ensure("4:LLColor4:Fail to initialize ", ((vec[0] == llcolor4c.mV[VX]) && (vec[1] == llcolor4c.mV[VY]) && (vec[2] == llcolor4c.mV[VZ])&& (vec[3] == llcolor4c.mV[VW])));		
 64		
 65		LLColor3 llcolor3(-2.23f,1.01f,42.3f);
 66		F32 val = -.1f;
 67		LLColor4 llcolor4d(llcolor3,val);
 68		ensure("5:LLColor4:Fail to initialize ", ((llcolor3.mV[VX] == llcolor4d.mV[VX]) && (llcolor3.mV[VY] == llcolor4d.mV[VY]) && (llcolor3.mV[VZ] == llcolor4d.mV[VZ])&& (val == llcolor4d.mV[VW])));
 69
 70		LLSD sd = llcolor4d.getValue();
 71		LLColor4 llcolor4e(sd);
 72		ensure_equals("6:LLColor4:(LLSD) failed ", llcolor4d, llcolor4e);
 73		
 74		U8 r1 = 0xF2, g1 = 0xFA, b1 = 0xBF;
 75		LLColor4U color4u(r1,g1,b1);
 76		LLColor4 llcolor4g(color4u);
 77		const F32 SCALE = 1.f/255.f;
 78		F32 r2 = r1*SCALE, g2 = g1* SCALE, b2 = b1* SCALE;
 79		ensure("7:LLColor4:Fail to initialize ", ((r2 == llcolor4g.mV[VX]) && (g2 == llcolor4g.mV[VY]) && (b2 == llcolor4g.mV[VZ])));
 80	}
 81
 82	template<> template<>
 83	void v4color_object::test<2>()
 84	{
 85		LLColor4 llcolor(1.0, 2.0, 3.0, 4.0);
 86		LLSD llsd = llcolor.getValue();
 87		LLColor4 llcolor4(llsd), llcolor4a;
 88		llcolor4a.setValue(llsd);
 89		ensure("setValue: failed", (llcolor4 == llcolor4a));
 90		LLSD sd = llcolor4a.getValue();
 91		LLColor4 llcolor4b(sd);
 92		ensure("getValue: Failed ", (llcolor4b == llcolor4a));
 93	}
 94
 95	template<> template<>
 96	void v4color_object::test<3>()
 97	{
 98		F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 0xAF;
 99		LLColor4 llcolor4(r,g,b,a);
100		llcolor4.setToBlack();
101		ensure("setToBlack:Fail to set the black ", ((0 == llcolor4.mV[VX]) && (0 == llcolor4.mV[VY]) && (0 == llcolor4.mV[VZ])&& (1.0f == llcolor4.mV[VW])));
102
103		llcolor4.setToWhite();
104		ensure("setToWhite:Fail to set the white ", ((1.f == llcolor4.mV[VX]) && (1.f == llcolor4.mV[VY]) && (1.f == llcolor4.mV[VZ])&& (1.0f == llcolor4.mV[VW])));
105	}
106
107	template<> template<>
108	void v4color_object::test<4>()
109	{
110		F32 r = 0x20, g = 0xFFFF, b = 0xFF, a = 0xAF;
111		LLColor4 llcolor4;
112		llcolor4.setVec(r,g,b);
113		ensure("1:setVec:Fail to set the values ", ((r == llcolor4.mV[VX]) && (g == llcolor4.mV[VY]) && (b == llcolor4.mV[VZ])&& (1.f == llcolor4.mV[VW])));
114
115		llcolor4.setVec(r,g,b,a);
116		ensure("2:setVec:Fail to set the values ", ((r == llcolor4.mV[VX]) && (g == llcolor4.mV[VY]) && (b == llcolor4.mV[VZ])&& (a == llcolor4.mV[VW])));
117
118		LLColor4 llcolor4a; 
119		llcolor4a.setVec(llcolor4);
120		ensure_equals("3:setVec:Fail to set the values ", llcolor4a,llcolor4);
121
122		LLColor3 llcolor3(-2.23f,1.01f,42.3f);
123		llcolor4a.setVec(llcolor3);
124		ensure("4:setVec:Fail to set the values ", ((llcolor3.mV[VX] == llcolor4a.mV[VX]) && (llcolor3.mV[VY] == llcolor4a.mV[VY]) && (llcolor3.mV[VZ] == llcolor4a.mV[VZ])));
125
126		F32 val = -.33f;
127		llcolor4a.setVec(llcolor3,val);
128		ensure("4:setVec:Fail to set the values ", ((llcolor3.mV[VX] == llcolor4a.mV[VX]) && (llcolor3.mV[VY] == llcolor4a.mV[VY]) && (llcolor3.mV[VZ] == llcolor4a.mV[VZ]) && (val == llcolor4a.mV[VW])));
129
130		const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
131		LLColor4 llcolor4c;
132		llcolor4c.setVec(vec);
133		ensure("5:setVec:Fail to initialize ", ((vec[0] == llcolor4c.mV[VX]) && (vec[1] == llcolor4c.mV[VY]) && (vec[2] == llcolor4c.mV[VZ])&& (vec[3] == llcolor4c.mV[VW])));		
134
135		U8 r1 = 0xF2, g1 = 0xFA, b1= 0xBF;
136		LLColor4U color4u(r1,g1,b1);
137		llcolor4.setVec(color4u);
138		const F32 SCALE = 1.f/255.f;
139		F32 r2 = r1*SCALE, g2 = g1* SCALE, b2 = b1* SCALE;
140		ensure("6:setVec:Fail to initialize ", ((r2 == llcolor4.mV[VX]) && (g2 == llcolor4.mV[VY]) && (b2 == llcolor4.mV[VZ])));
141	}
142
143	template<> template<>
144	void v4color_object::test<5>()
145	{
146		F32 alpha = 0xAF;
147		LLColor4 llcolor4;
148		llcolor4.setAlpha(alpha);
149		ensure("setAlpha:Fail to initialize ", (alpha == llcolor4.mV[VW]));
150	}
151
152	template<> template<>
153	void v4color_object::test<6>()
154	{
155		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
156		LLColor4 llcolor4(r,g,b);
157		ensure("magVecSquared:Fail ", is_approx_equal(llcolor4.magVecSquared(), (r*r + g*g + b*b)));
158		ensure("magVec:Fail ", is_approx_equal(llcolor4.magVec(), (F32) sqrt(r*r + g*g + b*b)));
159	}
160
161	template<> template<>
162	void v4color_object::test<7>()
163	{
164		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
165		LLColor4 llcolor4(r,g,b);
166		F32 vecMag = llcolor4.normVec();
167		F32 mag = (F32) sqrt(r*r + g*g + b*b);
168		F32 oomag = 1.f / mag;
169		F32 val1 = r * oomag, val2 = g * oomag,	val3 = b * oomag;
170		ensure("1:normVec failed ", (is_approx_equal(val1, llcolor4.mV[0]) && is_approx_equal(val2, llcolor4.mV[1]) && is_approx_equal(val3, llcolor4.mV[2]) && is_approx_equal(vecMag, mag)));
171	}
172
173	template<> template<>
174	void v4color_object::test<8>()
175	{
176		LLColor4 llcolor4;
177		ensure("1:isOpaque failed ",(1 == llcolor4.isOpaque()));
178		F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 1.f;
179		llcolor4.setVec(r,g,b,a);
180		ensure("2:isOpaque failed ",(1 == llcolor4.isOpaque()));
181		a = 2.f;
182		llcolor4.setVec(r,g,b,a);
183		ensure("3:isOpaque failed ",(0 == llcolor4.isOpaque()));
184	}
185
186	template<> template<>
187	void v4color_object::test<9>()
188	{
189		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
190		LLColor4 llcolor4(r,g,b);
191		ensure("1:operator [] failed",( r ==  llcolor4[0]));	
192		ensure("2:operator [] failed",( g ==  llcolor4[1]));
193		ensure("3:operator [] failed",( b ==  llcolor4[2]));
194
195		r = 0xA20, g = 0xFBFF, b = 0xFFF;
196		llcolor4.setVec(r,g,b);
197		F32 &ref1 = llcolor4[0];
198		ensure("4:operator [] failed",( ref1 ==  llcolor4[0]));
199		F32 &ref2 = llcolor4[1];
200		ensure("5:operator [] failed",( ref2 ==  llcolor4[1]));
201		F32 &ref3 = llcolor4[2];
202		ensure("6:operator [] failed",( ref3 ==  llcolor4[2]));
203	}
204
205	template<> template<>
206	void v4color_object::test<10>()
207	{
208		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
209		LLColor3 llcolor3(r,g,b);
210		LLColor4 llcolor4a,llcolor4b;
211		llcolor4a = llcolor3;
212		ensure("Operator=:Fail to initialize ", ((llcolor3.mV[0] == llcolor4a.mV[VX]) && (llcolor3.mV[1] == llcolor4a.mV[VY]) && (llcolor3.mV[2] == llcolor4a.mV[VZ])));
213		LLSD sd = llcolor4a.getValue();
214		llcolor4b = LLColor4(sd);
215		ensure_equals("Operator= LLSD:Fail ", llcolor4a, llcolor4b);
216	}
217
218	template<> template<>
219	void v4color_object::test<11>()
220	{
221		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
222		std::ostringstream stream1, stream2;
223		LLColor4 llcolor4a(r,g,b),llcolor4b;
224		stream1 << llcolor4a;
225		llcolor4b.setVec(r,g,b);
226		stream2 << llcolor4b;
227		ensure("operator << failed ", (stream1.str() == stream2.str()));	
228	}
229
230	template<> template<>
231	void v4color_object::test<12>()
232	{
233		F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF;
234		F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
235		LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
236		llcolor4c = llcolor4b + llcolor4a;
237		ensure("operator+:Fail to Add the values ",  (is_approx_equal(r1+r2,llcolor4c.mV[VX]) && is_approx_equal(g1+g2,llcolor4c.mV[VY]) && is_approx_equal(b1+b2,llcolor4c.mV[VZ])));
238
239		llcolor4b += llcolor4a;
240		ensure("operator+=:Fail to Add the values ",  (is_approx_equal(r1+r2,llcolor4b.mV[VX]) && is_approx_equal(g1+g2,llcolor4b.mV[VY]) && is_approx_equal(b1+b2,llcolor4b.mV[VZ])));
241	}
242
243	template<> template<>
244	void v4color_object::test<13>()
245	{
246		F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF;
247		F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
248		LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
249		llcolor4c = llcolor4a - llcolor4b;
250		ensure("operator-:Fail to subtract the values ",  (is_approx_equal(r1-r2,llcolor4c.mV[VX]) && is_approx_equal(g1-g2,llcolor4c.mV[VY]) && is_approx_equal(b1-b2,llcolor4c.mV[VZ])));
251
252		llcolor4a -= llcolor4b;
253		ensure("operator-=:Fail to subtract the values ",  (is_approx_equal(r1-r2,llcolor4a.mV[VX]) && is_approx_equal(g1-g2,llcolor4a.mV[VY]) && is_approx_equal(b1-b2,llcolor4a.mV[VZ])));
254	}
255
256	template<> template<>
257	void v4color_object::test<14>()
258	{
259		F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF;
260		F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
261		LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
262		llcolor4c = llcolor4a * llcolor4b;
263		ensure("1:operator*:Fail to multiply the values",  (is_approx_equal(r1*r2,llcolor4c.mV[VX]) && is_approx_equal(g1*g2,llcolor4c.mV[VY]) && is_approx_equal(b1*b2,llcolor4c.mV[VZ])));
264		
265		F32 mulVal = 3.33f;
266		llcolor4c = llcolor4a * mulVal;
267		ensure("2:operator*:Fail ",  (is_approx_equal(r1*mulVal,llcolor4c.mV[VX]) && is_approx_equal(g1*mulVal,llcolor4c.mV[VY]) && is_approx_equal(b1*mulVal,llcolor4c.mV[VZ])));
268		llcolor4c = mulVal * llcolor4a;
269		ensure("3:operator*:Fail to multiply the values",  (is_approx_equal(r1*mulVal,llcolor4c.mV[VX]) && is_approx_equal(g1*mulVal,llcolor4c.mV[VY]) && is_approx_equal(b1*mulVal,llcolor4c.mV[VZ])));
270
271		llcolor4a *= mulVal;
272		ensure("4:operator*=:Fail to multiply the values ",  (is_approx_equal(r1*mulVal,llcolor4a.mV[VX]) && is_approx_equal(g1*mulVal,llcolor4a.mV[VY]) && is_approx_equal(b1*mulVal,llcolor4a.mV[VZ])));
273
274		LLColor4 llcolor4d(r1,g1,b1),llcolor4e(r2,g2,b2);
275		llcolor4e *= llcolor4d;
276		ensure("5:operator*=:Fail to multiply the values ",  (is_approx_equal(r1*r2,llcolor4e.mV[VX]) && is_approx_equal(g1*g2,llcolor4e.mV[VY]) && is_approx_equal(b1*b2,llcolor4e.mV[VZ])));
277	}
278
279	template<> template<>
280	void v4color_object::test<15>()
281	{
282		F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 0x30;
283		F32 div = 12.345f;
284		LLColor4 llcolor4a(r,g,b,a),llcolor4b;
285		llcolor4b = llcolor4a % div;//chnage only alpha value nor r,g,b;
286		ensure("1operator%:Fail ",  (is_approx_equal(r,llcolor4b.mV[VX]) && is_approx_equal(g,llcolor4b.mV[VY]) && is_approx_equal(b,llcolor4b.mV[VZ])&& is_approx_equal(div*a,llcolor4b.mV[VW])));
287		
288		llcolor4b = div % llcolor4a;
289		ensure("2operator%:Fail ",  (is_approx_equal(r,llcolor4b.mV[VX]) && is_approx_equal(g,llcolor4b.mV[VY]) && is_approx_equal(b,llcolor4b.mV[VZ])&& is_approx_equal(div*a,llcolor4b.mV[VW])));
290
291		llcolor4a %= div;
292		ensure("operator%=:Fail ",  (is_approx_equal(a*div,llcolor4a.mV[VW])));
293	}
294
295	template<> template<>
296	void v4color_object::test<16>()
297	{
298		F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 0x30;
299		LLColor4 llcolor4a(r,g,b,a),llcolor4b;
300		llcolor4b = llcolor4a;
301		ensure("1:operator== failed to ensure the equality ", (llcolor4b == llcolor4a));	
302		F32 r1 = 0x2, g1 = 0xFF, b1 = 0xFA;
303		LLColor3 llcolor3(r1,g1,b1);
304		llcolor4b = llcolor3;
305		ensure("2:operator== failed to ensure the equality ", (llcolor4b == llcolor3));	
306		ensure("2:operator!= failed to ensure the equality ", (llcolor4a != llcolor3));
307	}
308
309	template<> template<>
310	void v4color_object::test<17>()
311	{
312		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
313		LLColor4 llcolor4a(r,g,b),llcolor4b;
314		LLColor3 llcolor3 = vec4to3(llcolor4a);
315		ensure("vec4to3:Fail to convert vec4 to vec3 ",  (is_approx_equal(llcolor3.mV[VX],llcolor4a.mV[VX]) && is_approx_equal(llcolor3.mV[VY],llcolor4a.mV[VY]) && is_approx_equal(llcolor3.mV[VZ],llcolor4a.mV[VZ])));
316		llcolor4b = vec3to4(llcolor3);
317		ensure_equals("vec3to4:Fail to convert vec3 to vec4 ",  llcolor4b, llcolor4a);
318	}
319
320	template<> template<>
321	void v4color_object::test<18>()
322	{
323		F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF, val = 0x20;
324		F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
325		LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
326		llcolor4c = lerp(llcolor4a,llcolor4b,val);
327		ensure("lerp:Fail ",  (is_approx_equal(r1 + (r2 - r1)* val,llcolor4c.mV[VX]) && is_approx_equal(g1 + (g2 - g1)* val,llcolor4c.mV[VY]) && is_approx_equal(b1 + (b2 - b1)* val,llcolor4c.mV[VZ])));
328	}
329
330	template<> template<>
331	void v4color_object::test<19>()
332	{
333		F32 r = 12.0f, g = -2.3f, b = 1.32f, a = 5.0f;
334		LLColor4 llcolor4a(r,g,b,a),llcolor4b;
335		std::string color("red");
336		LLColor4::parseColor(color, &llcolor4b);
337		ensure_equals("1:parseColor() failed to parse the color value ", llcolor4b, LLColor4::red);
338
339		color = "12.0, -2.3, 1.32, 5.0";
340		LLColor4::parseColor(color, &llcolor4b);
341		llcolor4a = llcolor4a * (1.f / 255.f);
342		ensure_equals("2:parseColor() failed to parse the color value ",  llcolor4a,llcolor4b);
343
344		color = "yellow5";
345		llcolor4a.setVec(r,g,b);
346		LLColor4::parseColor(color, &llcolor4a);
347		ensure_equals("3:parseColor() failed to parse the color value ", llcolor4a, LLColor4::yellow5);
348	}
349
350	template<> template<>
351	void v4color_object::test<20>()
352	{
353		F32 r = 12.0f, g = -2.3f, b = 1.32f, a = 5.0f;
354		LLColor4 llcolor4a(r,g,b,a),llcolor4b;
355		std::string color("12.0, -2.3, 1.32, 5.0");
356		LLColor4::parseColor4(color, &llcolor4b);
357		ensure_equals("parseColor4() failed to parse the color value ",  llcolor4a, llcolor4b);
358	}
359}