irrArray.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (C) 2008-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. #include <irrlicht.h>
  5. using namespace irr;
  6. using namespace core;
  7. core::map<int, int> countReferences;
  8. struct SDummy
  9. {
  10. SDummy(int a) : x(a)
  11. {
  12. countReferences.insert(x,1);
  13. }
  14. SDummy() : x(0)
  15. {
  16. countReferences.insert(x,1);
  17. }
  18. SDummy(const SDummy& other)
  19. {
  20. x = other.x;
  21. countReferences[x] = countReferences[x] + 1;
  22. }
  23. ~SDummy()
  24. {
  25. countReferences[x] = countReferences[x] - 1;
  26. }
  27. int x;
  28. };
  29. static bool testErase()
  30. {
  31. {
  32. core::array<SDummy> aaa;
  33. aaa.push_back(SDummy(0));
  34. aaa.push_back(SDummy(1));
  35. aaa.push_back(SDummy(2));
  36. aaa.push_back(SDummy(3));
  37. aaa.push_back(SDummy(4));
  38. aaa.push_back(SDummy(5));
  39. aaa.erase(0,2);
  40. }
  41. for ( core::map<int,int>::Iterator it = countReferences.getIterator(); !it.atEnd(); it++ )
  42. {
  43. if ( it->getValue() != 0 )
  44. {
  45. logTestString("testErase: wrong count for %d, it's: %d\n", it->getKey(), it->getValue());
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. struct VarArray
  52. {
  53. core::array < int, core::irrAllocatorFast<int> > MyArray;
  54. };
  55. static bool testSelfAssignment()
  56. {
  57. core::array<int> myArray;
  58. myArray.push_back(1);
  59. myArray = myArray;
  60. return myArray.size() == 1;
  61. }
  62. // this will (did once) crash when wrong due to deallocating memory twice, so no return value
  63. static void crashTestFastAlloc()
  64. {
  65. core::array < VarArray, core::irrAllocatorFast<VarArray> > ArrayArray;
  66. ArrayArray.setAllocStrategy(core::ALLOC_STRATEGY_SAFE); // force more re-allocations
  67. VarArray var;
  68. var.MyArray.setAllocStrategy(core::ALLOC_STRATEGY_SAFE); // force more re-allocations
  69. var.MyArray.push_back( 0 );
  70. for ( int i=0; i< 100; ++i )
  71. {
  72. ArrayArray.push_back(var);
  73. ArrayArray.push_back(var);
  74. }
  75. }
  76. static bool testSwap()
  77. {
  78. bool result = true;
  79. core::array<int> array1, array2, copy1, copy2;
  80. for ( int i=0; i<99; ++i )
  81. {
  82. array1.push_back(i);
  83. if ( i < 10 ) // we want also different container sizes
  84. array2.push_back(99-i);
  85. }
  86. copy1 = array1;
  87. copy2 = array2;
  88. array1.swap(array2);
  89. result &= (array1 == copy2);
  90. result &= (array2 == copy1);
  91. assert_log( result );
  92. return result;
  93. }
  94. // add numbers to the array going down from size to 1
  95. static void addInvNumbers(irr::core::array<int>& arr, irr::u32 size)
  96. {
  97. for ( irr::u32 i=0; i<size; ++i )
  98. {
  99. arr.push_back(size-i);
  100. }
  101. }
  102. // Ensure numbers are sorted in ascending order
  103. static bool validateSortedAscending(const irr::core::array<int>& arr)
  104. {
  105. for ( irr::u32 i=1; i< arr.size(); ++ i)
  106. {
  107. if ( arr[i-1] > arr[i] )
  108. return false;
  109. }
  110. return true;
  111. }
  112. static bool testSort()
  113. {
  114. irr::core::array<int> arr;
  115. for ( irr::u32 i=0; i<1000; ++i )
  116. {
  117. arr.clear();
  118. addInvNumbers(arr, i);
  119. arr.sort();
  120. if ( !validateSortedAscending(arr) )
  121. {
  122. return false;
  123. }
  124. }
  125. for ( irr::u32 i=0; i<1000; ++i )
  126. {
  127. arr.clear();
  128. addInvNumbers(arr, i);
  129. addInvNumbers(arr, i);
  130. arr.sort();
  131. if ( !validateSortedAscending(arr) )
  132. {
  133. return false;
  134. }
  135. }
  136. return true;
  137. }
  138. // Test the functionality of core::array
  139. bool testIrrArray(void)
  140. {
  141. bool allExpected = true;
  142. logTestString("crashTestFastAlloc\n");
  143. crashTestFastAlloc();
  144. allExpected &= testSelfAssignment();
  145. allExpected &= testSwap();
  146. allExpected &= testErase();
  147. allExpected &= testSort();
  148. if(allExpected)
  149. logTestString("\nAll tests passed\n");
  150. else
  151. logTestString("\nFAIL!\n");
  152. return allExpected;
  153. }