Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UnsafeHashMapTests.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. using System.Diagnostics;
  10. internal class UnsafeHashMapTests : CollectionsTestCommonBase
  11. {
  12. [Test]
  13. public void UnsafeHashMap_ForEach([Values(10, 1000)] int n)
  14. {
  15. var seen = new NativeArray<int>(n, Allocator.Temp);
  16. using (var container = new UnsafeHashMap<int, int>(32, CommonRwdAllocator.Handle))
  17. {
  18. for (int i = 0; i < n; i++)
  19. {
  20. container.Add(i, i * 37);
  21. }
  22. var count = 0;
  23. foreach (var kv in container)
  24. {
  25. int value;
  26. Assert.True(container.TryGetValue(kv.Key, out value));
  27. Assert.AreEqual(value, kv.Value);
  28. Assert.AreEqual(kv.Key * 37, kv.Value);
  29. seen[kv.Key] = seen[kv.Key] + 1;
  30. ++count;
  31. }
  32. Assert.AreEqual(container.Count, count);
  33. for (int i = 0; i < n; i++)
  34. {
  35. Assert.AreEqual(1, seen[i], $"Incorrect key count {i}");
  36. }
  37. }
  38. }
  39. [Test]
  40. public void UnsafeHashMap_ForEach_FixedStringKey()
  41. {
  42. using (var stringList = new NativeList<FixedString32Bytes>(10, Allocator.Persistent) { "Hello", ",", "World", "!" })
  43. {
  44. var seen = new NativeArray<int>(stringList.Length, Allocator.Temp);
  45. var container = new UnsafeHashMap<FixedString128Bytes, float>(50, Allocator.Temp);
  46. foreach (var str in stringList)
  47. {
  48. container.Add(str, 0);
  49. }
  50. foreach (var pair in container)
  51. {
  52. int index = stringList.IndexOf(pair.Key);
  53. Assert.AreEqual(stringList[index], pair.Key.ToString());
  54. seen[index] = seen[index] + 1;
  55. }
  56. for (int i = 0; i < stringList.Length; i++)
  57. {
  58. Assert.AreEqual(1, seen[i], $"Incorrect value count {stringList[i]}");
  59. }
  60. }
  61. }
  62. struct UnsafeHashMap_ForEachIterator : IJob
  63. {
  64. [ReadOnly]
  65. public UnsafeHashMap<int, int>.Enumerator Iter;
  66. public void Execute()
  67. {
  68. while (Iter.MoveNext())
  69. {
  70. }
  71. }
  72. }
  73. [Test]
  74. public void UnsafeHashMap_ForEach_Throws_Job_Iterator()
  75. {
  76. using (var container = new UnsafeHashMap<int, int>(32, CommonRwdAllocator.Handle))
  77. {
  78. var jobHandle = new UnsafeHashMap_ForEachIterator
  79. {
  80. Iter = container.GetEnumerator()
  81. }.Schedule();
  82. jobHandle.Complete();
  83. }
  84. }
  85. struct UnsafeHashMap_ForEach_Job : IJob
  86. {
  87. public UnsafeHashMap<int, int>.ReadOnly Input;
  88. [ReadOnly]
  89. public int Num;
  90. public void Execute()
  91. {
  92. var seen = new NativeArray<int>(Num, Allocator.Temp);
  93. var count = 0;
  94. foreach (var kv in Input)
  95. {
  96. int value;
  97. Assert.True(Input.TryGetValue(kv.Key, out value));
  98. Assert.AreEqual(value, kv.Value);
  99. Assert.AreEqual(kv.Key * 37, kv.Value);
  100. seen[kv.Key] = seen[kv.Key] + 1;
  101. ++count;
  102. }
  103. Assert.AreEqual(Input.Count, count);
  104. for (int i = 0; i < Num; i++)
  105. {
  106. Assert.AreEqual(1, seen[i], $"Incorrect key count {i}");
  107. }
  108. seen.Dispose();
  109. }
  110. }
  111. [Test]
  112. public void UnsafeHashMap_ForEach_From_Job([Values(10, 1000)] int n)
  113. {
  114. var seen = new NativeArray<int>(n, Allocator.Temp);
  115. using (var container = new UnsafeHashMap<int, int>(32, CommonRwdAllocator.Handle))
  116. {
  117. for (int i = 0; i < n; i++)
  118. {
  119. container.Add(i, i * 37);
  120. }
  121. new UnsafeHashMap_ForEach_Job
  122. {
  123. Input = container.AsReadOnly(),
  124. Num = n,
  125. }.Run();
  126. }
  127. }
  128. [Test]
  129. public void UnsafeHashMap_EnumeratorDoesNotReturnRemovedElementsTest()
  130. {
  131. UnsafeHashMap<int, int> container = new UnsafeHashMap<int, int>(5, Allocator.Temp);
  132. for (int i = 0; i < 5; i++)
  133. {
  134. container.Add(i, i);
  135. }
  136. int elementToRemove = 2;
  137. Assert.IsTrue(container.Remove(elementToRemove));
  138. Assert.IsFalse(container.Remove(elementToRemove));
  139. using (var enumerator = container.GetEnumerator())
  140. {
  141. while (enumerator.MoveNext())
  142. {
  143. Assert.AreNotEqual(elementToRemove, enumerator.Current.Key);
  144. }
  145. }
  146. container.Dispose();
  147. }
  148. [Test]
  149. public void UnsafeHashMap_EnumeratorInfiniteIterationTest()
  150. {
  151. UnsafeHashMap<int, int> container = new UnsafeHashMap<int, int>(5, Allocator.Temp);
  152. for (int i = 0; i < 5; i++)
  153. {
  154. container.Add(i, i);
  155. }
  156. for (int i = 0; i < 2; i++)
  157. {
  158. Assert.IsTrue(container.Remove(i));
  159. }
  160. var expected = container.Count;
  161. int count = 0;
  162. using (var enumerator = container.GetEnumerator())
  163. {
  164. while (enumerator.MoveNext())
  165. {
  166. if (count++ > expected)
  167. {
  168. break;
  169. }
  170. }
  171. }
  172. Assert.AreEqual(expected, count);
  173. container.Dispose();
  174. }
  175. [Test]
  176. public void UnsafeHashMap_CustomAllocatorTest()
  177. {
  178. AllocatorManager.Initialize();
  179. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  180. ref var allocator = ref allocatorHelper.Allocator;
  181. allocator.Initialize();
  182. using (var container = new UnsafeHashMap<int, int>(1, allocator.Handle))
  183. {
  184. }
  185. Assert.IsTrue(allocator.WasUsed);
  186. allocator.Dispose();
  187. allocatorHelper.Dispose();
  188. AllocatorManager.Shutdown();
  189. }
  190. [BurstCompile]
  191. struct BurstedCustomAllocatorJob : IJob
  192. {
  193. [NativeDisableUnsafePtrRestriction]
  194. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  195. public void Execute()
  196. {
  197. unsafe
  198. {
  199. using (var container = new UnsafeHashMap<int, int>(1, Allocator->Handle))
  200. {
  201. }
  202. }
  203. }
  204. }
  205. [Test]
  206. public unsafe void UnsafeHashMap_BurstedCustomAllocatorTest()
  207. {
  208. AllocatorManager.Initialize();
  209. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  210. ref var allocator = ref allocatorHelper.Allocator;
  211. allocator.Initialize();
  212. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  213. unsafe
  214. {
  215. var handle = new BurstedCustomAllocatorJob { Allocator = allocatorPtr }.Schedule();
  216. handle.Complete();
  217. }
  218. Assert.IsTrue(allocator.WasUsed);
  219. allocator.Dispose();
  220. allocatorHelper.Dispose();
  221. AllocatorManager.Shutdown();
  222. }
  223. static void ExpectedCount<TKey, TValue>(ref UnsafeHashMap<TKey, TValue> container, int expected)
  224. where TKey : unmanaged, IEquatable<TKey>
  225. where TValue : unmanaged
  226. {
  227. Assert.AreEqual(expected == 0, container.IsEmpty);
  228. Assert.AreEqual(expected, container.Count);
  229. }
  230. [Test]
  231. public void UnsafeHashMap_TryAdd_TryGetValue_Clear()
  232. {
  233. var container = new UnsafeHashMap<int, int>(16, Allocator.Temp);
  234. ExpectedCount(ref container, 0);
  235. int iSquared;
  236. // Make sure GetValue fails if container is empty
  237. Assert.IsFalse(container.TryGetValue(0, out iSquared), "TryGetValue on empty container did not fail");
  238. // Make sure inserting values work
  239. for (int i = 0; i < 16; ++i)
  240. Assert.IsTrue(container.TryAdd(i, i * i), "Failed to add value");
  241. ExpectedCount(ref container, 16);
  242. // Make sure inserting duplicate keys fails
  243. for (int i = 0; i < 16; ++i)
  244. Assert.IsFalse(container.TryAdd(i, i), "Adding duplicate keys did not fail");
  245. ExpectedCount(ref container, 16);
  246. // Make sure reading the inserted values work
  247. for (int i = 0; i < 16; ++i)
  248. {
  249. Assert.IsTrue(container.TryGetValue(i, out iSquared), "Failed get value from hash table");
  250. Assert.AreEqual(iSquared, i * i, "Got the wrong value from the hash table");
  251. }
  252. // Make sure clearing removes all keys
  253. container.Clear();
  254. ExpectedCount(ref container, 0);
  255. for (int i = 0; i < 16; ++i)
  256. Assert.IsFalse(container.TryGetValue(i, out iSquared), "Got value from hash table after clearing");
  257. container.Dispose();
  258. }
  259. [Test]
  260. public void UnsafeHashMap_Key_Collisions()
  261. {
  262. var container = new UnsafeHashMap<int, int>(16, Allocator.Temp);
  263. int iSquared;
  264. // Make sure GetValue fails if container is empty
  265. Assert.IsFalse(container.TryGetValue(0, out iSquared), "TryGetValue on empty container did not fail");
  266. // Make sure inserting values work
  267. for (int i = 0; i < 8; ++i)
  268. {
  269. Assert.IsTrue(container.TryAdd(i, i * i), "Failed to add value");
  270. }
  271. // The bucket size is capacity * 2, adding that number should result in hash collisions
  272. for (int i = 0; i < 8; ++i)
  273. {
  274. Assert.IsTrue(container.TryAdd(i + 32, i), "Failed to add value with potential hash collision");
  275. }
  276. // Make sure reading the inserted values work
  277. for (int i = 0; i < 8; ++i)
  278. {
  279. Assert.IsTrue(container.TryGetValue(i, out iSquared), "Failed get value from hash table");
  280. Assert.AreEqual(iSquared, i * i, "Got the wrong value from the hash table");
  281. }
  282. for (int i = 0; i < 8; ++i)
  283. {
  284. Assert.IsTrue(container.TryGetValue(i + 32, out iSquared), "Failed get value from hash table");
  285. Assert.AreEqual(iSquared, i, "Got the wrong value from the hash table");
  286. }
  287. container.Dispose();
  288. }
  289. [Test]
  290. public void UnsafeHashMap_SupportsAutomaticCapacityChange()
  291. {
  292. var container = new UnsafeHashMap<int, int>(1, Allocator.Temp);
  293. int iSquared;
  294. // Make sure inserting values work and grows the capacity
  295. for (int i = 0; i < 8; ++i)
  296. {
  297. Assert.IsTrue(container.TryAdd(i, i * i), "Failed to add value");
  298. }
  299. Assert.IsTrue(container.Capacity >= 8, "Capacity was not updated correctly");
  300. // Make sure reading the inserted values work
  301. for (int i = 0; i < 8; ++i)
  302. {
  303. Assert.IsTrue(container.TryGetValue(i, out iSquared), "Failed get value from hash table");
  304. Assert.AreEqual(iSquared, i * i, "Got the wrong value from the hash table");
  305. }
  306. container.Dispose();
  307. }
  308. [Test]
  309. [TestRequiresDotsDebugOrCollectionChecks]
  310. public void UnsafeHashMap_SameKey()
  311. {
  312. using (var container = new UnsafeHashMap<int, int>(0, Allocator.Persistent))
  313. {
  314. Assert.DoesNotThrow(() => container.Add(0, 0));
  315. Assert.Throws<ArgumentException>(() => container.Add(0, 0));
  316. }
  317. using (var container = new UnsafeHashMap<int, int>(0, Allocator.Persistent))
  318. {
  319. Assert.IsTrue(container.TryAdd(0, 0));
  320. Assert.IsFalse(container.TryAdd(0, 0));
  321. }
  322. }
  323. [Test]
  324. public void UnsafeHashMap_IsEmpty()
  325. {
  326. var container = new UnsafeHashMap<int, int>(0, Allocator.Persistent);
  327. Assert.IsTrue(container.IsEmpty);
  328. container.TryAdd(0, 0);
  329. Assert.IsFalse(container.IsEmpty);
  330. ExpectedCount(ref container, 1);
  331. Assert.IsTrue(container.Remove(0));
  332. Assert.IsFalse(container.Remove(0));
  333. Assert.IsTrue(container.IsEmpty);
  334. container.TryAdd(0, 0);
  335. container.Clear();
  336. Assert.IsTrue(container.IsEmpty);
  337. container.Dispose();
  338. }
  339. [Test]
  340. public void UnsafeHashMap_EmptyCapacity()
  341. {
  342. var container = new UnsafeHashMap<int, int>(0, Allocator.Persistent);
  343. container.TryAdd(0, 0);
  344. ExpectedCount(ref container, 1);
  345. container.Dispose();
  346. }
  347. [Test]
  348. public void UnsafeHashMap_Remove()
  349. {
  350. var container = new UnsafeHashMap<int, int>(8, Allocator.Temp);
  351. int iSquared;
  352. for (int rm = 0; rm < 8; ++rm)
  353. {
  354. Assert.IsFalse(container.Remove(rm));
  355. }
  356. // Make sure inserting values work
  357. for (int i = 0; i < 8; ++i)
  358. {
  359. Assert.IsTrue(container.TryAdd(i, i * i), "Failed to add value");
  360. }
  361. Assert.AreEqual(8, container.Count);
  362. // Make sure reading the inserted values work
  363. for (int i = 0; i < 8; ++i)
  364. {
  365. Assert.IsTrue(container.TryGetValue(i, out iSquared), "Failed get value from hash table");
  366. Assert.AreEqual(iSquared, i * i, "Got the wrong value from the hash table");
  367. }
  368. for (int rm = 0; rm < 8; ++rm)
  369. {
  370. Assert.IsTrue(container.Remove(rm));
  371. Assert.IsFalse(container.TryGetValue(rm, out iSquared), "Failed to remove value from hash table");
  372. for (int i = rm + 1; i < 8; ++i)
  373. {
  374. Assert.IsTrue(container.TryGetValue(i, out iSquared), "Failed get value from hash table");
  375. Assert.AreEqual(iSquared, i * i, "Got the wrong value from the hash table");
  376. }
  377. }
  378. Assert.AreEqual(0, container.Count);
  379. // Make sure entries were freed
  380. for (int i = 0; i < 8; ++i)
  381. {
  382. Assert.IsTrue(container.TryAdd(i, i * i), "Failed to add value");
  383. }
  384. Assert.AreEqual(8, container.Count);
  385. container.Dispose();
  386. }
  387. [Test]
  388. public void UnsafeHashMap_RemoveOnEmptyMap_DoesNotThrow()
  389. {
  390. var container = new UnsafeHashMap<int, int>(0, Allocator.Temp);
  391. Assert.DoesNotThrow(() => container.Remove(0));
  392. Assert.DoesNotThrow(() => container.Remove(-425196));
  393. container.Dispose();
  394. }
  395. [Test]
  396. public void UnsafeHashMap_TryAddScalability()
  397. {
  398. var container = new UnsafeHashMap<int, int>(1, Allocator.Persistent);
  399. Assert.AreEqual(container.Capacity, (1 << container.m_Data.Log2MinGrowth));
  400. for (int i = 0; i != 1000 * 100; i++)
  401. {
  402. container.Add(i, i);
  403. }
  404. int value;
  405. Assert.AreEqual(container.Count, 100000);
  406. Assert.IsFalse(container.TryGetValue(-1, out value));
  407. Assert.IsFalse(container.TryGetValue(1000 * 1000, out value));
  408. for (int i = 0; i != 1000 * 100; i++)
  409. {
  410. Assert.IsTrue(container.TryGetValue(i, out value));
  411. Assert.AreEqual(i, value);
  412. }
  413. container.Dispose();
  414. }
  415. [Test]
  416. public unsafe void UnsafeHashMap_TrimExcess()
  417. {
  418. using (var container = new UnsafeHashMap<int, int>(1024, Allocator.Persistent))
  419. {
  420. var oldCapacity = container.Capacity;
  421. container.Add(123, 345);
  422. container.TrimExcess();
  423. Assert.AreEqual(container.Capacity, (1 << container.m_Data.Log2MinGrowth));
  424. Assert.AreEqual(1, container.Count);
  425. Assert.AreNotEqual(oldCapacity, container.Capacity);
  426. oldCapacity = container.Capacity;
  427. container.Remove(123);
  428. Assert.AreEqual(container.Count, 0);
  429. container.TrimExcess();
  430. Assert.AreEqual(oldCapacity, container.Capacity);
  431. container.Add(123, 345);
  432. Assert.AreEqual(container.Count, 1);
  433. Assert.AreEqual(oldCapacity, container.Capacity);
  434. container.Clear();
  435. Assert.AreEqual(container.Count, 0);
  436. Assert.AreEqual(oldCapacity, container.Capacity);
  437. }
  438. }
  439. [Test]
  440. public void UnsafeHashMap_IndexerAdd_ResizesContainer()
  441. {
  442. var container = new UnsafeHashMap<int, int>(8, Allocator.Persistent);
  443. for (int i = 0; i < 1024; i++)
  444. {
  445. container[i] = i;
  446. }
  447. Assert.AreEqual(1024, container.Count);
  448. container.Dispose();
  449. }
  450. }