您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

testvector3d.cpp 7.1KB

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