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