Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RewindableAllocatorTests.cs 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using NUnit.Framework;
  3. using Unity.Collections;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using Unity.Collections.Tests;
  6. using Unity.Jobs.LowLevel.Unsafe;
  7. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  8. internal class RewindableAllocatorTests
  9. {
  10. AllocatorHelper<RewindableAllocator> m_AllocatorHelper;
  11. protected ref RewindableAllocator RwdAllocator => ref m_AllocatorHelper.Allocator;
  12. [SetUp]
  13. public void Setup()
  14. {
  15. m_AllocatorHelper = new AllocatorHelper<RewindableAllocator>(Allocator.Persistent);
  16. m_AllocatorHelper.Allocator.Initialize(128 * 1024, true);
  17. }
  18. [TearDown]
  19. public void TearDown()
  20. {
  21. m_AllocatorHelper.Allocator.Dispose();
  22. m_AllocatorHelper.Dispose();
  23. }
  24. [Test]
  25. public unsafe void RewindTestVersionOverflow()
  26. {
  27. // Check allocator version overflow
  28. for (int i = 0; i < 65536 + 100; i++)
  29. {
  30. var container = RwdAllocator.AllocateNativeList<byte>(RwdAllocator.InitialSizeInBytes / 1000);
  31. container.Resize(1, NativeArrayOptions.ClearMemory);
  32. container[0] = 0xFE;
  33. RwdAllocator.Rewind();
  34. CollectionHelper.CheckAllocator(RwdAllocator.ToAllocator);
  35. }
  36. }
  37. #if UNITY_2022_3_OR_NEWER
  38. [Test]
  39. public unsafe void NativeArrayCustomAllocatorExceptionWorks()
  40. {
  41. NativeArray<int> array = default;
  42. Assert.Throws<ArgumentException>(() =>
  43. {
  44. array = new NativeArray<int>(2, RwdAllocator.ToAllocator);
  45. });
  46. }
  47. #endif
  48. [TestRequiresCollectionChecks]
  49. public unsafe void RewindInvalidatesNativeList()
  50. {
  51. var container = RwdAllocator.AllocateNativeList<byte>(RwdAllocator.InitialSizeInBytes / 1000);
  52. container.Resize(1, NativeArrayOptions.ClearMemory);
  53. container[0] = 0xFE;
  54. RwdAllocator.Rewind();
  55. Assert.Throws<ObjectDisposedException>(() =>
  56. {
  57. container[0] = 0xEF;
  58. });
  59. }
  60. [Test]
  61. [TestRequiresCollectionChecks]
  62. public unsafe void RewindInvalidatesNativeArray()
  63. {
  64. var container = RwdAllocator.AllocateNativeArray<byte>(RwdAllocator.InitialSizeInBytes / 1000);
  65. container[0] = 0xFE;
  66. RwdAllocator.Rewind();
  67. Assert.Throws<ObjectDisposedException>(() =>
  68. {
  69. container[0] = 0xEF;
  70. });
  71. }
  72. [Test]
  73. public unsafe void NativeListCanBeCreatedViaMemberFunction()
  74. {
  75. var container = RwdAllocator.AllocateNativeList<byte>(RwdAllocator.InitialSizeInBytes / 1000);
  76. container.Resize(1, NativeArrayOptions.ClearMemory);
  77. container[0] = 0xFE;
  78. }
  79. [Test]
  80. public unsafe void NativeListCanBeDisposed()
  81. {
  82. var container = RwdAllocator.AllocateNativeList<byte>(RwdAllocator.InitialSizeInBytes / 1000);
  83. container.Resize(1, NativeArrayOptions.ClearMemory);
  84. container[0] = 0xFE;
  85. container.Dispose();
  86. RwdAllocator.Rewind();
  87. }
  88. [Test]
  89. public void NativeArrayCanBeDisposed()
  90. {
  91. var container = RwdAllocator.AllocateNativeArray<byte>(RwdAllocator.InitialSizeInBytes / 1000);
  92. container[0] = 0xFE;
  93. container.Dispose();
  94. RwdAllocator.Rewind();
  95. }
  96. [Test]
  97. public void NumberOfBlocksIsTemporarilyStable()
  98. {
  99. RwdAllocator.AllocateNativeList<byte>(RwdAllocator.InitialSizeInBytes * 10);
  100. var blocksBefore = RwdAllocator.BlocksAllocated;
  101. RwdAllocator.Rewind();
  102. var blocksAfter = RwdAllocator.BlocksAllocated;
  103. Assert.AreEqual(blocksAfter, blocksBefore);
  104. }
  105. [Test]
  106. public void NumberOfBlocksEventuallyDrops()
  107. {
  108. RwdAllocator.AllocateNativeList<byte>(RwdAllocator.InitialSizeInBytes * 10);
  109. var blocksBefore = RwdAllocator.BlocksAllocated;
  110. RwdAllocator.Rewind();
  111. RwdAllocator.Rewind();
  112. var blocksAfter = RwdAllocator.BlocksAllocated;
  113. Assert.IsTrue(blocksAfter < blocksBefore);
  114. }
  115. [Test]
  116. public void PossibleToAllocateGigabytes()
  117. {
  118. const int giga = 1024 * 1024 * 1024;
  119. var container0 = RwdAllocator.AllocateNativeList<byte>(giga);
  120. var container1 = RwdAllocator.AllocateNativeList<byte>(giga);
  121. var container2 = RwdAllocator.AllocateNativeList<byte>(giga);
  122. container0.Resize(1, NativeArrayOptions.ClearMemory);
  123. container1.Resize(1, NativeArrayOptions.ClearMemory);
  124. container2.Resize(1, NativeArrayOptions.ClearMemory);
  125. container0[0] = 0;
  126. container1[0] = 1;
  127. container2[0] = 2;
  128. Assert.AreEqual((byte)0, container0[0]);
  129. Assert.AreEqual((byte)1, container1[0]);
  130. Assert.AreEqual((byte)2, container2[0]);
  131. }
  132. [Test]
  133. public void ExhaustsFirstBlockBeforeAllocatingMore()
  134. {
  135. for (var i = 0; i < 50; ++i)
  136. {
  137. RwdAllocator.AllocateNativeList<byte>(RwdAllocator.InitialSizeInBytes / 100);
  138. Assert.AreEqual(1, RwdAllocator.BlocksAllocated);
  139. }
  140. RwdAllocator.AllocateNativeList<byte>(RwdAllocator.InitialSizeInBytes);
  141. Assert.AreEqual(2, RwdAllocator.BlocksAllocated);
  142. }
  143. unsafe struct ListProvider
  144. {
  145. NativeList<byte> m_Bytes;
  146. public ListProvider(AllocatorManager.AllocatorHandle allocatorHandle) => m_Bytes = new NativeList<byte>(allocatorHandle);
  147. public void Append<T>(ref T data) where T : unmanaged =>
  148. m_Bytes.AddRange(UnsafeUtility.AddressOf(ref data), UnsafeUtility.SizeOf<T>());
  149. }
  150. static void TriggerBug(AllocatorManager.AllocatorHandle allocatorHandle, NativeArray<byte> data)
  151. {
  152. var listProvider = new ListProvider(allocatorHandle);
  153. var datum = 0u;
  154. listProvider.Append(ref datum); // 'data' is now invalid after call to AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(m_Safety);
  155. Assert.That(data[0], Is.EqualTo(0));
  156. }
  157. [Test]
  158. public void AddRange_WhenCalledOnStructMember_DoesNotInvalidateUnrelatedListHigherOnCallStack()
  159. {
  160. AllocatorManager.AllocatorHandle allocatorHandle = RwdAllocator.Handle;
  161. var unrelatedList = new NativeList<byte>(allocatorHandle) { 0, 0 };
  162. Assert.That(unrelatedList.Length, Is.EqualTo(2));
  163. Assert.That(unrelatedList[0], Is.EqualTo(0));
  164. TriggerBug(allocatorHandle, unrelatedList.AsArray());
  165. }
  166. [Test]
  167. unsafe public void ExceedMaxBlockSize_BlockSizeLinearGrow()
  168. {
  169. AllocatorManager.AllocatorHandle allocatorHandle = RwdAllocator.Handle;
  170. var allocationSizes = new NativeList<int>(Allocator.Persistent);
  171. allocationSizes.Add(1);
  172. allocationSizes.Add((int)RwdAllocator.MaxMemoryBlockSize + 256);
  173. allocationSizes.Add(1);
  174. allocationSizes.Add(RwdAllocator.InitialSizeInBytes);
  175. int mask = JobsUtility.CacheLineSize - 1;
  176. var expectedBlockSizes = new NativeList<int>(Allocator.Persistent);
  177. expectedBlockSizes.Add(RwdAllocator.InitialSizeInBytes);
  178. expectedBlockSizes.Add(RwdAllocator.InitialSizeInBytes + (((int)RwdAllocator.MaxMemoryBlockSize + 256 + mask) & ~mask));
  179. expectedBlockSizes.Add(RwdAllocator.InitialSizeInBytes + (((int)RwdAllocator.MaxMemoryBlockSize + 256 + mask) & ~mask));
  180. var expected = RwdAllocator.InitialSizeInBytes + (((int)RwdAllocator.MaxMemoryBlockSize + 256 + mask) & ~mask) +
  181. (((int)RwdAllocator.MaxMemoryBlockSize + 256 + mask) & ~mask) +(int)RwdAllocator.MaxMemoryBlockSize;
  182. expectedBlockSizes.Add(expected);
  183. for(int i = 0; i < allocationSizes.Length; i++)
  184. {
  185. AllocatorManager.Allocate(allocatorHandle, sizeof(byte), sizeof(byte), allocationSizes[i]);
  186. int bytesUsed = (int)RwdAllocator.BytesAllocated;
  187. Assert.AreEqual(bytesUsed, expectedBlockSizes[i]);
  188. }
  189. allocationSizes.Dispose();
  190. expectedBlockSizes.Dispose();
  191. }
  192. }
  193. #endif