暫無描述
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.

NativeHashSetTests.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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.Jobs;
  8. using Unity.Collections.Tests;
  9. using UnityEngine;
  10. using UnityEngine.TestTools;
  11. #if !UNITY_PORTABLE_TEST_RUNNER
  12. using System.Text.RegularExpressions;
  13. #endif
  14. internal class NativeHashSetTests: CollectionsTestFixture
  15. {
  16. static void ExpectedCount<T>(ref NativeHashSet<T> container, int expected)
  17. where T : unmanaged, IEquatable<T>
  18. {
  19. Assert.AreEqual(expected == 0, container.IsEmpty);
  20. Assert.AreEqual(expected, container.Count());
  21. }
  22. [Test]
  23. public void NativeHashSet_IsEmpty()
  24. {
  25. var container = new NativeHashSet<int>(0, Allocator.Persistent);
  26. Assert.IsTrue(container.IsEmpty);
  27. Assert.IsTrue(container.Add(0));
  28. Assert.IsFalse(container.IsEmpty);
  29. Assert.AreEqual(1, container.Capacity);
  30. ExpectedCount(ref container, 1);
  31. container.Remove(0);
  32. Assert.IsTrue(container.IsEmpty);
  33. Assert.IsTrue(container.Add(0));
  34. container.Clear();
  35. Assert.IsTrue(container.IsEmpty);
  36. container.Dispose();
  37. }
  38. [Test]
  39. public void UnsafeHashSet_Capacity()
  40. {
  41. var container = new NativeHashSet<int>(0, Allocator.Persistent);
  42. Assert.IsTrue(container.IsEmpty);
  43. Assert.AreEqual(0, container.Capacity);
  44. container.Capacity = 10;
  45. Assert.AreEqual(10, container.Capacity);
  46. container.Dispose();
  47. }
  48. #if !UNITY_DOTSRUNTIME // DOTS-Runtime has an assertion in the C++ layer, that can't be caught in C#
  49. [Test]
  50. public void NativeHashSet_Full_Throws()
  51. {
  52. var container = new NativeHashSet<int>(16, Allocator.Temp);
  53. ExpectedCount(ref container, 0);
  54. for (int i = 0, capacity = container.Capacity; i < capacity; ++i)
  55. {
  56. Assert.DoesNotThrow(() => { container.Add(i); });
  57. }
  58. ExpectedCount(ref container, container.Capacity);
  59. // Make sure overallocating throws and exception if using the Concurrent version - normal hash map would grow
  60. var writer = container.AsParallelWriter();
  61. Assert.Throws<System.InvalidOperationException>(() => { writer.Add(100); });
  62. ExpectedCount(ref container, container.Capacity);
  63. container.Clear();
  64. ExpectedCount(ref container, 0);
  65. container.Dispose();
  66. }
  67. #endif
  68. [Test]
  69. public void NativeHashSet_RemoveOnEmptyMap_DoesNotThrow()
  70. {
  71. var container = new NativeHashSet<int>(0, Allocator.Temp);
  72. Assert.DoesNotThrow(() => container.Remove(0));
  73. Assert.DoesNotThrow(() => container.Remove(-425196));
  74. container.Dispose();
  75. }
  76. [Test]
  77. public void NativeHashSet_Double_Deallocate_Throws()
  78. {
  79. var hashMap = new NativeHashSet<int>(16, CommonRwdAllocator.Handle);
  80. hashMap.Dispose();
  81. Assert.Throws<ObjectDisposedException>(
  82. () => { hashMap.Dispose(); });
  83. }
  84. [Test]
  85. public void NativeHashSet_Collisions()
  86. {
  87. var container = new NativeHashSet<int>(16, Allocator.Temp);
  88. Assert.IsFalse(container.Contains(0), "Contains on empty hash map did not fail");
  89. ExpectedCount(ref container, 0);
  90. // Make sure inserting values work
  91. for (int i = 0; i < 8; ++i)
  92. {
  93. Assert.IsTrue(container.Add(i), "Failed to add value");
  94. }
  95. ExpectedCount(ref container, 8);
  96. // The bucket size is capacity * 2, adding that number should result in hash collisions
  97. for (int i = 0; i < 8; ++i)
  98. {
  99. Assert.IsTrue(container.Add(i + 32), "Failed to add value with potential hash collision");
  100. }
  101. // Make sure reading the inserted values work
  102. for (int i = 0; i < 8; ++i)
  103. {
  104. Assert.IsTrue(container.Contains(i), "Failed get value from hash set");
  105. }
  106. for (int i = 0; i < 8; ++i)
  107. {
  108. Assert.IsTrue(container.Contains(i + 32), "Failed get value from hash set");
  109. }
  110. container.Dispose();
  111. }
  112. [Test]
  113. public void NativeHashSet_SameElement()
  114. {
  115. using (var container = new NativeHashSet<int>(0, Allocator.Persistent))
  116. {
  117. Assert.IsTrue(container.Add(0));
  118. Assert.IsFalse(container.Add(0));
  119. }
  120. }
  121. [Test]
  122. public void NativeHashSet_ParallelWriter_CanBeUsedInJob()
  123. {
  124. const int count = 32;
  125. using (var hashSet = new NativeHashSet<int>(count, CommonRwdAllocator.Handle))
  126. {
  127. new ParallelWriteToHashSetJob
  128. {
  129. Writer = hashSet.AsParallelWriter()
  130. }.Schedule(count, 2).Complete();
  131. var result = hashSet.ToNativeArray(Allocator.Temp);
  132. result.Sort();
  133. for (int i = 0; i < count; i++)
  134. Assert.AreEqual(i, result[i]);
  135. }
  136. }
  137. struct ParallelWriteToHashSetJob : IJobParallelFor
  138. {
  139. [WriteOnly]
  140. public NativeHashSet<int>.ParallelWriter Writer;
  141. public void Execute(int index)
  142. {
  143. Writer.Add(index);
  144. }
  145. }
  146. [Test]
  147. public void NativeHashSet_CanBeReadFromJob()
  148. {
  149. using (var hashSet = new NativeHashSet<int>(1, CommonRwdAllocator.Handle))
  150. using (var result = new NativeReference<int>(CommonRwdAllocator.Handle))
  151. {
  152. hashSet.Add(42);
  153. new ReadHashSetJob
  154. {
  155. Input = hashSet,
  156. Output = result,
  157. }.Run();
  158. Assert.AreEqual(42, result.Value);
  159. }
  160. }
  161. struct ReadHashSetJob : IJob
  162. {
  163. [ReadOnly]
  164. public NativeHashSet<int> Input;
  165. public NativeReference<int> Output;
  166. public void Execute()
  167. {
  168. Output.Value = Input.ToNativeArray(Allocator.Temp)[0];
  169. foreach (var value in Input)
  170. {
  171. Assert.AreEqual(42, value);
  172. }
  173. }
  174. }
  175. [Test]
  176. public void NativeHashSet_ForEach_FixedStringInHashMap()
  177. {
  178. using (var stringList = new NativeList<FixedString32Bytes>(10, Allocator.Persistent) { "Hello", ",", "World", "!" })
  179. {
  180. var container = new NativeHashSet<FixedString128Bytes>(50, Allocator.Temp);
  181. var seen = new NativeArray<int>(stringList.Length, Allocator.Temp);
  182. foreach (var str in stringList)
  183. {
  184. container.Add(str);
  185. }
  186. foreach (var value in container)
  187. {
  188. int index = stringList.IndexOf(value);
  189. Assert.AreEqual(stringList[index], value.ToString());
  190. seen[index] = seen[index] + 1;
  191. }
  192. for (int i = 0; i < stringList.Length; i++)
  193. {
  194. Assert.AreEqual(1, seen[i], $"Incorrect value count {stringList[i]}");
  195. }
  196. }
  197. }
  198. [Test]
  199. public void NativeHashSet_ForEach([Values(10, 1000)]int n)
  200. {
  201. var seen = new NativeArray<int>(n, Allocator.Temp);
  202. using (var container = new NativeHashSet<int>(32, CommonRwdAllocator.Handle))
  203. {
  204. for (int i = 0; i < n; i++)
  205. {
  206. container.Add(i);
  207. }
  208. var count = 0;
  209. foreach (var item in container)
  210. {
  211. Assert.True(container.Contains(item));
  212. seen[item] = seen[item] + 1;
  213. ++count;
  214. }
  215. Assert.AreEqual(container.Count(), count);
  216. for (int i = 0; i < n; i++)
  217. {
  218. Assert.AreEqual(1, seen[i], $"Incorrect item count {i}");
  219. }
  220. }
  221. }
  222. struct NativeHashSet_ForEach_Job : IJob
  223. {
  224. [ReadOnly]
  225. public NativeHashSet<int> Input;
  226. [ReadOnly]
  227. public int Num;
  228. public void Execute()
  229. {
  230. var seen = new NativeArray<int>(Num, Allocator.Temp);
  231. var count = 0;
  232. foreach (var item in Input)
  233. {
  234. Assert.True(Input.Contains(item));
  235. seen[item] = seen[item] + 1;
  236. ++count;
  237. }
  238. Assert.AreEqual(Input.Count(), count);
  239. for (int i = 0; i < Num; i++)
  240. {
  241. Assert.AreEqual(1, seen[i], $"Incorrect item count {i}");
  242. }
  243. seen.Dispose();
  244. }
  245. }
  246. [Test]
  247. public void NativeHashSet_ForEach_From_Job([Values(10, 1000)] int n)
  248. {
  249. using (var container = new NativeHashSet<int>(32, CommonRwdAllocator.Handle))
  250. {
  251. for (int i = 0; i < n; i++)
  252. {
  253. container.Add(i);
  254. }
  255. new NativeHashSet_ForEach_Job
  256. {
  257. Input = container,
  258. Num = n,
  259. }.Run();
  260. }
  261. }
  262. [Test]
  263. public void NativeHashSet_ForEach_Throws_When_Modified()
  264. {
  265. using (var container = new NativeHashSet<int>(32, CommonRwdAllocator.Handle))
  266. {
  267. container.Add(0);
  268. container.Add(1);
  269. container.Add(2);
  270. container.Add(3);
  271. container.Add(4);
  272. container.Add(5);
  273. container.Add(6);
  274. container.Add(7);
  275. container.Add(8);
  276. container.Add(9);
  277. Assert.Throws<ObjectDisposedException>(() =>
  278. {
  279. foreach (var item in container)
  280. {
  281. container.Add(10);
  282. }
  283. });
  284. Assert.Throws<ObjectDisposedException>(() =>
  285. {
  286. foreach (var item in container)
  287. {
  288. container.Remove(1);
  289. }
  290. });
  291. }
  292. }
  293. [Test]
  294. public void NativeHashSet_ForEach_Throws()
  295. {
  296. using (var container = new NativeHashSet<int>(32, CommonRwdAllocator.Handle))
  297. {
  298. var iter = container.GetEnumerator();
  299. var jobHandle = new ParallelWriteToHashSetJob
  300. {
  301. Writer = container.AsParallelWriter()
  302. }.Schedule(1, 2);
  303. Assert.Throws<ObjectDisposedException>(() =>
  304. {
  305. while (iter.MoveNext())
  306. {
  307. }
  308. });
  309. jobHandle.Complete();
  310. }
  311. }
  312. struct ForEachIterator : IJob
  313. {
  314. [ReadOnly]
  315. public NativeHashSet<int>.Enumerator Iter;
  316. public void Execute()
  317. {
  318. while (Iter.MoveNext())
  319. {
  320. }
  321. }
  322. }
  323. [Test]
  324. public void NativeHashSet_ForEach_Throws_Job_Iterator()
  325. {
  326. using (var container = new NativeHashSet<int>(32, CommonRwdAllocator.Handle))
  327. {
  328. var jobHandle = new ForEachIterator
  329. {
  330. Iter = container.GetEnumerator()
  331. }.Schedule();
  332. Assert.Throws<InvalidOperationException>(() => { container.Add(1); });
  333. jobHandle.Complete();
  334. }
  335. }
  336. [Test]
  337. public void NativeHashSet_EIU_ExceptWith_Empty()
  338. {
  339. var setA = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { };
  340. var setB = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { };
  341. setA.ExceptWith(setB);
  342. ExpectedCount(ref setA, 0);
  343. setA.Dispose();
  344. setB.Dispose();
  345. }
  346. [Test]
  347. public void NativeHashSet_EIU_ExceptWith_AxB()
  348. {
  349. var setA = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { 0, 1, 2, 3, 4, 5 };
  350. var setB = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { 3, 4, 5, 6, 7, 8 };
  351. setA.ExceptWith(setB);
  352. ExpectedCount(ref setA, 3);
  353. Assert.True(setA.Contains(0));
  354. Assert.True(setA.Contains(1));
  355. Assert.True(setA.Contains(2));
  356. setA.Dispose();
  357. setB.Dispose();
  358. }
  359. [Test]
  360. public void NativeHashSet_EIU_ExceptWith_BxA()
  361. {
  362. var setA = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { 0, 1, 2, 3, 4, 5 };
  363. var setB = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { 3, 4, 5, 6, 7, 8 };
  364. setB.ExceptWith(setA);
  365. ExpectedCount(ref setB, 3);
  366. Assert.True(setB.Contains(6));
  367. Assert.True(setB.Contains(7));
  368. Assert.True(setB.Contains(8));
  369. setA.Dispose();
  370. setB.Dispose();
  371. }
  372. [Test]
  373. public void NativeHashSet_EIU_IntersectWith_Empty()
  374. {
  375. var setA = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { };
  376. var setB = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { };
  377. setA.IntersectWith(setB);
  378. ExpectedCount(ref setA, 0);
  379. setA.Dispose();
  380. setB.Dispose();
  381. }
  382. [Test]
  383. public void NativeHashSet_EIU_IntersectWith()
  384. {
  385. var setA = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { 0, 1, 2, 3, 4, 5 };
  386. var setB = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { 3, 4, 5, 6, 7, 8 };
  387. setA.IntersectWith(setB);
  388. ExpectedCount(ref setA, 3);
  389. Assert.True(setA.Contains(3));
  390. Assert.True(setA.Contains(4));
  391. Assert.True(setA.Contains(5));
  392. setA.Dispose();
  393. setB.Dispose();
  394. }
  395. [Test]
  396. public void NativeHashSet_EIU_UnionWith_Empty()
  397. {
  398. var setA = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { };
  399. var setB = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { };
  400. setA.UnionWith(setB);
  401. ExpectedCount(ref setA, 0);
  402. setA.Dispose();
  403. setB.Dispose();
  404. }
  405. [Test]
  406. public void NativeHashSet_EIU_UnionWith()
  407. {
  408. var setA = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { 0, 1, 2, 3, 4, 5 };
  409. var setB = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { 3, 4, 5, 6, 7, 8 };
  410. setA.UnionWith(setB);
  411. ExpectedCount(ref setA, 9);
  412. Assert.True(setA.Contains(0));
  413. Assert.True(setA.Contains(1));
  414. Assert.True(setA.Contains(2));
  415. Assert.True(setA.Contains(3));
  416. Assert.True(setA.Contains(4));
  417. Assert.True(setA.Contains(5));
  418. Assert.True(setA.Contains(6));
  419. Assert.True(setA.Contains(7));
  420. Assert.True(setA.Contains(8));
  421. setA.Dispose();
  422. setB.Dispose();
  423. }
  424. #if !NET_DOTS // Array.Sort is not supported
  425. [Test]
  426. public void NativeHashSet_ToArray()
  427. {
  428. using (var set = new NativeHashSet<int>(8, CommonRwdAllocator.Handle) { 0, 1, 2, 3, 4, 5 })
  429. {
  430. var array = set.ToArray();
  431. Array.Sort(array);
  432. for (int i = 0, num = set.Count(); i < num; i++)
  433. {
  434. Assert.AreEqual(array[i], i);
  435. }
  436. }
  437. }
  438. #endif
  439. [Test]
  440. public void NativeHashSet_CustomAllocatorTest()
  441. {
  442. AllocatorManager.Initialize();
  443. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  444. ref var allocator = ref allocatorHelper.Allocator;
  445. allocator.Initialize();
  446. using (var container = new NativeHashSet<int>(1, allocator.Handle))
  447. {
  448. }
  449. FastAssert.IsTrue(allocator.WasUsed);
  450. allocator.Dispose();
  451. allocatorHelper.Dispose();
  452. AllocatorManager.Shutdown();
  453. }
  454. [BurstCompile]
  455. struct BurstedCustomAllocatorJob : IJob
  456. {
  457. [NativeDisableUnsafePtrRestriction]
  458. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  459. public void Execute()
  460. {
  461. unsafe
  462. {
  463. using (var container = new NativeHashSet<int>(1, Allocator->Handle))
  464. {
  465. }
  466. }
  467. }
  468. }
  469. [Test]
  470. public unsafe void NativeHashSet_BurstedCustomAllocatorTest()
  471. {
  472. AllocatorManager.Initialize();
  473. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  474. ref var allocator = ref allocatorHelper.Allocator;
  475. allocator.Initialize();
  476. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  477. unsafe
  478. {
  479. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr }.Schedule();
  480. handle.Complete();
  481. }
  482. FastAssert.IsTrue(allocator.WasUsed);
  483. allocator.Dispose();
  484. allocatorHelper.Dispose();
  485. AllocatorManager.Shutdown();
  486. }
  487. }