Няма описание
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.1KB

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