Açıklama Yok
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.

NativeRingQueueTests.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using NUnit.Framework;
  4. using Unity.Burst;
  5. using Unity.Collections;
  6. using Unity.Collections.Tests;
  7. using Unity.Jobs;
  8. using UnityEngine;
  9. using UnityEngine.TestTools;
  10. internal class NativeRingQueueTests
  11. {
  12. [Test]
  13. public void NativeRingQueue_UseAfterFree_UsesCustomOwnerTypeName()
  14. {
  15. var test = new NativeRingQueue<int>(1, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  16. test.Dispose();
  17. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  18. NUnit.Framework.Assert.That(() => test.Dequeue(),
  19. Throws.Exception.TypeOf<ObjectDisposedException>()
  20. .With.Message.Contains($"The {test.GetType()} has been deallocated"));
  21. #endif
  22. }
  23. [Test]
  24. public void NativeRingQueue_AtomicSafetyHandle_AllocatorTemp_UniqueStaticSafetyIds()
  25. {
  26. var test = new NativeRingQueue<int>(1, Allocator.Temp, NativeArrayOptions.ClearMemory);
  27. // All collections that use Allocator.Temp share the same core AtomicSafetyHandle.
  28. // This test verifies that containers can proceed to assign unique static safety IDs to each
  29. // AtomicSafetyHandle value, which will not be shared by other containers using Allocator.Temp.
  30. var test0 = new NativeRingQueue<int>(1, Allocator.Temp, NativeArrayOptions.ClearMemory);
  31. var test1 = new NativeRingQueue<int>(1, Allocator.Temp, NativeArrayOptions.ClearMemory);
  32. test0.Enqueue(123);
  33. test0.Dispose();
  34. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  35. NUnit.Framework.Assert.That(() => test0.Dequeue(),
  36. Throws.Exception.With.TypeOf<ObjectDisposedException>()
  37. .With.Message.Contains($"The {test0.GetType()} has been deallocated"));
  38. #endif
  39. test.Enqueue(123);
  40. test1.Dispose();
  41. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  42. NUnit.Framework.Assert.That(() => test1.Dequeue(),
  43. Throws.Exception.With.TypeOf<ObjectDisposedException>()
  44. .With.Message.Contains($"The {test1.GetType()} has been deallocated"));
  45. #endif
  46. }
  47. [BurstCompile(CompileSynchronously = true)]
  48. struct NativeRingQueueCreateAndUseAfterFreeBurst : IJob
  49. {
  50. public void Execute()
  51. {
  52. var test = new NativeRingQueue<int>(1, Allocator.Temp, NativeArrayOptions.ClearMemory);
  53. test.Enqueue(123);
  54. test.Dispose();
  55. test.Enqueue(456);
  56. }
  57. }
  58. [Test]
  59. [TestRequiresCollectionChecks]
  60. public void NativeRingQueue_CreateAndUseAfterFreeInBurstJob_UsesCustomOwnerTypeName()
  61. {
  62. var test = new NativeRingQueue<int>(1, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  63. test.Dispose();
  64. var job = new NativeRingQueueCreateAndUseAfterFreeBurst
  65. {
  66. };
  67. // Two things:
  68. // 1. This exception is logged, not thrown; thus, we use LogAssert to detect it.
  69. // 2. Calling write operation after container.Dispose() emits an unintuitive error message. For now, all this test cares about is whether it contains the
  70. // expected type name.
  71. job.Run();
  72. LogAssert.Expect(LogType.Exception,
  73. new Regex($"InvalidOperationException: The {Regex.Escape(test.GetType().ToString())} has been declared as \\[ReadOnly\\] in the job, but you are writing to it"));
  74. }
  75. struct NativeRingQueueUseInJob : IJob
  76. {
  77. public NativeRingQueue<int> Test;
  78. public void Execute()
  79. {
  80. Test.Enqueue(456);
  81. Test.Enqueue(789);
  82. }
  83. }
  84. [Test]
  85. public void NativeRingQueue_UseInJob()
  86. {
  87. var container = new NativeRingQueue<int>(10, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  88. Assert.AreEqual(0, container.Length);
  89. var job = new NativeRingQueueUseInJob
  90. {
  91. Test = container,
  92. };
  93. Assert.DoesNotThrow(() => container.Enqueue(123));
  94. Assert.AreEqual(1, container.Length);
  95. var handle = job.Schedule();
  96. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  97. Assert.Throws<InvalidOperationException>(() => container.Enqueue(321));
  98. Assert.Throws<InvalidOperationException>(() => container.TryDequeue(out _));
  99. #endif
  100. handle.Complete();
  101. Assert.AreEqual(3, container.Length);
  102. Assert.DoesNotThrow(() => container.Enqueue(987));
  103. Assert.AreEqual(4, container.Length);
  104. int item;
  105. Assert.True(container.TryDequeue(out item));
  106. Assert.AreEqual(123, item);
  107. Assert.AreEqual(3, container.Length);
  108. Assert.AreEqual(456, container.Dequeue());
  109. Assert.AreEqual(2, container.Length);
  110. Assert.AreEqual(789, container.Dequeue());
  111. Assert.AreEqual(1, container.Length);
  112. Assert.AreEqual(987, container.Dequeue());
  113. Assert.AreEqual(0, container.Length);
  114. container.Dispose();
  115. }
  116. }