Beispiel #1
0
    public unsafe void UnsafeUtility_ReadArrayElementBoundsChecked_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);

        for (int i = 0; i < numElements; ++i)
        {
            list.Add(new TestStruct {
                x = i, y = (short)(i + 1), z = true
            });
        }

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

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

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