Ejemplo n.º 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();
    }
Ejemplo n.º 2
0
    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); });
        }
    }
Ejemplo n.º 3
0
    public unsafe void UnsafeUtility_MemSwap_DoesThrow_Overlapped()
    {
        using (var array0 = MakeTestArray(0x12345678, 0x12345678, 0x12345678, 0x12345678, 0x12345678, 0x12345678))
        {
            var mem = (byte *)NativeArrayUnsafeUtility.GetUnsafePtr(array0);
            var len = array0.Length * UnsafeUtility.SizeOf <int>();

            Assert.DoesNotThrow(() => { UnsafeUtilityExtensions.MemSwap(mem + 10, mem, 10); });
            Assert.Throws <InvalidOperationException>(() => { UnsafeUtilityExtensions.MemSwap(mem + 10, mem, len - 10); });

            Assert.DoesNotThrow(() => { UnsafeUtilityExtensions.MemSwap(mem, mem + 10, 10); });
            Assert.Throws <InvalidOperationException>(() => { UnsafeUtilityExtensions.MemSwap(mem, mem + 10, len - 10); });
        }
    }
Ejemplo n.º 4
0
    public unsafe void UnsafeUtility_MemSwap()
    {
        using (var array0 = MakeTestArray(0x12345678, 0x12345678, 0x12345678, 0x12345678, 0x12345678, 0x12345678))
            using (var array1 = MakeTestArray(0x21436587, 0x21436587, 0x21436587, 0x21436587, 0x21436587, 0x21436587))
            {
                UnsafeUtilityExtensions.MemSwap(NativeArrayUnsafeUtility.GetUnsafePtr(array0), NativeArrayUnsafeUtility.GetUnsafePtr(array1), array0.Length * UnsafeUtility.SizeOf <int>());

                foreach (var b in array0)
                {
                    Assert.AreEqual(0x21436587, b);
                }
                foreach (var b in array1)
                {
                    Assert.AreEqual(0x12345678, b);
                }
            }
    }
Ejemplo n.º 5
0
    public unsafe void UnsafeUtility_WriteArrayElementBoundsChecked_Performance()
    {
        const int numElements = 16 << 10;

        var list = new UnsafeList <TestStruct>(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();
    }