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

NativeParallelHashMapTests_InJobs.cs 7.5KB

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