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

UnsafeStreamTests.cs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using NUnit.Framework;
  2. using System;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.Collections.Tests;
  7. using Unity.Jobs;
  8. internal class UnsafeStreamTests : CollectionsTestCommonBase
  9. {
  10. [Test]
  11. public void UnsafeStream_CustomAllocatorTest()
  12. {
  13. AllocatorManager.Initialize();
  14. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  15. ref var allocator = ref allocatorHelper.Allocator;
  16. allocator.Initialize();
  17. using (var container = new UnsafeStream(1, allocator.Handle))
  18. {
  19. }
  20. Assert.IsTrue(allocator.WasUsed);
  21. allocator.Dispose();
  22. allocatorHelper.Dispose();
  23. AllocatorManager.Shutdown();
  24. }
  25. [BurstCompile]
  26. struct BurstedCustomAllocatorJob : IJob
  27. {
  28. [NativeDisableUnsafePtrRestriction]
  29. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  30. public void Execute()
  31. {
  32. unsafe
  33. {
  34. using (var container = new UnsafeStream(1, Allocator->Handle))
  35. {
  36. }
  37. }
  38. }
  39. }
  40. [Test]
  41. public unsafe void UnsafeStream_BurstedCustomAllocatorTest()
  42. {
  43. AllocatorManager.Initialize();
  44. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  45. ref var allocator = ref allocatorHelper.Allocator;
  46. allocator.Initialize();
  47. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  48. unsafe
  49. {
  50. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
  51. handle.Complete();
  52. }
  53. Assert.IsTrue(allocator.WasUsed);
  54. allocator.Dispose();
  55. allocatorHelper.Dispose();
  56. AllocatorManager.Shutdown();
  57. }
  58. [Test]
  59. public void UnsafeStream_ScheduleCreate_NativeList()
  60. {
  61. var container = new NativeList<int>(Allocator.Persistent);
  62. container.Add(13);
  63. container.Add(13);
  64. container.Add(13);
  65. container.Add(13);
  66. UnsafeStream stream;
  67. var jobHandle = UnsafeStream.ScheduleConstruct(out stream, container, default, CommonRwdAllocator.Handle);
  68. jobHandle.Complete();
  69. Assert.AreEqual(4, stream.ForEachCount);
  70. stream.Dispose();
  71. container.Dispose();
  72. }
  73. [Test]
  74. public void UnsafeStream_ScheduleCreate_NativeArray()
  75. {
  76. var container = new NativeArray<int>(1, Allocator.Persistent);
  77. container[0] = 4;
  78. UnsafeStream stream;
  79. var jobHandle = UnsafeStream.ScheduleConstruct(out stream, container, default, CommonRwdAllocator.Handle);
  80. jobHandle.Complete();
  81. Assert.AreEqual(4, stream.ForEachCount);
  82. stream.Dispose();
  83. container.Dispose();
  84. }
  85. }