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.

053-TestConstArrays.cs 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using NUnit.Framework;
  2. using Unity.Burst;
  3. using Unity.Mathematics;
  4. namespace Burst.Compiler.IL.Tests
  5. {
  6. internal class TestConstArrays
  7. {
  8. [TestCompiler]
  9. public static int ReadFromIntArray()
  10. {
  11. return StructWithConstArray1.IntValues[1];
  12. }
  13. [TestCompiler]
  14. public static unsafe int ReadViaFixed()
  15. {
  16. fixed (int* ptr = StructWithConstArray1.IntValues)
  17. {
  18. return ptr[2];
  19. }
  20. }
  21. [TestCompiler]
  22. public static int ReadFromColorArray()
  23. {
  24. var color = StructWithConstArrayWithStruct1.Colors[1];
  25. return ((color.R * 255) + color.G) * 255 + color.B;
  26. }
  27. [TestCompiler]
  28. public static int ReadFromColorArray2()
  29. {
  30. var color = StaticArrayStruct.Colors[1];
  31. return ((color.R * 255) + color.G) * 255 + color.B;
  32. }
  33. struct StructWithConstArray1
  34. {
  35. public static readonly int[] IntValues = new int[4] { 1, 2, 3, 4 };
  36. }
  37. struct StructWithConstArrayWithStruct1
  38. {
  39. public static readonly Color[] Colors = { new Color(), new Color(1, 2, 3, 255) };
  40. }
  41. private struct Color
  42. {
  43. public Color(byte r, byte g, byte b, byte a)
  44. {
  45. R = r;
  46. G = g;
  47. B = b;
  48. A = a;
  49. }
  50. public byte R, G, B, A;
  51. }
  52. private struct StaticArrayStruct
  53. {
  54. public static readonly double[] Doubles = { 3, 6, 9, 42, 43 };
  55. public static readonly byte[] Bytes = { 1, 2, 3 };
  56. public static readonly ushort[] UShorts = { 2, 6, 8, 2, 0 };
  57. public static readonly int[] Ints = { -6, 6, 50 };
  58. public static readonly int[] ZeroData = { 0, 0, 0, 0 };
  59. public static readonly int[] ZeroLength = { };
  60. public static readonly Color[] ZeroLengthStruct = { };
  61. public static readonly Color[] Colors = { new Color(), new Color(1, 2, 3, 255) };
  62. public static readonly int3[] Positions = { new int3(0, 0, 1), new int3(0, 1, 0), new int3(1, 0, 0) };
  63. }
  64. [TestCompiler]
  65. public static int TestStaticReadonlyArrayLength()
  66. {
  67. return StaticArrayStruct.Doubles.Length + StaticArrayStruct.Bytes.Length +
  68. StaticArrayStruct.UShorts.Length + StaticArrayStruct.Ints.Length +
  69. StaticArrayStruct.ZeroData.Length + StaticArrayStruct.ZeroLength.Length +
  70. StaticArrayStruct.ZeroLengthStruct.Length + StaticArrayStruct.Colors.Length +
  71. StaticArrayStruct.Positions.Length;
  72. }
  73. private struct StructP
  74. {
  75. public static readonly int[] Value = new int[One()];
  76. public static int One()
  77. {
  78. return 1;
  79. }
  80. }
  81. [TestCompiler]
  82. public static int TestStaticReadonlyArrayNonConstantLength()
  83. {
  84. return StructP.Value.Length;
  85. }
  86. private struct StructQ
  87. {
  88. public static readonly int[] Value = new int[10];
  89. public static int One()
  90. {
  91. return 1;
  92. }
  93. static StructQ()
  94. {
  95. Value[One()] = 1;
  96. }
  97. }
  98. [TestCompiler]
  99. public static int TestStaticReadonlyArrayWithNonConstantStelemIndex()
  100. {
  101. return StructQ.Value[1];
  102. }
  103. private struct StructR
  104. {
  105. #pragma warning disable 0649
  106. public static int[] Value;
  107. #pragma warning restore 0649
  108. static StructR()
  109. {
  110. Value[0] = 1;
  111. }
  112. }
  113. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_LoadingFromManagedNonReadonlyStaticFieldNotSupported)]
  114. public static int TestStaticReadonlyArrayExplicitConstructionOfUninitialized()
  115. {
  116. return StructR.Value.Length;
  117. }
  118. private struct StructS
  119. {
  120. public static readonly int[] Value = new int[10];
  121. static StructS()
  122. {
  123. Value[0] = 1;
  124. Value[1] = 2;
  125. Value[2] = 8;
  126. Value[3] = 2;
  127. Value[4] = 0;
  128. Value[5] = 2;
  129. Value[6] = 1;
  130. Value[7] = 2;
  131. Value[8] = 2;
  132. Value[9] = 3;
  133. }
  134. }
  135. [TestCompiler]
  136. public static int TestStaticReadonlyArrayExplicitConstruction()
  137. {
  138. int sum = 0;
  139. for (int i = 0; i < 10; i++) sum += StructS.Value[i];
  140. return sum;
  141. }
  142. [TestCompiler]
  143. public static int TestStaticReadonlyArrayLdelem()
  144. {
  145. var doubles = StaticArrayStruct.Doubles[0];
  146. for (int i = 1; i < StaticArrayStruct.Doubles.Length; i++) doubles += StaticArrayStruct.Doubles[i];
  147. var bytes = StaticArrayStruct.Bytes[0];
  148. for (int i = 1; i < StaticArrayStruct.Bytes.Length; i++) bytes += StaticArrayStruct.Bytes[i];
  149. var ushorts = StaticArrayStruct.UShorts[0];
  150. for (int i = 1; i < StaticArrayStruct.UShorts.Length; i++) ushorts += StaticArrayStruct.UShorts[i];
  151. var ints = StaticArrayStruct.Ints[0];
  152. for (int i = 1; i < StaticArrayStruct.Ints.Length; i++) ints += StaticArrayStruct.Ints[i];
  153. ints += StaticArrayStruct.ZeroData[0];
  154. for (int i = 1; i < StaticArrayStruct.ZeroData.Length; i++) ints += StaticArrayStruct.ZeroData[i];
  155. for (int i = 0; i < StaticArrayStruct.ZeroLength.Length; i++) doubles += StaticArrayStruct.ZeroLength[i];
  156. bytes = (byte)(StaticArrayStruct.Colors[0].R + StaticArrayStruct.Colors[0].G
  157. + StaticArrayStruct.Colors[0].B
  158. + StaticArrayStruct.Colors[0].A);
  159. for (int i = 1; i < StaticArrayStruct.Colors.Length; i++)
  160. bytes += (byte)(StaticArrayStruct.Colors[i].R + StaticArrayStruct.Colors[i].G
  161. + StaticArrayStruct.Colors[i].B
  162. + StaticArrayStruct.Colors[i].A);
  163. for (int i = 1; i < StaticArrayStruct.Positions.Length; i++)
  164. ints += math.dot(StaticArrayStruct.Positions[i - 1], StaticArrayStruct.Positions[i]);
  165. return (int)doubles + bytes + ushorts + ints;
  166. }
  167. private static T TakesRef<T>(ref T x)
  168. {
  169. return x;
  170. }
  171. [TestCompiler]
  172. public static int TestStaticReadonlyArrayWithElementRef()
  173. {
  174. return TakesRef(ref StaticArrayStruct.Ints[1]);
  175. }
  176. [TestCompiler]
  177. public static int TestStaticReadonlyArrayWithElementVectorRef()
  178. {
  179. var x = TakesRef(ref StaticArrayStruct.Positions[1]);
  180. return math.dot(x, x);
  181. }
  182. [TestCompiler(1)]
  183. [TestCompiler(2)]
  184. [TestCompiler(3)]
  185. [TestCompiler(4)]
  186. public static int TestStaticReadonlyArrayWithDynamicLdelem(int count)
  187. {
  188. int sum = 0;
  189. for (int i = 0; i < count; i++)
  190. {
  191. sum += (int)StaticArrayStruct.Doubles[i];
  192. }
  193. return sum;
  194. }
  195. public struct ContainerStruct
  196. {
  197. public SmallStruct A;
  198. public SmallStruct B;
  199. public static readonly ContainerStruct[] CoolStructs =
  200. {
  201. new ContainerStruct
  202. {
  203. A = new SmallStruct { a = 3, b = 5 },
  204. B = new SmallStruct { a = 9, b = 10 }
  205. },
  206. new ContainerStruct
  207. {
  208. A = new SmallStruct { a = 1, b = 5 },
  209. B = new SmallStruct { a = 7, b = 8 }
  210. }
  211. };
  212. }
  213. [TestCompiler]
  214. public static int TestStaticReadonlyArrayOfStructOfStructs()
  215. {
  216. return ContainerStruct.CoolStructs[0].A.a + ContainerStruct.CoolStructs[0].A.b +
  217. ContainerStruct.CoolStructs[0].B.a + ContainerStruct.CoolStructs[0].B.b +
  218. ContainerStruct.CoolStructs[1].A.a + ContainerStruct.CoolStructs[1].A.b +
  219. ContainerStruct.CoolStructs[1].B.a + ContainerStruct.CoolStructs[1].B.b;
  220. }
  221. /* There's currently no way of settings the safety checks on from here
  222. [TestCompiler(0xFFFFFFF, ExpectedException = typeof(IndexOutOfRangeException))]
  223. public static int TestStaticReadonlyLdelemDynamicIndexOfBounds(int x)
  224. {
  225. return StaticArrayStruct.Ints[x];
  226. }
  227. */
  228. public struct SmallStruct
  229. {
  230. public int a;
  231. public int b;
  232. }
  233. public struct NullArrayHolder
  234. {
  235. public static readonly int[] Array = null;
  236. }
  237. [TestCompiler()]
  238. public static int TestStaticReadonlyNullArray()
  239. {
  240. if (NullArrayHolder.Array == null)
  241. {
  242. return 40;
  243. }
  244. return 3;
  245. }
  246. private static readonly int[] SomeArray = { 42, 13 };
  247. [TestCompiler(42)]
  248. public static int StoreNullIntoLocalArray(int x)
  249. {
  250. int[] someArray;
  251. if (x == 0)
  252. {
  253. someArray = SomeArray;
  254. }
  255. else
  256. {
  257. someArray = null;
  258. }
  259. return someArray?.Length ?? 0;
  260. }
  261. }
  262. }