PageRenderTime 63ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/tests/v3color_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 309 lines | 254 code | 28 blank | 27 comment | 75 complexity | 81f7c2d96b199a78ac56f04dd0ebd70d MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file v3color_test.cpp
  3. * @author Adroit
  4. * @date 2007-03
  5. * @brief v3color 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 "../v3color.h"
  31. namespace tut
  32. {
  33. struct v3color_data
  34. {
  35. };
  36. typedef test_group<v3color_data> v3color_test;
  37. typedef v3color_test::object v3color_object;
  38. tut::v3color_test v3color_testcase("v3color_h");
  39. template<> template<>
  40. void v3color_object::test<1>()
  41. {
  42. LLColor3 llcolor3;
  43. ensure("1:LLColor3:Fail to default-initialize ", (0.0f == llcolor3.mV[0]) && (0.0f == llcolor3.mV[1]) && (0.0f == llcolor3.mV[2]));
  44. F32 r = 2.0f, g = 3.2f, b = 1.f;
  45. F32 v1,v2,v3;
  46. LLColor3 llcolor3a(r,g,b);
  47. ensure("2:LLColor3:Fail to initialize " ,(2.0f == llcolor3a.mV[0]) && (3.2f == llcolor3a.mV[1]) && (1.f == llcolor3a.mV[2]));
  48. const F32 vec[3] = {2.0f, 3.2f,1.f};
  49. LLColor3 llcolor3b(vec);
  50. ensure("3:LLColor3:Fail to initialize " ,(2.0f == llcolor3b.mV[0]) && (3.2f == llcolor3b.mV[1]) && (1.f == llcolor3b.mV[2]));
  51. const char* str = "561122";
  52. LLColor3 llcolor3c(str);
  53. v1 = (F32)86.0f/255.0f; // 0x56 = 86
  54. v2 = (F32)17.0f/255.0f; // 0x11 = 17
  55. v3 = (F32)34.0f/255.f; // 0x22 = 34
  56. ensure("4:LLColor3:Fail to initialize " , is_approx_equal(v1, llcolor3c.mV[0]) && is_approx_equal(v2, llcolor3c.mV[1]) && is_approx_equal(v3, llcolor3c.mV[2]));
  57. }
  58. template<> template<>
  59. void v3color_object::test<2>()
  60. {
  61. LLColor3 llcolor3;
  62. llcolor3.setToBlack();
  63. ensure("setToBlack:Fail to set black ", ((llcolor3.mV[0] == 0.f) && (llcolor3.mV[1] == 0.f) && (llcolor3.mV[2] == 0.f)));
  64. llcolor3.setToWhite();
  65. ensure("setToWhite:Fail to set white ", ((llcolor3.mV[0] == 1.f) && (llcolor3.mV[1] == 1.f) && (llcolor3.mV[2] == 1.f)));
  66. }
  67. template<> template<>
  68. void v3color_object::test<3>()
  69. {
  70. F32 r = 2.3436212f, g = 1231.f, b = 4.7849321232f;
  71. LLColor3 llcolor3, llcolor3a;
  72. llcolor3.setVec(r,g,b);
  73. ensure("1:setVec(r,g,b) Fail ",((r == llcolor3.mV[0]) && (g == llcolor3.mV[1]) && (b == llcolor3.mV[2])));
  74. llcolor3a.setVec(llcolor3);
  75. ensure_equals("2:setVec(LLColor3) Fail ", llcolor3,llcolor3a);
  76. F32 vec[3] = {1.2324f, 2.45634f, .234563f};
  77. llcolor3.setToBlack();
  78. llcolor3.setVec(vec);
  79. ensure("3:setVec(F32*) Fail ",((vec[0] == llcolor3.mV[0]) && (vec[1] == llcolor3.mV[1]) && (vec[2] == llcolor3.mV[2])));
  80. }
  81. template<> template<>
  82. void v3color_object::test<4>()
  83. {
  84. F32 r = 2.3436212f, g = 1231.f, b = 4.7849321232f;
  85. LLColor3 llcolor3(r,g,b);
  86. ensure("magVecSquared:Fail ", is_approx_equal(llcolor3.magVecSquared(), (r*r + g*g + b*b)));
  87. ensure("magVec:Fail ", is_approx_equal(llcolor3.magVec(), (F32) sqrt(r*r + g*g + b*b)));
  88. }
  89. template<> template<>
  90. void v3color_object::test<5>()
  91. {
  92. F32 r = 2.3436212f, g = 1231.f, b = 4.7849321232f;
  93. F32 val1, val2,val3;
  94. LLColor3 llcolor3(r,g,b);
  95. F32 vecMag = llcolor3.normVec();
  96. F32 mag = (F32) sqrt(r*r + g*g + b*b);
  97. F32 oomag = 1.f / mag;
  98. val1 = r * oomag;
  99. val2 = g * oomag;
  100. val3 = b * oomag;
  101. ensure("1:normVec failed ", (is_approx_equal(val1, llcolor3.mV[0]) && is_approx_equal(val2, llcolor3.mV[1]) && is_approx_equal(val3, llcolor3.mV[2]) && is_approx_equal(vecMag, mag)));
  102. r = .000000000f, g = 0.f, b = 0.0f;
  103. llcolor3.setVec(r,g,b);
  104. vecMag = llcolor3.normVec();
  105. ensure("2:normVec failed should be 0. ", (0. == llcolor3.mV[0] && 0. == llcolor3.mV[1] && 0. == llcolor3.mV[2] && vecMag == 0.));
  106. }
  107. template<> template<>
  108. void v3color_object::test<6>()
  109. {
  110. F32 r = 2.3436212f, g = -1231.f, b = .7849321232f;
  111. std::ostringstream stream1, stream2;
  112. LLColor3 llcolor3(r,g,b),llcolor3a;
  113. stream1 << llcolor3;
  114. llcolor3a.setVec(r,g,b);
  115. stream2 << llcolor3a;
  116. ensure("operator << failed ", (stream1.str() == stream2.str()));
  117. }
  118. template<> template<>
  119. void v3color_object::test<7>()
  120. {
  121. F32 r = 2.3436212f, g = -1231.f, b = .7849321232f;
  122. LLColor3 llcolor3(r,g,b),llcolor3a;
  123. llcolor3a = llcolor3;
  124. ensure("operator == failed ", (llcolor3a == llcolor3));
  125. }
  126. template<> template<>
  127. void v3color_object::test<8>()
  128. {
  129. F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
  130. LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2),llcolor3b;
  131. llcolor3b = llcolor3 + llcolor3a ;
  132. ensure("1:operator+ failed",is_approx_equal(r1+r2 ,llcolor3b.mV[0]) && is_approx_equal(g1+g2,llcolor3b.mV[1])&& is_approx_equal(b1+b2,llcolor3b.mV[2]));
  133. r1 = -.235f, g1 = -24.32f, b1 = 2.13f, r2 = -2.3f, g2 = 1.f, b2 = 34.21f;
  134. llcolor3.setVec(r1,g1,b1);
  135. llcolor3a.setVec(r2,g2,b2);
  136. llcolor3b = llcolor3 + llcolor3a;
  137. ensure("2:operator+ failed",is_approx_equal(r1+r2 ,llcolor3b.mV[0]) && is_approx_equal(g1+g2,llcolor3b.mV[1])&& is_approx_equal(b1+b2,llcolor3b.mV[2]));
  138. }
  139. template<> template<>
  140. void v3color_object::test<9>()
  141. {
  142. F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
  143. LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2),llcolor3b;
  144. llcolor3b = llcolor3 - llcolor3a ;
  145. ensure("1:operator- failed",is_approx_equal(r1-r2 ,llcolor3b.mV[0]) && is_approx_equal(g1-g2,llcolor3b.mV[1])&& is_approx_equal(b1-b2,llcolor3b.mV[2]));
  146. r1 = -.235f, g1 = -24.32f, b1 = 2.13f, r2 = -2.3f, g2 = 1.f, b2 = 34.21f;
  147. llcolor3.setVec(r1,g1,b1);
  148. llcolor3a.setVec(r2,g2,b2);
  149. llcolor3b = llcolor3 - llcolor3a;
  150. ensure("2:operator- failed",is_approx_equal(r1-r2 ,llcolor3b.mV[0]) && is_approx_equal(g1-g2,llcolor3b.mV[1])&& is_approx_equal(b1-b2,llcolor3b.mV[2]));
  151. }
  152. template<> template<>
  153. void v3color_object::test<10>()
  154. {
  155. F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
  156. LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2),llcolor3b;
  157. llcolor3b = llcolor3 * llcolor3a;
  158. ensure("1:operator* failed",is_approx_equal(r1*r2 ,llcolor3b.mV[0]) && is_approx_equal(g1*g2,llcolor3b.mV[1])&& is_approx_equal(b1*b2,llcolor3b.mV[2]));
  159. llcolor3a.setToBlack();
  160. F32 mulVal = 4.332f;
  161. llcolor3a = llcolor3 * mulVal;
  162. ensure("2:operator* failed",is_approx_equal(r1*mulVal ,llcolor3a.mV[0]) && is_approx_equal(g1*mulVal,llcolor3a.mV[1])&& is_approx_equal(b1*mulVal,llcolor3a.mV[2]));
  163. llcolor3a.setToBlack();
  164. llcolor3a = mulVal * llcolor3;
  165. ensure("3:operator* failed",is_approx_equal(r1*mulVal ,llcolor3a.mV[0]) && is_approx_equal(g1*mulVal,llcolor3a.mV[1])&& is_approx_equal(b1*mulVal,llcolor3a.mV[2]));
  166. }
  167. template<> template<>
  168. void v3color_object::test<11>()
  169. {
  170. F32 r = 2.3436212f, g = 1231.f, b = 4.7849321232f;
  171. LLColor3 llcolor3(r,g,b),llcolor3a;
  172. llcolor3a = -llcolor3;
  173. ensure("operator- failed ", (-llcolor3a == llcolor3));
  174. }
  175. template<> template<>
  176. void v3color_object::test<12>()
  177. {
  178. F32 r = 2.3436212f, g = 1231.f, b = 4.7849321232f;
  179. LLColor3 llcolor3(r,g,b),llcolor3a(r,g,b);
  180. ensure_equals("1:operator== failed",llcolor3a,llcolor3);
  181. r = 13.3436212f, g = -11.f, b = .7849321232f;
  182. llcolor3.setVec(r,g,b);
  183. llcolor3a.setVec(r,g,b);
  184. ensure_equals("2:operator== failed",llcolor3a,llcolor3);
  185. }
  186. template<> template<>
  187. void v3color_object::test<13>()
  188. {
  189. F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
  190. LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
  191. ensure("1:operator!= failed",(llcolor3 != llcolor3a));
  192. llcolor3.setToBlack();
  193. llcolor3a.setVec(llcolor3);
  194. ensure("2:operator!= failed", ( FALSE == (llcolor3a != llcolor3)));
  195. }
  196. template<> template<>
  197. void v3color_object::test<14>()
  198. {
  199. F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
  200. LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
  201. llcolor3a += llcolor3;
  202. ensure("1:operator+= failed",is_approx_equal(r1+r2 ,llcolor3a.mV[0]) && is_approx_equal(g1+g2,llcolor3a.mV[1])&& is_approx_equal(b1+b2,llcolor3a.mV[2]));
  203. llcolor3.setVec(r1,g1,b1);
  204. llcolor3a.setVec(r2,g2,b2);
  205. llcolor3a += llcolor3;
  206. ensure("2:operator+= failed",is_approx_equal(r1+r2 ,llcolor3a.mV[0]) && is_approx_equal(g1+g2,llcolor3a.mV[1])&& is_approx_equal(b1+b2,llcolor3a.mV[2]));
  207. }
  208. template<> template<>
  209. void v3color_object::test<15>()
  210. {
  211. F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
  212. LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
  213. llcolor3a -= llcolor3;
  214. ensure("1:operator-= failed", is_approx_equal(r2-r1, llcolor3a.mV[0]));
  215. ensure("2:operator-= failed", is_approx_equal(g2-g1, llcolor3a.mV[1]));
  216. ensure("3:operator-= failed", is_approx_equal(b2-b1, llcolor3a.mV[2]));
  217. llcolor3.setVec(r1,g1,b1);
  218. llcolor3a.setVec(r2,g2,b2);
  219. llcolor3a -= llcolor3;
  220. ensure("4:operator-= failed", is_approx_equal(r2-r1, llcolor3a.mV[0]));
  221. ensure("5:operator-= failed", is_approx_equal(g2-g1, llcolor3a.mV[1]));
  222. ensure("6:operator-= failed", is_approx_equal(b2-b1, llcolor3a.mV[2]));
  223. }
  224. template<> template<>
  225. void v3color_object::test<16>()
  226. {
  227. F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
  228. LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
  229. llcolor3a *= llcolor3;
  230. ensure("1:operator*= failed",is_approx_equal(r1*r2 ,llcolor3a.mV[0]) && is_approx_equal(g1*g2,llcolor3a.mV[1])&& is_approx_equal(b1*b2,llcolor3a.mV[2]));
  231. F32 mulVal = 4.332f;
  232. llcolor3 *=mulVal;
  233. ensure("2:operator*= failed",is_approx_equal(r1*mulVal ,llcolor3.mV[0]) && is_approx_equal(g1*mulVal,llcolor3.mV[1])&& is_approx_equal(b1*mulVal,llcolor3.mV[2]));
  234. }
  235. template<> template<>
  236. void v3color_object::test<17>()
  237. {
  238. F32 r = 2.3436212f, g = -1231.f, b = .7849321232f;
  239. LLColor3 llcolor3(r,g,b);
  240. llcolor3.clamp();
  241. ensure("1:clamp:Fail to clamp " ,(1.0f == llcolor3.mV[0]) && (0.f == llcolor3.mV[1]) && (b == llcolor3.mV[2]));
  242. r = -2.3436212f, g = -1231.f, b = 67.7849321232f;
  243. llcolor3.setVec(r,g,b);
  244. llcolor3.clamp();
  245. ensure("2:clamp:Fail to clamp " ,(0.f == llcolor3.mV[0]) && (0.f == llcolor3.mV[1]) && (1.f == llcolor3.mV[2]));
  246. }
  247. template<> template<>
  248. void v3color_object::test<18>()
  249. {
  250. F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
  251. F32 val = 2.3f,val1,val2,val3;
  252. LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
  253. val1 = r1 + (r2 - r1)* val;
  254. val2 = g1 + (g2 - g1)* val;
  255. val3 = b1 + (b2 - b1)* val;
  256. LLColor3 llcolor3b = lerp(llcolor3,llcolor3a,val);
  257. ensure("lerp failed ", ((val1 ==llcolor3b.mV[0])&& (val2 ==llcolor3b.mV[1]) && (val3 ==llcolor3b.mV[2])));
  258. }
  259. template<> template<>
  260. void v3color_object::test<19>()
  261. {
  262. F32 r1 =1.f, g1 = 2.f,b1 = 1.2f, r2 = -2.3f, g2 = 1.11f, b2 = 1234.234f;
  263. LLColor3 llcolor3(r1,g1,b1),llcolor3a(r2,g2,b2);
  264. F32 val = distVec(llcolor3,llcolor3a);
  265. ensure("distVec failed ", is_approx_equal((F32) sqrt((r1-r2)*(r1-r2) + (g1-g2)*(g1-g2) + (b1-b2)*(b1-b2)) ,val));
  266. F32 val1 = distVec_squared(llcolor3,llcolor3a);
  267. ensure("distVec_squared failed ", is_approx_equal(((r1-r2)*(r1-r2) + (g1-g2)*(g1-g2) + (b1-b2)*(b1-b2)) ,val1));
  268. }
  269. template<> template<>
  270. void v3color_object::test<20>()
  271. {
  272. F32 r1 = 1.02223f, g1 = 22222.212f, b1 = 122222.00002f;
  273. LLColor3 llcolor31(r1,g1,b1);
  274. LLSD sd = llcolor31.getValue();
  275. LLColor3 llcolor32;
  276. llcolor32.setValue(sd);
  277. ensure_equals("LLColor3::setValue/getValue failed", llcolor31, llcolor32);
  278. LLColor3 llcolor33(sd);
  279. ensure_equals("LLColor3(LLSD) failed", llcolor31, llcolor33);
  280. }
  281. }