v4color_test.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:14k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /**
  2.  * @file v4color_test.cpp
  3.  * @author Adroit
  4.  * @date 2007-03
  5.  * @brief v4color test cases.
  6.  *
  7.  * $LicenseInfo:firstyear=2007&license=viewergpl$
  8.  * 
  9.  * Copyright (c) 2007-2010, Linden Research, Inc.
  10.  * 
  11.  * Second Life Viewer Source Code
  12.  * The source code in this file ("Source Code") is provided by Linden Lab
  13.  * to you under the terms of the GNU General Public License, version 2.0
  14.  * ("GPL"), unless you have obtained a separate licensing agreement
  15.  * ("Other License"), formally executed by you and Linden Lab.  Terms of
  16.  * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17.  * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18.  * 
  19.  * There are special exceptions to the terms and conditions of the GPL as
  20.  * it is applied to this Source Code. View the full text of the exception
  21.  * in the file doc/FLOSS-exception.txt in this software distribution, or
  22.  * online at
  23.  * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24.  * 
  25.  * By copying, modifying or distributing this software, you acknowledge
  26.  * that you have read and understood your obligations described above,
  27.  * and agree to abide by those obligations.
  28.  * 
  29.  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30.  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31.  * COMPLETENESS OR PERFORMANCE.
  32.  * $/LicenseInfo$
  33.  */
  34. #include "linden_common.h"
  35. #include "../test/lltut.h"
  36. #include "../v4coloru.h"
  37. #include "llsd.h"
  38. #include "../v3color.h"
  39. #include "../v4color.h"
  40. namespace tut
  41. {
  42. struct v4color_data
  43. {
  44. };
  45. typedef test_group<v4color_data> v4color_test;
  46. typedef v4color_test::object v4color_object;
  47. tut::v4color_test v4color_testcase("v4color");
  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. F32 r = 0x20, g = 0xFFFF, b = 0xFF, a = 0xAF;
  54. LLColor4 llcolor4a(r,g,b);
  55. ensure("2:LLColor4:Fail to initialize ", ((r == llcolor4a.mV[VX]) && (g == llcolor4a.mV[VY]) && (b == llcolor4a.mV[VZ])&& (1.0f == llcolor4a.mV[VW])));
  56. LLColor4 llcolor4b(r,g,b,a);
  57. ensure("3:LLColor4:Fail to initialize ", ((r == llcolor4b.mV[VX]) && (g == llcolor4b.mV[VY]) && (b == llcolor4b.mV[VZ])&& (a == llcolor4b.mV[VW])));
  58. const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
  59. LLColor4 llcolor4c(vec);
  60. 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])));
  61. LLColor3 llcolor3(-2.23f,1.01f,42.3f);
  62. F32 val = -.1f;
  63. LLColor4 llcolor4d(llcolor3,val);
  64. 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])));
  65. LLSD sd = llcolor4d.getValue();
  66. LLColor4 llcolor4e(sd);
  67. ensure_equals("6:LLColor4:(LLSD) failed ", llcolor4d, llcolor4e);
  68. U8 r1 = 0xF2, g1 = 0xFA, b1 = 0xBF;
  69. LLColor4U color4u(r1,g1,b1);
  70. LLColor4 llcolor4g(color4u);
  71. const F32 SCALE = 1.f/255.f;
  72. F32 r2 = r1*SCALE, g2 = g1* SCALE, b2 = b1* SCALE;
  73. ensure("7:LLColor4:Fail to initialize ", ((r2 == llcolor4g.mV[VX]) && (g2 == llcolor4g.mV[VY]) && (b2 == llcolor4g.mV[VZ])));
  74. }
  75. template<> template<>
  76. void v4color_object::test<2>()
  77. {
  78. LLColor4 llcolor(1.0, 2.0, 3.0, 4.0);
  79. LLSD llsd = llcolor.getValue();
  80. LLColor4 llcolor4(llsd), llcolor4a;
  81. llcolor4a.setValue(llsd);
  82. ensure("setValue: failed", (llcolor4 == llcolor4a));
  83. LLSD sd = llcolor4a.getValue();
  84. LLColor4 llcolor4b(sd);
  85. ensure("getValue: Failed ", (llcolor4b == llcolor4a));
  86. }
  87. template<> template<>
  88. void v4color_object::test<3>()
  89. {
  90. F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 0xAF;
  91. LLColor4 llcolor4(r,g,b,a);
  92. llcolor4.setToBlack();
  93. ensure("setToBlack:Fail to set the black ", ((0 == llcolor4.mV[VX]) && (0 == llcolor4.mV[VY]) && (0 == llcolor4.mV[VZ])&& (1.0f == llcolor4.mV[VW])));
  94. llcolor4.setToWhite();
  95. 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])));
  96. }
  97. template<> template<>
  98. void v4color_object::test<4>()
  99. {
  100. F32 r = 0x20, g = 0xFFFF, b = 0xFF, a = 0xAF;
  101. LLColor4 llcolor4;
  102. llcolor4.setVec(r,g,b);
  103. 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])));
  104. llcolor4.setVec(r,g,b,a);
  105. ensure("2:setVec:Fail to set the values ", ((r == llcolor4.mV[VX]) && (g == llcolor4.mV[VY]) && (b == llcolor4.mV[VZ])&& (a == llcolor4.mV[VW])));
  106. LLColor4 llcolor4a; 
  107. llcolor4a.setVec(llcolor4);
  108. ensure_equals("3:setVec:Fail to set the values ", llcolor4a,llcolor4);
  109. LLColor3 llcolor3(-2.23f,1.01f,42.3f);
  110. llcolor4a.setVec(llcolor3);
  111. 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])));
  112. F32 val = -.33f;
  113. llcolor4a.setVec(llcolor3,val);
  114. 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])));
  115. const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
  116. LLColor4 llcolor4c;
  117. llcolor4c.setVec(vec);
  118. 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])));
  119. U8 r1 = 0xF2, g1 = 0xFA, b1= 0xBF;
  120. LLColor4U color4u(r1,g1,b1);
  121. llcolor4.setVec(color4u);
  122. const F32 SCALE = 1.f/255.f;
  123. F32 r2 = r1*SCALE, g2 = g1* SCALE, b2 = b1* SCALE;
  124. ensure("6:setVec:Fail to initialize ", ((r2 == llcolor4.mV[VX]) && (g2 == llcolor4.mV[VY]) && (b2 == llcolor4.mV[VZ])));
  125. }
  126. template<> template<>
  127. void v4color_object::test<5>()
  128. {
  129. F32 alpha = 0xAF;
  130. LLColor4 llcolor4;
  131. llcolor4.setAlpha(alpha);
  132. ensure("setAlpha:Fail to initialize ", (alpha == llcolor4.mV[VW]));
  133. }
  134. template<> template<>
  135. void v4color_object::test<6>()
  136. {
  137. F32 r = 0x20, g = 0xFFFF, b = 0xFF;
  138. LLColor4 llcolor4(r,g,b);
  139. ensure("magVecSquared:Fail ", is_approx_equal(llcolor4.magVecSquared(), (r*r + g*g + b*b)));
  140. ensure("magVec:Fail ", is_approx_equal(llcolor4.magVec(), fsqrtf(r*r + g*g + b*b)));
  141. }
  142. template<> template<>
  143. void v4color_object::test<7>()
  144. {
  145. F32 r = 0x20, g = 0xFFFF, b = 0xFF;
  146. LLColor4 llcolor4(r,g,b);
  147. F32 vecMag = llcolor4.normVec();
  148. F32 mag = fsqrtf(r*r + g*g + b*b);
  149. F32 oomag = 1.f / mag;
  150. F32 val1 = r * oomag, val2 = g * oomag, val3 = b * oomag;
  151. 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)));
  152. }
  153. template<> template<>
  154. void v4color_object::test<8>()
  155. {
  156. LLColor4 llcolor4;
  157. ensure("1:isOpaque failed ",(1 == llcolor4.isOpaque()));
  158. F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 1.f;
  159. llcolor4.setVec(r,g,b,a);
  160. ensure("2:isOpaque failed ",(1 == llcolor4.isOpaque()));
  161. a = 2.f;
  162. llcolor4.setVec(r,g,b,a);
  163. ensure("3:isOpaque failed ",(0 == llcolor4.isOpaque()));
  164. }
  165. template<> template<>
  166. void v4color_object::test<9>()
  167. {
  168. F32 r = 0x20, g = 0xFFFF, b = 0xFF;
  169. LLColor4 llcolor4(r,g,b);
  170. ensure("1:operator [] failed",( r ==  llcolor4[0]));
  171. ensure("2:operator [] failed",( g ==  llcolor4[1]));
  172. ensure("3:operator [] failed",( b ==  llcolor4[2]));
  173. r = 0xA20, g = 0xFBFF, b = 0xFFF;
  174. llcolor4.setVec(r,g,b);
  175. F32 &ref1 = llcolor4[0];
  176. ensure("4:operator [] failed",( ref1 ==  llcolor4[0]));
  177. F32 &ref2 = llcolor4[1];
  178. ensure("5:operator [] failed",( ref2 ==  llcolor4[1]));
  179. F32 &ref3 = llcolor4[2];
  180. ensure("6:operator [] failed",( ref3 ==  llcolor4[2]));
  181. }
  182. template<> template<>
  183. void v4color_object::test<10>()
  184. {
  185. F32 r = 0x20, g = 0xFFFF, b = 0xFF;
  186. LLColor3 llcolor3(r,g,b);
  187. LLColor4 llcolor4a,llcolor4b;
  188. llcolor4a = llcolor3;
  189. ensure("Operator=:Fail to initialize ", ((llcolor3.mV[0] == llcolor4a.mV[VX]) && (llcolor3.mV[1] == llcolor4a.mV[VY]) && (llcolor3.mV[2] == llcolor4a.mV[VZ])));
  190. LLSD sd = llcolor4a.getValue();
  191. llcolor4b = LLColor4(sd);
  192. ensure_equals("Operator= LLSD:Fail ", llcolor4a, llcolor4b);
  193. }
  194. template<> template<>
  195. void v4color_object::test<11>()
  196. {
  197. F32 r = 0x20, g = 0xFFFF, b = 0xFF;
  198. std::ostringstream stream1, stream2;
  199. LLColor4 llcolor4a(r,g,b),llcolor4b;
  200. stream1 << llcolor4a;
  201. llcolor4b.setVec(r,g,b);
  202. stream2 << llcolor4b;
  203. ensure("operator << failed ", (stream1.str() == stream2.str()));
  204. }
  205. template<> template<>
  206. void v4color_object::test<12>()
  207. {
  208. F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF;
  209. F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
  210. LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
  211. llcolor4c = llcolor4b + llcolor4a;
  212. 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])));
  213. llcolor4b += llcolor4a;
  214. 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])));
  215. }
  216. template<> template<>
  217. void v4color_object::test<13>()
  218. {
  219. F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF;
  220. F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
  221. LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
  222. llcolor4c = llcolor4a - llcolor4b;
  223. 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])));
  224. llcolor4a -= llcolor4b;
  225. 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])));
  226. }
  227. template<> template<>
  228. void v4color_object::test<14>()
  229. {
  230. F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF;
  231. F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
  232. LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
  233. llcolor4c = llcolor4a * llcolor4b;
  234. 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])));
  235. F32 mulVal = 3.33f;
  236. llcolor4c = llcolor4a * mulVal;
  237. 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])));
  238. llcolor4c = mulVal * llcolor4a;
  239. 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])));
  240. llcolor4a *= mulVal;
  241. 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])));
  242. LLColor4 llcolor4d(r1,g1,b1),llcolor4e(r2,g2,b2);
  243. llcolor4e *= llcolor4d;
  244. 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])));
  245. }
  246. template<> template<>
  247. void v4color_object::test<15>()
  248. {
  249. F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 0x30;
  250. F32 div = 12.345f;
  251. LLColor4 llcolor4a(r,g,b,a),llcolor4b;
  252. llcolor4b = llcolor4a % div;//chnage only alpha value nor r,g,b;
  253. 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])));
  254. llcolor4b = div % llcolor4a;
  255. 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])));
  256. llcolor4a %= div;
  257. ensure("operator%=:Fail ",  (is_approx_equal(a*div,llcolor4a.mV[VW])));
  258. }
  259. template<> template<>
  260. void v4color_object::test<16>()
  261. {
  262. F32 r = 0x20, g = 0xFFFF, b = 0xFF,a = 0x30;
  263. LLColor4 llcolor4a(r,g,b,a),llcolor4b;
  264. llcolor4b = llcolor4a;
  265. ensure("1:operator== failed to ensure the equality ", (llcolor4b == llcolor4a));
  266. F32 r1 = 0x2, g1 = 0xFF, b1 = 0xFA;
  267. LLColor3 llcolor3(r1,g1,b1);
  268. llcolor4b = llcolor3;
  269. ensure("2:operator== failed to ensure the equality ", (llcolor4b == llcolor3));
  270. ensure("2:operator!= failed to ensure the equality ", (llcolor4a != llcolor3));
  271. }
  272. template<> template<>
  273. void v4color_object::test<17>()
  274. {
  275. F32 r = 0x20, g = 0xFFFF, b = 0xFF;
  276. LLColor4 llcolor4a(r,g,b),llcolor4b;
  277. LLColor3 llcolor3 = vec4to3(llcolor4a);
  278. 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])));
  279. llcolor4b = vec3to4(llcolor3);
  280. ensure_equals("vec3to4:Fail to convert vec3 to vec4 ",  llcolor4b, llcolor4a);
  281. }
  282. template<> template<>
  283. void v4color_object::test<18>()
  284. {
  285. F32 r1 = 0x20, g1 = 0xFFFF, b1 = 0xFF, val = 0x20;
  286. F32 r2 = 0xABF, g2 = 0xFB, b2 = 0xFFF;
  287. LLColor4 llcolor4a(r1,g1,b1),llcolor4b(r2,g2,b2),llcolor4c;
  288. llcolor4c = lerp(llcolor4a,llcolor4b,val);
  289. 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])));
  290. }
  291. template<> template<>
  292. void v4color_object::test<19>()
  293. {
  294. F32 r = 12.0f, g = -2.3f, b = 1.32f, a = 5.0f;
  295. LLColor4 llcolor4a(r,g,b,a),llcolor4b;
  296. std::string color("red");
  297. LLColor4::parseColor(color, &llcolor4b);
  298. ensure_equals("1:parseColor() failed to parse the color value ", llcolor4b, LLColor4::red);
  299. color = "12.0, -2.3, 1.32, 5.0";
  300. LLColor4::parseColor(color, &llcolor4b);
  301. llcolor4a = llcolor4a * (1.f / 255.f);
  302. ensure_equals("2:parseColor() failed to parse the color value ",  llcolor4a,llcolor4b);
  303. color = "yellow5";
  304. llcolor4a.setVec(r,g,b);
  305. LLColor4::parseColor(color, &llcolor4a);
  306. ensure_equals("3:parseColor() failed to parse the color value ", llcolor4a, LLColor4::yellow5);
  307. }
  308. template<> template<>
  309. void v4color_object::test<20>()
  310. {
  311. F32 r = 12.0f, g = -2.3f, b = 1.32f, a = 5.0f;
  312. LLColor4 llcolor4a(r,g,b,a),llcolor4b;
  313. std::string color("12.0, -2.3, 1.32, 5.0");
  314. LLColor4::parseColor4(color, &llcolor4b);
  315. ensure_equals("parseColor4() failed to parse the color value ",  llcolor4a, llcolor4b);
  316. }
  317. }