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

/indra/llmath/tests/llrect_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 526 lines | 342 code | 90 blank | 94 comment | 29 complexity | 9250f38033998a1b3c9c3aa06a1aee0c MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llrect_test.cpp
  3. * @author Martin Reddy
  4. * @date 2009-06-25
  5. * @brief Test for llrect.cpp.
  6. *
  7. * $LicenseInfo:firstyear=2009&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 "../llrect.h"
  31. namespace tut
  32. {
  33. struct LLRectData
  34. {
  35. };
  36. typedef test_group<LLRectData> factory;
  37. typedef factory::object object;
  38. }
  39. namespace
  40. {
  41. tut::factory llrect_test_factory("LLRect");
  42. }
  43. namespace tut
  44. {
  45. template<> template<>
  46. void object::test<1>()
  47. {
  48. //
  49. // test the LLRect default constructor
  50. //
  51. LLSD zero;
  52. zero.append(0); zero.append(0); zero.append(0); zero.append(0);
  53. // default constructor
  54. LLRect rect1;
  55. ensure_equals("Empty rect", rect1.getValue(), zero);
  56. ensure_equals("Empty rect left", rect1.mLeft, 0);
  57. ensure_equals("Empty rect top", rect1.mTop, 0);
  58. ensure_equals("Empty rect right", rect1.mRight, 0);
  59. ensure_equals("Empty rect bottom", rect1.mBottom, 0);
  60. ensure_equals("Empty rect width", rect1.getWidth(), 0);
  61. ensure_equals("Empty rect height", rect1.getHeight(), 0);
  62. ensure_equals("Empty rect centerx", rect1.getCenterX(), 0);
  63. ensure_equals("Empty rect centery", rect1.getCenterY(), 0);
  64. }
  65. template<> template<>
  66. void object::test<2>()
  67. {
  68. //
  69. // test the LLRectf default constructor
  70. //
  71. LLSD zerof;
  72. zerof.append(0.0f); zerof.append(0.0f); zerof.append(0.0f); zerof.append(0.0f);
  73. LLRectf rect2;
  74. ensure_equals("Empty rectf", rect2.getValue(), zerof);
  75. ensure_equals("Empty rectf left", rect2.mLeft, 0.0f);
  76. ensure_equals("Empty rectf top", rect2.mTop, 0.0f);
  77. ensure_equals("Empty rectf right", rect2.mRight, 0.0f);
  78. ensure_equals("Empty rectf bottom", rect2.mBottom, 0.0f);
  79. ensure_equals("Empty rectf width", rect2.getWidth(), 0.0f);
  80. ensure_equals("Empty rectf height", rect2.getHeight(), 0.0f);
  81. ensure_equals("Empty rectf centerx", rect2.getCenterX(), 0.0f);
  82. ensure_equals("Empty rectf centery", rect2.getCenterY(), 0.0f);
  83. }
  84. template<> template<>
  85. void object::test<3>()
  86. {
  87. //
  88. // test the LLRect constructor from another LLRect
  89. //
  90. LLRect rect3(LLRect(1, 6, 7, 2));
  91. ensure_equals("Default rect left", rect3.mLeft, 1);
  92. ensure_equals("Default rect top", rect3.mTop, 6);
  93. ensure_equals("Default rect right", rect3.mRight, 7);
  94. ensure_equals("Default rect bottom", rect3.mBottom, 2);
  95. ensure_equals("Default rect width", rect3.getWidth(), 6);
  96. ensure_equals("Default rect height", rect3.getHeight(), 4);
  97. ensure_equals("Default rect centerx", rect3.getCenterX(), 4);
  98. ensure_equals("Default rect centery", rect3.getCenterY(), 4);
  99. }
  100. template<> template<>
  101. void object::test<4>()
  102. {
  103. //
  104. // test the LLRectf four-float constructor
  105. //
  106. LLRectf rect4(1.0f, 5.0f, 6.0f, 2.0f);
  107. ensure_equals("Default rectf left", rect4.mLeft, 1.0f);
  108. ensure_equals("Default rectf top", rect4.mTop, 5.0f);
  109. ensure_equals("Default rectf right", rect4.mRight, 6.0f);
  110. ensure_equals("Default rectf bottom", rect4.mBottom, 2.0f);
  111. ensure_equals("Default rectf width", rect4.getWidth(), 5.0f);
  112. ensure_equals("Default rectf height", rect4.getHeight(), 3.0f);
  113. ensure_equals("Default rectf centerx", rect4.getCenterX(), 3.5f);
  114. ensure_equals("Default rectf centery", rect4.getCenterY(), 3.5f);
  115. }
  116. template<> template<>
  117. void object::test<5>()
  118. {
  119. //
  120. // test the LLRectf LLSD constructor
  121. //
  122. LLSD array;
  123. array.append(-1.0f); array.append(0.0f); array.append(0.0f); array.append(-1.0f);
  124. LLRectf rect5(array);
  125. ensure_equals("LLSD rectf left", rect5.mLeft, -1.0f);
  126. ensure_equals("LLSD rectf top", rect5.mTop, 0.0f);
  127. ensure_equals("LLSD rectf right", rect5.mRight, 0.0f);
  128. ensure_equals("LLSD rectf bottom", rect5.mBottom, -1.0f);
  129. ensure_equals("LLSD rectf width", rect5.getWidth(), 1.0f);
  130. ensure_equals("LLSD rectf height", rect5.getHeight(), 1.0f);
  131. ensure_equals("LLSD rectf centerx", rect5.getCenterX(), -0.5f);
  132. ensure_equals("LLSD rectf centery", rect5.getCenterY(), -0.5f);
  133. }
  134. template<> template<>
  135. void object::test<6>()
  136. {
  137. //
  138. // test directly setting the member variables for dimensions
  139. //
  140. LLRectf rectf;
  141. rectf.mLeft = -1.0f;
  142. rectf.mTop = 1.0f;
  143. rectf.mRight = 1.0f;
  144. rectf.mBottom = -1.0f;
  145. ensure_equals("Member-set rectf left", rectf.mLeft, -1.0f);
  146. ensure_equals("Member-set rectf top", rectf.mTop, 1.0f);
  147. ensure_equals("Member-set rectf right", rectf.mRight, 1.0f);
  148. ensure_equals("Member-set rectf bottom", rectf.mBottom, -1.0f);
  149. ensure_equals("Member-set rectf width", rectf.getWidth(), 2.0f);
  150. ensure_equals("Member-set rectf height", rectf.getHeight(), 2.0f);
  151. ensure_equals("Member-set rectf centerx", rectf.getCenterX(), 0.0f);
  152. ensure_equals("Member-set rectf centery", rectf.getCenterY(), 0.0f);
  153. }
  154. template<> template<>
  155. void object::test<7>()
  156. {
  157. //
  158. // test the setValue() method
  159. //
  160. LLRectf rectf;
  161. LLSD array;
  162. array.append(-1.0f); array.append(0.0f); array.append(0.0f); array.append(-1.0f);
  163. rectf.setValue(array);
  164. ensure_equals("setValue() rectf left", rectf.mLeft, -1.0f);
  165. ensure_equals("setValue() rectf top", rectf.mTop, 0.0f);
  166. ensure_equals("setValue() rectf right", rectf.mRight, 0.0f);
  167. ensure_equals("setValue() rectf bottom", rectf.mBottom, -1.0f);
  168. ensure_equals("setValue() rectf width", rectf.getWidth(), 1.0f);
  169. ensure_equals("setValue() rectf height", rectf.getHeight(), 1.0f);
  170. ensure_equals("setValue() rectf centerx", rectf.getCenterX(), -0.5f);
  171. ensure_equals("setValue() rectf centery", rectf.getCenterY(), -0.5f);
  172. }
  173. template<> template<>
  174. void object::test<8>()
  175. {
  176. //
  177. // test the set() method
  178. //
  179. LLRect rect;
  180. rect.set(10, 90, 70, 10);
  181. ensure_equals("set() rectf left", rect.mLeft, 10);
  182. ensure_equals("set() rectf top", rect.mTop, 90);
  183. ensure_equals("set() rectf right", rect.mRight, 70);
  184. ensure_equals("set() rectf bottom", rect.mBottom, 10);
  185. ensure_equals("set() rectf width", rect.getWidth(), 60);
  186. ensure_equals("set() rectf height", rect.getHeight(), 80);
  187. ensure_equals("set() rectf centerx", rect.getCenterX(), 40);
  188. ensure_equals("set() rectf centery", rect.getCenterY(), 50);
  189. }
  190. template<> template<>
  191. void object::test<9>()
  192. {
  193. //
  194. // test the setOriginAndSize() method
  195. //
  196. LLRectf rectf;
  197. rectf.setOriginAndSize(0.0f, 0.0f, 2.0f, 1.0f);
  198. ensure_equals("setOriginAndSize() rectf left", rectf.mLeft, 0.0f);
  199. ensure_equals("setOriginAndSize() rectf top", rectf.mTop, 1.0f);
  200. ensure_equals("setOriginAndSize() rectf right", rectf.mRight, 2.0f);
  201. ensure_equals("setOriginAndSize() rectf bottom", rectf.mBottom, 0.0f);
  202. ensure_equals("setOriginAndSize() rectf width", rectf.getWidth(), 2.0f);
  203. ensure_equals("setOriginAndSize() rectf height", rectf.getHeight(), 1.0f);
  204. ensure_equals("setOriginAndSize() rectf centerx", rectf.getCenterX(), 1.0f);
  205. ensure_equals("setOriginAndSize() rectf centery", rectf.getCenterY(), 0.5f);
  206. }
  207. template<> template<>
  208. void object::test<10>()
  209. {
  210. //
  211. // test the setLeftTopAndSize() method
  212. //
  213. LLRectf rectf;
  214. rectf.setLeftTopAndSize(0.0f, 0.0f, 2.0f, 1.0f);
  215. ensure_equals("setLeftTopAndSize() rectf left", rectf.mLeft, 0.0f);
  216. ensure_equals("setLeftTopAndSize() rectf top", rectf.mTop, 0.0f);
  217. ensure_equals("setLeftTopAndSize() rectf right", rectf.mRight, 2.0f);
  218. ensure_equals("setLeftTopAndSize() rectf bottom", rectf.mBottom, -1.0f);
  219. ensure_equals("setLeftTopAndSize() rectf width", rectf.getWidth(), 2.0f);
  220. ensure_equals("setLeftTopAndSize() rectf height", rectf.getHeight(), 1.0f);
  221. ensure_equals("setLeftTopAndSize() rectf centerx", rectf.getCenterX(), 1.0f);
  222. ensure_equals("setLeftTopAndSize() rectf centery", rectf.getCenterY(), -0.5f);
  223. }
  224. template<> template<>
  225. void object::test<11>()
  226. {
  227. //
  228. // test the setCenterAndSize() method
  229. //
  230. LLRectf rectf;
  231. rectf.setCenterAndSize(0.0f, 0.0f, 2.0f, 1.0f);
  232. ensure_equals("setCenterAndSize() rectf left", rectf.mLeft, -1.0f);
  233. ensure_equals("setCenterAndSize() rectf top", rectf.mTop, 0.5f);
  234. ensure_equals("setCenterAndSize() rectf right", rectf.mRight, 1.0f);
  235. ensure_equals("setCenterAndSize() rectf bottom", rectf.mBottom, -0.5f);
  236. ensure_equals("setCenterAndSize() rectf width", rectf.getWidth(), 2.0f);
  237. ensure_equals("setCenterAndSize() rectf height", rectf.getHeight(), 1.0f);
  238. ensure_equals("setCenterAndSize() rectf centerx", rectf.getCenterX(), 0.0f);
  239. ensure_equals("setCenterAndSize() rectf centery", rectf.getCenterY(), 0.0f);
  240. }
  241. template<> template<>
  242. void object::test<12>()
  243. {
  244. //
  245. // test the validity checking method
  246. //
  247. LLRectf rectf;
  248. rectf.set(-1.0f, 1.0f, 1.0f, -1.0f);
  249. ensure("BBox is valid", rectf.isValid());
  250. rectf.mLeft = 2.0f;
  251. ensure("BBox is not valid", ! rectf.isValid());
  252. rectf.makeValid();
  253. ensure("BBox forced valid", rectf.isValid());
  254. rectf.set(-1.0f, -1.0f, -1.0f, -1.0f);
  255. ensure("BBox(0,0,0,0) is valid", rectf.isValid());
  256. }
  257. template<> template<>
  258. void object::test<13>()
  259. {
  260. //
  261. // test the null checking methods
  262. //
  263. LLRectf rectf;
  264. rectf.set(-1.0f, 1.0f, 1.0f, -1.0f);
  265. ensure("BBox is not Null", ! rectf.isEmpty());
  266. ensure("BBox notNull", rectf.notEmpty());
  267. rectf.mLeft = 2.0f;
  268. rectf.makeValid();
  269. ensure("BBox is now Null", rectf.isEmpty());
  270. rectf.set(-1.0f, -1.0f, -1.0f, -1.0f);
  271. ensure("BBox(0,0,0,0) is Null", rectf.isEmpty());
  272. }
  273. template<> template<>
  274. void object::test<14>()
  275. {
  276. //
  277. // test the (in)equality operators
  278. //
  279. LLRectf rect1, rect2;
  280. rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
  281. rect2.set(-1.0f, 0.9f, 1.0f, -1.0f);
  282. ensure("rect1 == rect2 (false)", ! (rect1 == rect2));
  283. ensure("rect1 != rect2 (true)", rect1 != rect2);
  284. ensure("rect1 == rect1 (true)", rect1 == rect1);
  285. ensure("rect1 != rect1 (false)", ! (rect1 != rect1));
  286. }
  287. template<> template<>
  288. void object::test<15>()
  289. {
  290. //
  291. // test the copy constructor
  292. //
  293. LLRectf rect1, rect2(rect1);
  294. ensure("rect1 == rect2 (true)", rect1 == rect2);
  295. ensure("rect1 != rect2 (false)", ! (rect1 != rect2));
  296. }
  297. template<> template<>
  298. void object::test<16>()
  299. {
  300. //
  301. // test the translate() method
  302. //
  303. LLRectf rect1(-1.0f, 1.0f, 1.0f, -1.0f);
  304. LLRectf rect2(rect1);
  305. rect1.translate(0.0f, 0.0f);
  306. ensure("translate(0, 0)", rect1 == rect2);
  307. rect1.translate(100.0f, 100.0f);
  308. rect1.translate(-100.0f, -100.0f);
  309. ensure("translate(100, 100) + translate(-100, -100)", rect1 == rect2);
  310. rect1.translate(10.0f, 0.0f);
  311. rect2.set(9.0f, 1.0f, 11.0f, -1.0f);
  312. ensure("translate(10, 0)", rect1 == rect2);
  313. rect1.translate(0.0f, 10.0f);
  314. rect2.set(9.0f, 11.0f, 11.0f, 9.0f);
  315. ensure("translate(0, 10)", rect1 == rect2);
  316. rect1.translate(-10.0f, -10.0f);
  317. rect2.set(-1.0f, 1.0f, 1.0f, -1.0f);
  318. ensure("translate(-10, -10)", rect1 == rect2);
  319. }
  320. template<> template<>
  321. void object::test<17>()
  322. {
  323. //
  324. // test the stretch() method
  325. //
  326. LLRectf rect1(-1.0f, 1.0f, 1.0f, -1.0f);
  327. LLRectf rect2(rect1);
  328. rect1.stretch(0.0f);
  329. ensure("stretch(0)", rect1 == rect2);
  330. rect1.stretch(0.0f, 0.0f);
  331. ensure("stretch(0, 0)", rect1 == rect2);
  332. rect1.stretch(10.0f);
  333. rect1.stretch(-10.0f);
  334. ensure("stretch(10) + stretch(-10)", rect1 == rect2);
  335. rect1.stretch(2.0f, 1.0f);
  336. rect2.set(-3.0f, 2.0f, 3.0f, -2.0f);
  337. ensure("stretch(2, 1)", rect1 == rect2);
  338. }
  339. template<> template<>
  340. void object::test<18>()
  341. {
  342. //
  343. // test the unionWith() method
  344. //
  345. LLRectf rect1, rect2, rect3;
  346. rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
  347. rect2.set(-1.0f, 1.0f, 1.0f, -1.0f);
  348. rect3 = rect1;
  349. rect3.unionWith(rect2);
  350. ensure_equals("union with self", rect3, rect1);
  351. rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
  352. rect2.set(-2.0f, 2.0f, 0.0f, 0.0f);
  353. rect3 = rect1;
  354. rect3.unionWith(rect2);
  355. ensure_equals("union - overlap", rect3, LLRectf(-2.0f, 2.0f, 1.0f, -1.0f));
  356. rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
  357. rect2.set(5.0f, 10.0f, 10.0f, 5.0f);
  358. rect3 = rect1;
  359. rect3.unionWith(rect2);
  360. ensure_equals("union - no overlap", rect3, LLRectf(-1.0f, 10.0f, 10.0f, -1.0f));
  361. }
  362. template<> template<>
  363. void object::test<19>()
  364. {
  365. //
  366. // test the intersectWith() methods
  367. //
  368. LLRectf rect1, rect2, rect3;
  369. rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
  370. rect2.set(-1.0f, 1.0f, 1.0f, -1.0f);
  371. rect3 = rect1;
  372. rect3.intersectWith(rect2);
  373. ensure_equals("intersect with self", rect3, rect1);
  374. rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
  375. rect2.set(-2.0f, 2.0f, 0.0f, 0.0f);
  376. rect3 = rect1;
  377. rect3.intersectWith(rect2);
  378. ensure_equals("intersect - overlap", rect3, LLRectf(-1.0f, 1.0f, 0.0f, 0.0f));
  379. rect1.set(-1.0f, 1.0f, 1.0f, -1.0f);
  380. rect2.set(5.0f, 10.0f, 10.0f, 5.0f);
  381. rect3 = rect1;
  382. rect3.intersectWith(rect2);
  383. ensure("intersect - no overlap", rect3.isEmpty());
  384. }
  385. template<> template<>
  386. void object::test<20>()
  387. {
  388. //
  389. // test the pointInRect() method
  390. //
  391. LLRectf rect(1.0f, 3.0f, 3.0f, 1.0f);
  392. ensure("(0,0) not in rect", rect.pointInRect(0.0f, 0.0f) == FALSE);
  393. ensure("(2,2) in rect", rect.pointInRect(2.0f, 2.0f) == TRUE);
  394. ensure("(1,1) in rect", rect.pointInRect(1.0f, 1.0f) == TRUE);
  395. ensure("(3,3) not in rect", rect.pointInRect(3.0f, 3.0f) == FALSE);
  396. ensure("(2.999,2.999) in rect", rect.pointInRect(2.999f, 2.999f) == TRUE);
  397. ensure("(2.999,3.0) not in rect", rect.pointInRect(2.999f, 3.0f) == FALSE);
  398. ensure("(3.0,2.999) not in rect", rect.pointInRect(3.0f, 2.999f) == FALSE);
  399. }
  400. template<> template<>
  401. void object::test<21>()
  402. {
  403. //
  404. // test the localPointInRect() method
  405. //
  406. LLRectf rect(1.0f, 3.0f, 3.0f, 1.0f);
  407. ensure("(0,0) in local rect", rect.localPointInRect(0.0f, 0.0f) == TRUE);
  408. ensure("(-0.0001,-0.0001) not in local rect", rect.localPointInRect(-0.0001f, -0.001f) == FALSE);
  409. ensure("(1,1) in local rect", rect.localPointInRect(1.0f, 1.0f) == TRUE);
  410. ensure("(2,2) not in local rect", rect.localPointInRect(2.0f, 2.0f) == FALSE);
  411. ensure("(1.999,1.999) in local rect", rect.localPointInRect(1.999f, 1.999f) == TRUE);
  412. ensure("(1.999,2.0) not in local rect", rect.localPointInRect(1.999f, 2.0f) == FALSE);
  413. ensure("(2.0,1.999) not in local rect", rect.localPointInRect(2.0f, 1.999f) == FALSE);
  414. }
  415. template<> template<>
  416. void object::test<22>()
  417. {
  418. //
  419. // test the clampPointToRect() method
  420. //
  421. LLRectf rect(1.0f, 3.0f, 3.0f, 1.0f);
  422. F32 x, y;
  423. x = 2.0f; y = 2.0f;
  424. rect.clampPointToRect(x, y);
  425. ensure_equals("clamp x-coord within rect", x, 2.0f);
  426. ensure_equals("clamp y-coord within rect", y, 2.0f);
  427. x = -100.0f; y = 100.0f;
  428. rect.clampPointToRect(x, y);
  429. ensure_equals("clamp x-coord outside rect", x, 1.0f);
  430. ensure_equals("clamp y-coord outside rect", y, 3.0f);
  431. x = 3.0f; y = 1.0f;
  432. rect.clampPointToRect(x, y);
  433. ensure_equals("clamp x-coord edge rect", x, 3.0f);
  434. ensure_equals("clamp y-coord edge rect", y, 1.0f);
  435. }
  436. }