Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

testvector3d.cpp 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include <QtTest/QtTest>
  2. #include "utils/vector3d.h"
  3. class TestVector3D : public QObject
  4. {
  5. Q_OBJECT
  6. private slots:
  7. void gettersSetters1();
  8. void gettersSetters2();
  9. void gettersSetters3();
  10. void isNull1();
  11. void isNull1Op();
  12. void isNull2();
  13. void isNull2Op();
  14. void equal1();
  15. void equal2();
  16. void equal3();
  17. void equal4();
  18. void equal5();
  19. void equal6();
  20. void add1();
  21. void add1Op();
  22. void add2();
  23. void add2Op();
  24. void add3Op();
  25. void sub1();
  26. void sub1Op();
  27. void sub2();
  28. void sub2Op();
  29. void sub3Op();
  30. void mult();
  31. void mult1Op();
  32. void mult2Op();
  33. void mult3Op();
  34. void div();
  35. void divOp();
  36. void dotProduct1();
  37. void dotProduct2();
  38. void crossProduct1();
  39. void crossProduct2();
  40. void crossProduct3();
  41. void norm1();
  42. void norm2();
  43. void getScalarOp();
  44. };
  45. void TestVector3D::gettersSetters1()
  46. {
  47. Vector3D v;
  48. QCOMPARE(v.getX(), 0.0);
  49. QCOMPARE(v.getY(), 0.0);
  50. QCOMPARE(v.getZ(), 0.0);
  51. }
  52. void TestVector3D::gettersSetters2()
  53. {
  54. double x = 1.42, y = 2.0, z = 3.0;
  55. Vector3D v(x, y, z);
  56. QCOMPARE(v.getX(), x);
  57. QCOMPARE(v.getY(), y);
  58. QCOMPARE(v.getZ(), z);
  59. }
  60. void TestVector3D::gettersSetters3()
  61. {
  62. double x = 1.42, y = 2.0, z = 3.0;
  63. Vector3D v;
  64. v.setX(x).setY(y).setZ(z);
  65. QCOMPARE(v.getX(), x);
  66. QCOMPARE(v.getY(), y);
  67. QCOMPARE(v.getZ(), z);
  68. }
  69. void TestVector3D::isNull1()
  70. {
  71. double x = 1.42, y = 2.0, z = 3.0;
  72. Vector3D v(x, y, z);
  73. QCOMPARE(v.isNull(), false);
  74. }
  75. void TestVector3D::isNull1Op()
  76. {
  77. double x = 1.42, y = 2.0, z = 3.0;
  78. Vector3D v(x, y, z);
  79. QCOMPARE((bool)v, true);
  80. }
  81. void TestVector3D::isNull2()
  82. {
  83. Vector3D v;
  84. QCOMPARE(v.isNull(), true);
  85. }
  86. void TestVector3D::isNull2Op()
  87. {
  88. Vector3D v;
  89. QCOMPARE(!v, true);
  90. }
  91. void TestVector3D::equal1()
  92. {
  93. Vector3D v1(1.42, 2.0, 3.0);
  94. Vector3D v2(1.42, 2.0, 3.0);
  95. QCOMPARE(v1.equal(v2), true);
  96. }
  97. void TestVector3D::equal2()
  98. {
  99. Vector3D v1(1.42, 2.0, 3.0);
  100. Vector3D v2(1.40, 2.0, 3.0);
  101. QCOMPARE(v1.equal(v2), false);
  102. }
  103. void TestVector3D::equal3()
  104. {
  105. Vector3D v1(1.42, 2.0, 3.0);
  106. Vector3D v2(1.42, 2.1, 3.0);
  107. QCOMPARE(v1.equal(v2), false);
  108. }
  109. void TestVector3D::equal4()
  110. {
  111. Vector3D v1(1.42, 2.0, 3.0);
  112. Vector3D v2(1.42, 2.0, 3.1);
  113. QCOMPARE(v1.equal(v2), false);
  114. }
  115. void TestVector3D::equal5()
  116. {
  117. Vector3D v1(1.42, 2.0, 3.0);
  118. Vector3D v2(1.42, 2.0, 3.1);
  119. QCOMPARE(v1 == v2, false);
  120. }
  121. void TestVector3D::equal6()
  122. {
  123. Vector3D v1(1.42, 2.0, 3.0);
  124. Vector3D v2(1.42, 2.0, 3.0);
  125. QCOMPARE(v1 == v2, true);
  126. }
  127. void TestVector3D::add1()
  128. {
  129. double x = 1.42, y = 2.0, z = 3.0, x2 = 0.24, y2 = 24.0, z2 = 42.0;
  130. Vector3D v(x, y, z);
  131. v.add(x2, y2, z2);
  132. QCOMPARE(v.getX(), x + x2);
  133. QCOMPARE(v.getY(), y + y2);
  134. QCOMPARE(v.getZ(), z + z2);
  135. }
  136. void TestVector3D::add1Op()
  137. {
  138. double x = 1.42, y = 2.0, z = 3.0, x2 = 0.24, y2 = 24.0, z2 = 42.0;
  139. Vector3D v1(x, y, z);
  140. Vector3D v2(x2, y2, z2);
  141. Vector3D v = v1 + v2;
  142. QCOMPARE(v.getX(), x + x2);
  143. QCOMPARE(v.getY(), y + y2);
  144. QCOMPARE(v.getZ(), z + z2);
  145. QCOMPARE(v1.getX(), x);
  146. QCOMPARE(v1.getY(), y);
  147. QCOMPARE(v1.getZ(), z);
  148. QCOMPARE(v2.getX(), x2);
  149. QCOMPARE(v2.getY(), y2);
  150. QCOMPARE(v2.getZ(), z2);
  151. }
  152. void TestVector3D::add2()
  153. {
  154. double x = 1.42, y = 2.0, z = 3.0, m = 0.24;
  155. Vector3D v(x, y, z);
  156. v.add(m);
  157. QCOMPARE(v.getX(), x + m);
  158. QCOMPARE(v.getY(), y + m);
  159. QCOMPARE(v.getZ(), z + m);
  160. }
  161. void TestVector3D::add2Op()
  162. {
  163. double x = 1.42, y = 2.0, z = 3.0, m = 0.24;
  164. Vector3D v1(x, y, z);
  165. Vector3D v = v1 + m;
  166. QCOMPARE(v.getX(), x + m);
  167. QCOMPARE(v.getY(), y + m);
  168. QCOMPARE(v.getZ(), z + m);
  169. QCOMPARE(v1.getX(), x);
  170. QCOMPARE(v1.getY(), y);
  171. QCOMPARE(v1.getZ(), z);
  172. }
  173. void TestVector3D::add3Op()
  174. {
  175. double x = 1.42, y = 2.0, z = 3.0;
  176. Vector3D v1(x, y, z);
  177. Vector3D v = +v1;
  178. QCOMPARE(v.getX(), x);
  179. QCOMPARE(v.getY(), y);
  180. QCOMPARE(v.getZ(), z);
  181. QCOMPARE(v1.getX(), x);
  182. QCOMPARE(v1.getY(), y);
  183. QCOMPARE(v1.getZ(), z);
  184. }
  185. void TestVector3D::sub1()
  186. {
  187. double x = 1.42, y = 2.0, z = 3.0, x2 = 0.24, y2 = 24.0, z2 = 42.0;
  188. Vector3D v(x, y, z);
  189. v.sub(x2, y2, z2);
  190. QCOMPARE(v.getX(), x - x2);
  191. QCOMPARE(v.getY(), y - y2);
  192. QCOMPARE(v.getZ(), z - z2);
  193. }
  194. void TestVector3D::sub1Op()
  195. {
  196. double x = 1.42, y = 2.0, z = 3.0, x2 = 0.24, y2 = 24.0, z2 = 42.0;
  197. Vector3D v1(x, y, z);
  198. Vector3D v2(x2, y2, z2);
  199. Vector3D v = v1 - v2;
  200. QCOMPARE(v.getX(), x - x2);
  201. QCOMPARE(v.getY(), y - y2);
  202. QCOMPARE(v.getZ(), z - z2);
  203. QCOMPARE(v1.getX(), x);
  204. QCOMPARE(v1.getY(), y);
  205. QCOMPARE(v1.getZ(), z);
  206. QCOMPARE(v2.getX(), x2);
  207. QCOMPARE(v2.getY(), y2);
  208. QCOMPARE(v2.getZ(), z2);
  209. }
  210. void TestVector3D::sub2()
  211. {
  212. double x = 1.42, y = 2.0, z = 3.0, m = 0.24;
  213. Vector3D v(x, y, z);
  214. v.sub(m);
  215. QCOMPARE(v.getX(), x - m);
  216. QCOMPARE(v.getY(), y - m);
  217. QCOMPARE(v.getZ(), z - m);
  218. }
  219. void TestVector3D::sub2Op()
  220. {
  221. double x = 1.42, y = 2.0, z = 3.0, m = 0.24;
  222. Vector3D v1(x, y, z);
  223. Vector3D v = v1 - m;
  224. QCOMPARE(v.getX(), x - m);
  225. QCOMPARE(v.getY(), y - m);
  226. QCOMPARE(v.getZ(), z - m);
  227. QCOMPARE(v1.getX(), x);
  228. QCOMPARE(v1.getY(), y);
  229. QCOMPARE(v1.getZ(), z);
  230. }
  231. void TestVector3D::sub3Op()
  232. {
  233. double x = 1.42, y = 2.0, z = 3.0;
  234. Vector3D v1(x, y, z);
  235. Vector3D v = -v1;
  236. QCOMPARE(v.getX(), -x);
  237. QCOMPARE(v.getY(), -y);
  238. QCOMPARE(v.getZ(), -z);
  239. QCOMPARE(v1.getX(), x);
  240. QCOMPARE(v1.getY(), y);
  241. QCOMPARE(v1.getZ(), z);
  242. }
  243. void TestVector3D::mult()
  244. {
  245. double x = 1.42, y = 2.0, z = 3.0, m = 2.0;
  246. Vector3D v(x, y, z);
  247. v.mult(m);
  248. QCOMPARE(v.getX(), x * m);
  249. QCOMPARE(v.getY(), y * m);
  250. QCOMPARE(v.getZ(), z * m);
  251. }
  252. void TestVector3D::mult1Op()
  253. {
  254. double x = 1.42, y = 2.0, z = 3.0, m = 2.0;
  255. Vector3D v1(x, y, z);
  256. Vector3D v = v1 * m;
  257. QCOMPARE(v.getX(), x * m);
  258. QCOMPARE(v.getY(), y * m);
  259. QCOMPARE(v.getZ(), z * m);
  260. }
  261. void TestVector3D::mult2Op()
  262. {
  263. Vector3D v1(1.5, 2.0, 3.0);
  264. Vector3D v2(42.0, 5.0, 7.0);
  265. Vector3D v = v1 * v2;
  266. QCOMPARE(v.getX(), v1.getX() * v2.getX());
  267. QCOMPARE(v.getY(), v1.getY() * v2.getY());
  268. QCOMPARE(v.getZ(), v1.getZ() * v2.getZ());
  269. }
  270. void TestVector3D::mult3Op()
  271. {
  272. Vector3D v1(20.0, 42.0, 10.0);
  273. Vector3D v2(42.0, 5.0, 7.0);
  274. Vector3D v = v1 * v2;
  275. QCOMPARE(v.getX(), v1.getX() * v2.getX());
  276. QCOMPARE(v.getY(), v1.getY() * v2.getY());
  277. QCOMPARE(v.getZ(), v1.getZ() * v2.getZ());
  278. }
  279. void TestVector3D::div()
  280. {
  281. double x = 1.42, y = 2.0, z = 3.0, m = 2.0;
  282. Vector3D v(x, y, z);
  283. v.div(m);
  284. QCOMPARE(v.getX(), x / m);
  285. QCOMPARE(v.getY(), y / m);
  286. QCOMPARE(v.getZ(), z / m);
  287. }
  288. void TestVector3D::divOp()
  289. {
  290. double x = 1.42, y = 2.0, z = 3.0, m = 2.0;
  291. Vector3D v1(x, y, z);
  292. Vector3D v = v1 / m;
  293. QCOMPARE(v.getX(), x / m);
  294. QCOMPARE(v.getY(), y / m);
  295. QCOMPARE(v.getZ(), z / m);
  296. }
  297. void TestVector3D::dotProduct1()
  298. {
  299. Vector3D v1(1.5, 2.0, 3.0);
  300. Vector3D v2(42.0, 5.0, 7.0);
  301. QCOMPARE(v1.dotProduct(v2), 94.0);
  302. }
  303. void TestVector3D::dotProduct2()
  304. {
  305. Vector3D v1(20, 42, 10);
  306. Vector3D v2(42.0, 5.0, 7.0);
  307. QCOMPARE(v1.dotProduct(v2), 1120.0);
  308. }
  309. void TestVector3D::crossProduct1()
  310. {
  311. Vector3D v(10.0, 42.0, 5.0);
  312. Vector3D v2(9.0, 5.0, 4.0);
  313. v.crossProduct(v2);
  314. QCOMPARE(v.getX(), 143.0);
  315. QCOMPARE(v.getY(), 5.0);
  316. QCOMPARE(v.getZ(), -328.0);
  317. }
  318. void TestVector3D::crossProduct2()
  319. {
  320. Vector3D v(10.0, 24.0, 8.0);
  321. Vector3D v2(15.0, 5.0, 0.0);
  322. v.crossProduct(v2);
  323. QCOMPARE(v.getX(), -40.0);
  324. QCOMPARE(v.getY(), 120.0);
  325. QCOMPARE(v.getZ(), -310.0);
  326. }
  327. void TestVector3D::crossProduct3()
  328. {
  329. Vector3D v1(10.0, 24.0, 8.0);
  330. Vector3D v2(15.0, 5.0, 0.0);
  331. Vector3D v = Vector3D::crossProduct(v1, v2);
  332. QCOMPARE(v.getX(), -40.0);
  333. QCOMPARE(v.getY(), 120.0);
  334. QCOMPARE(v.getZ(), -310.0);
  335. }
  336. void TestVector3D::norm1()
  337. {
  338. Vector3D v(3.0, 4.0, 0.0);;
  339. QCOMPARE(v.norm(), 5.0);
  340. }
  341. void TestVector3D::norm2()
  342. {
  343. Vector3D v(0.0, 30.0, 40.0);;
  344. QCOMPARE(v.norm(), 50.0);
  345. }
  346. void TestVector3D::getScalarOp()
  347. {
  348. double x = 1.42, y = 2.0, z = 3.0;
  349. Vector3D v(x, y, z);
  350. QCOMPARE(v[0], x);
  351. QCOMPARE(v[1], y);
  352. QCOMPARE(v[2], z);
  353. }
  354. QTEST_MAIN(TestVector3D)
  355. #include "testvector3d.moc"