Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

NativeArrayTests.cs 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using NUnit.Framework;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Collections.Tests;
  6. using Unity.Jobs;
  7. using Unity.Collections.LowLevel.Unsafe;
  8. internal class NativeArrayTests : CollectionsTestFixture
  9. {
  10. [Test]
  11. public unsafe void NativeArray_IndexOf_Int()
  12. {
  13. var container = new NativeArray<int>(10, Allocator.Persistent);
  14. container[0] = 123;
  15. container[1] = 789;
  16. bool r0 = false, r1 = false, r2 = false;
  17. GCAllocRecorder.ValidateNoGCAllocs(() =>
  18. {
  19. r0 = -1 != container.IndexOf(456);
  20. r1 = container.Contains(123);
  21. r2 = container.Contains(789);
  22. });
  23. Assert.False(r0);
  24. Assert.False(-1 != NativeArrayExtensions.IndexOf<int, int>(container.GetUnsafePtr(), container.Length, 456));
  25. Assert.True(r1);
  26. Assert.True(NativeArrayExtensions.Contains<int, int>(container.GetUnsafePtr(), container.Length, 123));
  27. Assert.True(r2);
  28. Assert.True(NativeArrayExtensions.Contains<int, int>(container.GetUnsafePtr(), container.Length, 789));
  29. container.Dispose();
  30. }
  31. [Test]
  32. public unsafe void NativeArray_IndexOf_FixedString128()
  33. {
  34. var container = new NativeArray<FixedString128Bytes>(10, Allocator.Persistent);
  35. container[0] = new FixedString128Bytes("123");
  36. container[1] = new FixedString128Bytes("789");
  37. bool r0 = false, r1 = false, r2 = false;
  38. GCAllocRecorder.ValidateNoGCAllocs(() =>
  39. {
  40. r0 = -1 != container.IndexOf(new FixedString128Bytes("456"));
  41. r1 = container.Contains(new FixedString128Bytes("123"));
  42. r2 = container.Contains(new FixedString128Bytes("789"));
  43. });
  44. Assert.False(r0);
  45. Assert.False(-1 != NativeArrayExtensions.IndexOf<FixedString128Bytes, FixedString128Bytes>(container.GetUnsafePtr(), container.Length, new FixedString128Bytes("456")));
  46. Assert.True(r1);
  47. Assert.True(NativeArrayExtensions.Contains<FixedString128Bytes, FixedString128Bytes>(container.GetUnsafePtr(), container.Length, new FixedString128Bytes("123")));
  48. Assert.True(r2);
  49. Assert.True(NativeArrayExtensions.Contains<FixedString128Bytes, FixedString128Bytes>(container.GetUnsafePtr(), container.Length, new FixedString128Bytes("789")));
  50. container.Dispose();
  51. }
  52. [Test]
  53. public void NativeArray_DisposeJob()
  54. {
  55. var container = new NativeArray<int>(1, Allocator.Persistent);
  56. Assert.True(container.IsCreated);
  57. Assert.DoesNotThrow(() => { container[0] = 1; });
  58. var disposeJob = container.Dispose(default);
  59. Assert.False(container.IsCreated);
  60. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  61. Assert.Throws<ObjectDisposedException>(
  62. () => { container[0] = 2; });
  63. #endif
  64. disposeJob.Complete();
  65. }
  66. [BurstCompile(CompileSynchronously = true)]
  67. struct NativeArrayPokeJob : IJob
  68. {
  69. NativeArray<int> array;
  70. public NativeArrayPokeJob(NativeArray<int> array) { this.array = array; }
  71. public void Execute()
  72. {
  73. array[0] = 1;
  74. }
  75. }
  76. [Test]
  77. [TestRequiresCollectionChecks]
  78. public void NativeArray_DisposeJobWithMissingDependencyThrows()
  79. {
  80. var array = new NativeArray<int>(1, Allocator.Persistent);
  81. var deps = new NativeArrayPokeJob(array).Schedule();
  82. Assert.Throws<InvalidOperationException>(() => { array.Dispose(default); });
  83. deps.Complete();
  84. array.Dispose();
  85. }
  86. [Test]
  87. [TestRequiresCollectionChecks]
  88. public void NativeArray_DisposeJobCantBeScheduled()
  89. {
  90. var array = new NativeArray<int>(1, Allocator.Persistent);
  91. var deps = array.Dispose(default);
  92. Assert.Throws<InvalidOperationException>(() => { new NativeArrayPokeJob(array).Schedule(deps); });
  93. deps.Complete();
  94. }
  95. [Test]
  96. unsafe public void NativeArray_ConvertExistingDataToNativeArray()
  97. {
  98. AllocatorManager.Initialize();
  99. var allocatorHelper = new AllocatorHelper<RewindableAllocator>(AllocatorManager.Persistent);
  100. allocatorHelper.Allocator.Initialize(64 * 1024);
  101. var nativeList = new NativeList<int>(allocatorHelper.Allocator.Handle);
  102. for(int i = 0; i < 20; i++)
  103. {
  104. nativeList.Add(i);
  105. }
  106. var nativeArray = CollectionHelper.ConvertExistingDataToNativeArray<int>(nativeList.GetUnsafePtr(), nativeList.Length, allocatorHelper.Allocator.Handle);
  107. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  108. var listSafety = NativeListUnsafeUtility.GetAtomicSafetyHandle(ref nativeList);
  109. NativeArrayUnsafeUtility.SetAtomicSafetyHandle<int>(ref nativeArray, listSafety);
  110. #endif
  111. for (int i = 0; i < 20; i++)
  112. {
  113. Assert.AreEqual(nativeArray[i], i);
  114. }
  115. nativeArray.Dispose();
  116. allocatorHelper.Allocator.Dispose();
  117. allocatorHelper.Dispose();
  118. AllocatorManager.Shutdown();
  119. }
  120. [Test]
  121. unsafe public void NativeArray_ConvertExistingDataToNativeArray_SetTempMemoryHandle()
  122. {
  123. var nativeList = new NativeList<int>(Allocator.Temp);
  124. for (int i = 0; i < 20; i++)
  125. {
  126. nativeList.Add(i);
  127. }
  128. var nativeArray = CollectionHelper.ConvertExistingDataToNativeArray<int>(nativeList.GetUnsafePtr(), nativeList.Length, Allocator.Temp, true);
  129. for (int i = 0; i < 20; i++)
  130. {
  131. Assert.AreEqual(nativeArray[i], i);
  132. }
  133. nativeArray.Dispose();
  134. }
  135. [Test]
  136. unsafe public void NativeArray_ConvertExistingNativeListToNativeArray()
  137. {
  138. AllocatorManager.Initialize();
  139. var allocatorHelper = new AllocatorHelper<RewindableAllocator>(AllocatorManager.Persistent);
  140. allocatorHelper.Allocator.Initialize(64 * 1024);
  141. var nativeList = new NativeList<int>(allocatorHelper.Allocator.Handle);
  142. for (int i = 0; i < 20; i++)
  143. {
  144. nativeList.Add(i);
  145. }
  146. var nativeArray = CollectionHelper.ConvertExistingNativeListToNativeArray<int>(ref nativeList, nativeList.Length, allocatorHelper.Allocator.Handle);
  147. for (int i = 0; i < 20; i++)
  148. {
  149. Assert.AreEqual(nativeArray[i], i);
  150. }
  151. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  152. var listSafety = NativeListUnsafeUtility.GetAtomicSafetyHandle(ref nativeList);
  153. var arraySafety = CollectionHelper.GetNativeArraySafetyHandle(ref nativeArray);
  154. Assert.AreEqual(listSafety, arraySafety);
  155. #endif
  156. nativeArray.Dispose();
  157. allocatorHelper.Allocator.Dispose();
  158. allocatorHelper.Dispose();
  159. AllocatorManager.Shutdown();
  160. }
  161. [Ignore("Wait for DOTS-7576 trunk PR in")]
  162. [Test]
  163. public void NativeArray_CustomAllocator_DisposeException()
  164. {
  165. AllocatorManager.Initialize();
  166. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  167. ref var allocator = ref allocatorHelper.Allocator;
  168. allocator.Initialize();
  169. var nativeArray = CollectionHelper.CreateNativeArray<int>(100, allocator.ToAllocator, NativeArrayOptions.UninitializedMemory);
  170. nativeArray[0] = 0xFE;
  171. var nativeList = new NativeList<int>(allocator.ToAllocator);
  172. for (int i = 0; i < 50; i++)
  173. {
  174. nativeList.Add(i);
  175. }
  176. Assert.IsTrue(allocator.WasUsed);
  177. Assert.Throws<InvalidOperationException>(() => nativeArray.Dispose());
  178. CollectionHelper.Dispose(nativeArray);
  179. nativeList.Dispose();
  180. allocator.Dispose();
  181. allocatorHelper.Dispose();
  182. AllocatorManager.Shutdown();
  183. }
  184. public struct SimpleTestJob : IJob
  185. {
  186. public int ElementValue;
  187. [WriteOnly]
  188. public NativeArray<int> Result;
  189. public void Execute()
  190. {
  191. for (int i = 0; i < Result.Length; ++i)
  192. Result[i] = ElementValue;
  193. }
  194. }
  195. [Ignore("Wait for DOTS-7576 trunk PR in")]
  196. [Test]
  197. public void NativeArray_CustomAllocator_DisposeHandleException()
  198. {
  199. AllocatorManager.Initialize();
  200. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  201. ref var allocator = ref allocatorHelper.Allocator;
  202. allocator.Initialize();
  203. var nativeArray = CollectionHelper.CreateNativeArray<int>(100, allocator.ToAllocator, NativeArrayOptions.UninitializedMemory);
  204. SimpleTestJob job = new SimpleTestJob()
  205. {
  206. ElementValue = 0xFE,
  207. Result = nativeArray
  208. };
  209. var jobHandle = job.Schedule();
  210. jobHandle.Complete();
  211. Assert.IsTrue(allocator.WasUsed);
  212. Assert.Throws<InvalidOperationException>(() => nativeArray.Dispose(jobHandle));
  213. CollectionHelper.Dispose(nativeArray);
  214. allocator.Dispose();
  215. allocatorHelper.Dispose();
  216. AllocatorManager.Shutdown();
  217. }
  218. }