暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CollectionsTestFixture.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using NUnit.Framework;
  2. using Unity.Burst;
  3. using Unity.Jobs.LowLevel.Unsafe;
  4. namespace Unity.Collections.Tests
  5. {
  6. // If ENABLE_UNITY_COLLECTIONS_CHECKS is not defined we will ignore the test
  7. // When using this attribute, consider it to logically AND with any other TestRequiresxxxx attrubute
  8. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  9. internal class TestRequiresCollectionChecks : System.Attribute
  10. {
  11. public TestRequiresCollectionChecks(string msg = null) { }
  12. }
  13. #else
  14. internal class TestRequiresCollectionChecks : IgnoreAttribute
  15. {
  16. public TestRequiresCollectionChecks(string msg = null) : base($"Test requires ENABLE_UNITY_COLLECTION_CHECKS which is not defined{(msg == null ? "." : $": {msg}")}") { }
  17. }
  18. #endif
  19. // If ENABLE_UNITY_COLLECTIONS_CHECKS and UNITY_DOTS_DEBUG is not defined we will ignore the test
  20. // conversely if either of them are defined the test will be run.
  21. // When using this attribute, consider it to logically AND with any other TestRequiresxxxx attrubute
  22. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  23. internal class TestRequiresDotsDebugOrCollectionChecks : System.Attribute
  24. {
  25. public TestRequiresDotsDebugOrCollectionChecks(string msg = null) { }
  26. }
  27. #else
  28. internal class TestRequiresDotsDebugOrCollectionChecks : IgnoreAttribute
  29. {
  30. public TestRequiresDotsDebugOrCollectionChecks(string msg = null) : base($"Test requires UNITY_DOTS_DEBUG || ENABLE_UNITY_COLLECTION_CHECKS which neither are defined{(msg == null ? "." : $": {msg}")}") { }
  31. }
  32. #endif
  33. internal class CollectionsTestCommonBase
  34. {
  35. AllocatorHelper<RewindableAllocator> rwdAllocatorHelper;
  36. protected AllocatorHelper<RewindableAllocator> CommonRwdAllocatorHelper => rwdAllocatorHelper;
  37. protected ref RewindableAllocator CommonRwdAllocator => ref rwdAllocatorHelper.Allocator;
  38. [SetUp]
  39. public virtual void Setup()
  40. {
  41. }
  42. [OneTimeSetUp]
  43. public void OneTimeSetup()
  44. {
  45. rwdAllocatorHelper = new AllocatorHelper<RewindableAllocator>(Allocator.Persistent);
  46. CommonRwdAllocator.Initialize(128 * 1024, true);
  47. }
  48. [OneTimeTearDown]
  49. public void OneTimeTearDown()
  50. {
  51. CommonRwdAllocator.Dispose();
  52. rwdAllocatorHelper.Dispose();
  53. }
  54. [TearDown]
  55. public virtual void TearDown()
  56. {
  57. CommonRwdAllocator.Rewind();
  58. // This is test only behavior for determinism. Rewind twice such that all
  59. // tests start with an allocator containing only one memory block.
  60. CommonRwdAllocator.Rewind();
  61. }
  62. }
  63. /// <summary>
  64. /// Collections test fixture to do setup and teardown.
  65. /// </summary>
  66. /// <remarks>
  67. /// Jobs debugger and safety checks should always be enabled when running collections tests. This fixture verifies
  68. /// those are enabled to prevent crashing the editor.
  69. /// </remarks>
  70. internal abstract class CollectionsTestFixture : CollectionsTestCommonBase
  71. {
  72. static string SafetyChecksMenu = "Jobs > Burst > Safety Checks";
  73. private bool JobsDebuggerWasEnabled;
  74. [SetUp]
  75. public override void Setup()
  76. {
  77. base.Setup();
  78. // Many ECS tests will only pass if the Jobs Debugger enabled;
  79. // force it enabled for all tests, and restore the original value at teardown.
  80. JobsDebuggerWasEnabled = JobsUtility.JobDebuggerEnabled;
  81. JobsUtility.JobDebuggerEnabled = true;
  82. Assert.IsTrue(BurstCompiler.Options.EnableBurstSafetyChecks, $"Collections tests must have Burst safety checks enabled! To enable, go to {SafetyChecksMenu}");
  83. }
  84. [TearDown]
  85. public override void TearDown()
  86. {
  87. JobsUtility.JobDebuggerEnabled = JobsDebuggerWasEnabled;
  88. base.TearDown();
  89. }
  90. }
  91. }