Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

NativeQueueTests.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.Tests;
  7. using Unity.Jobs;
  8. using UnityEngine;
  9. using UnityEngine.TestTools;
  10. #if !UNITY_PORTABLE_TEST_RUNNER
  11. using System.Text.RegularExpressions;
  12. #endif
  13. using Assert = FastAssert;
  14. internal class NativeQueueTests : CollectionsTestCommonBase
  15. {
  16. static void ExpectedCount<T>(ref NativeQueue<T> container, int expected) where T : unmanaged
  17. {
  18. Assert.AreEqual(expected == 0, container.IsEmpty());
  19. Assert.AreEqual(expected, container.Count);
  20. }
  21. [Test]
  22. public void Enqueue_Dequeue()
  23. {
  24. var queue = new NativeQueue<int>(Allocator.Temp);
  25. ExpectedCount(ref queue, 0);
  26. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  27. for (int i = 0; i < 16; ++i)
  28. queue.Enqueue(i);
  29. ExpectedCount(ref queue, 16);
  30. for (int i = 0; i < 16; ++i)
  31. Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
  32. ExpectedCount(ref queue, 0);
  33. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  34. queue.Dispose();
  35. }
  36. [Test]
  37. public void ConcurrentEnqueue_Dequeue()
  38. {
  39. var queue = new NativeQueue<int>(Allocator.Temp);
  40. var cQueue = queue.AsParallelWriter();
  41. ExpectedCount(ref queue, 0);
  42. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  43. for (int i = 0; i < 16; ++i)
  44. cQueue.Enqueue(i);
  45. ExpectedCount(ref queue, 16);
  46. for (int i = 0; i < 16; ++i)
  47. Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
  48. ExpectedCount(ref queue, 0);
  49. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  50. queue.Dispose();
  51. }
  52. [Test]
  53. public void Enqueue_Dequeue_Peek()
  54. {
  55. var queue = new NativeQueue<int>(Allocator.Temp);
  56. ExpectedCount(ref queue, 0);
  57. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  58. for (int i = 0; i < 16; ++i)
  59. queue.Enqueue(i);
  60. ExpectedCount(ref queue, 16);
  61. for (int i = 0; i < 16; ++i)
  62. {
  63. Assert.AreEqual(i, queue.Peek(), "Got the wrong value from the queue");
  64. queue.Dequeue();
  65. }
  66. ExpectedCount(ref queue, 0);
  67. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  68. queue.Dispose();
  69. }
  70. [Test]
  71. public void Enqueue_Dequeue_Clear()
  72. {
  73. var queue = new NativeQueue<int>(Allocator.Temp);
  74. ExpectedCount(ref queue, 0);
  75. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  76. for (int i = 0; i < 16; ++i)
  77. queue.Enqueue(i);
  78. ExpectedCount(ref queue, 16);
  79. for (int i = 0; i < 8; ++i)
  80. Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
  81. ExpectedCount(ref queue, 8);
  82. queue.Clear();
  83. ExpectedCount(ref queue, 0);
  84. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  85. queue.Dispose();
  86. }
  87. [Test]
  88. public void Double_Deallocate_Throws()
  89. {
  90. var queue = new NativeQueue<int>(CommonRwdAllocator.Handle);
  91. queue.Dispose();
  92. Assert.Throws<ObjectDisposedException>(
  93. () => { queue.Dispose(); });
  94. }
  95. [Test]
  96. public void EnqueueScalability()
  97. {
  98. var queue = new NativeQueue<int>(Allocator.Persistent);
  99. for (int i = 0; i != 1000 * 100; i++)
  100. {
  101. queue.Enqueue(i);
  102. }
  103. ExpectedCount(ref queue, 1000 * 100);
  104. for (int i = 0; i != 1000 * 100; i++)
  105. Assert.AreEqual(i, queue.Dequeue());
  106. ExpectedCount(ref queue, 0);
  107. queue.Dispose();
  108. }
  109. [Test]
  110. public void Enqueue_Wrap()
  111. {
  112. var queue = new NativeQueue<int>(Allocator.Temp);
  113. ExpectedCount(ref queue, 0);
  114. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  115. for (int i = 0; i < 256; ++i)
  116. queue.Enqueue(i);
  117. ExpectedCount(ref queue, 256);
  118. for (int i = 0; i < 128; ++i)
  119. Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
  120. ExpectedCount(ref queue, 128);
  121. for (int i = 0; i < 128; ++i)
  122. queue.Enqueue(i);
  123. ExpectedCount(ref queue, 256);
  124. for (int i = 128; i < 256; ++i)
  125. Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
  126. ExpectedCount(ref queue, 128);
  127. for (int i = 0; i < 128; ++i)
  128. Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
  129. ExpectedCount(ref queue, 0);
  130. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  131. queue.Dispose();
  132. }
  133. [Test]
  134. public void ConcurrentEnqueue_Wrap()
  135. {
  136. var queue = new NativeQueue<int>(Allocator.Temp);
  137. var cQueue = queue.AsParallelWriter();
  138. ExpectedCount(ref queue, 0);
  139. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  140. for (int i = 0; i < 256; ++i)
  141. cQueue.Enqueue(i);
  142. ExpectedCount(ref queue, 256);
  143. for (int i = 0; i < 128; ++i)
  144. Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
  145. ExpectedCount(ref queue, 128);
  146. for (int i = 0; i < 128; ++i)
  147. cQueue.Enqueue(i);
  148. ExpectedCount(ref queue, 256);
  149. for (int i = 128; i < 256; ++i)
  150. Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
  151. ExpectedCount(ref queue, 128);
  152. for (int i = 0; i < 128; ++i)
  153. Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
  154. ExpectedCount(ref queue, 0);
  155. Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
  156. queue.Dispose();
  157. }
  158. [Test]
  159. public void NativeQueue_DisposeJob()
  160. {
  161. var container = new NativeQueue<int>(Allocator.Persistent);
  162. Assert.True(container.IsCreated);
  163. Assert.DoesNotThrow(() => { container.Enqueue(0); });
  164. var disposeJob = container.Dispose(default);
  165. Assert.False(container.IsCreated);
  166. Assert.Throws<ObjectDisposedException>(
  167. () => { container.Enqueue(0); });
  168. disposeJob.Complete();
  169. }
  170. [Test]
  171. public void TryDequeue_OnEmptyQueueWhichHadElements_RetainsValidState()
  172. {
  173. using (var queue = new NativeQueue<int>(Allocator.Temp))
  174. {
  175. for (int i = 0; i < 3; i++)
  176. {
  177. queue.Enqueue(i);
  178. Assert.AreEqual(1, queue.Count);
  179. int value;
  180. while (queue.TryDequeue(out value))
  181. {
  182. Assert.AreEqual(i, value);
  183. }
  184. Assert.AreEqual(0, queue.Count);
  185. }
  186. }
  187. }
  188. [Test]
  189. public void TryDequeue_OnEmptyQueue_RetainsValidState()
  190. {
  191. using (var queue = new NativeQueue<int>(Allocator.Temp))
  192. {
  193. Assert.IsFalse(queue.TryDequeue(out _));
  194. queue.Enqueue(1);
  195. Assert.AreEqual(1, queue.Count);
  196. }
  197. }
  198. [Test]
  199. public void ToArray_ContainsCorrectElements()
  200. {
  201. using (var queue = new NativeQueue<int>(Allocator.Temp))
  202. {
  203. for (int i = 0; i < 100; i++)
  204. queue.Enqueue(i);
  205. using (var array = queue.ToArray(Allocator.Temp))
  206. {
  207. Assert.AreEqual(queue.Count, array.Length);
  208. for (int i = 0; i < array.Length; i++)
  209. Assert.AreEqual(i, array[i]);
  210. }
  211. }
  212. }
  213. [Test]
  214. public void ToArray_RespectsDequeue()
  215. {
  216. using (var queue = new NativeQueue<int>(Allocator.Temp))
  217. {
  218. for (int i = 0; i < 100; i++)
  219. queue.Enqueue(i);
  220. for (int i = 0; i < 50; i++)
  221. queue.Dequeue();
  222. using (var array = queue.ToArray(Allocator.Temp))
  223. {
  224. Assert.AreEqual(queue.Count, array.Length);
  225. for (int i = 0; i < array.Length; i++)
  226. Assert.AreEqual(50 + i, array[i]);
  227. }
  228. }
  229. }
  230. // These tests require:
  231. // - JobsDebugger support for static safety IDs (added in 2020.1)
  232. // - Asserting throws
  233. #if !UNITY_DOTSRUNTIME
  234. [Test,DotsRuntimeIgnore]
  235. public void NativeQueue_UseAfterFree_UsesCustomOwnerTypeName()
  236. {
  237. var container = new NativeQueue<int>(CommonRwdAllocator.Handle);
  238. container.Enqueue(123);
  239. container.Dispose();
  240. NUnit.Framework.Assert.That(() => container.Dequeue(),
  241. Throws.Exception.TypeOf<ObjectDisposedException>()
  242. .With.Message.Contains($"The {container.GetType()} has been deallocated"));
  243. }
  244. #endif
  245. [Test]
  246. public void NativeQueue_CustomAllocatorTest()
  247. {
  248. AllocatorManager.Initialize();
  249. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  250. ref var allocator = ref allocatorHelper.Allocator;
  251. allocator.Initialize();
  252. using (var container = new NativeQueue<int>(allocator.Handle))
  253. {
  254. }
  255. Assert.IsTrue(allocator.WasUsed);
  256. allocator.Dispose();
  257. allocatorHelper.Dispose();
  258. AllocatorManager.Shutdown();
  259. }
  260. [BurstCompile]
  261. struct BurstedCustomAllocatorJob : IJob
  262. {
  263. [NativeDisableUnsafePtrRestriction]
  264. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  265. public void Execute()
  266. {
  267. unsafe
  268. {
  269. using (var container = new NativeQueue<int>(Allocator->Handle))
  270. {
  271. }
  272. }
  273. }
  274. }
  275. [Test]
  276. public unsafe void NativeQueue_BurstedCustomAllocatorTest()
  277. {
  278. AllocatorManager.Initialize();
  279. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  280. ref var allocator = ref allocatorHelper.Allocator;
  281. allocator.Initialize();
  282. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  283. unsafe
  284. {
  285. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
  286. handle.Complete();
  287. }
  288. Assert.IsTrue(allocator.WasUsed);
  289. allocator.Dispose();
  290. allocatorHelper.Dispose();
  291. AllocatorManager.Shutdown();
  292. }
  293. }