using System; using NUnit.Framework; using Unity.Burst; using Unity.Collections; using Unity.Collections.Tests; using Unity.Jobs; using Unity.Collections.LowLevel.Unsafe; internal class NativeArrayTests : CollectionsTestFixture { [Test] public unsafe void NativeArray_IndexOf_Int() { var container = new NativeArray(10, Allocator.Persistent); container[0] = 123; container[1] = 789; bool r0 = false, r1 = false, r2 = false; GCAllocRecorder.ValidateNoGCAllocs(() => { r0 = -1 != container.IndexOf(456); r1 = container.Contains(123); r2 = container.Contains(789); }); Assert.False(r0); Assert.False(-1 != NativeArrayExtensions.IndexOf(container.GetUnsafePtr(), container.Length, 456)); Assert.True(r1); Assert.True(NativeArrayExtensions.Contains(container.GetUnsafePtr(), container.Length, 123)); Assert.True(r2); Assert.True(NativeArrayExtensions.Contains(container.GetUnsafePtr(), container.Length, 789)); container.Dispose(); } [Test] public unsafe void NativeArray_IndexOf_FixedString128() { var container = new NativeArray(10, Allocator.Persistent); container[0] = new FixedString128Bytes("123"); container[1] = new FixedString128Bytes("789"); bool r0 = false, r1 = false, r2 = false; GCAllocRecorder.ValidateNoGCAllocs(() => { r0 = -1 != container.IndexOf(new FixedString128Bytes("456")); r1 = container.Contains(new FixedString128Bytes("123")); r2 = container.Contains(new FixedString128Bytes("789")); }); Assert.False(r0); Assert.False(-1 != NativeArrayExtensions.IndexOf(container.GetUnsafePtr(), container.Length, new FixedString128Bytes("456"))); Assert.True(r1); Assert.True(NativeArrayExtensions.Contains(container.GetUnsafePtr(), container.Length, new FixedString128Bytes("123"))); Assert.True(r2); Assert.True(NativeArrayExtensions.Contains(container.GetUnsafePtr(), container.Length, new FixedString128Bytes("789"))); container.Dispose(); } [Test] public void NativeArray_DisposeJob() { var container = new NativeArray(1, Allocator.Persistent); Assert.True(container.IsCreated); Assert.DoesNotThrow(() => { container[0] = 1; }); var disposeJob = container.Dispose(default); Assert.False(container.IsCreated); Assert.Throws( () => { container[0] = 2; }); disposeJob.Complete(); } [BurstCompile(CompileSynchronously = true)] struct NativeArrayPokeJob : IJob { NativeArray array; public NativeArrayPokeJob(NativeArray array) { this.array = array; } public void Execute() { array[0] = 1; } } [Test] public void NativeArray_DisposeJobWithMissingDependencyThrows() { var array = new NativeArray(1, Allocator.Persistent); var deps = new NativeArrayPokeJob(array).Schedule(); Assert.Throws(() => { array.Dispose(default); }); deps.Complete(); array.Dispose(); } [Test] public void NativeArray_DisposeJobCantBeScheduled() { var array = new NativeArray(1, Allocator.Persistent); var deps = array.Dispose(default); Assert.Throws(() => { new NativeArrayPokeJob(array).Schedule(deps); }); deps.Complete(); } }