Array.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "pch.h"
  2. #include "CppUnitTest.h"
  3. #include <Array.h>
  4. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  5. namespace FrameworkTests
  6. {
  7. TEST_CLASS( ArrayTests )
  8. {
  9. public:
  10. TEST_METHOD( EmtptyTest )
  11. {
  12. Framework::Array<int> array;
  13. Assert::IsTrue( array.getEintragAnzahl() == 0, L"getEintragAnzahl() on empty array should be 0" );
  14. int count = 0;
  15. for( auto i : array ) count++;
  16. Assert::IsTrue( count == 0, L"Iteration with : over an empty array should not do anything" );
  17. auto end = array.end();
  18. Assert::IsFalse( array.begin() != end, L"Iterator of an empty array should not have an element" );
  19. Assert::IsFalse( array.hat( 0 ), L"Empty Array should not have an element" );
  20. }
  21. TEST_METHOD( AddTest )
  22. {
  23. Framework::Array<int> array;
  24. array.add( 100 );
  25. array.add( 20, 1 );
  26. array.add( 10, 0 );
  27. array.add( 0 );
  28. Assert::IsTrue( array.getEintragAnzahl() == 4, L"after adding 4 elements getEintragAnzahl() should be 4" );
  29. Assert::IsTrue( array.get( 0 ) == 10, L"invalid value at index 0 of array after adding elements" );
  30. Assert::IsTrue( array.get( 1 ) == 100, L"invalid value at index 1 of array after adding elements" );
  31. Assert::IsTrue( array.get( 2 ) == 20, L"invalid value at index 1 of array after adding elements" );
  32. Assert::IsTrue( array.get( 3 ) == 0, L"invalid value at index 1 of array after adding elements" );
  33. auto i = array.begin();
  34. Assert::IsTrue( i.val() == 10, L"invalid value at index 0 of array after adding elements" );
  35. i++;
  36. Assert::IsTrue( i.val() == 100, L"invalid value at index 1 of array after adding elements" );
  37. ++i;
  38. Assert::IsTrue( i.val() == 20, L"invalid value at index 2 of array after adding elements" );
  39. i = i.next();
  40. Assert::IsTrue( i.val() == 0, L"invalid value at index 3 of array after adding elements" );
  41. Assert::IsFalse( i.hasNext(), L"Iterator has to much elements after adding elements to array" );
  42. auto end = array.end();
  43. Assert::IsFalse( ++i != end, L"iterator should match end() after iterating throu the array" );
  44. }
  45. TEST_METHOD( RemoveTest )
  46. {
  47. Framework::Array<int> array;
  48. array.add( 100 );
  49. array.add( 20, 1 );
  50. array.add( 10, 0 );
  51. array.add( 0 );
  52. array.remove( 3 );
  53. array.remove( 1 );
  54. array.remove( 0 );
  55. Assert::IsTrue( array.getEintragAnzahl() == 1, L"after adding 4 elements and removing 3 elements getEintragAnzahl() should be 1" );
  56. Assert::IsTrue( array.get( 0 ) == 20, L"invalid value at index 0 of array after removing elements" );
  57. Assert::IsTrue( array.begin().val() == 20, L"invalid value at index 0 of array after removing elements" );
  58. Assert::IsFalse( array.begin().hasNext(), L"Iterator has to many elements after removing elements" );
  59. auto end = array.end();
  60. Assert::IsFalse( array.begin().next() != end, L"Iterator has to many elements after removing elements" );
  61. }
  62. TEST_METHOD( SwapTest )
  63. {
  64. Framework::Array<int> array;
  65. array.add( 100 );
  66. array.add( 20, 1 );
  67. array.add( 10, 0 );
  68. array.add( 0 );
  69. array.tausch( 0, 3 );
  70. array.tausch( 1, 2 );
  71. Assert::IsTrue( array.getEintragAnzahl() == 4, L"after adding 4 elements and swap elements getEintragAnzahl() should be 4" );
  72. Assert::IsTrue( array.get( 0 ) == 0, L"invalid value at index 0 of array after swapping elements" );
  73. Assert::IsTrue( array.get( 1 ) == 20, L"invalid value at index 1 of array after swapping elements" );
  74. Assert::IsTrue( array.get( 2 ) == 100, L"invalid value at index 2 of array after swapping elements" );
  75. Assert::IsTrue( array.get( 3 ) == 10, L"invalid value at index 3 of array after swapping elements" );
  76. array.tausch( 2, 0 );
  77. Assert::IsTrue( array.get( 2 ) == 0, L"invalid value at index 2 of array after swapping elements" );
  78. Assert::IsTrue( array.get( 0 ) == 100, L"invalid value at index 0 of array after swapping elements" );
  79. }
  80. TEST_METHOD( SetTest )
  81. {
  82. Framework::Array<int> array;
  83. array.add( 100 );
  84. array.add( 20, 1 );
  85. array.add( 10, 0 );
  86. array.add( 0 );
  87. array.set( 30, 0 );
  88. array.set( 200, 3 );
  89. array.set( 500, 1 );
  90. Assert::IsTrue( array.getEintragAnzahl() == 4, L"after adding 4 elements and changing elements getEintragAnzahl() should be 4" );
  91. Assert::IsTrue( array.get( 0 ) == 30, L"invalid value at index 0 of array after swapping elements" );
  92. Assert::IsTrue( array.get( 1 ) == 500, L"invalid value at index 1 of array after swapping elements" );
  93. Assert::IsTrue( array.get( 2 ) == 20, L"invalid value at index 2 of array after swapping elements" );
  94. Assert::IsTrue( array.get( 3 ) == 200, L"invalid value at index 3 of array after swapping elements" );
  95. }
  96. TEST_METHOD( ValueTest )
  97. {
  98. Framework::Array<int> array;
  99. array.add( 100 );
  100. array.add( 20, 1 );
  101. array.add( 10, 0 );
  102. array.add( 0 );
  103. array.removeValue( 10 );
  104. Assert::IsTrue( array.getEintragAnzahl() == 3, L"after adding 4 elements and removing elements by value getEintragAnzahl() should be 3" );
  105. Assert::IsTrue( array.getWertIndex( 0 ) == 2, L"invalid value index of value after removing elements" );
  106. Assert::IsTrue( array.getWertIndex( 10 ) < 0, L"value is still in array after removeValue" );
  107. }
  108. };
  109. template<typename T>
  110. class Test : public Framework::ReferenceCounter
  111. {
  112. int val = 0;
  113. T* tc;
  114. public:
  115. Test( const int v, T* tc )
  116. : ReferenceCounter(),
  117. val( v ),
  118. tc( tc )
  119. {}
  120. ~Test()
  121. {
  122. tc->deleteCounter++;
  123. }
  124. operator int()
  125. {
  126. return val;
  127. }
  128. int getVal()
  129. {
  130. return val;
  131. }
  132. };
  133. TEST_CLASS( RCArrayTests )
  134. {
  135. public:
  136. int deleteCounter = 0;
  137. TEST_METHOD_INITIALIZE( InitTest )
  138. {
  139. deleteCounter = 0;
  140. }
  141. TEST_METHOD( EmtptyTest )
  142. {
  143. Framework::RCArray<Test<RCArrayTests>> array;
  144. Assert::IsTrue( array.getEintragAnzahl() == 0, L"getEintragAnzahl() on empty array should be 0" );
  145. int count = 0;
  146. for( auto i : array ) count++;
  147. Assert::IsTrue( count == 0, L"Iteration with : over an empty array should not do anything" );
  148. auto end = array.end();
  149. Assert::IsFalse( array.begin() != end, L"Iterator of an empty array should not have an element" );
  150. Assert::IsFalse( array.hat( 0 ), L"Empty Array should not have an element" );
  151. }
  152. TEST_METHOD( AddTest )
  153. {
  154. Framework::RCArray<Test<RCArrayTests>> array;
  155. array.add( new Test<RCArrayTests>( 100, this ) );
  156. array.add( new Test<RCArrayTests>( 20, this ), 1 );
  157. array.add( new Test<RCArrayTests>( 10, this ), 0 );
  158. array.add( new Test<RCArrayTests>( 0, this ) );
  159. Assert::IsTrue( array.getEintragAnzahl() == 4, L"after adding 4 elements getEintragAnzahl() should be 4" );
  160. Assert::IsTrue( (int)*array.z( 0 ) == 10, L"invalid value at index 0 of array after adding elements" );
  161. Assert::IsTrue( (int)*array.z( 1 ) == 100, L"invalid value at index 1 of array after adding elements" );
  162. Assert::IsTrue( (int)*array.z( 2 ) == 20, L"invalid value at index 1 of array after adding elements" );
  163. Assert::IsTrue( (int)*array.z( 3 ) == 0, L"invalid value at index 1 of array after adding elements" );
  164. auto i = array.begin();
  165. Assert::IsTrue( i->getVal() == 10, L"invalid value at index 0 of array after adding elements" );
  166. i++;
  167. Assert::IsTrue( i->getVal() == 100, L"invalid value at index 1 of array after adding elements" );
  168. ++i;
  169. Assert::IsTrue( i->getVal() == 20, L"invalid value at index 2 of array after adding elements" );
  170. i = i.next();
  171. Assert::IsTrue( i->getVal() == 0, L"invalid value at index 3 of array after adding elements" );
  172. Assert::IsFalse( i.hasNext(), L"Iterator has to much elements after adding elements to array" );
  173. auto end = array.end();
  174. Assert::IsFalse( ++i != end, L"iterator should match end() after iterating throu the array" );
  175. array.leeren();
  176. Assert::IsTrue( deleteCounter == 4, L"Memory leaks detected" );
  177. }
  178. TEST_METHOD( RemoveTest )
  179. {
  180. Framework::RCArray<Test<RCArrayTests>> array;
  181. array.add( new Test<RCArrayTests>( 100, this ) );
  182. array.add( new Test<RCArrayTests>( 20, this ), 1 );
  183. array.add( new Test<RCArrayTests>( 10, this ), 0 );
  184. array.add( new Test<RCArrayTests>( 0, this ) );
  185. array.remove( 3 );
  186. array.remove( 1 );
  187. array.remove( 0 );
  188. Assert::IsTrue( array.getEintragAnzahl() == 1, L"after adding 4 elements and removing 3 elements getEintragAnzahl() should be 1" );
  189. Assert::IsTrue( (int)*array.z( 0 ) == 20, L"invalid value at index 0 of array after removing elements" );
  190. Assert::IsTrue( array.begin()->getVal() == 20, L"invalid value at index 0 of array after removing elements" );
  191. Assert::IsFalse( array.begin().hasNext(), L"Iterator has to many elements after removing elements" );
  192. auto end = array.end();
  193. Assert::IsFalse( array.begin().next() != end, L"Iterator has to many elements after removing elements" );
  194. Assert::IsTrue( deleteCounter == 3, L"Memory leaks detected" );
  195. array.leeren();
  196. Assert::IsTrue( deleteCounter == 4, L"Memory leaks detected" );
  197. }
  198. TEST_METHOD( SwapTest )
  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. array.tausch( 0, 3 );
  206. array.tausch( 1, 2 );
  207. Assert::IsTrue( array.getEintragAnzahl() == 4, L"after adding 4 elements and swap elements getEintragAnzahl() should be 4" );
  208. Assert::IsTrue( (int)*array.z( 0 ) == 0, L"invalid value at index 0 of array after swapping elements" );
  209. Assert::IsTrue( (int)*array.z( 1 ) == 20, L"invalid value at index 1 of array after swapping elements" );
  210. Assert::IsTrue( (int)*array.z( 2 ) == 100, L"invalid value at index 2 of array after swapping elements" );
  211. Assert::IsTrue( (int)*array.z( 3 ) == 10, L"invalid value at index 3 of array after swapping elements" );
  212. array.tausch( 2, 0 );
  213. Assert::IsTrue( (int)*array.z( 2 ) == 0, L"invalid value at index 2 of array after swapping elements" );
  214. Assert::IsTrue( (int)*array.z( 0 ) == 100, L"invalid value at index 0 of array after swapping elements" );
  215. array.leeren();
  216. Assert::IsTrue( deleteCounter == 4, L"Memory leaks detected" );
  217. }
  218. TEST_METHOD( SetTest )
  219. {
  220. Framework::RCArray<Test<RCArrayTests>> array;
  221. array.add( new Test<RCArrayTests>( 100, this ) );
  222. array.add( new Test<RCArrayTests>( 20, this ), 1 );
  223. array.add( new Test<RCArrayTests>( 10, this ), 0 );
  224. array.add( new Test<RCArrayTests>( 0, this ) );
  225. array.set( new Test<RCArrayTests>( 30, this ), 0 );
  226. array.set( new Test<RCArrayTests>( 200, this ), 3 );
  227. array.set( new Test<RCArrayTests>( 500, this ), 1 );
  228. Assert::IsTrue( array.getEintragAnzahl() == 4, L"after adding 4 elements and changing elements getEintragAnzahl() should be 4" );
  229. Assert::IsTrue( (int)*array.z( 0 ) == 30, L"invalid value at index 0 of array after swapping elements" );
  230. Assert::IsTrue( (int)*array.z( 1 ) == 500, L"invalid value at index 1 of array after swapping elements" );
  231. Assert::IsTrue( (int)*array.z( 2 ) == 20, L"invalid value at index 2 of array after swapping elements" );
  232. Assert::IsTrue( (int)*array.z( 3 ) == 200, L"invalid value at index 3 of array after swapping elements" );
  233. Assert::IsTrue( deleteCounter == 3, L"Memory leaks detected" );
  234. array.leeren();
  235. Assert::IsTrue( deleteCounter == 7, L"Memory leaks detected" );
  236. }
  237. };
  238. }