Geen omschrijving
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.

UnsafeQueueTests.cs 11KB

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