설명 없음
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.

NativeReferenceTests.cs 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. using NUnit.Framework;
  2. using System;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.Collections.Tests;
  7. using Unity.Jobs;
  8. class NativeReferenceTests : CollectionsTestCommonBase
  9. {
  10. [Test]
  11. public void NativeReference_AllocateDeallocate_ReadWrite()
  12. {
  13. var reference = new NativeReference<int>(Allocator.Persistent);
  14. reference.Value = 1;
  15. Assert.That(reference.Value, Is.EqualTo(1));
  16. reference.Dispose();
  17. }
  18. [Test]
  19. public void NativeReference_CopyFrom()
  20. {
  21. var referenceA = new NativeReference<TestData>(Allocator.Persistent);
  22. var referenceB = new NativeReference<TestData>(Allocator.Persistent);
  23. referenceA.Value = new TestData { Integer = 42, Float = 3.1416f };
  24. referenceB.CopyFrom(referenceA);
  25. Assert.That(referenceB.Value, Is.EqualTo(referenceA.Value));
  26. referenceA.Dispose();
  27. referenceB.Dispose();
  28. }
  29. [Test]
  30. public void NativeReference_CopyTo()
  31. {
  32. var referenceA = new NativeReference<TestData>(Allocator.Persistent);
  33. var referenceB = new NativeReference<TestData>(Allocator.Persistent);
  34. referenceA.Value = new TestData { Integer = 42, Float = 3.1416f };
  35. referenceA.CopyTo(referenceB);
  36. Assert.That(referenceB.Value, Is.EqualTo(referenceA.Value));
  37. referenceA.Dispose();
  38. referenceB.Dispose();
  39. }
  40. [Test]
  41. [TestRequiresCollectionChecks]
  42. public void NativeReference_NullThrows()
  43. {
  44. var reference = new NativeReference<int>();
  45. Assert.Throws<NullReferenceException>(() => reference.Value = 5);
  46. }
  47. [Test]
  48. public void NativeReference_CopiedIsKeptInSync()
  49. {
  50. var reference = new NativeReference<int>(Allocator.Persistent);
  51. var referenceCopy = reference;
  52. reference.Value = 42;
  53. Assert.That(reference.Value, Is.EqualTo(referenceCopy.Value));
  54. reference.Dispose();
  55. }
  56. struct TestData
  57. {
  58. public int Integer;
  59. public float Float;
  60. }
  61. [BurstCompile(CompileSynchronously = true)]
  62. struct TempNativeReferenceInJob : IJob
  63. {
  64. public NativeReference<int> Output;
  65. public void Execute()
  66. {
  67. var reference = new NativeReference<int>(Allocator.Temp);
  68. reference.Value = 42;
  69. Output.Value = reference.Value;
  70. reference.Dispose();
  71. }
  72. }
  73. [Test]
  74. public void NativeReference_TempInBurstJob()
  75. {
  76. var job = new TempNativeReferenceInJob() { Output = new NativeReference<int>(CommonRwdAllocator.Handle) };
  77. job.Schedule().Complete();
  78. Assert.That(job.Output.Value, Is.EqualTo(42));
  79. job.Output.Dispose();
  80. }
  81. [Test]
  82. public unsafe void NativeReference_UnsafePtr()
  83. {
  84. var reference = new NativeReference<int>(CommonRwdAllocator.Handle);
  85. var job = new TempNativeReferenceInJob() { Output = reference };
  86. var jobHandle = job.Schedule();
  87. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  88. Assert.Throws<InvalidOperationException>(() => reference.GetUnsafePtr());
  89. Assert.Throws<InvalidOperationException>(() => reference.GetUnsafeReadOnlyPtr());
  90. #endif
  91. Assert.DoesNotThrow(() => reference.GetUnsafePtrWithoutChecks());
  92. jobHandle.Complete();
  93. Assert.AreEqual(*reference.GetUnsafePtr(), 42);
  94. Assert.AreEqual(*reference.GetUnsafeReadOnlyPtr(), 42);
  95. Assert.AreEqual(*reference.GetUnsafePtrWithoutChecks(), 42);
  96. Assert.That(job.Output.Value, Is.EqualTo(42));
  97. job.Output.Dispose();
  98. }
  99. [Test]
  100. public void NativeReference_DisposeJob()
  101. {
  102. var reference = new NativeReference<int>(Allocator.Persistent);
  103. Assert.That(reference.IsCreated, Is.True);
  104. Assert.DoesNotThrow(() => reference.Value = 99);
  105. var disposeJob = reference.Dispose(default);
  106. Assert.That(reference.IsCreated, Is.False);
  107. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  108. Assert.Throws<ObjectDisposedException>(() => reference.Value = 3);
  109. #endif
  110. disposeJob.Complete();
  111. }
  112. [Test]
  113. public void NativeReference_NoGCAllocations()
  114. {
  115. var reference = new NativeReference<int>(Allocator.Persistent);
  116. GCAllocRecorder.ValidateNoGCAllocs(() =>
  117. {
  118. reference.Value = 1;
  119. reference.Value++;
  120. });
  121. Assert.That(reference.Value, Is.EqualTo(2));
  122. reference.Dispose();
  123. }
  124. [Test]
  125. public void NativeReference_Equals()
  126. {
  127. var referenceA = new NativeReference<int>(12345, Allocator.Persistent);
  128. var referenceB = new NativeReference<int>(Allocator.Persistent) { Value = 12345 };
  129. Assert.That(referenceA, Is.EqualTo(referenceB));
  130. referenceB.Value = 54321;
  131. Assert.AreNotEqual(referenceA, referenceB);
  132. referenceA.Dispose();
  133. referenceB.Dispose();
  134. }
  135. [Test]
  136. public void NativeReference_ReadOnly()
  137. {
  138. var referenceA = new NativeReference<int>(12345, Allocator.Persistent);
  139. var referenceB = new NativeReference<int>(Allocator.Persistent) { Value = 12345 };
  140. var referenceARO = referenceA.AsReadOnly();
  141. Assert.AreEqual(referenceARO.Value, referenceB.Value);
  142. referenceA.Dispose();
  143. referenceB.Dispose();
  144. }
  145. [Test]
  146. public void NativeReference_GetHashCode()
  147. {
  148. var integer = 42;
  149. var reference = new NativeReference<int>(integer, Allocator.Persistent);
  150. Assert.That(reference.GetHashCode(), Is.EqualTo(integer.GetHashCode()));
  151. reference.Dispose();
  152. }
  153. [Test]
  154. public void NativeReference_CustomAllocatorTest()
  155. {
  156. AllocatorManager.Initialize();
  157. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  158. ref var allocator = ref allocatorHelper.Allocator;
  159. allocator.Initialize();
  160. using (var container = new NativeReference<int>(allocator.Handle))
  161. {
  162. }
  163. Assert.IsTrue(allocator.WasUsed);
  164. allocator.Dispose();
  165. allocatorHelper.Dispose();
  166. AllocatorManager.Shutdown();
  167. }
  168. [BurstCompile]
  169. struct BurstedCustomAllocatorJob : IJob
  170. {
  171. [NativeDisableUnsafePtrRestriction]
  172. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  173. public void Execute()
  174. {
  175. unsafe
  176. {
  177. using (var container = new NativeReference<int>(Allocator->Handle))
  178. {
  179. }
  180. }
  181. }
  182. }
  183. [Test]
  184. public unsafe void NativeReference_BurstedCustomAllocatorTest()
  185. {
  186. AllocatorManager.Initialize();
  187. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  188. ref var allocator = ref allocatorHelper.Allocator;
  189. allocator.Initialize();
  190. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  191. unsafe
  192. {
  193. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
  194. handle.Complete();
  195. }
  196. Assert.IsTrue(allocator.WasUsed);
  197. allocator.Dispose();
  198. allocatorHelper.Dispose();
  199. AllocatorManager.Shutdown();
  200. }
  201. public struct NestedContainer
  202. {
  203. public NativeReference<int> data;
  204. }
  205. [Test]
  206. public void NativeReference_Nested()
  207. {
  208. var inner = new NativeReference<int>(CommonRwdAllocator.Handle);
  209. NestedContainer nestedStruct = new NestedContainer { data = inner };
  210. var containerNestedStruct = new NativeReference<NestedContainer>(CommonRwdAllocator.Handle);
  211. var containerNested = new NativeReference<NativeReference<int>>(CommonRwdAllocator.Handle);
  212. containerNested.Value = inner;
  213. containerNestedStruct.Value = nestedStruct;
  214. containerNested.Dispose();
  215. containerNestedStruct.Dispose();
  216. inner.Dispose();
  217. }
  218. struct NestedContainerJob : IJob
  219. {
  220. public NativeReference<NativeReference<int>> nestedContainer;
  221. public void Execute()
  222. {
  223. nestedContainer.Value = default;
  224. }
  225. }
  226. [Test]
  227. [TestRequiresCollectionChecks]
  228. public void NativeReference_NestedJob_Error()
  229. {
  230. var container = new NativeReference<NativeReference<int>>(CommonRwdAllocator.Handle);
  231. var nestedJob = new NestedContainerJob
  232. {
  233. nestedContainer = container
  234. };
  235. JobHandle job = default;
  236. Assert.Throws<System.InvalidOperationException>(() => { job = nestedJob.Schedule(); });
  237. job.Complete();
  238. container.Dispose();
  239. }
  240. }