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.

NativeParallelMultiHashMapTests.cs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. using System;
  2. using NUnit.Framework;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.Collections.NotBurstCompatible;
  7. using Unity.Collections.Tests;
  8. using Unity.Jobs;
  9. using UnityEngine;
  10. using UnityEngine.TestTools;
  11. using System.Text.RegularExpressions;
  12. internal class NativeParallelMultiHashMapTests : CollectionsTestFixture
  13. {
  14. [Test]
  15. [TestRequiresCollectionChecks]
  16. public void NativeParallelMultiHashMap_UseAfterFree_UsesCustomOwnerTypeName()
  17. {
  18. var container = new NativeParallelMultiHashMap<int, int>(10, CommonRwdAllocator.Handle);
  19. container.Add(0, 123);
  20. container.Dispose();
  21. Assert.That(() => container.ContainsKey(0),
  22. Throws.Exception.TypeOf<ObjectDisposedException>()
  23. .With.Message.Contains($"The {container.GetType()} has been deallocated"));
  24. }
  25. [BurstCompile(CompileSynchronously = true)]
  26. struct NativeParallelMultiHashMap_CreateAndUseAfterFreeBurst : IJob
  27. {
  28. public void Execute()
  29. {
  30. var container = new NativeParallelMultiHashMap<int, int>(10, Allocator.Temp);
  31. container.Add(0, 17);
  32. container.Dispose();
  33. container.Add(1, 42);
  34. }
  35. }
  36. [Test]
  37. [TestRequiresCollectionChecks]
  38. public void NativeParallelMultiHashMap_CreateAndUseAfterFreeInBurstJob_UsesCustomOwnerTypeName()
  39. {
  40. // Make sure this isn't the first container of this type ever created, so that valid static safety data exists
  41. var container = new NativeParallelMultiHashMap<int, int>(10, CommonRwdAllocator.Handle);
  42. container.Dispose();
  43. var job = new NativeParallelMultiHashMap_CreateAndUseAfterFreeBurst
  44. {
  45. };
  46. // Two things:
  47. // 1. This exception is logged, not thrown; thus, we use LogAssert to detect it.
  48. // 2. Calling write operation after container.Dispose() emits an unintuitive error message. For now, all this test cares about is whether it contains the
  49. // expected type name.
  50. job.Run();
  51. LogAssert.Expect(LogType.Exception,
  52. new Regex($"InvalidOperationException: The {Regex.Escape(container.GetType().ToString())} has been declared as \\[ReadOnly\\] in the job, but you are writing to it"));
  53. }
  54. [Test]
  55. public void NativeParallelMultiHashMap_IsEmpty()
  56. {
  57. var container = new NativeParallelMultiHashMap<int, int>(0, Allocator.Persistent);
  58. Assert.IsTrue(container.IsEmpty);
  59. container.Add(0, 0);
  60. Assert.IsFalse(container.IsEmpty);
  61. Assert.AreEqual(1, container.Capacity);
  62. ExpectedCount(ref container, 1);
  63. container.Remove(0, 0);
  64. Assert.IsTrue(container.IsEmpty);
  65. container.Add(0, 0);
  66. container.Clear();
  67. Assert.IsTrue(container.IsEmpty);
  68. container.Dispose();
  69. }
  70. [Test]
  71. public void NativeParallelMultiHashMap_CountValuesForKey()
  72. {
  73. var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
  74. hashMap.Add(5, 7);
  75. hashMap.Add(6, 9);
  76. hashMap.Add(6, 10);
  77. Assert.AreEqual(1, hashMap.CountValuesForKey(5));
  78. Assert.AreEqual(2, hashMap.CountValuesForKey(6));
  79. Assert.AreEqual(0, hashMap.CountValuesForKey(7));
  80. hashMap.Dispose();
  81. }
  82. [Test]
  83. public void NativeParallelMultiHashMap_RemoveKeyAndValue()
  84. {
  85. var hashMap = new NativeParallelMultiHashMap<int, long>(1, Allocator.Temp);
  86. hashMap.Add(10, 0);
  87. hashMap.Add(10, 1);
  88. hashMap.Add(10, 2);
  89. hashMap.Add(20, 2);
  90. hashMap.Add(20, 2);
  91. hashMap.Add(20, 1);
  92. hashMap.Add(20, 2);
  93. hashMap.Add(20, 1);
  94. hashMap.Remove(10, 1L);
  95. ExpectValues(hashMap, 10, new[] { 0L, 2L });
  96. ExpectValues(hashMap, 20, new[] { 1L, 1L, 2L, 2L, 2L });
  97. hashMap.Remove(20, 2L);
  98. ExpectValues(hashMap, 10, new[] { 0L, 2L });
  99. ExpectValues(hashMap, 20, new[] { 1L, 1L });
  100. hashMap.Remove(20, 1L);
  101. ExpectValues(hashMap, 10, new[] { 0L, 2L });
  102. ExpectValues(hashMap, 20, new long[0]);
  103. hashMap.Dispose();
  104. }
  105. [Test]
  106. public void NativeParallelMultiHashMap_ValueIterator()
  107. {
  108. var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
  109. hashMap.Add(5, 0);
  110. hashMap.Add(5, 1);
  111. hashMap.Add(5, 2);
  112. var list = new NativeList<int>(CommonRwdAllocator.Handle);
  113. GCAllocRecorder.ValidateNoGCAllocs(() =>
  114. {
  115. list.Clear();
  116. foreach (var value in hashMap.GetValuesForKey(5))
  117. list.Add(value);
  118. });
  119. list.Sort();
  120. Assert.AreEqual(list.ToArrayNBC(), new int[] { 0, 1, 2 });
  121. foreach (var value in hashMap.GetValuesForKey(6))
  122. Assert.Fail();
  123. list.Dispose();
  124. hashMap.Dispose();
  125. }
  126. [Test]
  127. public void NativeParallelMultiHashMap_RemoveKeyValueDoesntDeallocate()
  128. {
  129. var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp) { { 5, 1 } };
  130. hashMap.Remove(5, 5);
  131. GCAllocRecorder.ValidateNoGCAllocs(() =>
  132. {
  133. hashMap.Remove(5, 1);
  134. });
  135. Assert.IsTrue(hashMap.IsEmpty);
  136. hashMap.Dispose();
  137. }
  138. static void ExpectedCount<TKey, TValue>(ref NativeParallelMultiHashMap<TKey, TValue> container, int expected)
  139. where TKey : unmanaged, IEquatable<TKey>
  140. where TValue : unmanaged
  141. {
  142. Assert.AreEqual(expected == 0, container.IsEmpty);
  143. Assert.AreEqual(expected, container.Count());
  144. }
  145. [Test]
  146. public void NativeParallelMultiHashMap_RemoveOnEmptyMap_DoesNotThrow()
  147. {
  148. var hashMap = new NativeParallelMultiHashMap<int, int>(0, Allocator.Temp);
  149. Assert.DoesNotThrow(() => hashMap.Remove(0));
  150. Assert.DoesNotThrow(() => hashMap.Remove(-425196));
  151. Assert.DoesNotThrow(() => hashMap.Remove(0, 0));
  152. Assert.DoesNotThrow(() => hashMap.Remove(-425196, 0));
  153. hashMap.Dispose();
  154. }
  155. [Test]
  156. public void NativeParallelMultiHashMap_RemoveFromMultiHashMap()
  157. {
  158. var hashMap = new NativeParallelMultiHashMap<int, int>(16, Allocator.Temp);
  159. int iSquared;
  160. // Make sure inserting values work
  161. for (int i = 0; i < 8; ++i)
  162. hashMap.Add(i, i * i);
  163. for (int i = 0; i < 8; ++i)
  164. hashMap.Add(i, i);
  165. Assert.AreEqual(16, hashMap.Capacity, "HashMap grew larger than expected");
  166. // Make sure reading the inserted values work
  167. for (int i = 0; i < 8; ++i)
  168. {
  169. NativeParallelMultiHashMapIterator<int> it;
  170. Assert.IsTrue(hashMap.TryGetFirstValue(i, out iSquared, out it), "Failed get value from hash table");
  171. Assert.AreEqual(iSquared, i, "Got the wrong value from the hash table");
  172. Assert.IsTrue(hashMap.TryGetNextValue(out iSquared, ref it), "Failed get value from hash table");
  173. Assert.AreEqual(iSquared, i * i, "Got the wrong value from the hash table");
  174. }
  175. for (int rm = 0; rm < 8; ++rm)
  176. {
  177. Assert.AreEqual(2, hashMap.Remove(rm));
  178. NativeParallelMultiHashMapIterator<int> it;
  179. Assert.IsFalse(hashMap.TryGetFirstValue(rm, out iSquared, out it), "Failed to remove value from hash table");
  180. for (int i = rm + 1; i < 8; ++i)
  181. {
  182. Assert.IsTrue(hashMap.TryGetFirstValue(i, out iSquared, out it), "Failed get value from hash table");
  183. Assert.AreEqual(iSquared, i, "Got the wrong value from the hash table");
  184. Assert.IsTrue(hashMap.TryGetNextValue(out iSquared, ref it), "Failed get value from hash table");
  185. Assert.AreEqual(iSquared, i * i, "Got the wrong value from the hash table");
  186. }
  187. }
  188. // Make sure entries were freed
  189. for (int i = 0; i < 8; ++i)
  190. hashMap.Add(i, i * i);
  191. for (int i = 0; i < 8; ++i)
  192. hashMap.Add(i, i);
  193. Assert.AreEqual(16, hashMap.Capacity, "HashMap grew larger than expected");
  194. hashMap.Dispose();
  195. }
  196. void ExpectValues(NativeParallelMultiHashMap<int, long> hashMap, int key, long[] expectedValues)
  197. {
  198. var list = new NativeList<long>(CommonRwdAllocator.Handle);
  199. foreach (var value in hashMap.GetValuesForKey(key))
  200. list.Add(value);
  201. list.Sort();
  202. Assert.AreEqual(list.ToArrayNBC(), expectedValues);
  203. list.Dispose();
  204. }
  205. [Test]
  206. public void NativeParallelMultiHashMap_GetKeys()
  207. {
  208. var container = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
  209. for (int i = 0; i < 30; ++i)
  210. {
  211. container.Add(i, 2 * i);
  212. container.Add(i, 3 * i);
  213. }
  214. var keys = container.GetKeyArray(Allocator.Temp);
  215. var (unique, uniqueLength) = container.GetUniqueKeyArray(Allocator.Temp);
  216. Assert.AreEqual(30, uniqueLength);
  217. Assert.AreEqual(60, keys.Length);
  218. keys.Sort();
  219. for (int i = 0; i < 30; ++i)
  220. {
  221. Assert.AreEqual(i, keys[i * 2 + 0]);
  222. Assert.AreEqual(i, keys[i * 2 + 1]);
  223. Assert.AreEqual(i, unique[i]);
  224. }
  225. }
  226. [Test]
  227. public void NativeParallelMultiHashMap_GetUniqueKeysEmpty()
  228. {
  229. var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
  230. var keys = hashMap.GetUniqueKeyArray(Allocator.Temp);
  231. Assert.AreEqual(0, keys.Item1.Length);
  232. Assert.AreEqual(0, keys.Item2);
  233. }
  234. [Test]
  235. public void NativeParallelMultiHashMap_GetUniqueKeys()
  236. {
  237. var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
  238. for (int i = 0; i < 30; ++i)
  239. {
  240. hashMap.Add(i, 2 * i);
  241. hashMap.Add(i, 3 * i);
  242. }
  243. var keys = hashMap.GetUniqueKeyArray(Allocator.Temp);
  244. hashMap.Dispose();
  245. Assert.AreEqual(30, keys.Item2);
  246. for (int i = 0; i < 30; ++i)
  247. {
  248. Assert.AreEqual(i, keys.Item1[i]);
  249. }
  250. keys.Item1.Dispose();
  251. }
  252. [Test]
  253. public void NativeParallelMultiHashMap_GetValues()
  254. {
  255. var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
  256. for (int i = 0; i < 30; ++i)
  257. {
  258. hashMap.Add(i, 30 + i);
  259. hashMap.Add(i, 60 + i);
  260. }
  261. var values = hashMap.GetValueArray(Allocator.Temp);
  262. hashMap.Dispose();
  263. Assert.AreEqual(60, values.Length);
  264. values.Sort();
  265. for (int i = 0; i < 60; ++i)
  266. {
  267. Assert.AreEqual(30 + i, values[i]);
  268. }
  269. values.Dispose();
  270. }
  271. [Test]
  272. public void NativeParallelMultiHashMap_ForEach_FixedStringInHashMap()
  273. {
  274. using (var stringList = new NativeList<FixedString32Bytes>(10, Allocator.Persistent) { "Hello", ",", "World", "!" })
  275. {
  276. var container = new NativeParallelMultiHashMap<FixedString128Bytes, float>(50, Allocator.Temp);
  277. var seen = new NativeArray<int>(stringList.Length, Allocator.Temp);
  278. foreach (var str in stringList)
  279. {
  280. container.Add(str, 0);
  281. }
  282. foreach (var pair in container)
  283. {
  284. int index = stringList.IndexOf(pair.Key);
  285. Assert.AreEqual(stringList[index], pair.Key.ToString());
  286. seen[index] = seen[index] + 1;
  287. }
  288. for (int i = 0; i < stringList.Length; i++)
  289. {
  290. Assert.AreEqual(1, seen[i], $"Incorrect value count {stringList[i]}");
  291. }
  292. }
  293. }
  294. [Test]
  295. public void NativeParallelMultiHashMap_ForEach([Values(10, 1000)]int n)
  296. {
  297. var seenKeys = new NativeArray<int>(n, Allocator.Temp);
  298. var seenValues = new NativeArray<int>(n * 2, Allocator.Temp);
  299. using (var container = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp))
  300. {
  301. for (int i = 0; i < n; ++i)
  302. {
  303. container.Add(i, i);
  304. container.Add(i, i + n);
  305. }
  306. var count = 0;
  307. foreach (var kv in container)
  308. {
  309. if (kv.Value < n)
  310. {
  311. Assert.AreEqual(kv.Key, kv.Value);
  312. }
  313. else
  314. {
  315. Assert.AreEqual(kv.Key + n, kv.Value);
  316. }
  317. seenKeys[kv.Key] = seenKeys[kv.Key] + 1;
  318. seenValues[kv.Value] = seenValues[kv.Value] + 1;
  319. ++count;
  320. }
  321. Assert.AreEqual(container.Count(), count);
  322. for (int i = 0; i < n; i++)
  323. {
  324. Assert.AreEqual(2, seenKeys[i], $"Incorrect key count {i}");
  325. Assert.AreEqual(1, seenValues[i], $"Incorrect value count {i}");
  326. Assert.AreEqual(1, seenValues[i + n], $"Incorrect value count {i + n}");
  327. }
  328. }
  329. }
  330. struct NativeParallelMultiHashMap_ForEach_Job : IJob
  331. {
  332. [ReadOnly]
  333. public NativeParallelMultiHashMap<int, int> Input;
  334. [ReadOnly]
  335. public int Num;
  336. public void Execute()
  337. {
  338. var seenKeys = new NativeArray<int>(Num, Allocator.Temp);
  339. var seenValues = new NativeArray<int>(Num * 2, Allocator.Temp);
  340. var count = 0;
  341. foreach (var kv in Input)
  342. {
  343. if (kv.Value < Num)
  344. {
  345. Assert.AreEqual(kv.Key, kv.Value);
  346. }
  347. else
  348. {
  349. Assert.AreEqual(kv.Key + Num, kv.Value);
  350. }
  351. seenKeys[kv.Key] = seenKeys[kv.Key] + 1;
  352. seenValues[kv.Value] = seenValues[kv.Value] + 1;
  353. ++count;
  354. }
  355. Assert.AreEqual(Input.Count(), count);
  356. for (int i = 0; i < Num; i++)
  357. {
  358. Assert.AreEqual(2, seenKeys[i], $"Incorrect key count {i}");
  359. Assert.AreEqual(1, seenValues[i], $"Incorrect value count {i}");
  360. Assert.AreEqual(1, seenValues[i + Num], $"Incorrect value count {i + Num}");
  361. }
  362. seenKeys.Dispose();
  363. seenValues.Dispose();
  364. }
  365. }
  366. [Test]
  367. public void NativeParallelMultiHashMap_ForEach_From_Job([Values(10, 1000)] int n)
  368. {
  369. using (var container = new NativeParallelMultiHashMap<int, int>(1, CommonRwdAllocator.Handle))
  370. {
  371. for (int i = 0; i < n; ++i)
  372. {
  373. container.Add(i, i);
  374. container.Add(i, i + n);
  375. }
  376. new NativeParallelMultiHashMap_ForEach_Job
  377. {
  378. Input = container,
  379. Num = n,
  380. }.Run();
  381. }
  382. }
  383. [Test]
  384. [TestRequiresCollectionChecks]
  385. public void NativeParallelMultiHashMap_ForEach_Throws_When_Modified()
  386. {
  387. using (var container = new NativeParallelMultiHashMap<int, int>(32, CommonRwdAllocator.Handle))
  388. {
  389. for (int i = 0; i < 30; ++i)
  390. {
  391. container.Add(i, 30 + i);
  392. container.Add(i, 60 + i);
  393. }
  394. Assert.Throws<ObjectDisposedException>(() =>
  395. {
  396. foreach (var kv in container)
  397. {
  398. container.Add(10, 10);
  399. }
  400. });
  401. Assert.Throws<ObjectDisposedException>(() =>
  402. {
  403. foreach (var kv in container)
  404. {
  405. container.Remove(1);
  406. }
  407. });
  408. }
  409. }
  410. struct NativeParallelMultiHashMap_ForEachIterator : IJob
  411. {
  412. [ReadOnly]
  413. public NativeParallelMultiHashMap<int, int>.KeyValueEnumerator Iter;
  414. public void Execute()
  415. {
  416. while (Iter.MoveNext())
  417. {
  418. }
  419. }
  420. }
  421. [Test]
  422. [TestRequiresCollectionChecks]
  423. public void NativeParallelMultiHashMap_ForEach_Throws_Job_Iterator()
  424. {
  425. using (var container = new NativeParallelMultiHashMap<int, int>(32, CommonRwdAllocator.Handle))
  426. {
  427. var jobHandle = new NativeParallelMultiHashMap_ForEachIterator
  428. {
  429. Iter = container.GetEnumerator()
  430. }.Schedule();
  431. Assert.Throws<InvalidOperationException>(() => { container.Add(1, 1); });
  432. jobHandle.Complete();
  433. }
  434. }
  435. struct ParallelWriteToMultiHashMapJob : IJobParallelFor
  436. {
  437. [WriteOnly]
  438. public NativeParallelMultiHashMap<int, int>.ParallelWriter Writer;
  439. public void Execute(int index)
  440. {
  441. Writer.Add(index, 0);
  442. }
  443. }
  444. [Test]
  445. [TestRequiresCollectionChecks]
  446. public void NativeParallelMultiHashMap_ForEach_Throws_When_Modified_From_Job()
  447. {
  448. using (var container = new NativeParallelMultiHashMap<int, int>(32, CommonRwdAllocator.Handle))
  449. {
  450. var iter = container.GetEnumerator();
  451. var jobHandle = new ParallelWriteToMultiHashMapJob
  452. {
  453. Writer = container.AsParallelWriter()
  454. }.Schedule(1, 2);
  455. Assert.Throws<ObjectDisposedException>(() =>
  456. {
  457. while (iter.MoveNext())
  458. {
  459. }
  460. });
  461. jobHandle.Complete();
  462. }
  463. }
  464. [Test]
  465. public void NativeParallelMultiHashMap_GetKeysAndValues()
  466. {
  467. var container = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
  468. for (int i = 0; i < 30; ++i)
  469. {
  470. container.Add(i, 30 + i);
  471. container.Add(i, 60 + i);
  472. }
  473. var keysValues = container.GetKeyValueArrays(Allocator.Temp);
  474. container.Dispose();
  475. Assert.AreEqual(60, keysValues.Keys.Length);
  476. Assert.AreEqual(60, keysValues.Values.Length);
  477. // ensure keys and matching values are aligned (though unordered)
  478. for (int i = 0; i < 30; ++i)
  479. {
  480. var k0 = keysValues.Keys[i * 2 + 0];
  481. var k1 = keysValues.Keys[i * 2 + 1];
  482. var v0 = keysValues.Values[i * 2 + 0];
  483. var v1 = keysValues.Values[i * 2 + 1];
  484. if (v0 > v1)
  485. (v0, v1) = (v1, v0);
  486. Assert.AreEqual(k0, k1);
  487. Assert.AreEqual(30 + k0, v0);
  488. Assert.AreEqual(60 + k0, v1);
  489. }
  490. keysValues.Keys.Sort();
  491. for (int i = 0; i < 30; ++i)
  492. {
  493. Assert.AreEqual(i, keysValues.Keys[i * 2 + 0]);
  494. Assert.AreEqual(i, keysValues.Keys[i * 2 + 1]);
  495. }
  496. keysValues.Values.Sort();
  497. for (int i = 0; i < 60; ++i)
  498. {
  499. Assert.AreEqual(30 + i, keysValues.Values[i]);
  500. }
  501. keysValues.Dispose();
  502. }
  503. [Test]
  504. public void NativeParallelMultiHashMap_ContainsKeyMultiHashMap()
  505. {
  506. var container = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
  507. container.Add(5, 7);
  508. container.Add(6, 9);
  509. container.Add(6, 10);
  510. Assert.IsTrue(container.ContainsKey(5));
  511. Assert.IsTrue(container.ContainsKey(6));
  512. Assert.IsFalse(container.ContainsKey(4));
  513. container.Dispose();
  514. }
  515. [Test]
  516. public void NativeParallelMultiHashMap_CustomAllocatorTest()
  517. {
  518. AllocatorManager.Initialize();
  519. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  520. ref var allocator = ref allocatorHelper.Allocator;
  521. allocator.Initialize();
  522. using (var container = new NativeParallelMultiHashMap<int, int>(1, allocator.Handle))
  523. {
  524. }
  525. Assert.IsTrue(allocator.WasUsed);
  526. allocator.Dispose();
  527. allocatorHelper.Dispose();
  528. AllocatorManager.Shutdown();
  529. }
  530. [BurstCompile]
  531. struct BurstedCustomAllocatorJob : IJob
  532. {
  533. [NativeDisableUnsafePtrRestriction]
  534. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  535. public void Execute()
  536. {
  537. unsafe
  538. {
  539. using (var container = new NativeParallelMultiHashMap<int, int>(1, Allocator->Handle))
  540. {
  541. }
  542. }
  543. }
  544. }
  545. [Test]
  546. public unsafe void NativeParallelMultiHashMap_BurstedCustomAllocatorTest()
  547. {
  548. AllocatorManager.Initialize();
  549. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  550. ref var allocator = ref allocatorHelper.Allocator;
  551. allocator.Initialize();
  552. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  553. unsafe
  554. {
  555. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
  556. handle.Complete();
  557. }
  558. Assert.IsTrue(allocator.WasUsed);
  559. allocator.Dispose();
  560. allocatorHelper.Dispose();
  561. AllocatorManager.Shutdown();
  562. }
  563. public struct NestedHashMap
  564. {
  565. public NativeParallelMultiHashMap<int, int> map;
  566. }
  567. [Test]
  568. public void NativeParallelMultiHashMap_Nested()
  569. {
  570. var mapInner = new NativeParallelMultiHashMap<int, int>(16, CommonRwdAllocator.Handle);
  571. NestedHashMap mapStruct = new NestedHashMap { map = mapInner };
  572. var mapNestedStruct = new NativeParallelMultiHashMap<int, NestedHashMap>(16, CommonRwdAllocator.Handle);
  573. var mapNested = new NativeParallelMultiHashMap<int, NativeParallelMultiHashMap<int, int>>(16, CommonRwdAllocator.Handle);
  574. mapNested.Add(14, mapInner);
  575. mapNestedStruct.Add(17, mapStruct);
  576. mapNested.Dispose();
  577. mapNestedStruct.Dispose();
  578. mapInner.Dispose();
  579. }
  580. }