PageRenderTime 41ms CodeModel.GetById 6ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/llmath/tests/v4coloru_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 336 lines | 272 code | 37 blank | 27 comment | 90 complexity | cc2e1e5be185be538bf6dcd31a5b3b68 MD5 | raw file
  1/**
  2 * @file v4coloru_test.cpp
  3 * @author Adroit
  4 * @date 2007-03
  5 * @brief v4coloru 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 "llsd.h"
 34
 35#include "../v4coloru.h"
 36
 37
 38namespace tut
 39{
 40	struct v4coloru_data
 41	{
 42	};
 43	typedef test_group<v4coloru_data> v4coloru_test;
 44	typedef v4coloru_test::object v4coloru_object;
 45	tut::v4coloru_test v4coloru_testcase("v4coloru_h");
 46
 47	template<> template<>
 48	void v4coloru_object::test<1>()
 49	{
 50		LLColor4U llcolor4u;
 51		ensure("1:LLColor4u:Fail to initialize ", ((0 == llcolor4u.mV[VX]) && (0 == llcolor4u.mV[VY]) && (0 == llcolor4u.mV[VZ])&& (255 == llcolor4u.mV[VW])));
 52
 53		U8 r = 0x12, g = 0xFF, b = 0xAF, a = 0x23;
 54		LLColor4U llcolor4u1(r,g,b);
 55		ensure("2:LLColor4u:Fail to initialize ", ((r == llcolor4u1.mV[VX]) && (g == llcolor4u1.mV[VY]) && (b == llcolor4u1.mV[VZ])&& (255 == llcolor4u1.mV[VW])));
 56		
 57		LLColor4U llcolor4u2(r,g,b,a);
 58		ensure("3:LLColor4u:Fail to initialize ", ((r == llcolor4u2.mV[VX]) && (g == llcolor4u2.mV[VY]) && (b == llcolor4u2.mV[VZ])&& (a == llcolor4u2.mV[VW])));
 59
 60		const U8 vec[4] = {0x12,0xFF,0xAF,0x23};
 61		LLColor4U llcolor4u3(vec);
 62		ensure("4:LLColor4u:Fail to initialize ", ((vec[0] == llcolor4u3.mV[VX]) && (vec[1] == llcolor4u3.mV[VY]) && (vec[2] == llcolor4u3.mV[VZ])&& (vec[3] == llcolor4u3.mV[VW])));		
 63
 64		LLSD sd = llcolor4u3.getValue();
 65		LLColor4U llcolor4u4(sd);
 66		ensure_equals("5:LLColor4u (LLSD) Failed ", llcolor4u4, llcolor4u3);
 67	}
 68	
 69	template<> template<>
 70	void v4coloru_object::test<2>()
 71	{
 72		LLColor4U llcolor4ua(1, 2, 3, 4);
 73		LLSD sd = llcolor4ua.getValue();
 74		LLColor4U llcolor4u;
 75		llcolor4u.setValue(sd);
 76		ensure_equals("setValue(LLSD)/getValue Failed ", llcolor4u, llcolor4ua);
 77	}
 78
 79	template<> template<>
 80	void v4coloru_object::test<3>()
 81	{
 82		U8 r = 0x12, g = 0xFF, b = 0xAF, a = 0x23;
 83		LLColor4U llcolor4u(r,g,b,a);
 84		llcolor4u.setToBlack();
 85		ensure("setToBlack:Fail to set black ", ((0 == llcolor4u.mV[VX]) && (0 == llcolor4u.mV[VY]) && (0 == llcolor4u.mV[VZ])&& (255 == llcolor4u.mV[VW])));
 86
 87		llcolor4u.setToWhite();
 88		ensure("setToWhite:Fail to white ", ((255 == llcolor4u.mV[VX]) && (255 == llcolor4u.mV[VY]) && (255 == llcolor4u.mV[VZ])&& (255 == llcolor4u.mV[VW])));
 89	}
 90	
 91	template<> template<>
 92	void v4coloru_object::test<4>()
 93	{
 94		U8 r = 0x12, g = 0xFF, b = 0xAF, a = 0x23;
 95		LLColor4U llcolor4ua(r,g,b,a);
 96		LLSD sd = llcolor4ua.getValue();
 97		LLColor4U llcolor4u = (LLColor4U)sd;
 98		ensure_equals("Operator=(LLSD) Failed ",  llcolor4u, llcolor4ua);
 99	}
100
101	template<> template<>
102	void v4coloru_object::test<5>()
103	{
104		U8 r = 0x12, g = 0xFF, b = 0xAF, a = 0x23;
105		LLColor4U llcolor4u;
106		llcolor4u.setVec(r,g,b,a);
107		ensure("1:setVec:Fail to set the values ", ((r == llcolor4u.mV[VX]) && (g == llcolor4u.mV[VY]) && (b == llcolor4u.mV[VZ])&& (a == llcolor4u.mV[VW])));
108
109		llcolor4u.setToBlack();
110		llcolor4u.setVec(r,g,b);
111		ensure("2:setVec:Fail to set the values ", ((r == llcolor4u.mV[VX]) && (g == llcolor4u.mV[VY]) && (b == llcolor4u.mV[VZ])&& (255 == llcolor4u.mV[VW])));
112
113		LLColor4U llcolor4u1;
114		llcolor4u1.setVec(llcolor4u);
115		ensure_equals("3:setVec:Fail to set the values ", llcolor4u1,llcolor4u);
116		
117		const U8 vec[4] = {0x12,0xFF,0xAF,0x23};
118		LLColor4U llcolor4u2;
119		llcolor4u2.setVec(vec);
120		ensure("4:setVec:Fail to set the values ", ((vec[0] == llcolor4u2.mV[VX]) && (vec[1] == llcolor4u2.mV[VY]) && (vec[2] == llcolor4u2.mV[VZ])&& (vec[3] == llcolor4u2.mV[VW])));		
121	}
122	
123	template<> template<>
124	void v4coloru_object::test<6>()
125	{
126		U8 alpha = 0x12;
127		LLColor4U llcolor4u;
128		llcolor4u.setAlpha(alpha);
129		ensure("setAlpha:Fail to set alpha value ", (alpha == llcolor4u.mV[VW]));
130	}
131	
132	template<> template<>
133	void v4coloru_object::test<7>()
134	{
135		U8 r = 0x12, g = 0xFF, b = 0xAF;
136		LLColor4U llcolor4u(r,g,b);
137		ensure("magVecSquared:Fail ", is_approx_equal(llcolor4u.magVecSquared(), (F32)(r*r + g*g + b*b)));
138		ensure("magVec:Fail ", is_approx_equal(llcolor4u.magVec(), (F32) sqrt((F32) (r*r + g*g + b*b))));
139	}
140
141	template<> template<>
142	void v4coloru_object::test<8>()
143	{
144		U8 r = 0x12, g = 0xFF, b = 0xAF;
145		std::ostringstream stream1, stream2;
146		LLColor4U llcolor4u1(r,g,b),llcolor4u2;
147		stream1 << llcolor4u1;
148		llcolor4u2.setVec(r,g,b);
149		stream2 << llcolor4u2;
150		ensure("operator << failed ", (stream1.str() == stream2.str()));	
151	}
152
153	template<> template<>
154	void v4coloru_object::test<9>()
155	{
156		U8 r1 = 0x12, g1 = 0xFF, b1 = 0xAF;
157		U8 r2 = 0x1C, g2 = 0x9A, b2 = 0x1B;
158		LLColor4U llcolor4u1(r1,g1,b1), llcolor4u2(r2,g2,b2),llcolor4u3;
159		llcolor4u3 = llcolor4u1 + llcolor4u2;
160		ensure_equals(
161			"1a.operator+:Fail to Add the values ",
162			llcolor4u3.mV[VX],
163			(U8)(r1+r2));
164		ensure_equals(
165			"1b.operator+:Fail to Add the values ",
166			llcolor4u3.mV[VY],
167			(U8)(g1+g2));
168		ensure_equals(
169			"1c.operator+:Fail to Add the values ",
170			llcolor4u3.mV[VZ],
171			(U8)(b1+b2));
172
173		llcolor4u2 += llcolor4u1;
174		ensure_equals(
175			"2a.operator+=:Fail to Add the values ",
176			llcolor4u2.mV[VX],
177			(U8)(r1+r2));
178		ensure_equals(
179			"2b.operator+=:Fail to Add the values ",
180			llcolor4u2.mV[VY],
181			(U8)(g1+g2));
182		ensure_equals(
183			"2c.operator+=:Fail to Add the values ",
184			llcolor4u2.mV[VZ],
185			(U8)(b1+b2));
186	}
187
188	template<> template<>
189	void v4coloru_object::test<10>()
190	{
191		U8 r1 = 0x12, g1 = 0xFF, b1 = 0xAF;
192		U8 r2 = 0x1C, g2 = 0x9A, b2 = 0x1B;
193		LLColor4U llcolor4u1(r1,g1,b1), llcolor4u2(r2,g2,b2),llcolor4u3;
194		llcolor4u3 = llcolor4u1 - llcolor4u2;
195		ensure_equals(
196			"1a. operator-:Fail to Add the values ",
197			llcolor4u3.mV[VX],
198			(U8)(r1-r2));
199		ensure_equals(
200			"1b. operator-:Fail to Add the values ",
201			llcolor4u3.mV[VY],
202			(U8)(g1-g2));
203		ensure_equals(
204			"1c. operator-:Fail to Add the values ",
205			llcolor4u3.mV[VZ],
206			(U8)(b1-b2));
207
208		llcolor4u1 -= llcolor4u2;
209		ensure_equals(
210			"2a. operator-=:Fail to Add the values ",
211			llcolor4u1.mV[VX],
212			(U8)(r1-r2));
213		ensure_equals(
214			"2b. operator-=:Fail to Add the values ",
215			llcolor4u1.mV[VY],
216			(U8)(g1-g2));
217		ensure_equals(
218			"2c. operator-=:Fail to Add the values ",
219			llcolor4u1.mV[VZ],
220			(U8)(b1-b2));
221	}
222
223	template<> template<>
224	void v4coloru_object::test<11>()
225	{
226		U8 r1 = 0x12, g1 = 0xFF, b1 = 0xAF;
227		U8 r2 = 0x1C, g2 = 0x9A, b2 = 0x1B;
228		LLColor4U llcolor4u1(r1,g1,b1), llcolor4u2(r2,g2,b2),llcolor4u3;
229		llcolor4u3 = llcolor4u1 * llcolor4u2;
230		ensure_equals(
231			"1a. operator*:Fail to multiply the values",
232			llcolor4u3.mV[VX],
233			(U8)(r1*r2));
234		ensure_equals(
235			"1b. operator*:Fail to multiply the values",
236			llcolor4u3.mV[VY],
237			(U8)(g1*g2));
238		ensure_equals(
239			"1c. operator*:Fail to multiply the values",
240			llcolor4u3.mV[VZ],
241			(U8)(b1*b2));
242		
243		U8 mulVal = 123;
244		llcolor4u1 *= mulVal;
245		ensure_equals(
246			"2a. operator*=:Fail to multiply the values",
247			llcolor4u1.mV[VX],
248			(U8)(r1*mulVal));
249		ensure_equals(
250			"2b. operator*=:Fail to multiply the values",
251			llcolor4u1.mV[VY],
252			(U8)(g1*mulVal));
253		ensure_equals(
254			"2c. operator*=:Fail to multiply the values",
255			llcolor4u1.mV[VZ],
256			(U8)(b1*mulVal));
257	}
258
259	template<> template<>
260	void v4coloru_object::test<12>()
261	{
262		U8 r = 0x12, g = 0xFF, b = 0xAF;
263		LLColor4U llcolor4u(r,g,b),llcolor4u1;
264		llcolor4u1 = llcolor4u;
265		ensure("operator== failed to ensure the equality ", (llcolor4u1 == llcolor4u));	
266		llcolor4u1.setToBlack();
267		ensure("operator!= failed to ensure the equality ", (llcolor4u1 != llcolor4u));	
268	}
269
270	template<> template<>
271	void v4coloru_object::test<13>()
272	{
273		U8 r = 0x12, g = 0xFF, b = 0xAF, a = 12;
274		LLColor4U llcolor4u(r,g,b,a);
275		U8 modVal = 45;
276		llcolor4u %= modVal;
277		ensure_equals("operator%=:Fail ", llcolor4u.mV[VW], (U8)(a * modVal));
278	}
279
280	template<> template<>
281	void v4coloru_object::test<14>()
282	{
283		U8 r = 0x12, g = 0xFF, b = 0xAF, a = 12;
284		LLColor4U llcolor4u1(r,g,b,a);
285		std::string color("12, 23, 132, 50");
286		LLColor4U::parseColor4U(color, &llcolor4u1);
287		ensure("parseColor4U() failed to parse the color value ", ((12 == llcolor4u1.mV[VX]) && (23 == llcolor4u1.mV[VY]) && (132 == llcolor4u1.mV[VZ])&& (50 == llcolor4u1.mV[VW])));
288
289		color = "12, 23, 132";
290		ensure("2:parseColor4U() failed to parse the color value ",  (FALSE == LLColor4U::parseColor4U(color, &llcolor4u1)));
291
292		color = "12";
293		ensure("2:parseColor4U() failed to parse the color value ",  (FALSE == LLColor4U::parseColor4U(color, &llcolor4u1)));
294	}
295
296	template<> template<>
297	void v4coloru_object::test<15>()
298	{
299		U8 r = 12, g = 123, b = 3, a = 2;
300		LLColor4U llcolor4u(r,g,b,a),llcolor4u1;
301		const F32 fVal = 3.f;
302		llcolor4u1 = llcolor4u.multAll(fVal);
303		ensure("multAll:Fail to multiply ", (((U8)llround(r * fVal) == llcolor4u1.mV[VX]) && (U8)llround(g * fVal) == llcolor4u1.mV[VY]
304											&& ((U8)llround(b * fVal) == llcolor4u1.mV[VZ])&& ((U8)llround(a * fVal) == llcolor4u1.mV[VW])));		
305	}
306
307	template<> template<>
308	void v4coloru_object::test<16>()
309	{
310		U8 r1 = 12, g1 = 123, b1 = 3, a1 = 2;
311		U8 r2 = 23, g2 = 230, b2 = 124, a2 = 255;
312		LLColor4U llcolor4u(r1,g1,b1,a1),llcolor4u1(r2,g2,b2,a2);
313		llcolor4u1 = llcolor4u1.addClampMax(llcolor4u);
314		ensure("1:addClampMax():Fail to add the value ",  ((r1+r2 == llcolor4u1.mV[VX]) && (255 == llcolor4u1.mV[VY]) && (b1+b2 == llcolor4u1.mV[VZ])&& (255 == llcolor4u1.mV[VW])));
315
316		r1 = 132, g1 = 3, b1 = 3, a1 = 2;
317		r2 = 123, g2 = 230, b2 = 154, a2 = 25;
318		LLColor4U llcolor4u2(r1,g1,b1,a1),llcolor4u3(r2,g2,b2,a2);
319		llcolor4u3 = llcolor4u3.addClampMax(llcolor4u2);
320		ensure("2:addClampMax():Fail to add the value ",  ((255 == llcolor4u3.mV[VX]) && (g1+g2 == llcolor4u3.mV[VY]) && (b1+b2 == llcolor4u3.mV[VZ])&& (a1+a2 == llcolor4u3.mV[VW])));
321	}
322
323	template<> template<>
324	void v4coloru_object::test<17>()
325	{
326		F32 r = 23.f, g = 12.32f, b = -12.3f;
327		LLColor3 color3(r,g,b);
328		LLColor4U llcolor4u;
329		llcolor4u.setVecScaleClamp(color3);
330		const S32 MAX_COLOR = 255;
331		F32 color_scale_factor = MAX_COLOR/r;
332		S32 r2 = llround(r * color_scale_factor);
333		S32 g2 = llround(g * color_scale_factor);
334		ensure("setVecScaleClamp():Fail to add the value ",  ((r2 == llcolor4u.mV[VX]) && (g2 == llcolor4u.mV[VY]) && (0 == llcolor4u.mV[VZ])&& (255 == llcolor4u.mV[VW])));
335	}
336}