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.

BurstReflectionTests.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using NUnit.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using Unity.Burst;
  5. using Unity.Burst.Editor;
  6. using Unity.Jobs;
  7. // This concrete generic type is only referenced in this assembly-level attribute,
  8. // not anywhere else in code. This is to test that such types can be picked up
  9. // by BurstReflection.
  10. [assembly: BurstReflectionTests.RegisterGenericJobType(typeof(BurstReflectionTests.GenericParallelForJob<int>))]
  11. [TestFixture]
  12. public class BurstReflectionTests
  13. {
  14. private List<System.Reflection.Assembly> _assemblies;
  15. [OneTimeSetUp]
  16. public void SetUp()
  17. {
  18. _assemblies = BurstReflection.EditorAssembliesThatCanPossiblyContainJobs;
  19. }
  20. [Test]
  21. public void CanGetAssemblyList()
  22. {
  23. Assert.That(_assemblies, Has.Count.GreaterThan(0));
  24. }
  25. [Test]
  26. [TestCase("BurstReflectionTests.MyJob - (IJob)")]
  27. [TestCase("BurstReflectionTests.MyGenericJob`1[System.Int32] - (IJob)")]
  28. [TestCase("BurstReflectionTests.MyGenericJob2`1[System.Int32] - (BurstReflectionTests.IMyGenericJob`1[System.Int32])")]
  29. [TestCase("BurstReflectionTests.MyGenericJob2`1[System.Double] - (BurstReflectionTests.IMyGenericJob`1[System.Double])")]
  30. [TestCase("BurstReflectionTests.NonGenericType.TestMethod1()")]
  31. [TestCase("BurstReflectionTests.GenericType`1[System.Int32].TestMethod1()")]
  32. [TestCase("BurstReflectionTests.GenericType`1.NestedNonGeneric[System.Int32].TestMethod2()")]
  33. [TestCase("BurstReflectionTests.GenericType`1.NestedGeneric`1[System.Int32,System.Single].TestMethod3()")]
  34. [TestCase("BurstReflectionTests.MyGenericJobSeparateAssembly`1[System.Int32] - (BurstReflectionTestsSeparateAssembly.IMyGenericJobSeparateAssembly`1[System.Int32])")]
  35. [TestCase("BurstReflectionTests.GenericParallelForJob`1[System.Int32] - (IJobParallelFor)")]
  36. public void CanFindJobType(string compileTargetName)
  37. {
  38. var result = BurstReflection.FindExecuteMethods(_assemblies, BurstReflectionAssemblyOptions.None);
  39. Assert.That(result.LogMessages, Is.Empty);
  40. var compileTarget = result.CompileTargets.Find(x => x.GetDisplayName() == compileTargetName);
  41. Assert.That(compileTarget, Is.Not.Null);
  42. }
  43. [BurstCompile]
  44. private struct MyJob : IJob
  45. {
  46. public void Execute() { }
  47. }
  48. [BurstCompile]
  49. private struct MyGenericJob<T> : IJob
  50. {
  51. public void Execute() { }
  52. private static void UseConcreteType()
  53. {
  54. new MyGenericJob<int>().Execute();
  55. }
  56. }
  57. [Unity.Jobs.LowLevel.Unsafe.JobProducerType(typeof(MyJobProducer<,>))]
  58. private interface IMyGenericJob<T>
  59. {
  60. void Execute();
  61. }
  62. [BurstCompile]
  63. private struct MyGenericJob2<T> : IMyGenericJob<T>
  64. {
  65. public void Execute() { }
  66. private static void UseConcreteType()
  67. {
  68. new MyGenericJob2<int>().Execute();
  69. }
  70. }
  71. private static class MyJobProducer<TJob, T>
  72. {
  73. public static void Execute(ref TJob job)
  74. {
  75. }
  76. }
  77. private struct MyGenericJob2Wrapper<T1, T2>
  78. {
  79. public MyGenericJob2<T2> Job;
  80. private static void UseConcreteType()
  81. {
  82. var x = new MyGenericJob2Wrapper<float, double>();
  83. x.Job.Execute();
  84. }
  85. }
  86. [BurstCompile]
  87. private struct NonGenericType
  88. {
  89. [BurstCompile]
  90. public static void TestMethod1() { }
  91. }
  92. [BurstCompile]
  93. private struct GenericType<T>
  94. {
  95. public static Action Delegate1;
  96. public static Action Delegate2;
  97. public static Action Delegate3;
  98. [BurstCompile]
  99. public static void TestMethod1() { }
  100. [BurstCompile]
  101. public class NestedNonGeneric
  102. {
  103. [BurstCompile]
  104. public static void TestMethod2() { }
  105. }
  106. [BurstCompile]
  107. public class NestedGeneric<T2>
  108. {
  109. [BurstCompile]
  110. public static void TestMethod3() { }
  111. }
  112. private static void UseConcreteType()
  113. {
  114. // Store the delegates to static fields to avoid
  115. // them being optimized-away in Release builds
  116. Delegate1 = GenericType<int>.TestMethod1;
  117. Delegate2 = GenericType<int>.NestedNonGeneric.TestMethod2;
  118. Delegate3 = GenericType<int>.NestedGeneric<float>.TestMethod3;
  119. }
  120. }
  121. [BurstCompile]
  122. private struct MyGenericJobSeparateAssembly<T> : BurstReflectionTestsSeparateAssembly.IMyGenericJobSeparateAssembly<T>
  123. {
  124. public void Execute() { }
  125. private static void UseConcreteType()
  126. {
  127. new MyGenericJobSeparateAssembly<int>().Execute();
  128. }
  129. }
  130. [Test]
  131. [TestCase("BurstReflectionTests.GenericMethodContainer.GenericMethod(T)")]
  132. public void ExcludesGenericMethods(string compileTargetName)
  133. {
  134. var result = BurstReflection.FindExecuteMethods(_assemblies, BurstReflectionAssemblyOptions.None);
  135. Assert.That(result.LogMessages, Is.Empty);
  136. var compileTarget = result.CompileTargets.Find(x => x.GetDisplayName() == compileTargetName);
  137. Assert.That(compileTarget, Is.Null);
  138. }
  139. [BurstCompile]
  140. private static class GenericMethodContainer
  141. {
  142. [BurstCompile]
  143. private static void GenericMethod<T>(T p)
  144. {
  145. }
  146. }
  147. [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
  148. internal class RegisterGenericJobTypeAttribute : Attribute
  149. {
  150. public Type ConcreteType;
  151. public RegisterGenericJobTypeAttribute(Type type)
  152. {
  153. ConcreteType = type;
  154. }
  155. }
  156. [BurstCompile]
  157. internal struct GenericParallelForJob<T> : IJobParallelFor
  158. where T : struct
  159. {
  160. public void Execute(int index)
  161. {
  162. throw new System.NotImplementedException();
  163. }
  164. }
  165. }