Array.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include "pch.h"
  2. #define NO_MAIN
  3. #include "Array.h"
  4. #include "CppUnitTest.h"
  5. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  6. namespace FrameworkTests
  7. {
  8. TEST_CLASS (ArrayTests)
  9. {
  10. public:
  11. TEST_METHOD (EmtptyTest)
  12. {
  13. Framework::Array<int> array;
  14. Assert::IsTrue(array.getEintragAnzahl() == 0,
  15. L"getEintragAnzahl() on empty array should be 0");
  16. int count = 0;
  17. for (auto i : array)
  18. count++;
  19. Assert::IsTrue(count == 0,
  20. L"Iteration with : over an empty array should not do anything");
  21. auto end = array.end();
  22. Assert::IsFalse(array.begin() != end,
  23. L"Iterator of an empty array should not have an element");
  24. Assert::IsFalse(
  25. array.hat(0), L"Empty Array should not have an element");
  26. }
  27. TEST_METHOD (AddTest)
  28. {
  29. Framework::Array<int> array;
  30. array.add(100);
  31. array.add(20, 1);
  32. array.add(10, 0);
  33. array.add(0);
  34. Assert::IsTrue(array.getEintragAnzahl() == 4,
  35. L"after adding 4 elements getEintragAnzahl() should be 4");
  36. Assert::IsTrue(array.get(0) == 10,
  37. L"invalid value at index 0 of array after adding elements");
  38. Assert::IsTrue(array.get(1) == 100,
  39. L"invalid value at index 1 of array after adding elements");
  40. Assert::IsTrue(array.get(2) == 20,
  41. L"invalid value at index 1 of array after adding elements");
  42. Assert::IsTrue(array.get(3) == 0,
  43. L"invalid value at index 1 of array after adding elements");
  44. auto i = array.begin();
  45. Assert::IsTrue(i.val() == 10,
  46. L"invalid value at index 0 of array after adding elements");
  47. i++;
  48. Assert::IsTrue(i.val() == 100,
  49. L"invalid value at index 1 of array after adding elements");
  50. ++i;
  51. Assert::IsTrue(i.val() == 20,
  52. L"invalid value at index 2 of array after adding elements");
  53. i = i.next();
  54. Assert::IsTrue(i.val() == 0,
  55. L"invalid value at index 3 of array after adding elements");
  56. Assert::IsFalse(i.hasNext(),
  57. L"Iterator has to much elements after adding elements to "
  58. L"array");
  59. auto end = array.end();
  60. Assert::IsFalse(++i != end,
  61. L"iterator should match end() after iterating throu the array");
  62. }
  63. TEST_METHOD (RemoveTest)
  64. {
  65. Framework::Array<int> array;
  66. array.add(100);
  67. array.add(20, 1);
  68. array.add(10, 0);
  69. array.add(0);
  70. array.remove(3);
  71. array.remove(1);
  72. array.remove(0);
  73. Assert::IsTrue(array.getEintragAnzahl() == 1,
  74. L"after adding 4 elements and removing 3 elements "
  75. L"getEintragAnzahl() should be 1");
  76. Assert::IsTrue(array.get(0) == 20,
  77. L"invalid value at index 0 of array after removing elements");
  78. Assert::IsTrue(array.begin().val() == 20,
  79. L"invalid value at index 0 of array after removing elements");
  80. Assert::IsFalse(array.begin().hasNext(),
  81. L"Iterator has to many elements after removing elements");
  82. auto end = array.end();
  83. Assert::IsFalse(array.begin().next() != end,
  84. L"Iterator has to many elements after removing elements");
  85. }
  86. TEST_METHOD (SwapTest)
  87. {
  88. Framework::Array<int> array;
  89. array.add(100);
  90. array.add(20, 1);
  91. array.add(10, 0);
  92. array.add(0);
  93. array.tausch(0, 3);
  94. array.tausch(1, 2);
  95. Assert::IsTrue(array.getEintragAnzahl() == 4,
  96. L"after adding 4 elements and swap elements getEintragAnzahl() "
  97. L"should be 4");
  98. Assert::IsTrue(array.get(0) == 0,
  99. L"invalid value at index 0 of array after swapping elements");
  100. Assert::IsTrue(array.get(1) == 20,
  101. L"invalid value at index 1 of array after swapping elements");
  102. Assert::IsTrue(array.get(2) == 100,
  103. L"invalid value at index 2 of array after swapping elements");
  104. Assert::IsTrue(array.get(3) == 10,
  105. L"invalid value at index 3 of array after swapping elements");
  106. array.tausch(2, 0);
  107. Assert::IsTrue(array.get(2) == 0,
  108. L"invalid value at index 2 of array after swapping elements");
  109. Assert::IsTrue(array.get(0) == 100,
  110. L"invalid value at index 0 of array after swapping elements");
  111. }
  112. TEST_METHOD (SetTest)
  113. {
  114. Framework::Array<int> array;
  115. array.add(100);
  116. array.add(20, 1);
  117. array.add(10, 0);
  118. array.add(0);
  119. array.set(30, 0);
  120. array.set(200, 3);
  121. array.set(500, 1);
  122. Assert::IsTrue(array.getEintragAnzahl() == 4,
  123. L"after adding 4 elements and changing elements "
  124. L"getEintragAnzahl() should be 4");
  125. Assert::IsTrue(array.get(0) == 30,
  126. L"invalid value at index 0 of array after swapping elements");
  127. Assert::IsTrue(array.get(1) == 500,
  128. L"invalid value at index 1 of array after swapping elements");
  129. Assert::IsTrue(array.get(2) == 20,
  130. L"invalid value at index 2 of array after swapping elements");
  131. Assert::IsTrue(array.get(3) == 200,
  132. L"invalid value at index 3 of array after swapping elements");
  133. }
  134. TEST_METHOD (ValueTest)
  135. {
  136. Framework::Array<int> array;
  137. array.add(100);
  138. array.add(20, 1);
  139. array.add(10, 0);
  140. array.add(0);
  141. array.removeValue(10);
  142. Assert::IsTrue(array.getEintragAnzahl() == 3,
  143. L"after adding 4 elements and removing elements by value "
  144. L"getEintragAnzahl() should be 3");
  145. Assert::IsTrue(array.getWertIndex(0) == 2,
  146. L"invalid value index of value after removing elements");
  147. Assert::IsTrue(array.getWertIndex(10) < 0,
  148. L"value is still in array after removeValue");
  149. }
  150. };
  151. template<typename T> class Test : public Framework::ReferenceCounter
  152. {
  153. int val = 0;
  154. T* tc;
  155. public:
  156. Test(const int v, T* tc)
  157. : ReferenceCounter(),
  158. val(v),
  159. tc(tc)
  160. {}
  161. ~Test()
  162. {
  163. tc->deleteCounter++;
  164. }
  165. operator int()
  166. {
  167. return val;
  168. }
  169. int getVal()
  170. {
  171. return val;
  172. }
  173. };
  174. TEST_CLASS (RCArrayTests)
  175. {
  176. public:
  177. int deleteCounter = 0;
  178. TEST_METHOD_INITIALIZE(InitTest)
  179. {
  180. deleteCounter = 0;
  181. }
  182. TEST_METHOD (EmtptyTest)
  183. {
  184. Framework::RCArray<Test<RCArrayTests>> array;
  185. Assert::IsTrue(array.getEintragAnzahl() == 0,
  186. L"getEintragAnzahl() on empty array should be 0");
  187. int count = 0;
  188. for (auto i : array)
  189. count++;
  190. Assert::IsTrue(count == 0,
  191. L"Iteration with : over an empty array should not do anything");
  192. auto end = array.end();
  193. Assert::IsFalse(array.begin() != end,
  194. L"Iterator of an empty array should not have an element");
  195. Assert::IsFalse(
  196. array.hat(0), L"Empty Array should not have an element");
  197. }
  198. TEST_METHOD (AddTest)
  199. {
  200. Framework::RCArray<Test<RCArrayTests>> array;
  201. array.add(new Test<RCArrayTests>(100, this));
  202. array.add(new Test<RCArrayTests>(20, this), 1);
  203. array.add(new Test<RCArrayTests>(10, this), 0);
  204. array.add(new Test<RCArrayTests>(0, this));
  205. Assert::IsTrue(array.getEintragAnzahl() == 4,
  206. L"after adding 4 elements getEintragAnzahl() should be 4");
  207. Assert::IsTrue((int)*array.z(0) == 10,
  208. L"invalid value at index 0 of array after adding elements");
  209. Assert::IsTrue((int)*array.z(1) == 100,
  210. L"invalid value at index 1 of array after adding elements");
  211. Assert::IsTrue((int)*array.z(2) == 20,
  212. L"invalid value at index 1 of array after adding elements");
  213. Assert::IsTrue((int)*array.z(3) == 0,
  214. L"invalid value at index 1 of array after adding elements");
  215. auto i = array.begin();
  216. Assert::IsTrue(i->getVal() == 10,
  217. L"invalid value at index 0 of array after adding elements");
  218. i++;
  219. Assert::IsTrue(i->getVal() == 100,
  220. L"invalid value at index 1 of array after adding elements");
  221. ++i;
  222. Assert::IsTrue(i->getVal() == 20,
  223. L"invalid value at index 2 of array after adding elements");
  224. i = i.next();
  225. Assert::IsTrue(i->getVal() == 0,
  226. L"invalid value at index 3 of array after adding elements");
  227. Assert::IsFalse(i.hasNext(),
  228. L"Iterator has to much elements after adding elements to "
  229. L"array");
  230. auto end = array.end();
  231. Assert::IsFalse(++i != end,
  232. L"iterator should match end() after iterating throu the array");
  233. array.leeren();
  234. Assert::IsTrue(deleteCounter == 4, L"Memory leaks detected");
  235. }
  236. TEST_METHOD (RemoveTest)
  237. {
  238. Framework::RCArray<Test<RCArrayTests>> array;
  239. array.add(new Test<RCArrayTests>(100, this));
  240. array.add(new Test<RCArrayTests>(20, this), 1);
  241. array.add(new Test<RCArrayTests>(10, this), 0);
  242. array.add(new Test<RCArrayTests>(0, this));
  243. array.remove(3);
  244. array.remove(1);
  245. array.remove(0);
  246. Assert::IsTrue(array.getEintragAnzahl() == 1,
  247. L"after adding 4 elements and removing 3 elements "
  248. L"getEintragAnzahl() should be 1");
  249. Assert::IsTrue((int)*array.z(0) == 20,
  250. L"invalid value at index 0 of array after removing elements");
  251. Assert::IsTrue(array.begin()->getVal() == 20,
  252. L"invalid value at index 0 of array after removing elements");
  253. Assert::IsFalse(array.begin().hasNext(),
  254. L"Iterator has to many elements after removing elements");
  255. auto end = array.end();
  256. Assert::IsFalse(array.begin().next() != end,
  257. L"Iterator has to many elements after removing elements");
  258. Assert::IsTrue(deleteCounter == 3, L"Memory leaks detected");
  259. array.leeren();
  260. Assert::IsTrue(deleteCounter == 4, L"Memory leaks detected");
  261. }
  262. TEST_METHOD (SwapTest)
  263. {
  264. Framework::RCArray<Test<RCArrayTests>> array;
  265. array.add(new Test<RCArrayTests>(100, this));
  266. array.add(new Test<RCArrayTests>(20, this), 1);
  267. array.add(new Test<RCArrayTests>(10, this), 0);
  268. array.add(new Test<RCArrayTests>(0, this));
  269. array.tausch(0, 3);
  270. array.tausch(1, 2);
  271. Assert::IsTrue(array.getEintragAnzahl() == 4,
  272. L"after adding 4 elements and swap elements getEintragAnzahl() "
  273. L"should be 4");
  274. Assert::IsTrue((int)*array.z(0) == 0,
  275. L"invalid value at index 0 of array after swapping elements");
  276. Assert::IsTrue((int)*array.z(1) == 20,
  277. L"invalid value at index 1 of array after swapping elements");
  278. Assert::IsTrue((int)*array.z(2) == 100,
  279. L"invalid value at index 2 of array after swapping elements");
  280. Assert::IsTrue((int)*array.z(3) == 10,
  281. L"invalid value at index 3 of array after swapping elements");
  282. array.tausch(2, 0);
  283. Assert::IsTrue((int)*array.z(2) == 0,
  284. L"invalid value at index 2 of array after swapping elements");
  285. Assert::IsTrue((int)*array.z(0) == 100,
  286. L"invalid value at index 0 of array after swapping elements");
  287. Assert::IsTrue(array.getEintragAnzahl() == 4,
  288. L"invalid value count in array after swapping elements");
  289. array.leeren();
  290. Assert::IsTrue(deleteCounter == 4, L"Memory leaks detected");
  291. }
  292. TEST_METHOD (SetTest)
  293. {
  294. Framework::RCArray<Test<RCArrayTests>> array;
  295. array.add(new Test<RCArrayTests>(100, this));
  296. array.add(new Test<RCArrayTests>(20, this), 1);
  297. array.add(new Test<RCArrayTests>(10, this), 0);
  298. array.add(new Test<RCArrayTests>(0, this));
  299. array.set(new Test<RCArrayTests>(30, this), 0);
  300. array.set(new Test<RCArrayTests>(200, this), 3);
  301. array.set(new Test<RCArrayTests>(500, this), 1);
  302. Assert::IsTrue(array.getEintragAnzahl() == 4,
  303. L"after adding 4 elements and changing elements "
  304. L"getEintragAnzahl() should be 4");
  305. Assert::IsTrue((int)*array.z(0) == 30,
  306. L"invalid value at index 0 of array after swapping elements");
  307. Assert::IsTrue((int)*array.z(1) == 500,
  308. L"invalid value at index 1 of array after swapping elements");
  309. Assert::IsTrue((int)*array.z(2) == 20,
  310. L"invalid value at index 2 of array after swapping elements");
  311. Assert::IsTrue((int)*array.z(3) == 200,
  312. L"invalid value at index 3 of array after swapping elements");
  313. Assert::IsTrue(deleteCounter == 3, L"Memory leaks detected");
  314. array.leeren();
  315. Assert::IsTrue(deleteCounter == 7, L"Memory leaks detected");
  316. }
  317. TEST_METHOD (IteratorTest)
  318. {
  319. Framework::RCArray<Test<RCArrayTests>> array;
  320. array.add(new Test<RCArrayTests>(100, this));
  321. array.begin().remove();
  322. Assert::IsTrue(array.getEintragAnzahl() == 0,
  323. L"after adding 1 element and removing it "
  324. L"getEintragAnzahl() should be 0");
  325. array.add(new Test<RCArrayTests>(100, this));
  326. array.add(new Test<RCArrayTests>(20, this), 1);
  327. array.add(new Test<RCArrayTests>(10, this), 0);
  328. array.add(new Test<RCArrayTests>(0, this));
  329. auto i = array.begin();
  330. Assert::IsTrue(i->getVal() == 10,
  331. L"invalid value at index 0 of array after adding elements");
  332. Assert::IsTrue(i.hasNext(), L"No next element after first element");
  333. Assert::IsTrue((++i)->getVal() == 100,
  334. L"invalid value at index 1 of array after adding elements");
  335. Assert::IsTrue(
  336. i.hasNext(), L"No next element after second element");
  337. Assert::IsTrue((++i)->getVal() == 20,
  338. L"invalid value at index 2 of array after adding elements");
  339. Assert::IsTrue(i.hasNext(), L"No next element after third element");
  340. Assert::IsTrue((++i)->getVal() == 0,
  341. L"invalid value at index 3 of array after adding elements");
  342. Assert::IsFalse(i.hasNext(), L"Next element after last element");
  343. i = array.begin();
  344. int count = 4;
  345. while (i)
  346. {
  347. i.remove();
  348. Assert::IsTrue(array.getEintragAnzahl() == --count,
  349. L"after removing 1 element "
  350. L"getEintragAnzahl() should be reduced");
  351. }
  352. Assert::IsTrue(deleteCounter == 5, L"Memory leaks detected");
  353. }
  354. TEST_METHOD (MoveTest)
  355. {
  356. Framework::RCArray<Test<RCArrayTests>> array;
  357. array.add(new Test<RCArrayTests>(100, this));
  358. array.add(new Test<RCArrayTests>(20, this));
  359. array.setPosition(0, 1);
  360. array.add(new Test<RCArrayTests>(10, this));
  361. Assert::IsTrue(array.getEintragAnzahl() == 3,
  362. L"after adding 3 elements "
  363. L"getEintragAnzahl() should be 0");
  364. Assert::IsTrue(array.z(2)->getVal() == 10,
  365. L"third element of array has an invalid value");
  366. }
  367. };
  368. } // namespace FrameworkTests