Bez popisu
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.

UnsafeMultiHashMapTests.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using NUnit.Framework;
  2. using Unity.Burst;
  3. using Unity.Collections;
  4. using Unity.Collections.NotBurstCompatible;
  5. using Unity.Collections.Tests;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. using Unity.Jobs;
  8. internal class UnsafeMultiHashMapTests : CollectionsTestCommonBase
  9. {
  10. [BurstCompile(CompileSynchronously = true)]
  11. public struct UnsafeMultiHashMapAddJob : IJobParallelFor
  12. {
  13. public UnsafeMultiHashMap<int, int>.ParallelWriter Writer;
  14. public void Execute(int index)
  15. {
  16. Writer.Add(123, index);
  17. }
  18. }
  19. [Test]
  20. public void UnsafeMultiHashMap_AddJob()
  21. {
  22. var container = new UnsafeMultiHashMap<int, int>(32, CommonRwdAllocator.Handle);
  23. var job = new UnsafeMultiHashMapAddJob()
  24. {
  25. Writer = container.AsParallelWriter(),
  26. };
  27. job.Schedule(3, 1).Complete();
  28. Assert.True(container.ContainsKey(123));
  29. Assert.AreEqual(container.CountValuesForKey(123), 3);
  30. container.Dispose();
  31. }
  32. [Test]
  33. public void UnsafeHashMap_RemoveOnEmptyMap_DoesNotThrow()
  34. {
  35. var container = new UnsafeHashMap<int, int>(0, Allocator.Temp);
  36. Assert.DoesNotThrow(() => container.Remove(0));
  37. Assert.DoesNotThrow(() => container.Remove(-425196));
  38. container.Dispose();
  39. }
  40. [Test]
  41. public void UnsafeMultiHashMap_RemoveOnEmptyMap_DoesNotThrow()
  42. {
  43. var container = new UnsafeMultiHashMap<int, int>(0, Allocator.Temp);
  44. Assert.DoesNotThrow(() => container.Remove(0));
  45. Assert.DoesNotThrow(() => container.Remove(-425196));
  46. Assert.DoesNotThrow(() => container.Remove(0, 0));
  47. Assert.DoesNotThrow(() => container.Remove(-425196, 0));
  48. container.Dispose();
  49. }
  50. [Test]
  51. public void UnsafeMultiHashMap_ForEach_FixedStringInHashMap()
  52. {
  53. using (var stringList = new NativeList<FixedString32Bytes>(10, Allocator.Persistent) { "Hello", ",", "World", "!" })
  54. {
  55. var container = new UnsafeMultiHashMap<FixedString128Bytes, float>(50, Allocator.Temp);
  56. var seen = new NativeArray<int>(stringList.Length, Allocator.Temp);
  57. foreach (var str in stringList)
  58. {
  59. container.Add(str, 0);
  60. }
  61. foreach (var pair in container)
  62. {
  63. int index = stringList.IndexOf(pair.Key);
  64. Assert.AreEqual(stringList[index], pair.Key.ToString());
  65. seen[index] = seen[index] + 1;
  66. }
  67. for (int i = 0; i < stringList.Length; i++)
  68. {
  69. Assert.AreEqual(1, seen[i], $"Incorrect value count {stringList[i]}");
  70. }
  71. }
  72. }
  73. [Test]
  74. public void UnsafeMultiHashMap_ForEach([Values(10, 1000)]int n)
  75. {
  76. var seenKeys = new NativeArray<int>(n, Allocator.Temp);
  77. var seenValues = new NativeArray<int>(n * 2, Allocator.Temp);
  78. using (var container = new UnsafeMultiHashMap<int, int>(1, Allocator.Temp))
  79. {
  80. for (int i = 0; i < n; ++i)
  81. {
  82. container.Add(i, i);
  83. container.Add(i, i + n);
  84. }
  85. var count = 0;
  86. foreach (var kv in container)
  87. {
  88. if (kv.Value < n)
  89. {
  90. Assert.AreEqual(kv.Key, kv.Value);
  91. }
  92. else
  93. {
  94. Assert.AreEqual(kv.Key + n, kv.Value);
  95. }
  96. seenKeys[kv.Key] = seenKeys[kv.Key] + 1;
  97. seenValues[kv.Value] = seenValues[kv.Value] + 1;
  98. ++count;
  99. }
  100. Assert.AreEqual(container.Count(), count);
  101. for (int i = 0; i < n; i++)
  102. {
  103. Assert.AreEqual(2, seenKeys[i], $"Incorrect key count {i}");
  104. Assert.AreEqual(1, seenValues[i], $"Incorrect value count {i}");
  105. Assert.AreEqual(1, seenValues[i + n], $"Incorrect value count {i + n}");
  106. }
  107. }
  108. }
  109. [Test]
  110. public void UnsafeMultiHashMap_GetKeys()
  111. {
  112. var container = new UnsafeMultiHashMap<int, int>(1, Allocator.Temp);
  113. for (int i = 0; i < 30; ++i)
  114. {
  115. container.Add(i, 2 * i);
  116. container.Add(i, 3 * i);
  117. }
  118. var keys = container.GetKeyArray(Allocator.Temp);
  119. #if !NET_DOTS // Tuple is not supported by TinyBCL
  120. var (unique, uniqueLength) = container.GetUniqueKeyArray(Allocator.Temp);
  121. Assert.AreEqual(30, uniqueLength);
  122. #endif
  123. Assert.AreEqual(60, keys.Length);
  124. keys.Sort();
  125. for (int i = 0; i < 30; ++i)
  126. {
  127. Assert.AreEqual(i, keys[i * 2 + 0]);
  128. Assert.AreEqual(i, keys[i * 2 + 1]);
  129. #if !NET_DOTS // Tuple is not supported by TinyBCL
  130. Assert.AreEqual(i, unique[i]);
  131. #endif
  132. }
  133. }
  134. [Test]
  135. public void UnsafeMultiHashMap_CustomAllocatorTest()
  136. {
  137. AllocatorManager.Initialize();
  138. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  139. ref var allocator = ref allocatorHelper.Allocator;
  140. allocator.Initialize();
  141. using (var container = new UnsafeMultiHashMap<int, int>(1, allocator.Handle))
  142. {
  143. }
  144. Assert.IsTrue(allocator.WasUsed);
  145. allocator.Dispose();
  146. allocatorHelper.Dispose();
  147. AllocatorManager.Shutdown();
  148. }
  149. [BurstCompile]
  150. struct BurstedCustomAllocatorJob : IJob
  151. {
  152. [NativeDisableUnsafePtrRestriction]
  153. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  154. public void Execute()
  155. {
  156. unsafe
  157. {
  158. using (var container = new UnsafeMultiHashMap<int, int>(1, Allocator->Handle))
  159. {
  160. }
  161. }
  162. }
  163. }
  164. [Test]
  165. public unsafe void UnsafeMultiHashMap_BurstedCustomAllocatorTest()
  166. {
  167. AllocatorManager.Initialize();
  168. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  169. ref var allocator = ref allocatorHelper.Allocator;
  170. allocator.Initialize();
  171. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  172. unsafe
  173. {
  174. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
  175. handle.Complete();
  176. }
  177. Assert.IsTrue(allocator.WasUsed);
  178. allocator.Dispose();
  179. allocatorHelper.Dispose();
  180. AllocatorManager.Shutdown();
  181. }
  182. }