public static unsafe void GetAsciiSpanFromSBytePointerTest()
    {
        Assert.That(() => MarshalUtilities.GetAsciiSpan(null, -1).ToArray(),
                    Is.EqualTo(Array.Empty <sbyte>())
                    );

        fixed(sbyte *source = Array.Empty <sbyte>())
        {
            var pSource = source;

            Assert.That(() => MarshalUtilities.GetAsciiSpan(pSource, -1).ToArray(),
                        Is.EqualTo(Array.Empty <sbyte>())
                        );
        }

        fixed(sbyte *source = new sbyte[] { (sbyte)'A', (sbyte)'B', (sbyte)'C' })
        {
            var pSource = source;

            Assert.That(() => MarshalUtilities.GetAsciiSpan(pSource, -1).ToArray(),
                        Is.EqualTo(new sbyte[] { (sbyte)'A', (sbyte)'B', (sbyte)'C' })
                        );

            Assert.That(() => MarshalUtilities.GetAsciiSpan(pSource, 2).ToArray(),
                        Is.EqualTo(new sbyte[] { (sbyte)'A', (sbyte)'B' })
                        );
        }
    }
    public static unsafe void GetUtf16SpanFromSBytePointerTest()
    {
        Assert.That(() => MarshalUtilities.GetUtf16Span(null, -1).ToArray(),
                    Is.EqualTo(Array.Empty <ushort>())
                    );

        fixed(ushort *source = Array.Empty <ushort>())
        {
            var pSource = source;

            Assert.That(() => MarshalUtilities.GetUtf16Span(pSource, -1).ToArray(),
                        Is.EqualTo(Array.Empty <ushort>())
                        );
        }

        fixed(ushort *source = new ushort[] { 'A', 'B', 'C' })
        {
            var pSource = source;

            Assert.That(() => MarshalUtilities.GetUtf16Span(pSource, -1).ToArray(),
                        Is.EqualTo(new ushort[] { 'A', 'B', 'C' })
                        );

            Assert.That(() => MarshalUtilities.GetUtf16Span(pSource, 2).ToArray(),
                        Is.EqualTo(new ushort[] { 'A', 'B' })
                        );
        }
    }
