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