暫無描述
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-ReadOnlySpan.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.ReadOnlySpan{T}"/>.
  8. /// </summary>
  9. internal partial class ReadOnlySpan
  10. {
  11. [TestCompiler]
  12. public static int CreateDefault()
  13. {
  14. var span = new ReadOnlySpan<int>();
  15. return span.Length;
  16. }
  17. [TestCompiler]
  18. public static int CreateStackalloc()
  19. {
  20. ReadOnlySpan<int> span = stackalloc int[42];
  21. return span.Length;
  22. }
  23. [TestCompiler(42)]
  24. public static int CreateFromNullPointer(int size)
  25. {
  26. ReadOnlySpan<double> span;
  27. unsafe
  28. {
  29. span = new ReadOnlySpan<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. var span = new ReadOnlySpan<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. ReadOnlySpan<int> span = stackalloc int[42];
  47. return span[41];
  48. }
  49. [TestCompiler]
  50. public static int SliceFromStart()
  51. {
  52. ReadOnlySpan<int> span = stackalloc int[42];
  53. var newSpan = span.Slice(10);
  54. return newSpan[0] + newSpan.Length;
  55. }
  56. [TestCompiler]
  57. public static int SliceFromStartWithLength()
  58. {
  59. ReadOnlySpan<int> span = stackalloc int[42];
  60. var newSpan = span.Slice(10, 4);
  61. return newSpan[3] + newSpan.Length;
  62. }
  63. [TestCompiler]
  64. public static int CopyTo()
  65. {
  66. Span<int> span = stackalloc int[42];
  67. for (int i = 0; i < span.Length; i++)
  68. {
  69. span[i] = i;
  70. }
  71. ReadOnlySpan<int> other = stackalloc int[4];
  72. other.CopyTo(span);
  73. int result = 0;
  74. for (int i = 0; i < span.Length; i++)
  75. {
  76. result += span[i];
  77. }
  78. return result;
  79. }
  80. [TestCompiler]
  81. public static int IsEmpty() => new ReadOnlySpan<int>().IsEmpty ? 1 : 0;
  82. [TestCompiler]
  83. public static int Empty() => ReadOnlySpan<double>.Empty.Length;
  84. [TestCompiler]
  85. public static int GetEnumerator()
  86. {
  87. ReadOnlySpan<int> span = stackalloc int[42];
  88. int result = 0;
  89. var enumerator = span.GetEnumerator();
  90. while (enumerator.MoveNext())
  91. {
  92. result += enumerator.Current;
  93. }
  94. return result;
  95. }
  96. [TestCompiler]
  97. public static int OperatorEquality() => new ReadOnlySpan<double>() == ReadOnlySpan<double>.Empty ? 1 : 0;
  98. [TestCompiler]
  99. public static int OperatorInEquality() => new ReadOnlySpan<double>() != ReadOnlySpan<double>.Empty ? 1 : 0;
  100. [TestCompiler]
  101. public static int Fixed()
  102. {
  103. Span<int> span = stackalloc int[42];
  104. for (int i = 0; i < span.Length; i++)
  105. {
  106. span[i] = i;
  107. }
  108. ReadOnlySpan<int> readOnlySpan = span;
  109. unsafe
  110. {
  111. fixed (int* ptr = readOnlySpan)
  112. {
  113. return ptr[41];
  114. }
  115. }
  116. }
  117. }
  118. #endif
  119. }