Exemplo n.º 3
0
        /// <summary>
        /// Instantiates a spline from an individual OBJ file which contains a set of vertices representing the individual points of the spline.
        /// </summary>
        /// <param name="path">An OBJ file containing the points of the spline as vertices.</param>
        public static Spline *MakeSpline(string path)
        {
            // Store newly created Spline
            Spline spline = new Spline();

            // Parse individual spline file.
            ObjParser parser = new ObjParser(path);

            // Spline Information
            spline.Enabler          = 1;
            spline.SplineType       = parser.GetSplineType();
            spline.NumberOfVertices = (ushort)parser.Vertices.Count;

            // Get vertices & calculate total length
            SplineVertex[] vertices = MakeSplineVertices(ref parser);
            spline.TotalSplineLength = vertices.Sum(x => x.DistanceToNextVertex);

            // Write vertices to unmanaged memory
            int    structSize            = Marshal.SizeOf(vertices[0]) * vertices.Length;
            IntPtr splineVerticesPointer = Marshal.AllocHGlobal(structSize);

            MarshalUtilities.StructureArrayToPointer(vertices, splineVerticesPointer);
            spline.VertexList = (SplineVertex *)splineVerticesPointer;

            // Write spline to unmanaged memory
            int    splineSize    = Marshal.SizeOf(spline);
            IntPtr splinePointer = Marshal.AllocHGlobal(splineSize);

            Marshal.StructureToPtr(spline, splinePointer, true);

            return((Spline *)splinePointer);
        }
    public static void GetAsciiSpanFromStringTest()
    {
        Assert.That(() => MarshalUtilities.GetAsciiSpan(null).ToArray(),
                    Is.EqualTo(Array.Empty <sbyte>())
                    );

        Assert.That(() => MarshalUtilities.GetAsciiSpan(string.Empty).ToArray(),
                    Is.EqualTo(Array.Empty <sbyte>())
                    );

        Assert.That(() => MarshalUtilities.GetAsciiSpan("ABC").ToArray(),
                    Is.EqualTo(new sbyte[] { (sbyte)'A', (sbyte)'B', (sbyte)'C' })
                    );
    }
    public static void GetStringReadOnlySpanUInt16Test()
    {
        Assert.That(() => MarshalUtilities.GetString(default(ushort[])),
                    Is.Null
                    );

        Assert.That(() => MarshalUtilities.GetString(Array.Empty <ushort>()),
                    Is.EqualTo(string.Empty)
                    );

        Assert.That(() => MarshalUtilities.GetString(new ushort[] { 'A', 'B', 'C' }),
                    Is.EqualTo("ABC")
                    );
    }
    public static void GetUtf16SpanFromStringTest()
    {
        Assert.That(() => MarshalUtilities.GetUtf16Span(null).ToArray(),
                    Is.EqualTo(Array.Empty <ushort>())
                    );

        Assert.That(() => MarshalUtilities.GetUtf16Span(string.Empty).ToArray(),
                    Is.EqualTo(Array.Empty <ushort>())
                    );

        Assert.That(() => MarshalUtilities.GetUtf16Span("ABC").ToArray(),
                    Is.EqualTo(new ushort[] { 'A', 'B', 'C' })
                    );
    }
    public static void GetStringReadOnlySpanSByteTest()
    {
        Assert.That(() => MarshalUtilities.GetString(default(sbyte[])),
                    Is.Null
                    );

        Assert.That(() => MarshalUtilities.GetString(Array.Empty <sbyte>()),
                    Is.EqualTo(string.Empty)
                    );

        Assert.That(() => MarshalUtilities.GetString(new sbyte[] { (sbyte)'A', (sbyte)'B', (sbyte)'C' }),
                    Is.EqualTo("ABC")
                    );
    }
    public static unsafe void GetAsciiSpanFromSByteReferenceTest()
    {
        Assert.That(() => MarshalUtilities.GetAsciiSpan(UnsafeUtilities.NullRef <sbyte>(), -1).ToArray(),
                    Is.EqualTo(Array.Empty <sbyte>())
                    );

        var source = new sbyte[] { (sbyte)'A', (sbyte)'B', (sbyte)'C' };

        Assert.That(() => MarshalUtilities.GetAsciiSpan(in source[0], -1).ToArray(),
                    Is.EqualTo(new sbyte[] { (sbyte)'A', (sbyte)'B', (sbyte)'C' })
                    );

        Assert.That(() => MarshalUtilities.GetAsciiSpan(in source[0], 2).ToArray(),
                    Is.EqualTo(new sbyte[] { (sbyte)'A', (sbyte)'B' })
                    );
    }
    public static unsafe void GetUtf16SpanFromSByteReferenceTest()
    {
        Assert.That(() => MarshalUtilities.GetUtf16Span(UnsafeUtilities.NullRef <ushort>(), -1).ToArray(),
                    Is.EqualTo(Array.Empty <ushort>())
                    );

        var source = new ushort[] { 'A', 'B', 'C' };

        Assert.That(() => MarshalUtilities.GetUtf16Span(in source[0], -1).ToArray(),
                    Is.EqualTo(new ushort[] { 'A', 'B', 'C' })
                    );

        Assert.That(() => MarshalUtilities.GetUtf16Span(in source[0], 2).ToArray(),
                    Is.EqualTo(new ushort[] { 'A', 'B' })
                    );
    }
        /// <summary>
        /// Instantiates a spline from an individual OBJ file which contains a set of vertices representing the individual points of the spline.
        /// </summary>
        /// <param name="path">An OBJ file containing the points of the spline as vertices.</param>
        public static BobsledSpline *MakeSpline(string path)
        {
            // Store newly created Spline
            BobsledSpline spline = new BobsledSpline();

            // Parse individual spline file.
            ObjParser parser = new ObjParser(path);

            // Spline Information
            spline.Enabler          = 0;
            spline.Null             = 0;
            spline.NumberOfVertices = (ushort)parser.Vertices.Count;

            // Get vertices & calculate total length
            Vector[] vertices = parser.Vertices.ToArray();

            spline.TotalSplineLength = 0;
            for (int i = 0; i < vertices.Length - 1; i++)
            {
                spline.TotalSplineLength += SplineVertexExtensions.Distance(vertices[i], vertices[i + 1]);
            }

            // Write vertices to unmanaged memory
            int    structSize            = Marshal.SizeOf(vertices[0]) * vertices.Length;
            IntPtr splineVerticesPointer = Marshal.AllocHGlobal(structSize);

            MarshalUtilities.StructureArrayToPointer(vertices, splineVerticesPointer);
            spline.VertexList = (Vector *)splineVerticesPointer;

            // Write spline to unmanaged memory
            int    splineSize    = Marshal.SizeOf(spline);
            IntPtr splinePointer = Marshal.AllocHGlobal(splineSize);

            Marshal.StructureToPtr(spline, splinePointer, true);

            return((BobsledSpline *)splinePointer);
        }