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