123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- using System;
- using NUnit.Framework;
- using Unity.Burst;
- using Unity.Collections;
- using Unity.Collections.LowLevel.Unsafe;
- using Unity.Collections.Tests;
- using Unity.Jobs;
- using Assert = FastAssert;
-
- [BurstCompile]
- internal class UnsafeQueueTests : CollectionsTestCommonBase
- {
- static void ExpectedCount<T>(ref UnsafeQueue<T> container, int expected) where T : unmanaged
- {
- Assert.AreEqual(expected == 0, container.IsEmpty());
- Assert.AreEqual(expected, container.Count);
- }
-
- [Test]
- public void Enqueue_Dequeue()
- {
- var queue = new UnsafeQueue<int>(Allocator.Temp);
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- for (int i = 0; i < 16; ++i)
- queue.Enqueue(i);
- ExpectedCount(ref queue, 16);
- for (int i = 0; i < 16; ++i)
- Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- queue.Dispose();
- }
-
- [Test]
- public void ConcurrentEnqueue_Dequeue()
- {
- var queue = new UnsafeQueue<int>(Allocator.Temp);
- var cQueue = queue.AsParallelWriter();
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- for (int i = 0; i < 16; ++i)
- cQueue.Enqueue(i);
- ExpectedCount(ref queue, 16);
- for (int i = 0; i < 16; ++i)
- Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- queue.Dispose();
- }
-
- [Test]
- public void Enqueue_Dequeue_Peek()
- {
- var queue = new UnsafeQueue<int>(Allocator.Temp);
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- for (int i = 0; i < 16; ++i)
- queue.Enqueue(i);
- ExpectedCount(ref queue, 16);
- for (int i = 0; i < 16; ++i)
- {
- Assert.AreEqual(i, queue.Peek(), "Got the wrong value from the queue");
- queue.Dequeue();
- }
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- queue.Dispose();
- }
-
- [Test]
- public void Enqueue_Dequeue_Clear()
- {
- var queue = new UnsafeQueue<int>(Allocator.Temp);
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- for (int i = 0; i < 16; ++i)
- queue.Enqueue(i);
- ExpectedCount(ref queue, 16);
- for (int i = 0; i < 8; ++i)
- Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
- ExpectedCount(ref queue, 8);
- queue.Clear();
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- queue.Dispose();
- }
-
- [Test]
- public void Double_Deallocate_DoesNotThrow()
- {
- var queue = new UnsafeQueue<int>(CommonRwdAllocator.Handle);
- queue.Dispose();
- Assert.DoesNotThrow(
- () => { queue.Dispose(); });
- }
-
- [Test]
- public void EnqueueScalability()
- {
- var queue = new UnsafeQueue<int>(Allocator.Persistent);
- for (int i = 0; i != 1000 * 100; i++)
- {
- queue.Enqueue(i);
- }
-
- ExpectedCount(ref queue, 1000 * 100);
-
- for (int i = 0; i != 1000 * 100; i++)
- Assert.AreEqual(i, queue.Dequeue());
- ExpectedCount(ref queue, 0);
-
- queue.Dispose();
- }
-
- [Test]
- public void Enqueue_Wrap()
- {
- var queue = new UnsafeQueue<int>(Allocator.Temp);
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- for (int i = 0; i < 256; ++i)
- queue.Enqueue(i);
- ExpectedCount(ref queue, 256);
-
- for (int i = 0; i < 128; ++i)
- Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
- ExpectedCount(ref queue, 128);
-
- for (int i = 0; i < 128; ++i)
- queue.Enqueue(i);
- ExpectedCount(ref queue, 256);
-
- for (int i = 128; i < 256; ++i)
- Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
- ExpectedCount(ref queue, 128);
-
- for (int i = 0; i < 128; ++i)
- Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- queue.Dispose();
- }
-
- [Test]
- public void ConcurrentEnqueue_Wrap()
- {
- var queue = new UnsafeQueue<int>(Allocator.Temp);
- var cQueue = queue.AsParallelWriter();
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- for (int i = 0; i < 256; ++i)
- cQueue.Enqueue(i);
- ExpectedCount(ref queue, 256);
-
- for (int i = 0; i < 128; ++i)
- Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
- ExpectedCount(ref queue, 128);
-
- for (int i = 0; i < 128; ++i)
- cQueue.Enqueue(i);
- ExpectedCount(ref queue, 256);
-
- for (int i = 128; i < 256; ++i)
- Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
- ExpectedCount(ref queue, 128);
-
- for (int i = 0; i < 128; ++i)
- Assert.AreEqual(i, queue.Dequeue(), "Got the wrong value from the queue");
- ExpectedCount(ref queue, 0);
-
- #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
- Assert.Throws<System.InvalidOperationException>(() => {queue.Dequeue(); });
- #endif
-
- queue.Dispose();
- }
-
- [Test]
- public void TryDequeue_OnEmptyQueueWhichHadElements_RetainsValidState()
- {
- using (var queue = new UnsafeQueue<int>(Allocator.Temp))
- {
- for (int i = 0; i < 3; i++)
- {
- queue.Enqueue(i);
- Assert.AreEqual(1, queue.Count);
-
- int value;
- while (queue.TryDequeue(out value))
- {
- Assert.AreEqual(i, value);
- }
-
- Assert.AreEqual(0, queue.Count);
- }
- }
- }
-
- [Test]
- public void TryDequeue_OnEmptyQueue_RetainsValidState()
- {
- using (var queue = new UnsafeQueue<int>(Allocator.Temp))
- {
- Assert.IsFalse(queue.TryDequeue(out _));
- queue.Enqueue(1);
- Assert.AreEqual(1, queue.Count);
- }
- }
-
- [Test]
- public void ToArray_ContainsCorrectElements()
- {
- using (var queue = new UnsafeQueue<int>(Allocator.Temp))
- {
- for (int i = 0; i < 100; i++)
- queue.Enqueue(i);
- using (var array = queue.ToArray(Allocator.Temp))
- {
- Assert.AreEqual(queue.Count, array.Length);
- for (int i = 0; i < array.Length; i++)
- Assert.AreEqual(i, array[i]);
- }
- }
- }
-
- [Test]
- public void ToArray_RespectsDequeue()
- {
- using (var queue = new UnsafeQueue<int>(Allocator.Temp))
- {
- for (int i = 0; i < 100; i++)
- queue.Enqueue(i);
- for (int i = 0; i < 50; i++)
- queue.Dequeue();
- using (var array = queue.ToArray(Allocator.Temp))
- {
- Assert.AreEqual(queue.Count, array.Length);
- for (int i = 0; i < array.Length; i++)
- Assert.AreEqual(50 + i, array[i]);
- }
- }
- }
-
- [Test]
- public void UnsafeQueue_CustomAllocatorTest()
- {
- AllocatorManager.Initialize();
- var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- using (var container = new UnsafeQueue<int>(allocator.Handle))
- {
- }
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- allocatorHelper.Dispose();
- AllocatorManager.Shutdown();
- }
-
- [BurstCompile(CompileSynchronously = true)]
- struct BurstedCustomAllocatorJob : IJob
- {
- [NativeDisableUnsafePtrRestriction]
- public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
-
- public void Execute()
- {
- unsafe
- {
- using (var container = new UnsafeQueue<int>(Allocator->Handle))
- {
- }
- }
- }
- }
-
- [Test]
- public unsafe void UnsafeQueue_BurstedCustomAllocatorTest()
- {
- AllocatorManager.Initialize();
- var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
- ref var allocator = ref allocatorHelper.Allocator;
-
- allocator.Initialize();
-
- var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
- unsafe
- {
- var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
- handle.Complete();
- }
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- allocatorHelper.Dispose();
- AllocatorManager.Shutdown();
- }
-
- public struct NestedContainer
- {
- public UnsafeQueue<int> data;
- }
-
- [Test]
- public void UnsafeQueue_Nested()
- {
- var inner = new UnsafeQueue<int>(CommonRwdAllocator.Handle);
- NestedContainer nestedStruct = new NestedContainer { data = inner };
-
- var containerNestedStruct = new UnsafeQueue<NestedContainer>(CommonRwdAllocator.Handle);
- var containerNested = new UnsafeQueue<UnsafeQueue<int>>(CommonRwdAllocator.Handle);
-
- containerNested.Enqueue(inner);
- containerNestedStruct.Enqueue(nestedStruct);
-
- containerNested.Dispose();
- containerNestedStruct.Dispose();
- inner.Dispose();
- }
- }
|