コード例 #1
0
 public void ConstructorDefaultsValueToZero()
 {
     using (NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp))
     {
         Assert.That(intPtr.Value, Is.EqualTo(0));
     }
 }
コード例 #2
0
 public void ConstructorSetsInitialValue()
 {
     using (NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp, 123))
     {
         Assert.That(intPtr.Value, Is.EqualTo(123));
     }
 }
コード例 #3
0
        public void ParallelForJobCanUseParallelPtr()
        {
            using (NativeArray <int> array = new NativeArray <int>(
                       3,
                       Allocator.TempJob))
            {
                NativeArray <int> arrayCopy = array;
                arrayCopy[0] = 10;
                arrayCopy[1] = 20;
                arrayCopy[2] = 30;

                using (NativeIntPtr sum = new NativeIntPtr(
                           Allocator.TempJob))
                {
                    ParallelForTestJob job = new ParallelForTestJob
                    {
                        Array = array,
                        Sum   = sum.GetParallel()
                    };
                    job.Run(array.Length);

                    Assert.That(sum.Value, Is.EqualTo(60));
                }
            }
        }
コード例 #4
0
        public void Parse()
        {
            var    value = NativeIntPtr.Parse("343946");
            string text  = value.ToString();

            Assert.AreEqual("0x00053f8a", text);
        }
コード例 #5
0
        public void Print()
        {
            var    value = new NativeIntPtr(0x4589df);
            string text  = value.ToString();

            Assert.AreEqual("0x004589df", text);
        }
コード例 #6
0
 public void DisposeRequiresReadAccess()
 {
     using (NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp))
     {
         AssertRequiresReadOrWriteAccess(
             intPtr,
             intPtr.Dispose);
     }
 }
コード例 #7
0
        public void DisposeMakesUnusable()
        {
            NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp);

            intPtr.Dispose();
            Assert.That(
                () => intPtr.Value = 10,
                Throws.TypeOf <InvalidOperationException>());
        }
コード例 #8
0
        public void ParallelIncrementIncrementsValue()
        {
            using (NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp, 123))
            {
                NativeIntPtr.Parallel parallel = intPtr.GetParallel();
                parallel.Increment();

                Assert.That(intPtr.Value, Is.EqualTo(124));
            }
        }
コード例 #9
0
 public void SetValueRequiresReadAccess()
 {
     using (NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp))
     {
         NativeIntPtr copy = intPtr;
         AssertRequiresReadOrWriteAccess(
             intPtr,
             () => copy.Value = 123);
     }
 }
コード例 #10
0
 public void GetValueRequiresReadAccess()
 {
     using (NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp))
     {
         int value;
         AssertRequiresReadOrWriteAccess(
             intPtr,
             () => value = intPtr.Value);
     }
 }
コード例 #11
0
        public void GetValueReturnsWhatSetValueSets()
        {
            using (NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp))
            {
                NativeIntPtr copy = intPtr;
                copy.Value = 123;

                Assert.That(intPtr.Value, Is.EqualTo(123));
            }
        }
コード例 #12
0
 public void ParallelDecrementRequiresReadAccess()
 {
     using (NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp))
     {
         NativeIntPtr.Parallel parallel = intPtr.GetParallel();
         AssertRequiresReadOrWriteAccess(
             intPtr,
             parallel.Decrement);
     }
 }
コード例 #13
0
        public void IsCreatedOnlyReturnsTrueBeforeDispose()
        {
            NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp);

            Assert.That(intPtr.IsCreated, Is.True);

            intPtr.Dispose();

            Assert.That(intPtr.IsCreated, Is.False);
        }
コード例 #14
0
 public void ParallelAddRequiresReadAccess()
 {
     using (NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp))
     {
         NativeIntPtr.Parallel parallel = intPtr.GetParallel();
         AssertRequiresReadOrWriteAccess(
             intPtr,
             () => parallel.Add(10));
     }
 }
コード例 #15
0
        public void CanDeallocateOnJobCompletion()
        {
            NativeIntPtr intPtr = new NativeIntPtr(Allocator.TempJob);
            var          job    = new DeallocateOnJobCompletionJob {
                IntPtr = intPtr
            };

            job.Run();

            Assert.That(
                () => intPtr.Value = 10,
                Throws.TypeOf <InvalidOperationException>());
        }
コード例 #16
0
        public void ParallelAddOffsetsValue()
        {
            using (NativeIntPtr intPtr = new NativeIntPtr(Allocator.Temp, 123))
            {
                NativeIntPtr.Parallel parallel = intPtr.GetParallel();
                parallel.Add(5);

                Assert.That(intPtr.Value, Is.EqualTo(128));

                parallel.Add(-15);

                Assert.That(intPtr.Value, Is.EqualTo(113));
            }
        }
