Esempio n. 1
0
    public unsafe void UnsafeUtility_WriteArrayElementBoundsChecked_Performance()
    {
        const int numElements = 16 << 10;
        var       sizeOf      = UnsafeUtility.SizeOf <TestStruct>();
        var       alignOf     = UnsafeUtility.AlignOf <TestStruct>();

        var list = new UnsafeList(sizeOf, alignOf, numElements, Allocator.Persistent, NativeArrayOptions.ClearMemory);

        var test = new TestStruct {
            x = 0, y = 1, z = true
        };

        Measure.Method(() =>
        {
            for (int i = 0; i < numElements; ++i)
            {
                UnsafeUtilityExtensions.WriteArrayElementBoundsChecked(list.Ptr, i, test, numElements);
            }
        })
        .WarmupCount(100)
        .MeasurementCount(1000)
        .Run();

        list.Dispose();
    }
    public unsafe void UnsafeUtility_WriteArrayElementBoundsChecked_Works()
    {
        using (var array0 = MakeTestArray(0x12345678, 0x12345678, 0x12345678, 0x12345678, 0x12345678, 0x12345678))
        {
            var mem = (byte *)NativeArrayUnsafeUtility.GetUnsafePtr(array0);
            var len = array0.Length;

            Assert.DoesNotThrow(() => { UnsafeUtilityExtensions.WriteArrayElementBoundsChecked(mem, 5, -98765432, len); });
            Assert.Throws <IndexOutOfRangeException>(() => { UnsafeUtilityExtensions.WriteArrayElementBoundsChecked(mem, 6, -98765432, len); });
            Assert.Throws <IndexOutOfRangeException>(() => { UnsafeUtilityExtensions.WriteArrayElementBoundsChecked(mem, -1, -98765432, len); });
        }
    }