<commit_before>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Buffers;

namespace System.SpanTests
{
    public static partial class MemoryMarshalTests
    {
        [Fact]
        public static void CreateFromPinnedArrayInt()
        {
            int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 };
            Memory<int> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 3, 2);
            pinnedMemory.Validate(93, 94);
        }

        [Fact]
        public static void CreateFromPinnedArrayLong()
        {
            long[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 };
            Memory<long> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 4, 3);
            pinnedMemory.Validate(94, 95, 96);
        }

        [Fact]
        public static void CreateFromPinnedArrayString()
        {
            string[] a = { "90", "91", "92", "93", "94", "95", "96", "97", "98" };
            Memory<string> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 4, 3);
            pinnedMemory.Validate("94", "95", "96");
        }
        
        [Fact]
        public static void CreateFromPinnedArrayIntSliceRemainsPinned()
        {
            int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 };
            Memory<int> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 3, 5);
            pinnedMemory.Validate(93, 94, 95, 96, 97);

            TestMemory<int> testPinnedMemory = Unsafe.As<Memory<int>, TestMemory<int>>(ref pinnedMemory);

            Memory<int> slice = pinnedMemory.Slice(0);
            TestMemory<int> testSlice = Unsafe.As<Memory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(testPinnedMemory._index, testSlice._index);
            Assert.Equal(testPinnedMemory._length, testSlice._length);

            slice = pinnedMemory.Slice(0, pinnedMemory.Length);
            testSlice = Unsafe.As<Memory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(testPinnedMemory._index, testSlice._index);
            Assert.Equal(testPinnedMemory._length, testSlice._length);

            int expectedLength = testPinnedMemory._length - 1;
            slice = pinnedMemory.Slice(1);
            testSlice = Unsafe.As<Memory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(testPinnedMemory._index + 1, testSlice._index);
            Assert.Equal(expectedLength, testSlice._length);

            expectedLength = 2 | (1 << 31);
            slice = pinnedMemory.Slice(1, 2);
            testSlice = Unsafe.As<Memory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(testPinnedMemory._index + 1, testSlice._index);
            Assert.Equal(expectedLength, testSlice._length);
        }
        
        [Fact]
        public static void CreateFromPinnedArrayIntReadOnlyMemorySliceRemainsPinned()
        {
            int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 };
            ReadOnlyMemory<int> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 3, 5);
            pinnedMemory.Validate(93, 94, 95, 96, 97);

            TestMemory<int> testPinnedMemory = Unsafe.As<ReadOnlyMemory<int>, TestMemory<int>>(ref pinnedMemory);

            ReadOnlyMemory<int> slice = pinnedMemory.Slice(0);
            TestMemory<int> testSlice = Unsafe.As<ReadOnlyMemory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(testPinnedMemory._index, testSlice._index);
            Assert.Equal(testPinnedMemory._length, testSlice._length);

            slice = pinnedMemory.Slice(0, pinnedMemory.Length);
            testSlice = Unsafe.As<ReadOnlyMemory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(testPinnedMemory._index, testSlice._index);
            Assert.Equal(testPinnedMemory._length, testSlice._length);

            int expectedLength = testPinnedMemory._length - 1;
            slice = pinnedMemory.Slice(1);
            testSlice = Unsafe.As<ReadOnlyMemory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(testPinnedMemory._index + 1, testSlice._index);
            Assert.Equal(expectedLength, testSlice._length);

            expectedLength = 2 | (1 << 31);
            slice = pinnedMemory.Slice(1, 2);
            testSlice = Unsafe.As<ReadOnlyMemory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(testPinnedMemory._index + 1, testSlice._index);
            Assert.Equal(expectedLength, testSlice._length);
        }

        [Fact]
        public static void CreateFromPinnedArrayWithStartAndLengthRangeExtendsToEndOfArray()
        {
            long[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 };
            Memory<long> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 4, 5);
            pinnedMemory.Validate(94, 95, 96, 97, 98);
        }

        [Fact]
        public static void CreateFromPinnedArrayZeroLength()
        {
            int[] empty = Array.Empty<int>();
            Memory<int> memory = MemoryMarshal.CreateFromPinnedArray(empty, 0, empty.Length);
            memory.Validate();
        }

        [Fact]
        public static void CreateFromPinnedArrayNullArray()
        {
            Memory<int> memory = MemoryMarshal.CreateFromPinnedArray((int[])null, 0, 0);
            memory.Validate();
            Assert.Equal(default, memory);
        }


        [Fact]
        public static void CreateFromPinnedArrayNullArrayNonZeroStartAndLength()
        {<commit_msg>Update unit tests to account for Memory<T> changes<commit_after>// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Buffers;

namespace System.SpanTests
{
    public static partial class MemoryMarshalTests
    {
        [Fact]
        public static void CreateFromPinnedArrayInt()
        {
            int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 };
            Memory<int> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 3, 2);
            pinnedMemory.Validate(93, 94);
        }

        [Fact]
        public static void CreateFromPinnedArrayLong()
        {
            long[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 };
            Memory<long> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 4, 3);
            pinnedMemory.Validate(94, 95, 96);
        }

        [Fact]
        public static void CreateFromPinnedArrayString()
        {
            string[] a = { "90", "91", "92", "93", "94", "95", "96", "97", "98" };
            Memory<string> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 4, 3);
            pinnedMemory.Validate("94", "95", "96");
        }
        
        [Fact]
        public static void CreateFromPinnedArrayIntSliceRemainsPinned()
        {
            int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 };
            Memory<int> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 3, 5);
            pinnedMemory.Validate(93, 94, 95, 96, 97);

            Memory<int> slice = pinnedMemory.Slice(0);
            TestMemory<int> testSlice = Unsafe.As<Memory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(3 | (1 << 31), testSlice._index);
            Assert.Equal(5, testSlice._length);

            slice = pinnedMemory.Slice(0, pinnedMemory.Length);
            testSlice = Unsafe.As<Memory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(3 | (1 << 31), testSlice._index);
            Assert.Equal(5, testSlice._length);

            slice = pinnedMemory.Slice(1);
            testSlice = Unsafe.As<Memory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(4 | (1 << 31), testSlice._index);
            Assert.Equal(4, testSlice._length);

            slice = pinnedMemory.Slice(1, 2);
            testSlice = Unsafe.As<Memory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(4 | (1 << 31), testSlice._index);
            Assert.Equal(2, testSlice._length);
        }
        
        [Fact]
        public static void CreateFromPinnedArrayIntReadOnlyMemorySliceRemainsPinned()
        {
            int[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 };
            ReadOnlyMemory<int> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 3, 5);
            pinnedMemory.Validate(93, 94, 95, 96, 97);

            ReadOnlyMemory<int> slice = pinnedMemory.Slice(0);
            TestMemory<int> testSlice = Unsafe.As<ReadOnlyMemory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(3 | (1 << 31), testSlice._index);
            Assert.Equal(5, testSlice._length);

            slice = pinnedMemory.Slice(0, pinnedMemory.Length);
            testSlice = Unsafe.As<ReadOnlyMemory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(3 | (1 << 31), testSlice._index);
            Assert.Equal(5, testSlice._length);

            slice = pinnedMemory.Slice(1);
            testSlice = Unsafe.As<ReadOnlyMemory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(4 | (1 << 31), testSlice._index);
            Assert.Equal(4, testSlice._length);

            slice = pinnedMemory.Slice(1, 2);
            testSlice = Unsafe.As<ReadOnlyMemory<int>, TestMemory<int>>(ref slice);
            Assert.Equal(4 | (1 << 31), testSlice._index);
            Assert.Equal(2, testSlice._length);
        }

        [Fact]
        public static void CreateFromPinnedArrayWithStartAndLengthRangeExtendsToEndOfArray()
        {
            long[] a = { 90, 91, 92, 93, 94, 95, 96, 97, 98 };
            Memory<long> pinnedMemory = MemoryMarshal.CreateFromPinnedArray(a, 4, 5);
            pinnedMemory.Validate(94, 95, 96, 97, 98);
        }

        [Fact]
        public static void CreateFromPinnedArrayZeroLength()
        {
            int[] empty = Array.Empty<int>();
            Memory<int> memory = MemoryMarshal.CreateFromPinnedArray(empty, 0, empty.Length);
            memory.Validate();
        }

        [Fact]
        public static void CreateFromPinnedArrayNullArray()
        {
            Memory<int> memory = MemoryMarshal.CreateFromPinnedArray((int[])null, 0, 0);
            memory.Validate();
            Assert.Equal(default, memory);
        }


        [Fact]
        public static void CreateFromPinnedArrayNullArrayNonZeroStartAndLength()
        {
{'fraction_non_alphanumeric': 0.11932629174992863, 'fraction_numerical': 0.03920449138833381, 'mean_word_length': 2.3881366860090263, 'pattern_counts': {'":': 0, '<': 106, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5394950', 'n_tokens_mistral': 3334, 'n_tokens_neox': 2958, 'n_words': 689}