summaryrefslogtreecommitdiff
path: root/indra/llmath/tests/v4color_test.cpp
blob: 33921e0f0f659f7ad7fc55a4b4241f8a4d48a0bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
 * @file v4color_test.cpp
 * @author Adroit
 * @date 2007-03
 * @brief v4color test cases.
 *
 * $LicenseInfo:firstyear=2007&license=viewergpl$
 * 
 * Copyright (c) 2007-2009, Linden Research, Inc.
 * 
 * Second Life Viewer Source Code
 * The source code in this file ("Source Code") is provided by Linden Lab
 * to you under the terms of the GNU General Public License, version 2.0
 * ("GPL"), unless you have obtained a separate licensing agreement
 * ("Other License"), formally executed by you and Linden Lab.  Terms of
 * the GPL can be found in doc/GPL-license.txt in this distribution, or
 * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
 * 
 * There are special exceptions to the terms and conditions of the GPL as
 * it is applied to this Source Code. View the full text of the exception
 * in the file doc/FLOSS-exception.txt in this software distribution, or
 * online at
 * http://secondlifegrid.net/programs/open_source/licensing/flossexception
 * 
 * By copying, modifying or distributing this software, you acknowledge
 * that you have read and understood your obligations described above,
 * and agree to abide by those obligations.
 * 
 * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
 * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
 * COMPLETENESS OR PERFORMANCE.
 * $/LicenseInfo$
 */


#include "linden_common.h"
#include "../test/lltut.h"

#include "../v4coloru.h"
#include "llsd.h"
#include "../v3color.h"
#include "../v4color.h"


namespace tut
{
	struct v4color_data
	{
	};
	typedef test_group<v4color_data> v4color_test;
	typedef v4color_test::object v4color_object;
	tut::v4color_test v4color_testcase("v4color");

