irrArray.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // Test the functionality of core::array
  95. bool testIrrArray(void)
  96. {
  97. bool allExpected = true;
  98. logTestString("crashTestFastAlloc\n");
  99. crashTestFastAlloc();
  100. allExpected &= testSelfAssignment();
  101. allExpected &= testSwap();
  102. allExpected &= testErase();
  103. if(allExpected)
  104. logTestString("\nAll tests passed\n");
  105. else
  106. logTestString("\nFAIL!\n");
  107. return allExpected;
  108. }