Nessuna descrizione
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.

011-Span.cs 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. namespace Burst.Compiler.IL.Tests
  5. {
  6. #if UNITY_2021_2_OR_NEWER || BURST_INTERNAL
  7. /// <summary>
  8. /// Test <see cref="System.Span{T}"/>.
  9. /// </summary>
  10. internal partial class Span
  11. {
  12. [TestCompiler]
  13. public static int CreateDefault()
  14. {
  15. var span = new Span<int>();
  16. return span.Length;
  17. }
  18. [TestCompiler]
  19. public static int CreateStackalloc()
  20. {
  21. Span<int> span = stackalloc int[42];
  22. return span.Length;
  23. }
  24. [TestCompiler(42)]
  25. public static int CreateFromNullPointer(int size)
  26. {
  27. Span<double> span;
  28. unsafe
  29. {
  30. span = new Span<double>(null, size);
  31. }
  32. return span.Length;
  33. }
  34. [TestCompiler]
  35. public static unsafe double CreateFromMalloc()
  36. {
  37. double* malloc = (double*)UnsafeUtility.Malloc(UnsafeUtility.SizeOf<double>(), UnsafeUtility.AlignOf<double>(), Unity.Collections.Allocator.Persistent);
  38. *malloc = 42.0f;
  39. Span<double> span = new Span<double>(malloc, 1);
  40. double result = span[0];
  41. UnsafeUtility.Free(malloc, Unity.Collections.Allocator.Persistent);
  42. return result;
  43. }
  44. [TestCompiler]
  45. public static int GetItem()
  46. {
  47. Span<int> span = stackalloc int[42];
  48. return span[41];
  49. }
  50. [TestCompiler]
  51. public static int SetItem()
  52. {
  53. Span<int> span = stackalloc int[42];
  54. span[41] = 13;
  55. return span[41];
  56. }
  57. [TestCompiler]
  58. public static int Clear()
  59. {
  60. Span<int> span = stackalloc int[42];
  61. for (int i = 0; i < span.Length; i++)
  62. {
  63. span[i] = i;
  64. }
  65. span.Clear();
  66. int result = 0;
  67. for (int i = 0; i < span.Length; i++)
  68. {
  69. result += span[i];
  70. }
  71. return result;
  72. }
  73. [TestCompiler]
  74. public static int SliceFromStart()
  75. {
  76. Span<int> span = stackalloc int[42];
  77. for (int i = 0; i < span.Length; i++)
  78. {
  79. span[i] = i;
  80. }
  81. var newSpan = span.Slice(10);
  82. return newSpan[0] + newSpan.Length;
  83. }
  84. [TestCompiler]
  85. public static int SliceFromStartWithLength()
  86. {
  87. Span<int> span = stackalloc int[42];
  88. for (int i = 0; i < span.Length; i++)
  89. {
  90. span[i] = i;
  91. }
  92. var newSpan = span.Slice(10, 4);
  93. return newSpan[3] + newSpan.Length;
  94. }
  95. [TestCompiler]
  96. public static int CopyTo()
  97. {
  98. Span<int> span = stackalloc int[42];
  99. for (int i = 0; i < span.Length; i++)
  100. {
  101. span[i] = i;
  102. }
  103. Span<int> other = stackalloc int[4];
  104. for (int i = 0; i < other.Length; i++)
  105. {
  106. other[i] = -i - 1;
  107. }
  108. other.CopyTo(span);
  109. int result = 0;
  110. for (int i = 0; i < span.Length; i++)
  111. {
  112. result += span[i];
  113. }
  114. return result;
  115. }
  116. [TestCompiler]
  117. public static int Fill()
  118. {
  119. Span<int> span = stackalloc int[42];
  120. span.Fill(123);
  121. int result = 0;
  122. for (int i = 0; i < span.Length; i++)
  123. {
  124. result += span[i];
  125. }
  126. return result;
  127. }
  128. [TestCompiler]
  129. public static int IsEmpty() => new Span<int>().IsEmpty ? 1 : 0;
  130. [TestCompiler]
  131. public static int Empty() => Span<double>.Empty.Length;
  132. [TestCompiler]
  133. public static int GetEnumerator()
  134. {
  135. Span<int> span = stackalloc int[42];
  136. int result = 0;
  137. var enumerator = span.GetEnumerator();
  138. while (enumerator.MoveNext())
  139. {
  140. result += enumerator.Current;
  141. }
  142. return result;
  143. }
  144. [TestCompiler]
  145. public static int OperatorEquality() => new Span<double>() == Span<double>.Empty ? 1 : 0;
  146. [TestCompiler]
  147. public static int OperatorInEquality() => new Span<double>() != Span<double>.Empty ? 1 : 0;
  148. [TestCompiler]
  149. public static int OperatorImplicit()
  150. {
  151. ReadOnlySpan<double> span = new Span<double>();
  152. return span.Length;
  153. }
  154. [TestCompiler]
  155. public static int Fixed()
  156. {
  157. Span<int> span = stackalloc int[42];
  158. for (int i = 0; i < span.Length; i++)
  159. {
  160. span[i] = i;
  161. }
  162. unsafe
  163. {
  164. fixed (int* ptr = span)
  165. {
  166. *ptr = 42;
  167. return ptr[41];
  168. }
  169. }
  170. }
  171. [TestCompiler]
  172. public static int TestMemoryMarshalGetReference()
  173. {
  174. Span<int> span = stackalloc int[42];
  175. ref int x = ref MemoryMarshal.GetReference(span);
  176. return x;
  177. }
  178. }
  179. #endif
  180. }