	template<> template<>
	void v4color_object::test<1>()
	{
		LLColor4 llcolor4;
		ensure("1:LLColor4:Fail to initialize ", ((0 == llcolor4.mV[VX]) && (0 == llcolor4.mV[VY]) && (0 == llcolor4.mV[VZ])&& (1.0f == llcolor4.mV[VW])));

		F32 r = 0x20, g = 0xFFFF, b = 0xFF, a = 0xAF;
		LLColor4 llcolor4a(r,g,b);
		ensure("2:LLColor4:Fail to initialize ", ((r == llcolor4a.mV[VX]) && (g == llcolor4a.mV[VY]) && (b == llcolor4a.mV[VZ])&& (1.0f == llcolor4a.mV[VW])));
		
		LLColor4 llcolor4b(r,g,b,a);
		ensure("3:LLColor4:Fail to initialize ", ((r == llcolor4b.mV[VX]) && (g == llcolor4b.mV[VY]) && (b == llcolor4b.mV[VZ])&& (a == llcolor4b.mV[VW])));
		
		const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
		LLColor4 llcolor4c(vec);
		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])));		
		
		LLColor3 llcolor3(-2.23f,1.01f,42.3f);
		F32 val = -.1f;
		LLColor4 llcolor4d(llcolor3,val);
		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])));

		LLSD sd = llcolor4d.getValue();
		LLColor4 llcolor4e(sd);
		ensure_equals("6:LLColor4:(LLSD) failed ", llcolor4d, llcolor4e);
		
		U8 r1 = 0xF2, g1 = 0xFA, b1 = 0xBF;
		LLColor4U color4u(r1,g1,b1);
		LLColor4 llcolor4g(color4u);
		const F32 SCALE = 1.f/255.f;
		F32 r2 = r1*SCALE, g2 = g1* SCALE, b2 = b1* SCALE;
		ensure("7:LLColor4:Fail to initialize ", ((r2 == llcolor4g.mV[VX]) && (g2 == llcolor4g.mV[VY]) && (b2 == llcolor4g.mV[VZ])));
	}

	template<> template<>
	void v4color_object::test<2>()
	{
		LLColor4 llcolor(1.0, 2.0, 3.0, 4.0);
		LLSD llsd = llcolor.getValue();
		LLColor4 llcolor4(llsd), llcolor4a;
		llcolor4a.setValue(llsd);
		ensure("setValue: failed", (llcolor4 == llcolor4a));
		LLSD sd = llcolor4a.getValue();
		LLColor4 llcolor4b(sd);
		ensure("getValue: Failed ", (llcolor4b == llcolor4a));
	}

	template<> template<>
	void v4color_object::test<3>()
	{
		F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 0xAF;
		LLColor4 llcolor4(r,g,b,a);
		llcolor4.setToBlack();
		ensure("setToBlack:Fail to set the black ", ((0 == llcolor4.mV[VX]) && (0 == llcolor4.mV[VY]) && (0 == llcolor4.mV[VZ])&& (1.0f == llcolor4.mV[VW])));

		llcolor4.setToWhite();
		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])));
	}

	template<> template<>
	void v4color_object::test<4>()
	{
		F32 r = 0x20, g = 0xFFFF, b = 0xFF, a = 0xAF;
		LLColor4 llcolor4;
		llcolor4.setVec(r,g,b);
		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])));

		llcolor4.setVec(r,g,b,a);
		ensure("2:setVec:Fail to set the values ", ((r == llcolor4.mV[VX]) && (g == llcolor4.mV[VY]) && (b == llcolor4.mV[VZ])&& (a == llcolor4.mV[VW])));

		LLColor4 llcolor4a; 
		llcolor4a.setVec(llcolor4);
		ensure_equals("3:setVec:Fail to set the values ", llcolor4a,llcolor4);

		LLColor3 llcolor3(-2.23f,1.01f,42.3f);
		llcolor4a.setVec(llcolor3);
		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])));

		F32 val = -.33f;
		llcolor4a.setVec(llcolor3,val);
		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])));

		const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
		LLColor4 llcolor4c;
		llcolor4c.setVec(vec);
		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])));		

		U8 r1 = 0xF2, g1 = 0xFA, b1= 0xBF;
		LLColor4U color4u(r1,g1,b1);
		llcolor4.setVec(color4u);
		const F32 SCALE = 1.f/255.f;
		F32 r2 = r1*SCALE, g2 = g1* SCALE, b2 = b1* SCALE;
		ensure("6:setVec:Fail to initialize ", ((r2 == llcolor4.mV[VX]) && (g2 == llcolor4.mV[VY]) && (b2 == llcolor4.mV[VZ])));
	}

	template<> template<>
	void v4color_object::test<5>()
	{
		F32 alpha = 0xAF;
		LLColor4 llcolor4;
		llcolor4.setAlpha(alpha);
		ensure("setAlpha:Fail to initialize ", (alpha == llcolor4.mV[VW]));
	}

	template<> template<>
	void v4color_object::test<6>()
	{
		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
		LLColor4 llcolor4(r,g,b);
		ensure("magVecSquared:Fail ", is_approx_equal(llcolor4.magVecSquared(), (r*r + g*g + b*b)));
		ensure("magVec:Fail ", is_approx_equal(llcolor4.magVec(), fsqrtf(r*r + g*g + b*b)));
	}

	template<> template<>
	void v4color_object::test<7>()
	{
		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
		LLColor4 llcolor4(r,g,b);
		F32 vecMag = llcolor4.normVec();
		F32 mag = fsqrtf(r*r + g*g + b*b);
		F32 oomag = 1.f / mag;
		F32 val1 = r * oomag, val2 = g * oomag,	val3 = b * oomag;
		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)));
	}

	template<> template<>
	void v4color_object::test<8>()
	{
		LLColor4 llcolor4;
		ensure("1:isOpaque failed ",(1 == llcolor4.isOpaque()));
		F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 1.f;
		llcolor4.setVec(r,g,b,a);
		ensure("2:isOpaque failed ",(1 == llcolor4.isOpaque()));
		a = 2.f;
		llcolor4.setVec(r,g,b,a);
		ensure("3:isOpaque failed ",(0 == llcolor4.isOpaque()));
	}

	template<> template<>
	void v4color_object::test<9>()
	{
		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
		LLColor4 llcolor4(r,g,b);
		ensure("1:operator [] failed",( r ==  llcolor4[0]));	
		ensure("2:operator [] failed",( g ==  llcolor4[1]));
		ensure("3:operator [] failed",( b ==  llcolor4[2]));

		r = 0xA20, g = 0xFBFF, b = 0xFFF;
		llcolor4.setVec(r,g,b);
		F32 &ref1 = llcolor4[0];
		ensure("4:operator [] failed",( ref1 ==  llcolor4[0]));
		F32 &ref2 = llcolor4[1];
		ensure("5:operator [] failed",( ref2 ==  llcolor4[1]));
		F32 &ref3 = llcolor4[2];
		ensure("6:operator [] failed",( ref3 ==  llcolor4[2]));
	}

	template<> template<>
	void v4color_object::test<10>()
	{
		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
		LLColor3 llcolor3(r,g,b);
		LLColor4 llcolor4a,llcolor4b;
		llcolor4a = llcolor3;
		ensure("Operator=:Fail to initialize ", ((llcolor3.mV[0] == llcolor4a.mV[VX]) && (llcolor3.mV[1] == llcolor4a.mV[VY]) && (llcolor3.mV[2] == llcolor4a.mV[VZ])));
		LLSD sd = llcolor4a.getValue();
		llcolor4b = LLColor4(sd);
		ensure_equals("Operator= LLSD:Fail ", llcolor4a, llcolor4b);
	}

	template<> template<>
	void v4color_object::test<11>()
	{
		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
		std::ostringstream stream1, stream2;
		LLColor4 llcolor4a(r,g,b),llcolor4b;
		stream1 << llcolor4a;
		llcolor4b.setVec(r,g,b);
		stream2 << llcolor4b;
		ensure("operator << failed ", (stream1.str() == stream2.str()));	
	}

	template<> template<>
	void v4color_object::test<12>()
	{
		F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF;
		F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
		LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
		llcolor4c = llcolor4b + llcolor4a;
		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])));

		llcolor4b += llcolor4a;
		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])));
	}

	template<> template<>
	void v4color_object::test<13>()
	{
		F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF;
		F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
		LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
		llcolor4c = llcolor4a - llcolor4b;
		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])));

		llcolor4a -= llcolor4b;
		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])));
	}

	template<> template<>
	void v4color_object::test<14>()
	{
		F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF;
		F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
		LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
		llcolor4c = llcolor4a * llcolor4b;
		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])));
		
		F32 mulVal = 3.33f;
		llcolor4c = llcolor4a * mulVal;
		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])));
		llcolor4c = mulVal * llcolor4a;
		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])));

		llcolor4a *= mulVal;
		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])));

		LLColor4 llcolor4d(r1,g1,b1),llcolor4e(r2,g2,b2);
		llcolor4e *= llcolor4d;
		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])));
	}

	template<> template<>
	void v4color_object::test<15>()
	{
		F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 0x30;
		F32 div = 12.345f;
		LLColor4 llcolor4a(r,g,b,a),llcolor4b;
		llcolor4b = llcolor4a % div;//chnage only alpha value nor r,g,b;
		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])));
		
		llcolor4b = div % llcolor4a;
		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])));

		llcolor4a %= div;
		ensure("operator%=:Fail ",  (is_approx_equal(a*div,llcolor4a.mV[VW])));
	}

	template<> template<>
	void v4color_object::test<16>()
	{
		F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 0x30;
		LLColor4 llcolor4a(r,g,b,a),llcolor4b;
		llcolor4b = llcolor4a;
		ensure("1:operator== failed to ensure the equality ", (llcolor4b == llcolor4a));	
		F32 r1 = 0x2, g1 = 0xFF, b1 = 0xFA;
		LLColor3 llcolor3(r1,g1,b1);
		llcolor4b = llcolor3;
		ensure("2:operator== failed to ensure the equality ", (llcolor4b == llcolor3));	
		ensure("2:operator!= failed to ensure the equality ", (llcolor4a != llcolor3));
	}

	template<> template<>
	void v4color_object::test<17>()
	{
		F32 r = 0x20, g = 0xFFFF, b = 0xFF;
		LLColor4 llcolor4a(r,g,b),llcolor4b;
		LLColor3 llcolor3 = vec4to3(llcolor4a);
		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])));
		llcolor4b = vec3to4(llcolor3);
		ensure_equals("vec3to4:Fail to convert vec3 to vec4 ",  llcolor4b, llcolor4a);
	}

	template<> template<>
	void v4color_object::test<18>()
	{
		F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF, val = 0x20;
		F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
		LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
		llcolor4c = lerp(llcolor4a,llcolor4b,val);
		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])));
	}

	template<> template<>
	void v4color_object::test<19>()
	{
		F32 r = 12.0f, g = -2.3f, b = 1.32f, a = 5.0f;
		LLColor4 llcolor4a(r,g,b,a),llcolor4b;
		std::string color("red");
		LLColor4::parseColor(color, &llcolor4b);
		ensure_equals("1:parseColor() failed to parse the color value ", llcolor4b, LLColor4::red);

		color = "12.0, -2.3, 1.32, 5.0";
		LLColor4::parseColor(color, &llcolor4b);
		llcolor4a = llcolor4a * (1.f / 255.f);
		ensure_equals("2:parseColor() failed to parse the color value ",  llcolor4a,llcolor4b);

		color = "yellow5";
		llcolor4a.setVec(r,g,b);
		LLColor4::parseColor(color, &llcolor4a);
		ensure_equals("3:parseColor() failed to parse the color value ", llcolor4a, LLColor4::yellow5);
	}

	template<> template<>
	void v4color_object::test<20>()
	{
		F32 r = 12.0f, g = -2.3f, b = 1.32f, a = 5.0f;
		LLColor4 llcolor4a(r,g,b,a),llcolor4b;
		std::string color("12.0, -2.3, 1.32, 5.0");
		LLColor4::parseColor4(color, &llcolor4b);
		ensure_equals("parseColor4() failed to parse the color value ",  llcolor4a, llcolor4b);
	}
}