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

NativeListDeferredArrayTests.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using NUnit.Framework;
  3. using Unity.Collections;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using Unity.Jobs;
  6. using Unity.Jobs.LowLevel.Unsafe;
  7. using Unity.Jobs.Tests.ManagedJobs;
  8. internal class NativeListDeferredArrayTests : JobTestsFixtureBasic
  9. {
  10. private bool JobsDebuggerWasEnabled;
  11. struct AliasJob : IJob
  12. {
  13. public NativeArray<int> array;
  14. public NativeList<int> list;
  15. public void Execute()
  16. {
  17. }
  18. }
  19. struct SetListLengthJob : IJob
  20. {
  21. public int ResizeLength;
  22. public NativeList<int> list;
  23. public void Execute()
  24. {
  25. list.Resize(ResizeLength, NativeArrayOptions.UninitializedMemory);
  26. }
  27. }
  28. struct SetArrayValuesJobParallel : IJobParallelForDefer
  29. {
  30. public NativeArray<int> array;
  31. public void Execute(int index)
  32. {
  33. array[index] = array.Length;
  34. }
  35. }
  36. struct GetArrayValuesJobParallel : IJobParallelForDefer
  37. {
  38. [ReadOnly]
  39. public NativeArray<int> array;
  40. public void Execute(int index)
  41. {
  42. }
  43. }
  44. struct ParallelForWithoutList : IJobParallelForDefer
  45. {
  46. public void Execute(int index)
  47. {
  48. }
  49. }
  50. [SetUp]
  51. public void NativeListDeferredArrayTestsSetup()
  52. {
  53. // Many ECS tests will only pass if the Jobs Debugger enabled;
  54. // force it enabled for all tests, and restore the original value at teardown.
  55. JobsDebuggerWasEnabled = JobsUtility.JobDebuggerEnabled;
  56. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  57. JobsUtility.JobDebuggerEnabled = true;
  58. #endif
  59. }
  60. [Test]
  61. public void ResizedListToDeferredJobArray([Values(0, 1, 2, 3, 4, 5, 6, 42, 97, 1023)] int length)
  62. {
  63. var list = new NativeList<int>(RwdAllocator.ToAllocator);
  64. var setLengthJob = new SetListLengthJob { list = list, ResizeLength = length };
  65. var jobHandle = setLengthJob.Schedule();
  66. var setValuesJob = new SetArrayValuesJobParallel { array = list.AsDeferredJobArray() };
  67. setValuesJob.Schedule(list, 3, jobHandle).Complete();
  68. Assert.AreEqual(length, list.Length);
  69. for (int i = 0; i != list.Length; i++)
  70. Assert.AreEqual(length, list[i]);
  71. }
  72. [Test]
  73. public unsafe void DeferredParallelForFromIntPtr()
  74. {
  75. int length = 10;
  76. var lengthValue = CollectionHelper.CreateNativeArray<int>(1, RwdAllocator.ToAllocator);
  77. lengthValue[0] = length;
  78. var array = CollectionHelper.CreateNativeArray<int>(length, RwdAllocator.ToAllocator);
  79. var setValuesJob = new SetArrayValuesJobParallel { array = array };
  80. setValuesJob.Schedule((int*)lengthValue.GetUnsafePtr(), 3).Complete();
  81. for (int i = 0; i != array.Length; i++)
  82. Assert.AreEqual(length, array[i]);
  83. }
  84. [Test]
  85. public void ResizeListBeforeSchedule([Values(5)] int length)
  86. {
  87. var list = new NativeList<int>(RwdAllocator.ToAllocator);
  88. var setLengthJob = new SetListLengthJob { list = list, ResizeLength = length }.Schedule();
  89. var setValuesJob = new SetArrayValuesJobParallel { array = list.AsDeferredJobArray() };
  90. setLengthJob.Complete();
  91. setValuesJob.Schedule(list, 3).Complete();
  92. Assert.AreEqual(length, list.Length);
  93. for (int i = 0; i != list.Length; i++)
  94. Assert.AreEqual(length, list[i]);
  95. }
  96. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  97. [Test]
  98. public void ResizedListToDeferredJobArray()
  99. {
  100. var list = new NativeList<int>(RwdAllocator.ToAllocator);
  101. list.Add(1);
  102. var array = list.AsDeferredJobArray();
  103. #pragma warning disable 0219 // assigned but its value is never used
  104. Assert.Throws<IndexOutOfRangeException>(() => { var value = array[0]; });
  105. #pragma warning restore 0219
  106. Assert.AreEqual(0, array.Length);
  107. }
  108. [Test]
  109. public void ResizeListWhileJobIsRunning()
  110. {
  111. var list = new NativeList<int>(RwdAllocator.ToAllocator);
  112. list.Resize(42, NativeArrayOptions.UninitializedMemory);
  113. var setValuesJob = new GetArrayValuesJobParallel { array = list.AsDeferredJobArray() };
  114. var jobHandle = setValuesJob.Schedule(list, 3);
  115. Assert.Throws<InvalidOperationException>(() => list.Resize(1, NativeArrayOptions.UninitializedMemory));
  116. jobHandle.Complete();
  117. }
  118. [Test]
  119. public void AliasArrayThrows()
  120. {
  121. var list = new NativeList<int>(RwdAllocator.ToAllocator);
  122. var aliasJob = new AliasJob { list = list, array = list.AsDeferredJobArray() };
  123. Assert.Throws<InvalidOperationException>(() => aliasJob.Schedule());
  124. }
  125. [Test]
  126. public void DeferredListMustExistInJobData()
  127. {
  128. var list = new NativeList<int>(RwdAllocator.ToAllocator);
  129. var job = new ParallelForWithoutList();
  130. Assert.Throws<InvalidOperationException>(() => job.Schedule(list, 64));
  131. }
  132. [Test]
  133. public void DeferredListCantBeDeletedWhileJobIsRunning()
  134. {
  135. var list = new NativeList<int>(RwdAllocator.ToAllocator);
  136. list.Resize(42, NativeArrayOptions.UninitializedMemory);
  137. var setValuesJob = new GetArrayValuesJobParallel { array = list.AsDeferredJobArray() };
  138. var jobHandle = setValuesJob.Schedule(list, 3);
  139. Assert.Throws<InvalidOperationException>(() => list.Dispose());
  140. jobHandle.Complete();
  141. }
  142. [Test]
  143. public void DeferredArrayCantBeAccessedOnMainthread()
  144. {
  145. var list = new NativeList<int>(RwdAllocator.ToAllocator);
  146. list.Add(1);
  147. var defer = list.AsDeferredJobArray();
  148. Assert.AreEqual(0, defer.Length);
  149. Assert.Throws<IndexOutOfRangeException>(() => defer[0] = 5);
  150. }
  151. #endif
  152. [TearDown]
  153. public void TearDown()
  154. {
  155. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  156. JobsUtility.JobDebuggerEnabled = JobsDebuggerWasEnabled;
  157. #endif
  158. }
  159. }