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.

UnsafeHashMapTests.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 UnsafeHashMapTests : CollectionsTestCommonBase
  10. {
  11. // Burst error BC1071: Unsupported assert type
  12. // [BurstCompile(CompileSynchronously = true)]
  13. public struct UnsafeHashMapAddJob : IJob
  14. {
  15. public UnsafeHashMap<int, int>.ParallelWriter Writer;
  16. public void Execute()
  17. {
  18. Assert.True(Writer.TryAdd(123, 1));
  19. }
  20. }
  21. [Test]
  22. public void UnsafeHashMap_AddJob()
  23. {
  24. var container = new UnsafeHashMap<int, int>(32, CommonRwdAllocator.Handle);
  25. var job = new UnsafeHashMapAddJob()
  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 UnsafeHashMap_ForEach([Values(10, 1000)]int n)
  35. {
  36. var seen = new NativeArray<int>(n, Allocator.Temp);
  37. using (var container = new UnsafeHashMap<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. #if !NET_DOTS // Array.Sort is not supported
  61. [Test]
  62. public void UnsafeHashSet_ToArray()
  63. {
  64. using (var set = new UnsafeHashSet<int>(8, CommonRwdAllocator.Handle) { 0, 1, 2, 3, 4, 5 })
  65. {
  66. var array = set.ToArray();
  67. Array.Sort(array);
  68. for (int i = 0, num = set.Count(); i < num; i++)
  69. {
  70. Assert.AreEqual(array[i], i);
  71. }
  72. }
  73. }
  74. #endif
  75. [Test]
  76. public void UnsafeHashMap_CustomAllocatorTest()
  77. {
  78. AllocatorManager.Initialize();
  79. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  80. ref var allocator = ref allocatorHelper.Allocator;
  81. allocator.Initialize();
  82. using (var container = new UnsafeHashMap<int, int>(1, allocator.Handle))
  83. {
  84. }
  85. Assert.IsTrue(allocator.WasUsed);
  86. allocator.Dispose();
  87. allocatorHelper.Dispose();
  88. AllocatorManager.Shutdown();
  89. }
  90. [BurstCompile]
  91. struct BurstedCustomAllocatorJob : IJob
  92. {
  93. [NativeDisableUnsafePtrRestriction]
  94. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  95. public void Execute()
  96. {
  97. unsafe
  98. {
  99. using (var container = new UnsafeHashMap<int, int>(1, Allocator->Handle))
  100. {
  101. }
  102. }
  103. }
  104. }
  105. [Test]
  106. public unsafe void UnsafeHashMap_BurstedCustomAllocatorTest()
  107. {
  108. AllocatorManager.Initialize();
  109. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  110. ref var allocator = ref allocatorHelper.Allocator;
  111. allocator.Initialize();
  112. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  113. unsafe
  114. {
  115. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
  116. handle.Complete();
  117. }
  118. Assert.IsTrue(allocator.WasUsed);
  119. allocator.Dispose();
  120. allocatorHelper.Dispose();
  121. AllocatorManager.Shutdown();
  122. }
  123. }