PageRenderTime 106ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llmath/tests/v2math_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 448 lines | 365 code | 56 blank | 27 comment | 102 complexity | 63e005f2ecd6236bb4dbbc3dc015ee04 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file v2math_test.cpp
  3. * @author Adroit
  4. * @date 2007-02
  5. * @brief v2math 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 "../v2math.h"
  31. namespace tut
  32. {
  33. struct v2math_data
  34. {
  35. };
  36. typedef test_group<v2math_data> v2math_test;
  37. typedef v2math_test::object v2math_object;
  38. tut::v2math_test v2math_testcase("v2math_h");
  39. template<> template<>
  40. void v2math_object::test<1>()
  41. {
  42. LLVector2 vec2;
  43. ensure("LLVector2:Fail to initialize ", (0.f == vec2.mV[VX] && 0.f == vec2.mV[VY]));
  44. F32 x =2.0f, y = 3.2f ;
  45. LLVector2 vec3(x,y);
  46. ensure("LLVector2(F32 x, F32 y):Fail to initialize ", (x == vec3.mV[VX]) && (y == vec3.mV[VY]));
  47. const F32 vec[2] = {3.2f, 4.5f};
  48. LLVector2 vec4(vec);
  49. ensure("LLVector2(const F32 *vec):Fail to initialize ", (vec[0] == vec4.mV[VX]) && (vec[1] == vec4.mV[VY]));
  50. vec4.clearVec();
  51. ensure("clearVec():Fail to clean the values ", (0.f == vec4.mV[VX] && 0.f == vec4.mV[VY]));
  52. vec3.zeroVec();
  53. ensure("zeroVec():Fail to fill the zero ", (0.f == vec3.mV[VX] && 0.f == vec3.mV[VY]));
  54. }
  55. template<> template<>
  56. void v2math_object::test<2>()
  57. {
  58. F32 x = 123.356f, y = 2387.453f;
  59. LLVector2 vec2,vec3;
  60. vec2.setVec(x, y);
  61. ensure("1:setVec: Fail ", (x == vec2.mV[VX]) && (y == vec2.mV[VY]));
  62. vec3.setVec(vec2);
  63. ensure("2:setVec: Fail " ,(vec2 == vec3));
  64. vec3.zeroVec();
  65. const F32 vec[2] = {3.24653f, 457653.4f};
  66. vec3.setVec(vec);
  67. ensure("3:setVec: Fail ", (vec[0] == vec3.mV[VX]) && (vec[1] == vec3.mV[VY]));
  68. }
  69. template<> template<>
  70. void v2math_object::test<3>()
  71. {
  72. F32 x = 2.2345f, y = 3.5678f ;
  73. LLVector2 vec2(x,y);
  74. ensure("magVecSquared:Fail ", is_approx_equal(vec2.magVecSquared(), (x*x + y*y)));
  75. ensure("magVec:Fail ", is_approx_equal(vec2.magVec(), (F32) sqrt(x*x + y*y)));
  76. }
  77. template<> template<>
  78. void v2math_object::test<4>()
  79. {
  80. F32 x =-2.0f, y = -3.0f ;
  81. LLVector2 vec2(x,y);
  82. ensure_equals("abs():Fail", vec2.abs(), TRUE);
  83. ensure("abs() x", is_approx_equal(vec2.mV[VX], 2.f));
  84. ensure("abs() y", is_approx_equal(vec2.mV[VY], 3.f));
  85. ensure("isNull():Fail ", FALSE == vec2.isNull()); //Returns TRUE if vector has a _very_small_ length
  86. x =.00000001f, y = .000001001f;
  87. vec2.setVec(x, y);
  88. ensure("isNull(): Fail ", TRUE == vec2.isNull());
  89. }
  90. template<> template<>
  91. void v2math_object::test<5>()
  92. {
  93. F32 x =1.f, y = 2.f;
  94. LLVector2 vec2(x, y), vec3;
  95. vec3 = vec3.scaleVec(vec2);
  96. ensure("scaleVec: Fail ", vec3.mV[VX] == 0. && vec3.mV[VY] == 0.);
  97. ensure("isExactlyZero(): Fail", TRUE == vec3.isExactlyZero());
  98. vec3.setVec(2.f, 1.f);
  99. vec3 = vec3.scaleVec(vec2);
  100. ensure("scaleVec: Fail ", (2.f == vec3.mV[VX]) && (2.f == vec3.mV[VY]));
  101. ensure("isExactlyZero():Fail", FALSE == vec3.isExactlyZero());
  102. }
  103. template<> template<>
  104. void v2math_object::test<6>()
  105. {
  106. F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
  107. F32 val1, val2;
  108. LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
  109. vec4 = vec2 + vec3 ;
  110. val1 = x1+x2;
  111. val2 = y1+y2;
  112. ensure("1:operator+ failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
  113. vec2.clearVec();
  114. vec3.clearVec();
  115. x1 = -.235f, y1 = -24.32f, x2 = -2.3f, y2 = 1.f;
  116. vec2.setVec(x1, y1);
  117. vec3.setVec(x2, y2);
  118. vec4 = vec2 + vec3;
  119. val1 = x1+x2;
  120. val2 = y1+y2;
  121. ensure("2:operator+ failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
  122. }
  123. template<> template<>
  124. void v2math_object::test<7>()
  125. {
  126. F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
  127. F32 val1, val2;
  128. LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
  129. vec4 = vec2 - vec3 ;
  130. val1 = x1-x2;
  131. val2 = y1-y2;
  132. ensure("1:operator- failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
  133. vec2.clearVec();
  134. vec3.clearVec();
  135. vec4.clearVec();
  136. x1 = -.235f, y1 = -24.32f, x2 = -2.3f, y2 = 1.f;
  137. vec2.setVec(x1, y1);
  138. vec3.setVec(x2, y2);
  139. vec4 = vec2 - vec3;
  140. val1 = x1-x2;
  141. val2 = y1-y2;
  142. ensure("2:operator- failed",(val1 == vec4.mV[VX]) && ((val2 == vec4.mV[VY])));
  143. }
  144. template<> template<>
  145. void v2math_object::test<8>()
  146. {
  147. F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
  148. F32 val1, val2;
  149. LLVector2 vec2(x1, y1), vec3(x2, y2);
  150. val1 = vec2 * vec3;
  151. val2 = x1*x2 + y1*y2;
  152. ensure("1:operator* failed",(val1 == val2));
  153. vec3.clearVec();
  154. F32 mulVal = 4.332f;
  155. vec3 = vec2 * mulVal;
  156. val1 = x1*mulVal;
  157. val2 = y1*mulVal;
  158. ensure("2:operator* failed",(val1 == vec3.mV[VX]) && (val2 == vec3.mV[VY]));
  159. vec3.clearVec();
  160. vec3 = mulVal * vec2;
  161. ensure("3:operator* failed",(val1 == vec3.mV[VX]) && (val2 == vec3.mV[VY]));
  162. }
  163. template<> template<>
  164. void v2math_object::test<9>()
  165. {
  166. F32 x1 =1.f, y1 = 2.f, div = 3.2f;
  167. F32 val1, val2;
  168. LLVector2 vec2(x1, y1), vec3;
  169. vec3 = vec2 / div;
  170. val1 = x1 / div;
  171. val2 = y1 / div;
  172. ensure("1:operator/ failed", is_approx_equal(val1, vec3.mV[VX]) && is_approx_equal(val2, vec3.mV[VY]));
  173. vec3.clearVec();
  174. x1 = -.235f, y1 = -24.32f, div = -2.2f;
  175. vec2.setVec(x1, y1);
  176. vec3 = vec2 / div;
  177. val1 = x1 / div;
  178. val2 = y1 / div;
  179. ensure("2:operator/ failed", is_approx_equal(val1, vec3.mV[VX]) && is_approx_equal(val2, vec3.mV[VY]));
  180. }
  181. template<> template<>
  182. void v2math_object::test<10>()
  183. {
  184. F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
  185. F32 val1, val2;
  186. LLVector2 vec2(x1, y1), vec3(x2, y2), vec4;
  187. vec4 = vec2 % vec3;
  188. val1 = x1*y2 - x2*y1;
  189. val2 = y1*x2 - y2*x1;
  190. ensure("1:operator% failed",(val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY]));
  191. vec2.clearVec();
  192. vec3.clearVec();
  193. vec4.clearVec();
  194. x1 = -.235f, y1 = -24.32f, x2 = -2.3f, y2 = 1.f;
  195. vec2.setVec(x1, y1);
  196. vec3.setVec(x2, y2);
  197. vec4 = vec2 % vec3;
  198. val1 = x1*y2 - x2*y1;
  199. val2 = y1*x2 - y2*x1;
  200. ensure("2:operator% failed",(val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY]));
  201. }
  202. template<> template<>
  203. void v2math_object::test<11>()
  204. {
  205. F32 x1 =1.f, y1 = 2.f;
  206. LLVector2 vec2(x1, y1), vec3(x1, y1);
  207. ensure("1:operator== failed",(vec2 == vec3));
  208. vec2.clearVec();
  209. vec3.clearVec();
  210. x1 = -.235f, y1 = -24.32f;
  211. vec2.setVec(x1, y1);
  212. vec3.setVec(vec2);
  213. ensure("2:operator== failed",(vec2 == vec3));
  214. }
  215. template<> template<>
  216. void v2math_object::test<12>()
  217. {
  218. F32 x1 = 1.f, y1 = 2.f,x2 = 2.332f, y2 = -1.23f;
  219. LLVector2 vec2(x1, y1), vec3(x2, y2);
  220. ensure("1:operator!= failed",(vec2 != vec3));
  221. vec2.clearVec();
  222. vec3.clearVec();
  223. vec2.setVec(x1, y1);
  224. vec3.setVec(vec2);
  225. ensure("2:operator!= failed", (FALSE == (vec2 != vec3)));
  226. }
  227. template<> template<>
  228. void v2math_object::test<13>()
  229. {
  230. F32 x1 = 1.f, y1 = 2.f,x2 = 2.332f, y2 = -1.23f;
  231. F32 val1, val2;
  232. LLVector2 vec2(x1, y1), vec3(x2, y2);
  233. vec2 +=vec3;
  234. val1 = x1+x2;
  235. val2 = y1+y2;
  236. ensure("1:operator+= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
  237. vec2.setVec(x1, y1);
  238. vec2 -=vec3;
  239. val1 = x1-x2;
  240. val2 = y1-y2;
  241. ensure("2:operator-= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
  242. vec2.clearVec();
  243. vec3.clearVec();
  244. x1 = -21.000466f, y1 = 2.98382f,x2 = 0.332f, y2 = -01.23f;
  245. vec2.setVec(x1, y1);
  246. vec3.setVec(x2, y2);
  247. vec2 +=vec3;
  248. val1 = x1+x2;
  249. val2 = y1+y2;
  250. ensure("3:operator+= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
  251. vec2.setVec(x1, y1);
  252. vec2 -=vec3;
  253. val1 = x1-x2;
  254. val2 = y1-y2;
  255. ensure("4:operator-= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
  256. }
  257. template<> template<>
  258. void v2math_object::test<14>()
  259. {
  260. F32 x1 =1.f, y1 = 2.f;
  261. F32 val1, val2, mulVal = 4.332f;
  262. LLVector2 vec2(x1, y1);
  263. vec2 /=mulVal;
  264. val1 = x1 / mulVal;
  265. val2 = y1 / mulVal;
  266. ensure("1:operator/= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
  267. vec2.clearVec();
  268. x1 = .213f, y1 = -2.34f, mulVal = -.23f;
  269. vec2.setVec(x1, y1);
  270. vec2 /=mulVal;
  271. val1 = x1 / mulVal;
  272. val2 = y1 / mulVal;
  273. ensure("2:operator/= failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]));
  274. }
  275. template<> template<>
  276. void v2math_object::test<15>()
  277. {
  278. F32 x1 =1.f, y1 = 2.f;
  279. F32 val1, val2, mulVal = 4.332f;
  280. LLVector2 vec2(x1, y1);
  281. vec2 *=mulVal;
  282. val1 = x1*mulVal;
  283. val2 = y1*mulVal;
  284. ensure("1:operator*= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
  285. vec2.clearVec();
  286. x1 = .213f, y1 = -2.34f, mulVal = -.23f;
  287. vec2.setVec(x1, y1);
  288. vec2 *=mulVal;
  289. val1 = x1*mulVal;
  290. val2 = y1*mulVal;
  291. ensure("2:operator*= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
  292. }
  293. template<> template<>
  294. void v2math_object::test<16>()
  295. {
  296. F32 x1 =1.f, y1 = 2.f, x2 = -2.3f, y2 = 1.11f;
  297. F32 val1, val2;
  298. LLVector2 vec2(x1, y1), vec3(x2, y2);
  299. vec2 %= vec3;
  300. val1 = x1*y2 - x2*y1;
  301. val2 = y1*x2 - y2*x1;
  302. ensure("1:operator%= failed",(val1 == vec2.mV[VX]) && (val2 == vec2.mV[VY]));
  303. }
  304. template<> template<>
  305. void v2math_object::test<17>()
  306. {
  307. F32 x1 =1.f, y1 = 2.f;
  308. LLVector2 vec2(x1, y1),vec3;
  309. vec3 = -vec2;
  310. ensure("1:operator- failed",(-vec3 == vec2));
  311. }
  312. template<> template<>
  313. void v2math_object::test<18>()
  314. {
  315. F32 x1 =1.f, y1 = 2.f;
  316. std::ostringstream stream1, stream2;
  317. LLVector2 vec2(x1, y1),vec3;
  318. stream1 << vec2;
  319. vec3.setVec(x1, y1);
  320. stream2 << vec3;
  321. ensure("1:operator << failed",(stream1.str() == stream2.str()));
  322. }
  323. template<> template<>
  324. void v2math_object::test<19>()
  325. {
  326. F32 x1 =1.0f, y1 = 2.0f, x2 = -.32f, y2 = .2234f;
  327. LLVector2 vec2(x1, y1),vec3(x2, y2);
  328. ensure("1:operator < failed",(vec3 < vec2));
  329. x1 = 1.0f, y1 = 2.0f, x2 = 1.0f, y2 = 3.2234f;
  330. vec2.setVec(x1, y1);
  331. vec3.setVec(x2, y2);
  332. ensure("2:operator < failed", (FALSE == (vec3 < vec2)));
  333. }
  334. template<> template<>
  335. void v2math_object::test<20>()
  336. {
  337. F32 x1 =1.0f, y1 = 2.0f;
  338. LLVector2 vec2(x1, y1);
  339. ensure("1:operator [] failed",( x1 == vec2[0]));
  340. ensure("2:operator [] failed",( y1 == vec2[1]));
  341. vec2.clearVec();
  342. x1 = 23.0f, y1 = -.2361f;
  343. vec2.setVec(x1, y1);
  344. F32 ref1 = vec2[0];
  345. ensure("3:operator [] failed", ( ref1 == x1));
  346. F32 ref2 = vec2[1];
  347. ensure("4:operator [] failed", ( ref2 == y1));
  348. }
  349. template<> template<>
  350. void v2math_object::test<21>()
  351. {
  352. F32 x1 =1.f, y1 = 2.f, x2 = -.32f, y2 = .2234f;
  353. F32 val1, val2;
  354. LLVector2 vec2(x1, y1),vec3(x2, y2);
  355. val1 = dist_vec_squared2D(vec2, vec3);
  356. val2 = (x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2);
  357. ensure_equals("dist_vec_squared2D values are not equal",val2, val1);
  358. val1 = dist_vec_squared(vec2, vec3);
  359. ensure_equals("dist_vec_squared values are not equal",val2, val1);
  360. val1 = dist_vec(vec2, vec3);
  361. val2 = (F32) sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2));
  362. ensure_equals("dist_vec values are not equal",val2, val1);
  363. }
  364. template<> template<>
  365. void v2math_object::test<22>()
  366. {
  367. F32 x1 =1.f, y1 = 2.f, x2 = -.32f, y2 = .2234f,fVal = .0121f;
  368. F32 val1, val2;
  369. LLVector2 vec2(x1, y1),vec3(x2, y2);
  370. LLVector2 vec4 = lerp(vec2, vec3, fVal);
  371. val1 = x1 + (x2 - x1) * fVal;
  372. val2 = y1 + (y2 - y1) * fVal;
  373. ensure("lerp values are not equal", ((val1 == vec4.mV[VX]) && (val2 == vec4.mV[VY])));
  374. }
  375. template<> template<>
  376. void v2math_object::test<23>()
  377. {
  378. F32 x1 =1.f, y1 = 2.f;
  379. F32 val1, val2;
  380. LLVector2 vec2(x1, y1);
  381. F32 vecMag = vec2.normVec();
  382. F32 mag = (F32) sqrt(x1*x1 + y1*y1);
  383. F32 oomag = 1.f / mag;
  384. val1 = x1 * oomag;
  385. val2 = y1 * oomag;
  386. ensure("normVec failed", is_approx_equal(val1, vec2.mV[VX]) && is_approx_equal(val2, vec2.mV[VY]) && is_approx_equal(vecMag, mag));
  387. x1 =.00000001f, y1 = 0.f;
  388. vec2.setVec(x1, y1);
  389. vecMag = vec2.normVec();
  390. ensure("normVec failed should be 0.", 0. == vec2.mV[VX] && 0. == vec2.mV[VY] && vecMag == 0.);
  391. }
  392. }