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

游戏引擎

开发平台:

C++ Builder

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