No Description
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.0KB

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