Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PlaymodeTest.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System.Collections;
  2. using NUnit.Framework;
  3. using Unity.Burst;
  4. using UnityEngine;
  5. using Unity.Jobs.LowLevel.Unsafe;
  6. using UnityEngine.TestTools;
  7. using System;
  8. using Unity.Jobs;
  9. [TestFixture]
  10. public class PlaymodeTest
  11. {
  12. // [UnityTest]
  13. public IEnumerator CheckBurstJobEnabledDisabled()
  14. {
  15. BurstCompiler.Options.EnableBurstCompileSynchronously = true;
  16. #if UNITY_2019_3_OR_NEWER
  17. foreach(var item in CheckBurstJobDisabled()) yield return item;
  18. #endif
  19. foreach(var item in CheckBurstJobEnabled()) yield return item;
  20. }
  21. private IEnumerable CheckBurstJobEnabled()
  22. {
  23. BurstCompiler.Options.EnableBurstCompilation = true;
  24. yield return null;
  25. using (var jobTester = new BurstJobTester2())
  26. {
  27. var result = jobTester.Calculate();
  28. Assert.AreNotEqual(0.0f, result);
  29. }
  30. }
  31. private IEnumerable CheckBurstJobDisabled()
  32. {
  33. BurstCompiler.Options.EnableBurstCompilation = false;
  34. yield return null;
  35. using (var jobTester = new BurstJobTester2())
  36. {
  37. var result = jobTester.Calculate();
  38. Assert.AreEqual(0.0f, result);
  39. }
  40. }
  41. [BurstCompile(CompileSynchronously = true)]
  42. private struct ThrowingJob : IJob
  43. {
  44. public int I;
  45. public void Execute()
  46. {
  47. if (I < 0)
  48. {
  49. throw new System.Exception("Some Exception!");
  50. }
  51. }
  52. }
  53. [Test]
  54. public void NoSafetyCheckExceptionWarningInEditor()
  55. {
  56. var job = new ThrowingJob { I = 42 };
  57. job.Schedule().Complete();
  58. // UNITY_BURST_DEBUG enables additional logging which messes with our check.
  59. if (null == System.Environment.GetEnvironmentVariable("UNITY_BURST_DEBUG"))
  60. {
  61. LogAssert.NoUnexpectedReceived();
  62. }
  63. }
  64. #if UNITY_2019_3_OR_NEWER
  65. [BurstCompile]
  66. public struct SomeFunctionPointers
  67. {
  68. [BurstDiscard]
  69. private static void MessWith(ref int a) => a += 13;
  70. [BurstCompile]
  71. public static int A(int a, int b)
  72. {
  73. MessWith(ref a);
  74. return a + b;
  75. }
  76. [BurstCompile(DisableDirectCall = true)]
  77. public static int B(int a, int b)
  78. {
  79. MessWith(ref a);
  80. return a - b;
  81. }
  82. [BurstCompile(CompileSynchronously = true)]
  83. public static int C(int a, int b)
  84. {
  85. MessWith(ref a);
  86. return a * b;
  87. }
  88. [BurstCompile(CompileSynchronously = true, DisableDirectCall = true)]
  89. public static int D(int a, int b)
  90. {
  91. MessWith(ref a);
  92. return a / b;
  93. }
  94. public delegate int Delegate(int a, int b);
  95. }
  96. [Test]
  97. public void TestDirectCalls()
  98. {
  99. Assert.IsTrue(BurstCompiler.IsEnabled);
  100. // a can either be (42 + 13) + 53 or 42 + 53 (depending on whether it was burst compiled).
  101. var a = SomeFunctionPointers.A(42, 53);
  102. Assert.IsTrue((a == ((42 + 13) + 53)) || (a == (42 + 53)));
  103. // b can only be (42 + 13) - 53, because direct call is disabled and so we always call the managed method.
  104. var b = SomeFunctionPointers.B(42, 53);
  105. Assert.AreEqual((42 + 13) - 53, b);
  106. // c can only be 42 * 53, because synchronous compilation is enabled.
  107. var c = SomeFunctionPointers.C(42, 53);
  108. Assert.AreEqual(42 * 53, c);
  109. // d can only be (42 + 13) / 53, because even though synchronous compilation is enabled, direct call is disabled.
  110. var d = SomeFunctionPointers.D(42, 53);
  111. Assert.AreEqual((42 + 13) / 53, d);
  112. }
  113. [Test]
  114. public void TestDirectCallInNamespacedClass()
  115. {
  116. void onCompileILPPMethod()
  117. {
  118. Assert.Fail("BurstCompiler.CompileILPPMethod should not have been called at this time");
  119. }
  120. // We expect BurstCompiler.CompileILPPMethod2 to have been called at startup, via
  121. // [InitializeOnLoad] or [RuntimeInitializeOnLoadMethod]. If it's called when we invoke
  122. // N.C.A(), then something has gone wrong.
  123. try
  124. {
  125. BurstCompiler.OnCompileILPPMethod += onCompileILPPMethod;
  126. var result = N.C.A();
  127. Assert.AreEqual(42, result);
  128. }
  129. finally
  130. {
  131. BurstCompiler.OnCompileILPPMethod -= onCompileILPPMethod;
  132. }
  133. }
  134. [Test]
  135. public void TestFunctionPointers()
  136. {
  137. Assert.IsTrue(BurstCompiler.IsEnabled);
  138. var A = BurstCompiler.CompileFunctionPointer<SomeFunctionPointers.Delegate>(SomeFunctionPointers.A);
  139. var B = BurstCompiler.CompileFunctionPointer<SomeFunctionPointers.Delegate>(SomeFunctionPointers.B);
  140. var C = BurstCompiler.CompileFunctionPointer<SomeFunctionPointers.Delegate>(SomeFunctionPointers.C);
  141. var D = BurstCompiler.CompileFunctionPointer<SomeFunctionPointers.Delegate>(SomeFunctionPointers.D);
  142. // a can either be (42 + 13) + 53 or 42 + 53 (depending on whether it was burst compiled).
  143. var a = A.Invoke(42, 53);
  144. Assert.IsTrue((a == ((42 + 13) + 53)) || (a == (42 + 53)));
  145. // b can either be (42 + 13) - 53 or 42 - 53 (depending on whether it was burst compiled).
  146. var b = B.Invoke(42, 53);
  147. Assert.IsTrue((b == ((42 + 13) - 53)) || (b == (42 - 53)));
  148. // c can only be 42 * 53, because synchronous compilation is enabled.
  149. var c = C.Invoke(42, 53);
  150. Assert.AreEqual(42 * 53, c);
  151. // d can only be 42 / 53, because synchronous compilation is enabled.
  152. var d = D.Invoke(42, 53);
  153. Assert.AreEqual(42 / 53, d);
  154. }
  155. [BurstCompile]
  156. public static class GenericClass<T>
  157. {
  158. [BurstCompile]
  159. public static int ConcreteMethod() => 3;
  160. }
  161. public delegate int NoArgsIntReturnDelegate();
  162. [Test]
  163. public void TestGenericClassConcreteMethodFunctionPointer()
  164. {
  165. Assert.IsTrue(BurstCompiler.IsEnabled);
  166. var F = BurstCompiler.CompileFunctionPointer<NoArgsIntReturnDelegate>(GenericClass<int>.ConcreteMethod);
  167. Assert.AreEqual(3, F.Invoke());
  168. }
  169. #endif
  170. }
  171. #if UNITY_2019_3_OR_NEWER
  172. // This test class is intentionally in a namespace to ensure that our
  173. // direct-call [RuntimeInitializeOnLoadMethod] works correctly in that
  174. // scenario.
  175. namespace N
  176. {
  177. [BurstCompile]
  178. internal static class C
  179. {
  180. public static int A() => B();
  181. [BurstCompile(CompileSynchronously = true)]
  182. private static int B()
  183. {
  184. var x = 42;
  185. DiscardedMethod(ref x);
  186. return x;
  187. }
  188. [BurstDiscard]
  189. private static void DiscardedMethod(ref int x)
  190. {
  191. x += 1;
  192. }
  193. }
  194. }
  195. #endif