Brak opisu
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.

TestCompilerAttribute.cs 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. using NUnit.Framework.Internal.Builders;
  7. using Unity.Burst;
  8. using UnityEngine;
  9. namespace Burst.Compiler.IL.Tests
  10. {
  11. internal class TestCompilerAttribute : TestCompilerAttributeBase
  12. {
  13. public TestCompilerAttribute(params object[] arguments) : base(arguments)
  14. {
  15. }
  16. protected override bool IsCommandLine()
  17. {
  18. return false;
  19. }
  20. protected override bool IsMono()
  21. {
  22. return true;
  23. }
  24. protected override bool SupportException {
  25. get {
  26. // We don't support exception in Unity editor
  27. return false;
  28. }
  29. }
  30. protected override TestCompilerCommandBase GetTestCommand(TestCompilerAttributeBase attribute, Test test, TestMethod originalMethod)
  31. {
  32. return new TestCompilerCommand(attribute, test, originalMethod);
  33. }
  34. public class TestCompilerCommand : TestCompilerCommandBase
  35. {
  36. public TestCompilerCommand(TestCompilerAttributeBase attribute, Test test, TestMethod originalMethod) : base(attribute, test, originalMethod)
  37. {
  38. }
  39. protected override int GetRunCount()
  40. {
  41. return 1;
  42. }
  43. protected override void CompleteTest(ITestExecutionContext context)
  44. {
  45. context.CurrentResult.SetResult(ResultState.Success);
  46. }
  47. protected override int GetULP()
  48. {
  49. return 512;
  50. }
  51. protected override object[] GetArgumentsArray(TestMethod method)
  52. {
  53. return method.parms.Arguments.ToArray();
  54. }
  55. protected unsafe Delegate CompileDelegate(ITestExecutionContext context, MethodInfo methodInfo,
  56. Type delegateType, byte* returnBox, out Type returnBoxType) {
  57. return CompileDelegate(context, methodInfo, delegateType, returnBox, out returnBoxType, out _);
  58. }
  59. protected unsafe override Delegate CompileDelegate(ITestExecutionContext context, MethodInfo methodInfo,
  60. Type delegateType, byte* returnBox, out Type returnBoxType,
  61. out Delegate interpretDelegate)
  62. {
  63. interpretDelegate = null;
  64. returnBoxType = null;
  65. var functionDelegate = Delegate.CreateDelegate(delegateType, methodInfo);
  66. var compiledFunction = BurstCompiler.CompileDelegate(functionDelegate);
  67. return compiledFunction;
  68. }
  69. protected override void GoldFileTestForOtherPlatforms(MethodInfo methodInfo, bool testWasRun)
  70. {
  71. // This is a no-op here.
  72. }
  73. protected override bool TestOnCurrentHostEnvironment(MethodInfo methodInfo)
  74. {
  75. // Query architecture via burst to avoid mono bug in detecting processor architecture
  76. if (BurstCompiler.IsHostEditorArm())
  77. return !methodInfo.CustomAttributes.Any((e) => e.AttributeType.Name == "TestCpuAttribute");
  78. return true;
  79. }
  80. protected override object CompileFunctionPointer(MethodInfo methodInfo, Type functionType)
  81. {
  82. throw new NotImplementedException();
  83. }
  84. protected override bool InterpretMethod(Delegate interpretDelegate, MethodInfo methodInfo, object[] args, Type returnType, out string reason, out object result) {
  85. reason = null;
  86. result = null;
  87. return false;
  88. }
  89. protected override void Setup()
  90. {
  91. }
  92. protected override TestResult HandleCompilerException(ITestExecutionContext context, MethodInfo methodInfo)
  93. {
  94. var arguments = GetArgumentsArray(_originalMethod);
  95. Type[] nativeArgTypes = new Type[arguments.Length];
  96. for (var i = 0; i < arguments.Length; ++i)
  97. {
  98. nativeArgTypes[i] = arguments[i].GetType();
  99. }
  100. bool isInRegistry;
  101. Func<object, object[], object> caller;
  102. var delegateType = CreateNativeDelegateType(methodInfo.ReturnType, nativeArgTypes, out isInRegistry, out caller);
  103. var functionDelegate = Delegate.CreateDelegate(delegateType, methodInfo);
  104. Delegate compiledFunction = BurstCompiler.CompileDelegate(functionDelegate);
  105. if (functionDelegate == compiledFunction)
  106. context.CurrentResult.SetResult(ResultState.Success);
  107. else
  108. context.CurrentResult.SetResult(ResultState.Failure, $"The function have been compiled successfully, but an error was expected.");
  109. return context.CurrentResult;
  110. }
  111. string cachedLog = null;
  112. bool logPresent = false;
  113. protected override string GetLinesFromDeterminismLog()
  114. {
  115. if (cachedLog==null)
  116. {
  117. try
  118. {
  119. TextAsset txtData = (TextAsset)Resources.Load("btests_deterministic");
  120. cachedLog = txtData.text;
  121. logPresent = true;
  122. }
  123. catch
  124. {
  125. logPresent = false;
  126. }
  127. }
  128. return cachedLog;
  129. }
  130. protected override bool IsDeterministicTest(TestMethod method)
  131. {
  132. return logPresent && (method.Method.ReturnType.IsType(typeof(double)) ||
  133. method.Method.ReturnType.IsType(typeof(float)));
  134. }
  135. protected override TestCompilerBaseExtensions GetExtension()
  136. {
  137. return null;
  138. }
  139. protected override bool TargetIs32Bit()
  140. {
  141. return false;
  142. }
  143. }
  144. }
  145. }