暫無描述
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.

UnsafeRingQueueTests.cs 4.6KB

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