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

NativeHashMapTests_InJobs.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using NUnit.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using Unity.Jobs;
  5. using Unity.Burst;
  6. using Unity.Collections;
  7. using Assert = FastAssert;
  8. internal class NativeHashMapTests_InJobs : NativeHashMapTestsFixture
  9. {
  10. [Test]
  11. public void NativeHashMap_Read_And_Write()
  12. {
  13. var hashMap = new NativeHashMap<int, int>(hashMapSize, CommonRwdAllocator.Handle);
  14. var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  15. var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  16. var writeData = new HashMapWriteJob()
  17. {
  18. hashMap = hashMap.AsParallelWriter(),
  19. status = writeStatus,
  20. keyMod = hashMapSize,
  21. };
  22. var readData = new HashMapReadParallelForJob()
  23. {
  24. hashMap = hashMap,
  25. values = readValues,
  26. keyMod = writeData.keyMod,
  27. };
  28. var writeJob = writeData.Schedule();
  29. var readJob = readData.Schedule(hashMapSize, 1, writeJob);
  30. readJob.Complete();
  31. for (int i = 0; i < hashMapSize; ++i)
  32. {
  33. Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
  34. Assert.AreEqual(i, readValues[i], "Job failed to read from hash map");
  35. }
  36. hashMap.Dispose();
  37. writeStatus.Dispose();
  38. readValues.Dispose();
  39. }
  40. [Test]
  41. [IgnoreInPortableTests("The hash map exception is fatal.")]
  42. public void NativeHashMap_Read_And_Write_Full()
  43. {
  44. var hashMap = new NativeHashMap<int, int>(hashMapSize / 2, CommonRwdAllocator.Handle);
  45. var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  46. var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  47. var writeData = new HashMapWriteJob()
  48. {
  49. hashMap = hashMap.AsParallelWriter(),
  50. status = writeStatus,
  51. keyMod = hashMapSize,
  52. };
  53. var readData = new HashMapReadParallelForJob()
  54. {
  55. hashMap = hashMap,
  56. values = readValues,
  57. keyMod = writeData.keyMod,
  58. };
  59. var writeJob = writeData.Schedule();
  60. var readJob = readData.Schedule(hashMapSize, 1, writeJob);
  61. readJob.Complete();
  62. var missing = new Dictionary<int, bool>();
  63. for (int i = 0; i < hashMapSize; ++i)
  64. {
  65. if (writeStatus[i] == -2)
  66. {
  67. missing[i] = true;
  68. Assert.AreEqual(-1, readValues[i], "Job read a value form hash map which should not be there");
  69. }
  70. else
  71. {
  72. Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
  73. Assert.AreEqual(i, readValues[i], "Job failed to read from hash map");
  74. }
  75. }
  76. Assert.AreEqual(hashMapSize - hashMapSize / 2, missing.Count, "Wrong indices written to hash map");
  77. hashMap.Dispose();
  78. writeStatus.Dispose();
  79. readValues.Dispose();
  80. }
  81. [Test]
  82. public void NativeHashMap_Key_Collisions()
  83. {
  84. var hashMap = new NativeHashMap<int, int>(hashMapSize, CommonRwdAllocator.Handle);
  85. var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  86. var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  87. var writeData = new HashMapWriteJob()
  88. {
  89. hashMap = hashMap.AsParallelWriter(),
  90. status = writeStatus,
  91. keyMod = 16,
  92. };
  93. var readData = new HashMapReadParallelForJob()
  94. {
  95. hashMap = hashMap,
  96. values = readValues,
  97. keyMod = writeData.keyMod,
  98. };
  99. var writeJob = writeData.Schedule();
  100. var readJob = readData.Schedule(hashMapSize, 1, writeJob);
  101. readJob.Complete();
  102. var missing = new Dictionary<int, bool>();
  103. for (int i = 0; i < hashMapSize; ++i)
  104. {
  105. if (writeStatus[i] == -1)
  106. {
  107. missing[i] = true;
  108. Assert.AreNotEqual(i, readValues[i], "Job read a value form hash map which should not be there");
  109. }
  110. else
  111. {
  112. Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
  113. Assert.AreEqual(i, readValues[i], "Job failed to read from hash map");
  114. }
  115. }
  116. Assert.AreEqual(hashMapSize - writeData.keyMod, missing.Count, "Wrong indices written to hash map");
  117. hashMap.Dispose();
  118. writeStatus.Dispose();
  119. readValues.Dispose();
  120. }
  121. [BurstCompile(CompileSynchronously = true)]
  122. struct Clear : IJob
  123. {
  124. public NativeHashMap<int, int> hashMap;
  125. public void Execute()
  126. {
  127. hashMap.Clear();
  128. }
  129. }
  130. [Test]
  131. [IgnoreInPortableTests("Hash map throws when full.")]
  132. public void NativeHashMap_Clear_And_Write()
  133. {
  134. var hashMap = new NativeHashMap<int, int>(hashMapSize / 2, CommonRwdAllocator.Handle);
  135. var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  136. var clearJob = new Clear
  137. {
  138. hashMap = hashMap
  139. };
  140. var clearJobHandle = clearJob.Schedule();
  141. var writeJob = new HashMapWriteJob
  142. {
  143. hashMap = hashMap.AsParallelWriter(),
  144. status = writeStatus,
  145. keyMod = hashMapSize,
  146. };
  147. var writeJobHandle = writeJob.Schedule(clearJobHandle);
  148. writeJobHandle.Complete();
  149. writeStatus.Dispose();
  150. hashMap.Dispose();
  151. }
  152. [Test]
  153. public void NativeHashMap_DisposeJob()
  154. {
  155. var container0 = new NativeHashMap<int, int>(1, Allocator.Persistent);
  156. Assert.True(container0.IsCreated);
  157. Assert.DoesNotThrow(() => { container0.Add(0, 1); });
  158. Assert.True(container0.ContainsKey(0));
  159. var container1 = new NativeMultiHashMap<int, int>(1, Allocator.Persistent);
  160. Assert.True(container1.IsCreated);
  161. Assert.DoesNotThrow(() => { container1.Add(1, 2); });
  162. Assert.True(container1.ContainsKey(1));
  163. var disposeJob0 = container0.Dispose(default);
  164. Assert.False(container0.IsCreated);
  165. Assert.Throws<ObjectDisposedException>(
  166. () => { container0.ContainsKey(0); });
  167. var disposeJob = container1.Dispose(disposeJob0);
  168. Assert.False(container1.IsCreated);
  169. Assert.Throws<ObjectDisposedException>(
  170. () => { container1.ContainsKey(1); });
  171. disposeJob.Complete();
  172. }
  173. [Test]
  174. public void NativeHashMap_DisposeJobWithMissingDependencyThrows()
  175. {
  176. var hashMap = new NativeHashMap<int, int>(hashMapSize / 2, CommonRwdAllocator.Handle);
  177. var deps = new Clear { hashMap = hashMap }.Schedule();
  178. Assert.Throws<InvalidOperationException>(() => { hashMap.Dispose(default); });
  179. deps.Complete();
  180. hashMap.Dispose();
  181. }
  182. [Test]
  183. public void NativeHashMap_DisposeJobCantBeScheduled()
  184. {
  185. var hashMap = new NativeHashMap<int, int>(hashMapSize / 2, CommonRwdAllocator.Handle);
  186. var deps = hashMap.Dispose(default);
  187. Assert.Throws<InvalidOperationException>(() => { new Clear { hashMap = hashMap }.Schedule(deps); });
  188. deps.Complete();
  189. }
  190. }