No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JobTestsFixture.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using NUnit.Framework;
  2. using Unity.Collections;
  3. using Unity.Jobs;
  4. namespace Unity.Jobs.Tests.ManagedJobs
  5. {
  6. internal class JobTestsFixtureBasic
  7. {
  8. AllocatorHelper<RewindableAllocator> m_AllocatorHelper;
  9. protected ref RewindableAllocator RwdAllocator => ref m_AllocatorHelper.Allocator;
  10. [OneTimeSetUp]
  11. public virtual void OneTimeSetUp()
  12. {
  13. m_AllocatorHelper = new AllocatorHelper<RewindableAllocator>(Allocator.Persistent);
  14. m_AllocatorHelper.Allocator.Initialize(128 * 1024, true);
  15. }
  16. [OneTimeTearDown]
  17. public virtual void OneTimeTearDown()
  18. {
  19. m_AllocatorHelper.Allocator.Dispose();
  20. m_AllocatorHelper.Dispose();
  21. }
  22. [TearDown]
  23. public void Teardown()
  24. {
  25. RwdAllocator.Rewind();
  26. // This is test only behavior for determinism. Rewind twice such that all
  27. // tests start with an allocator containing only one memory block.
  28. RwdAllocator.Rewind();
  29. }
  30. }
  31. internal class JobTestsFixture : JobTestsFixtureBasic
  32. {
  33. /*
  34. * this used to test both, and maybe it should again, but we have to make GetExecuteMethod() work with
  35. * multiple interfaces, hopefully in a non-global way
  36. */
  37. public struct SumDataParallelForJob : /*IJob,*/ IJobParallelFor
  38. {
  39. [ReadOnly] public NativeArray<int> input0;
  40. [ReadOnly] public NativeArray<int> input1;
  41. public NativeArray<int> output;
  42. /* public void Execute()
  43. {
  44. for (var i = 0; i < output.Length; ++i)
  45. output[i] = input0[i] + input1[i];
  46. }*/
  47. public void Execute(int i)
  48. {
  49. output[i] = input0[i] + input1[i];
  50. }
  51. }
  52. public struct CopyAndDestroyNativeArrayParallelForJob : IJobParallelFor
  53. {
  54. [ReadOnlyAttribute]
  55. public NativeArray<int> input;
  56. public NativeArray<int> output;
  57. public void Execute(int i)
  58. {
  59. output[i] = input[i];
  60. }
  61. }
  62. public SumDataParallelForJob data;
  63. public int[] expectedInput0;
  64. public NativeArray<int> input0;
  65. public NativeArray<int> input1;
  66. public NativeArray<int> input2;
  67. public NativeArray<int> output;
  68. public NativeArray<int> output2;
  69. [SetUp]
  70. public void Init()
  71. {
  72. expectedInput0 = new int[10];
  73. input0 = new NativeArray<int>(10, Allocator.Persistent);
  74. input1 = new NativeArray<int>(10, Allocator.Persistent);
  75. input2 = new NativeArray<int>(10, Allocator.Persistent);
  76. output = new NativeArray<int>(10, Allocator.Persistent);
  77. output2 = new NativeArray<int>(10, Allocator.Persistent);
  78. for (var i = 0; i < output.Length; i++)
  79. {
  80. expectedInput0[i] = i;
  81. input0[i] = i;
  82. input1[i] = 10 * i;
  83. input2[i] = 100 * i;
  84. output[i] = 0;
  85. output2[i] = 0;
  86. }
  87. data.input0 = input0;
  88. data.input1 = input1;
  89. data.output = output;
  90. }
  91. public void ExpectOutputSumOfInput0And1()
  92. {
  93. for (var i = 0; i != output.Length; i++)
  94. Assert.AreEqual(input0[i] + input1[i], output[i]);
  95. }
  96. public void ExpectOutputSumOfInput0And1And2()
  97. {
  98. for (var i = 0; i != output.Length; i++)
  99. Assert.AreEqual(input0[i] + input1[i] + input2[i], output[i]);
  100. }
  101. [TearDown]
  102. public void Cleanup()
  103. {
  104. try
  105. {
  106. input0.Dispose();
  107. }
  108. catch
  109. {
  110. }
  111. try
  112. {
  113. input1.Dispose();
  114. }
  115. catch
  116. {
  117. }
  118. try
  119. {
  120. input2.Dispose();
  121. }
  122. catch
  123. {
  124. }
  125. try
  126. {
  127. output.Dispose();
  128. }
  129. catch
  130. {
  131. }
  132. try
  133. {
  134. output2.Dispose();
  135. }
  136. catch
  137. {
  138. }
  139. }
  140. }
  141. }