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