Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

UnsafeAppendBufferTests.cs 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using NUnit.Framework;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.Collections.Tests;
  7. using Unity.Jobs;
  8. internal class UnsafeAppendBufferTests : CollectionsTestCommonBase
  9. {
  10. struct TestHeader
  11. {
  12. public int Type;
  13. public int PayloadSize;
  14. }
  15. [Test]
  16. public void UnsafeAppendBuffer_DisposeEmpty()
  17. {
  18. var buffer = new UnsafeAppendBuffer(0, 8, Allocator.Temp);
  19. Assert.False(buffer.IsCreated);
  20. Assert.True(buffer.IsEmpty);
  21. buffer.Dispose();
  22. }
  23. [Test]
  24. public void UnsafeAppendBuffer_DisposeAllocated()
  25. {
  26. var buffer = new UnsafeAppendBuffer(1, 8, Allocator.Temp);
  27. Assert.True(buffer.IsCreated);
  28. Assert.True(buffer.IsEmpty);
  29. buffer.Dispose();
  30. }
  31. [Test]
  32. unsafe public void UnsafeAppendBuffer_DisposeExternal()
  33. {
  34. var data = stackalloc int[1];
  35. var buffer = new UnsafeAppendBuffer(data, sizeof(int));
  36. buffer.Add(5);
  37. buffer.Dispose();
  38. Assert.AreEqual(5, data[0]);
  39. }
  40. [Test]
  41. public void UnsafeAppendBuffer_ThrowZeroAlignment()
  42. {
  43. Assert.Throws<ArgumentException>(() =>
  44. {
  45. var buffer = new UnsafeAppendBuffer(0, 0, Allocator.Temp);
  46. });
  47. }
  48. [Test]
  49. public unsafe void UnsafeAppendBuffer_PushHeadersWithPackets()
  50. {
  51. var buffer = new UnsafeAppendBuffer(0, 8, Allocator.Temp);
  52. var scratchPayload = stackalloc byte[1024];
  53. var expectedSize = 0;
  54. for (int i = 0; i < 1024; i++)
  55. {
  56. var packeType = i;
  57. var packetSize = i;
  58. buffer.Add(new TestHeader
  59. {
  60. Type = packeType,
  61. PayloadSize = packetSize
  62. });
  63. expectedSize += UnsafeUtility.SizeOf<TestHeader>();
  64. buffer.Add(scratchPayload, i);
  65. expectedSize += i;
  66. }
  67. Assert.True(expectedSize == buffer.Length);
  68. buffer.Dispose();
  69. }
  70. [Test]
  71. public unsafe void UnsafeAppendBuffer_ReadHeadersWithPackets()
  72. {
  73. var buffer = new UnsafeAppendBuffer(0, 8, Allocator.Temp);
  74. var scratchPayload = stackalloc byte[1024];
  75. for (int i = 0; i < 1024; i++)
  76. {
  77. var packeType = i;
  78. var packetSize = i;
  79. buffer.Add(new TestHeader
  80. {
  81. Type = packeType,
  82. PayloadSize = packetSize
  83. });
  84. UnsafeUtility.MemSet(scratchPayload, (byte)(i & 0xff), packetSize);
  85. buffer.Add(scratchPayload, i);
  86. }
  87. var reader = buffer.AsReader();
  88. for (int i = 0; i < 1024; i++)
  89. {
  90. var packetHeader = reader.ReadNext<TestHeader>();
  91. Assert.AreEqual(i, packetHeader.Type);
  92. Assert.AreEqual(i, packetHeader.PayloadSize);
  93. if (packetHeader.PayloadSize > 0)
  94. {
  95. var packetPayload = reader.ReadNext(packetHeader.PayloadSize);
  96. Assert.AreEqual((byte)(i & 0xff), *(byte*)packetPayload);
  97. }
  98. }
  99. Assert.True(reader.EndOfBuffer);
  100. buffer.Dispose();
  101. }
  102. [Test]
  103. public unsafe void UnsafeAppendBuffer_AddAndPop()
  104. {
  105. var buffer = new UnsafeAppendBuffer(0, 8, Allocator.Temp);
  106. buffer.Add<int>(123);
  107. buffer.Add<int>(234);
  108. buffer.Add<int>(345);
  109. {
  110. var array = new NativeArray<int>(3, Allocator.Temp);
  111. buffer.Pop(array.GetUnsafePtr(), 3 * UnsafeUtility.SizeOf<int>());
  112. CollectionAssert.AreEqual(new[] {123, 234, 345}, array);
  113. }
  114. {
  115. var array = new NativeArray<int>(4, Allocator.Temp);
  116. array.CopyFrom(new[] {987, 876, 765, 654});
  117. buffer.Add(array.GetUnsafePtr(), 4 * UnsafeUtility.SizeOf<int>());
  118. }
  119. Assert.AreEqual(654, buffer.Pop<int>());
  120. Assert.AreEqual(765, buffer.Pop<int>());
  121. Assert.AreEqual(876, buffer.Pop<int>());
  122. Assert.AreEqual(987, buffer.Pop<int>());
  123. buffer.Dispose();
  124. }
  125. [Test]
  126. public unsafe void UnsafeAppendBuffer_ReadNextArray()
  127. {
  128. var values = new NativeArray<int>(new[] {123, 234, 345}, Allocator.Temp);
  129. var buffer = new UnsafeAppendBuffer(0, 8, Allocator.Temp);
  130. buffer.Add(values);
  131. var array = (int*)buffer.AsReader().ReadNextArray<int>(out var count);
  132. Assert.AreEqual(values.Length, count);
  133. for (int i = 0; i < count; ++i)
  134. {
  135. Assert.AreEqual(values[i], array[i]);
  136. }
  137. values.Dispose();
  138. buffer.Dispose();
  139. }
  140. [Test]
  141. public unsafe void UnsafeAppendBuffer_DisposeJob()
  142. {
  143. var sizeOf = UnsafeUtility.SizeOf<int>();
  144. var alignOf = UnsafeUtility.AlignOf<int>();
  145. var container = new UnsafeAppendBuffer(5, 16, Allocator.Persistent);
  146. var disposeJob = container.Dispose(default);
  147. Assert.IsTrue(container.Ptr == null);
  148. disposeJob.Complete();
  149. }
  150. [Test]
  151. public void UnsafeAppendBuffer_CustomAllocatorTest()
  152. {
  153. AllocatorManager.Initialize();
  154. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  155. ref var allocator = ref allocatorHelper.Allocator;
  156. allocator.Initialize();
  157. using (var container = new UnsafeAppendBuffer(1, 1, allocator.Handle))
  158. {
  159. }
  160. Assert.IsTrue(allocator.WasUsed);
  161. allocator.Dispose();
  162. allocatorHelper.Dispose();
  163. AllocatorManager.Shutdown();
  164. }
  165. [BurstCompile]
  166. struct BurstedCustomAllocatorJob : IJob
  167. {
  168. [NativeDisableUnsafePtrRestriction]
  169. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  170. public void Execute()
  171. {
  172. unsafe
  173. {
  174. using (var container = new UnsafeAppendBuffer(1, 1, Allocator->Handle))
  175. {
  176. }
  177. }
  178. }
  179. }
  180. [Test]
  181. public unsafe void UnsafeAppendBuffer_BurstedCustomAllocatorTest()
  182. {
  183. AllocatorManager.Initialize();
  184. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  185. ref var allocator = ref allocatorHelper.Allocator;
  186. allocator.Initialize();
  187. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  188. unsafe
  189. {
  190. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
  191. handle.Complete();
  192. }
  193. Assert.IsTrue(allocator.WasUsed);
  194. allocator.Dispose();
  195. allocatorHelper.Dispose();
  196. AllocatorManager.Shutdown();
  197. }
  198. }