A game about forced loneliness, made by TACStudios
at master 146 lines 4.7 kB view raw
1using System; 2using NUnit.Framework; 3using Unity.Burst; 4using Unity.Collections; 5using Unity.Collections.LowLevel.Unsafe; 6using Unity.Jobs; 7 8namespace Unity.Collections.Tests 9{ 10 internal class UnsafeRingQueueTests 11 { 12 [Test] 13 public void UnsafeRingQueue_Enqueue_Dequeue() 14 { 15 var test = new UnsafeRingQueue<int>(16, Allocator.Persistent); 16 17 Assert.AreEqual(0, test.Length); 18 19 int item; 20 Assert.False(test.TryDequeue(out item)); 21 22 test.Enqueue(123); 23 Assert.AreEqual(1, test.Length); 24 25 Assert.True(test.TryEnqueue(456)); 26 Assert.AreEqual(2, test.Length); 27 28 Assert.True(test.TryDequeue(out item)); 29 Assert.AreEqual(123, item); 30 Assert.AreEqual(1, test.Length); 31 32 Assert.AreEqual(456, test.Dequeue()); 33 Assert.AreEqual(0, test.Length); 34 35 test.Dispose(); 36 } 37 38 [Test] 39 public unsafe void UnsafeRingQueue_Enqueue_Dequeue_View() 40 { 41 var list = new UnsafeList<int>(16, Allocator.Persistent); 42 list.Length = 16; 43 var test = new UnsafeRingQueue<int>(list.Ptr, list.Length); 44 45 Assert.AreEqual(0, test.Length); 46 47 int item; 48 Assert.False(test.TryDequeue(out item)); 49 50 test.Enqueue(123); 51 Assert.AreEqual(1, test.Length); 52 53 Assert.True(test.TryEnqueue(456)); 54 Assert.AreEqual(2, test.Length); 55 56 Assert.True(test.TryDequeue(out item)); 57 Assert.AreEqual(123, item); 58 Assert.AreEqual(1, test.Length); 59 60 Assert.AreEqual(456, test.Dequeue()); 61 Assert.AreEqual(0, test.Length); 62 63 test.Dispose(); 64 list.Dispose(); 65 } 66 67 [Test] 68 [TestRequiresDotsDebugOrCollectionChecks] 69 public void UnsafeRingQueue_Throws() 70 { 71 using (var test = new UnsafeRingQueue<int>(1, Allocator.Persistent)) 72 { 73 Assert.Throws<InvalidOperationException>(() => { test.Dequeue(); }); 74 75 Assert.DoesNotThrow(() => { test.Enqueue(123); }); 76 Assert.Throws<InvalidOperationException>(() => { test.Enqueue(456); }); 77 78 int item = 0; 79 Assert.DoesNotThrow(() => { item = test.Dequeue(); }); 80 Assert.AreEqual(123, item); 81 82 Assert.DoesNotThrow(() => { test.Enqueue(456); }); 83 Assert.DoesNotThrow(() => { item = test.Dequeue(); }); 84 Assert.AreEqual(456, item); 85 86 Assert.Throws<InvalidOperationException>(() => { test.Dequeue(); }); 87 } 88 } 89 90 [Test] 91 public void UnsafeRingQueue_CustomAllocatorTest() 92 { 93 AllocatorManager.Initialize(); 94 var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent); 95 ref var allocator = ref allocatorHelper.Allocator; 96 allocator.Initialize(); 97 98 using (var container = new UnsafeRingQueue<int>(1, allocator.Handle)) 99 { 100 } 101 102 Assert.IsTrue(allocator.WasUsed); 103 allocator.Dispose(); 104 allocatorHelper.Dispose(); 105 AllocatorManager.Shutdown(); 106 } 107 108 [BurstCompile] 109 struct BurstedCustomAllocatorJob : IJob 110 { 111 [NativeDisableUnsafePtrRestriction] 112 public unsafe CustomAllocatorTests.CountingAllocator* Allocator; 113 114 public void Execute() 115 { 116 unsafe 117 { 118 using (var container = new UnsafeRingQueue<int>(1, Allocator->Handle)) 119 { 120 } 121 } 122 } 123 } 124 125 [Test] 126 public unsafe void UnsafeRingQueue_BurstedCustomAllocatorTest() 127 { 128 AllocatorManager.Initialize(); 129 var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent); 130 ref var allocator = ref allocatorHelper.Allocator; 131 allocator.Initialize(); 132 133 var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator); 134 unsafe 135 { 136 var handle = new BurstedCustomAllocatorJob { Allocator = allocatorPtr }.Schedule(); 137 handle.Complete(); 138 } 139 140 Assert.IsTrue(allocator.WasUsed); 141 allocator.Dispose(); 142 allocatorHelper.Dispose(); 143 AllocatorManager.Shutdown(); 144 } 145 } 146}