A game about forced loneliness, made by TACStudios
1using NUnit.Framework;
2using System.Collections.Generic;
3using Unity.Jobs;
4using Unity.Burst;
5using Unity.Collections;
6using Assert = FastAssert;
7
8namespace Unity.Collections.Tests
9{
10 internal class NativeParallelMultiHashMapTests_InJobs : NativeParallelMultiHashMapTestsFixture
11 {
12 [Test]
13 public void NativeParallelMultiHashMap_Read_And_Write()
14 {
15 var hashMap = new NativeParallelMultiHashMap<int, int>(hashMapSize, CommonRwdAllocator.Handle);
16 var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
17 var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
18
19 var writeData = new MultiHashMapWriteParallelForJob()
20 {
21 hashMap = hashMap.AsParallelWriter(),
22 status = writeStatus,
23 keyMod = hashMapSize,
24 };
25
26 var readData = new MultiHashMapReadParallelForJob()
27 {
28 hashMap = hashMap,
29 values = readValues,
30 keyMod = writeData.keyMod,
31 };
32
33 var writeJob = writeData.Schedule(hashMapSize, 1);
34 var readJob = readData.Schedule(hashMapSize, 1, writeJob);
35 readJob.Complete();
36
37 for (int i = 0; i < hashMapSize; ++i)
38 {
39 Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
40 Assert.AreEqual(1, readValues[i], "Job failed to read from hash map");
41 }
42
43 hashMap.Dispose();
44 writeStatus.Dispose();
45 readValues.Dispose();
46 }
47
48 [Test]
49 [TestRequiresCollectionChecks]
50 public void NativeParallelMultiHashMap_Read_And_Write_Full()
51 {
52 var hashMap = new NativeParallelMultiHashMap<int, int>(hashMapSize / 2, CommonRwdAllocator.Handle);
53 var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
54 var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
55
56 var writeData = new MultiHashMapWriteParallelForJob()
57 {
58 hashMap = hashMap.AsParallelWriter(),
59 status = writeStatus,
60 keyMod = hashMapSize,
61 };
62
63 var readData = new MultiHashMapReadParallelForJob()
64 {
65 hashMap = hashMap,
66 values = readValues,
67 keyMod = writeData.keyMod,
68 };
69
70 var writeJob = writeData.Schedule(hashMapSize, 1);
71 var readJob = readData.Schedule(hashMapSize, 1, writeJob);
72 readJob.Complete();
73
74 var missing = new Dictionary<int, bool>();
75 for (int i = 0; i < hashMapSize; ++i)
76 {
77 if (writeStatus[i] == -2)
78 {
79 missing[i] = true;
80 Assert.AreEqual(-1, readValues[i], "Job read a value form hash map which should not be there");
81 }
82 else
83 {
84 Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
85 Assert.AreEqual(1, readValues[i], "Job failed to read from hash map");
86 }
87 }
88 Assert.AreEqual(hashMapSize - hashMapSize / 2, missing.Count, "Wrong indices written to hash map");
89
90 hashMap.Dispose();
91 writeStatus.Dispose();
92 readValues.Dispose();
93 }
94
95 [Test]
96 public void NativeParallelMultiHashMap_Key_Collisions()
97 {
98 var hashMap = new NativeParallelMultiHashMap<int, int>(hashMapSize, CommonRwdAllocator.Handle);
99 var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
100 var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
101
102 var writeData = new MultiHashMapWriteParallelForJob()
103 {
104 hashMap = hashMap.AsParallelWriter(),
105 status = writeStatus,
106 keyMod = 16,
107 };
108
109 var readData = new MultiHashMapReadParallelForJob()
110 {
111 hashMap = hashMap,
112 values = readValues,
113 keyMod = writeData.keyMod,
114 };
115
116 var writeJob = writeData.Schedule(hashMapSize, 1);
117 var readJob = readData.Schedule(hashMapSize, 1, writeJob);
118 readJob.Complete();
119
120 for (int i = 0; i < hashMapSize; ++i)
121 {
122 Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
123 Assert.AreEqual(hashMapSize / readData.keyMod, readValues[i], "Job failed to read from hash map");
124 }
125
126 hashMap.Dispose();
127 writeStatus.Dispose();
128 readValues.Dispose();
129 }
130
131 [BurstCompile(CompileSynchronously = true)]
132 struct AddMultiIndex : IJobParallelFor
133 {
134 public NativeParallelMultiHashMap<int, int>.ParallelWriter hashMap;
135
136 public void Execute(int index)
137 {
138 hashMap.Add(index, index);
139 }
140 }
141
142 [Test]
143 public void NativeParallelMultiHashMap_TryMultiAddScalabilityConcurrent()
144 {
145 for (int count = 0; count < 1024; count++)
146 {
147 var hashMap = new NativeParallelMultiHashMap<int, int>(count, CommonRwdAllocator.Handle);
148 var addIndexJob = new AddMultiIndex
149 {
150 hashMap = hashMap.AsParallelWriter()
151 };
152
153 var addIndexJobHandle = addIndexJob.Schedule(count, 64);
154 addIndexJobHandle.Complete();
155 hashMap.Dispose();
156 }
157 }
158 }
159}