暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

EditmodeTest.cs 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. using System;
  2. using System.Collections;
  3. using NUnit.Framework;
  4. using UnityEngine;
  5. using Unity.Jobs.LowLevel.Unsafe;
  6. using UnityEngine.TestTools;
  7. using Unity.Burst;
  8. using Unity.Collections;
  9. using Unity.Jobs;
  10. using System.Threading;
  11. using System.Diagnostics;
  12. using UnityEditor;
  13. using Debug = UnityEngine.Debug;
  14. using System.Text.RegularExpressions;
  15. using Unity.Profiling;
  16. using UnityEditor.Compilation;
  17. using System.IO;
  18. [TestFixture]
  19. public class EditModeTest
  20. {
  21. private const int MaxIterations = 500;
  22. [UnityTest]
  23. public IEnumerator CheckBurstJobEnabledDisabled()
  24. {
  25. BurstCompiler.Options.EnableBurstCompileSynchronously = true;
  26. try
  27. {
  28. foreach(var item in CheckBurstJobDisabled()) yield return item;
  29. foreach(var item in CheckBurstJobEnabled()) yield return item;
  30. }
  31. finally
  32. {
  33. BurstCompiler.Options.EnableBurstCompilation = true;
  34. }
  35. }
  36. private IEnumerable CheckBurstJobEnabled()
  37. {
  38. BurstCompiler.Options.EnableBurstCompilation = true;
  39. yield return null;
  40. using (var jobTester = new BurstJobTester2())
  41. {
  42. var result = jobTester.Calculate();
  43. Assert.AreNotEqual(0.0f, result);
  44. }
  45. }
  46. private IEnumerable CheckBurstJobDisabled()
  47. {
  48. BurstCompiler.Options.EnableBurstCompilation = false;
  49. yield return null;
  50. using (var jobTester = new BurstJobTester2())
  51. {
  52. var result = jobTester.Calculate();
  53. Assert.AreEqual(0.0f, result);
  54. }
  55. }
  56. [UnityTest]
  57. public IEnumerator CheckJobWithNativeArray()
  58. {
  59. BurstCompiler.Options.EnableBurstCompileSynchronously = true;
  60. BurstCompiler.Options.EnableBurstCompilation = true;
  61. yield return null;
  62. var job = new BurstJobTester2.MyJobCreatingAndDisposingNativeArray()
  63. {
  64. Length = 128,
  65. Result = new NativeArray<int>(16, Allocator.TempJob)
  66. };
  67. var handle = job.Schedule();
  68. handle.Complete();
  69. try
  70. {
  71. Assert.AreEqual(job.Length, job.Result[0]);
  72. }
  73. finally
  74. {
  75. job.Result.Dispose();
  76. }
  77. }
  78. #if UNITY_BURST_BUG_FUNCTION_POINTER_FIXED
  79. [UnityTest]
  80. public IEnumerator CheckBurstFunctionPointerException()
  81. {
  82. BurstCompiler.Options.EnableBurstCompileSynchronously = true;
  83. BurstCompiler.Options.EnableBurstCompilation = true;
  84. yield return null;
  85. using (var jobTester = new BurstJobTester())
  86. {
  87. var exception = Assert.Throws<InvalidOperationException>(() => jobTester.CheckFunctionPointer());
  88. StringAssert.Contains("Exception was thrown from a function compiled with Burst", exception.Message);
  89. }
  90. }
  91. #endif
  92. [BurstCompile(CompileSynchronously = true)]
  93. private struct HashTestJob : IJob
  94. {
  95. public NativeArray<int> Hashes;
  96. public void Execute()
  97. {
  98. Hashes[0] = BurstRuntime.GetHashCode32<int>();
  99. Hashes[1] = TypeHashWrapper.GetIntHash();
  100. Hashes[2] = BurstRuntime.GetHashCode32<TypeHashWrapper.SomeStruct<int>>();
  101. Hashes[3] = TypeHashWrapper.GetGenericHash<int>();
  102. }
  103. }
  104. [Test]
  105. public static void TestTypeHash()
  106. {
  107. HashTestJob job = new HashTestJob
  108. {
  109. Hashes = new NativeArray<int>(4, Allocator.TempJob)
  110. };
  111. job.Schedule().Complete();
  112. var hash0 = job.Hashes[0];
  113. var hash1 = job.Hashes[1];
  114. var hash2 = job.Hashes[2];
  115. var hash3 = job.Hashes[3];
  116. job.Hashes.Dispose();
  117. Assert.AreEqual(hash0, hash1, "BurstRuntime.GetHashCode32<int>() has returned two different hashes");
  118. Assert.AreEqual(hash2, hash3, "BurstRuntime.GetHashCode32<SomeStruct<int>>() has returned two different hashes");
  119. }
  120. [UnityTest]
  121. public IEnumerator CheckSafetyChecksWithDomainReload()
  122. {
  123. {
  124. var job = new SafetyCheckJobWithDomainReload();
  125. {
  126. // Run with safety-checks true
  127. BurstCompiler.Options.EnableBurstSafetyChecks = true;
  128. job.Result = new NativeArray<int>(1, Allocator.TempJob);
  129. try
  130. {
  131. var handle = job.Schedule();
  132. handle.Complete();
  133. Assert.AreEqual(2, job.Result[0]);
  134. }
  135. finally
  136. {
  137. job.Result.Dispose();
  138. }
  139. }
  140. {
  141. // Run with safety-checks false
  142. BurstCompiler.Options.EnableBurstSafetyChecks = false;
  143. job.Result = new NativeArray<int>(1, Allocator.TempJob);
  144. bool hasException = false;
  145. try
  146. {
  147. var handle = job.Schedule();
  148. handle.Complete();
  149. Assert.AreEqual(1, job.Result[0]);
  150. }
  151. catch
  152. {
  153. hasException = true;
  154. throw;
  155. }
  156. finally
  157. {
  158. job.Result.Dispose();
  159. if (hasException)
  160. {
  161. BurstCompiler.Options.EnableBurstSafetyChecks = true;
  162. }
  163. }
  164. }
  165. }
  166. // Ask for domain reload
  167. EditorUtility.RequestScriptReload();
  168. // Wait for the domain reload to be completed
  169. yield return new WaitForDomainReload();
  170. {
  171. // The safety checks should have been disabled by the previous code
  172. Assert.False(BurstCompiler.Options.EnableBurstSafetyChecks);
  173. // Restore safety checks
  174. BurstCompiler.Options.EnableBurstSafetyChecks = true;
  175. }
  176. }
  177. [UnityTest]
  178. public IEnumerator CheckConditionalAttribute()
  179. {
  180. {
  181. var job = new ConditonAttributeCheckerJob();
  182. {
  183. // Run with safety-checks true
  184. BurstCompiler.Options.EnableBurstSafetyChecks = true;
  185. job.Result = new NativeArray<int>(1, Allocator.TempJob);
  186. try
  187. {
  188. var handle = job.Schedule();
  189. handle.Complete();
  190. Assert.AreEqual(1, job.Result[0]);
  191. }
  192. finally
  193. {
  194. job.Result.Dispose();
  195. }
  196. }
  197. {
  198. // Run with safety-checks false
  199. BurstCompiler.Options.EnableBurstSafetyChecks = false;
  200. job.Result = new NativeArray<int>(1, Allocator.TempJob);
  201. bool hasException = false;
  202. try
  203. {
  204. var handle = job.Schedule();
  205. handle.Complete();
  206. Assert.AreEqual(1, job.Result[0]);
  207. }
  208. catch
  209. {
  210. hasException = true;
  211. throw;
  212. }
  213. finally
  214. {
  215. job.Result.Dispose();
  216. if (hasException)
  217. {
  218. BurstCompiler.Options.EnableBurstSafetyChecks = true;
  219. }
  220. }
  221. }
  222. }
  223. // Ask for domain reload
  224. EditorUtility.RequestScriptReload();
  225. // Wait for the domain reload to be completed
  226. yield return new WaitForDomainReload();
  227. {
  228. // The safety checks should have been disabled by the previous code
  229. Assert.False(BurstCompiler.Options.EnableBurstSafetyChecks);
  230. // Restore safety checks
  231. BurstCompiler.Options.EnableBurstSafetyChecks = true;
  232. }
  233. }
  234. [BurstCompile(CompileSynchronously = true)]
  235. private struct DebugLogJob : IJob
  236. {
  237. public int Value;
  238. public void Execute()
  239. {
  240. UnityEngine.Debug.Log($"This is a string logged from a job with burst with the following {Value}");
  241. }
  242. }
  243. [Test]
  244. public static void TestDebugLog()
  245. {
  246. var job = new DebugLogJob
  247. {
  248. Value = 256
  249. };
  250. job.Schedule().Complete();
  251. }
  252. [BurstCompile(CompileSynchronously = true, Debug = true)]
  253. struct DebugLogErrorJob : IJob
  254. {
  255. public void Execute()
  256. {
  257. UnityEngine.Debug.LogError("X");
  258. }
  259. }
  260. [UnityTest]
  261. public IEnumerator DebugLogError()
  262. {
  263. LogAssert.Expect(LogType.Error, "X");
  264. var jobData = new DebugLogErrorJob();
  265. jobData.Run();
  266. yield return null;
  267. }
  268. [BurstCompile(CompileSynchronously = true)]
  269. private struct SafetyCheckJobWithDomainReload : IJob
  270. {
  271. public NativeArray<int> Result;
  272. public void Execute()
  273. {
  274. Result[0] = 1;
  275. SetResultWithSafetyChecksOnly();
  276. }
  277. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  278. private void SetResultWithSafetyChecksOnly()
  279. {
  280. Result[0] = 2;
  281. }
  282. }
  283. [BurstCompile(CompileSynchronously = true)]
  284. private struct ConditonAttributeCheckerJob : IJob
  285. {
  286. public NativeArray<int> Result;
  287. public void Execute()
  288. {
  289. int x = 0;
  290. OnlyRunIfSafetyChecksAreOnOrFOOIsDefined(ref x);
  291. Result[0] = x;
  292. }
  293. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("FOO")]
  294. static void OnlyRunIfSafetyChecksAreOnOrFOOIsDefined(ref int x)
  295. {
  296. x += 1;
  297. }
  298. }
  299. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  300. private static void SafelySetSomeBool(ref bool b)
  301. {
  302. b = true;
  303. }
  304. [BurstCompile(DisableSafetyChecks = false)]
  305. private struct EnabledSafetyChecksJob : IJob
  306. {
  307. [WriteOnly] public NativeArray<int> WasHit;
  308. public void Execute()
  309. {
  310. var b = false;
  311. SafelySetSomeBool(ref b);
  312. WasHit[0] = b ? 1 : 0;
  313. }
  314. }
  315. [BurstCompile(DisableSafetyChecks = true)]
  316. private struct DisabledSafetyChecksJob : IJob
  317. {
  318. [WriteOnly] public NativeArray<int> WasHit;
  319. public void Execute()
  320. {
  321. var b = false;
  322. SafelySetSomeBool(ref b);
  323. WasHit[0] = b ? 1 : 0;
  324. }
  325. }
  326. [UnityTest]
  327. public IEnumerator CheckSafetyChecksOffGloballyAndOnInJob()
  328. {
  329. BurstCompiler.Options.EnableBurstSafetyChecks = false;
  330. BurstCompiler.Options.ForceEnableBurstSafetyChecks = false;
  331. yield return null;
  332. var job = new EnabledSafetyChecksJob()
  333. {
  334. WasHit = new NativeArray<int>(1, Allocator.TempJob)
  335. };
  336. job.Schedule().Complete();
  337. try
  338. {
  339. // Safety checks are off globally which overwrites the job having safety checks on.
  340. Assert.AreEqual(0, job.WasHit[0]);
  341. }
  342. finally
  343. {
  344. job.WasHit.Dispose();
  345. }
  346. }
  347. [UnityTest]
  348. public IEnumerator CheckSafetyChecksOffGloballyAndOffInJob()
  349. {
  350. BurstCompiler.Options.EnableBurstSafetyChecks = false;
  351. BurstCompiler.Options.ForceEnableBurstSafetyChecks = false;
  352. yield return null;
  353. var job = new DisabledSafetyChecksJob()
  354. {
  355. WasHit = new NativeArray<int>(1, Allocator.TempJob)
  356. };
  357. job.Schedule().Complete();
  358. try
  359. {
  360. // Safety checks are off globally and off in job.
  361. Assert.AreEqual(0, job.WasHit[0]);
  362. }
  363. finally
  364. {
  365. job.WasHit.Dispose();
  366. }
  367. }
  368. [UnityTest]
  369. public IEnumerator CheckSafetyChecksOnGloballyAndOnInJob()
  370. {
  371. BurstCompiler.Options.EnableBurstSafetyChecks = true;
  372. BurstCompiler.Options.ForceEnableBurstSafetyChecks = false;
  373. yield return null;
  374. var job = new EnabledSafetyChecksJob()
  375. {
  376. WasHit = new NativeArray<int>(1, Allocator.TempJob)
  377. };
  378. job.Schedule().Complete();
  379. try
  380. {
  381. // Safety checks are on globally and on in job.
  382. Assert.AreEqual(1, job.WasHit[0]);
  383. }
  384. finally
  385. {
  386. job.WasHit.Dispose();
  387. }
  388. }
  389. [UnityTest]
  390. public IEnumerator CheckSafetyChecksOnGloballyAndOffInJob()
  391. {
  392. BurstCompiler.Options.EnableBurstSafetyChecks = true;
  393. BurstCompiler.Options.ForceEnableBurstSafetyChecks = false;
  394. yield return null;
  395. var job = new DisabledSafetyChecksJob()
  396. {
  397. WasHit = new NativeArray<int>(1, Allocator.TempJob)
  398. };
  399. job.Schedule().Complete();
  400. try
  401. {
  402. // Safety checks are on globally but off in job.
  403. Assert.AreEqual(0, job.WasHit[0]);
  404. }
  405. finally
  406. {
  407. job.WasHit.Dispose();
  408. }
  409. }
  410. [UnityTest]
  411. public IEnumerator CheckForceSafetyChecksWorks()
  412. {
  413. BurstCompiler.Options.ForceEnableBurstSafetyChecks = true;
  414. yield return null;
  415. var job = new DisabledSafetyChecksJob()
  416. {
  417. WasHit = new NativeArray<int>(1, Allocator.TempJob)
  418. };
  419. job.Schedule().Complete();
  420. try
  421. {
  422. // Even though the job has set disabled safety checks, the menu item 'Force On'
  423. // has been set which overrides any other requested behaviour.
  424. Assert.AreEqual(1, job.WasHit[0]);
  425. }
  426. finally
  427. {
  428. job.WasHit.Dispose();
  429. }
  430. }
  431. [UnityTest]
  432. public IEnumerator CheckSharedStaticWithDomainReload()
  433. {
  434. // Check that on a first access, SharedStatic is always empty
  435. AssertTestSharedStaticEmpty();
  436. // Fill with some data
  437. TestSharedStatic.SharedValue.Data = new TestSharedStatic(1, 2, 3, 4);
  438. Assert.AreEqual(1, TestSharedStatic.SharedValue.Data.Value1);
  439. Assert.AreEqual(2, TestSharedStatic.SharedValue.Data.Value2);
  440. Assert.AreEqual(3, TestSharedStatic.SharedValue.Data.Value3);
  441. Assert.AreEqual(4, TestSharedStatic.SharedValue.Data.Value4);
  442. // Ask for domain reload
  443. EditorUtility.RequestScriptReload();
  444. // Wait for the domain reload to be completed
  445. yield return new WaitForDomainReload();
  446. // Make sure that after a domain reload everything is initialized back to zero
  447. AssertTestSharedStaticEmpty();
  448. }
  449. private static void AssertTestSharedStaticEmpty()
  450. {
  451. Assert.AreEqual(0, TestSharedStatic.SharedValue.Data.Value1);
  452. Assert.AreEqual(0, TestSharedStatic.SharedValue.Data.Value2);
  453. Assert.AreEqual(0, TestSharedStatic.SharedValue.Data.Value3);
  454. Assert.AreEqual(0, TestSharedStatic.SharedValue.Data.Value4);
  455. }
  456. private struct TestSharedStatic
  457. {
  458. public static readonly SharedStatic<TestSharedStatic> SharedValue = SharedStatic<TestSharedStatic>.GetOrCreate<TestSharedStatic>();
  459. public TestSharedStatic(int value1, long value2, long value3, long value4)
  460. {
  461. Value1 = value1;
  462. Value2 = value2;
  463. Value3 = value3;
  464. Value4 = value4;
  465. }
  466. public int Value1;
  467. public long Value2;
  468. public long Value3;
  469. public long Value4;
  470. }
  471. static EditModeTest()
  472. {
  473. // UnityEngine.Debug.Log("Domain Reload");
  474. }
  475. [BurstCompile]
  476. private static class FunctionPointers
  477. {
  478. public delegate int SafetyChecksDelegate();
  479. [BurstCompile(DisableSafetyChecks = false)]
  480. public static int WithSafetyChecksEnabled()
  481. {
  482. var b = false;
  483. SafelySetSomeBool(ref b);
  484. return b ? 1 : 0;
  485. }
  486. [BurstCompile(DisableSafetyChecks = true)]
  487. public static int WithSafetyChecksDisabled()
  488. {
  489. var b = false;
  490. SafelySetSomeBool(ref b);
  491. return b ? 1 : 0;
  492. }
  493. }
  494. [UnityTest]
  495. public IEnumerator CheckSafetyChecksOffGloballyAndOffInFunctionPointer()
  496. {
  497. BurstCompiler.Options.EnableBurstSafetyChecks = false;
  498. BurstCompiler.Options.ForceEnableBurstSafetyChecks = false;
  499. yield return null;
  500. var funcPtr = BurstCompiler.CompileFunctionPointer<FunctionPointers.SafetyChecksDelegate>(FunctionPointers.WithSafetyChecksDisabled);
  501. // Safety Checks are off globally and off in the job.
  502. Assert.AreEqual(0, funcPtr.Invoke());
  503. }
  504. [UnityTest]
  505. public IEnumerator CheckSafetyChecksOffGloballyAndOnInFunctionPointer()
  506. {
  507. BurstCompiler.Options.EnableBurstSafetyChecks = false;
  508. BurstCompiler.Options.ForceEnableBurstSafetyChecks = false;
  509. yield return null;
  510. var funcPtr = BurstCompiler.CompileFunctionPointer<FunctionPointers.SafetyChecksDelegate>(FunctionPointers.WithSafetyChecksEnabled);
  511. // Safety Checks are off globally and on in job, but the global setting takes precedence.
  512. Assert.AreEqual(0, funcPtr.Invoke());
  513. }
  514. [UnityTest]
  515. public IEnumerator CheckSafetyChecksOnGloballyAndOffInFunctionPointer()
  516. {
  517. BurstCompiler.Options.EnableBurstSafetyChecks = true;
  518. BurstCompiler.Options.ForceEnableBurstSafetyChecks = false;
  519. yield return null;
  520. var funcPtr = BurstCompiler.CompileFunctionPointer<FunctionPointers.SafetyChecksDelegate>(FunctionPointers.WithSafetyChecksDisabled);
  521. // Safety Checks are on globally and off in the job, so the job takes predence.
  522. Assert.AreEqual(0, funcPtr.Invoke());
  523. }
  524. [UnityTest]
  525. public IEnumerator CheckSafetyChecksOnGloballyAndOnInFunctionPointer()
  526. {
  527. BurstCompiler.Options.EnableBurstSafetyChecks = true;
  528. BurstCompiler.Options.ForceEnableBurstSafetyChecks = false;
  529. yield return null;
  530. var funcPtr = BurstCompiler.CompileFunctionPointer<FunctionPointers.SafetyChecksDelegate>(FunctionPointers.WithSafetyChecksEnabled);
  531. // Safety Checks are on globally and on in the job.
  532. Assert.AreEqual(1, funcPtr.Invoke());
  533. }
  534. [UnityTest]
  535. public IEnumerator CheckFunctionPointerForceSafetyChecksWorks()
  536. {
  537. BurstCompiler.Options.ForceEnableBurstSafetyChecks = true;
  538. yield return null;
  539. var funcPtr = BurstCompiler.CompileFunctionPointer<FunctionPointers.SafetyChecksDelegate>(FunctionPointers.WithSafetyChecksDisabled);
  540. // Even though the job has set disabled safety checks, the menu item 'Force On'
  541. // has been set which overrides any other requested behaviour.
  542. Assert.AreEqual(1, funcPtr.Invoke());
  543. }
  544. [BurstCompile(CompileSynchronously = true)]
  545. private struct DebugDrawLineJob : IJob
  546. {
  547. public void Execute()
  548. {
  549. Debug.DrawLine(new Vector3(0, 0, 0), new Vector3(5, 0, 0), Color.green);
  550. }
  551. }
  552. [Test]
  553. public void TestDebugDrawLine()
  554. {
  555. var job = new DebugDrawLineJob();
  556. job.Schedule().Complete();
  557. }
  558. [BurstCompile]
  559. private static class ProfilerMarkerWrapper
  560. {
  561. private static readonly ProfilerMarker StaticMarker = new ProfilerMarker("TestStaticBurst");
  562. [BurstCompile(CompileSynchronously = true)]
  563. public static int CreateAndUseProfilerMarker(int start)
  564. {
  565. using (StaticMarker.Auto())
  566. {
  567. var p = new ProfilerMarker("TestBurst");
  568. p.Begin();
  569. var result = 0;
  570. for (var i = start; i < start + 100000; i++)
  571. {
  572. result += i;
  573. }
  574. p.End();
  575. return result;
  576. }
  577. }
  578. }
  579. private delegate int IntReturnIntDelegate(int param);
  580. [Test]
  581. public void TestCreateProfilerMarker()
  582. {
  583. var fp = BurstCompiler.CompileFunctionPointer<IntReturnIntDelegate>(ProfilerMarkerWrapper.CreateAndUseProfilerMarker);
  584. fp.Invoke(5);
  585. }
  586. [BurstCompile]
  587. private static class EnsureAssemblyBuilderDoesNotInvalidFunctionPointers
  588. {
  589. [BurstDiscard]
  590. private static void MessOnManaged(ref int x) => x = 42;
  591. [BurstCompile(CompileSynchronously = true)]
  592. public static int WithBurst()
  593. {
  594. int x = 13;
  595. MessOnManaged(ref x);
  596. return x;
  597. }
  598. }
  599. #if !UNITY_2023_1_OR_NEWER
  600. [Test]
  601. public void TestAssemblyBuilder()
  602. {
  603. var preBuilder = EnsureAssemblyBuilderDoesNotInvalidFunctionPointers.WithBurst();
  604. Assert.AreEqual(13, preBuilder);
  605. var tempDirectory = Path.GetTempPath();
  606. var script = Path.Combine(tempDirectory, "BurstGeneratedAssembly.cs");
  607. File.WriteAllText(script, @"
  608. using Unity.Burst;
  609. namespace BurstGeneratedAssembly
  610. {
  611. [BurstCompile]
  612. public static class MyStuff
  613. {
  614. [BurstCompile(CompileSynchronously = true)]
  615. public static int BurstedFunction(int x) => x + 1;
  616. }
  617. }
  618. ");
  619. var dll = Path.Combine(tempDirectory, "BurstGeneratedAssembly.dll");
  620. var builder = new AssemblyBuilder(dll, script);
  621. Assert.IsTrue(builder.Build());
  622. // Busy wait for the build to be done.
  623. while (builder.status != AssemblyBuilderStatus.Finished)
  624. {
  625. Assert.AreEqual(preBuilder, EnsureAssemblyBuilderDoesNotInvalidFunctionPointers.WithBurst());
  626. Thread.Sleep(10);
  627. }
  628. Assert.AreEqual(preBuilder, EnsureAssemblyBuilderDoesNotInvalidFunctionPointers.WithBurst());
  629. }
  630. #endif
  631. [UnityTest]
  632. public IEnumerator CheckChangingScriptOptimizationMode()
  633. {
  634. static void CheckBurstIsEnabled()
  635. {
  636. using (var jobTester = new BurstJobTester2())
  637. {
  638. var result = jobTester.Calculate();
  639. Assert.AreNotEqual(0.0f, result);
  640. }
  641. }
  642. CheckBurstIsEnabled();
  643. // Switch scripting code optimization mode from Release to Debug.
  644. Assert.AreEqual(CodeOptimization.Release, CompilationPipeline.codeOptimization);
  645. CompilationPipeline.codeOptimization = CodeOptimization.Debug;
  646. // Wait for the domain reload to be completed
  647. yield return new WaitForDomainReload();
  648. CheckBurstIsEnabled();
  649. // Set scripting code optimization mode back to Release.
  650. CompilationPipeline.codeOptimization = CodeOptimization.Release;
  651. // Wait for the domain reload to be completed
  652. yield return new WaitForDomainReload();
  653. CheckBurstIsEnabled();
  654. }
  655. }