コード例 #17
0
 private static void AssertRequiresReadOrWriteAccess(
     NativeIntPtr intPtr,
     Action action)
 {
     intPtr.TestUseOnlySetAllowReadAndWriteAccess(false);
     try
     {
         Assert.That(
             () => action(),
             Throws.TypeOf <InvalidOperationException>());
     }
     finally
     {
         intPtr.TestUseOnlySetAllowReadAndWriteAccess(true);
     }
 }
コード例 #18
0
    // Warm up the job system

    static void WarmUpJobSystem()
    {
        // Create native collections

        NativeArray <int> sum   = new NativeArray <int>(1, Allocator.TempJob);
        NativeArray <int> array = new NativeArray <int>(
            4,
            Allocator.TempJob);
        NativeList <int> list = new NativeList <int>(
            4,
            Allocator.TempJob);
        NativeLinkedList <int> linkedList = new NativeLinkedList <int>(
            4,
            Allocator.TempJob);
        NativeChunkedList <int> chunkedList = new NativeChunkedList <int>(
            4,
            4,
            Allocator.TempJob);
        NativeHashSet <int> hashSet = new NativeHashSet <int>(
            4,
            Allocator.TempJob);
        NativeIntPtr             nativeIntPtr             = new NativeIntPtr(Allocator.TempJob);
        NativePerJobThreadIntPtr nativePerJobThreadIntPtr = new NativePerJobThreadIntPtr(
            Allocator.TempJob);

        // Create jobs

        ListAddJob listAddJob = new ListAddJob
        {
            List = list
        };
        LinkedListAddJob linkedListAddJob = new LinkedListAddJob
        {
            List = linkedList
        };
        ChunkedListAddJob chunkedListAddJob = new ChunkedListAddJob
        {
            List = chunkedList
        };
        HashSetAddJob hashSetAddJob = new HashSetAddJob
        {
            Set = hashSet
        };
        ArrayIterateJob arrayIterateJob = new ArrayIterateJob
        {
            Array = array,
            Sum   = sum
        };
        ListIterateJob listIterateJob = new ListIterateJob
        {
            List = list,
            Sum  = sum
        };
        LinkedListIterateJob linkedListIterateJob = new LinkedListIterateJob
        {
            List = linkedList,
            Sum  = sum
        };
        ChunkedListIterateJob chunkedListIterateJob = new ChunkedListIterateJob
        {
            List = chunkedList,
            Sum  = sum
        };
        ArrayIterateJobParallelFor arrayIterateJobParallelFor = new ArrayIterateJobParallelFor
        {
            Array = array,
            Sum   = sum
        };
        LinkedListIterateJobParallelFor linkedListIterateJobParallelFor = new LinkedListIterateJobParallelFor
        {
            List = linkedList,
            Sum  = sum
        };
        ChunkedListIterateJobParallelFor chunkedListIterateJobParallelFor = new ChunkedListIterateJobParallelFor
        {
            List = chunkedList,
            Sum  = sum
        };
        LinkedListInsertJob linkedListInsertJob = new LinkedListInsertJob
        {
            LinkedList = linkedList
        };
        ChunkedListInsertJob chunkedListInsertJob = new ChunkedListInsertJob
        {
            List = chunkedList
        };
        LinkedListRemoveJob linkedListRemoveJob = new LinkedListRemoveJob
        {
            List = linkedList
        };
        ChunkedListRemoveJob chunkedListRemoveJob = new ChunkedListRemoveJob
        {
            List = chunkedList
        };
        HashSetRemoveJob hashSetRemoveJob = new HashSetRemoveJob
        {
            Set = hashSet
        };
        NativeIntPtrParallelJob nativeIntPtrParallelJob = new NativeIntPtrParallelJob
        {
            Array = array,
            Sum   = nativeIntPtr.GetParallel()
        };
        NativePerJobThreadIntPtrParallelJob nativePerJobThreadIntPtrParallelJob = new NativePerJobThreadIntPtrParallelJob
        {
            Array = array,
            Sum   = nativePerJobThreadIntPtr.GetParallel()
        };

        // Run jobs

        listAddJob.Run();
        linkedListAddJob.Run();
        chunkedListAddJob.Run();
        hashSetAddJob.Run();
        arrayIterateJob.Run();
        listIterateJob.Run();
        linkedListIterateJob.Run();
        chunkedListIterateJob.Run();
        arrayIterateJobParallelFor.Run(array.Length);
        linkedListIterateJobParallelFor.Run(linkedList.Length);
        chunkedListIterateJobParallelFor.RunRanged(chunkedList.Length);
        list.Clear();
        linkedList.Clear();
        chunkedList.Clear();
        hashSet.Clear();
        linkedListInsertJob.Run();
        chunkedListInsertJob.Run();
        linkedListRemoveJob.Run();
        chunkedListRemoveJob.Run();
        hashSetRemoveJob.Run();
        nativeIntPtrParallelJob.Run(array.Length);
        nativePerJobThreadIntPtrParallelJob.Run(array.Length);

        // Dispose native collections

        sum.Dispose();
        array.Dispose();
        list.Dispose();
        linkedList.Dispose();
        chunkedList.Dispose();
        hashSet.Dispose();
        nativeIntPtr.Dispose();
        nativePerJobThreadIntPtr.Dispose();
    }
