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