You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testvector3d.cpp 7.4KB

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