コード例 #19
0
    // Run the test

    void Start()
    {
        WarmUpJobSystem();

        const int size =
#if UNITY_EDITOR
            1000
#else
            10000
#endif
        ;

        const int chunkSize           = 1024;
        const int numElementsPerChunk = chunkSize / sizeof(int);

        // Create native collections

        NativeArray <int> sum   = new NativeArray <int>(1, Allocator.TempJob);
        NativeArray <int> array = new NativeArray <int>(
            size,
            Allocator.TempJob);
        NativeList <int> list = new NativeList <int>(
            0,
            Allocator.TempJob);
        NativeLinkedList <int> linkedList = new NativeLinkedList <int>(
            0,
            Allocator.TempJob);
        NativeChunkedList <int> chunkedList = new NativeChunkedList <int>(
            numElementsPerChunk,
            0,
            Allocator.TempJob);
        NativeHashSet <int> hashSet = new NativeHashSet <int>(
            0,
            Allocator.TempJob);
        NativeIntPtr             nativeIntPtr             = new NativeIntPtr(Allocator.TempJob);
        NativePerJobThreadIntPtr nativePerJobThreadIntPtr = new NativePerJobThreadIntPtr(
            Allocator.TempJob);

        // Run add jobs

        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

        ListAddJob listAddJob = new ListAddJob
        {
            List             = list,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        listAddJob.Run();
        long listAddTicks = sw.ElapsedTicks;

        LinkedListAddJob linkedListAddJob = new LinkedListAddJob
        {
            List             = linkedList,
            NumNodesToInsert = size
        };

        sw.Reset();
        sw.Start();
        linkedListAddJob.Run();
        long linkedListAddTicks = sw.ElapsedTicks;

        ChunkedListAddJob chunkedListAddJob = new ChunkedListAddJob
        {
            List             = chunkedList,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        chunkedListAddJob.Run();
        long chunkedListAddTicks = sw.ElapsedTicks;

        HashSetAddJob hashSetAddJob = new HashSetAddJob
        {
            Set = hashSet,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        hashSetAddJob.Run();
        long hashSetAddTicks = sw.ElapsedTicks;

        // Run iterate jobs

        ArrayIterateJob arrayIterateJob = new ArrayIterateJob
        {
            Array = array,
            Sum   = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        arrayIterateJob.Run();
        long arrayIterateTicks = sw.ElapsedTicks;

        ListIterateJob listIterateJob = new ListIterateJob
        {
            List = list,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        listIterateJob.Run();
        long listIterateTicks = sw.ElapsedTicks;

        LinkedListIterateJob linkedListIterateJob = new LinkedListIterateJob
        {
            List = linkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        linkedListIterateJob.Run();
        long linkedListIterateTicks = sw.ElapsedTicks;

        ChunkedListIterateJob chunkedListIterateJob = new ChunkedListIterateJob
        {
            List = chunkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        chunkedListIterateJob.Run();
        long chunkedListIterateTicks = sw.ElapsedTicks;

        ArrayIterateJobParallelFor arrayIterateJobParallelFor = new ArrayIterateJobParallelFor
        {
            Array = array,
            Sum   = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        arrayIterateJobParallelFor.Run(size);
        long arrayIterateParallelForTicks = sw.ElapsedTicks;

        linkedList.PrepareForParallelForJob();
        LinkedListIterateJobParallelFor linkedListIterateJobParallelFor = new LinkedListIterateJobParallelFor
        {
            List = linkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        linkedListIterateJobParallelFor.Run(size);
        long linkedListIterateParallelForTicks = sw.ElapsedTicks;

        chunkedList.PrepareForParallelForJob();
        ChunkedListIterateJobParallelFor chunkedListIterateJobParallelFor = new ChunkedListIterateJobParallelFor
        {
            List = chunkedList,
            Sum  = sum
        };

        sum[0] = 0;
        sw.Reset();
        sw.Start();
        chunkedListIterateJobParallelFor.RunRanged(size);
        long chunkedListIterateParallelForTicks = sw.ElapsedTicks;

        // Clear native collections

        list.Clear();
        linkedList.Clear();
        chunkedList.Clear();

        // Run insert jobs

        LinkedListInsertJob linkedListInsertJob = new LinkedListInsertJob
        {
            LinkedList       = linkedList,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        linkedListInsertJob.Run();
        long linkedListInsertTicks = sw.ElapsedTicks;

        ChunkedListInsertJob chunkedListInsertJob = new ChunkedListInsertJob
        {
            List             = chunkedList,
            NumElementsToAdd = size
        };

        sw.Reset();
        sw.Start();
        chunkedListInsertJob.Run();
        long chunkedListInsertTicks = sw.ElapsedTicks;

        // Run remove jobs

        LinkedListRemoveJob linkedListRemoveJob = new LinkedListRemoveJob
        {
            List = linkedList,
            NumElementsToRemove = size
        };

        sw.Reset();
        sw.Start();
        linkedListRemoveJob.Run();
        long linkedListRemoveTicks = sw.ElapsedTicks;

        ChunkedListRemoveJob chunkedListRemoveJob = new ChunkedListRemoveJob
        {
            List = chunkedList,
            NumElementsToRemove = size
        };

        sw.Reset();
        sw.Start();
        chunkedListRemoveJob.Run();
        long chunkedListRemoveTicks = sw.ElapsedTicks;

        HashSetRemoveJob hashSetRemoveJob = new HashSetRemoveJob
        {
            Set = hashSet,
            NumElementsToRemove = size
        };

        sw.Reset();
        sw.Start();
        hashSetRemoveJob.Run();
        long hashSetRemoveTicks = sw.ElapsedTicks;

        // Run NativeIntPtr and NativePerJobThreadIntPtr jobs

        NativeIntPtrParallelJob nativeIntPtrParallelJob = new NativeIntPtrParallelJob
        {
            Array = array,
            Sum   = nativeIntPtr.GetParallel()
        };

        sw.Reset();
        sw.Start();
        nativeIntPtrParallelJob.Run(size);
        long nativeIntPtrTicks = sw.ElapsedTicks;

        NativePerJobThreadIntPtrParallelJob nativePerJobThreadIntPtrParallelJob = new NativePerJobThreadIntPtrParallelJob
        {
            Array = array,
            Sum   = nativePerJobThreadIntPtr.GetParallel()
        };

        sw.Reset();
        sw.Start();
        nativePerJobThreadIntPtrParallelJob.Run(size);
        long nativePerJobThreadIntPtrTicks = sw.ElapsedTicks;

        // Report results
        Debug.Log(
            "Operation,Job Type,NativeArray,NativeList,NativeLinkedList,NativeChunkedList,NativeHashSet\n" +
            "Add,Single," + "n/a" + "," + listAddTicks + "," + linkedListAddTicks + "," + chunkedListAddTicks + "," + hashSetAddTicks + "\n" +
            "Iterate,Single," + arrayIterateTicks + "," + listIterateTicks + "," + linkedListIterateTicks + "," + chunkedListIterateTicks + "," + "n/a" + "\n" +
            "Iterate,ParallelFor," + arrayIterateParallelForTicks + "," + "n/a" + "," + linkedListIterateParallelForTicks + "," + chunkedListIterateParallelForTicks + "," + "n/a" + "\n" +
            "Insert,Single," + "n/a" + "," + "n/a" + "," + linkedListInsertTicks + "," + chunkedListInsertTicks + "," + "n/a" + "\n" +
            "Remove,Single," + "n/a" + "," + "n/a" + "," + linkedListRemoveTicks + "," + chunkedListRemoveTicks + "," + hashSetRemoveTicks);
        Debug.Log(
            "Operation,Job Type,NativeIntPtr,NativePerJobThreadIntPtr\n" +
            "Sum,ParallelFor," + nativeIntPtrTicks + "," + nativePerJobThreadIntPtrTicks);

        // Dispose native collections
        sum.Dispose();
        array.Dispose();
        list.Dispose();
        linkedList.Dispose();
        chunkedList.Dispose();
        hashSet.Dispose();
        nativeIntPtr.Dispose();
        nativePerJobThreadIntPtr.Dispose();

        // Quit
#if UNITY_EDITOR
        EditorApplication.isPlaying = false;
#else
        Application.Quit();
#endif
    }
コード例 #20
0
 public void Parse_null_field_should_throw_exception()
 {
     NativeIntPtr.Parse(null);
 }