Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

UnsafeParallelHashMapTests.cs 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using NUnit.Framework;
  2. using Unity.Collections;
  3. using Unity.Jobs;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using Unity.Collections.LowLevel.Unsafe.NotBurstCompatible;
  6. using Unity.Collections.Tests;
  7. using System;
  8. using Unity.Burst;
  9. internal class UnsafeParallelHashMapTests : CollectionsTestCommonBase
  10. {
  11. // Burst error BC1071: Unsupported assert type
  12. // [BurstCompile(CompileSynchronously = true)]
  13. public struct UnsafeParallelHashMapAddJob : IJob
  14. {
  15. public UnsafeParallelHashMap<int, int>.ParallelWriter Writer;
  16. public void Execute()
  17. {
  18. Assert.True(Writer.TryAdd(123, 1));
  19. }
  20. }
  21. [Test]
  22. public void UnsafeParallelHashMap_AddJob()
  23. {
  24. var container = new UnsafeParallelHashMap<int, int>(32, CommonRwdAllocator.Handle);
  25. var job = new UnsafeParallelHashMapAddJob()
  26. {
  27. Writer = container.AsParallelWriter(),
  28. };
  29. job.Schedule().Complete();
  30. Assert.True(container.ContainsKey(123));
  31. container.Dispose();
  32. }
  33. [Test]
  34. public void UnsafeParallelHashMap_ForEach([Values(10, 1000)]int n)
  35. {
  36. var seen = new NativeArray<int>(n, Allocator.Temp);
  37. using (var container = new UnsafeParallelHashMap<int, int>(32, CommonRwdAllocator.Handle))
  38. {
  39. for (int i = 0; i < n; i++)
  40. {
  41. container.Add(i, i * 37);
  42. }
  43. var count = 0;
  44. foreach (var kv in container)
  45. {
  46. int value;
  47. Assert.True(container.TryGetValue(kv.Key, out value));
  48. Assert.AreEqual(value, kv.Value);
  49. Assert.AreEqual(kv.Key * 37, kv.Value);
  50. seen[kv.Key] = seen[kv.Key] + 1;
  51. ++count;
  52. }
  53. Assert.AreEqual(container.Count(), count);
  54. for (int i = 0; i < n; i++)
  55. {
  56. Assert.AreEqual(1, seen[i], $"Incorrect key count {i}");
  57. }
  58. }
  59. }
  60. [Test]
  61. public void UnsafeParallelHashSet_ToArray()
  62. {
  63. using (var set = new UnsafeParallelHashSet<int>(8, CommonRwdAllocator.Handle) { 0, 1, 2, 3, 4, 5 })
  64. {
  65. var array = set.ToArray();
  66. Array.Sort(array);
  67. for (int i = 0, num = set.Count(); i < num; i++)
  68. {
  69. Assert.AreEqual(array[i], i);
  70. }
  71. }
  72. }
  73. [Test]
  74. public void UnsafeParallelHashMap_CustomAllocatorTest()
  75. {
  76. AllocatorManager.Initialize();
  77. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  78. ref var allocator = ref allocatorHelper.Allocator;
  79. allocator.Initialize();
  80. using (var container = new UnsafeParallelHashMap<int, int>(1, allocator.Handle))
  81. {
  82. }
  83. Assert.IsTrue(allocator.WasUsed);
  84. allocator.Dispose();
  85. allocatorHelper.Dispose();
  86. AllocatorManager.Shutdown();
  87. }
  88. [BurstCompile]
  89. struct BurstedCustomAllocatorJob : IJob
  90. {
  91. [NativeDisableUnsafePtrRestriction]
  92. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  93. public void Execute()
  94. {
  95. unsafe
  96. {
  97. using (var container = new UnsafeParallelHashMap<int, int>(1, Allocator->Handle))
  98. {
  99. }
  100. }
  101. }
  102. }
  103. [Test]
  104. public unsafe void UnsafeParallelHashMap_BurstedCustomAllocatorTest()
  105. {
  106. AllocatorManager.Initialize();
  107. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  108. ref var allocator = ref allocatorHelper.Allocator;
  109. allocator.Initialize();
  110. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  111. unsafe
  112. {
  113. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
  114. handle.Complete();
  115. }
  116. Assert.IsTrue(allocator.WasUsed);
  117. allocator.Dispose();
  118. allocatorHelper.Dispose();
  119. AllocatorManager.Shutdown();
  120. }
  121. [Test]
  122. public void UnsafeParallelHashMap_IndexerAdd_ResizesContainer()
  123. {
  124. var container = new UnsafeParallelHashMap<int, int>(8, Allocator.Persistent);
  125. for (int i = 0; i < 1024; i++)
  126. {
  127. container[i] = i;
  128. }
  129. Assert.AreEqual(1024, container.Count());
  130. container.Dispose();
  131. }
  132. struct UnsafeParallelHashMap_ForEach_Job : IJob
  133. {
  134. public UnsafeParallelHashMap<int, int>.ReadOnly Input;
  135. [ReadOnly]
  136. public int Num;
  137. public void Execute()
  138. {
  139. var seen = new NativeArray<int>(Num, Allocator.Temp);
  140. var count = 0;
  141. foreach (var kv in Input)
  142. {
  143. int value;
  144. Assert.True(Input.TryGetValue(kv.Key, out value));
  145. Assert.AreEqual(value, kv.Value);
  146. Assert.AreEqual(kv.Key * 37, kv.Value);
  147. seen[kv.Key] = seen[kv.Key] + 1;
  148. ++count;
  149. }
  150. Assert.AreEqual(Input.Count(), count);
  151. for (int i = 0; i < Num; i++)
  152. {
  153. Assert.AreEqual(1, seen[i], $"Incorrect key count {i}");
  154. }
  155. seen.Dispose();
  156. }
  157. }
  158. [Test]
  159. public void UnsafeParallelHashMap_ForEach_From_Job([Values(10, 1000)] int n)
  160. {
  161. var seen = new NativeArray<int>(n, Allocator.Temp);
  162. using (var container = new UnsafeParallelHashMap<int, int>(32, CommonRwdAllocator.Handle))
  163. {
  164. for (int i = 0; i < n; i++)
  165. {
  166. container.Add(i, i * 37);
  167. }
  168. new UnsafeParallelHashMap_ForEach_Job
  169. {
  170. Input = container.AsReadOnly(),
  171. Num = n,
  172. }.Run();
  173. }
  174. }
  175. }