pmtcreationandcleanup.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. class OwnershipTestDummyClass
  2. {
  3. // Simply exists
  4. }
  5. class PMTCreationAndCleanup : PMTF
  6. {
  7. int m_EventTestManagerID;
  8. bool m_bTestEventsPassed = false;
  9. int m_OwnershipTestManagerID;
  10. //---------------------------------------------------------------------------
  11. // Ctor - Decides the tests to run
  12. //---------------------------------------------------------------------------
  13. void PMTCreationAndCleanup()
  14. {
  15. //AddInitTest("TestInvalidSize");
  16. AddInitTest("TestCCSB");
  17. AddInitTest("TestEvents");
  18. AddInitTest("TestOwnership");
  19. }
  20. //---------------------------------------------------------------------------
  21. // Tests
  22. //---------------------------------------------------------------------------
  23. // Test invalid size VMEs
  24. TFResult TestInvalidSize()
  25. {
  26. Debug.ParticleLog("Expecting VME: Invalid size. (0)", this, "TestInvalidSize");
  27. TestCreationSmallBlocking(0, false);
  28. Debug.ParticleLog("Expecting VME: Invalid size. (-3)", this, "TestInvalidSize");
  29. TestCreationSmallBlocking(-3, false);
  30. return NTFR(TFR.SUCCESS); // No way to check if a VME popped or not...
  31. }
  32. //---------------------------------------------------------------------------
  33. // TestCreationCleanupSmallBlocking
  34. TFResult TestCCSB()
  35. {
  36. return TestCleanup("TestMultiCreation", 3000);
  37. }
  38. //---------------------------------------------------------------------------
  39. // Test if events are called properly
  40. TFResult TestEvents()
  41. {
  42. ParticleManager pm = new ParticleManager(new ParticleManagerSettings(3000));
  43. bool success = !pm.IsFinishedAllocating(); // We need it to not be done in the same frame
  44. if (Assert(success))
  45. {
  46. pm.GetEvents().Event_OnAllocationEnd.Insert(PassCheckEvents);
  47. m_EventTestManagerID = InsertManager(pm);
  48. AddFrameTest("CheckTestEvents");
  49. return NTFR(TFR.SUCCESS);
  50. }
  51. return NTFR(TFR.FAIL);
  52. }
  53. //---------------------------------------------------------------------------
  54. // Test ownership
  55. TFResult TestOwnership()
  56. {
  57. ParticleManager pm = CreatePMFixedBlocking(1);
  58. bool success = pm.IsFinishedAllocating(); // We need it to be done in the same frame
  59. if (Assert(success))
  60. {
  61. m_OwnershipTestManagerID = InsertManager(pm);
  62. OwnershipTestDummyClass dummy = new OwnershipTestDummyClass();
  63. ParticleProperties pp = new ParticleProperties(GetGame().GetPlayer().GetPosition(), ParticlePropertiesFlags.NONE, null, vector.Zero, dummy);
  64. string particlePath = ParticleList.GetParticleFullPath(ParticleList.EXPLOSION_LANDMINE);
  65. bool result = Assert(pm.CreateParticleByPath(particlePath, pp) != null);
  66. Debug.ParticleLog("Expecting VME: All particles in pool are already used.", this, "TestOwnership");
  67. result &= Assert(pm.CreateParticleByPath(particlePath, pp) == null);
  68. delete dummy;
  69. result &= Assert(pm.CreateParticleByPath(particlePath, pp) != null);
  70. return BTFR(result);
  71. }
  72. return NTFR(TFR.FAIL);
  73. }
  74. //---------------------------------------------------------------------------
  75. // OnFrame Checks
  76. //---------------------------------------------------------------------------
  77. TFResult CheckTestEvents()
  78. {
  79. ParticleManager pm;
  80. if (GetManager(m_EventTestManagerID, pm))
  81. {
  82. if (pm)
  83. {
  84. if (pm.IsFinishedAllocating())
  85. {
  86. return BTFR(Assert(m_bTestEventsPassed));
  87. }
  88. }
  89. else
  90. {
  91. return BTFR(Assert(false));
  92. }
  93. }
  94. else
  95. {
  96. return BTFR(Assert(false));
  97. }
  98. return NTFR(TFR.PENDING);
  99. }
  100. //---------------------------------------------------------------------------
  101. // Passes
  102. //---------------------------------------------------------------------------
  103. void PassCheckEvents(ParticleManager pm)
  104. {
  105. Assert(pm.IsFinishedAllocating());
  106. m_bTestEventsPassed = true;
  107. }
  108. //---------------------------------------------------------------------------
  109. // Helpers
  110. //---------------------------------------------------------------------------
  111. TFResult TestCreationSmallBlocking(int size, bool enableAsserts = true)
  112. {
  113. // Blocking, so that we can test AllocatedCount
  114. ParticleManager pm = CreatePMFixedBlocking(size);
  115. PrintPMStats(pm);
  116. bool success = true;
  117. if (enableAsserts)
  118. {
  119. success &= Assert(pm.GetPoolSize() == size);
  120. success &= Assert(pm.GetAllocatedCount() == size);
  121. success &= Assert(pm.GetEvents() != null);
  122. }
  123. return BTFR(success);
  124. }
  125. TFResult TestCleanup(string f, int p1 = 0)
  126. {
  127. int pmTotal = ParticleManager.GetStaticActiveCount();
  128. int psTotal = ParticleSource.GetStaticActiveCount();
  129. PrintActiveStats();
  130. TFResult res = CTFR();
  131. GetGame().GameScript.CallFunction(this, f, res, p1);
  132. int pmTotalPost = ParticleManager.GetStaticActiveCount();
  133. int psTotalPost = ParticleSource.GetStaticActiveCount();
  134. PrintActiveStats();
  135. bool success = Assert(pmTotal == pmTotalPost);
  136. success &= Assert(psTotal == psTotalPost);
  137. return res.And(BTFR(success));
  138. }
  139. TFResult TestMultiCreation(int instances)
  140. {
  141. TFResult res = CTFR();
  142. for (int i = 0; i < instances; ++i)
  143. {
  144. res.And(TestCreationSmallBlocking(1));
  145. }
  146. return res;
  147. }
  148. }